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