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 * @see_also: #GstOutputSelector, #GstInputSelector
24 * Direct input stream to one out of N output pads.
33 #include "gstoutputselector.h"
35 GST_DEBUG_CATEGORY_STATIC (output_selector_debug);
36 #define GST_CAT_DEFAULT output_selector_debug
38 static GstStaticPadTemplate gst_output_selector_sink_factory =
39 GST_STATIC_PAD_TEMPLATE ("sink",
44 static GstStaticPadTemplate gst_output_selector_src_factory =
45 GST_STATIC_PAD_TEMPLATE ("src_%u",
50 #define GST_TYPE_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE (gst_output_selector_pad_negotiation_mode_get_type())
52 gst_output_selector_pad_negotiation_mode_get_type (void)
54 static GType pad_negotiation_mode_type = 0;
55 static const GEnumValue pad_negotiation_modes[] = {
56 {GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE, "None", "none"},
57 {GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL, "All", "all"},
58 {GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ACTIVE, "Active", "active"},
62 if (!pad_negotiation_mode_type) {
63 pad_negotiation_mode_type =
64 g_enum_register_static ("GstOutputSelectorPadNegotiationMode",
65 pad_negotiation_modes);
67 return pad_negotiation_mode_type;
76 PROP_PAD_NEGOTIATION_MODE
79 #define DEFAULT_PAD_NEGOTIATION_MODE GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL
82 GST_DEBUG_CATEGORY_INIT (output_selector_debug, \
83 "output-selector", 0, "Output stream selector");
84 #define gst_output_selector_parent_class parent_class
85 G_DEFINE_TYPE_WITH_CODE (GstOutputSelector, gst_output_selector,
86 GST_TYPE_ELEMENT, _do_init);
88 static void gst_output_selector_dispose (GObject * object);
89 static void gst_output_selector_set_property (GObject * object,
90 guint prop_id, const GValue * value, GParamSpec * pspec);
91 static void gst_output_selector_get_property (GObject * object,
92 guint prop_id, GValue * value, GParamSpec * pspec);
93 static GstPad *gst_output_selector_request_new_pad (GstElement * element,
94 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps);
95 static void gst_output_selector_release_pad (GstElement * element,
97 static GstFlowReturn gst_output_selector_chain (GstPad * pad,
98 GstObject * parent, GstBuffer * buf);
99 static GstStateChangeReturn gst_output_selector_change_state (GstElement *
100 element, GstStateChange transition);
101 static gboolean gst_output_selector_event (GstPad * pad, GstObject * parent,
103 static gboolean gst_output_selector_query (GstPad * pad, GstObject * parent,
105 static void gst_output_selector_switch_pad_negotiation_mode (GstOutputSelector *
109 gst_output_selector_class_init (GstOutputSelectorClass * klass)
111 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
112 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
114 gobject_class->dispose = gst_output_selector_dispose;
116 gobject_class->set_property = gst_output_selector_set_property;
117 gobject_class->get_property = gst_output_selector_get_property;
119 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
120 g_param_spec_object ("active-pad", "Active pad",
121 "Currently active src pad", GST_TYPE_PAD,
122 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
123 G_PARAM_STATIC_STRINGS));
124 g_object_class_install_property (gobject_class, PROP_RESEND_LATEST,
125 g_param_spec_boolean ("resend-latest", "Resend latest buffer",
126 "Resend latest buffer after a switch to a new pad", FALSE,
127 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
128 g_object_class_install_property (gobject_class, PROP_PAD_NEGOTIATION_MODE,
129 g_param_spec_enum ("pad-negotiation-mode", "Pad negotiation mode",
130 "The mode to be used for pad negotiation",
131 GST_TYPE_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE,
132 DEFAULT_PAD_NEGOTIATION_MODE,
133 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
135 gst_element_class_set_static_metadata (gstelement_class, "Output selector",
136 "Generic", "1-to-N output stream selector",
137 "Stefan Kost <stefan.kost@nokia.com>");
138 gst_element_class_add_pad_template (gstelement_class,
139 gst_static_pad_template_get (&gst_output_selector_sink_factory));
140 gst_element_class_add_pad_template (gstelement_class,
141 gst_static_pad_template_get (&gst_output_selector_src_factory));
143 gstelement_class->request_new_pad =
144 GST_DEBUG_FUNCPTR (gst_output_selector_request_new_pad);
145 gstelement_class->release_pad =
146 GST_DEBUG_FUNCPTR (gst_output_selector_release_pad);
148 gstelement_class->change_state = gst_output_selector_change_state;
152 gst_output_selector_init (GstOutputSelector * sel)
155 gst_pad_new_from_static_template (&gst_output_selector_sink_factory,
157 gst_pad_set_chain_function (sel->sinkpad,
158 GST_DEBUG_FUNCPTR (gst_output_selector_chain));
159 gst_pad_set_event_function (sel->sinkpad,
160 GST_DEBUG_FUNCPTR (gst_output_selector_event));
161 gst_pad_set_query_function (sel->sinkpad,
162 GST_DEBUG_FUNCPTR (gst_output_selector_query));
164 gst_element_add_pad (GST_ELEMENT (sel), sel->sinkpad);
166 /* srcpad management */
167 sel->active_srcpad = NULL;
169 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
170 sel->pending_srcpad = NULL;
172 sel->resend_latest = FALSE;
173 sel->latest_buffer = NULL;
174 gst_output_selector_switch_pad_negotiation_mode (sel,
175 DEFAULT_PAD_NEGOTIATION_MODE);
179 gst_output_selector_reset (GstOutputSelector * osel)
181 if (osel->pending_srcpad != NULL) {
182 gst_object_unref (osel->pending_srcpad);
183 osel->pending_srcpad = NULL;
185 if (osel->latest_buffer != NULL) {
186 gst_buffer_unref (osel->latest_buffer);
187 osel->latest_buffer = NULL;
189 gst_segment_init (&osel->segment, GST_FORMAT_UNDEFINED);
193 gst_output_selector_dispose (GObject * object)
195 GstOutputSelector *osel = GST_OUTPUT_SELECTOR (object);
197 gst_output_selector_reset (osel);
199 G_OBJECT_CLASS (parent_class)->dispose (object);
203 gst_output_selector_set_property (GObject * object, guint prop_id,
204 const GValue * value, GParamSpec * pspec)
206 GstOutputSelector *sel = GST_OUTPUT_SELECTOR (object);
209 case PROP_ACTIVE_PAD:
213 next_pad = g_value_get_object (value);
215 GST_INFO_OBJECT (sel, "Activating pad %s:%s",
216 GST_DEBUG_PAD_NAME (next_pad));
218 GST_OBJECT_LOCK (object);
219 if (next_pad != sel->active_srcpad) {
220 /* switch to new srcpad in next chain run */
221 if (sel->pending_srcpad != NULL) {
222 GST_INFO ("replacing pending switch");
223 gst_object_unref (sel->pending_srcpad);
226 gst_object_ref (next_pad);
227 sel->pending_srcpad = next_pad;
229 GST_INFO ("pad already active");
230 if (sel->pending_srcpad != NULL) {
231 gst_object_unref (sel->pending_srcpad);
232 sel->pending_srcpad = NULL;
235 GST_OBJECT_UNLOCK (object);
238 case PROP_RESEND_LATEST:{
239 sel->resend_latest = g_value_get_boolean (value);
242 case PROP_PAD_NEGOTIATION_MODE:{
243 gst_output_selector_switch_pad_negotiation_mode (sel,
244 g_value_get_enum (value));
248 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254 gst_output_selector_get_property (GObject * object, guint prop_id,
255 GValue * value, GParamSpec * pspec)
257 GstOutputSelector *sel = GST_OUTPUT_SELECTOR (object);
260 case PROP_ACTIVE_PAD:
261 GST_OBJECT_LOCK (object);
262 g_value_set_object (value,
263 sel->pending_srcpad ? sel->pending_srcpad : sel->active_srcpad);
264 GST_OBJECT_UNLOCK (object);
266 case PROP_RESEND_LATEST:{
267 GST_OBJECT_LOCK (object);
268 g_value_set_boolean (value, sel->resend_latest);
269 GST_OBJECT_UNLOCK (object);
272 case PROP_PAD_NEGOTIATION_MODE:
273 g_value_set_enum (value, sel->pad_negotiation_mode);
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
282 gst_output_selector_get_active (GstOutputSelector * sel)
284 GstPad *active = NULL;
286 GST_OBJECT_LOCK (sel);
287 if (sel->pending_srcpad)
288 active = gst_object_ref (sel->pending_srcpad);
289 else if (sel->active_srcpad)
290 active = gst_object_ref (sel->active_srcpad);
291 GST_OBJECT_UNLOCK (sel);
297 gst_output_selector_switch_pad_negotiation_mode (GstOutputSelector * sel,
300 sel->pad_negotiation_mode = mode;
304 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
306 GstPad *srcpad = GST_PAD_CAST (user_data);
308 gst_pad_push_event (srcpad, gst_event_ref (*event));
314 gst_output_selector_request_new_pad (GstElement * element,
315 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
319 GstOutputSelector *osel;
321 osel = GST_OUTPUT_SELECTOR (element);
323 GST_DEBUG_OBJECT (osel, "requesting pad");
325 GST_OBJECT_LOCK (osel);
326 padname = g_strdup_printf ("src_%u", osel->nb_srcpads++);
327 srcpad = gst_pad_new_from_template (templ, padname);
328 GST_OBJECT_UNLOCK (osel);
330 gst_pad_set_active (srcpad, TRUE);
332 /* Forward sticky events to the new srcpad */
333 gst_pad_sticky_events_foreach (osel->sinkpad, forward_sticky_events, srcpad);
335 gst_element_add_pad (GST_ELEMENT (osel), srcpad);
337 /* Set the first requested src pad as active by default */
338 if (osel->active_srcpad == NULL) {
339 osel->active_srcpad = srcpad;
347 gst_output_selector_release_pad (GstElement * element, GstPad * pad)
349 GstOutputSelector *osel;
351 osel = GST_OUTPUT_SELECTOR (element);
353 GST_DEBUG_OBJECT (osel, "releasing pad");
355 gst_pad_set_active (pad, FALSE);
357 gst_element_remove_pad (GST_ELEMENT_CAST (osel), pad);
361 gst_output_selector_switch (GstOutputSelector * osel)
363 gboolean res = FALSE;
365 GstSegment *seg = NULL;
368 GST_OBJECT_LOCK (GST_OBJECT (osel));
369 GST_INFO_OBJECT (osel, "switching to pad %" GST_PTR_FORMAT,
370 osel->pending_srcpad);
371 if (gst_pad_is_linked (osel->pending_srcpad)) {
372 osel->active_srcpad = osel->pending_srcpad;
375 gst_object_unref (osel->pending_srcpad);
376 osel->pending_srcpad = NULL;
377 GST_OBJECT_UNLOCK (GST_OBJECT (osel));
379 /* Send SEGMENT event and latest buffer if switching succeeded
380 * and we already have a valid segment configured */
382 gst_pad_sticky_events_foreach (osel->sinkpad, forward_sticky_events,
383 osel->active_srcpad);
385 /* update segment if required */
386 if (osel->segment.format != GST_FORMAT_UNDEFINED) {
387 /* Send SEGMENT to the pad we are going to switch to */
388 seg = &osel->segment;
389 /* If resending then mark segment start and position accordingly */
390 if (osel->resend_latest && osel->latest_buffer &&
391 GST_BUFFER_TIMESTAMP_IS_VALID (osel->latest_buffer)) {
392 seg->position = GST_BUFFER_TIMESTAMP (osel->latest_buffer);
395 ev = gst_event_new_segment (seg);
397 if (!gst_pad_push_event (osel->active_srcpad, ev)) {
398 GST_WARNING_OBJECT (osel,
399 "newsegment handling failed in %" GST_PTR_FORMAT,
400 osel->active_srcpad);
404 /* Resend latest buffer to newly switched pad */
405 if (osel->resend_latest && osel->latest_buffer) {
406 GST_INFO ("resending latest buffer");
407 gst_pad_push (osel->active_srcpad, gst_buffer_ref (osel->latest_buffer));
410 GST_WARNING_OBJECT (osel, "switch failed, pad not linked");
417 gst_output_selector_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
420 GstOutputSelector *osel;
421 GstClockTime position, duration;
423 osel = GST_OUTPUT_SELECTOR (parent);
426 * The _switch function might push a buffer if 'resend-latest' is true.
428 * Elements/Applications (e.g. camerabin) might use pad probes to
429 * switch output-selector's active pad. If we simply switch and don't
430 * recheck any pending pad switch the following codepath could end
431 * up pushing a buffer on a non-active pad. This is bad.
433 * So we always should check the pending_srcpad before going further down
434 * the chain and pushing the new buffer
436 while (osel->pending_srcpad) {
438 gst_output_selector_switch (osel);
441 if (osel->latest_buffer) {
442 gst_buffer_unref (osel->latest_buffer);
443 osel->latest_buffer = NULL;
446 if (osel->resend_latest) {
447 /* Keep reference to latest buffer to resend it after switch */
448 osel->latest_buffer = gst_buffer_ref (buf);
451 /* Keep track of last stop and use it in SEGMENT start after
452 switching to a new src pad */
453 position = GST_BUFFER_TIMESTAMP (buf);
454 if (GST_CLOCK_TIME_IS_VALID (position)) {
455 duration = GST_BUFFER_DURATION (buf);
456 if (GST_CLOCK_TIME_IS_VALID (duration)) {
457 position += duration;
459 GST_LOG_OBJECT (osel, "setting last stop %" GST_TIME_FORMAT,
460 GST_TIME_ARGS (position));
461 osel->segment.position = position;
464 GST_LOG_OBJECT (osel, "pushing buffer to %" GST_PTR_FORMAT,
465 osel->active_srcpad);
466 res = gst_pad_push (osel->active_srcpad, buf);
471 static GstStateChangeReturn
472 gst_output_selector_change_state (GstElement * element,
473 GstStateChange transition)
475 GstOutputSelector *sel;
476 GstStateChangeReturn result;
478 sel = GST_OUTPUT_SELECTOR (element);
480 switch (transition) {
481 case GST_STATE_CHANGE_READY_TO_PAUSED:
487 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
489 switch (transition) {
490 case GST_STATE_CHANGE_PAUSED_TO_READY:
491 gst_output_selector_reset (sel);
501 gst_output_selector_forward_event (GstOutputSelector * sel, GstEvent * event)
506 switch (sel->pad_negotiation_mode) {
507 case GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL:
508 /* Send to all src pads */
509 res = gst_pad_event_default (sel->sinkpad, GST_OBJECT_CAST (sel), event);
511 case GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE:
512 gst_event_unref (event);
515 active = gst_output_selector_get_active (sel);
517 res = gst_pad_push_event (active, event);
518 gst_object_unref (active);
520 gst_event_unref (event);
529 gst_output_selector_event (GstPad * pad, GstObject * parent, GstEvent * event)
532 GstOutputSelector *sel;
533 GstPad *active = NULL;
535 sel = GST_OUTPUT_SELECTOR (parent);
537 switch (GST_EVENT_TYPE (event)) {
540 res = gst_output_selector_forward_event (sel, event);
543 case GST_EVENT_SEGMENT:
545 gst_event_copy_segment (event, &sel->segment);
546 GST_DEBUG_OBJECT (sel, "configured SEGMENT %" GST_SEGMENT_FORMAT,
552 active = gst_output_selector_get_active (sel);
554 res = gst_pad_push_event (active, event);
555 gst_object_unref (active);
557 gst_event_unref (event);
567 gst_output_selector_query (GstPad * pad, GstObject * parent, GstQuery * query)
570 GstOutputSelector *sel;
571 GstPad *active = NULL;
573 sel = GST_OUTPUT_SELECTOR (parent);
575 switch (GST_QUERY_TYPE (query)) {
578 switch (sel->pad_negotiation_mode) {
579 case GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL:
580 /* Send caps to all src pads */
581 res = gst_pad_proxy_query_caps (pad, query);
583 case GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE:
587 active = gst_output_selector_get_active (sel);
589 res = gst_pad_peer_query (active, query);
590 gst_object_unref (active);
598 case GST_QUERY_DRAIN:
599 if (sel->latest_buffer) {
600 gst_buffer_unref (sel->latest_buffer);
601 sel->latest_buffer = NULL;
605 res = gst_pad_query_default (pad, parent, query);