Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2022 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #ifndef FIREBOLT_RIALTO_SERVER_WEB_AUDIO_PLAYER_SERVER_INTERNAL_H_
21 : #define FIREBOLT_RIALTO_SERVER_WEB_AUDIO_PLAYER_SERVER_INTERNAL_H_
22 :
23 : #include "IGstWebAudioPlayer.h"
24 : #include "IMainThread.h"
25 : #include "ITimer.h"
26 : #include "IWebAudioPlayer.h"
27 : #include "IWebAudioPlayerServerInternal.h"
28 :
29 : #include <memory>
30 : #include <stdint.h>
31 : #include <string>
32 :
33 : namespace firebolt::rialto::server
34 : {
35 : /**
36 : * @brief IWebAudioPlayer factory class definition.
37 : */
38 : class WebAudioPlayerServerInternalFactory : public IWebAudioPlayerServerInternalFactory
39 : {
40 : public:
41 2 : WebAudioPlayerServerInternalFactory() = default;
42 2 : ~WebAudioPlayerServerInternalFactory() override = default;
43 :
44 : std::unique_ptr<IWebAudioPlayer> createWebAudioPlayer(std::weak_ptr<IWebAudioPlayerClient> client,
45 : const std::string &audioMimeType, const uint32_t priority,
46 : std::weak_ptr<const WebAudioConfig> config) const override;
47 :
48 : std::unique_ptr<IWebAudioPlayerServerInternal> createWebAudioPlayerServerInternal(
49 : std::weak_ptr<IWebAudioPlayerClient> client, const std::string &audioMimeType, const uint32_t priority,
50 : std::weak_ptr<const WebAudioConfig> config, const std::shared_ptr<ISharedMemoryBuffer> &shmBuffer, int handle,
51 : const std::shared_ptr<firebolt::rialto::server::IMainThreadFactory> &mainThreadFactory,
52 : const std::shared_ptr<firebolt::rialto::server::IGstWebAudioPlayerFactory> &gstPlayerFactory,
53 : std::weak_ptr<firebolt::rialto::common::ITimerFactory> timerFactory) const override;
54 : };
55 :
56 : /**
57 : * @brief The definition of the WebAudioPlayerServerInternal.
58 : */
59 : class WebAudioPlayerServerInternal : public IWebAudioPlayerServerInternal, public IGstWebAudioPlayerClient
60 : {
61 : public:
62 : /**
63 : * @brief The constructor.
64 : *
65 : * @param[in] client : The Web Audio Player client.
66 : * @param[in] audioMimeType : The audio encoding format, currently only "audio/x-raw" (PCM).
67 : * @param[in] priority : Priority value for this pipeline.
68 : * @param[in] config : Additional type dependent configuration data or nullptr.
69 : * @param[in] shmBuffer : The shared memory buffer.
70 : * @param[in] handle : The handle for this WebAudioPlayer.
71 : * @param[in] mainThreadFactory : The main thread factory.
72 : * @param[in] gstPlayerFactory : The gstreamer player factory.
73 : * @param[in] timerFactory : The timer factory.
74 : */
75 : WebAudioPlayerServerInternal(std::weak_ptr<IWebAudioPlayerClient> client, const std::string &audioMimeType,
76 : const uint32_t priority, std::weak_ptr<const WebAudioConfig> config,
77 : const std::shared_ptr<ISharedMemoryBuffer> &shmBuffer, int handle,
78 : const std::shared_ptr<IMainThreadFactory> &mainThreadFactory,
79 : const std::shared_ptr<IGstWebAudioPlayerFactory> &gstPlayerFactory,
80 : std::weak_ptr<common::ITimerFactory> timerFactory);
81 :
82 : /**
83 : * @brief Virtual destructor.
84 : */
85 : virtual ~WebAudioPlayerServerInternal();
86 :
87 : bool play() override;
88 :
89 : bool pause() override;
90 :
91 : bool setEos() override;
92 :
93 : bool getBufferAvailable(uint32_t &availableFrames, std::shared_ptr<WebAudioShmInfo> &webAudioShmInfo) override;
94 :
95 : bool getBufferDelay(uint32_t &delayFrames) override;
96 :
97 : bool writeBuffer(const uint32_t numberOfFrames, void *data) override;
98 :
99 : bool getDeviceInfo(uint32_t &preferredFrames, uint32_t &maximumFrames, bool &supportDeferredPlay) override;
100 :
101 : bool setVolume(double volume) override;
102 :
103 : bool getVolume(double &volume) override;
104 :
105 : std::weak_ptr<IWebAudioPlayerClient> getClient() override;
106 :
107 : void notifyState(WebAudioPlayerState state) override;
108 :
109 : void ping(std::unique_ptr<IHeartbeatHandler> &&heartbeatHandler) override;
110 :
111 : protected:
112 : /**
113 : * @brief The web audio player client.
114 : */
115 : std::shared_ptr<IWebAudioPlayerClient> m_webAudioPlayerClient;
116 :
117 : /**
118 : * @brief Shared memory buffer.
119 : */
120 : std::shared_ptr<ISharedMemoryBuffer> m_shmBuffer;
121 :
122 : /**
123 : * @brief The mainThread object.
124 : */
125 : std::shared_ptr<IMainThread> m_mainThread;
126 :
127 : /**
128 : * @brief This objects id registered on the main thread.
129 : */
130 : uint32_t m_mainThreadClientId;
131 :
132 : /**
133 : * @brief This priority of the WebAudioPlayer object.
134 : */
135 : const uint32_t m_priority;
136 :
137 : /**
138 : * @brief The gstreamer player.
139 : */
140 : std::unique_ptr<IGstWebAudioPlayer> m_gstPlayer;
141 :
142 : /**
143 : * @brief The id of the shared memory partition.
144 : */
145 : const int m_shmId;
146 :
147 : /**
148 : * @brief Pointer to the start of the shared buffer.
149 : */
150 : uint8_t *m_shmPtr;
151 :
152 : /**
153 : * @brief Offset of the web audio partition relative to the start of the shared memory.
154 : */
155 : uint32_t m_partitionOffset;
156 :
157 : /**
158 : * @brief Length of the shared buffer partition.
159 : */
160 : uint32_t m_maxDataLength;
161 :
162 : /**
163 : * @brief The details of the free space in the shared buffer partition.
164 : */
165 : WebAudioShmInfo m_availableBuffer;
166 :
167 : /**
168 : * @brief True if a writeBuffer call is expected.
169 : */
170 : bool m_expectWriteBuffer;
171 :
172 : /**
173 : * @brief Factory for creating timers.
174 : */
175 : std::shared_ptr<common::ITimerFactory> m_timerFactory;
176 :
177 : /**
178 : * @brief Timer set to write data to gstreamer.
179 : */
180 : std::unique_ptr<firebolt::rialto::common::ITimer> m_writeDataTimer;
181 :
182 : /**
183 : * @brief The bytes per frame for this audio playback.
184 : */
185 : uint32_t m_bytesPerFrame;
186 :
187 : /**
188 : * @brief Whether EOS has been requested at the end of the buffer.
189 : */
190 : bool m_isEosRequested;
191 :
192 : /**
193 : * @brief Initalises the WebAudioPlayer.
194 : *
195 : * @param[in] audioMimeType : The audio encoding format.
196 : * @param[in] config : Additional type dependent configuration data or nullptr.
197 : * @param[in] gstPlayerFactory : The gstreamer player factory.
198 : *
199 : * @retval true on success.
200 : */
201 : bool initWebAudioPlayerInternal(const std::string &audioMimeType, std::weak_ptr<const WebAudioConfig> config,
202 : const std::shared_ptr<IGstWebAudioPlayerFactory> &gstPlayerFactory);
203 :
204 : /**
205 : * @brief Initalises the GstWebAudioPlayer.
206 : *
207 : * @param[in] audioMimeType : The audio encoding format.
208 : * @param[in] config : Additional type dependent configuration data or nullptr.
209 : * @param[in] gstPlayerFactory : The gstreamer player factory.
210 : *
211 : * @retval true on success.
212 : */
213 : bool initGstWebAudioPlayer(const std::string &audioMimeType, std::weak_ptr<const WebAudioConfig> config,
214 : const std::shared_ptr<IGstWebAudioPlayerFactory> &gstPlayerFactory);
215 :
216 : /**
217 : * @brief Write audio frames internally, only to be called on the main thread.
218 : *
219 : * @param[in] numberOfFrames : Number of frames of audio in the shared memory.
220 : *
221 : * @retval true on success.
222 : */
223 : bool writeBufferInternal(const uint32_t numberOfFrames);
224 :
225 : /**
226 : * @brief Write the data that has been stored in the shared memory to gstreamer.
227 : *
228 : * Uses the available buffer variable to calculate the data to pass to gstreamer for writting.
229 : *
230 : * @retval true if all stored data was written to gstreamer.
231 : */
232 : bool writeStoredBuffers();
233 :
234 : /**
235 : * @brief Update the available buffer variable with the new bytes written to the shared memory and gstreamer.
236 : *
237 : * @param[in] bytesWrittenToShm : Number of bytes newly written to the shared memory.
238 : * @param[in] bytesWrittenToGst : Number of bytes newly written to gstreamer.
239 : */
240 : void updateAvailableBuffer(uint32_t bytesWrittenToShm, uint32_t bytesWrittenToGst);
241 :
242 : /**
243 : * @brief Handles the timeout of the write data timer.
244 : */
245 : void handleWriteDataTimer();
246 :
247 : /**
248 : * @brief Gets the number of queued frames in the shared memory.
249 : *
250 : * @retval The number of frames queued.
251 : */
252 : uint32_t getQueuedFramesInShm();
253 : };
254 :
255 : }; // namespace firebolt::rialto::server
256 :
257 : #endif // FIREBOLT_RIALTO_SERVER_WEB_AUDIO_PLAYER_SERVER_INTERNAL_H_
|