LCOV - code coverage report
Current view: top level - media/server/main/source - MediaPipelineServerInternal.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 93.0 % 949 883
Test Date: 2026-12-04 20:46:59 Functions: 96.9 % 192 186

            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 <algorithm>
      21              : #include <stdexcept>
      22              : 
      23              : #include "ActiveRequests.h"
      24              : #include "DataReaderFactory.h"
      25              : #include "IDataReader.h"
      26              : #include "IRdkGstreamerUtilsWrapper.h"
      27              : #include "IRialtoMetrics.h"
      28              : #include "ISharedMemoryBuffer.h"
      29              : #include "MediaPipelineServerInternal.h"
      30              : #include "NeedMediaData.h"
      31              : #include "RialtoServerLogging.h"
      32              : #include "TypeConverters.h"
      33              : 
      34              : namespace
      35              : {
      36            1 : const char *toString(const firebolt::rialto::MediaSourceStatus &status)
      37              : {
      38            1 :     switch (status)
      39              :     {
      40            0 :     case firebolt::rialto::MediaSourceStatus::OK:
      41            0 :         return "OK";
      42            0 :     case firebolt::rialto::MediaSourceStatus::EOS:
      43            0 :         return "EOS";
      44            1 :     case firebolt::rialto::MediaSourceStatus::ERROR:
      45            1 :         return "ERROR";
      46            0 :     case firebolt::rialto::MediaSourceStatus::CODEC_CHANGED:
      47            0 :         return "CODEC_CHANGED";
      48            0 :     case firebolt::rialto::MediaSourceStatus::NO_AVAILABLE_SAMPLES:
      49            0 :         return "NO_AVAILABLE_SAMPLES";
      50            0 :     case firebolt::rialto::MediaSourceStatus::NO_SPACE_FOR_SAMPLES:
      51            0 :         return "NO_SPACE_FOR_SAMPLES";
      52              :     }
      53            0 :     return "Unknown";
      54              : }
      55              : 
      56           45 : std::int32_t generateSourceId()
      57              : {
      58              :     static std::int32_t sourceId{1};
      59           45 :     return sourceId++;
      60              : }
      61              : 
      62              : using firebolt::rialto::MetricType;
      63              : } // namespace
      64              : 
      65              : namespace firebolt::rialto
      66              : {
      67            1 : std::shared_ptr<IMediaPipelineFactory> IMediaPipelineFactory::createFactory()
      68              : {
      69            1 :     return server::MediaPipelineServerInternalFactory::createFactory();
      70              : }
      71              : }; // namespace firebolt::rialto
      72              : 
      73              : namespace firebolt::rialto::server
      74              : {
      75            0 : std::shared_ptr<server::IMediaPipelineServerInternalFactory> IMediaPipelineServerInternalFactory::createFactory()
      76              : {
      77            0 :     return MediaPipelineServerInternalFactory::createFactory();
      78              : }
      79              : 
      80            2 : std::shared_ptr<MediaPipelineServerInternalFactory> MediaPipelineServerInternalFactory::createFactory()
      81              : {
      82            2 :     std::shared_ptr<MediaPipelineServerInternalFactory> factory;
      83              : 
      84              :     try
      85              :     {
      86            2 :         factory = std::make_shared<MediaPipelineServerInternalFactory>();
      87              :     }
      88            0 :     catch (const std::exception &e)
      89              :     {
      90            0 :         RIALTO_SERVER_LOG_ERROR("Failed to create the media player server internal factory, reason: %s", e.what());
      91              :     }
      92              : 
      93            2 :     return factory;
      94              : }
      95              : 
      96              : std::unique_ptr<IMediaPipeline>
      97            1 : MediaPipelineServerInternalFactory::createMediaPipeline(std::weak_ptr<IMediaPipelineClient> client,
      98              :                                                         const VideoRequirements &videoRequirements) const
      99              : {
     100            1 :     RIALTO_SERVER_LOG_ERROR(
     101              :         "This function can't be used by rialto server. Please use createMediaPipelineServerInternal");
     102            1 :     return nullptr;
     103              : }
     104              : 
     105            1 : std::unique_ptr<server::IMediaPipelineServerInternal> MediaPipelineServerInternalFactory::createMediaPipelineServerInternal(
     106              :     std::weak_ptr<IMediaPipelineClient> client, const VideoRequirements &videoRequirements, int sessionId,
     107              :     const std::shared_ptr<ISharedMemoryBuffer> &shmBuffer, IDecryptionService &decryptionService) const
     108              : {
     109            1 :     std::shared_ptr<IMediaPipelineClient> sharedClient = client.lock();
     110            1 :     if (!sharedClient)
     111              :     {
     112            0 :         RIALTO_SERVER_LOG_ERROR("Couldn't create client's shared pointer");
     113            0 :         return nullptr;
     114              :     }
     115              : 
     116            1 :     std::unique_ptr<server::MediaPipelineServerInternal> mediaPipeline;
     117              :     try
     118              :     {
     119              :         mediaPipeline =
     120            2 :             std::make_unique<server::MediaPipelineServerInternal>(sharedClient, videoRequirements,
     121            2 :                                                                   server::IGstGenericPlayerFactory::getFactory(),
     122              :                                                                   sessionId, shmBuffer,
     123            2 :                                                                   server::IMainThreadFactory::createFactory(),
     124            2 :                                                                   common::ITimerFactory::getFactory(),
     125            2 :                                                                   std::make_unique<DataReaderFactory>(),
     126            3 :                                                                   std::make_unique<ActiveRequests>(), decryptionService);
     127              :     }
     128            0 :     catch (const std::exception &e)
     129              :     {
     130            0 :         RIALTO_SERVER_LOG_ERROR("Failed to create the media player server internal, reason: %s", e.what());
     131              :     }
     132              : 
     133            1 :     return mediaPipeline;
     134              : }
     135              : 
     136          178 : MediaPipelineServerInternal::MediaPipelineServerInternal(
     137              :     const std::shared_ptr<IMediaPipelineClient> &client, const VideoRequirements &videoRequirements,
     138              :     const std::shared_ptr<IGstGenericPlayerFactory> &gstPlayerFactory, int sessionId,
     139              :     const std::shared_ptr<ISharedMemoryBuffer> &shmBuffer, const std::shared_ptr<IMainThreadFactory> &mainThreadFactory,
     140              :     const std::shared_ptr<common::ITimerFactory> &timerFactory, std::unique_ptr<IDataReaderFactory> &&dataReaderFactory,
     141          178 :     std::unique_ptr<IActiveRequests> &&activeRequests, IDecryptionService &decryptionService)
     142          178 :     : m_mediaPipelineClient(client), m_kGstPlayerFactory(gstPlayerFactory), m_kVideoRequirements(videoRequirements),
     143          178 :       m_sessionId{sessionId}, m_shmBuffer{shmBuffer}, m_dataReaderFactory{std::move(dataReaderFactory)},
     144          178 :       m_timerFactory{timerFactory}, m_activeRequests{std::move(activeRequests)}, m_decryptionService{decryptionService},
     145          534 :       m_currentPlaybackState{PlaybackState::UNKNOWN}, m_wasAllSourcesAttachedCalled{false}
     146              : {
     147          178 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     148              : 
     149          178 :     m_mainThread = mainThreadFactory->getMainThread();
     150          178 :     if (!m_mainThread)
     151              :     {
     152            0 :         throw std::runtime_error("Failed to get the main thread");
     153              :     }
     154          178 :     m_mainThreadClientId = m_mainThread->registerClient();
     155              : 
     156          178 :     bool result = false;
     157          178 :     auto task = [&]()
     158              :     {
     159          178 :         if (!m_shmBuffer->mapPartition(ISharedMemoryBuffer::MediaPlaybackType::GENERIC, m_sessionId))
     160              :         {
     161            0 :             RIALTO_SERVER_LOG_ERROR("Unable to map shm partition");
     162              :         }
     163              :         else
     164              :         {
     165          178 :             result = true;
     166              :         }
     167          356 :     };
     168              : 
     169          178 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     170          178 :     if (!result)
     171              :     {
     172            0 :         throw std::runtime_error("MediaPipelineServerInternal construction failed");
     173              :     }
     174          178 : }
     175              : 
     176          534 : MediaPipelineServerInternal::~MediaPipelineServerInternal()
     177              : {
     178          178 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     179              : 
     180          178 :     auto task = [&]()
     181              :     {
     182          184 :         for (const auto &timer : m_needMediaDataTimers)
     183              :         {
     184            6 :             if (timer.second && timer.second->isActive())
     185              :             {
     186            6 :                 timer.second->cancel();
     187              :             }
     188              :         }
     189          178 :         if (!m_shmBuffer->unmapPartition(ISharedMemoryBuffer::MediaPlaybackType::GENERIC, m_sessionId))
     190              :         {
     191            0 :             RIALTO_SERVER_LOG_ERROR("Unable to unmap shm partition");
     192              :         }
     193              : 
     194          178 :         m_shmBuffer.reset();
     195          178 :         m_mainThread->unregisterClient(m_mainThreadClientId);
     196          356 :     };
     197          178 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     198          356 : }
     199              : 
     200          131 : bool MediaPipelineServerInternal::load(MediaType type, const std::string &mimeType, const std::string &url, bool isLive)
     201              : {
     202          131 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     203              : 
     204              :     bool result;
     205          131 :     auto task = [&]() { result = loadInternal(type, mimeType, url, isLive); };
     206              : 
     207          131 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     208          131 :     return result;
     209              : }
     210              : 
     211          131 : bool MediaPipelineServerInternal::loadInternal(MediaType type, const std::string &mimeType, const std::string &url,
     212              :                                                bool isLive)
     213              : {
     214          131 :     std::unique_lock lock{m_getPropertyMutex};
     215              :     /* If gstreamer player already created, destroy the old one first */
     216          131 :     if (m_gstPlayer)
     217              :     {
     218            0 :         m_gstPlayer.reset();
     219              :     }
     220              : 
     221              :     m_gstPlayer =
     222          131 :         m_kGstPlayerFactory
     223          393 :             ->createGstGenericPlayer(this, m_decryptionService, type, m_kVideoRequirements, isLive,
     224          393 :                                      firebolt::rialto::wrappers::IRdkGstreamerUtilsWrapperFactory::getFactory());
     225          131 :     if (!m_gstPlayer)
     226              :     {
     227            1 :         RIALTO_SERVER_LOG_ERROR("Failed to load gstreamer player");
     228            1 :         return false;
     229              :     }
     230              : 
     231          130 :     const auto metrics = m_gstPlayer->getRialtoMetrics();
     232          130 :     if (metrics)
     233              :     {
     234            0 :         RIALTO_SERVER_LOG_DEBUG("Assigning playback metrics sessionId: %d", m_sessionId);
     235            0 :         metrics->setSessionId(m_sessionId);
     236              :     }
     237              :     else
     238              :     {
     239          130 :         RIALTO_SERVER_LOG_WARN("Playback metrics unavailable for sessionId: %d", m_sessionId);
     240              :     }
     241          130 :     m_gstPlayer->setMetricSessionId(m_sessionId);
     242              : 
     243          130 :     notifyNetworkState(NetworkState::BUFFERING);
     244              : 
     245          130 :     return true;
     246          131 : }
     247              : 
     248           47 : bool MediaPipelineServerInternal::attachSource(const std::unique_ptr<MediaSource> &source)
     249              : {
     250           47 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     251              : 
     252              :     bool result;
     253           47 :     auto task = [&]() { result = attachSourceInternal(source); };
     254              : 
     255           47 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     256           47 :     return result;
     257              : }
     258              : 
     259           47 : bool MediaPipelineServerInternal::attachSourceInternal(const std::unique_ptr<MediaSource> &source)
     260              : {
     261           47 :     source->setId(-1);
     262              : 
     263           47 :     if (!m_gstPlayer)
     264              :     {
     265            1 :         RIALTO_SERVER_LOG_ERROR("Gstreamer player has not been loaded");
     266            1 :         return false;
     267              :     }
     268              : 
     269           46 :     if (source->getType() == MediaSourceType::UNKNOWN)
     270              :     {
     271            0 :         RIALTO_SERVER_LOG_ERROR("Media source type unknown");
     272            0 :         return false;
     273              :     }
     274              : 
     275           46 :     const auto kSourceIter = m_attachedSources.find(source->getType());
     276           46 :     const bool kIsNewSource = m_attachedSources.cend() == kSourceIter;
     277           46 :     if (kIsNewSource)
     278              :     {
     279           45 :         source->setId(generateSourceId());
     280              :     }
     281              : 
     282              :     // Repeat attaches still reach the player so that an audio source can be re-attached with new caps
     283           46 :     m_gstPlayer->attachSource(source);
     284              : 
     285           46 :     if (!kIsNewSource)
     286              :     {
     287            1 :         RIALTO_SERVER_LOG_WARN("SourceType '%s' already attached", common::convertMediaSourceType(source->getType()));
     288            1 :         return false;
     289              :     }
     290              : 
     291           45 :     RIALTO_SERVER_LOG_DEBUG("New ID generated for MediaSourceType: %s: %d",
     292              :                             common::convertMediaSourceType(source->getType()), source->getId());
     293           45 :     m_attachedSources.emplace(source->getType(), source->getId());
     294              : 
     295           45 :     return true;
     296              : }
     297              : 
     298            4 : bool MediaPipelineServerInternal::removeSource(int32_t id)
     299              : {
     300            4 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     301              : 
     302              :     bool result;
     303            4 :     auto task = [&]() { result = removeSourceInternal(id); };
     304              : 
     305            4 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     306            4 :     return result;
     307              : }
     308              : 
     309            4 : bool MediaPipelineServerInternal::removeSourceInternal(int32_t id)
     310              : {
     311            4 :     if (!m_gstPlayer)
     312              :     {
     313            1 :         RIALTO_SERVER_LOG_ERROR("Failed to remove source - Gstreamer player has not been loaded");
     314            1 :         return false;
     315              :     }
     316            3 :     auto sourceIter = std::find_if(m_attachedSources.begin(), m_attachedSources.end(),
     317            2 :                                    [id](const auto &src) { return src.second == id; });
     318            3 :     if (sourceIter == m_attachedSources.end())
     319              :     {
     320            1 :         RIALTO_SERVER_LOG_ERROR("Failed to remove source with id %d- Source not found", id);
     321            1 :         return false;
     322              :     }
     323              : 
     324            2 :     MediaSourceType type = sourceIter->first;
     325              : 
     326            2 :     m_gstPlayer->removeSource(type);
     327            2 :     m_needMediaDataTimers.erase(type);
     328            2 :     m_noAvailableSamplesCounter.erase(type);
     329            2 :     m_isMediaTypeEosMap.erase(type);
     330              : 
     331            2 :     m_attachedSources.erase(sourceIter);
     332            2 :     return true;
     333              : }
     334              : 
     335            4 : bool MediaPipelineServerInternal::allSourcesAttached()
     336              : {
     337            4 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     338              : 
     339              :     bool result;
     340            4 :     auto task = [&]() { result = allSourcesAttachedInternal(); };
     341              : 
     342            4 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     343            4 :     return result;
     344              : }
     345              : 
     346            4 : bool MediaPipelineServerInternal::allSourcesAttachedInternal()
     347              : {
     348            4 :     if (!m_gstPlayer)
     349              :     {
     350            1 :         RIALTO_SERVER_LOG_ERROR("Failed to notify all sources attached - Gstreamer player has not been loaded");
     351            1 :         return false;
     352              :     }
     353              : 
     354            3 :     if (m_wasAllSourcesAttachedCalled)
     355              :     {
     356            1 :         RIALTO_SERVER_LOG_WARN("Failed to notify all sources attached - It was already called");
     357            1 :         return false;
     358              :     }
     359              : 
     360            2 :     m_gstPlayer->allSourcesAttached();
     361            2 :     m_wasAllSourcesAttachedCalled = true;
     362            2 :     return true;
     363              : }
     364              : 
     365            2 : bool MediaPipelineServerInternal::play(bool &async)
     366              : {
     367            2 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     368              : 
     369              :     bool result;
     370            2 :     auto task = [&]() { result = playInternal(async); };
     371              : 
     372            2 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     373            2 :     return result;
     374              : }
     375              : 
     376            2 : bool MediaPipelineServerInternal::playInternal(bool &async)
     377              : {
     378            2 :     if (!m_gstPlayer)
     379              :     {
     380            1 :         RIALTO_SERVER_LOG_ERROR("Failed to play - Gstreamer player has not been loaded");
     381            1 :         return false;
     382              :     }
     383              : 
     384            1 :     m_gstPlayer->play(async);
     385            1 :     return true;
     386              : }
     387              : 
     388            2 : bool MediaPipelineServerInternal::pause()
     389              : {
     390            2 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     391              : 
     392              :     bool result;
     393            2 :     auto task = [&]() { result = pauseInternal(); };
     394              : 
     395            2 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     396            2 :     return result;
     397              : }
     398              : 
     399            2 : bool MediaPipelineServerInternal::pauseInternal()
     400              : {
     401            2 :     if (!m_gstPlayer)
     402              :     {
     403            1 :         RIALTO_SERVER_LOG_ERROR("Failed to pause - Gstreamer player has not been loaded");
     404            1 :         return false;
     405              :     }
     406              : 
     407            1 :     m_gstPlayer->pause();
     408            1 :     return true;
     409              : }
     410              : 
     411            2 : bool MediaPipelineServerInternal::stop()
     412              : {
     413            2 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     414              : 
     415              :     bool result;
     416            2 :     auto task = [&]() { result = stopInternal(); };
     417              : 
     418            2 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     419            2 :     return result;
     420              : }
     421              : 
     422            2 : bool MediaPipelineServerInternal::stopInternal()
     423              : {
     424            2 :     if (!m_gstPlayer)
     425              :     {
     426            1 :         RIALTO_SERVER_LOG_ERROR("Failed to stop - Gstreamer player has not been loaded");
     427            1 :         return false;
     428              :     }
     429              : 
     430            1 :     m_gstPlayer->stop();
     431            1 :     return true;
     432              : }
     433              : 
     434            3 : bool MediaPipelineServerInternal::setPlaybackRate(double rate)
     435              : {
     436            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     437              : 
     438              :     bool result;
     439            3 :     auto task = [&]() { result = setPlaybackRateInternal(rate); };
     440              : 
     441            3 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     442            3 :     return result;
     443              : }
     444              : 
     445            3 : bool MediaPipelineServerInternal::setPlaybackRateInternal(double rate)
     446              : {
     447            3 :     if (!m_gstPlayer)
     448              :     {
     449            2 :         RIALTO_SERVER_LOG_ERROR("Failed to set playback rate - Gstreamer player has not been loaded");
     450            2 :         return false;
     451              :     }
     452              : 
     453            1 :     if (0.0 == rate)
     454              :     {
     455            0 :         RIALTO_SERVER_LOG_ERROR("Failed to set playback rate to 0.0 - pause method should be used instead.");
     456            0 :         return false;
     457              :     }
     458              : 
     459            1 :     m_gstPlayer->setPlaybackRate(rate);
     460            1 :     return true;
     461              : }
     462              : 
     463            2 : bool MediaPipelineServerInternal::setPosition(int64_t position)
     464              : {
     465            2 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     466              : 
     467              :     bool result;
     468            2 :     auto task = [&]() { result = setPositionInternal(position); };
     469              : 
     470            2 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     471            2 :     return result;
     472              : }
     473              : 
     474            2 : bool MediaPipelineServerInternal::setPositionInternal(int64_t position)
     475              : {
     476            2 :     if (!m_gstPlayer)
     477              :     {
     478            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set position - Gstreamer player has not been loaded");
     479            1 :         return false;
     480              :     }
     481              : 
     482            1 :     m_gstPlayer->setPosition(position);
     483              : 
     484              :     // Reset Eos on seek
     485            1 :     for (auto &isMediaTypeEos : m_isMediaTypeEosMap)
     486              :     {
     487            0 :         isMediaTypeEos.second = false;
     488              :     }
     489              : 
     490            1 :     m_needDataDelayCalculator.resetMediaDataDelay();
     491              : 
     492            1 :     return true;
     493              : }
     494              : 
     495            3 : bool MediaPipelineServerInternal::getPosition(int64_t &position)
     496              : {
     497            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     498              : 
     499            3 :     std::shared_lock lock{m_getPropertyMutex};
     500              : 
     501            3 :     if (!m_gstPlayer)
     502              :     {
     503            1 :         RIALTO_SERVER_LOG_ERROR("Failed to get position - Gstreamer player has not been loaded");
     504            1 :         return false;
     505              :     }
     506            2 :     return m_gstPlayer->getPosition(position);
     507            3 : }
     508              : 
     509            3 : bool MediaPipelineServerInternal::getDuration(int64_t &duration)
     510              : {
     511            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     512              : 
     513            3 :     std::shared_lock lock{m_getPropertyMutex};
     514              : 
     515            3 :     if (!m_gstPlayer)
     516              :     {
     517            1 :         RIALTO_SERVER_LOG_ERROR("Failed to get duration - Gstreamer player has not been loaded");
     518            1 :         return false;
     519              :     }
     520            2 :     return m_gstPlayer->getDuration(duration);
     521            3 : }
     522              : 
     523            4 : bool MediaPipelineServerInternal::getStats(int32_t sourceId, uint64_t &renderedFrames, uint64_t &droppedFrames)
     524              : {
     525            4 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     526              : 
     527              :     bool result;
     528            4 :     auto task = [&]() { result = getStatsInternal(sourceId, renderedFrames, droppedFrames); };
     529              : 
     530            4 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     531            4 :     return result;
     532              : }
     533              : 
     534            4 : bool MediaPipelineServerInternal::getStatsInternal(int32_t sourceId, uint64_t &renderedFrames, uint64_t &droppedFrames)
     535              : {
     536            4 :     if (!m_gstPlayer)
     537              :     {
     538            1 :         RIALTO_SERVER_LOG_ERROR("Failed to get stats - Gstreamer player has not been loaded");
     539            1 :         return false;
     540              :     }
     541            3 :     auto sourceIter = std::find_if(m_attachedSources.begin(), m_attachedSources.end(),
     542            2 :                                    [sourceId](const auto &src) { return src.second == sourceId; });
     543            3 :     if (sourceIter == m_attachedSources.end())
     544              :     {
     545            1 :         RIALTO_SERVER_LOG_ERROR("Failed to get stats - Source not found");
     546            1 :         return false;
     547              :     }
     548            2 :     return m_gstPlayer->getStats(sourceIter->first, renderedFrames, droppedFrames);
     549              : }
     550              : 
     551            0 : bool MediaPipelineServerInternal::getFrameCounters(int32_t sourceId, uint64_t &renderedFrames, uint64_t &droppedFrames,
     552              :                                                    uint64_t &corruptedFrames, uint64_t &timestamp)
     553              : {
     554            0 :     return getFrameCounters(m_sessionId, sourceId, renderedFrames, droppedFrames, corruptedFrames, timestamp);
     555              : }
     556              : 
     557            6 : bool MediaPipelineServerInternal::getFrameCounters(int32_t sessionId, int32_t sourceId, uint64_t &renderedFrames,
     558              :                                                    uint64_t &droppedFrames, uint64_t &corruptedFrames,
     559              :                                                    uint64_t &timestamp)
     560              : {
     561              :     bool result;
     562            6 :     auto task = [&]() {
     563            6 :         result = getFrameCountersInternal(sessionId, sourceId, renderedFrames, droppedFrames, corruptedFrames, timestamp);
     564           12 :     };
     565              : 
     566            6 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     567            6 :     return result;
     568              : }
     569              : 
     570            6 : bool MediaPipelineServerInternal::getFrameCountersInternal(int32_t sessionId, int32_t sourceId,
     571              :                                                            uint64_t &renderedFrames, uint64_t &droppedFrames,
     572              :                                                            uint64_t &corruptedFrames, uint64_t &timestamp)
     573              : {
     574            6 :     if (!m_gstPlayer)
     575              :     {
     576            0 :         RIALTO_SERVER_LOG_ERROR("Failed to get frame counters - Gstreamer player has not been loaded");
     577            0 :         return false;
     578              :     }
     579              : 
     580            6 :     if (sessionId != m_sessionId)
     581              :     {
     582            1 :         RIALTO_SERVER_LOG_ERROR("Failed to get frame counters - sessionId: %d does not match pipeline sessionId: %d",
     583              :                                 sessionId, m_sessionId);
     584            1 :         return false;
     585              :     }
     586              : 
     587            5 :     const auto sourceIter = std::find_if(m_attachedSources.begin(), m_attachedSources.end(),
     588            5 :                                          [sourceId](const auto &source) { return source.second == sourceId; });
     589            5 :     if (sourceIter == m_attachedSources.end())
     590              :     {
     591            1 :         RIALTO_SERVER_LOG_ERROR("Failed to get frame counters - sourceId: %d is not attached", sourceId);
     592            1 :         return false;
     593              :     }
     594              : 
     595            4 :     const auto metrics = m_gstPlayer->getRialtoMetrics();
     596            4 :     if (!metrics)
     597              :     {
     598            0 :         RIALTO_SERVER_LOG_ERROR("Failed to get frame counters - RialtoMetrics is unavailable");
     599            0 :         return false;
     600              :     }
     601              : 
     602            4 :     CounterSnapshot counters;
     603            4 :     if (!metrics->getFrameCounters(sessionId, sourceId, sourceIter->first, counters))
     604              :     {
     605            0 :         RIALTO_SERVER_LOG_ERROR("Failed to get frame counters - no cached metrics for sessionId: %d, sourceId: %d",
     606              :                                 sessionId, sourceId);
     607            0 :         return false;
     608              :     }
     609              : 
     610            4 :     renderedFrames = counters.counters.renderedFrames;
     611            4 :     droppedFrames = counters.counters.droppedFrames;
     612            4 :     corruptedFrames = counters.counters.corruptedFrames;
     613            4 :     timestamp = counters.timestamp;
     614            4 :     return true;
     615              : }
     616              : 
     617            6 : bool MediaPipelineServerInternal::setImmediateOutput(int32_t sourceId, bool immediateOutput)
     618              : {
     619            6 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     620              : 
     621              :     bool result;
     622            6 :     auto task = [&]() { result = setImmediateOutputInternal(sourceId, immediateOutput); };
     623              : 
     624            6 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     625            6 :     return result;
     626              : }
     627              : 
     628            6 : bool MediaPipelineServerInternal::setImmediateOutputInternal(int32_t sourceId, bool immediateOutput)
     629              : {
     630            6 :     if (!m_gstPlayer)
     631              :     {
     632            1 :         RIALTO_SERVER_LOG_ERROR("Failed - Gstreamer player has not been loaded");
     633            1 :         return false;
     634              :     }
     635            5 :     auto sourceIter = std::find_if(m_attachedSources.begin(), m_attachedSources.end(),
     636            4 :                                    [sourceId](const auto &src) { return src.second == sourceId; });
     637            5 :     if (sourceIter == m_attachedSources.end())
     638              :     {
     639            1 :         RIALTO_SERVER_LOG_ERROR("Failed - Source not found");
     640            1 :         return false;
     641              :     }
     642              : 
     643            4 :     if (sourceIter->first == MediaSourceType::VIDEO)
     644              :     {
     645            4 :         m_IsLowLatencyVideoPlayer = immediateOutput;
     646              :     }
     647            0 :     else if (sourceIter->first == MediaSourceType::AUDIO)
     648              :     {
     649            0 :         m_IsLowLatencyAudioPlayer = immediateOutput;
     650              :     }
     651            4 :     return m_gstPlayer->setImmediateOutput(sourceIter->first, immediateOutput);
     652              : }
     653              : 
     654            5 : bool MediaPipelineServerInternal::setReportDecodeErrors(int32_t sourceId, bool reportDecodeErrors)
     655              : {
     656            5 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     657              : 
     658              :     bool result;
     659            5 :     auto task = [&]() { result = setReportDecodeErrorsInternal(sourceId, reportDecodeErrors); };
     660              : 
     661            5 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     662            5 :     return result;
     663              : }
     664              : 
     665            5 : bool MediaPipelineServerInternal::setReportDecodeErrorsInternal(int32_t sourceId, bool reportDecodeErrors)
     666              : {
     667            5 :     if (!m_gstPlayer)
     668              :     {
     669            1 :         RIALTO_SERVER_LOG_ERROR("Failed - Gstreamer player has not been loaded");
     670            1 :         return false;
     671              :     }
     672            4 :     auto sourceIter = std::find_if(m_attachedSources.begin(), m_attachedSources.end(),
     673            3 :                                    [sourceId](const auto &src) { return src.second == sourceId; });
     674            4 :     if (sourceIter == m_attachedSources.end())
     675              :     {
     676            1 :         RIALTO_SERVER_LOG_ERROR("Failed - Source not found");
     677            1 :         return false;
     678              :     }
     679              : 
     680            3 :     return m_gstPlayer->setReportDecodeErrors(sourceIter->first, reportDecodeErrors);
     681              : }
     682              : 
     683            4 : bool MediaPipelineServerInternal::setMetricSubscription(const std::vector<MetricType> &types)
     684              : {
     685            4 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     686              : 
     687              :     bool result;
     688            4 :     auto task = [&]() { result = setMetricSubscriptionInternal(types); };
     689              : 
     690            4 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     691            4 :     return result;
     692              : }
     693              : 
     694            4 : bool MediaPipelineServerInternal::setMetricSubscriptionInternal(const std::vector<MetricType> &types)
     695              : {
     696            4 :     std::lock_guard<std::mutex> lock{m_metricSubscriptionMutex};
     697            4 :     m_metricSubscription = std::set<MetricType>(types.begin(), types.end());
     698            4 :     RIALTO_SERVER_LOG_DEBUG("Metric subscription replaced, number of metric types: %zu", types.size());
     699            4 :     return true;
     700              : }
     701              : 
     702            5 : bool MediaPipelineServerInternal::getQueuedFrames(int32_t sourceId, uint32_t &queuedFrames)
     703              : {
     704            5 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     705              : 
     706              :     bool result;
     707            5 :     auto task = [&]() { result = getQueuedFramesInternal(sourceId, queuedFrames); };
     708              : 
     709            5 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     710            5 :     return result;
     711              : }
     712              : 
     713            5 : bool MediaPipelineServerInternal::getQueuedFramesInternal(int32_t sourceId, uint32_t &queuedFrames)
     714              : {
     715            5 :     if (!m_gstPlayer)
     716              :     {
     717            1 :         RIALTO_SERVER_LOG_ERROR("Failed - Gstreamer player has not been loaded");
     718            1 :         return false;
     719              :     }
     720            4 :     auto sourceIter = std::find_if(m_attachedSources.begin(), m_attachedSources.end(),
     721            3 :                                    [sourceId](const auto &src) { return src.second == sourceId; });
     722            4 :     if (sourceIter == m_attachedSources.end())
     723              :     {
     724            1 :         RIALTO_SERVER_LOG_ERROR("Failed - Source not found");
     725            1 :         return false;
     726              :     }
     727            3 :     return m_gstPlayer->getQueuedFrames(queuedFrames);
     728              : }
     729              : 
     730            5 : bool MediaPipelineServerInternal::getImmediateOutput(int32_t sourceId, bool &immediateOutput)
     731              : {
     732            5 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     733              : 
     734              :     bool result;
     735            5 :     auto task = [&]() { result = getImmediateOutputInternal(sourceId, immediateOutput); };
     736              : 
     737            5 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     738            5 :     return result;
     739              : }
     740              : 
     741            5 : bool MediaPipelineServerInternal::getImmediateOutputInternal(int32_t sourceId, bool &immediateOutput)
     742              : {
     743            5 :     if (!m_gstPlayer)
     744              :     {
     745            1 :         RIALTO_SERVER_LOG_ERROR("Failed - Gstreamer player has not been loaded");
     746            1 :         return false;
     747              :     }
     748            4 :     auto sourceIter = std::find_if(m_attachedSources.begin(), m_attachedSources.end(),
     749            3 :                                    [sourceId](const auto &src) { return src.second == sourceId; });
     750            4 :     if (sourceIter == m_attachedSources.end())
     751              :     {
     752            1 :         RIALTO_SERVER_LOG_ERROR("Failed - Source not found");
     753            1 :         return false;
     754              :     }
     755            3 :     return m_gstPlayer->getImmediateOutput(sourceIter->first, immediateOutput);
     756              : }
     757              : 
     758            2 : bool MediaPipelineServerInternal::setVideoWindow(uint32_t x, uint32_t y, uint32_t width, uint32_t height)
     759              : {
     760            2 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     761              : 
     762              :     bool result;
     763            2 :     auto task = [&]() { result = setVideoWindowInternal(x, y, width, height); };
     764              : 
     765            2 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     766            2 :     return result;
     767              : }
     768              : 
     769            2 : bool MediaPipelineServerInternal::setVideoWindowInternal(uint32_t x, uint32_t y, uint32_t width, uint32_t height)
     770              : {
     771            2 :     if (!m_gstPlayer)
     772              :     {
     773            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set video window - Gstreamer player has not been loaded");
     774            1 :         return false;
     775              :     }
     776              : 
     777            1 :     m_gstPlayer->setVideoGeometry(x, y, width, height);
     778            1 :     return true;
     779              : }
     780              : 
     781           13 : bool MediaPipelineServerInternal::haveData(MediaSourceStatus status, uint32_t needDataRequestId)
     782              : {
     783           13 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     784              : 
     785              :     bool result;
     786           13 :     auto task = [&]() { result = haveDataInternal(status, needDataRequestId); };
     787              : 
     788           13 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     789           13 :     return result;
     790              : }
     791              : 
     792           13 : bool MediaPipelineServerInternal::haveDataInternal(MediaSourceStatus status, uint32_t needDataRequestId)
     793              : {
     794           13 :     if (!m_gstPlayer)
     795              :     {
     796            1 :         RIALTO_SERVER_LOG_ERROR("HaveData failed - Gstreamer player has not been loaded");
     797            1 :         return false;
     798              :     }
     799              : 
     800           12 :     MediaSourceType mediaSourceType = m_activeRequests->getType(needDataRequestId);
     801           12 :     if (MediaSourceType::UNKNOWN == mediaSourceType)
     802              :     {
     803            1 :         RIALTO_SERVER_LOG_WARN("NeedData RequestID is not valid: %u", needDataRequestId);
     804            1 :         return true;
     805              :     }
     806              : 
     807           11 :     unsigned int &counter = m_noAvailableSamplesCounter[mediaSourceType];
     808           11 :     if (status != MediaSourceStatus::OK && status != MediaSourceStatus::EOS)
     809              :     {
     810              :         // Incrementing the counter allows us to track the occurrences where the status is other than OK or EOS.
     811              : 
     812            3 :         ++counter;
     813            3 :         if (status == MediaSourceStatus::NO_AVAILABLE_SAMPLES)
     814              :         {
     815            2 :             RIALTO_SERVER_LOG_DEBUG("Data request for needDataRequestId: %u. NO_AVAILABLE_SAMPLES received: %u "
     816              :                                     "consecutively for mediaSourceType: %s",
     817              :                                     needDataRequestId, counter, common::convertMediaSourceType(mediaSourceType));
     818              :         }
     819              :         else
     820              :         {
     821            1 :             RIALTO_SERVER_LOG_WARN("%s Data request for needDataRequestId: %u received with wrong status: %s",
     822              :                                    common::convertMediaSourceType(mediaSourceType), needDataRequestId, toString(status));
     823            1 :             counter = 0;
     824              :         }
     825              : 
     826            3 :         m_activeRequests->erase(needDataRequestId);
     827            3 :         scheduleNotifyNeedMediaData(mediaSourceType);
     828            3 :         return true;
     829              :     }
     830              :     else
     831              :     {
     832            8 :         RIALTO_SERVER_LOG_DEBUG("%s Data request for needDataRequestId: %u received with correct status",
     833              :                                 common::convertMediaSourceType(mediaSourceType), needDataRequestId);
     834            8 :         counter = 0;
     835              :     }
     836              : 
     837              :     try
     838              :     {
     839            8 :         const IMediaPipeline::MediaSegmentVector &kSegments = m_activeRequests->getSegments(needDataRequestId);
     840            7 :         m_gstPlayer->attachSamples(kSegments);
     841              :     }
     842            1 :     catch (const std::runtime_error &e)
     843              :     {
     844            1 :         RIALTO_SERVER_LOG_ERROR("Failed to get segments %s", e.what());
     845            1 :         m_activeRequests->erase(needDataRequestId);
     846            1 :         return false;
     847              :     }
     848              : 
     849            7 :     m_activeRequests->erase(needDataRequestId);
     850            7 :     if (status == MediaSourceStatus::EOS)
     851              :     {
     852            6 :         m_gstPlayer->setEos(mediaSourceType);
     853            6 :         m_isMediaTypeEosMap[mediaSourceType] = true;
     854              :     }
     855              : 
     856            7 :     return true;
     857              : }
     858              : 
     859           15 : bool MediaPipelineServerInternal::haveData(MediaSourceStatus status, uint32_t numFrames, uint32_t needDataRequestId)
     860              : {
     861           15 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     862              : 
     863              :     bool result;
     864           15 :     auto task = [&]() { result = haveDataInternal(status, numFrames, needDataRequestId); };
     865              : 
     866           15 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     867           15 :     return result;
     868              : }
     869              : 
     870           15 : bool MediaPipelineServerInternal::haveDataInternal(MediaSourceStatus status, uint32_t numFrames,
     871              :                                                    uint32_t needDataRequestId)
     872              : {
     873           15 :     if (!m_gstPlayer)
     874              :     {
     875            1 :         RIALTO_SERVER_LOG_ERROR("HaveData failed - Gstreamer player has not been loaded");
     876            1 :         return false;
     877              :     }
     878           14 :     MediaSourceType mediaSourceType = m_activeRequests->getType(needDataRequestId);
     879           14 :     if (MediaSourceType::UNKNOWN == mediaSourceType)
     880              :     {
     881            1 :         RIALTO_SERVER_LOG_WARN("NeedData RequestID is not valid: %u", needDataRequestId);
     882            1 :         return true;
     883              :     }
     884           13 :     const std::uint32_t kMaxNumFrames = m_activeRequests->getMaxFrames(needDataRequestId);
     885           13 :     m_activeRequests->erase(needDataRequestId);
     886              : 
     887           13 :     unsigned int &counter = m_noAvailableSamplesCounter[mediaSourceType];
     888           13 :     if (status != MediaSourceStatus::OK && status != MediaSourceStatus::EOS &&
     889              :         status != MediaSourceStatus::NO_SPACE_FOR_SAMPLES)
     890              :     {
     891              :         // Incrementing the counter allows us to track the occurrences where the status is other than OK or EOS.
     892              : 
     893            4 :         ++counter;
     894            4 :         if (status == MediaSourceStatus::NO_AVAILABLE_SAMPLES)
     895              :         {
     896            0 :             RIALTO_SERVER_LOG_DEBUG("Data request for needDataRequestId: %u. NO_AVAILABLE_SAMPLES received: %u "
     897              :                                     "consecutively for mediaSourceType: %s",
     898              :                                     needDataRequestId, counter, common::convertMediaSourceType(mediaSourceType));
     899              :         }
     900              :         else
     901              :         {
     902            4 :             RIALTO_SERVER_LOG_WARN("%s Data request for needDataRequestId: %u received with wrong status",
     903              :                                    common::convertMediaSourceType(mediaSourceType), needDataRequestId);
     904            4 :             counter = 0;
     905              :         }
     906            4 :         scheduleNotifyNeedMediaData(mediaSourceType);
     907            4 :         return true;
     908              :     }
     909              :     else
     910              :     {
     911            9 :         RIALTO_SERVER_LOG_DEBUG("%s Data request for needDataRequestId: %u received with correct status",
     912              :                                 common::convertMediaSourceType(mediaSourceType), needDataRequestId);
     913            9 :         counter = 0;
     914              :     }
     915              : 
     916            9 :     uint8_t *buffer = m_shmBuffer->getBuffer();
     917            9 :     if (!buffer)
     918              :     {
     919            1 :         RIALTO_SERVER_LOG_ERROR("No buffer available");
     920            1 :         notifyPlaybackState(PlaybackState::FAILURE);
     921            1 :         return false;
     922              :     }
     923              : 
     924            8 :     std::uint32_t regionOffset = 0;
     925              :     try
     926              :     {
     927              :         regionOffset =
     928            8 :             m_shmBuffer->getDataOffset(ISharedMemoryBuffer::MediaPlaybackType::GENERIC, m_sessionId, mediaSourceType);
     929              :     }
     930            1 :     catch (const std::runtime_error &e)
     931              :     {
     932            1 :         RIALTO_SERVER_LOG_ERROR("Failed to get region's buffer offset, reason: %s", e.what());
     933            1 :         notifyPlaybackState(PlaybackState::FAILURE);
     934            1 :         return false;
     935              :     }
     936              : 
     937            7 :     if (0 != numFrames)
     938              :     {
     939            6 :         const bool kIsBufferFull = kMaxNumFrames == numFrames || status == MediaSourceStatus::NO_SPACE_FOR_SAMPLES ||
     940              :                                    status == MediaSourceStatus::EOS;
     941              :         std::shared_ptr<IDataReader> dataReader =
     942            6 :             m_dataReaderFactory->createDataReader(mediaSourceType, buffer, regionOffset, numFrames, kIsBufferFull);
     943            6 :         if (!dataReader)
     944              :         {
     945            1 :             RIALTO_SERVER_LOG_ERROR("Metadata version not supported for %s request id: %u",
     946              :                                     common::convertMediaSourceType(mediaSourceType), needDataRequestId);
     947            1 :             notifyPlaybackState(PlaybackState::FAILURE);
     948            1 :             return false;
     949              :         }
     950            5 :         m_gstPlayer->attachSamples(dataReader);
     951            6 :     }
     952            6 :     if (status == MediaSourceStatus::EOS)
     953              :     {
     954            2 :         m_gstPlayer->setEos(mediaSourceType);
     955            2 :         m_isMediaTypeEosMap[mediaSourceType] = true;
     956              :     }
     957              : 
     958            6 :     return true;
     959              : }
     960              : 
     961            2 : void MediaPipelineServerInternal::ping(std::unique_ptr<IHeartbeatHandler> &&heartbeatHandler)
     962              : {
     963            2 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     964              : 
     965            2 :     auto task = [&]() { pingInternal(std::move(heartbeatHandler)); };
     966            2 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     967              : }
     968              : 
     969            2 : void MediaPipelineServerInternal::pingInternal(std::unique_ptr<IHeartbeatHandler> &&heartbeatHandler)
     970              : {
     971            2 :     if (!m_gstPlayer)
     972              :     {
     973              :         // No need to check GstPlayer worker thread, we reached this function, so main thread is working fine.
     974            1 :         heartbeatHandler.reset();
     975            1 :         return;
     976              :     }
     977              :     // Check GstPlayer worker thread
     978            1 :     m_gstPlayer->ping(std::move(heartbeatHandler));
     979              : }
     980              : 
     981            2 : bool MediaPipelineServerInternal::renderFrame()
     982              : {
     983            2 :     RIALTO_SERVER_LOG_DEBUG("entry:");
     984              : 
     985              :     bool result;
     986            2 :     auto task = [&]() { result = renderFrameInternal(); };
     987              : 
     988            2 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
     989            2 :     return result;
     990              : }
     991              : 
     992            2 : bool MediaPipelineServerInternal::renderFrameInternal()
     993              : {
     994            2 :     if (!m_gstPlayer)
     995              :     {
     996            1 :         RIALTO_SERVER_LOG_ERROR("renderFrame failed - Gstreamer player has not been loaded");
     997            1 :         return false;
     998              :     }
     999              : 
    1000            1 :     m_gstPlayer->renderFrame();
    1001            1 :     return true;
    1002              : }
    1003              : 
    1004            2 : bool MediaPipelineServerInternal::setVolume(double targetVolume, uint32_t volumeDuration, EaseType easeType)
    1005              : {
    1006            2 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1007              : 
    1008              :     bool result;
    1009            2 :     auto task = [&]() { result = setVolumeInternal(targetVolume, volumeDuration, easeType); };
    1010              : 
    1011            2 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1012            2 :     return result;
    1013              : }
    1014              : 
    1015            2 : bool MediaPipelineServerInternal::setVolumeInternal(double targetVolume, uint32_t volumeDuration, EaseType easeType)
    1016              : {
    1017            2 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1018              : 
    1019            2 :     if (!m_gstPlayer)
    1020              :     {
    1021            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set volume - Gstreamer player has not been loaded");
    1022            1 :         return false;
    1023              :     }
    1024            1 :     m_gstPlayer->setVolume(targetVolume, volumeDuration, easeType);
    1025            1 :     return true;
    1026              : }
    1027              : 
    1028            3 : bool MediaPipelineServerInternal::getVolume(double &currentVolume)
    1029              : {
    1030            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1031              : 
    1032              :     bool result;
    1033            3 :     auto task = [&]() { result = getVolumeInternal(currentVolume); };
    1034              : 
    1035            3 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1036            3 :     return result;
    1037              : }
    1038              : 
    1039            3 : bool MediaPipelineServerInternal::getVolumeInternal(double &currentVolume)
    1040              : {
    1041            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1042              : 
    1043            3 :     if (!m_gstPlayer)
    1044              :     {
    1045            1 :         RIALTO_SERVER_LOG_ERROR("Failed to get volume - Gstreamer player has not been loaded");
    1046            1 :         return false;
    1047              :     }
    1048            2 :     return m_gstPlayer->getVolume(currentVolume);
    1049              : }
    1050              : 
    1051            3 : bool MediaPipelineServerInternal::setMute(std::int32_t sourceId, bool mute)
    1052              : {
    1053            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1054              : 
    1055              :     bool result;
    1056            3 :     auto task = [&]() { result = setMuteInternal(sourceId, mute); };
    1057              : 
    1058            3 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1059            3 :     return result;
    1060              : }
    1061              : 
    1062            3 : bool MediaPipelineServerInternal::setMuteInternal(std::int32_t sourceId, bool mute)
    1063              : {
    1064            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1065              : 
    1066            3 :     if (!m_gstPlayer)
    1067              :     {
    1068            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set mute - Gstreamer player has not been loaded");
    1069            1 :         return false;
    1070              :     }
    1071              : 
    1072            2 :     auto sourceIter = std::find_if(m_attachedSources.begin(), m_attachedSources.end(),
    1073            1 :                                    [sourceId](const auto &src) { return src.second == sourceId; });
    1074            2 :     if (sourceIter == m_attachedSources.end())
    1075              :     {
    1076            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set mute - Source with id: %d not found", sourceId);
    1077            1 :         return false;
    1078              :     }
    1079              : 
    1080            1 :     m_gstPlayer->setMute(sourceIter->first, mute);
    1081              : 
    1082            1 :     return true;
    1083              : }
    1084              : 
    1085            4 : bool MediaPipelineServerInternal::getMute(std::int32_t sourceId, bool &mute)
    1086              : {
    1087            4 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1088              : 
    1089              :     bool result;
    1090            4 :     auto task = [&]() { result = getMuteInternal(sourceId, mute); };
    1091              : 
    1092            4 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1093            4 :     return result;
    1094              : }
    1095              : 
    1096            4 : bool MediaPipelineServerInternal::getMuteInternal(std::int32_t sourceId, bool &mute)
    1097              : {
    1098            4 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1099              : 
    1100            4 :     if (!m_gstPlayer)
    1101              :     {
    1102            1 :         RIALTO_SERVER_LOG_ERROR("Failed to get mute - Gstreamer player has not been loaded");
    1103            1 :         return false;
    1104              :     }
    1105              : 
    1106            3 :     auto sourceIter = std::find_if(m_attachedSources.begin(), m_attachedSources.end(),
    1107            2 :                                    [sourceId](const auto &src) { return src.second == sourceId; });
    1108            3 :     if (sourceIter == m_attachedSources.end())
    1109              :     {
    1110            1 :         RIALTO_SERVER_LOG_ERROR("Failed to get mute - Source with id: %d not found", sourceId);
    1111            1 :         return false;
    1112              :     }
    1113              : 
    1114            2 :     return m_gstPlayer->getMute(sourceIter->first, mute);
    1115              : }
    1116              : 
    1117            2 : bool MediaPipelineServerInternal::setTextTrackIdentifier(const std::string &textTrackIdentifier)
    1118              : {
    1119            2 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1120              : 
    1121              :     bool result;
    1122            2 :     auto task = [&]() { result = setTextTrackIdentifierInternal(textTrackIdentifier); };
    1123              : 
    1124            2 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1125            2 :     return result;
    1126              : }
    1127              : 
    1128            2 : bool MediaPipelineServerInternal::setTextTrackIdentifierInternal(const std::string &textTrackIdentifier)
    1129              : {
    1130            2 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1131              : 
    1132            2 :     if (!m_gstPlayer)
    1133              :     {
    1134            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set text track identifier - Gstreamer player has not been loaded");
    1135            1 :         return false;
    1136              :     }
    1137              : 
    1138            1 :     m_gstPlayer->setTextTrackIdentifier(textTrackIdentifier);
    1139              : 
    1140            1 :     return true;
    1141              : }
    1142              : 
    1143            3 : bool MediaPipelineServerInternal::getTextTrackIdentifier(std::string &textTrackIdentifier)
    1144              : {
    1145            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1146              : 
    1147              :     bool result;
    1148            3 :     auto task = [&]() { result = getTextTrackIdentifierInternal(textTrackIdentifier); };
    1149              : 
    1150            3 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1151            3 :     return result;
    1152              : }
    1153              : 
    1154            3 : bool MediaPipelineServerInternal::getTextTrackIdentifierInternal(std::string &textTrackIdentifier)
    1155              : {
    1156            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1157              : 
    1158            3 :     if (!m_gstPlayer)
    1159              :     {
    1160            1 :         RIALTO_SERVER_LOG_ERROR("Failed to get mute - Gstreamer player has not been loaded");
    1161            1 :         return false;
    1162              :     }
    1163              : 
    1164            2 :     return m_gstPlayer->getTextTrackIdentifier(textTrackIdentifier);
    1165              : }
    1166              : 
    1167            4 : bool MediaPipelineServerInternal::flush(int32_t sourceId, bool resetTime, bool &async)
    1168              : {
    1169            4 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1170              : 
    1171              :     bool result;
    1172            4 :     auto task = [&]() { result = flushInternal(sourceId, resetTime, async); };
    1173              : 
    1174            4 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1175            4 :     return result;
    1176              : }
    1177              : 
    1178            4 : bool MediaPipelineServerInternal::setLowLatency(bool lowLatency)
    1179              : {
    1180            4 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1181              : 
    1182              :     bool result;
    1183            4 :     auto task = [&]() { result = setLowLatencyInternal(lowLatency); };
    1184              : 
    1185            4 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1186            4 :     return result;
    1187              : }
    1188              : 
    1189            4 : bool MediaPipelineServerInternal::setLowLatencyInternal(bool lowLatency)
    1190              : {
    1191            4 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1192              : 
    1193            4 :     if (!m_gstPlayer)
    1194              :     {
    1195            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set low latency - Gstreamer player has not been loaded");
    1196            1 :         return false;
    1197              :     }
    1198            3 :     m_IsLowLatencyAudioPlayer = lowLatency;
    1199              : 
    1200            3 :     return m_gstPlayer->setLowLatency(lowLatency);
    1201              : }
    1202              : 
    1203            3 : bool MediaPipelineServerInternal::setSync(bool sync)
    1204              : {
    1205            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1206              : 
    1207              :     bool result;
    1208            3 :     auto task = [&]() { result = setSyncInternal(sync); };
    1209              : 
    1210            3 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1211            3 :     return result;
    1212              : }
    1213              : 
    1214            3 : bool MediaPipelineServerInternal::setSyncInternal(bool sync)
    1215              : {
    1216            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1217              : 
    1218            3 :     if (!m_gstPlayer)
    1219              :     {
    1220            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set sync - Gstreamer player has not been loaded");
    1221            1 :         return false;
    1222              :     }
    1223            2 :     return m_gstPlayer->setSync(sync);
    1224              : }
    1225              : 
    1226            3 : bool MediaPipelineServerInternal::getSync(bool &sync)
    1227              : {
    1228            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1229              : 
    1230              :     bool result;
    1231            3 :     auto task = [&]() { result = getSyncInternal(sync); };
    1232              : 
    1233            3 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1234            3 :     return result;
    1235              : }
    1236              : 
    1237            3 : bool MediaPipelineServerInternal::getSyncInternal(bool &sync)
    1238              : {
    1239            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1240              : 
    1241            3 :     if (!m_gstPlayer)
    1242              :     {
    1243            1 :         RIALTO_SERVER_LOG_ERROR("Failed to get sync - Gstreamer player has not been loaded");
    1244            1 :         return false;
    1245              :     }
    1246            2 :     return m_gstPlayer->getSync(sync);
    1247              : }
    1248              : 
    1249            3 : bool MediaPipelineServerInternal::setSyncOff(bool syncOff)
    1250              : {
    1251            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1252              : 
    1253              :     bool result;
    1254            3 :     auto task = [&]() { result = setSyncOffInternal(syncOff); };
    1255              : 
    1256            3 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1257            3 :     return result;
    1258              : }
    1259              : 
    1260            3 : bool MediaPipelineServerInternal::setSyncOffInternal(bool syncOff)
    1261              : {
    1262            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1263              : 
    1264            3 :     if (!m_gstPlayer)
    1265              :     {
    1266            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set sync off - Gstreamer player has not been loaded");
    1267            1 :         return false;
    1268              :     }
    1269            2 :     return m_gstPlayer->setSyncOff(syncOff);
    1270              : }
    1271              : 
    1272            4 : bool MediaPipelineServerInternal::setStreamSyncMode(int32_t sourceId, int32_t streamSyncMode)
    1273              : {
    1274            4 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1275              : 
    1276              :     bool result;
    1277            4 :     auto task = [&]() { result = setStreamSyncModeInternal(sourceId, streamSyncMode); };
    1278              : 
    1279            4 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1280            4 :     return result;
    1281              : }
    1282              : 
    1283            4 : bool MediaPipelineServerInternal::setStreamSyncModeInternal(int32_t sourceId, int32_t streamSyncMode)
    1284              : {
    1285            4 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1286              : 
    1287            4 :     if (!m_gstPlayer)
    1288              :     {
    1289            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set stream sync mode - Gstreamer player has not been loaded");
    1290            1 :         return false;
    1291              :     }
    1292              : 
    1293            3 :     auto sourceIter = std::find_if(m_attachedSources.begin(), m_attachedSources.end(),
    1294            2 :                                    [sourceId](const auto &src) { return src.second == sourceId; });
    1295            3 :     if (sourceIter == m_attachedSources.end())
    1296              :     {
    1297            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set stream sync mode - Source with id: %d not found", sourceId);
    1298            1 :         return false;
    1299              :     }
    1300              : 
    1301            2 :     return m_gstPlayer->setStreamSyncMode(sourceIter->first, streamSyncMode);
    1302              : }
    1303              : 
    1304            3 : bool MediaPipelineServerInternal::getStreamSyncMode(int32_t &streamSyncMode)
    1305              : {
    1306            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1307              : 
    1308              :     bool result;
    1309            3 :     auto task = [&]() { result = getStreamSyncModeInternal(streamSyncMode); };
    1310              : 
    1311            3 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1312            3 :     return result;
    1313              : }
    1314              : 
    1315            3 : bool MediaPipelineServerInternal::getStreamSyncModeInternal(int32_t &streamSyncMode)
    1316              : {
    1317            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1318              : 
    1319            3 :     if (!m_gstPlayer)
    1320              :     {
    1321            1 :         RIALTO_SERVER_LOG_ERROR("Failed to get stream sync mode - Gstreamer player has not been loaded");
    1322            1 :         return false;
    1323              :     }
    1324            2 :     return m_gstPlayer->getStreamSyncMode(streamSyncMode);
    1325              : }
    1326              : 
    1327            4 : bool MediaPipelineServerInternal::flushInternal(int32_t sourceId, bool resetTime, bool &async)
    1328              : {
    1329            4 :     if (!m_gstPlayer)
    1330              :     {
    1331            1 :         RIALTO_SERVER_LOG_ERROR("Failed to flush - Gstreamer player has not been loaded");
    1332            1 :         return false;
    1333              :     }
    1334            3 :     auto sourceIter = std::find_if(m_attachedSources.begin(), m_attachedSources.end(),
    1335            2 :                                    [sourceId](const auto &src) { return src.second == sourceId; });
    1336            3 :     if (sourceIter == m_attachedSources.end())
    1337              :     {
    1338            1 :         RIALTO_SERVER_LOG_ERROR("Failed to flush - Source with id: %d not found", sourceId);
    1339            1 :         return false;
    1340              :     }
    1341              : 
    1342            2 :     m_gstPlayer->flush(sourceIter->first, resetTime, async);
    1343              : 
    1344            2 :     m_needMediaDataTimers.erase(sourceIter->first);
    1345              : 
    1346              :     // Reset Eos on flush
    1347            2 :     auto it = m_isMediaTypeEosMap.find(sourceIter->first);
    1348            2 :     if (it != m_isMediaTypeEosMap.end() && it->second)
    1349              :     {
    1350            1 :         it->second = false;
    1351              :     }
    1352              : 
    1353            2 :     return true;
    1354              : }
    1355              : 
    1356            3 : bool MediaPipelineServerInternal::setSourcePosition(int32_t sourceId, int64_t position, bool resetTime,
    1357              :                                                     double appliedRate, uint64_t stopPosition)
    1358              : {
    1359            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1360              : 
    1361              :     bool result;
    1362            3 :     auto task = [&]() { result = setSourcePositionInternal(sourceId, position, resetTime, appliedRate, stopPosition); };
    1363              : 
    1364            3 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1365            3 :     return result;
    1366              : }
    1367              : 
    1368            3 : bool MediaPipelineServerInternal::setSourcePositionInternal(int32_t sourceId, int64_t position, bool resetTime,
    1369              :                                                             double appliedRate, uint64_t stopPosition)
    1370              : {
    1371            3 :     if (!m_gstPlayer)
    1372              :     {
    1373            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set source position - Gstreamer player has not been loaded");
    1374            1 :         return false;
    1375              :     }
    1376            2 :     auto sourceIter = std::find_if(m_attachedSources.begin(), m_attachedSources.end(),
    1377            1 :                                    [sourceId](const auto &src) { return src.second == sourceId; });
    1378            2 :     if (sourceIter == m_attachedSources.end())
    1379              :     {
    1380            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set source position - Source with id: %d not found", sourceId);
    1381            1 :         return false;
    1382              :     }
    1383              : 
    1384            1 :     m_gstPlayer->setSourcePosition(sourceIter->first, position, resetTime, appliedRate, stopPosition);
    1385              : 
    1386              :     // Reset Eos on seek
    1387            1 :     auto it = m_isMediaTypeEosMap.find(sourceIter->first);
    1388            1 :     if (it != m_isMediaTypeEosMap.end() && it->second)
    1389              :     {
    1390            0 :         it->second = false;
    1391              :     }
    1392              : 
    1393            1 :     return true;
    1394              : }
    1395              : 
    1396            3 : bool MediaPipelineServerInternal::setSubtitleOffset(int32_t sourceId, int64_t position)
    1397              : {
    1398            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1399              : 
    1400              :     bool result;
    1401            3 :     auto task = [&]() { result = setSubtitleOffsetInternal(sourceId, position); };
    1402              : 
    1403            3 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1404            3 :     return result;
    1405              : }
    1406              : 
    1407            3 : bool MediaPipelineServerInternal::setSubtitleOffsetInternal(int32_t sourceId, int64_t position)
    1408              : {
    1409            3 :     if (!m_gstPlayer)
    1410              :     {
    1411            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set subtitle offset - Gstreamer player has not been loaded");
    1412            1 :         return false;
    1413              :     }
    1414            2 :     auto sourceIter = std::find_if(m_attachedSources.begin(), m_attachedSources.end(),
    1415            1 :                                    [sourceId](const auto &src) { return src.second == sourceId; });
    1416            2 :     if (sourceIter == m_attachedSources.end())
    1417              :     {
    1418            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set subtitle offset - Source with id: %d not found", sourceId);
    1419            1 :         return false;
    1420              :     }
    1421              : 
    1422            1 :     m_gstPlayer->setSubtitleOffset(position);
    1423            1 :     return true;
    1424              : }
    1425              : 
    1426            2 : bool MediaPipelineServerInternal::processAudioGap(int64_t position, uint32_t duration, int64_t discontinuityGap,
    1427              :                                                   bool audioAac)
    1428              : {
    1429            2 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1430              : 
    1431              :     bool result;
    1432            2 :     auto task = [&]() { result = processAudioGapInternal(position, duration, discontinuityGap, audioAac); };
    1433              : 
    1434            2 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1435            2 :     return result;
    1436              : }
    1437              : 
    1438            2 : bool MediaPipelineServerInternal::processAudioGapInternal(int64_t position, uint32_t duration, int64_t discontinuityGap,
    1439              :                                                           bool audioAac)
    1440              : {
    1441            2 :     if (!m_gstPlayer)
    1442              :     {
    1443            1 :         RIALTO_SERVER_LOG_ERROR("Failed to process audio gap - Gstreamer player has not been loaded");
    1444            1 :         return false;
    1445              :     }
    1446            1 :     m_gstPlayer->processAudioGap(position, duration, discontinuityGap, audioAac);
    1447            1 :     return true;
    1448              : }
    1449              : 
    1450            2 : bool MediaPipelineServerInternal::setBufferingLimit(uint32_t limitBufferingMs)
    1451              : {
    1452              :     bool result;
    1453            2 :     auto task = [&]() { result = setBufferingLimitInternal(limitBufferingMs); };
    1454              : 
    1455            2 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1456            2 :     return result;
    1457              : }
    1458              : 
    1459            2 : bool MediaPipelineServerInternal::setBufferingLimitInternal(uint32_t limitBufferingMs)
    1460              : {
    1461            2 :     if (!m_gstPlayer)
    1462              :     {
    1463            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set buffering limit - Gstreamer player has not been loaded");
    1464            1 :         return false;
    1465              :     }
    1466            1 :     m_gstPlayer->setBufferingLimit(limitBufferingMs);
    1467            1 :     return true;
    1468              : }
    1469              : 
    1470            3 : bool MediaPipelineServerInternal::getBufferingLimit(uint32_t &limitBufferingMs)
    1471              : {
    1472              :     bool result;
    1473            3 :     auto task = [&]() { result = getBufferingLimitInternal(limitBufferingMs); };
    1474              : 
    1475            3 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1476            3 :     return result;
    1477              : }
    1478              : 
    1479            3 : bool MediaPipelineServerInternal::getBufferingLimitInternal(uint32_t &limitBufferingMs)
    1480              : {
    1481            3 :     if (!m_gstPlayer)
    1482              :     {
    1483            1 :         RIALTO_SERVER_LOG_ERROR("Failed to get buffering limit - Gstreamer player has not been loaded");
    1484            1 :         return false;
    1485              :     }
    1486            2 :     return m_gstPlayer->getBufferingLimit(limitBufferingMs);
    1487              : }
    1488              : 
    1489            2 : bool MediaPipelineServerInternal::setUseBuffering(bool useBuffering)
    1490              : {
    1491              :     bool result;
    1492            2 :     auto task = [&]() { result = setUseBufferingInternal(useBuffering); };
    1493              : 
    1494            2 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1495            2 :     return result;
    1496              : }
    1497              : 
    1498            2 : bool MediaPipelineServerInternal::setUseBufferingInternal(bool useBuffering)
    1499              : {
    1500            2 :     if (!m_gstPlayer)
    1501              :     {
    1502            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set use buffering - Gstreamer player has not been loaded");
    1503            1 :         return false;
    1504              :     }
    1505            1 :     m_gstPlayer->setUseBuffering(useBuffering);
    1506            1 :     return true;
    1507              : }
    1508              : 
    1509            3 : bool MediaPipelineServerInternal::getUseBuffering(bool &useBuffering)
    1510              : {
    1511              :     bool result;
    1512            3 :     auto task = [&]() { result = getUseBufferingInternal(useBuffering); };
    1513              : 
    1514            3 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1515            3 :     return result;
    1516              : }
    1517              : 
    1518            3 : bool MediaPipelineServerInternal::getUseBufferingInternal(bool &useBuffering)
    1519              : {
    1520            3 :     if (!m_gstPlayer)
    1521              :     {
    1522            1 :         RIALTO_SERVER_LOG_ERROR("Failed to get use buffering - Gstreamer player has not been loaded");
    1523            1 :         return false;
    1524              :     }
    1525            2 :     return m_gstPlayer->getUseBuffering(useBuffering);
    1526              : }
    1527              : 
    1528            2 : bool MediaPipelineServerInternal::switchSource(const std::unique_ptr<MediaSource> &source)
    1529              : {
    1530              :     bool result;
    1531            2 :     auto task = [&]() { result = switchSourceInternal(source); };
    1532              : 
    1533            2 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1534            2 :     return result;
    1535              : }
    1536              : 
    1537            2 : bool MediaPipelineServerInternal::switchSourceInternal(const std::unique_ptr<MediaSource> &source)
    1538              : {
    1539            2 :     if (!m_gstPlayer)
    1540              :     {
    1541            1 :         RIALTO_SERVER_LOG_ERROR("Failed to switch source - Gstreamer player has not been loaded");
    1542            1 :         return false;
    1543              :     }
    1544            1 :     m_gstPlayer->switchSource(source);
    1545            1 :     return true;
    1546              : }
    1547              : 
    1548            3 : AddSegmentStatus MediaPipelineServerInternal::addSegment(uint32_t needDataRequestId,
    1549              :                                                          const std::unique_ptr<MediaSegment> &mediaSegment)
    1550              : {
    1551            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1552              : 
    1553            3 :     AddSegmentStatus status{AddSegmentStatus::ERROR};
    1554            3 :     auto task = [&]() { status = addSegmentInternal(needDataRequestId, mediaSegment); };
    1555              : 
    1556            3 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1557            3 :     return status;
    1558              : }
    1559              : 
    1560            3 : AddSegmentStatus MediaPipelineServerInternal::addSegmentInternal(uint32_t needDataRequestId,
    1561              :                                                                  const std::unique_ptr<MediaSegment> &mediaSegment)
    1562              : {
    1563            3 :     AddSegmentStatus status = m_activeRequests->addSegment(needDataRequestId, mediaSegment);
    1564            3 :     if (status != AddSegmentStatus::OK)
    1565              :     {
    1566            2 :         RIALTO_SERVER_LOG_ERROR("Failed to add segment for request id: %u", needDataRequestId);
    1567              :     }
    1568              : 
    1569            3 :     return status;
    1570              : }
    1571              : 
    1572            0 : std::weak_ptr<IMediaPipelineClient> MediaPipelineServerInternal::getClient()
    1573              : {
    1574            0 :     return m_mediaPipelineClient;
    1575              : }
    1576              : 
    1577           10 : void MediaPipelineServerInternal::notifyPlaybackState(PlaybackState state)
    1578              : {
    1579           10 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1580              : 
    1581           10 :     auto task = [&, state]()
    1582              :     {
    1583           10 :         m_currentPlaybackState = state;
    1584           10 :         if (m_mediaPipelineClient)
    1585              :         {
    1586           10 :             m_mediaPipelineClient->notifyPlaybackState(state);
    1587              :         }
    1588           20 :     };
    1589              : 
    1590           10 :     m_mainThread->enqueueTask(m_mainThreadClientId, task);
    1591              : }
    1592              : 
    1593            7 : bool MediaPipelineServerInternal::notifyNeedMediaData(MediaSourceType mediaSourceType)
    1594              : {
    1595            7 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1596              : 
    1597              :     // the task won't execute for a disconnected client therefore
    1598              :     // set a default value of true which will help to stop any further
    1599              :     // action being taken
    1600            7 :     bool result{true};
    1601              : 
    1602            7 :     auto task = [&]()
    1603              :     {
    1604            7 :         result = notifyNeedMediaDataInternal(mediaSourceType);
    1605            7 :         if (result)
    1606              :         {
    1607            4 :             m_needDataDelayCalculator.decreaseNeedMediaDataDelay(mediaSourceType);
    1608              :         }
    1609           14 :     };
    1610              : 
    1611            7 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1612              : 
    1613            7 :     return result;
    1614              : }
    1615              : 
    1616            8 : bool MediaPipelineServerInternal::notifyNeedMediaDataInternal(MediaSourceType mediaSourceType)
    1617              : {
    1618            8 :     m_needMediaDataTimers.erase(mediaSourceType);
    1619            8 :     m_shmBuffer->clearData(ISharedMemoryBuffer::MediaPlaybackType::GENERIC, m_sessionId, mediaSourceType);
    1620            8 :     const auto kSourceIter = m_attachedSources.find(mediaSourceType);
    1621              : 
    1622            8 :     if (m_attachedSources.cend() == kSourceIter)
    1623              :     {
    1624            1 :         RIALTO_SERVER_LOG_WARN("NeedMediaData event sending failed for %s - sourceId not found",
    1625              :                                common::convertMediaSourceType(mediaSourceType));
    1626            1 :         return false;
    1627              :     }
    1628            7 :     auto it = m_isMediaTypeEosMap.find(mediaSourceType);
    1629            7 :     if (it != m_isMediaTypeEosMap.end() && it->second)
    1630              :     {
    1631            2 :         RIALTO_SERVER_LOG_INFO("EOS, NeedMediaData not needed for %s", common::convertMediaSourceType(mediaSourceType));
    1632            2 :         return false;
    1633              :     }
    1634            5 :     NeedMediaData event{m_mediaPipelineClient, *m_activeRequests,   *m_shmBuffer,          m_sessionId,
    1635           10 :                         mediaSourceType,       kSourceIter->second, m_currentPlaybackState};
    1636            5 :     if (!event.send())
    1637              :     {
    1638            0 :         RIALTO_SERVER_LOG_WARN("NeedMediaData event sending failed for %s",
    1639              :                                common::convertMediaSourceType(mediaSourceType));
    1640            0 :         return false;
    1641              :     }
    1642              : 
    1643            5 :     RIALTO_SERVER_LOG_DEBUG("%s NeedMediaData sent.", common::convertMediaSourceType(mediaSourceType));
    1644              : 
    1645            5 :     return true;
    1646              : }
    1647              : 
    1648            3 : bool MediaPipelineServerInternal::notifyNeedMediaDataWithDelay(MediaSourceType mediaSourceType)
    1649              : {
    1650            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1651              : 
    1652              :     // the task won't execute for a disconnected client therefore
    1653              :     // set a default value of true which will help to stop any further
    1654              :     // action being taken
    1655            3 :     bool result{true};
    1656              : 
    1657            3 :     auto task = [&]() { result = notifyNeedMediaDataWithDelayInternal(mediaSourceType); };
    1658              : 
    1659            3 :     m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
    1660              : 
    1661            3 :     return result;
    1662              : }
    1663              : 
    1664            3 : bool MediaPipelineServerInternal::notifyNeedMediaDataWithDelayInternal(MediaSourceType mediaSourceType)
    1665              : {
    1666            3 :     m_needMediaDataTimers.erase(mediaSourceType);
    1667            3 :     m_shmBuffer->clearData(ISharedMemoryBuffer::MediaPlaybackType::GENERIC, m_sessionId, mediaSourceType);
    1668            3 :     const auto kSourceIter = m_attachedSources.find(mediaSourceType);
    1669              : 
    1670            3 :     if (m_attachedSources.cend() == kSourceIter)
    1671              :     {
    1672            1 :         RIALTO_SERVER_LOG_WARN("NeedMediaData event sending failed for %s - sourceId not found",
    1673              :                                common::convertMediaSourceType(mediaSourceType));
    1674            1 :         return false;
    1675              :     }
    1676            2 :     auto it = m_isMediaTypeEosMap.find(mediaSourceType);
    1677            2 :     if (it != m_isMediaTypeEosMap.end() && it->second)
    1678              :     {
    1679            1 :         RIALTO_SERVER_LOG_INFO("EOS, NeedMediaData not needed for %s", common::convertMediaSourceType(mediaSourceType));
    1680            1 :         return false;
    1681              :     }
    1682              : 
    1683            1 :     scheduleNotifyNeedMediaData(mediaSourceType);
    1684              : 
    1685            1 :     RIALTO_SERVER_LOG_DEBUG("%s NeedMediaData scheduled.", common::convertMediaSourceType(mediaSourceType));
    1686              : 
    1687            1 :     return true;
    1688              : }
    1689              : 
    1690            1 : void MediaPipelineServerInternal::notifyPosition(std::int64_t position)
    1691              : {
    1692            1 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1693              : 
    1694            1 :     auto task = [&, position]()
    1695              :     {
    1696            1 :         if (m_mediaPipelineClient)
    1697              :         {
    1698            1 :             m_mediaPipelineClient->notifyPosition(position);
    1699              :         }
    1700            2 :     };
    1701              : 
    1702            1 :     m_mainThread->enqueueTask(m_mainThreadClientId, task);
    1703              : }
    1704              : 
    1705          131 : void MediaPipelineServerInternal::notifyNetworkState(NetworkState state)
    1706              : {
    1707          131 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1708              : 
    1709          131 :     auto task = [&, state]()
    1710              :     {
    1711          131 :         if (m_mediaPipelineClient)
    1712              :         {
    1713          131 :             m_mediaPipelineClient->notifyNetworkState(state);
    1714              :         }
    1715          262 :     };
    1716              : 
    1717          131 :     m_mainThread->enqueueTask(m_mainThreadClientId, task);
    1718              : }
    1719              : 
    1720            1 : void MediaPipelineServerInternal::clearActiveRequestsCache()
    1721              : {
    1722            1 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1723              : 
    1724            1 :     auto task = [&]() { m_activeRequests->clear(); };
    1725              : 
    1726            1 :     m_mainThread->enqueueTask(m_mainThreadClientId, task);
    1727              : }
    1728              : 
    1729            1 : void MediaPipelineServerInternal::invalidateActiveRequests(const MediaSourceType &type)
    1730              : {
    1731            1 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1732              : 
    1733            1 :     auto task = [&, type]() { m_activeRequests->erase(type); };
    1734              : 
    1735            1 :     m_mainThread->enqueueTask(m_mainThreadClientId, task);
    1736              : }
    1737              : 
    1738            2 : void MediaPipelineServerInternal::notifyQos(MediaSourceType mediaSourceType, const QosInfo &qosInfo)
    1739              : {
    1740            2 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1741              : 
    1742            2 :     auto task = [&, mediaSourceType, qosInfo]()
    1743              :     {
    1744            2 :         if (m_mediaPipelineClient)
    1745              :         {
    1746            2 :             const auto kSourceIter = m_attachedSources.find(mediaSourceType);
    1747            2 :             if (m_attachedSources.cend() == kSourceIter)
    1748              :             {
    1749            1 :                 RIALTO_SERVER_LOG_WARN("Qos notification failed - sourceId not found for %s",
    1750              :                                        common::convertMediaSourceType(mediaSourceType));
    1751            1 :                 return;
    1752              :             }
    1753            1 :             m_mediaPipelineClient->notifyQos(kSourceIter->second, qosInfo);
    1754              :         }
    1755            2 :     };
    1756              : 
    1757            2 :     m_mainThread->enqueueTask(m_mainThreadClientId, task);
    1758              : }
    1759              : 
    1760           12 : void MediaPipelineServerInternal::notifyMetric(const MetricEvent &metric)
    1761              : {
    1762              :     {
    1763           12 :         std::lock_guard<std::mutex> lock{m_metricSubscriptionMutex};
    1764           12 :         if (m_metricSubscription.find(metric.type) == m_metricSubscription.end())
    1765              :         {
    1766            2 :             return;
    1767              :         }
    1768           12 :     }
    1769              : 
    1770           10 :     auto task = [&, metric]()
    1771              :     {
    1772              :         {
    1773           10 :             std::lock_guard<std::mutex> lock{m_metricSubscriptionMutex};
    1774           10 :             if (m_metricSubscription.find(metric.type) == m_metricSubscription.end())
    1775              :             {
    1776            1 :                 return;
    1777              :             }
    1778           10 :         }
    1779              : 
    1780            9 :         if (m_mediaPipelineClient)
    1781              :         {
    1782            9 :             m_mediaPipelineClient->notifyMetric(metric);
    1783              :         }
    1784              :         else
    1785              :         {
    1786            0 :             RIALTO_SERVER_LOG_WARN("Dropping playback metric event for sourceId: %d - pipeline client is unavailable",
    1787              :                                    metric.sourceId);
    1788              :         }
    1789           10 :     };
    1790              : 
    1791           10 :     m_mainThread->enqueueTask(m_mainThreadClientId, task);
    1792              : }
    1793              : 
    1794            0 : void MediaPipelineServerInternal::notifyBufferUnderflow(MediaSourceType mediaSourceType)
    1795              : {
    1796            0 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1797              : 
    1798            0 :     auto task = [&, mediaSourceType]()
    1799              :     {
    1800            0 :         m_needDataDelayCalculator.resetMediaDataDelay(mediaSourceType);
    1801            0 :         if (m_mediaPipelineClient)
    1802              :         {
    1803            0 :             const auto kSourceIter = m_attachedSources.find(mediaSourceType);
    1804            0 :             if (m_attachedSources.cend() == kSourceIter)
    1805              :             {
    1806            0 :                 RIALTO_SERVER_LOG_WARN("Buffer underflow notification failed - sourceId not found for %s",
    1807              :                                        common::convertMediaSourceType(mediaSourceType));
    1808            0 :                 return;
    1809              :             }
    1810            0 :             m_mediaPipelineClient->notifyBufferUnderflow(kSourceIter->second);
    1811              :         }
    1812            0 :     };
    1813              : 
    1814            0 :     m_mainThread->enqueueTask(m_mainThreadClientId, task);
    1815              : }
    1816              : 
    1817            2 : void MediaPipelineServerInternal::notifyFirstFrameReceived(MediaSourceType mediaSourceType)
    1818              : {
    1819            2 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1820              : 
    1821            2 :     auto task = [&, mediaSourceType]()
    1822              :     {
    1823            2 :         if (m_mediaPipelineClient)
    1824              :         {
    1825            2 :             const auto kSourceIter = m_attachedSources.find(mediaSourceType);
    1826            2 :             if (m_attachedSources.cend() == kSourceIter)
    1827              :             {
    1828            1 :                 RIALTO_SERVER_LOG_WARN("First frame notification failed - sourceId not found for %s",
    1829              :                                        common::convertMediaSourceType(mediaSourceType));
    1830            1 :                 return;
    1831              :             }
    1832            1 :             m_mediaPipelineClient->notifyFirstFrameReceived(kSourceIter->second);
    1833              :         }
    1834            2 :     };
    1835              : 
    1836            2 :     m_mainThread->enqueueTask(m_mainThreadClientId, task);
    1837              : }
    1838              : 
    1839            2 : void MediaPipelineServerInternal::notifyPlaybackError(MediaSourceType mediaSourceType, PlaybackError error)
    1840              : {
    1841            2 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1842              : 
    1843            2 :     auto task = [&, mediaSourceType, error]()
    1844              :     {
    1845            2 :         if (m_mediaPipelineClient)
    1846              :         {
    1847            2 :             const auto kSourceIter = m_attachedSources.find(mediaSourceType);
    1848            2 :             if (m_attachedSources.cend() == kSourceIter)
    1849              :             {
    1850            1 :                 RIALTO_SERVER_LOG_WARN("Playback error notification failed - sourceId not found for %s",
    1851              :                                        common::convertMediaSourceType(mediaSourceType));
    1852            1 :                 return;
    1853              :             }
    1854            1 :             m_mediaPipelineClient->notifyPlaybackError(kSourceIter->second, error);
    1855              :         }
    1856            2 :     };
    1857              : 
    1858            2 :     m_mainThread->enqueueTask(m_mainThreadClientId, task);
    1859              : }
    1860              : 
    1861            3 : void MediaPipelineServerInternal::notifyOutputRestrictionCleared(MediaSourceType mediaSourceType)
    1862              : {
    1863            3 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1864              : 
    1865            3 :     auto task = [&, mediaSourceType]()
    1866              :     {
    1867            3 :         if (!m_mediaPipelineClient)
    1868              :         {
    1869            0 :             return;
    1870              :         }
    1871              :         // Temporary transport until a dedicated IMediaPipelineClient notification is agreed
    1872            3 :         if (MediaSourceType::VIDEO == mediaSourceType)
    1873              :         {
    1874            1 :             m_mediaPipelineClient->notifyVideoData(true);
    1875              :         }
    1876            2 :         else if (MediaSourceType::AUDIO == mediaSourceType)
    1877              :         {
    1878            1 :             m_mediaPipelineClient->notifyAudioData(true);
    1879              :         }
    1880              :         else
    1881              :         {
    1882            1 :             RIALTO_SERVER_LOG_WARN("Output restriction cleared for unsupported source %s",
    1883              :                                    common::convertMediaSourceType(mediaSourceType));
    1884              :         }
    1885            3 :     };
    1886              : 
    1887            3 :     m_mainThread->enqueueTask(m_mainThreadClientId, task);
    1888              : }
    1889              : 
    1890            2 : void MediaPipelineServerInternal::notifySourceFlushed(MediaSourceType mediaSourceType)
    1891              : {
    1892            2 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1893              : 
    1894            2 :     auto task = [&, mediaSourceType]()
    1895              :     {
    1896            2 :         if (m_mediaPipelineClient)
    1897              :         {
    1898            2 :             const auto kSourceIter = m_attachedSources.find(mediaSourceType);
    1899            2 :             if (m_attachedSources.cend() == kSourceIter)
    1900              :             {
    1901            1 :                 RIALTO_SERVER_LOG_WARN("Source flushed notification failed - sourceId not found for: %s",
    1902              :                                        common::convertMediaSourceType(mediaSourceType));
    1903            1 :                 return;
    1904              :             }
    1905            1 :             m_mediaPipelineClient->notifySourceFlushed(kSourceIter->second);
    1906            1 :             RIALTO_SERVER_LOG_DEBUG("%s source flushed", common::convertMediaSourceType(mediaSourceType));
    1907              :         }
    1908            1 :         m_needDataDelayCalculator.resetMediaDataDelay(mediaSourceType);
    1909            2 :     };
    1910              : 
    1911            2 :     m_mainThread->enqueueTask(m_mainThreadClientId, task);
    1912              : }
    1913              : 
    1914            0 : void MediaPipelineServerInternal::notifyPlaybackInfo(const PlaybackInfo &playbackInfo)
    1915              : {
    1916            0 :     if (m_mediaPipelineClient)
    1917              :     {
    1918            0 :         m_mediaPipelineClient->notifyPlaybackInfo(playbackInfo);
    1919              :     }
    1920              : }
    1921              : 
    1922            8 : void MediaPipelineServerInternal::scheduleNotifyNeedMediaData(MediaSourceType mediaSourceType)
    1923              : {
    1924            8 :     RIALTO_SERVER_LOG_DEBUG("entry:");
    1925            8 :     auto timer = m_needMediaDataTimers.find(mediaSourceType);
    1926            8 :     if (m_needMediaDataTimers.end() != timer && timer->second && timer->second->isActive())
    1927              :     {
    1928            1 :         RIALTO_SERVER_LOG_DEBUG("Skip scheduling need media data for %s - it is already scheduled",
    1929              :                                 common::convertMediaSourceType(mediaSourceType));
    1930            1 :         return;
    1931              :     }
    1932              : 
    1933            7 :     m_needMediaDataTimers[mediaSourceType] =
    1934            7 :         m_timerFactory
    1935           21 :             ->createTimer(getNeedMediaDataTimeout(mediaSourceType),
    1936           14 :                           [this, mediaSourceType]()
    1937              :                           {
    1938            1 :                               m_mainThread
    1939            2 :                                   ->enqueueTask(m_mainThreadClientId,
    1940            1 :                                                 [this, mediaSourceType]()
    1941              :                                                 {
    1942            1 :                                                     m_needMediaDataTimers.erase(mediaSourceType);
    1943            1 :                                                     if (!notifyNeedMediaDataInternal(mediaSourceType))
    1944              :                                                     {
    1945            0 :                                                         RIALTO_SERVER_LOG_WARN("Scheduled Need media data sending "
    1946              :                                                                                "failed for: %s. Scheduling again...",
    1947              :                                                                                common::convertMediaSourceType(
    1948              :                                                                                    mediaSourceType));
    1949            0 :                                                         scheduleNotifyNeedMediaData(mediaSourceType);
    1950              :                                                     }
    1951              :                                                     else
    1952              :                                                     {
    1953            1 :                                                         m_needDataDelayCalculator.increaseNeedMediaDataDelay(
    1954              :                                                             mediaSourceType);
    1955              :                                                     }
    1956            1 :                                                 });
    1957            8 :                           });
    1958              : }
    1959              : 
    1960            7 : std::chrono::milliseconds MediaPipelineServerInternal::getNeedMediaDataTimeout(MediaSourceType mediaSourceType)
    1961              : {
    1962            7 :     constexpr std::chrono::milliseconds kNeedMediaDataResendTimeMsForLowLatency{5};
    1963            7 :     if ((mediaSourceType == MediaSourceType::VIDEO && m_IsLowLatencyVideoPlayer) ||
    1964            1 :         (mediaSourceType == MediaSourceType::AUDIO && m_IsLowLatencyAudioPlayer))
    1965              :     {
    1966            2 :         return kNeedMediaDataResendTimeMsForLowLatency;
    1967              :     }
    1968            5 :     return m_needDataDelayCalculator.getNeedMediaDataDelay(mediaSourceType);
    1969              : }
    1970              : }; // namespace firebolt::rialto::server
        

Generated by: LCOV version 2.0-1