plugins/elements/gstinputselector.*: Added "select-all" property to make it work...
[platform/upstream/gstreamer.git] / plugins / elements / gstinputselector.c
1 /* GStreamer
2  * Copyright (C) 2003 Julien Moutte <julien@moutte.net>
3  * Copyright (C) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
4  * Copyright (C) 2005 Jan Schmidt <thaytan@mad.scientist.com>
5  * Copyright (C) 2007 Wim Taymans <wim.taymans@gmail.com>
6  * Copyright (C) 2007 Andy Wingo <wingo@pobox.com>
7  * Copyright (C) 2008 Nokia Corporation. (contact <stefan.kost@nokia.com>)
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 /**
26  * SECTION:element-input-selector
27  * @short_description: N-to-1 stream selectoring
28  * @see_also: #GstOutputSelector
29  *
30  * Direct one out of N input streams to the output pad.
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <string.h>
38
39 #include "gstinputselector.h"
40 #include "gstselector-marshal.h"
41
42 GST_DEBUG_CATEGORY_STATIC (input_selector_debug);
43 #define GST_CAT_DEFAULT input_selector_debug
44
45 static const GstElementDetails gst_input_selector_details =
46 GST_ELEMENT_DETAILS ("Input selector",
47     "Generic",
48     "N-to-1 input stream selectoring",
49     "Julien Moutte <julien@moutte.net>\n"
50     "Ronald S. Bultje <rbultje@ronald.bitfreak.net>\n"
51     "Jan Schmidt <thaytan@mad.scientist.com>\n"
52     "Wim Taymans <wim.taymans@gmail.com>");
53
54 static GstStaticPadTemplate gst_input_selector_sink_factory =
55 GST_STATIC_PAD_TEMPLATE ("sink%d",
56     GST_PAD_SINK,
57     GST_PAD_REQUEST,
58     GST_STATIC_CAPS_ANY);
59
60 static GstStaticPadTemplate gst_input_selector_src_factory =
61 GST_STATIC_PAD_TEMPLATE ("src",
62     GST_PAD_SRC,
63     GST_PAD_ALWAYS,
64     GST_STATIC_CAPS_ANY);
65
66 enum
67 {
68   PROP_ACTIVE_PAD = 1,
69   PROP_SELECT_ALL
70 };
71
72 enum
73 {
74   PAD_PROP_RUNNING_TIME = 1
75 };
76
77 enum
78 {
79   /* methods */
80   SIGNAL_BLOCK,
81   SIGNAL_SWITCH,
82   LAST_SIGNAL
83 };
84 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
85
86 static gboolean gst_input_selector_is_active_sinkpad (GstInputSelector * sel,
87     GstPad * pad);
88 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
89     GstPad * pad);
90 static GstPad *gst_input_selector_get_linked_pad (GstPad * pad,
91     gboolean strict);
92 static void gst_input_selector_push_pending_stop (GstInputSelector * self);
93 static gboolean gst_input_selector_check_eos (GstElement * selector);
94
95 #define GST_TYPE_SELECTOR_PAD \
96   (gst_selector_pad_get_type())
97 #define GST_SELECTOR_PAD(obj) \
98   (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
99 #define GST_SELECTOR_PAD_CLASS(klass) \
100   (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
101 #define GST_IS_SELECTOR_PAD(obj) \
102   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
103 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
104   (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
105 #define GST_SELECTOR_PAD_CAST(obj) \
106   ((GstSelectorPad *)(obj))
107
108 typedef struct _GstSelectorPad GstSelectorPad;
109 typedef struct _GstSelectorPadClass GstSelectorPadClass;
110
111 struct _GstSelectorPad
112 {
113   GstPad parent;
114
115   gboolean active;
116   gboolean eos;
117   gboolean segment_pending;
118   GstSegment segment;
119 };
120
121 struct _GstSelectorPadClass
122 {
123   GstPadClass parent;
124 };
125
126 static void gst_selector_pad_class_init (GstSelectorPadClass * klass);
127 static void gst_selector_pad_init (GstSelectorPad * pad);
128 static void gst_selector_pad_finalize (GObject * object);
129 static void gst_selector_pad_get_property (GObject * object,
130     guint prop_id, GValue * value, GParamSpec * pspec);
131
132 static GstPadClass *selector_pad_parent_class = NULL;
133
134 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
135 static void gst_selector_pad_reset (GstSelectorPad * pad);
136 static gboolean gst_selector_pad_event (GstPad * pad, GstEvent * event);
137 static GstCaps *gst_selector_pad_getcaps (GstPad * pad);
138 static GList *gst_selector_pad_get_linked_pads (GstPad * pad);
139 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstBuffer * buf);
140 static GstFlowReturn gst_selector_pad_bufferalloc (GstPad * pad,
141     guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
142
143 static GType
144 gst_selector_pad_get_type (void)
145 {
146   static GType selector_pad_type = 0;
147
148   if (!selector_pad_type) {
149     static const GTypeInfo selector_pad_info = {
150       sizeof (GstSelectorPadClass),
151       NULL,
152       NULL,
153       (GClassInitFunc) gst_selector_pad_class_init,
154       NULL,
155       NULL,
156       sizeof (GstSelectorPad),
157       0,
158       (GInstanceInitFunc) gst_selector_pad_init,
159     };
160
161     selector_pad_type =
162         g_type_register_static (GST_TYPE_PAD, "GstSelectorPad",
163         &selector_pad_info, 0);
164   }
165   return selector_pad_type;
166 }
167
168 static void
169 gst_selector_pad_class_init (GstSelectorPadClass * klass)
170 {
171   GObjectClass *gobject_class;
172
173   gobject_class = (GObjectClass *) klass;
174
175   selector_pad_parent_class = g_type_class_peek_parent (klass);
176
177   gobject_class->get_property =
178       GST_DEBUG_FUNCPTR (gst_selector_pad_get_property);
179   g_object_class_install_property (gobject_class, PAD_PROP_RUNNING_TIME,
180       g_param_spec_int64 ("running-time", "Running time",
181           "Running time of stream on pad", 0, G_MAXINT64, 0, G_PARAM_READABLE));
182
183   gobject_class->finalize = gst_selector_pad_finalize;
184 }
185
186 static void
187 gst_selector_pad_init (GstSelectorPad * pad)
188 {
189   gst_selector_pad_reset (pad);
190 }
191
192 static void
193 gst_selector_pad_finalize (GObject * object)
194 {
195   GstSelectorPad *pad;
196
197   pad = GST_SELECTOR_PAD_CAST (object);
198
199   G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
200 }
201
202 static void
203 gst_selector_pad_get_property (GObject * object, guint prop_id,
204     GValue * value, GParamSpec * pspec)
205 {
206   GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
207
208   switch (prop_id) {
209     case PAD_PROP_RUNNING_TIME:
210       g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
211       break;
212
213     default:
214       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215       break;
216   }
217 }
218
219 static gint64
220 gst_selector_pad_get_running_time (GstSelectorPad * pad)
221 {
222   gint64 ret = 0;
223
224   GST_OBJECT_LOCK (pad);
225
226   if (pad->active) {
227     gint64 last_stop = pad->segment.last_stop;
228
229     if (last_stop >= 0)
230       ret = gst_segment_to_running_time (&pad->segment, GST_FORMAT_TIME,
231           last_stop);
232   }
233
234   GST_OBJECT_UNLOCK (pad);
235
236   GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
237       GST_TIME_ARGS (ret));
238
239   return ret;
240 }
241
242 static void
243 gst_selector_pad_reset (GstSelectorPad * pad)
244 {
245   pad->active = FALSE;
246   pad->eos = FALSE;
247   gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
248 }
249
250 /* strictly get the linked pad from the sinkpad. If the pad is active we return
251  * the srcpad else we return NULL */
252 static GList *
253 gst_selector_pad_get_linked_pads (GstPad * pad)
254 {
255   GstPad *otherpad;
256
257   otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
258   if (!otherpad)
259     return NULL;
260
261   /* need to drop the ref, internal linked pads is not MT safe */
262   gst_object_unref (otherpad);
263
264   return g_list_append (NULL, otherpad);
265 }
266
267 static gboolean
268 gst_selector_pad_event (GstPad * pad, GstEvent * event)
269 {
270   gboolean res = TRUE;
271   gboolean forward = TRUE;
272   GstInputSelector *sel;
273   GstSelectorPad *selpad;
274
275   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
276   selpad = GST_SELECTOR_PAD_CAST (pad);
277
278   /* only forward if we are dealing with the active sinkpad */
279   forward = gst_input_selector_is_active_sinkpad (sel, pad);
280
281   /* forward all events in select_all mode by default */
282   if (sel->select_all) {
283     forward = TRUE;
284   }
285
286   switch (GST_EVENT_TYPE (event)) {
287     case GST_EVENT_FLUSH_STOP:
288       gst_selector_pad_reset (selpad);
289       break;
290     case GST_EVENT_NEWSEGMENT:
291     {
292       gboolean update;
293       GstFormat format;
294       gdouble rate, arate;
295       gint64 start, stop, time;
296
297       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
298           &start, &stop, &time);
299
300       GST_DEBUG_OBJECT (sel,
301           "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
302           "format %d, "
303           "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
304           G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
305
306       gst_segment_set_newsegment_full (&selpad->segment, update,
307           rate, arate, format, start, stop, time);
308       /* if we are not going to forward the segment, mark the segment as
309        * pending */
310       if (!forward)
311         selpad->segment_pending = TRUE;
312       break;
313     }
314     case GST_EVENT_EOS:
315       selpad->eos = TRUE;
316       /* don't forward eos in select_all mode until all sink pads have eos */
317       if (sel->select_all && !gst_input_selector_check_eos (GST_ELEMENT (sel))) {
318         forward = FALSE;
319       }
320       break;
321     default:
322       break;
323   }
324   if (forward)
325     res = gst_pad_push_event (sel->srcpad, event);
326   else
327     gst_event_unref (event);
328
329   gst_object_unref (sel);
330
331   return res;
332 }
333
334 static GstCaps *
335 gst_selector_pad_getcaps (GstPad * pad)
336 {
337   GstInputSelector *sel;
338   GstCaps *caps;
339
340   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
341
342   GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
343   caps = gst_pad_peer_get_caps (sel->srcpad);
344   if (caps == NULL)
345     caps = gst_caps_new_any ();
346
347   gst_object_unref (sel);
348
349   return caps;
350 }
351
352 static GstFlowReturn
353 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
354     guint size, GstCaps * caps, GstBuffer ** buf)
355 {
356   GstInputSelector *sel;
357   GstFlowReturn result;
358   GstPad *active_sinkpad;
359
360   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
361
362   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
363
364   /* Fallback allocation for buffers from pads except the selected one */
365   if (pad != active_sinkpad && !sel->select_all) {
366     GST_DEBUG_OBJECT (sel,
367         "Pad %s:%s is not selected. Performing fallback allocation",
368         GST_DEBUG_PAD_NAME (pad));
369
370     *buf = NULL;
371     result = GST_FLOW_OK;
372   } else {
373     result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
374
375     /* FIXME: HACK. If buffer alloc returns not-linked, perform a fallback
376      * allocation.  This should NOT be necessary, because playbin should
377      * properly block the source pad from running until it's finished hooking 
378      * everything up, but playbin needs refactoring first. */
379     if (result == GST_FLOW_NOT_LINKED) {
380       GST_DEBUG_OBJECT (sel,
381           "No peer pad yet - performing fallback allocation for pad %s:%s",
382           GST_DEBUG_PAD_NAME (pad));
383
384       *buf = NULL;
385       result = GST_FLOW_OK;
386     }
387   }
388
389   gst_object_unref (sel);
390
391   return result;
392 }
393
394 static gboolean
395 gst_input_selector_wait (GstInputSelector * self, GstPad * pad)
396 {
397   gboolean flushing;
398
399   GST_OBJECT_LOCK (self);
400
401   while (self->blocked)
402     g_cond_wait (self->blocked_cond, GST_OBJECT_GET_LOCK (self));
403
404   GST_OBJECT_UNLOCK (self);
405
406   GST_OBJECT_LOCK (pad);
407   flushing = GST_PAD_IS_FLUSHING (pad);
408   GST_OBJECT_UNLOCK (pad);
409
410   return flushing;
411 }
412
413 static GstFlowReturn
414 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
415 {
416   GstInputSelector *sel;
417   GstFlowReturn res;
418   GstPad *active_sinkpad;
419   GstSelectorPad *selpad;
420   GstClockTime end_time, duration;
421   GstSegment *seg;
422
423   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
424   selpad = GST_SELECTOR_PAD_CAST (pad);
425   seg = &selpad->segment;
426
427   if (gst_input_selector_wait (sel, pad))
428     goto ignore;
429
430   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
431
432   end_time = GST_BUFFER_TIMESTAMP (buf);
433   if (GST_CLOCK_TIME_IS_VALID (end_time)) {
434     duration = GST_BUFFER_DURATION (buf);
435     if (GST_CLOCK_TIME_IS_VALID (duration))
436       end_time += duration;
437     GST_DEBUG_OBJECT (sel, "received end time %" GST_TIME_FORMAT,
438         GST_TIME_ARGS (end_time));
439     gst_segment_set_last_stop (seg, seg->format, end_time);
440   }
441
442   /* Ignore buffers from pads except the selected one */
443   if (pad != active_sinkpad && !sel->select_all)
444     goto ignore;
445
446   gst_input_selector_push_pending_stop (sel);
447
448   /* if we have a pending segment, push it out now */
449   if (selpad->segment_pending) {
450     gst_pad_push_event (sel->srcpad, gst_event_new_new_segment_full (FALSE,
451             seg->rate, seg->applied_rate, seg->format, seg->start, seg->stop,
452             seg->time));
453
454     selpad->segment_pending = FALSE;
455   }
456
457   /* forward */
458   GST_DEBUG_OBJECT (sel, "Forwarding buffer %p from pad %s:%s", buf,
459       GST_DEBUG_PAD_NAME (pad));
460   res = gst_pad_push (sel->srcpad, buf);
461 done:
462   gst_object_unref (sel);
463   return res;
464   /* dropped buffers */
465 ignore:
466   {
467     GST_DEBUG_OBJECT (sel, "Ignoring buffer %p from pad %s:%s",
468         buf, GST_DEBUG_PAD_NAME (pad));
469     gst_buffer_unref (buf);
470     res = GST_FLOW_OK;
471     goto done;
472   }
473 }
474
475 static void gst_input_selector_dispose (GObject * object);
476 static void gst_input_selector_init (GstInputSelector * sel);
477 static void gst_input_selector_base_init (GstInputSelectorClass * klass);
478 static void gst_input_selector_class_init (GstInputSelectorClass * klass);
479 static void gst_input_selector_set_property (GObject * object,
480     guint prop_id, const GValue * value, GParamSpec * pspec);
481 static void gst_input_selector_get_property (GObject * object,
482     guint prop_id, GValue * value, GParamSpec * pspec);
483 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
484     GstPadTemplate * templ, const gchar * unused);
485 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
486 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
487     element, GstStateChange transition);
488 static GList *gst_input_selector_get_linked_pads (GstPad * pad);
489 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
490 static gint64 gst_input_selector_block (GstInputSelector * self);
491 static void gst_input_selector_switch (GstInputSelector * self,
492     const gchar * pad_name, gint64 stop_time, gint64 start_time);
493
494 static GstElementClass *parent_class = NULL;
495
496 GType
497 gst_input_selector_get_type (void)
498 {
499   static GType input_selector_type = 0;
500
501   if (!input_selector_type) {
502     static const GTypeInfo input_selector_info = {
503       sizeof (GstInputSelectorClass),
504       (GBaseInitFunc) gst_input_selector_base_init,
505       NULL,
506       (GClassInitFunc) gst_input_selector_class_init,
507       NULL,
508       NULL,
509       sizeof (GstInputSelector),
510       0,
511       (GInstanceInitFunc) gst_input_selector_init,
512     };
513     input_selector_type =
514         g_type_register_static (GST_TYPE_ELEMENT,
515         "GstInputSelector", &input_selector_info, 0);
516     GST_DEBUG_CATEGORY_INIT (input_selector_debug,
517         "input-selector", 0, "An input stream selector element");
518   }
519
520   return input_selector_type;
521 }
522
523 static void
524 gst_input_selector_base_init (GstInputSelectorClass * klass)
525 {
526   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
527
528   gst_element_class_set_details (element_class, &gst_input_selector_details);
529   gst_element_class_add_pad_template (element_class,
530       gst_static_pad_template_get (&gst_input_selector_sink_factory));
531   gst_element_class_add_pad_template (element_class,
532       gst_static_pad_template_get (&gst_input_selector_src_factory));
533 }
534
535 static void
536 gst_input_selector_class_init (GstInputSelectorClass * klass)
537 {
538   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
539   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
540
541   parent_class = g_type_class_peek_parent (klass);
542   gobject_class->set_property =
543       GST_DEBUG_FUNCPTR (gst_input_selector_set_property);
544   gobject_class->get_property =
545       GST_DEBUG_FUNCPTR (gst_input_selector_get_property);
546   g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
547       g_param_spec_string ("active-pad", "Active pad",
548           "Name of the currently" " active sink pad", NULL, G_PARAM_READWRITE));
549   g_object_class_install_property (gobject_class, PROP_SELECT_ALL,
550       g_param_spec_boolean ("select-all", "Select all mode",
551           "Forwards data from all input pads", FALSE, G_PARAM_READWRITE));
552   gobject_class->dispose = gst_input_selector_dispose;
553   gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
554   gstelement_class->release_pad = gst_input_selector_release_pad;
555   gstelement_class->change_state = gst_input_selector_change_state;
556
557   /**
558    * GstInputSelector::block:
559    * @inputselector: the #GstInputSelector
560    *
561    * Block all sink pads in preparation for a switch. Returns the stop time of
562    * the current switch segment, as a running time, or 0 if there is no current
563    * active pad or the current active pad never received data.
564    */
565   gst_input_selector_signals[SIGNAL_BLOCK] =
566       g_signal_new ("block", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
567       G_STRUCT_OFFSET (GstInputSelectorClass, block),
568       NULL, NULL, gst_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
569   /**
570    * GstInputSelector::switch:
571    * @inputselector: the #GstInputSelector
572    * @pad:            name of pad to switch to
573    * @stop_time:      running time at which to close the previous segment, or -1
574    *                  to use the running time of the previously active sink pad
575    * @start_time:     running time at which to start the new segment, or -1 to
576    *                  use the running time of the newly active sink pad
577    *
578    * Switch to a new feed. The segment opened by the previously active pad, if
579    * any, will be closed, and a new segment opened before data flows again.
580    *
581    * This signal must be emitted when the element has been blocked via the <link
582    * linkend="GstInputSelector-block">block</link> signal.
583    *
584    * If you have a stream with only one switch element, such as an audio-only
585    * stream, a stream switch should be performed by first emitting the block
586    * signal, and then emitting the switch signal with -1 for the stop and start
587    * time values.
588    *
589    * The intention of the @stop_time and @start_time arguments is to allow
590    * multiple switch elements to switch and maintain stream synchronization.
591    * When switching a stream with multiple feeds, you will need as many switch
592    * elements as you have feeds. For example, a feed with audio and video will
593    * have one switch element between the audio feeds and one for video.
594    *
595    * A switch over multiple switch elements should be performed as follows:
596    * First, emit the <link linkend="GstInputSelector-block">block</link>
597    * signal, collecting the returned values. The maximum running time returned
598    * by block should then be used as the time at which to close the previous
599    * segment.
600    *
601    * Then, query the running times of the new audio and video pads that you will
602    * switch to. Naturally, these pads are on separate switch elements. Take the
603    * minimum running time for those streams and use it for the time at which to
604    * open the new segment.
605    *
606    * If @pad is the same as the current active pad, the element will cancel any
607    * previous block without adjusting segments.
608    */
609   gst_input_selector_signals[SIGNAL_SWITCH] =
610       g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
611       G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
612       NULL, NULL, gst_selector_marshal_VOID__STRING_INT64_INT64,
613       G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_INT64, G_TYPE_INT64);
614
615   klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
616   klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
617 }
618
619 static void
620 gst_input_selector_init (GstInputSelector * sel)
621 {
622   sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
623   gst_pad_set_internal_link_function (sel->srcpad,
624       GST_DEBUG_FUNCPTR (gst_input_selector_get_linked_pads));
625   gst_pad_set_getcaps_function (sel->srcpad,
626       GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
627   gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
628   /* sinkpad management */
629   sel->active_sinkpad = NULL;
630   sel->nb_sinkpads = 0;
631   gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
632
633   sel->blocked_cond = g_cond_new ();
634   sel->blocked = FALSE;
635
636   sel->select_all = FALSE;
637 }
638
639 static void
640 gst_input_selector_dispose (GObject * object)
641 {
642   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
643
644   if (sel->active_sinkpad) {
645     gst_object_unref (sel->active_sinkpad);
646     sel->active_sinkpad = NULL;
647   }
648
649   if (sel->blocked_cond) {
650     g_cond_free (sel->blocked_cond);
651     sel->blocked_cond = NULL;
652   }
653
654   G_OBJECT_CLASS (parent_class)->dispose (object);
655 }
656
657 /* Solve the following equation for B.timestamp, and set that as the segment
658  * stop:
659  * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
660  */
661 static gint64
662 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
663 {
664   return (running_time - segment->accum) * segment->abs_rate + segment->start;
665 }
666
667 static void
668 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
669 {
670   segment->stop = gst_segment_get_timestamp (segment, running_time);
671   segment->last_stop = -1;
672 }
673
674 static void
675 gst_segment_set_start (GstSegment * segment, gint64 running_time)
676 {
677   segment->start = gst_segment_get_timestamp (segment, running_time);
678 }
679
680 static void
681 gst_input_selector_set_active_pad (GstInputSelector * self,
682     const gchar * pad_name, gint64 stop_time, gint64 start_time)
683 {
684   GstPad *pad;
685   GstSelectorPad *old, *new;
686   GstPad **active_pad_p;
687
688   if (strcmp (pad_name, "") != 0)
689     pad = gst_element_get_pad (GST_ELEMENT (self), pad_name);
690   else
691     pad = NULL;
692
693   GST_OBJECT_LOCK (self);
694
695   if (pad == self->active_sinkpad)
696     goto done;
697
698   old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
699   new = GST_SELECTOR_PAD_CAST (pad);
700
701   if (old && old->active && !self->pending_stop && stop_time >= 0) {
702     /* schedule a last_stop update if one isn't already scheduled, and a
703        segment has been pushed before. */
704     memcpy (&self->pending_stop_segment, &old->segment,
705         sizeof (self->pending_stop_segment));
706     gst_segment_set_stop (&self->pending_stop_segment, stop_time);
707     self->pending_stop = TRUE;
708   }
709
710   if (new && new->active && start_time >= 0) {
711     /* schedule a new segment push */
712     gst_segment_set_start (&new->segment, start_time);
713     new->segment_pending = TRUE;
714   }
715
716   active_pad_p = &self->active_sinkpad;
717   gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
718   GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
719       self->active_sinkpad);
720
721 done:
722   GST_OBJECT_UNLOCK (self);
723
724   if (pad)
725     gst_object_unref (pad);
726 }
727
728
729 static void
730 gst_input_selector_set_property (GObject * object, guint prop_id,
731     const GValue * value, GParamSpec * pspec)
732 {
733   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
734
735   switch (prop_id) {
736     case PROP_ACTIVE_PAD:
737       gst_input_selector_set_active_pad (sel,
738           g_value_get_string (value), GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
739       break;
740     case PROP_SELECT_ALL:
741       sel->select_all = g_value_get_boolean (value);
742       break;
743     default:
744       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
745       break;
746   }
747 }
748
749 static void
750 gst_input_selector_get_property (GObject * object, guint prop_id,
751     GValue * value, GParamSpec * pspec)
752 {
753   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
754
755   switch (prop_id) {
756     case PROP_ACTIVE_PAD:{
757       GST_OBJECT_LOCK (object);
758       if (sel->active_sinkpad != NULL) {
759         g_value_take_string (value, gst_pad_get_name (sel->active_sinkpad));
760       } else {
761         g_value_set_string (value, "");
762       }
763       GST_OBJECT_UNLOCK (object);
764       break;
765     }
766     case PROP_SELECT_ALL:
767       g_value_set_boolean (value, sel->select_all);
768       break;
769     default:
770       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
771       break;
772   }
773 }
774
775 static GstPad *
776 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
777 {
778   GstInputSelector *sel;
779   GstPad *otherpad = NULL;
780
781   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
782   GST_OBJECT_LOCK (sel);
783   if (pad == sel->srcpad)
784     otherpad = sel->active_sinkpad;
785   else if (pad == sel->active_sinkpad || !strict)
786     otherpad = sel->srcpad;
787   if (otherpad)
788     gst_object_ref (otherpad);
789   GST_OBJECT_UNLOCK (sel);
790   gst_object_unref (sel);
791   return otherpad;
792 }
793
794 static GstCaps *
795 gst_input_selector_getcaps (GstPad * pad)
796 {
797   GstPad *otherpad;
798   GstObject *parent;
799   GstCaps *caps;
800
801   parent = gst_object_get_parent (GST_OBJECT (pad));
802   if (GST_INPUT_SELECTOR (parent)->select_all) {
803     caps = gst_pad_proxy_getcaps (pad);
804     goto done;
805   }
806
807   otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
808   if (!otherpad) {
809     GST_DEBUG_OBJECT (parent,
810         "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
811     caps = gst_caps_new_any ();
812   } else {
813     GST_DEBUG_OBJECT (parent,
814         "Pad %s:%s is linked (to %s:%s), returning peer caps",
815         GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
816     /* if the peer has caps, use those. If the pad is not linked, this function
817      * returns NULL and we return ANY */
818     if (!(caps = gst_pad_peer_get_caps (otherpad)))
819       caps = gst_caps_new_any ();
820     gst_object_unref (otherpad);
821   }
822
823 done:
824   gst_object_unref (parent);
825   return caps;
826 }
827
828 /* check if the pad is the active sinkpad */
829 static gboolean
830 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
831 {
832   GstSelectorPad *selpad;
833   gboolean res;
834
835   selpad = GST_SELECTOR_PAD_CAST (pad);
836
837   GST_OBJECT_LOCK (sel);
838   res = (pad == sel->active_sinkpad);
839   GST_OBJECT_UNLOCK (sel);
840
841   return res;
842 }
843
844 /* Get or create the active sinkpad */
845 static GstPad *
846 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
847 {
848   GstPad *active_sinkpad;
849   GstSelectorPad *selpad;
850
851   selpad = GST_SELECTOR_PAD_CAST (pad);
852
853   GST_OBJECT_LOCK (sel);
854   selpad->active = TRUE;
855   active_sinkpad = sel->active_sinkpad;
856   if (active_sinkpad == NULL) {
857     /* first pad we get an alloc on becomes the activated pad by default */
858     active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
859     GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
860   }
861   GST_OBJECT_UNLOCK (sel);
862
863   return active_sinkpad;
864 }
865
866 static GList *
867 gst_input_selector_get_linked_pads (GstPad * pad)
868 {
869   GstPad *otherpad;
870
871   otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
872   if (!otherpad)
873     return NULL;
874   /* need to drop the ref, internal linked pads is not MT safe */
875   gst_object_unref (otherpad);
876   return g_list_append (NULL, otherpad);
877 }
878
879 static GstPad *
880 gst_input_selector_request_new_pad (GstElement * element,
881     GstPadTemplate * templ, const gchar * unused)
882 {
883   GstInputSelector *sel;
884   gchar *name = NULL;
885   GstPad *sinkpad = NULL;
886
887   sel = GST_INPUT_SELECTOR (element);
888   g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
889   GST_LOG_OBJECT (sel, "Creating new pad %d", sel->nb_sinkpads);
890   GST_OBJECT_LOCK (sel);
891   name = g_strdup_printf ("sink%d", sel->nb_sinkpads++);
892   sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
893       "name", name, "direction", templ->direction, "template", templ, NULL);
894   g_free (name);
895   GST_OBJECT_UNLOCK (sel);
896
897   gst_pad_set_event_function (sinkpad,
898       GST_DEBUG_FUNCPTR (gst_selector_pad_event));
899   gst_pad_set_getcaps_function (sinkpad,
900       GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
901   gst_pad_set_chain_function (sinkpad,
902       GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
903   gst_pad_set_internal_link_function (sinkpad,
904       GST_DEBUG_FUNCPTR (gst_selector_pad_get_linked_pads));
905   gst_pad_set_bufferalloc_function (sinkpad,
906       GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
907
908   gst_pad_set_active (sinkpad, TRUE);
909   gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
910   return sinkpad;
911 }
912
913 static void
914 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
915 {
916   GstInputSelector *sel;
917
918   sel = GST_INPUT_SELECTOR (element);
919   GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
920
921   GST_OBJECT_LOCK (sel);
922   /* if the pad was the active pad, makes us select a new one */
923   if (sel->active_sinkpad == pad) {
924     GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
925     sel->active_sinkpad = NULL;
926   }
927   GST_OBJECT_UNLOCK (sel);
928
929   gst_pad_set_active (pad, FALSE);
930   gst_element_remove_pad (GST_ELEMENT (sel), pad);
931 }
932
933 static GstStateChangeReturn
934 gst_input_selector_change_state (GstElement * element,
935     GstStateChange transition)
936 {
937   GstInputSelector *self = GST_INPUT_SELECTOR (element);
938   GstStateChangeReturn result;
939
940   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
941
942   if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
943     GST_OBJECT_LOCK (self);
944     self->blocked = FALSE;
945     g_cond_broadcast (self->blocked_cond);
946     GST_OBJECT_UNLOCK (self);
947   }
948
949   return result;
950 }
951
952 static gint64
953 gst_input_selector_block (GstInputSelector * self)
954 {
955   gint64 ret = 0;
956   GstSelectorPad *spad;
957
958   GST_OBJECT_LOCK (self);
959
960   if (self->blocked)
961     GST_WARNING_OBJECT (self, "switch already blocked");
962
963   self->blocked = TRUE;
964   spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
965
966   if (self->active_sinkpad)
967     ret = gst_selector_pad_get_running_time (spad);
968   else
969     GST_DEBUG_OBJECT (self, "no active pad while blocking");
970
971   GST_OBJECT_UNLOCK (self);
972
973   return ret;
974 }
975
976 static void
977 gst_input_selector_push_pending_stop (GstInputSelector * self)
978 {
979   GstEvent *event = NULL;
980
981   GST_OBJECT_LOCK (self);
982
983   if (G_UNLIKELY (self->pending_stop)) {
984     GstSegment *seg = &self->pending_stop_segment;
985
986     event = gst_event_new_new_segment_full (TRUE, seg->rate,
987         seg->applied_rate, seg->format, seg->start, seg->stop, seg->stop);
988
989     self->pending_stop = FALSE;
990   }
991
992   GST_OBJECT_UNLOCK (self);
993
994   if (event)
995     gst_pad_push_event (self->srcpad, event);
996 }
997
998 /* stop_time and start_time are running times */
999 static void
1000 gst_input_selector_switch (GstInputSelector * self, const gchar * pad_name,
1001     gint64 stop_time, gint64 start_time)
1002 {
1003   g_return_if_fail (self->blocked == TRUE);
1004
1005   gst_input_selector_set_active_pad (self, pad_name, stop_time, start_time);
1006
1007   GST_OBJECT_LOCK (self);
1008   self->blocked = FALSE;
1009   g_cond_broadcast (self->blocked_cond);
1010   GST_OBJECT_UNLOCK (self);
1011 }
1012
1013 static gboolean
1014 gst_input_selector_check_eos (GstElement * selector)
1015 {
1016   GstIterator *it = gst_element_iterate_sink_pads (selector);
1017   GstIteratorResult ires;
1018   gpointer item;
1019   gboolean done = FALSE, is_eos = FALSE;
1020   GstSelectorPad *pad;
1021
1022   while (!done) {
1023     ires = gst_iterator_next (it, &item);
1024     switch (ires) {
1025       case GST_ITERATOR_DONE:
1026         GST_INFO_OBJECT (selector, "all sink pads have eos");
1027         done = TRUE;
1028         is_eos = TRUE;
1029         break;
1030       case GST_ITERATOR_OK:
1031         pad = GST_SELECTOR_PAD_CAST (item);
1032         if (!pad->eos) {
1033           done = TRUE;
1034         }
1035         break;
1036       case GST_ITERATOR_RESYNC:
1037         gst_iterator_resync (it);
1038         break;
1039       default:
1040         done = TRUE;
1041         break;
1042     }
1043   }
1044   gst_iterator_free (it);
1045
1046   return is_eos;
1047 }