]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - plugins/elements/gstoutputselector.c
Bump git version after unplanned 0.10.35 release
[glsdk/gstreamer0-10.git] / plugins / elements / gstoutputselector.c
1 /* GStreamer output selector
2  * Copyright (C) 2008 Nokia Corporation. (contact <stefan.kost@nokia.com>)
3  *
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.
8  *
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.
13  *
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.
18  */
20 /**
21  * SECTION:element-output-selector
22  * @see_also: #GstOutputSelector, #GstInputSelector
23  *
24  * Direct input stream to one out of N output pads.
25  *
26  * Since: 0.10.32
27  */
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
33 #include <string.h>
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",
42     GST_PAD_SINK,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS_ANY);
46 static GstStaticPadTemplate gst_output_selector_src_factory =
47 GST_STATIC_PAD_TEMPLATE ("src%d",
48     GST_PAD_SRC,
49     GST_PAD_REQUEST,
50     GST_STATIC_CAPS_ANY);
52 enum GstOutputSelectorPadNegotiationMode
53 {
54   GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE,
55   GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL,
56   GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ACTIVE
57 };
58 #define GST_TYPE_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE (gst_output_selector_pad_negotiation_mode_get_type())
59 static GType
60 gst_output_selector_pad_negotiation_mode_get_type (void)
61 {
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"},
67     {0, NULL, NULL}
68   };
70   if (!pad_negotiation_mode_type) {
71     pad_negotiation_mode_type =
72         g_enum_register_static ("GstOutputSelectorPadNegotiationMode",
73         pad_negotiation_modes);
74   }
75   return pad_negotiation_mode_type;
76 }
79 enum
80 {
81   PROP_0,
82   PROP_ACTIVE_PAD,
83   PROP_RESEND_LATEST,
84   PROP_PAD_NEGOTIATION_MODE
85 };
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,
104     GstPad * pad);
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,
111     GstEvent * event);
112 static void gst_output_selector_switch_pad_negotiation_mode (GstOutputSelector *
113     sel, gint mode);
115 static void
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));
129 static void
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;
163 static void
164 gst_output_selector_init (GstOutputSelector * sel,
165     GstOutputSelectorClass * g_class)
167   sel->sinkpad =
168       gst_pad_new_from_static_template (&gst_output_selector_sink_factory,
169       "sink");
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;
181   sel->nb_srcpads = 0;
182   gst_segment_init (&sel->segment, GST_FORMAT_TIME);
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);
191 static void
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;
197   }
198   if (osel->latest_buffer != NULL) {
199     gst_buffer_unref (osel->latest_buffer);
200     osel->latest_buffer = NULL;
201   }
202   gst_segment_init (&osel->segment, GST_FORMAT_UNDEFINED);
205 static void
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);
215 static void
216 gst_output_selector_set_property (GObject * object, guint prop_id,
217     const GValue * value, GParamSpec * pspec)
219   GstOutputSelector *sel = GST_OUTPUT_SELECTOR (object);
221   switch (prop_id) {
222     case PROP_ACTIVE_PAD:
223     {
224       GstPad *next_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);
237         }
238         if (next_pad)
239           gst_object_ref (next_pad);
240         sel->pending_srcpad = next_pad;
241       } else {
242         GST_INFO ("pad already active");
243         if (sel->pending_srcpad != NULL) {
244           gst_object_unref (sel->pending_srcpad);
245           sel->pending_srcpad = NULL;
246         }
247       }
248       GST_OBJECT_UNLOCK (object);
249       break;
250     }
251     case PROP_RESEND_LATEST:{
252       sel->resend_latest = g_value_get_boolean (value);
253       break;
254     }
255     case PROP_PAD_NEGOTIATION_MODE:{
256       gst_output_selector_switch_pad_negotiation_mode (sel,
257           g_value_get_enum (value));
258       break;
259     }
260     default:
261       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
262       break;
263   }
266 static void
267 gst_output_selector_get_property (GObject * object, guint prop_id,
268     GValue * value, GParamSpec * pspec)
270   GstOutputSelector *sel = GST_OUTPUT_SELECTOR (object);
272   switch (prop_id) {
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);
278       break;
279     case PROP_RESEND_LATEST:{
280       GST_OBJECT_LOCK (object);
281       g_value_set_boolean (value, sel->resend_latest);
282       GST_OBJECT_UNLOCK (object);
283       break;
284     }
285     case PROP_PAD_NEGOTIATION_MODE:
286       g_value_set_enum (value, sel->pad_negotiation_mode);
287       break;
288     default:
289       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
290       break;
291   }
294 static GstCaps *
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);
311   if (active) {
312     caps = gst_pad_peer_get_caps_reffed (active);
313     gst_object_unref (active);
314   }
315   if (caps == NULL) {
316     caps = gst_caps_new_any ();
317   }
319   gst_object_unref (sel);
320   return caps;
323 static gboolean
324 gst_output_selector_sink_setcaps (GstPad * pad, GstCaps * caps)
326   GstOutputSelector *sel = GST_OUTPUT_SELECTOR (GST_PAD_PARENT (pad));
327   GstPad *active = NULL;
328   gboolean ret = TRUE;
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);
337   if (active) {
338     ret = gst_pad_set_caps (active, caps);
339     gst_object_unref (active);
340   }
341   return ret;
344 static void
345 gst_output_selector_switch_pad_negotiation_mode (GstOutputSelector * sel,
346     gint mode)
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);
360   }
363 static GstFlowReturn
364 gst_output_selector_buffer_alloc (GstPad * pad, guint64 offset, guint size,
365     GstCaps * caps, GstBuffer ** buf)
367   GstOutputSelector *sel;
368   GstFlowReturn res;
369   GstPad *allocpad;
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;
377   if (allocpad) {
378     /* if we had a previous pad we used for allocating a buffer, continue using
379      * it. */
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);
389   } else {
390     /* fallback case, allocate a buffer of our own, add pad caps. */
391     GST_DEBUG_OBJECT (pad, "fallback buffer alloc");
392     *buf = NULL;
393     res = GST_FLOW_OK;
394   }
395   GST_OBJECT_UNLOCK (sel);
397   GST_DEBUG_OBJECT (sel, "buffer alloc finished: %s", gst_flow_get_name (res));
399   gst_object_unref (sel);
400   return res;
403 static GstPad *
404 gst_output_selector_request_new_pad (GstElement * element,
405     GstPadTemplate * templ, const gchar * name)
407   gchar *padname;
408   GstPad *srcpad;
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;
426   }
427   g_free (padname);
429   return srcpad;
432 static void
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);
446 static gboolean
447 gst_output_selector_switch (GstOutputSelector * osel)
449   gboolean res = FALSE;
450   GstEvent *ev = NULL;
451   GstSegment *seg = NULL;
452   gint64 start = 0, position = 0;
454   /* Switch */
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;
459     res = TRUE;
460   }
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   if (res) {
467     /* Send NEWSEGMENT to the pad we are going to switch to */
468     seg = &osel->segment;
469     /* If resending then mark newsegment start and position accordingly */
470     if (osel->resend_latest && osel->latest_buffer &&
471         GST_BUFFER_TIMESTAMP_IS_VALID (osel->latest_buffer)) {
472       start = position = GST_BUFFER_TIMESTAMP (osel->latest_buffer);
473     } else {
474       start = position = seg->last_stop;
475     }
476     ev = gst_event_new_new_segment (TRUE, seg->rate,
477         seg->format, start, seg->stop, position);
478     if (!gst_pad_push_event (osel->active_srcpad, ev)) {
479       GST_WARNING_OBJECT (osel,
480           "newsegment handling failed in %" GST_PTR_FORMAT,
481           osel->active_srcpad);
482     }
484     /* Resend latest buffer to newly switched pad */
485     if (osel->resend_latest && osel->latest_buffer) {
486       GST_INFO ("resending latest buffer");
487       gst_pad_push (osel->active_srcpad, gst_buffer_ref (osel->latest_buffer));
488     }
489   } else {
490     GST_WARNING_OBJECT (osel, "switch failed, pad not linked");
491   }
493   return res;
496 static GstFlowReturn
497 gst_output_selector_chain (GstPad * pad, GstBuffer * buf)
499   GstFlowReturn res;
500   GstOutputSelector *osel;
501   GstClockTime last_stop, duration;
503   osel = GST_OUTPUT_SELECTOR (gst_pad_get_parent (pad));
505   /*
506    * The _switch function might push a buffer if 'resend-latest' is true.
507    *
508    * Elements/Applications (e.g. camerabin) might use pad probes to
509    * switch output-selector's active pad. If we simply switch and don't
510    * recheck any pending pad switch the following codepath could end
511    * up pushing a buffer on a non-active pad. This is bad.
512    *
513    * So we always should check the pending_srcpad before going further down
514    * the chain and pushing the new buffer
515    */
516   while (osel->pending_srcpad) {
517     /* Do the switch */
518     gst_output_selector_switch (osel);
519   }
521   if (osel->latest_buffer) {
522     gst_buffer_unref (osel->latest_buffer);
523     osel->latest_buffer = NULL;
524   }
526   if (osel->resend_latest) {
527     /* Keep reference to latest buffer to resend it after switch */
528     osel->latest_buffer = gst_buffer_ref (buf);
529   }
531   /* Keep track of last stop and use it in NEWSEGMENT start after 
532      switching to a new src pad */
533   last_stop = GST_BUFFER_TIMESTAMP (buf);
534   if (GST_CLOCK_TIME_IS_VALID (last_stop)) {
535     duration = GST_BUFFER_DURATION (buf);
536     if (GST_CLOCK_TIME_IS_VALID (duration)) {
537       last_stop += duration;
538     }
539     GST_LOG_OBJECT (osel, "setting last stop %" GST_TIME_FORMAT,
540         GST_TIME_ARGS (last_stop));
541     gst_segment_set_last_stop (&osel->segment, osel->segment.format, last_stop);
542   }
544   GST_LOG_OBJECT (osel, "pushing buffer to %" GST_PTR_FORMAT,
545       osel->active_srcpad);
546   res = gst_pad_push (osel->active_srcpad, buf);
547   gst_object_unref (osel);
549   return res;
552 static GstStateChangeReturn
553 gst_output_selector_change_state (GstElement * element,
554     GstStateChange transition)
556   GstOutputSelector *sel;
557   GstStateChangeReturn result;
559   sel = GST_OUTPUT_SELECTOR (element);
561   switch (transition) {
562     case GST_STATE_CHANGE_READY_TO_PAUSED:
563       break;
564     default:
565       break;
566   }
568   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
570   switch (transition) {
571     case GST_STATE_CHANGE_PAUSED_TO_READY:
572       gst_output_selector_reset (sel);
573       break;
574     default:
575       break;
576   }
578   return result;
581 static gboolean
582 gst_output_selector_handle_sink_event (GstPad * pad, GstEvent * event)
584   gboolean res = TRUE;
585   GstOutputSelector *sel;
586   GstPad *output_pad = NULL;
588   sel = GST_OUTPUT_SELECTOR (gst_pad_get_parent (pad));
589   if (G_UNLIKELY (sel == NULL)) {
590     gst_event_unref (event);
591     return FALSE;
592   }
594   switch (GST_EVENT_TYPE (event)) {
595     case GST_EVENT_NEWSEGMENT:
596     {
597       gboolean update;
598       GstFormat format;
599       gdouble rate, arate;
600       gint64 start, stop, time;
602       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
603           &start, &stop, &time);
605       GST_DEBUG_OBJECT (sel,
606           "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
607           "format %d, " "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
608           G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
610       gst_segment_set_newsegment_full (&sel->segment, update,
611           rate, arate, format, start, stop, time);
613       /* Send newsegment to all src pads */
614       gst_pad_event_default (pad, event);
615       break;
616     }
617     case GST_EVENT_EOS:
618       /* Send eos to all src pads */
619       gst_pad_event_default (pad, event);
620       break;
621     default:
622       /* Send other events to pending or active src pad */
623       output_pad =
624           sel->pending_srcpad ? sel->pending_srcpad : sel->active_srcpad;
625       res = gst_pad_push_event (output_pad, event);
626       break;
627   }
629   gst_object_unref (sel);
631   return res;