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