LCOV - code coverage report
Current view: top level - media/server/gstplayer/source/tasks/generic - HandleBusMessage.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 96.8 % 125 121
Test Date: 2025-10-07 14:22:52 Functions: 100.0 % 4 4

            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 "tasks/generic/HandleBusMessage.h"
      21              : #include "GenericPlayerContext.h"
      22              : #include "IGstGenericPlayerClient.h"
      23              : #include "IGstWrapper.h"
      24              : #include "RialtoServerLogging.h"
      25              : 
      26              : namespace firebolt::rialto::server::tasks::generic
      27              : {
      28           36 : HandleBusMessage::HandleBusMessage(GenericPlayerContext &context, IGstGenericPlayerPrivate &player,
      29              :                                    IGstGenericPlayerClient *client,
      30              :                                    const std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> &gstWrapper,
      31              :                                    const std::shared_ptr<firebolt::rialto::wrappers::IGlibWrapper> &glibWrapper,
      32           36 :                                    GstMessage *message, const IFlushWatcher &flushWatcher)
      33           36 :     : m_context{context}, m_player{player}, m_gstPlayerClient{client}, m_gstWrapper{gstWrapper},
      34           36 :       m_glibWrapper{glibWrapper}, m_message{message}, m_flushWatcher{flushWatcher},
      35           36 :       m_isFlushOngoingDuringCreation{flushWatcher.isFlushOngoing()},
      36           72 :       m_isAsyncFlushOngoingDuringCreation{flushWatcher.isAsyncFlushOngoing()}
      37              : {
      38           36 :     RIALTO_SERVER_LOG_DEBUG("Constructing HandleBusMessage");
      39              : }
      40              : 
      41           37 : HandleBusMessage::~HandleBusMessage()
      42              : {
      43           36 :     RIALTO_SERVER_LOG_DEBUG("HandleBusMessage finished");
      44           37 : }
      45              : 
      46           35 : void HandleBusMessage::execute() const
      47              : {
      48           35 :     RIALTO_SERVER_LOG_DEBUG("Executing HandleBusMessage");
      49           35 :     switch (GST_MESSAGE_TYPE(m_message))
      50              :     {
      51           14 :     case GST_MESSAGE_STATE_CHANGED:
      52              :     {
      53           14 :         if (m_context.pipeline && GST_MESSAGE_SRC(m_message) == GST_OBJECT(m_context.pipeline))
      54              :         {
      55              :             GstState oldState, newState, pending;
      56           12 :             m_gstWrapper->gstMessageParseStateChanged(m_message, &oldState, &newState, &pending);
      57           12 :             RIALTO_SERVER_LOG_MIL("State changed (old: %s, new: %s, pending: %s)",
      58              :                                   m_gstWrapper->gstElementStateGetName(oldState),
      59              :                                   m_gstWrapper->gstElementStateGetName(newState),
      60              :                                   m_gstWrapper->gstElementStateGetName(pending));
      61              : 
      62           12 :             if (!m_gstPlayerClient)
      63              :             {
      64            1 :                 break;
      65              :             }
      66           11 :             switch (newState)
      67              :             {
      68            1 :             case GST_STATE_NULL:
      69              :             {
      70            1 :                 m_gstPlayerClient->notifyPlaybackState(PlaybackState::STOPPED);
      71            1 :                 break;
      72              :             }
      73            5 :             case GST_STATE_PAUSED:
      74              :             {
      75            5 :                 if (pending != GST_STATE_PAUSED)
      76              :                 {
      77              :                     // If async flush was requested before HandleBusMessage task creation (but it was not executed yet)
      78              :                     // or if async flush was created after HandleBusMessage task creation (but before its execution)
      79              :                     // we can't report playback state, because async flush causes state loss - reported state is probably invalid.
      80            4 :                     if (m_isAsyncFlushOngoingDuringCreation || m_flushWatcher.isAsyncFlushOngoing())
      81              :                     {
      82            2 :                         RIALTO_SERVER_LOG_WARN("Skip PAUSED notification - flush is ongoing");
      83            2 :                         break;
      84              :                     }
      85              :                     // newState==GST_STATE_PAUSED, pending==GST_STATE_PAUSED state transition is received as a result of
      86              :                     // waiting for preroll after seek.
      87              :                     // Subsequent newState==GST_STATE_PAUSED, pending!=GST_STATE_PAUSED transition will
      88              :                     // indicate that the pipeline is prerolled and it reached GST_STATE_PAUSED state after seek.
      89            2 :                     m_gstPlayerClient->notifyPlaybackState(PlaybackState::PAUSED);
      90              :                 }
      91            3 :                 if (m_player.hasSourceType(MediaSourceType::SUBTITLE))
      92              :                 {
      93            0 :                     m_player.stopSubtitleClockResyncTimer();
      94              :                 }
      95            3 :                 break;
      96              :             }
      97            5 :             case GST_STATE_PLAYING:
      98              :             {
      99              :                 // If async flush was requested before HandleBusMessage task creation (but it was not executed yet)
     100              :                 // or if async flush was created after HandleBusMessage task creation (but before its execution)
     101              :                 // we can't report playback state, because async flush causes state loss - reported state is probably invalid.
     102            5 :                 if (m_isAsyncFlushOngoingDuringCreation || m_flushWatcher.isAsyncFlushOngoing())
     103              :                 {
     104            2 :                     RIALTO_SERVER_LOG_WARN("Skip PLAYING notification - flush is ongoing");
     105            2 :                     break;
     106              :                 }
     107              : 
     108            3 :                 m_context.isPlaying = true;
     109            3 :                 m_gstPlayerClient->notifyPlaybackState(PlaybackState::PLAYING);
     110              : 
     111            3 :                 if (m_context.pendingPlaybackRate != kNoPendingPlaybackRate)
     112              :                 {
     113            1 :                     m_player.setPendingPlaybackRate();
     114              :                 }
     115            3 :                 m_player.startPositionReportingAndCheckAudioUnderflowTimer();
     116            3 :                 if (m_player.hasSourceType(MediaSourceType::SUBTITLE))
     117              :                 {
     118            0 :                     m_player.startSubtitleClockResyncTimer();
     119              :                 }
     120            3 :                 break;
     121              :             }
     122            0 :             case GST_STATE_VOID_PENDING:
     123              :             case GST_STATE_READY:
     124              :             {
     125            0 :                 break;
     126              :             }
     127              :             }
     128           22 :             std::string filename = std::string(m_gstWrapper->gstElementStateGetName(oldState)) + "-" +
     129           44 :                                    std::string(m_gstWrapper->gstElementStateGetName(newState));
     130           11 :             m_gstWrapper->gstDebugBinToDotFileWithTs(GST_BIN(m_context.pipeline), GST_DEBUG_GRAPH_SHOW_ALL,
     131              :                                                      filename.c_str());
     132              :         }
     133           13 :         break;
     134              :     }
     135            6 :     case GST_MESSAGE_EOS:
     136              :     {
     137              :         // If flush was requested before HandleBusMessage task creation (but it was not executed yet)
     138              :         // or if flush was created after HandleBusMessage task creation (but before its execution)
     139              :         // we can't report EOS, because flush clears EOS.
     140            6 :         if (m_isFlushOngoingDuringCreation || m_flushWatcher.isFlushOngoing())
     141              :         {
     142            2 :             RIALTO_SERVER_LOG_WARN("Skip EOS notification - flush is ongoing");
     143            2 :             break;
     144              :         }
     145            4 :         if (m_context.pipeline && GST_MESSAGE_SRC(m_message) == GST_OBJECT(m_context.pipeline))
     146              :         {
     147            2 :             RIALTO_SERVER_LOG_MIL("End of stream reached.");
     148            2 :             if (!m_context.eosNotified && m_gstPlayerClient)
     149              :             {
     150            1 :                 m_gstPlayerClient->notifyPlaybackState(PlaybackState::END_OF_STREAM);
     151            1 :                 m_context.eosNotified = true;
     152              :             }
     153              :         }
     154            4 :         break;
     155              :     }
     156            4 :     case GST_MESSAGE_QOS:
     157              :     {
     158              :         GstFormat format;
     159            4 :         gboolean isLive = FALSE;
     160            4 :         guint64 runningTime = 0;
     161            4 :         guint64 streamTime = 0;
     162            4 :         guint64 timestamp = 0;
     163            4 :         guint64 duration = 0;
     164            4 :         guint64 dropped = 0;
     165            4 :         guint64 processed = 0;
     166              : 
     167            4 :         m_gstWrapper->gstMessageParseQos(m_message, &isLive, &runningTime, &streamTime, &timestamp, &duration);
     168            4 :         m_gstWrapper->gstMessageParseQosStats(m_message, &format, &processed, &dropped);
     169              : 
     170            4 :         if (GST_FORMAT_BUFFERS == format || GST_FORMAT_DEFAULT == format)
     171              :         {
     172            3 :             RIALTO_SERVER_LOG_INFO("QOS message: runningTime  %" G_GUINT64_FORMAT ", streamTime %" G_GUINT64_FORMAT
     173              :                                    ", timestamp %" G_GUINT64_FORMAT ", duration %" G_GUINT64_FORMAT
     174              :                                    ", format %u, processed %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
     175              :                                    runningTime, streamTime, timestamp, duration, format, processed, dropped);
     176              : 
     177            3 :             if (m_gstPlayerClient)
     178              :             {
     179            3 :                 firebolt::rialto::QosInfo qosInfo = {processed, dropped};
     180              :                 const gchar *klass;
     181            3 :                 klass = m_gstWrapper->gstElementClassGetMetadata(GST_ELEMENT_GET_CLASS(GST_MESSAGE_SRC(m_message)),
     182              :                                                                  GST_ELEMENT_METADATA_KLASS);
     183              : 
     184            3 :                 if (g_strrstr(klass, "Video"))
     185              :                 {
     186            1 :                     m_gstPlayerClient->notifyQos(firebolt::rialto::MediaSourceType::VIDEO, qosInfo);
     187              :                 }
     188            2 :                 else if (g_strrstr(klass, "Audio"))
     189              :                 {
     190            1 :                     m_gstPlayerClient->notifyQos(firebolt::rialto::MediaSourceType::AUDIO, qosInfo);
     191              :                 }
     192              :                 else
     193              :                 {
     194            1 :                     RIALTO_SERVER_LOG_WARN("Unknown source type for class '%s', ignoring QOS Message", klass);
     195              :                 }
     196              :             }
     197            3 :         }
     198              :         else
     199              :         {
     200            1 :             RIALTO_SERVER_LOG_WARN("Received a QOS_MESSAGE with unhandled format %s",
     201              :                                    m_gstWrapper->gstFormatGetName(format));
     202              :         }
     203            4 :         break;
     204              :     }
     205            6 :     case GST_MESSAGE_ERROR:
     206              :     {
     207            6 :         GError *err = nullptr;
     208            6 :         gchar *debug = nullptr;
     209            6 :         m_gstWrapper->gstMessageParseError(m_message, &err, &debug);
     210              : 
     211            6 :         if ((err->domain == GST_STREAM_ERROR) && (allSourcesEos()))
     212              :         {
     213            2 :             RIALTO_SERVER_LOG_WARN("Got stream error from %s. But all streams are ended, so reporting EOS. Error code "
     214              :                                    "%d: %s "
     215              :                                    "(%s).",
     216              :                                    GST_OBJECT_NAME(GST_MESSAGE_SRC(m_message)), err->code, err->message, debug);
     217            2 :             if (!m_context.eosNotified && m_gstPlayerClient)
     218              :             {
     219            1 :                 m_gstPlayerClient->notifyPlaybackState(PlaybackState::END_OF_STREAM);
     220            1 :                 m_context.eosNotified = true;
     221              :             }
     222              :         }
     223              :         else
     224              :         {
     225            4 :             RIALTO_SERVER_LOG_ERROR("Error from %s - %d: %s (%s)", GST_OBJECT_NAME(GST_MESSAGE_SRC(m_message)),
     226              :                                     err->code, err->message, debug);
     227            4 :             m_gstPlayerClient->notifyPlaybackState(PlaybackState::FAILURE);
     228              :         }
     229              : 
     230            6 :         m_glibWrapper->gFree(debug);
     231            6 :         m_glibWrapper->gErrorFree(err);
     232            6 :         break;
     233              :     }
     234            4 :     case GST_MESSAGE_WARNING:
     235              :     {
     236            4 :         PlaybackError rialtoError = PlaybackError::UNKNOWN;
     237            4 :         GError *err = nullptr;
     238            4 :         gchar *debug = nullptr;
     239            4 :         m_gstWrapper->gstMessageParseWarning(m_message, &err, &debug);
     240              : 
     241            4 :         if ((err->domain == GST_STREAM_ERROR) && (err->code == GST_STREAM_ERROR_DECRYPT))
     242              :         {
     243            3 :             RIALTO_SERVER_LOG_WARN("Decrypt error %s - %d: %s (%s)", GST_OBJECT_NAME(GST_MESSAGE_SRC(m_message)),
     244              :                                    err->code, err->message, debug);
     245            3 :             rialtoError = PlaybackError::DECRYPTION;
     246              :         }
     247              :         else
     248              :         {
     249            1 :             RIALTO_SERVER_LOG_WARN("Unknown warning, ignoring %s - %d: %s (%s)",
     250              :                                    GST_OBJECT_NAME(GST_MESSAGE_SRC(m_message)), err->code, err->message, debug);
     251              :         }
     252              : 
     253            4 :         if ((PlaybackError::UNKNOWN != rialtoError) && (m_gstPlayerClient))
     254              :         {
     255            3 :             const gchar *kName = GST_ELEMENT_NAME(GST_ELEMENT(GST_MESSAGE_SRC(m_message)));
     256            3 :             if (g_strrstr(kName, "video"))
     257              :             {
     258            1 :                 m_gstPlayerClient->notifyPlaybackError(firebolt::rialto::MediaSourceType::VIDEO,
     259              :                                                        PlaybackError::DECRYPTION);
     260              :             }
     261            2 :             else if (g_strrstr(kName, "audio"))
     262              :             {
     263            1 :                 m_gstPlayerClient->notifyPlaybackError(firebolt::rialto::MediaSourceType::AUDIO,
     264              :                                                        PlaybackError::DECRYPTION);
     265              :             }
     266              :             else
     267              :             {
     268            1 :                 RIALTO_SERVER_LOG_WARN("Unknown source type for element '%s', not propagating error", kName);
     269              :             }
     270              :         }
     271              : 
     272            4 :         m_glibWrapper->gFree(debug);
     273            4 :         m_glibWrapper->gErrorFree(err);
     274            4 :         break;
     275              :     }
     276            1 :     default:
     277            1 :         break;
     278              :     }
     279              : 
     280           35 :     m_gstWrapper->gstMessageUnref(m_message);
     281              : }
     282              : 
     283            4 : bool HandleBusMessage::allSourcesEos() const
     284              : {
     285            8 :     for (const auto &streamInfo : m_context.streamInfo)
     286              :     {
     287            6 :         const auto eosInfoIt = m_context.endOfStreamInfo.find(streamInfo.first);
     288            6 :         if (eosInfoIt == m_context.endOfStreamInfo.end() || eosInfoIt->second != EosState::SET)
     289              :         {
     290            2 :             return false;
     291              :         }
     292              :     }
     293            2 :     return true;
     294              : }
     295              : } // namespace firebolt::rialto::server::tasks::generic
        

Generated by: LCOV version 2.0-1