plugins/elements/gstinputselector.c: Don't leak event on pads that are not linked...
[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   else
316     gst_event_unref (event);
317
318   gst_object_unref (sel);
319
320   return res;
321 }
322
323 static GstCaps *
324 gst_selector_pad_getcaps (GstPad * pad)
325 {
326   GstInputSelector *sel;
327   GstCaps *caps;
328
329   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
330
331   GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
332   caps = gst_pad_peer_get_caps (sel->srcpad);
333   if (caps == NULL)
334     caps = gst_caps_new_any ();
335
336   gst_object_unref (sel);
337
338   return caps;
339 }
340
341 static GstFlowReturn
342 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
343     guint size, GstCaps * caps, GstBuffer ** buf)
344 {
345   GstInputSelector *sel;
346   GstFlowReturn result;
347   GstPad *active_sinkpad;
348
349   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
350
351   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
352
353   /* Fallback allocation for buffers from pads except the selected one */
354   if (pad != active_sinkpad) {
355     GST_DEBUG_OBJECT (sel,
356         "Pad %s:%s is not selected. Performing fallback allocation",
357         GST_DEBUG_PAD_NAME (pad));
358
359     *buf = NULL;
360     result = GST_FLOW_OK;
361   } else {
362     result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
363
364     /* FIXME: HACK. If buffer alloc returns not-linked, perform a fallback
365      * allocation.  This should NOT be necessary, because playbin should
366      * properly block the source pad from running until it's finished hooking 
367      * everything up, but playbin needs refactoring first. */
368     if (result == GST_FLOW_NOT_LINKED) {
369       GST_DEBUG_OBJECT (sel,
370           "No peer pad yet - performing fallback allocation for pad %s:%s",
371           GST_DEBUG_PAD_NAME (pad));
372
373       *buf = NULL;
374       result = GST_FLOW_OK;
375     }
376   }
377
378   gst_object_unref (sel);
379
380   return result;
381 }
382
383 static gboolean
384 gst_input_selector_wait (GstInputSelector * self, GstPad * pad)
385 {
386   gboolean flushing;
387
388   GST_OBJECT_LOCK (self);
389
390   while (self->blocked)
391     g_cond_wait (self->blocked_cond, GST_OBJECT_GET_LOCK (self));
392
393   GST_OBJECT_UNLOCK (self);
394
395   GST_OBJECT_LOCK (pad);
396   flushing = GST_PAD_IS_FLUSHING (pad);
397   GST_OBJECT_UNLOCK (pad);
398
399   return flushing;
400 }
401
402 static GstFlowReturn
403 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
404 {
405   GstInputSelector *sel;
406   GstFlowReturn res;
407   GstPad *active_sinkpad;
408   GstSelectorPad *selpad;
409   GstClockTime end_time, duration;
410   GstSegment *seg;
411
412   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
413   selpad = GST_SELECTOR_PAD_CAST (pad);
414   seg = &selpad->segment;
415
416   if (gst_input_selector_wait (sel, pad))
417     goto ignore;
418
419   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
420
421   end_time = GST_BUFFER_TIMESTAMP (buf);
422   if (GST_CLOCK_TIME_IS_VALID (end_time)) {
423     duration = GST_BUFFER_DURATION (buf);
424     if (GST_CLOCK_TIME_IS_VALID (duration))
425       end_time += duration;
426     GST_DEBUG_OBJECT (sel, "received end time %" GST_TIME_FORMAT,
427         GST_TIME_ARGS (end_time));
428     gst_segment_set_last_stop (seg, seg->format, end_time);
429   }
430
431   /* Ignore buffers from pads except the selected one */
432   if (pad != active_sinkpad)
433     goto ignore;
434
435   gst_input_selector_push_pending_stop (sel);
436
437   /* if we have a pending segment, push it out now */
438   if (selpad->segment_pending) {
439     gst_pad_push_event (sel->srcpad, gst_event_new_new_segment_full (FALSE,
440             seg->rate, seg->applied_rate, seg->format, seg->start, seg->stop,
441             seg->time));
442
443     selpad->segment_pending = FALSE;
444   }
445
446   /* forward */
447   GST_DEBUG_OBJECT (sel, "Forwarding buffer %p from pad %s:%s", buf,
448       GST_DEBUG_PAD_NAME (pad));
449   res = gst_pad_push (sel->srcpad, buf);
450 done:
451   gst_object_unref (sel);
452   return res;
453   /* dropped buffers */
454 ignore:
455   {
456     GST_DEBUG_OBJECT (sel, "Ignoring buffer %p from pad %s:%s",
457         buf, GST_DEBUG_PAD_NAME (pad));
458     gst_buffer_unref (buf);
459     res = GST_FLOW_OK;
460     goto done;
461   }
462 }
463
464 static void gst_input_selector_dispose (GObject * object);
465 static void gst_input_selector_init (GstInputSelector * sel);
466 static void gst_input_selector_base_init (GstInputSelectorClass * klass);
467 static void gst_input_selector_class_init (GstInputSelectorClass * klass);
468 static void gst_input_selector_set_property (GObject * object,
469     guint prop_id, const GValue * value, GParamSpec * pspec);
470 static void gst_input_selector_get_property (GObject * object,
471     guint prop_id, GValue * value, GParamSpec * pspec);
472 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
473     GstPadTemplate * templ, const gchar * unused);
474 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
475 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
476     element, GstStateChange transition);
477 static GList *gst_input_selector_get_linked_pads (GstPad * pad);
478 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
479 static gint64 gst_input_selector_block (GstInputSelector * self);
480 static void gst_input_selector_switch (GstInputSelector * self,
481     const gchar * pad_name, gint64 stop_time, gint64 start_time);
482
483 static GstElementClass *parent_class = NULL;
484
485 GType
486 gst_input_selector_get_type (void)
487 {
488   static GType input_selector_type = 0;
489
490   if (!input_selector_type) {
491     static const GTypeInfo input_selector_info = {
492       sizeof (GstInputSelectorClass),
493       (GBaseInitFunc) gst_input_selector_base_init,
494       NULL,
495       (GClassInitFunc) gst_input_selector_class_init,
496       NULL,
497       NULL,
498       sizeof (GstInputSelector),
499       0,
500       (GInstanceInitFunc) gst_input_selector_init,
501     };
502     input_selector_type =
503         g_type_register_static (GST_TYPE_ELEMENT,
504         "GstInputSelector", &input_selector_info, 0);
505     GST_DEBUG_CATEGORY_INIT (input_selector_debug,
506         "input-selector", 0, "An input stream selector element");
507   }
508
509   return input_selector_type;
510 }
511
512 static void
513 gst_input_selector_base_init (GstInputSelectorClass * klass)
514 {
515   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
516
517   gst_element_class_set_details (element_class, &gst_input_selector_details);
518   gst_element_class_add_pad_template (element_class,
519       gst_static_pad_template_get (&gst_input_selector_sink_factory));
520   gst_element_class_add_pad_template (element_class,
521       gst_static_pad_template_get (&gst_input_selector_src_factory));
522 }
523
524 static void
525 gst_input_selector_class_init (GstInputSelectorClass * klass)
526 {
527   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
528   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
529
530   parent_class = g_type_class_peek_parent (klass);
531   gobject_class->set_property =
532       GST_DEBUG_FUNCPTR (gst_input_selector_set_property);
533   gobject_class->get_property =
534       GST_DEBUG_FUNCPTR (gst_input_selector_get_property);
535   g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
536       g_param_spec_string ("active-pad", "Active pad",
537           "Name of the currently" " active sink pad", NULL, G_PARAM_READWRITE));
538   gobject_class->dispose = gst_input_selector_dispose;
539   gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
540   gstelement_class->release_pad = gst_input_selector_release_pad;
541   gstelement_class->change_state = gst_input_selector_change_state;
542
543   /**
544    * GstInputSelector::block:
545    * @inputselector: the #GstInputSelector
546    *
547    * Block all sink pads in preparation for a switch. Returns the stop time of
548    * the current switch segment, as a running time, or 0 if there is no current
549    * active pad or the current active pad never received data.
550    */
551   gst_input_selector_signals[SIGNAL_BLOCK] =
552       g_signal_new ("block", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
553       G_STRUCT_OFFSET (GstInputSelectorClass, block),
554       NULL, NULL, gst_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
555   /**
556    * GstInputSelector::switch:
557    * @inputselector: the #GstInputSelector
558    * @pad:            name of pad to switch to
559    * @stop_time:      running time at which to close the previous segment, or -1
560    *                  to use the running time of the previously active sink pad
561    * @start_time:     running time at which to start the new segment, or -1 to
562    *                  use the running time of the newly active sink pad
563    *
564    * Switch to a new feed. The segment opened by the previously active pad, if
565    * any, will be closed, and a new segment opened before data flows again.
566    *
567    * This signal must be emitted when the element has been blocked via the <link
568    * linkend="GstInputSelector-block">block</link> signal.
569    *
570    * If you have a stream with only one switch element, such as an audio-only
571    * stream, a stream switch should be performed by first emitting the block
572    * signal, and then emitting the switch signal with -1 for the stop and start
573    * time values.
574    *
575    * The intention of the @stop_time and @start_time arguments is to allow
576    * multiple switch elements to switch and maintain stream synchronization.
577    * When switching a stream with multiple feeds, you will need as many switch
578    * elements as you have feeds. For example, a feed with audio and video will
579    * have one switch element between the audio feeds and one for video.
580    *
581    * A switch over multiple switch elements should be performed as follows:
582    * First, emit the <link linkend="GstInputSelector-block">block</link>
583    * signal, collecting the returned values. The maximum running time returned
584    * by block should then be used as the time at which to close the previous
585    * segment.
586    *
587    * Then, query the running times of the new audio and video pads that you will
588    * switch to. Naturally, these pads are on separate switch elements. Take the
589    * minimum running time for those streams and use it for the time at which to
590    * open the new segment.
591    *
592    * If @pad is the same as the current active pad, the element will cancel any
593    * previous block without adjusting segments.
594    */
595   gst_input_selector_signals[SIGNAL_SWITCH] =
596       g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
597       G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
598       NULL, NULL, gst_selector_marshal_VOID__STRING_INT64_INT64,
599       G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_INT64, G_TYPE_INT64);
600
601   klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
602   klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
603 }
604
605 static void
606 gst_input_selector_init (GstInputSelector * sel)
607 {
608   sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
609   gst_pad_set_internal_link_function (sel->srcpad,
610       GST_DEBUG_FUNCPTR (gst_input_selector_get_linked_pads));
611   gst_pad_set_getcaps_function (sel->srcpad,
612       GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
613   gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
614   /* sinkpad management */
615   sel->active_sinkpad = NULL;
616   sel->nb_sinkpads = 0;
617   gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
618
619   sel->blocked_cond = g_cond_new ();
620   sel->blocked = FALSE;
621 }
622
623 static void
624 gst_input_selector_dispose (GObject * object)
625 {
626   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
627
628   if (sel->active_sinkpad) {
629     gst_object_unref (sel->active_sinkpad);
630     sel->active_sinkpad = NULL;
631   }
632
633   if (sel->blocked_cond) {
634     g_cond_free (sel->blocked_cond);
635     sel->blocked_cond = NULL;
636   }
637
638   G_OBJECT_CLASS (parent_class)->dispose (object);
639 }
640
641 /* Solve the following equation for B.timestamp, and set that as the segment
642  * stop:
643  * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
644  */
645 static gint64
646 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
647 {
648   return (running_time - segment->accum) * segment->abs_rate + segment->start;
649 }
650
651 static void
652 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
653 {
654   segment->stop = gst_segment_get_timestamp (segment, running_time);
655   segment->last_stop = -1;
656 }
657
658 static void
659 gst_segment_set_start (GstSegment * segment, gint64 running_time)
660 {
661   segment->start = gst_segment_get_timestamp (segment, running_time);
662 }
663
664 static void
665 gst_input_selector_set_active_pad (GstInputSelector * self,
666     const gchar * pad_name, gint64 stop_time, gint64 start_time)
667 {
668   GstPad *pad;
669   GstSelectorPad *old, *new;
670   GstPad **active_pad_p;
671
672   if (strcmp (pad_name, "") != 0)
673     pad = gst_element_get_pad (GST_ELEMENT (self), pad_name);
674   else
675     pad = NULL;
676
677   GST_OBJECT_LOCK (self);
678
679   if (pad == self->active_sinkpad)
680     goto done;
681
682   old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
683   new = GST_SELECTOR_PAD_CAST (pad);
684
685   if (old && old->active && !self->pending_stop && stop_time >= 0) {
686     /* schedule a last_stop update if one isn't already scheduled, and a
687        segment has been pushed before. */
688     memcpy (&self->pending_stop_segment, &old->segment,
689         sizeof (self->pending_stop_segment));
690     gst_segment_set_stop (&self->pending_stop_segment, stop_time);
691     self->pending_stop = TRUE;
692   }
693
694   if (new && new->active && start_time >= 0) {
695     /* schedule a new segment push */
696     gst_segment_set_start (&new->segment, start_time);
697     new->segment_pending = TRUE;
698   }
699
700   active_pad_p = &self->active_sinkpad;
701   gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
702   GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
703       self->active_sinkpad);
704
705 done:
706   GST_OBJECT_UNLOCK (self);
707
708   if (pad)
709     gst_object_unref (pad);
710 }
711
712
713 static void
714 gst_input_selector_set_property (GObject * object, guint prop_id,
715     const GValue * value, GParamSpec * pspec)
716 {
717   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
718
719   switch (prop_id) {
720     case PROP_ACTIVE_PAD:
721       gst_input_selector_set_active_pad (sel,
722           g_value_get_string (value), GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
723       break;
724     default:
725       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
726       break;
727   }
728 }
729
730 static void
731 gst_input_selector_get_property (GObject * object, guint prop_id,
732     GValue * value, GParamSpec * pspec)
733 {
734   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
735
736   switch (prop_id) {
737     case PROP_ACTIVE_PAD:{
738       GST_OBJECT_LOCK (object);
739       if (sel->active_sinkpad != NULL) {
740         g_value_take_string (value, gst_pad_get_name (sel->active_sinkpad));
741       } else {
742         g_value_set_string (value, "");
743       }
744       GST_OBJECT_UNLOCK (object);
745       break;
746     }
747     default:
748       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
749       break;
750   }
751 }
752
753 static GstPad *
754 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
755 {
756   GstInputSelector *sel;
757   GstPad *otherpad = NULL;
758
759   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
760   GST_OBJECT_LOCK (sel);
761   if (pad == sel->srcpad)
762     otherpad = sel->active_sinkpad;
763   else if (pad == sel->active_sinkpad || !strict)
764     otherpad = sel->srcpad;
765   if (otherpad)
766     gst_object_ref (otherpad);
767   GST_OBJECT_UNLOCK (sel);
768   gst_object_unref (sel);
769   return otherpad;
770 }
771
772 static GstCaps *
773 gst_input_selector_getcaps (GstPad * pad)
774 {
775   GstPad *otherpad;
776   GstObject *parent;
777   GstCaps *caps;
778
779   otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
780   parent = gst_object_get_parent (GST_OBJECT (pad));
781   if (!otherpad) {
782     GST_DEBUG_OBJECT (parent,
783         "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
784     caps = gst_caps_new_any ();
785   } else {
786     GST_DEBUG_OBJECT (parent,
787         "Pad %s:%s is linked (to %s:%s), returning peer caps",
788         GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
789     /* if the peer has caps, use those. If the pad is not linked, this function
790      * returns NULL and we return ANY */
791     if (!(caps = gst_pad_peer_get_caps (otherpad)))
792       caps = gst_caps_new_any ();
793     gst_object_unref (otherpad);
794   }
795
796   gst_object_unref (parent);
797   return caps;
798 }
799
800 /* check if the pad is the active sinkpad */
801 static gboolean
802 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
803 {
804   GstSelectorPad *selpad;
805   gboolean res;
806
807   selpad = GST_SELECTOR_PAD_CAST (pad);
808
809   GST_OBJECT_LOCK (sel);
810   res = (pad == sel->active_sinkpad);
811   GST_OBJECT_UNLOCK (sel);
812
813   return res;
814 }
815
816 /* Get or create the active sinkpad */
817 static GstPad *
818 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
819 {
820   GstPad *active_sinkpad;
821   GstSelectorPad *selpad;
822
823   selpad = GST_SELECTOR_PAD_CAST (pad);
824
825   GST_OBJECT_LOCK (sel);
826   selpad->active = TRUE;
827   active_sinkpad = sel->active_sinkpad;
828   if (active_sinkpad == NULL) {
829     /* first pad we get an alloc on becomes the activated pad by default */
830     active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
831     GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
832   }
833   GST_OBJECT_UNLOCK (sel);
834
835   return active_sinkpad;
836 }
837
838 static GList *
839 gst_input_selector_get_linked_pads (GstPad * pad)
840 {
841   GstPad *otherpad;
842
843   otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
844   if (!otherpad)
845     return NULL;
846   /* need to drop the ref, internal linked pads is not MT safe */
847   gst_object_unref (otherpad);
848   return g_list_append (NULL, otherpad);
849 }
850
851 static GstPad *
852 gst_input_selector_request_new_pad (GstElement * element,
853     GstPadTemplate * templ, const gchar * unused)
854 {
855   GstInputSelector *sel;
856   gchar *name = NULL;
857   GstPad *sinkpad = NULL;
858
859   sel = GST_INPUT_SELECTOR (element);
860   g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
861   GST_LOG_OBJECT (sel, "Creating new pad %d", sel->nb_sinkpads);
862   GST_OBJECT_LOCK (sel);
863   name = g_strdup_printf ("sink%d", sel->nb_sinkpads++);
864   sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
865       "name", name, "direction", templ->direction, "template", templ, NULL);
866   g_free (name);
867   GST_OBJECT_UNLOCK (sel);
868
869   gst_pad_set_event_function (sinkpad,
870       GST_DEBUG_FUNCPTR (gst_selector_pad_event));
871   gst_pad_set_getcaps_function (sinkpad,
872       GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
873   gst_pad_set_chain_function (sinkpad,
874       GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
875   gst_pad_set_internal_link_function (sinkpad,
876       GST_DEBUG_FUNCPTR (gst_selector_pad_get_linked_pads));
877   gst_pad_set_bufferalloc_function (sinkpad,
878       GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
879
880   gst_pad_set_active (sinkpad, TRUE);
881   gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
882   return sinkpad;
883 }
884
885 static void
886 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
887 {
888   GstInputSelector *sel;
889
890   sel = GST_INPUT_SELECTOR (element);
891   GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
892
893   GST_OBJECT_LOCK (sel);
894   /* if the pad was the active pad, makes us select a new one */
895   if (sel->active_sinkpad == pad) {
896     GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
897     sel->active_sinkpad = NULL;
898   }
899   GST_OBJECT_UNLOCK (sel);
900
901   gst_pad_set_active (pad, FALSE);
902   gst_element_remove_pad (GST_ELEMENT (sel), pad);
903 }
904
905 static GstStateChangeReturn
906 gst_input_selector_change_state (GstElement * element,
907     GstStateChange transition)
908 {
909   GstInputSelector *self = GST_INPUT_SELECTOR (element);
910   GstStateChangeReturn result;
911
912   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
913
914   if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
915     GST_OBJECT_LOCK (self);
916     self->blocked = FALSE;
917     g_cond_broadcast (self->blocked_cond);
918     GST_OBJECT_UNLOCK (self);
919   }
920
921   return result;
922 }
923
924 static gint64
925 gst_input_selector_block (GstInputSelector * self)
926 {
927   gint64 ret = 0;
928   GstSelectorPad *spad;
929
930   GST_OBJECT_LOCK (self);
931
932   if (self->blocked)
933     GST_WARNING_OBJECT (self, "switch already blocked");
934
935   self->blocked = TRUE;
936   spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
937
938   if (self->active_sinkpad)
939     ret = gst_selector_pad_get_running_time (spad);
940   else
941     GST_DEBUG_OBJECT (self, "no active pad while blocking");
942
943   GST_OBJECT_UNLOCK (self);
944
945   return ret;
946 }
947
948 static void
949 gst_input_selector_push_pending_stop (GstInputSelector * self)
950 {
951   GstEvent *event = NULL;
952
953   GST_OBJECT_LOCK (self);
954
955   if (G_UNLIKELY (self->pending_stop)) {
956     GstSegment *seg = &self->pending_stop_segment;
957
958     event = gst_event_new_new_segment_full (TRUE, seg->rate,
959         seg->applied_rate, seg->format, seg->start, seg->stop, seg->stop);
960
961     self->pending_stop = FALSE;
962   }
963
964   GST_OBJECT_UNLOCK (self);
965
966   if (event)
967     gst_pad_push_event (self->srcpad, event);
968 }
969
970 /* stop_time and start_time are running times */
971 static void
972 gst_input_selector_switch (GstInputSelector * self, const gchar * pad_name,
973     gint64 stop_time, gint64 start_time)
974 {
975   g_return_if_fail (self->blocked == TRUE);
976
977   gst_input_selector_set_active_pad (self, pad_name, stop_time, start_time);
978
979   GST_OBJECT_LOCK (self);
980   self->blocked = FALSE;
981   g_cond_broadcast (self->blocked_cond);
982   GST_OBJECT_UNLOCK (self);
983 }