~aleteoryx/muditaos

ref: 5dbd726c3e9f070f03e833b1e1a9cf215f156784 muditaos/module-services/service-audio/AudioServiceAPI.cpp -rw-r--r-- 6.5 KiB
5dbd726c — Wojtek Cichon [EGD-6554] Fix emulator to simulator 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "service-audio/AudioServiceAPI.hpp"
#include "service-audio/ServiceAudio.hpp"
#include "service-audio/AudioMessage.hpp"

#include <Audio/decoder/Decoder.hpp>
#include <Service/Common.hpp>
#include <log/log.hpp>

#include <utility>

namespace sys
{
    class Service;
} // namespace sys

using namespace audio;
namespace AudioServiceAPI
{
    namespace
    {
        auto SendAudioRequest(sys::Service *serv, std::shared_ptr<AudioMessage> msg)
        {
            auto msgType = static_cast<int>(msg->type);
            LOG_DEBUG("Msg type %d", msgType);
            auto ret = serv->bus.sendUnicast(msg, service::name::audio, sys::BusProxy::defaultTimeout);
            if (ret.first == sys::ReturnCodes::Success) {
                if (auto resp = std::dynamic_pointer_cast<AudioResponseMessage>(ret.second)) {
                    LOG_DEBUG("Msg type %d done", msgType);
                    return resp;
                }
                LOG_ERROR("msgType %d - not AudioResponseMessage", msgType);
                return std::make_shared<AudioResponseMessage>(audio::RetCode::Failed);
            }
            LOG_ERROR("Command %d Failed with %d error", msgType, static_cast<int>(ret.first));
            return std::make_shared<AudioResponseMessage>(audio::RetCode::Failed);
        }
    } // namespace

    bool PlaybackStart(sys::Service *serv, const audio::PlaybackType &playbackType, const std::string &fileName)
    {
        auto msg = std::make_shared<AudioStartPlaybackRequest>(fileName, playbackType);
        return serv->bus.sendUnicast(msg, service::name::audio);
    }

    bool RecordingStart(sys::Service *serv, const std::string &fileName)
    {
        auto msg = std::make_shared<AudioStartRecorderRequest>(fileName);
        return serv->bus.sendUnicast(msg, service::name::audio);
    }

    bool RoutingStart(sys::Service *serv)
    {
        auto msg = std::make_shared<AudioStartRoutingRequest>();
        return serv->bus.sendUnicast(msg, service::name::audio);
    }

    bool Stop(sys::Service *serv, const std::vector<audio::PlaybackType> &stopVec)
    {
        if (stopVec.empty()) {
            return true;
        }
        auto msg = std::make_shared<AudioStopRequest>(stopVec);
        return serv->bus.sendUnicast(msg, service::name::audio);
    }

    bool Stop(sys::Service *serv, const audio::Token &token)
    {
        auto msg = std::make_shared<AudioStopRequest>(token);
        return serv->bus.sendUnicast(msg, service::name::audio);
    }

    bool StopAll(sys::Service *serv)
    {
        auto msg = std::make_shared<AudioStopRequest>();
        return serv->bus.sendUnicast(msg, service::name::audio);
    }

    bool Pause(sys::Service *serv, const audio::Token &token)
    {
        auto msg = std::make_shared<AudioPauseRequest>(token);
        return serv->bus.sendUnicast(msg, service::name::audio);
    }

    bool Resume(sys::Service *serv, const audio::Token &token)
    {
        auto msg = std::make_shared<AudioResumeRequest>(token);
        return serv->bus.sendUnicast(msg, service::name::audio);
    }

    bool SendEvent(sys::Service *serv, std::shared_ptr<audio::Event> evt)
    {
        auto msg = std::make_shared<AudioEventRequest>(std::move(evt));
        return serv->bus.sendUnicast(msg, service::name::audio);
    }

    bool SendEvent(sys::Service *serv, audio::EventType eType, audio::Event::DeviceState state)
    {
        auto msg = std::make_shared<AudioEventRequest>(eType, state);
        return serv->bus.sendUnicast(msg, service::name::audio);
    }

    template <typename T>
    audio::RetCode GetSetting(sys::Service *serv,
                              const audio::Setting &setting,
                              T &value,
                              const audio::PlaybackType &playbackType,
                              const audio::Profile::Type &profileType)
    {
        auto msg  = std::make_shared<AudioGetSetting>(profileType, playbackType, setting);
        auto resp = SendAudioRequest(serv, msg);
        if (resp->retCode == RetCode::Success) {
            value = resp->val;
        }

        return resp->retCode;
    }

    template audio::RetCode GetSetting<uint32_t>(sys::Service *serv,
                                                 const audio::Setting &setting,
                                                 uint32_t &value,
                                                 const audio::PlaybackType &playbackType,
                                                 const audio::Profile::Type &profileType);

    template audio::RetCode GetSetting<bool>(sys::Service *serv,
                                             const audio::Setting &setting,
                                             bool &value,
                                             const audio::PlaybackType &playbackType,
                                             const audio::Profile::Type &profileType);

    template <typename T>
    audio::RetCode SetSetting(sys::Service *serv,
                              const audio::Setting &setting,
                              const T value,
                              const audio::PlaybackType &playbackType,
                              const audio::Profile::Type &profileType)
    {
        auto msg = std::make_shared<AudioSetSetting>(profileType, playbackType, setting, std::to_string(value));

        return SendAudioRequest(serv, msg)->retCode;
    }

    template audio::RetCode SetSetting<uint32_t>(
        sys::Service *, const Setting &, const uint32_t, const PlaybackType &, const Profile::Type &);

    template audio::RetCode SetSetting<bool>(
        sys::Service *, const Setting &, const bool, const PlaybackType &, const Profile::Type &);

    std::optional<Tags> GetFileTags(sys::Service *serv, const std::string &fileName)
    {
        auto msg = std::make_shared<AudioGetFileTagsRequest>(fileName);

        auto resp = SendAudioRequest(serv, msg);
        if (resp->retCode == audio::RetCode::Success) {
            return resp->tags;
        }
        else {
            return std::nullopt;
        }
    }

    bool KeyPressed(sys::Service *serv, const int step)
    {
        auto msg = std::make_shared<AudioKeyPressedRequest>(step);
        return serv->bus.sendUnicast(msg, service::name::audio);
    }

    bool BluetoothVolumeChanged(sys::Service *serv, const uint8_t volume)
    {
        return serv->bus.sendUnicast(std::make_shared<BluetoothDeviceVolumeChanged>(volume), service::name::audio);
    }
} // namespace AudioServiceAPI