~aleteoryx/muditaos

ref: d14e40d3781d4852630932e2a5051db6b7e075ca muditaos/module-audio/Audio/Endpoint.hpp -rw-r--r-- 2.3 KiB
d14e40d3 — Radoslaw Wicik [EGD-4831] Add license headers to c, h and sql files 5 years 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "Stream.hpp"

namespace audio
{
    class Endpoint
    {
      public:
        struct Capabilities
        {
            bool usesDMA             = false;
            std::size_t maxBlockSize = 0;
        };

        Endpoint() = default;
        Endpoint(const Capabilities &caps);

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

        [[nodiscard]] const Capabilities &getCapabilities() const noexcept;

      protected:
        Capabilities _caps;
        Stream *_stream = nullptr;
    };

    class Sink : public Endpoint
    {
      public:
        virtual void onDataSend()    = 0;
        virtual void enableOutput()  = 0;
        virtual void disableOutput() = 0;
    };

    class Source : public Endpoint
    {
      public:
        virtual void onDataReceive() = 0;
        virtual void enableInput()   = 0;
        virtual void disableInput()  = 0;
    };

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

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

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

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

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

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

        [[nodiscard]] Source *getSource() const noexcept;
        [[nodiscard]] Sink *getSink() const noexcept;
        [[nodiscard]] Stream *getStream() const noexcept;

        [[nodiscard]] bool isEnabled() const noexcept;

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