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: 95.1 % 162 154
Test Date: 2026-12-04 20:46:59 Functions: 100.0 % 5 5

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

Generated by: LCOV version 2.0-1