~aleteoryx/muditaos

ref: 7f89f4185cf2d4b220fc7627895c74c2c7b4f6db muditaos/module-audio/Audio/Operation/Operation.hpp -rw-r--r-- 4.3 KiB
7f89f418 — jimmorrisson [EGD-2408] Fetch sms at startup (#991) 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 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 <memory>
#include <optional>
#include <functional>

#include <Audio/AudioCommon.hpp>
#include <Audio/encoder/Encoder.hpp>
#include <Audio/Profiles/Profile.hpp>

#include <service-bluetooth/ServiceBluetoothCommon.hpp>

namespace audio
{
    class Operation
    {
      public:
        Operation(const bool &isInitialized               = false,
                  const audio::PlaybackType &playbackType = audio::PlaybackType::None)
            : isInitialized{isInitialized}, playbackType{playbackType}
        {}

        enum class State
        {
            Idle,
            Active,
            Paused
        };

        enum class Type
        {
            Idle,
            Playback,
            Recorder,
            Router
        };

        [[nodiscard]] static inline auto c_str(Type type) -> const char *
        {
            switch (type) {
            case Type::Idle:
                return "Idle";
            case Type::Playback:
                return "Playback";
            case Type::Recorder:
                return "Recorder";
            case Type::Router:
                return "Router ";
            }
            return "";
        }

        virtual ~Operation() = default;

        static std::unique_ptr<Operation> Create(
            Type t,
            const char *fileName                  = "",
            const audio::PlaybackType &operations = audio::PlaybackType::None,
            std::function<uint32_t(const std::string &path, const uint32_t &defaultValue)> dbCallback = nullptr);

        virtual audio::RetCode Start(audio::AsyncCallback callback, audio::Token token) = 0;
        virtual audio::RetCode Stop() = 0;
        virtual audio::RetCode Pause() = 0;
        virtual audio::RetCode Resume() = 0;
        virtual audio::RetCode SendEvent(std::shared_ptr<Event> evt) = 0;
        virtual audio::RetCode SetOutputVolume(float vol) = 0;
        virtual audio::RetCode SetInputGain(float gain) = 0;

        virtual Position GetPosition() = 0;
        virtual void SetBluetoothStreamData(std::shared_ptr<BluetoothStreamData> data);

        Volume GetOutputVolume() const
        {
            return (currentProfile != nullptr) ? currentProfile->GetOutputVolume() : Volume{};
        }

        Gain GetInputGain() const
        {
            return currentProfile->GetInputGain();
        }

        State GetState() const
        {
            return state;
        }

        const std::shared_ptr<Profile> GetProfile() const
        {
            return currentProfile;
        }

        audio::PlaybackType GetPlaybackType() const noexcept
        {
            return playbackType;
        }

        const audio::Token &GetToken() const noexcept
        {
            return operationToken;
        }

        Type GetOperationType() const noexcept
        {
            return opType;
        }

        std::string GetFilePath() const noexcept
        {
            return filePath;
        }

        audio::RetCode SwitchToPriorityProfile();

      protected:
        struct SupportedProfile
        {
            SupportedProfile(std::shared_ptr<Profile> profile, bool isAvailable)
                : profile(std::move(profile)), isAvailable(isAvailable)
            {}

            std::shared_ptr<Profile> profile;
            bool isAvailable;
        };

        std::shared_ptr<Profile> currentProfile;
        std::unique_ptr<bsp::AudioDevice> audioDevice;

        std::vector<SupportedProfile> supportedProfiles;
        void SetProfileAvailability(std::vector<Profile::Type> profiles, bool available);

        State state = State::Idle;
        audio::AsyncCallback eventCallback;

        AudioSinkState audioSinkState;

        audio::Token operationToken;
        Type opType = Type::Idle;
        std::string filePath;

        bool isInitialized               = false;
        audio::PlaybackType playbackType = audio::PlaybackType::None;

        virtual audio::RetCode SwitchProfile(const Profile::Type type) = 0;

        std::shared_ptr<Profile> GetProfile(const Profile::Type type);

        std::function<int32_t(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer)>
            audioCallback = nullptr;
    };

} // namespace audio