~aleteoryx/muditaos

ref: f1fc9df1527237c0449585bcaa620851583a271e muditaos/module-audio/Audio/Operation/Operation.hpp -rw-r--r-- 4.4 KiB
f1fc9df1 — Marcin Smoczyński [EGD-4977] Reduce audio lag during voice call 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
// Copyright (c) 2017-2021, 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(AudioServiceMessage::Callback callback, const PlaybackType &playbackType = PlaybackType::None)
            : playbackType(playbackType), serviceCallback(callback)
        {}

        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,
                                                 AudioServiceMessage::Callback callback = nullptr);

        virtual audio::RetCode Start(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;

        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;

        State state = State::Idle;
        audio::Token operationToken;
        Type opType = Type::Idle;
        std::string filePath;
        audio::PlaybackType playbackType = audio::PlaybackType::None;

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

        void SetProfileAvailability(std::vector<Profile::Type> profiles, bool available);
        void AddProfile(const Profile::Type &profile, const PlaybackType &playback, bool isAvailable);

        virtual audio::RetCode SwitchProfile(const Profile::Type type) = 0;
        std::shared_ptr<Profile> GetProfile(const Profile::Type type);

        std::optional<std::unique_ptr<bsp::AudioDevice>> CreateDevice(bsp::AudioDevice::Type type);
    };

} // namespace audio