1 /*
2 * Copyright (C) 2018 Centricular Ltd.
3 * Author: Sebastian Dröge <sebastian@centricular.com>
4 * Author: Nirbheek Chauhan <nirbheek@centricular.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 /**
23 * SECTION:element-proxysrc
24 * @title: proxysrc
25 *
26 * Proxysrc is a source element that proxies events, queries, and buffers from
27 * another pipeline that contains a matching proxysink element. The purpose is
28 * to allow two decoupled pipelines to function as though they are one without
29 * having to manually shuttle buffers, events, queries, etc between the two.
30 *
31 * The element queues buffers from the matching proxysink to an internal queue,
32 * so everything downstream is properly decoupled from the upstream pipeline.
33 * However, the queue may get filled up if the downstream pipeline does not
34 * accept buffers quickly enough; perhaps because it is not yet PLAYING.
35 *
36 * ## Usage
37 *
38 * |[<!-- language="C" -->
39 * GstElement *pipe1, *pipe2, *psink, *psrc;
40 * GstClock *clock;
41 *
42 * pipe1 = gst_parse_launch ("audiotestsrc ! proxysink name=psink", NULL);
43 * psink = gst_bin_get_by_name (GST_BIN (pipe1), "psink");
44 *
45 * pipe2 = gst_parse_launch ("proxysrc name=psrc ! autoaudiosink", NULL);
46 * psrc = gst_bin_get_by_name (GST_BIN (pipe2), "psrc");
47 *
48 * // Connect the two pipelines
49 * g_object_set (psrc, "proxysink", psink, NULL);
50 *
51 * // Both pipelines must agree on the timing information or we'll get glitches
52 * // or overruns/underruns. Ideally, we should tell pipe1 to use the same clock
53 * // as pipe2, but since that will be set asynchronously to the audio clock, it
54 * // is simpler and likely accurate enough to use the system clock for both
55 * // pipelines. If no element in either pipeline will provide a clock, this
56 * // is not needed.
57 * clock = gst_system_clock_obtain ();
58 * gst_pipeline_use_clock (GST_PIPELINE (pipe1), clock);
59 * gst_pipeline_use_clock (GST_PIPELINE (pipe2), clock);
60 * g_object_unref (clock);
61 *
62 * // This is not really needed in this case since the pipelines are created and
63 * // started at the same time. However, an application that dynamically
64 * // generates pipelines must ensure that all the pipelines that will be
65 * // connected together share the same base time.
66 * gst_element_set_base_time (pipe1, 0);
67 * gst_element_set_base_time (pipe2, 0);
68 *
69 * gst_element_set_state (pipe1, GST_STATE_PLAYING);
70 * gst_element_set_state (pipe2, GST_STATE_PLAYING);
71 * ]|
72 *
73 */
74
75 #ifdef HAVE_CONFIG_H
76 #include "config.h"
77 #endif
78 #include "gstproxysrc.h"
79 #include "gstproxysink.h"
80 #include "gstproxy-priv.h"
81
82 #define GST_CAT_DEFAULT gst_proxy_src_debug
83 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
84
85 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
86 GST_PAD_SRC,
87 GST_PAD_ALWAYS,
88 GST_STATIC_CAPS_ANY);
89
90 enum
91 {
92 PROP_0,
93 PROP_PROXYSINK,
94 };
95
96 /* We're not subclassing from basesrc because we don't want any of the special
97 * handling it has for events/queries/etc. We just pass-through everything. */
98
99 /* Our parent type is a GstBin instead of GstElement because we contain a queue
100 * element */
101 #define parent_class gst_proxy_src_parent_class
102 G_DEFINE_TYPE (GstProxySrc, gst_proxy_src, GST_TYPE_BIN);
103 GST_ELEMENT_REGISTER_DEFINE (proxysrc, "proxysrc", GST_RANK_NONE,
104 GST_TYPE_PROXY_SRC);
105
106 static gboolean gst_proxy_src_internal_src_query (GstPad * pad,
107 GstObject * parent, GstQuery * query);
108 static gboolean gst_proxy_src_internal_src_event (GstPad * pad,
109 GstObject * parent, GstEvent * event);
110
111 static GstStateChangeReturn gst_proxy_src_change_state (GstElement * element,
112 GstStateChange transition);
113 static gboolean gst_proxy_src_send_event (GstElement * element,
114 GstEvent * event);
115 static gboolean gst_proxy_src_query (GstElement * element, GstQuery * query);
116 static void gst_proxy_src_dispose (GObject * object);
117
118 static void
gst_proxy_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * spec)119 gst_proxy_src_get_property (GObject * object, guint prop_id, GValue * value,
120 GParamSpec * spec)
121 {
122 GstProxySrc *self = GST_PROXY_SRC (object);
123
124 switch (prop_id) {
125 case PROP_PROXYSINK:
126 g_value_take_object (value, g_weak_ref_get (&self->proxysink));
127 break;
128 default:
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
130 break;
131 }
132 }
133
134 static void
gst_proxy_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * spec)135 gst_proxy_src_set_property (GObject * object, guint prop_id,
136 const GValue * value, GParamSpec * spec)
137 {
138 GstProxySrc *self = GST_PROXY_SRC (object);
139 GstProxySink *sink;
140
141 switch (prop_id) {
142 case PROP_PROXYSINK:
143 sink = g_value_dup_object (value);
144 if (sink == NULL) {
145 /* Unset proxysrc property on the existing proxysink to break the
146 * connection in that direction */
147 GstProxySink *old_sink = g_weak_ref_get (&self->proxysink);
148 if (old_sink) {
149 gst_proxy_sink_set_proxysrc (old_sink, NULL);
150 g_object_unref (old_sink);
151 }
152 g_weak_ref_set (&self->proxysink, NULL);
153 } else {
154 /* Set proxysrc property on the new proxysink to point to us */
155 gst_proxy_sink_set_proxysrc (sink, self);
156 g_weak_ref_set (&self->proxysink, sink);
157 g_object_unref (sink);
158 }
159 break;
160 default:
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
162 }
163 }
164
165 static void
gst_proxy_src_class_init(GstProxySrcClass * klass)166 gst_proxy_src_class_init (GstProxySrcClass * klass)
167 {
168 GObjectClass *gobject_class = (GObjectClass *) klass;
169 GstElementClass *gstelement_class = (GstElementClass *) klass;
170
171 GST_DEBUG_CATEGORY_INIT (gst_proxy_src_debug, "proxysrc", 0, "proxy sink");
172
173 gobject_class->dispose = gst_proxy_src_dispose;
174
175 gobject_class->get_property = gst_proxy_src_get_property;
176 gobject_class->set_property = gst_proxy_src_set_property;
177
178 g_object_class_install_property (gobject_class, PROP_PROXYSINK,
179 g_param_spec_object ("proxysink", "Proxysink", "Matching proxysink",
180 GST_TYPE_PROXY_SINK, G_PARAM_READWRITE));
181
182 gstelement_class->change_state = gst_proxy_src_change_state;
183 gstelement_class->send_event = gst_proxy_src_send_event;
184 gstelement_class->query = gst_proxy_src_query;
185 gst_element_class_add_pad_template (gstelement_class,
186 gst_static_pad_template_get (&src_template));
187
188 gst_element_class_set_static_metadata (gstelement_class, "Proxy source",
189 "Source", "Proxy source for internal process communication",
190 "Sebastian Dröge <sebastian@centricular.com>");
191 }
192
193 static void
gst_proxy_src_init(GstProxySrc * self)194 gst_proxy_src_init (GstProxySrc * self)
195 {
196 GstPad *srcpad, *sinkpad;
197 GstPadTemplate *templ;
198
199 GST_OBJECT_FLAG_SET (self, GST_ELEMENT_FLAG_SOURCE);
200
201 /* We feed incoming buffers into a queue to decouple the downstream pipeline
202 * from the upstream pipeline */
203 self->queue = gst_element_factory_make ("queue", NULL);
204 gst_bin_add (GST_BIN (self), self->queue);
205
206 srcpad = gst_element_get_static_pad (self->queue, "src");
207 templ = gst_static_pad_template_get (&src_template);
208 self->srcpad = gst_ghost_pad_new_from_template ("src", srcpad, templ);
209 gst_object_unref (templ);
210 gst_object_unref (srcpad);
211
212 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
213
214 /* A dummy sinkpad that's not actually used anywhere
215 * Explanation for why this is needed is below */
216 self->dummy_sinkpad = gst_pad_new ("dummy_sinkpad", GST_PAD_SINK);
217 gst_object_set_parent (GST_OBJECT (self->dummy_sinkpad), GST_OBJECT (self));
218
219 self->internal_srcpad = gst_pad_new ("internal_src", GST_PAD_SRC);
220 gst_object_set_parent (GST_OBJECT (self->internal_srcpad),
221 GST_OBJECT (self->dummy_sinkpad));
222 gst_pad_set_event_function (self->internal_srcpad,
223 gst_proxy_src_internal_src_event);
224 gst_pad_set_query_function (self->internal_srcpad,
225 gst_proxy_src_internal_src_query);
226
227 /* We need to link internal_srcpad from proxysink to the sinkpad of our
228 * queue. However, two pads can only be linked if they share a common parent.
229 * Above, we set the parent of the dummy_sinkpad as proxysrc, and then we set
230 * the parent of internal_srcpad as dummy_sinkpad. This causes both these pads
231 * to share a parent allowing us to link them.
232 * Yes, this is a hack/workaround. */
233 sinkpad = gst_element_get_static_pad (self->queue, "sink");
234 gst_pad_link (self->internal_srcpad, sinkpad);
235 gst_object_unref (sinkpad);
236
237 gst_bin_set_suppressed_flags (GST_BIN (self),
238 GST_ELEMENT_FLAG_SOURCE | GST_ELEMENT_FLAG_SINK);
239 GST_OBJECT_FLAG_SET (self, GST_ELEMENT_FLAG_SOURCE);
240 }
241
242 static void
gst_proxy_src_dispose(GObject * object)243 gst_proxy_src_dispose (GObject * object)
244 {
245 GstProxySrc *self = GST_PROXY_SRC (object);
246
247 gst_object_unparent (GST_OBJECT (self->dummy_sinkpad));
248 self->dummy_sinkpad = NULL;
249
250 gst_object_unparent (GST_OBJECT (self->internal_srcpad));
251 self->internal_srcpad = NULL;
252
253 g_weak_ref_set (&self->proxysink, NULL);
254
255 G_OBJECT_CLASS (gst_proxy_src_parent_class)->dispose (object);
256 }
257
258 static GstStateChangeReturn
gst_proxy_src_change_state(GstElement * element,GstStateChange transition)259 gst_proxy_src_change_state (GstElement * element, GstStateChange transition)
260 {
261 GstElementClass *gstelement_class =
262 GST_ELEMENT_CLASS (gst_proxy_src_parent_class);
263 GstProxySrc *self = GST_PROXY_SRC (element);
264 GstStateChangeReturn ret;
265
266 ret = gstelement_class->change_state (element, transition);
267 if (ret == GST_STATE_CHANGE_FAILURE)
268 return ret;
269
270 switch (transition) {
271 case GST_STATE_CHANGE_READY_TO_PAUSED:
272 ret = GST_STATE_CHANGE_NO_PREROLL;
273 gst_pad_set_active (self->internal_srcpad, TRUE);
274 break;
275 case GST_STATE_CHANGE_PAUSED_TO_READY:
276 gst_pad_set_active (self->internal_srcpad, FALSE);
277 break;
278 default:
279 break;
280 }
281
282 return ret;
283 }
284
285 static gboolean
gst_proxy_src_send_event(GstElement * element,GstEvent * event)286 gst_proxy_src_send_event (GstElement * element, GstEvent * event)
287 {
288 GstProxySrc *self = GST_PROXY_SRC (element);
289
290 if (GST_EVENT_IS_DOWNSTREAM (event)) {
291 GstPad *sinkpad = gst_element_get_static_pad (self->queue, "sink");
292 gboolean ret;
293
294 ret = gst_pad_send_event (sinkpad, event);
295 gst_object_unref (sinkpad);
296 return ret;
297 } else {
298 gst_event_unref (event);
299 return FALSE;
300 }
301 }
302
303 static gboolean
gst_proxy_src_query(GstElement * element,GstQuery * query)304 gst_proxy_src_query (GstElement * element, GstQuery * query)
305 {
306 GstProxySrc *self = GST_PROXY_SRC (element);
307
308 if (GST_QUERY_IS_DOWNSTREAM (query)) {
309 GstPad *sinkpad = gst_element_get_static_pad (self->queue, "sink");
310 gboolean ret;
311
312 ret = gst_pad_query (sinkpad, query);
313 gst_object_unref (sinkpad);
314 return ret;
315 } else {
316 return FALSE;
317 }
318 }
319
320 static gboolean
gst_proxy_src_internal_src_query(GstPad * pad,GstObject * parent,GstQuery * query)321 gst_proxy_src_internal_src_query (GstPad * pad, GstObject * parent,
322 GstQuery * query)
323 {
324 GstProxySrc *self = GST_PROXY_SRC (gst_object_get_parent (parent));
325 GstProxySink *sink;
326 gboolean ret = FALSE;
327
328 if (!self)
329 return ret;
330
331 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
332 gst_query_type_get_name (GST_QUERY_TYPE (query)));
333
334 sink = g_weak_ref_get (&self->proxysink);
335 if (sink) {
336 GstPad *sinkpad;
337 sinkpad = gst_proxy_sink_get_internal_sinkpad (sink);
338
339 ret = gst_pad_peer_query (sinkpad, query);
340 gst_object_unref (sinkpad);
341 gst_object_unref (sink);
342 }
343
344 gst_object_unref (self);
345
346 return ret;
347 }
348
349 static gboolean
gst_proxy_src_internal_src_event(GstPad * pad,GstObject * parent,GstEvent * event)350 gst_proxy_src_internal_src_event (GstPad * pad, GstObject * parent,
351 GstEvent * event)
352 {
353 GstProxySrc *self = GST_PROXY_SRC (gst_object_get_parent (parent));
354 GstProxySink *sink;
355 gboolean ret = FALSE;
356
357 if (!self)
358 return ret;
359
360 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
361
362 sink = g_weak_ref_get (&self->proxysink);
363 if (sink) {
364 GstPad *sinkpad;
365 sinkpad = gst_proxy_sink_get_internal_sinkpad (sink);
366
367 ret = gst_pad_push_event (sinkpad, event);
368 gst_object_unref (sinkpad);
369 gst_object_unref (sink);
370 } else
371 gst_event_unref (event);
372
373
374 gst_object_unref (self);
375
376 return ret;
377 }
378
379 /* Wrapper function for accessing private member */
380 GstPad *
gst_proxy_src_get_internal_srcpad(GstProxySrc * self)381 gst_proxy_src_get_internal_srcpad (GstProxySrc * self)
382 {
383 return gst_object_ref (self->internal_srcpad);
384 }
385