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 2023 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 : #include <cstring>
21 : #include <limits.h>
22 : #include <mutex>
23 : #include <stdexcept>
24 :
25 : #include "IWebAudioPlayerIpc.h"
26 : #include "IWebAudioPlayerIpcClient.h"
27 : #include "RialtoClientLogging.h"
28 : #include "WebAudioPlayer.h"
29 : #include "WebAudioPlayerProxy.h"
30 :
31 : namespace firebolt::rialto
32 : {
33 2 : std::shared_ptr<IWebAudioPlayerFactory> IWebAudioPlayerFactory::createFactory()
34 : {
35 2 : std::shared_ptr<IWebAudioPlayerFactory> factory;
36 :
37 : try
38 : {
39 2 : factory = std::make_shared<WebAudioPlayerFactory>();
40 : }
41 0 : catch (const std::exception &e)
42 : {
43 0 : RIALTO_CLIENT_LOG_ERROR("Failed to create the web audio player factory, reason: %s", e.what());
44 : }
45 :
46 2 : return factory;
47 : }
48 :
49 : std::unique_ptr<IWebAudioPlayer>
50 0 : WebAudioPlayerFactory::createWebAudioPlayer(std::weak_ptr<IWebAudioPlayerClient> client, const std::string &audioMimeType,
51 : const uint32_t priority, std::weak_ptr<const WebAudioConfig> config) const
52 : {
53 0 : return createWebAudioPlayer(client, audioMimeType, priority, config, {}, {});
54 : }
55 :
56 : std::unique_ptr<IWebAudioPlayer>
57 2 : WebAudioPlayerFactory::createWebAudioPlayer(std::weak_ptr<IWebAudioPlayerClient> client, const std::string &audioMimeType,
58 : const uint32_t priority, std::weak_ptr<const WebAudioConfig> config,
59 : std::weak_ptr<client::IWebAudioPlayerIpcFactory> webAudioPlayerIpcFactory,
60 : std::weak_ptr<client::IClientController> clientController) const
61 : {
62 2 : std::unique_ptr<IWebAudioPlayer> webAudioPlayer;
63 : try
64 : {
65 : std::shared_ptr<client::IWebAudioPlayerIpcFactory> webAudioPlayerIpcFactoryLocked =
66 2 : webAudioPlayerIpcFactory.lock();
67 2 : std::shared_ptr<client::IClientController> clientControllerLocked = clientController.lock();
68 2 : client::IClientController &cc = clientControllerLocked
69 2 : ? *clientControllerLocked
70 0 : : client::IClientControllerAccessor::instance().getClientController();
71 :
72 : auto wap{std::make_shared<client::WebAudioPlayer>(client, audioMimeType, priority, config,
73 2 : webAudioPlayerIpcFactoryLocked
74 5 : ? webAudioPlayerIpcFactoryLocked
75 : : client::IWebAudioPlayerIpcFactory::getFactory(),
76 2 : cc)};
77 1 : webAudioPlayer = std::make_unique<WebAudioPlayerProxy>(wap, cc);
78 3 : }
79 1 : catch (const std::exception &e)
80 : {
81 1 : RIALTO_CLIENT_LOG_ERROR("Failed to create the web audio player, reason: %s", e.what());
82 : }
83 :
84 2 : return webAudioPlayer;
85 : }
86 :
87 4 : WebAudioPlayerProxy::WebAudioPlayerProxy(const std::shared_ptr<IWebAudioPlayerAndIControlClient> &ptr,
88 4 : client::IClientController &clientController)
89 4 : : m_webAudioPlayer(ptr), m_clientController{clientController}
90 : {
91 4 : ApplicationState state{ApplicationState::UNKNOWN};
92 4 : if (!m_clientController.registerClient(m_webAudioPlayer, state))
93 : {
94 1 : throw std::runtime_error("Failed to register client with clientController");
95 : }
96 3 : m_webAudioPlayer->notifyApplicationState(state);
97 5 : }
98 :
99 4 : WebAudioPlayerProxy::~WebAudioPlayerProxy()
100 : {
101 3 : if (!m_clientController.unregisterClient(m_webAudioPlayer))
102 : {
103 0 : RIALTO_CLIENT_LOG_WARN("Failed to unregister client with clientController");
104 : }
105 4 : }
106 :
107 : }; // namespace firebolt::rialto
108 :
109 : namespace firebolt::rialto::client
110 : {
111 32 : WebAudioPlayer::WebAudioPlayer(std::weak_ptr<IWebAudioPlayerClient> client, const std::string &audioMimeType,
112 : const uint32_t priority, std::weak_ptr<const WebAudioConfig> webAudioConfig,
113 : const std::shared_ptr<IWebAudioPlayerIpcFactory> &webAudioPlayerIpcFactory,
114 32 : IClientController &clientController)
115 96 : : m_webAudioPlayerClient(client), m_clientController{clientController}, m_bytesPerFrame{0},
116 32 : m_currentAppState{ApplicationState::UNKNOWN}
117 : {
118 32 : RIALTO_CLIENT_LOG_DEBUG("entry:");
119 :
120 32 : if (audioMimeType == "audio/x-raw")
121 : {
122 32 : std::shared_ptr<const WebAudioConfig> kConfig = webAudioConfig.lock();
123 32 : if (!kConfig)
124 : {
125 0 : throw std::runtime_error("Config is null for 'audio/x-raw'");
126 : }
127 32 : m_bytesPerFrame = kConfig->pcm.channels * (kConfig->pcm.sampleSize / CHAR_BIT);
128 32 : if (m_bytesPerFrame == 0)
129 : {
130 0 : throw std::runtime_error("Bytes per frame cannot be 0, channels " + std::to_string(kConfig->pcm.channels) +
131 0 : ", sampleSize " + std::to_string(kConfig->pcm.sampleSize));
132 : }
133 32 : }
134 :
135 32 : if (!webAudioPlayerIpcFactory)
136 : {
137 0 : throw std::runtime_error("Web audio player ipc factory could not be null");
138 : }
139 :
140 : m_webAudioPlayerIpc =
141 32 : webAudioPlayerIpcFactory->createWebAudioPlayerIpc(this, audioMimeType, priority, webAudioConfig);
142 32 : if (!m_webAudioPlayerIpc)
143 : {
144 3 : throw std::runtime_error("Web audio player ipc could not be created");
145 : }
146 :
147 29 : m_shmHandleObserverId = m_clientController.subscribeSharedMemoryHandle(
148 29 : [this](const std::shared_ptr<ISharedMemoryHandle> &handle)
149 : {
150 5 : std::lock_guard<std::mutex> lock{m_shmHandleMutex};
151 5 : m_shmHandle = handle;
152 5 : });
153 47 : }
154 :
155 56 : WebAudioPlayer::~WebAudioPlayer()
156 : {
157 29 : RIALTO_CLIENT_LOG_DEBUG("entry:");
158 :
159 29 : m_clientController.unsubscribeSharedMemoryHandle(m_shmHandleObserverId);
160 29 : m_webAudioPlayerIpc.reset();
161 56 : }
162 :
163 2 : bool WebAudioPlayer::play()
164 : {
165 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
166 :
167 2 : return m_webAudioPlayerIpc->play();
168 : }
169 :
170 2 : bool WebAudioPlayer::pause()
171 : {
172 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
173 :
174 2 : return m_webAudioPlayerIpc->pause();
175 : }
176 :
177 2 : bool WebAudioPlayer::setEos()
178 : {
179 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
180 :
181 2 : return m_webAudioPlayerIpc->setEos();
182 : }
183 :
184 9 : bool WebAudioPlayer::getBufferAvailable(uint32_t &availableFrames, std::shared_ptr<WebAudioShmInfo> &)
185 : {
186 9 : RIALTO_CLIENT_LOG_DEBUG("entry:");
187 9 : std::lock_guard<std::mutex> bufLocker(m_bufLock);
188 9 : if (!m_webAudioShmInfo)
189 : {
190 9 : m_webAudioShmInfo = std::make_shared<WebAudioShmInfo>();
191 : }
192 18 : return m_webAudioPlayerIpc->getBufferAvailable(availableFrames, m_webAudioShmInfo);
193 9 : }
194 :
195 2 : bool WebAudioPlayer::getBufferDelay(uint32_t &delayFrames)
196 : {
197 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
198 :
199 2 : return m_webAudioPlayerIpc->getBufferDelay(delayFrames);
200 : }
201 :
202 8 : bool WebAudioPlayer::writeBuffer(const uint32_t numberOfFrames, void *data)
203 : {
204 8 : RIALTO_CLIENT_LOG_DEBUG("entry:");
205 :
206 8 : std::lock_guard<std::mutex> bufLocker(m_bufLock);
207 8 : if (!m_webAudioShmInfo)
208 : {
209 1 : RIALTO_CLIENT_LOG_ERROR("Web audio shared info is null!");
210 1 : return false;
211 : }
212 :
213 7 : uint32_t dataLength = numberOfFrames * m_bytesPerFrame;
214 7 : uint32_t availableDataLength = m_webAudioShmInfo->lengthMain + m_webAudioShmInfo->lengthWrap;
215 7 : if (dataLength > availableDataLength)
216 : {
217 1 : RIALTO_CLIENT_LOG_ERROR("The number of frames to write exceeds the available space!");
218 1 : return false;
219 : }
220 :
221 6 : if (ApplicationState::RUNNING != m_currentAppState)
222 : {
223 1 : RIALTO_CLIENT_LOG_ERROR("Current ApplicationState is not RUNNING!");
224 1 : return false;
225 : }
226 :
227 5 : std::shared_ptr<ISharedMemoryHandle> shmHandle;
228 : {
229 5 : std::lock_guard<std::mutex> shmLock{m_shmHandleMutex};
230 5 : shmHandle = m_shmHandle;
231 : }
232 5 : if (nullptr == shmHandle || nullptr == shmHandle->getShm())
233 : {
234 2 : RIALTO_CLIENT_LOG_ERROR("Shared buffer no longer valid");
235 2 : return false;
236 : }
237 :
238 3 : if (dataLength > m_webAudioShmInfo->lengthMain)
239 : {
240 1 : std::memcpy(shmHandle->getShm() + m_webAudioShmInfo->offsetMain, data, m_webAudioShmInfo->lengthMain);
241 1 : std::memcpy(shmHandle->getShm() + m_webAudioShmInfo->offsetWrap,
242 1 : reinterpret_cast<uint8_t *>(data) + m_webAudioShmInfo->lengthMain,
243 1 : dataLength - m_webAudioShmInfo->lengthMain);
244 : }
245 : else
246 : {
247 2 : std::memcpy(shmHandle->getShm() + m_webAudioShmInfo->offsetMain, data, dataLength);
248 : }
249 3 : return m_webAudioPlayerIpc->writeBuffer(numberOfFrames);
250 8 : }
251 :
252 2 : bool WebAudioPlayer::getDeviceInfo(uint32_t &preferredFrames, uint32_t &maximumFrames, bool &supportDeferredPlay)
253 : {
254 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
255 :
256 2 : return m_webAudioPlayerIpc->getDeviceInfo(preferredFrames, maximumFrames, supportDeferredPlay);
257 : }
258 :
259 2 : bool WebAudioPlayer::setVolume(double volume)
260 : {
261 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
262 :
263 2 : return m_webAudioPlayerIpc->setVolume(volume);
264 : }
265 :
266 2 : bool WebAudioPlayer::getVolume(double &volume)
267 : {
268 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
269 :
270 2 : return m_webAudioPlayerIpc->getVolume(volume);
271 : }
272 :
273 0 : std::weak_ptr<IWebAudioPlayerClient> WebAudioPlayer::getClient()
274 : {
275 0 : return m_webAudioPlayerClient;
276 : }
277 :
278 1 : void WebAudioPlayer::notifyState(WebAudioPlayerState state)
279 : {
280 1 : std::shared_ptr<IWebAudioPlayerClient> client = m_webAudioPlayerClient.lock();
281 1 : if (client)
282 : {
283 1 : client->notifyState(state);
284 : }
285 : }
286 :
287 28 : void WebAudioPlayer::notifyApplicationState(ApplicationState state)
288 : {
289 28 : m_currentAppState = state;
290 : }
291 :
292 : }; // namespace firebolt::rialto::client
|