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/SetupElement.h"
21 : #include "GenericPlayerContext.h"
22 : #include "IGlibWrapper.h"
23 : #include "IGstGenericPlayerPrivate.h"
24 : #include "IGstWrapper.h"
25 : #include "RialtoServerLogging.h"
26 : #include "Utils.h"
27 :
28 : namespace
29 : {
30 : /**
31 : * @brief Callback for audio underflow event from sink. Called by the Gstreamer thread.
32 : *
33 : * @param[in] object : the object that emitted the signal
34 : * @param[in] fifoDepth : the fifo depth (may be 0)
35 : * @param[in] queueDepth : the queue depth (may be NULL)
36 : * @param[in] self : The pointer to IGstGenericPlayerPrivate
37 : *
38 : * @retval true if the handling of the message is successful, false otherwise.
39 : */
40 1 : void audioUnderflowCallback(GstElement *object, guint fifoDepth, gpointer queueDepth, gpointer self)
41 : {
42 1 : firebolt::rialto::server::IGstGenericPlayerPrivate *player =
43 : static_cast<firebolt::rialto::server::IGstGenericPlayerPrivate *>(self);
44 1 : player->scheduleAudioUnderflow();
45 : }
46 :
47 : /**
48 : * @brief Callback for video underflow event from sink. Called by the Gstreamer thread.
49 : *
50 : * @param[in] object : the object that emitted the signal
51 : * @param[in] fifoDepth : the fifo depth (may be 0)
52 : * @param[in] queueDepth : the queue depth (may be NULL)
53 : * @param[in] self : The pointer to IGstGenericPlayerPrivate
54 : *
55 : * @retval true if the handling of the message is successful, false otherwise.
56 : */
57 1 : void videoUnderflowCallback(GstElement *object, guint fifoDepth, gpointer queueDepth, gpointer self)
58 : {
59 1 : firebolt::rialto::server::IGstGenericPlayerPrivate *player =
60 : static_cast<firebolt::rialto::server::IGstGenericPlayerPrivate *>(self);
61 1 : player->scheduleVideoUnderflow();
62 : }
63 :
64 : /**
65 : * @brief Callback for first video frame event from the emitting video element. Called by the Gstreamer thread.
66 : *
67 : * @param[in] object : the object that emitted the signal
68 : * @param[in] fifoDepth : the fifo depth (may be 0)
69 : * @param[in] queueDepth : the queue depth (may be NULL)
70 : * @param[in] self : The pointer to IGstGenericPlayerPrivate
71 : */
72 1 : void firstVideoFrameCallback(GstElement *object, guint fifoDepth, gpointer queueDepth, gpointer self)
73 : {
74 1 : firebolt::rialto::server::IGstGenericPlayerPrivate *player =
75 : static_cast<firebolt::rialto::server::IGstGenericPlayerPrivate *>(self);
76 1 : player->scheduleFirstVideoFrameReceived();
77 : }
78 :
79 : /**
80 : * @brief Callback for first audio frame event from the emitting audio element. Called by the Gstreamer thread.
81 : *
82 : * @param[in] object : the object that emitted the signal
83 : * @param[in] fifoDepth : the fifo depth (may be 0)
84 : * @param[in] queueDepth : the queue depth (may be NULL)
85 : * @param[in] self : The pointer to IGstGenericPlayerPrivate
86 : */
87 1 : void firstAudioFrameCallback(GstElement *object, guint fifoDepth, gpointer queueDepth, gpointer self)
88 : {
89 1 : firebolt::rialto::server::IGstGenericPlayerPrivate *player =
90 : static_cast<firebolt::rialto::server::IGstGenericPlayerPrivate *>(self);
91 1 : player->scheduleFirstAudioFrameReceived(firebolt::rialto::server::AudioFirstFrameAction::CLEAR_PROBE);
92 : }
93 :
94 : /**
95 : * @brief Fallback probe callback for first audio frame on sink pad.
96 : */
97 1 : GstPadProbeReturn firstAudioFrameProbeCallback(GstPad *pad, GstPadProbeInfo *info, gpointer self)
98 : {
99 1 : if (!(info->type & GST_PAD_PROBE_TYPE_BUFFER) || !GST_PAD_PROBE_INFO_BUFFER(info))
100 : {
101 0 : return GST_PAD_PROBE_OK;
102 : }
103 :
104 1 : firebolt::rialto::server::IGstGenericPlayerPrivate *player =
105 : static_cast<firebolt::rialto::server::IGstGenericPlayerPrivate *>(self);
106 1 : player->scheduleFirstAudioFrameReceived(firebolt::rialto::server::AudioFirstFrameAction::CLEAR_PROBE_STATE);
107 1 : return GST_PAD_PROBE_REMOVE;
108 : }
109 :
110 : /**
111 : * @brief Callback for a autovideosink when a child has been added to the sink.
112 : *
113 : * @param[in] obj : the parent element (autovideosink)
114 : * @param[in] object : the child element
115 : * @param[in] name : the name of the child element
116 : * @param[in] self : The pointer to IGstGenericPlayerPrivate
117 : */
118 1 : void autoVideoSinkChildAddedCallback(GstChildProxy *obj, GObject *object, gchar *name, gpointer self)
119 : {
120 1 : RIALTO_SERVER_LOG_DEBUG("AutoVideoSink added element %s", name);
121 1 : firebolt::rialto::server::IGstGenericPlayerPrivate *player =
122 : static_cast<firebolt::rialto::server::IGstGenericPlayerPrivate *>(self);
123 1 : player->addAutoVideoSinkChild(object);
124 : }
125 :
126 : /**
127 : * @brief Callback for a autoaudiosink when a child has been added to the sink.
128 : *
129 : * @param[in] obj : the parent element (autoaudiosink)
130 : * @param[in] object : the child element
131 : * @param[in] name : the name of the child element
132 : * @param[in] self : The pointer to IGstGenericPlayerPrivate
133 : */
134 1 : void autoAudioSinkChildAddedCallback(GstChildProxy *obj, GObject *object, gchar *name, gpointer self)
135 : {
136 1 : RIALTO_SERVER_LOG_DEBUG("AutoAudioSink added element %s", name);
137 1 : firebolt::rialto::server::IGstGenericPlayerPrivate *player =
138 : static_cast<firebolt::rialto::server::IGstGenericPlayerPrivate *>(self);
139 1 : player->addAutoAudioSinkChild(object);
140 : }
141 :
142 : /**
143 : * @brief Callback for a autovideosink when a child has been removed from the sink.
144 : *
145 : * @param[in] obj : the parent element (autovideosink)
146 : * @param[in] object : the child element
147 : * @param[in] name : the name of the child element
148 : * @param[in] self : The pointer to IGstGenericPlayerPrivate
149 : */
150 1 : void autoVideoSinkChildRemovedCallback(GstChildProxy *obj, GObject *object, gchar *name, gpointer self)
151 : {
152 1 : RIALTO_SERVER_LOG_DEBUG("AutoVideoSink removed element %s", name);
153 1 : firebolt::rialto::server::IGstGenericPlayerPrivate *player =
154 : static_cast<firebolt::rialto::server::IGstGenericPlayerPrivate *>(self);
155 1 : player->removeAutoVideoSinkChild(object);
156 : }
157 :
158 : /**
159 : * @brief Callback for a autoaudiosink when a child has been removed from the sink.
160 : *
161 : * @param[in] obj : the parent element (autoaudiosink)
162 : * @param[in] object : the child element
163 : * @param[in] name : the name of the child element
164 : * @param[in] self : The pointer to IGstGenericPlayerPrivate
165 : */
166 1 : void autoAudioSinkChildRemovedCallback(GstChildProxy *obj, GObject *object, gchar *name, gpointer self)
167 : {
168 1 : RIALTO_SERVER_LOG_DEBUG("AutoAudioSink removed element %s", name);
169 1 : firebolt::rialto::server::IGstGenericPlayerPrivate *player =
170 : static_cast<firebolt::rialto::server::IGstGenericPlayerPrivate *>(self);
171 1 : player->removeAutoAudioSinkChild(object);
172 : }
173 : } // namespace
174 :
175 : namespace firebolt::rialto::server::tasks::generic
176 : {
177 37 : SetupElement::SetupElement(GenericPlayerContext &context,
178 : const std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> &gstWrapper,
179 : const std::shared_ptr<firebolt::rialto::wrappers::IGlibWrapper> &glibWrapper,
180 37 : IGstGenericPlayerPrivate &player, GstElement *element)
181 37 : : m_context{context}, m_gstWrapper{gstWrapper}, m_glibWrapper{glibWrapper}, m_player{player}, m_element{element}
182 : {
183 37 : RIALTO_SERVER_LOG_DEBUG("Constructing SetupElement");
184 : }
185 :
186 38 : SetupElement::~SetupElement()
187 : {
188 37 : RIALTO_SERVER_LOG_DEBUG("SetupElement finished");
189 38 : }
190 :
191 36 : void SetupElement::execute() const
192 : {
193 36 : RIALTO_SERVER_LOG_DEBUG("Executing SetupElement");
194 :
195 36 : const std::string kElementTypeName = m_glibWrapper->gTypeName(G_OBJECT_TYPE(m_element));
196 36 : if (kElementTypeName == "GstAutoVideoSink")
197 : {
198 : // Check and store child sink so we can set underlying properties
199 5 : m_glibWrapper->gSignalConnect(m_element, "child-added", G_CALLBACK(autoVideoSinkChildAddedCallback), &m_player);
200 5 : m_glibWrapper->gSignalConnect(m_element, "child-removed", G_CALLBACK(autoVideoSinkChildRemovedCallback),
201 5 : &m_player);
202 :
203 : // AutoVideoSink sets child before it is setup on the pipeline, so check for children here
204 5 : GstIterator *sinks = m_gstWrapper->gstBinIterateSinks(GST_BIN(m_element));
205 5 : if (sinks && sinks->size > 1)
206 : {
207 1 : RIALTO_SERVER_LOG_WARN("More than one child sink attached");
208 : }
209 :
210 5 : GValue elem = G_VALUE_INIT;
211 5 : if (m_gstWrapper->gstIteratorNext(sinks, &elem) == GST_ITERATOR_OK)
212 : {
213 2 : m_player.addAutoVideoSinkChild(G_OBJECT(m_glibWrapper->gValueGetObject(&elem)));
214 : }
215 5 : m_glibWrapper->gValueUnset(&elem);
216 :
217 5 : if (sinks)
218 5 : m_gstWrapper->gstIteratorFree(sinks);
219 : }
220 31 : else if (kElementTypeName == "GstAutoAudioSink")
221 : {
222 : // Check and store child sink so we can set underlying properties
223 5 : m_glibWrapper->gSignalConnect(m_element, "child-added", G_CALLBACK(autoAudioSinkChildAddedCallback), &m_player);
224 5 : m_glibWrapper->gSignalConnect(m_element, "child-removed", G_CALLBACK(autoAudioSinkChildRemovedCallback),
225 5 : &m_player);
226 :
227 : // AutoAudioSink sets child before it is setup on the pipeline, so check for children here
228 5 : GstIterator *sinks = m_gstWrapper->gstBinIterateSinks(GST_BIN(m_element));
229 5 : if (sinks && sinks->size > 1)
230 : {
231 1 : RIALTO_SERVER_LOG_WARN("More than one child sink attached");
232 : }
233 :
234 5 : GValue elem = G_VALUE_INIT;
235 5 : if (m_gstWrapper->gstIteratorNext(sinks, &elem) == GST_ITERATOR_OK)
236 : {
237 2 : m_player.addAutoAudioSinkChild(G_OBJECT(m_glibWrapper->gValueGetObject(&elem)));
238 : }
239 5 : m_glibWrapper->gValueUnset(&elem);
240 :
241 5 : if (sinks)
242 5 : m_gstWrapper->gstIteratorFree(sinks);
243 : }
244 :
245 36 : if (m_glibWrapper->gStrHasPrefix(GST_ELEMENT_NAME(m_element), "amlhalasink"))
246 : {
247 2 : if (m_context.streamInfo.find(MediaSourceType::VIDEO) != m_context.streamInfo.end())
248 : {
249 : // Wait for video so that the audio aligns at the starting point with timeout of 4000ms.
250 1 : m_glibWrapper->gObjectSet(m_element, "wait-video", TRUE, "a-wait-timeout", 4000, nullptr);
251 : }
252 :
253 : // Xrun occasionally pauses the underlying sink due to unstable playback, but the rest of the pipeline
254 : // remains in the playing state. This causes problems with the synchronization of gst element and rialto
255 : // ultimately hangs waiting for pipeline termination.
256 2 : m_glibWrapper->gObjectSet(m_element, "disable-xrun", TRUE, nullptr);
257 : }
258 34 : else if (m_glibWrapper->gStrHasPrefix(GST_ELEMENT_NAME(m_element), "brcmaudiosink"))
259 : {
260 1 : m_glibWrapper->gObjectSet(m_element, "async", TRUE, nullptr);
261 : }
262 33 : else if (m_glibWrapper->gStrHasPrefix(GST_ELEMENT_NAME(m_element), "rialtotexttracksink"))
263 : {
264 : // in cannot be set during construction, because playsink overwrites "sync" value of text-sink during setup
265 1 : m_glibWrapper->gObjectSet(m_element, "sync", FALSE, nullptr);
266 : }
267 :
268 36 : if (m_context.subtitleSink)
269 : {
270 3 : if (!m_context.isVideoHandleSet && isVideoDecoder(*m_gstWrapper, m_element))
271 : {
272 3 : if (m_glibWrapper->gStrHasPrefix(GST_ELEMENT_NAME(m_element), "westerossink"))
273 : {
274 2 : gpointer decoder{nullptr};
275 2 : m_glibWrapper->gObjectGet(m_element, "videodecoder", &decoder, nullptr);
276 2 : if (decoder)
277 : {
278 1 : m_glibWrapper->gObjectSet(m_context.subtitleSink, "video-decoder", decoder, nullptr);
279 1 : RIALTO_SERVER_LOG_INFO("Setting video decoder handle for subtitle sink: %p", decoder);
280 1 : m_context.isVideoHandleSet = true;
281 : }
282 : else
283 : {
284 1 : m_glibWrapper->gObjectSet(m_context.subtitleSink, "video-decoder", m_element, nullptr);
285 1 : RIALTO_SERVER_LOG_INFO("Setting video decoder handle for subtitle sink: %p", m_element);
286 1 : m_context.isVideoHandleSet = true;
287 : }
288 : }
289 1 : else if (m_glibWrapper->gStrHasPrefix(GST_ELEMENT_NAME(m_element), "omx"))
290 : {
291 1 : m_glibWrapper->gObjectSet(m_context.subtitleSink, "video-decoder", m_element, nullptr);
292 1 : RIALTO_SERVER_LOG_INFO("Setting video decoder handle for subtitle sink: %p", m_element);
293 1 : m_context.isVideoHandleSet = true;
294 : }
295 3 : if (m_context.pendingReportDecodeErrorsForVideo.has_value())
296 : {
297 0 : m_player.setReportDecodeErrors();
298 : }
299 : }
300 : }
301 :
302 36 : if (isDecoder(*m_gstWrapper, m_element) || isSink(*m_gstWrapper, m_element))
303 : {
304 30 : std::optional<std::string> underflowSignalName = getUnderflowSignalName(*m_glibWrapper, m_element);
305 30 : if (underflowSignalName)
306 : {
307 30 : if (isAudio(*m_gstWrapper, m_element))
308 : {
309 18 : RIALTO_SERVER_LOG_INFO("Connecting audio underflow callback for signal: %s",
310 : underflowSignalName.value().c_str());
311 36 : m_glibWrapper->gSignalConnect(m_element, underflowSignalName.value().c_str(),
312 18 : G_CALLBACK(audioUnderflowCallback), &m_player);
313 : }
314 12 : else if (isVideo(*m_gstWrapper, m_element))
315 : {
316 12 : RIALTO_SERVER_LOG_INFO("Connecting video underflow callback for signal: %s",
317 : underflowSignalName.value().c_str());
318 24 : m_glibWrapper->gSignalConnect(m_element, underflowSignalName.value().c_str(),
319 12 : G_CALLBACK(videoUnderflowCallback), &m_player);
320 : }
321 : }
322 :
323 30 : std::optional<std::string> firstFrameSignalName = getFirstFrameSignalName(*m_glibWrapper, m_element);
324 30 : if (firstFrameSignalName)
325 : {
326 2 : if (isVideo(*m_gstWrapper, m_element))
327 : {
328 1 : RIALTO_SERVER_LOG_INFO("Connecting first video frame callback for signal: %s",
329 : firstFrameSignalName.value().c_str());
330 2 : m_glibWrapper->gSignalConnect(m_element, firstFrameSignalName.value().c_str(),
331 1 : G_CALLBACK(firstVideoFrameCallback), &m_player);
332 : }
333 1 : else if (isAudio(*m_gstWrapper, m_element))
334 : {
335 1 : RIALTO_SERVER_LOG_INFO("Connecting first audio frame callback for signal: %s",
336 : firstFrameSignalName.value().c_str());
337 2 : m_glibWrapper->gSignalConnect(m_element, firstFrameSignalName.value().c_str(),
338 1 : G_CALLBACK(firstAudioFrameCallback), &m_player);
339 : }
340 : }
341 28 : else if (isAudioSink(*m_gstWrapper, m_element))
342 : {
343 11 : GstPad *sinkPad = m_gstWrapper->gstElementGetStaticPad(m_element, "sink");
344 11 : if (sinkPad)
345 : {
346 1 : gulong probeId = m_gstWrapper->gstPadAddProbe(sinkPad, GST_PAD_PROBE_TYPE_BUFFER,
347 1 : firstAudioFrameProbeCallback, &m_player, nullptr);
348 :
349 1 : if (probeId != 0)
350 : {
351 1 : RIALTO_SERVER_LOG_INFO("Installed first audio frame fallback probe on sink");
352 1 : m_player.setAudioFirstFrameFallbackProbe(sinkPad, probeId);
353 : }
354 : else
355 : {
356 0 : m_gstWrapper->gstObjectUnref(sinkPad);
357 : }
358 : }
359 : }
360 30 : }
361 :
362 36 : if (isVideoSink(*m_gstWrapper, m_element))
363 : {
364 10 : if (!m_context.videoSink)
365 : {
366 10 : m_gstWrapper->gstObjectRef(m_element);
367 10 : m_context.videoSink = m_element;
368 : }
369 10 : if (!m_context.pendingGeometry.empty())
370 : {
371 1 : m_player.setVideoSinkRectangle();
372 : }
373 10 : if (m_context.pendingImmediateOutputForVideo.has_value())
374 : {
375 1 : m_player.setImmediateOutput();
376 : }
377 10 : if (m_context.pendingRenderFrame)
378 : {
379 1 : m_player.setRenderFrame();
380 : }
381 10 : if (m_context.pendingShowVideoWindow.has_value())
382 : {
383 1 : m_player.setShowVideoWindow();
384 : }
385 : }
386 26 : else if (isAudioDecoder(*m_gstWrapper, m_element))
387 : {
388 7 : if (m_context.pendingSyncOff.has_value())
389 : {
390 1 : m_player.setSyncOff();
391 : }
392 7 : if (m_context.pendingStreamSyncMode.find(MediaSourceType::AUDIO) != m_context.pendingStreamSyncMode.end())
393 : {
394 1 : m_player.setStreamSyncMode(MediaSourceType::AUDIO);
395 : }
396 7 : if (m_context.pendingBufferingLimit.has_value())
397 : {
398 1 : m_player.setBufferingLimit();
399 : }
400 8 : if (m_context.isLive &&
401 1 : m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(m_element), "enable-rate-correction"))
402 : {
403 1 : RIALTO_SERVER_LOG_INFO("Enabling rate correction for broadcom decoder.");
404 1 : m_glibWrapper->gObjectSet(m_element, "enable-rate-correction", TRUE, nullptr);
405 : }
406 : }
407 19 : else if (isAudioSink(*m_gstWrapper, m_element))
408 : {
409 11 : if (m_context.pendingLowLatency.has_value())
410 : {
411 1 : m_player.setLowLatency();
412 : }
413 11 : if (m_context.pendingSync.has_value())
414 : {
415 1 : m_player.setSync();
416 : }
417 : }
418 8 : else if (isVideoParser(*m_gstWrapper, m_element))
419 : {
420 1 : if (m_context.pendingStreamSyncMode.find(MediaSourceType::VIDEO) != m_context.pendingStreamSyncMode.end())
421 : {
422 1 : m_player.setStreamSyncMode(MediaSourceType::VIDEO);
423 : }
424 : }
425 :
426 36 : if (m_gstWrapper->gstIsBaseParse(m_element))
427 : {
428 1 : m_gstWrapper->gstBaseParseSetPtsInterpolation(GST_BASE_PARSE(m_element), FALSE);
429 : }
430 36 : m_gstWrapper->gstObjectUnref(m_element);
431 : }
432 : } // namespace firebolt::rialto::server::tasks::generic
|