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