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