LCOV - code coverage report
Current view: top level - media/server/gstplayer/source - GstGenericPlayer.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 94.9 % 1103 1047
Test Date: 2025-12-03 11:09:22 Functions: 94.6 % 111 105

            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 <chrono>
      21              : #include <cinttypes>
      22              : #include <stdexcept>
      23              : 
      24              : #include "FlushWatcher.h"
      25              : #include "GstDispatcherThread.h"
      26              : #include "GstGenericPlayer.h"
      27              : #include "GstProtectionMetadata.h"
      28              : #include "IGstTextTrackSinkFactory.h"
      29              : #include "IMediaPipeline.h"
      30              : #include "ITimer.h"
      31              : #include "RialtoServerLogging.h"
      32              : #include "TypeConverters.h"
      33              : #include "Utils.h"
      34              : #include "WorkerThread.h"
      35              : #include "tasks/generic/GenericPlayerTaskFactory.h"
      36              : 
      37              : namespace
      38              : {
      39              : /**
      40              :  * @brief Report position interval in ms.
      41              :  *        The position reporting timer should be started whenever the PLAYING state is entered and stopped
      42              :  *        whenever the session moves to another playback state.
      43              :  */
      44              : constexpr std::chrono::milliseconds kPositionReportTimerMs{250};
      45              : constexpr std::chrono::seconds kSubtitleClockResyncInterval{10};
      46              : 
      47            1 : bool operator==(const firebolt::rialto::server::SegmentData &lhs, const firebolt::rialto::server::SegmentData &rhs)
      48              : {
      49            2 :     return (lhs.position == rhs.position) && (lhs.resetTime == rhs.resetTime) && (lhs.appliedRate == rhs.appliedRate) &&
      50            2 :            (lhs.stopPosition == rhs.stopPosition);
      51              : }
      52              : } // namespace
      53              : 
      54              : namespace firebolt::rialto::server
      55              : {
      56              : std::weak_ptr<IGstGenericPlayerFactory> GstGenericPlayerFactory::m_factory;
      57              : 
      58            3 : std::shared_ptr<IGstGenericPlayerFactory> IGstGenericPlayerFactory::getFactory()
      59              : {
      60            3 :     std::shared_ptr<IGstGenericPlayerFactory> factory = GstGenericPlayerFactory::m_factory.lock();
      61              : 
      62            3 :     if (!factory)
      63              :     {
      64              :         try
      65              :         {
      66            3 :             factory = std::make_shared<GstGenericPlayerFactory>();
      67              :         }
      68            0 :         catch (const std::exception &e)
      69              :         {
      70            0 :             RIALTO_SERVER_LOG_ERROR("Failed to create the gstreamer player factory, reason: %s", e.what());
      71              :         }
      72              : 
      73            3 :         GstGenericPlayerFactory::m_factory = factory;
      74              :     }
      75              : 
      76            3 :     return factory;
      77              : }
      78              : 
      79            1 : std::unique_ptr<IGstGenericPlayer> GstGenericPlayerFactory::createGstGenericPlayer(
      80              :     IGstGenericPlayerClient *client, IDecryptionService &decryptionService, MediaType type,
      81              :     const VideoRequirements &videoRequirements,
      82              :     const std::shared_ptr<firebolt::rialto::wrappers::IRdkGstreamerUtilsWrapperFactory> &rdkGstreamerUtilsWrapperFactory)
      83              : {
      84            1 :     std::unique_ptr<IGstGenericPlayer> gstPlayer;
      85              : 
      86              :     try
      87              :     {
      88            1 :         auto gstWrapperFactory = firebolt::rialto::wrappers::IGstWrapperFactory::getFactory();
      89            1 :         auto glibWrapperFactory = firebolt::rialto::wrappers::IGlibWrapperFactory::getFactory();
      90            1 :         std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> gstWrapper;
      91            1 :         std::shared_ptr<firebolt::rialto::wrappers::IGlibWrapper> glibWrapper;
      92            1 :         std::shared_ptr<firebolt::rialto::wrappers::IRdkGstreamerUtilsWrapper> rdkGstreamerUtilsWrapper;
      93            1 :         if ((!gstWrapperFactory) || (!(gstWrapper = gstWrapperFactory->getGstWrapper())))
      94              :         {
      95            0 :             throw std::runtime_error("Cannot create GstWrapper");
      96              :         }
      97            1 :         if ((!glibWrapperFactory) || (!(glibWrapper = glibWrapperFactory->getGlibWrapper())))
      98              :         {
      99            0 :             throw std::runtime_error("Cannot create GlibWrapper");
     100              :         }
     101            2 :         if ((!rdkGstreamerUtilsWrapperFactory) ||
     102            2 :             (!(rdkGstreamerUtilsWrapper = rdkGstreamerUtilsWrapperFactory->createRdkGstreamerUtilsWrapper())))
     103              :         {
     104            0 :             throw std::runtime_error("Cannot create RdkGstreamerUtilsWrapper");
     105              :         }
     106              :         gstPlayer = std::make_unique<
     107            2 :             GstGenericPlayer>(client, decryptionService, type, videoRequirements, gstWrapper, glibWrapper,
     108            2 :                               rdkGstreamerUtilsWrapper, IGstInitialiser::instance(), std::make_unique<FlushWatcher>(),
     109            2 :                               IGstSrcFactory::getFactory(), common::ITimerFactory::getFactory(),
     110            2 :                               std::make_unique<GenericPlayerTaskFactory>(client, gstWrapper, glibWrapper,
     111              :                                                                          rdkGstreamerUtilsWrapper,
     112            2 :                                                                          IGstTextTrackSinkFactory::createFactory()),
     113            2 :                               std::make_unique<WorkerThreadFactory>(), std::make_unique<GstDispatcherThreadFactory>(),
     114            3 :                               IGstProtectionMetadataHelperFactory::createFactory());
     115            1 :     }
     116            0 :     catch (const std::exception &e)
     117              :     {
     118            0 :         RIALTO_SERVER_LOG_ERROR("Failed to create the gstreamer player, reason: %s", e.what());
     119              :     }
     120              : 
     121            1 :     return gstPlayer;
     122              : }
     123              : 
     124          212 : GstGenericPlayer::GstGenericPlayer(
     125              :     IGstGenericPlayerClient *client, IDecryptionService &decryptionService, MediaType type,
     126              :     const VideoRequirements &videoRequirements,
     127              :     const std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> &gstWrapper,
     128              :     const std::shared_ptr<firebolt::rialto::wrappers::IGlibWrapper> &glibWrapper,
     129              :     const std::shared_ptr<firebolt::rialto::wrappers::IRdkGstreamerUtilsWrapper> &rdkGstreamerUtilsWrapper,
     130              :     const IGstInitialiser &gstInitialiser, std::unique_ptr<IFlushWatcher> &&flushWatcher,
     131              :     const std::shared_ptr<IGstSrcFactory> &gstSrcFactory, std::shared_ptr<common::ITimerFactory> timerFactory,
     132              :     std::unique_ptr<IGenericPlayerTaskFactory> taskFactory, std::unique_ptr<IWorkerThreadFactory> workerThreadFactory,
     133              :     std::unique_ptr<IGstDispatcherThreadFactory> gstDispatcherThreadFactory,
     134          212 :     std::shared_ptr<IGstProtectionMetadataHelperFactory> gstProtectionMetadataFactory)
     135          212 :     : m_gstPlayerClient(client), m_gstWrapper{gstWrapper}, m_glibWrapper{glibWrapper},
     136          424 :       m_rdkGstreamerUtilsWrapper{rdkGstreamerUtilsWrapper}, m_timerFactory{timerFactory},
     137          636 :       m_taskFactory{std::move(taskFactory)}, m_flushWatcher{std::move(flushWatcher)}
     138              : {
     139          212 :     RIALTO_SERVER_LOG_DEBUG("GstGenericPlayer is constructed.");
     140              : 
     141          212 :     gstInitialiser.waitForInitialisation();
     142              : 
     143          212 :     m_context.decryptionService = &decryptionService;
     144              : 
     145          212 :     if ((!gstSrcFactory) || (!(m_context.gstSrc = gstSrcFactory->getGstSrc())))
     146              :     {
     147            2 :         throw std::runtime_error("Cannot create GstSrc");
     148              :     }
     149              : 
     150          210 :     if (!timerFactory)
     151              :     {
     152            1 :         throw std::runtime_error("TimeFactory is invalid");
     153              :     }
     154              : 
     155          418 :     if ((!gstProtectionMetadataFactory) ||
     156          418 :         (!(m_protectionMetadataWrapper = gstProtectionMetadataFactory->createProtectionMetadataWrapper(m_gstWrapper))))
     157              :     {
     158            0 :         throw std::runtime_error("Cannot create protection metadata wrapper");
     159              :     }
     160              : 
     161              :     // Ensure that rialtosrc has been initalised
     162          209 :     m_context.gstSrc->initSrc();
     163              : 
     164              :     // Start task thread
     165          209 :     if ((!workerThreadFactory) || (!(m_workerThread = workerThreadFactory->createWorkerThread())))
     166              :     {
     167            0 :         throw std::runtime_error("Failed to create the worker thread");
     168              :     }
     169              : 
     170              :     // Initialise pipeline
     171          209 :     switch (type)
     172              :     {
     173          208 :     case MediaType::MSE:
     174              :     {
     175          208 :         initMsePipeline();
     176          208 :         break;
     177              :     }
     178            1 :     default:
     179              :     {
     180            1 :         resetWorkerThread();
     181            1 :         throw std::runtime_error("Media type not supported");
     182              :     }
     183              :     }
     184              : 
     185              :     // Check the video requirements for a limited video.
     186              :     // If the video requirements are set to anything lower than the minimum, this playback is assumed to be a secondary
     187              :     // video in a dual video scenario.
     188          208 :     if ((kMinPrimaryVideoWidth > videoRequirements.maxWidth) || (kMinPrimaryVideoHeight > videoRequirements.maxHeight))
     189              :     {
     190            8 :         RIALTO_SERVER_LOG_MIL("Secondary video playback selected");
     191            8 :         bool westerossinkSecondaryVideoResult = setWesterossinkSecondaryVideo();
     192            8 :         bool ermContextResult = setErmContext();
     193            8 :         if (!westerossinkSecondaryVideoResult && !ermContextResult)
     194              :         {
     195            1 :             resetWorkerThread();
     196            1 :             termPipeline();
     197            1 :             throw std::runtime_error("Could not set secondary video");
     198              :         }
     199            7 :     }
     200              :     else
     201              :     {
     202          200 :         RIALTO_SERVER_LOG_MIL("Primary video playback selected");
     203              :     }
     204              : 
     205          621 :     m_gstDispatcherThread = gstDispatcherThreadFactory->createGstDispatcherThread(*this, m_context.pipeline, m_gstWrapper,
     206          414 :                                                                                   m_context.flushOnPrerollController);
     207          297 : }
     208              : 
     209          414 : GstGenericPlayer::~GstGenericPlayer()
     210              : {
     211          207 :     RIALTO_SERVER_LOG_DEBUG("GstGenericPlayer is destructed.");
     212          207 :     m_gstDispatcherThread.reset();
     213              : 
     214          207 :     resetWorkerThread();
     215              : 
     216          207 :     termPipeline();
     217          414 : }
     218              : 
     219          208 : void GstGenericPlayer::initMsePipeline()
     220              : {
     221              :     // Make playbin
     222          208 :     m_context.pipeline = m_gstWrapper->gstElementFactoryMake("playbin", "media_pipeline");
     223              :     // Set pipeline flags
     224          208 :     setPlaybinFlags(true);
     225              : 
     226              :     // Set callbacks
     227          208 :     m_glibWrapper->gSignalConnect(m_context.pipeline, "source-setup", G_CALLBACK(&GstGenericPlayer::setupSource), this);
     228          208 :     m_glibWrapper->gSignalConnect(m_context.pipeline, "element-setup", G_CALLBACK(&GstGenericPlayer::setupElement), this);
     229          208 :     m_glibWrapper->gSignalConnect(m_context.pipeline, "deep-element-added",
     230              :                                   G_CALLBACK(&GstGenericPlayer::deepElementAdded), this);
     231              : 
     232              :     // Set uri
     233          208 :     m_glibWrapper->gObjectSet(m_context.pipeline, "uri", "rialto://", nullptr);
     234              : 
     235              :     // Check playsink
     236          208 :     GstElement *playsink = (m_gstWrapper->gstBinGetByName(GST_BIN(m_context.pipeline), "playsink"));
     237          208 :     if (playsink)
     238              :     {
     239          207 :         m_glibWrapper->gObjectSet(G_OBJECT(playsink), "send-event-mode", 0, nullptr);
     240          207 :         m_gstWrapper->gstObjectUnref(playsink);
     241              :     }
     242              :     else
     243              :     {
     244            1 :         GST_WARNING("No playsink ?!?!?");
     245              :     }
     246          208 :     if (GST_STATE_CHANGE_FAILURE == m_gstWrapper->gstElementSetState(m_context.pipeline, GST_STATE_READY))
     247              :     {
     248            1 :         GST_WARNING("Failed to set pipeline to READY state");
     249              :     }
     250          208 :     RIALTO_SERVER_LOG_MIL("New RialtoServer's pipeline created");
     251              : }
     252              : 
     253          209 : void GstGenericPlayer::resetWorkerThread()
     254              : {
     255          209 :     m_postponedFlushes.clear();
     256              :     // Shutdown task thread
     257          209 :     m_workerThread->enqueueTask(m_taskFactory->createShutdown(*this));
     258          209 :     m_workerThread->join();
     259          209 :     m_workerThread.reset();
     260              : }
     261              : 
     262          208 : void GstGenericPlayer::termPipeline()
     263              : {
     264          208 :     if (m_finishSourceSetupTimer && m_finishSourceSetupTimer->isActive())
     265              :     {
     266            0 :         m_finishSourceSetupTimer->cancel();
     267              :     }
     268              : 
     269          208 :     m_finishSourceSetupTimer.reset();
     270              : 
     271          257 :     for (auto &elem : m_context.streamInfo)
     272              :     {
     273           49 :         StreamInfo &streamInfo = elem.second;
     274           51 :         for (auto &buffer : streamInfo.buffers)
     275              :         {
     276            2 :             m_gstWrapper->gstBufferUnref(buffer);
     277              :         }
     278              : 
     279           49 :         streamInfo.buffers.clear();
     280              :     }
     281              : 
     282          208 :     m_taskFactory->createStop(m_context, *this)->execute();
     283          208 :     GstBus *bus = m_gstWrapper->gstPipelineGetBus(GST_PIPELINE(m_context.pipeline));
     284          208 :     m_gstWrapper->gstBusSetSyncHandler(bus, nullptr, nullptr, nullptr);
     285          208 :     m_gstWrapper->gstObjectUnref(bus);
     286              : 
     287          208 :     if (m_context.source)
     288              :     {
     289            1 :         m_gstWrapper->gstObjectUnref(m_context.source);
     290              :     }
     291          208 :     if (m_context.subtitleSink)
     292              :     {
     293            4 :         m_gstWrapper->gstObjectUnref(m_context.subtitleSink);
     294            4 :         m_context.subtitleSink = nullptr;
     295              :     }
     296              : 
     297          208 :     if (m_context.videoSink)
     298              :     {
     299            0 :         m_gstWrapper->gstObjectUnref(m_context.videoSink);
     300            0 :         m_context.videoSink = nullptr;
     301              :     }
     302              : 
     303              :     // Delete the pipeline
     304          208 :     m_gstWrapper->gstObjectUnref(m_context.pipeline);
     305              : 
     306          208 :     RIALTO_SERVER_LOG_MIL("RialtoServer's pipeline terminated");
     307              : }
     308              : 
     309          833 : unsigned GstGenericPlayer::getGstPlayFlag(const char *nick)
     310              : {
     311              :     GFlagsClass *flagsClass =
     312          833 :         static_cast<GFlagsClass *>(m_glibWrapper->gTypeClassRef(m_glibWrapper->gTypeFromName("GstPlayFlags")));
     313          833 :     GFlagsValue *flag = m_glibWrapper->gFlagsGetValueByNick(flagsClass, nick);
     314          833 :     return flag ? flag->value : 0;
     315              : }
     316              : 
     317            1 : void GstGenericPlayer::setupSource(GstElement *pipeline, GstElement *source, GstGenericPlayer *self)
     318              : {
     319            1 :     self->m_gstWrapper->gstObjectRef(source);
     320            1 :     if (self->m_workerThread)
     321              :     {
     322            1 :         self->m_workerThread->enqueueTask(self->m_taskFactory->createSetupSource(self->m_context, *self, source));
     323              :     }
     324              : }
     325              : 
     326            1 : void GstGenericPlayer::setupElement(GstElement *pipeline, GstElement *element, GstGenericPlayer *self)
     327              : {
     328            1 :     RIALTO_SERVER_LOG_DEBUG("Element %s added to the pipeline", GST_ELEMENT_NAME(element));
     329            1 :     self->m_gstWrapper->gstObjectRef(element);
     330            1 :     if (self->m_workerThread)
     331              :     {
     332            1 :         self->m_workerThread->enqueueTask(self->m_taskFactory->createSetupElement(self->m_context, *self, element));
     333              :     }
     334              : }
     335              : 
     336            1 : void GstGenericPlayer::deepElementAdded(GstBin *pipeline, GstBin *bin, GstElement *element, GstGenericPlayer *self)
     337              : {
     338            1 :     RIALTO_SERVER_LOG_DEBUG("Deep element %s added to the pipeline", GST_ELEMENT_NAME(element));
     339            1 :     if (self->m_workerThread)
     340              :     {
     341            2 :         self->m_workerThread->enqueueTask(
     342            2 :             self->m_taskFactory->createDeepElementAdded(self->m_context, *self, pipeline, bin, element));
     343              :     }
     344            1 : }
     345              : 
     346            1 : void GstGenericPlayer::attachSource(const std::unique_ptr<IMediaPipeline::MediaSource> &attachedSource)
     347              : {
     348            1 :     if (m_workerThread)
     349              :     {
     350            1 :         m_workerThread->enqueueTask(m_taskFactory->createAttachSource(m_context, *this, attachedSource));
     351              :     }
     352              : }
     353              : 
     354            1 : void GstGenericPlayer::removeSource(const MediaSourceType &mediaSourceType)
     355              : {
     356            1 :     if (m_workerThread)
     357              :     {
     358            1 :         m_workerThread->enqueueTask(m_taskFactory->createRemoveSource(m_context, *this, mediaSourceType));
     359              :     }
     360              : }
     361              : 
     362            2 : void GstGenericPlayer::allSourcesAttached()
     363              : {
     364            2 :     if (m_workerThread)
     365              :     {
     366            2 :         m_workerThread->enqueueTask(m_taskFactory->createFinishSetupSource(m_context, *this));
     367              :     }
     368              : }
     369              : 
     370            1 : void GstGenericPlayer::attachSamples(const IMediaPipeline::MediaSegmentVector &mediaSegments)
     371              : {
     372            1 :     if (m_workerThread)
     373              :     {
     374            1 :         m_workerThread->enqueueTask(m_taskFactory->createAttachSamples(m_context, *this, mediaSegments));
     375              :     }
     376              : }
     377              : 
     378            1 : void GstGenericPlayer::attachSamples(const std::shared_ptr<IDataReader> &dataReader)
     379              : {
     380            1 :     if (m_workerThread)
     381              :     {
     382            1 :         m_workerThread->enqueueTask(m_taskFactory->createReadShmDataAndAttachSamples(m_context, *this, dataReader));
     383              :     }
     384              : }
     385              : 
     386            1 : void GstGenericPlayer::setPosition(std::int64_t position)
     387              : {
     388            1 :     if (m_workerThread)
     389              :     {
     390            1 :         m_workerThread->enqueueTask(m_taskFactory->createSetPosition(m_context, *this, position));
     391              :     }
     392              : }
     393              : 
     394            1 : void GstGenericPlayer::setPlaybackRate(double rate)
     395              : {
     396            1 :     if (m_workerThread)
     397              :     {
     398            1 :         m_workerThread->enqueueTask(m_taskFactory->createSetPlaybackRate(m_context, rate));
     399              :     }
     400              : }
     401              : 
     402           11 : bool GstGenericPlayer::getPosition(std::int64_t &position)
     403              : {
     404              :     // We are on main thread here, but m_context.pipeline can be used, because it's modified only in GstGenericPlayer
     405              :     // constructor and destructor. GstGenericPlayer is created/destructed on main thread, so we won't have a crash here.
     406           11 :     position = getPosition(m_context.pipeline);
     407           11 :     if (position == -1)
     408              :     {
     409            3 :         RIALTO_SERVER_LOG_WARN("Query position failed");
     410            3 :         return false;
     411              :     }
     412              : 
     413            8 :     return true;
     414              : }
     415              : 
     416           44 : GstElement *GstGenericPlayer::getSink(const MediaSourceType &mediaSourceType) const
     417              : {
     418           44 :     const char *kSinkName{nullptr};
     419           44 :     GstElement *sink{nullptr};
     420           44 :     switch (mediaSourceType)
     421              :     {
     422           24 :     case MediaSourceType::AUDIO:
     423           24 :         kSinkName = "audio-sink";
     424           24 :         break;
     425           18 :     case MediaSourceType::VIDEO:
     426           18 :         kSinkName = "video-sink";
     427           18 :         break;
     428            2 :     default:
     429            2 :         break;
     430              :     }
     431           44 :     if (!kSinkName)
     432              :     {
     433            2 :         RIALTO_SERVER_LOG_WARN("mediaSourceType not supported %d", static_cast<int>(mediaSourceType));
     434              :     }
     435              :     else
     436              :     {
     437           42 :         if (m_context.pipeline == nullptr)
     438              :         {
     439            0 :             RIALTO_SERVER_LOG_WARN("Pipeline is NULL!");
     440              :         }
     441              :         else
     442              :         {
     443           42 :             RIALTO_SERVER_LOG_DEBUG("Pipeline is valid: %p", m_context.pipeline);
     444              :         }
     445           42 :         m_glibWrapper->gObjectGet(m_context.pipeline, kSinkName, &sink, nullptr);
     446           42 :         if (sink)
     447              :         {
     448           25 :             GstElement *autoSink{sink};
     449           25 :             if (firebolt::rialto::MediaSourceType::VIDEO == mediaSourceType)
     450           14 :                 autoSink = getSinkChildIfAutoVideoSink(sink);
     451           11 :             else if (firebolt::rialto::MediaSourceType::AUDIO == mediaSourceType)
     452           11 :                 autoSink = getSinkChildIfAutoAudioSink(sink);
     453              : 
     454              :             // Is this an auto-sink?...
     455           25 :             if (autoSink != sink)
     456              :             {
     457            2 :                 m_gstWrapper->gstObjectUnref(GST_OBJECT(sink));
     458              : 
     459              :                 // increase the reference count of the auto sink
     460            2 :                 sink = GST_ELEMENT(m_gstWrapper->gstObjectRef(GST_OBJECT(autoSink)));
     461              :             }
     462              :         }
     463              :     }
     464           44 :     return sink;
     465              : }
     466              : 
     467            1 : void GstGenericPlayer::setSourceFlushed(const MediaSourceType &mediaSourceType)
     468              : {
     469            1 :     m_flushWatcher->setFlushed(mediaSourceType);
     470              : }
     471              : 
     472            1 : void GstGenericPlayer::postponeFlush(const MediaSourceType &mediaSourceType, bool resetTime)
     473              : {
     474            1 :     m_postponedFlushes.emplace_back(std::make_pair(mediaSourceType, resetTime));
     475              : }
     476              : 
     477            1 : void GstGenericPlayer::executePostponedFlushes()
     478              : {
     479            1 :     if (m_workerThread)
     480              :     {
     481            2 :         for (const auto &[mediaSourceType, resetTime] : m_postponedFlushes)
     482              :         {
     483            1 :             m_workerThread->enqueueTask(m_taskFactory->createFlush(m_context, *this, mediaSourceType, resetTime));
     484              :         }
     485              :     }
     486            1 :     m_postponedFlushes.clear();
     487              : }
     488              : 
     489           19 : GstElement *GstGenericPlayer::getDecoder(const MediaSourceType &mediaSourceType)
     490              : {
     491           19 :     GstIterator *it = m_gstWrapper->gstBinIterateRecurse(GST_BIN(m_context.pipeline));
     492           19 :     GValue item = G_VALUE_INIT;
     493           19 :     gboolean done = FALSE;
     494              : 
     495           28 :     while (!done)
     496              :     {
     497           21 :         switch (m_gstWrapper->gstIteratorNext(it, &item))
     498              :         {
     499           12 :         case GST_ITERATOR_OK:
     500              :         {
     501           12 :             GstElement *element = GST_ELEMENT(m_glibWrapper->gValueGetObject(&item));
     502           12 :             GstElementFactory *factory = m_gstWrapper->gstElementGetFactory(element);
     503              : 
     504           12 :             if (factory)
     505              :             {
     506           12 :                 GstElementFactoryListType type = GST_ELEMENT_FACTORY_TYPE_DECODER;
     507           12 :                 if (mediaSourceType == MediaSourceType::AUDIO)
     508              :                 {
     509           12 :                     type |= GST_ELEMENT_FACTORY_TYPE_MEDIA_AUDIO;
     510              :                 }
     511            0 :                 else if (mediaSourceType == MediaSourceType::VIDEO)
     512              :                 {
     513            0 :                     type |= GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO;
     514              :                 }
     515              : 
     516           12 :                 if (m_gstWrapper->gstElementFactoryListIsType(factory, type))
     517              :                 {
     518           12 :                     m_glibWrapper->gValueUnset(&item);
     519           12 :                     m_gstWrapper->gstIteratorFree(it);
     520           12 :                     return GST_ELEMENT(m_gstWrapper->gstObjectRef(element));
     521              :                 }
     522              :             }
     523              : 
     524            0 :             m_glibWrapper->gValueUnset(&item);
     525            0 :             break;
     526              :         }
     527            2 :         case GST_ITERATOR_RESYNC:
     528            2 :             m_gstWrapper->gstIteratorResync(it);
     529            2 :             break;
     530            7 :         case GST_ITERATOR_ERROR:
     531              :         case GST_ITERATOR_DONE:
     532            7 :             done = TRUE;
     533            7 :             break;
     534              :         }
     535              :     }
     536              : 
     537            7 :     RIALTO_SERVER_LOG_WARN("Could not find decoder");
     538              : 
     539            7 :     m_glibWrapper->gValueUnset(&item);
     540            7 :     m_gstWrapper->gstIteratorFree(it);
     541              : 
     542            7 :     return nullptr;
     543              : }
     544              : 
     545            3 : GstElement *GstGenericPlayer::getParser(const MediaSourceType &mediaSourceType)
     546              : {
     547            3 :     GstIterator *it = m_gstWrapper->gstBinIterateRecurse(GST_BIN(m_context.pipeline));
     548            3 :     GValue item = G_VALUE_INIT;
     549            3 :     gboolean done = FALSE;
     550              : 
     551            4 :     while (!done)
     552              :     {
     553            3 :         switch (m_gstWrapper->gstIteratorNext(it, &item))
     554              :         {
     555            2 :         case GST_ITERATOR_OK:
     556              :         {
     557            2 :             GstElement *element = GST_ELEMENT(m_glibWrapper->gValueGetObject(&item));
     558            2 :             GstElementFactory *factory = m_gstWrapper->gstElementGetFactory(element);
     559              : 
     560            2 :             if (factory)
     561              :             {
     562            2 :                 GstElementFactoryListType type = GST_ELEMENT_FACTORY_TYPE_PARSER;
     563            2 :                 if (mediaSourceType == MediaSourceType::AUDIO)
     564              :                 {
     565            0 :                     type |= GST_ELEMENT_FACTORY_TYPE_MEDIA_AUDIO;
     566              :                 }
     567            2 :                 else if (mediaSourceType == MediaSourceType::VIDEO)
     568              :                 {
     569            2 :                     type |= GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO;
     570              :                 }
     571              : 
     572            2 :                 if (m_gstWrapper->gstElementFactoryListIsType(factory, type))
     573              :                 {
     574            2 :                     m_glibWrapper->gValueUnset(&item);
     575            2 :                     m_gstWrapper->gstIteratorFree(it);
     576            2 :                     return GST_ELEMENT(m_gstWrapper->gstObjectRef(element));
     577              :                 }
     578              :             }
     579              : 
     580            0 :             m_glibWrapper->gValueUnset(&item);
     581            0 :             break;
     582              :         }
     583            0 :         case GST_ITERATOR_RESYNC:
     584            0 :             m_gstWrapper->gstIteratorResync(it);
     585            0 :             break;
     586            1 :         case GST_ITERATOR_ERROR:
     587              :         case GST_ITERATOR_DONE:
     588            1 :             done = TRUE;
     589            1 :             break;
     590              :         }
     591              :     }
     592              : 
     593            1 :     RIALTO_SERVER_LOG_WARN("Could not find parser");
     594              : 
     595            1 :     m_glibWrapper->gValueUnset(&item);
     596            1 :     m_gstWrapper->gstIteratorFree(it);
     597              : 
     598            1 :     return nullptr;
     599              : }
     600              : 
     601              : std::optional<firebolt::rialto::wrappers::AudioAttributesPrivate>
     602            5 : GstGenericPlayer::createAudioAttributes(const std::unique_ptr<IMediaPipeline::MediaSource> &source) const
     603              : {
     604            5 :     std::optional<firebolt::rialto::wrappers::AudioAttributesPrivate> audioAttributes;
     605            5 :     const IMediaPipeline::MediaSourceAudio *kSource = dynamic_cast<IMediaPipeline::MediaSourceAudio *>(source.get());
     606            5 :     if (kSource)
     607              :     {
     608            4 :         firebolt::rialto::AudioConfig audioConfig = kSource->getAudioConfig();
     609              :         audioAttributes =
     610           12 :             firebolt::rialto::wrappers::AudioAttributesPrivate{"", // param set below.
     611            4 :                                                                audioConfig.numberOfChannels, audioConfig.sampleRate,
     612              :                                                                0, // used only in one of logs in rdk_gstreamer_utils, no
     613              :                                                                   // need to set this param.
     614              :                                                                0, // used only in one of logs in rdk_gstreamer_utils, no
     615              :                                                                   // need to set this param.
     616            4 :                                                                audioConfig.codecSpecificConfig.data(),
     617              :                                                                static_cast<std::uint32_t>(
     618            4 :                                                                    audioConfig.codecSpecificConfig.size())};
     619            4 :         if (source->getMimeType() == "audio/mp4" || source->getMimeType() == "audio/aac")
     620              :         {
     621            2 :             audioAttributes->m_codecParam = "mp4a";
     622              :         }
     623            2 :         else if (source->getMimeType() == "audio/x-eac3")
     624              :         {
     625            1 :             audioAttributes->m_codecParam = "ec-3";
     626              :         }
     627            1 :         else if (source->getMimeType() == "audio/b-wav" || source->getMimeType() == "audio/x-raw")
     628              :         {
     629            1 :             audioAttributes->m_codecParam = "lpcm";
     630              :         }
     631            4 :     }
     632              :     else
     633              :     {
     634            1 :         RIALTO_SERVER_LOG_ERROR("Failed to cast source");
     635              :     }
     636              : 
     637            5 :     return audioAttributes;
     638              : }
     639              : 
     640            1 : bool GstGenericPlayer::setImmediateOutput(const MediaSourceType &mediaSourceType, bool immediateOutputParam)
     641              : {
     642            1 :     if (!m_workerThread)
     643            0 :         return false;
     644              : 
     645            2 :     m_workerThread->enqueueTask(
     646            2 :         m_taskFactory->createSetImmediateOutput(m_context, *this, mediaSourceType, immediateOutputParam));
     647            1 :     return true;
     648              : }
     649              : 
     650            5 : bool GstGenericPlayer::getImmediateOutput(const MediaSourceType &mediaSourceType, bool &immediateOutputRef)
     651              : {
     652            5 :     bool returnValue{false};
     653            5 :     GstElement *sink{getSink(mediaSourceType)};
     654            5 :     if (sink)
     655              :     {
     656            3 :         if (m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(sink), "immediate-output"))
     657              :         {
     658            2 :             m_glibWrapper->gObjectGet(sink, "immediate-output", &immediateOutputRef, nullptr);
     659            2 :             returnValue = true;
     660              :         }
     661              :         else
     662              :         {
     663            1 :             RIALTO_SERVER_LOG_ERROR("immediate-output not supported in element %s", GST_ELEMENT_NAME(sink));
     664              :         }
     665            3 :         m_gstWrapper->gstObjectUnref(sink);
     666              :     }
     667              :     else
     668              :     {
     669            2 :         RIALTO_SERVER_LOG_ERROR("Failed to set immediate-output property, sink is NULL");
     670              :     }
     671              : 
     672            5 :     return returnValue;
     673              : }
     674              : 
     675            5 : bool GstGenericPlayer::getStats(const MediaSourceType &mediaSourceType, uint64_t &renderedFrames, uint64_t &droppedFrames)
     676              : {
     677            5 :     bool returnValue{false};
     678            5 :     GstElement *sink{getSink(mediaSourceType)};
     679            5 :     if (sink)
     680              :     {
     681            3 :         GstStructure *stats{nullptr};
     682            3 :         m_glibWrapper->gObjectGet(sink, "stats", &stats, nullptr);
     683            3 :         if (!stats)
     684              :         {
     685            1 :             RIALTO_SERVER_LOG_ERROR("failed to get stats from '%s'", GST_ELEMENT_NAME(sink));
     686              :         }
     687              :         else
     688              :         {
     689              :             guint64 renderedFramesTmp;
     690              :             guint64 droppedFramesTmp;
     691            3 :             if (m_gstWrapper->gstStructureGetUint64(stats, "rendered", &renderedFramesTmp) &&
     692            1 :                 m_gstWrapper->gstStructureGetUint64(stats, "dropped", &droppedFramesTmp))
     693              :             {
     694            1 :                 renderedFrames = renderedFramesTmp;
     695            1 :                 droppedFrames = droppedFramesTmp;
     696            1 :                 returnValue = true;
     697              :             }
     698              :             else
     699              :             {
     700            1 :                 RIALTO_SERVER_LOG_ERROR("failed to get 'rendered' or 'dropped' from structure (%s)",
     701              :                                         GST_ELEMENT_NAME(sink));
     702              :             }
     703            2 :             m_gstWrapper->gstStructureFree(stats);
     704              :         }
     705            3 :         m_gstWrapper->gstObjectUnref(sink);
     706              :     }
     707              :     else
     708              :     {
     709            2 :         RIALTO_SERVER_LOG_ERROR("Failed to get stats, sink is NULL");
     710              :     }
     711              : 
     712            5 :     return returnValue;
     713              : }
     714              : 
     715            4 : GstBuffer *GstGenericPlayer::createBuffer(const IMediaPipeline::MediaSegment &mediaSegment) const
     716              : {
     717            4 :     GstBuffer *gstBuffer = m_gstWrapper->gstBufferNewAllocate(nullptr, mediaSegment.getDataLength(), nullptr);
     718            4 :     m_gstWrapper->gstBufferFill(gstBuffer, 0, mediaSegment.getData(), mediaSegment.getDataLength());
     719              : 
     720            4 :     if (mediaSegment.isEncrypted())
     721              :     {
     722            3 :         GstBuffer *keyId = m_gstWrapper->gstBufferNewAllocate(nullptr, mediaSegment.getKeyId().size(), nullptr);
     723            3 :         m_gstWrapper->gstBufferFill(keyId, 0, mediaSegment.getKeyId().data(), mediaSegment.getKeyId().size());
     724              : 
     725            3 :         GstBuffer *initVector = m_gstWrapper->gstBufferNewAllocate(nullptr, mediaSegment.getInitVector().size(), nullptr);
     726            6 :         m_gstWrapper->gstBufferFill(initVector, 0, mediaSegment.getInitVector().data(),
     727            3 :                                     mediaSegment.getInitVector().size());
     728            3 :         GstBuffer *subsamples{nullptr};
     729            3 :         if (!mediaSegment.getSubSamples().empty())
     730              :         {
     731            3 :             auto subsamplesRawSize = mediaSegment.getSubSamples().size() * (sizeof(guint16) + sizeof(guint32));
     732            3 :             guint8 *subsamplesRaw = static_cast<guint8 *>(m_glibWrapper->gMalloc(subsamplesRawSize));
     733              :             GstByteWriter writer;
     734            3 :             m_gstWrapper->gstByteWriterInitWithData(&writer, subsamplesRaw, subsamplesRawSize, FALSE);
     735              : 
     736            6 :             for (const auto &subSample : mediaSegment.getSubSamples())
     737              :             {
     738            3 :                 m_gstWrapper->gstByteWriterPutUint16Be(&writer, subSample.numClearBytes);
     739            3 :                 m_gstWrapper->gstByteWriterPutUint32Be(&writer, subSample.numEncryptedBytes);
     740              :             }
     741            3 :             subsamples = m_gstWrapper->gstBufferNewWrapped(subsamplesRaw, subsamplesRawSize);
     742              :         }
     743              : 
     744            3 :         uint32_t crypt = 0;
     745            3 :         uint32_t skip = 0;
     746            3 :         bool encryptionPatternSet = mediaSegment.getEncryptionPattern(crypt, skip);
     747              : 
     748            3 :         GstRialtoProtectionData data = {mediaSegment.getMediaKeySessionId(),
     749            3 :                                         static_cast<uint32_t>(mediaSegment.getSubSamples().size()),
     750            3 :                                         mediaSegment.getInitWithLast15(),
     751              :                                         keyId,
     752              :                                         initVector,
     753              :                                         subsamples,
     754            6 :                                         mediaSegment.getCipherMode(),
     755              :                                         crypt,
     756              :                                         skip,
     757              :                                         encryptionPatternSet,
     758            6 :                                         m_context.decryptionService};
     759              : 
     760            3 :         if (!m_protectionMetadataWrapper->addProtectionMetadata(gstBuffer, data))
     761              :         {
     762            1 :             RIALTO_SERVER_LOG_ERROR("Failed to add protection metadata");
     763            1 :             if (keyId)
     764              :             {
     765            1 :                 m_gstWrapper->gstBufferUnref(keyId);
     766              :             }
     767            1 :             if (initVector)
     768              :             {
     769            1 :                 m_gstWrapper->gstBufferUnref(initVector);
     770              :             }
     771            1 :             if (subsamples)
     772              :             {
     773            1 :                 m_gstWrapper->gstBufferUnref(subsamples);
     774              :             }
     775              :         }
     776              :     }
     777              : 
     778            4 :     GST_BUFFER_TIMESTAMP(gstBuffer) = mediaSegment.getTimeStamp();
     779            4 :     GST_BUFFER_DURATION(gstBuffer) = mediaSegment.getDuration();
     780            4 :     return gstBuffer;
     781              : }
     782              : 
     783            4 : void GstGenericPlayer::notifyNeedMediaData(const MediaSourceType mediaSource)
     784              : {
     785            4 :     auto elem = m_context.streamInfo.find(mediaSource);
     786            4 :     if (elem != m_context.streamInfo.end())
     787              :     {
     788            2 :         StreamInfo &streamInfo = elem->second;
     789            2 :         streamInfo.isNeedDataPending = false;
     790              : 
     791              :         // Send new NeedMediaData if we still need it
     792            2 :         if (m_gstPlayerClient && streamInfo.isDataNeeded)
     793              :         {
     794            2 :             streamInfo.isNeedDataPending = m_gstPlayerClient->notifyNeedMediaData(mediaSource);
     795              :         }
     796              :     }
     797              :     else
     798              :     {
     799            2 :         RIALTO_SERVER_LOG_WARN("Media type %s could not be found", common::convertMediaSourceType(mediaSource));
     800              :     }
     801            4 : }
     802              : 
     803           19 : void GstGenericPlayer::attachData(const firebolt::rialto::MediaSourceType mediaType)
     804              : {
     805           19 :     auto elem = m_context.streamInfo.find(mediaType);
     806           19 :     if (elem != m_context.streamInfo.end())
     807              :     {
     808           16 :         StreamInfo &streamInfo = elem->second;
     809           16 :         if (streamInfo.buffers.empty() || !streamInfo.isDataNeeded)
     810              :         {
     811            2 :             return;
     812              :         }
     813              : 
     814           14 :         if (firebolt::rialto::MediaSourceType::SUBTITLE == mediaType)
     815              :         {
     816            2 :             setTextTrackPositionIfRequired(streamInfo.appSrc);
     817              :         }
     818              :         else
     819              :         {
     820           36 :             pushSampleIfRequired(streamInfo.appSrc, common::convertMediaSourceType(mediaType));
     821              :         }
     822           14 :         if (mediaType == firebolt::rialto::MediaSourceType::AUDIO)
     823              :         {
     824              :             // This needs to be done before gstAppSrcPushBuffer() is
     825              :             // called because it can free the memory
     826            7 :             m_context.lastAudioSampleTimestamps = static_cast<int64_t>(GST_BUFFER_PTS(streamInfo.buffers.back()));
     827              :         }
     828              : 
     829           28 :         for (GstBuffer *buffer : streamInfo.buffers)
     830              :         {
     831           14 :             m_gstWrapper->gstAppSrcPushBuffer(GST_APP_SRC(streamInfo.appSrc), buffer);
     832              :         }
     833           14 :         streamInfo.buffers.clear();
     834           14 :         streamInfo.isDataPushed = true;
     835              : 
     836           14 :         const bool kIsSingle = m_context.streamInfo.size() == 1;
     837           14 :         bool allOtherStreamsPushed = std::all_of(m_context.streamInfo.begin(), m_context.streamInfo.end(),
     838           15 :                                                  [](const auto &entry) { return entry.second.isDataPushed; });
     839              : 
     840           14 :         if (!m_context.bufferedNotificationSent && (allOtherStreamsPushed || kIsSingle) && m_gstPlayerClient)
     841              :         {
     842            1 :             m_context.bufferedNotificationSent = true;
     843            1 :             m_gstPlayerClient->notifyNetworkState(NetworkState::BUFFERED);
     844            1 :             RIALTO_SERVER_LOG_MIL("Buffered NetworkState reached");
     845              :         }
     846           14 :         cancelUnderflow(mediaType);
     847              : 
     848           14 :         const auto eosInfoIt = m_context.endOfStreamInfo.find(mediaType);
     849           14 :         if (eosInfoIt != m_context.endOfStreamInfo.end() && eosInfoIt->second == EosState::PENDING)
     850              :         {
     851            0 :             setEos(mediaType);
     852              :         }
     853              :     }
     854              : }
     855              : 
     856            7 : void GstGenericPlayer::updateAudioCaps(int32_t rate, int32_t channels, const std::shared_ptr<CodecData> &codecData)
     857              : {
     858            7 :     auto elem = m_context.streamInfo.find(firebolt::rialto::MediaSourceType::AUDIO);
     859            7 :     if (elem != m_context.streamInfo.end())
     860              :     {
     861            6 :         StreamInfo &streamInfo = elem->second;
     862              : 
     863            6 :         constexpr int kInvalidRate{0}, kInvalidChannels{0};
     864            6 :         GstCaps *currentCaps = m_gstWrapper->gstAppSrcGetCaps(GST_APP_SRC(streamInfo.appSrc));
     865            6 :         GstCaps *newCaps = m_gstWrapper->gstCapsCopy(currentCaps);
     866              : 
     867            6 :         if (rate != kInvalidRate)
     868              :         {
     869            3 :             m_gstWrapper->gstCapsSetSimple(newCaps, "rate", G_TYPE_INT, rate, NULL);
     870              :         }
     871              : 
     872            6 :         if (channels != kInvalidChannels)
     873              :         {
     874            3 :             m_gstWrapper->gstCapsSetSimple(newCaps, "channels", G_TYPE_INT, channels, NULL);
     875              :         }
     876              : 
     877            6 :         setCodecData(newCaps, codecData);
     878              : 
     879            6 :         if (!m_gstWrapper->gstCapsIsEqual(currentCaps, newCaps))
     880              :         {
     881            5 :             m_gstWrapper->gstAppSrcSetCaps(GST_APP_SRC(streamInfo.appSrc), newCaps);
     882              :         }
     883              : 
     884            6 :         m_gstWrapper->gstCapsUnref(newCaps);
     885            6 :         m_gstWrapper->gstCapsUnref(currentCaps);
     886              :     }
     887            7 : }
     888              : 
     889            8 : void GstGenericPlayer::updateVideoCaps(int32_t width, int32_t height, Fraction frameRate,
     890              :                                        const std::shared_ptr<CodecData> &codecData)
     891              : {
     892            8 :     auto elem = m_context.streamInfo.find(firebolt::rialto::MediaSourceType::VIDEO);
     893            8 :     if (elem != m_context.streamInfo.end())
     894              :     {
     895            7 :         StreamInfo &streamInfo = elem->second;
     896              : 
     897            7 :         GstCaps *currentCaps = m_gstWrapper->gstAppSrcGetCaps(GST_APP_SRC(streamInfo.appSrc));
     898            7 :         GstCaps *newCaps = m_gstWrapper->gstCapsCopy(currentCaps);
     899              : 
     900            7 :         if (width > 0)
     901              :         {
     902            6 :             m_gstWrapper->gstCapsSetSimple(newCaps, "width", G_TYPE_INT, width, NULL);
     903              :         }
     904              : 
     905            7 :         if (height > 0)
     906              :         {
     907            6 :             m_gstWrapper->gstCapsSetSimple(newCaps, "height", G_TYPE_INT, height, NULL);
     908              :         }
     909              : 
     910            7 :         if ((kUndefinedSize != frameRate.numerator) && (kUndefinedSize != frameRate.denominator))
     911              :         {
     912            6 :             m_gstWrapper->gstCapsSetSimple(newCaps, "framerate", GST_TYPE_FRACTION, frameRate.numerator,
     913              :                                            frameRate.denominator, NULL);
     914              :         }
     915              : 
     916            7 :         setCodecData(newCaps, codecData);
     917              : 
     918            7 :         if (!m_gstWrapper->gstCapsIsEqual(currentCaps, newCaps))
     919              :         {
     920            6 :             m_gstWrapper->gstAppSrcSetCaps(GST_APP_SRC(streamInfo.appSrc), newCaps);
     921              :         }
     922              : 
     923            7 :         m_gstWrapper->gstCapsUnref(currentCaps);
     924            7 :         m_gstWrapper->gstCapsUnref(newCaps);
     925              :     }
     926            8 : }
     927              : 
     928            5 : void GstGenericPlayer::addAudioClippingToBuffer(GstBuffer *buffer, uint64_t clippingStart, uint64_t clippingEnd) const
     929              : {
     930            5 :     if (clippingStart || clippingEnd)
     931              :     {
     932            4 :         if (m_gstWrapper->gstBufferAddAudioClippingMeta(buffer, GST_FORMAT_TIME, clippingStart, clippingEnd))
     933              :         {
     934            3 :             RIALTO_SERVER_LOG_DEBUG("Added audio clipping to buffer %p, start: %" PRIu64 ", end %" PRIu64, buffer,
     935              :                                     clippingStart, clippingEnd);
     936              :         }
     937              :         else
     938              :         {
     939            1 :             RIALTO_SERVER_LOG_WARN("Failed to add audio clipping to buffer %p, start: %" PRIu64 ", end %" PRIu64,
     940              :                                    buffer, clippingStart, clippingEnd);
     941              :         }
     942              :     }
     943            5 : }
     944              : 
     945           13 : bool GstGenericPlayer::setCodecData(GstCaps *caps, const std::shared_ptr<CodecData> &codecData) const
     946              : {
     947           13 :     if (codecData && CodecDataType::BUFFER == codecData->type)
     948              :     {
     949            7 :         gpointer memory = m_glibWrapper->gMemdup(codecData->data.data(), codecData->data.size());
     950            7 :         GstBuffer *buf = m_gstWrapper->gstBufferNewWrapped(memory, codecData->data.size());
     951            7 :         m_gstWrapper->gstCapsSetSimple(caps, "codec_data", GST_TYPE_BUFFER, buf, nullptr);
     952            7 :         m_gstWrapper->gstBufferUnref(buf);
     953            7 :         return true;
     954              :     }
     955            6 :     if (codecData && CodecDataType::STRING == codecData->type)
     956              :     {
     957            2 :         std::string codecDataStr(codecData->data.begin(), codecData->data.end());
     958            2 :         m_gstWrapper->gstCapsSetSimple(caps, "codec_data", G_TYPE_STRING, codecDataStr.c_str(), nullptr);
     959            2 :         return true;
     960              :     }
     961            4 :     return false;
     962              : }
     963              : 
     964           12 : void GstGenericPlayer::pushSampleIfRequired(GstElement *source, const std::string &typeStr)
     965              : {
     966           12 :     auto initialPosition = m_context.initialPositions.find(source);
     967           12 :     if (m_context.initialPositions.end() == initialPosition)
     968              :     {
     969              :         // Sending initial sample not needed
     970            7 :         return;
     971              :     }
     972              :     // GstAppSrc does not replace segment, if it's the same as previous one.
     973              :     // It causes problems with position reporing in amlogic devices, so we need to push
     974              :     // two segments with different reset time value.
     975            5 :     pushAdditionalSegmentIfRequired(source);
     976              : 
     977           10 :     for (const auto &[position, resetTime, appliedRate, stopPosition] : initialPosition->second)
     978              :     {
     979            6 :         GstSeekFlags seekFlag = resetTime ? GST_SEEK_FLAG_FLUSH : GST_SEEK_FLAG_NONE;
     980            6 :         RIALTO_SERVER_LOG_DEBUG("Pushing new %s sample...", typeStr.c_str());
     981            6 :         GstSegment *segment{m_gstWrapper->gstSegmentNew()};
     982            6 :         m_gstWrapper->gstSegmentInit(segment, GST_FORMAT_TIME);
     983            6 :         if (!m_gstWrapper->gstSegmentDoSeek(segment, m_context.playbackRate, GST_FORMAT_TIME, seekFlag,
     984              :                                             GST_SEEK_TYPE_SET, position, GST_SEEK_TYPE_SET, stopPosition, nullptr))
     985              :         {
     986            1 :             RIALTO_SERVER_LOG_WARN("Segment seek failed.");
     987            1 :             m_gstWrapper->gstSegmentFree(segment);
     988            1 :             m_context.initialPositions.erase(initialPosition);
     989            1 :             return;
     990              :         }
     991            5 :         segment->applied_rate = appliedRate;
     992            5 :         RIALTO_SERVER_LOG_MIL("New %s segment: [%" GST_TIME_FORMAT ", %" GST_TIME_FORMAT
     993              :                               "], rate: %f, appliedRate %f, reset_time: %d\n",
     994              :                               typeStr.c_str(), GST_TIME_ARGS(segment->start), GST_TIME_ARGS(segment->stop),
     995              :                               segment->rate, segment->applied_rate, resetTime);
     996              : 
     997            5 :         GstCaps *currentCaps = m_gstWrapper->gstAppSrcGetCaps(GST_APP_SRC(source));
     998              :         // We can't pass buffer in GstSample, because implementation of gst_app_src_push_sample
     999              :         // uses gst_buffer_copy, which loses RialtoProtectionMeta (that causes problems with EME
    1000              :         // for first frame).
    1001            5 :         GstSample *sample = m_gstWrapper->gstSampleNew(nullptr, currentCaps, segment, nullptr);
    1002            5 :         m_gstWrapper->gstAppSrcPushSample(GST_APP_SRC(source), sample);
    1003            5 :         m_gstWrapper->gstSampleUnref(sample);
    1004            5 :         m_gstWrapper->gstCapsUnref(currentCaps);
    1005              : 
    1006            5 :         m_gstWrapper->gstSegmentFree(segment);
    1007              :     }
    1008            4 :     m_context.currentPosition[source] = initialPosition->second.back();
    1009            4 :     m_context.initialPositions.erase(initialPosition);
    1010            4 :     return;
    1011              : }
    1012              : 
    1013            5 : void GstGenericPlayer::pushAdditionalSegmentIfRequired(GstElement *source)
    1014              : {
    1015            5 :     auto currentPosition = m_context.currentPosition.find(source);
    1016            5 :     if (m_context.currentPosition.end() == currentPosition)
    1017              :     {
    1018            4 :         return;
    1019              :     }
    1020            1 :     auto initialPosition = m_context.initialPositions.find(source);
    1021            1 :     if (m_context.initialPositions.end() == initialPosition)
    1022              :     {
    1023            0 :         return;
    1024              :     }
    1025            2 :     if (initialPosition->second.size() == 1 && initialPosition->second.back().resetTime &&
    1026            1 :         currentPosition->second == initialPosition->second.back())
    1027              :     {
    1028            1 :         RIALTO_SERVER_LOG_INFO("Adding additional segment with reset_time = false");
    1029            1 :         SegmentData additionalSegment = initialPosition->second.back();
    1030            1 :         additionalSegment.resetTime = false;
    1031            1 :         initialPosition->second.push_back(additionalSegment);
    1032              :     }
    1033              : }
    1034              : 
    1035            2 : void GstGenericPlayer::setTextTrackPositionIfRequired(GstElement *source)
    1036              : {
    1037            2 :     auto initialPosition = m_context.initialPositions.find(source);
    1038            2 :     if (m_context.initialPositions.end() == initialPosition)
    1039              :     {
    1040              :         // Sending initial sample not needed
    1041            1 :         return;
    1042              :     }
    1043              : 
    1044            1 :     RIALTO_SERVER_LOG_MIL("New subtitle position set %" GST_TIME_FORMAT,
    1045              :                           GST_TIME_ARGS(initialPosition->second.back().position));
    1046            1 :     m_glibWrapper->gObjectSet(m_context.subtitleSink, "position",
    1047            1 :                               static_cast<guint64>(initialPosition->second.back().position), nullptr);
    1048              : 
    1049            1 :     m_context.initialPositions.erase(initialPosition);
    1050              : }
    1051              : 
    1052            7 : bool GstGenericPlayer::reattachSource(const std::unique_ptr<IMediaPipeline::MediaSource> &source)
    1053              : {
    1054            7 :     if (m_context.streamInfo.find(source->getType()) == m_context.streamInfo.end())
    1055              :     {
    1056            1 :         RIALTO_SERVER_LOG_ERROR("Unable to switch source, type does not exist");
    1057            1 :         return false;
    1058              :     }
    1059            6 :     if (source->getMimeType().empty())
    1060              :     {
    1061            1 :         RIALTO_SERVER_LOG_WARN("Skip switch audio source. Unknown mime type");
    1062            1 :         return false;
    1063              :     }
    1064            5 :     std::optional<firebolt::rialto::wrappers::AudioAttributesPrivate> audioAttributes{createAudioAttributes(source)};
    1065            5 :     if (!audioAttributes)
    1066              :     {
    1067            1 :         RIALTO_SERVER_LOG_ERROR("Failed to create audio attributes");
    1068            1 :         return false;
    1069              :     }
    1070              : 
    1071            4 :     long long currentDispPts = getPosition(m_context.pipeline); // NOLINT(runtime/int)
    1072            4 :     GstCaps *caps{createCapsFromMediaSource(m_gstWrapper, m_glibWrapper, source)};
    1073            4 :     GstAppSrc *appSrc{GST_APP_SRC(m_context.streamInfo[source->getType()].appSrc)};
    1074            4 :     GstCaps *oldCaps = m_gstWrapper->gstAppSrcGetCaps(appSrc);
    1075            4 :     if ((!oldCaps) || (!m_gstWrapper->gstCapsIsEqual(caps, oldCaps)))
    1076              :     {
    1077            3 :         RIALTO_SERVER_LOG_DEBUG("Caps not equal. Perform audio track codec channel switch.");
    1078            3 :         int sampleAttributes{
    1079              :             0}; // rdk_gstreamer_utils::performAudioTrackCodecChannelSwitch checks if this param != NULL only.
    1080            3 :         std::uint32_t status{0};   // must be 0 to make rdk_gstreamer_utils::performAudioTrackCodecChannelSwitch work
    1081            3 :         unsigned int ui32Delay{0}; // output param
    1082            3 :         long long audioChangeTargetPts{-1}; // NOLINT(runtime/int) output param. Set audioChangeTargetPts =
    1083              :                                             // currentDispPts in rdk_gstreamer_utils function stub
    1084            3 :         unsigned int audioChangeStage{0};   // Output param. Set to AUDCHG_ALIGN in rdk_gstreamer_utils function stub
    1085            3 :         gchar *oldCapsCStr = m_gstWrapper->gstCapsToString(oldCaps);
    1086            3 :         std::string oldCapsStr = std::string(oldCapsCStr);
    1087            3 :         m_glibWrapper->gFree(oldCapsCStr);
    1088            3 :         bool audioAac{oldCapsStr.find("audio/mpeg") != std::string::npos};
    1089            3 :         bool svpEnabled{true}; // assume always true
    1090            3 :         bool retVal{false};    // Output param. Set to TRUE in rdk_gstreamer_utils function stub
    1091              :         bool result =
    1092            3 :             m_rdkGstreamerUtilsWrapper
    1093            6 :                 ->performAudioTrackCodecChannelSwitch(&m_context.playbackGroup, &sampleAttributes, &(*audioAttributes),
    1094              :                                                       &status, &ui32Delay, &audioChangeTargetPts, &currentDispPts,
    1095              :                                                       &audioChangeStage,
    1096              :                                                       &caps, // may fail for amlogic - that implementation changes
    1097              :                                                              // this parameter, it's probably used by Netflix later
    1098            3 :                                                       &audioAac, svpEnabled, GST_ELEMENT(appSrc), &retVal);
    1099              : 
    1100            3 :         if (!result || !retVal)
    1101              :         {
    1102            3 :             RIALTO_SERVER_LOG_WARN("performAudioTrackCodecChannelSwitch failed! Result: %d, retval %d", result, retVal);
    1103              :         }
    1104              :     }
    1105              :     else
    1106              :     {
    1107            1 :         RIALTO_SERVER_LOG_DEBUG("Skip switching audio source - caps are the same.");
    1108              :     }
    1109              : 
    1110            4 :     m_context.lastAudioSampleTimestamps = currentDispPts;
    1111            4 :     if (caps)
    1112            4 :         m_gstWrapper->gstCapsUnref(caps);
    1113            4 :     if (oldCaps)
    1114            4 :         m_gstWrapper->gstCapsUnref(oldCaps);
    1115              : 
    1116            4 :     return true;
    1117            5 : }
    1118              : 
    1119            0 : bool GstGenericPlayer::hasSourceType(const MediaSourceType &mediaSourceType) const
    1120              : {
    1121            0 :     return m_context.streamInfo.find(mediaSourceType) != m_context.streamInfo.end();
    1122              : }
    1123              : 
    1124           88 : void GstGenericPlayer::scheduleNeedMediaData(GstAppSrc *src)
    1125              : {
    1126           88 :     if (m_workerThread)
    1127              :     {
    1128           88 :         m_workerThread->enqueueTask(m_taskFactory->createNeedData(m_context, *this, src));
    1129              :     }
    1130              : }
    1131              : 
    1132            1 : void GstGenericPlayer::scheduleEnoughData(GstAppSrc *src)
    1133              : {
    1134            1 :     if (m_workerThread)
    1135              :     {
    1136            1 :         m_workerThread->enqueueTask(m_taskFactory->createEnoughData(m_context, src));
    1137              :     }
    1138              : }
    1139              : 
    1140            3 : void GstGenericPlayer::scheduleAudioUnderflow()
    1141              : {
    1142            3 :     if (m_workerThread)
    1143              :     {
    1144            3 :         bool underflowEnabled = m_context.isPlaying && !m_context.audioSourceRemoved;
    1145            6 :         m_workerThread->enqueueTask(
    1146            6 :             m_taskFactory->createUnderflow(m_context, *this, underflowEnabled, MediaSourceType::AUDIO));
    1147              :     }
    1148            3 : }
    1149              : 
    1150            2 : void GstGenericPlayer::scheduleVideoUnderflow()
    1151              : {
    1152            2 :     if (m_workerThread)
    1153              :     {
    1154            2 :         bool underflowEnabled = m_context.isPlaying;
    1155            4 :         m_workerThread->enqueueTask(
    1156            4 :             m_taskFactory->createUnderflow(m_context, *this, underflowEnabled, MediaSourceType::VIDEO));
    1157              :     }
    1158            2 : }
    1159              : 
    1160            1 : void GstGenericPlayer::scheduleAllSourcesAttached()
    1161              : {
    1162            1 :     allSourcesAttached();
    1163              : }
    1164              : 
    1165           14 : void GstGenericPlayer::cancelUnderflow(firebolt::rialto::MediaSourceType mediaSource)
    1166              : {
    1167           14 :     auto elem = m_context.streamInfo.find(mediaSource);
    1168           14 :     if (elem != m_context.streamInfo.end())
    1169              :     {
    1170           14 :         StreamInfo &streamInfo = elem->second;
    1171           14 :         if (!streamInfo.underflowOccured)
    1172              :         {
    1173           11 :             return;
    1174              :         }
    1175              : 
    1176            3 :         RIALTO_SERVER_LOG_DEBUG("Cancelling %s underflow", common::convertMediaSourceType(mediaSource));
    1177            3 :         streamInfo.underflowOccured = false;
    1178              :     }
    1179              : }
    1180              : 
    1181            1 : void GstGenericPlayer::play()
    1182              : {
    1183            1 :     if (m_workerThread)
    1184              :     {
    1185            1 :         m_workerThread->enqueueTask(m_taskFactory->createPlay(*this));
    1186              :     }
    1187              : }
    1188              : 
    1189            1 : void GstGenericPlayer::pause()
    1190              : {
    1191            1 :     if (m_workerThread)
    1192              :     {
    1193            1 :         m_workerThread->enqueueTask(m_taskFactory->createPause(m_context, *this));
    1194              :     }
    1195              : }
    1196              : 
    1197            1 : void GstGenericPlayer::stop()
    1198              : {
    1199            1 :     if (m_workerThread)
    1200              :     {
    1201            1 :         m_workerThread->enqueueTask(m_taskFactory->createStop(m_context, *this));
    1202              :     }
    1203              : }
    1204              : 
    1205            4 : bool GstGenericPlayer::changePipelineState(GstState newState)
    1206              : {
    1207            4 :     if (!m_context.pipeline)
    1208              :     {
    1209            1 :         RIALTO_SERVER_LOG_ERROR("Change state failed - pipeline is nullptr");
    1210            1 :         if (m_gstPlayerClient)
    1211            1 :             m_gstPlayerClient->notifyPlaybackState(PlaybackState::FAILURE);
    1212            1 :         return false;
    1213              :     }
    1214            3 :     if (m_gstWrapper->gstElementSetState(m_context.pipeline, newState) == GST_STATE_CHANGE_FAILURE)
    1215              :     {
    1216            1 :         RIALTO_SERVER_LOG_ERROR("Change state failed - Gstreamer returned an error");
    1217            1 :         if (m_gstPlayerClient)
    1218            1 :             m_gstPlayerClient->notifyPlaybackState(PlaybackState::FAILURE);
    1219            1 :         return false;
    1220              :     }
    1221            2 :     return true;
    1222              : }
    1223              : 
    1224           15 : int64_t GstGenericPlayer::getPosition(GstElement *element)
    1225              : {
    1226           15 :     if (!element)
    1227              :     {
    1228            1 :         RIALTO_SERVER_LOG_WARN("Element is null");
    1229            1 :         return -1;
    1230              :     }
    1231              : 
    1232           14 :     m_gstWrapper->gstStateLock(element);
    1233              : 
    1234           28 :     if (m_gstWrapper->gstElementGetState(element) < GST_STATE_PAUSED ||
    1235           14 :         (m_gstWrapper->gstElementGetStateReturn(element) == GST_STATE_CHANGE_ASYNC &&
    1236            1 :          m_gstWrapper->gstElementGetStateNext(element) == GST_STATE_PAUSED))
    1237              :     {
    1238            1 :         RIALTO_SERVER_LOG_WARN("Element is prerolling or in invalid state - state: %s, return: %s, next: %s",
    1239              :                                m_gstWrapper->gstElementStateGetName(m_gstWrapper->gstElementGetState(element)),
    1240              :                                m_gstWrapper->gstElementStateChangeReturnGetName(
    1241              :                                    m_gstWrapper->gstElementGetStateReturn(element)),
    1242              :                                m_gstWrapper->gstElementStateGetName(m_gstWrapper->gstElementGetStateNext(element)));
    1243              : 
    1244            1 :         m_gstWrapper->gstStateUnlock(element);
    1245            1 :         return -1;
    1246              :     }
    1247           13 :     m_gstWrapper->gstStateUnlock(element);
    1248              : 
    1249           13 :     gint64 position = -1;
    1250           13 :     if (!m_gstWrapper->gstElementQueryPosition(m_context.pipeline, GST_FORMAT_TIME, &position))
    1251              :     {
    1252            1 :         RIALTO_SERVER_LOG_WARN("Failed to query position");
    1253            1 :         return -1;
    1254              :     }
    1255              : 
    1256           12 :     return position;
    1257              : }
    1258              : 
    1259            1 : void GstGenericPlayer::setVideoGeometry(int x, int y, int width, int height)
    1260              : {
    1261            1 :     if (m_workerThread)
    1262              :     {
    1263            2 :         m_workerThread->enqueueTask(
    1264            2 :             m_taskFactory->createSetVideoGeometry(m_context, *this, Rectangle{x, y, width, height}));
    1265              :     }
    1266            1 : }
    1267              : 
    1268            1 : void GstGenericPlayer::setEos(const firebolt::rialto::MediaSourceType &type)
    1269              : {
    1270            1 :     if (m_workerThread)
    1271              :     {
    1272            1 :         m_workerThread->enqueueTask(m_taskFactory->createEos(m_context, *this, type));
    1273              :     }
    1274              : }
    1275              : 
    1276            4 : bool GstGenericPlayer::setVideoSinkRectangle()
    1277              : {
    1278            4 :     bool result = false;
    1279            4 :     GstElement *videoSink{getSink(MediaSourceType::VIDEO)};
    1280            4 :     if (videoSink)
    1281              :     {
    1282            3 :         if (m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(videoSink), "rectangle"))
    1283              :         {
    1284              :             std::string rect =
    1285            4 :                 std::to_string(m_context.pendingGeometry.x) + ',' + std::to_string(m_context.pendingGeometry.y) + ',' +
    1286            6 :                 std::to_string(m_context.pendingGeometry.width) + ',' + std::to_string(m_context.pendingGeometry.height);
    1287            2 :             m_glibWrapper->gObjectSet(videoSink, "rectangle", rect.c_str(), nullptr);
    1288            2 :             m_context.pendingGeometry.clear();
    1289            2 :             result = true;
    1290              :         }
    1291              :         else
    1292              :         {
    1293            1 :             RIALTO_SERVER_LOG_ERROR("Failed to set the video rectangle");
    1294              :         }
    1295            3 :         m_gstWrapper->gstObjectUnref(videoSink);
    1296              :     }
    1297              :     else
    1298              :     {
    1299            1 :         RIALTO_SERVER_LOG_ERROR("Failed to set video rectangle, sink is NULL");
    1300              :     }
    1301              : 
    1302            4 :     return result;
    1303              : }
    1304              : 
    1305            3 : bool GstGenericPlayer::setImmediateOutput()
    1306              : {
    1307            3 :     bool result{false};
    1308            3 :     if (m_context.pendingImmediateOutputForVideo.has_value())
    1309              :     {
    1310            3 :         GstElement *sink{getSink(MediaSourceType::VIDEO)};
    1311            3 :         if (sink)
    1312              :         {
    1313            2 :             bool immediateOutput{m_context.pendingImmediateOutputForVideo.value()};
    1314            2 :             RIALTO_SERVER_LOG_DEBUG("Set immediate-output to %s", immediateOutput ? "TRUE" : "FALSE");
    1315              : 
    1316            2 :             if (m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(sink), "immediate-output"))
    1317              :             {
    1318            1 :                 gboolean immediateOutputGboolean{immediateOutput ? TRUE : FALSE};
    1319            1 :                 m_glibWrapper->gObjectSet(sink, "immediate-output", immediateOutputGboolean, nullptr);
    1320            1 :                 result = true;
    1321              :             }
    1322              :             else
    1323              :             {
    1324            1 :                 RIALTO_SERVER_LOG_ERROR("Failed to set immediate-output property on sink '%s'", GST_ELEMENT_NAME(sink));
    1325              :             }
    1326            2 :             m_context.pendingImmediateOutputForVideo.reset();
    1327            2 :             m_gstWrapper->gstObjectUnref(sink);
    1328              :         }
    1329              :         else
    1330              :         {
    1331            1 :             RIALTO_SERVER_LOG_DEBUG("Pending an immediate-output, sink is NULL");
    1332              :         }
    1333              :     }
    1334            3 :     return result;
    1335              : }
    1336              : 
    1337            4 : bool GstGenericPlayer::setShowVideoWindow()
    1338              : {
    1339            4 :     if (!m_context.pendingShowVideoWindow.has_value())
    1340              :     {
    1341            1 :         RIALTO_SERVER_LOG_WARN("No show video window value to be set. Aborting...");
    1342            1 :         return false;
    1343              :     }
    1344              : 
    1345            3 :     GstElement *videoSink{getSink(MediaSourceType::VIDEO)};
    1346            3 :     if (!videoSink)
    1347              :     {
    1348            1 :         RIALTO_SERVER_LOG_DEBUG("Setting show video window queued. Video sink is NULL");
    1349            1 :         return false;
    1350              :     }
    1351            2 :     bool result{false};
    1352            2 :     if (m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(videoSink), "show-video-window"))
    1353              :     {
    1354            1 :         m_glibWrapper->gObjectSet(videoSink, "show-video-window", m_context.pendingShowVideoWindow.value(), nullptr);
    1355            1 :         result = true;
    1356              :     }
    1357              :     else
    1358              :     {
    1359            1 :         RIALTO_SERVER_LOG_ERROR("Setting show video window failed. Property does not exist");
    1360              :     }
    1361            2 :     m_context.pendingShowVideoWindow.reset();
    1362            2 :     m_gstWrapper->gstObjectUnref(GST_OBJECT(videoSink));
    1363            2 :     return result;
    1364              : }
    1365              : 
    1366            4 : bool GstGenericPlayer::setLowLatency()
    1367              : {
    1368            4 :     bool result{false};
    1369            4 :     if (m_context.pendingLowLatency.has_value())
    1370              :     {
    1371            4 :         GstElement *sink{getSink(MediaSourceType::AUDIO)};
    1372            4 :         if (sink)
    1373              :         {
    1374            3 :             bool lowLatency{m_context.pendingLowLatency.value()};
    1375            3 :             RIALTO_SERVER_LOG_DEBUG("Set low-latency to %s", lowLatency ? "TRUE" : "FALSE");
    1376              : 
    1377            3 :             if (m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(sink), "low-latency"))
    1378              :             {
    1379            2 :                 gboolean lowLatencyGboolean{lowLatency ? TRUE : FALSE};
    1380            2 :                 m_glibWrapper->gObjectSet(sink, "low-latency", lowLatencyGboolean, nullptr);
    1381            2 :                 result = true;
    1382              :             }
    1383              :             else
    1384              :             {
    1385            1 :                 RIALTO_SERVER_LOG_ERROR("Failed to set low-latency property on sink '%s'", GST_ELEMENT_NAME(sink));
    1386              :             }
    1387            3 :             m_context.pendingLowLatency.reset();
    1388            3 :             m_gstWrapper->gstObjectUnref(sink);
    1389              :         }
    1390              :         else
    1391              :         {
    1392            1 :             RIALTO_SERVER_LOG_DEBUG("Pending low-latency, sink is NULL");
    1393              :         }
    1394              :     }
    1395            4 :     return result;
    1396              : }
    1397              : 
    1398            3 : bool GstGenericPlayer::setSync()
    1399              : {
    1400            3 :     bool result{false};
    1401            3 :     if (m_context.pendingSync.has_value())
    1402              :     {
    1403            3 :         GstElement *sink{getSink(MediaSourceType::AUDIO)};
    1404            3 :         if (sink)
    1405              :         {
    1406            2 :             bool sync{m_context.pendingSync.value()};
    1407            2 :             RIALTO_SERVER_LOG_DEBUG("Set sync to %s", sync ? "TRUE" : "FALSE");
    1408              : 
    1409            2 :             if (m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(sink), "sync"))
    1410              :             {
    1411            1 :                 gboolean syncGboolean{sync ? TRUE : FALSE};
    1412            1 :                 m_glibWrapper->gObjectSet(sink, "sync", syncGboolean, nullptr);
    1413            1 :                 result = true;
    1414              :             }
    1415              :             else
    1416              :             {
    1417            1 :                 RIALTO_SERVER_LOG_ERROR("Failed to set sync property on sink '%s'", GST_ELEMENT_NAME(sink));
    1418              :             }
    1419            2 :             m_context.pendingSync.reset();
    1420            2 :             m_gstWrapper->gstObjectUnref(sink);
    1421              :         }
    1422              :         else
    1423              :         {
    1424            1 :             RIALTO_SERVER_LOG_DEBUG("Pending sync, sink is NULL");
    1425              :         }
    1426              :     }
    1427            3 :     return result;
    1428              : }
    1429              : 
    1430            3 : bool GstGenericPlayer::setSyncOff()
    1431              : {
    1432            3 :     bool result{false};
    1433            3 :     if (m_context.pendingSyncOff.has_value())
    1434              :     {
    1435            3 :         GstElement *decoder = getDecoder(MediaSourceType::AUDIO);
    1436            3 :         if (decoder)
    1437              :         {
    1438            2 :             bool syncOff{m_context.pendingSyncOff.value()};
    1439            2 :             RIALTO_SERVER_LOG_DEBUG("Set sync-off to %s", syncOff ? "TRUE" : "FALSE");
    1440              : 
    1441            2 :             if (m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(decoder), "sync-off"))
    1442              :             {
    1443            1 :                 gboolean syncOffGboolean{decoder ? TRUE : FALSE};
    1444            1 :                 m_glibWrapper->gObjectSet(decoder, "sync-off", syncOffGboolean, nullptr);
    1445            1 :                 result = true;
    1446              :             }
    1447              :             else
    1448              :             {
    1449            1 :                 RIALTO_SERVER_LOG_ERROR("Failed to set sync-off property on decoder '%s'", GST_ELEMENT_NAME(decoder));
    1450              :             }
    1451            2 :             m_context.pendingSyncOff.reset();
    1452            2 :             m_gstWrapper->gstObjectUnref(decoder);
    1453              :         }
    1454              :         else
    1455              :         {
    1456            1 :             RIALTO_SERVER_LOG_DEBUG("Pending sync-off, decoder is NULL");
    1457              :         }
    1458              :     }
    1459            3 :     return result;
    1460              : }
    1461              : 
    1462            6 : bool GstGenericPlayer::setStreamSyncMode(const MediaSourceType &type)
    1463              : {
    1464            6 :     bool result{false};
    1465            6 :     int32_t streamSyncMode{0};
    1466              :     {
    1467            6 :         std::unique_lock lock{m_context.propertyMutex};
    1468            6 :         if (m_context.pendingStreamSyncMode.find(type) == m_context.pendingStreamSyncMode.end())
    1469              :         {
    1470            0 :             return false;
    1471              :         }
    1472            6 :         streamSyncMode = m_context.pendingStreamSyncMode[type];
    1473              :     }
    1474            6 :     if (MediaSourceType::AUDIO == type)
    1475              :     {
    1476            3 :         GstElement *decoder = getDecoder(MediaSourceType::AUDIO);
    1477            3 :         if (!decoder)
    1478              :         {
    1479            1 :             RIALTO_SERVER_LOG_DEBUG("Pending stream-sync-mode, decoder is NULL");
    1480            1 :             return false;
    1481              :         }
    1482              : 
    1483            2 :         RIALTO_SERVER_LOG_DEBUG("Set stream-sync-mode to %d", streamSyncMode);
    1484              : 
    1485            2 :         if (m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(decoder), "stream-sync-mode"))
    1486              :         {
    1487            1 :             gint streamSyncModeGint{static_cast<gint>(streamSyncMode)};
    1488            1 :             m_glibWrapper->gObjectSet(decoder, "stream-sync-mode", streamSyncModeGint, nullptr);
    1489            1 :             result = true;
    1490              :         }
    1491              :         else
    1492              :         {
    1493            1 :             RIALTO_SERVER_LOG_ERROR("Failed to set stream-sync-mode property on decoder '%s'", GST_ELEMENT_NAME(decoder));
    1494              :         }
    1495            2 :         m_gstWrapper->gstObjectUnref(decoder);
    1496            2 :         std::unique_lock lock{m_context.propertyMutex};
    1497            2 :         m_context.pendingStreamSyncMode.erase(type);
    1498              :     }
    1499            3 :     else if (MediaSourceType::VIDEO == type)
    1500              :     {
    1501            3 :         GstElement *parser = getParser(MediaSourceType::VIDEO);
    1502            3 :         if (!parser)
    1503              :         {
    1504            1 :             RIALTO_SERVER_LOG_DEBUG("Pending syncmode-streaming, parser is NULL");
    1505            1 :             return false;
    1506              :         }
    1507              : 
    1508            2 :         gboolean streamSyncModeBoolean{static_cast<gboolean>(streamSyncMode)};
    1509            2 :         RIALTO_SERVER_LOG_DEBUG("Set syncmode-streaming to %d", streamSyncMode);
    1510              : 
    1511            2 :         if (m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(parser), "syncmode-streaming"))
    1512              :         {
    1513            1 :             m_glibWrapper->gObjectSet(parser, "syncmode-streaming", streamSyncModeBoolean, nullptr);
    1514            1 :             result = true;
    1515              :         }
    1516              :         else
    1517              :         {
    1518            1 :             RIALTO_SERVER_LOG_ERROR("Failed to set syncmode-streaming property on parser '%s'", GST_ELEMENT_NAME(parser));
    1519              :         }
    1520            2 :         m_gstWrapper->gstObjectUnref(parser);
    1521            2 :         std::unique_lock lock{m_context.propertyMutex};
    1522            2 :         m_context.pendingStreamSyncMode.erase(type);
    1523              :     }
    1524            4 :     return result;
    1525              : }
    1526              : 
    1527            3 : bool GstGenericPlayer::setRenderFrame()
    1528              : {
    1529            3 :     bool result{false};
    1530            3 :     if (m_context.pendingRenderFrame)
    1531              :     {
    1532            5 :         static const std::string kStepOnPrerollPropertyName = "frame-step-on-preroll";
    1533            3 :         GstElement *sink{getSink(MediaSourceType::VIDEO)};
    1534            3 :         if (sink)
    1535              :         {
    1536            2 :             if (m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(sink), kStepOnPrerollPropertyName.c_str()))
    1537              :             {
    1538            1 :                 RIALTO_SERVER_LOG_INFO("Rendering preroll");
    1539              : 
    1540            1 :                 m_glibWrapper->gObjectSet(sink, kStepOnPrerollPropertyName.c_str(), 1, nullptr);
    1541            1 :                 m_gstWrapper->gstElementSendEvent(sink, m_gstWrapper->gstEventNewStep(GST_FORMAT_BUFFERS, 1, 1.0, true,
    1542              :                                                                                       false));
    1543            1 :                 m_glibWrapper->gObjectSet(sink, kStepOnPrerollPropertyName.c_str(), 0, nullptr);
    1544            1 :                 result = true;
    1545              :             }
    1546              :             else
    1547              :             {
    1548            1 :                 RIALTO_SERVER_LOG_ERROR("Video sink doesn't have property `%s`", kStepOnPrerollPropertyName.c_str());
    1549              :             }
    1550            2 :             m_gstWrapper->gstObjectUnref(sink);
    1551            2 :             m_context.pendingRenderFrame = false;
    1552              :         }
    1553              :         else
    1554              :         {
    1555            1 :             RIALTO_SERVER_LOG_DEBUG("Pending render frame, sink is NULL");
    1556              :         }
    1557              :     }
    1558            3 :     return result;
    1559              : }
    1560              : 
    1561            3 : bool GstGenericPlayer::setBufferingLimit()
    1562              : {
    1563            3 :     bool result{false};
    1564            3 :     guint bufferingLimit{0};
    1565              :     {
    1566            3 :         std::unique_lock lock{m_context.propertyMutex};
    1567            3 :         if (!m_context.pendingBufferingLimit.has_value())
    1568              :         {
    1569            0 :             return false;
    1570              :         }
    1571            3 :         bufferingLimit = static_cast<guint>(m_context.pendingBufferingLimit.value());
    1572              :     }
    1573              : 
    1574            3 :     GstElement *decoder{getDecoder(MediaSourceType::AUDIO)};
    1575            3 :     if (decoder)
    1576              :     {
    1577            2 :         RIALTO_SERVER_LOG_DEBUG("Set limit-buffering-ms to %u", bufferingLimit);
    1578              : 
    1579            2 :         if (m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(decoder), "limit-buffering-ms"))
    1580              :         {
    1581            1 :             m_glibWrapper->gObjectSet(decoder, "limit-buffering-ms", bufferingLimit, nullptr);
    1582            1 :             result = true;
    1583              :         }
    1584              :         else
    1585              :         {
    1586            1 :             RIALTO_SERVER_LOG_ERROR("Failed to set limit-buffering-ms property on decoder '%s'",
    1587              :                                     GST_ELEMENT_NAME(decoder));
    1588              :         }
    1589            2 :         m_gstWrapper->gstObjectUnref(decoder);
    1590            2 :         std::unique_lock lock{m_context.propertyMutex};
    1591            2 :         m_context.pendingBufferingLimit.reset();
    1592              :     }
    1593              :     else
    1594              :     {
    1595            1 :         RIALTO_SERVER_LOG_DEBUG("Pending limit-buffering-ms, decoder is NULL");
    1596              :     }
    1597            3 :     return result;
    1598              : }
    1599              : 
    1600            2 : bool GstGenericPlayer::setUseBuffering()
    1601              : {
    1602            2 :     std::unique_lock lock{m_context.propertyMutex};
    1603            2 :     if (m_context.pendingUseBuffering.has_value())
    1604              :     {
    1605            2 :         if (m_context.playbackGroup.m_curAudioDecodeBin)
    1606              :         {
    1607            1 :             gboolean useBufferingGboolean{m_context.pendingUseBuffering.value() ? TRUE : FALSE};
    1608            1 :             RIALTO_SERVER_LOG_DEBUG("Set use-buffering to %d", useBufferingGboolean);
    1609            1 :             m_glibWrapper->gObjectSet(m_context.playbackGroup.m_curAudioDecodeBin, "use-buffering",
    1610              :                                       useBufferingGboolean, nullptr);
    1611            1 :             m_context.pendingUseBuffering.reset();
    1612            1 :             return true;
    1613              :         }
    1614              :         else
    1615              :         {
    1616            1 :             RIALTO_SERVER_LOG_DEBUG("Pending use-buffering, decodebin is NULL");
    1617              :         }
    1618              :     }
    1619            1 :     return false;
    1620            2 : }
    1621              : 
    1622            8 : bool GstGenericPlayer::setWesterossinkSecondaryVideo()
    1623              : {
    1624            8 :     bool result = false;
    1625            8 :     GstElementFactory *factory = m_gstWrapper->gstElementFactoryFind("westerossink");
    1626            8 :     if (factory)
    1627              :     {
    1628            7 :         GstElement *videoSink = m_gstWrapper->gstElementFactoryCreate(factory, nullptr);
    1629            7 :         if (videoSink)
    1630              :         {
    1631            5 :             if (m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(videoSink), "res-usage"))
    1632              :             {
    1633            4 :                 m_glibWrapper->gObjectSet(videoSink, "res-usage", 0x0u, nullptr);
    1634            4 :                 m_glibWrapper->gObjectSet(m_context.pipeline, "video-sink", videoSink, nullptr);
    1635            4 :                 result = true;
    1636              :             }
    1637              :             else
    1638              :             {
    1639            1 :                 RIALTO_SERVER_LOG_ERROR("Failed to set the westerossink res-usage");
    1640            1 :                 m_gstWrapper->gstObjectUnref(GST_OBJECT(videoSink));
    1641              :             }
    1642              :         }
    1643              :         else
    1644              :         {
    1645            2 :             RIALTO_SERVER_LOG_ERROR("Failed to create the westerossink");
    1646              :         }
    1647              : 
    1648            7 :         m_gstWrapper->gstObjectUnref(GST_OBJECT(factory));
    1649              :     }
    1650              :     else
    1651              :     {
    1652              :         // No westeros sink
    1653            1 :         result = true;
    1654              :     }
    1655              : 
    1656            8 :     return result;
    1657              : }
    1658              : 
    1659            8 : bool GstGenericPlayer::setErmContext()
    1660              : {
    1661            8 :     bool result = false;
    1662            8 :     GstContext *context = m_gstWrapper->gstContextNew("erm", false);
    1663            8 :     if (context)
    1664              :     {
    1665            6 :         GstStructure *contextStructure = m_gstWrapper->gstContextWritableStructure(context);
    1666            6 :         if (contextStructure)
    1667              :         {
    1668            5 :             m_gstWrapper->gstStructureSet(contextStructure, "res-usage", G_TYPE_UINT, 0x0u, nullptr);
    1669            5 :             m_gstWrapper->gstElementSetContext(GST_ELEMENT(m_context.pipeline), context);
    1670            5 :             result = true;
    1671              :         }
    1672              :         else
    1673              :         {
    1674            1 :             RIALTO_SERVER_LOG_ERROR("Failed to create the erm structure");
    1675              :         }
    1676            6 :         m_gstWrapper->gstContextUnref(context);
    1677              :     }
    1678              :     else
    1679              :     {
    1680            2 :         RIALTO_SERVER_LOG_ERROR("Failed to create the erm context");
    1681              :     }
    1682              : 
    1683            8 :     return result;
    1684              : }
    1685              : 
    1686            6 : void GstGenericPlayer::startPositionReportingAndCheckAudioUnderflowTimer()
    1687              : {
    1688              :     static constexpr std::chrono::milliseconds kPlaybackInfoTimerMs{32};
    1689            6 :     if (m_positionReportingAndCheckAudioUnderflowTimer && m_positionReportingAndCheckAudioUnderflowTimer->isActive())
    1690              :     {
    1691            1 :         return;
    1692              :     }
    1693              : 
    1694           15 :     m_positionReportingAndCheckAudioUnderflowTimer = m_timerFactory->createTimer(
    1695              :         kPositionReportTimerMs,
    1696            5 :         [this]()
    1697              :         {
    1698            1 :             if (m_workerThread)
    1699              :             {
    1700            1 :                 m_workerThread->enqueueTask(m_taskFactory->createReportPosition(m_context, *this));
    1701            1 :                 m_workerThread->enqueueTask(m_taskFactory->createCheckAudioUnderflow(m_context, *this));
    1702              :             }
    1703            1 :         },
    1704            5 :         firebolt::rialto::common::TimerType::PERIODIC);
    1705              : 
    1706            5 :     PlaybackInfo info;
    1707            5 :     getPosition(info.currentPosition);
    1708            5 :     getVolume(info.volume);
    1709            5 :     m_gstPlayerClient->notifyPlaybackInfo(info);
    1710              : 
    1711           15 :     m_playbackInfoTimer = m_timerFactory->createTimer(
    1712              :         kPlaybackInfoTimerMs,
    1713           10 :         [this]()
    1714              :         {
    1715            1 :             PlaybackInfo info;
    1716            1 :             getPosition(info.currentPosition);
    1717            1 :             getVolume(info.volume);
    1718            1 :             m_gstPlayerClient->notifyPlaybackInfo(info);
    1719            1 :         },
    1720            5 :         firebolt::rialto::common::TimerType::PERIODIC);
    1721              : }
    1722              : 
    1723            4 : void GstGenericPlayer::stopPositionReportingAndCheckAudioUnderflowTimer()
    1724              : {
    1725            4 :     if (m_positionReportingAndCheckAudioUnderflowTimer && m_positionReportingAndCheckAudioUnderflowTimer->isActive())
    1726              :     {
    1727            1 :         m_positionReportingAndCheckAudioUnderflowTimer->cancel();
    1728            1 :         m_positionReportingAndCheckAudioUnderflowTimer.reset();
    1729              :     }
    1730              : 
    1731            4 :     if (m_playbackInfoTimer && m_playbackInfoTimer->isActive())
    1732              :     {
    1733            1 :         m_playbackInfoTimer->cancel();
    1734            1 :         m_playbackInfoTimer.reset();
    1735              :     }
    1736            4 : }
    1737              : 
    1738            0 : void GstGenericPlayer::startSubtitleClockResyncTimer()
    1739              : {
    1740            0 :     if (m_subtitleClockResyncTimer && m_subtitleClockResyncTimer->isActive())
    1741              :     {
    1742            0 :         return;
    1743              :     }
    1744              : 
    1745            0 :     m_subtitleClockResyncTimer = m_timerFactory->createTimer(
    1746              :         kSubtitleClockResyncInterval,
    1747            0 :         [this]()
    1748              :         {
    1749            0 :             if (m_workerThread)
    1750              :             {
    1751            0 :                 m_workerThread->enqueueTask(m_taskFactory->createSynchroniseSubtitleClock(m_context, *this));
    1752              :             }
    1753            0 :         },
    1754            0 :         firebolt::rialto::common::TimerType::PERIODIC);
    1755              : }
    1756              : 
    1757            0 : void GstGenericPlayer::stopSubtitleClockResyncTimer()
    1758              : {
    1759            0 :     if (m_subtitleClockResyncTimer && m_subtitleClockResyncTimer->isActive())
    1760              :     {
    1761            0 :         m_subtitleClockResyncTimer->cancel();
    1762            0 :         m_subtitleClockResyncTimer.reset();
    1763              :     }
    1764              : }
    1765              : 
    1766            2 : void GstGenericPlayer::stopWorkerThread()
    1767              : {
    1768            2 :     if (m_workerThread)
    1769              :     {
    1770            2 :         m_workerThread->stop();
    1771              :     }
    1772              : }
    1773              : 
    1774            0 : void GstGenericPlayer::setPendingPlaybackRate()
    1775              : {
    1776            0 :     RIALTO_SERVER_LOG_INFO("Setting pending playback rate");
    1777            0 :     setPlaybackRate(m_context.pendingPlaybackRate);
    1778              : }
    1779              : 
    1780            1 : void GstGenericPlayer::renderFrame()
    1781              : {
    1782            1 :     if (m_workerThread)
    1783              :     {
    1784            1 :         m_workerThread->enqueueTask(m_taskFactory->createRenderFrame(m_context, *this));
    1785              :     }
    1786              : }
    1787              : 
    1788           18 : void GstGenericPlayer::setVolume(double targetVolume, uint32_t volumeDuration, firebolt::rialto::EaseType easeType)
    1789              : {
    1790           18 :     if (m_workerThread)
    1791              :     {
    1792           36 :         m_workerThread->enqueueTask(
    1793           36 :             m_taskFactory->createSetVolume(m_context, *this, targetVolume, volumeDuration, easeType));
    1794              :     }
    1795           18 : }
    1796              : 
    1797            9 : bool GstGenericPlayer::getVolume(double &currentVolume)
    1798              : {
    1799              :     // We are on main thread here, but m_context.pipeline can be used, because it's modified only in GstGenericPlayer
    1800              :     // constructor and destructor. GstGenericPlayer is created/destructed on main thread, so we won't have a crash here.
    1801            9 :     if (!m_context.pipeline)
    1802              :     {
    1803            0 :         return false;
    1804              :     }
    1805              : 
    1806              :     // NOTE: No gstreamer documentation for "fade-volume" could be found at the time this code was written.
    1807              :     // Therefore the author performed several tests on a supported platform (Flex2) to determine the behaviour of this property.
    1808              :     // The code has been written to be backwardly compatible on platforms that don't have this property.
    1809              :     // The observed behaviour was:
    1810              :     //    - if the returned fade volume is negative then audio-fade is not active. In this case the usual technique
    1811              :     //      to find volume in the pipeline works and is used.
    1812              :     //    - if the returned fade volume is positive then audio-fade is active. In this case the returned fade volume
    1813              :     //      directly returns the current volume level 0=min to 100=max (and the pipeline's current volume level is
    1814              :     //      meaningless and doesn't contribute in this case).
    1815            9 :     GstElement *sink{getSink(MediaSourceType::AUDIO)};
    1816           11 :     if (m_context.audioFadeEnabled && sink &&
    1817            2 :         m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(sink), "fade-volume"))
    1818              :     {
    1819            2 :         gint fadeVolume{-100};
    1820            2 :         m_glibWrapper->gObjectGet(sink, "fade-volume", &fadeVolume, NULL);
    1821            2 :         if (fadeVolume < 0)
    1822              :         {
    1823            1 :             currentVolume = m_gstWrapper->gstStreamVolumeGetVolume(GST_STREAM_VOLUME(m_context.pipeline),
    1824              :                                                                    GST_STREAM_VOLUME_FORMAT_LINEAR);
    1825            1 :             RIALTO_SERVER_LOG_INFO("Fade volume is negative, using volume from pipeline: %f", currentVolume);
    1826              :         }
    1827              :         else
    1828              :         {
    1829            1 :             currentVolume = static_cast<double>(fadeVolume) / 100.0;
    1830            1 :             RIALTO_SERVER_LOG_INFO("Fade volume is supported: %f", currentVolume);
    1831              :         }
    1832              :     }
    1833              :     else
    1834              :     {
    1835            7 :         currentVolume = m_gstWrapper->gstStreamVolumeGetVolume(GST_STREAM_VOLUME(m_context.pipeline),
    1836              :                                                                GST_STREAM_VOLUME_FORMAT_LINEAR);
    1837            7 :         RIALTO_SERVER_LOG_INFO("Fade volume is not supported, using volume from pipeline: %f", currentVolume);
    1838              :     }
    1839              : 
    1840            9 :     if (sink)
    1841            2 :         m_gstWrapper->gstObjectUnref(sink);
    1842              : 
    1843            9 :     return true;
    1844              : }
    1845              : 
    1846            1 : void GstGenericPlayer::setMute(const MediaSourceType &mediaSourceType, bool mute)
    1847              : {
    1848            1 :     if (m_workerThread)
    1849              :     {
    1850            1 :         m_workerThread->enqueueTask(m_taskFactory->createSetMute(m_context, *this, mediaSourceType, mute));
    1851              :     }
    1852              : }
    1853              : 
    1854            5 : bool GstGenericPlayer::getMute(const MediaSourceType &mediaSourceType, bool &mute)
    1855              : {
    1856              :     // We are on main thread here, but m_context.pipeline can be used, because it's modified only in GstGenericPlayer
    1857              :     // constructor and destructor. GstGenericPlayer is created/destructed on main thread, so we won't have a crash here.
    1858            5 :     if (mediaSourceType == MediaSourceType::SUBTITLE)
    1859              :     {
    1860            2 :         if (!m_context.subtitleSink)
    1861              :         {
    1862            1 :             RIALTO_SERVER_LOG_ERROR("There is no subtitle sink");
    1863            1 :             return false;
    1864              :         }
    1865            1 :         gboolean muteValue{FALSE};
    1866            1 :         m_glibWrapper->gObjectGet(m_context.subtitleSink, "mute", &muteValue, nullptr);
    1867            1 :         mute = muteValue;
    1868              :     }
    1869            3 :     else if (mediaSourceType == MediaSourceType::AUDIO)
    1870              :     {
    1871            2 :         if (!m_context.pipeline)
    1872              :         {
    1873            1 :             return false;
    1874              :         }
    1875            1 :         mute = m_gstWrapper->gstStreamVolumeGetMute(GST_STREAM_VOLUME(m_context.pipeline));
    1876              :     }
    1877              :     else
    1878              :     {
    1879            1 :         RIALTO_SERVER_LOG_ERROR("Getting mute for type %s unsupported", common::convertMediaSourceType(mediaSourceType));
    1880            1 :         return false;
    1881              :     }
    1882              : 
    1883            2 :     return true;
    1884              : }
    1885              : 
    1886            1 : bool GstGenericPlayer::isAsync(const MediaSourceType &mediaSourceType) const
    1887              : {
    1888            1 :     GstElement *sink = getSink(mediaSourceType);
    1889            1 :     if (!sink)
    1890              :     {
    1891            0 :         RIALTO_SERVER_LOG_WARN("Sink not found for %s", common::convertMediaSourceType(mediaSourceType));
    1892            0 :         return true; // Our sinks are async by default
    1893              :     }
    1894            1 :     gboolean returnValue{TRUE};
    1895            1 :     m_glibWrapper->gObjectGet(sink, "async", &returnValue, nullptr);
    1896            1 :     m_gstWrapper->gstObjectUnref(sink);
    1897            1 :     return returnValue == TRUE;
    1898              : }
    1899              : 
    1900            1 : void GstGenericPlayer::setTextTrackIdentifier(const std::string &textTrackIdentifier)
    1901              : {
    1902            1 :     if (m_workerThread)
    1903              :     {
    1904            1 :         m_workerThread->enqueueTask(m_taskFactory->createSetTextTrackIdentifier(m_context, textTrackIdentifier));
    1905              :     }
    1906              : }
    1907              : 
    1908            3 : bool GstGenericPlayer::getTextTrackIdentifier(std::string &textTrackIdentifier)
    1909              : {
    1910            3 :     if (!m_context.subtitleSink)
    1911              :     {
    1912            1 :         RIALTO_SERVER_LOG_ERROR("There is no subtitle sink");
    1913            1 :         return false;
    1914              :     }
    1915              : 
    1916            2 :     gchar *identifier = nullptr;
    1917            2 :     m_glibWrapper->gObjectGet(m_context.subtitleSink, "text-track-identifier", &identifier, nullptr);
    1918              : 
    1919            2 :     if (identifier)
    1920              :     {
    1921            1 :         textTrackIdentifier = identifier;
    1922            1 :         m_glibWrapper->gFree(identifier);
    1923            1 :         return true;
    1924              :     }
    1925              :     else
    1926              :     {
    1927            1 :         RIALTO_SERVER_LOG_ERROR("Failed to get text track identifier");
    1928            1 :         return false;
    1929              :     }
    1930              : }
    1931              : 
    1932            1 : bool GstGenericPlayer::setLowLatency(bool lowLatency)
    1933              : {
    1934            1 :     if (m_workerThread)
    1935              :     {
    1936            1 :         m_workerThread->enqueueTask(m_taskFactory->createSetLowLatency(m_context, *this, lowLatency));
    1937              :     }
    1938            1 :     return true;
    1939              : }
    1940              : 
    1941            1 : bool GstGenericPlayer::setSync(bool sync)
    1942              : {
    1943            1 :     if (m_workerThread)
    1944              :     {
    1945            1 :         m_workerThread->enqueueTask(m_taskFactory->createSetSync(m_context, *this, sync));
    1946              :     }
    1947            1 :     return true;
    1948              : }
    1949              : 
    1950            4 : bool GstGenericPlayer::getSync(bool &sync)
    1951              : {
    1952            4 :     bool returnValue{false};
    1953            4 :     GstElement *sink{getSink(MediaSourceType::AUDIO)};
    1954            4 :     if (sink)
    1955              :     {
    1956            2 :         if (m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(sink), "sync"))
    1957              :         {
    1958            1 :             m_glibWrapper->gObjectGet(sink, "sync", &sync, nullptr);
    1959            1 :             returnValue = true;
    1960              :         }
    1961              :         else
    1962              :         {
    1963            1 :             RIALTO_SERVER_LOG_ERROR("Sync not supported in sink '%s'", GST_ELEMENT_NAME(sink));
    1964              :         }
    1965            2 :         m_gstWrapper->gstObjectUnref(sink);
    1966              :     }
    1967            2 :     else if (m_context.pendingSync.has_value())
    1968              :     {
    1969            1 :         RIALTO_SERVER_LOG_DEBUG("Returning queued value");
    1970            1 :         sync = m_context.pendingSync.value();
    1971            1 :         returnValue = true;
    1972              :     }
    1973              :     else
    1974              :     {
    1975              :         // We dont know the default setting on the sync, so return failure here
    1976            1 :         RIALTO_SERVER_LOG_WARN("No audio sink attached or queued value");
    1977              :     }
    1978              : 
    1979            4 :     return returnValue;
    1980              : }
    1981              : 
    1982            1 : bool GstGenericPlayer::setSyncOff(bool syncOff)
    1983              : {
    1984            1 :     if (m_workerThread)
    1985              :     {
    1986            1 :         m_workerThread->enqueueTask(m_taskFactory->createSetSyncOff(m_context, *this, syncOff));
    1987              :     }
    1988            1 :     return true;
    1989              : }
    1990              : 
    1991            1 : bool GstGenericPlayer::setStreamSyncMode(const MediaSourceType &mediaSourceType, int32_t streamSyncMode)
    1992              : {
    1993            1 :     if (m_workerThread)
    1994              :     {
    1995            2 :         m_workerThread->enqueueTask(
    1996            2 :             m_taskFactory->createSetStreamSyncMode(m_context, *this, mediaSourceType, streamSyncMode));
    1997              :     }
    1998            1 :     return true;
    1999              : }
    2000              : 
    2001            5 : bool GstGenericPlayer::getStreamSyncMode(int32_t &streamSyncMode)
    2002              : {
    2003            5 :     bool returnValue{false};
    2004            5 :     GstElement *decoder = getDecoder(MediaSourceType::AUDIO);
    2005            5 :     if (decoder && m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(decoder), "stream-sync-mode"))
    2006              :     {
    2007            2 :         m_glibWrapper->gObjectGet(decoder, "stream-sync-mode", &streamSyncMode, nullptr);
    2008            2 :         returnValue = true;
    2009              :     }
    2010              :     else
    2011              :     {
    2012            3 :         std::unique_lock lock{m_context.propertyMutex};
    2013            3 :         if (m_context.pendingStreamSyncMode.find(MediaSourceType::AUDIO) != m_context.pendingStreamSyncMode.end())
    2014              :         {
    2015            1 :             RIALTO_SERVER_LOG_DEBUG("Returning queued value");
    2016            1 :             streamSyncMode = m_context.pendingStreamSyncMode[MediaSourceType::AUDIO];
    2017            1 :             returnValue = true;
    2018              :         }
    2019              :         else
    2020              :         {
    2021            2 :             RIALTO_SERVER_LOG_ERROR("Stream sync mode not supported in decoder '%s'",
    2022              :                                     (decoder ? GST_ELEMENT_NAME(decoder) : "null"));
    2023              :         }
    2024            3 :     }
    2025              : 
    2026            5 :     if (decoder)
    2027            3 :         m_gstWrapper->gstObjectUnref(GST_OBJECT(decoder));
    2028              : 
    2029            5 :     return returnValue;
    2030              : }
    2031              : 
    2032            1 : void GstGenericPlayer::ping(std::unique_ptr<IHeartbeatHandler> &&heartbeatHandler)
    2033              : {
    2034            1 :     if (m_workerThread)
    2035              :     {
    2036            1 :         m_workerThread->enqueueTask(m_taskFactory->createPing(std::move(heartbeatHandler)));
    2037              :     }
    2038              : }
    2039              : 
    2040            1 : void GstGenericPlayer::flush(const MediaSourceType &mediaSourceType, bool resetTime, bool &async)
    2041              : {
    2042            1 :     if (m_workerThread)
    2043              :     {
    2044            1 :         async = isAsync(mediaSourceType);
    2045            1 :         m_flushWatcher->setFlushing(mediaSourceType, async);
    2046            1 :         m_workerThread->enqueueTask(m_taskFactory->createFlush(m_context, *this, mediaSourceType, resetTime));
    2047              :     }
    2048              : }
    2049              : 
    2050            1 : void GstGenericPlayer::setSourcePosition(const MediaSourceType &mediaSourceType, int64_t position, bool resetTime,
    2051              :                                          double appliedRate, uint64_t stopPosition)
    2052              : {
    2053            1 :     if (m_workerThread)
    2054              :     {
    2055            1 :         m_workerThread->enqueueTask(m_taskFactory->createSetSourcePosition(m_context, mediaSourceType, position,
    2056              :                                                                            resetTime, appliedRate, stopPosition));
    2057              :     }
    2058              : }
    2059              : 
    2060            0 : void GstGenericPlayer::setSubtitleOffset(int64_t position)
    2061              : {
    2062            0 :     if (m_workerThread)
    2063              :     {
    2064            0 :         m_workerThread->enqueueTask(m_taskFactory->createSetSubtitleOffset(m_context, position));
    2065              :     }
    2066              : }
    2067              : 
    2068            1 : void GstGenericPlayer::processAudioGap(int64_t position, uint32_t duration, int64_t discontinuityGap, bool audioAac)
    2069              : {
    2070            1 :     if (m_workerThread)
    2071              :     {
    2072            2 :         m_workerThread->enqueueTask(
    2073            2 :             m_taskFactory->createProcessAudioGap(m_context, position, duration, discontinuityGap, audioAac));
    2074              :     }
    2075            1 : }
    2076              : 
    2077            1 : void GstGenericPlayer::setBufferingLimit(uint32_t limitBufferingMs)
    2078              : {
    2079            1 :     if (m_workerThread)
    2080              :     {
    2081            1 :         m_workerThread->enqueueTask(m_taskFactory->createSetBufferingLimit(m_context, *this, limitBufferingMs));
    2082              :     }
    2083              : }
    2084              : 
    2085            5 : bool GstGenericPlayer::getBufferingLimit(uint32_t &limitBufferingMs)
    2086              : {
    2087            5 :     bool returnValue{false};
    2088            5 :     GstElement *decoder = getDecoder(MediaSourceType::AUDIO);
    2089            5 :     if (decoder && m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(decoder), "limit-buffering-ms"))
    2090              :     {
    2091            2 :         m_glibWrapper->gObjectGet(decoder, "limit-buffering-ms", &limitBufferingMs, nullptr);
    2092            2 :         returnValue = true;
    2093              :     }
    2094              :     else
    2095              :     {
    2096            3 :         std::unique_lock lock{m_context.propertyMutex};
    2097            3 :         if (m_context.pendingBufferingLimit.has_value())
    2098              :         {
    2099            1 :             RIALTO_SERVER_LOG_DEBUG("Returning queued value");
    2100            1 :             limitBufferingMs = m_context.pendingBufferingLimit.value();
    2101            1 :             returnValue = true;
    2102              :         }
    2103              :         else
    2104              :         {
    2105            2 :             RIALTO_SERVER_LOG_ERROR("buffering limit not supported in decoder '%s'",
    2106              :                                     (decoder ? GST_ELEMENT_NAME(decoder) : "null"));
    2107              :         }
    2108            3 :     }
    2109              : 
    2110            5 :     if (decoder)
    2111            3 :         m_gstWrapper->gstObjectUnref(GST_OBJECT(decoder));
    2112              : 
    2113            5 :     return returnValue;
    2114              : }
    2115              : 
    2116            1 : void GstGenericPlayer::setUseBuffering(bool useBuffering)
    2117              : {
    2118            1 :     if (m_workerThread)
    2119              :     {
    2120            1 :         m_workerThread->enqueueTask(m_taskFactory->createSetUseBuffering(m_context, *this, useBuffering));
    2121              :     }
    2122              : }
    2123              : 
    2124            3 : bool GstGenericPlayer::getUseBuffering(bool &useBuffering)
    2125              : {
    2126            3 :     if (m_context.playbackGroup.m_curAudioDecodeBin)
    2127              :     {
    2128            1 :         m_glibWrapper->gObjectGet(m_context.playbackGroup.m_curAudioDecodeBin, "use-buffering", &useBuffering, nullptr);
    2129            1 :         return true;
    2130              :     }
    2131              :     else
    2132              :     {
    2133            2 :         std::unique_lock lock{m_context.propertyMutex};
    2134            2 :         if (m_context.pendingUseBuffering.has_value())
    2135              :         {
    2136            1 :             RIALTO_SERVER_LOG_DEBUG("Returning queued value");
    2137            1 :             useBuffering = m_context.pendingUseBuffering.value();
    2138            1 :             return true;
    2139              :         }
    2140            2 :     }
    2141            1 :     return false;
    2142              : }
    2143              : 
    2144            1 : void GstGenericPlayer::switchSource(const std::unique_ptr<IMediaPipeline::MediaSource> &mediaSource)
    2145              : {
    2146            1 :     if (m_workerThread)
    2147              :     {
    2148            1 :         m_workerThread->enqueueTask(m_taskFactory->createSwitchSource(*this, mediaSource));
    2149              :     }
    2150              : }
    2151              : 
    2152            1 : void GstGenericPlayer::handleBusMessage(GstMessage *message)
    2153              : {
    2154            1 :     m_workerThread->enqueueTask(m_taskFactory->createHandleBusMessage(m_context, *this, message, *m_flushWatcher));
    2155              : }
    2156              : 
    2157            1 : void GstGenericPlayer::updatePlaybackGroup(GstElement *typefind, const GstCaps *caps)
    2158              : {
    2159            1 :     m_workerThread->enqueueTask(m_taskFactory->createUpdatePlaybackGroup(m_context, *this, typefind, caps));
    2160              : }
    2161              : 
    2162            3 : void GstGenericPlayer::addAutoVideoSinkChild(GObject *object)
    2163              : {
    2164              :     // Only add children that are sinks
    2165            3 :     if (GST_OBJECT_FLAG_IS_SET(GST_ELEMENT(object), GST_ELEMENT_FLAG_SINK))
    2166              :     {
    2167            2 :         RIALTO_SERVER_LOG_DEBUG("Store AutoVideoSink child sink");
    2168              : 
    2169            2 :         if (m_context.autoVideoChildSink && m_context.autoVideoChildSink != GST_ELEMENT(object))
    2170              :         {
    2171            1 :             RIALTO_SERVER_LOG_MIL("AutoVideoSink child is been overwritten");
    2172              :         }
    2173            2 :         m_context.autoVideoChildSink = GST_ELEMENT(object);
    2174              :     }
    2175            3 : }
    2176              : 
    2177            3 : void GstGenericPlayer::addAutoAudioSinkChild(GObject *object)
    2178              : {
    2179              :     // Only add children that are sinks
    2180            3 :     if (GST_OBJECT_FLAG_IS_SET(GST_ELEMENT(object), GST_ELEMENT_FLAG_SINK))
    2181              :     {
    2182            2 :         RIALTO_SERVER_LOG_DEBUG("Store AutoAudioSink child sink");
    2183              : 
    2184            2 :         if (m_context.autoAudioChildSink && m_context.autoAudioChildSink != GST_ELEMENT(object))
    2185              :         {
    2186            1 :             RIALTO_SERVER_LOG_MIL("AutoAudioSink child is been overwritten");
    2187              :         }
    2188            2 :         m_context.autoAudioChildSink = GST_ELEMENT(object);
    2189              :     }
    2190            3 : }
    2191              : 
    2192            3 : void GstGenericPlayer::removeAutoVideoSinkChild(GObject *object)
    2193              : {
    2194            3 :     if (GST_OBJECT_FLAG_IS_SET(GST_ELEMENT(object), GST_ELEMENT_FLAG_SINK))
    2195              :     {
    2196            3 :         RIALTO_SERVER_LOG_DEBUG("Remove AutoVideoSink child sink");
    2197              : 
    2198            3 :         if (m_context.autoVideoChildSink && m_context.autoVideoChildSink != GST_ELEMENT(object))
    2199              :         {
    2200            1 :             RIALTO_SERVER_LOG_MIL("AutoVideoSink child sink is not the same as the one stored");
    2201            1 :             return;
    2202              :         }
    2203              : 
    2204            2 :         m_context.autoVideoChildSink = nullptr;
    2205              :     }
    2206              : }
    2207              : 
    2208            3 : void GstGenericPlayer::removeAutoAudioSinkChild(GObject *object)
    2209              : {
    2210            3 :     if (GST_OBJECT_FLAG_IS_SET(GST_ELEMENT(object), GST_ELEMENT_FLAG_SINK))
    2211              :     {
    2212            3 :         RIALTO_SERVER_LOG_DEBUG("Remove AutoAudioSink child sink");
    2213              : 
    2214            3 :         if (m_context.autoAudioChildSink && m_context.autoAudioChildSink != GST_ELEMENT(object))
    2215              :         {
    2216            1 :             RIALTO_SERVER_LOG_MIL("AutoAudioSink child sink is not the same as the one stored");
    2217            1 :             return;
    2218              :         }
    2219              : 
    2220            2 :         m_context.autoAudioChildSink = nullptr;
    2221              :     }
    2222              : }
    2223              : 
    2224           14 : GstElement *GstGenericPlayer::getSinkChildIfAutoVideoSink(GstElement *sink) const
    2225              : {
    2226           14 :     const gchar *kTmpName = m_glibWrapper->gTypeName(G_OBJECT_TYPE(sink));
    2227           14 :     if (!kTmpName)
    2228            0 :         return sink;
    2229              : 
    2230           28 :     const std::string kElementTypeName{kTmpName};
    2231           14 :     if (kElementTypeName == "GstAutoVideoSink")
    2232              :     {
    2233            1 :         if (!m_context.autoVideoChildSink)
    2234              :         {
    2235            0 :             RIALTO_SERVER_LOG_WARN("No child sink has been added to the autovideosink");
    2236              :         }
    2237              :         else
    2238              :         {
    2239            1 :             return m_context.autoVideoChildSink;
    2240              :         }
    2241              :     }
    2242           13 :     return sink;
    2243           14 : }
    2244              : 
    2245           11 : GstElement *GstGenericPlayer::getSinkChildIfAutoAudioSink(GstElement *sink) const
    2246              : {
    2247           11 :     const gchar *kTmpName = m_glibWrapper->gTypeName(G_OBJECT_TYPE(sink));
    2248           11 :     if (!kTmpName)
    2249            0 :         return sink;
    2250              : 
    2251           22 :     const std::string kElementTypeName{kTmpName};
    2252           11 :     if (kElementTypeName == "GstAutoAudioSink")
    2253              :     {
    2254            1 :         if (!m_context.autoAudioChildSink)
    2255              :         {
    2256            0 :             RIALTO_SERVER_LOG_WARN("No child sink has been added to the autoaudiosink");
    2257              :         }
    2258              :         else
    2259              :         {
    2260            1 :             return m_context.autoAudioChildSink;
    2261              :         }
    2262              :     }
    2263           10 :     return sink;
    2264           11 : }
    2265              : 
    2266          208 : void GstGenericPlayer::setPlaybinFlags(bool enableAudio)
    2267              : {
    2268          208 :     unsigned flags = getGstPlayFlag("video") | getGstPlayFlag("native-video") | getGstPlayFlag("text");
    2269              : 
    2270          208 :     if (enableAudio)
    2271              :     {
    2272          208 :         flags |= getGstPlayFlag("audio");
    2273          208 :         flags |= shouldEnableNativeAudio() ? getGstPlayFlag("native-audio") : 0;
    2274              :     }
    2275              : 
    2276          208 :     m_glibWrapper->gObjectSet(m_context.pipeline, "flags", flags, nullptr);
    2277              : }
    2278              : 
    2279          208 : bool GstGenericPlayer::shouldEnableNativeAudio()
    2280              : {
    2281          208 :     GstElementFactory *factory = m_gstWrapper->gstElementFactoryFind("brcmaudiosink");
    2282          208 :     if (factory)
    2283              :     {
    2284            1 :         m_gstWrapper->gstObjectUnref(GST_OBJECT(factory));
    2285            1 :         return true;
    2286              :     }
    2287          207 :     return false;
    2288              : }
    2289              : 
    2290              : }; // namespace firebolt::rialto::server
        

Generated by: LCOV version 2.0-1