1 /* GStreamer output selector
2 * Copyright (C) 2008 Nokia Corporation. (contact <stefan.kost@nokia.com>)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * SECTION:element-output-selector
22 * @see_also: #GstOutputSelector, #GstInputSelector
24 * Direct input stream to one out of N output pads.
35 #include "gstoutputselector.h"
37 GST_DEBUG_CATEGORY_STATIC (output_selector_debug);
38 #define GST_CAT_DEFAULT output_selector_debug
40 static GstStaticPadTemplate gst_output_selector_sink_factory =
41 GST_STATIC_PAD_TEMPLATE ("sink",
46 static GstStaticPadTemplate gst_output_selector_src_factory =
47 GST_STATIC_PAD_TEMPLATE ("src%d",
52 enum GstOutputSelectorPadNegotiationMode
54 GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE,
55 GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL,
56 GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ACTIVE
58 #define GST_TYPE_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE (gst_output_selector_pad_negotiation_mode_get_type())
60 gst_output_selector_pad_negotiation_mode_get_type (void)
62 static GType pad_negotiation_mode_type = 0;
63 static GEnumValue pad_negotiation_modes[] = {
64 {GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE, "None", "none"},
65 {GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL, "All", "all"},
66 {GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ACTIVE, "Active", "active"},
70 if (!pad_negotiation_mode_type) {
71 pad_negotiation_mode_type =
72 g_enum_register_static ("GstOutputSelectorPadNegotiationMode",
73 pad_negotiation_modes);
75 return pad_negotiation_mode_type;
84 PROP_PAD_NEGOTIATION_MODE
87 #define DEFAULT_PAD_NEGOTIATION_MODE GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL
89 #define _do_init(bla) \
90 GST_DEBUG_CATEGORY_INIT (output_selector_debug, \
91 "output-selector", 0, "Output stream selector");
93 GST_BOILERPLATE_FULL (GstOutputSelector, gst_output_selector, GstElement,
94 GST_TYPE_ELEMENT, _do_init);
96 static void gst_output_selector_dispose (GObject * object);
97 static void gst_output_selector_set_property (GObject * object,
98 guint prop_id, const GValue * value, GParamSpec * pspec);
99 static void gst_output_selector_get_property (GObject * object,
100 guint prop_id, GValue * value, GParamSpec * pspec);
101 static GstPad *gst_output_selector_request_new_pad (GstElement * element,
102 GstPadTemplate * templ, const gchar * unused);
103 static void gst_output_selector_release_pad (GstElement * element,
105 static GstFlowReturn gst_output_selector_chain (GstPad * pad, GstBuffer * buf);
106 static GstFlowReturn gst_output_selector_buffer_alloc (GstPad * pad,
107 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
108 static GstStateChangeReturn gst_output_selector_change_state (GstElement *
109 element, GstStateChange transition);
110 static gboolean gst_output_selector_handle_sink_event (GstPad * pad,
112 static void gst_output_selector_switch_pad_negotiation_mode (GstOutputSelector *
116 gst_output_selector_base_init (gpointer g_class)
118 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
120 gst_element_class_set_details_simple (element_class, "Output selector",
121 "Generic", "1-to-N output stream selector",
122 "Stefan Kost <stefan.kost@nokia.com>");
123 gst_element_class_add_pad_template (element_class,
124 gst_static_pad_template_get (&gst_output_selector_sink_factory));
125 gst_element_class_add_pad_template (element_class,
126 gst_static_pad_template_get (&gst_output_selector_src_factory));
130 gst_output_selector_class_init (GstOutputSelectorClass * klass)
132 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
133 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
135 gobject_class->dispose = gst_output_selector_dispose;
137 gobject_class->set_property = gst_output_selector_set_property;
138 gobject_class->get_property = gst_output_selector_get_property;
140 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
141 g_param_spec_object ("active-pad", "Active pad",
142 "Currently active src pad", GST_TYPE_PAD,
143 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
144 g_object_class_install_property (gobject_class, PROP_RESEND_LATEST,
145 g_param_spec_boolean ("resend-latest", "Resend latest buffer",
146 "Resend latest buffer after a switch to a new pad", FALSE,
147 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
148 g_object_class_install_property (gobject_class, PROP_PAD_NEGOTIATION_MODE,
149 g_param_spec_enum ("pad-negotiation-mode", "Pad negotiation mode",
150 "The mode to be used for pad negotiation",
151 GST_TYPE_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE,
152 DEFAULT_PAD_NEGOTIATION_MODE,
153 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155 gstelement_class->request_new_pad =
156 GST_DEBUG_FUNCPTR (gst_output_selector_request_new_pad);
157 gstelement_class->release_pad =
158 GST_DEBUG_FUNCPTR (gst_output_selector_release_pad);
160 gstelement_class->change_state = gst_output_selector_change_state;
164 gst_output_selector_init (GstOutputSelector * sel,
165 GstOutputSelectorClass * g_class)
168 gst_pad_new_from_static_template (&gst_output_selector_sink_factory,
170 gst_pad_set_chain_function (sel->sinkpad,
171 GST_DEBUG_FUNCPTR (gst_output_selector_chain));
172 gst_pad_set_event_function (sel->sinkpad,
173 GST_DEBUG_FUNCPTR (gst_output_selector_handle_sink_event));
174 gst_pad_set_bufferalloc_function (sel->sinkpad,
175 GST_DEBUG_FUNCPTR (gst_output_selector_buffer_alloc));
177 gst_element_add_pad (GST_ELEMENT (sel), sel->sinkpad);
179 /* srcpad management */
180 sel->active_srcpad = NULL;
182 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
183 sel->pending_srcpad = NULL;
185 sel->resend_latest = FALSE;
186 sel->latest_buffer = NULL;
187 gst_output_selector_switch_pad_negotiation_mode (sel,
188 DEFAULT_PAD_NEGOTIATION_MODE);
192 gst_output_selector_reset (GstOutputSelector * osel)
194 if (osel->pending_srcpad != NULL) {
195 gst_object_unref (osel->pending_srcpad);
196 osel->pending_srcpad = NULL;
198 if (osel->latest_buffer != NULL) {
199 gst_buffer_unref (osel->latest_buffer);
200 osel->latest_buffer = NULL;
202 gst_segment_init (&osel->segment, GST_FORMAT_UNDEFINED);
206 gst_output_selector_dispose (GObject * object)
208 GstOutputSelector *osel = GST_OUTPUT_SELECTOR (object);
210 gst_output_selector_reset (osel);
212 G_OBJECT_CLASS (parent_class)->dispose (object);
216 gst_output_selector_set_property (GObject * object, guint prop_id,
217 const GValue * value, GParamSpec * pspec)
219 GstOutputSelector *sel = GST_OUTPUT_SELECTOR (object);
222 case PROP_ACTIVE_PAD:
226 next_pad = g_value_get_object (value);
228 GST_INFO_OBJECT (sel, "Activating pad %s:%s",
229 GST_DEBUG_PAD_NAME (next_pad));
231 GST_OBJECT_LOCK (object);
232 if (next_pad != sel->active_srcpad) {
233 /* switch to new srcpad in next chain run */
234 if (sel->pending_srcpad != NULL) {
235 GST_INFO ("replacing pending switch");
236 gst_object_unref (sel->pending_srcpad);
239 gst_object_ref (next_pad);
240 sel->pending_srcpad = next_pad;
242 GST_INFO ("pad already active");
243 if (sel->pending_srcpad != NULL) {
244 gst_object_unref (sel->pending_srcpad);
245 sel->pending_srcpad = NULL;
248 GST_OBJECT_UNLOCK (object);
251 case PROP_RESEND_LATEST:{
252 sel->resend_latest = g_value_get_boolean (value);
255 case PROP_PAD_NEGOTIATION_MODE:{
256 gst_output_selector_switch_pad_negotiation_mode (sel,
257 g_value_get_enum (value));
261 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
267 gst_output_selector_get_property (GObject * object, guint prop_id,
268 GValue * value, GParamSpec * pspec)
270 GstOutputSelector *sel = GST_OUTPUT_SELECTOR (object);
273 case PROP_ACTIVE_PAD:
274 GST_OBJECT_LOCK (object);
275 g_value_set_object (value,
276 sel->pending_srcpad ? sel->pending_srcpad : sel->active_srcpad);
277 GST_OBJECT_UNLOCK (object);
279 case PROP_RESEND_LATEST:{
280 GST_OBJECT_LOCK (object);
281 g_value_set_boolean (value, sel->resend_latest);
282 GST_OBJECT_UNLOCK (object);
285 case PROP_PAD_NEGOTIATION_MODE:
286 g_value_set_enum (value, sel->pad_negotiation_mode);
289 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
295 gst_output_selector_sink_getcaps (GstPad * pad)
297 GstOutputSelector *sel = GST_OUTPUT_SELECTOR (gst_pad_get_parent (pad));
298 GstPad *active = NULL;
299 GstCaps *caps = NULL;
301 if (G_UNLIKELY (sel == NULL))
302 return gst_caps_new_any ();
304 GST_OBJECT_LOCK (sel);
305 if (sel->pending_srcpad)
306 active = gst_object_ref (sel->pending_srcpad);
307 else if (sel->active_srcpad)
308 active = gst_object_ref (sel->active_srcpad);
309 GST_OBJECT_UNLOCK (sel);
312 caps = gst_pad_peer_get_caps_reffed (active);
313 gst_object_unref (active);
316 caps = gst_caps_new_any ();
319 gst_object_unref (sel);
324 gst_output_selector_sink_setcaps (GstPad * pad, GstCaps * caps)
326 GstOutputSelector *sel = GST_OUTPUT_SELECTOR (GST_PAD_PARENT (pad));
327 GstPad *active = NULL;
330 GST_OBJECT_LOCK (sel);
331 if (sel->pending_srcpad)
332 active = gst_object_ref (sel->pending_srcpad);
333 else if (sel->active_srcpad)
334 active = gst_object_ref (sel->active_srcpad);
335 GST_OBJECT_UNLOCK (sel);
338 ret = gst_pad_set_caps (active, caps);
339 gst_object_unref (active);
345 gst_output_selector_switch_pad_negotiation_mode (GstOutputSelector * sel,
348 sel->pad_negotiation_mode = mode;
349 if (mode == GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL) {
350 gst_pad_set_getcaps_function (sel->sinkpad, gst_pad_proxy_getcaps);
351 gst_pad_set_setcaps_function (sel->sinkpad, gst_pad_proxy_setcaps);
352 } else if (mode == GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE) {
353 gst_pad_set_getcaps_function (sel->sinkpad, NULL);
354 gst_pad_set_setcaps_function (sel->sinkpad, NULL);
355 } else { /* active */
356 gst_pad_set_getcaps_function (sel->sinkpad,
357 gst_output_selector_sink_getcaps);
358 gst_pad_set_setcaps_function (sel->sinkpad,
359 gst_output_selector_sink_setcaps);
364 gst_output_selector_buffer_alloc (GstPad * pad, guint64 offset, guint size,
365 GstCaps * caps, GstBuffer ** buf)
367 GstOutputSelector *sel;
371 sel = GST_OUTPUT_SELECTOR (gst_pad_get_parent (pad));
372 if (G_UNLIKELY (sel == NULL))
373 return GST_FLOW_WRONG_STATE;
375 GST_OBJECT_LOCK (sel);
376 allocpad = sel->pending_srcpad ? sel->pending_srcpad : sel->active_srcpad;
378 /* if we had a previous pad we used for allocating a buffer, continue using
380 GST_DEBUG_OBJECT (sel, "using pad %s:%s for alloc",
381 GST_DEBUG_PAD_NAME (allocpad));
382 gst_object_ref (allocpad);
383 GST_OBJECT_UNLOCK (sel);
385 res = gst_pad_alloc_buffer (allocpad, offset, size, caps, buf);
386 gst_object_unref (allocpad);
388 GST_OBJECT_LOCK (sel);
390 /* fallback case, allocate a buffer of our own, add pad caps. */
391 GST_DEBUG_OBJECT (pad, "fallback buffer alloc");
395 GST_OBJECT_UNLOCK (sel);
397 GST_DEBUG_OBJECT (sel, "buffer alloc finished: %s", gst_flow_get_name (res));
399 gst_object_unref (sel);
404 gst_output_selector_request_new_pad (GstElement * element,
405 GstPadTemplate * templ, const gchar * name)
409 GstOutputSelector *osel;
411 osel = GST_OUTPUT_SELECTOR (element);
413 GST_DEBUG_OBJECT (osel, "requesting pad");
415 GST_OBJECT_LOCK (osel);
416 padname = g_strdup_printf ("src%d", osel->nb_srcpads++);
417 srcpad = gst_pad_new_from_template (templ, padname);
418 GST_OBJECT_UNLOCK (osel);
420 gst_pad_set_active (srcpad, TRUE);
421 gst_element_add_pad (GST_ELEMENT (osel), srcpad);
423 /* Set the first requested src pad as active by default */
424 if (osel->active_srcpad == NULL) {
425 osel->active_srcpad = srcpad;
433 gst_output_selector_release_pad (GstElement * element, GstPad * pad)
435 GstOutputSelector *osel;
437 osel = GST_OUTPUT_SELECTOR (element);
439 GST_DEBUG_OBJECT (osel, "releasing pad");
441 gst_pad_set_active (pad, FALSE);
443 gst_element_remove_pad (GST_ELEMENT_CAST (osel), pad);
447 gst_output_selector_switch (GstOutputSelector * osel)
449 gboolean res = FALSE;
451 GstSegment *seg = NULL;
452 gint64 start = 0, position = 0;
455 GST_OBJECT_LOCK (GST_OBJECT (osel));
456 GST_INFO ("switching to pad %" GST_PTR_FORMAT, osel->pending_srcpad);
457 if (gst_pad_is_linked (osel->pending_srcpad)) {
458 osel->active_srcpad = osel->pending_srcpad;
461 gst_object_unref (osel->pending_srcpad);
462 osel->pending_srcpad = NULL;
463 GST_OBJECT_UNLOCK (GST_OBJECT (osel));
465 /* Send NEWSEGMENT event and latest buffer if switching succeeded
466 * and we already have a valid segment configured */
467 if (res && osel->segment.format != GST_FORMAT_UNDEFINED) {
468 /* Send NEWSEGMENT to the pad we are going to switch to */
469 seg = &osel->segment;
471 /* If resending then mark newsegment start and position accordingly */
472 if (osel->resend_latest && osel->latest_buffer &&
473 GST_BUFFER_TIMESTAMP_IS_VALID (osel->latest_buffer)) {
474 start = position = GST_BUFFER_TIMESTAMP (osel->latest_buffer);
476 start = position = seg->last_stop;
478 ev = gst_event_new_new_segment (TRUE, seg->rate,
479 seg->format, start, seg->stop, position);
480 if (!gst_pad_push_event (osel->active_srcpad, ev)) {
481 GST_WARNING_OBJECT (osel,
482 "newsegment handling failed in %" GST_PTR_FORMAT,
483 osel->active_srcpad);
486 /* Resend latest buffer to newly switched pad */
487 if (osel->resend_latest && osel->latest_buffer) {
488 GST_INFO ("resending latest buffer");
489 gst_pad_push (osel->active_srcpad, gst_buffer_ref (osel->latest_buffer));
492 GST_WARNING_OBJECT (osel, "switch failed, pad not linked");
499 gst_output_selector_chain (GstPad * pad, GstBuffer * buf)
502 GstOutputSelector *osel;
503 GstClockTime last_stop, duration;
505 osel = GST_OUTPUT_SELECTOR (gst_pad_get_parent (pad));
508 * The _switch function might push a buffer if 'resend-latest' is true.
510 * Elements/Applications (e.g. camerabin) might use pad probes to
511 * switch output-selector's active pad. If we simply switch and don't
512 * recheck any pending pad switch the following codepath could end
513 * up pushing a buffer on a non-active pad. This is bad.
515 * So we always should check the pending_srcpad before going further down
516 * the chain and pushing the new buffer
518 while (osel->pending_srcpad) {
520 gst_output_selector_switch (osel);
523 if (osel->latest_buffer) {
524 gst_buffer_unref (osel->latest_buffer);
525 osel->latest_buffer = NULL;
528 if (osel->resend_latest) {
529 /* Keep reference to latest buffer to resend it after switch */
530 osel->latest_buffer = gst_buffer_ref (buf);
533 /* Keep track of last stop and use it in NEWSEGMENT start after
534 switching to a new src pad */
535 last_stop = GST_BUFFER_TIMESTAMP (buf);
536 if (GST_CLOCK_TIME_IS_VALID (last_stop)) {
537 duration = GST_BUFFER_DURATION (buf);
538 if (GST_CLOCK_TIME_IS_VALID (duration)) {
539 last_stop += duration;
541 GST_LOG_OBJECT (osel, "setting last stop %" GST_TIME_FORMAT,
542 GST_TIME_ARGS (last_stop));
543 gst_segment_set_last_stop (&osel->segment, osel->segment.format, last_stop);
546 GST_LOG_OBJECT (osel, "pushing buffer to %" GST_PTR_FORMAT,
547 osel->active_srcpad);
548 res = gst_pad_push (osel->active_srcpad, buf);
549 gst_object_unref (osel);
554 static GstStateChangeReturn
555 gst_output_selector_change_state (GstElement * element,
556 GstStateChange transition)
558 GstOutputSelector *sel;
559 GstStateChangeReturn result;
561 sel = GST_OUTPUT_SELECTOR (element);
563 switch (transition) {
564 case GST_STATE_CHANGE_READY_TO_PAUSED:
570 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
572 switch (transition) {
573 case GST_STATE_CHANGE_PAUSED_TO_READY:
574 gst_output_selector_reset (sel);
584 gst_output_selector_handle_sink_event (GstPad * pad, GstEvent * event)
587 GstOutputSelector *sel;
588 GstPad *output_pad = NULL;
590 sel = GST_OUTPUT_SELECTOR (gst_pad_get_parent (pad));
591 if (G_UNLIKELY (sel == NULL)) {
592 gst_event_unref (event);
596 switch (GST_EVENT_TYPE (event)) {
597 case GST_EVENT_NEWSEGMENT:
602 gint64 start, stop, time;
604 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
605 &start, &stop, &time);
607 GST_DEBUG_OBJECT (sel,
608 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
609 "format %d, " "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
610 G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
612 gst_segment_set_newsegment_full (&sel->segment, update,
613 rate, arate, format, start, stop, time);
615 /* Send newsegment to all src pads */
616 gst_pad_event_default (pad, event);
620 /* Send eos to all src pads */
621 gst_pad_event_default (pad, event);
624 /* Send other events to pending or active src pad */
626 sel->pending_srcpad ? sel->pending_srcpad : sel->active_srcpad;
627 res = gst_pad_push_event (output_pad, event);
631 gst_object_unref (sel);