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
90 GST_DEBUG_CATEGORY_INIT (output_selector_debug, \
91 "output-selector", 0, "Output stream selector");
92 #define gst_output_selector_parent_class parent_class
93 G_DEFINE_TYPE_WITH_CODE (GstOutputSelector, gst_output_selector,
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, const GstCaps * caps);
103 static void gst_output_selector_release_pad (GstElement * element,
105 static GstFlowReturn gst_output_selector_chain (GstPad * pad, GstBuffer * buf);
106 static GstStateChangeReturn gst_output_selector_change_state (GstElement *
107 element, GstStateChange transition);
108 static gboolean gst_output_selector_handle_sink_event (GstPad * pad,
110 static void gst_output_selector_switch_pad_negotiation_mode (GstOutputSelector *
114 gst_output_selector_class_init (GstOutputSelectorClass * klass)
116 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
117 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
119 gobject_class->dispose = gst_output_selector_dispose;
121 gobject_class->set_property = gst_output_selector_set_property;
122 gobject_class->get_property = gst_output_selector_get_property;
124 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
125 g_param_spec_object ("active-pad", "Active pad",
126 "Currently active src pad", GST_TYPE_PAD,
127 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
128 g_object_class_install_property (gobject_class, PROP_RESEND_LATEST,
129 g_param_spec_boolean ("resend-latest", "Resend latest buffer",
130 "Resend latest buffer after a switch to a new pad", FALSE,
131 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
132 g_object_class_install_property (gobject_class, PROP_PAD_NEGOTIATION_MODE,
133 g_param_spec_enum ("pad-negotiation-mode", "Pad negotiation mode",
134 "The mode to be used for pad negotiation",
135 GST_TYPE_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE,
136 DEFAULT_PAD_NEGOTIATION_MODE,
137 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
139 gst_element_class_set_details_simple (gstelement_class, "Output selector",
140 "Generic", "1-to-N output stream selector",
141 "Stefan Kost <stefan.kost@nokia.com>");
142 gst_element_class_add_pad_template (gstelement_class,
143 gst_static_pad_template_get (&gst_output_selector_sink_factory));
144 gst_element_class_add_pad_template (gstelement_class,
145 gst_static_pad_template_get (&gst_output_selector_src_factory));
147 gstelement_class->request_new_pad =
148 GST_DEBUG_FUNCPTR (gst_output_selector_request_new_pad);
149 gstelement_class->release_pad =
150 GST_DEBUG_FUNCPTR (gst_output_selector_release_pad);
152 gstelement_class->change_state = gst_output_selector_change_state;
156 gst_output_selector_init (GstOutputSelector * sel)
159 gst_pad_new_from_static_template (&gst_output_selector_sink_factory,
161 gst_pad_set_chain_function (sel->sinkpad,
162 GST_DEBUG_FUNCPTR (gst_output_selector_chain));
163 gst_pad_set_event_function (sel->sinkpad,
164 GST_DEBUG_FUNCPTR (gst_output_selector_handle_sink_event));
166 gst_element_add_pad (GST_ELEMENT (sel), sel->sinkpad);
168 /* srcpad management */
169 sel->active_srcpad = NULL;
171 gst_segment_init (&sel->segment, GST_FORMAT_TIME);
172 sel->pending_srcpad = NULL;
174 sel->resend_latest = FALSE;
175 sel->latest_buffer = NULL;
176 gst_output_selector_switch_pad_negotiation_mode (sel,
177 DEFAULT_PAD_NEGOTIATION_MODE);
181 gst_output_selector_reset (GstOutputSelector * osel)
183 if (osel->pending_srcpad != NULL) {
184 gst_object_unref (osel->pending_srcpad);
185 osel->pending_srcpad = NULL;
187 if (osel->latest_buffer != NULL) {
188 gst_buffer_unref (osel->latest_buffer);
189 osel->latest_buffer = NULL;
191 gst_segment_init (&osel->segment, GST_FORMAT_UNDEFINED);
195 gst_output_selector_dispose (GObject * object)
197 GstOutputSelector *osel = GST_OUTPUT_SELECTOR (object);
199 gst_output_selector_reset (osel);
201 G_OBJECT_CLASS (parent_class)->dispose (object);
205 gst_output_selector_set_property (GObject * object, guint prop_id,
206 const GValue * value, GParamSpec * pspec)
208 GstOutputSelector *sel = GST_OUTPUT_SELECTOR (object);
211 case PROP_ACTIVE_PAD:
215 next_pad = g_value_get_object (value);
217 GST_INFO_OBJECT (sel, "Activating pad %s:%s",
218 GST_DEBUG_PAD_NAME (next_pad));
220 GST_OBJECT_LOCK (object);
221 if (next_pad != sel->active_srcpad) {
222 /* switch to new srcpad in next chain run */
223 if (sel->pending_srcpad != NULL) {
224 GST_INFO ("replacing pending switch");
225 gst_object_unref (sel->pending_srcpad);
228 gst_object_ref (next_pad);
229 sel->pending_srcpad = next_pad;
231 GST_INFO ("pad already active");
232 if (sel->pending_srcpad != NULL) {
233 gst_object_unref (sel->pending_srcpad);
234 sel->pending_srcpad = NULL;
237 GST_OBJECT_UNLOCK (object);
240 case PROP_RESEND_LATEST:{
241 sel->resend_latest = g_value_get_boolean (value);
244 case PROP_PAD_NEGOTIATION_MODE:{
245 gst_output_selector_switch_pad_negotiation_mode (sel,
246 g_value_get_enum (value));
250 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256 gst_output_selector_get_property (GObject * object, guint prop_id,
257 GValue * value, GParamSpec * pspec)
259 GstOutputSelector *sel = GST_OUTPUT_SELECTOR (object);
262 case PROP_ACTIVE_PAD:
263 GST_OBJECT_LOCK (object);
264 g_value_set_object (value,
265 sel->pending_srcpad ? sel->pending_srcpad : sel->active_srcpad);
266 GST_OBJECT_UNLOCK (object);
268 case PROP_RESEND_LATEST:{
269 GST_OBJECT_LOCK (object);
270 g_value_set_boolean (value, sel->resend_latest);
271 GST_OBJECT_UNLOCK (object);
274 case PROP_PAD_NEGOTIATION_MODE:
275 g_value_set_enum (value, sel->pad_negotiation_mode);
278 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284 gst_output_selector_sink_getcaps (GstPad * pad)
286 GstOutputSelector *sel = GST_OUTPUT_SELECTOR (gst_pad_get_parent (pad));
287 GstPad *active = NULL;
288 GstCaps *caps = NULL;
290 if (G_UNLIKELY (sel == NULL))
291 return gst_caps_new_any ();
293 GST_OBJECT_LOCK (sel);
294 if (sel->pending_srcpad)
295 active = gst_object_ref (sel->pending_srcpad);
296 else if (sel->active_srcpad)
297 active = gst_object_ref (sel->active_srcpad);
298 GST_OBJECT_UNLOCK (sel);
301 caps = gst_pad_peer_get_caps_reffed (active);
302 gst_object_unref (active);
305 caps = gst_caps_new_any ();
308 gst_object_unref (sel);
313 gst_output_selector_sink_setcaps (GstPad * pad, GstCaps * caps)
315 GstOutputSelector *sel = GST_OUTPUT_SELECTOR (GST_PAD_PARENT (pad));
316 GstPad *active = NULL;
319 GST_OBJECT_LOCK (sel);
320 if (sel->pending_srcpad)
321 active = gst_object_ref (sel->pending_srcpad);
322 else if (sel->active_srcpad)
323 active = gst_object_ref (sel->active_srcpad);
324 GST_OBJECT_UNLOCK (sel);
327 ret = gst_pad_set_caps (active, caps);
328 gst_object_unref (active);
334 gst_output_selector_switch_pad_negotiation_mode (GstOutputSelector * sel,
337 sel->pad_negotiation_mode = mode;
338 if (mode == GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL) {
339 gst_pad_set_getcaps_function (sel->sinkpad, gst_pad_proxy_getcaps);
340 gst_pad_set_setcaps_function (sel->sinkpad, gst_pad_proxy_setcaps);
341 } else if (mode == GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE) {
342 gst_pad_set_getcaps_function (sel->sinkpad, NULL);
343 gst_pad_set_setcaps_function (sel->sinkpad, NULL);
344 } else { /* active */
345 gst_pad_set_getcaps_function (sel->sinkpad,
346 gst_output_selector_sink_getcaps);
347 gst_pad_set_setcaps_function (sel->sinkpad,
348 gst_output_selector_sink_setcaps);
353 gst_output_selector_request_new_pad (GstElement * element,
354 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
358 GstOutputSelector *osel;
360 osel = GST_OUTPUT_SELECTOR (element);
362 GST_DEBUG_OBJECT (osel, "requesting pad");
364 GST_OBJECT_LOCK (osel);
365 padname = g_strdup_printf ("src%d", osel->nb_srcpads++);
366 srcpad = gst_pad_new_from_template (templ, padname);
367 GST_OBJECT_UNLOCK (osel);
369 gst_pad_set_active (srcpad, TRUE);
370 gst_element_add_pad (GST_ELEMENT (osel), srcpad);
372 /* Set the first requested src pad as active by default */
373 if (osel->active_srcpad == NULL) {
374 osel->active_srcpad = srcpad;
382 gst_output_selector_release_pad (GstElement * element, GstPad * pad)
384 GstOutputSelector *osel;
386 osel = GST_OUTPUT_SELECTOR (element);
388 GST_DEBUG_OBJECT (osel, "releasing pad");
390 gst_pad_set_active (pad, FALSE);
392 gst_element_remove_pad (GST_ELEMENT_CAST (osel), pad);
396 gst_output_selector_switch (GstOutputSelector * osel)
398 gboolean res = FALSE;
400 GstSegment *seg = NULL;
401 gint64 start = 0, position = 0;
404 GST_OBJECT_LOCK (GST_OBJECT (osel));
405 GST_INFO ("switching to pad %" GST_PTR_FORMAT, osel->pending_srcpad);
406 if (gst_pad_is_linked (osel->pending_srcpad)) {
407 osel->active_srcpad = osel->pending_srcpad;
410 gst_object_unref (osel->pending_srcpad);
411 osel->pending_srcpad = NULL;
412 GST_OBJECT_UNLOCK (GST_OBJECT (osel));
414 /* Send NEWSEGMENT event and latest buffer if switching succeeded */
416 /* Send NEWSEGMENT to the pad we are going to switch to */
417 seg = &osel->segment;
418 /* If resending then mark newsegment start and position accordingly */
419 if (osel->resend_latest && osel->latest_buffer &&
420 GST_BUFFER_TIMESTAMP_IS_VALID (osel->latest_buffer)) {
421 start = position = GST_BUFFER_TIMESTAMP (osel->latest_buffer);
423 start = position = seg->last_stop;
425 ev = gst_event_new_new_segment (TRUE, seg->rate, seg->applied_rate,
426 seg->format, start, seg->stop, position);
427 if (!gst_pad_push_event (osel->active_srcpad, ev)) {
428 GST_WARNING_OBJECT (osel,
429 "newsegment handling failed in %" GST_PTR_FORMAT,
430 osel->active_srcpad);
433 /* Resend latest buffer to newly switched pad */
434 if (osel->resend_latest && osel->latest_buffer) {
435 GST_INFO ("resending latest buffer");
436 gst_pad_push (osel->active_srcpad, gst_buffer_ref (osel->latest_buffer));
439 GST_WARNING_OBJECT (osel, "switch failed, pad not linked");
446 gst_output_selector_chain (GstPad * pad, GstBuffer * buf)
449 GstOutputSelector *osel;
450 GstClockTime last_stop, duration;
452 osel = GST_OUTPUT_SELECTOR (gst_pad_get_parent (pad));
455 * The _switch function might push a buffer if 'resend-latest' is true.
457 * Elements/Applications (e.g. camerabin) might use pad probes to
458 * switch output-selector's active pad. If we simply switch and don't
459 * recheck any pending pad switch the following codepath could end
460 * up pushing a buffer on a non-active pad. This is bad.
462 * So we always should check the pending_srcpad before going further down
463 * the chain and pushing the new buffer
465 while (osel->pending_srcpad) {
467 gst_output_selector_switch (osel);
470 if (osel->latest_buffer) {
471 gst_buffer_unref (osel->latest_buffer);
472 osel->latest_buffer = NULL;
475 if (osel->resend_latest) {
476 /* Keep reference to latest buffer to resend it after switch */
477 osel->latest_buffer = gst_buffer_ref (buf);
480 /* Keep track of last stop and use it in NEWSEGMENT start after
481 switching to a new src pad */
482 last_stop = GST_BUFFER_TIMESTAMP (buf);
483 if (GST_CLOCK_TIME_IS_VALID (last_stop)) {
484 duration = GST_BUFFER_DURATION (buf);
485 if (GST_CLOCK_TIME_IS_VALID (duration)) {
486 last_stop += duration;
488 GST_LOG_OBJECT (osel, "setting last stop %" GST_TIME_FORMAT,
489 GST_TIME_ARGS (last_stop));
490 gst_segment_set_last_stop (&osel->segment, osel->segment.format, last_stop);
493 GST_LOG_OBJECT (osel, "pushing buffer to %" GST_PTR_FORMAT,
494 osel->active_srcpad);
495 res = gst_pad_push (osel->active_srcpad, buf);
496 gst_object_unref (osel);
501 static GstStateChangeReturn
502 gst_output_selector_change_state (GstElement * element,
503 GstStateChange transition)
505 GstOutputSelector *sel;
506 GstStateChangeReturn result;
508 sel = GST_OUTPUT_SELECTOR (element);
510 switch (transition) {
511 case GST_STATE_CHANGE_READY_TO_PAUSED:
517 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
519 switch (transition) {
520 case GST_STATE_CHANGE_PAUSED_TO_READY:
521 gst_output_selector_reset (sel);
531 gst_output_selector_handle_sink_event (GstPad * pad, GstEvent * event)
534 GstOutputSelector *sel;
535 GstPad *output_pad = NULL;
537 sel = GST_OUTPUT_SELECTOR (gst_pad_get_parent (pad));
538 if (G_UNLIKELY (sel == NULL)) {
539 gst_event_unref (event);
543 switch (GST_EVENT_TYPE (event)) {
544 case GST_EVENT_NEWSEGMENT:
549 gint64 start, stop, time;
551 gst_event_parse_new_segment (event, &update, &rate, &arate, &format,
552 &start, &stop, &time);
554 GST_DEBUG_OBJECT (sel,
555 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
556 "format %d, " "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
557 G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
559 gst_segment_set_newsegment (&sel->segment, update,
560 rate, arate, format, start, stop, time);
562 /* Send newsegment to all src pads */
563 gst_pad_event_default (pad, event);
567 /* Send eos to all src pads */
568 gst_pad_event_default (pad, event);
571 /* Send other events to pending or active src pad */
573 sel->pending_srcpad ? sel->pending_srcpad : sel->active_srcpad;
575 res = gst_pad_push_event (output_pad, event);
579 gst_object_unref (sel);