~aleteoryx/muditaos

muditaos/module-audio/Audio/Endpoint.hpp -rw-r--r-- 3.2 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#pragma once

#include "AbstractStream.hpp"
#include "AudioFormat.hpp"

#include <chrono>
#include <optional>
#include <vector>

#include <cstdint>

namespace audio
{
    class Endpoint
    {
      public:
        struct Traits
        {
            bool usesDMA = false;

            // optional constraints
            std::optional<std::size_t> blockSizeConstraint          = std::nullopt;
            std::optional<std::chrono::milliseconds> timeConstraint = std::nullopt;
        };

        Endpoint() = default;

        void connectStream(AbstractStream &stream);
        void disconnectStream();
        bool isConnected() const noexcept;

        [[nodiscard]] virtual auto getTraits() const -> Traits = 0;

        auto isFormatSupported(const AudioFormat &format) -> bool;
        virtual auto getSupportedFormats() -> std::vector<AudioFormat> = 0;

      protected:
        AbstractStream *_stream = nullptr;
    };

    class Sink : public Endpoint
    {
      public:
        virtual auto getSinkFormat() -> AudioFormat = 0;
        virtual void onDataSend()                   = 0;
        virtual void enableOutput()                 = 0;
        virtual void disableOutput()                = 0;
    };

    class Source : public Endpoint
    {
      public:
        virtual auto getSourceFormat() -> AudioFormat = 0;
        virtual void onDataReceive()                  = 0;
        virtual void enableInput()                    = 0;
        virtual void disableInput()                   = 0;
    };

    class IOProxy : public Source, public Sink
    {
      public:
        inline bool isSinkConnected() const noexcept
        {
            return Sink::isConnected();
        }

        inline bool isSourceConnected() const noexcept
        {
            return Source::isConnected();
        }

        inline void connectOutputStream(AbstractStream &stream)
        {
            Sink::connectStream(stream);
        }

        inline void connectInputStream(AbstractStream &stream)
        {
            Source::connectStream(stream);
        }

        inline auto isFormatSupportedBySink(const AudioFormat &format) -> bool
        {
            return Sink::isFormatSupported(format);
        }

        inline auto isFormatSupportedBySource(const AudioFormat &format) -> bool
        {
            return Source::isFormatSupported(format);
        }
    };

    class StreamConnection
    {
      public:
        StreamConnection() = default;
        StreamConnection(Source *source, Sink *sink, AbstractStream *stream);
        ~StreamConnection();

        void enable();
        void disable();
        void destroy();

        [[nodiscard]] Source *getSource() const noexcept;
        [[nodiscard]] Sink *getSink() const noexcept;
        [[nodiscard]] AbstractStream *getStream() const noexcept;
        [[nodiscard]] bool isEnabled() const noexcept;

      private:
        bool enabled            = false;
        Sink *_sink             = nullptr;
        Source *_source         = nullptr;
        AbstractStream *_stream = nullptr;
    };
}; // namespace audio