Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gstreamer.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  */
19
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  */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <string.h>
34
35 #include "gstoutputselector.h"
36
37 GST_DEBUG_CATEGORY_STATIC (output_selector_debug);
38 #define GST_CAT_DEFAULT output_selector_debug
39
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);
45
46 static GstStaticPadTemplate gst_output_selector_src_factory =
47 GST_STATIC_PAD_TEMPLATE ("src_%u",
48     GST_PAD_SRC,
49     GST_PAD_REQUEST,
50     GST_STATIC_CAPS_ANY);
51
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   };
69
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 }
77
78
79 enum
80 {
81   PROP_0,
82   PROP_ACTIVE_PAD,
83   PROP_RESEND_LATEST,
84   PROP_PAD_NEGOTIATION_MODE
85 };
86
87 #define DEFAULT_PAD_NEGOTIATION_MODE GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL
88
89 #define _do_init \
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);
95
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,
104     GstPad * pad);
105 static GstFlowReturn gst_output_selector_chain (GstPad * pad,
106     GstObject * parent, GstBuffer * buf);
107 static GstStateChangeReturn gst_output_selector_change_state (GstElement *
108     element, GstStateChange transition);
109 static gboolean gst_output_selector_event (GstPad * pad, GstObject * parent,
110     GstEvent * event);
111 static gboolean gst_output_selector_query (GstPad * pad, GstObject * parent,
112     GstQuery * query);
113 static void gst_output_selector_switch_pad_negotiation_mode (GstOutputSelector *
114     sel, gint mode);
115
116 static void
117 gst_output_selector_class_init (GstOutputSelectorClass * klass)
118 {
119   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
120   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
121
122   gobject_class->dispose = gst_output_selector_dispose;
123
124   gobject_class->set_property = gst_output_selector_set_property;
125   gobject_class->get_property = gst_output_selector_get_property;
126
127   g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
128       g_param_spec_object ("active-pad", "Active pad",
129           "Currently active src pad", GST_TYPE_PAD,
130           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
131   g_object_class_install_property (gobject_class, PROP_RESEND_LATEST,
132       g_param_spec_boolean ("resend-latest", "Resend latest buffer",
133           "Resend latest buffer after a switch to a new pad", FALSE,
134           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
135   g_object_class_install_property (gobject_class, PROP_PAD_NEGOTIATION_MODE,
136       g_param_spec_enum ("pad-negotiation-mode", "Pad negotiation mode",
137           "The mode to be used for pad negotiation",
138           GST_TYPE_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE,
139           DEFAULT_PAD_NEGOTIATION_MODE,
140           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
141
142   gst_element_class_set_details_simple (gstelement_class, "Output selector",
143       "Generic", "1-to-N output stream selector",
144       "Stefan Kost <stefan.kost@nokia.com>");
145   gst_element_class_add_pad_template (gstelement_class,
146       gst_static_pad_template_get (&gst_output_selector_sink_factory));
147   gst_element_class_add_pad_template (gstelement_class,
148       gst_static_pad_template_get (&gst_output_selector_src_factory));
149
150   gstelement_class->request_new_pad =
151       GST_DEBUG_FUNCPTR (gst_output_selector_request_new_pad);
152   gstelement_class->release_pad =
153       GST_DEBUG_FUNCPTR (gst_output_selector_release_pad);
154
155   gstelement_class->change_state = gst_output_selector_change_state;
156 }
157
158 static void
159 gst_output_selector_init (GstOutputSelector * sel)
160 {
161   sel->sinkpad =
162       gst_pad_new_from_static_template (&gst_output_selector_sink_factory,
163       "sink");
164   gst_pad_set_chain_function (sel->sinkpad,
165       GST_DEBUG_FUNCPTR (gst_output_selector_chain));
166   gst_pad_set_event_function (sel->sinkpad,
167       GST_DEBUG_FUNCPTR (gst_output_selector_event));
168   gst_pad_set_query_function (sel->sinkpad,
169       GST_DEBUG_FUNCPTR (gst_output_selector_query));
170
171   gst_element_add_pad (GST_ELEMENT (sel), sel->sinkpad);
172
173   /* srcpad management */
174   sel->active_srcpad = NULL;
175   sel->nb_srcpads = 0;
176   gst_segment_init (&sel->segment, GST_FORMAT_TIME);
177   sel->pending_srcpad = NULL;
178
179   sel->resend_latest = FALSE;
180   sel->latest_buffer = NULL;
181   gst_output_selector_switch_pad_negotiation_mode (sel,
182       DEFAULT_PAD_NEGOTIATION_MODE);
183 }
184
185 static void
186 gst_output_selector_reset (GstOutputSelector * osel)
187 {
188   if (osel->pending_srcpad != NULL) {
189     gst_object_unref (osel->pending_srcpad);
190     osel->pending_srcpad = NULL;
191   }
192   if (osel->latest_buffer != NULL) {
193     gst_buffer_unref (osel->latest_buffer);
194     osel->latest_buffer = NULL;
195   }
196   gst_segment_init (&osel->segment, GST_FORMAT_UNDEFINED);
197 }
198
199 static void
200 gst_output_selector_dispose (GObject * object)
201 {
202   GstOutputSelector *osel = GST_OUTPUT_SELECTOR (object);
203
204   gst_output_selector_reset (osel);
205
206   G_OBJECT_CLASS (parent_class)->dispose (object);
207 }
208
209 static void
210 gst_output_selector_set_property (GObject * object, guint prop_id,
211     const GValue * value, GParamSpec * pspec)
212 {
213   GstOutputSelector *sel = GST_OUTPUT_SELECTOR (object);
214
215   switch (prop_id) {
216     case PROP_ACTIVE_PAD:
217     {
218       GstPad *next_pad;
219
220       next_pad = g_value_get_object (value);
221
222       GST_INFO_OBJECT (sel, "Activating pad %s:%s",
223           GST_DEBUG_PAD_NAME (next_pad));
224
225       GST_OBJECT_LOCK (object);
226       if (next_pad != sel->active_srcpad) {
227         /* switch to new srcpad in next chain run */
228         if (sel->pending_srcpad != NULL) {
229           GST_INFO ("replacing pending switch");
230           gst_object_unref (sel->pending_srcpad);
231         }
232         if (next_pad)
233           gst_object_ref (next_pad);
234         sel->pending_srcpad = next_pad;
235       } else {
236         GST_INFO ("pad already active");
237         if (sel->pending_srcpad != NULL) {
238           gst_object_unref (sel->pending_srcpad);
239           sel->pending_srcpad = NULL;
240         }
241       }
242       GST_OBJECT_UNLOCK (object);
243       break;
244     }
245     case PROP_RESEND_LATEST:{
246       sel->resend_latest = g_value_get_boolean (value);
247       break;
248     }
249     case PROP_PAD_NEGOTIATION_MODE:{
250       gst_output_selector_switch_pad_negotiation_mode (sel,
251           g_value_get_enum (value));
252       break;
253     }
254     default:
255       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256       break;
257   }
258 }
259
260 static void
261 gst_output_selector_get_property (GObject * object, guint prop_id,
262     GValue * value, GParamSpec * pspec)
263 {
264   GstOutputSelector *sel = GST_OUTPUT_SELECTOR (object);
265
266   switch (prop_id) {
267     case PROP_ACTIVE_PAD:
268       GST_OBJECT_LOCK (object);
269       g_value_set_object (value,
270           sel->pending_srcpad ? sel->pending_srcpad : sel->active_srcpad);
271       GST_OBJECT_UNLOCK (object);
272       break;
273     case PROP_RESEND_LATEST:{
274       GST_OBJECT_LOCK (object);
275       g_value_set_boolean (value, sel->resend_latest);
276       GST_OBJECT_UNLOCK (object);
277       break;
278     }
279     case PROP_PAD_NEGOTIATION_MODE:
280       g_value_set_enum (value, sel->pad_negotiation_mode);
281       break;
282     default:
283       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284       break;
285   }
286 }
287
288 static GstPad *
289 gst_output_selector_get_active (GstOutputSelector * sel)
290 {
291   GstPad *active = NULL;
292
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);
299
300   return active;
301 }
302
303 static void
304 gst_output_selector_switch_pad_negotiation_mode (GstOutputSelector * sel,
305     gint mode)
306 {
307   sel->pad_negotiation_mode = mode;
308 }
309
310 static gboolean
311 forward_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
312 {
313   GstPad *srcpad = GST_PAD_CAST (user_data);
314
315   gst_pad_push_event (srcpad, gst_event_ref (*event));
316
317   return TRUE;
318 }
319
320 static GstPad *
321 gst_output_selector_request_new_pad (GstElement * element,
322     GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
323 {
324   gchar *padname;
325   GstPad *srcpad;
326   GstOutputSelector *osel;
327
328   osel = GST_OUTPUT_SELECTOR (element);
329
330   GST_DEBUG_OBJECT (osel, "requesting pad");
331
332   GST_OBJECT_LOCK (osel);
333   padname = g_strdup_printf ("src_%u", osel->nb_srcpads++);
334   srcpad = gst_pad_new_from_template (templ, padname);
335   GST_OBJECT_UNLOCK (osel);
336
337   gst_pad_set_active (srcpad, TRUE);
338
339   /* Forward sticky events to the new srcpad */
340   gst_pad_sticky_events_foreach (osel->sinkpad, forward_sticky_events, srcpad);
341
342   gst_element_add_pad (GST_ELEMENT (osel), srcpad);
343
344   /* Set the first requested src pad as active by default */
345   if (osel->active_srcpad == NULL) {
346     osel->active_srcpad = srcpad;
347   }
348   g_free (padname);
349
350   return srcpad;
351 }
352
353 static void
354 gst_output_selector_release_pad (GstElement * element, GstPad * pad)
355 {
356   GstOutputSelector *osel;
357
358   osel = GST_OUTPUT_SELECTOR (element);
359
360   GST_DEBUG_OBJECT (osel, "releasing pad");
361
362   gst_pad_set_active (pad, FALSE);
363
364   gst_element_remove_pad (GST_ELEMENT_CAST (osel), pad);
365 }
366
367 static gboolean
368 gst_output_selector_switch (GstOutputSelector * osel)
369 {
370   gboolean res = FALSE;
371   GstEvent *ev = NULL;
372   GstSegment *seg = NULL;
373   gint64 start = 0, position = 0;
374
375   /* Switch */
376   GST_OBJECT_LOCK (GST_OBJECT (osel));
377   GST_INFO ("switching to pad %" GST_PTR_FORMAT, osel->pending_srcpad);
378   if (gst_pad_is_linked (osel->pending_srcpad)) {
379     osel->active_srcpad = osel->pending_srcpad;
380     res = TRUE;
381   }
382   gst_object_unref (osel->pending_srcpad);
383   osel->pending_srcpad = NULL;
384   GST_OBJECT_UNLOCK (GST_OBJECT (osel));
385
386   /* Send SEGMENT event and latest buffer if switching succeeded */
387   if (res) {
388     /* Send SEGMENT to the pad we are going to switch to */
389     seg = &osel->segment;
390     /* If resending then mark segment start and position accordingly */
391     if (osel->resend_latest && osel->latest_buffer &&
392         GST_BUFFER_TIMESTAMP_IS_VALID (osel->latest_buffer)) {
393       start = position = GST_BUFFER_TIMESTAMP (osel->latest_buffer);
394     } else {
395       start = position = seg->position;
396     }
397
398     seg->start = start;
399     seg->position = position;
400     ev = gst_event_new_segment (seg);
401
402     if (!gst_pad_push_event (osel->active_srcpad, ev)) {
403       GST_WARNING_OBJECT (osel,
404           "newsegment handling failed in %" GST_PTR_FORMAT,
405           osel->active_srcpad);
406     }
407
408     /* Resend latest buffer to newly switched pad */
409     if (osel->resend_latest && osel->latest_buffer) {
410       GST_INFO ("resending latest buffer");
411       gst_pad_push (osel->active_srcpad, gst_buffer_ref (osel->latest_buffer));
412     }
413   } else {
414     GST_WARNING_OBJECT (osel, "switch failed, pad not linked");
415   }
416
417   return res;
418 }
419
420 static GstFlowReturn
421 gst_output_selector_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
422 {
423   GstFlowReturn res;
424   GstOutputSelector *osel;
425   GstClockTime position, duration;
426
427   osel = GST_OUTPUT_SELECTOR (parent);
428
429   /*
430    * The _switch function might push a buffer if 'resend-latest' is true.
431    *
432    * Elements/Applications (e.g. camerabin) might use pad probes to
433    * switch output-selector's active pad. If we simply switch and don't
434    * recheck any pending pad switch the following codepath could end
435    * up pushing a buffer on a non-active pad. This is bad.
436    *
437    * So we always should check the pending_srcpad before going further down
438    * the chain and pushing the new buffer
439    */
440   while (osel->pending_srcpad) {
441     /* Do the switch */
442     gst_output_selector_switch (osel);
443   }
444
445   if (osel->latest_buffer) {
446     gst_buffer_unref (osel->latest_buffer);
447     osel->latest_buffer = NULL;
448   }
449
450   if (osel->resend_latest) {
451     /* Keep reference to latest buffer to resend it after switch */
452     osel->latest_buffer = gst_buffer_ref (buf);
453   }
454
455   /* Keep track of last stop and use it in NEWSEGMENT start after 
456      switching to a new src pad */
457   position = GST_BUFFER_TIMESTAMP (buf);
458   if (GST_CLOCK_TIME_IS_VALID (position)) {
459     duration = GST_BUFFER_DURATION (buf);
460     if (GST_CLOCK_TIME_IS_VALID (duration)) {
461       position += duration;
462     }
463     GST_LOG_OBJECT (osel, "setting last stop %" GST_TIME_FORMAT,
464         GST_TIME_ARGS (position));
465     osel->segment.position = position;
466   }
467
468   GST_LOG_OBJECT (osel, "pushing buffer to %" GST_PTR_FORMAT,
469       osel->active_srcpad);
470   res = gst_pad_push (osel->active_srcpad, buf);
471
472   return res;
473 }
474
475 static GstStateChangeReturn
476 gst_output_selector_change_state (GstElement * element,
477     GstStateChange transition)
478 {
479   GstOutputSelector *sel;
480   GstStateChangeReturn result;
481
482   sel = GST_OUTPUT_SELECTOR (element);
483
484   switch (transition) {
485     case GST_STATE_CHANGE_READY_TO_PAUSED:
486       break;
487     default:
488       break;
489   }
490
491   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
492
493   switch (transition) {
494     case GST_STATE_CHANGE_PAUSED_TO_READY:
495       gst_output_selector_reset (sel);
496       break;
497     default:
498       break;
499   }
500
501   return result;
502 }
503
504 static gboolean
505 gst_output_selector_event (GstPad * pad, GstObject * parent, GstEvent * event)
506 {
507   gboolean res = TRUE;
508   GstOutputSelector *sel;
509   GstPad *active = NULL;
510
511   sel = GST_OUTPUT_SELECTOR (parent);
512
513   switch (GST_EVENT_TYPE (event)) {
514     case GST_EVENT_CAPS:
515     {
516       switch (sel->pad_negotiation_mode) {
517         case GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL:
518           /* Send caps to all src pads */
519           res = gst_pad_event_default (pad, parent, event);
520           break;
521         case GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE:
522           gst_event_unref (event);
523           break;
524         default:
525           active = gst_output_selector_get_active (sel);
526           if (active) {
527             res = gst_pad_push_event (active, event);
528             gst_object_unref (active);
529           } else {
530             gst_event_unref (event);
531           }
532           break;
533       }
534       break;
535     }
536     case GST_EVENT_SEGMENT:
537     {
538       gst_event_copy_segment (event, &sel->segment);
539
540       GST_DEBUG_OBJECT (sel, "configured SEGMENT %" GST_SEGMENT_FORMAT,
541           &sel->segment);
542
543       /* Send newsegment to all src pads */
544       res = gst_pad_event_default (pad, parent, event);
545       break;
546     }
547     case GST_EVENT_EOS:
548       /* Send eos to all src pads */
549       res = gst_pad_event_default (pad, parent, event);
550       break;
551     default:
552     {
553       /* Send other events to pending or active src pad */
554       active = gst_output_selector_get_active (sel);
555       if (active) {
556         res = gst_pad_push_event (active, event);
557         gst_object_unref (active);
558       } else {
559         gst_event_unref (event);
560       }
561       break;
562     }
563   }
564
565   return res;
566 }
567
568 static gboolean
569 gst_output_selector_query (GstPad * pad, GstObject * parent, GstQuery * query)
570 {
571   gboolean res = TRUE;
572   GstOutputSelector *sel;
573   GstPad *active = NULL;
574
575   sel = GST_OUTPUT_SELECTOR (parent);
576
577   switch (GST_QUERY_TYPE (query)) {
578     case GST_QUERY_CAPS:
579     {
580       switch (sel->pad_negotiation_mode) {
581         case GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_ALL:
582           /* Send caps to all src pads */
583           res = gst_pad_proxy_query_caps (pad, query);
584           break;
585         case GST_OUTPUT_SELECTOR_PAD_NEGOTIATION_MODE_NONE:
586           res = FALSE;
587           break;
588         default:
589           active = gst_output_selector_get_active (sel);
590           if (active) {
591             res = gst_pad_peer_query (active, query);
592             gst_object_unref (active);
593           } else {
594             res = FALSE;
595           }
596           break;
597       }
598       break;
599     }
600     default:
601       res = gst_pad_query_default (pad, parent, query);
602       break;
603   }
604   return res;
605 }