Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-base.git] / gst / playback / gstplaybin2.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  * Copyright (C) <2011> Sebastian Dröge <sebastian.droege@collabora.co.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * SECTION:element-playbin
23  *
24  * Playbin provides a stand-alone everything-in-one abstraction for an
25  * audio and/or video player.
26  *
27  * Playbin can handle both audio and video files and features
28  * <itemizedlist>
29  * <listitem>
30  * automatic file type recognition and based on that automatic
31  * selection and usage of the right audio/video/subtitle demuxers/decoders
32  * </listitem>
33  * <listitem>
34  * visualisations for audio files
35  * </listitem>
36  * <listitem>
37  * subtitle support for video files. Subtitles can be store in external
38  * files.
39  * </listitem>
40  * <listitem>
41  * stream selection between different video/audio/subtitles streams
42  * </listitem>
43  * <listitem>
44  * meta info (tag) extraction
45  * </listitem>
46  * <listitem>
47  * easy access to the last video sample
48  * </listitem>
49  * <listitem>
50  * buffering when playing streams over a network
51  * </listitem>
52  * <listitem>
53  * volume control with mute option
54  * </listitem>
55  * </itemizedlist>
56  *
57  * <refsect2>
58  * <title>Usage</title>
59  * <para>
60  * A playbin element can be created just like any other element using
61  * gst_element_factory_make(). The file/URI to play should be set via the #GstPlayBin:uri
62  * property. This must be an absolute URI, relative file paths are not allowed.
63  * Example URIs are file:///home/joe/movie.avi or http://www.joedoe.com/foo.ogg
64  *
65  * Playbin is a #GstPipeline. It will notify the application of everything
66  * that's happening (errors, end of stream, tags found, state changes, etc.)
67  * by posting messages on its #GstBus. The application needs to watch the
68  * bus.
69  *
70  * Playback can be initiated by setting the element to PLAYING state using
71  * gst_element_set_state(). Note that the state change will take place in
72  * the background in a separate thread, when the function returns playback
73  * is probably not happening yet and any errors might not have occured yet.
74  * Applications using playbin should ideally be written to deal with things
75  * completely asynchroneous.
76  *
77  * When playback has finished (an EOS message has been received on the bus)
78  * or an error has occured (an ERROR message has been received on the bus) or
79  * the user wants to play a different track, playbin should be set back to
80  * READY or NULL state, then the #GstPlayBin:uri property should be set to the
81  * new location and then playbin be set to PLAYING state again.
82  *
83  * Seeking can be done using gst_element_seek_simple() or gst_element_seek()
84  * on the playbin element. Again, the seek will not be executed
85  * instantaneously, but will be done in a background thread. When the seek
86  * call returns the seek will most likely still be in process. An application
87  * may wait for the seek to finish (or fail) using gst_element_get_state() with
88  * -1 as the timeout, but this will block the user interface and is not
89  * recommended at all.
90  *
91  * Applications may query the current position and duration of the stream
92  * via gst_element_query_position() and gst_element_query_duration() and
93  * setting the format passed to GST_FORMAT_TIME. If the query was successful,
94  * the duration or position will have been returned in units of nanoseconds.
95  * </para>
96  * </refsect2>
97  * <refsect2>
98  * <title>Advanced Usage: specifying the audio and video sink</title>
99  * <para>
100  * By default, if no audio sink or video sink has been specified via the
101  * #GstPlayBin:audio-sink or #GstPlayBin:video-sink property, playbin will use the autoaudiosink
102  * and autovideosink elements to find the first-best available output method.
103  * This should work in most cases, but is not always desirable. Often either
104  * the user or application might want to specify more explicitly what to use
105  * for audio and video output.
106  *
107  * If the application wants more control over how audio or video should be
108  * output, it may create the audio/video sink elements itself (for example
109  * using gst_element_factory_make()) and provide them to playbin using the
110  * #GstPlayBin:audio-sink or #GstPlayBin:video-sink property.
111  *
112  * GNOME-based applications, for example, will usually want to create
113  * gconfaudiosink and gconfvideosink elements and make playbin use those,
114  * so that output happens to whatever the user has configured in the GNOME
115  * Multimedia System Selector configuration dialog.
116  *
117  * The sink elements do not necessarily need to be ready-made sinks. It is
118  * possible to create container elements that look like a sink to playbin,
119  * but in reality contain a number of custom elements linked together. This
120  * can be achieved by creating a #GstBin and putting elements in there and
121  * linking them, and then creating a sink #GstGhostPad for the bin and pointing
122  * it to the sink pad of the first element within the bin. This can be used
123  * for a number of purposes, for example to force output to a particular
124  * format or to modify or observe the data before it is output.
125  *
126  * It is also possible to 'suppress' audio and/or video output by using
127  * 'fakesink' elements (or capture it from there using the fakesink element's
128  * "handoff" signal, which, nota bene, is fired from the streaming thread!).
129  * </para>
130  * </refsect2>
131  * <refsect2>
132  * <title>Retrieving Tags and Other Meta Data</title>
133  * <para>
134  * Most of the common meta data (artist, title, etc.) can be retrieved by
135  * watching for TAG messages on the pipeline's bus (see above).
136  *
137  * Other more specific meta information like width/height/framerate of video
138  * streams or samplerate/number of channels of audio streams can be obtained
139  * from the negotiated caps on the sink pads of the sinks.
140  * </para>
141  * </refsect2>
142  * <refsect2>
143  * <title>Buffering</title>
144  * Playbin handles buffering automatically for the most part, but applications
145  * need to handle parts of the buffering process as well. Whenever playbin is
146  * buffering, it will post BUFFERING messages on the bus with a percentage
147  * value that shows the progress of the buffering process. Applications need
148  * to set playbin to PLAYING or PAUSED state in response to these messages.
149  * They may also want to convey the buffering progress to the user in some
150  * way. Here is how to extract the percentage information from the message
151  * (requires GStreamer >= 0.10.11):
152  * |[
153  * switch (GST_MESSAGE_TYPE (msg)) {
154  *   case GST_MESSAGE_BUFFERING: {
155  *     gint percent = 0;
156  *     gst_message_parse_buffering (msg, &amp;percent);
157  *     g_print ("Buffering (%%u percent done)", percent);
158  *     break;
159  *   }
160  *   ...
161  * }
162  * ]|
163  * Note that applications should keep/set the pipeline in the PAUSED state when
164  * a BUFFERING message is received with a buffer percent value < 100 and set
165  * the pipeline back to PLAYING state when a BUFFERING message with a value
166  * of 100 percent is received (if PLAYING is the desired state, that is).
167  * </refsect2>
168  * <refsect2>
169  * <title>Embedding the video window in your application</title>
170  * By default, playbin (or rather the video sinks used) will create their own
171  * window. Applications will usually want to force output to a window of their
172  * own, however. This can be done using the #GstVideoOverlay interface, which most
173  * video sinks implement. See the documentation there for more details.
174  * </refsect2>
175  * <refsect2>
176  * <title>Specifying which CD/DVD device to use</title>
177  * The device to use for CDs/DVDs needs to be set on the source element
178  * playbin creates before it is opened. The most generic way of doing this
179  * is to connect to playbin's "source-setup" (or "notify::source") signal,
180  * which will be emitted by playbin2 when it has created the source element
181  * for a particular URI. In the signal callback you can check if the source
182  * element has a "device" property and set it appropriately. In some cases
183  * the device can also be set as part of the URI, but it depends on the
184  * elements involved if this will work or not. For example, for DVD menu
185  * playback, the following syntax might work (if the resindvd plugin is used):
186  * dvd://[/path/to/device]
187  * </refsect2>
188  * <refsect2>
189  * <title>Handling redirects</title>
190  * <para>
191  * Some elements may post 'redirect' messages on the bus to tell the
192  * application to open another location. These are element messages containing
193  * a structure named 'redirect' along with a 'new-location' field of string
194  * type. The new location may be a relative or an absolute URI. Examples
195  * for such redirects can be found in many quicktime movie trailers.
196  * </para>
197  * </refsect2>
198  * <refsect2>
199  * <title>Examples</title>
200  * |[
201  * gst-launch -v playbin2 uri=file:///path/to/somefile.avi
202  * ]| This will play back the given AVI video file, given that the video and
203  * audio decoders required to decode the content are installed. Since no
204  * special audio sink or video sink is supplied (not possible via gst-launch),
205  * playbin will try to find a suitable audio and video sink automatically
206  * using the autoaudiosink and autovideosink elements.
207  * |[
208  * gst-launch -v playbin2 uri=cdda://4
209  * ]| This will play back track 4 on an audio CD in your disc drive (assuming
210  * the drive is detected automatically by the plugin).
211  * |[
212  * gst-launch -v playbin2 uri=dvd://
213  * ]| This will play back the DVD in your disc drive (assuming
214  * the drive is detected automatically by the plugin).
215  * </refsect2>
216  */
217
218 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
219  * with newer GLib versions (>= 2.31.0) */
220 #define GLIB_DISABLE_DEPRECATION_WARNINGS
221
222 #ifdef HAVE_CONFIG_H
223 #include "config.h"
224 #endif
225
226 #include <string.h>
227 #include <gst/gst.h>
228
229 #include <gst/gst-i18n-plugin.h>
230 #include <gst/pbutils/pbutils.h>
231 #include <gst/audio/streamvolume.h>
232 #include <gst/video/videooverlay.h>
233 #include <gst/interfaces/navigation.h>
234 #include <gst/video/colorbalance.h>
235 #include "gstplay-enum.h"
236 #include "gstplay-marshal.h"
237 #include "gstplayback.h"
238 #include "gstplaysink.h"
239 #include "gstsubtitleoverlay.h"
240 #include "gst/glib-compat-private.h"
241 GST_DEBUG_CATEGORY_STATIC (gst_play_bin_debug);
242 #define GST_CAT_DEFAULT gst_play_bin_debug
243
244 #define GST_TYPE_PLAY_BIN               (gst_play_bin_get_type())
245 #define GST_PLAY_BIN(obj)               (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PLAY_BIN,GstPlayBin))
246 #define GST_PLAY_BIN_CLASS(klass)       (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PLAY_BIN,GstPlayBinClass))
247 #define GST_IS_PLAY_BIN(obj)            (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PLAY_BIN))
248 #define GST_IS_PLAY_BIN_CLASS(klass)    (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PLAY_BIN))
249
250 #define VOLUME_MAX_DOUBLE 10.0
251
252 typedef struct _GstPlayBin GstPlayBin;
253 typedef struct _GstPlayBinClass GstPlayBinClass;
254 typedef struct _GstSourceGroup GstSourceGroup;
255 typedef struct _GstSourceSelect GstSourceSelect;
256
257 typedef GstCaps *(*SourceSelectGetMediaCapsFunc) (void);
258
259 /* has the info for a selector and provides the link to the sink */
260 struct _GstSourceSelect
261 {
262   const gchar *media_list[8];   /* the media types for the selector */
263   SourceSelectGetMediaCapsFunc get_media_caps;  /* more complex caps for the selector */
264   GstPlaySinkType type;         /* the sink pad type of the selector */
265
266   GstElement *selector;         /* the selector */
267   GPtrArray *channels;
268   GstPad *srcpad;               /* the source pad of the selector */
269   GstPad *sinkpad;              /* the sinkpad of the sink when the selector
270                                  * is linked
271                                  */
272   GstEvent *sinkpad_delayed_event;
273   gulong sinkpad_data_probe;
274   gulong block_id;
275 };
276
277 #define GST_SOURCE_GROUP_GET_LOCK(group) (&((GstSourceGroup*)(group))->lock)
278 #define GST_SOURCE_GROUP_LOCK(group) (g_mutex_lock (GST_SOURCE_GROUP_GET_LOCK(group)))
279 #define GST_SOURCE_GROUP_UNLOCK(group) (g_mutex_unlock (GST_SOURCE_GROUP_GET_LOCK(group)))
280
281 enum
282 {
283   PLAYBIN_STREAM_AUDIO = 0,
284   PLAYBIN_STREAM_VIDEO,
285   PLAYBIN_STREAM_TEXT,
286   PLAYBIN_STREAM_LAST
287 };
288
289 /* a structure to hold the objects for decoding a uri and the subtitle uri
290  */
291 struct _GstSourceGroup
292 {
293   GstPlayBin *playbin;
294
295   GMutex lock;
296
297   gboolean valid;               /* the group has valid info to start playback */
298   gboolean active;              /* the group is active */
299
300   /* properties */
301   gchar *uri;
302   gchar *suburi;
303   GValueArray *streaminfo;
304   GstElement *source;
305
306   GPtrArray *video_channels;    /* links to selector pads */
307   GPtrArray *audio_channels;    /* links to selector pads */
308   GPtrArray *text_channels;     /* links to selector pads */
309
310   GstElement *audio_sink;       /* autoplugged audio and video sinks */
311   GstElement *video_sink;
312
313   /* uridecodebins for uri and subtitle uri */
314   GstElement *uridecodebin;
315   GstElement *suburidecodebin;
316   gint pending;
317   gboolean sub_pending;
318
319   gulong pad_added_id;
320   gulong pad_removed_id;
321   gulong no_more_pads_id;
322   gulong notify_source_id;
323   gulong drained_id;
324   gulong autoplug_factories_id;
325   gulong autoplug_select_id;
326   gulong autoplug_continue_id;
327
328   gulong sub_pad_added_id;
329   gulong sub_pad_removed_id;
330   gulong sub_no_more_pads_id;
331   gulong sub_autoplug_continue_id;
332
333   gulong block_id;
334
335   GMutex stream_changed_pending_lock;
336   GList *stream_changed_pending;
337
338   /* selectors for different streams */
339   GstSourceSelect selector[PLAYBIN_STREAM_LAST];
340 };
341
342 #define GST_PLAY_BIN_GET_LOCK(bin) (&((GstPlayBin*)(bin))->lock)
343 #define GST_PLAY_BIN_LOCK(bin) (g_rec_mutex_lock (GST_PLAY_BIN_GET_LOCK(bin)))
344 #define GST_PLAY_BIN_UNLOCK(bin) (g_rec_mutex_unlock (GST_PLAY_BIN_GET_LOCK(bin)))
345
346 /* lock to protect dynamic callbacks, like no-more-pads */
347 #define GST_PLAY_BIN_DYN_LOCK(bin)    g_mutex_lock (&(bin)->dyn_lock)
348 #define GST_PLAY_BIN_DYN_UNLOCK(bin)  g_mutex_unlock (&(bin)->dyn_lock)
349
350 /* lock for shutdown */
351 #define GST_PLAY_BIN_SHUTDOWN_LOCK(bin,label)           \
352 G_STMT_START {                                          \
353   if (G_UNLIKELY (g_atomic_int_get (&bin->shutdown)))   \
354     goto label;                                         \
355   GST_PLAY_BIN_DYN_LOCK (bin);                          \
356   if (G_UNLIKELY (g_atomic_int_get (&bin->shutdown))) { \
357     GST_PLAY_BIN_DYN_UNLOCK (bin);                      \
358     goto label;                                         \
359   }                                                     \
360 } G_STMT_END
361
362 /* unlock for shutdown */
363 #define GST_PLAY_BIN_SHUTDOWN_UNLOCK(bin)         \
364   GST_PLAY_BIN_DYN_UNLOCK (bin);                  \
365
366 /**
367  * GstPlayBin:
368  *
369  * playbin element structure
370  */
371 struct _GstPlayBin
372 {
373   GstPipeline parent;
374
375   GRecMutex lock;               /* to protect group switching */
376
377   /* the groups, we use a double buffer to switch between current and next */
378   GstSourceGroup groups[2];     /* array with group info */
379   GstSourceGroup *curr_group;   /* pointer to the currently playing group */
380   GstSourceGroup *next_group;   /* pointer to the next group */
381
382   /* properties */
383   guint64 connection_speed;     /* connection speed in bits/sec (0 = unknown) */
384   gint current_video;           /* the currently selected stream */
385   gint current_audio;           /* the currently selected stream */
386   gint current_text;            /* the currently selected stream */
387
388   guint64 buffer_duration;      /* When buffering, the max buffer duration (ns) */
389   guint buffer_size;            /* When buffering, the max buffer size (bytes) */
390
391   /* our play sink */
392   GstPlaySink *playsink;
393
394   /* the last activated source */
395   GstElement *source;
396
397   /* lock protecting dynamic adding/removing */
398   GMutex dyn_lock;
399   /* if we are shutting down or not */
400   gint shutdown;
401
402   GMutex elements_lock;
403   guint32 elements_cookie;
404   GList *elements;              /* factories we can use for selecting elements */
405
406   gboolean have_selector;       /* set to FALSE when we fail to create an
407                                  * input-selector, so that we only post a
408                                  * warning once */
409
410   GstElement *audio_sink;       /* configured audio sink, or NULL      */
411   GstElement *video_sink;       /* configured video sink, or NULL      */
412   GstElement *text_sink;        /* configured text sink, or NULL       */
413
414   struct
415   {
416     gboolean valid;
417     GstFormat format;
418     gint64 duration;
419   } duration[5];                /* cached durations */
420
421   guint64 ring_buffer_max_size; /* 0 means disabled */
422 };
423
424 struct _GstPlayBinClass
425 {
426   GstPipelineClass parent_class;
427
428   /* notify app that the current uri finished decoding and it is possible to
429    * queue a new one for gapless playback */
430   void (*about_to_finish) (GstPlayBin * playbin);
431
432   /* notify app that number of audio/video/text streams changed */
433   void (*video_changed) (GstPlayBin * playbin);
434   void (*audio_changed) (GstPlayBin * playbin);
435   void (*text_changed) (GstPlayBin * playbin);
436
437   /* notify app that the tags of audio/video/text streams changed */
438   void (*video_tags_changed) (GstPlayBin * playbin, gint stream);
439   void (*audio_tags_changed) (GstPlayBin * playbin, gint stream);
440   void (*text_tags_changed) (GstPlayBin * playbin, gint stream);
441
442   /* get audio/video/text tags for a stream */
443   GstTagList *(*get_video_tags) (GstPlayBin * playbin, gint stream);
444   GstTagList *(*get_audio_tags) (GstPlayBin * playbin, gint stream);
445   GstTagList *(*get_text_tags) (GstPlayBin * playbin, gint stream);
446
447   /* get the last video sample and convert it to the given caps */
448   GstSample *(*convert_sample) (GstPlayBin * playbin, GstCaps * caps);
449
450   /* get audio/video/text pad for a stream */
451   GstPad *(*get_video_pad) (GstPlayBin * playbin, gint stream);
452   GstPad *(*get_audio_pad) (GstPlayBin * playbin, gint stream);
453   GstPad *(*get_text_pad) (GstPlayBin * playbin, gint stream);
454 };
455
456 /* props */
457 #define DEFAULT_URI               NULL
458 #define DEFAULT_SUBURI            NULL
459 #define DEFAULT_SOURCE            NULL
460 #define DEFAULT_FLAGS             GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_TEXT | \
461                                   GST_PLAY_FLAG_SOFT_VOLUME | GST_PLAY_FLAG_SOFT_COLORBALANCE
462 #define DEFAULT_N_VIDEO           0
463 #define DEFAULT_CURRENT_VIDEO     -1
464 #define DEFAULT_N_AUDIO           0
465 #define DEFAULT_CURRENT_AUDIO     -1
466 #define DEFAULT_N_TEXT            0
467 #define DEFAULT_CURRENT_TEXT      -1
468 #define DEFAULT_SUBTITLE_ENCODING NULL
469 #define DEFAULT_AUDIO_SINK        NULL
470 #define DEFAULT_VIDEO_SINK        NULL
471 #define DEFAULT_VIS_PLUGIN        NULL
472 #define DEFAULT_TEXT_SINK         NULL
473 #define DEFAULT_VOLUME            1.0
474 #define DEFAULT_MUTE              FALSE
475 #define DEFAULT_FRAME             NULL
476 #define DEFAULT_FONT_DESC         NULL
477 #define DEFAULT_CONNECTION_SPEED  0
478 #define DEFAULT_BUFFER_DURATION   -1
479 #define DEFAULT_BUFFER_SIZE       -1
480 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
481
482 enum
483 {
484   PROP_0,
485   PROP_URI,
486   PROP_SUBURI,
487   PROP_SOURCE,
488   PROP_FLAGS,
489   PROP_N_VIDEO,
490   PROP_CURRENT_VIDEO,
491   PROP_N_AUDIO,
492   PROP_CURRENT_AUDIO,
493   PROP_N_TEXT,
494   PROP_CURRENT_TEXT,
495   PROP_SUBTITLE_ENCODING,
496   PROP_AUDIO_SINK,
497   PROP_VIDEO_SINK,
498   PROP_VIS_PLUGIN,
499   PROP_TEXT_SINK,
500   PROP_VOLUME,
501   PROP_MUTE,
502   PROP_SAMPLE,
503   PROP_FONT_DESC,
504   PROP_CONNECTION_SPEED,
505   PROP_BUFFER_SIZE,
506   PROP_BUFFER_DURATION,
507   PROP_AV_OFFSET,
508   PROP_RING_BUFFER_MAX_SIZE,
509   PROP_LAST
510 };
511
512 /* signals */
513 enum
514 {
515   SIGNAL_ABOUT_TO_FINISH,
516   SIGNAL_CONVERT_SAMPLE,
517   SIGNAL_VIDEO_CHANGED,
518   SIGNAL_AUDIO_CHANGED,
519   SIGNAL_TEXT_CHANGED,
520   SIGNAL_VIDEO_TAGS_CHANGED,
521   SIGNAL_AUDIO_TAGS_CHANGED,
522   SIGNAL_TEXT_TAGS_CHANGED,
523   SIGNAL_GET_VIDEO_TAGS,
524   SIGNAL_GET_AUDIO_TAGS,
525   SIGNAL_GET_TEXT_TAGS,
526   SIGNAL_GET_VIDEO_PAD,
527   SIGNAL_GET_AUDIO_PAD,
528   SIGNAL_GET_TEXT_PAD,
529   SIGNAL_SOURCE_SETUP,
530   LAST_SIGNAL
531 };
532
533 static void gst_play_bin_class_init (GstPlayBinClass * klass);
534 static void gst_play_bin_init (GstPlayBin * playbin);
535 static void gst_play_bin_finalize (GObject * object);
536
537 static void gst_play_bin_set_property (GObject * object, guint prop_id,
538     const GValue * value, GParamSpec * spec);
539 static void gst_play_bin_get_property (GObject * object, guint prop_id,
540     GValue * value, GParamSpec * spec);
541
542 static GstStateChangeReturn gst_play_bin_change_state (GstElement * element,
543     GstStateChange transition);
544
545 static void gst_play_bin_handle_message (GstBin * bin, GstMessage * message);
546 static gboolean gst_play_bin_query (GstElement * element, GstQuery * query);
547
548 static GstTagList *gst_play_bin_get_video_tags (GstPlayBin * playbin,
549     gint stream);
550 static GstTagList *gst_play_bin_get_audio_tags (GstPlayBin * playbin,
551     gint stream);
552 static GstTagList *gst_play_bin_get_text_tags (GstPlayBin * playbin,
553     gint stream);
554
555 static GstSample *gst_play_bin_convert_sample (GstPlayBin * playbin,
556     GstCaps * caps);
557
558 static GstPad *gst_play_bin_get_video_pad (GstPlayBin * playbin, gint stream);
559 static GstPad *gst_play_bin_get_audio_pad (GstPlayBin * playbin, gint stream);
560 static GstPad *gst_play_bin_get_text_pad (GstPlayBin * playbin, gint stream);
561
562 static gboolean setup_next_source (GstPlayBin * playbin, GstState target);
563
564 static void no_more_pads_cb (GstElement * decodebin, GstSourceGroup * group);
565 static void pad_removed_cb (GstElement * decodebin, GstPad * pad,
566     GstSourceGroup * group);
567
568 static void gst_play_bin_suburidecodebin_block (GstSourceGroup * group,
569     GstElement * suburidecodebin, gboolean block);
570 static void gst_play_bin_suburidecodebin_seek_to_start (GstElement *
571     suburidecodebin);
572
573 static GstElementClass *parent_class;
574
575 static guint gst_play_bin_signals[LAST_SIGNAL] = { 0 };
576
577 #define REMOVE_SIGNAL(obj,id)            \
578 if (id) {                                \
579   g_signal_handler_disconnect (obj, id); \
580   id = 0;                                \
581 }
582
583 static void gst_play_bin_overlay_init (gpointer g_iface, gpointer g_iface_data);
584 static void gst_play_bin_navigation_init (gpointer g_iface,
585     gpointer g_iface_data);
586 static void gst_play_bin_colorbalance_init (gpointer g_iface,
587     gpointer g_iface_data);
588
589 static GType
590 gst_play_bin_get_type (void)
591 {
592   static GType gst_play_bin_type = 0;
593
594   if (!gst_play_bin_type) {
595     static const GTypeInfo gst_play_bin_info = {
596       sizeof (GstPlayBinClass),
597       NULL,
598       NULL,
599       (GClassInitFunc) gst_play_bin_class_init,
600       NULL,
601       NULL,
602       sizeof (GstPlayBin),
603       0,
604       (GInstanceInitFunc) gst_play_bin_init,
605       NULL
606     };
607     static const GInterfaceInfo svol_info = {
608       NULL, NULL, NULL
609     };
610     static const GInterfaceInfo ov_info = {
611       gst_play_bin_overlay_init,
612       NULL, NULL
613     };
614     static const GInterfaceInfo nav_info = {
615       gst_play_bin_navigation_init,
616       NULL, NULL
617     };
618     static const GInterfaceInfo col_info = {
619       gst_play_bin_colorbalance_init,
620       NULL, NULL
621     };
622
623     gst_play_bin_type = g_type_register_static (GST_TYPE_PIPELINE,
624         "GstPlayBin", &gst_play_bin_info, 0);
625
626     g_type_add_interface_static (gst_play_bin_type, GST_TYPE_STREAM_VOLUME,
627         &svol_info);
628     g_type_add_interface_static (gst_play_bin_type, GST_TYPE_VIDEO_OVERLAY,
629         &ov_info);
630     g_type_add_interface_static (gst_play_bin_type, GST_TYPE_NAVIGATION,
631         &nav_info);
632     g_type_add_interface_static (gst_play_bin_type, GST_TYPE_COLOR_BALANCE,
633         &col_info);
634   }
635
636   return gst_play_bin_type;
637 }
638
639 static void
640 gst_play_bin_class_init (GstPlayBinClass * klass)
641 {
642   GObjectClass *gobject_klass;
643   GstElementClass *gstelement_klass;
644   GstBinClass *gstbin_klass;
645
646   gobject_klass = (GObjectClass *) klass;
647   gstelement_klass = (GstElementClass *) klass;
648   gstbin_klass = (GstBinClass *) klass;
649
650   parent_class = g_type_class_peek_parent (klass);
651
652   gobject_klass->set_property = gst_play_bin_set_property;
653   gobject_klass->get_property = gst_play_bin_get_property;
654
655   gobject_klass->finalize = gst_play_bin_finalize;
656
657   /**
658    * GstPlayBin:uri
659    *
660    * Set the next URI that playbin will play. This property can be set from the
661    * about-to-finish signal to queue the next media file.
662    */
663   g_object_class_install_property (gobject_klass, PROP_URI,
664       g_param_spec_string ("uri", "URI", "URI of the media to play",
665           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
666
667   /**
668    * GstPlayBin:suburi
669    *
670    * Set the next subtitle URI that playbin will play. This property can be
671    * set from the about-to-finish signal to queue the next subtitle media file.
672    */
673   g_object_class_install_property (gobject_klass, PROP_SUBURI,
674       g_param_spec_string ("suburi", ".sub-URI", "Optional URI of a subtitle",
675           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
676
677   g_object_class_install_property (gobject_klass, PROP_SOURCE,
678       g_param_spec_object ("source", "Source", "Source element",
679           GST_TYPE_ELEMENT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
680
681   /**
682    * GstPlayBin:flags
683    *
684    * Control the behaviour of playbin.
685    */
686   g_object_class_install_property (gobject_klass, PROP_FLAGS,
687       g_param_spec_flags ("flags", "Flags", "Flags to control behaviour",
688           GST_TYPE_PLAY_FLAGS, DEFAULT_FLAGS,
689           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
690
691   /**
692    * GstPlayBin:n-video
693    *
694    * Get the total number of available video streams.
695    */
696   g_object_class_install_property (gobject_klass, PROP_N_VIDEO,
697       g_param_spec_int ("n-video", "Number Video",
698           "Total number of video streams", 0, G_MAXINT, 0,
699           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
700   /**
701    * GstPlayBin:current-video
702    *
703    * Get or set the currently playing video stream. By default the first video
704    * stream with data is played.
705    */
706   g_object_class_install_property (gobject_klass, PROP_CURRENT_VIDEO,
707       g_param_spec_int ("current-video", "Current Video",
708           "Currently playing video stream (-1 = auto)",
709           -1, G_MAXINT, -1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
710   /**
711    * GstPlayBin:n-audio
712    *
713    * Get the total number of available audio streams.
714    */
715   g_object_class_install_property (gobject_klass, PROP_N_AUDIO,
716       g_param_spec_int ("n-audio", "Number Audio",
717           "Total number of audio streams", 0, G_MAXINT, 0,
718           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
719   /**
720    * GstPlayBin:current-audio
721    *
722    * Get or set the currently playing audio stream. By default the first audio
723    * stream with data is played.
724    */
725   g_object_class_install_property (gobject_klass, PROP_CURRENT_AUDIO,
726       g_param_spec_int ("current-audio", "Current audio",
727           "Currently playing audio stream (-1 = auto)",
728           -1, G_MAXINT, -1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
729   /**
730    * GstPlayBin:n-text
731    *
732    * Get the total number of available subtitle streams.
733    */
734   g_object_class_install_property (gobject_klass, PROP_N_TEXT,
735       g_param_spec_int ("n-text", "Number Text",
736           "Total number of text streams", 0, G_MAXINT, 0,
737           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
738   /**
739    * GstPlayBin:current-text:
740    *
741    * Get or set the currently playing subtitle stream. By default the first
742    * subtitle stream with data is played.
743    */
744   g_object_class_install_property (gobject_klass, PROP_CURRENT_TEXT,
745       g_param_spec_int ("current-text", "Current Text",
746           "Currently playing text stream (-1 = auto)",
747           -1, G_MAXINT, -1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
748
749   g_object_class_install_property (gobject_klass, PROP_SUBTITLE_ENCODING,
750       g_param_spec_string ("subtitle-encoding", "subtitle encoding",
751           "Encoding to assume if input subtitles are not in UTF-8 encoding. "
752           "If not set, the GST_SUBTITLE_ENCODING environment variable will "
753           "be checked for an encoding to use. If that is not set either, "
754           "ISO-8859-15 will be assumed.", NULL,
755           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
756
757   g_object_class_install_property (gobject_klass, PROP_VIDEO_SINK,
758       g_param_spec_object ("video-sink", "Video Sink",
759           "the video output element to use (NULL = default sink)",
760           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
761   g_object_class_install_property (gobject_klass, PROP_AUDIO_SINK,
762       g_param_spec_object ("audio-sink", "Audio Sink",
763           "the audio output element to use (NULL = default sink)",
764           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
765   g_object_class_install_property (gobject_klass, PROP_VIS_PLUGIN,
766       g_param_spec_object ("vis-plugin", "Vis plugin",
767           "the visualization element to use (NULL = default)",
768           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
769   g_object_class_install_property (gobject_klass, PROP_TEXT_SINK,
770       g_param_spec_object ("text-sink", "Text plugin",
771           "the text output element to use (NULL = default textoverlay)",
772           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
773
774   /**
775    * GstPlayBin:volume:
776    *
777    * Get or set the current audio stream volume. 1.0 means 100%,
778    * 0.0 means mute. This uses a linear volume scale.
779    *
780    */
781   g_object_class_install_property (gobject_klass, PROP_VOLUME,
782       g_param_spec_double ("volume", "Volume", "The audio volume, 1.0=100%",
783           0.0, VOLUME_MAX_DOUBLE, 1.0,
784           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
785   g_object_class_install_property (gobject_klass, PROP_MUTE,
786       g_param_spec_boolean ("mute", "Mute",
787           "Mute the audio channel without changing the volume", FALSE,
788           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
789
790   /**
791    * GstPlayBin:sample:
792    * @playbin: a #GstPlayBin
793    *
794    * Get the currently rendered or prerolled sample in the video sink.
795    * The #GstCaps in the sample will describe the format of the buffer.
796    */
797   g_object_class_install_property (gobject_klass, PROP_SAMPLE,
798       g_param_spec_boxed ("sample", "Sample",
799           "The last sample (NULL = no video available)",
800           GST_TYPE_SAMPLE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
801
802   g_object_class_install_property (gobject_klass, PROP_FONT_DESC,
803       g_param_spec_string ("subtitle-font-desc",
804           "Subtitle font description",
805           "Pango font description of font "
806           "to be used for subtitle rendering", NULL,
807           G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
808
809   g_object_class_install_property (gobject_klass, PROP_CONNECTION_SPEED,
810       g_param_spec_uint64 ("connection-speed", "Connection Speed",
811           "Network connection speed in kbps (0 = unknown)",
812           0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
813           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
814
815   g_object_class_install_property (gobject_klass, PROP_BUFFER_SIZE,
816       g_param_spec_int ("buffer-size", "Buffer size (bytes)",
817           "Buffer size when buffering network streams",
818           -1, G_MAXINT, DEFAULT_BUFFER_SIZE,
819           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
820   g_object_class_install_property (gobject_klass, PROP_BUFFER_DURATION,
821       g_param_spec_int64 ("buffer-duration", "Buffer duration (ns)",
822           "Buffer duration when buffering network streams",
823           -1, G_MAXINT64, DEFAULT_BUFFER_DURATION,
824           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
825   /**
826    * GstPlayBin:av-offset:
827    *
828    * Control the synchronisation offset between the audio and video streams.
829    * Positive values make the audio ahead of the video and negative values make
830    * the audio go behind the video.
831    *
832    * Since: 0.10.30
833    */
834   g_object_class_install_property (gobject_klass, PROP_AV_OFFSET,
835       g_param_spec_int64 ("av-offset", "AV Offset",
836           "The synchronisation offset between audio and video in nanoseconds",
837           G_MININT64, G_MAXINT64, 0,
838           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
839
840   /**
841    * GstQueue2:ring-buffer-max-size
842    *
843    * The maximum size of the ring buffer in bytes. If set to 0, the ring
844    * buffer is disabled. Default 0.
845    *
846    * Since: 0.10.31
847    */
848   g_object_class_install_property (gobject_klass, PROP_RING_BUFFER_MAX_SIZE,
849       g_param_spec_uint64 ("ring-buffer-max-size",
850           "Max. ring buffer size (bytes)",
851           "Max. amount of data in the ring buffer (bytes, 0 = ring buffer disabled)",
852           0, G_MAXUINT, DEFAULT_RING_BUFFER_MAX_SIZE,
853           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
854
855   /**
856    * GstPlayBin::about-to-finish
857    * @playbin: a #GstPlayBin
858    *
859    * This signal is emitted when the current uri is about to finish. You can
860    * set the uri and suburi to make sure that playback continues.
861    *
862    * This signal is emitted from the context of a GStreamer streaming thread.
863    */
864   gst_play_bin_signals[SIGNAL_ABOUT_TO_FINISH] =
865       g_signal_new ("about-to-finish", G_TYPE_FROM_CLASS (klass),
866       G_SIGNAL_RUN_LAST,
867       G_STRUCT_OFFSET (GstPlayBinClass, about_to_finish), NULL, NULL,
868       gst_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
869
870   /**
871    * GstPlayBin::video-changed
872    * @playbin: a #GstPlayBin
873    *
874    * This signal is emitted whenever the number or order of the video
875    * streams has changed. The application will most likely want to select
876    * a new video stream.
877    *
878    * This signal is usually emitted from the context of a GStreamer streaming
879    * thread. You can use gst_message_new_application() and
880    * gst_element_post_message() to notify your application's main thread.
881    */
882   /* FIXME 0.11: turn video-changed signal into message? */
883   gst_play_bin_signals[SIGNAL_VIDEO_CHANGED] =
884       g_signal_new ("video-changed", G_TYPE_FROM_CLASS (klass),
885       G_SIGNAL_RUN_LAST,
886       G_STRUCT_OFFSET (GstPlayBinClass, video_changed), NULL, NULL,
887       gst_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
888   /**
889    * GstPlayBin::audio-changed
890    * @playbin: a #GstPlayBin
891    *
892    * This signal is emitted whenever the number or order of the audio
893    * streams has changed. The application will most likely want to select
894    * a new audio stream.
895    *
896    * This signal may be emitted from the context of a GStreamer streaming thread.
897    * You can use gst_message_new_application() and gst_element_post_message()
898    * to notify your application's main thread.
899    */
900   /* FIXME 0.11: turn audio-changed signal into message? */
901   gst_play_bin_signals[SIGNAL_AUDIO_CHANGED] =
902       g_signal_new ("audio-changed", G_TYPE_FROM_CLASS (klass),
903       G_SIGNAL_RUN_LAST,
904       G_STRUCT_OFFSET (GstPlayBinClass, audio_changed), NULL, NULL,
905       gst_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
906   /**
907    * GstPlayBin::text-changed
908    * @playbin: a #GstPlayBin
909    *
910    * This signal is emitted whenever the number or order of the text
911    * streams has changed. The application will most likely want to select
912    * a new text stream.
913    *
914    * This signal may be emitted from the context of a GStreamer streaming thread.
915    * You can use gst_message_new_application() and gst_element_post_message()
916    * to notify your application's main thread.
917    */
918   /* FIXME 0.11: turn text-changed signal into message? */
919   gst_play_bin_signals[SIGNAL_TEXT_CHANGED] =
920       g_signal_new ("text-changed", G_TYPE_FROM_CLASS (klass),
921       G_SIGNAL_RUN_LAST,
922       G_STRUCT_OFFSET (GstPlayBinClass, text_changed), NULL, NULL,
923       gst_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
924
925   /**
926    * GstPlayBin::video-tags-changed
927    * @playbin: a #GstPlayBin
928    * @stream: stream index with changed tags
929    *
930    * This signal is emitted whenever the tags of a video stream have changed.
931    * The application will most likely want to get the new tags.
932    *
933    * This signal may be emitted from the context of a GStreamer streaming thread.
934    * You can use gst_message_new_application() and gst_element_post_message()
935    * to notify your application's main thread.
936    *
937    * Since: 0.10.24
938    */
939   gst_play_bin_signals[SIGNAL_VIDEO_TAGS_CHANGED] =
940       g_signal_new ("video-tags-changed", G_TYPE_FROM_CLASS (klass),
941       G_SIGNAL_RUN_LAST,
942       G_STRUCT_OFFSET (GstPlayBinClass, video_tags_changed), NULL, NULL,
943       gst_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
944
945   /**
946    * GstPlayBin::audio-tags-changed
947    * @playbin: a #GstPlayBin
948    * @stream: stream index with changed tags
949    *
950    * This signal is emitted whenever the tags of an audio stream have changed.
951    * The application will most likely want to get the new tags.
952    *
953    * This signal may be emitted from the context of a GStreamer streaming thread.
954    * You can use gst_message_new_application() and gst_element_post_message()
955    * to notify your application's main thread.
956    *
957    * Since: 0.10.24
958    */
959   gst_play_bin_signals[SIGNAL_AUDIO_TAGS_CHANGED] =
960       g_signal_new ("audio-tags-changed", G_TYPE_FROM_CLASS (klass),
961       G_SIGNAL_RUN_LAST,
962       G_STRUCT_OFFSET (GstPlayBinClass, audio_tags_changed), NULL, NULL,
963       gst_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
964
965   /**
966    * GstPlayBin::text-tags-changed
967    * @playbin: a #GstPlayBin
968    * @stream: stream index with changed tags
969    *
970    * This signal is emitted whenever the tags of a text stream have changed.
971    * The application will most likely want to get the new tags.
972    *
973    * This signal may be emitted from the context of a GStreamer streaming thread.
974    * You can use gst_message_new_application() and gst_element_post_message()
975    * to notify your application's main thread.
976    *
977    * Since: 0.10.24
978    */
979   gst_play_bin_signals[SIGNAL_TEXT_TAGS_CHANGED] =
980       g_signal_new ("text-tags-changed", G_TYPE_FROM_CLASS (klass),
981       G_SIGNAL_RUN_LAST,
982       G_STRUCT_OFFSET (GstPlayBinClass, text_tags_changed), NULL, NULL,
983       gst_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
984
985   /**
986    * GstPlayBin::source-setup:
987    * @playbin: a #GstPlayBin
988    * @source: source element
989    *
990    * This signal is emitted after the source element has been created, so
991    * it can be configured by setting additional properties (e.g. set a
992    * proxy server for an http source, or set the device and read speed for
993    * an audio cd source). This is functionally equivalent to connecting to
994    * the notify::source signal, but more convenient.
995    *
996    * This signal is usually emitted from the context of a GStreamer streaming
997    * thread.
998    *
999    * Since: 0.10.33
1000    */
1001   gst_play_bin_signals[SIGNAL_SOURCE_SETUP] =
1002       g_signal_new ("source-setup", G_TYPE_FROM_CLASS (klass),
1003       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
1004       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
1005
1006   /**
1007    * GstPlayBin::get-video-tags
1008    * @playbin: a #GstPlayBin
1009    * @stream: a video stream number
1010    *
1011    * Action signal to retrieve the tags of a specific video stream number.
1012    * This information can be used to select a stream.
1013    *
1014    * Returns: a GstTagList with tags or NULL when the stream number does not
1015    * exist.
1016    */
1017   gst_play_bin_signals[SIGNAL_GET_VIDEO_TAGS] =
1018       g_signal_new ("get-video-tags", G_TYPE_FROM_CLASS (klass),
1019       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1020       G_STRUCT_OFFSET (GstPlayBinClass, get_video_tags), NULL, NULL,
1021       gst_play_marshal_BOXED__INT, GST_TYPE_TAG_LIST, 1, G_TYPE_INT);
1022   /**
1023    * GstPlayBin::get-audio-tags
1024    * @playbin: a #GstPlayBin
1025    * @stream: an audio stream number
1026    *
1027    * Action signal to retrieve the tags of a specific audio stream number.
1028    * This information can be used to select a stream.
1029    *
1030    * Returns: a GstTagList with tags or NULL when the stream number does not
1031    * exist.
1032    */
1033   gst_play_bin_signals[SIGNAL_GET_AUDIO_TAGS] =
1034       g_signal_new ("get-audio-tags", G_TYPE_FROM_CLASS (klass),
1035       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1036       G_STRUCT_OFFSET (GstPlayBinClass, get_audio_tags), NULL, NULL,
1037       gst_play_marshal_BOXED__INT, GST_TYPE_TAG_LIST, 1, G_TYPE_INT);
1038   /**
1039    * GstPlayBin::get-text-tags
1040    * @playbin: a #GstPlayBin
1041    * @stream: a text stream number
1042    *
1043    * Action signal to retrieve the tags of a specific text stream number.
1044    * This information can be used to select a stream.
1045    *
1046    * Returns: a GstTagList with tags or NULL when the stream number does not
1047    * exist.
1048    */
1049   gst_play_bin_signals[SIGNAL_GET_TEXT_TAGS] =
1050       g_signal_new ("get-text-tags", G_TYPE_FROM_CLASS (klass),
1051       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1052       G_STRUCT_OFFSET (GstPlayBinClass, get_text_tags), NULL, NULL,
1053       gst_play_marshal_BOXED__INT, GST_TYPE_TAG_LIST, 1, G_TYPE_INT);
1054   /**
1055    * GstPlayBin::convert-sample
1056    * @playbin: a #GstPlayBin
1057    * @caps: the target format of the frame
1058    *
1059    * Action signal to retrieve the currently playing video frame in the format
1060    * specified by @caps.
1061    * If @caps is %NULL, no conversion will be performed and this function is
1062    * equivalent to the #GstPlayBin::frame property.
1063    *
1064    * Returns: a #GstBuffer of the current video frame converted to #caps.
1065    * The caps on the buffer will describe the final layout of the buffer data.
1066    * %NULL is returned when no current buffer can be retrieved or when the
1067    * conversion failed.
1068    */
1069   gst_play_bin_signals[SIGNAL_CONVERT_SAMPLE] =
1070       g_signal_new ("convert-sample", G_TYPE_FROM_CLASS (klass),
1071       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1072       G_STRUCT_OFFSET (GstPlayBinClass, convert_sample), NULL, NULL,
1073       gst_play_marshal_SAMPLE__BOXED, GST_TYPE_SAMPLE, 1, GST_TYPE_CAPS);
1074
1075   /**
1076    * GstPlayBin::get-video-pad
1077    * @playbin: a #GstPlayBin
1078    * @stream: a video stream number
1079    *
1080    * Action signal to retrieve the stream-selector sinkpad for a specific
1081    * video stream.
1082    * This pad can be used for notifications of caps changes, stream-specific
1083    * queries, etc.
1084    *
1085    * Returns: a #GstPad, or NULL when the stream number does not exist.
1086    */
1087   gst_play_bin_signals[SIGNAL_GET_VIDEO_PAD] =
1088       g_signal_new ("get-video-pad", G_TYPE_FROM_CLASS (klass),
1089       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1090       G_STRUCT_OFFSET (GstPlayBinClass, get_video_pad), NULL, NULL,
1091       gst_play_marshal_OBJECT__INT, GST_TYPE_PAD, 1, G_TYPE_INT);
1092   /**
1093    * GstPlayBin::get-audio-pad
1094    * @playbin: a #GstPlayBin
1095    * @stream: an audio stream number
1096    *
1097    * Action signal to retrieve the stream-selector sinkpad for a specific
1098    * audio stream.
1099    * This pad can be used for notifications of caps changes, stream-specific
1100    * queries, etc.
1101    *
1102    * Returns: a #GstPad, or NULL when the stream number does not exist.
1103    */
1104   gst_play_bin_signals[SIGNAL_GET_AUDIO_PAD] =
1105       g_signal_new ("get-audio-pad", G_TYPE_FROM_CLASS (klass),
1106       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1107       G_STRUCT_OFFSET (GstPlayBinClass, get_audio_pad), NULL, NULL,
1108       gst_play_marshal_OBJECT__INT, GST_TYPE_PAD, 1, G_TYPE_INT);
1109   /**
1110    * GstPlayBin::get-text-pad
1111    * @playbin: a #GstPlayBin
1112    * @stream: a text stream number
1113    *
1114    * Action signal to retrieve the stream-selector sinkpad for a specific
1115    * text stream.
1116    * This pad can be used for notifications of caps changes, stream-specific
1117    * queries, etc.
1118    *
1119    * Returns: a #GstPad, or NULL when the stream number does not exist.
1120    */
1121   gst_play_bin_signals[SIGNAL_GET_TEXT_PAD] =
1122       g_signal_new ("get-text-pad", G_TYPE_FROM_CLASS (klass),
1123       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1124       G_STRUCT_OFFSET (GstPlayBinClass, get_text_pad), NULL, NULL,
1125       gst_play_marshal_OBJECT__INT, GST_TYPE_PAD, 1, G_TYPE_INT);
1126
1127   klass->get_video_tags = gst_play_bin_get_video_tags;
1128   klass->get_audio_tags = gst_play_bin_get_audio_tags;
1129   klass->get_text_tags = gst_play_bin_get_text_tags;
1130
1131   klass->convert_sample = gst_play_bin_convert_sample;
1132
1133   klass->get_video_pad = gst_play_bin_get_video_pad;
1134   klass->get_audio_pad = gst_play_bin_get_audio_pad;
1135   klass->get_text_pad = gst_play_bin_get_text_pad;
1136
1137   gst_element_class_set_details_simple (gstelement_klass,
1138       "Player Bin 2", "Generic/Bin/Player",
1139       "Autoplug and play media from an uri",
1140       "Wim Taymans <wim.taymans@gmail.com>");
1141
1142   gstelement_klass->change_state =
1143       GST_DEBUG_FUNCPTR (gst_play_bin_change_state);
1144   gstelement_klass->query = GST_DEBUG_FUNCPTR (gst_play_bin_query);
1145
1146   gstbin_klass->handle_message =
1147       GST_DEBUG_FUNCPTR (gst_play_bin_handle_message);
1148 }
1149
1150 static void
1151 init_group (GstPlayBin * playbin, GstSourceGroup * group)
1152 {
1153   int n;
1154
1155   /* store the array for the different channels */
1156   group->video_channels = g_ptr_array_new ();
1157   group->audio_channels = g_ptr_array_new ();
1158   group->text_channels = g_ptr_array_new ();
1159   g_mutex_init (&group->lock);
1160   /* init selectors. The selector is found by finding the first prefix that
1161    * matches the media. */
1162   group->playbin = playbin;
1163   /* If you add any items to these lists, check that media_list[] is defined
1164    * above to be large enough to hold MAX(items)+1, so as to accommodate a
1165    * NULL terminator (set when the memory is zeroed on allocation) */
1166   group->selector[PLAYBIN_STREAM_AUDIO].media_list[0] = "audio/";
1167   group->selector[PLAYBIN_STREAM_AUDIO].type = GST_PLAY_SINK_TYPE_AUDIO;
1168   group->selector[PLAYBIN_STREAM_AUDIO].channels = group->audio_channels;
1169   group->selector[PLAYBIN_STREAM_VIDEO].media_list[0] = "video/";
1170   group->selector[PLAYBIN_STREAM_VIDEO].type = GST_PLAY_SINK_TYPE_VIDEO;
1171   group->selector[PLAYBIN_STREAM_VIDEO].channels = group->video_channels;
1172   group->selector[PLAYBIN_STREAM_TEXT].media_list[0] = "text/";
1173   group->selector[PLAYBIN_STREAM_TEXT].media_list[1] = "application/x-subtitle";
1174   group->selector[PLAYBIN_STREAM_TEXT].media_list[2] = "application/x-ssa";
1175   group->selector[PLAYBIN_STREAM_TEXT].media_list[3] = "application/x-ass";
1176   group->selector[PLAYBIN_STREAM_TEXT].media_list[4] = "video/x-dvd-subpicture";
1177   group->selector[PLAYBIN_STREAM_TEXT].media_list[5] = "subpicture/";
1178   group->selector[PLAYBIN_STREAM_TEXT].media_list[6] = "subtitle/";
1179   group->selector[PLAYBIN_STREAM_TEXT].get_media_caps =
1180       gst_subtitle_overlay_create_factory_caps;
1181   group->selector[PLAYBIN_STREAM_TEXT].type = GST_PLAY_SINK_TYPE_TEXT;
1182   group->selector[PLAYBIN_STREAM_TEXT].channels = group->text_channels;
1183
1184   for (n = 0; n < PLAYBIN_STREAM_LAST; n++) {
1185     GstSourceSelect *select = &group->selector[n];
1186     select->sinkpad_delayed_event = NULL;
1187     select->sinkpad_data_probe = 0;
1188   }
1189 }
1190
1191 static void
1192 free_group (GstPlayBin * playbin, GstSourceGroup * group)
1193 {
1194   int n;
1195
1196   for (n = 0; n < PLAYBIN_STREAM_LAST; n++) {
1197     GstSourceSelect *select = &group->selector[n];
1198     if (select->sinkpad && select->sinkpad_data_probe)
1199       gst_pad_remove_probe (select->sinkpad, select->sinkpad_data_probe);
1200     if (select->sinkpad_delayed_event)
1201       gst_event_unref (select->sinkpad_delayed_event);
1202   }
1203
1204   g_free (group->uri);
1205   g_free (group->suburi);
1206   g_ptr_array_free (group->video_channels, TRUE);
1207   g_ptr_array_free (group->audio_channels, TRUE);
1208   g_ptr_array_free (group->text_channels, TRUE);
1209
1210   g_mutex_clear (&group->lock);
1211   if (group->audio_sink) {
1212     if (group->audio_sink != playbin->audio_sink)
1213       gst_element_set_state (group->audio_sink, GST_STATE_NULL);
1214     gst_object_unref (group->audio_sink);
1215   }
1216   group->audio_sink = NULL;
1217   if (group->video_sink) {
1218     if (group->video_sink != playbin->video_sink)
1219       gst_element_set_state (group->video_sink, GST_STATE_NULL);
1220     gst_object_unref (group->video_sink);
1221   }
1222   group->video_sink = NULL;
1223
1224   g_list_free (group->stream_changed_pending);
1225   group->stream_changed_pending = NULL;
1226
1227   if (group->stream_changed_pending_lock.p)
1228     g_mutex_clear (&group->stream_changed_pending_lock);
1229   group->stream_changed_pending_lock.p = NULL;
1230 }
1231
1232 static void
1233 notify_volume_cb (GObject * selector, GParamSpec * pspec, GstPlayBin * playbin)
1234 {
1235   g_object_notify (G_OBJECT (playbin), "volume");
1236 }
1237
1238 static void
1239 notify_mute_cb (GObject * selector, GParamSpec * pspec, GstPlayBin * playbin)
1240 {
1241   g_object_notify (G_OBJECT (playbin), "mute");
1242 }
1243
1244 static void
1245 colorbalance_value_changed_cb (GstColorBalance * balance,
1246     GstColorBalanceChannel * channel, gint value, GstPlayBin * playbin)
1247 {
1248   gst_color_balance_value_changed (GST_COLOR_BALANCE (playbin), channel, value);
1249 }
1250
1251 /* Must be called with elements lock! */
1252 static void
1253 gst_play_bin_update_elements_list (GstPlayBin * playbin)
1254 {
1255   GList *res, *tmp;
1256   guint cookie;
1257
1258   cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
1259   if (!playbin->elements || playbin->elements_cookie != cookie) {
1260     if (playbin->elements)
1261       gst_plugin_feature_list_free (playbin->elements);
1262     res =
1263         gst_element_factory_list_get_elements
1264         (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
1265     tmp =
1266         gst_element_factory_list_get_elements
1267         (GST_ELEMENT_FACTORY_TYPE_AUDIOVIDEO_SINKS, GST_RANK_MARGINAL);
1268     playbin->elements = g_list_concat (res, tmp);
1269     playbin->elements =
1270         g_list_sort (playbin->elements, gst_plugin_feature_rank_compare_func);
1271     playbin->elements_cookie = cookie;
1272   }
1273 }
1274
1275 static void
1276 gst_play_bin_init (GstPlayBin * playbin)
1277 {
1278   g_rec_mutex_init (&playbin->lock);
1279   g_mutex_init (&playbin->dyn_lock);
1280
1281   /* assume we can create a selector */
1282   playbin->have_selector = TRUE;
1283
1284   /* init groups */
1285   playbin->curr_group = &playbin->groups[0];
1286   playbin->next_group = &playbin->groups[1];
1287   init_group (playbin, &playbin->groups[0]);
1288   init_group (playbin, &playbin->groups[1]);
1289
1290   /* first filter out the interesting element factories */
1291   g_mutex_init (&playbin->elements_lock);
1292
1293   /* add sink */
1294   playbin->playsink =
1295       g_object_new (GST_TYPE_PLAY_SINK, "name", "playsink", NULL);
1296   gst_bin_add (GST_BIN_CAST (playbin), GST_ELEMENT_CAST (playbin->playsink));
1297   gst_play_sink_set_flags (playbin->playsink, DEFAULT_FLAGS);
1298   /* Connect to notify::volume and notify::mute signals for proxying */
1299   g_signal_connect (playbin->playsink, "notify::volume",
1300       G_CALLBACK (notify_volume_cb), playbin);
1301   g_signal_connect (playbin->playsink, "notify::mute",
1302       G_CALLBACK (notify_mute_cb), playbin);
1303   g_signal_connect (playbin->playsink, "value-changed",
1304       G_CALLBACK (colorbalance_value_changed_cb), playbin);
1305
1306   playbin->current_video = DEFAULT_CURRENT_VIDEO;
1307   playbin->current_audio = DEFAULT_CURRENT_AUDIO;
1308   playbin->current_text = DEFAULT_CURRENT_TEXT;
1309
1310   playbin->buffer_duration = DEFAULT_BUFFER_DURATION;
1311   playbin->buffer_size = DEFAULT_BUFFER_SIZE;
1312   playbin->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
1313 }
1314
1315 static void
1316 gst_play_bin_finalize (GObject * object)
1317 {
1318   GstPlayBin *playbin;
1319
1320   playbin = GST_PLAY_BIN (object);
1321
1322   free_group (playbin, &playbin->groups[0]);
1323   free_group (playbin, &playbin->groups[1]);
1324
1325   if (playbin->source)
1326     gst_object_unref (playbin->source);
1327   if (playbin->video_sink) {
1328     gst_element_set_state (playbin->video_sink, GST_STATE_NULL);
1329     gst_object_unref (playbin->video_sink);
1330   }
1331   if (playbin->audio_sink) {
1332     gst_element_set_state (playbin->audio_sink, GST_STATE_NULL);
1333     gst_object_unref (playbin->audio_sink);
1334   }
1335   if (playbin->text_sink) {
1336     gst_element_set_state (playbin->text_sink, GST_STATE_NULL);
1337     gst_object_unref (playbin->text_sink);
1338   }
1339
1340   if (playbin->elements)
1341     gst_plugin_feature_list_free (playbin->elements);
1342
1343   g_rec_mutex_clear (&playbin->lock);
1344   g_mutex_clear (&playbin->dyn_lock);
1345   g_mutex_clear (&playbin->elements_lock);
1346
1347   G_OBJECT_CLASS (parent_class)->finalize (object);
1348 }
1349
1350 static gboolean
1351 gst_playbin_uri_is_valid (GstPlayBin * playbin, const gchar * uri)
1352 {
1353   const gchar *c;
1354
1355   GST_LOG_OBJECT (playbin, "checking uri '%s'", uri);
1356
1357   /* this just checks the protocol */
1358   if (!gst_uri_is_valid (uri))
1359     return FALSE;
1360
1361   for (c = uri; *c != '\0'; ++c) {
1362     if (!g_ascii_isprint (*c))
1363       goto invalid;
1364     if (*c == ' ')
1365       goto invalid;
1366   }
1367
1368   return TRUE;
1369
1370 invalid:
1371   {
1372     GST_WARNING_OBJECT (playbin, "uri '%s' not valid, character #%u",
1373         uri, (guint) ((guintptr) c - (guintptr) uri));
1374     return FALSE;
1375   }
1376 }
1377
1378 static void
1379 gst_play_bin_set_uri (GstPlayBin * playbin, const gchar * uri)
1380 {
1381   GstSourceGroup *group;
1382
1383   if (uri == NULL) {
1384     g_warning ("cannot set NULL uri");
1385     return;
1386   }
1387
1388   if (!gst_playbin_uri_is_valid (playbin, uri)) {
1389     if (g_str_has_prefix (uri, "file:")) {
1390       GST_WARNING_OBJECT (playbin, "not entirely correct file URI '%s' - make "
1391           "sure to escape spaces and non-ASCII characters properly and specify "
1392           "an absolute path. Use gst_filename_to_uri() to convert filenames "
1393           "to URIs", uri);
1394     } else {
1395       /* GST_ERROR_OBJECT (playbin, "malformed URI '%s'", uri); */
1396     }
1397   }
1398
1399   GST_PLAY_BIN_LOCK (playbin);
1400   group = playbin->next_group;
1401
1402   GST_SOURCE_GROUP_LOCK (group);
1403   /* store the uri in the next group we will play */
1404   g_free (group->uri);
1405   group->uri = g_strdup (uri);
1406   group->valid = TRUE;
1407   GST_SOURCE_GROUP_UNLOCK (group);
1408
1409   GST_DEBUG ("set new uri to %s", uri);
1410   GST_PLAY_BIN_UNLOCK (playbin);
1411 }
1412
1413 static void
1414 gst_play_bin_set_suburi (GstPlayBin * playbin, const gchar * suburi)
1415 {
1416   GstSourceGroup *group;
1417
1418   GST_PLAY_BIN_LOCK (playbin);
1419   group = playbin->next_group;
1420
1421   GST_SOURCE_GROUP_LOCK (group);
1422   g_free (group->suburi);
1423   group->suburi = g_strdup (suburi);
1424   GST_SOURCE_GROUP_UNLOCK (group);
1425
1426   GST_DEBUG ("setting new .sub uri to %s", suburi);
1427
1428   GST_PLAY_BIN_UNLOCK (playbin);
1429 }
1430
1431 static void
1432 gst_play_bin_set_flags (GstPlayBin * playbin, GstPlayFlags flags)
1433 {
1434   gst_play_sink_set_flags (playbin->playsink, flags);
1435   gst_play_sink_reconfigure (playbin->playsink);
1436 }
1437
1438 static GstPlayFlags
1439 gst_play_bin_get_flags (GstPlayBin * playbin)
1440 {
1441   GstPlayFlags flags;
1442
1443   flags = gst_play_sink_get_flags (playbin->playsink);
1444
1445   return flags;
1446 }
1447
1448 /* get the currently playing group or if nothing is playing, the next
1449  * group. Must be called with the PLAY_BIN_LOCK. */
1450 static GstSourceGroup *
1451 get_group (GstPlayBin * playbin)
1452 {
1453   GstSourceGroup *result;
1454
1455   if (!(result = playbin->curr_group))
1456     result = playbin->next_group;
1457
1458   return result;
1459 }
1460
1461 static GstPad *
1462 gst_play_bin_get_video_pad (GstPlayBin * playbin, gint stream)
1463 {
1464   GstPad *sinkpad = NULL;
1465   GstSourceGroup *group;
1466
1467   GST_PLAY_BIN_LOCK (playbin);
1468   group = get_group (playbin);
1469   if (stream < group->video_channels->len) {
1470     sinkpad = g_ptr_array_index (group->video_channels, stream);
1471     gst_object_ref (sinkpad);
1472   }
1473   GST_PLAY_BIN_UNLOCK (playbin);
1474
1475   return sinkpad;
1476 }
1477
1478 static GstPad *
1479 gst_play_bin_get_audio_pad (GstPlayBin * playbin, gint stream)
1480 {
1481   GstPad *sinkpad = NULL;
1482   GstSourceGroup *group;
1483
1484   GST_PLAY_BIN_LOCK (playbin);
1485   group = get_group (playbin);
1486   if (stream < group->audio_channels->len) {
1487     sinkpad = g_ptr_array_index (group->audio_channels, stream);
1488     gst_object_ref (sinkpad);
1489   }
1490   GST_PLAY_BIN_UNLOCK (playbin);
1491
1492   return sinkpad;
1493 }
1494
1495 static GstPad *
1496 gst_play_bin_get_text_pad (GstPlayBin * playbin, gint stream)
1497 {
1498   GstPad *sinkpad = NULL;
1499   GstSourceGroup *group;
1500
1501   GST_PLAY_BIN_LOCK (playbin);
1502   group = get_group (playbin);
1503   if (stream < group->text_channels->len) {
1504     sinkpad = g_ptr_array_index (group->text_channels, stream);
1505     gst_object_ref (sinkpad);
1506   }
1507   GST_PLAY_BIN_UNLOCK (playbin);
1508
1509   return sinkpad;
1510 }
1511
1512
1513 static GstTagList *
1514 get_tags (GstPlayBin * playbin, GPtrArray * channels, gint stream)
1515 {
1516   GstTagList *result;
1517   GstPad *sinkpad;
1518
1519   if (!channels || stream >= channels->len)
1520     return NULL;
1521
1522   sinkpad = g_ptr_array_index (channels, stream);
1523   g_object_get (sinkpad, "tags", &result, NULL);
1524
1525   return result;
1526 }
1527
1528 static GstTagList *
1529 gst_play_bin_get_video_tags (GstPlayBin * playbin, gint stream)
1530 {
1531   GstTagList *result;
1532   GstSourceGroup *group;
1533
1534   GST_PLAY_BIN_LOCK (playbin);
1535   group = get_group (playbin);
1536   result = get_tags (playbin, group->video_channels, stream);
1537   GST_PLAY_BIN_UNLOCK (playbin);
1538
1539   return result;
1540 }
1541
1542 static GstTagList *
1543 gst_play_bin_get_audio_tags (GstPlayBin * playbin, gint stream)
1544 {
1545   GstTagList *result;
1546   GstSourceGroup *group;
1547
1548   GST_PLAY_BIN_LOCK (playbin);
1549   group = get_group (playbin);
1550   result = get_tags (playbin, group->audio_channels, stream);
1551   GST_PLAY_BIN_UNLOCK (playbin);
1552
1553   return result;
1554 }
1555
1556 static GstTagList *
1557 gst_play_bin_get_text_tags (GstPlayBin * playbin, gint stream)
1558 {
1559   GstTagList *result;
1560   GstSourceGroup *group;
1561
1562   GST_PLAY_BIN_LOCK (playbin);
1563   group = get_group (playbin);
1564   result = get_tags (playbin, group->text_channels, stream);
1565   GST_PLAY_BIN_UNLOCK (playbin);
1566
1567   return result;
1568 }
1569
1570 static GstSample *
1571 gst_play_bin_convert_sample (GstPlayBin * playbin, GstCaps * caps)
1572 {
1573   return gst_play_sink_convert_sample (playbin->playsink, caps);
1574 }
1575
1576 /* Returns current stream number, or -1 if none has been selected yet */
1577 static int
1578 get_current_stream_number (GstPlayBin * playbin, GPtrArray * channels)
1579 {
1580   /* Internal API cleanup would make this easier... */
1581   int i;
1582   GstPad *pad, *current;
1583   GstObject *selector = NULL;
1584   int ret = -1;
1585
1586   for (i = 0; i < channels->len; i++) {
1587     pad = g_ptr_array_index (channels, i);
1588     if ((selector = gst_pad_get_parent (pad))) {
1589       g_object_get (selector, "active-pad", &current, NULL);
1590       gst_object_unref (selector);
1591
1592       if (pad == current) {
1593         gst_object_unref (current);
1594         ret = i;
1595         break;
1596       }
1597
1598       if (current)
1599         gst_object_unref (current);
1600     }
1601   }
1602
1603   return ret;
1604 }
1605
1606 static gboolean
1607 gst_play_bin_set_current_video_stream (GstPlayBin * playbin, gint stream)
1608 {
1609   GstSourceGroup *group;
1610   GPtrArray *channels;
1611   GstPad *sinkpad;
1612
1613   GST_PLAY_BIN_LOCK (playbin);
1614
1615   GST_DEBUG_OBJECT (playbin, "Changing current video stream %d -> %d",
1616       playbin->current_video, stream);
1617
1618   group = get_group (playbin);
1619   if (!(channels = group->video_channels))
1620     goto no_channels;
1621
1622   if (stream == -1 || channels->len <= stream) {
1623     sinkpad = NULL;
1624   } else {
1625     /* take channel from selected stream */
1626     sinkpad = g_ptr_array_index (channels, stream);
1627   }
1628
1629   if (sinkpad)
1630     gst_object_ref (sinkpad);
1631   GST_PLAY_BIN_UNLOCK (playbin);
1632
1633   if (sinkpad) {
1634     GstObject *selector;
1635
1636     if ((selector = gst_pad_get_parent (sinkpad))) {
1637       /* activate the selected pad */
1638       g_object_set (selector, "active-pad", sinkpad, NULL);
1639       gst_object_unref (selector);
1640     }
1641     gst_object_unref (sinkpad);
1642   }
1643   return TRUE;
1644
1645 no_channels:
1646   {
1647     GST_PLAY_BIN_UNLOCK (playbin);
1648     GST_DEBUG_OBJECT (playbin, "can't switch video, we have no channels");
1649     return FALSE;
1650   }
1651 }
1652
1653 static gboolean
1654 gst_play_bin_set_current_audio_stream (GstPlayBin * playbin, gint stream)
1655 {
1656   GstSourceGroup *group;
1657   GPtrArray *channels;
1658   GstPad *sinkpad;
1659
1660   GST_PLAY_BIN_LOCK (playbin);
1661
1662   GST_DEBUG_OBJECT (playbin, "Changing current audio stream %d -> %d",
1663       playbin->current_audio, stream);
1664
1665   group = get_group (playbin);
1666   if (!(channels = group->audio_channels))
1667     goto no_channels;
1668
1669   if (stream == -1 || channels->len <= stream) {
1670     sinkpad = NULL;
1671   } else {
1672     /* take channel from selected stream */
1673     sinkpad = g_ptr_array_index (channels, stream);
1674   }
1675
1676   if (sinkpad)
1677     gst_object_ref (sinkpad);
1678   GST_PLAY_BIN_UNLOCK (playbin);
1679
1680   if (sinkpad) {
1681     GstObject *selector;
1682
1683     if ((selector = gst_pad_get_parent (sinkpad))) {
1684       /* activate the selected pad */
1685       g_object_set (selector, "active-pad", sinkpad, NULL);
1686       gst_object_unref (selector);
1687     }
1688     gst_object_unref (sinkpad);
1689   }
1690   return TRUE;
1691
1692 no_channels:
1693   {
1694     GST_PLAY_BIN_UNLOCK (playbin);
1695     GST_DEBUG_OBJECT (playbin, "can't switch audio, we have no channels");
1696     return FALSE;
1697   }
1698 }
1699
1700 static void
1701 gst_play_bin_suburidecodebin_seek_to_start (GstElement * suburidecodebin)
1702 {
1703   GstIterator *it = gst_element_iterate_src_pads (suburidecodebin);
1704   GstPad *sinkpad;
1705   GValue item = { 0, };
1706
1707   if (it && gst_iterator_next (it, &item) == GST_ITERATOR_OK
1708       && ((sinkpad = g_value_get_object (&item)) != NULL)) {
1709     GstEvent *event;
1710
1711     event =
1712         gst_event_new_seek (1.0, GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
1713         GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, -1);
1714     if (!gst_pad_send_event (sinkpad, event)) {
1715       event =
1716           gst_event_new_seek (1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
1717           GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, -1);
1718       if (!gst_pad_send_event (sinkpad, event))
1719         GST_DEBUG_OBJECT (suburidecodebin, "Seeking to the beginning failed!");
1720     }
1721
1722     g_value_unset (&item);
1723   }
1724
1725   if (it)
1726     gst_iterator_free (it);
1727 }
1728
1729 static void
1730 gst_play_bin_suburidecodebin_block (GstSourceGroup * group,
1731     GstElement * suburidecodebin, gboolean block)
1732 {
1733   GstIterator *it = gst_element_iterate_src_pads (suburidecodebin);
1734   gboolean done = FALSE;
1735   GValue item = { 0, };
1736
1737   GST_DEBUG_OBJECT (suburidecodebin, "Blocking suburidecodebin: %d", block);
1738
1739   if (!it)
1740     return;
1741   while (!done) {
1742     GstPad *sinkpad;
1743
1744     switch (gst_iterator_next (it, &item)) {
1745       case GST_ITERATOR_OK:
1746         sinkpad = g_value_get_object (&item);
1747         if (block) {
1748           group->block_id =
1749               gst_pad_add_probe (sinkpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
1750               NULL, NULL, NULL);
1751         } else if (group->block_id) {
1752           gst_pad_remove_probe (sinkpad, group->block_id);
1753           group->block_id = 0;
1754         }
1755         g_value_reset (&item);
1756         break;
1757       case GST_ITERATOR_DONE:
1758         done = TRUE;
1759         break;
1760       case GST_ITERATOR_RESYNC:
1761         gst_iterator_resync (it);
1762         break;
1763       case GST_ITERATOR_ERROR:
1764         done = TRUE;
1765         break;
1766     }
1767   }
1768   g_value_unset (&item);
1769   gst_iterator_free (it);
1770 }
1771
1772 static gboolean
1773 gst_play_bin_set_current_text_stream (GstPlayBin * playbin, gint stream)
1774 {
1775   GstSourceGroup *group;
1776   GPtrArray *channels;
1777   GstPad *sinkpad;
1778
1779   GST_PLAY_BIN_LOCK (playbin);
1780
1781   GST_DEBUG_OBJECT (playbin, "Changing current text stream %d -> %d",
1782       playbin->current_text, stream);
1783
1784   group = get_group (playbin);
1785   if (!(channels = group->text_channels))
1786     goto no_channels;
1787
1788   if (stream == -1 || channels->len <= stream) {
1789     sinkpad = NULL;
1790   } else {
1791     /* take channel from selected stream */
1792     sinkpad = g_ptr_array_index (channels, stream);
1793   }
1794
1795   if (sinkpad)
1796     gst_object_ref (sinkpad);
1797   GST_PLAY_BIN_UNLOCK (playbin);
1798
1799   if (sinkpad) {
1800     GstObject *selector;
1801
1802     if ((selector = gst_pad_get_parent (sinkpad))) {
1803       GstPad *old_sinkpad;
1804
1805       g_object_get (selector, "active-pad", &old_sinkpad, NULL);
1806
1807       if (old_sinkpad != sinkpad) {
1808         gboolean need_unblock, need_block, need_seek;
1809         GstPad *src, *peer = NULL, *oldpeer = NULL;
1810         GstElement *parent_element = NULL, *old_parent_element = NULL;
1811
1812         /* Now check if we need to seek the suburidecodebin to the beginning
1813          * or if we need to block all suburidecodebin sinkpads or if we need
1814          * to unblock all suburidecodebin sinkpads
1815          */
1816         if (sinkpad)
1817           peer = gst_pad_get_peer (sinkpad);
1818         if (old_sinkpad)
1819           oldpeer = gst_pad_get_peer (old_sinkpad);
1820
1821         if (peer)
1822           parent_element = gst_pad_get_parent_element (peer);
1823         if (oldpeer)
1824           old_parent_element = gst_pad_get_parent_element (oldpeer);
1825
1826         need_block = (old_parent_element == group->suburidecodebin
1827             && parent_element != old_parent_element);
1828         need_unblock = (parent_element == group->suburidecodebin
1829             && parent_element != old_parent_element);
1830         need_seek = (parent_element == group->suburidecodebin);
1831
1832         if (peer)
1833           gst_object_unref (peer);
1834         if (oldpeer)
1835           gst_object_unref (oldpeer);
1836         if (parent_element)
1837           gst_object_unref (parent_element);
1838         if (old_parent_element)
1839           gst_object_unref (old_parent_element);
1840
1841         /* Block all suburidecodebin sinkpads */
1842         if (need_block)
1843           gst_play_bin_suburidecodebin_block (group, group->suburidecodebin,
1844               TRUE);
1845
1846         /* activate the selected pad */
1847         g_object_set (selector, "active-pad", sinkpad, NULL);
1848
1849         src = gst_element_get_static_pad (GST_ELEMENT_CAST (selector), "src");
1850         peer = gst_pad_get_peer (src);
1851         if (peer) {
1852           GstStructure *s;
1853           GstEvent *event;
1854           /* Flush the subtitle renderer to remove any
1855            * currently displayed subtitles. This event will
1856            * never travel outside subtitleoverlay!
1857            */
1858           s = gst_structure_new_empty ("subtitleoverlay-flush-subtitle");
1859           event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_OOB, s);
1860           gst_pad_send_event (peer, event);
1861           gst_object_unref (peer);
1862         }
1863         gst_object_unref (src);
1864
1865         /* Unblock pads if necessary */
1866         if (need_unblock)
1867           gst_play_bin_suburidecodebin_block (group, group->suburidecodebin,
1868               FALSE);
1869
1870         /* seek to the beginning */
1871         if (need_seek)
1872           gst_play_bin_suburidecodebin_seek_to_start (group->suburidecodebin);
1873       }
1874       gst_object_unref (selector);
1875
1876       if (old_sinkpad)
1877         gst_object_unref (old_sinkpad);
1878     }
1879     gst_object_unref (sinkpad);
1880   }
1881   return TRUE;
1882
1883 no_channels:
1884   {
1885     GST_PLAY_BIN_UNLOCK (playbin);
1886     return FALSE;
1887   }
1888 }
1889
1890 static void
1891 gst_play_bin_set_sink (GstPlayBin * playbin, GstElement ** elem,
1892     const gchar * dbg, GstElement * sink)
1893 {
1894   GST_INFO_OBJECT (playbin, "Setting %s sink to %" GST_PTR_FORMAT, dbg, sink);
1895
1896   GST_PLAY_BIN_LOCK (playbin);
1897   if (*elem != sink) {
1898     GstElement *old;
1899
1900     old = *elem;
1901     if (sink)
1902       gst_object_ref_sink (sink);
1903
1904     *elem = sink;
1905     if (old)
1906       gst_object_unref (old);
1907   }
1908   GST_LOG_OBJECT (playbin, "%s sink now %" GST_PTR_FORMAT, dbg, *elem);
1909   GST_PLAY_BIN_UNLOCK (playbin);
1910 }
1911
1912 static void
1913 gst_play_bin_set_encoding (GstPlayBin * playbin, const gchar * encoding)
1914 {
1915   GstElement *elem;
1916
1917   GST_PLAY_BIN_LOCK (playbin);
1918
1919   /* set subtitles on all current and next decodebins. */
1920   if ((elem = playbin->groups[0].uridecodebin))
1921     g_object_set (G_OBJECT (elem), "subtitle-encoding", encoding, NULL);
1922   if ((elem = playbin->groups[0].suburidecodebin))
1923     g_object_set (G_OBJECT (elem), "subtitle-encoding", encoding, NULL);
1924   if ((elem = playbin->groups[1].uridecodebin))
1925     g_object_set (G_OBJECT (elem), "subtitle-encoding", encoding, NULL);
1926   if ((elem = playbin->groups[1].suburidecodebin))
1927     g_object_set (G_OBJECT (elem), "subtitle-encoding", encoding, NULL);
1928
1929   gst_play_sink_set_subtitle_encoding (playbin->playsink, encoding);
1930   GST_PLAY_BIN_UNLOCK (playbin);
1931 }
1932
1933 static void
1934 gst_play_bin_set_property (GObject * object, guint prop_id,
1935     const GValue * value, GParamSpec * pspec)
1936 {
1937   GstPlayBin *playbin = GST_PLAY_BIN (object);
1938
1939   switch (prop_id) {
1940     case PROP_URI:
1941       gst_play_bin_set_uri (playbin, g_value_get_string (value));
1942       break;
1943     case PROP_SUBURI:
1944       gst_play_bin_set_suburi (playbin, g_value_get_string (value));
1945       break;
1946     case PROP_FLAGS:
1947       gst_play_bin_set_flags (playbin, g_value_get_flags (value));
1948       break;
1949     case PROP_CURRENT_VIDEO:
1950       gst_play_bin_set_current_video_stream (playbin, g_value_get_int (value));
1951       break;
1952     case PROP_CURRENT_AUDIO:
1953       gst_play_bin_set_current_audio_stream (playbin, g_value_get_int (value));
1954       break;
1955     case PROP_CURRENT_TEXT:
1956       gst_play_bin_set_current_text_stream (playbin, g_value_get_int (value));
1957       break;
1958     case PROP_SUBTITLE_ENCODING:
1959       gst_play_bin_set_encoding (playbin, g_value_get_string (value));
1960       break;
1961     case PROP_VIDEO_SINK:
1962       gst_play_bin_set_sink (playbin, &playbin->video_sink, "video",
1963           g_value_get_object (value));
1964       break;
1965     case PROP_AUDIO_SINK:
1966       gst_play_bin_set_sink (playbin, &playbin->audio_sink, "audio",
1967           g_value_get_object (value));
1968       break;
1969     case PROP_VIS_PLUGIN:
1970       gst_play_sink_set_vis_plugin (playbin->playsink,
1971           g_value_get_object (value));
1972       break;
1973     case PROP_TEXT_SINK:
1974       gst_play_bin_set_sink (playbin, &playbin->text_sink, "text",
1975           g_value_get_object (value));
1976       break;
1977     case PROP_VOLUME:
1978       gst_play_sink_set_volume (playbin->playsink, g_value_get_double (value));
1979       break;
1980     case PROP_MUTE:
1981       gst_play_sink_set_mute (playbin->playsink, g_value_get_boolean (value));
1982       break;
1983     case PROP_FONT_DESC:
1984       gst_play_sink_set_font_desc (playbin->playsink,
1985           g_value_get_string (value));
1986       break;
1987     case PROP_CONNECTION_SPEED:
1988       GST_PLAY_BIN_LOCK (playbin);
1989       playbin->connection_speed = g_value_get_uint64 (value) * 1000;
1990       GST_PLAY_BIN_UNLOCK (playbin);
1991       break;
1992     case PROP_BUFFER_SIZE:
1993       playbin->buffer_size = g_value_get_int (value);
1994       break;
1995     case PROP_BUFFER_DURATION:
1996       playbin->buffer_duration = g_value_get_int64 (value);
1997       break;
1998     case PROP_AV_OFFSET:
1999       gst_play_sink_set_av_offset (playbin->playsink,
2000           g_value_get_int64 (value));
2001       break;
2002     case PROP_RING_BUFFER_MAX_SIZE:
2003       playbin->ring_buffer_max_size = g_value_get_uint64 (value);
2004       break;
2005     default:
2006       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2007       break;
2008   }
2009 }
2010
2011 static GstElement *
2012 gst_play_bin_get_current_sink (GstPlayBin * playbin, GstElement ** elem,
2013     const gchar * dbg, GstPlaySinkType type)
2014 {
2015   GstElement *sink = gst_play_sink_get_sink (playbin->playsink, type);
2016
2017   GST_LOG_OBJECT (playbin, "play_sink_get_sink() returned %s sink %"
2018       GST_PTR_FORMAT ", the originally set %s sink is %" GST_PTR_FORMAT,
2019       dbg, sink, dbg, *elem);
2020
2021   if (sink == NULL) {
2022     GST_PLAY_BIN_LOCK (playbin);
2023     if ((sink = *elem))
2024       gst_object_ref (sink);
2025     GST_PLAY_BIN_UNLOCK (playbin);
2026   }
2027
2028   return sink;
2029 }
2030
2031 static void
2032 gst_play_bin_get_property (GObject * object, guint prop_id, GValue * value,
2033     GParamSpec * pspec)
2034 {
2035   GstPlayBin *playbin = GST_PLAY_BIN (object);
2036
2037   switch (prop_id) {
2038     case PROP_URI:
2039     {
2040       GstSourceGroup *group;
2041
2042       GST_PLAY_BIN_LOCK (playbin);
2043       group = get_group (playbin);
2044       g_value_set_string (value, group->uri);
2045       GST_PLAY_BIN_UNLOCK (playbin);
2046       break;
2047     }
2048     case PROP_SUBURI:
2049     {
2050       GstSourceGroup *group;
2051
2052       GST_PLAY_BIN_LOCK (playbin);
2053       group = get_group (playbin);
2054       g_value_set_string (value, group->suburi);
2055       GST_PLAY_BIN_UNLOCK (playbin);
2056       break;
2057     }
2058     case PROP_SOURCE:
2059     {
2060       GST_OBJECT_LOCK (playbin);
2061       g_value_set_object (value, playbin->source);
2062       GST_OBJECT_UNLOCK (playbin);
2063       break;
2064     }
2065     case PROP_FLAGS:
2066       g_value_set_flags (value, gst_play_bin_get_flags (playbin));
2067       break;
2068     case PROP_N_VIDEO:
2069     {
2070       GstSourceGroup *group;
2071       gint n_video;
2072
2073       GST_PLAY_BIN_LOCK (playbin);
2074       group = get_group (playbin);
2075       n_video = (group->video_channels ? group->video_channels->len : 0);
2076       g_value_set_int (value, n_video);
2077       GST_PLAY_BIN_UNLOCK (playbin);
2078       break;
2079     }
2080     case PROP_CURRENT_VIDEO:
2081       GST_PLAY_BIN_LOCK (playbin);
2082       g_value_set_int (value, playbin->current_video);
2083       GST_PLAY_BIN_UNLOCK (playbin);
2084       break;
2085     case PROP_N_AUDIO:
2086     {
2087       GstSourceGroup *group;
2088       gint n_audio;
2089
2090       GST_PLAY_BIN_LOCK (playbin);
2091       group = get_group (playbin);
2092       n_audio = (group->audio_channels ? group->audio_channels->len : 0);
2093       g_value_set_int (value, n_audio);
2094       GST_PLAY_BIN_UNLOCK (playbin);
2095       break;
2096     }
2097     case PROP_CURRENT_AUDIO:
2098       GST_PLAY_BIN_LOCK (playbin);
2099       g_value_set_int (value, playbin->current_audio);
2100       GST_PLAY_BIN_UNLOCK (playbin);
2101       break;
2102     case PROP_N_TEXT:
2103     {
2104       GstSourceGroup *group;
2105       gint n_text;
2106
2107       GST_PLAY_BIN_LOCK (playbin);
2108       group = get_group (playbin);
2109       n_text = (group->text_channels ? group->text_channels->len : 0);
2110       g_value_set_int (value, n_text);
2111       GST_PLAY_BIN_UNLOCK (playbin);
2112       break;
2113     }
2114     case PROP_CURRENT_TEXT:
2115       GST_PLAY_BIN_LOCK (playbin);
2116       g_value_set_int (value, playbin->current_text);
2117       GST_PLAY_BIN_UNLOCK (playbin);
2118       break;
2119     case PROP_SUBTITLE_ENCODING:
2120       GST_PLAY_BIN_LOCK (playbin);
2121       g_value_take_string (value,
2122           gst_play_sink_get_subtitle_encoding (playbin->playsink));
2123       GST_PLAY_BIN_UNLOCK (playbin);
2124       break;
2125     case PROP_VIDEO_SINK:
2126       g_value_take_object (value,
2127           gst_play_bin_get_current_sink (playbin, &playbin->video_sink,
2128               "video", GST_PLAY_SINK_TYPE_VIDEO));
2129       break;
2130     case PROP_AUDIO_SINK:
2131       g_value_take_object (value,
2132           gst_play_bin_get_current_sink (playbin, &playbin->audio_sink,
2133               "audio", GST_PLAY_SINK_TYPE_AUDIO));
2134       break;
2135     case PROP_VIS_PLUGIN:
2136       g_value_take_object (value,
2137           gst_play_sink_get_vis_plugin (playbin->playsink));
2138       break;
2139     case PROP_TEXT_SINK:
2140       g_value_take_object (value,
2141           gst_play_bin_get_current_sink (playbin, &playbin->text_sink,
2142               "text", GST_PLAY_SINK_TYPE_TEXT));
2143       break;
2144     case PROP_VOLUME:
2145       g_value_set_double (value, gst_play_sink_get_volume (playbin->playsink));
2146       break;
2147     case PROP_MUTE:
2148       g_value_set_boolean (value, gst_play_sink_get_mute (playbin->playsink));
2149       break;
2150     case PROP_SAMPLE:
2151       gst_value_take_sample (value,
2152           gst_play_sink_get_last_sample (playbin->playsink));
2153       break;
2154     case PROP_FONT_DESC:
2155       g_value_take_string (value,
2156           gst_play_sink_get_font_desc (playbin->playsink));
2157       break;
2158     case PROP_CONNECTION_SPEED:
2159       GST_PLAY_BIN_LOCK (playbin);
2160       g_value_set_uint64 (value, playbin->connection_speed / 1000);
2161       GST_PLAY_BIN_UNLOCK (playbin);
2162       break;
2163     case PROP_BUFFER_SIZE:
2164       GST_OBJECT_LOCK (playbin);
2165       g_value_set_int (value, playbin->buffer_size);
2166       GST_OBJECT_UNLOCK (playbin);
2167       break;
2168     case PROP_BUFFER_DURATION:
2169       GST_OBJECT_LOCK (playbin);
2170       g_value_set_int64 (value, playbin->buffer_duration);
2171       GST_OBJECT_UNLOCK (playbin);
2172       break;
2173     case PROP_AV_OFFSET:
2174       g_value_set_int64 (value,
2175           gst_play_sink_get_av_offset (playbin->playsink));
2176       break;
2177     case PROP_RING_BUFFER_MAX_SIZE:
2178       g_value_set_uint64 (value, playbin->ring_buffer_max_size);
2179       break;
2180     default:
2181       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2182       break;
2183   }
2184 }
2185
2186 static void
2187 gst_play_bin_update_cached_duration_from_query (GstPlayBin * playbin,
2188     gboolean valid, GstQuery * query)
2189 {
2190   GstFormat fmt;
2191   gint64 duration;
2192   gint i;
2193
2194   GST_DEBUG_OBJECT (playbin, "Updating cached duration from query");
2195   gst_query_parse_duration (query, &fmt, &duration);
2196
2197   for (i = 0; i < G_N_ELEMENTS (playbin->duration); i++) {
2198     if (playbin->duration[i].format == 0 || fmt == playbin->duration[i].format) {
2199       playbin->duration[i].valid = valid;
2200       playbin->duration[i].format = fmt;
2201       playbin->duration[i].duration = valid ? duration : -1;
2202       break;
2203     }
2204   }
2205 }
2206
2207 static void
2208 gst_play_bin_update_cached_duration (GstPlayBin * playbin)
2209 {
2210   const GstFormat formats[] =
2211       { GST_FORMAT_TIME, GST_FORMAT_BYTES, GST_FORMAT_DEFAULT };
2212   gboolean ret;
2213   GstQuery *query;
2214   gint i;
2215
2216   GST_DEBUG_OBJECT (playbin, "Updating cached durations before group switch");
2217   for (i = 0; i < G_N_ELEMENTS (formats); i++) {
2218     query = gst_query_new_duration (formats[i]);
2219     ret =
2220         GST_ELEMENT_CLASS (parent_class)->query (GST_ELEMENT_CAST (playbin),
2221         query);
2222     gst_play_bin_update_cached_duration_from_query (playbin, ret, query);
2223     gst_query_unref (query);
2224   }
2225 }
2226
2227 static gboolean
2228 gst_play_bin_query (GstElement * element, GstQuery * query)
2229 {
2230   GstPlayBin *playbin = GST_PLAY_BIN (element);
2231   gboolean ret;
2232
2233   /* During a group switch we shouldn't allow duration queries
2234    * because it's not clear if the old or new group's duration
2235    * is returned and if the sinks are already playing new data
2236    * or old data. See bug #585969
2237    *
2238    * While we're at it, also don't do any other queries during
2239    * a group switch or any other event that causes topology changes
2240    * by taking the playbin lock in any case.
2241    */
2242   GST_PLAY_BIN_LOCK (playbin);
2243
2244   if (GST_QUERY_TYPE (query) == GST_QUERY_DURATION) {
2245     GstSourceGroup *group = playbin->curr_group;
2246     gboolean pending;
2247
2248     GST_SOURCE_GROUP_LOCK (group);
2249     if (group->stream_changed_pending_lock.p) {
2250       g_mutex_lock (&group->stream_changed_pending_lock);
2251       pending = group->pending || group->stream_changed_pending;
2252       g_mutex_unlock (&group->stream_changed_pending_lock);
2253     } else {
2254       pending = group->pending;
2255     }
2256     if (pending) {
2257       GstFormat fmt;
2258       gint i;
2259
2260       ret = FALSE;
2261       gst_query_parse_duration (query, &fmt, NULL);
2262       for (i = 0; i < G_N_ELEMENTS (playbin->duration); i++) {
2263         if (fmt == playbin->duration[i].format) {
2264           ret = playbin->duration[i].valid;
2265           gst_query_set_duration (query, fmt,
2266               (ret ? playbin->duration[i].duration : -1));
2267           break;
2268         }
2269       }
2270       /* if nothing cached yet, we might as well request duration,
2271        * such as during initial startup */
2272       if (ret) {
2273         GST_DEBUG_OBJECT (playbin,
2274             "Taking cached duration because of pending group switch: %d", ret);
2275         GST_SOURCE_GROUP_UNLOCK (group);
2276         GST_PLAY_BIN_UNLOCK (playbin);
2277         return ret;
2278       }
2279     }
2280     GST_SOURCE_GROUP_UNLOCK (group);
2281   }
2282
2283   ret = GST_ELEMENT_CLASS (parent_class)->query (element, query);
2284
2285   if (GST_QUERY_TYPE (query) == GST_QUERY_DURATION)
2286     gst_play_bin_update_cached_duration_from_query (playbin, ret, query);
2287   GST_PLAY_BIN_UNLOCK (playbin);
2288
2289   return ret;
2290 }
2291
2292 /* mime types we are not handling on purpose right now, don't post a
2293  * missing-plugin message for these */
2294 static const gchar *blacklisted_mimes[] = {
2295   NULL
2296 };
2297
2298 static void
2299 gst_play_bin_handle_message (GstBin * bin, GstMessage * msg)
2300 {
2301   GstPlayBin *playbin = GST_PLAY_BIN (bin);
2302   GstSourceGroup *group;
2303
2304   if (gst_is_missing_plugin_message (msg)) {
2305     gchar *detail;
2306     guint i;
2307
2308     detail = gst_missing_plugin_message_get_installer_detail (msg);
2309     for (i = 0; detail != NULL && blacklisted_mimes[i] != NULL; ++i) {
2310       if (strstr (detail, "|decoder-") && strstr (detail, blacklisted_mimes[i])) {
2311         GST_LOG_OBJECT (bin, "suppressing message %" GST_PTR_FORMAT, msg);
2312         gst_message_unref (msg);
2313         g_free (detail);
2314         return;
2315       }
2316     }
2317     g_free (detail);
2318   } else if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ELEMENT) {
2319     const GstStructure *s = gst_message_get_structure (msg);
2320
2321     /* Drop all stream-changed messages except the last one */
2322     if (strcmp ("playbin-stream-changed", gst_structure_get_name (s)) == 0) {
2323       guint32 seqnum = gst_message_get_seqnum (msg);
2324       GList *l, *l_prev;
2325
2326       group = playbin->curr_group;
2327       g_mutex_lock (&group->stream_changed_pending_lock);
2328       for (l = group->stream_changed_pending; l;) {
2329         guint32 l_seqnum = GPOINTER_TO_UINT (l->data);
2330
2331         if (l_seqnum == seqnum) {
2332           l_prev = l;
2333           l = l->next;
2334           group->stream_changed_pending =
2335               g_list_delete_link (group->stream_changed_pending, l_prev);
2336           if (group->stream_changed_pending) {
2337             gst_message_unref (msg);
2338             msg = NULL;
2339             break;
2340           }
2341         } else {
2342           l = l->next;
2343         }
2344       }
2345       g_mutex_unlock (&group->stream_changed_pending_lock);
2346     }
2347   } else if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ASYNC_START ||
2348       GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ASYNC_DONE) {
2349     GstObject *src = GST_OBJECT_CAST (msg->src);
2350
2351     /* Ignore async state changes from the uridecodebin children,
2352      * see bug #602000. */
2353     group = playbin->curr_group;
2354     if (src && (group = playbin->curr_group) &&
2355         ((group->uridecodebin && src == GST_OBJECT_CAST (group->uridecodebin))
2356             || (group->suburidecodebin
2357                 && src == GST_OBJECT_CAST (group->suburidecodebin)))) {
2358       GST_DEBUG_OBJECT (playbin,
2359           "Ignoring async state change of uridecodebin: %s",
2360           GST_OBJECT_NAME (src));
2361       gst_message_unref (msg);
2362       msg = NULL;
2363     }
2364   } else if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
2365     /* If we get an error of the subtitle uridecodebin transform
2366      * them into warnings and disable the subtitles */
2367     group = playbin->curr_group;
2368     if (group && group->suburidecodebin) {
2369       if (G_UNLIKELY (gst_object_has_ancestor (msg->src, GST_OBJECT_CAST
2370                   (group->suburidecodebin)))) {
2371         GError *err;
2372         gchar *debug = NULL;
2373         GstMessage *new_msg;
2374         GstIterator *it;
2375         gboolean done = FALSE;
2376         GValue item = { 0, };
2377
2378         gst_message_parse_error (msg, &err, &debug);
2379         new_msg = gst_message_new_warning (msg->src, err, debug);
2380
2381         gst_message_unref (msg);
2382         g_error_free (err);
2383         g_free (debug);
2384         msg = new_msg;
2385
2386         REMOVE_SIGNAL (group->suburidecodebin, group->sub_pad_added_id);
2387         REMOVE_SIGNAL (group->suburidecodebin, group->sub_pad_removed_id);
2388         REMOVE_SIGNAL (group->suburidecodebin, group->sub_no_more_pads_id);
2389         REMOVE_SIGNAL (group->suburidecodebin, group->sub_autoplug_continue_id);
2390
2391         it = gst_element_iterate_src_pads (group->suburidecodebin);
2392         while (it && !done) {
2393           GstPad *p = NULL;
2394           GstIteratorResult res;
2395
2396           res = gst_iterator_next (it, &item);
2397
2398           switch (res) {
2399             case GST_ITERATOR_DONE:
2400               done = TRUE;
2401               break;
2402             case GST_ITERATOR_OK:
2403               p = g_value_get_object (&item);
2404               pad_removed_cb (NULL, p, group);
2405               g_value_reset (&item);
2406               break;
2407
2408             case GST_ITERATOR_RESYNC:
2409               gst_iterator_resync (it);
2410               break;
2411             case GST_ITERATOR_ERROR:
2412               done = TRUE;
2413               break;
2414           }
2415         }
2416         g_value_unset (&item);
2417         if (it)
2418           gst_iterator_free (it);
2419
2420         gst_object_ref (group->suburidecodebin);
2421         gst_bin_remove (bin, group->suburidecodebin);
2422         gst_element_set_locked_state (group->suburidecodebin, FALSE);
2423
2424         if (group->sub_pending) {
2425           group->sub_pending = FALSE;
2426           no_more_pads_cb (NULL, group);
2427         }
2428       }
2429     }
2430   }
2431
2432   if (msg)
2433     GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
2434 }
2435
2436 static void
2437 selector_active_pad_changed (GObject * selector, GParamSpec * pspec,
2438     GstPlayBin * playbin)
2439 {
2440   const gchar *property;
2441   GstSourceGroup *group;
2442   GstSourceSelect *select = NULL;
2443   int i;
2444
2445   GST_PLAY_BIN_LOCK (playbin);
2446   group = get_group (playbin);
2447
2448   for (i = 0; i < PLAYBIN_STREAM_LAST; i++) {
2449     if (selector == G_OBJECT (group->selector[i].selector)) {
2450       select = &group->selector[i];
2451     }
2452   }
2453
2454   /* We got a pad-change after our group got switched out; no need to notify */
2455   if (!select) {
2456     GST_PLAY_BIN_UNLOCK (playbin);
2457     return;
2458   }
2459
2460   switch (select->type) {
2461     case GST_PLAY_SINK_TYPE_VIDEO:
2462     case GST_PLAY_SINK_TYPE_VIDEO_RAW:
2463       property = "current-video";
2464       playbin->current_video = get_current_stream_number (playbin,
2465           group->video_channels);
2466       break;
2467     case GST_PLAY_SINK_TYPE_AUDIO:
2468     case GST_PLAY_SINK_TYPE_AUDIO_RAW:
2469       property = "current-audio";
2470       playbin->current_audio = get_current_stream_number (playbin,
2471           group->audio_channels);
2472       break;
2473     case GST_PLAY_SINK_TYPE_TEXT:
2474       property = "current-text";
2475       playbin->current_text = get_current_stream_number (playbin,
2476           group->text_channels);
2477       break;
2478     default:
2479       property = NULL;
2480   }
2481   GST_PLAY_BIN_UNLOCK (playbin);
2482
2483   if (property)
2484     g_object_notify (G_OBJECT (playbin), property);
2485 }
2486
2487 /* this callback sends a delayed event once the pad becomes unblocked */
2488 static GstPadProbeReturn
2489 stream_changed_data_probe (GstPad * pad, GstPadProbeInfo * info, gpointer data)
2490 {
2491   GstMiniObject *object = GST_PAD_PROBE_INFO_DATA (info);
2492   GstSourceSelect *select = (GstSourceSelect *) data;
2493   GstEvent *e;
2494
2495   /* we need do this just once, so cleanup first */
2496   gst_pad_remove_probe (pad, select->sinkpad_data_probe);
2497   select->sinkpad_data_probe = 0;
2498   e = select->sinkpad_delayed_event;
2499   select->sinkpad_delayed_event = NULL;
2500
2501   /* really, this should not happen */
2502   if (!e) {
2503     GST_WARNING ("Data probed called, but no delayed event");
2504     return GST_PAD_PROBE_OK;
2505   }
2506
2507   if (GST_IS_EVENT (object)
2508       && GST_EVENT_TYPE (GST_EVENT_CAST (object)) == GST_EVENT_SEGMENT) {
2509     /* push the event first, then send the delayed one */
2510     gst_event_ref (GST_EVENT_CAST (object));
2511     gst_pad_send_event (pad, GST_EVENT_CAST (object));
2512     gst_pad_send_event (pad, e);
2513     return GST_PAD_PROBE_DROP;
2514   } else {
2515     /* send delayed event, then allow the caller to go on */
2516     gst_pad_send_event (pad, e);
2517     return GST_PAD_PROBE_OK;
2518   }
2519 }
2520
2521 /* helper function to lookup stuff in lists */
2522 static gboolean
2523 array_has_value (const gchar * values[], const gchar * value, gboolean exact)
2524 {
2525   gint i;
2526
2527   for (i = 0; values[i]; i++) {
2528     if (exact && !strcmp (value, values[i]))
2529       return TRUE;
2530     if (!exact && g_str_has_prefix (value, values[i]))
2531       return TRUE;
2532   }
2533   return FALSE;
2534 }
2535
2536 typedef struct
2537 {
2538   GstPlayBin *playbin;
2539   gint stream_id;
2540   GstPlaySinkType type;
2541 } NotifyTagsData;
2542
2543 static void
2544 notify_tags_cb (GObject * object, GParamSpec * pspec, gpointer user_data)
2545 {
2546   NotifyTagsData *ntdata = (NotifyTagsData *) user_data;
2547   gint signal;
2548
2549   GST_DEBUG_OBJECT (ntdata->playbin, "Tags on pad %" GST_PTR_FORMAT
2550       " with stream id %d and type %d have changed",
2551       object, ntdata->stream_id, ntdata->type);
2552
2553   switch (ntdata->type) {
2554     case GST_PLAY_SINK_TYPE_VIDEO:
2555     case GST_PLAY_SINK_TYPE_VIDEO_RAW:
2556       signal = SIGNAL_VIDEO_TAGS_CHANGED;
2557       break;
2558     case GST_PLAY_SINK_TYPE_AUDIO:
2559     case GST_PLAY_SINK_TYPE_AUDIO_RAW:
2560       signal = SIGNAL_AUDIO_TAGS_CHANGED;
2561       break;
2562     case GST_PLAY_SINK_TYPE_TEXT:
2563       signal = SIGNAL_TEXT_TAGS_CHANGED;
2564       break;
2565     default:
2566       signal = -1;
2567       break;
2568   }
2569
2570   if (signal >= 0)
2571     g_signal_emit (G_OBJECT (ntdata->playbin), gst_play_bin_signals[signal], 0,
2572         ntdata->stream_id);
2573 }
2574
2575 /* this function is called when a new pad is added to decodebin. We check the
2576  * type of the pad and add it to the selector element of the group.
2577  */
2578 static void
2579 pad_added_cb (GstElement * decodebin, GstPad * pad, GstSourceGroup * group)
2580 {
2581   GstPlayBin *playbin;
2582   GstCaps *caps;
2583   const GstStructure *s;
2584   const gchar *name;
2585   GstPad *sinkpad;
2586   GstPadLinkReturn res;
2587   GstSourceSelect *select = NULL;
2588   gint i, pass;
2589   gboolean changed = FALSE;
2590
2591   playbin = group->playbin;
2592
2593   caps = gst_pad_query_caps (pad, NULL);
2594   s = gst_caps_get_structure (caps, 0);
2595   name = gst_structure_get_name (s);
2596
2597   GST_DEBUG_OBJECT (playbin,
2598       "pad %s:%s with caps %" GST_PTR_FORMAT " added in group %p",
2599       GST_DEBUG_PAD_NAME (pad), caps, group);
2600
2601   /* major type of the pad, this determines the selector to use,
2602      try exact match first so we don't prematurely match video/
2603      for video/x-dvd-subpicture */
2604   for (pass = 0; !select && pass < 2; pass++) {
2605     for (i = 0; i < PLAYBIN_STREAM_LAST; i++) {
2606       if (array_has_value (group->selector[i].media_list, name, pass == 0)) {
2607         select = &group->selector[i];
2608         break;
2609       } else if (group->selector[i].get_media_caps) {
2610         GstCaps *media_caps = group->selector[i].get_media_caps ();
2611
2612         if (media_caps && gst_caps_can_intersect (media_caps, caps)) {
2613           select = &group->selector[i];
2614           gst_caps_unref (media_caps);
2615           break;
2616         }
2617         gst_caps_unref (media_caps);
2618       }
2619     }
2620   }
2621   /* no selector found for the media type, don't bother linking it to a
2622    * selector. This will leave the pad unlinked and thus ignored. */
2623   if (select == NULL)
2624     goto unknown_type;
2625
2626   GST_SOURCE_GROUP_LOCK (group);
2627   if (select->selector == NULL && playbin->have_selector) {
2628     /* no selector, create one */
2629     GST_DEBUG_OBJECT (playbin, "creating new input selector");
2630     select->selector = gst_element_factory_make ("input-selector", NULL);
2631     if (select->selector == NULL) {
2632       /* post the missing selector message only once */
2633       playbin->have_selector = FALSE;
2634       gst_element_post_message (GST_ELEMENT_CAST (playbin),
2635           gst_missing_element_message_new (GST_ELEMENT_CAST (playbin),
2636               "input-selector"));
2637       GST_ELEMENT_WARNING (playbin, CORE, MISSING_PLUGIN,
2638           (_("Missing element '%s' - check your GStreamer installation."),
2639               "input-selector"), (NULL));
2640     } else {
2641       g_object_set (select->selector, "sync-streams", TRUE, NULL);
2642
2643       g_signal_connect (select->selector, "notify::active-pad",
2644           G_CALLBACK (selector_active_pad_changed), playbin);
2645
2646       GST_DEBUG_OBJECT (playbin, "adding new selector %p", select->selector);
2647       gst_bin_add (GST_BIN_CAST (playbin), select->selector);
2648       gst_element_set_state (select->selector, GST_STATE_PAUSED);
2649     }
2650   }
2651
2652   if (select->srcpad == NULL) {
2653     if (select->selector) {
2654       /* save source pad of the selector */
2655       select->srcpad = gst_element_get_static_pad (select->selector, "src");
2656     } else {
2657       /* no selector, use the pad as the source pad then */
2658       select->srcpad = gst_object_ref (pad);
2659     }
2660
2661     /* block the selector srcpad. It's possible that multiple decodebins start
2662      * pushing data into the selectors before we have a chance to collect all
2663      * streams and connect the sinks, resulting in not-linked errors. After we
2664      * configured the sinks we will unblock them all. */
2665     GST_DEBUG_OBJECT (playbin, "blocking %" GST_PTR_FORMAT, select->srcpad);
2666     select->block_id =
2667         gst_pad_add_probe (select->srcpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
2668         NULL, NULL, NULL);
2669   }
2670
2671   /* get sinkpad for the new stream */
2672   if (select->selector) {
2673     if ((sinkpad = gst_element_get_request_pad (select->selector, "sink_%u"))) {
2674       gulong notify_tags_handler = 0;
2675       NotifyTagsData *ntdata;
2676
2677       GST_DEBUG_OBJECT (playbin, "got pad %s:%s from selector",
2678           GST_DEBUG_PAD_NAME (sinkpad));
2679
2680       /* store the selector for the pad */
2681       g_object_set_data (G_OBJECT (sinkpad), "playbin.select", select);
2682
2683       /* connect to the notify::tags signal for our
2684        * own *-tags-changed signals
2685        */
2686       ntdata = g_new0 (NotifyTagsData, 1);
2687       ntdata->playbin = playbin;
2688       ntdata->stream_id = select->channels->len;
2689       ntdata->type = select->type;
2690
2691       notify_tags_handler =
2692           g_signal_connect_data (G_OBJECT (sinkpad), "notify::tags",
2693           G_CALLBACK (notify_tags_cb), ntdata, (GClosureNotify) g_free,
2694           (GConnectFlags) 0);
2695       g_object_set_data (G_OBJECT (sinkpad), "playbin.notify_tags_handler",
2696           (gpointer) (guintptr) notify_tags_handler);
2697
2698       /* store the pad in the array */
2699       GST_DEBUG_OBJECT (playbin, "pad %p added to array", sinkpad);
2700       g_ptr_array_add (select->channels, sinkpad);
2701
2702       res = gst_pad_link (pad, sinkpad);
2703       if (GST_PAD_LINK_FAILED (res))
2704         goto link_failed;
2705
2706       /* store selector pad so we can release it */
2707       g_object_set_data (G_OBJECT (pad), "playbin.sinkpad", sinkpad);
2708
2709       changed = TRUE;
2710       GST_DEBUG_OBJECT (playbin, "linked pad %s:%s to selector %p",
2711           GST_DEBUG_PAD_NAME (pad), select->selector);
2712     }
2713   } else {
2714     /* no selector, don't configure anything, we'll link the new pad directly to
2715      * the sink. */
2716     changed = FALSE;
2717     sinkpad = NULL;
2718   }
2719   GST_SOURCE_GROUP_UNLOCK (group);
2720
2721   if (changed) {
2722     int signal;
2723     gboolean always_ok = (decodebin == group->suburidecodebin);
2724
2725     switch (select->type) {
2726       case GST_PLAY_SINK_TYPE_VIDEO:
2727       case GST_PLAY_SINK_TYPE_VIDEO_RAW:
2728         /* we want to return NOT_LINKED for unselected pads but only for pads
2729          * from the normal uridecodebin. This makes sure that subtitle streams
2730          * are not raced past audio/video from decodebin's multiqueue.
2731          * For pads from suburidecodebin OK should always be returned, otherwise
2732          * it will most likely stop. */
2733         g_object_set (sinkpad, "always-ok", always_ok, NULL);
2734         signal = SIGNAL_VIDEO_CHANGED;
2735         break;
2736       case GST_PLAY_SINK_TYPE_AUDIO:
2737       case GST_PLAY_SINK_TYPE_AUDIO_RAW:
2738         g_object_set (sinkpad, "always-ok", always_ok, NULL);
2739         signal = SIGNAL_AUDIO_CHANGED;
2740         break;
2741       case GST_PLAY_SINK_TYPE_TEXT:
2742         g_object_set (sinkpad, "always-ok", always_ok, NULL);
2743         signal = SIGNAL_TEXT_CHANGED;
2744         break;
2745       default:
2746         signal = -1;
2747     }
2748
2749     if (signal >= 0)
2750       g_signal_emit (G_OBJECT (playbin), gst_play_bin_signals[signal], 0, NULL);
2751   }
2752
2753 done:
2754   gst_caps_unref (caps);
2755   return;
2756
2757   /* ERRORS */
2758 unknown_type:
2759   {
2760     GST_ERROR_OBJECT (playbin, "unknown type %s for pad %s:%s",
2761         name, GST_DEBUG_PAD_NAME (pad));
2762     goto done;
2763   }
2764 link_failed:
2765   {
2766     GST_ERROR_OBJECT (playbin,
2767         "failed to link pad %s:%s to selector, reason %d",
2768         GST_DEBUG_PAD_NAME (pad), res);
2769     GST_SOURCE_GROUP_UNLOCK (group);
2770     goto done;
2771   }
2772 }
2773
2774 /* called when a pad is removed from the uridecodebin. We unlink the pad from
2775  * the selector. This will make the selector select a new pad. */
2776 static void
2777 pad_removed_cb (GstElement * decodebin, GstPad * pad, GstSourceGroup * group)
2778 {
2779   GstPlayBin *playbin;
2780   GstPad *peer;
2781   GstElement *selector;
2782   GstSourceSelect *select;
2783
2784   playbin = group->playbin;
2785
2786   GST_DEBUG_OBJECT (playbin,
2787       "pad %s:%s removed from group %p", GST_DEBUG_PAD_NAME (pad), group);
2788
2789   GST_SOURCE_GROUP_LOCK (group);
2790   /* get the selector sinkpad */
2791   if (!(peer = g_object_get_data (G_OBJECT (pad), "playbin.sinkpad")))
2792     goto not_linked;
2793
2794   if ((select = g_object_get_data (G_OBJECT (peer), "playbin.select"))) {
2795     gulong notify_tags_handler;
2796
2797     notify_tags_handler =
2798         (guintptr) g_object_get_data (G_OBJECT (peer),
2799         "playbin.notify_tags_handler");
2800     if (notify_tags_handler != 0)
2801       g_signal_handler_disconnect (G_OBJECT (peer), notify_tags_handler);
2802     g_object_set_data (G_OBJECT (peer), "playbin.notify_tags_handler", NULL);
2803
2804     /* remove the pad from the array */
2805     g_ptr_array_remove (select->channels, peer);
2806     GST_DEBUG_OBJECT (playbin, "pad %p removed from array", peer);
2807   }
2808
2809   /* unlink the pad now (can fail, the pad is unlinked before it's removed) */
2810   gst_pad_unlink (pad, peer);
2811
2812   /* get selector, this can be NULL when the element is removing the pads
2813    * because it's being disposed. */
2814   selector = GST_ELEMENT_CAST (gst_pad_get_parent (peer));
2815   if (!selector) {
2816     gst_object_unref (peer);
2817     goto no_selector;
2818   }
2819
2820   /* release the pad to the selector, this will make the selector choose a new
2821    * pad. */
2822   gst_element_release_request_pad (selector, peer);
2823   gst_object_unref (peer);
2824
2825   gst_object_unref (selector);
2826   GST_SOURCE_GROUP_UNLOCK (group);
2827
2828   return;
2829
2830   /* ERRORS */
2831 not_linked:
2832   {
2833     GST_DEBUG_OBJECT (playbin, "pad not linked");
2834     GST_SOURCE_GROUP_UNLOCK (group);
2835     return;
2836   }
2837 no_selector:
2838   {
2839     GST_DEBUG_OBJECT (playbin, "selector not found");
2840     GST_SOURCE_GROUP_UNLOCK (group);
2841     return;
2842   }
2843 }
2844
2845 /* we get called when all pads are available and we must connect the sinks to
2846  * them.
2847  * The main purpose of the code is to see if we have video/audio and subtitles
2848  * and pick the right pipelines to display them.
2849  *
2850  * The selectors installed on the group tell us about the presence of
2851  * audio/video and subtitle streams. This allows us to see if we need
2852  * visualisation, video or/and audio.
2853  */
2854 static void
2855 no_more_pads_cb (GstElement * decodebin, GstSourceGroup * group)
2856 {
2857   GstPlayBin *playbin;
2858   GstPadLinkReturn res;
2859   gint i;
2860   gboolean configure;
2861
2862   playbin = group->playbin;
2863
2864   GST_DEBUG_OBJECT (playbin, "no more pads in group %p", group);
2865
2866   GST_PLAY_BIN_SHUTDOWN_LOCK (playbin, shutdown);
2867
2868   GST_SOURCE_GROUP_LOCK (group);
2869   for (i = 0; i < PLAYBIN_STREAM_LAST; i++) {
2870     GstSourceSelect *select = &group->selector[i];
2871
2872     /* check if the specific media type was detected and thus has a selector
2873      * created for it. If there is the media type, get a sinkpad from the sink
2874      * and link it. We only do this if we have not yet requested the sinkpad
2875      * before. */
2876     if (select->srcpad && select->sinkpad == NULL) {
2877       GST_DEBUG_OBJECT (playbin, "requesting new sink pad %d", select->type);
2878       select->sinkpad =
2879           gst_play_sink_request_pad (playbin->playsink, select->type);
2880
2881       res = gst_pad_link (select->srcpad, select->sinkpad);
2882       GST_DEBUG_OBJECT (playbin, "linked type %s, result: %d",
2883           select->media_list[0], res);
2884       if (res != GST_PAD_LINK_OK) {
2885         GST_ELEMENT_ERROR (playbin, CORE, PAD,
2886             ("Internal playbin error."),
2887             ("Failed to link selector to sink. Error %d", res));
2888       }
2889     }
2890   }
2891   GST_DEBUG_OBJECT (playbin, "pending %d > %d", group->pending,
2892       group->pending - 1);
2893
2894   if (group->pending > 0)
2895     group->pending--;
2896
2897   if (group->suburidecodebin == decodebin)
2898     group->sub_pending = FALSE;
2899
2900   if (group->pending == 0) {
2901     /* we are the last group to complete, we will configure the output and then
2902      * signal the other waiters. */
2903     GST_LOG_OBJECT (playbin, "last group complete");
2904     configure = TRUE;
2905   } else {
2906     GST_LOG_OBJECT (playbin, "have more pending groups");
2907     configure = FALSE;
2908   }
2909   GST_SOURCE_GROUP_UNLOCK (group);
2910
2911   if (configure) {
2912     /* if we have custom sinks, configure them now */
2913     GST_SOURCE_GROUP_LOCK (group);
2914
2915     if (group->audio_sink) {
2916       GST_INFO_OBJECT (playbin, "setting custom audio sink %" GST_PTR_FORMAT,
2917           group->audio_sink);
2918       gst_play_sink_set_sink (playbin->playsink, GST_PLAY_SINK_TYPE_AUDIO,
2919           group->audio_sink);
2920     }
2921
2922     if (group->video_sink) {
2923       GST_INFO_OBJECT (playbin, "setting custom video sink %" GST_PTR_FORMAT,
2924           group->video_sink);
2925       gst_play_sink_set_sink (playbin->playsink, GST_PLAY_SINK_TYPE_VIDEO,
2926           group->video_sink);
2927     }
2928
2929     if (playbin->text_sink) {
2930       GST_INFO_OBJECT (playbin, "setting custom text sink %" GST_PTR_FORMAT,
2931           playbin->text_sink);
2932       gst_play_sink_set_sink (playbin->playsink, GST_PLAY_SINK_TYPE_TEXT,
2933           playbin->text_sink);
2934     }
2935
2936     GST_SOURCE_GROUP_UNLOCK (group);
2937
2938     /* signal the other decodebins that they can continue now. */
2939     GST_SOURCE_GROUP_LOCK (group);
2940     /* unblock all selectors */
2941     for (i = 0; i < PLAYBIN_STREAM_LAST; i++) {
2942       GstSourceSelect *select = &group->selector[i];
2943
2944       /* All streamsynchronizer streams should see stream-changed message,
2945        * to arrange for blocking unblocking. */
2946       if (select->sinkpad) {
2947         GstStructure *s;
2948         GstMessage *msg;
2949         GstEvent *event;
2950         guint32 seqnum;
2951
2952         s = gst_structure_new ("playbin-stream-changed", "uri", G_TYPE_STRING,
2953             group->uri, NULL);
2954         if (group->suburi)
2955           gst_structure_set (s, "suburi", G_TYPE_STRING, group->suburi, NULL);
2956         msg = gst_message_new_element (GST_OBJECT_CAST (playbin), s);
2957         seqnum = gst_message_get_seqnum (msg);
2958         event = gst_event_new_sink_message (msg);
2959         g_mutex_lock (&group->stream_changed_pending_lock);
2960         group->stream_changed_pending =
2961             g_list_prepend (group->stream_changed_pending,
2962             GUINT_TO_POINTER (seqnum));
2963
2964         /* remove any data probe we might have, and replace */
2965         if (select->sinkpad_delayed_event)
2966           gst_event_unref (select->sinkpad_delayed_event);
2967         select->sinkpad_delayed_event = event;
2968         if (select->sinkpad_data_probe)
2969           gst_pad_remove_probe (select->sinkpad, select->sinkpad_data_probe);
2970
2971         /* we go to the trouble of setting a probe on the pad to send
2972            the playbin-stream-changed event as sending it here might
2973            find that the pad is blocked, so we'd block here, and the
2974            pad might not be linked yet. Additionally, sending it here
2975            apparently would be on the wrong thread */
2976         select->sinkpad_data_probe =
2977             gst_pad_add_probe (select->sinkpad,
2978             GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
2979             stream_changed_data_probe, (gpointer) select, NULL);
2980
2981         g_mutex_unlock (&group->stream_changed_pending_lock);
2982         gst_message_unref (msg);
2983       }
2984
2985       if (select->srcpad) {
2986         GST_DEBUG_OBJECT (playbin, "unblocking %" GST_PTR_FORMAT,
2987             select->srcpad);
2988         if (select->block_id) {
2989           gst_pad_remove_probe (select->srcpad, select->block_id);
2990           select->block_id = 0;
2991         }
2992       }
2993     }
2994     GST_SOURCE_GROUP_UNLOCK (group);
2995   }
2996
2997   GST_PLAY_BIN_SHUTDOWN_UNLOCK (playbin);
2998
2999   return;
3000
3001 shutdown:
3002   {
3003     GST_DEBUG ("ignoring, we are shutting down");
3004     /* Request a flushing pad from playsink that we then link to the selector.
3005      * Then we unblock the selectors so that they stop with a WRONG_STATE
3006      * instead of a NOT_LINKED error.
3007      */
3008     GST_SOURCE_GROUP_LOCK (group);
3009     for (i = 0; i < PLAYBIN_STREAM_LAST; i++) {
3010       GstSourceSelect *select = &group->selector[i];
3011
3012       if (select->srcpad) {
3013         if (select->sinkpad == NULL) {
3014           GST_DEBUG_OBJECT (playbin, "requesting new flushing sink pad");
3015           select->sinkpad =
3016               gst_play_sink_request_pad (playbin->playsink,
3017               GST_PLAY_SINK_TYPE_FLUSHING);
3018           res = gst_pad_link (select->srcpad, select->sinkpad);
3019           GST_DEBUG_OBJECT (playbin, "linked flushing, result: %d", res);
3020         }
3021         GST_DEBUG_OBJECT (playbin, "unblocking %" GST_PTR_FORMAT,
3022             select->srcpad);
3023         if (select->block_id) {
3024           gst_pad_remove_probe (select->srcpad, select->block_id);
3025           select->block_id = 0;
3026         }
3027       }
3028     }
3029     GST_SOURCE_GROUP_UNLOCK (group);
3030     return;
3031   }
3032 }
3033
3034 static void
3035 drained_cb (GstElement * decodebin, GstSourceGroup * group)
3036 {
3037   GstPlayBin *playbin;
3038
3039   playbin = group->playbin;
3040
3041   GST_DEBUG_OBJECT (playbin, "about to finish in group %p", group);
3042
3043   /* after this call, we should have a next group to activate or we EOS */
3044   g_signal_emit (G_OBJECT (playbin),
3045       gst_play_bin_signals[SIGNAL_ABOUT_TO_FINISH], 0, NULL);
3046
3047   /* now activate the next group. If the app did not set a uri, this will
3048    * fail and we can do EOS */
3049   setup_next_source (playbin, GST_STATE_PAUSED);
3050 }
3051
3052 /* Like gst_element_factory_can_sink_any_caps() but doesn't
3053  * allow ANY caps on the sinkpad template */
3054 static gboolean
3055 _factory_can_sink_caps (GstElementFactory * factory, GstCaps * caps)
3056 {
3057   const GList *templs;
3058
3059   templs = gst_element_factory_get_static_pad_templates (factory);
3060
3061   while (templs) {
3062     GstStaticPadTemplate *templ = (GstStaticPadTemplate *) templs->data;
3063
3064     if (templ->direction == GST_PAD_SINK) {
3065       GstCaps *templcaps = gst_static_caps_get (&templ->static_caps);
3066
3067       if (!gst_caps_is_any (templcaps)
3068           && gst_caps_can_intersect (templcaps, caps)) {
3069         gst_caps_unref (templcaps);
3070         return TRUE;
3071       }
3072       gst_caps_unref (templcaps);
3073     }
3074     templs = g_list_next (templs);
3075   }
3076
3077   return FALSE;
3078 }
3079
3080 /* Called when we must provide a list of factories to plug to @pad with @caps.
3081  * We first check if we have a sink that can handle the format and if we do, we
3082  * return NULL, to expose the pad. If we have no sink (or the sink does not
3083  * work), we return the list of elements that can connect. */
3084 static GValueArray *
3085 autoplug_factories_cb (GstElement * decodebin, GstPad * pad,
3086     GstCaps * caps, GstSourceGroup * group)
3087 {
3088   GstPlayBin *playbin;
3089   GList *mylist, *tmp;
3090   GValueArray *result;
3091
3092   playbin = group->playbin;
3093
3094   GST_DEBUG_OBJECT (playbin, "factories group %p for %s:%s, %" GST_PTR_FORMAT,
3095       group, GST_DEBUG_PAD_NAME (pad), caps);
3096
3097   /* filter out the elements based on the caps. */
3098   g_mutex_lock (&playbin->elements_lock);
3099   gst_play_bin_update_elements_list (playbin);
3100   mylist =
3101       gst_element_factory_list_filter (playbin->elements, caps, GST_PAD_SINK,
3102       FALSE);
3103   g_mutex_unlock (&playbin->elements_lock);
3104
3105   GST_DEBUG_OBJECT (playbin, "found factories %p", mylist);
3106   GST_PLUGIN_FEATURE_LIST_DEBUG (mylist);
3107
3108   /* 2 additional elements for the already set audio/video sinks */
3109   result = g_value_array_new (g_list_length (mylist) + 2);
3110
3111   /* Check if we already have an audio/video sink and if this is the case
3112    * put it as the first element of the array */
3113   if (group->audio_sink) {
3114     GstElementFactory *factory = gst_element_get_factory (group->audio_sink);
3115
3116     if (factory && _factory_can_sink_caps (factory, caps)) {
3117       GValue val = { 0, };
3118
3119       g_value_init (&val, G_TYPE_OBJECT);
3120       g_value_set_object (&val, factory);
3121       result = g_value_array_append (result, &val);
3122       g_value_unset (&val);
3123     }
3124   }
3125
3126   if (group->video_sink) {
3127     GstElementFactory *factory = gst_element_get_factory (group->video_sink);
3128
3129     if (factory && _factory_can_sink_caps (factory, caps)) {
3130       GValue val = { 0, };
3131
3132       g_value_init (&val, G_TYPE_OBJECT);
3133       g_value_set_object (&val, factory);
3134       result = g_value_array_append (result, &val);
3135       g_value_unset (&val);
3136     }
3137   }
3138
3139   for (tmp = mylist; tmp; tmp = tmp->next) {
3140     GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
3141     GValue val = { 0, };
3142
3143     if (group->audio_sink && gst_element_factory_list_is_type (factory,
3144             GST_ELEMENT_FACTORY_TYPE_SINK |
3145             GST_ELEMENT_FACTORY_TYPE_MEDIA_AUDIO)) {
3146       continue;
3147     }
3148     if (group->video_sink && gst_element_factory_list_is_type (factory,
3149             GST_ELEMENT_FACTORY_TYPE_SINK | GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO
3150             | GST_ELEMENT_FACTORY_TYPE_MEDIA_IMAGE)) {
3151       continue;
3152     }
3153
3154     g_value_init (&val, G_TYPE_OBJECT);
3155     g_value_set_object (&val, factory);
3156     g_value_array_append (result, &val);
3157     g_value_unset (&val);
3158   }
3159   gst_plugin_feature_list_free (mylist);
3160
3161   return result;
3162 }
3163
3164 /* autoplug-continue decides, if a pad has raw caps that can be exposed
3165  * directly or if further decoding is necessary. We use this to expose
3166  * supported subtitles directly */
3167
3168 /* FIXME 0.11: Remove the checks for ANY caps, a sink should specify
3169  * explicitly the caps it supports and if it claims to support ANY
3170  * caps it really should support everything */
3171 static gboolean
3172 autoplug_continue_cb (GstElement * element, GstPad * pad, GstCaps * caps,
3173     GstSourceGroup * group)
3174 {
3175   gboolean ret = TRUE;
3176   GstElement *sink;
3177   GstPad *sinkpad = NULL;
3178
3179   GST_PLAY_BIN_LOCK (group->playbin);
3180   GST_SOURCE_GROUP_LOCK (group);
3181
3182   if ((sink = group->playbin->text_sink))
3183     sinkpad = gst_element_get_static_pad (sink, "sink");
3184   if (sinkpad) {
3185     GstCaps *sinkcaps;
3186
3187     /* Ignore errors here, if a custom sink fails to go
3188      * to READY things are wrong and will error out later
3189      */
3190     if (GST_STATE (sink) < GST_STATE_READY)
3191       gst_element_set_state (sink, GST_STATE_READY);
3192
3193     sinkcaps = gst_pad_query_caps (sinkpad, NULL);
3194     if (!gst_caps_is_any (sinkcaps))
3195       ret = !gst_pad_query_accept_caps (sinkpad, caps);
3196     gst_caps_unref (sinkcaps);
3197     gst_object_unref (sinkpad);
3198   } else {
3199     GstCaps *subcaps = gst_subtitle_overlay_create_factory_caps ();
3200     ret = !gst_caps_is_subset (caps, subcaps);
3201     gst_caps_unref (subcaps);
3202   }
3203   /* If autoplugging can stop don't do additional checks */
3204   if (!ret)
3205     goto done;
3206
3207   /* If this is from the subtitle uridecodebin we don't need to
3208    * check the audio and video sink */
3209   if (group->suburidecodebin
3210       && gst_object_has_ancestor (GST_OBJECT_CAST (element),
3211           GST_OBJECT_CAST (group->suburidecodebin)))
3212     goto done;
3213
3214   if ((sink = group->audio_sink)) {
3215     sinkpad = gst_element_get_static_pad (sink, "sink");
3216     if (sinkpad) {
3217       GstCaps *sinkcaps;
3218
3219       /* Ignore errors here, if a custom sink fails to go
3220        * to READY things are wrong and will error out later
3221        */
3222       if (GST_STATE (sink) < GST_STATE_READY)
3223         gst_element_set_state (sink, GST_STATE_READY);
3224
3225       sinkcaps = gst_pad_query_caps (sinkpad, NULL);
3226       if (!gst_caps_is_any (sinkcaps))
3227         ret = !gst_pad_query_accept_caps (sinkpad, caps);
3228       gst_caps_unref (sinkcaps);
3229       gst_object_unref (sinkpad);
3230     }
3231   }
3232   if (!ret)
3233     goto done;
3234
3235   if ((sink = group->video_sink)) {
3236     sinkpad = gst_element_get_static_pad (sink, "sink");
3237     if (sinkpad) {
3238       GstCaps *sinkcaps;
3239
3240       /* Ignore errors here, if a custom sink fails to go
3241        * to READY things are wrong and will error out later
3242        */
3243       if (GST_STATE (sink) < GST_STATE_READY)
3244         gst_element_set_state (sink, GST_STATE_READY);
3245
3246       sinkcaps = gst_pad_query_caps (sinkpad, NULL);
3247       if (!gst_caps_is_any (sinkcaps))
3248         ret = !gst_pad_query_accept_caps (sinkpad, caps);
3249       gst_caps_unref (sinkcaps);
3250       gst_object_unref (sinkpad);
3251     }
3252   }
3253
3254 done:
3255   GST_SOURCE_GROUP_UNLOCK (group);
3256   GST_PLAY_BIN_UNLOCK (group->playbin);
3257
3258   GST_DEBUG_OBJECT (group->playbin,
3259       "continue autoplugging group %p for %s:%s, %" GST_PTR_FORMAT ": %d",
3260       group, GST_DEBUG_PAD_NAME (pad), caps, ret);
3261
3262   return ret;
3263 }
3264
3265 static gboolean
3266 sink_accepts_caps (GstElement * sink, GstCaps * caps)
3267 {
3268   GstPad *sinkpad;
3269
3270   /* ... activate it ... We do this before adding it to the bin so that we
3271    * don't accidentally make it post error messages that will stop
3272    * everything. */
3273   if (GST_STATE (sink) < GST_STATE_READY &&
3274       gst_element_set_state (sink,
3275           GST_STATE_READY) == GST_STATE_CHANGE_FAILURE) {
3276     return FALSE;
3277   }
3278
3279   if ((sinkpad = gst_element_get_static_pad (sink, "sink"))) {
3280     /* Got the sink pad, now let's see if the element actually does accept the
3281      * caps that we have */
3282     if (!gst_pad_query_accept_caps (sinkpad, caps)) {
3283       gst_object_unref (sinkpad);
3284       return FALSE;
3285     }
3286     gst_object_unref (sinkpad);
3287   }
3288
3289   return TRUE;
3290 }
3291
3292 static GstStaticCaps raw_audio_caps = GST_STATIC_CAPS ("audio/x-raw");
3293 static GstStaticCaps raw_video_caps = GST_STATIC_CAPS ("video/x-raw");
3294
3295 /* We are asked to select an element. See if the next element to check
3296  * is a sink. If this is the case, we see if the sink works by setting it to
3297  * READY. If the sink works, we return SELECT_EXPOSE to make decodebin
3298  * expose the raw pad so that we can setup the mixers. */
3299 static GstAutoplugSelectResult
3300 autoplug_select_cb (GstElement * decodebin, GstPad * pad,
3301     GstCaps * caps, GstElementFactory * factory, GstSourceGroup * group)
3302 {
3303   GstPlayBin *playbin;
3304   GstElement *element;
3305   const gchar *klass;
3306   GstPlaySinkType type;
3307   GstElement **sinkp;
3308
3309   playbin = group->playbin;
3310
3311   GST_DEBUG_OBJECT (playbin, "select group %p for %s:%s, %" GST_PTR_FORMAT,
3312       group, GST_DEBUG_PAD_NAME (pad), caps);
3313
3314   GST_DEBUG_OBJECT (playbin, "checking factory %s", GST_OBJECT_NAME (factory));
3315
3316   /* if it's not a sink, we make sure the element is compatible with
3317    * the fixed sink */
3318   if (!gst_element_factory_list_is_type (factory,
3319           GST_ELEMENT_FACTORY_TYPE_SINK)) {
3320     gboolean isvideodec = gst_element_factory_list_is_type (factory,
3321         GST_ELEMENT_FACTORY_TYPE_DECODER |
3322         GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO |
3323         GST_ELEMENT_FACTORY_TYPE_MEDIA_IMAGE);
3324     gboolean isaudiodec = gst_element_factory_list_is_type (factory,
3325         GST_ELEMENT_FACTORY_TYPE_DECODER |
3326         GST_ELEMENT_FACTORY_TYPE_MEDIA_AUDIO);
3327
3328     /* If it is a decoder and we have a fixed sink for the media
3329      * type it outputs, check that the decoder is compatible with this sink */
3330     if ((isvideodec && group->video_sink) || (isaudiodec && group->audio_sink)) {
3331       gboolean compatible = TRUE;
3332       GstPad *sinkpad;
3333       GstCaps *caps;
3334       GstElement *sink;
3335
3336       if (isaudiodec)
3337         sink = group->audio_sink;
3338       else
3339         sink = group->video_sink;
3340
3341       if ((sinkpad = gst_element_get_static_pad (sink, "sink"))) {
3342         GstPlayFlags flags = gst_play_bin_get_flags (playbin);
3343         GstCaps *raw_caps =
3344             (isaudiodec) ? gst_static_caps_get (&raw_audio_caps) :
3345             gst_static_caps_get (&raw_video_caps);
3346
3347         caps = gst_pad_query_caps (sinkpad, NULL);
3348
3349         /* If the sink supports raw audio/video, we first check
3350          * if the decoder could output any raw audio/video format
3351          * and assume it is compatible with the sink then. We don't
3352          * do a complete compatibility check here if converters
3353          * are plugged between the decoder and the sink because
3354          * the converters will convert between raw formats and
3355          * even if the decoder format is not supported by the decoder
3356          * a converter will convert it.
3357          *
3358          * We assume here that the converters can convert between
3359          * any raw format.
3360          */
3361         if ((isaudiodec && !(flags & GST_PLAY_FLAG_NATIVE_AUDIO)
3362                 && gst_caps_can_intersect (caps, raw_caps)) || (!isaudiodec
3363                 && !(flags & GST_PLAY_FLAG_NATIVE_VIDEO)
3364                 && gst_caps_can_intersect (caps, raw_caps))) {
3365           compatible = gst_element_factory_can_src_any_caps (factory, raw_caps)
3366               || gst_element_factory_can_src_any_caps (factory, caps);
3367         } else {
3368           compatible = gst_element_factory_can_src_any_caps (factory, caps);
3369         }
3370
3371         gst_object_unref (sinkpad);
3372         gst_caps_unref (caps);
3373       }
3374
3375       if (compatible)
3376         return GST_AUTOPLUG_SELECT_TRY;
3377
3378       GST_DEBUG_OBJECT (playbin, "%s not compatible with the fixed sink",
3379           GST_OBJECT_NAME (factory));
3380
3381       return GST_AUTOPLUG_SELECT_SKIP;
3382     } else
3383       return GST_AUTOPLUG_SELECT_TRY;
3384   }
3385
3386   /* it's a sink, see if an instance of it actually works */
3387   GST_DEBUG_OBJECT (playbin, "we found a sink");
3388
3389   klass = gst_element_factory_get_klass (factory);
3390
3391   /* figure out the klass */
3392   if (strstr (klass, "Audio")) {
3393     GST_DEBUG_OBJECT (playbin, "we found an audio sink");
3394     type = GST_PLAY_SINK_TYPE_AUDIO;
3395     sinkp = &group->audio_sink;
3396   } else if (strstr (klass, "Video")) {
3397     GST_DEBUG_OBJECT (playbin, "we found a video sink");
3398     type = GST_PLAY_SINK_TYPE_VIDEO;
3399     sinkp = &group->video_sink;
3400   } else {
3401     /* unknown klass, skip this element */
3402     GST_WARNING_OBJECT (playbin, "unknown sink klass %s found", klass);
3403     return GST_AUTOPLUG_SELECT_SKIP;
3404   }
3405
3406   /* if we are asked to do visualisations and it's an audio sink, skip the
3407    * element. We can only do visualisations with raw sinks */
3408   if (gst_play_sink_get_flags (playbin->playsink) & GST_PLAY_FLAG_VIS) {
3409     if (type == GST_PLAY_SINK_TYPE_AUDIO) {
3410       GST_DEBUG_OBJECT (playbin, "skip audio sink because of vis");
3411       return GST_AUTOPLUG_SELECT_SKIP;
3412     }
3413   }
3414
3415   /* now see if we already have a sink element */
3416   GST_SOURCE_GROUP_LOCK (group);
3417   if (*sinkp) {
3418     GstElement *sink = gst_object_ref (*sinkp);
3419
3420     if (sink_accepts_caps (sink, caps)) {
3421       GST_DEBUG_OBJECT (playbin,
3422           "Existing sink '%s' accepts caps: %" GST_PTR_FORMAT,
3423           GST_ELEMENT_NAME (sink), caps);
3424       gst_object_unref (sink);
3425       GST_SOURCE_GROUP_UNLOCK (group);
3426       return GST_AUTOPLUG_SELECT_EXPOSE;
3427     } else {
3428       GST_DEBUG_OBJECT (playbin,
3429           "Existing sink '%s' does not accept caps: %" GST_PTR_FORMAT,
3430           GST_ELEMENT_NAME (sink), caps);
3431       gst_object_unref (sink);
3432       GST_SOURCE_GROUP_UNLOCK (group);
3433       return GST_AUTOPLUG_SELECT_SKIP;
3434     }
3435   }
3436   GST_DEBUG_OBJECT (playbin, "we have no pending sink, try to create one");
3437
3438   if ((element = gst_element_factory_create (factory, NULL)) == NULL) {
3439     GST_WARNING_OBJECT (playbin, "Could not create an element from %s",
3440         gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
3441     GST_SOURCE_GROUP_UNLOCK (group);
3442     return GST_AUTOPLUG_SELECT_SKIP;
3443   }
3444
3445   /* Check if the selected sink actually supports the
3446    * caps and can be set to READY*/
3447   if (!sink_accepts_caps (element, caps)) {
3448     gst_element_set_state (element, GST_STATE_NULL);
3449     gst_object_unref (element);
3450     GST_SOURCE_GROUP_UNLOCK (group);
3451     return GST_AUTOPLUG_SELECT_SKIP;
3452   }
3453
3454   /* remember the sink in the group now, the element is floating, we take
3455    * ownership now 
3456    *
3457    * store the sink in the group, we will configure it later when we
3458    * reconfigure the sink */
3459   GST_DEBUG_OBJECT (playbin, "remember sink");
3460   gst_object_ref_sink (element);
3461   *sinkp = element;
3462   GST_SOURCE_GROUP_UNLOCK (group);
3463
3464   /* tell decodebin to expose the pad because we are going to use this
3465    * sink */
3466   GST_DEBUG_OBJECT (playbin, "we found a working sink, expose pad");
3467
3468   return GST_AUTOPLUG_SELECT_EXPOSE;
3469 }
3470
3471 static void
3472 notify_source_cb (GstElement * uridecodebin, GParamSpec * pspec,
3473     GstSourceGroup * group)
3474 {
3475   GstPlayBin *playbin;
3476   GstElement *source;
3477
3478   playbin = group->playbin;
3479
3480   g_object_get (group->uridecodebin, "source", &source, NULL);
3481
3482   GST_OBJECT_LOCK (playbin);
3483   if (playbin->source)
3484     gst_object_unref (playbin->source);
3485   playbin->source = source;
3486   GST_OBJECT_UNLOCK (playbin);
3487
3488   g_object_notify (G_OBJECT (playbin), "source");
3489
3490   g_signal_emit (playbin, gst_play_bin_signals[SIGNAL_SOURCE_SETUP],
3491       0, playbin->source);
3492 }
3493
3494 /* must be called with the group lock */
3495 static gboolean
3496 group_set_locked_state_unlocked (GstPlayBin * playbin, GstSourceGroup * group,
3497     gboolean locked)
3498 {
3499   GST_DEBUG_OBJECT (playbin, "locked_state %d on group %p", locked, group);
3500
3501   if (group->uridecodebin)
3502     gst_element_set_locked_state (group->uridecodebin, locked);
3503   if (group->suburidecodebin)
3504     gst_element_set_locked_state (group->suburidecodebin, locked);
3505
3506   return TRUE;
3507 }
3508
3509 /* must be called with PLAY_BIN_LOCK */
3510 static gboolean
3511 activate_group (GstPlayBin * playbin, GstSourceGroup * group, GstState target)
3512 {
3513   GstElement *uridecodebin;
3514   GstElement *suburidecodebin = NULL;
3515   GstPlayFlags flags;
3516
3517   g_return_val_if_fail (group->valid, FALSE);
3518   g_return_val_if_fail (!group->active, FALSE);
3519
3520   GST_DEBUG_OBJECT (playbin, "activating group %p", group);
3521
3522   GST_SOURCE_GROUP_LOCK (group);
3523
3524   /* First set up the custom sources */
3525   if (playbin->audio_sink)
3526     group->audio_sink = gst_object_ref (playbin->audio_sink);
3527   if (playbin->video_sink)
3528     group->video_sink = gst_object_ref (playbin->video_sink);
3529
3530   g_list_free (group->stream_changed_pending);
3531   group->stream_changed_pending = NULL;
3532   if (!group->stream_changed_pending_lock.p)
3533     g_mutex_init (&group->stream_changed_pending_lock);
3534
3535   if (group->uridecodebin) {
3536     GST_DEBUG_OBJECT (playbin, "reusing existing uridecodebin");
3537     uridecodebin = group->uridecodebin;
3538     gst_element_set_state (uridecodebin, GST_STATE_READY);
3539     gst_bin_add (GST_BIN_CAST (playbin), gst_object_ref (uridecodebin));
3540   } else {
3541     GST_DEBUG_OBJECT (playbin, "making new uridecodebin");
3542     uridecodebin = gst_element_factory_make ("uridecodebin", NULL);
3543     if (!uridecodebin)
3544       goto no_decodebin;
3545     gst_bin_add (GST_BIN_CAST (playbin), uridecodebin);
3546     group->uridecodebin = gst_object_ref (uridecodebin);
3547   }
3548
3549   flags = gst_play_sink_get_flags (playbin->playsink);
3550
3551   g_object_set (uridecodebin,
3552       /* configure connection speed */
3553       "connection-speed", playbin->connection_speed / 1000,
3554       /* configure uri */
3555       "uri", group->uri,
3556       /* configure download buffering */
3557       "download", ((flags & GST_PLAY_FLAG_DOWNLOAD) != 0),
3558       /* configure buffering of demuxed/parsed data */
3559       "use-buffering", ((flags & GST_PLAY_FLAG_BUFFERING) != 0),
3560       /* configure buffering parameters */
3561       "buffer-duration", playbin->buffer_duration,
3562       "buffer-size", playbin->buffer_size,
3563       "ring-buffer-max-size", playbin->ring_buffer_max_size, NULL);
3564
3565   /* connect pads and other things */
3566   group->pad_added_id = g_signal_connect (uridecodebin, "pad-added",
3567       G_CALLBACK (pad_added_cb), group);
3568   group->pad_removed_id = g_signal_connect (uridecodebin, "pad-removed",
3569       G_CALLBACK (pad_removed_cb), group);
3570   group->no_more_pads_id = g_signal_connect (uridecodebin, "no-more-pads",
3571       G_CALLBACK (no_more_pads_cb), group);
3572   group->notify_source_id = g_signal_connect (uridecodebin, "notify::source",
3573       G_CALLBACK (notify_source_cb), group);
3574
3575   /* we have 1 pending no-more-pads */
3576   group->pending = 1;
3577
3578   /* is called when the uridecodebin is out of data and we can switch to the
3579    * next uri */
3580   group->drained_id =
3581       g_signal_connect (uridecodebin, "drained", G_CALLBACK (drained_cb),
3582       group);
3583
3584   /* will be called when a new media type is found. We return a list of decoders
3585    * including sinks for decodebin to try */
3586   group->autoplug_factories_id =
3587       g_signal_connect (uridecodebin, "autoplug-factories",
3588       G_CALLBACK (autoplug_factories_cb), group);
3589   group->autoplug_select_id =
3590       g_signal_connect (uridecodebin, "autoplug-select",
3591       G_CALLBACK (autoplug_select_cb), group);
3592   group->autoplug_continue_id =
3593       g_signal_connect (uridecodebin, "autoplug-continue",
3594       G_CALLBACK (autoplug_continue_cb), group);
3595
3596   if (group->suburi) {
3597     /* subtitles */
3598     if (group->suburidecodebin) {
3599       GST_DEBUG_OBJECT (playbin, "reusing existing suburidecodebin");
3600       suburidecodebin = group->suburidecodebin;
3601       gst_element_set_state (suburidecodebin, GST_STATE_READY);
3602       gst_bin_add (GST_BIN_CAST (playbin), gst_object_ref (suburidecodebin));
3603     } else {
3604       GST_DEBUG_OBJECT (playbin, "making new suburidecodebin");
3605       suburidecodebin = gst_element_factory_make ("uridecodebin", NULL);
3606       if (!suburidecodebin)
3607         goto no_decodebin;
3608
3609       gst_bin_add (GST_BIN_CAST (playbin), suburidecodebin);
3610       group->suburidecodebin = gst_object_ref (suburidecodebin);
3611     }
3612
3613     g_object_set (suburidecodebin,
3614         /* configure connection speed */
3615         "connection-speed", playbin->connection_speed,
3616         /* configure uri */
3617         "uri", group->suburi, NULL);
3618
3619     /* connect pads and other things */
3620     group->sub_pad_added_id = g_signal_connect (suburidecodebin, "pad-added",
3621         G_CALLBACK (pad_added_cb), group);
3622     group->sub_pad_removed_id = g_signal_connect (suburidecodebin,
3623         "pad-removed", G_CALLBACK (pad_removed_cb), group);
3624     group->sub_no_more_pads_id = g_signal_connect (suburidecodebin,
3625         "no-more-pads", G_CALLBACK (no_more_pads_cb), group);
3626
3627     group->sub_autoplug_continue_id =
3628         g_signal_connect (suburidecodebin, "autoplug-continue",
3629         G_CALLBACK (autoplug_continue_cb), group);
3630
3631     /* we have 2 pending no-more-pads */
3632     group->pending = 2;
3633     group->sub_pending = TRUE;
3634   } else {
3635     group->sub_pending = FALSE;
3636   }
3637
3638   /* release the group lock before setting the state of the decodebins, they
3639    * might fire signals in this thread that we need to handle with the
3640    * group_lock taken. */
3641   GST_SOURCE_GROUP_UNLOCK (group);
3642
3643   if (suburidecodebin) {
3644     if (gst_element_set_state (suburidecodebin,
3645             target) == GST_STATE_CHANGE_FAILURE) {
3646       GST_DEBUG_OBJECT (playbin,
3647           "failed state change of subtitle uridecodebin");
3648       GST_SOURCE_GROUP_LOCK (group);
3649
3650       REMOVE_SIGNAL (group->suburidecodebin, group->sub_pad_added_id);
3651       REMOVE_SIGNAL (group->suburidecodebin, group->sub_pad_removed_id);
3652       REMOVE_SIGNAL (group->suburidecodebin, group->sub_no_more_pads_id);
3653       REMOVE_SIGNAL (group->suburidecodebin, group->sub_autoplug_continue_id);
3654       /* Might already be removed because of an error message */
3655       if (GST_OBJECT_PARENT (suburidecodebin) == GST_OBJECT_CAST (playbin))
3656         gst_bin_remove (GST_BIN_CAST (playbin), suburidecodebin);
3657       if (group->sub_pending) {
3658         group->pending--;
3659         group->sub_pending = FALSE;
3660       }
3661       gst_element_set_state (suburidecodebin, GST_STATE_READY);
3662       GST_SOURCE_GROUP_UNLOCK (group);
3663     }
3664   }
3665   if (gst_element_set_state (uridecodebin, target) == GST_STATE_CHANGE_FAILURE)
3666     goto uridecodebin_failure;
3667
3668   GST_SOURCE_GROUP_LOCK (group);
3669   /* alow state changes of the playbin affect the group elements now */
3670   group_set_locked_state_unlocked (playbin, group, FALSE);
3671   group->active = TRUE;
3672   GST_SOURCE_GROUP_UNLOCK (group);
3673
3674   return TRUE;
3675
3676   /* ERRORS */
3677 no_decodebin:
3678   {
3679     GstMessage *msg;
3680
3681     /* delete any custom sinks we might have */
3682     if (group->audio_sink) {
3683       /* If this is a automatically created sink set it to NULL */
3684       if (group->audio_sink != playbin->audio_sink)
3685         gst_element_set_state (group->audio_sink, GST_STATE_NULL);
3686       gst_object_unref (group->audio_sink);
3687     }
3688     group->audio_sink = NULL;
3689     if (group->video_sink) {
3690       /* If this is a automatically created sink set it to NULL */
3691       if (group->video_sink != playbin->video_sink)
3692         gst_element_set_state (group->video_sink, GST_STATE_NULL);
3693       gst_object_unref (group->video_sink);
3694     }
3695     group->video_sink = NULL;
3696
3697     GST_SOURCE_GROUP_UNLOCK (group);
3698     msg =
3699         gst_missing_element_message_new (GST_ELEMENT_CAST (playbin),
3700         "uridecodebin");
3701     gst_element_post_message (GST_ELEMENT_CAST (playbin), msg);
3702
3703     GST_ELEMENT_ERROR (playbin, CORE, MISSING_PLUGIN,
3704         (_("Could not create \"uridecodebin\" element.")), (NULL));
3705     return FALSE;
3706   }
3707 uridecodebin_failure:
3708   {
3709     /* delete any custom sinks we might have */
3710     if (group->audio_sink) {
3711       /* If this is a automatically created sink set it to NULL */
3712       if (group->audio_sink != playbin->audio_sink)
3713         gst_element_set_state (group->audio_sink, GST_STATE_NULL);
3714       gst_object_unref (group->audio_sink);
3715     }
3716     group->audio_sink = NULL;
3717     if (group->video_sink) {
3718       /* If this is a automatically created sink set it to NULL */
3719       if (group->video_sink != playbin->video_sink)
3720         gst_element_set_state (group->video_sink, GST_STATE_NULL);
3721       gst_object_unref (group->video_sink);
3722     }
3723     group->video_sink = NULL;
3724
3725     GST_DEBUG_OBJECT (playbin, "failed state change of uridecodebin");
3726     return FALSE;
3727   }
3728 }
3729
3730 /* unlink a group of uridecodebins from the sink.
3731  * must be called with PLAY_BIN_LOCK */
3732 static gboolean
3733 deactivate_group (GstPlayBin * playbin, GstSourceGroup * group)
3734 {
3735   gint i;
3736
3737   g_return_val_if_fail (group->valid, FALSE);
3738   g_return_val_if_fail (group->active, FALSE);
3739
3740   GST_DEBUG_OBJECT (playbin, "unlinking group %p", group);
3741
3742   GST_SOURCE_GROUP_LOCK (group);
3743   group->active = FALSE;
3744   for (i = 0; i < PLAYBIN_STREAM_LAST; i++) {
3745     GstSourceSelect *select = &group->selector[i];
3746
3747     GST_DEBUG_OBJECT (playbin, "unlinking selector %s", select->media_list[0]);
3748
3749     if (select->srcpad) {
3750       if (select->sinkpad) {
3751         GST_LOG_OBJECT (playbin, "unlinking from sink");
3752         gst_pad_unlink (select->srcpad, select->sinkpad);
3753
3754         /* release back */
3755         GST_LOG_OBJECT (playbin, "release sink pad");
3756         gst_play_sink_release_pad (playbin->playsink, select->sinkpad);
3757         select->sinkpad = NULL;
3758       }
3759
3760       gst_object_unref (select->srcpad);
3761       select->srcpad = NULL;
3762     }
3763
3764     if (select->selector) {
3765       gint n;
3766
3767       /* release and unref requests pad from the selector */
3768       for (n = 0; n < select->channels->len; n++) {
3769         GstPad *sinkpad = g_ptr_array_index (select->channels, n);
3770
3771         gst_element_release_request_pad (select->selector, sinkpad);
3772         gst_object_unref (sinkpad);
3773       }
3774       g_ptr_array_set_size (select->channels, 0);
3775
3776       gst_element_set_state (select->selector, GST_STATE_NULL);
3777       gst_bin_remove (GST_BIN_CAST (playbin), select->selector);
3778       select->selector = NULL;
3779     }
3780   }
3781   /* delete any custom sinks we might have */
3782   if (group->audio_sink) {
3783     /* If this is a automatically created sink set it to NULL */
3784     if (group->audio_sink != playbin->audio_sink)
3785       gst_element_set_state (group->audio_sink, GST_STATE_NULL);
3786     gst_object_unref (group->audio_sink);
3787   }
3788   group->audio_sink = NULL;
3789   if (group->video_sink) {
3790     /* If this is a automatically created sink set it to NULL */
3791     if (group->video_sink != playbin->video_sink)
3792       gst_element_set_state (group->video_sink, GST_STATE_NULL);
3793     gst_object_unref (group->video_sink);
3794   }
3795   group->video_sink = NULL;
3796
3797   if (group->uridecodebin) {
3798     REMOVE_SIGNAL (group->uridecodebin, group->pad_added_id);
3799     REMOVE_SIGNAL (group->uridecodebin, group->pad_removed_id);
3800     REMOVE_SIGNAL (group->uridecodebin, group->no_more_pads_id);
3801     REMOVE_SIGNAL (group->uridecodebin, group->notify_source_id);
3802     REMOVE_SIGNAL (group->uridecodebin, group->drained_id);
3803     REMOVE_SIGNAL (group->uridecodebin, group->autoplug_factories_id);
3804     REMOVE_SIGNAL (group->uridecodebin, group->autoplug_select_id);
3805     REMOVE_SIGNAL (group->uridecodebin, group->autoplug_continue_id);
3806     gst_bin_remove (GST_BIN_CAST (playbin), group->uridecodebin);
3807   }
3808
3809   if (group->suburidecodebin) {
3810     REMOVE_SIGNAL (group->suburidecodebin, group->sub_pad_added_id);
3811     REMOVE_SIGNAL (group->suburidecodebin, group->sub_pad_removed_id);
3812     REMOVE_SIGNAL (group->suburidecodebin, group->sub_no_more_pads_id);
3813     REMOVE_SIGNAL (group->suburidecodebin, group->sub_autoplug_continue_id);
3814
3815     /* Might already be removed because of errors */
3816     if (GST_OBJECT_PARENT (group->suburidecodebin) == GST_OBJECT_CAST (playbin))
3817       gst_bin_remove (GST_BIN_CAST (playbin), group->suburidecodebin);
3818   }
3819
3820   GST_SOURCE_GROUP_UNLOCK (group);
3821
3822   return TRUE;
3823 }
3824
3825 /* setup the next group to play, this assumes the next_group is valid and
3826  * configured. It swaps out the current_group and activates the valid
3827  * next_group. */
3828 static gboolean
3829 setup_next_source (GstPlayBin * playbin, GstState target)
3830 {
3831   GstSourceGroup *new_group, *old_group;
3832
3833   GST_DEBUG_OBJECT (playbin, "setup sources");
3834
3835   /* see if there is a next group */
3836   GST_PLAY_BIN_LOCK (playbin);
3837   new_group = playbin->next_group;
3838   if (!new_group || !new_group->valid)
3839     goto no_next_group;
3840
3841   /* first unlink the current source, if any */
3842   old_group = playbin->curr_group;
3843   if (old_group && old_group->valid && old_group->active) {
3844     gst_play_bin_update_cached_duration (playbin);
3845     /* unlink our pads with the sink */
3846     deactivate_group (playbin, old_group);
3847     old_group->valid = FALSE;
3848   }
3849
3850   /* swap old and new */
3851   playbin->curr_group = new_group;
3852   playbin->next_group = old_group;
3853
3854   /* activate the new group */
3855   if (!activate_group (playbin, new_group, target))
3856     goto activate_failed;
3857
3858   GST_PLAY_BIN_UNLOCK (playbin);
3859
3860   return TRUE;
3861
3862   /* ERRORS */
3863 no_next_group:
3864   {
3865     GST_DEBUG_OBJECT (playbin, "no next group");
3866     if (target == GST_STATE_READY && new_group && new_group->uri == NULL)
3867       GST_ELEMENT_ERROR (playbin, RESOURCE, NOT_FOUND, ("No URI set"), (NULL));
3868     GST_PLAY_BIN_UNLOCK (playbin);
3869     return FALSE;
3870   }
3871 activate_failed:
3872   {
3873     GST_DEBUG_OBJECT (playbin, "activate failed");
3874     GST_PLAY_BIN_UNLOCK (playbin);
3875     return FALSE;
3876   }
3877 }
3878
3879 /* The group that is currently playing is copied again to the
3880  * next_group so that it will start playing the next time.
3881  */
3882 static gboolean
3883 save_current_group (GstPlayBin * playbin)
3884 {
3885   GstSourceGroup *curr_group;
3886
3887   GST_DEBUG_OBJECT (playbin, "save current group");
3888
3889   /* see if there is a current group */
3890   GST_PLAY_BIN_LOCK (playbin);
3891   curr_group = playbin->curr_group;
3892   if (curr_group && curr_group->valid && curr_group->active) {
3893     /* unlink our pads with the sink */
3894     deactivate_group (playbin, curr_group);
3895   }
3896   /* swap old and new */
3897   playbin->curr_group = playbin->next_group;
3898   playbin->next_group = curr_group;
3899   GST_PLAY_BIN_UNLOCK (playbin);
3900
3901   return TRUE;
3902 }
3903
3904 /* clear the locked state from all groups. This function is called before a
3905  * state change to NULL is performed on them. */
3906 static gboolean
3907 groups_set_locked_state (GstPlayBin * playbin, gboolean locked)
3908 {
3909   GST_DEBUG_OBJECT (playbin, "setting locked state to %d on all groups",
3910       locked);
3911
3912   GST_PLAY_BIN_LOCK (playbin);
3913   GST_SOURCE_GROUP_LOCK (playbin->curr_group);
3914   group_set_locked_state_unlocked (playbin, playbin->curr_group, locked);
3915   GST_SOURCE_GROUP_UNLOCK (playbin->curr_group);
3916   GST_SOURCE_GROUP_LOCK (playbin->next_group);
3917   group_set_locked_state_unlocked (playbin, playbin->next_group, locked);
3918   GST_SOURCE_GROUP_UNLOCK (playbin->next_group);
3919   GST_PLAY_BIN_UNLOCK (playbin);
3920
3921   return TRUE;
3922 }
3923
3924 static GstStateChangeReturn
3925 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
3926 {
3927   GstStateChangeReturn ret;
3928   GstPlayBin *playbin;
3929   gboolean do_save = FALSE;
3930
3931   playbin = GST_PLAY_BIN (element);
3932
3933   switch (transition) {
3934     case GST_STATE_CHANGE_NULL_TO_READY:
3935       memset (&playbin->duration, 0, sizeof (playbin->duration));
3936       break;
3937     case GST_STATE_CHANGE_READY_TO_PAUSED:
3938       GST_LOG_OBJECT (playbin, "clearing shutdown flag");
3939       memset (&playbin->duration, 0, sizeof (playbin->duration));
3940       g_atomic_int_set (&playbin->shutdown, 0);
3941
3942       if (!setup_next_source (playbin, GST_STATE_READY)) {
3943         ret = GST_STATE_CHANGE_FAILURE;
3944         goto failure;
3945       }
3946       break;
3947     case GST_STATE_CHANGE_PAUSED_TO_READY:
3948     async_down:
3949       /* FIXME unlock our waiting groups */
3950       GST_LOG_OBJECT (playbin, "setting shutdown flag");
3951       g_atomic_int_set (&playbin->shutdown, 1);
3952       memset (&playbin->duration, 0, sizeof (playbin->duration));
3953
3954       /* wait for all callbacks to end by taking the lock.
3955        * No dynamic (critical) new callbacks will
3956        * be able to happen as we set the shutdown flag. */
3957       GST_PLAY_BIN_DYN_LOCK (playbin);
3958       GST_LOG_OBJECT (playbin, "dynamic lock taken, we can continue shutdown");
3959       GST_PLAY_BIN_DYN_UNLOCK (playbin);
3960       if (!do_save)
3961         break;
3962     case GST_STATE_CHANGE_READY_TO_NULL:
3963       /* we go async to PAUSED, so if that fails, we never make it to PAUSED
3964        * an no state change PAUSED to READY passes here,
3965        * though it is a nice-to-have ... */
3966       if (!g_atomic_int_get (&playbin->shutdown)) {
3967         do_save = TRUE;
3968         goto async_down;
3969       }
3970       memset (&playbin->duration, 0, sizeof (playbin->duration));
3971
3972       /* unlock so that all groups go to NULL */
3973       groups_set_locked_state (playbin, FALSE);
3974       break;
3975     default:
3976       break;
3977   }
3978
3979   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3980   if (ret == GST_STATE_CHANGE_FAILURE)
3981     goto failure;
3982
3983   switch (transition) {
3984     case GST_STATE_CHANGE_READY_TO_PAUSED:
3985       break;
3986     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3987       /* FIXME Release audio device when we implement that */
3988       break;
3989     case GST_STATE_CHANGE_PAUSED_TO_READY:
3990       save_current_group (playbin);
3991       break;
3992     case GST_STATE_CHANGE_READY_TO_NULL:
3993     {
3994       guint i;
3995
3996       /* also do missed state change down to READY */
3997       if (do_save)
3998         save_current_group (playbin);
3999       /* Deactive the groups, set the uridecodebins to NULL
4000        * and unref them.
4001        */
4002       for (i = 0; i < 2; i++) {
4003         if (playbin->groups[i].active && playbin->groups[i].valid) {
4004           deactivate_group (playbin, &playbin->groups[i]);
4005           playbin->groups[i].valid = FALSE;
4006         }
4007
4008         if (playbin->groups[i].uridecodebin) {
4009           gst_element_set_state (playbin->groups[i].uridecodebin,
4010               GST_STATE_NULL);
4011           gst_object_unref (playbin->groups[i].uridecodebin);
4012           playbin->groups[i].uridecodebin = NULL;
4013         }
4014
4015         if (playbin->groups[i].suburidecodebin) {
4016           gst_element_set_state (playbin->groups[i].suburidecodebin,
4017               GST_STATE_NULL);
4018           gst_object_unref (playbin->groups[i].suburidecodebin);
4019           playbin->groups[i].suburidecodebin = NULL;
4020         }
4021       }
4022
4023       /* Set our sinks back to NULL, they might not be child of playbin */
4024       if (playbin->audio_sink)
4025         gst_element_set_state (playbin->audio_sink, GST_STATE_NULL);
4026       if (playbin->video_sink)
4027         gst_element_set_state (playbin->video_sink, GST_STATE_NULL);
4028       if (playbin->text_sink)
4029         gst_element_set_state (playbin->text_sink, GST_STATE_NULL);
4030
4031       /* make sure the groups don't perform a state change anymore until we
4032        * enable them again */
4033       groups_set_locked_state (playbin, TRUE);
4034       break;
4035     }
4036     default:
4037       break;
4038   }
4039
4040   return ret;
4041
4042   /* ERRORS */
4043 failure:
4044   {
4045     if (transition == GST_STATE_CHANGE_READY_TO_PAUSED) {
4046       GstSourceGroup *curr_group;
4047
4048       curr_group = playbin->curr_group;
4049       if (curr_group && curr_group->active && curr_group->valid) {
4050         /* unlink our pads with the sink */
4051         deactivate_group (playbin, curr_group);
4052         curr_group->valid = FALSE;
4053       }
4054
4055       /* Swap current and next group back */
4056       playbin->curr_group = playbin->next_group;
4057       playbin->next_group = curr_group;
4058     }
4059     return ret;
4060   }
4061 }
4062
4063 static void
4064 gst_play_bin_overlay_expose (GstVideoOverlay * overlay)
4065 {
4066   GstPlayBin *playbin = GST_PLAY_BIN (overlay);
4067
4068   gst_video_overlay_expose (GST_VIDEO_OVERLAY (playbin->playsink));
4069 }
4070
4071 static void
4072 gst_play_bin_overlay_handle_events (GstVideoOverlay * overlay,
4073     gboolean handle_events)
4074 {
4075   GstPlayBin *playbin = GST_PLAY_BIN (overlay);
4076
4077   gst_video_overlay_handle_events (GST_VIDEO_OVERLAY (playbin->playsink),
4078       handle_events);
4079 }
4080
4081 static void
4082 gst_play_bin_overlay_set_render_rectangle (GstVideoOverlay * overlay, gint x,
4083     gint y, gint width, gint height)
4084 {
4085   GstPlayBin *playbin = GST_PLAY_BIN (overlay);
4086
4087   gst_video_overlay_set_render_rectangle (GST_VIDEO_OVERLAY (playbin->playsink),
4088       x, y, width, height);
4089 }
4090
4091 static void
4092 gst_play_bin_overlay_set_window_handle (GstVideoOverlay * overlay,
4093     guintptr handle)
4094 {
4095   GstPlayBin *playbin = GST_PLAY_BIN (overlay);
4096
4097   gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (playbin->playsink),
4098       handle);
4099 }
4100
4101 static void
4102 gst_play_bin_overlay_init (gpointer g_iface, gpointer g_iface_data)
4103 {
4104   GstVideoOverlayInterface *iface = (GstVideoOverlayInterface *) g_iface;
4105   iface->expose = gst_play_bin_overlay_expose;
4106   iface->handle_events = gst_play_bin_overlay_handle_events;
4107   iface->set_render_rectangle = gst_play_bin_overlay_set_render_rectangle;
4108   iface->set_window_handle = gst_play_bin_overlay_set_window_handle;
4109 }
4110
4111 static void
4112 gst_play_bin_navigation_send_event (GstNavigation * navigation,
4113     GstStructure * structure)
4114 {
4115   GstPlayBin *playbin = GST_PLAY_BIN (navigation);
4116
4117   gst_navigation_send_event (GST_NAVIGATION (playbin->playsink), structure);
4118 }
4119
4120 static void
4121 gst_play_bin_navigation_init (gpointer g_iface, gpointer g_iface_data)
4122 {
4123   GstNavigationInterface *iface = (GstNavigationInterface *) g_iface;
4124
4125   iface->send_event = gst_play_bin_navigation_send_event;
4126 }
4127
4128 static const GList *
4129 gst_play_bin_colorbalance_list_channels (GstColorBalance * balance)
4130 {
4131   GstPlayBin *playbin = GST_PLAY_BIN (balance);
4132
4133   return
4134       gst_color_balance_list_channels (GST_COLOR_BALANCE (playbin->playsink));
4135 }
4136
4137 static void
4138 gst_play_bin_colorbalance_set_value (GstColorBalance * balance,
4139     GstColorBalanceChannel * channel, gint value)
4140 {
4141   GstPlayBin *playbin = GST_PLAY_BIN (balance);
4142
4143   gst_color_balance_set_value (GST_COLOR_BALANCE (playbin->playsink), channel,
4144       value);
4145 }
4146
4147 static gint
4148 gst_play_bin_colorbalance_get_value (GstColorBalance * balance,
4149     GstColorBalanceChannel * channel)
4150 {
4151   GstPlayBin *playbin = GST_PLAY_BIN (balance);
4152
4153   return gst_color_balance_get_value (GST_COLOR_BALANCE (playbin->playsink),
4154       channel);
4155 }
4156
4157 static GstColorBalanceType
4158 gst_play_bin_colorbalance_get_balance_type (GstColorBalance * balance)
4159 {
4160   GstPlayBin *playbin = GST_PLAY_BIN (balance);
4161
4162   return
4163       gst_color_balance_get_balance_type (GST_COLOR_BALANCE
4164       (playbin->playsink));
4165 }
4166
4167 static void
4168 gst_play_bin_colorbalance_init (gpointer g_iface, gpointer g_iface_data)
4169 {
4170   GstColorBalanceInterface *iface = (GstColorBalanceInterface *) g_iface;
4171
4172   iface->list_channels = gst_play_bin_colorbalance_list_channels;
4173   iface->set_value = gst_play_bin_colorbalance_set_value;
4174   iface->get_value = gst_play_bin_colorbalance_get_value;
4175   iface->get_balance_type = gst_play_bin_colorbalance_get_balance_type;
4176 }
4177
4178 gboolean
4179 gst_play_bin2_plugin_init (GstPlugin * plugin)
4180 {
4181   GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
4182
4183   return gst_element_register (plugin, "playbin", GST_RANK_NONE,
4184       GST_TYPE_PLAY_BIN);
4185 }