LCOV - code coverage report
Current view: top level - media/server/gstplayer/source - GstDispatcherThread.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 93.0 % 86 80
Test Date: 2026-11-10 16:58:19 Functions: 100.0 % 7 7

            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 "GstDispatcherThread.h"
      21              : #include "RialtoServerLogging.h"
      22              : 
      23              : namespace
      24              : {
      25              : // Messages that were previously requested via gst_bus_timed_pop_filtered's type mask.
      26              : constexpr GstMessageType kHandledMessageTypes =
      27              :     static_cast<GstMessageType>(GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_QOS | GST_MESSAGE_EOS | GST_MESSAGE_ERROR |
      28              :                                 GST_MESSAGE_WARNING | GST_MESSAGE_APPLICATION);
      29              : } // namespace
      30              : 
      31              : namespace firebolt::rialto::server
      32              : {
      33            2 : std::unique_ptr<IGstDispatcherThread> GstDispatcherThreadFactory::createGstDispatcherThread(
      34              :     IGstDispatcherThreadClient &client, GstElement *pipeline,
      35              :     const std::shared_ptr<IFlushOnPrerollController> &flushOnPrerollController,
      36              :     const std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> &gstWrapper,
      37              :     const std::shared_ptr<firebolt::rialto::wrappers::IGlibWrapper> &glibWrapper) const
      38              : {
      39            2 :     return std::make_unique<GstDispatcherThread>(client, pipeline, flushOnPrerollController, gstWrapper, glibWrapper);
      40              : }
      41              : 
      42           13 : GstDispatcherThread::GstDispatcherThread(IGstDispatcherThreadClient &client, GstElement *pipeline,
      43              :                                          const std::shared_ptr<IFlushOnPrerollController> &flushOnPrerollController,
      44              :                                          const std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> &gstWrapper,
      45           13 :                                          const std::shared_ptr<firebolt::rialto::wrappers::IGlibWrapper> &glibWrapper)
      46           13 :     : m_client{client}, m_flushOnPrerollController{flushOnPrerollController}, m_gstWrapper{gstWrapper},
      47           26 :       m_glibWrapper{glibWrapper}, m_isGstreamerDispatcherActive{true}, m_pipeline{pipeline}
      48              : {
      49           13 :     RIALTO_SERVER_LOG_INFO("GstDispatcherThread is starting");
      50              : 
      51           13 :     GstBus *bus = m_gstWrapper->gstPipelineGetBus(GST_PIPELINE(pipeline));
      52           13 :     if (!bus)
      53              :     {
      54            3 :         RIALTO_SERVER_LOG_ERROR("Failed to get gst bus");
      55            3 :         return;
      56              :     }
      57              : 
      58              :     // Own GMainContext so this dispatcher doesn't share a thread/wheel with other GLib users in the process.
      59           10 :     m_mainContext = m_glibWrapper->gMainContextNew();
      60           10 :     m_mainLoop = m_glibWrapper->gMainLoopNew(m_mainContext, FALSE);
      61              : 
      62           10 :     GSource *busSource = m_gstWrapper->gstBusCreateWatch(bus);
      63           10 :     m_glibWrapper->gSourceSetCallback(busSource, reinterpret_cast<GSourceFunc>(&GstDispatcherThread::onBusMessage),
      64              :                                       this, nullptr);
      65           10 :     m_glibWrapper->gSourceAttach(busSource, m_mainContext);
      66           10 :     m_glibWrapper->gSourceUnref(busSource);
      67           10 :     m_gstWrapper->gstObjectUnref(bus);
      68              : 
      69           10 :     m_gstBusDispatcherThread = std::thread(&GstDispatcherThread::gstBusEventHandler, this);
      70              : }
      71              : 
      72           26 : GstDispatcherThread::~GstDispatcherThread()
      73              : {
      74           13 :     RIALTO_SERVER_LOG_INFO("Stopping GstDispatcherThread");
      75           13 :     m_isGstreamerDispatcherActive = false;
      76           13 :     if (m_mainLoop)
      77              :     {
      78              :         // Schedule the quit as an idle source on the private context rather than calling gMainLoopQuit
      79              :         // directly here: gMainLoopRun() unconditionally marks the loop as running on entry, so a direct
      80              :         // quit issued before the worker thread reaches gMainLoopRun() would be silently lost and join()
      81              :         // below would block forever. An idle source only dispatches once the loop is actually running.
      82           10 :         GSource *quitSource = m_glibWrapper->gIdleSourceNew();
      83           10 :         m_glibWrapper->gSourceSetCallback(quitSource, &GstDispatcherThread::onQuitRequested, this, nullptr);
      84           10 :         m_glibWrapper->gSourceAttach(quitSource, m_mainContext);
      85           10 :         m_glibWrapper->gSourceUnref(quitSource);
      86              :     }
      87           13 :     if (m_gstBusDispatcherThread.joinable())
      88              :     {
      89           10 :         m_gstBusDispatcherThread.join();
      90              :     }
      91           13 :     if (m_mainLoop)
      92              :     {
      93           10 :         m_glibWrapper->gMainLoopUnref(m_mainLoop);
      94           10 :         m_glibWrapper->gMainContextUnref(m_mainContext);
      95              :     }
      96           26 : }
      97              : 
      98            8 : gboolean GstDispatcherThread::onBusMessage(GstBus * /*bus*/, GstMessage *message, gpointer userData)
      99              : {
     100            8 :     static_cast<GstDispatcherThread *>(userData)->handleMessage(message);
     101            8 :     return G_SOURCE_CONTINUE;
     102              : }
     103              : 
     104           10 : gboolean GstDispatcherThread::onQuitRequested(gpointer userData)
     105              : {
     106           10 :     auto *self = static_cast<GstDispatcherThread *>(userData);
     107           10 :     self->m_glibWrapper->gMainLoopQuit(self->m_mainLoop);
     108           10 :     return G_SOURCE_REMOVE;
     109              : }
     110              : 
     111           10 : void GstDispatcherThread::gstBusEventHandler()
     112              : {
     113           10 :     m_glibWrapper->gMainContextPushThreadDefault(m_mainContext);
     114           10 :     m_glibWrapper->gMainLoopRun(m_mainLoop);
     115           10 :     m_glibWrapper->gMainContextPopThreadDefault(m_mainContext);
     116              : 
     117           10 :     RIALTO_SERVER_LOG_INFO("Gstbus dispatcher exitting");
     118              : }
     119              : 
     120            8 : void GstDispatcherThread::handleMessage(GstMessage *message)
     121              : {
     122              :     // Checked ahead of the type filter below: the bus watch has higher priority than the destructor's
     123              :     // idle quit source, so a bus kept ready by excluded message types (e.g. BUFFERING) would otherwise
     124              :     // starve the idle source forever, leaving the destructor blocked in join().
     125            8 :     if (!m_isGstreamerDispatcherActive)
     126              :     {
     127            0 :         m_glibWrapper->gMainLoopQuit(m_mainLoop);
     128            0 :         return;
     129              :     }
     130              :     // The bus watch delivers every message; only these types were previously popped from the bus at all.
     131            8 :     if (!(GST_MESSAGE_TYPE(message) & kHandledMessageTypes))
     132              :     {
     133            1 :         return;
     134              :     }
     135              : 
     136            7 :     bool shouldHandleMessage{true};
     137            7 :     if (GST_MESSAGE_SRC(message) == GST_OBJECT(m_pipeline))
     138              :     {
     139            5 :         switch (GST_MESSAGE_TYPE(message))
     140              :         {
     141            4 :         case GST_MESSAGE_STATE_CHANGED:
     142              :         {
     143              :             GstState oldState, newState, pending;
     144            4 :             m_gstWrapper->gstMessageParseStateChanged(message, &oldState, &newState, &pending);
     145            4 :             switch (newState)
     146              :             {
     147            1 :             case GST_STATE_NULL:
     148              :             {
     149            1 :                 m_isGstreamerDispatcherActive = false;
     150            1 :                 if (m_flushOnPrerollController)
     151              :                 {
     152            1 :                     m_flushOnPrerollController->reset();
     153              :                 }
     154            1 :                 break;
     155              :             }
     156            2 :             case GST_STATE_PAUSED:
     157              :             {
     158            2 :                 if (m_flushOnPrerollController && pending != GST_STATE_PAUSED)
     159              :                 {
     160            1 :                     m_flushOnPrerollController->stateReached(newState);
     161              :                 }
     162            1 :                 else if (m_flushOnPrerollController && pending == GST_STATE_PAUSED)
     163              :                 {
     164            1 :                     m_flushOnPrerollController->setPrerolling();
     165              :                 }
     166            2 :                 break;
     167              :             }
     168            1 :             case GST_STATE_PLAYING:
     169              :             {
     170            1 :                 if (m_flushOnPrerollController)
     171              :                 {
     172            1 :                     m_flushOnPrerollController->stateReached(newState);
     173              :                 }
     174            1 :                 break;
     175              :             }
     176            0 :             case GST_STATE_READY:
     177              :             case GST_STATE_VOID_PENDING:
     178              :             {
     179            0 :                 break;
     180              :             }
     181              :             }
     182            4 :             break;
     183              :         }
     184            1 :         case GST_MESSAGE_ERROR:
     185              :         {
     186            1 :             m_isGstreamerDispatcherActive = false;
     187            1 :             break;
     188              :         }
     189            0 :         default:
     190              :         {
     191            0 :             break;
     192              :         }
     193              :         }
     194              :     }
     195            2 :     else if (GST_MESSAGE_STATE_CHANGED == GST_MESSAGE_TYPE(message))
     196              :     {
     197              :         // Skip handling GST_MESSAGE_STATE_CHANGED for non-pipeline objects.
     198              :         // It signifficantly slows down rialto gst worker thread
     199            1 :         shouldHandleMessage = false;
     200              :     }
     201              : 
     202            7 :     if (shouldHandleMessage)
     203              :     {
     204              :         // The bus watch source unrefs message once this callback returns; the async task needs its own ref.
     205            6 :         m_client.handleBusMessage(m_gstWrapper->gstMessageRef(message));
     206              :     }
     207              : 
     208            7 :     if (!m_isGstreamerDispatcherActive)
     209              :     {
     210            2 :         m_glibWrapper->gMainLoopQuit(m_mainLoop);
     211              :     }
     212              : }
     213              : } // namespace firebolt::rialto::server
        

Generated by: LCOV version 2.0-1