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