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  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 /**
24  * SECTION:element-playbin
25  * @title: playbin
26  *
27  * Playbin provides a stand-alone everything-in-one abstraction for an
28  * audio and/or video player.
29  *
30  * Playbin can handle both audio and video files and features
31  *
32  * * automatic file type recognition and based on that automatic
33  * selection and usage of the right audio/video/subtitle demuxers/decoders
34  * * visualisations for audio files
35  * * subtitle support for video files. Subtitles can be store in external
36  * files.
37  * * stream selection between different video/audio/subtitles streams
38  * * meta info (tag) extraction
39  * * easy access to the last video sample
40  * * buffering when playing streams over a network
41  * * volume control with mute option
42  *
43  * ## Usage
44  *
45  * A playbin element can be created just like any other element using
46  * gst_element_factory_make(). The file/URI to play should be set via the #GstPlayBin:uri
47  * property. This must be an absolute URI, relative file paths are not allowed.
48  * Example URIs are file:///home/joe/movie.avi or http://www.joedoe.com/foo.ogg
49  *
50  * Playbin is a #GstPipeline. It will notify the application of everything
51  * that's happening (errors, end of stream, tags found, state changes, etc.)
52  * by posting messages on its #GstBus. The application needs to watch the
53  * bus.
54  *
55  * Playback can be initiated by setting the element to PLAYING state using
56  * gst_element_set_state(). Note that the state change will take place in
57  * the background in a separate thread, when the function returns playback
58  * is probably not happening yet and any errors might not have occured yet.
59  * Applications using playbin should ideally be written to deal with things
60  * completely asynchroneous.
61  *
62  * When playback has finished (an EOS message has been received on the bus)
63  * or an error has occured (an ERROR message has been received on the bus) or
64  * the user wants to play a different track, playbin should be set back to
65  * READY or NULL state, then the #GstPlayBin:uri property should be set to the
66  * new location and then playbin be set to PLAYING state again.
67  *
68  * Seeking can be done using gst_element_seek_simple() or gst_element_seek()
69  * on the playbin element. Again, the seek will not be executed
70  * instantaneously, but will be done in a background thread. When the seek
71  * call returns the seek will most likely still be in process. An application
72  * may wait for the seek to finish (or fail) using gst_element_get_state() with
73  * -1 as the timeout, but this will block the user interface and is not
74  * recommended at all.
75  *
76  * Applications may query the current position and duration of the stream
77  * via gst_element_query_position() and gst_element_query_duration() and
78  * setting the format passed to GST_FORMAT_TIME. If the query was successful,
79  * the duration or position will have been returned in units of nanoseconds.
80  *
81  * ## Advanced Usage: specifying the audio and video sink
82  *
83  * By default, if no audio sink or video sink has been specified via the
84  * #GstPlayBin:audio-sink or #GstPlayBin:video-sink property, playbin will use the autoaudiosink
85  * and autovideosink elements to find the first-best available output method.
86  * This should work in most cases, but is not always desirable. Often either
87  * the user or application might want to specify more explicitly what to use
88  * for audio and video output.
89  *
90  * If the application wants more control over how audio or video should be
91  * output, it may create the audio/video sink elements itself (for example
92  * using gst_element_factory_make()) and provide them to playbin using the
93  * #GstPlayBin:audio-sink or #GstPlayBin:video-sink property.
94  *
95  * GNOME-based applications, for example, will usually want to create
96  * gconfaudiosink and gconfvideosink elements and make playbin use those,
97  * so that output happens to whatever the user has configured in the GNOME
98  * Multimedia System Selector configuration dialog.
99  *
100  * The sink elements do not necessarily need to be ready-made sinks. It is
101  * possible to create container elements that look like a sink to playbin,
102  * but in reality contain a number of custom elements linked together. This
103  * can be achieved by creating a #GstBin and putting elements in there and
104  * linking them, and then creating a sink #GstGhostPad for the bin and pointing
105  * it to the sink pad of the first element within the bin. This can be used
106  * for a number of purposes, for example to force output to a particular
107  * format or to modify or observe the data before it is output.
108  *
109  * It is also possible to 'suppress' audio and/or video output by using
110  * 'fakesink' elements (or capture it from there using the fakesink element's
111  * "handoff" signal, which, nota bene, is fired from the streaming thread!).
112  *
113  * ## Retrieving Tags and Other Meta Data
114  *
115  * Most of the common meta data (artist, title, etc.) can be retrieved by
116  * watching for TAG messages on the pipeline's bus (see above).
117  *
118  * Other more specific meta information like width/height/framerate of video
119  * streams or samplerate/number of channels of audio streams can be obtained
120  * from the negotiated caps on the sink pads of the sinks.
121  *
122  * ## Buffering
123  * Playbin handles buffering automatically for the most part, but applications
124  * need to handle parts of the buffering process as well. Whenever playbin is
125  * buffering, it will post BUFFERING messages on the bus with a percentage
126  * value that shows the progress of the buffering process. Applications need
127  * to set playbin to PLAYING or PAUSED state in response to these messages.
128  * They may also want to convey the buffering progress to the user in some
129  * way. Here is how to extract the percentage information from the message:
130  * |[
131  * switch (GST_MESSAGE_TYPE (msg)) {
132  *   case GST_MESSAGE_BUFFERING: {
133  *     gint percent = 0;
134  *     gst_message_parse_buffering (msg, &percent);
135  *     g_print ("Buffering (%u percent done)", percent);
136  *     break;
137  *   }
138  *   ...
139  * }
140  * ]|
141  *
142  * Note that applications should keep/set the pipeline in the PAUSED state when
143  * a BUFFERING message is received with a buffer percent value < 100 and set
144  * the pipeline back to PLAYING state when a BUFFERING message with a value
145  * of 100 percent is received (if PLAYING is the desired state, that is).
146  *
147  * ## Embedding the video window in your application
148  * By default, playbin (or rather the video sinks used) will create their own
149  * window. Applications will usually want to force output to a window of their
150  * own, however. This can be done using the #GstVideoOverlay interface, which most
151  * video sinks implement. See the documentation there for more details.
152  *
153  * ## Specifying which CD/DVD device to use
154  * The device to use for CDs/DVDs needs to be set on the source element
155  * playbin creates before it is opened. The most generic way of doing this
156  * is to connect to playbin's "source-setup" (or "notify::source") signal,
157  * which will be emitted by playbin when it has created the source element
158  * for a particular URI. In the signal callback you can check if the source
159  * element has a "device" property and set it appropriately. In some cases
160  * the device can also be set as part of the URI, but it depends on the
161  * elements involved if this will work or not. For example, for DVD menu
162  * playback, the following syntax might work (if the resindvd plugin is used):
163  * dvd://[/path/to/device]
164  *
165  * ## Handling redirects
166  *
167  * Some elements may post 'redirect' messages on the bus to tell the
168  * application to open another location. These are element messages containing
169  * a structure named 'redirect' along with a 'new-location' field of string
170  * type. The new location may be a relative or an absolute URI. Examples
171  * for such redirects can be found in many quicktime movie trailers.
172  *
173  * ## Examples
174  * |[
175  * gst-launch-1.0 -v playbin uri=file:///path/to/somefile.mp4
176  * ]|
177  *  This will play back the given AVI video file, given that the video and
178  * audio decoders required to decode the content are installed. Since no
179  * special audio sink or video sink is supplied (via playbin's audio-sink or
180  * video-sink properties) playbin will try to find a suitable audio and
181  * video sink automatically using the autoaudiosink and autovideosink elements.
182  * |[
183  * gst-launch-1.0 -v playbin uri=cdda://4
184  * ]|
185  *  This will play back track 4 on an audio CD in your disc drive (assuming
186  * the drive is detected automatically by the plugin).
187  * |[
188  * gst-launch-1.0 -v playbin uri=dvd://
189  * ]|
190  *  This will play back the DVD in your disc drive (assuming
191  * the drive is detected automatically by the plugin).
192  *
193  */
194 
195 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
196  * with newer GLib versions (>= 2.31.0) */
197 #define GLIB_DISABLE_DEPRECATION_WARNINGS
198 
199 #ifdef HAVE_CONFIG_H
200 #include "config.h"
201 #endif
202 
203 #include <string.h>
204 #include <gst/gst.h>
205 
206 #include <gst/gst-i18n-plugin.h>
207 #include <gst/pbutils/pbutils.h>
208 #include <gst/audio/streamvolume.h>
209 #include <gst/video/video-info.h>
210 #include <gst/video/video-multiview.h>
211 #include <gst/video/videooverlay.h>
212 #include <gst/video/navigation.h>
213 #include <gst/video/colorbalance.h>
214 #include "gstplay-enum.h"
215 #include "gstplayback.h"
216 #include "gstplaysink.h"
217 #include "gstsubtitleoverlay.h"
218 #include "gstplaybackutils.h"
219 
220 GST_DEBUG_CATEGORY_STATIC (gst_play_bin_debug);
221 #define GST_CAT_DEFAULT gst_play_bin_debug
222 
223 #define GST_TYPE_PLAY_BIN               (gst_play_bin_get_type())
224 #define GST_PLAY_BIN(obj)               (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PLAY_BIN,GstPlayBin))
225 #define GST_PLAY_BIN_CLASS(klass)       (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PLAY_BIN,GstPlayBinClass))
226 #define GST_IS_PLAY_BIN(obj)            (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PLAY_BIN))
227 #define GST_IS_PLAY_BIN_CLASS(klass)    (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PLAY_BIN))
228 
229 #define ULONG_TO_POINTER(number)        ((gpointer) (guintptr) (number))
230 #define POINTER_TO_ULONG(number)        ((guintptr) (number))
231 
232 #define VOLUME_MAX_DOUBLE 10.0
233 
234 typedef struct _GstPlayBin GstPlayBin;
235 typedef struct _GstPlayBinClass GstPlayBinClass;
236 typedef struct _GstSourceGroup GstSourceGroup;
237 typedef struct _GstSourceCombine GstSourceCombine;
238 
239 typedef GstCaps *(*SourceCombineGetMediaCapsFunc) (void);
240 
241 /* has the info for a combiner and provides the link to the sink */
242 struct _GstSourceCombine
243 {
244   const gchar *media_list[8];   /* the media types for the combiner */
245   SourceCombineGetMediaCapsFunc get_media_caps; /* more complex caps for the combiner */
246   GstPlaySinkType type;         /* the sink pad type of the combiner */
247 
248   GstElement *combiner;         /* the combiner */
249   GPtrArray *channels;
250   GstPad *srcpad;               /* the source pad of the combiner */
251   GstPad *sinkpad;              /* the sinkpad of the sink when the combiner
252                                  * is linked
253                                  */
254   gulong block_id;
255 
256   gboolean has_active_pad;      /* stream combiner has the "active-pad" property */
257 
258   gboolean has_always_ok;       /* stream combiner's sink pads have the "always-ok" property */
259   gboolean has_tags;            /* stream combiner's sink pads have the "tags" property */
260 };
261 
262 #define GST_SOURCE_GROUP_GET_LOCK(group) (&((GstSourceGroup*)(group))->lock)
263 #define GST_SOURCE_GROUP_LOCK(group) (g_mutex_lock (GST_SOURCE_GROUP_GET_LOCK(group)))
264 #define GST_SOURCE_GROUP_UNLOCK(group) (g_mutex_unlock (GST_SOURCE_GROUP_GET_LOCK(group)))
265 
266 enum
267 {
268   PLAYBIN_STREAM_AUDIO = 0,
269   PLAYBIN_STREAM_VIDEO,
270   PLAYBIN_STREAM_TEXT,
271   PLAYBIN_STREAM_LAST
272 };
273 
274 static void avelements_free (gpointer data);
275 static GSequence *avelements_create (GstPlayBin * playbin,
276     gboolean isaudioelement);
277 
278 /* The GstAudioVideoElement structure holding the audio/video decoder
279  * and the audio/video sink factories together with field indicating
280  * the number of common caps features */
281 typedef struct
282 {
283   GstElementFactory *dec;       /* audio:video decoder */
284   GstElementFactory *sink;      /* audio:video sink */
285   guint n_comm_cf;              /* number of common caps features */
286 } GstAVElement;
287 
288 /* a structure to hold the objects for decoding a uri and the subtitle uri
289  */
290 struct _GstSourceGroup
291 {
292   GstPlayBin *playbin;
293 
294   GMutex lock;
295 
296   gboolean valid;               /* the group has valid info to start playback */
297   gboolean active;              /* the group is active */
298 
299   /* properties */
300   gchar *uri;
301   gchar *suburi;
302   GValueArray *streaminfo;
303   GstElement *source;
304 
305   GPtrArray *video_channels;    /* links to combiner pads */
306   GPtrArray *audio_channels;    /* links to combiner pads */
307   GPtrArray *text_channels;     /* links to combiner pads */
308 
309   /* Sinks for this group. These are initialized with
310    * the configure or currently used sink, otherwise
311    * left as NULL and playbin tries to automatically
312    * select a good sink
313    */
314   GstElement *audio_sink;
315   GstElement *video_sink;
316   GstElement *text_sink;
317 
318   /* uridecodebins for uri and subtitle uri */
319   GstElement *uridecodebin;
320   GstElement *suburidecodebin;
321   gint pending;
322   gboolean sub_pending;
323 
324   gboolean have_group_id;
325   guint group_id;
326 
327   gulong pad_added_id;
328   gulong pad_removed_id;
329   gulong no_more_pads_id;
330   gulong notify_source_id;
331   gulong drained_id;
332   gulong autoplug_factories_id;
333   gulong autoplug_select_id;
334   gulong autoplug_continue_id;
335   gulong autoplug_query_id;
336 
337   gulong sub_pad_added_id;
338   gulong sub_pad_removed_id;
339   gulong sub_no_more_pads_id;
340   gulong sub_autoplug_continue_id;
341   gulong sub_autoplug_query_id;
342 
343   gulong block_id;
344 
345   GMutex stream_changed_pending_lock;
346   gboolean stream_changed_pending;
347 
348   /* to prevent that suburidecodebin seek flushes disrupt playback */
349   GMutex suburi_flushes_to_drop_lock;
350   GSList *suburi_flushes_to_drop;
351 
352   /* buffering message stored for after switching */
353   GstMessage *pending_buffering_msg;
354 
355   /* combiners for different streams */
356   GstSourceCombine combiner[PLAYBIN_STREAM_LAST];
357 };
358 
359 #define GST_PLAY_BIN_GET_LOCK(bin) (&((GstPlayBin*)(bin))->lock)
360 #define GST_PLAY_BIN_LOCK(bin) (g_rec_mutex_lock (GST_PLAY_BIN_GET_LOCK(bin)))
361 #define GST_PLAY_BIN_UNLOCK(bin) (g_rec_mutex_unlock (GST_PLAY_BIN_GET_LOCK(bin)))
362 
363 /* lock to protect dynamic callbacks, like no-more-pads */
364 #define GST_PLAY_BIN_DYN_LOCK(bin)    g_mutex_lock (&(bin)->dyn_lock)
365 #define GST_PLAY_BIN_DYN_UNLOCK(bin)  g_mutex_unlock (&(bin)->dyn_lock)
366 
367 /* lock for shutdown */
368 #define GST_PLAY_BIN_SHUTDOWN_LOCK(bin,label)           \
369 G_STMT_START {                                          \
370   if (G_UNLIKELY (g_atomic_int_get (&bin->shutdown)))   \
371     goto label;                                         \
372   GST_PLAY_BIN_DYN_LOCK (bin);                          \
373   if (G_UNLIKELY (g_atomic_int_get (&bin->shutdown))) { \
374     GST_PLAY_BIN_DYN_UNLOCK (bin);                      \
375     goto label;                                         \
376   }                                                     \
377 } G_STMT_END
378 
379 /* unlock for shutdown */
380 #define GST_PLAY_BIN_SHUTDOWN_UNLOCK(bin)         \
381   GST_PLAY_BIN_DYN_UNLOCK (bin);                  \
382 
383 /**
384  * GstPlayBin:
385  *
386  * playbin element structure
387  */
388 struct _GstPlayBin
389 {
390   GstPipeline parent;
391 
392   GRecMutex lock;               /* to protect group switching */
393 
394   /* the groups, we use a double buffer to switch between current and next */
395   GstSourceGroup groups[2];     /* array with group info */
396   GstSourceGroup *curr_group;   /* pointer to the currently playing group */
397   GstSourceGroup *next_group;   /* pointer to the next group */
398 
399   /* properties */
400   guint64 connection_speed;     /* connection speed in bits/sec (0 = unknown) */
401   gint current_video;           /* the currently selected stream */
402   gint current_audio;           /* the currently selected stream */
403   gint current_text;            /* the currently selected stream */
404 
405   guint64 buffer_duration;      /* When buffering, the max buffer duration (ns) */
406   guint buffer_size;            /* When buffering, the max buffer size (bytes) */
407   gboolean force_aspect_ratio;
408 
409   /* Multiview/stereoscopic overrides */
410   GstVideoMultiviewFramePacking multiview_mode;
411   GstVideoMultiviewFlags multiview_flags;
412 
413   /* our play sink */
414   GstPlaySink *playsink;
415 
416   /* the last activated source */
417   GstElement *source;
418 
419   /* lock protecting dynamic adding/removing */
420   GMutex dyn_lock;
421   /* if we are shutting down or not */
422   gint shutdown;
423   gboolean async_pending;       /* async-start has been emitted */
424 
425   GMutex elements_lock;
426   guint32 elements_cookie;
427   GList *elements;              /* factories we can use for selecting elements */
428 
429   gboolean have_selector;       /* set to FALSE when we fail to create an
430                                  * input-selector, so that we only post a
431                                  * warning once */
432 
433   gboolean video_pending_flush_finish;  /* whether we are pending to send a custom
434                                          * custom-video-flush-finish event
435                                          * on pad activation */
436   gboolean audio_pending_flush_finish;  /* whether we are pending to send a custom
437                                          * custom-audio-flush-finish event
438                                          * on pad activation */
439   gboolean text_pending_flush_finish;   /* whether we are pending to send a custom
440                                          * custom-subtitle-flush-finish event
441                                          * on pad activation */
442 
443   GstElement *audio_sink;       /* configured audio sink, or NULL      */
444   GstElement *video_sink;       /* configured video sink, or NULL      */
445   GstElement *text_sink;        /* configured text sink, or NULL       */
446 
447   GstElement *audio_stream_combiner;    /* configured audio stream combiner, or NULL */
448   GstElement *video_stream_combiner;    /* configured video stream combiner, or NULL */
449   GstElement *text_stream_combiner;     /* configured text stream combiner, or NULL */
450 
451   GSequence *aelements;         /* a list of GstAVElements for audio stream */
452   GSequence *velements;         /* a list of GstAVElements for video stream */
453 
454   struct
455   {
456     gboolean valid;
457     GstFormat format;
458     gint64 duration;
459   } duration[5];                /* cached durations */
460 
461   guint64 ring_buffer_max_size; /* 0 means disabled */
462 
463   GList *contexts;
464 };
465 
466 struct _GstPlayBinClass
467 {
468   GstPipelineClass parent_class;
469 
470   /* notify app that the current uri finished decoding and it is possible to
471    * queue a new one for gapless playback */
472   void (*about_to_finish) (GstPlayBin * playbin);
473 
474   /* notify app that number of audio/video/text streams changed */
475   void (*video_changed) (GstPlayBin * playbin);
476   void (*audio_changed) (GstPlayBin * playbin);
477   void (*text_changed) (GstPlayBin * playbin);
478 
479   /* notify app that the tags of audio/video/text streams changed */
480   void (*video_tags_changed) (GstPlayBin * playbin, gint stream);
481   void (*audio_tags_changed) (GstPlayBin * playbin, gint stream);
482   void (*text_tags_changed) (GstPlayBin * playbin, gint stream);
483 
484   /* get audio/video/text tags for a stream */
485   GstTagList *(*get_video_tags) (GstPlayBin * playbin, gint stream);
486   GstTagList *(*get_audio_tags) (GstPlayBin * playbin, gint stream);
487   GstTagList *(*get_text_tags) (GstPlayBin * playbin, gint stream);
488 
489   /* get the last video sample and convert it to the given caps */
490   GstSample *(*convert_sample) (GstPlayBin * playbin, GstCaps * caps);
491 
492   /* get audio/video/text pad for a stream */
493   GstPad *(*get_video_pad) (GstPlayBin * playbin, gint stream);
494   GstPad *(*get_audio_pad) (GstPlayBin * playbin, gint stream);
495   GstPad *(*get_text_pad) (GstPlayBin * playbin, gint stream);
496 };
497 
498 /* props */
499 #define DEFAULT_URI               NULL
500 #define DEFAULT_SUBURI            NULL
501 #define DEFAULT_SOURCE            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
505 #define DEFAULT_N_VIDEO           0
506 #define DEFAULT_CURRENT_VIDEO     -1
507 #define DEFAULT_N_AUDIO           0
508 #define DEFAULT_CURRENT_AUDIO     -1
509 #define DEFAULT_N_TEXT            0
510 #define DEFAULT_CURRENT_TEXT      -1
511 #define DEFAULT_SUBTITLE_ENCODING NULL
512 #define DEFAULT_AUDIO_SINK        NULL
513 #define DEFAULT_VIDEO_SINK        NULL
514 #define DEFAULT_VIS_PLUGIN        NULL
515 #define DEFAULT_TEXT_SINK         NULL
516 #define DEFAULT_VOLUME            1.0
517 #define DEFAULT_MUTE              FALSE
518 #define DEFAULT_FRAME             NULL
519 #define DEFAULT_FONT_DESC         NULL
520 #define DEFAULT_CONNECTION_SPEED  0
521 #define DEFAULT_BUFFER_DURATION   -1
522 #define DEFAULT_BUFFER_SIZE       -1
523 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
524 
525 enum
526 {
527   PROP_0,
528   PROP_URI,
529   PROP_CURRENT_URI,
530   PROP_SUBURI,
531   PROP_CURRENT_SUBURI,
532   PROP_SOURCE,
533   PROP_FLAGS,
534   PROP_N_VIDEO,
535   PROP_CURRENT_VIDEO,
536   PROP_N_AUDIO,
537   PROP_CURRENT_AUDIO,
538   PROP_N_TEXT,
539   PROP_CURRENT_TEXT,
540   PROP_SUBTITLE_ENCODING,
541   PROP_AUDIO_SINK,
542   PROP_VIDEO_SINK,
543   PROP_VIS_PLUGIN,
544   PROP_TEXT_SINK,
545   PROP_VIDEO_STREAM_COMBINER,
546   PROP_AUDIO_STREAM_COMBINER,
547   PROP_TEXT_STREAM_COMBINER,
548   PROP_VOLUME,
549   PROP_MUTE,
550   PROP_SAMPLE,
551   PROP_FONT_DESC,
552   PROP_CONNECTION_SPEED,
553   PROP_BUFFER_SIZE,
554   PROP_BUFFER_DURATION,
555   PROP_AV_OFFSET,
556   PROP_TEXT_OFFSET,
557   PROP_RING_BUFFER_MAX_SIZE,
558   PROP_FORCE_ASPECT_RATIO,
559   PROP_AUDIO_FILTER,
560   PROP_VIDEO_FILTER,
561   PROP_MULTIVIEW_MODE,
562   PROP_MULTIVIEW_FLAGS
563 };
564 
565 /* signals */
566 enum
567 {
568   SIGNAL_ABOUT_TO_FINISH,
569   SIGNAL_CONVERT_SAMPLE,
570   SIGNAL_VIDEO_CHANGED,
571   SIGNAL_AUDIO_CHANGED,
572   SIGNAL_TEXT_CHANGED,
573   SIGNAL_VIDEO_TAGS_CHANGED,
574   SIGNAL_AUDIO_TAGS_CHANGED,
575   SIGNAL_TEXT_TAGS_CHANGED,
576   SIGNAL_GET_VIDEO_TAGS,
577   SIGNAL_GET_AUDIO_TAGS,
578   SIGNAL_GET_TEXT_TAGS,
579   SIGNAL_GET_VIDEO_PAD,
580   SIGNAL_GET_AUDIO_PAD,
581   SIGNAL_GET_TEXT_PAD,
582   SIGNAL_SOURCE_SETUP,
583   SIGNAL_ELEMENT_SETUP,
584   LAST_SIGNAL
585 };
586 
587 static GstStaticCaps raw_audio_caps = GST_STATIC_CAPS ("audio/x-raw(ANY)");
588 static GstStaticCaps raw_video_caps = GST_STATIC_CAPS ("video/x-raw(ANY)");
589 
590 static void gst_play_bin_class_init (GstPlayBinClass * klass);
591 static void gst_play_bin_init (GstPlayBin * playbin);
592 static void gst_play_bin_finalize (GObject * object);
593 
594 static void gst_play_bin_set_property (GObject * object, guint prop_id,
595     const GValue * value, GParamSpec * spec);
596 static void gst_play_bin_get_property (GObject * object, guint prop_id,
597     GValue * value, GParamSpec * spec);
598 
599 static GstStateChangeReturn gst_play_bin_change_state (GstElement * element,
600     GstStateChange transition);
601 
602 static void gst_play_bin_handle_message (GstBin * bin, GstMessage * message);
603 static void gst_play_bin_deep_element_added (GstBin * playbin, GstBin * sub_bin,
604     GstElement * child);
605 static gboolean gst_play_bin_query (GstElement * element, GstQuery * query);
606 static void gst_play_bin_set_context (GstElement * element,
607     GstContext * context);
608 static gboolean gst_play_bin_send_event (GstElement * element,
609     GstEvent * event);
610 
611 static GstTagList *gst_play_bin_get_video_tags (GstPlayBin * playbin,
612     gint stream);
613 static GstTagList *gst_play_bin_get_audio_tags (GstPlayBin * playbin,
614     gint stream);
615 static GstTagList *gst_play_bin_get_text_tags (GstPlayBin * playbin,
616     gint stream);
617 
618 static GstSample *gst_play_bin_convert_sample (GstPlayBin * playbin,
619     GstCaps * caps);
620 
621 static GstPad *gst_play_bin_get_video_pad (GstPlayBin * playbin, gint stream);
622 static GstPad *gst_play_bin_get_audio_pad (GstPlayBin * playbin, gint stream);
623 static GstPad *gst_play_bin_get_text_pad (GstPlayBin * playbin, gint stream);
624 
625 static GstStateChangeReturn setup_next_source (GstPlayBin * playbin,
626     GstState target);
627 
628 static void no_more_pads_cb (GstElement * decodebin, GstSourceGroup * group);
629 static void pad_removed_cb (GstElement * decodebin, GstPad * pad,
630     GstSourceGroup * group);
631 
632 static void gst_play_bin_suburidecodebin_block (GstSourceGroup * group,
633     GstElement * suburidecodebin, gboolean block);
634 static void gst_play_bin_suburidecodebin_seek_to_start (GstSourceGroup * group);
635 
636 static GstElementClass *parent_class;
637 
638 static guint gst_play_bin_signals[LAST_SIGNAL] = { 0 };
639 
640 #define REMOVE_SIGNAL(obj,id)            \
641 if (id) {                                \
642   g_signal_handler_disconnect (obj, id); \
643   id = 0;                                \
644 }
645 
646 static void gst_play_bin_overlay_init (gpointer g_iface, gpointer g_iface_data);
647 static void gst_play_bin_navigation_init (gpointer g_iface,
648     gpointer g_iface_data);
649 static void gst_play_bin_colorbalance_init (gpointer g_iface,
650     gpointer g_iface_data);
651 
652 static GType
gst_play_bin_get_type(void)653 gst_play_bin_get_type (void)
654 {
655   static GType gst_play_bin_type = 0;
656 
657   if (!gst_play_bin_type) {
658     static const GTypeInfo gst_play_bin_info = {
659       sizeof (GstPlayBinClass),
660       NULL,
661       NULL,
662       (GClassInitFunc) gst_play_bin_class_init,
663       NULL,
664       NULL,
665       sizeof (GstPlayBin),
666       0,
667       (GInstanceInitFunc) gst_play_bin_init,
668       NULL
669     };
670     static const GInterfaceInfo svol_info = {
671       NULL, NULL, NULL
672     };
673     static const GInterfaceInfo ov_info = {
674       gst_play_bin_overlay_init,
675       NULL, NULL
676     };
677     static const GInterfaceInfo nav_info = {
678       gst_play_bin_navigation_init,
679       NULL, NULL
680     };
681     static const GInterfaceInfo col_info = {
682       gst_play_bin_colorbalance_init,
683       NULL, NULL
684     };
685 
686     gst_play_bin_type = g_type_register_static (GST_TYPE_PIPELINE,
687         "GstPlayBin", &gst_play_bin_info, 0);
688 
689     g_type_add_interface_static (gst_play_bin_type, GST_TYPE_STREAM_VOLUME,
690         &svol_info);
691     g_type_add_interface_static (gst_play_bin_type, GST_TYPE_VIDEO_OVERLAY,
692         &ov_info);
693     g_type_add_interface_static (gst_play_bin_type, GST_TYPE_NAVIGATION,
694         &nav_info);
695     g_type_add_interface_static (gst_play_bin_type, GST_TYPE_COLOR_BALANCE,
696         &col_info);
697   }
698 
699   return gst_play_bin_type;
700 }
701 
702 static void
gst_play_bin_class_init(GstPlayBinClass * klass)703 gst_play_bin_class_init (GstPlayBinClass * klass)
704 {
705   GObjectClass *gobject_klass;
706   GstElementClass *gstelement_klass;
707   GstBinClass *gstbin_klass;
708 
709   gobject_klass = (GObjectClass *) klass;
710   gstelement_klass = (GstElementClass *) klass;
711   gstbin_klass = (GstBinClass *) klass;
712 
713   parent_class = g_type_class_peek_parent (klass);
714 
715   gobject_klass->set_property = gst_play_bin_set_property;
716   gobject_klass->get_property = gst_play_bin_get_property;
717 
718   gobject_klass->finalize = gst_play_bin_finalize;
719 
720   /**
721    * GstPlayBin:uri
722    *
723    * Set the next URI that playbin will play. This property can be set from the
724    * about-to-finish signal to queue the next media file.
725    */
726   g_object_class_install_property (gobject_klass, PROP_URI,
727       g_param_spec_string ("uri", "URI", "URI of the media to play",
728           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
729 
730    /**
731    * GstPlayBin:current-uri
732    *
733    * The currently playing uri.
734    */
735   g_object_class_install_property (gobject_klass, PROP_CURRENT_URI,
736       g_param_spec_string ("current-uri", "Current URI",
737           "The currently playing URI", NULL,
738           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
739 
740   /**
741    * GstPlayBin:suburi
742    *
743    * Set the next subtitle URI that playbin will play. This property can be
744    * set from the about-to-finish signal to queue the next subtitle media file.
745    */
746   g_object_class_install_property (gobject_klass, PROP_SUBURI,
747       g_param_spec_string ("suburi", ".sub-URI", "Optional URI of a subtitle",
748           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
749 
750   /**
751    * GstPlayBin:current-suburi
752    *
753    * The currently playing subtitle uri.
754    */
755   g_object_class_install_property (gobject_klass, PROP_CURRENT_SUBURI,
756       g_param_spec_string ("current-suburi", "Current .sub-URI",
757           "The currently playing URI of a subtitle",
758           NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
759 
760   g_object_class_install_property (gobject_klass, PROP_SOURCE,
761       g_param_spec_object ("source", "Source", "Source element",
762           GST_TYPE_ELEMENT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
763 
764   /**
765    * GstPlayBin:flags
766    *
767    * Control the behaviour of playbin.
768    */
769   g_object_class_install_property (gobject_klass, PROP_FLAGS,
770       g_param_spec_flags ("flags", "Flags", "Flags to control behaviour",
771           GST_TYPE_PLAY_FLAGS, DEFAULT_FLAGS,
772           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
773 
774   /**
775    * GstPlayBin:n-video
776    *
777    * Get the total number of available video streams.
778    */
779   g_object_class_install_property (gobject_klass, PROP_N_VIDEO,
780       g_param_spec_int ("n-video", "Number Video",
781           "Total number of video streams", 0, G_MAXINT, 0,
782           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
783   /**
784    * GstPlayBin:current-video
785    *
786    * Get or set the currently playing video stream. By default the first video
787    * stream with data is played.
788    */
789   g_object_class_install_property (gobject_klass, PROP_CURRENT_VIDEO,
790       g_param_spec_int ("current-video", "Current Video",
791           "Currently playing video stream (-1 = auto)",
792           -1, G_MAXINT, -1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
793   /**
794    * GstPlayBin:n-audio
795    *
796    * Get the total number of available audio streams.
797    */
798   g_object_class_install_property (gobject_klass, PROP_N_AUDIO,
799       g_param_spec_int ("n-audio", "Number Audio",
800           "Total number of audio streams", 0, G_MAXINT, 0,
801           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
802   /**
803    * GstPlayBin:current-audio
804    *
805    * Get or set the currently playing audio stream. By default the first audio
806    * stream with data is played.
807    */
808   g_object_class_install_property (gobject_klass, PROP_CURRENT_AUDIO,
809       g_param_spec_int ("current-audio", "Current audio",
810           "Currently playing audio stream (-1 = auto)",
811           -1, G_MAXINT, -1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
812   /**
813    * GstPlayBin:n-text
814    *
815    * Get the total number of available subtitle streams.
816    */
817   g_object_class_install_property (gobject_klass, PROP_N_TEXT,
818       g_param_spec_int ("n-text", "Number Text",
819           "Total number of text streams", 0, G_MAXINT, 0,
820           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
821   /**
822    * GstPlayBin:current-text:
823    *
824    * Get or set the currently playing subtitle stream. By default the first
825    * subtitle stream with data is played.
826    */
827   g_object_class_install_property (gobject_klass, PROP_CURRENT_TEXT,
828       g_param_spec_int ("current-text", "Current Text",
829           "Currently playing text stream (-1 = auto)",
830           -1, G_MAXINT, -1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
831 
832   g_object_class_install_property (gobject_klass, PROP_SUBTITLE_ENCODING,
833       g_param_spec_string ("subtitle-encoding", "subtitle encoding",
834           "Encoding to assume if input subtitles are not in UTF-8 encoding. "
835           "If not set, the GST_SUBTITLE_ENCODING environment variable will "
836           "be checked for an encoding to use. If that is not set either, "
837           "ISO-8859-15 will be assumed.", NULL,
838           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
839 
840   g_object_class_install_property (gobject_klass, PROP_VIDEO_FILTER,
841       g_param_spec_object ("video-filter", "Video filter",
842           "the video filter(s) to apply, if possible",
843           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
844   g_object_class_install_property (gobject_klass, PROP_AUDIO_FILTER,
845       g_param_spec_object ("audio-filter", "Audio filter",
846           "the audio filter(s) to apply, if possible",
847           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
848   g_object_class_install_property (gobject_klass, PROP_VIDEO_SINK,
849       g_param_spec_object ("video-sink", "Video Sink",
850           "the video output element to use (NULL = default sink)",
851           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
852   g_object_class_install_property (gobject_klass, PROP_AUDIO_SINK,
853       g_param_spec_object ("audio-sink", "Audio Sink",
854           "the audio output element to use (NULL = default sink)",
855           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
856   g_object_class_install_property (gobject_klass, PROP_VIS_PLUGIN,
857       g_param_spec_object ("vis-plugin", "Vis plugin",
858           "the visualization element to use (NULL = default)",
859           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
860   g_object_class_install_property (gobject_klass, PROP_TEXT_SINK,
861       g_param_spec_object ("text-sink", "Text plugin",
862           "the text output element to use (NULL = default subtitleoverlay)",
863           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
864   /**
865    * GstPlayBin:video-stream-combiner
866    *
867    * Get or set the current video stream combiner. By default, an input-selector
868    * is created and deleted as-needed.
869    */
870   g_object_class_install_property (gobject_klass, PROP_VIDEO_STREAM_COMBINER,
871       g_param_spec_object ("video-stream-combiner", "Video stream combiner",
872           "Current video stream combiner (NULL = input-selector)",
873           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
874   /**
875    * GstPlayBin:audio-stream-combiner
876    *
877    * Get or set the current audio stream combiner. By default, an input-selector
878    * is created and deleted as-needed.
879    */
880   g_object_class_install_property (gobject_klass, PROP_AUDIO_STREAM_COMBINER,
881       g_param_spec_object ("audio-stream-combiner", "Audio stream combiner",
882           "Current audio stream combiner (NULL = input-selector)",
883           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
884   /**
885    * GstPlayBin:text-stream-combiner
886    *
887    * Get or set the current text stream combiner. By default, an input-selector
888    * is created and deleted as-needed.
889    */
890   g_object_class_install_property (gobject_klass, PROP_TEXT_STREAM_COMBINER,
891       g_param_spec_object ("text-stream-combiner", "Text stream combiner",
892           "Current text stream combiner (NULL = input-selector)",
893           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
894 
895   /**
896    * GstPlayBin:volume:
897    *
898    * Get or set the current audio stream volume. 1.0 means 100%,
899    * 0.0 means mute. This uses a linear volume scale.
900    *
901    */
902   g_object_class_install_property (gobject_klass, PROP_VOLUME,
903       g_param_spec_double ("volume", "Volume", "The audio volume, 1.0=100%",
904           0.0, VOLUME_MAX_DOUBLE, 1.0,
905           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
906   g_object_class_install_property (gobject_klass, PROP_MUTE,
907       g_param_spec_boolean ("mute", "Mute",
908           "Mute the audio channel without changing the volume", FALSE,
909           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
910 
911   /**
912    * GstPlayBin:sample:
913    * @playbin: a #GstPlayBin
914    *
915    * Get the currently rendered or prerolled sample in the video sink.
916    * The #GstCaps in the sample will describe the format of the buffer.
917    */
918   g_object_class_install_property (gobject_klass, PROP_SAMPLE,
919       g_param_spec_boxed ("sample", "Sample",
920           "The last sample (NULL = no video available)",
921           GST_TYPE_SAMPLE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
922 
923   g_object_class_install_property (gobject_klass, PROP_FONT_DESC,
924       g_param_spec_string ("subtitle-font-desc",
925           "Subtitle font description",
926           "Pango font description of font "
927           "to be used for subtitle rendering", NULL,
928           G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
929 
930   g_object_class_install_property (gobject_klass, PROP_CONNECTION_SPEED,
931       g_param_spec_uint64 ("connection-speed", "Connection Speed",
932           "Network connection speed in kbps (0 = unknown)",
933           0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
934           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
935 
936   g_object_class_install_property (gobject_klass, PROP_BUFFER_SIZE,
937       g_param_spec_int ("buffer-size", "Buffer size (bytes)",
938           "Buffer size when buffering network streams",
939           -1, G_MAXINT, DEFAULT_BUFFER_SIZE,
940           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
941   g_object_class_install_property (gobject_klass, PROP_BUFFER_DURATION,
942       g_param_spec_int64 ("buffer-duration", "Buffer duration (ns)",
943           "Buffer duration when buffering network streams",
944           -1, G_MAXINT64, DEFAULT_BUFFER_DURATION,
945           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
946   /**
947    * GstPlayBin:av-offset:
948    *
949    * Control the synchronisation offset between the audio and video streams.
950    * Positive values make the audio ahead of the video and negative values make
951    * the audio go behind the video.
952    */
953   g_object_class_install_property (gobject_klass, PROP_AV_OFFSET,
954       g_param_spec_int64 ("av-offset", "AV Offset",
955           "The synchronisation offset between audio and video in nanoseconds",
956           G_MININT64, G_MAXINT64, 0,
957           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
958 
959   /**
960    * GstPlayBin:text-offset:
961    *
962    * Control the synchronisation offset between the text and video streams.
963    * Positive values make the text ahead of the video and negative values make
964    * the text go behind the video.
965    */
966   g_object_class_install_property (gobject_klass, PROP_TEXT_OFFSET,
967       g_param_spec_int64 ("text-offset", "Text Offset",
968           "The synchronisation offset between text and video in nanoseconds",
969           G_MININT64, G_MAXINT64, 0,
970           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
971 
972   /**
973    * GstPlayBin:ring-buffer-max-size
974    *
975    * The maximum size of the ring buffer in bytes. If set to 0, the ring
976    * buffer is disabled. Default 0.
977    */
978   g_object_class_install_property (gobject_klass, PROP_RING_BUFFER_MAX_SIZE,
979       g_param_spec_uint64 ("ring-buffer-max-size",
980           "Max. ring buffer size (bytes)",
981           "Max. amount of data in the ring buffer (bytes, 0 = ring buffer disabled)",
982           0, G_MAXUINT, DEFAULT_RING_BUFFER_MAX_SIZE,
983           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
984 
985   /**
986    * GstPlayBin::force-aspect-ratio:
987    *
988    * Requests the video sink to enforce the video display aspect ratio.
989    */
990   g_object_class_install_property (gobject_klass, PROP_FORCE_ASPECT_RATIO,
991       g_param_spec_boolean ("force-aspect-ratio", "Force Aspect Ratio",
992           "When enabled, scaling will respect original aspect ratio", TRUE,
993           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
994 
995   /**
996    * GstPlayBin::video-multiview-mode:
997    *
998    * Set the stereoscopic mode for video streams that don't contain
999    * any information in the stream, so they can be correctly played
1000    * as 3D streams. If a video already has multiview information
1001    * encoded, this property can override other modes in the set,
1002    * but cannot be used to re-interpret MVC or mixed-mono streams.
1003    *
1004    * See Also: The #GstPlayBin::video-multiview-flags property
1005    *
1006    */
1007   g_object_class_install_property (gobject_klass, PROP_MULTIVIEW_MODE,
1008       g_param_spec_enum ("video-multiview-mode",
1009           "Multiview Mode Override",
1010           "Re-interpret a video stream as one of several frame-packed stereoscopic modes.",
1011           GST_TYPE_VIDEO_MULTIVIEW_FRAME_PACKING,
1012           GST_VIDEO_MULTIVIEW_FRAME_PACKING_NONE,
1013           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1014 
1015   /**
1016    * GstPlayBin::video-multiview-flags:
1017    *
1018    * When overriding the multiview mode of an input stream,
1019    * these flags modify details of the view layout.
1020    *
1021    * See Also: The #GstPlayBin::video-multiview-mode property
1022    */
1023   g_object_class_install_property (gobject_klass, PROP_MULTIVIEW_FLAGS,
1024       g_param_spec_flags ("video-multiview-flags",
1025           "Multiview Flags Override",
1026           "Override details of the multiview frame layout",
1027           GST_TYPE_VIDEO_MULTIVIEW_FLAGS, GST_VIDEO_MULTIVIEW_FLAGS_NONE,
1028           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1029 
1030   /**
1031    * GstPlayBin::about-to-finish
1032    * @playbin: a #GstPlayBin
1033    *
1034    * This signal is emitted when the current uri is about to finish. You can
1035    * set the uri and suburi to make sure that playback continues.
1036    *
1037    * This signal is emitted from the context of a GStreamer streaming thread.
1038    */
1039   gst_play_bin_signals[SIGNAL_ABOUT_TO_FINISH] =
1040       g_signal_new ("about-to-finish", G_TYPE_FROM_CLASS (klass),
1041       G_SIGNAL_RUN_LAST,
1042       G_STRUCT_OFFSET (GstPlayBinClass, about_to_finish), NULL, NULL,
1043       g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
1044 
1045   /**
1046    * GstPlayBin::video-changed
1047    * @playbin: a #GstPlayBin
1048    *
1049    * This signal is emitted whenever the number or order of the video
1050    * streams has changed. The application will most likely want to select
1051    * a new video stream.
1052    *
1053    * This signal is usually emitted from the context of a GStreamer streaming
1054    * thread. You can use gst_message_new_application() and
1055    * gst_element_post_message() to notify your application's main thread.
1056    */
1057   /* FIXME 0.11: turn video-changed signal into message? */
1058   gst_play_bin_signals[SIGNAL_VIDEO_CHANGED] =
1059       g_signal_new ("video-changed", G_TYPE_FROM_CLASS (klass),
1060       G_SIGNAL_RUN_LAST,
1061       G_STRUCT_OFFSET (GstPlayBinClass, video_changed), NULL, NULL,
1062       g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
1063   /**
1064    * GstPlayBin::audio-changed
1065    * @playbin: a #GstPlayBin
1066    *
1067    * This signal is emitted whenever the number or order of the audio
1068    * streams has changed. The application will most likely want to select
1069    * a new audio stream.
1070    *
1071    * This signal may be emitted from the context of a GStreamer streaming thread.
1072    * You can use gst_message_new_application() and gst_element_post_message()
1073    * to notify your application's main thread.
1074    */
1075   /* FIXME 0.11: turn audio-changed signal into message? */
1076   gst_play_bin_signals[SIGNAL_AUDIO_CHANGED] =
1077       g_signal_new ("audio-changed", G_TYPE_FROM_CLASS (klass),
1078       G_SIGNAL_RUN_LAST,
1079       G_STRUCT_OFFSET (GstPlayBinClass, audio_changed), NULL, NULL,
1080       g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
1081   /**
1082    * GstPlayBin::text-changed
1083    * @playbin: a #GstPlayBin
1084    *
1085    * This signal is emitted whenever the number or order of the text
1086    * streams has changed. The application will most likely want to select
1087    * a new text stream.
1088    *
1089    * This signal may be emitted from the context of a GStreamer streaming thread.
1090    * You can use gst_message_new_application() and gst_element_post_message()
1091    * to notify your application's main thread.
1092    */
1093   /* FIXME 0.11: turn text-changed signal into message? */
1094   gst_play_bin_signals[SIGNAL_TEXT_CHANGED] =
1095       g_signal_new ("text-changed", G_TYPE_FROM_CLASS (klass),
1096       G_SIGNAL_RUN_LAST,
1097       G_STRUCT_OFFSET (GstPlayBinClass, text_changed), NULL, NULL,
1098       g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
1099 
1100   /**
1101    * GstPlayBin::video-tags-changed
1102    * @playbin: a #GstPlayBin
1103    * @stream: stream index with changed tags
1104    *
1105    * This signal is emitted whenever the tags of a video stream have changed.
1106    * The application will most likely want to get the new tags.
1107    *
1108    * This signal may be emitted from the context of a GStreamer streaming thread.
1109    * You can use gst_message_new_application() and gst_element_post_message()
1110    * to notify your application's main thread.
1111    */
1112   gst_play_bin_signals[SIGNAL_VIDEO_TAGS_CHANGED] =
1113       g_signal_new ("video-tags-changed", G_TYPE_FROM_CLASS (klass),
1114       G_SIGNAL_RUN_LAST,
1115       G_STRUCT_OFFSET (GstPlayBinClass, video_tags_changed), NULL, NULL,
1116       g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
1117 
1118   /**
1119    * GstPlayBin::audio-tags-changed
1120    * @playbin: a #GstPlayBin
1121    * @stream: stream index with changed tags
1122    *
1123    * This signal is emitted whenever the tags of an audio stream have changed.
1124    * The application will most likely want to get the new tags.
1125    *
1126    * This signal may be emitted from the context of a GStreamer streaming thread.
1127    * You can use gst_message_new_application() and gst_element_post_message()
1128    * to notify your application's main thread.
1129    */
1130   gst_play_bin_signals[SIGNAL_AUDIO_TAGS_CHANGED] =
1131       g_signal_new ("audio-tags-changed", G_TYPE_FROM_CLASS (klass),
1132       G_SIGNAL_RUN_LAST,
1133       G_STRUCT_OFFSET (GstPlayBinClass, audio_tags_changed), NULL, NULL,
1134       g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
1135 
1136   /**
1137    * GstPlayBin::text-tags-changed
1138    * @playbin: a #GstPlayBin
1139    * @stream: stream index with changed tags
1140    *
1141    * This signal is emitted whenever the tags of a text stream have changed.
1142    * The application will most likely want to get the new tags.
1143    *
1144    * This signal may be emitted from the context of a GStreamer streaming thread.
1145    * You can use gst_message_new_application() and gst_element_post_message()
1146    * to notify your application's main thread.
1147    */
1148   gst_play_bin_signals[SIGNAL_TEXT_TAGS_CHANGED] =
1149       g_signal_new ("text-tags-changed", G_TYPE_FROM_CLASS (klass),
1150       G_SIGNAL_RUN_LAST,
1151       G_STRUCT_OFFSET (GstPlayBinClass, text_tags_changed), NULL, NULL,
1152       g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
1153 
1154   /**
1155    * GstPlayBin::source-setup:
1156    * @playbin: a #GstPlayBin
1157    * @source: source element
1158    *
1159    * This signal is emitted after the source element has been created, so
1160    * it can be configured by setting additional properties (e.g. set a
1161    * proxy server for an http source, or set the device and read speed for
1162    * an audio cd source). This is functionally equivalent to connecting to
1163    * the notify::source signal, but more convenient.
1164    *
1165    * This signal is usually emitted from the context of a GStreamer streaming
1166    * thread.
1167    */
1168   gst_play_bin_signals[SIGNAL_SOURCE_SETUP] =
1169       g_signal_new ("source-setup", G_TYPE_FROM_CLASS (klass),
1170       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
1171       g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
1172 
1173   /**
1174    * GstPlayBin::element-setup:
1175    * @playbin: a #GstPlayBin
1176    * @element: an element that was added to the playbin hierarchy
1177    *
1178    * This signal is emitted when a new element is added to playbin or any of
1179    * its sub-bins. This signal can be used to configure elements, e.g. to set
1180    * properties on decoders. This is functionally equivalent to connecting to
1181    * the deep-element-added signal, but more convenient.
1182    *
1183    * This signal is usually emitted from the context of a GStreamer streaming
1184    * thread, so might be called at the same time as code running in the main
1185    * application thread.
1186    *
1187    * Since: 1.10
1188    */
1189   gst_play_bin_signals[SIGNAL_ELEMENT_SETUP] =
1190       g_signal_new ("element-setup", G_TYPE_FROM_CLASS (klass),
1191       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
1192       g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
1193 
1194   /**
1195    * GstPlayBin::get-video-tags
1196    * @playbin: a #GstPlayBin
1197    * @stream: a video stream number
1198    *
1199    * Action signal to retrieve the tags of a specific video stream number.
1200    * This information can be used to select a stream.
1201    *
1202    * Returns: a GstTagList with tags or NULL when the stream number does not
1203    * exist.
1204    */
1205   gst_play_bin_signals[SIGNAL_GET_VIDEO_TAGS] =
1206       g_signal_new ("get-video-tags", G_TYPE_FROM_CLASS (klass),
1207       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1208       G_STRUCT_OFFSET (GstPlayBinClass, get_video_tags), NULL, NULL,
1209       g_cclosure_marshal_generic, GST_TYPE_TAG_LIST, 1, G_TYPE_INT);
1210   /**
1211    * GstPlayBin::get-audio-tags
1212    * @playbin: a #GstPlayBin
1213    * @stream: an audio stream number
1214    *
1215    * Action signal to retrieve the tags of a specific audio stream number.
1216    * This information can be used to select a stream.
1217    *
1218    * Returns: a GstTagList with tags or NULL when the stream number does not
1219    * exist.
1220    */
1221   gst_play_bin_signals[SIGNAL_GET_AUDIO_TAGS] =
1222       g_signal_new ("get-audio-tags", G_TYPE_FROM_CLASS (klass),
1223       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1224       G_STRUCT_OFFSET (GstPlayBinClass, get_audio_tags), NULL, NULL,
1225       g_cclosure_marshal_generic, GST_TYPE_TAG_LIST, 1, G_TYPE_INT);
1226   /**
1227    * GstPlayBin::get-text-tags
1228    * @playbin: a #GstPlayBin
1229    * @stream: a text stream number
1230    *
1231    * Action signal to retrieve the tags of a specific text stream number.
1232    * This information can be used to select a stream.
1233    *
1234    * Returns: a GstTagList with tags or NULL when the stream number does not
1235    * exist.
1236    */
1237   gst_play_bin_signals[SIGNAL_GET_TEXT_TAGS] =
1238       g_signal_new ("get-text-tags", G_TYPE_FROM_CLASS (klass),
1239       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1240       G_STRUCT_OFFSET (GstPlayBinClass, get_text_tags), NULL, NULL,
1241       g_cclosure_marshal_generic, GST_TYPE_TAG_LIST, 1, G_TYPE_INT);
1242   /**
1243    * GstPlayBin::convert-sample
1244    * @playbin: a #GstPlayBin
1245    * @caps: the target format of the frame
1246    *
1247    * Action signal to retrieve the currently playing video frame in the format
1248    * specified by @caps.
1249    * If @caps is %NULL, no conversion will be performed and this function is
1250    * equivalent to the #GstPlayBin::sample property.
1251    *
1252    * Returns: a #GstSample of the current video frame converted to #caps.
1253    * The caps on the sample will describe the final layout of the buffer data.
1254    * %NULL is returned when no current buffer can be retrieved or when the
1255    * conversion failed.
1256    */
1257   gst_play_bin_signals[SIGNAL_CONVERT_SAMPLE] =
1258       g_signal_new ("convert-sample", G_TYPE_FROM_CLASS (klass),
1259       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1260       G_STRUCT_OFFSET (GstPlayBinClass, convert_sample), NULL, NULL,
1261       g_cclosure_marshal_generic, GST_TYPE_SAMPLE, 1, GST_TYPE_CAPS);
1262 
1263   /**
1264    * GstPlayBin::get-video-pad
1265    * @playbin: a #GstPlayBin
1266    * @stream: a video stream number
1267    *
1268    * Action signal to retrieve the stream-combiner sinkpad for a specific
1269    * video stream.
1270    * This pad can be used for notifications of caps changes, stream-specific
1271    * queries, etc.
1272    *
1273    * Returns: a #GstPad, or NULL when the stream number does not exist.
1274    */
1275   gst_play_bin_signals[SIGNAL_GET_VIDEO_PAD] =
1276       g_signal_new ("get-video-pad", G_TYPE_FROM_CLASS (klass),
1277       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1278       G_STRUCT_OFFSET (GstPlayBinClass, get_video_pad), NULL, NULL,
1279       g_cclosure_marshal_generic, GST_TYPE_PAD, 1, G_TYPE_INT);
1280   /**
1281    * GstPlayBin::get-audio-pad
1282    * @playbin: a #GstPlayBin
1283    * @stream: an audio stream number
1284    *
1285    * Action signal to retrieve the stream-combiner sinkpad for a specific
1286    * audio stream.
1287    * This pad can be used for notifications of caps changes, stream-specific
1288    * queries, etc.
1289    *
1290    * Returns: a #GstPad, or NULL when the stream number does not exist.
1291    */
1292   gst_play_bin_signals[SIGNAL_GET_AUDIO_PAD] =
1293       g_signal_new ("get-audio-pad", G_TYPE_FROM_CLASS (klass),
1294       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1295       G_STRUCT_OFFSET (GstPlayBinClass, get_audio_pad), NULL, NULL,
1296       g_cclosure_marshal_generic, GST_TYPE_PAD, 1, G_TYPE_INT);
1297   /**
1298    * GstPlayBin::get-text-pad
1299    * @playbin: a #GstPlayBin
1300    * @stream: a text stream number
1301    *
1302    * Action signal to retrieve the stream-combiner sinkpad for a specific
1303    * text stream.
1304    * This pad can be used for notifications of caps changes, stream-specific
1305    * queries, etc.
1306    *
1307    * Returns: a #GstPad, or NULL when the stream number does not exist.
1308    */
1309   gst_play_bin_signals[SIGNAL_GET_TEXT_PAD] =
1310       g_signal_new ("get-text-pad", G_TYPE_FROM_CLASS (klass),
1311       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1312       G_STRUCT_OFFSET (GstPlayBinClass, get_text_pad), NULL, NULL,
1313       g_cclosure_marshal_generic, GST_TYPE_PAD, 1, G_TYPE_INT);
1314 
1315   klass->get_video_tags = gst_play_bin_get_video_tags;
1316   klass->get_audio_tags = gst_play_bin_get_audio_tags;
1317   klass->get_text_tags = gst_play_bin_get_text_tags;
1318 
1319   klass->convert_sample = gst_play_bin_convert_sample;
1320 
1321   klass->get_video_pad = gst_play_bin_get_video_pad;
1322   klass->get_audio_pad = gst_play_bin_get_audio_pad;
1323   klass->get_text_pad = gst_play_bin_get_text_pad;
1324 
1325   gst_element_class_set_static_metadata (gstelement_klass,
1326       "Player Bin 2", "Generic/Bin/Player",
1327       "Autoplug and play media from an uri",
1328       "Wim Taymans <wim.taymans@gmail.com>");
1329 
1330   gstelement_klass->change_state =
1331       GST_DEBUG_FUNCPTR (gst_play_bin_change_state);
1332   gstelement_klass->query = GST_DEBUG_FUNCPTR (gst_play_bin_query);
1333   gstelement_klass->set_context = GST_DEBUG_FUNCPTR (gst_play_bin_set_context);
1334   gstelement_klass->send_event = GST_DEBUG_FUNCPTR (gst_play_bin_send_event);
1335 
1336   gstbin_klass->handle_message =
1337       GST_DEBUG_FUNCPTR (gst_play_bin_handle_message);
1338   gstbin_klass->deep_element_added =
1339       GST_DEBUG_FUNCPTR (gst_play_bin_deep_element_added);
1340 }
1341 
1342 static void
do_async_start(GstPlayBin * playbin)1343 do_async_start (GstPlayBin * playbin)
1344 {
1345   GstMessage *message;
1346 
1347   playbin->async_pending = TRUE;
1348 
1349   message = gst_message_new_async_start (GST_OBJECT_CAST (playbin));
1350   GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (playbin),
1351       message);
1352 }
1353 
1354 static void
do_async_done(GstPlayBin * playbin)1355 do_async_done (GstPlayBin * playbin)
1356 {
1357   GstMessage *message;
1358 
1359   if (playbin->async_pending) {
1360     GST_DEBUG_OBJECT (playbin, "posting ASYNC_DONE");
1361     message =
1362         gst_message_new_async_done (GST_OBJECT_CAST (playbin),
1363         GST_CLOCK_TIME_NONE);
1364     GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (playbin),
1365         message);
1366 
1367     playbin->async_pending = FALSE;
1368   }
1369 }
1370 
1371 static void
init_group(GstPlayBin * playbin,GstSourceGroup * group)1372 init_group (GstPlayBin * playbin, GstSourceGroup * group)
1373 {
1374   /* store the array for the different channels */
1375   group->video_channels = g_ptr_array_new ();
1376   group->audio_channels = g_ptr_array_new ();
1377   group->text_channels = g_ptr_array_new ();
1378   g_mutex_init (&group->lock);
1379 
1380   group->stream_changed_pending = FALSE;
1381   g_mutex_init (&group->stream_changed_pending_lock);
1382 
1383   /* init combiners. The combiner is found by finding the first prefix that
1384    * matches the media. */
1385   group->playbin = playbin;
1386   /* If you add any items to these lists, check that media_list[] is defined
1387    * above to be large enough to hold MAX(items)+1, so as to accommodate a
1388    * NULL terminator (set when the memory is zeroed on allocation) */
1389   group->combiner[PLAYBIN_STREAM_AUDIO].media_list[0] = "audio/";
1390   group->combiner[PLAYBIN_STREAM_AUDIO].type = GST_PLAY_SINK_TYPE_AUDIO;
1391   group->combiner[PLAYBIN_STREAM_AUDIO].channels = group->audio_channels;
1392   group->combiner[PLAYBIN_STREAM_VIDEO].media_list[0] = "video/";
1393   group->combiner[PLAYBIN_STREAM_VIDEO].media_list[1] = "image/";
1394   group->combiner[PLAYBIN_STREAM_VIDEO].type = GST_PLAY_SINK_TYPE_VIDEO;
1395   group->combiner[PLAYBIN_STREAM_VIDEO].channels = group->video_channels;
1396   group->combiner[PLAYBIN_STREAM_TEXT].media_list[0] = "text/";
1397   group->combiner[PLAYBIN_STREAM_TEXT].media_list[1] = "application/x-subtitle";
1398   group->combiner[PLAYBIN_STREAM_TEXT].media_list[2] = "application/x-ssa";
1399   group->combiner[PLAYBIN_STREAM_TEXT].media_list[3] = "application/x-ass";
1400   group->combiner[PLAYBIN_STREAM_TEXT].media_list[4] = "subpicture/x-dvd";
1401   group->combiner[PLAYBIN_STREAM_TEXT].media_list[5] = "subpicture/";
1402   group->combiner[PLAYBIN_STREAM_TEXT].media_list[6] = "subtitle/";
1403   group->combiner[PLAYBIN_STREAM_TEXT].get_media_caps =
1404       gst_subtitle_overlay_create_factory_caps;
1405   group->combiner[PLAYBIN_STREAM_TEXT].type = GST_PLAY_SINK_TYPE_TEXT;
1406   group->combiner[PLAYBIN_STREAM_TEXT].channels = group->text_channels;
1407 }
1408 
1409 static void
free_group(GstPlayBin * playbin,GstSourceGroup * group)1410 free_group (GstPlayBin * playbin, GstSourceGroup * group)
1411 {
1412   g_free (group->uri);
1413   g_free (group->suburi);
1414   g_ptr_array_free (group->video_channels, TRUE);
1415   g_ptr_array_free (group->audio_channels, TRUE);
1416   g_ptr_array_free (group->text_channels, TRUE);
1417 
1418   g_mutex_clear (&group->lock);
1419   if (group->audio_sink)
1420     gst_object_unref (group->audio_sink);
1421   group->audio_sink = NULL;
1422   if (group->video_sink)
1423     gst_object_unref (group->video_sink);
1424   group->video_sink = NULL;
1425   if (group->text_sink)
1426     gst_object_unref (group->text_sink);
1427   group->text_sink = NULL;
1428 
1429   group->stream_changed_pending = FALSE;
1430   g_mutex_clear (&group->stream_changed_pending_lock);
1431 
1432   g_slist_free (group->suburi_flushes_to_drop);
1433   group->suburi_flushes_to_drop = NULL;
1434 
1435   if (group->suburi_flushes_to_drop_lock.p)
1436     g_mutex_clear (&group->suburi_flushes_to_drop_lock);
1437   group->suburi_flushes_to_drop_lock.p = NULL;
1438 
1439   if (group->pending_buffering_msg)
1440     gst_message_unref (group->pending_buffering_msg);
1441   group->pending_buffering_msg = NULL;
1442 }
1443 
1444 static void
notify_volume_cb(GObject * combiner,GParamSpec * pspec,GstPlayBin * playbin)1445 notify_volume_cb (GObject * combiner, GParamSpec * pspec, GstPlayBin * playbin)
1446 {
1447   g_object_notify (G_OBJECT (playbin), "volume");
1448 }
1449 
1450 static void
notify_mute_cb(GObject * combiner,GParamSpec * pspec,GstPlayBin * playbin)1451 notify_mute_cb (GObject * combiner, GParamSpec * pspec, GstPlayBin * playbin)
1452 {
1453   g_object_notify (G_OBJECT (playbin), "mute");
1454 }
1455 
1456 static void
colorbalance_value_changed_cb(GstColorBalance * balance,GstColorBalanceChannel * channel,gint value,GstPlayBin * playbin)1457 colorbalance_value_changed_cb (GstColorBalance * balance,
1458     GstColorBalanceChannel * channel, gint value, GstPlayBin * playbin)
1459 {
1460   gst_color_balance_value_changed (GST_COLOR_BALANCE (playbin), channel, value);
1461 }
1462 
1463 static gint
compare_factories_func(gconstpointer p1,gconstpointer p2)1464 compare_factories_func (gconstpointer p1, gconstpointer p2)
1465 {
1466   GstPluginFeature *f1, *f2;
1467   gboolean is_sink1, is_sink2;
1468   gboolean is_parser1, is_parser2;
1469 
1470   f1 = (GstPluginFeature *) p1;
1471   f2 = (GstPluginFeature *) p2;
1472 
1473   is_sink1 = gst_element_factory_list_is_type (GST_ELEMENT_FACTORY_CAST (f1),
1474       GST_ELEMENT_FACTORY_TYPE_SINK);
1475   is_sink2 = gst_element_factory_list_is_type (GST_ELEMENT_FACTORY_CAST (f2),
1476       GST_ELEMENT_FACTORY_TYPE_SINK);
1477   is_parser1 = gst_element_factory_list_is_type (GST_ELEMENT_FACTORY_CAST (f1),
1478       GST_ELEMENT_FACTORY_TYPE_PARSER);
1479   is_parser2 = gst_element_factory_list_is_type (GST_ELEMENT_FACTORY_CAST (f2),
1480       GST_ELEMENT_FACTORY_TYPE_PARSER);
1481 
1482   /* First we want all sinks as we prefer a sink if it directly
1483    * supports the current caps */
1484   if (is_sink1 && !is_sink2)
1485     return -1;
1486   else if (!is_sink1 && is_sink2)
1487     return 1;
1488 
1489   /* Then we want all parsers as we always want to plug parsers
1490    * before decoders */
1491   if (is_parser1 && !is_parser2)
1492     return -1;
1493   else if (!is_parser1 && is_parser2)
1494     return 1;
1495 
1496   /* And if it's a both a parser or sink we first sort by rank
1497    * and then by factory name */
1498   return gst_plugin_feature_rank_compare_func (p1, p2);
1499 }
1500 
1501 /* Must be called with elements lock! */
1502 static void
gst_play_bin_update_elements_list(GstPlayBin * playbin)1503 gst_play_bin_update_elements_list (GstPlayBin * playbin)
1504 {
1505   GList *res, *tmp;
1506   guint cookie;
1507 
1508   cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
1509 
1510   if (!playbin->elements || playbin->elements_cookie != cookie) {
1511     if (playbin->elements)
1512       gst_plugin_feature_list_free (playbin->elements);
1513     res =
1514         gst_element_factory_list_get_elements
1515         (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
1516     tmp =
1517         gst_element_factory_list_get_elements
1518         (GST_ELEMENT_FACTORY_TYPE_AUDIOVIDEO_SINKS, GST_RANK_MARGINAL);
1519     playbin->elements = g_list_concat (res, tmp);
1520     playbin->elements = g_list_sort (playbin->elements, compare_factories_func);
1521   }
1522 
1523   if (!playbin->aelements || playbin->elements_cookie != cookie) {
1524     if (playbin->aelements)
1525       g_sequence_free (playbin->aelements);
1526     playbin->aelements = avelements_create (playbin, TRUE);
1527   }
1528 
1529   if (!playbin->velements || playbin->elements_cookie != cookie) {
1530     if (playbin->velements)
1531       g_sequence_free (playbin->velements);
1532     playbin->velements = avelements_create (playbin, FALSE);
1533   }
1534 
1535   playbin->elements_cookie = cookie;
1536 }
1537 
1538 static void
gst_play_bin_init(GstPlayBin * playbin)1539 gst_play_bin_init (GstPlayBin * playbin)
1540 {
1541   g_rec_mutex_init (&playbin->lock);
1542   g_mutex_init (&playbin->dyn_lock);
1543 
1544   /* assume we can create an input-selector */
1545   playbin->have_selector = TRUE;
1546 
1547   /* init groups */
1548   playbin->curr_group = &playbin->groups[0];
1549   playbin->next_group = &playbin->groups[1];
1550   init_group (playbin, &playbin->groups[0]);
1551   init_group (playbin, &playbin->groups[1]);
1552 
1553   /* first filter out the interesting element factories */
1554   g_mutex_init (&playbin->elements_lock);
1555 
1556   /* add sink */
1557   playbin->playsink =
1558       g_object_new (GST_TYPE_PLAY_SINK, "name", "playsink", "send-event-mode",
1559       1, NULL);
1560   gst_bin_add (GST_BIN_CAST (playbin), GST_ELEMENT_CAST (playbin->playsink));
1561   gst_play_sink_set_flags (playbin->playsink, DEFAULT_FLAGS);
1562   /* Connect to notify::volume and notify::mute signals for proxying */
1563   g_signal_connect (playbin->playsink, "notify::volume",
1564       G_CALLBACK (notify_volume_cb), playbin);
1565   g_signal_connect (playbin->playsink, "notify::mute",
1566       G_CALLBACK (notify_mute_cb), playbin);
1567   g_signal_connect (playbin->playsink, "value-changed",
1568       G_CALLBACK (colorbalance_value_changed_cb), playbin);
1569 
1570   playbin->current_video = DEFAULT_CURRENT_VIDEO;
1571   playbin->current_audio = DEFAULT_CURRENT_AUDIO;
1572   playbin->current_text = DEFAULT_CURRENT_TEXT;
1573 
1574   playbin->buffer_duration = DEFAULT_BUFFER_DURATION;
1575   playbin->buffer_size = DEFAULT_BUFFER_SIZE;
1576   playbin->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
1577 
1578   playbin->force_aspect_ratio = TRUE;
1579 
1580   playbin->multiview_mode = GST_VIDEO_MULTIVIEW_FRAME_PACKING_NONE;
1581   playbin->multiview_flags = GST_VIDEO_MULTIVIEW_FLAGS_NONE;
1582 }
1583 
1584 static void
gst_play_bin_finalize(GObject * object)1585 gst_play_bin_finalize (GObject * object)
1586 {
1587   GstPlayBin *playbin;
1588 
1589   playbin = GST_PLAY_BIN (object);
1590 
1591   free_group (playbin, &playbin->groups[0]);
1592   free_group (playbin, &playbin->groups[1]);
1593 
1594   if (playbin->source)
1595     gst_object_unref (playbin->source);
1596 
1597   /* Setting states to NULL is safe here because playsink
1598    * will already be gone and none of these sinks will be
1599    * a child of playsink
1600    */
1601   if (playbin->video_sink) {
1602     gst_element_set_state (playbin->video_sink, GST_STATE_NULL);
1603     gst_object_unref (playbin->video_sink);
1604   }
1605   if (playbin->audio_sink) {
1606     gst_element_set_state (playbin->audio_sink, GST_STATE_NULL);
1607     gst_object_unref (playbin->audio_sink);
1608   }
1609   if (playbin->text_sink) {
1610     gst_element_set_state (playbin->text_sink, GST_STATE_NULL);
1611     gst_object_unref (playbin->text_sink);
1612   }
1613 
1614   if (playbin->video_stream_combiner) {
1615     gst_element_set_state (playbin->video_stream_combiner, GST_STATE_NULL);
1616     gst_object_unref (playbin->video_stream_combiner);
1617   }
1618   if (playbin->audio_stream_combiner) {
1619     gst_element_set_state (playbin->audio_stream_combiner, GST_STATE_NULL);
1620     gst_object_unref (playbin->audio_stream_combiner);
1621   }
1622   if (playbin->text_stream_combiner) {
1623     gst_element_set_state (playbin->text_stream_combiner, GST_STATE_NULL);
1624     gst_object_unref (playbin->text_stream_combiner);
1625   }
1626 
1627   if (playbin->elements)
1628     gst_plugin_feature_list_free (playbin->elements);
1629 
1630   if (playbin->aelements)
1631     g_sequence_free (playbin->aelements);
1632 
1633   if (playbin->velements)
1634     g_sequence_free (playbin->velements);
1635 
1636   g_list_free_full (playbin->contexts, (GDestroyNotify) gst_context_unref);
1637 
1638   g_rec_mutex_clear (&playbin->lock);
1639   g_mutex_clear (&playbin->dyn_lock);
1640   g_mutex_clear (&playbin->elements_lock);
1641 
1642   G_OBJECT_CLASS (parent_class)->finalize (object);
1643 }
1644 
1645 static gboolean
gst_playbin_uri_is_valid(GstPlayBin * playbin,const gchar * uri)1646 gst_playbin_uri_is_valid (GstPlayBin * playbin, const gchar * uri)
1647 {
1648   const gchar *c;
1649 
1650   GST_LOG_OBJECT (playbin, "checking uri '%s'", uri);
1651 
1652   /* this just checks the protocol */
1653   if (!gst_uri_is_valid (uri))
1654     return FALSE;
1655 
1656   for (c = uri; *c != '\0'; ++c) {
1657     if (!g_ascii_isprint (*c))
1658       goto invalid;
1659     if (*c == ' ')
1660       goto invalid;
1661   }
1662 
1663   return TRUE;
1664 
1665 invalid:
1666   {
1667     GST_WARNING_OBJECT (playbin, "uri '%s' not valid, character #%u",
1668         uri, (guint) ((guintptr) c - (guintptr) uri));
1669     return FALSE;
1670   }
1671 }
1672 
1673 static void
gst_play_bin_set_uri(GstPlayBin * playbin,const gchar * uri)1674 gst_play_bin_set_uri (GstPlayBin * playbin, const gchar * uri)
1675 {
1676   GstSourceGroup *group;
1677 
1678   if (uri == NULL) {
1679     g_warning ("cannot set NULL uri");
1680     return;
1681   }
1682 
1683   if (!gst_playbin_uri_is_valid (playbin, uri)) {
1684     if (g_str_has_prefix (uri, "file:")) {
1685       GST_WARNING_OBJECT (playbin, "not entirely correct file URI '%s' - make "
1686           "sure to escape spaces and non-ASCII characters properly and specify "
1687           "an absolute path. Use gst_filename_to_uri() to convert filenames "
1688           "to URIs", uri);
1689     } else {
1690       /* GST_ERROR_OBJECT (playbin, "malformed URI '%s'", uri); */
1691     }
1692   }
1693 
1694   GST_PLAY_BIN_LOCK (playbin);
1695   group = playbin->next_group;
1696 
1697   GST_SOURCE_GROUP_LOCK (group);
1698   /* store the uri in the next group we will play */
1699   g_free (group->uri);
1700   group->uri = g_strdup (uri);
1701   group->valid = TRUE;
1702   GST_SOURCE_GROUP_UNLOCK (group);
1703 
1704   GST_DEBUG ("set new uri to %s", uri);
1705   GST_PLAY_BIN_UNLOCK (playbin);
1706 }
1707 
1708 static void
gst_play_bin_set_suburi(GstPlayBin * playbin,const gchar * suburi)1709 gst_play_bin_set_suburi (GstPlayBin * playbin, const gchar * suburi)
1710 {
1711   GstSourceGroup *group;
1712 
1713   GST_PLAY_BIN_LOCK (playbin);
1714   group = playbin->next_group;
1715 
1716   GST_SOURCE_GROUP_LOCK (group);
1717   g_free (group->suburi);
1718   group->suburi = g_strdup (suburi);
1719   GST_SOURCE_GROUP_UNLOCK (group);
1720 
1721   GST_DEBUG ("setting new .sub uri to %s", suburi);
1722 
1723   GST_PLAY_BIN_UNLOCK (playbin);
1724 }
1725 
1726 static void
gst_play_bin_set_flags(GstPlayBin * playbin,GstPlayFlags flags)1727 gst_play_bin_set_flags (GstPlayBin * playbin, GstPlayFlags flags)
1728 {
1729   GstPlayFlags old_flags;
1730   old_flags = gst_play_sink_get_flags (playbin->playsink);
1731 
1732   if (flags != old_flags) {
1733     gst_play_sink_set_flags (playbin->playsink, flags);
1734     gst_play_sink_reconfigure (playbin->playsink);
1735   }
1736 }
1737 
1738 static GstPlayFlags
gst_play_bin_get_flags(GstPlayBin * playbin)1739 gst_play_bin_get_flags (GstPlayBin * playbin)
1740 {
1741   GstPlayFlags flags;
1742 
1743   flags = gst_play_sink_get_flags (playbin->playsink);
1744 
1745   return flags;
1746 }
1747 
1748 /* get the currently playing group or if nothing is playing, the next
1749  * group. Must be called with the PLAY_BIN_LOCK. */
1750 static GstSourceGroup *
get_group(GstPlayBin * playbin)1751 get_group (GstPlayBin * playbin)
1752 {
1753   GstSourceGroup *result;
1754 
1755   if (!(result = playbin->curr_group))
1756     result = playbin->next_group;
1757 
1758   return result;
1759 }
1760 
1761 static GstPad *
gst_play_bin_get_video_pad(GstPlayBin * playbin,gint stream)1762 gst_play_bin_get_video_pad (GstPlayBin * playbin, gint stream)
1763 {
1764   GstPad *sinkpad = NULL;
1765   GstSourceGroup *group;
1766 
1767   GST_PLAY_BIN_LOCK (playbin);
1768   group = get_group (playbin);
1769   if (stream < group->video_channels->len) {
1770     sinkpad = g_ptr_array_index (group->video_channels, stream);
1771     gst_object_ref (sinkpad);
1772   }
1773   GST_PLAY_BIN_UNLOCK (playbin);
1774 
1775   return sinkpad;
1776 }
1777 
1778 static GstPad *
gst_play_bin_get_audio_pad(GstPlayBin * playbin,gint stream)1779 gst_play_bin_get_audio_pad (GstPlayBin * playbin, gint stream)
1780 {
1781   GstPad *sinkpad = NULL;
1782   GstSourceGroup *group;
1783 
1784   GST_PLAY_BIN_LOCK (playbin);
1785   group = get_group (playbin);
1786   if (stream < group->audio_channels->len) {
1787     sinkpad = g_ptr_array_index (group->audio_channels, stream);
1788     gst_object_ref (sinkpad);
1789   }
1790   GST_PLAY_BIN_UNLOCK (playbin);
1791 
1792   return sinkpad;
1793 }
1794 
1795 static GstPad *
gst_play_bin_get_text_pad(GstPlayBin * playbin,gint stream)1796 gst_play_bin_get_text_pad (GstPlayBin * playbin, gint stream)
1797 {
1798   GstPad *sinkpad = NULL;
1799   GstSourceGroup *group;
1800 
1801   GST_PLAY_BIN_LOCK (playbin);
1802   group = get_group (playbin);
1803   if (stream < group->text_channels->len) {
1804     sinkpad = g_ptr_array_index (group->text_channels, stream);
1805     gst_object_ref (sinkpad);
1806   }
1807   GST_PLAY_BIN_UNLOCK (playbin);
1808 
1809   return sinkpad;
1810 }
1811 
1812 
1813 static GstTagList *
get_tags(GstPlayBin * playbin,GstSourceGroup * group,gint type,gint stream)1814 get_tags (GstPlayBin * playbin, GstSourceGroup * group, gint type, gint stream)
1815 {
1816   GstTagList *result;
1817   GPtrArray *channels;
1818   GstPad *sinkpad;
1819 
1820   switch (type) {
1821     case PLAYBIN_STREAM_AUDIO:
1822       channels = group->audio_channels;
1823       break;
1824     case PLAYBIN_STREAM_VIDEO:
1825       channels = group->video_channels;
1826       break;
1827     case PLAYBIN_STREAM_TEXT:
1828       channels = group->text_channels;
1829       break;
1830     default:
1831       channels = NULL;
1832       break;
1833   }
1834 
1835   if (!channels || stream >= channels->len || !group->combiner[type].has_tags)
1836     return NULL;
1837 
1838   sinkpad = g_ptr_array_index (channels, stream);
1839   g_object_get (sinkpad, "tags", &result, NULL);
1840 
1841   return result;
1842 }
1843 
1844 static GstTagList *
gst_play_bin_get_video_tags(GstPlayBin * playbin,gint stream)1845 gst_play_bin_get_video_tags (GstPlayBin * playbin, gint stream)
1846 {
1847   GstTagList *result;
1848   GstSourceGroup *group;
1849 
1850   GST_PLAY_BIN_LOCK (playbin);
1851   group = get_group (playbin);
1852   result = get_tags (playbin, group, PLAYBIN_STREAM_VIDEO, stream);
1853   GST_PLAY_BIN_UNLOCK (playbin);
1854 
1855   return result;
1856 }
1857 
1858 static GstTagList *
gst_play_bin_get_audio_tags(GstPlayBin * playbin,gint stream)1859 gst_play_bin_get_audio_tags (GstPlayBin * playbin, gint stream)
1860 {
1861   GstTagList *result;
1862   GstSourceGroup *group;
1863 
1864   GST_PLAY_BIN_LOCK (playbin);
1865   group = get_group (playbin);
1866   result = get_tags (playbin, group, PLAYBIN_STREAM_AUDIO, stream);
1867   GST_PLAY_BIN_UNLOCK (playbin);
1868 
1869   return result;
1870 }
1871 
1872 static GstTagList *
gst_play_bin_get_text_tags(GstPlayBin * playbin,gint stream)1873 gst_play_bin_get_text_tags (GstPlayBin * playbin, gint stream)
1874 {
1875   GstTagList *result;
1876   GstSourceGroup *group;
1877 
1878   GST_PLAY_BIN_LOCK (playbin);
1879   group = get_group (playbin);
1880   result = get_tags (playbin, group, PLAYBIN_STREAM_TEXT, stream);
1881   GST_PLAY_BIN_UNLOCK (playbin);
1882 
1883   return result;
1884 }
1885 
1886 static GstSample *
gst_play_bin_convert_sample(GstPlayBin * playbin,GstCaps * caps)1887 gst_play_bin_convert_sample (GstPlayBin * playbin, GstCaps * caps)
1888 {
1889   return gst_play_sink_convert_sample (playbin->playsink, caps);
1890 }
1891 
1892 /* Returns current stream number, or -1 if none has been selected yet */
1893 static int
get_current_stream_number(GstPlayBin * playbin,GstSourceCombine * combine,GPtrArray * channels)1894 get_current_stream_number (GstPlayBin * playbin, GstSourceCombine * combine,
1895     GPtrArray * channels)
1896 {
1897   /* Internal API cleanup would make this easier... */
1898   int i;
1899   GstPad *pad, *current;
1900   GstObject *combiner = NULL;
1901   int ret = -1;
1902 
1903   if (!combine->has_active_pad) {
1904     GST_WARNING_OBJECT (playbin,
1905         "combiner doesn't have the \"active-pad\" property");
1906     return ret;
1907   }
1908 
1909   for (i = 0; i < channels->len; i++) {
1910     pad = g_ptr_array_index (channels, i);
1911     if ((combiner = gst_pad_get_parent (pad))) {
1912       g_object_get (combiner, "active-pad", &current, NULL);
1913       gst_object_unref (combiner);
1914 
1915       if (pad == current) {
1916         gst_object_unref (current);
1917         ret = i;
1918         break;
1919       }
1920 
1921       if (current)
1922         gst_object_unref (current);
1923     }
1924   }
1925 
1926   return ret;
1927 }
1928 
1929 static gboolean
gst_play_bin_send_custom_event(GstObject * combiner,const gchar * event_name)1930 gst_play_bin_send_custom_event (GstObject * combiner, const gchar * event_name)
1931 {
1932   GstPad *src;
1933   GstPad *peer;
1934   GstStructure *s;
1935   GstEvent *event;
1936   gboolean ret = FALSE;
1937 
1938   src = gst_element_get_static_pad (GST_ELEMENT_CAST (combiner), "src");
1939   peer = gst_pad_get_peer (src);
1940   if (peer) {
1941     s = gst_structure_new_empty (event_name);
1942     event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_OOB, s);
1943     gst_pad_send_event (peer, event);
1944     gst_object_unref (peer);
1945     ret = TRUE;
1946   }
1947   gst_object_unref (src);
1948   return ret;
1949 }
1950 
1951 static gboolean
gst_play_bin_set_current_video_stream(GstPlayBin * playbin,gint stream)1952 gst_play_bin_set_current_video_stream (GstPlayBin * playbin, gint stream)
1953 {
1954   GstSourceGroup *group;
1955   GPtrArray *channels;
1956   GstPad *sinkpad;
1957 
1958   GST_PLAY_BIN_LOCK (playbin);
1959 
1960   GST_DEBUG_OBJECT (playbin, "Changing current video stream %d -> %d",
1961       playbin->current_video, stream);
1962 
1963   group = get_group (playbin);
1964   if (!group->combiner[PLAYBIN_STREAM_VIDEO].has_active_pad)
1965     goto no_active_pad;
1966   if (!(channels = group->video_channels))
1967     goto no_channels;
1968 
1969   if (stream == -1 || channels->len <= stream) {
1970     sinkpad = NULL;
1971   } else {
1972     /* take channel from selected stream */
1973     sinkpad = g_ptr_array_index (channels, stream);
1974   }
1975 
1976   if (sinkpad)
1977     gst_object_ref (sinkpad);
1978   GST_PLAY_BIN_UNLOCK (playbin);
1979 
1980   if (sinkpad) {
1981     GstObject *combiner;
1982 
1983     if ((combiner = gst_pad_get_parent (sinkpad))) {
1984       GstPad *old_sinkpad;
1985 
1986       g_object_get (combiner, "active-pad", &old_sinkpad, NULL);
1987 
1988       if (old_sinkpad != sinkpad) {
1989         if (gst_play_bin_send_custom_event (combiner,
1990                 "playsink-custom-video-flush"))
1991           playbin->video_pending_flush_finish = TRUE;
1992 
1993         /* activate the selected pad */
1994         g_object_set (combiner, "active-pad", sinkpad, NULL);
1995       }
1996 
1997       if (old_sinkpad)
1998         gst_object_unref (old_sinkpad);
1999 
2000       gst_object_unref (combiner);
2001     }
2002     gst_object_unref (sinkpad);
2003   }
2004   return TRUE;
2005 
2006 no_active_pad:
2007   {
2008     GST_PLAY_BIN_UNLOCK (playbin);
2009     GST_WARNING_OBJECT (playbin,
2010         "can't switch video, the stream combiner's sink pads don't have the \"active-pad\" property");
2011     return FALSE;
2012   }
2013 no_channels:
2014   {
2015     GST_PLAY_BIN_UNLOCK (playbin);
2016     GST_DEBUG_OBJECT (playbin, "can't switch video, we have no channels");
2017     return FALSE;
2018   }
2019 }
2020 
2021 static gboolean
gst_play_bin_set_current_audio_stream(GstPlayBin * playbin,gint stream)2022 gst_play_bin_set_current_audio_stream (GstPlayBin * playbin, gint stream)
2023 {
2024   GstSourceGroup *group;
2025   GPtrArray *channels;
2026   GstPad *sinkpad;
2027 
2028   GST_PLAY_BIN_LOCK (playbin);
2029 
2030   GST_DEBUG_OBJECT (playbin, "Changing current audio stream %d -> %d",
2031       playbin->current_audio, stream);
2032 
2033   group = get_group (playbin);
2034   if (!group->combiner[PLAYBIN_STREAM_AUDIO].has_active_pad)
2035     goto no_active_pad;
2036   if (!(channels = group->audio_channels))
2037     goto no_channels;
2038 
2039   if (stream == -1 || channels->len <= stream) {
2040     sinkpad = NULL;
2041   } else {
2042     /* take channel from selected stream */
2043     sinkpad = g_ptr_array_index (channels, stream);
2044   }
2045 
2046   if (sinkpad)
2047     gst_object_ref (sinkpad);
2048   GST_PLAY_BIN_UNLOCK (playbin);
2049 
2050   if (sinkpad) {
2051     GstObject *combiner;
2052 
2053     if ((combiner = gst_pad_get_parent (sinkpad))) {
2054       GstPad *old_sinkpad;
2055 
2056       g_object_get (combiner, "active-pad", &old_sinkpad, NULL);
2057 
2058       if (old_sinkpad != sinkpad) {
2059         if (gst_play_bin_send_custom_event (combiner,
2060                 "playsink-custom-audio-flush"))
2061           playbin->audio_pending_flush_finish = TRUE;
2062 
2063         /* activate the selected pad */
2064         g_object_set (combiner, "active-pad", sinkpad, NULL);
2065       }
2066 
2067       if (old_sinkpad)
2068         gst_object_unref (old_sinkpad);
2069 
2070       gst_object_unref (combiner);
2071     }
2072     gst_object_unref (sinkpad);
2073   }
2074   return TRUE;
2075 
2076 no_active_pad:
2077   {
2078     GST_PLAY_BIN_UNLOCK (playbin);
2079     GST_WARNING_OBJECT (playbin,
2080         "can't switch audio, the stream combiner's sink pads don't have the \"active-pad\" property");
2081     return FALSE;
2082   }
2083 no_channels:
2084   {
2085     GST_PLAY_BIN_UNLOCK (playbin);
2086     GST_DEBUG_OBJECT (playbin, "can't switch audio, we have no channels");
2087     return FALSE;
2088   }
2089 }
2090 
2091 static void
gst_play_bin_suburidecodebin_seek_to_start(GstSourceGroup * group)2092 gst_play_bin_suburidecodebin_seek_to_start (GstSourceGroup * group)
2093 {
2094   GstElement *suburidecodebin = group->suburidecodebin;
2095   GstIterator *it = gst_element_iterate_src_pads (suburidecodebin);
2096   GstPad *sinkpad;
2097   GValue item = { 0, };
2098 
2099   if (it && gst_iterator_next (it, &item) == GST_ITERATOR_OK
2100       && ((sinkpad = g_value_get_object (&item)) != NULL)) {
2101     GstEvent *event;
2102     guint32 seqnum;
2103 
2104     event =
2105         gst_event_new_seek (1.0, GST_FORMAT_BYTES, GST_SEEK_FLAG_FLUSH,
2106         GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, -1);
2107     seqnum = gst_event_get_seqnum (event);
2108 
2109     /* store the seqnum to drop flushes from this seek later */
2110     g_mutex_lock (&group->suburi_flushes_to_drop_lock);
2111     group->suburi_flushes_to_drop =
2112         g_slist_append (group->suburi_flushes_to_drop,
2113         GUINT_TO_POINTER (seqnum));
2114     g_mutex_unlock (&group->suburi_flushes_to_drop_lock);
2115 
2116     if (!gst_pad_send_event (sinkpad, event)) {
2117       event =
2118           gst_event_new_seek (1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
2119           GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, -1);
2120       gst_event_set_seqnum (event, seqnum);
2121       if (!gst_pad_send_event (sinkpad, event)) {
2122         GST_DEBUG_OBJECT (suburidecodebin, "Seeking to the beginning failed!");
2123 
2124         g_mutex_lock (&group->suburi_flushes_to_drop_lock);
2125         group->suburi_flushes_to_drop =
2126             g_slist_remove (group->suburi_flushes_to_drop,
2127             GUINT_TO_POINTER (seqnum));
2128         g_mutex_unlock (&group->suburi_flushes_to_drop_lock);
2129       }
2130     }
2131 
2132     g_value_unset (&item);
2133   }
2134 
2135   if (it)
2136     gst_iterator_free (it);
2137 }
2138 
2139 static void
source_combine_remove_pads(GstPlayBin * playbin,GstSourceCombine * combine)2140 source_combine_remove_pads (GstPlayBin * playbin, GstSourceCombine * combine)
2141 {
2142   if (combine->sinkpad) {
2143     GST_LOG_OBJECT (playbin, "unlinking from sink");
2144     gst_pad_unlink (combine->srcpad, combine->sinkpad);
2145 
2146     /* release back */
2147     GST_LOG_OBJECT (playbin, "release sink pad");
2148     gst_play_sink_release_pad (playbin->playsink, combine->sinkpad);
2149     gst_object_unref (combine->sinkpad);
2150     combine->sinkpad = NULL;
2151   }
2152   gst_object_unref (combine->srcpad);
2153   combine->srcpad = NULL;
2154 }
2155 
2156 static GstPadProbeReturn
block_serialized_data_cb(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)2157 block_serialized_data_cb (GstPad * pad, GstPadProbeInfo * info,
2158     gpointer user_data)
2159 {
2160   if (GST_IS_EVENT (info->data) && !GST_EVENT_IS_SERIALIZED (info->data)) {
2161     GST_DEBUG_OBJECT (pad, "Letting non-serialized event %s pass",
2162         GST_EVENT_TYPE_NAME (info->data));
2163     return GST_PAD_PROBE_PASS;
2164   }
2165 
2166   return GST_PAD_PROBE_OK;
2167 }
2168 
2169 static void
gst_play_bin_suburidecodebin_block(GstSourceGroup * group,GstElement * suburidecodebin,gboolean block)2170 gst_play_bin_suburidecodebin_block (GstSourceGroup * group,
2171     GstElement * suburidecodebin, gboolean block)
2172 {
2173   GstIterator *it = gst_element_iterate_src_pads (suburidecodebin);
2174   gboolean done = FALSE;
2175   GValue item = { 0, };
2176 
2177   GST_DEBUG_OBJECT (suburidecodebin, "Blocking suburidecodebin: %d", block);
2178 
2179   if (!it)
2180     return;
2181   while (!done) {
2182     GstPad *sinkpad;
2183 
2184     switch (gst_iterator_next (it, &item)) {
2185       case GST_ITERATOR_OK:
2186         sinkpad = g_value_get_object (&item);
2187         if (block) {
2188           group->block_id =
2189               gst_pad_add_probe (sinkpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
2190               block_serialized_data_cb, NULL, NULL);
2191         } else if (group->block_id) {
2192           gst_pad_remove_probe (sinkpad, group->block_id);
2193           group->block_id = 0;
2194         }
2195         g_value_reset (&item);
2196         break;
2197       case GST_ITERATOR_DONE:
2198         done = TRUE;
2199         break;
2200       case GST_ITERATOR_RESYNC:
2201         gst_iterator_resync (it);
2202         break;
2203       case GST_ITERATOR_ERROR:
2204         done = TRUE;
2205         break;
2206     }
2207   }
2208   g_value_unset (&item);
2209   gst_iterator_free (it);
2210 }
2211 
2212 static gboolean
gst_play_bin_set_current_text_stream(GstPlayBin * playbin,gint stream)2213 gst_play_bin_set_current_text_stream (GstPlayBin * playbin, gint stream)
2214 {
2215   GstSourceGroup *group;
2216   GPtrArray *channels;
2217   GstPad *sinkpad;
2218 
2219   GST_PLAY_BIN_LOCK (playbin);
2220 
2221   GST_DEBUG_OBJECT (playbin, "Changing current text stream %d -> %d",
2222       playbin->current_text, stream);
2223 
2224   group = get_group (playbin);
2225   if (!group->combiner[PLAYBIN_STREAM_TEXT].has_active_pad)
2226     goto no_active_pad;
2227   if (!(channels = group->text_channels))
2228     goto no_channels;
2229 
2230   if (stream == -1 || channels->len <= stream) {
2231     sinkpad = NULL;
2232   } else {
2233     /* take channel from selected stream */
2234     sinkpad = g_ptr_array_index (channels, stream);
2235   }
2236 
2237   if (sinkpad)
2238     gst_object_ref (sinkpad);
2239   GST_PLAY_BIN_UNLOCK (playbin);
2240 
2241   if (sinkpad) {
2242     GstObject *combiner;
2243 
2244     if ((combiner = gst_pad_get_parent (sinkpad))) {
2245       GstPad *old_sinkpad;
2246 
2247       g_object_get (combiner, "active-pad", &old_sinkpad, NULL);
2248 
2249       if (old_sinkpad != sinkpad) {
2250         gboolean need_unblock, need_block, need_seek;
2251         GstPad *peer = NULL, *oldpeer = NULL;
2252         GstElement *parent_element = NULL, *old_parent_element = NULL;
2253 
2254         /* Now check if we need to seek the suburidecodebin to the beginning
2255          * or if we need to block all suburidecodebin sinkpads or if we need
2256          * to unblock all suburidecodebin sinkpads
2257          */
2258         if (sinkpad)
2259           peer = gst_pad_get_peer (sinkpad);
2260         if (old_sinkpad)
2261           oldpeer = gst_pad_get_peer (old_sinkpad);
2262 
2263         if (peer)
2264           parent_element = gst_pad_get_parent_element (peer);
2265         if (oldpeer)
2266           old_parent_element = gst_pad_get_parent_element (oldpeer);
2267 
2268         need_block = (old_parent_element == group->suburidecodebin
2269             && parent_element != old_parent_element);
2270         need_unblock = (parent_element == group->suburidecodebin
2271             && parent_element != old_parent_element);
2272         need_seek = (parent_element == group->suburidecodebin);
2273 
2274         if (peer)
2275           gst_object_unref (peer);
2276         if (oldpeer)
2277           gst_object_unref (oldpeer);
2278         if (parent_element)
2279           gst_object_unref (parent_element);
2280         if (old_parent_element)
2281           gst_object_unref (old_parent_element);
2282 
2283         /* Block all suburidecodebin sinkpads */
2284         if (need_block)
2285           gst_play_bin_suburidecodebin_block (group, group->suburidecodebin,
2286               TRUE);
2287 
2288         if (gst_play_bin_send_custom_event (combiner,
2289                 "playsink-custom-subtitle-flush"))
2290           playbin->text_pending_flush_finish = TRUE;
2291 
2292         /* activate the selected pad */
2293         g_object_set (combiner, "active-pad", sinkpad, NULL);
2294 
2295         /* Unblock pads if necessary */
2296         if (need_unblock)
2297           gst_play_bin_suburidecodebin_block (group, group->suburidecodebin,
2298               FALSE);
2299 
2300         /* seek to the beginning */
2301         if (need_seek)
2302           gst_play_bin_suburidecodebin_seek_to_start (group);
2303       }
2304       gst_object_unref (combiner);
2305 
2306       if (old_sinkpad)
2307         gst_object_unref (old_sinkpad);
2308     }
2309     gst_object_unref (sinkpad);
2310   }
2311   return TRUE;
2312 
2313 no_active_pad:
2314   {
2315     GST_PLAY_BIN_UNLOCK (playbin);
2316     GST_WARNING_OBJECT (playbin,
2317         "can't switch text, the stream combiner's sink pads don't have the \"active-pad\" property");
2318     return FALSE;
2319   }
2320 no_channels:
2321   {
2322     GST_PLAY_BIN_UNLOCK (playbin);
2323     return FALSE;
2324   }
2325 }
2326 
2327 static void
gst_play_bin_set_sink(GstPlayBin * playbin,GstPlaySinkType type,const gchar * dbg,GstElement ** elem,GstElement * sink)2328 gst_play_bin_set_sink (GstPlayBin * playbin, GstPlaySinkType type,
2329     const gchar * dbg, GstElement ** elem, GstElement * sink)
2330 {
2331   GST_INFO_OBJECT (playbin, "Setting %s sink to %" GST_PTR_FORMAT, dbg, sink);
2332 
2333   gst_play_sink_set_sink (playbin->playsink, type, sink);
2334 
2335   if (*elem)
2336     gst_object_unref (*elem);
2337   *elem = sink ? gst_object_ref (sink) : NULL;
2338 }
2339 
2340 static void
gst_play_bin_set_stream_combiner(GstPlayBin * playbin,GstElement ** elem,const gchar * dbg,GstElement * combiner)2341 gst_play_bin_set_stream_combiner (GstPlayBin * playbin, GstElement ** elem,
2342     const gchar * dbg, GstElement * combiner)
2343 {
2344   GST_INFO_OBJECT (playbin, "Setting %s stream combiner to %" GST_PTR_FORMAT,
2345       dbg, combiner);
2346 
2347   GST_PLAY_BIN_LOCK (playbin);
2348   if (*elem != combiner) {
2349     GstElement *old;
2350 
2351     old = *elem;
2352     if (combiner)
2353       gst_object_ref_sink (combiner);
2354 
2355     *elem = combiner;
2356     if (old)
2357       gst_object_unref (old);
2358   }
2359   GST_LOG_OBJECT (playbin, "%s stream combiner now %" GST_PTR_FORMAT, dbg,
2360       *elem);
2361   GST_PLAY_BIN_UNLOCK (playbin);
2362 }
2363 
2364 static void
gst_play_bin_set_encoding(GstPlayBin * playbin,const gchar * encoding)2365 gst_play_bin_set_encoding (GstPlayBin * playbin, const gchar * encoding)
2366 {
2367   GstElement *elem;
2368 
2369   GST_PLAY_BIN_LOCK (playbin);
2370 
2371   /* set subtitles on all current and next decodebins. */
2372   if ((elem = playbin->groups[0].uridecodebin))
2373     g_object_set (G_OBJECT (elem), "subtitle-encoding", encoding, NULL);
2374   if ((elem = playbin->groups[0].suburidecodebin))
2375     g_object_set (G_OBJECT (elem), "subtitle-encoding", encoding, NULL);
2376   if ((elem = playbin->groups[1].uridecodebin))
2377     g_object_set (G_OBJECT (elem), "subtitle-encoding", encoding, NULL);
2378   if ((elem = playbin->groups[1].suburidecodebin))
2379     g_object_set (G_OBJECT (elem), "subtitle-encoding", encoding, NULL);
2380 
2381   gst_play_sink_set_subtitle_encoding (playbin->playsink, encoding);
2382   GST_PLAY_BIN_UNLOCK (playbin);
2383 }
2384 
2385 static void
gst_play_bin_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)2386 gst_play_bin_set_property (GObject * object, guint prop_id,
2387     const GValue * value, GParamSpec * pspec)
2388 {
2389   GstPlayBin *playbin = GST_PLAY_BIN (object);
2390 
2391   switch (prop_id) {
2392     case PROP_URI:
2393       gst_play_bin_set_uri (playbin, g_value_get_string (value));
2394       break;
2395     case PROP_SUBURI:
2396       gst_play_bin_set_suburi (playbin, g_value_get_string (value));
2397       break;
2398     case PROP_FLAGS:
2399       gst_play_bin_set_flags (playbin, g_value_get_flags (value));
2400       if (playbin->curr_group) {
2401         GST_SOURCE_GROUP_LOCK (playbin->curr_group);
2402         if (playbin->curr_group->uridecodebin) {
2403           g_object_set (playbin->curr_group->uridecodebin, "download",
2404               (g_value_get_flags (value) & GST_PLAY_FLAG_DOWNLOAD) != 0, NULL);
2405         }
2406         GST_SOURCE_GROUP_UNLOCK (playbin->curr_group);
2407       }
2408       break;
2409     case PROP_CURRENT_VIDEO:
2410       gst_play_bin_set_current_video_stream (playbin, g_value_get_int (value));
2411       break;
2412     case PROP_CURRENT_AUDIO:
2413       gst_play_bin_set_current_audio_stream (playbin, g_value_get_int (value));
2414       break;
2415     case PROP_CURRENT_TEXT:
2416       gst_play_bin_set_current_text_stream (playbin, g_value_get_int (value));
2417       break;
2418     case PROP_SUBTITLE_ENCODING:
2419       gst_play_bin_set_encoding (playbin, g_value_get_string (value));
2420       break;
2421     case PROP_VIDEO_FILTER:
2422       gst_play_sink_set_filter (playbin->playsink, GST_PLAY_SINK_TYPE_VIDEO,
2423           GST_ELEMENT (g_value_get_object (value)));
2424       break;
2425     case PROP_AUDIO_FILTER:
2426       gst_play_sink_set_filter (playbin->playsink, GST_PLAY_SINK_TYPE_AUDIO,
2427           GST_ELEMENT (g_value_get_object (value)));
2428       break;
2429     case PROP_VIDEO_SINK:
2430       gst_play_bin_set_sink (playbin, GST_PLAY_SINK_TYPE_VIDEO, "video",
2431           &playbin->video_sink, g_value_get_object (value));
2432       break;
2433     case PROP_AUDIO_SINK:
2434       gst_play_bin_set_sink (playbin, GST_PLAY_SINK_TYPE_AUDIO, "audio",
2435           &playbin->audio_sink, g_value_get_object (value));
2436       break;
2437     case PROP_VIS_PLUGIN:
2438       gst_play_sink_set_vis_plugin (playbin->playsink,
2439           g_value_get_object (value));
2440       break;
2441     case PROP_TEXT_SINK:
2442       gst_play_bin_set_sink (playbin, GST_PLAY_SINK_TYPE_TEXT, "text",
2443           &playbin->text_sink, g_value_get_object (value));
2444       break;
2445     case PROP_VIDEO_STREAM_COMBINER:
2446       gst_play_bin_set_stream_combiner (playbin,
2447           &playbin->video_stream_combiner, "video", g_value_get_object (value));
2448       break;
2449     case PROP_AUDIO_STREAM_COMBINER:
2450       gst_play_bin_set_stream_combiner (playbin,
2451           &playbin->audio_stream_combiner, "audio", g_value_get_object (value));
2452       break;
2453     case PROP_TEXT_STREAM_COMBINER:
2454       gst_play_bin_set_stream_combiner (playbin,
2455           &playbin->text_stream_combiner, "text", g_value_get_object (value));
2456       break;
2457     case PROP_VOLUME:
2458       gst_play_sink_set_volume (playbin->playsink, g_value_get_double (value));
2459       break;
2460     case PROP_MUTE:
2461       gst_play_sink_set_mute (playbin->playsink, g_value_get_boolean (value));
2462       break;
2463     case PROP_FONT_DESC:
2464       gst_play_sink_set_font_desc (playbin->playsink,
2465           g_value_get_string (value));
2466       break;
2467     case PROP_CONNECTION_SPEED:
2468       GST_PLAY_BIN_LOCK (playbin);
2469       playbin->connection_speed = g_value_get_uint64 (value) * 1000;
2470       GST_PLAY_BIN_UNLOCK (playbin);
2471       break;
2472     case PROP_BUFFER_SIZE:
2473       playbin->buffer_size = g_value_get_int (value);
2474       break;
2475     case PROP_BUFFER_DURATION:
2476       playbin->buffer_duration = g_value_get_int64 (value);
2477       break;
2478     case PROP_AV_OFFSET:
2479       gst_play_sink_set_av_offset (playbin->playsink,
2480           g_value_get_int64 (value));
2481       break;
2482     case PROP_TEXT_OFFSET:
2483       gst_play_sink_set_text_offset (playbin->playsink,
2484           g_value_get_int64 (value));
2485       break;
2486     case PROP_RING_BUFFER_MAX_SIZE:
2487       playbin->ring_buffer_max_size = g_value_get_uint64 (value);
2488       if (playbin->curr_group) {
2489         GST_SOURCE_GROUP_LOCK (playbin->curr_group);
2490         if (playbin->curr_group->uridecodebin) {
2491           g_object_set (playbin->curr_group->uridecodebin,
2492               "ring-buffer-max-size", playbin->ring_buffer_max_size, NULL);
2493         }
2494         GST_SOURCE_GROUP_UNLOCK (playbin->curr_group);
2495       }
2496       break;
2497     case PROP_FORCE_ASPECT_RATIO:
2498       g_object_set (playbin->playsink, "force-aspect-ratio",
2499           g_value_get_boolean (value), NULL);
2500       break;
2501     case PROP_MULTIVIEW_MODE:
2502       GST_PLAY_BIN_LOCK (playbin);
2503       playbin->multiview_mode = g_value_get_enum (value);
2504       GST_PLAY_BIN_UNLOCK (playbin);
2505       break;
2506     case PROP_MULTIVIEW_FLAGS:
2507       GST_PLAY_BIN_LOCK (playbin);
2508       playbin->multiview_flags = g_value_get_flags (value);
2509       GST_PLAY_BIN_UNLOCK (playbin);
2510       break;
2511     default:
2512       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2513       break;
2514   }
2515 }
2516 
2517 static GstElement *
gst_play_bin_get_current_sink(GstPlayBin * playbin,GstElement ** elem,const gchar * dbg,GstPlaySinkType type)2518 gst_play_bin_get_current_sink (GstPlayBin * playbin, GstElement ** elem,
2519     const gchar * dbg, GstPlaySinkType type)
2520 {
2521   GstElement *sink = gst_play_sink_get_sink (playbin->playsink, type);
2522 
2523   GST_LOG_OBJECT (playbin, "play_sink_get_sink() returned %s sink %"
2524       GST_PTR_FORMAT ", the originally set %s sink is %" GST_PTR_FORMAT,
2525       dbg, sink, dbg, *elem);
2526 
2527   if (sink == NULL) {
2528     GST_PLAY_BIN_LOCK (playbin);
2529     if ((sink = *elem))
2530       gst_object_ref (sink);
2531     GST_PLAY_BIN_UNLOCK (playbin);
2532   }
2533 
2534   return sink;
2535 }
2536 
2537 static GstElement *
gst_play_bin_get_current_stream_combiner(GstPlayBin * playbin,GstElement ** elem,const gchar * dbg,int stream_type)2538 gst_play_bin_get_current_stream_combiner (GstPlayBin * playbin,
2539     GstElement ** elem, const gchar * dbg, int stream_type)
2540 {
2541   GstElement *combiner;
2542 
2543   GST_PLAY_BIN_LOCK (playbin);
2544   if ((combiner = playbin->curr_group->combiner[stream_type].combiner))
2545     gst_object_ref (combiner);
2546   else if ((combiner = *elem))
2547     gst_object_ref (combiner);
2548   GST_PLAY_BIN_UNLOCK (playbin);
2549 
2550   return combiner;
2551 }
2552 
2553 static void
gst_play_bin_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)2554 gst_play_bin_get_property (GObject * object, guint prop_id, GValue * value,
2555     GParamSpec * pspec)
2556 {
2557   GstPlayBin *playbin = GST_PLAY_BIN (object);
2558 
2559   switch (prop_id) {
2560     case PROP_URI:
2561     {
2562       GstSourceGroup *group;
2563 
2564       GST_PLAY_BIN_LOCK (playbin);
2565       group = playbin->next_group;
2566       g_value_set_string (value, group->uri);
2567       GST_PLAY_BIN_UNLOCK (playbin);
2568       break;
2569     }
2570     case PROP_CURRENT_URI:
2571     {
2572       GstSourceGroup *group;
2573 
2574       GST_PLAY_BIN_LOCK (playbin);
2575       group = get_group (playbin);
2576       g_value_set_string (value, group->uri);
2577       GST_PLAY_BIN_UNLOCK (playbin);
2578       break;
2579     }
2580     case PROP_SUBURI:
2581     {
2582       GstSourceGroup *group;
2583 
2584       GST_PLAY_BIN_LOCK (playbin);
2585       group = playbin->next_group;
2586       g_value_set_string (value, group->suburi);
2587       GST_PLAY_BIN_UNLOCK (playbin);
2588       break;
2589     }
2590     case PROP_CURRENT_SUBURI:
2591     {
2592       GstSourceGroup *group;
2593 
2594       GST_PLAY_BIN_LOCK (playbin);
2595       group = get_group (playbin);
2596       g_value_set_string (value, group->suburi);
2597       GST_PLAY_BIN_UNLOCK (playbin);
2598       break;
2599     }
2600     case PROP_SOURCE:
2601     {
2602       GST_OBJECT_LOCK (playbin);
2603       g_value_set_object (value, playbin->source);
2604       GST_OBJECT_UNLOCK (playbin);
2605       break;
2606     }
2607     case PROP_FLAGS:
2608       g_value_set_flags (value, gst_play_bin_get_flags (playbin));
2609       break;
2610     case PROP_N_VIDEO:
2611     {
2612       GstSourceGroup *group;
2613       gint n_video;
2614 
2615       GST_PLAY_BIN_LOCK (playbin);
2616       group = get_group (playbin);
2617       n_video = (group->video_channels ? group->video_channels->len : 0);
2618       g_value_set_int (value, n_video);
2619       GST_PLAY_BIN_UNLOCK (playbin);
2620       break;
2621     }
2622     case PROP_CURRENT_VIDEO:
2623       GST_PLAY_BIN_LOCK (playbin);
2624       g_value_set_int (value, playbin->current_video);
2625       GST_PLAY_BIN_UNLOCK (playbin);
2626       break;
2627     case PROP_N_AUDIO:
2628     {
2629       GstSourceGroup *group;
2630       gint n_audio;
2631 
2632       GST_PLAY_BIN_LOCK (playbin);
2633       group = get_group (playbin);
2634       n_audio = (group->audio_channels ? group->audio_channels->len : 0);
2635       g_value_set_int (value, n_audio);
2636       GST_PLAY_BIN_UNLOCK (playbin);
2637       break;
2638     }
2639     case PROP_CURRENT_AUDIO:
2640       GST_PLAY_BIN_LOCK (playbin);
2641       g_value_set_int (value, playbin->current_audio);
2642       GST_PLAY_BIN_UNLOCK (playbin);
2643       break;
2644     case PROP_N_TEXT:
2645     {
2646       GstSourceGroup *group;
2647       gint n_text;
2648 
2649       GST_PLAY_BIN_LOCK (playbin);
2650       group = get_group (playbin);
2651       n_text = (group->text_channels ? group->text_channels->len : 0);
2652       g_value_set_int (value, n_text);
2653       GST_PLAY_BIN_UNLOCK (playbin);
2654       break;
2655     }
2656     case PROP_CURRENT_TEXT:
2657       GST_PLAY_BIN_LOCK (playbin);
2658       g_value_set_int (value, playbin->current_text);
2659       GST_PLAY_BIN_UNLOCK (playbin);
2660       break;
2661     case PROP_SUBTITLE_ENCODING:
2662       GST_PLAY_BIN_LOCK (playbin);
2663       g_value_take_string (value,
2664           gst_play_sink_get_subtitle_encoding (playbin->playsink));
2665       GST_PLAY_BIN_UNLOCK (playbin);
2666       break;
2667     case PROP_VIDEO_FILTER:
2668       g_value_take_object (value,
2669           gst_play_sink_get_filter (playbin->playsink,
2670               GST_PLAY_SINK_TYPE_VIDEO));
2671       break;
2672     case PROP_AUDIO_FILTER:
2673       g_value_take_object (value,
2674           gst_play_sink_get_filter (playbin->playsink,
2675               GST_PLAY_SINK_TYPE_AUDIO));
2676       break;
2677     case PROP_VIDEO_SINK:
2678       g_value_take_object (value,
2679           gst_play_bin_get_current_sink (playbin, &playbin->video_sink,
2680               "video", GST_PLAY_SINK_TYPE_VIDEO));
2681       break;
2682     case PROP_AUDIO_SINK:
2683       g_value_take_object (value,
2684           gst_play_bin_get_current_sink (playbin, &playbin->audio_sink,
2685               "audio", GST_PLAY_SINK_TYPE_AUDIO));
2686       break;
2687     case PROP_VIS_PLUGIN:
2688       g_value_take_object (value,
2689           gst_play_sink_get_vis_plugin (playbin->playsink));
2690       break;
2691     case PROP_TEXT_SINK:
2692       g_value_take_object (value,
2693           gst_play_bin_get_current_sink (playbin, &playbin->text_sink,
2694               "text", GST_PLAY_SINK_TYPE_TEXT));
2695       break;
2696     case PROP_VIDEO_STREAM_COMBINER:
2697       g_value_take_object (value,
2698           gst_play_bin_get_current_stream_combiner (playbin,
2699               &playbin->video_stream_combiner, "video", PLAYBIN_STREAM_VIDEO));
2700       break;
2701     case PROP_AUDIO_STREAM_COMBINER:
2702       g_value_take_object (value,
2703           gst_play_bin_get_current_stream_combiner (playbin,
2704               &playbin->audio_stream_combiner, "audio", PLAYBIN_STREAM_AUDIO));
2705       break;
2706     case PROP_TEXT_STREAM_COMBINER:
2707       g_value_take_object (value,
2708           gst_play_bin_get_current_stream_combiner (playbin,
2709               &playbin->text_stream_combiner, "text", PLAYBIN_STREAM_TEXT));
2710       break;
2711     case PROP_VOLUME:
2712       g_value_set_double (value, gst_play_sink_get_volume (playbin->playsink));
2713       break;
2714     case PROP_MUTE:
2715       g_value_set_boolean (value, gst_play_sink_get_mute (playbin->playsink));
2716       break;
2717     case PROP_SAMPLE:
2718       gst_value_take_sample (value,
2719           gst_play_sink_get_last_sample (playbin->playsink));
2720       break;
2721     case PROP_FONT_DESC:
2722       g_value_take_string (value,
2723           gst_play_sink_get_font_desc (playbin->playsink));
2724       break;
2725     case PROP_CONNECTION_SPEED:
2726       GST_PLAY_BIN_LOCK (playbin);
2727       g_value_set_uint64 (value, playbin->connection_speed / 1000);
2728       GST_PLAY_BIN_UNLOCK (playbin);
2729       break;
2730     case PROP_BUFFER_SIZE:
2731       GST_OBJECT_LOCK (playbin);
2732       g_value_set_int (value, playbin->buffer_size);
2733       GST_OBJECT_UNLOCK (playbin);
2734       break;
2735     case PROP_BUFFER_DURATION:
2736       GST_OBJECT_LOCK (playbin);
2737       g_value_set_int64 (value, playbin->buffer_duration);
2738       GST_OBJECT_UNLOCK (playbin);
2739       break;
2740     case PROP_AV_OFFSET:
2741       g_value_set_int64 (value,
2742           gst_play_sink_get_av_offset (playbin->playsink));
2743       break;
2744     case PROP_TEXT_OFFSET:
2745       g_value_set_int64 (value,
2746           gst_play_sink_get_text_offset (playbin->playsink));
2747       break;
2748     case PROP_RING_BUFFER_MAX_SIZE:
2749       g_value_set_uint64 (value, playbin->ring_buffer_max_size);
2750       break;
2751     case PROP_FORCE_ASPECT_RATIO:{
2752       gboolean v;
2753 
2754       g_object_get (playbin->playsink, "force-aspect-ratio", &v, NULL);
2755       g_value_set_boolean (value, v);
2756       break;
2757     }
2758     case PROP_MULTIVIEW_MODE:
2759       GST_OBJECT_LOCK (playbin);
2760       g_value_set_enum (value, playbin->multiview_mode);
2761       GST_OBJECT_UNLOCK (playbin);
2762       break;
2763     case PROP_MULTIVIEW_FLAGS:
2764       GST_OBJECT_LOCK (playbin);
2765       g_value_set_flags (value, playbin->multiview_flags);
2766       GST_OBJECT_UNLOCK (playbin);
2767       break;
2768     default:
2769       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2770       break;
2771   }
2772 }
2773 
2774 static void
gst_play_bin_update_cached_duration_from_query(GstPlayBin * playbin,gboolean valid,GstQuery * query)2775 gst_play_bin_update_cached_duration_from_query (GstPlayBin * playbin,
2776     gboolean valid, GstQuery * query)
2777 {
2778   GstFormat fmt;
2779   gint64 duration;
2780   gint i;
2781 
2782   GST_DEBUG_OBJECT (playbin, "Updating cached duration from query");
2783   gst_query_parse_duration (query, &fmt, &duration);
2784 
2785   for (i = 0; i < G_N_ELEMENTS (playbin->duration); i++) {
2786     if (playbin->duration[i].format == 0 || fmt == playbin->duration[i].format) {
2787       playbin->duration[i].valid = valid;
2788       playbin->duration[i].format = fmt;
2789       playbin->duration[i].duration = valid ? duration : -1;
2790       break;
2791     }
2792   }
2793 }
2794 
2795 static void
gst_play_bin_update_cached_duration(GstPlayBin * playbin)2796 gst_play_bin_update_cached_duration (GstPlayBin * playbin)
2797 {
2798   const GstFormat formats[] =
2799       { GST_FORMAT_TIME, GST_FORMAT_BYTES, GST_FORMAT_DEFAULT };
2800   gboolean ret;
2801   GstQuery *query;
2802   gint i;
2803 
2804   GST_DEBUG_OBJECT (playbin, "Updating cached durations before group switch");
2805   for (i = 0; i < G_N_ELEMENTS (formats); i++) {
2806     query = gst_query_new_duration (formats[i]);
2807     ret =
2808         GST_ELEMENT_CLASS (parent_class)->query (GST_ELEMENT_CAST (playbin),
2809         query);
2810     gst_play_bin_update_cached_duration_from_query (playbin, ret, query);
2811     gst_query_unref (query);
2812   }
2813 }
2814 
2815 static gboolean
gst_play_bin_query(GstElement * element,GstQuery * query)2816 gst_play_bin_query (GstElement * element, GstQuery * query)
2817 {
2818   GstPlayBin *playbin = GST_PLAY_BIN (element);
2819   gboolean ret;
2820 
2821   /* During a group switch we shouldn't allow duration queries
2822    * because it's not clear if the old or new group's duration
2823    * is returned and if the sinks are already playing new data
2824    * or old data. See bug #585969
2825    *
2826    * While we're at it, also don't do any other queries during
2827    * a group switch or any other event that causes topology changes
2828    * by taking the playbin lock in any case.
2829    */
2830   GST_PLAY_BIN_LOCK (playbin);
2831 
2832   if (GST_QUERY_TYPE (query) == GST_QUERY_DURATION) {
2833     GstSourceGroup *group = playbin->curr_group;
2834     gboolean pending;
2835 
2836     GST_SOURCE_GROUP_LOCK (group);
2837 
2838     pending = group->pending || group->stream_changed_pending;
2839 
2840     if (pending) {
2841       GstFormat fmt;
2842       gint i;
2843 
2844       ret = FALSE;
2845       gst_query_parse_duration (query, &fmt, NULL);
2846       for (i = 0; i < G_N_ELEMENTS (playbin->duration); i++) {
2847         if (fmt == playbin->duration[i].format) {
2848           ret = playbin->duration[i].valid;
2849           gst_query_set_duration (query, fmt,
2850               (ret ? playbin->duration[i].duration : -1));
2851           break;
2852         }
2853       }
2854       /* if nothing cached yet, we might as well request duration,
2855        * such as during initial startup */
2856       if (ret) {
2857         GST_DEBUG_OBJECT (playbin,
2858             "Taking cached duration because of pending group switch: %d", ret);
2859         GST_SOURCE_GROUP_UNLOCK (group);
2860         GST_PLAY_BIN_UNLOCK (playbin);
2861         return ret;
2862       }
2863     }
2864     GST_SOURCE_GROUP_UNLOCK (group);
2865   }
2866 
2867   ret = GST_ELEMENT_CLASS (parent_class)->query (element, query);
2868 
2869   if (GST_QUERY_TYPE (query) == GST_QUERY_DURATION)
2870     gst_play_bin_update_cached_duration_from_query (playbin, ret, query);
2871   GST_PLAY_BIN_UNLOCK (playbin);
2872 
2873   return ret;
2874 }
2875 
2876 /* mime types we are not handling on purpose right now, don't post a
2877  * missing-plugin message for these */
2878 static const gchar *blacklisted_mimes[] = {
2879   NULL
2880 };
2881 
2882 static void
gst_play_bin_handle_message(GstBin * bin,GstMessage * msg)2883 gst_play_bin_handle_message (GstBin * bin, GstMessage * msg)
2884 {
2885   GstPlayBin *playbin = GST_PLAY_BIN (bin);
2886   GstSourceGroup *group;
2887 
2888   if (gst_is_missing_plugin_message (msg)) {
2889     gchar *detail;
2890     guint i;
2891 
2892     detail = gst_missing_plugin_message_get_installer_detail (msg);
2893     for (i = 0; detail != NULL && blacklisted_mimes[i] != NULL; ++i) {
2894       if (strstr (detail, "|decoder-") && strstr (detail, blacklisted_mimes[i])) {
2895         GST_LOG_OBJECT (bin, "suppressing message %" GST_PTR_FORMAT, msg);
2896         gst_message_unref (msg);
2897         g_free (detail);
2898         return;
2899       }
2900     }
2901     g_free (detail);
2902   } else if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ASYNC_START ||
2903       GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ASYNC_DONE) {
2904     GstObject *src = GST_OBJECT_CAST (msg->src);
2905 
2906     /* Ignore async state changes from the uridecodebin children,
2907      * see bug #602000. */
2908     group = playbin->curr_group;
2909     if (src && group &&
2910         ((group->uridecodebin && src == GST_OBJECT_CAST (group->uridecodebin))
2911             || (group->suburidecodebin
2912                 && src == GST_OBJECT_CAST (group->suburidecodebin)))) {
2913       GST_DEBUG_OBJECT (playbin,
2914           "Ignoring async state change of uridecodebin: %s",
2915           GST_OBJECT_NAME (src));
2916       gst_message_unref (msg);
2917       msg = NULL;
2918     }
2919   } else if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_STREAM_START) {
2920     GstSourceGroup *new_group = playbin->curr_group;
2921     GstMessage *buffering_msg = NULL;
2922 
2923     GST_SOURCE_GROUP_LOCK (new_group);
2924     new_group->stream_changed_pending = FALSE;
2925     if (new_group->pending_buffering_msg) {
2926       buffering_msg = new_group->pending_buffering_msg;
2927       new_group->pending_buffering_msg = NULL;
2928     }
2929     GST_SOURCE_GROUP_UNLOCK (new_group);
2930 
2931     GST_DEBUG_OBJECT (playbin, "Stream start from new group %p", new_group);
2932 
2933     if (buffering_msg) {
2934       GST_DEBUG_OBJECT (playbin, "Posting pending buffering message: %"
2935           GST_PTR_FORMAT, buffering_msg);
2936       GST_BIN_CLASS (parent_class)->handle_message (bin, buffering_msg);
2937     }
2938 
2939   } else if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_BUFFERING) {
2940     GstSourceGroup *group = playbin->curr_group;
2941     gboolean pending;
2942 
2943     /* drop buffering messages from child queues while we are switching
2944      * groups (because the application set a new uri in about-to-finish)
2945      * if the playsink queue still has buffers to play */
2946 
2947     GST_SOURCE_GROUP_LOCK (group);
2948     pending = group->stream_changed_pending;
2949 
2950     if (pending) {
2951       GST_DEBUG_OBJECT (playbin, "Storing buffering message from pending group "
2952           "%p %" GST_PTR_FORMAT, group, msg);
2953       gst_message_replace (&group->pending_buffering_msg, msg);
2954       gst_message_unref (msg);
2955       msg = NULL;
2956     }
2957     GST_SOURCE_GROUP_UNLOCK (group);
2958   } else if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
2959     /* If we get an error of the subtitle uridecodebin transform
2960      * them into warnings and disable the subtitles */
2961     group = playbin->curr_group;
2962     if (group && group->suburidecodebin) {
2963       if (G_UNLIKELY (gst_object_has_as_ancestor (msg->src, GST_OBJECT_CAST
2964                   (group->suburidecodebin)))) {
2965         GError *err;
2966         gchar *debug = NULL;
2967         GstMessage *new_msg;
2968         GstIterator *it;
2969         gboolean done = FALSE;
2970         GValue item = { 0, };
2971 
2972         gst_message_parse_error (msg, &err, &debug);
2973         new_msg = gst_message_new_warning (msg->src, err, debug);
2974 
2975         gst_message_unref (msg);
2976         g_error_free (err);
2977         g_free (debug);
2978         msg = new_msg;
2979 
2980         REMOVE_SIGNAL (group->suburidecodebin, group->sub_pad_added_id);
2981         REMOVE_SIGNAL (group->suburidecodebin, group->sub_pad_removed_id);
2982         REMOVE_SIGNAL (group->suburidecodebin, group->sub_no_more_pads_id);
2983         REMOVE_SIGNAL (group->suburidecodebin, group->sub_autoplug_continue_id);
2984         REMOVE_SIGNAL (group->suburidecodebin, group->sub_autoplug_query_id);
2985 
2986         it = gst_element_iterate_src_pads (group->suburidecodebin);
2987         while (it && !done) {
2988           GstPad *p = NULL;
2989           GstIteratorResult res;
2990 
2991           res = gst_iterator_next (it, &item);
2992 
2993           switch (res) {
2994             case GST_ITERATOR_DONE:
2995               done = TRUE;
2996               break;
2997             case GST_ITERATOR_OK:
2998               p = g_value_get_object (&item);
2999               pad_removed_cb (NULL, p, group);
3000               g_value_reset (&item);
3001               break;
3002 
3003             case GST_ITERATOR_RESYNC:
3004               gst_iterator_resync (it);
3005               break;
3006             case GST_ITERATOR_ERROR:
3007               done = TRUE;
3008               break;
3009           }
3010         }
3011         g_value_unset (&item);
3012         if (it)
3013           gst_iterator_free (it);
3014 
3015         gst_object_ref (group->suburidecodebin);
3016         gst_bin_remove (bin, group->suburidecodebin);
3017         gst_element_set_locked_state (group->suburidecodebin, FALSE);
3018         gst_object_unref (group->suburidecodebin);
3019 
3020         GST_SOURCE_GROUP_LOCK (group);
3021         g_free (group->suburi);
3022         group->suburi = NULL;
3023         GST_SOURCE_GROUP_UNLOCK (group);
3024 
3025         if (group->sub_pending) {
3026           group->sub_pending = FALSE;
3027           no_more_pads_cb (NULL, group);
3028         }
3029       }
3030     }
3031   }
3032 
3033   if (msg)
3034     GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
3035 }
3036 
3037 static void
gst_play_bin_deep_element_added(GstBin * playbin,GstBin * sub_bin,GstElement * child)3038 gst_play_bin_deep_element_added (GstBin * playbin, GstBin * sub_bin,
3039     GstElement * child)
3040 {
3041   GST_LOG_OBJECT (playbin, "element %" GST_PTR_FORMAT " was added to "
3042       "%" GST_PTR_FORMAT, child, sub_bin);
3043 
3044   g_signal_emit (playbin, gst_play_bin_signals[SIGNAL_ELEMENT_SETUP], 0, child);
3045 
3046   GST_BIN_CLASS (parent_class)->deep_element_added (playbin, sub_bin, child);
3047 }
3048 
3049 static void
combiner_active_pad_changed(GObject * combiner,GParamSpec * pspec,GstPlayBin * playbin)3050 combiner_active_pad_changed (GObject * combiner, GParamSpec * pspec,
3051     GstPlayBin * playbin)
3052 {
3053   const gchar *property;
3054   GstSourceGroup *group;
3055   GstSourceCombine *combine = NULL;
3056   int i;
3057 
3058   GST_PLAY_BIN_LOCK (playbin);
3059   group = get_group (playbin);
3060 
3061   for (i = 0; i < PLAYBIN_STREAM_LAST; i++) {
3062     if (combiner == G_OBJECT (group->combiner[i].combiner)) {
3063       combine = &group->combiner[i];
3064     }
3065   }
3066 
3067   /* We got a pad-change after our group got switched out; no need to notify */
3068   if (!combine) {
3069     GST_PLAY_BIN_UNLOCK (playbin);
3070     return;
3071   }
3072 
3073   switch (combine->type) {
3074     case GST_PLAY_SINK_TYPE_VIDEO:
3075     case GST_PLAY_SINK_TYPE_VIDEO_RAW:
3076       property = "current-video";
3077       playbin->current_video = get_current_stream_number (playbin,
3078           combine, group->video_channels);
3079 
3080       if (playbin->video_pending_flush_finish) {
3081         playbin->video_pending_flush_finish = FALSE;
3082         GST_PLAY_BIN_UNLOCK (playbin);
3083         gst_play_bin_send_custom_event (GST_OBJECT (combiner),
3084             "playsink-custom-video-flush-finish");
3085         goto notify;
3086       }
3087       break;
3088     case GST_PLAY_SINK_TYPE_AUDIO:
3089     case GST_PLAY_SINK_TYPE_AUDIO_RAW:
3090       property = "current-audio";
3091       playbin->current_audio = get_current_stream_number (playbin,
3092           combine, group->audio_channels);
3093 
3094       if (playbin->audio_pending_flush_finish) {
3095         playbin->audio_pending_flush_finish = FALSE;
3096         GST_PLAY_BIN_UNLOCK (playbin);
3097         gst_play_bin_send_custom_event (GST_OBJECT (combiner),
3098             "playsink-custom-audio-flush-finish");
3099         goto notify;
3100       }
3101       break;
3102     case GST_PLAY_SINK_TYPE_TEXT:
3103       property = "current-text";
3104       playbin->current_text = get_current_stream_number (playbin,
3105           combine, group->text_channels);
3106 
3107       if (playbin->text_pending_flush_finish) {
3108         playbin->text_pending_flush_finish = FALSE;
3109         GST_PLAY_BIN_UNLOCK (playbin);
3110         gst_play_bin_send_custom_event (GST_OBJECT (combiner),
3111             "playsink-custom-subtitle-flush-finish");
3112         goto notify;
3113       }
3114       break;
3115     default:
3116       property = NULL;
3117   }
3118   GST_PLAY_BIN_UNLOCK (playbin);
3119 
3120 notify:
3121   if (property)
3122     g_object_notify (G_OBJECT (playbin), property);
3123 }
3124 
3125 static GstCaps *
update_video_multiview_caps(GstPlayBin * playbin,GstCaps * caps)3126 update_video_multiview_caps (GstPlayBin * playbin, GstCaps * caps)
3127 {
3128   GstVideoMultiviewMode mv_mode;
3129   GstVideoMultiviewMode cur_mv_mode;
3130   guint mv_flags, cur_mv_flags;
3131   GstStructure *s;
3132   const gchar *mview_mode_str;
3133   GstCaps *out_caps;
3134 
3135   GST_OBJECT_LOCK (playbin);
3136   mv_mode = (GstVideoMultiviewMode) playbin->multiview_mode;
3137   mv_flags = playbin->multiview_flags;
3138   GST_OBJECT_UNLOCK (playbin);
3139 
3140   if (mv_mode == GST_VIDEO_MULTIVIEW_MODE_NONE)
3141     return NULL;
3142 
3143   cur_mv_mode = GST_VIDEO_MULTIVIEW_MODE_NONE;
3144   cur_mv_flags = GST_VIDEO_MULTIVIEW_FLAGS_NONE;
3145 
3146   s = gst_caps_get_structure (caps, 0);
3147 
3148   gst_structure_get_flagset (s, "multiview-flags", &cur_mv_flags, NULL);
3149   if ((mview_mode_str = gst_structure_get_string (s, "multiview-mode")))
3150     cur_mv_mode = gst_video_multiview_mode_from_caps_string (mview_mode_str);
3151 
3152   /* We can't override an existing annotated multiview mode, except
3153    * maybe (in the future) we could change some flags. */
3154   if ((gint) cur_mv_mode > GST_VIDEO_MULTIVIEW_MAX_FRAME_PACKING) {
3155     GST_INFO_OBJECT (playbin, "Cannot override existing multiview mode");
3156     return NULL;
3157   }
3158 
3159   mview_mode_str = gst_video_multiview_mode_to_caps_string (mv_mode);
3160   g_assert (mview_mode_str != NULL);
3161   out_caps = gst_caps_copy (caps);
3162   s = gst_caps_get_structure (out_caps, 0);
3163 
3164   gst_structure_set (s, "multiview-mode", G_TYPE_STRING, mview_mode_str,
3165       "multiview-flags", GST_TYPE_VIDEO_MULTIVIEW_FLAGSET, mv_flags,
3166       GST_FLAG_SET_MASK_EXACT, NULL);
3167 
3168   return out_caps;
3169 }
3170 
3171 static GstPadProbeReturn
_uridecodebin_event_probe(GstPad * pad,GstPadProbeInfo * info,gpointer udata)3172 _uridecodebin_event_probe (GstPad * pad, GstPadProbeInfo * info, gpointer udata)
3173 {
3174   GstPadProbeReturn ret = GST_PAD_PROBE_OK;
3175   GstSourceGroup *group = udata;
3176   GstEvent *event = GST_PAD_PROBE_INFO_DATA (info);
3177   gboolean suburidecodebin = (GST_PAD_PARENT (pad) == group->suburidecodebin);
3178 
3179   if (suburidecodebin) {
3180     /* Drop flushes that we caused from the suburidecodebin */
3181     switch (GST_EVENT_TYPE (event)) {
3182       case GST_EVENT_FLUSH_START:
3183       case GST_EVENT_FLUSH_STOP:
3184       {
3185         guint32 seqnum = gst_event_get_seqnum (event);
3186         GSList *item = g_slist_find (group->suburi_flushes_to_drop,
3187             GUINT_TO_POINTER (seqnum));
3188         if (item) {
3189           if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
3190             group->suburi_flushes_to_drop =
3191                 g_slist_delete_link (group->suburi_flushes_to_drop, item);
3192           }
3193         }
3194       }
3195       default:
3196         break;
3197     }
3198   }
3199 
3200   switch (GST_EVENT_TYPE (event)) {
3201     case GST_EVENT_STREAM_START:{
3202       guint group_id;
3203 
3204       GST_SOURCE_GROUP_LOCK (group);
3205       if (gst_event_parse_group_id (event, &group_id)) {
3206         if (group->have_group_id) {
3207           if (group->group_id != group_id) {
3208             event = gst_event_copy (event);
3209             gst_event_set_group_id (event, group->group_id);
3210             gst_event_replace ((GstEvent **) & info->data, event);
3211             gst_event_unref (event);
3212           }
3213         } else {
3214           group->group_id = group_id;
3215           group->have_group_id = TRUE;
3216         }
3217       } else {
3218         GST_FIXME_OBJECT (pad,
3219             "Consider implementing group-id handling on stream-start event");
3220 
3221         if (!group->have_group_id) {
3222           group->group_id = gst_util_group_id_next ();
3223           group->have_group_id = TRUE;
3224         }
3225 
3226         event = gst_event_copy (event);
3227         gst_event_set_group_id (event, group->group_id);
3228         gst_event_replace ((GstEvent **) & info->data, event);
3229         gst_event_unref (event);
3230       }
3231       GST_SOURCE_GROUP_UNLOCK (group);
3232       break;
3233     }
3234     case GST_EVENT_CAPS:{
3235       GstCaps *caps = NULL;
3236       const GstStructure *s;
3237       const gchar *name;
3238 
3239       gst_event_parse_caps (event, &caps);
3240       /* If video caps, check if we should override multiview flags */
3241       s = gst_caps_get_structure (caps, 0);
3242       name = gst_structure_get_name (s);
3243       if (g_str_has_prefix (name, "video/")) {
3244         caps = update_video_multiview_caps (group->playbin, caps);
3245         if (caps) {
3246           gst_event_unref (event);
3247           event = gst_event_new_caps (caps);
3248           GST_PAD_PROBE_INFO_DATA (info) = event;
3249           gst_caps_unref (caps);
3250         }
3251       }
3252       break;
3253     }
3254     default:
3255       break;
3256   }
3257 
3258   return ret;
3259 }
3260 
3261 /* helper function to lookup stuff in lists */
3262 static gboolean
array_has_value(const gchar * values[],const gchar * value,gboolean exact)3263 array_has_value (const gchar * values[], const gchar * value, gboolean exact)
3264 {
3265   gint i;
3266 
3267   for (i = 0; values[i]; i++) {
3268     if (exact && !strcmp (value, values[i]))
3269       return TRUE;
3270     if (!exact && g_str_has_prefix (value, values[i]))
3271       return TRUE;
3272   }
3273   return FALSE;
3274 }
3275 
3276 typedef struct
3277 {
3278   GstPlayBin *playbin;
3279   gint stream_id;
3280   GstPlaySinkType type;
3281 } NotifyTagsData;
3282 
3283 static void
notify_tags_cb(GObject * object,GParamSpec * pspec,gpointer user_data)3284 notify_tags_cb (GObject * object, GParamSpec * pspec, gpointer user_data)
3285 {
3286   NotifyTagsData *ntdata = (NotifyTagsData *) user_data;
3287   gint signal;
3288 
3289   GST_DEBUG_OBJECT (ntdata->playbin, "Tags on pad %" GST_PTR_FORMAT
3290       " with stream id %d and type %d have changed",
3291       object, ntdata->stream_id, ntdata->type);
3292 
3293   switch (ntdata->type) {
3294     case GST_PLAY_SINK_TYPE_VIDEO:
3295     case GST_PLAY_SINK_TYPE_VIDEO_RAW:
3296       signal = SIGNAL_VIDEO_TAGS_CHANGED;
3297       break;
3298     case GST_PLAY_SINK_TYPE_AUDIO:
3299     case GST_PLAY_SINK_TYPE_AUDIO_RAW:
3300       signal = SIGNAL_AUDIO_TAGS_CHANGED;
3301       break;
3302     case GST_PLAY_SINK_TYPE_TEXT:
3303       signal = SIGNAL_TEXT_TAGS_CHANGED;
3304       break;
3305     default:
3306       signal = -1;
3307       break;
3308   }
3309 
3310   if (signal >= 0)
3311     g_signal_emit (G_OBJECT (ntdata->playbin), gst_play_bin_signals[signal], 0,
3312         ntdata->stream_id);
3313 }
3314 
3315 /* this function is called when a new pad is added to decodebin. We check the
3316  * type of the pad and add it to the combiner element of the group.
3317  */
3318 static void
pad_added_cb(GstElement * decodebin,GstPad * pad,GstSourceGroup * group)3319 pad_added_cb (GstElement * decodebin, GstPad * pad, GstSourceGroup * group)
3320 {
3321   GstPlayBin *playbin;
3322   GstCaps *caps;
3323   const GstStructure *s;
3324   const gchar *name;
3325   GstPad *sinkpad;
3326   GstPadLinkReturn res;
3327   GstSourceCombine *combine = NULL;
3328   gint i, pass;
3329   gboolean changed = FALSE;
3330   GstElement *custom_combiner = NULL;
3331   gulong group_id_probe_handler;
3332 
3333   playbin = group->playbin;
3334 
3335   GST_PLAY_BIN_SHUTDOWN_LOCK (playbin, shutdown);
3336 
3337   caps = gst_pad_get_current_caps (pad);
3338   if (!caps)
3339     caps = gst_pad_query_caps (pad, NULL);
3340   s = gst_caps_get_structure (caps, 0);
3341   name = gst_structure_get_name (s);
3342 
3343   GST_DEBUG_OBJECT (playbin,
3344       "pad %s:%s with caps %" GST_PTR_FORMAT " added in group %p",
3345       GST_DEBUG_PAD_NAME (pad), caps, group);
3346 
3347   /* major type of the pad, this determines the combiner to use,
3348      try exact match first */
3349   for (pass = 0; !combine && pass < 2; pass++) {
3350     for (i = 0; i < PLAYBIN_STREAM_LAST; i++) {
3351       if (array_has_value (group->combiner[i].media_list, name, pass == 0)) {
3352         combine = &group->combiner[i];
3353         break;
3354       } else if (group->combiner[i].get_media_caps) {
3355         GstCaps *media_caps = group->combiner[i].get_media_caps ();
3356 
3357         if (media_caps && gst_caps_can_intersect (media_caps, caps)) {
3358           combine = &group->combiner[i];
3359           gst_caps_unref (media_caps);
3360           break;
3361         }
3362         gst_caps_unref (media_caps);
3363       }
3364     }
3365     /* get custom stream combiner if there is one */
3366     if (combine) {
3367       if (i == PLAYBIN_STREAM_AUDIO) {
3368         custom_combiner = playbin->audio_stream_combiner;
3369       } else if (i == PLAYBIN_STREAM_TEXT) {
3370         custom_combiner = playbin->text_stream_combiner;
3371       } else if (i == PLAYBIN_STREAM_VIDEO) {
3372         custom_combiner = playbin->video_stream_combiner;
3373       }
3374     }
3375   }
3376   /* no combiner found for the media type, don't bother linking it to a
3377    * combiner. This will leave the pad unlinked and thus ignored. */
3378   if (combine == NULL) {
3379     GST_PLAY_BIN_SHUTDOWN_UNLOCK (playbin);
3380     goto unknown_type;
3381   }
3382 
3383   GST_SOURCE_GROUP_LOCK (group);
3384   if (combine->combiner == NULL && playbin->have_selector) {
3385     /* no combiner, create one */
3386     GST_DEBUG_OBJECT (playbin, "creating new input selector");
3387     if (custom_combiner)
3388       combine->combiner = custom_combiner;
3389     else
3390       combine->combiner = gst_element_factory_make ("input-selector", NULL);
3391 
3392     if (combine->combiner == NULL) {
3393       /* post the missing input-selector message only once */
3394       playbin->have_selector = FALSE;
3395       gst_element_post_message (GST_ELEMENT_CAST (playbin),
3396           gst_missing_element_message_new (GST_ELEMENT_CAST (playbin),
3397               "input-selector"));
3398       GST_ELEMENT_WARNING (playbin, CORE, MISSING_PLUGIN,
3399           (_("Missing element '%s' - check your GStreamer installation."),
3400               "input-selector"), (NULL));
3401     } else {
3402       /* find out which properties the stream combiner supports */
3403       combine->has_active_pad =
3404           g_object_class_find_property (G_OBJECT_GET_CLASS (combine->combiner),
3405           "active-pad") != NULL;
3406 
3407       if (!custom_combiner) {
3408         /* sync-mode=1, use clock */
3409         if (combine->type == GST_PLAY_SINK_TYPE_TEXT)
3410           g_object_set (combine->combiner, "sync-streams", TRUE,
3411               "sync-mode", 1, "cache-buffers", TRUE, NULL);
3412         else
3413           g_object_set (combine->combiner, "sync-streams", TRUE, NULL);
3414       }
3415 
3416       if (combine->has_active_pad)
3417         g_signal_connect (combine->combiner, "notify::active-pad",
3418             G_CALLBACK (combiner_active_pad_changed), playbin);
3419 
3420       GST_DEBUG_OBJECT (playbin, "adding new stream combiner %p",
3421           combine->combiner);
3422       gst_element_set_state (combine->combiner, GST_STATE_PAUSED);
3423       gst_bin_add (GST_BIN_CAST (playbin), combine->combiner);
3424     }
3425   }
3426 
3427   GST_PLAY_BIN_SHUTDOWN_UNLOCK (playbin);
3428 
3429   if (combine->srcpad == NULL) {
3430     if (combine->combiner) {
3431       /* save source pad of the combiner */
3432       combine->srcpad = gst_element_get_static_pad (combine->combiner, "src");
3433     } else {
3434       /* no combiner, use the pad as the source pad then */
3435       combine->srcpad = gst_object_ref (pad);
3436     }
3437 
3438     /* block the combiner srcpad. It's possible that multiple decodebins start
3439      * pushing data into the combiners before we have a chance to collect all
3440      * streams and connect the sinks, resulting in not-linked errors. After we
3441      * configured the sinks we will unblock them all. */
3442     GST_DEBUG_OBJECT (playbin, "blocking %" GST_PTR_FORMAT, combine->srcpad);
3443     combine->block_id =
3444         gst_pad_add_probe (combine->srcpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
3445         block_serialized_data_cb, NULL, NULL);
3446   }
3447 
3448   /* get sinkpad for the new stream */
3449   if (combine->combiner) {
3450     if ((sinkpad = gst_element_get_request_pad (combine->combiner, "sink_%u"))) {
3451 
3452       GST_DEBUG_OBJECT (playbin, "got pad %s:%s from combiner",
3453           GST_DEBUG_PAD_NAME (sinkpad));
3454 
3455       /* find out which properties the sink pad supports */
3456       combine->has_always_ok =
3457           g_object_class_find_property (G_OBJECT_GET_CLASS (sinkpad),
3458           "always-ok") != NULL;
3459       combine->has_tags =
3460           g_object_class_find_property (G_OBJECT_GET_CLASS (sinkpad),
3461           "tags") != NULL;
3462 
3463       /* store the combiner for the pad */
3464       g_object_set_data (G_OBJECT (sinkpad), "playbin.combine", combine);
3465 
3466       if (combine->has_tags) {
3467         gulong notify_tags_handler = 0;
3468         NotifyTagsData *ntdata;
3469 
3470         /* connect to the notify::tags signal for our
3471          * own *-tags-changed signals
3472          */
3473         ntdata = g_new0 (NotifyTagsData, 1);
3474         ntdata->playbin = playbin;
3475         ntdata->stream_id = combine->channels->len;
3476         ntdata->type = combine->type;
3477 
3478         notify_tags_handler =
3479             g_signal_connect_data (G_OBJECT (sinkpad), "notify::tags",
3480             G_CALLBACK (notify_tags_cb), ntdata, (GClosureNotify) g_free,
3481             (GConnectFlags) 0);
3482         g_object_set_data (G_OBJECT (sinkpad), "playbin.notify_tags_handler",
3483             ULONG_TO_POINTER (notify_tags_handler));
3484       }
3485 
3486       /* store the pad in the array */
3487       GST_DEBUG_OBJECT (playbin, "pad %p added to array", sinkpad);
3488       g_ptr_array_add (combine->channels, sinkpad);
3489 
3490       res = gst_pad_link (pad, sinkpad);
3491       if (GST_PAD_LINK_FAILED (res))
3492         goto link_failed;
3493 
3494       /* store combiner pad so we can release it */
3495       g_object_set_data (G_OBJECT (pad), "playbin.sinkpad", sinkpad);
3496 
3497       changed = TRUE;
3498       GST_DEBUG_OBJECT (playbin, "linked pad %s:%s to combiner %p",
3499           GST_DEBUG_PAD_NAME (pad), combine->combiner);
3500     } else {
3501       goto request_pad_failed;
3502     }
3503   } else {
3504     /* no combiner, don't configure anything, we'll link the new pad directly to
3505      * the sink. */
3506     changed = FALSE;
3507     sinkpad = NULL;
3508 
3509     /* store the combiner for the pad */
3510     g_object_set_data (G_OBJECT (pad), "playbin.combine", combine);
3511   }
3512   GST_SOURCE_GROUP_UNLOCK (group);
3513 
3514   group_id_probe_handler =
3515       gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
3516       _uridecodebin_event_probe, group, NULL);
3517   g_object_set_data (G_OBJECT (pad), "playbin.event_probe_id",
3518       ULONG_TO_POINTER (group_id_probe_handler));
3519 
3520   if (changed) {
3521     int signal;
3522 
3523     switch (combine->type) {
3524       case GST_PLAY_SINK_TYPE_VIDEO:
3525       case GST_PLAY_SINK_TYPE_VIDEO_RAW:
3526         signal = SIGNAL_VIDEO_CHANGED;
3527         break;
3528       case GST_PLAY_SINK_TYPE_AUDIO:
3529       case GST_PLAY_SINK_TYPE_AUDIO_RAW:
3530         signal = SIGNAL_AUDIO_CHANGED;
3531         break;
3532       case GST_PLAY_SINK_TYPE_TEXT:
3533         signal = SIGNAL_TEXT_CHANGED;
3534         break;
3535       default:
3536         signal = -1;
3537     }
3538 
3539     if (signal >= 0) {
3540       /* we want to return NOT_LINKED for unselected pads but only for pads
3541        * from the normal uridecodebin. This makes sure that subtitle streams
3542        * are not raced past audio/video from decodebin's multiqueue.
3543        * For pads from suburidecodebin OK should always be returned, otherwise
3544        * it will most likely stop. */
3545       if (combine->has_always_ok) {
3546         gboolean always_ok = (decodebin == group->suburidecodebin);
3547         g_object_set (sinkpad, "always-ok", always_ok, NULL);
3548       }
3549       g_signal_emit (G_OBJECT (playbin), gst_play_bin_signals[signal], 0, NULL);
3550     }
3551   }
3552 
3553 done:
3554   gst_caps_unref (caps);
3555   return;
3556 
3557   /* ERRORS */
3558 unknown_type:
3559   {
3560     GST_ERROR_OBJECT (playbin, "unknown type %s for pad %s:%s",
3561         name, GST_DEBUG_PAD_NAME (pad));
3562     goto done;
3563   }
3564 link_failed:
3565   {
3566     GST_ERROR_OBJECT (playbin,
3567         "failed to link pad %s:%s to combiner, reason %s (%d)",
3568         GST_DEBUG_PAD_NAME (pad), gst_pad_link_get_name (res), res);
3569     GST_SOURCE_GROUP_UNLOCK (group);
3570     goto done;
3571   }
3572 request_pad_failed:
3573   GST_ELEMENT_ERROR (playbin, CORE, PAD,
3574       ("Internal playbin error."),
3575       ("Failed to get request pad from combiner %p.", combine->combiner));
3576   GST_SOURCE_GROUP_UNLOCK (group);
3577   goto done;
3578 shutdown:
3579   {
3580     GST_DEBUG ("ignoring, we are shutting down. Pad will be left unlinked");
3581     /* not going to done as we didn't request the caps */
3582     return;
3583   }
3584 }
3585 
3586 /* called when a pad is removed from the uridecodebin. We unlink the pad from
3587  * the combiner. This will make the combiner select a new pad. */
3588 static void
pad_removed_cb(GstElement * decodebin,GstPad * pad,GstSourceGroup * group)3589 pad_removed_cb (GstElement * decodebin, GstPad * pad, GstSourceGroup * group)
3590 {
3591   GstPlayBin *playbin;
3592   GstPad *peer;
3593   GstElement *combiner;
3594   GstSourceCombine *combine;
3595   int signal = -1;
3596   gulong group_id_probe_handler;
3597 
3598   playbin = group->playbin;
3599 
3600   GST_DEBUG_OBJECT (playbin,
3601       "pad %s:%s removed from group %p", GST_DEBUG_PAD_NAME (pad), group);
3602 
3603   GST_SOURCE_GROUP_LOCK (group);
3604 
3605   if ((group_id_probe_handler =
3606           POINTER_TO_ULONG (g_object_get_data (G_OBJECT (pad),
3607                   "playbin.event_probe_id")))) {
3608     gst_pad_remove_probe (pad, group_id_probe_handler);
3609     g_object_set_data (G_OBJECT (pad), "playbin.event_probe_id", NULL);
3610   }
3611 
3612   if ((combine = g_object_get_data (G_OBJECT (pad), "playbin.combine"))) {
3613     g_assert (combine->combiner == NULL);
3614     g_assert (combine->srcpad == pad);
3615     source_combine_remove_pads (playbin, combine);
3616     goto exit;
3617   }
3618 
3619   /* get the combiner sinkpad */
3620   if (!(peer = g_object_get_data (G_OBJECT (pad), "playbin.sinkpad")))
3621     goto not_linked;
3622 
3623   /* unlink the pad now (can fail, the pad is unlinked before it's removed) */
3624   gst_pad_unlink (pad, peer);
3625 
3626   /* get combiner */
3627   combiner = GST_ELEMENT_CAST (gst_pad_get_parent (peer));
3628   g_assert (combiner != NULL);
3629 
3630   if ((combine = g_object_get_data (G_OBJECT (peer), "playbin.combine"))) {
3631     if (combine->has_tags) {
3632       gulong notify_tags_handler;
3633 
3634       notify_tags_handler =
3635           POINTER_TO_ULONG (g_object_get_data (G_OBJECT (peer),
3636               "playbin.notify_tags_handler"));
3637       if (notify_tags_handler != 0)
3638         g_signal_handler_disconnect (G_OBJECT (peer), notify_tags_handler);
3639       g_object_set_data (G_OBJECT (peer), "playbin.notify_tags_handler", NULL);
3640     }
3641 
3642     /* remove the pad from the array */
3643     g_ptr_array_remove (combine->channels, peer);
3644     GST_DEBUG_OBJECT (playbin, "pad %p removed from array", peer);
3645 
3646     /* get the correct type-changed signal */
3647     switch (combine->type) {
3648       case GST_PLAY_SINK_TYPE_VIDEO:
3649       case GST_PLAY_SINK_TYPE_VIDEO_RAW:
3650         signal = SIGNAL_VIDEO_CHANGED;
3651         break;
3652       case GST_PLAY_SINK_TYPE_AUDIO:
3653       case GST_PLAY_SINK_TYPE_AUDIO_RAW:
3654         signal = SIGNAL_AUDIO_CHANGED;
3655         break;
3656       case GST_PLAY_SINK_TYPE_TEXT:
3657         signal = SIGNAL_TEXT_CHANGED;
3658         break;
3659       default:
3660         signal = -1;
3661     }
3662 
3663     if (!combine->channels->len && combine->combiner) {
3664       GST_DEBUG_OBJECT (playbin, "all combiner sinkpads removed");
3665       GST_DEBUG_OBJECT (playbin, "removing combiner %p", combine->combiner);
3666       source_combine_remove_pads (playbin, combine);
3667       gst_element_set_state (combine->combiner, GST_STATE_NULL);
3668       gst_bin_remove (GST_BIN_CAST (playbin), combine->combiner);
3669       combine->combiner = NULL;
3670     }
3671   }
3672 
3673   /* release the pad to the combiner, this will make the combiner choose a new
3674    * pad. */
3675   gst_element_release_request_pad (combiner, peer);
3676   gst_object_unref (peer);
3677 
3678   gst_object_unref (combiner);
3679 exit:
3680   GST_SOURCE_GROUP_UNLOCK (group);
3681 
3682   if (signal >= 0)
3683     g_signal_emit (G_OBJECT (playbin), gst_play_bin_signals[signal], 0, NULL);
3684 
3685   return;
3686 
3687   /* ERRORS */
3688 not_linked:
3689   {
3690     GST_DEBUG_OBJECT (playbin, "pad not linked");
3691     goto exit;
3692   }
3693 }
3694 
3695 /* we get called when all pads are available and we must connect the sinks to
3696  * them.
3697  * The main purpose of the code is to see if we have video/audio and subtitles
3698  * and pick the right pipelines to display them.
3699  *
3700  * The combiners installed on the group tell us about the presence of
3701  * audio/video and subtitle streams. This allows us to see if we need
3702  * visualisation, video or/and audio.
3703  */
3704 static void
no_more_pads_cb(GstElement * decodebin,GstSourceGroup * group)3705 no_more_pads_cb (GstElement * decodebin, GstSourceGroup * group)
3706 {
3707   GstPlayBin *playbin;
3708   GstPadLinkReturn res;
3709   gint i;
3710   gboolean configure;
3711 
3712   playbin = group->playbin;
3713 
3714   GST_DEBUG_OBJECT (playbin, "no more pads in group %p", group);
3715 
3716   GST_PLAY_BIN_SHUTDOWN_LOCK (playbin, shutdown);
3717 
3718   GST_SOURCE_GROUP_LOCK (group);
3719   for (i = 0; i < PLAYBIN_STREAM_LAST; i++) {
3720     GstSourceCombine *combine = &group->combiner[i];
3721 
3722     /* check if the specific media type was detected and thus has a combiner
3723      * created for it. If there is the media type, get a sinkpad from the sink
3724      * and link it. We only do this if we have not yet requested the sinkpad
3725      * before. */
3726     if (combine->srcpad && combine->sinkpad == NULL) {
3727       GST_DEBUG_OBJECT (playbin, "requesting new sink pad %d", combine->type);
3728       combine->sinkpad =
3729           gst_play_sink_request_pad (playbin->playsink, combine->type);
3730       gst_object_ref (combine->sinkpad);
3731     } else if (combine->srcpad && combine->sinkpad) {
3732       GST_DEBUG_OBJECT (playbin, "refreshing new sink pad %d", combine->type);
3733       gst_play_sink_refresh_pad (playbin->playsink, combine->sinkpad,
3734           combine->type);
3735     } else if (combine->sinkpad && combine->srcpad == NULL) {
3736       GST_DEBUG_OBJECT (playbin, "releasing sink pad %d", combine->type);
3737       gst_play_sink_release_pad (playbin->playsink, combine->sinkpad);
3738       gst_object_unref (combine->sinkpad);
3739       combine->sinkpad = NULL;
3740     }
3741     if (combine->sinkpad && combine->srcpad &&
3742         !gst_pad_is_linked (combine->srcpad)) {
3743       res = gst_pad_link (combine->srcpad, combine->sinkpad);
3744       GST_DEBUG_OBJECT (playbin, "linked type %s, result: %d",
3745           combine->media_list[0], res);
3746       if (res != GST_PAD_LINK_OK) {
3747         GST_ELEMENT_ERROR (playbin, CORE, PAD,
3748             ("Internal playbin error."),
3749             ("Failed to link combiner to sink. Error %d", res));
3750       }
3751     }
3752   }
3753   GST_DEBUG_OBJECT (playbin, "pending %d > %d", group->pending,
3754       group->pending - 1);
3755 
3756   if (group->pending > 0)
3757     group->pending--;
3758 
3759   if (group->suburidecodebin == decodebin)
3760     group->sub_pending = FALSE;
3761 
3762   if (group->pending == 0) {
3763     /* we are the last group to complete, we will configure the output and then
3764      * signal the other waiters. */
3765     GST_LOG_OBJECT (playbin, "last group complete");
3766     configure = TRUE;
3767   } else {
3768     GST_LOG_OBJECT (playbin, "have more pending groups");
3769     configure = FALSE;
3770   }
3771   GST_SOURCE_GROUP_UNLOCK (group);
3772 
3773   if (configure) {
3774     /* if we have custom sinks, configure them now */
3775     GST_SOURCE_GROUP_LOCK (group);
3776 
3777     if (group->audio_sink) {
3778       GST_INFO_OBJECT (playbin, "setting custom audio sink %" GST_PTR_FORMAT,
3779           group->audio_sink);
3780       gst_play_sink_set_sink (playbin->playsink, GST_PLAY_SINK_TYPE_AUDIO,
3781           group->audio_sink);
3782     }
3783 
3784     if (group->video_sink) {
3785       GST_INFO_OBJECT (playbin, "setting custom video sink %" GST_PTR_FORMAT,
3786           group->video_sink);
3787       gst_play_sink_set_sink (playbin->playsink, GST_PLAY_SINK_TYPE_VIDEO,
3788           group->video_sink);
3789     }
3790 
3791     if (group->text_sink) {
3792       GST_INFO_OBJECT (playbin, "setting custom text sink %" GST_PTR_FORMAT,
3793           group->text_sink);
3794       gst_play_sink_set_sink (playbin->playsink, GST_PLAY_SINK_TYPE_TEXT,
3795           group->text_sink);
3796     }
3797 
3798     GST_SOURCE_GROUP_UNLOCK (group);
3799 
3800     /* signal the other decodebins that they can continue now. */
3801     GST_SOURCE_GROUP_LOCK (group);
3802     /* unblock all combiners */
3803     for (i = 0; i < PLAYBIN_STREAM_LAST; i++) {
3804       GstSourceCombine *combine = &group->combiner[i];
3805 
3806       if (combine->srcpad) {
3807         GST_DEBUG_OBJECT (playbin, "unblocking %" GST_PTR_FORMAT,
3808             combine->srcpad);
3809         if (combine->block_id) {
3810           gst_pad_remove_probe (combine->srcpad, combine->block_id);
3811           combine->block_id = 0;
3812         }
3813       }
3814     }
3815     GST_SOURCE_GROUP_UNLOCK (group);
3816     gst_play_sink_reconfigure (playbin->playsink);
3817   }
3818 
3819   GST_PLAY_BIN_SHUTDOWN_UNLOCK (playbin);
3820 
3821   if (configure) {
3822     do_async_done (playbin);
3823   }
3824 
3825   return;
3826 
3827 shutdown:
3828   {
3829     GST_DEBUG ("ignoring, we are shutting down");
3830     /* Request a flushing pad from playsink that we then link to the combiner.
3831      * Then we unblock the combiners so that they stop with a WRONG_STATE
3832      * instead of a NOT_LINKED error.
3833      */
3834     GST_SOURCE_GROUP_LOCK (group);
3835     for (i = 0; i < PLAYBIN_STREAM_LAST; i++) {
3836       GstSourceCombine *combine = &group->combiner[i];
3837 
3838       if (combine->srcpad) {
3839         if (combine->sinkpad == NULL) {
3840           GST_DEBUG_OBJECT (playbin, "requesting new flushing sink pad");
3841           combine->sinkpad =
3842               gst_play_sink_request_pad (playbin->playsink,
3843               GST_PLAY_SINK_TYPE_FLUSHING);
3844           gst_object_ref (combine->sinkpad);
3845           res = gst_pad_link (combine->srcpad, combine->sinkpad);
3846           GST_DEBUG_OBJECT (playbin, "linked flushing, result: %d", res);
3847         }
3848         GST_DEBUG_OBJECT (playbin, "unblocking %" GST_PTR_FORMAT,
3849             combine->srcpad);
3850         if (combine->block_id) {
3851           gst_pad_remove_probe (combine->srcpad, combine->block_id);
3852           combine->block_id = 0;
3853         }
3854       }
3855     }
3856     GST_SOURCE_GROUP_UNLOCK (group);
3857     return;
3858   }
3859 }
3860 
3861 static void
drained_cb(GstElement * decodebin,GstSourceGroup * group)3862 drained_cb (GstElement * decodebin, GstSourceGroup * group)
3863 {
3864   GstPlayBin *playbin;
3865 
3866   playbin = group->playbin;
3867 
3868   GST_DEBUG_OBJECT (playbin, "about to finish in group %p", group);
3869 
3870   /* after this call, we should have a next group to activate or we EOS */
3871   g_signal_emit (G_OBJECT (playbin),
3872       gst_play_bin_signals[SIGNAL_ABOUT_TO_FINISH], 0, NULL);
3873 
3874   /* now activate the next group. If the app did not set a uri, this will
3875    * fail and we can do EOS */
3876   setup_next_source (playbin, GST_STATE_PAUSED);
3877 }
3878 
3879 /* Like gst_element_factory_can_sink_any_caps() but doesn't
3880  * allow ANY caps on the sinkpad template */
3881 static gboolean
_factory_can_sink_caps(GstElementFactory * factory,GstCaps * caps)3882 _factory_can_sink_caps (GstElementFactory * factory, GstCaps * caps)
3883 {
3884   const GList *templs;
3885 
3886   templs = gst_element_factory_get_static_pad_templates (factory);
3887 
3888   while (templs) {
3889     GstStaticPadTemplate *templ = (GstStaticPadTemplate *) templs->data;
3890 
3891     if (templ->direction == GST_PAD_SINK) {
3892       GstCaps *templcaps = gst_static_caps_get (&templ->static_caps);
3893 
3894       if (!gst_caps_is_any (templcaps)
3895           && gst_caps_is_subset (caps, templcaps)) {
3896         gst_caps_unref (templcaps);
3897         return TRUE;
3898       }
3899       gst_caps_unref (templcaps);
3900     }
3901     templs = g_list_next (templs);
3902   }
3903 
3904   return FALSE;
3905 }
3906 
3907 static void
avelements_free(gpointer avelement)3908 avelements_free (gpointer avelement)
3909 {
3910   GstAVElement *elm = (GstAVElement *) avelement;
3911 
3912   if (elm->dec)
3913     gst_object_unref (elm->dec);
3914   if (elm->sink)
3915     gst_object_unref (elm->sink);
3916   g_slice_free (GstAVElement, elm);
3917 }
3918 
3919 static gint
avelement_compare_decoder(gconstpointer p1,gconstpointer p2,gpointer user_data)3920 avelement_compare_decoder (gconstpointer p1, gconstpointer p2,
3921     gpointer user_data)
3922 {
3923   GstAVElement *v1, *v2;
3924 
3925   v1 = (GstAVElement *) p1;
3926   v2 = (GstAVElement *) p2;
3927 
3928   return strcmp (GST_OBJECT_NAME (v1->dec), GST_OBJECT_NAME (v2->dec));
3929 }
3930 
3931 static gint
avelement_lookup_decoder(gconstpointer p1,gconstpointer p2,gpointer user_data)3932 avelement_lookup_decoder (gconstpointer p1, gconstpointer p2,
3933     gpointer user_data)
3934 {
3935   GstAVElement *v1;
3936   GstElementFactory *f2;
3937 
3938   v1 = (GstAVElement *) p1;
3939   f2 = (GstElementFactory *) p2;
3940 
3941   return strcmp (GST_OBJECT_NAME (v1->dec), GST_OBJECT_NAME (f2));
3942 }
3943 
3944 static gint
avelement_compare(gconstpointer p1,gconstpointer p2)3945 avelement_compare (gconstpointer p1, gconstpointer p2)
3946 {
3947   GstAVElement *v1, *v2;
3948   GstPluginFeature *fd1, *fd2, *fs1, *fs2;
3949   gint64 diff, v1_rank, v2_rank;
3950 
3951   v1 = (GstAVElement *) p1;
3952   v2 = (GstAVElement *) p2;
3953 
3954   fd1 = (GstPluginFeature *) v1->dec;
3955   fd2 = (GstPluginFeature *) v2->dec;
3956 
3957   /* If both have a sink, we also compare their ranks */
3958   if (v1->sink && v2->sink) {
3959     fs1 = (GstPluginFeature *) v1->sink;
3960     fs2 = (GstPluginFeature *) v2->sink;
3961     v1_rank =
3962         gst_plugin_feature_get_rank (fd1) * gst_plugin_feature_get_rank (fs1);
3963     v2_rank =
3964         gst_plugin_feature_get_rank (fd2) * gst_plugin_feature_get_rank (fs2);
3965   } else {
3966     v1_rank = gst_plugin_feature_get_rank (fd1);
3967     v2_rank = gst_plugin_feature_get_rank (fd2);
3968     fs1 = fs2 = NULL;
3969   }
3970 
3971   /* comparison based on the rank */
3972   diff = v2_rank - v1_rank;
3973   if (diff < 0)
3974     return -1;
3975   else if (diff > 0)
3976     return 1;
3977 
3978   /* comparison based on number of common caps features */
3979   diff = v2->n_comm_cf - v1->n_comm_cf;
3980   if (diff != 0)
3981     return diff;
3982 
3983   if (fs1 && fs2) {
3984     /* comparison based on the name of sink elements */
3985     diff = strcmp (GST_OBJECT_NAME (fs1), GST_OBJECT_NAME (fs2));
3986     if (diff != 0)
3987       return diff;
3988   }
3989 
3990   /* comparison based on the name of decoder elements */
3991   return strcmp (GST_OBJECT_NAME (fd1), GST_OBJECT_NAME (fd2));
3992 }
3993 
3994 static GSequence *
avelements_create(GstPlayBin * playbin,gboolean isaudioelement)3995 avelements_create (GstPlayBin * playbin, gboolean isaudioelement)
3996 {
3997   GstElementFactory *d_factory, *s_factory;
3998   GList *dec_list, *sink_list, *dl, *sl;
3999   GSequence *ave_seq = NULL;
4000   GstAVElement *ave;
4001   guint n_common_cf = 0;
4002 
4003   if (isaudioelement) {
4004     sink_list = gst_element_factory_list_get_elements
4005         (GST_ELEMENT_FACTORY_TYPE_SINK |
4006         GST_ELEMENT_FACTORY_TYPE_MEDIA_AUDIO, GST_RANK_MARGINAL);
4007     dec_list =
4008         gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECODER
4009         | GST_ELEMENT_FACTORY_TYPE_MEDIA_AUDIO, GST_RANK_MARGINAL);
4010   } else {
4011     sink_list = gst_element_factory_list_get_elements
4012         (GST_ELEMENT_FACTORY_TYPE_SINK |
4013         GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO |
4014         GST_ELEMENT_FACTORY_TYPE_MEDIA_IMAGE, GST_RANK_MARGINAL);
4015 
4016     dec_list =
4017         gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECODER
4018         | GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO |
4019         GST_ELEMENT_FACTORY_TYPE_MEDIA_IMAGE, GST_RANK_MARGINAL);
4020   }
4021 
4022   /* create a list of audio/video elements. Each element in the list
4023    * is holding an audio/video decoder and an audio/video sink in which
4024    * the decoders srcpad template caps and sink element's sinkpad template
4025    * caps are compatible */
4026   dl = dec_list;
4027   sl = sink_list;
4028 
4029   ave_seq = g_sequence_new ((GDestroyNotify) avelements_free);
4030 
4031   for (; dl; dl = dl->next) {
4032     d_factory = (GstElementFactory *) dl->data;
4033     for (; sl; sl = sl->next) {
4034       s_factory = (GstElementFactory *) sl->data;
4035 
4036       n_common_cf =
4037           gst_playback_utils_get_n_common_capsfeatures (d_factory, s_factory,
4038           gst_play_bin_get_flags (playbin), isaudioelement);
4039       if (n_common_cf < 1)
4040         continue;
4041 
4042       ave = g_slice_new (GstAVElement);
4043       ave->dec = gst_object_ref (d_factory);
4044       ave->sink = gst_object_ref (s_factory);
4045       ave->n_comm_cf = n_common_cf;
4046       g_sequence_append (ave_seq, ave);
4047     }
4048     sl = sink_list;
4049   }
4050   g_sequence_sort (ave_seq, (GCompareDataFunc) avelement_compare_decoder, NULL);
4051 
4052   gst_plugin_feature_list_free (dec_list);
4053   gst_plugin_feature_list_free (sink_list);
4054 
4055   return ave_seq;
4056 }
4057 
4058 static gboolean
avelement_iter_is_equal(GSequenceIter * iter,GstElementFactory * factory)4059 avelement_iter_is_equal (GSequenceIter * iter, GstElementFactory * factory)
4060 {
4061   GstAVElement *ave;
4062 
4063   if (!iter)
4064     return FALSE;
4065 
4066   ave = g_sequence_get (iter);
4067   if (!ave)
4068     return FALSE;
4069 
4070   return strcmp (GST_OBJECT_NAME (ave->dec), GST_OBJECT_NAME (factory)) == 0;
4071 }
4072 
4073 static GList *
create_decoders_list(GList * factory_list,GSequence * avelements)4074 create_decoders_list (GList * factory_list, GSequence * avelements)
4075 {
4076   GList *dec_list = NULL, *tmp;
4077   GList *ave_list = NULL;
4078   GList *ave_free_list = NULL;
4079   GstAVElement *ave, *best_ave;
4080 
4081   g_return_val_if_fail (factory_list != NULL, NULL);
4082   g_return_val_if_fail (avelements != NULL, NULL);
4083 
4084   for (tmp = factory_list; tmp; tmp = tmp->next) {
4085     GstElementFactory *factory = (GstElementFactory *) tmp->data;
4086 
4087     /* if there are parsers or sink elements, add them first */
4088     if (gst_element_factory_list_is_type (factory,
4089             GST_ELEMENT_FACTORY_TYPE_PARSER) ||
4090         gst_element_factory_list_is_type (factory,
4091             GST_ELEMENT_FACTORY_TYPE_SINK)) {
4092       dec_list = g_list_prepend (dec_list, gst_object_ref (factory));
4093     } else {
4094       GSequenceIter *seq_iter;
4095 
4096       seq_iter =
4097           g_sequence_lookup (avelements, factory,
4098           (GCompareDataFunc) avelement_lookup_decoder, NULL);
4099       if (!seq_iter) {
4100         GstAVElement *ave = g_slice_new0 (GstAVElement);
4101 
4102         ave->dec = factory;
4103         ave->sink = NULL;
4104         /* There's at least raw */
4105         ave->n_comm_cf = 1;
4106 
4107         ave_list = g_list_prepend (ave_list, ave);
4108 
4109         /* We need to free these later */
4110         ave_free_list = g_list_prepend (ave_free_list, ave);
4111         continue;
4112       }
4113 
4114       /* Go to first iter with that decoder */
4115       do {
4116         GSequenceIter *tmp_seq_iter;
4117 
4118         tmp_seq_iter = g_sequence_iter_prev (seq_iter);
4119         if (!avelement_iter_is_equal (tmp_seq_iter, factory))
4120           break;
4121         seq_iter = tmp_seq_iter;
4122       } while (!g_sequence_iter_is_begin (seq_iter));
4123 
4124       /* Get the best ranked GstAVElement for that factory */
4125       best_ave = NULL;
4126       while (!g_sequence_iter_is_end (seq_iter)
4127           && avelement_iter_is_equal (seq_iter, factory)) {
4128         ave = g_sequence_get (seq_iter);
4129 
4130         if (!best_ave || avelement_compare (ave, best_ave) < 0)
4131           best_ave = ave;
4132 
4133         seq_iter = g_sequence_iter_next (seq_iter);
4134       }
4135       ave_list = g_list_prepend (ave_list, best_ave);
4136     }
4137   }
4138 
4139   /* Sort all GstAVElements by their relative ranks and insert
4140    * into the decoders list */
4141   ave_list = g_list_sort (ave_list, (GCompareFunc) avelement_compare);
4142   for (tmp = ave_list; tmp; tmp = tmp->next) {
4143     ave = (GstAVElement *) tmp->data;
4144     dec_list = g_list_prepend (dec_list, gst_object_ref (ave->dec));
4145   }
4146   g_list_free (ave_list);
4147   gst_plugin_feature_list_free (factory_list);
4148 
4149   for (tmp = ave_free_list; tmp; tmp = tmp->next)
4150     g_slice_free (GstAVElement, tmp->data);
4151   g_list_free (ave_free_list);
4152 
4153   dec_list = g_list_reverse (dec_list);
4154 
4155   return dec_list;
4156 }
4157 
4158 /* Called when we must provide a list of factories to plug to @pad with @caps.
4159  * We first check if we have a sink that can handle the format and if we do, we
4160  * return NULL, to expose the pad. If we have no sink (or the sink does not
4161  * work), we return the list of elements that can connect. */
4162 static GValueArray *
autoplug_factories_cb(GstElement * decodebin,GstPad * pad,GstCaps * caps,GstSourceGroup * group)4163 autoplug_factories_cb (GstElement * decodebin, GstPad * pad,
4164     GstCaps * caps, GstSourceGroup * group)
4165 {
4166   GstPlayBin *playbin;
4167   GList *factory_list, *tmp;
4168   GValueArray *result;
4169   gboolean unref_caps = FALSE;
4170   gboolean isaudiodeclist = FALSE;
4171   gboolean isvideodeclist = FALSE;
4172 
4173   if (!caps) {
4174     caps = gst_caps_new_any ();
4175     unref_caps = TRUE;
4176   }
4177 
4178   playbin = group->playbin;
4179 
4180   GST_DEBUG_OBJECT (playbin, "factories group %p for %s:%s, %" GST_PTR_FORMAT,
4181       group, GST_DEBUG_PAD_NAME (pad), caps);
4182 
4183   /* filter out the elements based on the caps. */
4184   g_mutex_lock (&playbin->elements_lock);
4185   gst_play_bin_update_elements_list (playbin);
4186   factory_list =
4187       gst_element_factory_list_filter (playbin->elements, caps, GST_PAD_SINK,
4188       gst_caps_is_fixed (caps));
4189   g_mutex_unlock (&playbin->elements_lock);
4190 
4191   GST_DEBUG_OBJECT (playbin, "found factories %p", factory_list);
4192   GST_PLUGIN_FEATURE_LIST_DEBUG (factory_list);
4193 
4194   /* check whether the caps are asking for a list of audio/video decoders */
4195   tmp = factory_list;
4196   if (!gst_caps_is_any (caps)) {
4197     for (; tmp; tmp = tmp->next) {
4198       GstElementFactory *factory = (GstElementFactory *) tmp->data;
4199 
4200       isvideodeclist = gst_element_factory_list_is_type (factory,
4201           GST_ELEMENT_FACTORY_TYPE_DECODER |
4202           GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO |
4203           GST_ELEMENT_FACTORY_TYPE_MEDIA_IMAGE);
4204       isaudiodeclist = gst_element_factory_list_is_type (factory,
4205           GST_ELEMENT_FACTORY_TYPE_DECODER |
4206           GST_ELEMENT_FACTORY_TYPE_MEDIA_AUDIO);
4207 
4208       if (isaudiodeclist || isvideodeclist)
4209         break;
4210     }
4211   }
4212 
4213   if (isaudiodeclist || isvideodeclist) {
4214     GSequence **ave_list;
4215     if (isaudiodeclist)
4216       ave_list = &playbin->aelements;
4217     else
4218       ave_list = &playbin->velements;
4219 
4220     g_mutex_lock (&playbin->elements_lock);
4221     /* sort factory_list based on the GstAVElement list priority */
4222     factory_list = create_decoders_list (factory_list, *ave_list);
4223     g_mutex_unlock (&playbin->elements_lock);
4224   }
4225 
4226   /* 2 additional elements for the already set audio/video sinks */
4227   result = g_value_array_new (g_list_length (factory_list) + 2);
4228 
4229   /* Check if we already have an audio/video sink and if this is the case
4230    * put it as the first element of the array */
4231   if (group->audio_sink) {
4232     GstElementFactory *factory = gst_element_get_factory (group->audio_sink);
4233 
4234     if (factory && _factory_can_sink_caps (factory, caps)) {
4235       GValue val = { 0, };
4236 
4237       g_value_init (&val, G_TYPE_OBJECT);
4238       g_value_set_object (&val, factory);
4239       result = g_value_array_append (result, &val);
4240       g_value_unset (&val);
4241     }
4242   }
4243 
4244   if (group->video_sink) {
4245     GstElementFactory *factory = gst_element_get_factory (group->video_sink);
4246 
4247     if (factory && _factory_can_sink_caps (factory, caps)) {
4248       GValue val = { 0, };
4249 
4250       g_value_init (&val, G_TYPE_OBJECT);
4251       g_value_set_object (&val, factory);
4252       result = g_value_array_append (result, &val);
4253       g_value_unset (&val);
4254     }
4255   }
4256 
4257   for (tmp = factory_list; tmp; tmp = tmp->next) {
4258     GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
4259     GValue val = { 0, };
4260 
4261     if (group->audio_sink && gst_element_factory_list_is_type (factory,
4262             GST_ELEMENT_FACTORY_TYPE_SINK |
4263             GST_ELEMENT_FACTORY_TYPE_MEDIA_AUDIO)) {
4264       continue;
4265     }
4266     if (group->video_sink && gst_element_factory_list_is_type (factory,
4267             GST_ELEMENT_FACTORY_TYPE_SINK | GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO
4268             | GST_ELEMENT_FACTORY_TYPE_MEDIA_IMAGE)) {
4269       continue;
4270     }
4271 
4272     g_value_init (&val, G_TYPE_OBJECT);
4273     g_value_set_object (&val, factory);
4274     g_value_array_append (result, &val);
4275     g_value_unset (&val);
4276   }
4277   gst_plugin_feature_list_free (factory_list);
4278 
4279   if (unref_caps)
4280     gst_caps_unref (caps);
4281 
4282   return result;
4283 }
4284 
4285 static gboolean
gst_play_bin_send_event(GstElement * element,GstEvent * event)4286 gst_play_bin_send_event (GstElement * element, GstEvent * event)
4287 {
4288   GstPlayBin *playbin = GST_PLAY_BIN (element);
4289 
4290   /* Send event directly to playsink instead of letting GstBin iterate
4291    * over all sink elements. The latter might send the event multiple times
4292    * in case the SEEK causes a reconfiguration of the pipeline, as can easily
4293    * happen with adaptive streaming demuxers.
4294    *
4295    * What would then happen is that the iterator would be reset, we send the
4296    * event again, and on the second time it will fail in the majority of cases
4297    * because the pipeline is still being reconfigured
4298    */
4299   if (GST_EVENT_IS_UPSTREAM (event)) {
4300     return gst_element_send_event (GST_ELEMENT_CAST (playbin->playsink), event);
4301   }
4302 
4303   return GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
4304 }
4305 
4306 static void
gst_play_bin_set_context(GstElement * element,GstContext * context)4307 gst_play_bin_set_context (GstElement * element, GstContext * context)
4308 {
4309   GstPlayBin *playbin = GST_PLAY_BIN (element);
4310 
4311   /* Proxy contexts to the sinks, they might not be in playsink yet */
4312   GST_PLAY_BIN_LOCK (playbin);
4313   if (playbin->audio_sink)
4314     gst_element_set_context (playbin->audio_sink, context);
4315   if (playbin->video_sink)
4316     gst_element_set_context (playbin->video_sink, context);
4317   if (playbin->text_sink)
4318     gst_element_set_context (playbin->text_sink, context);
4319 
4320   GST_SOURCE_GROUP_LOCK (playbin->curr_group);
4321 
4322   if (playbin->curr_group->audio_sink)
4323     gst_element_set_context (playbin->curr_group->audio_sink, context);
4324   if (playbin->curr_group->video_sink)
4325     gst_element_set_context (playbin->curr_group->video_sink, context);
4326   if (playbin->curr_group->text_sink)
4327     gst_element_set_context (playbin->curr_group->text_sink, context);
4328 
4329   GST_SOURCE_GROUP_UNLOCK (playbin->curr_group);
4330   GST_PLAY_BIN_UNLOCK (playbin);
4331 
4332   GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
4333 }
4334 
4335 /* Pass sink messages to the application, e.g. NEED_CONTEXT messages */
4336 static void
gst_play_bin_update_context(GstPlayBin * playbin,GstContext * context)4337 gst_play_bin_update_context (GstPlayBin * playbin, GstContext * context)
4338 {
4339   GList *l;
4340   const gchar *context_type;
4341 
4342   GST_OBJECT_LOCK (playbin);
4343   context_type = gst_context_get_context_type (context);
4344   for (l = playbin->contexts; l; l = l->next) {
4345     GstContext *tmp = l->data;
4346     const gchar *tmp_type = gst_context_get_context_type (tmp);
4347 
4348     /* Always store newest context but never replace
4349      * a persistent one by a non-persistent one */
4350     if (strcmp (context_type, tmp_type) == 0 &&
4351         (gst_context_is_persistent (context) ||
4352             !gst_context_is_persistent (tmp))) {
4353       gst_context_replace ((GstContext **) & l->data, context);
4354       break;
4355     }
4356   }
4357   /* Not found? Add */
4358   if (l == NULL)
4359     playbin->contexts =
4360         g_list_prepend (playbin->contexts, gst_context_ref (context));
4361   GST_OBJECT_UNLOCK (playbin);
4362 }
4363 
4364 static GstBusSyncReply
activate_sink_bus_handler(GstBus * bus,GstMessage * msg,GstPlayBin * playbin)4365 activate_sink_bus_handler (GstBus * bus, GstMessage * msg, GstPlayBin * playbin)
4366 {
4367   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
4368     /* Only proxy errors from a fixed sink. If that fails we can just error out
4369      * early as stuff will fail later anyway */
4370     if (playbin->audio_sink
4371         && gst_object_has_as_ancestor (GST_MESSAGE_SRC (msg),
4372             GST_OBJECT_CAST (playbin->audio_sink)))
4373       gst_element_post_message (GST_ELEMENT_CAST (playbin), msg);
4374     else if (playbin->video_sink
4375         && gst_object_has_as_ancestor (GST_MESSAGE_SRC (msg),
4376             GST_OBJECT_CAST (playbin->video_sink)))
4377       gst_element_post_message (GST_ELEMENT_CAST (playbin), msg);
4378     else if (playbin->text_sink
4379         && gst_object_has_as_ancestor (GST_MESSAGE_SRC (msg),
4380             GST_OBJECT_CAST (playbin->text_sink)))
4381       gst_element_post_message (GST_ELEMENT_CAST (playbin), msg);
4382     else
4383       gst_message_unref (msg);
4384   } else if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_NEED_CONTEXT) {
4385     const gchar *context_type;
4386     GList *l;
4387 
4388     gst_message_parse_context_type (msg, &context_type);
4389     GST_OBJECT_LOCK (playbin);
4390     for (l = playbin->contexts; l; l = l->next) {
4391       GstContext *tmp = l->data;
4392       const gchar *tmp_type = gst_context_get_context_type (tmp);
4393 
4394       if (strcmp (context_type, tmp_type) == 0) {
4395         gst_element_set_context (GST_ELEMENT (GST_MESSAGE_SRC (msg)), l->data);
4396         break;
4397       }
4398     }
4399     GST_OBJECT_UNLOCK (playbin);
4400 
4401     /* Forward if we couldn't answer the message */
4402     if (l == NULL) {
4403       gst_element_post_message (GST_ELEMENT_CAST (playbin), msg);
4404     } else {
4405       gst_message_unref (msg);
4406     }
4407   } else if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_HAVE_CONTEXT) {
4408     GstContext *context;
4409 
4410     gst_message_parse_have_context (msg, &context);
4411     gst_play_bin_update_context (playbin, context);
4412     gst_context_unref (context);
4413 
4414     gst_element_post_message (GST_ELEMENT_CAST (playbin), msg);
4415   } else {
4416     gst_element_post_message (GST_ELEMENT_CAST (playbin), msg);
4417   }
4418 
4419   /* Doesn't really matter, nothing is using this bus */
4420   return GST_BUS_DROP;
4421 }
4422 
4423 static gboolean
activate_sink(GstPlayBin * playbin,GstElement * sink,gboolean * activated)4424 activate_sink (GstPlayBin * playbin, GstElement * sink, gboolean * activated)
4425 {
4426   GstState state;
4427   GstBus *bus = NULL;
4428   GstStateChangeReturn sret;
4429   gboolean ret = FALSE;
4430 
4431   if (activated)
4432     *activated = FALSE;
4433 
4434   GST_OBJECT_LOCK (sink);
4435   state = GST_STATE (sink);
4436   GST_OBJECT_UNLOCK (sink);
4437   if (state >= GST_STATE_READY) {
4438     ret = TRUE;
4439     goto done;
4440   }
4441 
4442   if (!GST_OBJECT_PARENT (sink)) {
4443     bus = gst_bus_new ();
4444     gst_bus_set_sync_handler (bus,
4445         (GstBusSyncHandler) activate_sink_bus_handler, playbin, NULL);
4446     gst_element_set_bus (sink, bus);
4447   }
4448 
4449   sret = gst_element_set_state (sink, GST_STATE_READY);
4450   if (sret == GST_STATE_CHANGE_FAILURE)
4451     goto done;
4452 
4453   if (activated)
4454     *activated = TRUE;
4455   ret = TRUE;
4456 
4457 done:
4458   if (bus) {
4459     gst_element_set_bus (sink, NULL);
4460     gst_object_unref (bus);
4461   }
4462 
4463   return ret;
4464 }
4465 
4466 /* autoplug-continue decides, if a pad has raw caps that can be exposed
4467  * directly or if further decoding is necessary. We use this to expose
4468  * supported subtitles directly */
4469 
4470 /* FIXME 0.11: Remove the checks for ANY caps, a sink should specify
4471  * explicitly the caps it supports and if it claims to support ANY
4472  * caps it really should support everything */
4473 static gboolean
autoplug_continue_cb(GstElement * element,GstPad * pad,GstCaps * caps,GstSourceGroup * group)4474 autoplug_continue_cb (GstElement * element, GstPad * pad, GstCaps * caps,
4475     GstSourceGroup * group)
4476 {
4477   gboolean ret = TRUE;
4478   GstPad *sinkpad = NULL;
4479   gboolean activated_sink;
4480 
4481   GST_SOURCE_GROUP_LOCK (group);
4482 
4483   if (group->text_sink &&
4484       activate_sink (group->playbin, group->text_sink, &activated_sink)) {
4485     sinkpad = gst_element_get_static_pad (group->text_sink, "sink");
4486     if (sinkpad) {
4487       GstCaps *sinkcaps;
4488 
4489       sinkcaps = gst_pad_query_caps (sinkpad, NULL);
4490       if (!gst_caps_is_any (sinkcaps))
4491         ret = !gst_caps_is_subset (caps, sinkcaps);
4492       gst_caps_unref (sinkcaps);
4493       gst_object_unref (sinkpad);
4494     }
4495     if (activated_sink)
4496       gst_element_set_state (group->text_sink, GST_STATE_NULL);
4497   } else {
4498     GstCaps *subcaps = gst_subtitle_overlay_create_factory_caps ();
4499     ret = !gst_caps_is_subset (caps, subcaps);
4500     gst_caps_unref (subcaps);
4501   }
4502   /* If autoplugging can stop don't do additional checks */
4503   if (!ret)
4504     goto done;
4505 
4506   /* If this is from the subtitle uridecodebin we don't need to
4507    * check the audio and video sink */
4508   if (group->suburidecodebin
4509       && gst_object_has_as_ancestor (GST_OBJECT_CAST (element),
4510           GST_OBJECT_CAST (group->suburidecodebin)))
4511     goto done;
4512 
4513   if (group->audio_sink &&
4514       activate_sink (group->playbin, group->audio_sink, &activated_sink)) {
4515 
4516     sinkpad = gst_element_get_static_pad (group->audio_sink, "sink");
4517     if (sinkpad) {
4518       GstCaps *sinkcaps;
4519 
4520       sinkcaps = gst_pad_query_caps (sinkpad, NULL);
4521       if (!gst_caps_is_any (sinkcaps))
4522         ret = !gst_caps_is_subset (caps, sinkcaps);
4523       gst_caps_unref (sinkcaps);
4524       gst_object_unref (sinkpad);
4525     }
4526     if (activated_sink)
4527       gst_element_set_state (group->audio_sink, GST_STATE_NULL);
4528   }
4529   if (!ret)
4530     goto done;
4531 
4532   if (group->video_sink
4533       && activate_sink (group->playbin, group->video_sink, &activated_sink)) {
4534     sinkpad = gst_element_get_static_pad (group->video_sink, "sink");
4535     if (sinkpad) {
4536       GstCaps *sinkcaps;
4537 
4538       sinkcaps = gst_pad_query_caps (sinkpad, NULL);
4539       if (!gst_caps_is_any (sinkcaps))
4540         ret = !gst_caps_is_subset (caps, sinkcaps);
4541       gst_caps_unref (sinkcaps);
4542       gst_object_unref (sinkpad);
4543     }
4544     if (activated_sink)
4545       gst_element_set_state (group->video_sink, GST_STATE_NULL);
4546   }
4547 
4548 done:
4549   GST_SOURCE_GROUP_UNLOCK (group);
4550 
4551   GST_DEBUG_OBJECT (group->playbin,
4552       "continue autoplugging group %p for %s:%s, %" GST_PTR_FORMAT ": %d",
4553       group, GST_DEBUG_PAD_NAME (pad), caps, ret);
4554 
4555   return ret;
4556 }
4557 
4558 static gboolean
sink_accepts_caps(GstPlayBin * playbin,GstElement * sink,GstCaps * caps)4559 sink_accepts_caps (GstPlayBin * playbin, GstElement * sink, GstCaps * caps)
4560 {
4561   GstPad *sinkpad;
4562   gboolean ret = TRUE;
4563 
4564   if ((sinkpad = gst_element_get_static_pad (sink, "sink"))) {
4565     GstCaps *sinkcaps;
4566 
4567     sinkcaps = gst_pad_query_caps (sinkpad, NULL);
4568     /* Got the sink pad, now let's see if the element actually does accept the
4569      * caps that we have */
4570     ret = gst_caps_is_subset (caps, sinkcaps);
4571     gst_caps_unref (sinkcaps);
4572     gst_object_unref (sinkpad);
4573   }
4574 
4575   return ret;
4576 }
4577 
4578 /* We are asked to select an element. See if the next element to check
4579  * is a sink. If this is the case, we see if the sink works by setting it to
4580  * READY. If the sink works, we return SELECT_EXPOSE to make decodebin
4581  * expose the raw pad so that we can setup the mixers. */
4582 static GstAutoplugSelectResult
autoplug_select_cb(GstElement * decodebin,GstPad * pad,GstCaps * caps,GstElementFactory * factory,GstSourceGroup * group)4583 autoplug_select_cb (GstElement * decodebin, GstPad * pad,
4584     GstCaps * caps, GstElementFactory * factory, GstSourceGroup * group)
4585 {
4586   GstPlayBin *playbin;
4587   GstElement *element;
4588   const gchar *klass;
4589   GstPlaySinkType type;
4590   GstElement **sinkp;
4591   GList *ave_list = NULL, *l;
4592   GstAVElement *ave = NULL;
4593   GSequence *ave_seq = NULL;
4594   GSequenceIter *seq_iter;
4595   gboolean created_sink = FALSE;
4596 
4597   playbin = group->playbin;
4598 
4599   GST_DEBUG_OBJECT (playbin, "select group %p for %s:%s, %" GST_PTR_FORMAT,
4600       group, GST_DEBUG_PAD_NAME (pad), caps);
4601 
4602   GST_DEBUG_OBJECT (playbin, "checking factory %s", GST_OBJECT_NAME (factory));
4603 
4604   /* if it's not a sink, we make sure the element is compatible with
4605    * the fixed sink */
4606   if (!gst_element_factory_list_is_type (factory,
4607           GST_ELEMENT_FACTORY_TYPE_SINK)) {
4608     gboolean isvideodec = gst_element_factory_list_is_type (factory,
4609         GST_ELEMENT_FACTORY_TYPE_DECODER |
4610         GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO |
4611         GST_ELEMENT_FACTORY_TYPE_MEDIA_IMAGE);
4612     gboolean isaudiodec = gst_element_factory_list_is_type (factory,
4613         GST_ELEMENT_FACTORY_TYPE_DECODER |
4614         GST_ELEMENT_FACTORY_TYPE_MEDIA_AUDIO);
4615 
4616     if (!isvideodec && !isaudiodec)
4617       return GST_AUTOPLUG_SELECT_TRY;
4618 
4619     GST_SOURCE_GROUP_LOCK (group);
4620     g_mutex_lock (&playbin->elements_lock);
4621 
4622     if (isaudiodec) {
4623       ave_seq = playbin->aelements;
4624       sinkp = &group->audio_sink;
4625     } else {
4626       ave_seq = playbin->velements;
4627       sinkp = &group->video_sink;
4628     }
4629 
4630     seq_iter =
4631         g_sequence_lookup (ave_seq, factory,
4632         (GCompareDataFunc) avelement_lookup_decoder, NULL);
4633     if (seq_iter) {
4634       /* Go to first iter with that decoder */
4635       do {
4636         GSequenceIter *tmp_seq_iter;
4637 
4638         tmp_seq_iter = g_sequence_iter_prev (seq_iter);
4639         if (!avelement_iter_is_equal (tmp_seq_iter, factory))
4640           break;
4641         seq_iter = tmp_seq_iter;
4642       } while (!g_sequence_iter_is_begin (seq_iter));
4643 
4644       while (!g_sequence_iter_is_end (seq_iter)
4645           && avelement_iter_is_equal (seq_iter, factory)) {
4646         ave = g_sequence_get (seq_iter);
4647         ave_list = g_list_prepend (ave_list, ave);
4648         seq_iter = g_sequence_iter_next (seq_iter);
4649       }
4650 
4651       /* Sort all GstAVElements by their relative ranks and insert
4652        * into the decoders list */
4653       ave_list = g_list_sort (ave_list, (GCompareFunc) avelement_compare);
4654     } else {
4655       ave_list = g_list_prepend (ave_list, NULL);
4656     }
4657 
4658     /* if it is a decoder and we don't have a fixed sink, then find out
4659      * the matching audio/video sink from GstAVElements list */
4660     for (l = ave_list; l; l = l->next) {
4661       ave = (GstAVElement *) l->data;
4662 
4663       if (((isaudiodec && !group->audio_sink) ||
4664               (isvideodec && !group->video_sink))) {
4665         if (ave && ave->sink) {
4666           GST_DEBUG_OBJECT (playbin,
4667               "Trying to create sink '%s' for decoder '%s'",
4668               gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (ave->sink)),
4669               gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
4670           if ((*sinkp = gst_element_factory_create (ave->sink, NULL)) == NULL) {
4671             GST_WARNING_OBJECT (playbin,
4672                 "Could not create an element from %s",
4673                 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (ave->sink)));
4674             continue;
4675           } else {
4676             /* The sink is ours now, don't leak the floating reference in the
4677              * state-changed messages */
4678             gst_object_ref_sink (*sinkp);
4679 
4680             if (!activate_sink (playbin, *sinkp, NULL)) {
4681               gst_object_unref (*sinkp);
4682               *sinkp = NULL;
4683               GST_WARNING_OBJECT (playbin,
4684                   "Could not activate sink %s",
4685                   gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (ave->sink)));
4686               continue;
4687             }
4688             created_sink = TRUE;
4689           }
4690         }
4691       }
4692 
4693       /* If it is a decoder and we have a fixed sink for the media
4694        * type it outputs, check that the decoder is compatible with this sink */
4695       if ((isaudiodec && group->audio_sink) || (isvideodec
4696               && group->video_sink)) {
4697         gboolean compatible = FALSE;
4698         GstPad *sinkpad;
4699         GstCaps *caps;
4700         GstElement *sink;
4701 
4702         sink = *sinkp;
4703 
4704         if ((sinkpad = gst_element_get_static_pad (sink, "sink"))) {
4705           GstPlayFlags flags = gst_play_bin_get_flags (playbin);
4706           GstCaps *raw_caps =
4707               (isaudiodec) ? gst_static_caps_get (&raw_audio_caps) :
4708               gst_static_caps_get (&raw_video_caps);
4709 
4710           caps = gst_pad_query_caps (sinkpad, NULL);
4711 
4712           /* If the sink supports raw audio/video, we first check
4713            * if the decoder could output any raw audio/video format
4714            * and assume it is compatible with the sink then. We don't
4715            * do a complete compatibility check here if converters
4716            * are plugged between the decoder and the sink because
4717            * the converters will convert between raw formats and
4718            * even if the decoder format is not supported by the decoder
4719            * a converter will convert it.
4720            *
4721            * We assume here that the converters can convert between
4722            * any raw format.
4723            */
4724           if ((isaudiodec && !(flags & GST_PLAY_FLAG_NATIVE_AUDIO)
4725                   && gst_caps_can_intersect (caps, raw_caps)) || (!isaudiodec
4726                   && !(flags & GST_PLAY_FLAG_NATIVE_VIDEO)
4727                   && gst_caps_can_intersect (caps, raw_caps))) {
4728             compatible =
4729                 gst_element_factory_can_src_any_caps (factory, raw_caps)
4730                 || gst_element_factory_can_src_any_caps (factory, caps);
4731           } else {
4732             compatible = gst_element_factory_can_src_any_caps (factory, caps);
4733           }
4734 
4735           gst_object_unref (sinkpad);
4736           gst_caps_unref (raw_caps);
4737           gst_caps_unref (caps);
4738         }
4739 
4740         if (compatible)
4741           break;
4742 
4743         GST_DEBUG_OBJECT (playbin, "%s not compatible with the fixed sink",
4744             GST_OBJECT_NAME (factory));
4745 
4746         /* If it is not compatible, either continue with the next possible
4747          * sink or if we have a fixed sink, skip the decoder */
4748         if (created_sink) {
4749           gst_element_set_state (*sinkp, GST_STATE_NULL);
4750           gst_object_unref (*sinkp);
4751           *sinkp = NULL;
4752           created_sink = FALSE;
4753         } else {
4754           g_mutex_unlock (&playbin->elements_lock);
4755           GST_SOURCE_GROUP_UNLOCK (group);
4756           return GST_AUTOPLUG_SELECT_SKIP;
4757         }
4758       }
4759     }
4760     g_list_free (ave_list);
4761     g_mutex_unlock (&playbin->elements_lock);
4762     GST_SOURCE_GROUP_UNLOCK (group);
4763     return GST_AUTOPLUG_SELECT_TRY;
4764   }
4765 
4766   /* it's a sink, see if an instance of it actually works */
4767   GST_DEBUG_OBJECT (playbin, "we found a sink '%s'", GST_OBJECT_NAME (factory));
4768 
4769   klass =
4770       gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
4771 
4772   /* figure out the klass */
4773   if (strstr (klass, "Audio")) {
4774     GST_DEBUG_OBJECT (playbin, "we found an audio sink");
4775     type = GST_PLAY_SINK_TYPE_AUDIO;
4776     sinkp = &group->audio_sink;
4777   } else if (strstr (klass, "Video")) {
4778     GST_DEBUG_OBJECT (playbin, "we found a video sink");
4779     type = GST_PLAY_SINK_TYPE_VIDEO;
4780     sinkp = &group->video_sink;
4781   } else {
4782     /* unknown klass, skip this element */
4783     GST_WARNING_OBJECT (playbin, "unknown sink klass %s found", klass);
4784     return GST_AUTOPLUG_SELECT_SKIP;
4785   }
4786 
4787   /* if we are asked to do visualisations and it's an audio sink, skip the
4788    * element. We can only do visualisations with raw sinks */
4789   if (gst_play_sink_get_flags (playbin->playsink) & GST_PLAY_FLAG_VIS) {
4790     if (type == GST_PLAY_SINK_TYPE_AUDIO) {
4791       GST_DEBUG_OBJECT (playbin, "skip audio sink because of vis");
4792       return GST_AUTOPLUG_SELECT_SKIP;
4793     }
4794   }
4795 
4796   /* now see if we already have a sink element */
4797   GST_SOURCE_GROUP_LOCK (group);
4798   if (*sinkp && GST_STATE (*sinkp) >= GST_STATE_READY) {
4799     GstElement *sink = gst_object_ref (*sinkp);
4800 
4801     if (sink_accepts_caps (playbin, sink, caps)) {
4802       GST_DEBUG_OBJECT (playbin,
4803           "Existing sink '%s' accepts caps: %" GST_PTR_FORMAT,
4804           GST_ELEMENT_NAME (sink), caps);
4805       gst_object_unref (sink);
4806       GST_SOURCE_GROUP_UNLOCK (group);
4807       return GST_AUTOPLUG_SELECT_EXPOSE;
4808     } else {
4809       GST_DEBUG_OBJECT (playbin,
4810           "Existing sink '%s' does not accept caps: %" GST_PTR_FORMAT,
4811           GST_ELEMENT_NAME (sink), caps);
4812       gst_object_unref (sink);
4813       GST_SOURCE_GROUP_UNLOCK (group);
4814       return GST_AUTOPLUG_SELECT_SKIP;
4815     }
4816   }
4817   GST_DEBUG_OBJECT (playbin, "we have no pending sink, try to create '%s'",
4818       gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
4819 
4820   if ((*sinkp = gst_element_factory_create (factory, NULL)) == NULL) {
4821     GST_WARNING_OBJECT (playbin, "Could not create an element from %s",
4822         gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
4823     GST_SOURCE_GROUP_UNLOCK (group);
4824     return GST_AUTOPLUG_SELECT_SKIP;
4825   }
4826 
4827   /* The sink is ours now, don't leak floating references in the state-changed
4828    * messages, but only do that if we didn't just create the sink above and
4829    * already ref_sink'd it there */
4830   if (!created_sink)
4831     gst_object_ref_sink (*sinkp);
4832 
4833   element = *sinkp;
4834 
4835   if (!activate_sink (playbin, element, NULL)) {
4836     GST_WARNING_OBJECT (playbin, "Could not activate sink %s",
4837         gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
4838     *sinkp = NULL;
4839     gst_object_unref (element);
4840     GST_SOURCE_GROUP_UNLOCK (group);
4841     return GST_AUTOPLUG_SELECT_SKIP;
4842   }
4843 
4844   /* Check if the selected sink actually supports the
4845    * caps and can be set to READY*/
4846   if (!sink_accepts_caps (playbin, element, caps)) {
4847     *sinkp = NULL;
4848     gst_element_set_state (element, GST_STATE_NULL);
4849     gst_object_unref (element);
4850     GST_SOURCE_GROUP_UNLOCK (group);
4851     return GST_AUTOPLUG_SELECT_SKIP;
4852   }
4853 
4854   /* remember the sink in the group now
4855    *
4856    * store the sink in the group, we will configure it later when we
4857    * reconfigure the sink */
4858   GST_DEBUG_OBJECT (playbin, "remember sink");
4859   GST_SOURCE_GROUP_UNLOCK (group);
4860 
4861   /* tell decodebin to expose the pad because we are going to use this
4862    * sink */
4863   GST_DEBUG_OBJECT (playbin, "we found a working sink, expose pad");
4864 
4865   return GST_AUTOPLUG_SELECT_EXPOSE;
4866 }
4867 
4868 #define GST_PLAY_BIN_FILTER_CAPS(filter,caps) G_STMT_START {                  \
4869   if ((filter)) {                                                             \
4870     GstCaps *intersection =                                                   \
4871         gst_caps_intersect_full ((filter), (caps), GST_CAPS_INTERSECT_FIRST); \
4872     gst_caps_unref ((caps));                                                  \
4873     (caps) = intersection;                                                    \
4874   }                                                                           \
4875 } G_STMT_END
4876 
4877 static gboolean
autoplug_query_caps(GstElement * uridecodebin,GstPad * pad,GstElement * element,GstQuery * query,GstSourceGroup * group)4878 autoplug_query_caps (GstElement * uridecodebin, GstPad * pad,
4879     GstElement * element, GstQuery * query, GstSourceGroup * group)
4880 {
4881   GstCaps *filter, *result = NULL;
4882   GstElement *sink;
4883   GstPad *sinkpad = NULL;
4884   GstElementFactory *factory;
4885   GstElementFactoryListType factory_type;
4886   gboolean have_sink = FALSE;
4887 
4888   GST_SOURCE_GROUP_LOCK (group);
4889   gst_query_parse_caps (query, &filter);
4890 
4891   factory = gst_element_get_factory (element);
4892   if (!factory)
4893     goto done;
4894 
4895   if (gst_element_factory_list_is_type (factory,
4896           GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO |
4897           GST_ELEMENT_FACTORY_TYPE_MEDIA_IMAGE)) {
4898     factory_type =
4899         GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO |
4900         GST_ELEMENT_FACTORY_TYPE_MEDIA_IMAGE;
4901 
4902     /* If this is from the subtitle uridecodebin we don't need to
4903      * check the audio and video sink */
4904     if (group->suburidecodebin
4905         && gst_object_has_as_ancestor (GST_OBJECT_CAST (pad),
4906             GST_OBJECT_CAST (group->suburidecodebin))) {
4907       goto done;
4908     }
4909 
4910     if ((sink = group->video_sink)) {
4911       sinkpad = gst_element_get_static_pad (sink, "sink");
4912       if (sinkpad) {
4913         GstCaps *sinkcaps;
4914 
4915         sinkcaps = gst_pad_query_caps (sinkpad, filter);
4916         if (!gst_caps_is_any (sinkcaps)) {
4917           if (!result)
4918             result = sinkcaps;
4919           else
4920             result = gst_caps_merge (result, sinkcaps);
4921         } else {
4922           gst_caps_unref (sinkcaps);
4923         }
4924         gst_object_unref (sinkpad);
4925       }
4926       have_sink = TRUE;
4927     }
4928   } else if (gst_element_factory_list_is_type (factory,
4929           GST_ELEMENT_FACTORY_TYPE_MEDIA_AUDIO)) {
4930     factory_type = GST_ELEMENT_FACTORY_TYPE_MEDIA_AUDIO;
4931 
4932     /* If this is from the subtitle uridecodebin we don't need to
4933      * check the audio and video sink */
4934     if (group->suburidecodebin
4935         && gst_object_has_as_ancestor (GST_OBJECT_CAST (pad),
4936             GST_OBJECT_CAST (group->suburidecodebin))) {
4937       goto done;
4938     }
4939 
4940     if ((sink = group->audio_sink)) {
4941       sinkpad = gst_element_get_static_pad (sink, "sink");
4942       if (sinkpad) {
4943         GstCaps *sinkcaps;
4944 
4945         sinkcaps = gst_pad_query_caps (sinkpad, filter);
4946         if (!gst_caps_is_any (sinkcaps)) {
4947           if (!result)
4948             result = sinkcaps;
4949           else
4950             result = gst_caps_merge (result, sinkcaps);
4951         } else {
4952           gst_caps_unref (sinkcaps);
4953         }
4954         gst_object_unref (sinkpad);
4955       }
4956       have_sink = TRUE;
4957     }
4958   } else if (gst_element_factory_list_is_type (factory,
4959           GST_ELEMENT_FACTORY_TYPE_MEDIA_SUBTITLE)) {
4960     factory_type = GST_ELEMENT_FACTORY_TYPE_MEDIA_SUBTITLE;
4961 
4962     if ((sink = group->playbin->text_sink)) {
4963       sinkpad = gst_element_get_static_pad (sink, "sink");
4964       if (sinkpad) {
4965         GstCaps *sinkcaps;
4966 
4967         sinkcaps = gst_pad_query_caps (sinkpad, filter);
4968         if (!gst_caps_is_any (sinkcaps)) {
4969           if (!result)
4970             result = sinkcaps;
4971           else
4972             result = gst_caps_merge (result, sinkcaps);
4973         } else {
4974           gst_caps_unref (sinkcaps);
4975         }
4976         gst_object_unref (sinkpad);
4977       }
4978       have_sink = TRUE;
4979     } else {
4980       GstCaps *subcaps = gst_subtitle_overlay_create_factory_caps ();
4981       GST_PLAY_BIN_FILTER_CAPS (filter, subcaps);
4982       if (!result)
4983         result = subcaps;
4984       else
4985         result = gst_caps_merge (result, subcaps);
4986     }
4987   } else {
4988     goto done;
4989   }
4990 
4991   if (!have_sink) {
4992     GValueArray *factories;
4993     gint i, n;
4994 
4995     factories = autoplug_factories_cb (uridecodebin, pad, NULL, group);
4996     n = factories->n_values;
4997     for (i = 0; i < n; i++) {
4998       GValue *v = g_value_array_get_nth (factories, i);
4999       GstElementFactory *f = g_value_get_object (v);
5000       const GList *templates;
5001       const GList *l;
5002       GstCaps *templ_caps;
5003 
5004       if (!gst_element_factory_list_is_type (f, factory_type))
5005         continue;
5006 
5007       templates = gst_element_factory_get_static_pad_templates (f);
5008 
5009       for (l = templates; l; l = l->next) {
5010         templ_caps = gst_static_pad_template_get_caps (l->data);
5011 
5012         if (!gst_caps_is_any (templ_caps)) {
5013           GST_PLAY_BIN_FILTER_CAPS (filter, templ_caps);
5014           if (!result)
5015             result = templ_caps;
5016           else
5017             result = gst_caps_merge (result, templ_caps);
5018         } else {
5019           gst_caps_unref (templ_caps);
5020         }
5021       }
5022     }
5023     g_value_array_free (factories);
5024   }
5025 
5026 done:
5027   GST_SOURCE_GROUP_UNLOCK (group);
5028 
5029   if (!result)
5030     return FALSE;
5031 
5032   /* Add the actual decoder/parser/etc caps at the very end to
5033    * make sure we don't cause empty caps to be returned, e.g.
5034    * if a parser asks us but a decoder is required after it
5035    * because no sink can handle the format directly.
5036    */
5037   {
5038     GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
5039 
5040     if (target) {
5041       GstCaps *target_caps = gst_pad_get_pad_template_caps (target);
5042 
5043       GST_PLAY_BIN_FILTER_CAPS (filter, target_caps);
5044       if (!gst_caps_is_any (target_caps)) {
5045         GstCaps *tmp;
5046         GstCapsFeatures *features;
5047         GstStructure *s;
5048         guint i, n;
5049 
5050         n = gst_caps_get_size (target_caps);
5051         tmp = gst_caps_new_empty ();
5052         for (i = 0; i < n; i++) {
5053           features = gst_caps_get_features (target_caps, i);
5054           s = gst_caps_get_structure (target_caps, i);
5055 
5056           if (!gst_structure_has_name (s, "video/x-raw") &&
5057               !gst_structure_has_name (s, "audio/x-raw")) {
5058             gst_caps_append_structure_full (tmp,
5059                 gst_structure_copy (s), gst_caps_features_copy (features));
5060           } else if (gst_caps_features_is_any (features)
5061               || gst_caps_features_is_equal (features,
5062                   GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY)) {
5063             gst_caps_append_structure (tmp, gst_structure_copy (s));
5064           }
5065         }
5066         gst_caps_unref (target_caps);
5067         target_caps = tmp;
5068       }
5069 
5070 
5071       result = gst_caps_merge (result, target_caps);
5072       gst_object_unref (target);
5073     }
5074   }
5075 
5076 
5077   gst_query_set_caps_result (query, result);
5078   gst_caps_unref (result);
5079 
5080   return TRUE;
5081 }
5082 
5083 static gboolean
autoplug_query_context(GstElement * uridecodebin,GstPad * pad,GstElement * element,GstQuery * query,GstSourceGroup * group)5084 autoplug_query_context (GstElement * uridecodebin, GstPad * pad,
5085     GstElement * element, GstQuery * query, GstSourceGroup * group)
5086 {
5087   GstElement *sink;
5088   GstPad *sinkpad = NULL;
5089   GstElementFactory *factory;
5090   gboolean res = FALSE;
5091 
5092   GST_SOURCE_GROUP_LOCK (group);
5093 
5094   factory = gst_element_get_factory (element);
5095   if (!factory)
5096     goto done;
5097 
5098   if (gst_element_factory_list_is_type (factory,
5099           GST_ELEMENT_FACTORY_TYPE_MEDIA_VIDEO |
5100           GST_ELEMENT_FACTORY_TYPE_MEDIA_IMAGE)) {
5101     /* If this is from the subtitle uridecodebin we don't need to
5102      * check the audio and video sink */
5103     if (group->suburidecodebin
5104         && gst_object_has_as_ancestor (GST_OBJECT_CAST (pad),
5105             GST_OBJECT_CAST (group->suburidecodebin))) {
5106       goto done;
5107     }
5108 
5109     if ((sink = group->video_sink)) {
5110       sinkpad = gst_element_get_static_pad (sink, "sink");
5111       if (sinkpad) {
5112         res = gst_pad_query (sinkpad, query);
5113         gst_object_unref (sinkpad);
5114       }
5115     }
5116   } else if (gst_element_factory_list_is_type (factory,
5117           GST_ELEMENT_FACTORY_TYPE_MEDIA_AUDIO)) {
5118     /* If this is from the subtitle uridecodebin we don't need to
5119      * check the audio and video sink */
5120     if (group->suburidecodebin
5121         && gst_object_has_as_ancestor (GST_OBJECT_CAST (pad),
5122             GST_OBJECT_CAST (group->suburidecodebin))) {
5123       goto done;
5124     }
5125 
5126     if ((sink = group->audio_sink)) {
5127       sinkpad = gst_element_get_static_pad (sink, "sink");
5128       if (sinkpad) {
5129         res = gst_pad_query (sinkpad, query);
5130         gst_object_unref (sinkpad);
5131       }
5132     }
5133   } else if (gst_element_factory_list_is_type (factory,
5134           GST_ELEMENT_FACTORY_TYPE_MEDIA_SUBTITLE)) {
5135     if ((sink = group->playbin->text_sink)) {
5136       sinkpad = gst_element_get_static_pad (sink, "sink");
5137       if (sinkpad) {
5138         res = gst_pad_query (sinkpad, query);
5139         gst_object_unref (sinkpad);
5140       }
5141     }
5142   } else {
5143     goto done;
5144   }
5145 
5146 done:
5147   GST_SOURCE_GROUP_UNLOCK (group);
5148 
5149   return res;
5150 }
5151 
5152 static gboolean
autoplug_query_cb(GstElement * uridecodebin,GstPad * pad,GstElement * element,GstQuery * query,GstSourceGroup * group)5153 autoplug_query_cb (GstElement * uridecodebin, GstPad * pad,
5154     GstElement * element, GstQuery * query, GstSourceGroup * group)
5155 {
5156 
5157   switch (GST_QUERY_TYPE (query)) {
5158     case GST_QUERY_CAPS:
5159       return autoplug_query_caps (uridecodebin, pad, element, query, group);
5160     case GST_QUERY_CONTEXT:
5161       return autoplug_query_context (uridecodebin, pad, element, query, group);
5162     default:
5163       return FALSE;
5164   }
5165 }
5166 
5167 static void
notify_source_cb(GstElement * uridecodebin,GParamSpec * pspec,GstSourceGroup * group)5168 notify_source_cb (GstElement * uridecodebin, GParamSpec * pspec,
5169     GstSourceGroup * group)
5170 {
5171   GstPlayBin *playbin;
5172   GstElement *source;
5173 
5174   playbin = group->playbin;
5175 
5176   g_object_get (group->uridecodebin, "source", &source, NULL);
5177 
5178   GST_OBJECT_LOCK (playbin);
5179   if (playbin->source)
5180     gst_object_unref (playbin->source);
5181   playbin->source = source;
5182   GST_OBJECT_UNLOCK (playbin);
5183 
5184   g_object_notify (G_OBJECT (playbin), "source");
5185 
5186   g_signal_emit (playbin, gst_play_bin_signals[SIGNAL_SOURCE_SETUP],
5187       0, playbin->source);
5188 }
5189 
5190 /* must be called with the group lock */
5191 static gboolean
group_set_locked_state_unlocked(GstPlayBin * playbin,GstSourceGroup * group,gboolean locked)5192 group_set_locked_state_unlocked (GstPlayBin * playbin, GstSourceGroup * group,
5193     gboolean locked)
5194 {
5195   GST_DEBUG_OBJECT (playbin, "locked_state %d on group %p", locked, group);
5196 
5197   if (group->uridecodebin)
5198     gst_element_set_locked_state (group->uridecodebin, locked);
5199   if (group->suburidecodebin)
5200     gst_element_set_locked_state (group->suburidecodebin, locked);
5201 
5202   return TRUE;
5203 }
5204 
5205 /* must be called with PLAY_BIN_LOCK */
5206 static GstStateChangeReturn
activate_group(GstPlayBin * playbin,GstSourceGroup * group,GstState target)5207 activate_group (GstPlayBin * playbin, GstSourceGroup * group, GstState target)
5208 {
5209   GstElement *uridecodebin = NULL;
5210   GstElement *suburidecodebin = NULL;
5211   GstPlayFlags flags;
5212   gboolean audio_sink_activated = FALSE;
5213   gboolean video_sink_activated = FALSE;
5214   gboolean text_sink_activated = FALSE;
5215   GstStateChangeReturn state_ret;
5216 
5217   g_return_val_if_fail (group->valid, GST_STATE_CHANGE_FAILURE);
5218   g_return_val_if_fail (!group->active, GST_STATE_CHANGE_FAILURE);
5219 
5220   GST_DEBUG_OBJECT (playbin, "activating group %p", group);
5221 
5222   GST_SOURCE_GROUP_LOCK (group);
5223 
5224   /* First set up the custom sources */
5225   if (playbin->audio_sink)
5226     group->audio_sink = gst_object_ref (playbin->audio_sink);
5227   else
5228     group->audio_sink =
5229         gst_play_sink_get_sink (playbin->playsink, GST_PLAY_SINK_TYPE_AUDIO);
5230 
5231   if (group->audio_sink) {
5232     if (!activate_sink (playbin, group->audio_sink, &audio_sink_activated)) {
5233       if (group->audio_sink == playbin->audio_sink) {
5234         goto sink_failure;
5235       } else {
5236         gst_object_unref (group->audio_sink);
5237         group->audio_sink = NULL;
5238       }
5239     }
5240   }
5241 
5242   if (playbin->video_sink)
5243     group->video_sink = gst_object_ref (playbin->video_sink);
5244   else
5245     group->video_sink =
5246         gst_play_sink_get_sink (playbin->playsink, GST_PLAY_SINK_TYPE_VIDEO);
5247 
5248   if (group->video_sink) {
5249     if (!activate_sink (playbin, group->video_sink, &video_sink_activated)) {
5250       if (group->video_sink == playbin->video_sink) {
5251         goto sink_failure;
5252       } else {
5253         gst_object_unref (group->video_sink);
5254         group->video_sink = NULL;
5255       }
5256     }
5257   }
5258 
5259   if (playbin->text_sink)
5260     group->text_sink = gst_object_ref (playbin->text_sink);
5261   else
5262     group->text_sink =
5263         gst_play_sink_get_sink (playbin->playsink, GST_PLAY_SINK_TYPE_TEXT);
5264 
5265   if (group->text_sink) {
5266     if (!activate_sink (playbin, group->text_sink, &text_sink_activated)) {
5267       if (group->text_sink == playbin->text_sink) {
5268         goto sink_failure;
5269       } else {
5270         gst_object_unref (group->text_sink);
5271         group->text_sink = NULL;
5272       }
5273     }
5274   }
5275 
5276   g_slist_free (group->suburi_flushes_to_drop);
5277   group->suburi_flushes_to_drop = NULL;
5278   if (!group->suburi_flushes_to_drop_lock.p)
5279     g_mutex_init (&group->suburi_flushes_to_drop_lock);
5280 
5281   if (group->uridecodebin) {
5282     GST_DEBUG_OBJECT (playbin, "reusing existing uridecodebin");
5283     uridecodebin = group->uridecodebin;
5284     gst_element_set_state (uridecodebin, GST_STATE_READY);
5285     /* no need to take extra ref, we already have one
5286      * and the bin will add one since it is no longer floating,
5287      * as it was at least added once before (below) */
5288     gst_bin_add (GST_BIN_CAST (playbin), uridecodebin);
5289   } else {
5290     GST_DEBUG_OBJECT (playbin, "making new uridecodebin");
5291     uridecodebin = gst_element_factory_make ("uridecodebin", NULL);
5292     if (!uridecodebin)
5293       goto no_decodebin;
5294     gst_bin_add (GST_BIN_CAST (playbin), uridecodebin);
5295     group->uridecodebin = gst_object_ref (uridecodebin);
5296   }
5297 
5298   flags = gst_play_sink_get_flags (playbin->playsink);
5299 
5300   g_object_set (uridecodebin,
5301       /* configure connection speed */
5302       "connection-speed", playbin->connection_speed / 1000,
5303       /* configure uri */
5304       "uri", group->uri,
5305       /* configure download buffering */
5306       "download", ((flags & GST_PLAY_FLAG_DOWNLOAD) != 0),
5307       /* configure buffering of demuxed/parsed data */
5308       "use-buffering", ((flags & GST_PLAY_FLAG_BUFFERING) != 0),
5309       /* configure buffering parameters */
5310       "buffer-duration", playbin->buffer_duration,
5311       "buffer-size", playbin->buffer_size,
5312       "ring-buffer-max-size", playbin->ring_buffer_max_size, NULL);
5313 
5314   /* connect pads and other things */
5315   group->pad_added_id = g_signal_connect (uridecodebin, "pad-added",
5316       G_CALLBACK (pad_added_cb), group);
5317   group->pad_removed_id = g_signal_connect (uridecodebin, "pad-removed",
5318       G_CALLBACK (pad_removed_cb), group);
5319   group->no_more_pads_id = g_signal_connect (uridecodebin, "no-more-pads",
5320       G_CALLBACK (no_more_pads_cb), group);
5321   group->notify_source_id = g_signal_connect (uridecodebin, "notify::source",
5322       G_CALLBACK (notify_source_cb), group);
5323 
5324   /* we have 1 pending no-more-pads */
5325   group->pending = 1;
5326 
5327   /* is called when the uridecodebin is out of data and we can switch to the
5328    * next uri */
5329   group->drained_id =
5330       g_signal_connect (uridecodebin, "drained", G_CALLBACK (drained_cb),
5331       group);
5332 
5333   /* will be called when a new media type is found. We return a list of decoders
5334    * including sinks for decodebin to try */
5335   group->autoplug_factories_id =
5336       g_signal_connect (uridecodebin, "autoplug-factories",
5337       G_CALLBACK (autoplug_factories_cb), group);
5338   group->autoplug_select_id =
5339       g_signal_connect (uridecodebin, "autoplug-select",
5340       G_CALLBACK (autoplug_select_cb), group);
5341   group->autoplug_continue_id =
5342       g_signal_connect (uridecodebin, "autoplug-continue",
5343       G_CALLBACK (autoplug_continue_cb), group);
5344   group->autoplug_query_id =
5345       g_signal_connect (uridecodebin, "autoplug-query",
5346       G_CALLBACK (autoplug_query_cb), group);
5347 
5348   if (group->suburi) {
5349     /* subtitles */
5350     if (group->suburidecodebin) {
5351       GST_DEBUG_OBJECT (playbin, "reusing existing suburidecodebin");
5352       suburidecodebin = group->suburidecodebin;
5353       gst_element_set_state (suburidecodebin, GST_STATE_READY);
5354       /* no need to take extra ref, we already have one
5355        * and the bin will add one since it is no longer floating,
5356        * as it was at least added once before (below) */
5357       gst_bin_add (GST_BIN_CAST (playbin), suburidecodebin);
5358     } else {
5359       GST_DEBUG_OBJECT (playbin, "making new suburidecodebin");
5360       suburidecodebin = gst_element_factory_make ("uridecodebin", NULL);
5361       if (!suburidecodebin)
5362         goto no_decodebin;
5363 
5364       gst_bin_add (GST_BIN_CAST (playbin), suburidecodebin);
5365       group->suburidecodebin = gst_object_ref (suburidecodebin);
5366     }
5367 
5368     g_object_set (suburidecodebin,
5369         /* configure connection speed */
5370         "connection-speed", playbin->connection_speed,
5371         /* configure uri */
5372         "uri", group->suburi, NULL);
5373 
5374     /* connect pads and other things */
5375     group->sub_pad_added_id = g_signal_connect (suburidecodebin, "pad-added",
5376         G_CALLBACK (pad_added_cb), group);
5377     group->sub_pad_removed_id = g_signal_connect (suburidecodebin,
5378         "pad-removed", G_CALLBACK (pad_removed_cb), group);
5379     group->sub_no_more_pads_id = g_signal_connect (suburidecodebin,
5380         "no-more-pads", G_CALLBACK (no_more_pads_cb), group);
5381 
5382     group->sub_autoplug_continue_id =
5383         g_signal_connect (suburidecodebin, "autoplug-continue",
5384         G_CALLBACK (autoplug_continue_cb), group);
5385 
5386     group->sub_autoplug_query_id =
5387         g_signal_connect (suburidecodebin, "autoplug-query",
5388         G_CALLBACK (autoplug_query_cb), group);
5389 
5390     /* we have 2 pending no-more-pads */
5391     group->pending = 2;
5392     group->sub_pending = TRUE;
5393   } else {
5394     group->sub_pending = FALSE;
5395   }
5396 
5397   /* release the group lock before setting the state of the decodebins, they
5398    * might fire signals in this thread that we need to handle with the
5399    * group_lock taken. */
5400   GST_SOURCE_GROUP_UNLOCK (group);
5401 
5402   if (suburidecodebin) {
5403     if (gst_element_set_state (suburidecodebin,
5404             target) == GST_STATE_CHANGE_FAILURE) {
5405       GST_DEBUG_OBJECT (playbin,
5406           "failed state change of subtitle uridecodebin");
5407       GST_SOURCE_GROUP_LOCK (group);
5408 
5409       REMOVE_SIGNAL (group->suburidecodebin, group->sub_pad_added_id);
5410       REMOVE_SIGNAL (group->suburidecodebin, group->sub_pad_removed_id);
5411       REMOVE_SIGNAL (group->suburidecodebin, group->sub_no_more_pads_id);
5412       REMOVE_SIGNAL (group->suburidecodebin, group->sub_autoplug_continue_id);
5413       REMOVE_SIGNAL (group->suburidecodebin, group->sub_autoplug_query_id);
5414       /* Might already be removed because of an error message */
5415       if (GST_OBJECT_PARENT (suburidecodebin) == GST_OBJECT_CAST (playbin))
5416         gst_bin_remove (GST_BIN_CAST (playbin), suburidecodebin);
5417       if (group->sub_pending) {
5418         group->pending--;
5419         group->sub_pending = FALSE;
5420       }
5421       gst_element_set_state (suburidecodebin, GST_STATE_READY);
5422       g_free (group->suburi);
5423       group->suburi = NULL;
5424       GST_SOURCE_GROUP_UNLOCK (group);
5425     }
5426   }
5427   if ((state_ret =
5428           gst_element_set_state (uridecodebin,
5429               target)) == GST_STATE_CHANGE_FAILURE)
5430     goto uridecodebin_failure;
5431 
5432   GST_SOURCE_GROUP_LOCK (group);
5433   /* allow state changes of the playbin affect the group elements now */
5434   group_set_locked_state_unlocked (playbin, group, FALSE);
5435   group->active = TRUE;
5436   GST_SOURCE_GROUP_UNLOCK (group);
5437 
5438   return state_ret;
5439 
5440   /* ERRORS */
5441 no_decodebin:
5442   {
5443     GstMessage *msg;
5444 
5445     GST_SOURCE_GROUP_UNLOCK (group);
5446     msg =
5447         gst_missing_element_message_new (GST_ELEMENT_CAST (playbin),
5448         "uridecodebin");
5449     gst_element_post_message (GST_ELEMENT_CAST (playbin), msg);
5450 
5451     GST_ELEMENT_ERROR (playbin, CORE, MISSING_PLUGIN,
5452         (_("Could not create \"uridecodebin\" element.")), (NULL));
5453 
5454     GST_SOURCE_GROUP_LOCK (group);
5455 
5456     goto error_cleanup;
5457   }
5458 uridecodebin_failure:
5459   {
5460     GST_DEBUG_OBJECT (playbin, "failed state change of uridecodebin");
5461     GST_SOURCE_GROUP_LOCK (group);
5462     goto error_cleanup;
5463   }
5464 sink_failure:
5465   {
5466     GST_ERROR_OBJECT (playbin, "failed to activate sinks");
5467     goto error_cleanup;
5468   }
5469 
5470 error_cleanup:
5471   {
5472     /* delete any custom sinks we might have */
5473     if (group->audio_sink) {
5474       /* If this is a automatically created sink set it to NULL */
5475       if (audio_sink_activated)
5476         gst_element_set_state (group->audio_sink, GST_STATE_NULL);
5477       gst_object_unref (group->audio_sink);
5478     }
5479     group->audio_sink = NULL;
5480 
5481     if (group->video_sink) {
5482       /* If this is a automatically created sink set it to NULL */
5483       if (video_sink_activated)
5484         gst_element_set_state (group->video_sink, GST_STATE_NULL);
5485       gst_object_unref (group->video_sink);
5486     }
5487     group->video_sink = NULL;
5488 
5489     if (group->text_sink) {
5490       /* If this is a automatically created sink set it to NULL */
5491       if (text_sink_activated)
5492         gst_element_set_state (group->text_sink, GST_STATE_NULL);
5493       gst_object_unref (group->text_sink);
5494     }
5495     group->text_sink = NULL;
5496 
5497     if (uridecodebin) {
5498       REMOVE_SIGNAL (group->uridecodebin, group->pad_added_id);
5499       REMOVE_SIGNAL (group->uridecodebin, group->pad_removed_id);
5500       REMOVE_SIGNAL (group->uridecodebin, group->no_more_pads_id);
5501       REMOVE_SIGNAL (group->uridecodebin, group->notify_source_id);
5502       REMOVE_SIGNAL (group->uridecodebin, group->drained_id);
5503       REMOVE_SIGNAL (group->uridecodebin, group->autoplug_factories_id);
5504       REMOVE_SIGNAL (group->uridecodebin, group->autoplug_select_id);
5505       REMOVE_SIGNAL (group->uridecodebin, group->autoplug_continue_id);
5506       REMOVE_SIGNAL (group->uridecodebin, group->autoplug_query_id);
5507 
5508       gst_element_set_state (uridecodebin, GST_STATE_NULL);
5509       gst_bin_remove (GST_BIN_CAST (playbin), uridecodebin);
5510     }
5511 
5512     GST_SOURCE_GROUP_UNLOCK (group);
5513 
5514     return GST_STATE_CHANGE_FAILURE;
5515   }
5516 }
5517 
5518 /* unlink a group of uridecodebins from the sink.
5519  * must be called with PLAY_BIN_LOCK */
5520 static gboolean
deactivate_group(GstPlayBin * playbin,GstSourceGroup * group)5521 deactivate_group (GstPlayBin * playbin, GstSourceGroup * group)
5522 {
5523   gint i;
5524 
5525   g_return_val_if_fail (group->active, FALSE);
5526   g_return_val_if_fail (group->valid, FALSE);
5527 
5528   GST_DEBUG_OBJECT (playbin, "unlinking group %p", group);
5529 
5530   GST_SOURCE_GROUP_LOCK (group);
5531   group->active = FALSE;
5532   for (i = 0; i < PLAYBIN_STREAM_LAST; i++) {
5533     GstSourceCombine *combine = &group->combiner[i];
5534 
5535     GST_DEBUG_OBJECT (playbin, "unlinking combiner %s", combine->media_list[0]);
5536 
5537     if (combine->srcpad) {
5538       source_combine_remove_pads (playbin, combine);
5539     }
5540 
5541     if (combine->combiner) {
5542       gint n;
5543 
5544       /* release and unref requests pad from the combiner */
5545       for (n = 0; n < combine->channels->len; n++) {
5546         GstPad *sinkpad = g_ptr_array_index (combine->channels, n);
5547 
5548         gst_element_release_request_pad (combine->combiner, sinkpad);
5549         gst_object_unref (sinkpad);
5550       }
5551       g_ptr_array_set_size (combine->channels, 0);
5552 
5553       gst_element_set_state (combine->combiner, GST_STATE_NULL);
5554       gst_bin_remove (GST_BIN_CAST (playbin), combine->combiner);
5555       combine->combiner = NULL;
5556     }
5557   }
5558   /* delete any custom sinks we might have.
5559    * conditionally set them to null if they aren't inside playsink yet */
5560   if (group->audio_sink) {
5561     if (!gst_object_has_as_ancestor (GST_OBJECT_CAST (group->audio_sink),
5562             GST_OBJECT_CAST (playbin->playsink))) {
5563       gst_element_set_state (group->audio_sink, GST_STATE_NULL);
5564     }
5565     gst_object_unref (group->audio_sink);
5566   }
5567   group->audio_sink = NULL;
5568   if (group->video_sink) {
5569     if (!gst_object_has_as_ancestor (GST_OBJECT_CAST (group->video_sink),
5570             GST_OBJECT_CAST (playbin->playsink))) {
5571       gst_element_set_state (group->video_sink, GST_STATE_NULL);
5572     }
5573     gst_object_unref (group->video_sink);
5574   }
5575   group->video_sink = NULL;
5576   if (group->text_sink) {
5577     if (!gst_object_has_as_ancestor (GST_OBJECT_CAST (group->text_sink),
5578             GST_OBJECT_CAST (playbin->playsink))) {
5579       gst_element_set_state (group->text_sink, GST_STATE_NULL);
5580     }
5581     gst_object_unref (group->text_sink);
5582   }
5583   group->text_sink = NULL;
5584 
5585   if (group->uridecodebin) {
5586     REMOVE_SIGNAL (group->uridecodebin, group->pad_added_id);
5587     REMOVE_SIGNAL (group->uridecodebin, group->pad_removed_id);
5588     REMOVE_SIGNAL (group->uridecodebin, group->no_more_pads_id);
5589     REMOVE_SIGNAL (group->uridecodebin, group->notify_source_id);
5590     REMOVE_SIGNAL (group->uridecodebin, group->drained_id);
5591     REMOVE_SIGNAL (group->uridecodebin, group->autoplug_factories_id);
5592     REMOVE_SIGNAL (group->uridecodebin, group->autoplug_select_id);
5593     REMOVE_SIGNAL (group->uridecodebin, group->autoplug_continue_id);
5594     REMOVE_SIGNAL (group->uridecodebin, group->autoplug_query_id);
5595     gst_bin_remove (GST_BIN_CAST (playbin), group->uridecodebin);
5596   }
5597 
5598   if (group->suburidecodebin) {
5599     REMOVE_SIGNAL (group->suburidecodebin, group->sub_pad_added_id);
5600     REMOVE_SIGNAL (group->suburidecodebin, group->sub_pad_removed_id);
5601     REMOVE_SIGNAL (group->suburidecodebin, group->sub_no_more_pads_id);
5602     REMOVE_SIGNAL (group->suburidecodebin, group->sub_autoplug_continue_id);
5603     REMOVE_SIGNAL (group->suburidecodebin, group->sub_autoplug_query_id);
5604 
5605     /* Might already be removed because of errors */
5606     if (GST_OBJECT_PARENT (group->suburidecodebin) == GST_OBJECT_CAST (playbin))
5607       gst_bin_remove (GST_BIN_CAST (playbin), group->suburidecodebin);
5608   }
5609 
5610   group->have_group_id = FALSE;
5611 
5612   GST_SOURCE_GROUP_UNLOCK (group);
5613 
5614   return TRUE;
5615 }
5616 
5617 /* setup the next group to play, this assumes the next_group is valid and
5618  * configured. It swaps out the current_group and activates the valid
5619  * next_group. */
5620 static GstStateChangeReturn
setup_next_source(GstPlayBin * playbin,GstState target)5621 setup_next_source (GstPlayBin * playbin, GstState target)
5622 {
5623   GstSourceGroup *new_group, *old_group;
5624   GstStateChangeReturn state_ret;
5625 
5626   GST_DEBUG_OBJECT (playbin, "setup sources");
5627 
5628   /* see if there is a next group */
5629   GST_PLAY_BIN_LOCK (playbin);
5630   new_group = playbin->next_group;
5631   if (!new_group || !new_group->valid)
5632     goto no_next_group;
5633 
5634   /* first unlink the current source, if any */
5635   old_group = playbin->curr_group;
5636   if (old_group && old_group->valid && old_group->active) {
5637     new_group->stream_changed_pending = TRUE;
5638 
5639     gst_play_bin_update_cached_duration (playbin);
5640     /* unlink our pads with the sink */
5641     deactivate_group (playbin, old_group);
5642     old_group->valid = FALSE;
5643   }
5644 
5645   /* swap old and new */
5646   playbin->curr_group = new_group;
5647   playbin->next_group = old_group;
5648 
5649   /* activate the new group */
5650   if ((state_ret =
5651           activate_group (playbin, new_group,
5652               target)) == GST_STATE_CHANGE_FAILURE)
5653     goto activate_failed;
5654 
5655   GST_PLAY_BIN_UNLOCK (playbin);
5656 
5657   return state_ret;
5658 
5659   /* ERRORS */
5660 no_next_group:
5661   {
5662     GST_DEBUG_OBJECT (playbin, "no next group");
5663     if (target == GST_STATE_READY && new_group && new_group->uri == NULL)
5664       GST_ELEMENT_ERROR (playbin, RESOURCE, NOT_FOUND, ("No URI set"), (NULL));
5665     GST_PLAY_BIN_UNLOCK (playbin);
5666     return GST_STATE_CHANGE_FAILURE;
5667   }
5668 activate_failed:
5669   {
5670     new_group->stream_changed_pending = FALSE;
5671     GST_DEBUG_OBJECT (playbin, "activate failed");
5672     new_group->valid = FALSE;
5673     GST_PLAY_BIN_UNLOCK (playbin);
5674     return GST_STATE_CHANGE_FAILURE;
5675   }
5676 }
5677 
5678 /* The group that is currently playing is copied again to the
5679  * next_group so that it will start playing the next time.
5680  */
5681 static gboolean
save_current_group(GstPlayBin * playbin)5682 save_current_group (GstPlayBin * playbin)
5683 {
5684   GstSourceGroup *curr_group;
5685 
5686   GST_DEBUG_OBJECT (playbin, "save current group");
5687 
5688   /* see if there is a current group */
5689   GST_PLAY_BIN_LOCK (playbin);
5690   curr_group = playbin->curr_group;
5691   if (curr_group && curr_group->valid && curr_group->active) {
5692     /* unlink our pads with the sink */
5693     deactivate_group (playbin, curr_group);
5694   }
5695   /* swap old and new */
5696   playbin->curr_group = playbin->next_group;
5697   playbin->next_group = curr_group;
5698   GST_PLAY_BIN_UNLOCK (playbin);
5699 
5700   return TRUE;
5701 }
5702 
5703 /* clear the locked state from all groups. This function is called before a
5704  * state change to NULL is performed on them. */
5705 static gboolean
groups_set_locked_state(GstPlayBin * playbin,gboolean locked)5706 groups_set_locked_state (GstPlayBin * playbin, gboolean locked)
5707 {
5708   GST_DEBUG_OBJECT (playbin, "setting locked state to %d on all groups",
5709       locked);
5710 
5711   GST_PLAY_BIN_LOCK (playbin);
5712   GST_SOURCE_GROUP_LOCK (playbin->curr_group);
5713   group_set_locked_state_unlocked (playbin, playbin->curr_group, locked);
5714   GST_SOURCE_GROUP_UNLOCK (playbin->curr_group);
5715   GST_SOURCE_GROUP_LOCK (playbin->next_group);
5716   group_set_locked_state_unlocked (playbin, playbin->next_group, locked);
5717   GST_SOURCE_GROUP_UNLOCK (playbin->next_group);
5718   GST_PLAY_BIN_UNLOCK (playbin);
5719 
5720   return TRUE;
5721 }
5722 
5723 static GstStateChangeReturn
gst_play_bin_change_state(GstElement * element,GstStateChange transition)5724 gst_play_bin_change_state (GstElement * element, GstStateChange transition)
5725 {
5726   GstStateChangeReturn ret;
5727   GstPlayBin *playbin;
5728   gboolean do_save = FALSE;
5729 
5730   playbin = GST_PLAY_BIN (element);
5731 
5732   switch (transition) {
5733     case GST_STATE_CHANGE_NULL_TO_READY:
5734       memset (&playbin->duration, 0, sizeof (playbin->duration));
5735       break;
5736     case GST_STATE_CHANGE_READY_TO_PAUSED:
5737       GST_LOG_OBJECT (playbin, "clearing shutdown flag");
5738       memset (&playbin->duration, 0, sizeof (playbin->duration));
5739       g_atomic_int_set (&playbin->shutdown, 0);
5740       do_async_start (playbin);
5741       break;
5742     case GST_STATE_CHANGE_PAUSED_TO_READY:
5743     async_down:
5744       /* FIXME unlock our waiting groups */
5745       GST_LOG_OBJECT (playbin, "setting shutdown flag");
5746       g_atomic_int_set (&playbin->shutdown, 1);
5747       memset (&playbin->duration, 0, sizeof (playbin->duration));
5748 
5749       /* wait for all callbacks to end by taking the lock.
5750        * No dynamic (critical) new callbacks will
5751        * be able to happen as we set the shutdown flag. */
5752       GST_PLAY_BIN_DYN_LOCK (playbin);
5753       GST_LOG_OBJECT (playbin, "dynamic lock taken, we can continue shutdown");
5754       GST_PLAY_BIN_DYN_UNLOCK (playbin);
5755       if (!do_save)
5756         break;
5757     case GST_STATE_CHANGE_READY_TO_NULL:
5758       /* we go async to PAUSED, so if that fails, we never make it to PAUSED
5759        * and no state change PAUSED to READY passes here,
5760        * though it is a nice-to-have ... */
5761       if (!g_atomic_int_get (&playbin->shutdown)) {
5762         do_save = TRUE;
5763         goto async_down;
5764       }
5765       memset (&playbin->duration, 0, sizeof (playbin->duration));
5766 
5767       /* unlock so that all groups go to NULL */
5768       groups_set_locked_state (playbin, FALSE);
5769       break;
5770     default:
5771       break;
5772   }
5773 
5774   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5775   if (ret == GST_STATE_CHANGE_FAILURE)
5776     goto failure;
5777 
5778   switch (transition) {
5779     case GST_STATE_CHANGE_READY_TO_PAUSED:
5780       if ((ret =
5781               setup_next_source (playbin,
5782                   GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE)
5783         goto failure;
5784       if (ret == GST_STATE_CHANGE_SUCCESS)
5785         ret = GST_STATE_CHANGE_ASYNC;
5786 
5787       break;
5788     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
5789       do_async_done (playbin);
5790       /* FIXME Release audio device when we implement that */
5791       break;
5792     case GST_STATE_CHANGE_PAUSED_TO_READY:
5793       save_current_group (playbin);
5794       break;
5795     case GST_STATE_CHANGE_READY_TO_NULL:
5796     {
5797       guint i;
5798       GList *l;
5799 
5800       /* also do missed state change down to READY */
5801       if (do_save)
5802         save_current_group (playbin);
5803       /* Deactive the groups, set the uridecodebins to NULL
5804        * and unref them.
5805        */
5806       for (i = 0; i < 2; i++) {
5807         if (playbin->groups[i].active && playbin->groups[i].valid) {
5808           deactivate_group (playbin, &playbin->groups[i]);
5809           playbin->groups[i].valid = FALSE;
5810         }
5811 
5812         if (playbin->groups[i].uridecodebin) {
5813           gst_element_set_state (playbin->groups[i].uridecodebin,
5814               GST_STATE_NULL);
5815           gst_object_unref (playbin->groups[i].uridecodebin);
5816           playbin->groups[i].uridecodebin = NULL;
5817         }
5818 
5819         if (playbin->groups[i].suburidecodebin) {
5820           gst_element_set_state (playbin->groups[i].suburidecodebin,
5821               GST_STATE_NULL);
5822           gst_object_unref (playbin->groups[i].suburidecodebin);
5823           playbin->groups[i].suburidecodebin = NULL;
5824         }
5825       }
5826 
5827       /* Set our sinks back to NULL, they might not be child of playbin */
5828       if (playbin->audio_sink)
5829         gst_element_set_state (playbin->audio_sink, GST_STATE_NULL);
5830       if (playbin->video_sink)
5831         gst_element_set_state (playbin->video_sink, GST_STATE_NULL);
5832       if (playbin->text_sink)
5833         gst_element_set_state (playbin->text_sink, GST_STATE_NULL);
5834 
5835       if (playbin->video_stream_combiner)
5836         gst_element_set_state (playbin->video_stream_combiner, GST_STATE_NULL);
5837       if (playbin->audio_stream_combiner)
5838         gst_element_set_state (playbin->audio_stream_combiner, GST_STATE_NULL);
5839       if (playbin->text_stream_combiner)
5840         gst_element_set_state (playbin->text_stream_combiner, GST_STATE_NULL);
5841 
5842       /* make sure the groups don't perform a state change anymore until we
5843        * enable them again */
5844       groups_set_locked_state (playbin, TRUE);
5845 
5846       /* Remove all non-persistent contexts */
5847       GST_OBJECT_LOCK (playbin);
5848       for (l = playbin->contexts; l;) {
5849         GstContext *context = l->data;
5850 
5851         if (!gst_context_is_persistent (context)) {
5852           GList *next;
5853 
5854           gst_context_unref (context);
5855 
5856           next = l->next;
5857           playbin->contexts = g_list_delete_link (playbin->contexts, l);
5858           l = next;
5859         } else {
5860           l = l->next;
5861         }
5862       }
5863 
5864       if (playbin->source) {
5865         gst_object_unref (playbin->source);
5866         playbin->source = NULL;
5867       }
5868 
5869       GST_OBJECT_UNLOCK (playbin);
5870       break;
5871     }
5872     default:
5873       break;
5874   }
5875 
5876   if (ret == GST_STATE_CHANGE_NO_PREROLL)
5877     do_async_done (playbin);
5878 
5879   return ret;
5880 
5881   /* ERRORS */
5882 failure:
5883   {
5884     do_async_done (playbin);
5885 
5886     if (transition == GST_STATE_CHANGE_READY_TO_PAUSED) {
5887       GstSourceGroup *curr_group;
5888 
5889       curr_group = playbin->curr_group;
5890       if (curr_group) {
5891         if (curr_group->active && curr_group->valid) {
5892           /* unlink our pads with the sink */
5893           deactivate_group (playbin, curr_group);
5894         }
5895         curr_group->valid = FALSE;
5896       }
5897 
5898       /* Swap current and next group back */
5899       playbin->curr_group = playbin->next_group;
5900       playbin->next_group = curr_group;
5901     }
5902     return ret;
5903   }
5904 }
5905 
5906 static void
gst_play_bin_overlay_expose(GstVideoOverlay * overlay)5907 gst_play_bin_overlay_expose (GstVideoOverlay * overlay)
5908 {
5909   GstPlayBin *playbin = GST_PLAY_BIN (overlay);
5910 
5911   gst_video_overlay_expose (GST_VIDEO_OVERLAY (playbin->playsink));
5912 }
5913 
5914 static void
gst_play_bin_overlay_handle_events(GstVideoOverlay * overlay,gboolean handle_events)5915 gst_play_bin_overlay_handle_events (GstVideoOverlay * overlay,
5916     gboolean handle_events)
5917 {
5918   GstPlayBin *playbin = GST_PLAY_BIN (overlay);
5919 
5920   gst_video_overlay_handle_events (GST_VIDEO_OVERLAY (playbin->playsink),
5921       handle_events);
5922 }
5923 
5924 static void
gst_play_bin_overlay_set_render_rectangle(GstVideoOverlay * overlay,gint x,gint y,gint width,gint height)5925 gst_play_bin_overlay_set_render_rectangle (GstVideoOverlay * overlay, gint x,
5926     gint y, gint width, gint height)
5927 {
5928   GstPlayBin *playbin = GST_PLAY_BIN (overlay);
5929 
5930   gst_video_overlay_set_render_rectangle (GST_VIDEO_OVERLAY (playbin->playsink),
5931       x, y, width, height);
5932 }
5933 
5934 static void
gst_play_bin_overlay_set_window_handle(GstVideoOverlay * overlay,guintptr handle)5935 gst_play_bin_overlay_set_window_handle (GstVideoOverlay * overlay,
5936     guintptr handle)
5937 {
5938   GstPlayBin *playbin = GST_PLAY_BIN (overlay);
5939 
5940   gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (playbin->playsink),
5941       handle);
5942 }
5943 
5944 static void
gst_play_bin_overlay_init(gpointer g_iface,gpointer g_iface_data)5945 gst_play_bin_overlay_init (gpointer g_iface, gpointer g_iface_data)
5946 {
5947   GstVideoOverlayInterface *iface = (GstVideoOverlayInterface *) g_iface;
5948   iface->expose = gst_play_bin_overlay_expose;
5949   iface->handle_events = gst_play_bin_overlay_handle_events;
5950   iface->set_render_rectangle = gst_play_bin_overlay_set_render_rectangle;
5951   iface->set_window_handle = gst_play_bin_overlay_set_window_handle;
5952 }
5953 
5954 static void
gst_play_bin_navigation_send_event(GstNavigation * navigation,GstStructure * structure)5955 gst_play_bin_navigation_send_event (GstNavigation * navigation,
5956     GstStructure * structure)
5957 {
5958   GstPlayBin *playbin = GST_PLAY_BIN (navigation);
5959 
5960   gst_navigation_send_event (GST_NAVIGATION (playbin->playsink), structure);
5961 }
5962 
5963 static void
gst_play_bin_navigation_init(gpointer g_iface,gpointer g_iface_data)5964 gst_play_bin_navigation_init (gpointer g_iface, gpointer g_iface_data)
5965 {
5966   GstNavigationInterface *iface = (GstNavigationInterface *) g_iface;
5967 
5968   iface->send_event = gst_play_bin_navigation_send_event;
5969 }
5970 
5971 static const GList *
gst_play_bin_colorbalance_list_channels(GstColorBalance * balance)5972 gst_play_bin_colorbalance_list_channels (GstColorBalance * balance)
5973 {
5974   GstPlayBin *playbin = GST_PLAY_BIN (balance);
5975 
5976   return
5977       gst_color_balance_list_channels (GST_COLOR_BALANCE (playbin->playsink));
5978 }
5979 
5980 static void
gst_play_bin_colorbalance_set_value(GstColorBalance * balance,GstColorBalanceChannel * channel,gint value)5981 gst_play_bin_colorbalance_set_value (GstColorBalance * balance,
5982     GstColorBalanceChannel * channel, gint value)
5983 {
5984   GstPlayBin *playbin = GST_PLAY_BIN (balance);
5985 
5986   gst_color_balance_set_value (GST_COLOR_BALANCE (playbin->playsink), channel,
5987       value);
5988 }
5989 
5990 static gint
gst_play_bin_colorbalance_get_value(GstColorBalance * balance,GstColorBalanceChannel * channel)5991 gst_play_bin_colorbalance_get_value (GstColorBalance * balance,
5992     GstColorBalanceChannel * channel)
5993 {
5994   GstPlayBin *playbin = GST_PLAY_BIN (balance);
5995 
5996   return gst_color_balance_get_value (GST_COLOR_BALANCE (playbin->playsink),
5997       channel);
5998 }
5999 
6000 static GstColorBalanceType
gst_play_bin_colorbalance_get_balance_type(GstColorBalance * balance)6001 gst_play_bin_colorbalance_get_balance_type (GstColorBalance * balance)
6002 {
6003   GstPlayBin *playbin = GST_PLAY_BIN (balance);
6004 
6005   return
6006       gst_color_balance_get_balance_type (GST_COLOR_BALANCE
6007       (playbin->playsink));
6008 }
6009 
6010 static void
gst_play_bin_colorbalance_init(gpointer g_iface,gpointer g_iface_data)6011 gst_play_bin_colorbalance_init (gpointer g_iface, gpointer g_iface_data)
6012 {
6013   GstColorBalanceInterface *iface = (GstColorBalanceInterface *) g_iface;
6014 
6015   iface->list_channels = gst_play_bin_colorbalance_list_channels;
6016   iface->set_value = gst_play_bin_colorbalance_set_value;
6017   iface->get_value = gst_play_bin_colorbalance_get_value;
6018   iface->get_balance_type = gst_play_bin_colorbalance_get_balance_type;
6019 }
6020 
6021 gboolean
gst_play_bin2_plugin_init(GstPlugin * plugin)6022 gst_play_bin2_plugin_init (GstPlugin * plugin)
6023 {
6024   GST_DEBUG_CATEGORY_INIT (gst_play_bin_debug, "playbin", 0, "play bin");
6025 
6026   return gst_element_register (plugin, "playbin", GST_RANK_NONE,
6027       GST_TYPE_PLAY_BIN);
6028 }
6029