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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * SECTION:element-output-selector
22 * @title: output-selector
23 * @see_also: #GstOutputSelector, #GstInputSelector
25 * Direct input stream to one out of N output pads.
34 #include "gstoutputselector.h"
36 GST_DEBUG_CATEGORY_STATIC (output_selector_debug);
37 #define GST_CAT_DEFAULT output_selector_debug
39 static GstStaticPadTemplate gst_output_selector_sink_factory =
40 GST_STATIC_PAD_TEMPLATE ("sink",
45 static GstStaticPadTemplate gst_output_selector_src_factory =
46 GST_STATIC_PAD_TEMPLATE ("src_%u",
51 #define GST_TYPE_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE (gst_output_selector_pad_negotiation_mode_get_type())
53 gst_output_selector_pad_negotiation_mode_get_type (void)
55 static GType pad_negotiation_mode_type = 0;
56 static const GEnumValue pad_negotiation_modes[] = {
57 {GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE, "None", "none"},
58 {GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL, "All", "all"},
59 {GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ACTIVE, "Active", "active"},
63 if (!pad_negotiation_mode_type) {
64 pad_negotiation_mode_type =
65 g_enum_register_static ("GstOutputSelectorPadNegotiationMode",
66 pad_negotiation_modes);
68 return pad_negotiation_mode_type;
77 PROP_PAD_NEGOTIATION_MODE
80 #define DEFAULT_PAD_NEGOTIATION_MODE GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL
83 GST_DEBUG_CATEGORY_INIT (output_selector_debug, \
84 "output-selector", 0, "Output stream selector");
85 #define gst_output_selector_parent_class parent_class
86 G_DEFINE_TYPE_WITH_CODE (GstOutputSelector, gst_output_selector,
87 GST_TYPE_ELEMENT, _do_init);
89 static void gst_output_selector_dispose (GObject * object);
90 static void gst_output_selector_set_property (GObject * object,
91 guint prop_id, const GValue * value, GParamSpec * pspec);
92 static void gst_output_selector_get_property (GObject * object,
93 guint prop_id, GValue * value, GParamSpec * pspec);
94 static GstPad *gst_output_selector_request_new_pad (GstElement * element,
95 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps);
96 static void gst_output_selector_release_pad (GstElement * element,
98 static GstFlowReturn gst_output_selector_chain (GstPad * pad,
99 GstObject * parent, GstBuffer * buf);
100 static GstStateChangeReturn gst_output_selector_change_state (GstElement *
101 element, GstStateChange transition);
102 static gboolean gst_output_selector_event (GstPad * pad, GstObject * parent,
104 static gboolean gst_output_selector_query (GstPad * pad, GstObject * parent,
106 static void gst_output_selector_switch_pad_negotiation_mode (GstOutputSelector *
110 gst_output_selector_class_init (GstOutputSelectorClass * klass)
112 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
113 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
115 gobject_class->dispose = gst_output_selector_dispose;
117 gobject_class->set_property = gst_output_selector_set_property;
118 gobject_class->get_property = gst_output_selector_get_property;
120 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
121 g_param_spec_object ("active-pad", "Active pad",
122 "Currently active src pad", GST_TYPE_PAD,
123 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
124 G_PARAM_STATIC_STRINGS));
125 g_object_class_install_property (gobject_class, PROP_RESEND_LATEST,
126 g_param_spec_boolean ("resend-latest", "Resend latest buffer",
127 "Resend latest buffer after a switch to a new pad", FALSE,
128 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
129 g_object_class_install_property (gobject_class, PROP_PAD_NEGOTIATION_MODE,
130 g_param_spec_enum ("pad-negotiation-mode", "Pad negotiation mode",
131 "The mode to be used for pad negotiation",
132 GST_TYPE_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE,
133 DEFAULT_PAD_NEGOTIATION_MODE,
134 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
136 gst_element_class_set_static_metadata (gstelement_class, "Output selector",
137 "Generic", "1-to-N output stream selector",
138 "Stefan Kost <stefan.kost@nokia.com>");
139 gst_element_class_add_static_pad_template (gstelement_class,
140 &gst_output_selector_sink_factory);
141 gst_element_class_add_static_pad_template (gstelement_class,
142 &gst_output_selector_src_factory);
144 gstelement_class->request_new_pad =
145 GST_DEBUG_FUNCPTR (gst_output_selector_request_new_pad);
146 gstelement_class->release_pad =
147 GST_DEBUG_FUNCPTR (gst_output_selector_release_pad);
149 gstelement_class->change_state = gst_output_selector_change_state;
153 gst_output_selector_init (GstOutputSelector * sel)
156 gst_pad_new_from_static_template (&gst_output_selector_sink_factory,
158 gst_pad_set_chain_function (sel->sinkpad,
159 GST_DEBUG_FUNCPTR (gst_output_selector_chain));
160 gst_pad_set_event_function (sel->sinkpad,
161 GST_DEBUG_FUNCPTR (gst_output_selector_event));
162 gst_pad_set_query_function (sel->sinkpad,
163 GST_DEBUG_FUNCPTR (gst_output_selector_query));
165 gst_element_add_pad (GST_ELEMENT (sel), sel->sinkpad);
167 /* srcpad management */
168 sel->active_srcpad = NULL;
170 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
171 sel->pending_srcpad = NULL;
173 sel->resend_latest = FALSE;
174 sel->latest_buffer = NULL;
175 gst_output_selector_switch_pad_negotiation_mode (sel,
176 DEFAULT_PAD_NEGOTIATION_MODE);
180 gst_output_selector_reset (GstOutputSelector * osel)
182 GST_OBJECT_LOCK (osel);
183 if (osel->pending_srcpad != NULL) {
184 gst_object_unref (osel->pending_srcpad);
185 osel->pending_srcpad = NULL;
188 if (osel->latest_buffer != NULL) {
189 gst_buffer_unref (osel->latest_buffer);
190 osel->latest_buffer = NULL;
192 GST_OBJECT_UNLOCK (osel);
193 gst_segment_init (&osel->segment, GST_FORMAT_UNDEFINED);
197 gst_output_selector_dispose (GObject * object)
199 GstOutputSelector *osel = GST_OUTPUT_SELECTOR (object);
201 gst_output_selector_reset (osel);
203 G_OBJECT_CLASS (parent_class)->dispose (object);
207 gst_output_selector_set_property (GObject * object, guint prop_id,
208 const GValue * value, GParamSpec * pspec)
210 GstOutputSelector *sel = GST_OUTPUT_SELECTOR (object);
213 case PROP_ACTIVE_PAD:
217 next_pad = g_value_get_object (value);
219 GST_INFO_OBJECT (sel, "Activating pad %s:%s",
220 GST_DEBUG_PAD_NAME (next_pad));
222 GST_OBJECT_LOCK (object);
223 if (next_pad != sel->active_srcpad) {
224 /* switch to new srcpad in next chain run */
225 if (sel->pending_srcpad != NULL) {
226 GST_INFO ("replacing pending switch");
227 gst_object_unref (sel->pending_srcpad);
230 gst_object_ref (next_pad);
231 sel->pending_srcpad = next_pad;
233 GST_INFO ("pad already active");
234 if (sel->pending_srcpad != NULL) {
235 gst_object_unref (sel->pending_srcpad);
236 sel->pending_srcpad = NULL;
239 GST_OBJECT_UNLOCK (object);
242 case PROP_RESEND_LATEST:{
243 sel->resend_latest = g_value_get_boolean (value);
246 case PROP_PAD_NEGOTIATION_MODE:{
247 gst_output_selector_switch_pad_negotiation_mode (sel,
248 g_value_get_enum (value));
252 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
258 gst_output_selector_get_property (GObject * object, guint prop_id,
259 GValue * value, GParamSpec * pspec)
261 GstOutputSelector *sel = GST_OUTPUT_SELECTOR (object);
264 case PROP_ACTIVE_PAD:
265 GST_OBJECT_LOCK (object);
266 g_value_set_object (value,
267 sel->pending_srcpad ? sel->pending_srcpad : sel->active_srcpad);
268 GST_OBJECT_UNLOCK (object);
270 case PROP_RESEND_LATEST:{
271 GST_OBJECT_LOCK (object);
272 g_value_set_boolean (value, sel->resend_latest);
273 GST_OBJECT_UNLOCK (object);
276 case PROP_PAD_NEGOTIATION_MODE:
277 g_value_set_enum (value, sel->pad_negotiation_mode);
280 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
286 gst_output_selector_get_active (GstOutputSelector * sel)
288 GstPad *active = NULL;
290 GST_OBJECT_LOCK (sel);
291 if (sel->pending_srcpad)
292 active = gst_object_ref (sel->pending_srcpad);
293 else if (sel->active_srcpad)
294 active = gst_object_ref (sel->active_srcpad);
295 GST_OBJECT_UNLOCK (sel);
301 gst_output_selector_switch_pad_negotiation_mode (GstOutputSelector * sel,
304 sel->pad_negotiation_mode = mode;
308 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
310 GstPad *srcpad = GST_PAD_CAST (user_data);
312 gst_pad_push_event (srcpad, gst_event_ref (*event));
318 gst_output_selector_request_new_pad (GstElement * element,
319 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
323 GstOutputSelector *osel;
325 osel = GST_OUTPUT_SELECTOR (element);
327 GST_DEBUG_OBJECT (osel, "requesting pad");
329 GST_OBJECT_LOCK (osel);
330 padname = g_strdup_printf ("src_%u", osel->nb_srcpads++);
331 srcpad = gst_pad_new_from_template (templ, padname);
332 GST_OBJECT_UNLOCK (osel);
334 gst_pad_set_active (srcpad, TRUE);
336 /* Forward sticky events to the new srcpad */
337 gst_pad_sticky_events_foreach (osel->sinkpad, forward_sticky_events, srcpad);
339 gst_element_add_pad (GST_ELEMENT (osel), srcpad);
341 /* Set the first requested src pad as active by default */
342 GST_OBJECT_LOCK (osel);
343 if (osel->active_srcpad == NULL) {
344 osel->active_srcpad = srcpad;
345 GST_OBJECT_UNLOCK (osel);
346 g_object_notify (G_OBJECT (osel), "active-pad");
348 GST_OBJECT_UNLOCK (osel);
356 gst_output_selector_release_pad (GstElement * element, GstPad * pad)
358 GstOutputSelector *osel;
360 osel = GST_OUTPUT_SELECTOR (element);
362 GST_DEBUG_OBJECT (osel, "releasing pad");
364 /* Disable active pad if it's the to be removed pad */
365 GST_OBJECT_LOCK (osel);
366 if (osel->active_srcpad == pad) {
367 osel->active_srcpad = NULL;
368 GST_OBJECT_UNLOCK (osel);
369 g_object_notify (G_OBJECT (osel), "active-pad");
371 GST_OBJECT_UNLOCK (osel);
374 gst_pad_set_active (pad, FALSE);
376 gst_element_remove_pad (GST_ELEMENT_CAST (osel), pad);
380 gst_output_selector_switch (GstOutputSelector * osel)
382 gboolean res = FALSE;
384 GstSegment *seg = NULL;
385 GstPad *active_srcpad;
388 GST_OBJECT_LOCK (osel);
389 GST_INFO_OBJECT (osel, "switching to pad %" GST_PTR_FORMAT,
390 osel->pending_srcpad);
391 if (!osel->pending_srcpad) {
392 GST_OBJECT_UNLOCK (osel);
396 if (gst_pad_is_linked (osel->pending_srcpad)) {
397 osel->active_srcpad = osel->pending_srcpad;
400 gst_object_unref (osel->pending_srcpad);
401 osel->pending_srcpad = NULL;
402 active_srcpad = res ? gst_object_ref (osel->active_srcpad) : NULL;
403 GST_OBJECT_UNLOCK (osel);
405 /* Send SEGMENT event and latest buffer if switching succeeded
406 * and we already have a valid segment configured */
408 GstBuffer *latest_buffer;
410 g_object_notify (G_OBJECT (osel), "active-pad");
412 GST_OBJECT_LOCK (osel);
414 osel->latest_buffer ? gst_buffer_ref (osel->latest_buffer) : NULL;
415 GST_OBJECT_UNLOCK (osel);
417 gst_pad_sticky_events_foreach (osel->sinkpad, forward_sticky_events,
420 /* update segment if required */
421 if (osel->segment.format != GST_FORMAT_UNDEFINED) {
422 /* Send SEGMENT to the pad we are going to switch to */
423 seg = &osel->segment;
424 /* If resending then mark segment start and position accordingly */
425 if (osel->resend_latest && latest_buffer &&
426 GST_BUFFER_TIMESTAMP_IS_VALID (latest_buffer)) {
427 seg->position = GST_BUFFER_TIMESTAMP (latest_buffer);
430 ev = gst_event_new_segment (seg);
432 if (!gst_pad_push_event (active_srcpad, ev)) {
433 GST_WARNING_OBJECT (osel,
434 "newsegment handling failed in %" GST_PTR_FORMAT, active_srcpad);
438 /* Resend latest buffer to newly switched pad */
439 if (osel->resend_latest && latest_buffer) {
440 GST_INFO ("resending latest buffer");
441 gst_pad_push (active_srcpad, latest_buffer);
442 } else if (latest_buffer) {
443 gst_buffer_unref (latest_buffer);
446 gst_object_unref (active_srcpad);
448 GST_WARNING_OBJECT (osel, "switch failed, pad not linked");
455 gst_output_selector_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
458 GstOutputSelector *osel;
459 GstClockTime position, duration;
460 GstPad *active_srcpad;
462 osel = GST_OUTPUT_SELECTOR (parent);
465 * The _switch function might push a buffer if 'resend-latest' is true.
467 * Elements/Applications (e.g. camerabin) might use pad probes to
468 * switch output-selector's active pad. If we simply switch and don't
469 * recheck any pending pad switch the following codepath could end
470 * up pushing a buffer on a non-active pad. This is bad.
472 * So we always should check the pending_srcpad before going further down
473 * the chain and pushing the new buffer
475 while (osel->pending_srcpad) {
477 gst_output_selector_switch (osel);
480 active_srcpad = gst_output_selector_get_active (osel);
481 if (!active_srcpad) {
482 GST_DEBUG_OBJECT (osel, "No active srcpad");
483 gst_buffer_unref (buf);
487 GST_OBJECT_LOCK (osel);
488 if (osel->latest_buffer) {
489 gst_buffer_unref (osel->latest_buffer);
490 osel->latest_buffer = NULL;
493 if (osel->resend_latest) {
494 /* Keep reference to latest buffer to resend it after switch */
495 osel->latest_buffer = gst_buffer_ref (buf);
497 GST_OBJECT_UNLOCK (osel);
499 /* Keep track of last stop and use it in SEGMENT start after
500 switching to a new src pad */
501 position = GST_BUFFER_TIMESTAMP (buf);
502 if (GST_CLOCK_TIME_IS_VALID (position)) {
503 duration = GST_BUFFER_DURATION (buf);
504 if (GST_CLOCK_TIME_IS_VALID (duration)) {
505 position += duration;
507 GST_LOG_OBJECT (osel, "setting last stop %" GST_TIME_FORMAT,
508 GST_TIME_ARGS (position));
509 osel->segment.position = position;
512 GST_LOG_OBJECT (osel, "pushing buffer to %" GST_PTR_FORMAT, active_srcpad);
513 res = gst_pad_push (active_srcpad, buf);
515 gst_object_unref (active_srcpad);
520 static GstStateChangeReturn
521 gst_output_selector_change_state (GstElement * element,
522 GstStateChange transition)
524 GstOutputSelector *sel;
525 GstStateChangeReturn result;
527 sel = GST_OUTPUT_SELECTOR (element);
529 switch (transition) {
530 case GST_STATE_CHANGE_READY_TO_PAUSED:
536 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
538 switch (transition) {
539 case GST_STATE_CHANGE_PAUSED_TO_READY:
540 gst_output_selector_reset (sel);
550 gst_output_selector_forward_event (GstOutputSelector * sel, GstEvent * event)
555 switch (sel->pad_negotiation_mode) {
556 case GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL:
557 /* Send to all src pads */
558 res = gst_pad_event_default (sel->sinkpad, GST_OBJECT_CAST (sel), event);
560 case GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE:
561 gst_event_unref (event);
564 active = gst_output_selector_get_active (sel);
566 res = gst_pad_push_event (active, event);
567 gst_object_unref (active);
569 gst_event_unref (event);
578 gst_output_selector_event (GstPad * pad, GstObject * parent, GstEvent * event)
581 GstOutputSelector *sel;
582 GstPad *active = NULL;
584 sel = GST_OUTPUT_SELECTOR (parent);
586 switch (GST_EVENT_TYPE (event)) {
589 res = gst_output_selector_forward_event (sel, event);
592 case GST_EVENT_SEGMENT:
594 gst_event_copy_segment (event, &sel->segment);
595 GST_DEBUG_OBJECT (sel, "configured SEGMENT %" GST_SEGMENT_FORMAT,
601 active = gst_output_selector_get_active (sel);
603 res = gst_pad_push_event (active, event);
604 gst_object_unref (active);
606 gst_event_unref (event);
616 gst_output_selector_query (GstPad * pad, GstObject * parent, GstQuery * query)
619 GstOutputSelector *sel;
620 GstPad *active = NULL;
622 sel = GST_OUTPUT_SELECTOR (parent);
624 switch (GST_QUERY_TYPE (query)) {
627 switch (sel->pad_negotiation_mode) {
628 case GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL:
629 /* Send caps to all src pads */
630 res = gst_pad_proxy_query_caps (pad, query);
632 case GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE:
636 active = gst_output_selector_get_active (sel);
638 res = gst_pad_peer_query (active, query);
639 gst_object_unref (active);
647 case GST_QUERY_DRAIN:
648 if (sel->latest_buffer) {
649 gst_buffer_unref (sel->latest_buffer);
650 sel->latest_buffer = NULL;
654 res = gst_pad_query_default (pad, parent, query);