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