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 a autovideosink when a child has been added to the sink.
66 : *
67 : * @param[in] obj : the parent element (autovideosink)
68 : * @param[in] object : the child element
69 : * @param[in] name : the name of the child element
70 : * @param[in] self : The pointer to IGstGenericPlayerPrivate
71 : */
72 1 : void autoVideoSinkChildAddedCallback(GstChildProxy *obj, GObject *object, gchar *name, gpointer self)
73 : {
74 1 : RIALTO_SERVER_LOG_DEBUG("AutoVideoSink added element %s", name);
75 1 : firebolt::rialto::server::IGstGenericPlayerPrivate *player =
76 : static_cast<firebolt::rialto::server::IGstGenericPlayerPrivate *>(self);
77 1 : player->addAutoVideoSinkChild(object);
78 : }
79 :
80 : /**
81 : * @brief Callback for a autoaudiosink when a child has been added to the sink.
82 : *
83 : * @param[in] obj : the parent element (autoaudiosink)
84 : * @param[in] object : the child element
85 : * @param[in] name : the name of the child element
86 : * @param[in] self : The pointer to IGstGenericPlayerPrivate
87 : */
88 1 : void autoAudioSinkChildAddedCallback(GstChildProxy *obj, GObject *object, gchar *name, gpointer self)
89 : {
90 1 : RIALTO_SERVER_LOG_DEBUG("AutoAudioSink added element %s", name);
91 1 : firebolt::rialto::server::IGstGenericPlayerPrivate *player =
92 : static_cast<firebolt::rialto::server::IGstGenericPlayerPrivate *>(self);
93 1 : player->addAutoAudioSinkChild(object);
94 : }
95 :
96 : /**
97 : * @brief Callback for a autovideosink when a child has been removed from the sink.
98 : *
99 : * @param[in] obj : the parent element (autovideosink)
100 : * @param[in] object : the child element
101 : * @param[in] name : the name of the child element
102 : * @param[in] self : The pointer to IGstGenericPlayerPrivate
103 : */
104 1 : void autoVideoSinkChildRemovedCallback(GstChildProxy *obj, GObject *object, gchar *name, gpointer self)
105 : {
106 1 : RIALTO_SERVER_LOG_DEBUG("AutoVideoSink removed element %s", name);
107 1 : firebolt::rialto::server::IGstGenericPlayerPrivate *player =
108 : static_cast<firebolt::rialto::server::IGstGenericPlayerPrivate *>(self);
109 1 : player->removeAutoVideoSinkChild(object);
110 : }
111 :
112 : /**
113 : * @brief Callback for a autoaudiosink when a child has been removed from the sink.
114 : *
115 : * @param[in] obj : the parent element (autoaudiosink)
116 : * @param[in] object : the child element
117 : * @param[in] name : the name of the child element
118 : * @param[in] self : The pointer to IGstGenericPlayerPrivate
119 : */
120 1 : void autoAudioSinkChildRemovedCallback(GstChildProxy *obj, GObject *object, gchar *name, gpointer self)
121 : {
122 1 : RIALTO_SERVER_LOG_DEBUG("AutoAudioSink removed element %s", name);
123 1 : firebolt::rialto::server::IGstGenericPlayerPrivate *player =
124 : static_cast<firebolt::rialto::server::IGstGenericPlayerPrivate *>(self);
125 1 : player->removeAutoAudioSinkChild(object);
126 : }
127 : } // namespace
128 :
129 : namespace firebolt::rialto::server::tasks::generic
130 : {
131 31 : SetupElement::SetupElement(GenericPlayerContext &context,
132 : std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> gstWrapper,
133 : std::shared_ptr<firebolt::rialto::wrappers::IGlibWrapper> glibWrapper,
134 31 : IGstGenericPlayerPrivate &player, GstElement *element)
135 31 : : m_context{context}, m_gstWrapper{gstWrapper}, m_glibWrapper{glibWrapper}, m_player{player}, m_element{element}
136 : {
137 31 : RIALTO_SERVER_LOG_DEBUG("Constructing SetupElement");
138 : }
139 :
140 32 : SetupElement::~SetupElement()
141 : {
142 31 : RIALTO_SERVER_LOG_DEBUG("SetupElement finished");
143 32 : }
144 :
145 30 : void SetupElement::execute() const
146 : {
147 30 : RIALTO_SERVER_LOG_DEBUG("Executing SetupElement");
148 :
149 30 : const std::string kElementTypeName = m_glibWrapper->gTypeName(G_OBJECT_TYPE(m_element));
150 30 : if (kElementTypeName == "GstAutoVideoSink")
151 : {
152 : // Check and store child sink so we can set underlying properties
153 5 : m_glibWrapper->gSignalConnect(m_element, "child-added", G_CALLBACK(autoVideoSinkChildAddedCallback), &m_player);
154 5 : m_glibWrapper->gSignalConnect(m_element, "child-removed", G_CALLBACK(autoVideoSinkChildRemovedCallback),
155 5 : &m_player);
156 :
157 : // AutoVideoSink sets child before it is setup on the pipeline, so check for children here
158 5 : GstIterator *sinks = m_gstWrapper->gstBinIterateSinks(GST_BIN(m_element));
159 5 : if (sinks && sinks->size > 1)
160 : {
161 1 : RIALTO_SERVER_LOG_WARN("More than one child sink attached");
162 : }
163 :
164 5 : GValue elem = G_VALUE_INIT;
165 5 : if (m_gstWrapper->gstIteratorNext(sinks, &elem) == GST_ITERATOR_OK)
166 : {
167 2 : m_player.addAutoVideoSinkChild(G_OBJECT(m_glibWrapper->gValueGetObject(&elem)));
168 : }
169 5 : m_glibWrapper->gValueUnset(&elem);
170 :
171 5 : if (sinks)
172 5 : m_gstWrapper->gstIteratorFree(sinks);
173 : }
174 25 : else if (kElementTypeName == "GstAutoAudioSink")
175 : {
176 : // Check and store child sink so we can set underlying properties
177 5 : m_glibWrapper->gSignalConnect(m_element, "child-added", G_CALLBACK(autoAudioSinkChildAddedCallback), &m_player);
178 5 : m_glibWrapper->gSignalConnect(m_element, "child-removed", G_CALLBACK(autoAudioSinkChildRemovedCallback),
179 5 : &m_player);
180 :
181 : // AutoAudioSink sets child before it is setup on the pipeline, so check for children here
182 5 : GstIterator *sinks = m_gstWrapper->gstBinIterateSinks(GST_BIN(m_element));
183 5 : if (sinks && sinks->size > 1)
184 : {
185 1 : RIALTO_SERVER_LOG_WARN("More than one child sink attached");
186 : }
187 :
188 5 : GValue elem = G_VALUE_INIT;
189 5 : if (m_gstWrapper->gstIteratorNext(sinks, &elem) == GST_ITERATOR_OK)
190 : {
191 2 : m_player.addAutoAudioSinkChild(G_OBJECT(m_glibWrapper->gValueGetObject(&elem)));
192 : }
193 5 : m_glibWrapper->gValueUnset(&elem);
194 :
195 5 : if (sinks)
196 5 : m_gstWrapper->gstIteratorFree(sinks);
197 : }
198 :
199 30 : if (m_glibWrapper->gStrHasPrefix(GST_ELEMENT_NAME(m_element), "amlhalasink"))
200 : {
201 2 : if (m_context.streamInfo.find(MediaSourceType::VIDEO) != m_context.streamInfo.end())
202 : {
203 : // Wait for video so that the audio aligns at the starting point with timeout of 4000ms.
204 1 : m_glibWrapper->gObjectSet(m_element, "wait-video", TRUE, "a-wait-timeout", 4000, nullptr);
205 : }
206 :
207 : // Xrun occasionally pauses the underlying sink due to unstable playback, but the rest of the pipeline
208 : // remains in the playing state. This causes problems with the synchronization of gst element and rialto
209 : // ultimately hangs waiting for pipeline termination.
210 2 : m_glibWrapper->gObjectSet(m_element, "disable-xrun", TRUE, nullptr);
211 : }
212 28 : else if (m_glibWrapper->gStrHasPrefix(GST_ELEMENT_NAME(m_element), "brcmaudiosink"))
213 : {
214 1 : m_glibWrapper->gObjectSet(m_element, "async", TRUE, nullptr);
215 : }
216 27 : else if (m_glibWrapper->gStrHasPrefix(GST_ELEMENT_NAME(m_element), "rialtotexttracksink"))
217 : {
218 : // in cannot be set during construction, because playsink overwrites "sync" value of text-sink during setup
219 1 : m_glibWrapper->gObjectSet(m_element, "sync", FALSE, nullptr);
220 : }
221 :
222 30 : if (m_context.subtitleSink)
223 : {
224 1 : if (!m_context.isVideoHandleSet && isVideoDecoder(*m_gstWrapper, m_element))
225 : {
226 1 : if (m_glibWrapper->gStrHasPrefix(GST_ELEMENT_NAME(m_element), "omx") ||
227 1 : m_glibWrapper->gStrHasPrefix(GST_ELEMENT_NAME(m_element), "westerossink") ||
228 0 : m_glibWrapper->gStrHasPrefix(GST_ELEMENT_NAME(m_element), "brcmvideodecoder"))
229 : {
230 1 : uintptr_t videoDecoderHandle = reinterpret_cast<uintptr_t>(m_element);
231 1 : m_glibWrapper->gObjectSet(m_context.subtitleSink, "video-decoder", videoDecoderHandle, nullptr);
232 1 : RIALTO_SERVER_LOG_INFO("Setting video decoder handle for subtitle sink: %zu", videoDecoderHandle);
233 1 : m_context.isVideoHandleSet = true;
234 : }
235 : }
236 : }
237 :
238 30 : if (isDecoder(*m_gstWrapper, m_element) || isSink(*m_gstWrapper, m_element))
239 : {
240 26 : std::optional<std::string> underflowSignalName = getUnderflowSignalName(*m_glibWrapper, m_element);
241 26 : if (underflowSignalName)
242 : {
243 26 : if (isAudio(*m_gstWrapper, m_element))
244 : {
245 15 : RIALTO_SERVER_LOG_INFO("Connecting audio underflow callback for signal: %s",
246 : underflowSignalName.value().c_str());
247 30 : m_glibWrapper->gSignalConnect(m_element, underflowSignalName.value().c_str(),
248 15 : G_CALLBACK(audioUnderflowCallback), &m_player);
249 : }
250 11 : else if (isVideo(*m_gstWrapper, m_element))
251 : {
252 11 : RIALTO_SERVER_LOG_INFO("Connecting video underflow callback for signal: %s",
253 : underflowSignalName.value().c_str());
254 22 : m_glibWrapper->gSignalConnect(m_element, underflowSignalName.value().c_str(),
255 11 : G_CALLBACK(videoUnderflowCallback), &m_player);
256 : }
257 : }
258 26 : }
259 :
260 30 : if (isVideoSink(*m_gstWrapper, m_element))
261 : {
262 10 : if (!m_context.pendingGeometry.empty())
263 : {
264 1 : m_player.setVideoSinkRectangle();
265 : }
266 10 : if (m_context.pendingImmediateOutputForVideo.has_value())
267 : {
268 1 : m_player.setImmediateOutput();
269 : }
270 10 : if (m_context.pendingRenderFrame)
271 : {
272 1 : m_player.setRenderFrame();
273 : }
274 10 : if (m_context.pendingShowVideoWindow.has_value())
275 : {
276 1 : m_player.setShowVideoWindow();
277 : }
278 : }
279 20 : else if (isAudioDecoder(*m_gstWrapper, m_element))
280 : {
281 5 : if (m_context.pendingSyncOff.has_value())
282 : {
283 1 : m_player.setSyncOff();
284 : }
285 5 : if (m_context.pendingStreamSyncMode.find(MediaSourceType::AUDIO) != m_context.pendingStreamSyncMode.end())
286 : {
287 1 : m_player.setStreamSyncMode(MediaSourceType::AUDIO);
288 : }
289 5 : if (m_context.pendingBufferingLimit.has_value())
290 : {
291 1 : m_player.setBufferingLimit();
292 : }
293 : }
294 15 : else if (isAudioSink(*m_gstWrapper, m_element))
295 : {
296 10 : if (m_context.pendingLowLatency.has_value())
297 : {
298 1 : m_player.setLowLatency();
299 : }
300 10 : if (m_context.pendingSync.has_value())
301 : {
302 1 : m_player.setSync();
303 : }
304 : }
305 5 : else if (isVideoParser(*m_gstWrapper, m_element))
306 : {
307 1 : if (m_context.pendingStreamSyncMode.find(MediaSourceType::VIDEO) != m_context.pendingStreamSyncMode.end())
308 : {
309 1 : m_player.setStreamSyncMode(MediaSourceType::VIDEO);
310 : }
311 : }
312 :
313 30 : if (m_gstWrapper->gstIsBaseParse(m_element))
314 : {
315 1 : m_gstWrapper->gstBaseParseSetPtsInterpolation(GST_BASE_PARSE(m_element), FALSE);
316 : }
317 :
318 30 : m_gstWrapper->gstObjectUnref(m_element);
319 : }
320 : } // namespace firebolt::rialto::server::tasks::generic
|