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 : #include <inttypes.h>
21 : #include <stdexcept>
22 : #include <stdint.h>
23 :
24 : #include "KeyIdMap.h"
25 : #include "MediaPipeline.h"
26 : #include "MediaPipelineProxy.h"
27 : #include "RialtoClientLogging.h"
28 :
29 : namespace
30 : {
31 155 : const char *toString(const firebolt::rialto::client::MediaPipeline::State &state)
32 : {
33 155 : switch (state)
34 : {
35 49 : case firebolt::rialto::client::MediaPipeline::State::IDLE:
36 49 : return "IDLE";
37 8 : case firebolt::rialto::client::MediaPipeline::State::BUFFERING:
38 8 : return "BUFFERING";
39 72 : case firebolt::rialto::client::MediaPipeline::State::PLAYING:
40 72 : return "PLAYING";
41 8 : case firebolt::rialto::client::MediaPipeline::State::SEEKING:
42 8 : return "SEEKING";
43 8 : case firebolt::rialto::client::MediaPipeline::State::FAILURE:
44 8 : return "FAILURE";
45 10 : case firebolt::rialto::client::MediaPipeline::State::END_OF_STREAM:
46 10 : return "END_OF_STREAM";
47 : }
48 0 : return "UNKNOWN";
49 : }
50 :
51 68 : const char *toString(const firebolt::rialto::PlaybackState &state)
52 : {
53 68 : switch (state)
54 : {
55 1 : case firebolt::rialto::PlaybackState::IDLE:
56 1 : return "IDLE";
57 50 : case firebolt::rialto::PlaybackState::PLAYING:
58 50 : return "PLAYING";
59 0 : case firebolt::rialto::PlaybackState::PAUSED:
60 0 : return "PAUSED";
61 4 : case firebolt::rialto::PlaybackState::SEEKING:
62 4 : return "SEEKING";
63 3 : case firebolt::rialto::PlaybackState::SEEK_DONE:
64 3 : return "SEEK_DONE";
65 3 : case firebolt::rialto::PlaybackState::STOPPED:
66 3 : return "STOPPED";
67 4 : case firebolt::rialto::PlaybackState::END_OF_STREAM:
68 4 : return "END_OF_STREAM";
69 3 : case firebolt::rialto::PlaybackState::FAILURE:
70 3 : return "FAILURE";
71 0 : case firebolt::rialto::PlaybackState::UNKNOWN:
72 0 : return "UNKNOWN";
73 : }
74 0 : return "UNKNOWN";
75 : }
76 :
77 4 : const char *toString(const firebolt::rialto::NetworkState &state)
78 : {
79 4 : switch (state)
80 : {
81 1 : case firebolt::rialto::NetworkState::IDLE:
82 1 : return "IDLE";
83 3 : case firebolt::rialto::NetworkState::BUFFERING:
84 3 : return "BUFFERING";
85 0 : case firebolt::rialto::NetworkState::BUFFERING_PROGRESS:
86 0 : return "BUFFERING_PROGRESS";
87 0 : case firebolt::rialto::NetworkState::BUFFERED:
88 0 : return "BUFFERED";
89 0 : case firebolt::rialto::NetworkState::STALLED:
90 0 : return "STALLED";
91 0 : case firebolt::rialto::NetworkState::FORMAT_ERROR:
92 0 : return "FORMAT_ERROR";
93 0 : case firebolt::rialto::NetworkState::NETWORK_ERROR:
94 0 : return "NETWORK_ERROR";
95 0 : case firebolt::rialto::NetworkState::DECODE_ERROR:
96 0 : return "DECODE_ERROR";
97 0 : case firebolt::rialto::NetworkState::UNKNOWN:
98 0 : return "UNKNOWN";
99 : }
100 0 : return "UNKNOWN";
101 : }
102 : } // namespace
103 :
104 : namespace firebolt::rialto
105 : {
106 3 : std::shared_ptr<IMediaPipelineFactory> IMediaPipelineFactory::createFactory()
107 : {
108 3 : std::shared_ptr<IMediaPipelineFactory> factory;
109 :
110 : try
111 : {
112 3 : factory = std::make_shared<MediaPipelineFactory>();
113 : }
114 0 : catch (const std::exception &e)
115 : {
116 0 : RIALTO_CLIENT_LOG_ERROR("Failed to create the media player factory, reason: %s", e.what());
117 : }
118 :
119 3 : return factory;
120 : }
121 :
122 0 : std::unique_ptr<IMediaPipeline> MediaPipelineFactory::createMediaPipeline(std::weak_ptr<IMediaPipelineClient> client,
123 : const VideoRequirements &videoRequirements) const
124 : {
125 0 : return createMediaPipeline(client, videoRequirements, {}, {});
126 : }
127 :
128 : std::unique_ptr<IMediaPipeline>
129 3 : MediaPipelineFactory::createMediaPipeline(std::weak_ptr<IMediaPipelineClient> client,
130 : const VideoRequirements &videoRequirements,
131 : std::weak_ptr<client::IMediaPipelineIpcFactory> mediaPipelineIpcFactory,
132 : std::weak_ptr<client::IClientController> clientController) const
133 : {
134 3 : std::unique_ptr<IMediaPipeline> mediaPipeline;
135 : try
136 : {
137 3 : std::shared_ptr<client::IMediaPipelineIpcFactory> mediaPipelineIpcFactoryLocked = mediaPipelineIpcFactory.lock();
138 3 : std::shared_ptr<client::IClientController> clientControllerLocked = clientController.lock();
139 : firebolt::rialto::client::IClientController &cc =
140 3 : clientControllerLocked ? *clientControllerLocked
141 0 : : client::IClientControllerAccessor::instance().getClientController();
142 :
143 : auto mp{std::make_shared<client::MediaPipeline>(client, videoRequirements,
144 3 : mediaPipelineIpcFactoryLocked
145 7 : ? mediaPipelineIpcFactoryLocked
146 : : client::IMediaPipelineIpcFactory::getFactory(),
147 10 : common::IMediaFrameWriterFactory::getFactory(), cc)};
148 2 : mediaPipeline = std::move(std::make_unique<client::MediaPipelineProxy>(mp, cc));
149 6 : }
150 2 : catch (const std::exception &e)
151 : {
152 2 : RIALTO_CLIENT_LOG_ERROR("Failed to create the media player, reason: %s", e.what());
153 : }
154 :
155 3 : return mediaPipeline;
156 : }
157 :
158 : }; // namespace firebolt::rialto
159 :
160 : namespace firebolt::rialto::client
161 : {
162 5 : MediaPipelineProxy::MediaPipelineProxy(const std::shared_ptr<IMediaPipelineAndIControlClient> &mediaPipeline,
163 5 : IClientController &clientController)
164 5 : : m_mediaPipeline{mediaPipeline}, m_clientController{clientController}
165 : {
166 5 : ApplicationState state{ApplicationState::UNKNOWN};
167 5 : if (!m_clientController.registerClient(m_mediaPipeline, state))
168 : {
169 2 : throw std::runtime_error("Failed to register client with clientController");
170 : }
171 3 : m_mediaPipeline->notifyApplicationState(state);
172 7 : }
173 :
174 5 : MediaPipelineProxy::~MediaPipelineProxy()
175 : {
176 3 : if (!m_clientController.unregisterClient(m_mediaPipeline))
177 : {
178 0 : RIALTO_CLIENT_LOG_WARN("Failed to unregister client with clientController");
179 : }
180 5 : }
181 :
182 132 : MediaPipeline::MediaPipeline(std::weak_ptr<IMediaPipelineClient> client, const VideoRequirements &videoRequirements,
183 : const std::shared_ptr<IMediaPipelineIpcFactory> &mediaPipelineIpcFactory,
184 : const std::shared_ptr<common::IMediaFrameWriterFactory> &mediaFrameWriterFactory,
185 132 : IClientController &clientController)
186 396 : : m_mediaPipelineClient(client), m_clientController{clientController}, m_currentAppState{ApplicationState::UNKNOWN},
187 132 : m_mediaFrameWriterFactory(mediaFrameWriterFactory), m_currentState(State::IDLE), m_attachingSource(false)
188 : {
189 132 : RIALTO_CLIENT_LOG_DEBUG("entry:");
190 :
191 132 : m_mediaPipelineIpc = mediaPipelineIpcFactory->createMediaPipelineIpc(this, videoRequirements);
192 :
193 132 : if (!m_mediaPipelineIpc)
194 : {
195 2 : throw std::runtime_error("Media player ipc could not be created");
196 : }
197 :
198 130 : m_shmHandleObserverId = m_clientController.subscribeSharedMemoryHandle(
199 130 : [this](const std::shared_ptr<ISharedMemoryHandle> &handle)
200 : {
201 19 : std::lock_guard<std::mutex> lock{m_shmMutex};
202 19 : m_shmHandle = handle;
203 19 : });
204 148 : }
205 :
206 258 : MediaPipeline::~MediaPipeline()
207 : {
208 130 : RIALTO_CLIENT_LOG_DEBUG("entry:");
209 :
210 130 : m_clientController.unsubscribeSharedMemoryHandle(m_shmHandleObserverId);
211 130 : m_mediaPipelineIpc.reset();
212 258 : }
213 :
214 2 : bool MediaPipeline::load(MediaType type, const std::string &mimeType, const std::string &url, bool isLive)
215 : {
216 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
217 :
218 2 : return m_mediaPipelineIpc->load(type, mimeType, url, isLive);
219 : }
220 :
221 39 : bool MediaPipeline::attachSource(const std::unique_ptr<IMediaPipeline::MediaSource> &source)
222 : {
223 39 : RIALTO_CLIENT_LOG_DEBUG("entry:");
224 :
225 : // We should not process needDatas while attach source is ongoing
226 : {
227 39 : std::unique_lock<std::mutex> lock{m_attachSourceMutex};
228 39 : m_attachingSource = true;
229 : }
230 :
231 39 : int32_t sourceId = -1;
232 :
233 39 : bool status = m_mediaPipelineIpc->attachSource(source, sourceId);
234 39 : if (status)
235 : {
236 38 : source->setId(sourceId);
237 38 : m_attachedSources.add(sourceId, source->getType());
238 : }
239 :
240 : // Unblock needDatas
241 : {
242 39 : std::unique_lock<std::mutex> lock{m_attachSourceMutex};
243 39 : m_attachingSource = false;
244 39 : m_attachSourceCond.notify_all();
245 : }
246 39 : return status;
247 : }
248 :
249 5 : bool MediaPipeline::removeSource(int32_t id)
250 : {
251 5 : RIALTO_CLIENT_LOG_DEBUG("entry:");
252 5 : m_attachedSources.remove(id);
253 5 : return m_mediaPipelineIpc->removeSource(id);
254 : }
255 :
256 2 : bool MediaPipeline::allSourcesAttached()
257 : {
258 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
259 :
260 2 : return m_mediaPipelineIpc->allSourcesAttached();
261 : }
262 :
263 2 : bool MediaPipeline::play(bool &async)
264 : {
265 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
266 :
267 2 : return m_mediaPipelineIpc->play(async);
268 : }
269 :
270 2 : bool MediaPipeline::pause()
271 : {
272 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
273 :
274 2 : return m_mediaPipelineIpc->pause();
275 : }
276 :
277 0 : bool MediaPipeline::stop()
278 : {
279 0 : RIALTO_CLIENT_LOG_DEBUG("entry:");
280 :
281 0 : m_currentState = State::IDLE;
282 :
283 0 : return m_mediaPipelineIpc->stop();
284 : }
285 :
286 2 : bool MediaPipeline::setPlaybackRate(double rate)
287 : {
288 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
289 :
290 2 : return m_mediaPipelineIpc->setPlaybackRate(rate);
291 : }
292 :
293 7 : bool MediaPipeline::setPosition(int64_t position)
294 : {
295 7 : switch (m_currentState)
296 : {
297 5 : case State::PLAYING:
298 : case State::BUFFERING:
299 : case State::SEEKING:
300 : case State::END_OF_STREAM:
301 : {
302 5 : return handleSetPosition(position);
303 : }
304 2 : case State::IDLE:
305 : case State::FAILURE:
306 : default:
307 : {
308 2 : RIALTO_CLIENT_LOG_WARN("SetPosition received in unexpected state '%s'", toString(m_currentState));
309 2 : return false;
310 : }
311 : }
312 : }
313 :
314 2 : bool MediaPipeline::getPosition(int64_t &position)
315 : {
316 2 : return m_mediaPipelineIpc->getPosition(position);
317 : }
318 :
319 2 : bool MediaPipeline::setImmediateOutput(int32_t sourceId, bool immediateOutput)
320 : {
321 2 : return m_mediaPipelineIpc->setImmediateOutput(sourceId, immediateOutput);
322 : }
323 :
324 2 : bool MediaPipeline::setReportDecodeErrors(int32_t sourceId, bool reportDecodeErrors)
325 : {
326 2 : return m_mediaPipelineIpc->setReportDecodeErrors(sourceId, reportDecodeErrors);
327 : }
328 :
329 2 : bool MediaPipeline::setMetricSubscription(const std::vector<MetricType> &types)
330 : {
331 2 : return m_mediaPipelineIpc->setMetricSubscription(types);
332 : }
333 :
334 2 : bool MediaPipeline::getQueuedFrames(int32_t sourceId, uint32_t &queuedFrames)
335 : {
336 2 : return m_mediaPipelineIpc->getQueuedFrames(sourceId, queuedFrames);
337 : }
338 :
339 2 : bool MediaPipeline::getImmediateOutput(int32_t sourceId, bool &immediateOutput)
340 : {
341 2 : return m_mediaPipelineIpc->getImmediateOutput(sourceId, immediateOutput);
342 : }
343 :
344 2 : bool MediaPipeline::getStats(int32_t sourceId, uint64_t &renderedFrames, uint64_t &droppedFrames)
345 : {
346 2 : return m_mediaPipelineIpc->getStats(sourceId, renderedFrames, droppedFrames);
347 : }
348 :
349 2 : bool MediaPipeline::getFrameCounters(int32_t sourceId, uint64_t &renderedFrames, uint64_t &droppedFrames,
350 : uint64_t &corruptedFrames, uint64_t ×tamp)
351 : {
352 2 : return m_mediaPipelineIpc->getFrameCounters(sourceId, renderedFrames, droppedFrames, corruptedFrames, timestamp);
353 : }
354 :
355 5 : bool MediaPipeline::handleSetPosition(int64_t position)
356 : {
357 : // needData requests no longer valid
358 : {
359 5 : std::lock_guard<std::mutex> lock{m_needDataRequestMapMutex};
360 5 : m_needDataRequestMap.clear();
361 : }
362 5 : return m_mediaPipelineIpc->setPosition(position);
363 : }
364 :
365 2 : bool MediaPipeline::setVideoWindow(uint32_t x, uint32_t y, uint32_t width, uint32_t height)
366 : {
367 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
368 :
369 2 : return m_mediaPipelineIpc->setVideoWindow(x, y, width, height);
370 : }
371 :
372 27 : bool MediaPipeline::haveData(MediaSourceStatus status, uint32_t needDataRequestId)
373 : {
374 27 : RIALTO_CLIENT_LOG_DEBUG("entry:");
375 :
376 27 : switch (m_currentState)
377 : {
378 22 : case State::BUFFERING:
379 : case State::PLAYING:
380 : {
381 22 : return handleHaveData(status, needDataRequestId);
382 : }
383 2 : case State::SEEKING:
384 : {
385 2 : RIALTO_CLIENT_LOG_INFO("HaveData received while seeking, discarding NeedData request %u", needDataRequestId);
386 2 : discardNeedDataRequest(needDataRequestId);
387 2 : return true;
388 : }
389 3 : case State::IDLE:
390 : case State::END_OF_STREAM:
391 : case State::FAILURE:
392 : default:
393 : {
394 3 : RIALTO_CLIENT_LOG_WARN("HaveData received in unexpected state '%s', discarding NeedData request %u",
395 : toString(m_currentState), needDataRequestId);
396 3 : discardNeedDataRequest(needDataRequestId);
397 3 : return false;
398 : }
399 : }
400 : }
401 :
402 22 : bool MediaPipeline::handleHaveData(MediaSourceStatus status, uint32_t needDataRequestId)
403 : {
404 22 : RIALTO_CLIENT_LOG_DEBUG("entry:");
405 :
406 22 : std::shared_ptr<NeedDataRequest> needDataRequest;
407 :
408 : // Find the needDataRequest for this needDataRequestId
409 : // The needData request can be cancelled from another thread
410 : {
411 22 : std::lock_guard<std::mutex> lock{m_needDataRequestMapMutex};
412 :
413 22 : auto needDataRequestIt = m_needDataRequestMap.find(needDataRequestId);
414 22 : if (needDataRequestIt == m_needDataRequestMap.end())
415 : {
416 : // Return success here as the data written is just ignored
417 19 : RIALTO_CLIENT_LOG_WARN("Could not find need data request, with id %u", needDataRequestId);
418 19 : return true;
419 : }
420 :
421 3 : needDataRequest = needDataRequestIt->second;
422 3 : m_needDataRequestMap.erase(needDataRequestIt);
423 22 : }
424 3 : if (m_attachedSources.isFlushing(needDataRequest->sourceId))
425 : {
426 0 : RIALTO_CLIENT_LOG_WARN("Source %d is flushing. Ignoring need data request, with id %u",
427 : needDataRequest->sourceId, needDataRequestId);
428 0 : return true;
429 : }
430 :
431 3 : uint32_t numFrames = needDataRequest->frameWriter ? needDataRequest->frameWriter->getNumFrames() : 0;
432 3 : return m_mediaPipelineIpc->haveData(status, numFrames, needDataRequestId);
433 22 : }
434 :
435 53 : AddSegmentStatus MediaPipeline::addSegment(uint32_t needDataRequestId, const std::unique_ptr<MediaSegment> &mediaSegment)
436 : {
437 53 : RIALTO_CLIENT_LOG_DEBUG("entry:");
438 :
439 53 : if (nullptr == mediaSegment || nullptr == mediaSegment->getData())
440 : {
441 2 : return AddSegmentStatus::ERROR;
442 : }
443 :
444 51 : std::lock_guard<std::mutex> lock{m_needDataRequestMapMutex};
445 51 : auto needDataRequestIt = m_needDataRequestMap.find(needDataRequestId);
446 51 : if (needDataRequestIt == m_needDataRequestMap.end())
447 : {
448 0 : RIALTO_CLIENT_LOG_ERROR("Could not find need data request, with id %u", needDataRequestId);
449 0 : return AddSegmentStatus::ERROR;
450 : }
451 :
452 51 : std::shared_ptr<NeedDataRequest> needDataRequest = needDataRequestIt->second;
453 51 : std::shared_ptr<ISharedMemoryHandle> shmHandle;
454 : {
455 51 : std::lock_guard<std::mutex> shmLock{m_shmMutex};
456 51 : shmHandle = m_shmHandle;
457 : }
458 51 : if (nullptr == shmHandle || nullptr == shmHandle->getShm())
459 : {
460 2 : RIALTO_CLIENT_LOG_ERROR("Shared buffer no longer valid");
461 2 : return AddSegmentStatus::ERROR;
462 : }
463 :
464 : // This block of code is only for playready apps using rialto c++ interface
465 : // Widevine apps and playready apps using rialto-ocdm set MediaSegment::keyId earlier
466 49 : if (mediaSegment->isEncrypted())
467 : {
468 2 : auto keyId = KeyIdMap::instance().get(mediaSegment->getMediaKeySessionId());
469 2 : if (!keyId.empty() && mediaSegment->getKeyId().empty())
470 : {
471 1 : RIALTO_CLIENT_LOG_DEBUG("Adding Playready keyID to media segment");
472 1 : mediaSegment->setKeyId(keyId);
473 : }
474 2 : }
475 :
476 49 : if (!needDataRequest->frameWriter)
477 : {
478 17 : if (firebolt::rialto::MediaSourceType::UNKNOWN != mediaSegment->getType())
479 : {
480 16 : needDataRequest->frameWriter =
481 32 : m_mediaFrameWriterFactory->createFrameWriter(shmHandle->getShm(), needDataRequest->shmInfo);
482 : }
483 : else
484 : {
485 1 : RIALTO_CLIENT_LOG_ERROR("Unrecognised type %u", static_cast<uint32_t>(mediaSegment->getType()));
486 1 : return AddSegmentStatus::ERROR;
487 : }
488 :
489 16 : if (!needDataRequest->frameWriter)
490 : {
491 1 : RIALTO_CLIENT_LOG_ERROR("Could not create frame writer");
492 1 : return AddSegmentStatus::ERROR;
493 : }
494 : }
495 :
496 47 : return needDataRequest->frameWriter->writeFrame(mediaSegment);
497 51 : }
498 :
499 2 : bool MediaPipeline::renderFrame()
500 : {
501 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
502 2 : return m_mediaPipelineIpc->renderFrame();
503 : }
504 :
505 3 : bool MediaPipeline::setVolume(double targetVolume, uint32_t volumeDuration, EaseType easeType)
506 : {
507 3 : RIALTO_CLIENT_LOG_DEBUG("entry:");
508 3 : return m_mediaPipelineIpc->setVolume(targetVolume, volumeDuration, easeType);
509 : }
510 :
511 2 : bool MediaPipeline::getVolume(double ¤tVolume)
512 : {
513 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
514 2 : return m_mediaPipelineIpc->getVolume(currentVolume);
515 : }
516 :
517 2 : bool MediaPipeline::setMute(int32_t sourceId, bool mute)
518 : {
519 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
520 2 : return m_mediaPipelineIpc->setMute(sourceId, mute);
521 : }
522 :
523 2 : bool MediaPipeline::getMute(int32_t sourceId, bool &mute)
524 : {
525 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
526 2 : return m_mediaPipelineIpc->getMute(sourceId, mute);
527 : }
528 :
529 2 : bool MediaPipeline::setTextTrackIdentifier(const std::string &textTrackIdentifier)
530 : {
531 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
532 2 : return m_mediaPipelineIpc->setTextTrackIdentifier(textTrackIdentifier);
533 : }
534 :
535 2 : bool MediaPipeline::getTextTrackIdentifier(std::string &textTrackIdentifier)
536 : {
537 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
538 2 : return m_mediaPipelineIpc->getTextTrackIdentifier(textTrackIdentifier);
539 : }
540 :
541 2 : bool MediaPipeline::setLowLatency(bool lowLatency)
542 : {
543 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
544 2 : return m_mediaPipelineIpc->setLowLatency(lowLatency);
545 : }
546 :
547 2 : bool MediaPipeline::setSync(bool sync)
548 : {
549 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
550 2 : return m_mediaPipelineIpc->setSync(sync);
551 : }
552 :
553 2 : bool MediaPipeline::getSync(bool &sync)
554 : {
555 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
556 2 : return m_mediaPipelineIpc->getSync(sync);
557 : }
558 :
559 2 : bool MediaPipeline::setSyncOff(bool syncOff)
560 : {
561 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
562 2 : return m_mediaPipelineIpc->setSyncOff(syncOff);
563 : }
564 :
565 2 : bool MediaPipeline::setStreamSyncMode(int32_t sourceId, int32_t streamSyncMode)
566 : {
567 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
568 2 : return m_mediaPipelineIpc->setStreamSyncMode(sourceId, streamSyncMode);
569 : }
570 :
571 2 : bool MediaPipeline::getStreamSyncMode(int32_t &streamSyncMode)
572 : {
573 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
574 2 : return m_mediaPipelineIpc->getStreamSyncMode(streamSyncMode);
575 : }
576 :
577 7 : bool MediaPipeline::flush(int32_t sourceId, bool resetTime, bool &async)
578 : {
579 7 : RIALTO_CLIENT_LOG_DEBUG("entry:");
580 :
581 : {
582 7 : std::unique_lock<std::mutex> flushLock{m_flushMutex};
583 7 : if (!m_mediaPipelineIpc->flush(sourceId, resetTime, async))
584 : {
585 1 : return false;
586 : }
587 6 : m_attachedSources.setFlushing(sourceId, true);
588 7 : }
589 :
590 : // Clear all need datas for flushed source
591 6 : std::lock_guard<std::mutex> lock{m_needDataRequestMapMutex};
592 8 : for (auto it = m_needDataRequestMap.begin(); it != m_needDataRequestMap.end();)
593 : {
594 2 : if (it->second->sourceId == sourceId)
595 : {
596 2 : it = m_needDataRequestMap.erase(it);
597 : }
598 : else
599 : {
600 0 : ++it;
601 : }
602 : }
603 6 : return true;
604 : }
605 :
606 2 : bool MediaPipeline::setSourcePosition(int32_t sourceId, int64_t position, bool resetTime, double appliedRate,
607 : uint64_t stopPosition)
608 : {
609 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
610 :
611 2 : return m_mediaPipelineIpc->setSourcePosition(sourceId, position, resetTime, appliedRate, stopPosition);
612 : }
613 :
614 2 : bool MediaPipeline::setSubtitleOffset(int32_t sourceId, int64_t position)
615 : {
616 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
617 :
618 2 : return m_mediaPipelineIpc->setSubtitleOffset(sourceId, position);
619 : }
620 :
621 2 : bool MediaPipeline::processAudioGap(int64_t position, uint32_t duration, int64_t discontinuityGap, bool audioAac)
622 : {
623 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
624 :
625 2 : return m_mediaPipelineIpc->processAudioGap(position, duration, discontinuityGap, audioAac);
626 : }
627 :
628 2 : bool MediaPipeline::setBufferingLimit(uint32_t limitBufferingMs)
629 : {
630 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
631 :
632 2 : return m_mediaPipelineIpc->setBufferingLimit(limitBufferingMs);
633 : }
634 :
635 2 : bool MediaPipeline::getBufferingLimit(uint32_t &limitBufferingMs)
636 : {
637 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
638 :
639 2 : return m_mediaPipelineIpc->getBufferingLimit(limitBufferingMs);
640 : }
641 :
642 2 : bool MediaPipeline::setUseBuffering(bool useBuffering)
643 : {
644 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
645 :
646 2 : return m_mediaPipelineIpc->setUseBuffering(useBuffering);
647 : }
648 :
649 2 : bool MediaPipeline::getUseBuffering(bool &useBuffering)
650 : {
651 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
652 :
653 2 : return m_mediaPipelineIpc->getUseBuffering(useBuffering);
654 : }
655 :
656 2 : bool MediaPipeline::switchSource(const std::unique_ptr<MediaSource> &source)
657 : {
658 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
659 :
660 2 : return m_mediaPipelineIpc->switchSource(source);
661 : }
662 :
663 2 : bool MediaPipeline::getDuration(int64_t &duration)
664 : {
665 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
666 :
667 2 : return m_mediaPipelineIpc->getDuration(duration);
668 : }
669 :
670 5 : void MediaPipeline::discardNeedDataRequest(uint32_t needDataRequestId)
671 : {
672 : // Find the needDataRequest for this needDataRequestId
673 : // The needData request can be cancelled from another thread
674 : {
675 5 : std::lock_guard<std::mutex> lock{m_needDataRequestMapMutex};
676 :
677 5 : auto needDataRequestIt = m_needDataRequestMap.find(needDataRequestId);
678 5 : if (needDataRequestIt == m_needDataRequestMap.end())
679 : {
680 0 : RIALTO_CLIENT_LOG_INFO("Could not find need data request, with id %u", needDataRequestId);
681 : }
682 : else
683 : {
684 5 : RIALTO_CLIENT_LOG_INFO("Discarding need data request with id %u", needDataRequestId);
685 5 : m_needDataRequestMap.erase(needDataRequestIt);
686 : }
687 : }
688 : }
689 :
690 0 : std::weak_ptr<IMediaPipelineClient> MediaPipeline::getClient()
691 : {
692 0 : return m_mediaPipelineClient;
693 : }
694 :
695 4 : void MediaPipeline::updateState(NetworkState state)
696 : {
697 4 : State newState = m_currentState;
698 :
699 4 : switch (state)
700 : {
701 3 : case NetworkState::BUFFERING:
702 : case NetworkState::BUFFERING_PROGRESS:
703 : case NetworkState::STALLED:
704 : {
705 3 : newState = State::BUFFERING;
706 3 : break;
707 : }
708 0 : case NetworkState::FORMAT_ERROR:
709 : case NetworkState::NETWORK_ERROR:
710 : case NetworkState::DECODE_ERROR:
711 : {
712 0 : newState = State::FAILURE;
713 0 : break;
714 : }
715 1 : default:
716 : {
717 1 : break;
718 : }
719 : }
720 :
721 4 : RIALTO_CLIENT_LOG_DEBUG("Received network state '%s', old state '%s', new state '%s'", toString(state),
722 : toString(m_currentState), toString(newState));
723 4 : m_currentState = newState;
724 : }
725 :
726 68 : void MediaPipeline::updateState(PlaybackState state)
727 : {
728 68 : State newState = m_currentState;
729 :
730 68 : switch (state)
731 : {
732 50 : case PlaybackState::PLAYING:
733 : case PlaybackState::PAUSED:
734 : {
735 50 : newState = State::PLAYING;
736 50 : break;
737 : }
738 4 : case PlaybackState::SEEKING:
739 : {
740 4 : newState = State::SEEKING;
741 4 : break;
742 : }
743 3 : case PlaybackState::STOPPED:
744 : {
745 3 : newState = State::IDLE;
746 3 : break;
747 : }
748 3 : case PlaybackState::SEEK_DONE:
749 : {
750 3 : newState = State::BUFFERING;
751 3 : break;
752 : }
753 4 : case PlaybackState::END_OF_STREAM:
754 : {
755 4 : newState = State::END_OF_STREAM;
756 4 : break;
757 : }
758 3 : case PlaybackState::FAILURE:
759 : {
760 3 : newState = State::FAILURE;
761 3 : break;
762 : }
763 1 : default:
764 : {
765 1 : break;
766 : }
767 : }
768 :
769 68 : RIALTO_CLIENT_LOG_DEBUG("Received playback state '%s', old state '%s', new state '%s'", toString(state),
770 : toString(m_currentState), toString(newState));
771 68 : m_currentState = newState;
772 : }
773 :
774 68 : void MediaPipeline::notifyPlaybackState(PlaybackState state)
775 : {
776 68 : RIALTO_CLIENT_LOG_DEBUG("entry:");
777 :
778 68 : updateState(state);
779 :
780 68 : std::shared_ptr<IMediaPipelineClient> client = m_mediaPipelineClient.lock();
781 68 : if (client)
782 : {
783 68 : client->notifyPlaybackState(state);
784 : }
785 : }
786 :
787 0 : void MediaPipeline::notifyPosition(int64_t position)
788 : {
789 0 : RIALTO_CLIENT_LOG_DEBUG("entry:");
790 :
791 0 : std::shared_ptr<IMediaPipelineClient> client = m_mediaPipelineClient.lock();
792 0 : if (client)
793 : {
794 0 : client->notifyPosition(position);
795 : }
796 : }
797 :
798 4 : void MediaPipeline::notifyNetworkState(NetworkState state)
799 : {
800 4 : RIALTO_CLIENT_LOG_DEBUG("entry:");
801 :
802 4 : updateState(state);
803 :
804 4 : std::shared_ptr<IMediaPipelineClient> client = m_mediaPipelineClient.lock();
805 4 : if (client)
806 : {
807 4 : client->notifyNetworkState(state);
808 : }
809 : }
810 :
811 49 : void MediaPipeline::notifyNeedMediaData(int32_t sourceId, size_t frameCount, uint32_t requestId,
812 : const std::shared_ptr<MediaPlayerShmInfo> &shmInfo)
813 : {
814 49 : RIALTO_CLIENT_LOG_DEBUG("entry:");
815 :
816 : // If attach source is ongoing wait till it has completed so that all sources are attached
817 : {
818 49 : std::unique_lock<std::mutex> lock{m_attachSourceMutex};
819 49 : if (m_attachingSource)
820 0 : m_attachSourceCond.wait(lock, [this] { return !m_attachingSource; });
821 49 : }
822 :
823 49 : if (MediaSourceType::UNKNOWN == m_attachedSources.getType(sourceId))
824 : {
825 2 : RIALTO_CLIENT_LOG_WARN("NeedMediaData received for unknown source %d, ignoring request id %u", sourceId,
826 : requestId);
827 2 : return;
828 : }
829 47 : if (m_attachedSources.isFlushing(sourceId))
830 : {
831 1 : RIALTO_CLIENT_LOG_WARN("NeedMediaData received for flushing source %d, ignoring request id %u", sourceId,
832 : requestId);
833 1 : return;
834 : }
835 :
836 46 : switch (m_currentState)
837 : {
838 39 : case State::BUFFERING:
839 : case State::PLAYING:
840 : {
841 39 : std::shared_ptr<NeedDataRequest> needDataRequest = std::make_shared<NeedDataRequest>();
842 39 : needDataRequest->sourceId = sourceId;
843 39 : needDataRequest->shmInfo = shmInfo;
844 :
845 : {
846 39 : std::lock_guard<std::mutex> lock{m_needDataRequestMapMutex};
847 39 : if (ApplicationState::RUNNING != m_currentAppState)
848 : {
849 1 : RIALTO_CLIENT_LOG_INFO("NeedMediaData received in state != RUNNING, ignoring request id %u", requestId);
850 1 : break;
851 : }
852 38 : m_needDataRequestMap[requestId] = std::move(needDataRequest);
853 39 : }
854 :
855 38 : std::shared_ptr<IMediaPipelineClient> client = m_mediaPipelineClient.lock();
856 38 : if (client)
857 : {
858 38 : client->notifyNeedMediaData(sourceId, frameCount, requestId, nullptr);
859 : }
860 :
861 38 : break;
862 39 : }
863 1 : case State::SEEKING:
864 : {
865 1 : RIALTO_CLIENT_LOG_INFO("NeedMediaData received while seeking, ignoring request id %u", requestId);
866 1 : break;
867 : }
868 6 : case State::IDLE:
869 : case State::END_OF_STREAM:
870 : case State::FAILURE:
871 : default:
872 : {
873 6 : RIALTO_CLIENT_LOG_WARN("NeedMediaData received in unexpected state '%s', ignoring request id %u",
874 : toString(m_currentState), requestId);
875 6 : break;
876 : }
877 : }
878 : }
879 :
880 129 : void MediaPipeline::notifyApplicationState(ApplicationState state)
881 : {
882 129 : RIALTO_CLIENT_LOG_DEBUG("entry:");
883 129 : std::lock_guard<std::mutex> lock{m_needDataRequestMapMutex};
884 129 : m_currentAppState = state;
885 129 : if (ApplicationState::RUNNING != state)
886 : {
887 : // If shared memory in use, wait for it to finish before returning
888 4 : m_needDataRequestMap.clear();
889 : }
890 129 : }
891 :
892 1 : void MediaPipeline::notifyQos(int32_t sourceId, const QosInfo &qosInfo)
893 : {
894 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
895 :
896 1 : std::shared_ptr<IMediaPipelineClient> client = m_mediaPipelineClient.lock();
897 1 : if (client)
898 : {
899 1 : client->notifyQos(sourceId, qosInfo);
900 : }
901 : }
902 :
903 1 : void MediaPipeline::notifyMetric(const MetricEvent &metric)
904 : {
905 1 : RIALTO_CLIENT_LOG_DEBUG("Forwarding playback metric event sourceId: %d, ptsNs: %llu, metric: %u, observationKind: "
906 : "%u, count: %llu, durationNs: %llu",
907 : metric.sourceId,
908 : static_cast<unsigned long long>(metric.presentationTimeStampNs.value_or(0)), // NOLINT(runtime/int)
909 : static_cast<uint32_t>(metric.type), static_cast<uint32_t>(metric.phase),
910 : static_cast<unsigned long long>(metric.count.value_or(0)), // NOLINT(runtime/int)
911 : static_cast<unsigned long long>(metric.durationNs.value_or(0))); // NOLINT(runtime/int)
912 :
913 1 : std::shared_ptr<IMediaPipelineClient> client = m_mediaPipelineClient.lock();
914 1 : if (client)
915 : {
916 1 : client->notifyMetric(metric);
917 : }
918 : else
919 : {
920 0 : RIALTO_CLIENT_LOG_DEBUG("Dropping playback metric event for sourceId: %d - client is unavailable",
921 : metric.sourceId);
922 : }
923 1 : }
924 :
925 0 : void MediaPipeline::notifyBufferUnderflow(int32_t sourceId)
926 : {
927 0 : RIALTO_CLIENT_LOG_DEBUG("entry:");
928 :
929 0 : std::shared_ptr<IMediaPipelineClient> client = m_mediaPipelineClient.lock();
930 0 : if (client)
931 : {
932 0 : client->notifyBufferUnderflow(sourceId);
933 : }
934 : }
935 :
936 1 : void MediaPipeline::notifyFirstFrameReceived(int32_t sourceId)
937 : {
938 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
939 :
940 1 : std::shared_ptr<IMediaPipelineClient> client = m_mediaPipelineClient.lock();
941 1 : if (client)
942 : {
943 1 : client->notifyFirstFrameReceived(sourceId);
944 : }
945 : }
946 :
947 1 : void MediaPipeline::notifyPlaybackError(int32_t sourceId, PlaybackError error)
948 : {
949 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
950 :
951 1 : std::shared_ptr<IMediaPipelineClient> client = m_mediaPipelineClient.lock();
952 1 : if (client)
953 : {
954 1 : client->notifyPlaybackError(sourceId, error);
955 : }
956 : }
957 :
958 1 : void MediaPipeline::notifyVideoData(bool hasData)
959 : {
960 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
961 :
962 1 : std::shared_ptr<IMediaPipelineClient> client = m_mediaPipelineClient.lock();
963 1 : if (client)
964 : {
965 1 : client->notifyVideoData(hasData);
966 : }
967 : }
968 :
969 1 : void MediaPipeline::notifyAudioData(bool hasData)
970 : {
971 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
972 :
973 1 : std::shared_ptr<IMediaPipelineClient> client = m_mediaPipelineClient.lock();
974 1 : if (client)
975 : {
976 1 : client->notifyAudioData(hasData);
977 : }
978 : }
979 :
980 4 : void MediaPipeline::notifySourceFlushed(int32_t sourceId)
981 : {
982 4 : RIALTO_CLIENT_LOG_DEBUG("entry:");
983 : {
984 4 : std::lock_guard<std::mutex> lock{m_flushMutex};
985 4 : m_attachedSources.setFlushing(sourceId, false);
986 : }
987 4 : std::shared_ptr<IMediaPipelineClient> client = m_mediaPipelineClient.lock();
988 4 : if (client)
989 : {
990 4 : client->notifySourceFlushed(sourceId);
991 : }
992 :
993 4 : State expected = State::END_OF_STREAM;
994 4 : m_currentState.compare_exchange_strong(expected, State::BUFFERING);
995 : }
996 :
997 1 : void MediaPipeline::notifyPlaybackInfo(const PlaybackInfo &playbackInfo)
998 : {
999 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
1000 :
1001 1 : std::shared_ptr<IMediaPipelineClient> client = m_mediaPipelineClient.lock();
1002 1 : if (client)
1003 : {
1004 1 : client->notifyPlaybackInfo(playbackInfo);
1005 : }
1006 : }
1007 :
1008 : }; // namespace firebolt::rialto::client
|