Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / gst / playback / gststreamselector.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  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <string.h>
28
29 #include "gststreamselector.h"
30
31 GST_DEBUG_CATEGORY_STATIC (stream_selector_debug);
32 #define GST_CAT_DEFAULT stream_selector_debug
33
34 static GstStaticPadTemplate gst_stream_selector_sink_factory =
35 GST_STATIC_PAD_TEMPLATE ("sink%d",
36     GST_PAD_SINK,
37     GST_PAD_REQUEST,
38     GST_STATIC_CAPS_ANY);
39
40 static GstStaticPadTemplate gst_stream_selector_src_factory =
41 GST_STATIC_PAD_TEMPLATE ("src",
42     GST_PAD_SRC,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS_ANY);
45
46 enum
47 {
48   PROP_0,
49   PROP_N_PADS,
50   PROP_ACTIVE_PAD,
51   PROP_LAST
52 };
53
54 static gboolean gst_stream_selector_is_active_sinkpad (GstStreamSelector * sel,
55     GstPad * pad);
56 static GstPad *gst_stream_selector_activate_sinkpad (GstStreamSelector * sel,
57     GstPad * pad);
58 static GstPad *gst_stream_selector_get_linked_pad (GstPad * pad,
59     gboolean strict);
60
61 #define GST_TYPE_SELECTOR_PAD \
62   (gst_selector_pad_get_type())
63 #define GST_SELECTOR_PAD(obj) \
64   (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
65 #define GST_SELECTOR_PAD_CLASS(klass) \
66   (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
67 #define GST_IS_SELECTOR_PAD(obj) \
68   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
69 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
70   (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
71 #define GST_SELECTOR_PAD_CAST(obj) \
72   ((GstSelectorPad *)(obj))
73
74 typedef struct _GstSelectorPad GstSelectorPad;
75 typedef struct _GstSelectorPadClass GstSelectorPadClass;
76
77 struct _GstSelectorPad
78 {
79   GstPad parent;
80
81   gboolean active;
82   gboolean eos;
83   gboolean segment_pending;
84   GstSegment segment;
85   GstTagList *tags;
86 };
87
88 struct _GstSelectorPadClass
89 {
90   GstPadClass parent;
91 };
92
93 static void gst_selector_pad_class_init (GstSelectorPadClass * klass);
94 static void gst_selector_pad_init (GstSelectorPad * pad);
95 static void gst_selector_pad_finalize (GObject * object);
96
97 static void gst_selector_pad_get_property (GObject * object,
98     guint prop_id, GValue * value, GParamSpec * pspec);
99
100 static GstPadClass *selector_pad_parent_class = NULL;
101
102 static void gst_selector_pad_reset (GstSelectorPad * pad);
103 static gboolean gst_selector_pad_event (GstPad * pad, GstEvent * event);
104 static GstCaps *gst_selector_pad_getcaps (GstPad * pad);
105 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstBuffer * buf);
106 static GstFlowReturn gst_selector_pad_bufferalloc (GstPad * pad,
107     guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
108
109 enum
110 {
111   PROP_PAD_0,
112   PROP_PAD_TAGS,
113   PROP_PAD_ACTIVE,
114   PROP_PAD_LAST
115 };
116
117 static GType
118 gst_selector_pad_get_type (void)
119 {
120   static GType selector_pad_type = 0;
121
122   if (!selector_pad_type) {
123     static const GTypeInfo selector_pad_info = {
124       sizeof (GstSelectorPadClass),
125       NULL,
126       NULL,
127       (GClassInitFunc) gst_selector_pad_class_init,
128       NULL,
129       NULL,
130       sizeof (GstSelectorPad),
131       0,
132       (GInstanceInitFunc) gst_selector_pad_init,
133     };
134
135     selector_pad_type =
136         g_type_register_static (GST_TYPE_PAD, "GstPlaybinSelectorPad",
137         &selector_pad_info, 0);
138   }
139   return selector_pad_type;
140 }
141
142 static void
143 gst_selector_pad_class_init (GstSelectorPadClass * klass)
144 {
145   GObjectClass *gobject_class;
146
147   gobject_class = (GObjectClass *) klass;
148
149   selector_pad_parent_class = g_type_class_peek_parent (klass);
150
151   gobject_class->finalize = gst_selector_pad_finalize;
152   gobject_class->get_property = gst_selector_pad_get_property;
153
154   g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
155       g_param_spec_boxed ("tags", "Tags",
156           "The currently active tags on the pad", GST_TYPE_TAG_LIST,
157           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
158
159   g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
160       g_param_spec_boolean ("active", "Active",
161           "If the pad is currently active", FALSE,
162           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
163 }
164
165 static void
166 gst_selector_pad_init (GstSelectorPad * pad)
167 {
168   gst_selector_pad_reset (pad);
169 }
170
171 static void
172 gst_selector_pad_finalize (GObject * object)
173 {
174   GstSelectorPad *pad;
175
176   pad = GST_SELECTOR_PAD_CAST (object);
177
178   if (pad->tags)
179     gst_tag_list_free (pad->tags);
180
181   G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
182 }
183
184 static void
185 gst_selector_pad_get_property (GObject * object,
186     guint prop_id, GValue * value, GParamSpec * pspec)
187 {
188   GstSelectorPad *pad;
189
190   pad = GST_SELECTOR_PAD (object);
191
192   switch (prop_id) {
193     case PROP_PAD_TAGS:
194       GST_OBJECT_LOCK (object);
195       g_value_set_boxed (value, pad->tags);
196       GST_OBJECT_UNLOCK (object);
197       break;
198     case PROP_PAD_ACTIVE:
199     {
200       GstStreamSelector *sel;
201
202       sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
203       g_value_set_boolean (value, gst_stream_selector_is_active_sinkpad (sel,
204               GST_PAD_CAST (pad)));
205       gst_object_unref (sel);
206       break;
207     }
208     default:
209       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
210       break;
211   }
212 }
213
214 static void
215 gst_selector_pad_reset (GstSelectorPad * pad)
216 {
217   pad->active = FALSE;
218   pad->eos = FALSE;
219   gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
220 }
221
222 static gboolean
223 gst_selector_pad_event (GstPad * pad, GstEvent * event)
224 {
225   gboolean res = TRUE;
226   gboolean forward = TRUE;
227   GstStreamSelector *sel;
228   GstSelectorPad *selpad;
229   GstPad *active_sinkpad;
230
231   sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
232   selpad = GST_SELECTOR_PAD_CAST (pad);
233
234   /* only forward if we are dealing with the active sinkpad */
235   active_sinkpad = gst_stream_selector_activate_sinkpad (sel, pad);
236   forward = (active_sinkpad == pad);
237
238   switch (GST_EVENT_TYPE (event)) {
239     case GST_EVENT_FLUSH_STOP:
240       gst_selector_pad_reset (selpad);
241       break;
242     case GST_EVENT_NEWSEGMENT:
243     {
244       gboolean update;
245       GstFormat format;
246       gdouble rate, arate;
247       gint64 start, stop, time;
248
249       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
250           &start, &stop, &time);
251
252       GST_DEBUG_OBJECT (selpad,
253           "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
254           "format %d, "
255           "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
256           G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
257
258       gst_segment_set_newsegment_full (&selpad->segment, update,
259           rate, arate, format, start, stop, time);
260       /* if we are not going to forward the segment, mark the segment as
261        * pending */
262       if (!forward)
263         selpad->segment_pending = TRUE;
264       break;
265     }
266     case GST_EVENT_TAG:
267     {
268       GstTagList *tags;
269
270       GST_OBJECT_LOCK (selpad);
271       if (selpad->tags)
272         gst_tag_list_free (selpad->tags);
273       gst_event_parse_tag (event, &tags);
274       if (tags)
275         tags = gst_tag_list_copy (tags);
276       selpad->tags = tags;
277       GST_DEBUG_OBJECT (sel, "received tags %" GST_PTR_FORMAT, selpad->tags);
278       GST_OBJECT_UNLOCK (selpad);
279       break;
280     }
281     case GST_EVENT_EOS:
282       selpad->eos = TRUE;
283       break;
284     default:
285       break;
286   }
287   if (forward)
288     res = gst_pad_push_event (sel->srcpad, event);
289   else
290     gst_event_unref (event);
291
292   gst_object_unref (sel);
293
294   return res;
295 }
296
297 static GstCaps *
298 gst_selector_pad_getcaps (GstPad * pad)
299 {
300   GstStreamSelector *sel;
301   GstCaps *caps;
302
303   sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
304
305   GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
306   caps = gst_pad_peer_get_caps (sel->srcpad);
307   if (caps == NULL)
308     caps = gst_caps_new_any ();
309
310   gst_object_unref (sel);
311
312   return caps;
313 }
314
315 static GstFlowReturn
316 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
317     guint size, GstCaps * caps, GstBuffer ** buf)
318 {
319   GstStreamSelector *sel;
320   GstFlowReturn result;
321   GstPad *active_sinkpad;
322
323   sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
324
325   active_sinkpad = gst_stream_selector_activate_sinkpad (sel, pad);
326
327   /* Fallback allocation for buffers from pads except the selected one */
328   if (pad != active_sinkpad) {
329     GST_DEBUG_OBJECT (sel,
330         "Pad %s:%s is not selected. Performing fallback allocation",
331         GST_DEBUG_PAD_NAME (pad));
332
333     *buf = NULL;
334     result = GST_FLOW_OK;
335   } else {
336     result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
337
338     /* FIXME: HACK. If buffer alloc returns not-linked, perform a fallback
339      * allocation.  This should NOT be necessary, because playbin should
340      * properly block the source pad from running until it's finished hooking 
341      * everything up, but playbin needs refactoring first. */
342     if (result == GST_FLOW_NOT_LINKED) {
343       GST_DEBUG_OBJECT (sel,
344           "No peer pad yet - performing fallback allocation for pad %s:%s",
345           GST_DEBUG_PAD_NAME (pad));
346
347       *buf = NULL;
348       result = GST_FLOW_OK;
349     }
350   }
351
352   gst_object_unref (sel);
353
354   return result;
355 }
356
357 static GstFlowReturn
358 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
359 {
360   GstStreamSelector *sel;
361   GstFlowReturn res;
362   GstPad *active_sinkpad;
363   GstSelectorPad *selpad;
364   GstClockTime timestamp;
365   GstSegment *seg;
366
367   sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
368   selpad = GST_SELECTOR_PAD_CAST (pad);
369   seg = &selpad->segment;
370
371   active_sinkpad = gst_stream_selector_activate_sinkpad (sel, pad);
372
373   timestamp = GST_BUFFER_TIMESTAMP (buf);
374   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
375     GST_DEBUG_OBJECT (sel, "received timestamp %" GST_TIME_FORMAT,
376         GST_TIME_ARGS (timestamp));
377     gst_segment_set_last_stop (seg, seg->format, timestamp);
378   }
379
380   /* Ignore buffers from pads except the selected one */
381   if (pad != active_sinkpad)
382     goto ignore;
383
384   /* if we have a pending segment, push it out now */
385   if (selpad->segment_pending) {
386     gst_pad_push_event (sel->srcpad, gst_event_new_new_segment_full (FALSE,
387             seg->rate, seg->applied_rate, seg->format, seg->start, seg->stop,
388             seg->time));
389
390     selpad->segment_pending = FALSE;
391   }
392
393   /* forward */
394   GST_DEBUG_OBJECT (sel, "Forwarding buffer %p from pad %s:%s", buf,
395       GST_DEBUG_PAD_NAME (pad));
396   res = gst_pad_push (sel->srcpad, buf);
397 done:
398   gst_object_unref (sel);
399   return res;
400   /* dropped buffers */
401 ignore:
402   {
403     GST_DEBUG_OBJECT (sel, "Ignoring buffer %p from pad %s:%s",
404         buf, GST_DEBUG_PAD_NAME (pad));
405     gst_buffer_unref (buf);
406     res = GST_FLOW_NOT_LINKED;
407     goto done;
408   }
409 }
410
411 static void gst_stream_selector_dispose (GObject * object);
412
413 static void gst_stream_selector_init (GstStreamSelector * sel);
414 static void gst_stream_selector_base_init (GstStreamSelectorClass * klass);
415 static void gst_stream_selector_class_init (GstStreamSelectorClass * klass);
416
417 static void gst_stream_selector_set_property (GObject * object,
418     guint prop_id, const GValue * value, GParamSpec * pspec);
419 static void gst_stream_selector_get_property (GObject * object,
420     guint prop_id, GValue * value, GParamSpec * pspec);
421
422 static GstPad *gst_stream_selector_request_new_pad (GstElement * element,
423     GstPadTemplate * templ, const gchar * unused);
424 static void gst_stream_selector_release_pad (GstElement * element,
425     GstPad * pad);
426 static GstIterator *gst_stream_selector_pad_iterate_linked_pads (GstPad * pad);
427 static GstCaps *gst_stream_selector_getcaps (GstPad * pad);
428
429 static GstElementClass *parent_class = NULL;
430
431 GType
432 gst_stream_selector_get_type (void)
433 {
434   static GType stream_selector_type = 0;
435
436   if (!stream_selector_type) {
437     static const GTypeInfo stream_selector_info = {
438       sizeof (GstStreamSelectorClass),
439       (GBaseInitFunc) gst_stream_selector_base_init,
440       NULL,
441       (GClassInitFunc) gst_stream_selector_class_init,
442       NULL,
443       NULL,
444       sizeof (GstStreamSelector),
445       0,
446       (GInstanceInitFunc) gst_stream_selector_init,
447     };
448     stream_selector_type =
449         g_type_register_static (GST_TYPE_ELEMENT,
450         "GstStreamSelector", &stream_selector_info, 0);
451     GST_DEBUG_CATEGORY_INIT (stream_selector_debug,
452         "streamselector", 0, "A stream-selector element");
453   }
454
455   return stream_selector_type;
456 }
457
458 static void
459 gst_stream_selector_base_init (GstStreamSelectorClass * klass)
460 {
461   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
462
463   gst_element_class_set_details_simple (element_class,
464       "StreamSelector", "Generic",
465       "N-to-1 input stream_selectoring",
466       "Julien Moutte <julien@moutte.net>, "
467       "Jan Schmidt <thaytan@mad.scientist.com>, "
468       "Wim Taymans <wim.taymans@gmail.com>");
469   gst_element_class_add_pad_template (element_class,
470       gst_static_pad_template_get (&gst_stream_selector_sink_factory));
471   gst_element_class_add_pad_template (element_class,
472       gst_static_pad_template_get (&gst_stream_selector_src_factory));
473 }
474
475 static void
476 gst_stream_selector_class_init (GstStreamSelectorClass * klass)
477 {
478   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
479   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
480
481   parent_class = g_type_class_peek_parent (klass);
482
483   gobject_class->dispose = gst_stream_selector_dispose;
484
485   gobject_class->set_property = gst_stream_selector_set_property;
486   gobject_class->get_property = gst_stream_selector_get_property;
487
488   g_object_class_install_property (gobject_class, PROP_N_PADS,
489       g_param_spec_uint ("n-pads", "Number of Pads",
490           "The number of sink pads", 0, G_MAXUINT, 0,
491           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
492   g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
493       g_param_spec_object ("active-pad", "Active Pad",
494           "The currently active sink pad", GST_TYPE_PAD,
495           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
496
497   gstelement_class->request_new_pad = gst_stream_selector_request_new_pad;
498   gstelement_class->release_pad = gst_stream_selector_release_pad;
499 }
500
501 static void
502 gst_stream_selector_init (GstStreamSelector * sel)
503 {
504   sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
505   gst_pad_set_iterate_internal_links_function (sel->srcpad,
506       GST_DEBUG_FUNCPTR (gst_stream_selector_pad_iterate_linked_pads));
507   gst_pad_set_getcaps_function (sel->srcpad,
508       GST_DEBUG_FUNCPTR (gst_stream_selector_getcaps));
509   gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
510   /* sinkpad management */
511   sel->padcount = 0;
512   sel->active_sinkpad = NULL;
513   gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
514 }
515
516 static void
517 gst_stream_selector_dispose (GObject * object)
518 {
519   GstStreamSelector *sel = GST_STREAM_SELECTOR (object);
520
521   if (sel->active_sinkpad) {
522     gst_object_unref (sel->active_sinkpad);
523     sel->active_sinkpad = NULL;
524   }
525
526   G_OBJECT_CLASS (parent_class)->dispose (object);
527 }
528
529 static void
530 gst_stream_selector_set_property (GObject * object, guint prop_id,
531     const GValue * value, GParamSpec * pspec)
532 {
533   GstStreamSelector *sel = GST_STREAM_SELECTOR (object);
534
535   switch (prop_id) {
536     case PROP_ACTIVE_PAD:
537     {
538       GstPad *pad = NULL;
539       GstPad **active_pad_p;
540
541       pad = g_value_get_object (value);
542
543       GST_OBJECT_LOCK (object);
544       if (pad != sel->active_sinkpad) {
545         GstSelectorPad *selpad;
546
547         selpad = GST_SELECTOR_PAD_CAST (pad);
548         /* we can only activate pads that have data received */
549         if (selpad && !selpad->active) {
550           GST_DEBUG_OBJECT (sel, "No data received on pad %" GST_PTR_FORMAT,
551               pad);
552         } else {
553           active_pad_p = &sel->active_sinkpad;
554           gst_object_replace ((GstObject **) active_pad_p,
555               GST_OBJECT_CAST (pad));
556           GST_DEBUG_OBJECT (sel, "New active pad is %" GST_PTR_FORMAT,
557               sel->active_sinkpad);
558         }
559       }
560       GST_OBJECT_UNLOCK (object);
561       break;
562     }
563     default:
564       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
565       break;
566   }
567 }
568
569 static void
570 gst_stream_selector_get_property (GObject * object, guint prop_id,
571     GValue * value, GParamSpec * pspec)
572 {
573   GstStreamSelector *sel = GST_STREAM_SELECTOR (object);
574
575   switch (prop_id) {
576     case PROP_N_PADS:
577       GST_OBJECT_LOCK (object);
578       g_value_set_uint (value, sel->n_pads);
579       GST_OBJECT_UNLOCK (object);
580       break;
581     case PROP_ACTIVE_PAD:{
582       GST_OBJECT_LOCK (object);
583       g_value_set_object (value, sel->active_sinkpad);
584       GST_OBJECT_UNLOCK (object);
585       break;
586     }
587     default:
588       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
589       break;
590   }
591 }
592
593 static GstPad *
594 gst_stream_selector_get_linked_pad (GstPad * pad, gboolean strict)
595 {
596   GstStreamSelector *sel;
597   GstPad *otherpad = NULL;
598
599   sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
600
601   GST_OBJECT_LOCK (sel);
602   if (pad == sel->srcpad)
603     otherpad = sel->active_sinkpad;
604   else if (pad == sel->active_sinkpad || !strict)
605     otherpad = sel->srcpad;
606   if (otherpad)
607     gst_object_ref (otherpad);
608   GST_OBJECT_UNLOCK (sel);
609   gst_object_unref (sel);
610   return otherpad;
611 }
612
613 static GstCaps *
614 gst_stream_selector_getcaps (GstPad * pad)
615 {
616   GstPad *otherpad;
617   GstObject *parent;
618   GstCaps *caps;
619
620   otherpad = gst_stream_selector_get_linked_pad (pad, FALSE);
621   parent = gst_object_get_parent (GST_OBJECT (pad));
622   if (!otherpad) {
623     GST_DEBUG_OBJECT (parent,
624         "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
625     caps = gst_caps_new_any ();
626   } else {
627     GST_DEBUG_OBJECT (parent,
628         "Pad %s:%s is linked (to %s:%s), returning peer caps",
629         GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
630     /* if the peer has caps, use those. If the pad is not linked, this function
631      * returns NULL and we return ANY */
632     if (!(caps = gst_pad_peer_get_caps (otherpad)))
633       caps = gst_caps_new_any ();
634     gst_object_unref (otherpad);
635   }
636
637   gst_object_unref (parent);
638   return caps;
639 }
640
641 /* check if the pad is the active sinkpad */
642 static gboolean
643 gst_stream_selector_is_active_sinkpad (GstStreamSelector * sel, GstPad * pad)
644 {
645   gboolean res;
646
647   GST_OBJECT_LOCK (sel);
648   res = (pad == sel->active_sinkpad);
649   GST_OBJECT_UNLOCK (sel);
650
651   return res;
652 }
653
654 /* Get or create the active sinkpad */
655 static GstPad *
656 gst_stream_selector_activate_sinkpad (GstStreamSelector * sel, GstPad * pad)
657 {
658   GstPad *active_sinkpad;
659   GstSelectorPad *selpad;
660
661   selpad = GST_SELECTOR_PAD_CAST (pad);
662
663   GST_OBJECT_LOCK (sel);
664   selpad->active = TRUE;
665   active_sinkpad = sel->active_sinkpad;
666   if (active_sinkpad == NULL) {
667     /* first pad we get an alloc on becomes the activated pad by default */
668     active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
669     GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
670   }
671   GST_OBJECT_UNLOCK (sel);
672
673   return active_sinkpad;
674 }
675
676 static GstIterator *
677 gst_stream_selector_pad_iterate_linked_pads (GstPad * pad)
678 {
679   GstStreamSelector *sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
680   GstPad *otherpad;
681   GstIterator *ret;
682
683   otherpad = gst_stream_selector_get_linked_pad (pad, TRUE);
684   ret =
685       gst_iterator_new_single (GST_TYPE_PAD, otherpad,
686       (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
687
688   if (otherpad)
689     gst_object_unref (otherpad);
690   gst_object_unref (sel);
691
692   return ret;
693 }
694
695 static GstPad *
696 gst_stream_selector_request_new_pad (GstElement * element,
697     GstPadTemplate * templ, const gchar * unused)
698 {
699   GstStreamSelector *sel;
700   gchar *name = NULL;
701   GstPad *sinkpad = NULL;
702
703   sel = GST_STREAM_SELECTOR (element);
704   g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
705   GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
706   GST_OBJECT_LOCK (sel);
707   name = g_strdup_printf ("sink%d", sel->padcount++);
708   sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
709       "name", name, "direction", templ->direction, "template", templ, NULL);
710   g_free (name);
711   sel->n_pads++;
712   GST_OBJECT_UNLOCK (sel);
713
714   gst_pad_set_event_function (sinkpad,
715       GST_DEBUG_FUNCPTR (gst_selector_pad_event));
716   gst_pad_set_getcaps_function (sinkpad,
717       GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
718   gst_pad_set_chain_function (sinkpad,
719       GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
720   gst_pad_set_iterate_internal_links_function (sinkpad,
721       GST_DEBUG_FUNCPTR (gst_stream_selector_pad_iterate_linked_pads));
722   gst_pad_set_bufferalloc_function (sinkpad,
723       GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
724
725   gst_pad_set_active (sinkpad, TRUE);
726   gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
727   return sinkpad;
728 }
729
730 static void
731 gst_stream_selector_release_pad (GstElement * element, GstPad * pad)
732 {
733   GstStreamSelector *sel;
734
735   sel = GST_STREAM_SELECTOR (element);
736   GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
737
738   GST_OBJECT_LOCK (sel);
739   /* if the pad was the active pad, makes us select a new one */
740   if (sel->active_sinkpad == pad) {
741     GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
742     sel->active_sinkpad = NULL;
743   }
744   sel->n_pads--;
745   GST_OBJECT_UNLOCK (sel);
746
747   gst_pad_set_active (pad, FALSE);
748   gst_element_remove_pad (GST_ELEMENT (sel), pad);
749 }