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