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