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