Merge branch '0.10'
[platform/upstream/gstreamer.git] / gst / playback / gstplaysink.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  * Copyright (C) <2011> Sebastian Dröge <sebastian.droege@collabora.co.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <string.h>
26 #include <gst/gst.h>
27
28 #include <gst/gst-i18n-plugin.h>
29 #include <gst/pbutils/pbutils.h>
30 #include <gst/video/video.h>
31 #include <gst/audio/streamvolume.h>
32 #include <gst/video/colorbalance.h>
33 #include <gst/video/videooverlay.h>
34 #include <gst/interfaces/navigation.h>
35
36 #include "gstplaysink.h"
37 #include "gststreamsynchronizer.h"
38 #include "gstplaysinkvideoconvert.h"
39 #include "gstplaysinkaudioconvert.h"
40
41 GST_DEBUG_CATEGORY_STATIC (gst_play_sink_debug);
42 #define GST_CAT_DEFAULT gst_play_sink_debug
43
44 #define VOLUME_MAX_DOUBLE 10.0
45
46 #define DEFAULT_FLAGS             GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_TEXT | \
47                                   GST_PLAY_FLAG_SOFT_VOLUME | GST_PLAY_FLAG_SOFT_COLORBALANCE
48
49 #define GST_PLAY_CHAIN(c) ((GstPlayChain *)(c))
50
51 /* enum types */
52 /**
53  * GstPlaySinkSendEventMode:
54  * @MODE_DEFAULT: default GstBin's send_event handling
55  * @MODE_FIRST: send event only to the first sink that return true
56  *
57  * Send event handling to use
58  */
59 typedef enum
60 {
61   MODE_DEFAULT = 0,
62   MODE_FIRST = 1
63 } GstPlaySinkSendEventMode;
64
65
66 #define GST_TYPE_PLAY_SINK_SEND_EVENT_MODE (gst_play_sink_send_event_mode_get_type ())
67 static GType
68 gst_play_sink_send_event_mode_get_type (void)
69 {
70   static GType gtype = 0;
71
72   if (gtype == 0) {
73     static const GEnumValue values[] = {
74       {MODE_DEFAULT, "Default GstBin's send_event handling (default)",
75           "default"},
76       {MODE_FIRST, "Sends the event to sinks until the first one handles it",
77           "first"},
78       {0, NULL, NULL}
79     };
80
81     gtype = g_enum_register_static ("GstPlaySinkSendEventMode", values);
82   }
83   return gtype;
84 }
85
86 /* holds the common data fields for the audio and video pipelines. We keep them
87  * in a structure to more easily have all the info available. */
88 typedef struct
89 {
90   GstPlaySink *playsink;
91   GstElement *bin;
92   gboolean added;
93   gboolean activated;
94   gboolean raw;
95 } GstPlayChain;
96
97 typedef struct
98 {
99   GstPlayChain chain;
100   GstPad *sinkpad;
101   GstElement *queue;
102   GstElement *conv;
103   GstElement *volume;           /* element with the volume property */
104   gboolean sink_volume;         /* if the volume was provided by the sink */
105   GstElement *mute;             /* element with the mute property */
106   GstElement *sink;
107   GstElement *ts_offset;
108 } GstPlayAudioChain;
109
110 typedef struct
111 {
112   GstPlayChain chain;
113   GstPad *sinkpad, *srcpad;
114   GstElement *conv;
115   GstElement *deinterlace;
116 } GstPlayVideoDeinterlaceChain;
117
118 typedef struct
119 {
120   GstPlayChain chain;
121   GstPad *sinkpad;
122   GstElement *queue;
123   GstElement *conv;
124   GstElement *sink;
125   gboolean async;
126   GstElement *ts_offset;
127 } GstPlayVideoChain;
128
129 typedef struct
130 {
131   GstPlayChain chain;
132   GstPad *sinkpad;
133   GstElement *queue;
134   GstElement *conv;
135   GstElement *resample;
136   GstPad *blockpad;             /* srcpad of resample, used for switching the vis */
137   GstPad *vissinkpad;           /* visualisation sinkpad, */
138   GstElement *vis;
139   GstPad *vissrcpad;            /* visualisation srcpad, */
140   GstPad *srcpad;               /* outgoing srcpad, used to connect to the next
141                                  * chain */
142 } GstPlayVisChain;
143
144 typedef struct
145 {
146   GstPlayChain chain;
147   GstPad *sinkpad;
148   GstElement *queue;
149   GstElement *identity;
150   GstElement *overlay;
151   GstPad *videosinkpad;
152   GstPad *textsinkpad;
153   GstPad *srcpad;               /* outgoing srcpad, used to connect to the next
154                                  * chain */
155   GstElement *sink;             /* custom sink to receive subtitle buffers */
156 } GstPlayTextChain;
157
158 #define GST_PLAY_SINK_GET_LOCK(playsink) (&((GstPlaySink *)playsink)->lock)
159 #define GST_PLAY_SINK_LOCK(playsink)     G_STMT_START { \
160   GST_LOG_OBJECT (playsink, "locking from thread %p", g_thread_self ()); \
161   g_rec_mutex_lock (GST_PLAY_SINK_GET_LOCK (playsink)); \
162   GST_LOG_OBJECT (playsink, "locked from thread %p", g_thread_self ()); \
163 } G_STMT_END
164 #define GST_PLAY_SINK_UNLOCK(playsink)   G_STMT_START { \
165   GST_LOG_OBJECT (playsink, "unlocking from thread %p", g_thread_self ()); \
166   g_rec_mutex_unlock (GST_PLAY_SINK_GET_LOCK (playsink)); \
167 } G_STMT_END
168
169 #define PENDING_FLAG_SET(playsink, flagtype) \
170   ((playsink->pending_blocked_pads) |= (1 << flagtype))
171 #define PENDING_FLAG_UNSET(playsink, flagtype) \
172   ((playsink->pending_blocked_pads) &= ~(1 << flagtype))
173 #define PENDING_FLAG_IS_SET(playsink, flagtype) \
174   ((playsink->pending_blocked_pads) & (1 << flagtype))
175 #define PENDING_VIDEO_BLOCK(playsink) \
176   ((playsink->pending_blocked_pads) & (1 << GST_PLAY_SINK_TYPE_VIDEO_RAW | 1 << GST_PLAY_SINK_TYPE_VIDEO))
177 #define PENDING_AUDIO_BLOCK(playsink) \
178   ((playsink->pending_blocked_pads) & (1 << GST_PLAY_SINK_TYPE_AUDIO_RAW | 1 << GST_PLAY_SINK_TYPE_AUDIO))
179 #define PENDING_TEXT_BLOCK(playsink) \
180   PENDING_FLAG_IS_SET(playsink, GST_PLAY_SINK_TYPE_TEXT)
181
182 struct _GstPlaySink
183 {
184   GstBin bin;
185
186   GRecMutex lock;
187
188   gboolean async_pending;
189   gboolean need_async_start;
190
191   GstPlayFlags flags;
192
193   GstStreamSynchronizer *stream_synchronizer;
194
195   /* chains */
196   GstPlayAudioChain *audiochain;
197   GstPlayVideoDeinterlaceChain *videodeinterlacechain;
198   GstPlayVideoChain *videochain;
199   GstPlayVisChain *vischain;
200   GstPlayTextChain *textchain;
201
202   /* audio */
203   GstPad *audio_pad;
204   gboolean audio_pad_raw;
205   gboolean audio_pad_blocked;
206   GstPad *audio_srcpad_stream_synchronizer;
207   GstPad *audio_sinkpad_stream_synchronizer;
208   gulong audio_block_id;
209   /* audio tee */
210   GstElement *audio_tee;
211   GstPad *audio_tee_sink;
212   GstPad *audio_tee_asrc;
213   GstPad *audio_tee_vissrc;
214   /* video */
215   GstPad *video_pad;
216   gboolean video_pad_raw;
217   gboolean video_pad_blocked;
218   GstPad *video_srcpad_stream_synchronizer;
219   GstPad *video_sinkpad_stream_synchronizer;
220   gulong video_block_id;
221   /* text */
222   GstPad *text_pad;
223   gboolean text_pad_blocked;
224   GstPad *text_srcpad_stream_synchronizer;
225   GstPad *text_sinkpad_stream_synchronizer;
226   gulong text_block_id;
227
228   guint32 pending_blocked_pads;
229
230   /* properties */
231   GstElement *audio_sink;
232   GstElement *video_sink;
233   GstElement *visualisation;
234   GstElement *text_sink;
235   gdouble volume;
236   gboolean mute;
237   gchar *font_desc;             /* font description */
238   gchar *subtitle_encoding;     /* subtitle encoding */
239   guint connection_speed;       /* connection speed in bits/sec (0 = unknown) */
240   guint count;
241   gboolean volume_changed;      /* volume/mute changed while no audiochain */
242   gboolean mute_changed;        /* ... has been created yet */
243   gint64 av_offset;
244   GstPlaySinkSendEventMode send_event_mode;
245
246   /* videooverlay proxy interface */
247   GstVideoOverlay *overlay_element;     /* protected with LOCK */
248   gboolean overlay_handle_set;
249   guintptr overlay_handle;
250   gboolean overlay_render_rectangle_set;
251   gint overlay_x, overlay_y, overlay_width, overlay_height;
252   gboolean overlay_handle_events_set;
253   gboolean overlay_handle_events;
254
255   /* colorbalance proxy interface */
256   GstColorBalance *colorbalance_element;
257   GList *colorbalance_channels; /* CONTRAST, BRIGHTNESS, HUE, SATURATION */
258   gint colorbalance_values[4];
259 };
260
261 struct _GstPlaySinkClass
262 {
263   GstBinClass parent_class;
264
265     gboolean (*reconfigure) (GstPlaySink * playsink);
266
267   GstSample *(*convert_sample) (GstPlaySink * playsink, GstCaps * caps);
268 };
269
270
271 static GstStaticPadTemplate audiotemplate =
272 GST_STATIC_PAD_TEMPLATE ("audio_sink",
273     GST_PAD_SINK,
274     GST_PAD_REQUEST,
275     GST_STATIC_CAPS_ANY);
276 static GstStaticPadTemplate videotemplate =
277 GST_STATIC_PAD_TEMPLATE ("video_sink",
278     GST_PAD_SINK,
279     GST_PAD_REQUEST,
280     GST_STATIC_CAPS_ANY);
281 static GstStaticPadTemplate texttemplate = GST_STATIC_PAD_TEMPLATE ("text_sink",
282     GST_PAD_SINK,
283     GST_PAD_REQUEST,
284     GST_STATIC_CAPS_ANY);
285
286 /* FIXME 0.11: Remove */
287 static GstStaticPadTemplate audiorawtemplate =
288 GST_STATIC_PAD_TEMPLATE ("audio_raw_sink",
289     GST_PAD_SINK,
290     GST_PAD_REQUEST,
291     GST_STATIC_CAPS_ANY);
292 static GstStaticPadTemplate videorawtemplate =
293 GST_STATIC_PAD_TEMPLATE ("video_raw_sink",
294     GST_PAD_SINK,
295     GST_PAD_REQUEST,
296     GST_STATIC_CAPS_ANY);
297
298
299 /* props */
300 enum
301 {
302   PROP_0,
303   PROP_FLAGS,
304   PROP_MUTE,
305   PROP_VOLUME,
306   PROP_FONT_DESC,
307   PROP_SUBTITLE_ENCODING,
308   PROP_VIS_PLUGIN,
309   PROP_SAMPLE,
310   PROP_AV_OFFSET,
311   PROP_VIDEO_SINK,
312   PROP_AUDIO_SINK,
313   PROP_TEXT_SINK,
314   PROP_SEND_EVENT_MODE,
315   PROP_LAST
316 };
317
318 /* signals */
319 enum
320 {
321   LAST_SIGNAL
322 };
323
324 static void gst_play_sink_dispose (GObject * object);
325 static void gst_play_sink_finalize (GObject * object);
326 static void gst_play_sink_set_property (GObject * object, guint prop_id,
327     const GValue * value, GParamSpec * spec);
328 static void gst_play_sink_get_property (GObject * object, guint prop_id,
329     GValue * value, GParamSpec * spec);
330
331 static GstPad *gst_play_sink_request_new_pad (GstElement * element,
332     GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
333 static void gst_play_sink_release_request_pad (GstElement * element,
334     GstPad * pad);
335 static gboolean gst_play_sink_send_event (GstElement * element,
336     GstEvent * event);
337 static GstStateChangeReturn gst_play_sink_change_state (GstElement * element,
338     GstStateChange transition);
339
340 static void gst_play_sink_handle_message (GstBin * bin, GstMessage * message);
341
342 static void notify_volume_cb (GObject * object, GParamSpec * pspec,
343     GstPlaySink * playsink);
344 static void notify_mute_cb (GObject * object, GParamSpec * pspec,
345     GstPlaySink * playsink);
346
347 static void update_av_offset (GstPlaySink * playsink);
348
349 /* static guint gst_play_sink_signals[LAST_SIGNAL] = { 0 }; */
350
351 static void gst_play_sink_overlay_init (gpointer g_iface,
352     gpointer g_iface_data);
353 static void gst_play_sink_navigation_init (gpointer g_iface,
354     gpointer g_iface_data);
355 static void gst_play_sink_colorbalance_init (gpointer g_iface,
356     gpointer g_iface_data);
357
358 static void
359 _do_init (GType type)
360 {
361   static const GInterfaceInfo svol_info = {
362     NULL, NULL, NULL
363   };
364   static const GInterfaceInfo ov_info = {
365     gst_play_sink_overlay_init,
366     NULL, NULL
367   };
368   static const GInterfaceInfo nav_info = {
369     gst_play_sink_navigation_init,
370     NULL, NULL
371   };
372   static const GInterfaceInfo col_info = {
373     gst_play_sink_colorbalance_init,
374     NULL, NULL
375   };
376
377   g_type_add_interface_static (type, GST_TYPE_STREAM_VOLUME, &svol_info);
378   g_type_add_interface_static (type, GST_TYPE_VIDEO_OVERLAY, &ov_info);
379   g_type_add_interface_static (type, GST_TYPE_NAVIGATION, &nav_info);
380   g_type_add_interface_static (type, GST_TYPE_COLOR_BALANCE, &col_info);
381 }
382
383 G_DEFINE_TYPE_WITH_CODE (GstPlaySink, gst_play_sink, GST_TYPE_BIN,
384     _do_init (g_define_type_id));
385
386 static void
387 gst_play_sink_class_init (GstPlaySinkClass * klass)
388 {
389   GObjectClass *gobject_klass;
390   GstElementClass *gstelement_klass;
391   GstBinClass *gstbin_klass;
392
393   gobject_klass = (GObjectClass *) klass;
394   gstelement_klass = (GstElementClass *) klass;
395   gstbin_klass = (GstBinClass *) klass;
396
397   gobject_klass->dispose = gst_play_sink_dispose;
398   gobject_klass->finalize = gst_play_sink_finalize;
399   gobject_klass->set_property = gst_play_sink_set_property;
400   gobject_klass->get_property = gst_play_sink_get_property;
401
402
403   /**
404    * GstPlaySink:flags
405    *
406    * Control the behaviour of playsink.
407    */
408   g_object_class_install_property (gobject_klass, PROP_FLAGS,
409       g_param_spec_flags ("flags", "Flags", "Flags to control behaviour",
410           GST_TYPE_PLAY_FLAGS, DEFAULT_FLAGS,
411           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
412
413   /**
414    * GstPlaySink:volume:
415    *
416    * Get or set the current audio stream volume. 1.0 means 100%,
417    * 0.0 means mute. This uses a linear volume scale.
418    *
419    */
420   g_object_class_install_property (gobject_klass, PROP_VOLUME,
421       g_param_spec_double ("volume", "Volume", "The audio volume, 1.0=100%",
422           0.0, VOLUME_MAX_DOUBLE, 1.0,
423           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
424   g_object_class_install_property (gobject_klass, PROP_MUTE,
425       g_param_spec_boolean ("mute", "Mute",
426           "Mute the audio channel without changing the volume", FALSE,
427           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
428   g_object_class_install_property (gobject_klass, PROP_FONT_DESC,
429       g_param_spec_string ("subtitle-font-desc",
430           "Subtitle font description",
431           "Pango font description of font "
432           "to be used for subtitle rendering", NULL,
433           G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
434   g_object_class_install_property (gobject_klass, PROP_SUBTITLE_ENCODING,
435       g_param_spec_string ("subtitle-encoding", "subtitle encoding",
436           "Encoding to assume if input subtitles are not in UTF-8 encoding. "
437           "If not set, the GST_SUBTITLE_ENCODING environment variable will "
438           "be checked for an encoding to use. If that is not set either, "
439           "ISO-8859-15 will be assumed.", NULL,
440           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
441   g_object_class_install_property (gobject_klass, PROP_VIS_PLUGIN,
442       g_param_spec_object ("vis-plugin", "Vis plugin",
443           "the visualization element to use (NULL = default)",
444           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
445   /**
446    * GstPlaySink:sample:
447    *
448    * Get the currently rendered or prerolled sample in the video sink.
449    * The #GstCaps in the sample will describe the format of the buffer.
450    */
451   g_object_class_install_property (gobject_klass, PROP_SAMPLE,
452       g_param_spec_boxed ("sample", "Sample",
453           "The last sample (NULL = no video available)",
454           GST_TYPE_SAMPLE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
455   /**
456    * GstPlaySink:av-offset:
457    *
458    * Control the synchronisation offset between the audio and video streams.
459    * Positive values make the audio ahead of the video and negative values make
460    * the audio go behind the video.
461    *
462    * Since: 0.10.30
463    */
464   g_object_class_install_property (gobject_klass, PROP_AV_OFFSET,
465       g_param_spec_int64 ("av-offset", "AV Offset",
466           "The synchronisation offset between audio and video in nanoseconds",
467           G_MININT64, G_MAXINT64, 0,
468           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
469
470   /**
471    * GstPlaySink:video-sink:
472    *
473    * Set the used video sink element. NULL will use the default sink. playsink
474    * must be in %GST_STATE_NULL
475    *
476    * Since: 0.10.36
477    */
478   g_object_class_install_property (gobject_klass, PROP_VIDEO_SINK,
479       g_param_spec_object ("video-sink", "Video Sink",
480           "the video output element to use (NULL = default sink)",
481           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
482   /**
483    * GstPlaySink:audio-sink:
484    *
485    * Set the used audio sink element. NULL will use the default sink. playsink
486    * must be in %GST_STATE_NULL
487    *
488    * Since: 0.10.36
489    */
490   g_object_class_install_property (gobject_klass, PROP_AUDIO_SINK,
491       g_param_spec_object ("audio-sink", "Audio Sink",
492           "the audio output element to use (NULL = default sink)",
493           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
494
495   /**
496    * GstPlaySink:text-sink:
497    *
498    * Set the used text sink element. NULL will use the default sink. playsink
499    * must be in %GST_STATE_NULL
500    *
501    * Since: 0.10.36
502    */
503   g_object_class_install_property (gobject_klass, PROP_TEXT_SINK,
504       g_param_spec_object ("text-sink", "Text sink",
505           "the text output element to use (NULL = default textoverlay)",
506           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
507
508   /**
509    * GstPlaySink::send-event-mode:
510    *
511    * Sets the handling method used for events received from send_event
512    * function. The default is %MODE_DEFAULT, that uses %GstBin's default
513    * handling (push the event to all internal sinks).
514    *
515    * Since: 0.10.37
516    */
517   g_object_class_install_property (gobject_klass, PROP_SEND_EVENT_MODE,
518       g_param_spec_enum ("send-event-mode", "Send event mode",
519           "How to send events received in send_event function",
520           GST_TYPE_PLAY_SINK_SEND_EVENT_MODE, MODE_DEFAULT,
521           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
522
523   g_signal_new ("reconfigure", G_TYPE_FROM_CLASS (klass),
524       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstPlaySinkClass,
525           reconfigure), NULL, NULL, g_cclosure_marshal_generic, G_TYPE_BOOLEAN,
526       0, G_TYPE_NONE);
527   /**
528    * GstPlaySink::convert-sample
529    * @playsink: a #GstPlaySink
530    * @caps: the target format of the sample
531    *
532    * Action signal to retrieve the currently playing video sample in the format
533    * specified by @caps.
534    * If @caps is %NULL, no conversion will be performed and this function is
535    * equivalent to the #GstPlaySink::sample property.
536    *
537    * Returns: a #GstSample of the current video sample converted to #caps.
538    * The caps in the sample will describe the final layout of the buffer data.
539    * %NULL is returned when no current sample can be retrieved or when the
540    * conversion failed.
541    */
542   g_signal_new ("convert-sample", G_TYPE_FROM_CLASS (klass),
543       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
544       G_STRUCT_OFFSET (GstPlaySinkClass, convert_sample), NULL, NULL,
545       g_cclosure_marshal_generic, GST_TYPE_SAMPLE, 1, GST_TYPE_CAPS);
546
547   gst_element_class_add_pad_template (gstelement_klass,
548       gst_static_pad_template_get (&audiorawtemplate));
549   gst_element_class_add_pad_template (gstelement_klass,
550       gst_static_pad_template_get (&audiotemplate));
551   gst_element_class_add_pad_template (gstelement_klass,
552       gst_static_pad_template_get (&videorawtemplate));
553   gst_element_class_add_pad_template (gstelement_klass,
554       gst_static_pad_template_get (&videotemplate));
555   gst_element_class_add_pad_template (gstelement_klass,
556       gst_static_pad_template_get (&texttemplate));
557   gst_element_class_set_details_simple (gstelement_klass, "Player Sink",
558       "Generic/Bin/Sink",
559       "Convenience sink for multiple streams",
560       "Wim Taymans <wim.taymans@gmail.com>");
561
562   gstelement_klass->change_state =
563       GST_DEBUG_FUNCPTR (gst_play_sink_change_state);
564   gstelement_klass->send_event = GST_DEBUG_FUNCPTR (gst_play_sink_send_event);
565   gstelement_klass->request_new_pad =
566       GST_DEBUG_FUNCPTR (gst_play_sink_request_new_pad);
567   gstelement_klass->release_pad =
568       GST_DEBUG_FUNCPTR (gst_play_sink_release_request_pad);
569
570   gstbin_klass->handle_message =
571       GST_DEBUG_FUNCPTR (gst_play_sink_handle_message);
572
573   klass->reconfigure = GST_DEBUG_FUNCPTR (gst_play_sink_reconfigure);
574   klass->convert_sample = GST_DEBUG_FUNCPTR (gst_play_sink_convert_sample);
575 }
576
577 static void
578 gst_play_sink_init (GstPlaySink * playsink)
579 {
580   GstColorBalanceChannel *channel;
581
582   /* init groups */
583   playsink->video_sink = NULL;
584   playsink->audio_sink = NULL;
585   playsink->visualisation = NULL;
586   playsink->text_sink = NULL;
587   playsink->volume = 1.0;
588   playsink->font_desc = NULL;
589   playsink->subtitle_encoding = NULL;
590   playsink->flags = DEFAULT_FLAGS;
591   playsink->send_event_mode = MODE_DEFAULT;
592
593   playsink->stream_synchronizer =
594       g_object_new (GST_TYPE_STREAM_SYNCHRONIZER, NULL);
595   gst_bin_add (GST_BIN_CAST (playsink),
596       GST_ELEMENT_CAST (playsink->stream_synchronizer));
597
598   g_rec_mutex_init (&playsink->lock);
599   GST_OBJECT_FLAG_SET (playsink, GST_ELEMENT_FLAG_SINK);
600
601   channel =
602       GST_COLOR_BALANCE_CHANNEL (g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL,
603           NULL));
604   channel->label = g_strdup ("CONTRAST");
605   channel->min_value = -1000;
606   channel->max_value = 1000;
607   playsink->colorbalance_channels =
608       g_list_append (playsink->colorbalance_channels, channel);
609   playsink->colorbalance_values[0] = 0;
610
611   channel =
612       GST_COLOR_BALANCE_CHANNEL (g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL,
613           NULL));
614   channel->label = g_strdup ("BRIGHTNESS");
615   channel->min_value = -1000;
616   channel->max_value = 1000;
617   playsink->colorbalance_channels =
618       g_list_append (playsink->colorbalance_channels, channel);
619   playsink->colorbalance_values[1] = 0;
620
621   channel =
622       GST_COLOR_BALANCE_CHANNEL (g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL,
623           NULL));
624   channel->label = g_strdup ("HUE");
625   channel->min_value = -1000;
626   channel->max_value = 1000;
627   playsink->colorbalance_channels =
628       g_list_append (playsink->colorbalance_channels, channel);
629   playsink->colorbalance_values[2] = 0;
630
631   channel =
632       GST_COLOR_BALANCE_CHANNEL (g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL,
633           NULL));
634   channel->label = g_strdup ("SATURATION");
635   channel->min_value = -1000;
636   channel->max_value = 1000;
637   playsink->colorbalance_channels =
638       g_list_append (playsink->colorbalance_channels, channel);
639   playsink->colorbalance_values[3] = 0;
640 }
641
642 static void
643 disconnect_chain (GstPlayAudioChain * chain, GstPlaySink * playsink)
644 {
645   if (chain) {
646     if (chain->volume)
647       g_signal_handlers_disconnect_by_func (chain->volume, notify_volume_cb,
648           playsink);
649     if (chain->mute)
650       g_signal_handlers_disconnect_by_func (chain->mute, notify_mute_cb,
651           playsink);
652   }
653 }
654
655 static void
656 free_chain (GstPlayChain * chain)
657 {
658   if (chain) {
659     if (chain->bin)
660       gst_object_unref (chain->bin);
661     g_free (chain);
662   }
663 }
664
665 static void
666 gst_play_sink_dispose (GObject * object)
667 {
668   GstPlaySink *playsink;
669
670   playsink = GST_PLAY_SINK (object);
671
672   if (playsink->audio_sink != NULL) {
673     gst_element_set_state (playsink->audio_sink, GST_STATE_NULL);
674     gst_object_unref (playsink->audio_sink);
675     playsink->audio_sink = NULL;
676   }
677   if (playsink->video_sink != NULL) {
678     gst_element_set_state (playsink->video_sink, GST_STATE_NULL);
679     gst_object_unref (playsink->video_sink);
680     playsink->video_sink = NULL;
681   }
682   if (playsink->visualisation != NULL) {
683     gst_element_set_state (playsink->visualisation, GST_STATE_NULL);
684     gst_object_unref (playsink->visualisation);
685     playsink->visualisation = NULL;
686   }
687   if (playsink->text_sink != NULL) {
688     gst_element_set_state (playsink->text_sink, GST_STATE_NULL);
689     gst_object_unref (playsink->text_sink);
690     playsink->text_sink = NULL;
691   }
692
693   free_chain ((GstPlayChain *) playsink->videodeinterlacechain);
694   playsink->videodeinterlacechain = NULL;
695   free_chain ((GstPlayChain *) playsink->videochain);
696   playsink->videochain = NULL;
697   free_chain ((GstPlayChain *) playsink->audiochain);
698   playsink->audiochain = NULL;
699   free_chain ((GstPlayChain *) playsink->vischain);
700   playsink->vischain = NULL;
701   free_chain ((GstPlayChain *) playsink->textchain);
702   playsink->textchain = NULL;
703
704   if (playsink->audio_tee_sink) {
705     gst_object_unref (playsink->audio_tee_sink);
706     playsink->audio_tee_sink = NULL;
707   }
708
709   if (playsink->audio_tee_vissrc) {
710     gst_element_release_request_pad (playsink->audio_tee,
711         playsink->audio_tee_vissrc);
712     gst_object_unref (playsink->audio_tee_vissrc);
713     playsink->audio_tee_vissrc = NULL;
714   }
715
716   if (playsink->audio_tee_asrc) {
717     gst_element_release_request_pad (playsink->audio_tee,
718         playsink->audio_tee_asrc);
719     gst_object_unref (playsink->audio_tee_asrc);
720     playsink->audio_tee_asrc = NULL;
721   }
722
723   g_free (playsink->font_desc);
724   playsink->font_desc = NULL;
725
726   g_free (playsink->subtitle_encoding);
727   playsink->subtitle_encoding = NULL;
728
729   playsink->stream_synchronizer = NULL;
730
731   g_list_foreach (playsink->colorbalance_channels, (GFunc) gst_object_unref,
732       NULL);
733   g_list_free (playsink->colorbalance_channels);
734   playsink->colorbalance_channels = NULL;
735
736   G_OBJECT_CLASS (gst_play_sink_parent_class)->dispose (object);
737 }
738
739 static void
740 gst_play_sink_finalize (GObject * object)
741 {
742   GstPlaySink *playsink;
743
744   playsink = GST_PLAY_SINK (object);
745
746   g_rec_mutex_clear (&playsink->lock);
747
748   G_OBJECT_CLASS (gst_play_sink_parent_class)->finalize (object);
749 }
750
751 void
752 gst_play_sink_set_sink (GstPlaySink * playsink, GstPlaySinkType type,
753     GstElement * sink)
754 {
755   GstElement **elem = NULL, *old = NULL;
756
757   GST_LOG ("Setting sink %" GST_PTR_FORMAT " as sink type %d", sink, type);
758
759   GST_PLAY_SINK_LOCK (playsink);
760   switch (type) {
761     case GST_PLAY_SINK_TYPE_AUDIO:
762     case GST_PLAY_SINK_TYPE_AUDIO_RAW:
763       elem = &playsink->audio_sink;
764       break;
765     case GST_PLAY_SINK_TYPE_VIDEO:
766     case GST_PLAY_SINK_TYPE_VIDEO_RAW:
767       elem = &playsink->video_sink;
768       break;
769     case GST_PLAY_SINK_TYPE_TEXT:
770       elem = &playsink->text_sink;
771       break;
772     default:
773       break;
774   }
775   if (elem) {
776     old = *elem;
777     if (sink)
778       gst_object_ref (sink);
779     *elem = sink;
780   }
781   GST_PLAY_SINK_UNLOCK (playsink);
782
783   if (old) {
784     if (old != sink)
785       gst_element_set_state (old, GST_STATE_NULL);
786     gst_object_unref (old);
787   }
788 }
789
790 GstElement *
791 gst_play_sink_get_sink (GstPlaySink * playsink, GstPlaySinkType type)
792 {
793   GstElement *result = NULL;
794   GstElement *elem = NULL, *chainp = NULL;
795
796   GST_PLAY_SINK_LOCK (playsink);
797   switch (type) {
798     case GST_PLAY_SINK_TYPE_AUDIO:
799     case GST_PLAY_SINK_TYPE_AUDIO_RAW:
800     {
801       GstPlayAudioChain *chain;
802       if ((chain = (GstPlayAudioChain *) playsink->audiochain))
803         chainp = chain->sink;
804       elem = playsink->audio_sink;
805       break;
806     }
807     case GST_PLAY_SINK_TYPE_VIDEO:
808     case GST_PLAY_SINK_TYPE_VIDEO_RAW:
809     {
810       GstPlayVideoChain *chain;
811       if ((chain = (GstPlayVideoChain *) playsink->videochain))
812         chainp = chain->sink;
813       elem = playsink->video_sink;
814       break;
815     }
816     case GST_PLAY_SINK_TYPE_TEXT:
817     {
818       GstPlayTextChain *chain;
819       if ((chain = (GstPlayTextChain *) playsink->textchain))
820         chainp = chain->sink;
821       elem = playsink->text_sink;
822       break;
823     }
824     default:
825       break;
826   }
827   if (chainp) {
828     /* we have an active chain with a sink, get the sink */
829     result = gst_object_ref (chainp);
830   }
831   /* nothing found, return last configured sink */
832   if (result == NULL && elem)
833     result = gst_object_ref (elem);
834   GST_PLAY_SINK_UNLOCK (playsink);
835
836   return result;
837 }
838
839 static GstPadProbeReturn
840 gst_play_sink_vis_blocked (GstPad * tee_pad, GstPadProbeInfo * info,
841     gpointer user_data)
842 {
843   GstPlaySink *playsink;
844   GstPlayVisChain *chain;
845
846   playsink = GST_PLAY_SINK (user_data);
847
848   GST_PLAY_SINK_LOCK (playsink);
849   GST_DEBUG_OBJECT (playsink, "vis pad blocked");
850   /* now try to change the plugin in the running vis chain */
851   if (!(chain = (GstPlayVisChain *) playsink->vischain))
852     goto done;
853
854   /* unlink the old plugin and unghost the pad */
855   gst_pad_unlink (chain->blockpad, chain->vissinkpad);
856   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (chain->srcpad), NULL);
857
858   /* set the old plugin to NULL and remove */
859   gst_element_set_state (chain->vis, GST_STATE_NULL);
860   gst_bin_remove (GST_BIN_CAST (chain->chain.bin), chain->vis);
861
862   /* add new plugin and set state to playing */
863   chain->vis = playsink->visualisation;
864   gst_bin_add (GST_BIN_CAST (chain->chain.bin), chain->vis);
865   gst_element_set_state (chain->vis, GST_STATE_PLAYING);
866
867   /* get pads */
868   chain->vissinkpad = gst_element_get_static_pad (chain->vis, "sink");
869   chain->vissrcpad = gst_element_get_static_pad (chain->vis, "src");
870
871   /* link pads */
872   gst_pad_link_full (chain->blockpad, chain->vissinkpad,
873       GST_PAD_LINK_CHECK_NOTHING);
874   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (chain->srcpad),
875       chain->vissrcpad);
876
877 done:
878   GST_PLAY_SINK_UNLOCK (playsink);
879
880   /* remove the probe and unblock the pad */
881   return GST_PAD_PROBE_REMOVE;
882 }
883
884 void
885 gst_play_sink_set_vis_plugin (GstPlaySink * playsink, GstElement * vis)
886 {
887   GstPlayVisChain *chain;
888
889   /* setting NULL means creating the default vis plugin */
890   if (vis == NULL)
891     vis = gst_element_factory_make ("goom", "vis");
892
893   /* simply return if we don't have a vis plugin here */
894   if (vis == NULL)
895     return;
896
897   GST_PLAY_SINK_LOCK (playsink);
898   /* first store the new vis */
899   if (playsink->visualisation)
900     gst_object_unref (playsink->visualisation);
901   /* take ownership */
902   gst_object_ref_sink (vis);
903   playsink->visualisation = vis;
904
905   /* now try to change the plugin in the running vis chain, if we have no chain,
906    * we don't bother, any future vis chain will be created with the new vis
907    * plugin. */
908   if (!(chain = (GstPlayVisChain *) playsink->vischain))
909     goto done;
910
911   /* block the pad, the next time the callback is called we can change the
912    * visualisation. It's possible that this never happens or that the pad was
913    * already blocked. If the callback never happens, we don't have new data so
914    * we don't need the new vis plugin. If the pad was already blocked, the
915    * function returns FALSE but the previous pad block will do the right thing
916    * anyway. */
917   GST_DEBUG_OBJECT (playsink, "blocking vis pad");
918   gst_pad_add_probe (chain->blockpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
919       gst_play_sink_vis_blocked, playsink, NULL);
920 done:
921   GST_PLAY_SINK_UNLOCK (playsink);
922
923   return;
924 }
925
926 GstElement *
927 gst_play_sink_get_vis_plugin (GstPlaySink * playsink)
928 {
929   GstElement *result = NULL;
930   GstPlayVisChain *chain;
931
932   GST_PLAY_SINK_LOCK (playsink);
933   if ((chain = (GstPlayVisChain *) playsink->vischain)) {
934     /* we have an active chain, get the sink */
935     if (chain->vis)
936       result = gst_object_ref (chain->vis);
937   }
938   /* nothing found, return last configured sink */
939   if (result == NULL && playsink->visualisation)
940     result = gst_object_ref (playsink->visualisation);
941   GST_PLAY_SINK_UNLOCK (playsink);
942
943   return result;
944 }
945
946 void
947 gst_play_sink_set_volume (GstPlaySink * playsink, gdouble volume)
948 {
949   GstPlayAudioChain *chain;
950
951   GST_PLAY_SINK_LOCK (playsink);
952   playsink->volume = volume;
953   chain = (GstPlayAudioChain *) playsink->audiochain;
954   if (chain && chain->volume) {
955     GST_LOG_OBJECT (playsink, "elements: volume=%" GST_PTR_FORMAT ", mute=%"
956         GST_PTR_FORMAT "; new volume=%.03f, mute=%d", chain->volume,
957         chain->mute, volume, playsink->mute);
958     /* if there is a mute element or we are not muted, set the volume */
959     if (chain->mute || !playsink->mute)
960       g_object_set (chain->volume, "volume", volume, NULL);
961   } else {
962     GST_LOG_OBJECT (playsink, "no volume element");
963     playsink->volume_changed = TRUE;
964   }
965   GST_PLAY_SINK_UNLOCK (playsink);
966 }
967
968 gdouble
969 gst_play_sink_get_volume (GstPlaySink * playsink)
970 {
971   gdouble result;
972   GstPlayAudioChain *chain;
973
974   GST_PLAY_SINK_LOCK (playsink);
975   chain = (GstPlayAudioChain *) playsink->audiochain;
976   result = playsink->volume;
977   if (chain && chain->volume) {
978     if (chain->mute || !playsink->mute) {
979       g_object_get (chain->volume, "volume", &result, NULL);
980       playsink->volume = result;
981     }
982   }
983   GST_PLAY_SINK_UNLOCK (playsink);
984
985   return result;
986 }
987
988 void
989 gst_play_sink_set_mute (GstPlaySink * playsink, gboolean mute)
990 {
991   GstPlayAudioChain *chain;
992
993   GST_PLAY_SINK_LOCK (playsink);
994   playsink->mute = mute;
995   chain = (GstPlayAudioChain *) playsink->audiochain;
996   if (chain) {
997     if (chain->mute) {
998       g_object_set (chain->mute, "mute", mute, NULL);
999     } else if (chain->volume) {
1000       if (mute)
1001         g_object_set (chain->volume, "volume", (gdouble) 0.0, NULL);
1002       else
1003         g_object_set (chain->volume, "volume", (gdouble) playsink->volume,
1004             NULL);
1005     }
1006   } else {
1007     playsink->mute_changed = TRUE;
1008   }
1009   GST_PLAY_SINK_UNLOCK (playsink);
1010 }
1011
1012 gboolean
1013 gst_play_sink_get_mute (GstPlaySink * playsink)
1014 {
1015   gboolean result;
1016   GstPlayAudioChain *chain;
1017
1018   GST_PLAY_SINK_LOCK (playsink);
1019   chain = (GstPlayAudioChain *) playsink->audiochain;
1020   if (chain && chain->mute) {
1021     g_object_get (chain->mute, "mute", &result, NULL);
1022     playsink->mute = result;
1023   } else {
1024     result = playsink->mute;
1025   }
1026   GST_PLAY_SINK_UNLOCK (playsink);
1027
1028   return result;
1029 }
1030
1031 static void
1032 post_missing_element_message (GstPlaySink * playsink, const gchar * name)
1033 {
1034   GstMessage *msg;
1035
1036   msg = gst_missing_element_message_new (GST_ELEMENT_CAST (playsink), name);
1037   gst_element_post_message (GST_ELEMENT_CAST (playsink), msg);
1038 }
1039
1040 static gboolean
1041 add_chain (GstPlayChain * chain, gboolean add)
1042 {
1043   if (chain->added == add)
1044     return TRUE;
1045
1046   if (add)
1047     gst_bin_add (GST_BIN_CAST (chain->playsink), chain->bin);
1048   else {
1049     gst_bin_remove (GST_BIN_CAST (chain->playsink), chain->bin);
1050     /* we don't want to lose our sink status */
1051     GST_OBJECT_FLAG_SET (chain->playsink, GST_ELEMENT_FLAG_SINK);
1052   }
1053
1054   chain->added = add;
1055
1056   return TRUE;
1057 }
1058
1059 static gboolean
1060 activate_chain (GstPlayChain * chain, gboolean activate)
1061 {
1062   GstState state;
1063
1064   if (chain->activated == activate)
1065     return TRUE;
1066
1067   GST_OBJECT_LOCK (chain->playsink);
1068   state = GST_STATE_TARGET (chain->playsink);
1069   GST_OBJECT_UNLOCK (chain->playsink);
1070
1071   if (activate)
1072     gst_element_set_state (chain->bin, state);
1073   else
1074     gst_element_set_state (chain->bin, GST_STATE_NULL);
1075
1076   chain->activated = activate;
1077
1078   return TRUE;
1079 }
1080
1081 static gboolean
1082 element_is_sink (GstElement * element)
1083 {
1084   gboolean is_sink;
1085
1086   GST_OBJECT_LOCK (element);
1087   is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
1088   GST_OBJECT_UNLOCK (element);
1089
1090   GST_DEBUG_OBJECT (element, "is a sink: %s", (is_sink) ? "yes" : "no");
1091   return is_sink;
1092 }
1093
1094 static gboolean
1095 element_has_property (GstElement * element, const gchar * pname, GType type)
1096 {
1097   GParamSpec *pspec;
1098
1099   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), pname);
1100
1101   if (pspec == NULL) {
1102     GST_DEBUG_OBJECT (element, "no %s property", pname);
1103     return FALSE;
1104   }
1105
1106   if (type == G_TYPE_INVALID || type == pspec->value_type ||
1107       g_type_is_a (pspec->value_type, type)) {
1108     GST_DEBUG_OBJECT (element, "has %s property of type %s", pname,
1109         (type == G_TYPE_INVALID) ? "any type" : g_type_name (type));
1110     return TRUE;
1111   }
1112
1113   GST_WARNING_OBJECT (element, "has %s property, but property is of type %s "
1114       "and we expected it to be of type %s", pname,
1115       g_type_name (pspec->value_type), g_type_name (type));
1116
1117   return FALSE;
1118 }
1119
1120 typedef struct
1121 {
1122   const gchar *prop_name;
1123   GType prop_type;
1124   gboolean need_sink;
1125 } FindPropertyHelper;
1126
1127 static gint
1128 find_property (const GValue * item, FindPropertyHelper * helper)
1129 {
1130   GstElement *element = g_value_get_object (item);
1131   if (helper->need_sink && !element_is_sink (element)) {
1132     return 1;
1133   }
1134
1135   if (!element_has_property (element, helper->prop_name, helper->prop_type)) {
1136     return 1;
1137   }
1138
1139   GST_INFO_OBJECT (element, "found %s with %s property", helper->prop_name,
1140       (helper->need_sink) ? "sink" : "element");
1141   return 0;                     /* keep it */
1142 }
1143
1144 /* FIXME: why not move these functions into core? */
1145 /* find a sink in the hierarchy with a property named @name. This function does
1146  * not increase the refcount of the returned object and thus remains valid as
1147  * long as the bin is valid. */
1148 static GstElement *
1149 gst_play_sink_find_property_sinks (GstPlaySink * playsink, GstElement * obj,
1150     const gchar * name, GType expected_type)
1151 {
1152   GstElement *result = NULL;
1153   GstIterator *it;
1154
1155   if (element_has_property (obj, name, expected_type)) {
1156     result = obj;
1157   } else if (GST_IS_BIN (obj)) {
1158     gboolean found;
1159     GValue item = { 0, };
1160     FindPropertyHelper helper = { name, expected_type, TRUE };
1161
1162     it = gst_bin_iterate_recurse (GST_BIN_CAST (obj));
1163     found = gst_iterator_find_custom (it,
1164         (GCompareFunc) find_property, &item, &helper);
1165     gst_iterator_free (it);
1166     if (found) {
1167       result = g_value_get_object (&item);
1168       /* we don't need the extra ref */
1169       g_value_unset (&item);
1170     }
1171   }
1172   return result;
1173 }
1174
1175 /* find an object in the hierarchy with a property named @name */
1176 static GstElement *
1177 gst_play_sink_find_property (GstPlaySink * playsink, GstElement * obj,
1178     const gchar * name, GType expected_type)
1179 {
1180   GstElement *result = NULL;
1181   GstIterator *it;
1182
1183   if (GST_IS_BIN (obj)) {
1184     gboolean found;
1185     GValue item = { 0, };
1186     FindPropertyHelper helper = { name, expected_type, FALSE };
1187
1188     it = gst_bin_iterate_recurse (GST_BIN_CAST (obj));
1189     found = gst_iterator_find_custom (it,
1190         (GCompareFunc) find_property, &item, &helper);
1191     gst_iterator_free (it);
1192     if (found)
1193       result = g_value_dup_object (&item);
1194     g_value_unset (&item);
1195   } else {
1196     if (element_has_property (obj, name, expected_type)) {
1197       result = obj;
1198       gst_object_ref (obj);
1199     }
1200   }
1201   return result;
1202 }
1203
1204 static void
1205 do_async_start (GstPlaySink * playsink)
1206 {
1207   GstMessage *message;
1208
1209   if (!playsink->need_async_start) {
1210     GST_INFO_OBJECT (playsink, "no async_start needed");
1211     return;
1212   }
1213
1214   playsink->async_pending = TRUE;
1215
1216   GST_INFO_OBJECT (playsink, "Sending async_start message");
1217   message = gst_message_new_async_start (GST_OBJECT_CAST (playsink));
1218   GST_BIN_CLASS (gst_play_sink_parent_class)->handle_message (GST_BIN_CAST
1219       (playsink), message);
1220 }
1221
1222 static void
1223 do_async_done (GstPlaySink * playsink)
1224 {
1225   GstMessage *message;
1226
1227   if (playsink->async_pending) {
1228     GST_INFO_OBJECT (playsink, "Sending async_done message");
1229     message = gst_message_new_async_done (GST_OBJECT_CAST (playsink), FALSE);
1230     GST_BIN_CLASS (gst_play_sink_parent_class)->handle_message (GST_BIN_CAST
1231         (playsink), message);
1232
1233     playsink->async_pending = FALSE;
1234   }
1235
1236   playsink->need_async_start = FALSE;
1237 }
1238
1239 /* try to change the state of an element. This function returns the element when
1240  * the state change could be performed. When this function returns NULL an error
1241  * occured and the element is unreffed if @unref is TRUE. */
1242 static GstElement *
1243 try_element (GstPlaySink * playsink, GstElement * element, gboolean unref)
1244 {
1245   GstStateChangeReturn ret;
1246
1247   if (element) {
1248     ret = gst_element_set_state (element, GST_STATE_READY);
1249     if (ret == GST_STATE_CHANGE_FAILURE) {
1250       GST_DEBUG_OBJECT (playsink, "failed state change..");
1251       gst_element_set_state (element, GST_STATE_NULL);
1252       if (unref)
1253         gst_object_unref (element);
1254       element = NULL;
1255     }
1256   }
1257   return element;
1258 }
1259
1260 /* make the element (bin) that contains the elements needed to perform
1261  * video display. Only used for *raw* video streams.
1262  *
1263  *  +------------------------------------------------------------+
1264  *  | vbin                                                       |
1265  *  |      +-------+   +----------+   +----------+   +---------+ |
1266  *  |      | queue |   |colorspace|   |videoscale|   |videosink| |
1267  *  |   +-sink    src-sink       src-sink       src-sink       | |
1268  *  |   |  +-------+   +----------+   +----------+   +---------+ |
1269  * sink-+                                                        |
1270  *  +------------------------------------------------------------+
1271  *
1272  */
1273 static GstPlayVideoDeinterlaceChain *
1274 gen_video_deinterlace_chain (GstPlaySink * playsink)
1275 {
1276   GstPlayVideoDeinterlaceChain *chain;
1277   GstBin *bin;
1278   GstPad *pad;
1279   GstElement *head = NULL, *prev = NULL;
1280
1281   chain = g_new0 (GstPlayVideoDeinterlaceChain, 1);
1282   chain->chain.playsink = playsink;
1283
1284   GST_DEBUG_OBJECT (playsink, "making video deinterlace chain %p", chain);
1285
1286   /* create a bin to hold objects, as we create them we add them to this bin so
1287    * that when something goes wrong we only need to unref the bin */
1288   chain->chain.bin = gst_bin_new ("vdbin");
1289   bin = GST_BIN_CAST (chain->chain.bin);
1290   gst_object_ref_sink (bin);
1291
1292   GST_DEBUG_OBJECT (playsink, "creating " COLORSPACE);
1293   chain->conv = gst_element_factory_make (COLORSPACE, "vdconv");
1294   if (chain->conv == NULL) {
1295     post_missing_element_message (playsink, COLORSPACE);
1296     GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1297         (_("Missing element '%s' - check your GStreamer installation."),
1298             COLORSPACE), ("video rendering might fail"));
1299   } else {
1300     gst_bin_add (bin, chain->conv);
1301     head = chain->conv;
1302     prev = chain->conv;
1303   }
1304
1305   GST_DEBUG_OBJECT (playsink, "creating deinterlace");
1306   chain->deinterlace = gst_element_factory_make ("deinterlace", "deinterlace");
1307   if (chain->deinterlace == NULL) {
1308     post_missing_element_message (playsink, "deinterlace");
1309     GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1310         (_("Missing element '%s' - check your GStreamer installation."),
1311             "deinterlace"), ("deinterlacing won't work"));
1312   } else {
1313     gst_bin_add (bin, chain->deinterlace);
1314     if (prev) {
1315       if (!gst_element_link_pads_full (prev, "src", chain->deinterlace, "sink",
1316               GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
1317         goto link_failed;
1318     } else {
1319       head = chain->deinterlace;
1320     }
1321     prev = chain->deinterlace;
1322   }
1323
1324   if (head) {
1325     pad = gst_element_get_static_pad (head, "sink");
1326     chain->sinkpad = gst_ghost_pad_new ("sink", pad);
1327     gst_object_unref (pad);
1328   } else {
1329     chain->sinkpad = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK);
1330   }
1331
1332   if (prev) {
1333     pad = gst_element_get_static_pad (prev, "src");
1334     chain->srcpad = gst_ghost_pad_new ("src", pad);
1335     gst_object_unref (pad);
1336   } else {
1337     chain->srcpad = gst_ghost_pad_new ("src", chain->sinkpad);
1338   }
1339
1340   gst_element_add_pad (chain->chain.bin, chain->sinkpad);
1341   gst_element_add_pad (chain->chain.bin, chain->srcpad);
1342
1343   return chain;
1344
1345 link_failed:
1346   {
1347     GST_ELEMENT_ERROR (playsink, CORE, PAD,
1348         (NULL), ("Failed to configure the video deinterlace chain."));
1349     free_chain ((GstPlayChain *) chain);
1350     return NULL;
1351   }
1352 }
1353
1354 static gboolean
1355 is_valid_color_balance_element (GstColorBalance * bal)
1356 {
1357   gboolean have_brightness = FALSE;
1358   gboolean have_contrast = FALSE;
1359   gboolean have_hue = FALSE;
1360   gboolean have_saturation = FALSE;
1361   const GList *channels, *l;
1362
1363   channels = gst_color_balance_list_channels (bal);
1364   for (l = channels; l; l = l->next) {
1365     GstColorBalanceChannel *ch = l->data;
1366
1367     if (g_strrstr (ch->label, "BRIGHTNESS"))
1368       have_brightness = TRUE;
1369     else if (g_strrstr (ch->label, "CONTRAST"))
1370       have_contrast = TRUE;
1371     else if (g_strrstr (ch->label, "HUE"))
1372       have_hue = TRUE;
1373     else if (g_strrstr (ch->label, "SATURATION"))
1374       have_saturation = TRUE;
1375   }
1376
1377   return have_brightness && have_contrast && have_hue && have_saturation;
1378 }
1379
1380 static void
1381 iterate_color_balance_elements (const GValue * item, gpointer user_data)
1382 {
1383   gboolean valid;
1384   GstColorBalance *cb, **cb_out = user_data;
1385
1386   cb = GST_COLOR_BALANCE (g_value_get_object (item));
1387   valid = is_valid_color_balance_element (cb);
1388   if (valid) {
1389     if (*cb_out
1390         && gst_color_balance_get_balance_type (*cb_out) ==
1391         GST_COLOR_BALANCE_SOFTWARE) {
1392       gst_object_unref (*cb_out);
1393       *cb_out = GST_COLOR_BALANCE (gst_object_ref (cb));
1394     } else if (!*cb_out) {
1395       *cb_out = GST_COLOR_BALANCE (gst_object_ref (cb));
1396     }
1397   }
1398 }
1399
1400 static GstColorBalance *
1401 find_color_balance_element (GstElement * element)
1402 {
1403   GstIterator *it;
1404   GstColorBalance *cb = NULL;
1405
1406   if (GST_IS_COLOR_BALANCE (element)
1407       && is_valid_color_balance_element (GST_COLOR_BALANCE (element)))
1408     return GST_COLOR_BALANCE (gst_object_ref (element));
1409   else if (!GST_IS_BIN (element))
1410     return FALSE;
1411
1412   it = gst_bin_iterate_all_by_interface (GST_BIN (element),
1413       GST_TYPE_COLOR_BALANCE);
1414   while (gst_iterator_foreach (it, iterate_color_balance_elements,
1415           &cb) == GST_ITERATOR_RESYNC)
1416     gst_iterator_resync (it);
1417   gst_iterator_free (it);
1418
1419   return cb;
1420 }
1421
1422 static void
1423 colorbalance_value_changed_cb (GstColorBalance * balance,
1424     GstColorBalanceChannel * channel, gint value, GstPlaySink * playsink)
1425 {
1426   GList *l;
1427   gint i;
1428
1429   for (i = 0, l = playsink->colorbalance_channels; l; l = l->next, i++) {
1430     GstColorBalanceChannel *proxy = l->data;
1431
1432     if (g_strrstr (channel->label, proxy->label)) {
1433       gdouble new_val;
1434
1435       /* Convert to [0, 1] range */
1436       new_val =
1437           ((gdouble) value -
1438           (gdouble) channel->min_value) / ((gdouble) channel->max_value -
1439           (gdouble) channel->min_value);
1440       /* Convert to proxy range */
1441       new_val =
1442           proxy->min_value + new_val * ((gdouble) proxy->max_value -
1443           (gdouble) proxy->min_value);
1444       playsink->colorbalance_values[i] = (gint) (0.5 + new_val);
1445
1446       gst_color_balance_value_changed (GST_COLOR_BALANCE (playsink), proxy,
1447           playsink->colorbalance_values[i]);
1448       break;
1449     }
1450   }
1451 }
1452
1453 static void
1454 update_colorbalance (GstPlaySink * playsink)
1455 {
1456   GstColorBalance *balance = NULL;
1457   GList *l;
1458   gint i;
1459
1460   GST_OBJECT_LOCK (playsink);
1461   if (playsink->colorbalance_element) {
1462     balance =
1463         GST_COLOR_BALANCE (gst_object_ref (playsink->colorbalance_element));
1464   }
1465   GST_OBJECT_UNLOCK (playsink);
1466   if (!balance)
1467     return;
1468
1469   g_signal_handlers_block_by_func (balance,
1470       G_CALLBACK (colorbalance_value_changed_cb), playsink);
1471
1472   for (i = 0, l = playsink->colorbalance_channels; l; l = l->next, i++) {
1473     GstColorBalanceChannel *proxy = l->data;
1474     GstColorBalanceChannel *channel = NULL;
1475     const GList *channels, *k;
1476
1477     channels = gst_color_balance_list_channels (balance);
1478     for (k = channels; k; k = k->next) {
1479       GstColorBalanceChannel *tmp = k->data;
1480
1481       if (g_strrstr (tmp->label, proxy->label)) {
1482         channel = tmp;
1483         break;
1484       }
1485     }
1486
1487     g_assert (channel);
1488
1489     gst_color_balance_set_value (balance, channel,
1490         playsink->colorbalance_values[i]);
1491   }
1492
1493   g_signal_handlers_unblock_by_func (balance,
1494       G_CALLBACK (colorbalance_value_changed_cb), playsink);
1495 }
1496
1497 /* make the element (bin) that contains the elements needed to perform
1498  * video display.
1499  *
1500  *  +------------------------------------------------------------+
1501  *  | vbin                                                       |
1502  *  |      +-------+   +----------+   +----------+   +---------+ |
1503  *  |      | queue |   |colorspace|   |videoscale|   |videosink| |
1504  *  |   +-sink    src-sink       src-sink       src-sink       | |
1505  *  |   |  +-------+   +----------+   +----------+   +---------+ |
1506  * sink-+                                                        |
1507  *  +------------------------------------------------------------+
1508  *
1509  */
1510 static GstPlayVideoChain *
1511 gen_video_chain (GstPlaySink * playsink, gboolean raw, gboolean async)
1512 {
1513   GstPlayVideoChain *chain;
1514   GstBin *bin;
1515   GstPad *pad;
1516   GstElement *head = NULL, *prev = NULL, *elem = NULL;
1517
1518   chain = g_new0 (GstPlayVideoChain, 1);
1519   chain->chain.playsink = playsink;
1520   chain->chain.raw = raw;
1521
1522   GST_DEBUG_OBJECT (playsink, "making video chain %p", chain);
1523
1524   if (playsink->video_sink) {
1525     GST_DEBUG_OBJECT (playsink, "trying configured videosink");
1526     chain->sink = try_element (playsink, playsink->video_sink, FALSE);
1527   } else {
1528     /* only try fallback if no specific sink was chosen */
1529     if (chain->sink == NULL) {
1530       GST_DEBUG_OBJECT (playsink, "trying autovideosink");
1531       elem = gst_element_factory_make ("autovideosink", "videosink");
1532       chain->sink = try_element (playsink, elem, TRUE);
1533     }
1534     if (chain->sink == NULL) {
1535       /* if default sink from config.h is different then try it too */
1536       if (strcmp (DEFAULT_VIDEOSINK, "autovideosink")) {
1537         GST_DEBUG_OBJECT (playsink, "trying " DEFAULT_VIDEOSINK);
1538         elem = gst_element_factory_make (DEFAULT_VIDEOSINK, "videosink");
1539         chain->sink = try_element (playsink, elem, TRUE);
1540       }
1541     }
1542     if (chain->sink)
1543       playsink->video_sink = gst_object_ref (chain->sink);
1544   }
1545   if (chain->sink == NULL)
1546     goto no_sinks;
1547   head = chain->sink;
1548
1549   /* if we can disable async behaviour of the sink, we can avoid adding a
1550    * queue for the audio chain. */
1551   elem =
1552       gst_play_sink_find_property_sinks (playsink, chain->sink, "async",
1553       G_TYPE_BOOLEAN);
1554   if (elem) {
1555     GST_DEBUG_OBJECT (playsink, "setting async property to %d on element %s",
1556         async, GST_ELEMENT_NAME (elem));
1557     g_object_set (elem, "async", async, NULL);
1558     chain->async = async;
1559   } else {
1560     GST_DEBUG_OBJECT (playsink, "no async property on the sink");
1561     chain->async = TRUE;
1562   }
1563
1564   /* Make sure the aspect ratio is kept */
1565   elem =
1566       gst_play_sink_find_property_sinks (playsink, chain->sink,
1567       "force-aspect-ratio", G_TYPE_BOOLEAN);
1568   if (elem)
1569     g_object_set (elem, "force-aspect-ratio", TRUE, NULL);
1570
1571   /* find ts-offset element */
1572   gst_object_replace ((GstObject **) & chain->ts_offset, (GstObject *)
1573       gst_play_sink_find_property_sinks (playsink, chain->sink, "ts-offset",
1574           G_TYPE_INT64));
1575
1576   /* create a bin to hold objects, as we create them we add them to this bin so
1577    * that when something goes wrong we only need to unref the bin */
1578   chain->chain.bin = gst_bin_new ("vbin");
1579   bin = GST_BIN_CAST (chain->chain.bin);
1580   gst_object_ref_sink (bin);
1581   gst_bin_add (bin, chain->sink);
1582
1583   /* Get the VideoOverlay element */
1584   {
1585     GstVideoOverlay *overlay = NULL;
1586
1587     GST_OBJECT_LOCK (playsink);
1588     if (playsink->overlay_element)
1589       gst_object_unref (playsink->overlay_element);
1590     playsink->overlay_element =
1591         GST_VIDEO_OVERLAY (gst_bin_get_by_interface (GST_BIN (chain->chain.bin),
1592             GST_TYPE_VIDEO_OVERLAY));
1593     if (playsink->overlay_element)
1594       overlay = GST_VIDEO_OVERLAY (gst_object_ref (playsink->overlay_element));
1595     GST_OBJECT_UNLOCK (playsink);
1596
1597     if (overlay) {
1598       if (playsink->overlay_handle_set)
1599         gst_video_overlay_set_window_handle (overlay, playsink->overlay_handle);
1600       if (playsink->overlay_handle_events_set)
1601         gst_video_overlay_handle_events (overlay,
1602             playsink->overlay_handle_events);
1603       if (playsink->overlay_render_rectangle_set)
1604         gst_video_overlay_set_render_rectangle (overlay,
1605             playsink->overlay_x, playsink->overlay_y,
1606             playsink->overlay_width, playsink->overlay_height);
1607       gst_object_unref (overlay);
1608     }
1609   }
1610
1611   /* decouple decoder from sink, this improves playback quite a lot since the
1612    * decoder can continue while the sink blocks for synchronisation. We don't
1613    * need a lot of buffers as this consumes a lot of memory and we don't want
1614    * too little because else we would be context switching too quickly. */
1615   chain->queue = gst_element_factory_make ("queue", "vqueue");
1616   if (chain->queue == NULL) {
1617     post_missing_element_message (playsink, "queue");
1618     GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1619         (_("Missing element '%s' - check your GStreamer installation."),
1620             "queue"), ("video rendering might be suboptimal"));
1621     head = chain->sink;
1622     prev = NULL;
1623   } else {
1624     g_object_set (G_OBJECT (chain->queue), "max-size-buffers", 3,
1625         "max-size-bytes", 0, "max-size-time", (gint64) 0, "silent", TRUE, NULL);
1626     gst_bin_add (bin, chain->queue);
1627     head = prev = chain->queue;
1628   }
1629
1630   GST_OBJECT_LOCK (playsink);
1631   if (playsink->colorbalance_element) {
1632     g_signal_handlers_disconnect_by_func (playsink->colorbalance_element,
1633         G_CALLBACK (colorbalance_value_changed_cb), playsink);
1634     gst_object_unref (playsink->colorbalance_element);
1635   }
1636   playsink->colorbalance_element = find_color_balance_element (chain->sink);
1637   GST_OBJECT_UNLOCK (playsink);
1638
1639   if (!(playsink->flags & GST_PLAY_FLAG_NATIVE_VIDEO)
1640       || (!playsink->colorbalance_element
1641           && (playsink->flags & GST_PLAY_FLAG_SOFT_COLORBALANCE))) {
1642     gboolean use_converters = !(playsink->flags & GST_PLAY_FLAG_NATIVE_VIDEO);
1643     gboolean use_balance = !playsink->colorbalance_element
1644         && (playsink->flags & GST_PLAY_FLAG_SOFT_COLORBALANCE);
1645
1646     GST_DEBUG_OBJECT (playsink, "creating videoconverter");
1647     chain->conv =
1648         g_object_new (GST_TYPE_PLAY_SINK_VIDEO_CONVERT, "name", "vconv",
1649         "use-converters", use_converters, "use-balance", use_balance, NULL);
1650
1651     GST_OBJECT_LOCK (playsink);
1652     if (use_balance && GST_PLAY_SINK_VIDEO_CONVERT (chain->conv)->balance)
1653       playsink->colorbalance_element =
1654           GST_COLOR_BALANCE (gst_object_ref (GST_PLAY_SINK_VIDEO_CONVERT
1655               (chain->conv)->balance));
1656     GST_OBJECT_UNLOCK (playsink);
1657
1658     gst_bin_add (bin, chain->conv);
1659     if (prev) {
1660       if (!gst_element_link_pads_full (prev, "src", chain->conv, "sink",
1661               GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
1662         goto link_failed;
1663     } else {
1664       head = chain->conv;
1665     }
1666     prev = chain->conv;
1667   }
1668
1669   update_colorbalance (playsink);
1670
1671   if (prev) {
1672     GST_DEBUG_OBJECT (playsink, "linking to sink");
1673     if (!gst_element_link_pads_full (prev, "src", chain->sink, NULL,
1674             GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
1675       goto link_failed;
1676   }
1677
1678   pad = gst_element_get_static_pad (head, "sink");
1679   chain->sinkpad = gst_ghost_pad_new ("sink", pad);
1680   gst_object_unref (pad);
1681
1682   gst_element_add_pad (chain->chain.bin, chain->sinkpad);
1683
1684   return chain;
1685
1686   /* ERRORS */
1687 no_sinks:
1688   {
1689     if (!elem && !playsink->video_sink) {
1690       post_missing_element_message (playsink, "autovideosink");
1691       if (strcmp (DEFAULT_VIDEOSINK, "autovideosink")) {
1692         post_missing_element_message (playsink, DEFAULT_VIDEOSINK);
1693         GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
1694             (_("Both autovideosink and %s elements are missing."),
1695                 DEFAULT_VIDEOSINK), (NULL));
1696       } else {
1697         GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
1698             (_("The autovideosink element is missing.")), (NULL));
1699       }
1700     } else {
1701       if (playsink->video_sink) {
1702         GST_ELEMENT_ERROR (playsink, CORE, STATE_CHANGE,
1703             (_("Configured videosink %s is not working."),
1704                 GST_ELEMENT_NAME (playsink->video_sink)), (NULL));
1705       } else if (strcmp (DEFAULT_VIDEOSINK, "autovideosink")) {
1706         GST_ELEMENT_ERROR (playsink, CORE, STATE_CHANGE,
1707             (_("Both autovideosink and %s elements are not working."),
1708                 DEFAULT_VIDEOSINK), (NULL));
1709       } else {
1710         GST_ELEMENT_ERROR (playsink, CORE, STATE_CHANGE,
1711             (_("The autovideosink element is not working.")), (NULL));
1712       }
1713     }
1714     free_chain ((GstPlayChain *) chain);
1715     return NULL;
1716   }
1717
1718 link_failed:
1719   {
1720     GST_ELEMENT_ERROR (playsink, CORE, PAD,
1721         (NULL), ("Failed to configure the video sink."));
1722     /* checking sink made it READY */
1723     gst_element_set_state (chain->sink, GST_STATE_NULL);
1724     /* Remove chain from the bin to allow reuse later */
1725     gst_bin_remove (bin, chain->sink);
1726     free_chain ((GstPlayChain *) chain);
1727     return NULL;
1728   }
1729 }
1730
1731 static gboolean
1732 setup_video_chain (GstPlaySink * playsink, gboolean raw, gboolean async)
1733 {
1734   GstElement *elem;
1735   GstPlayVideoChain *chain;
1736   GstStateChangeReturn ret;
1737
1738   chain = playsink->videochain;
1739
1740   chain->chain.raw = raw;
1741
1742   /* if the chain was active we don't do anything */
1743   if (GST_PLAY_CHAIN (chain)->activated == TRUE)
1744     return TRUE;
1745
1746   /* try to set the sink element to READY again */
1747   ret = gst_element_set_state (chain->sink, GST_STATE_READY);
1748   if (ret == GST_STATE_CHANGE_FAILURE)
1749     return FALSE;
1750
1751   /* Get the VideoOverlay element */
1752   {
1753     GstVideoOverlay *overlay = NULL;
1754
1755     GST_OBJECT_LOCK (playsink);
1756     if (playsink->overlay_element)
1757       gst_object_unref (playsink->overlay_element);
1758     playsink->overlay_element =
1759         GST_VIDEO_OVERLAY (gst_bin_get_by_interface (GST_BIN (chain->chain.bin),
1760             GST_TYPE_VIDEO_OVERLAY));
1761     if (playsink->overlay_element)
1762       overlay = GST_VIDEO_OVERLAY (gst_object_ref (playsink->overlay_element));
1763     GST_OBJECT_UNLOCK (playsink);
1764
1765     if (overlay) {
1766       if (playsink->overlay_handle_set)
1767         gst_video_overlay_set_window_handle (overlay, playsink->overlay_handle);
1768       if (playsink->overlay_handle_events_set)
1769         gst_video_overlay_handle_events (overlay,
1770             playsink->overlay_handle_events);
1771       if (playsink->overlay_render_rectangle_set)
1772         gst_video_overlay_set_render_rectangle (overlay,
1773             playsink->overlay_x, playsink->overlay_y,
1774             playsink->overlay_width, playsink->overlay_height);
1775       gst_object_unref (overlay);
1776     }
1777   }
1778
1779   /* find ts-offset element */
1780   gst_object_replace ((GstObject **) & chain->ts_offset, (GstObject *)
1781       gst_play_sink_find_property_sinks (playsink, chain->sink, "ts-offset",
1782           G_TYPE_INT64));
1783
1784   /* if we can disable async behaviour of the sink, we can avoid adding a
1785    * queue for the audio chain. */
1786   elem =
1787       gst_play_sink_find_property_sinks (playsink, chain->sink, "async",
1788       G_TYPE_BOOLEAN);
1789   if (elem) {
1790     GST_DEBUG_OBJECT (playsink, "setting async property to %d on element %s",
1791         async, GST_ELEMENT_NAME (elem));
1792     g_object_set (elem, "async", async, NULL);
1793     chain->async = async;
1794   } else {
1795     GST_DEBUG_OBJECT (playsink, "no async property on the sink");
1796     chain->async = TRUE;
1797   }
1798
1799   /* Make sure the aspect ratio is kept */
1800   elem =
1801       gst_play_sink_find_property_sinks (playsink, chain->sink,
1802       "force-aspect-ratio", G_TYPE_BOOLEAN);
1803   if (elem)
1804     g_object_set (elem, "force-aspect-ratio", TRUE, NULL);
1805
1806   GST_OBJECT_LOCK (playsink);
1807   if (playsink->colorbalance_element) {
1808     g_signal_handlers_disconnect_by_func (playsink->colorbalance_element,
1809         G_CALLBACK (colorbalance_value_changed_cb), playsink);
1810     gst_object_unref (playsink->colorbalance_element);
1811   }
1812   playsink->colorbalance_element = find_color_balance_element (chain->sink);
1813   GST_OBJECT_UNLOCK (playsink);
1814
1815   if (chain->conv) {
1816     gboolean use_balance = !playsink->colorbalance_element
1817         && (playsink->flags & GST_PLAY_FLAG_SOFT_COLORBALANCE);
1818
1819     g_object_set (chain->conv, "use-balance", use_balance, NULL);
1820
1821     GST_OBJECT_LOCK (playsink);
1822     if (use_balance && GST_PLAY_SINK_VIDEO_CONVERT (chain->conv)->balance)
1823       playsink->colorbalance_element =
1824           GST_COLOR_BALANCE (gst_object_ref (GST_PLAY_SINK_VIDEO_CONVERT
1825               (chain->conv)->balance));
1826     GST_OBJECT_UNLOCK (playsink);
1827   }
1828
1829   update_colorbalance (playsink);
1830
1831   return TRUE;
1832 }
1833
1834 /* make an element for playback of video with subtitles embedded.
1835  * Only used for *raw* video streams.
1836  *
1837  *  +--------------------------------------------+
1838  *  | tbin                                       |
1839  *  |     +--------+      +-----------------+    |
1840  *  |     | queue  |      | subtitleoverlay |    |
1841  * video--src     sink---video_sink         |    |
1842  *  |     +--------+     |                src--src
1843  * text------------------text_sink          |    |
1844  *  |                     +-----------------+    |
1845  *  +--------------------------------------------+
1846  *
1847  */
1848 static GstPlayTextChain *
1849 gen_text_chain (GstPlaySink * playsink)
1850 {
1851   GstPlayTextChain *chain;
1852   GstBin *bin;
1853   GstElement *elem;
1854   GstPad *videosinkpad, *textsinkpad, *srcpad;
1855
1856   chain = g_new0 (GstPlayTextChain, 1);
1857   chain->chain.playsink = playsink;
1858
1859   GST_DEBUG_OBJECT (playsink, "making text chain %p", chain);
1860
1861   chain->chain.bin = gst_bin_new ("tbin");
1862   bin = GST_BIN_CAST (chain->chain.bin);
1863   gst_object_ref_sink (bin);
1864
1865   videosinkpad = textsinkpad = srcpad = NULL;
1866
1867   /* first try to hook the text pad to the custom sink */
1868   if (playsink->text_sink) {
1869     GST_DEBUG_OBJECT (playsink, "trying configured textsink");
1870     chain->sink = try_element (playsink, playsink->text_sink, FALSE);
1871     if (chain->sink) {
1872       elem =
1873           gst_play_sink_find_property_sinks (playsink, chain->sink, "async",
1874           G_TYPE_BOOLEAN);
1875       if (elem) {
1876         /* make sure the sparse subtitles don't participate in the preroll */
1877         g_object_set (elem, "async", FALSE, NULL);
1878         GST_DEBUG_OBJECT (playsink, "adding custom text sink");
1879         gst_bin_add (bin, chain->sink);
1880         /* NOTE streamsynchronizer needs streams decoupled */
1881         /* make a little queue */
1882         chain->queue = gst_element_factory_make ("queue", "subqueue");
1883         if (chain->queue == NULL) {
1884           post_missing_element_message (playsink, "queue");
1885           GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1886               (_("Missing element '%s' - check your GStreamer installation."),
1887                   "queue"), ("rendering might be suboptimal"));
1888         } else {
1889           g_object_set (G_OBJECT (chain->queue), "max-size-buffers", 3,
1890               "max-size-bytes", 0, "max-size-time", (gint64) 0,
1891               "silent", TRUE, NULL);
1892           gst_bin_add (bin, chain->queue);
1893         }
1894         /* we have a custom sink, this will be our textsinkpad */
1895         if (gst_element_link_pads_full (chain->queue, "src", chain->sink,
1896                 "sink", GST_PAD_LINK_CHECK_TEMPLATE_CAPS)) {
1897           /* we're all fine now and we can add the sink to the chain */
1898           GST_DEBUG_OBJECT (playsink, "using custom text sink");
1899           textsinkpad = gst_element_get_static_pad (chain->queue, "sink");
1900         } else {
1901           GST_WARNING_OBJECT (playsink,
1902               "can't find a sink pad on custom text sink");
1903           gst_bin_remove (bin, chain->sink);
1904           gst_bin_remove (bin, chain->queue);
1905           chain->sink = NULL;
1906           chain->queue = NULL;
1907         }
1908         /* try to set sync to true but it's no biggie when we can't */
1909         if (chain->sink && (elem =
1910                 gst_play_sink_find_property_sinks (playsink, chain->sink,
1911                     "sync", G_TYPE_BOOLEAN)))
1912           g_object_set (elem, "sync", TRUE, NULL);
1913
1914         if (!textsinkpad)
1915           gst_bin_remove (bin, chain->sink);
1916       } else {
1917         GST_WARNING_OBJECT (playsink,
1918             "can't find async property in custom text sink");
1919       }
1920     }
1921     if (textsinkpad == NULL) {
1922       GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1923           (_("Custom text sink element is not usable.")),
1924           ("fallback to default textoverlay"));
1925     }
1926   }
1927
1928   if (textsinkpad == NULL) {
1929     if (!(playsink->flags & GST_PLAY_FLAG_NATIVE_VIDEO)) {
1930       /* make a little queue */
1931       chain->queue = gst_element_factory_make ("queue", "vqueue");
1932       if (chain->queue == NULL) {
1933         post_missing_element_message (playsink, "queue");
1934         GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1935             (_("Missing element '%s' - check your GStreamer installation."),
1936                 "queue"), ("video rendering might be suboptimal"));
1937       } else {
1938         g_object_set (G_OBJECT (chain->queue), "max-size-buffers", 3,
1939             "max-size-bytes", 0, "max-size-time", (gint64) 0,
1940             "silent", TRUE, NULL);
1941         gst_bin_add (bin, chain->queue);
1942         videosinkpad = gst_element_get_static_pad (chain->queue, "sink");
1943       }
1944
1945       chain->overlay =
1946           gst_element_factory_make ("subtitleoverlay", "suboverlay");
1947       if (chain->overlay == NULL) {
1948         post_missing_element_message (playsink, "subtitleoverlay");
1949         GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1950             (_("Missing element '%s' - check your GStreamer installation."),
1951                 "subtitleoverlay"), ("subtitle rendering disabled"));
1952       } else {
1953         GstElement *element;
1954
1955         gst_bin_add (bin, chain->overlay);
1956
1957         g_object_set (G_OBJECT (chain->overlay), "silent", FALSE, NULL);
1958         if (playsink->font_desc) {
1959           g_object_set (G_OBJECT (chain->overlay), "font-desc",
1960               playsink->font_desc, NULL);
1961         }
1962         if (playsink->subtitle_encoding) {
1963           g_object_set (G_OBJECT (chain->overlay), "subtitle-encoding",
1964               playsink->subtitle_encoding, NULL);
1965         }
1966
1967         gst_element_link_pads_full (chain->queue, "src", chain->overlay,
1968             "video_sink", GST_PAD_LINK_CHECK_TEMPLATE_CAPS);
1969
1970         /* make another little queue to decouple streams */
1971         element = gst_element_factory_make ("queue", "subqueue");
1972         if (element == NULL) {
1973           post_missing_element_message (playsink, "queue");
1974           GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1975               (_("Missing element '%s' - check your GStreamer installation."),
1976                   "queue"), ("rendering might be suboptimal"));
1977         } else {
1978           g_object_set (G_OBJECT (element), "max-size-buffers", 3,
1979               "max-size-bytes", 0, "max-size-time", (gint64) 0,
1980               "silent", TRUE, NULL);
1981           gst_bin_add (bin, element);
1982           if (gst_element_link_pads_full (element, "src", chain->overlay,
1983                   "subtitle_sink", GST_PAD_LINK_CHECK_TEMPLATE_CAPS)) {
1984             textsinkpad = gst_element_get_static_pad (element, "sink");
1985             srcpad = gst_element_get_static_pad (chain->overlay, "src");
1986           } else {
1987             gst_bin_remove (bin, chain->sink);
1988             gst_bin_remove (bin, chain->overlay);
1989             chain->sink = NULL;
1990             chain->overlay = NULL;
1991             gst_object_unref (videosinkpad);
1992             videosinkpad = NULL;
1993           }
1994         }
1995       }
1996     }
1997   }
1998
1999   if (videosinkpad == NULL) {
2000     /* if we still don't have a videosink, we don't have an overlay. the only
2001      * thing we can do is insert an identity and ghost the src
2002      * and sink pads. */
2003     chain->identity = gst_element_factory_make ("identity", "tidentity");
2004     if (chain->identity == NULL) {
2005       post_missing_element_message (playsink, "identity");
2006       GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
2007           (_("Missing element '%s' - check your GStreamer installation."),
2008               "identity"), (NULL));
2009     } else {
2010       g_object_set (chain->identity, "signal-handoffs", FALSE, NULL);
2011       g_object_set (chain->identity, "silent", TRUE, NULL);
2012       gst_bin_add (bin, chain->identity);
2013       srcpad = gst_element_get_static_pad (chain->identity, "src");
2014       videosinkpad = gst_element_get_static_pad (chain->identity, "sink");
2015     }
2016   }
2017
2018   /* expose the ghostpads */
2019   if (videosinkpad) {
2020     chain->videosinkpad = gst_ghost_pad_new ("sink", videosinkpad);
2021     gst_object_unref (videosinkpad);
2022     gst_element_add_pad (chain->chain.bin, chain->videosinkpad);
2023   }
2024   if (textsinkpad) {
2025     chain->textsinkpad = gst_ghost_pad_new ("text_sink", textsinkpad);
2026     gst_object_unref (textsinkpad);
2027     gst_element_add_pad (chain->chain.bin, chain->textsinkpad);
2028   }
2029   if (srcpad) {
2030     chain->srcpad = gst_ghost_pad_new ("src", srcpad);
2031     gst_object_unref (srcpad);
2032     gst_element_add_pad (chain->chain.bin, chain->srcpad);
2033   }
2034
2035   return chain;
2036 }
2037
2038 static void
2039 notify_volume_cb (GObject * object, GParamSpec * pspec, GstPlaySink * playsink)
2040 {
2041   gdouble vol;
2042
2043   g_object_get (object, "volume", &vol, NULL);
2044   playsink->volume = vol;
2045
2046   g_object_notify (G_OBJECT (playsink), "volume");
2047 }
2048
2049 static void
2050 notify_mute_cb (GObject * object, GParamSpec * pspec, GstPlaySink * playsink)
2051 {
2052   gboolean mute;
2053
2054   g_object_get (object, "mute", &mute, NULL);
2055   playsink->mute = mute;
2056
2057   g_object_notify (G_OBJECT (playsink), "mute");
2058 }
2059
2060 /* make the chain that contains the elements needed to perform
2061  * audio playback.
2062  *
2063  * We add a tee as the first element so that we can link the visualisation chain
2064  * to it when requested.
2065  *
2066  *  +-------------------------------------------------------------+
2067  *  | abin                                                        |
2068  *  |      +---------+   +----------+   +---------+   +---------+ |
2069  *  |      |audioconv|   |audioscale|   | volume  |   |audiosink| |
2070  *  |   +-srck      src-sink       src-sink      src-sink       | |
2071  *  |   |  +---------+   +----------+   +---------+   +---------+ |
2072  * sink-+                                                         |
2073  *  +-------------------------------------------------------------+
2074  */
2075 static GstPlayAudioChain *
2076 gen_audio_chain (GstPlaySink * playsink, gboolean raw)
2077 {
2078   GstPlayAudioChain *chain;
2079   GstBin *bin;
2080   gboolean have_volume;
2081   GstPad *pad;
2082   GstElement *head, *prev, *elem = NULL;
2083
2084   chain = g_new0 (GstPlayAudioChain, 1);
2085   chain->chain.playsink = playsink;
2086   chain->chain.raw = raw;
2087
2088   GST_DEBUG_OBJECT (playsink, "making audio chain %p", chain);
2089
2090   if (playsink->audio_sink) {
2091     GST_DEBUG_OBJECT (playsink, "trying configured audiosink %" GST_PTR_FORMAT,
2092         playsink->audio_sink);
2093     chain->sink = try_element (playsink, playsink->audio_sink, FALSE);
2094   } else {
2095     /* only try fallback if no specific sink was chosen */
2096     if (chain->sink == NULL) {
2097       GST_DEBUG_OBJECT (playsink, "trying autoaudiosink");
2098       elem = gst_element_factory_make ("autoaudiosink", "audiosink");
2099       chain->sink = try_element (playsink, elem, TRUE);
2100     }
2101     if (chain->sink == NULL) {
2102       /* if default sink from config.h is different then try it too */
2103       if (strcmp (DEFAULT_AUDIOSINK, "autoaudiosink")) {
2104         GST_DEBUG_OBJECT (playsink, "trying " DEFAULT_AUDIOSINK);
2105         elem = gst_element_factory_make (DEFAULT_AUDIOSINK, "audiosink");
2106         chain->sink = try_element (playsink, elem, TRUE);
2107       }
2108     }
2109     if (chain->sink)
2110       playsink->audio_sink = gst_object_ref (chain->sink);
2111   }
2112   if (chain->sink == NULL)
2113     goto no_sinks;
2114
2115   chain->chain.bin = gst_bin_new ("abin");
2116   bin = GST_BIN_CAST (chain->chain.bin);
2117   gst_object_ref_sink (bin);
2118   gst_bin_add (bin, chain->sink);
2119
2120   /* we have to add a queue when we need to decouple for the video sink in
2121    * visualisations and for streamsynchronizer */
2122   GST_DEBUG_OBJECT (playsink, "adding audio queue");
2123   chain->queue = gst_element_factory_make ("queue", "aqueue");
2124   if (chain->queue == NULL) {
2125     post_missing_element_message (playsink, "queue");
2126     GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
2127         (_("Missing element '%s' - check your GStreamer installation."),
2128             "queue"), ("audio playback and visualizations might not work"));
2129     head = chain->sink;
2130     prev = NULL;
2131   } else {
2132     g_object_set (chain->queue, "silent", TRUE, NULL);
2133     gst_bin_add (bin, chain->queue);
2134     prev = head = chain->queue;
2135   }
2136
2137   /* find ts-offset element */
2138   gst_object_replace ((GstObject **) & chain->ts_offset, (GstObject *)
2139       gst_play_sink_find_property_sinks (playsink, chain->sink, "ts-offset",
2140           G_TYPE_INT64));
2141
2142   /* check if the sink, or something within the sink, has the volume property.
2143    * If it does we don't need to add a volume element.  */
2144   elem =
2145       gst_play_sink_find_property_sinks (playsink, chain->sink, "volume",
2146       G_TYPE_DOUBLE);
2147   if (elem) {
2148     chain->volume = elem;
2149
2150     g_signal_connect (chain->volume, "notify::volume",
2151         G_CALLBACK (notify_volume_cb), playsink);
2152
2153     GST_DEBUG_OBJECT (playsink, "the sink has a volume property");
2154     have_volume = TRUE;
2155     chain->sink_volume = TRUE;
2156     /* if the sink also has a mute property we can use this as well. We'll only
2157      * use the mute property if there is a volume property. We can simulate the
2158      * mute with the volume otherwise. */
2159     chain->mute =
2160         gst_play_sink_find_property_sinks (playsink, chain->sink, "mute",
2161         G_TYPE_BOOLEAN);
2162     if (chain->mute) {
2163       GST_DEBUG_OBJECT (playsink, "the sink has a mute property");
2164       g_signal_connect (chain->mute, "notify::mute",
2165           G_CALLBACK (notify_mute_cb), playsink);
2166     }
2167     /* use the sink to control the volume and mute */
2168     if (playsink->volume_changed) {
2169       g_object_set (G_OBJECT (chain->volume), "volume", playsink->volume, NULL);
2170       playsink->volume_changed = FALSE;
2171     }
2172     if (playsink->mute_changed) {
2173       if (chain->mute) {
2174         g_object_set (chain->mute, "mute", playsink->mute, NULL);
2175       } else {
2176         if (playsink->mute)
2177           g_object_set (chain->volume, "volume", (gdouble) 0.0, NULL);
2178       }
2179       playsink->mute_changed = FALSE;
2180     }
2181   } else {
2182     /* no volume, we need to add a volume element when we can */
2183     GST_DEBUG_OBJECT (playsink, "the sink has no volume property");
2184     have_volume = FALSE;
2185     chain->sink_volume = FALSE;
2186   }
2187
2188   if (!(playsink->flags & GST_PLAY_FLAG_NATIVE_AUDIO) || (!have_volume
2189           && (playsink->flags & GST_PLAY_FLAG_SOFT_VOLUME))) {
2190     gboolean use_converters = !(playsink->flags & GST_PLAY_FLAG_NATIVE_AUDIO);
2191     gboolean use_volume =
2192         !have_volume && (playsink->flags & GST_PLAY_FLAG_SOFT_VOLUME);
2193     GST_DEBUG_OBJECT (playsink,
2194         "creating audioconvert with use-converters %d, use-volume %d",
2195         use_converters, use_volume);
2196     chain->conv =
2197         g_object_new (GST_TYPE_PLAY_SINK_AUDIO_CONVERT, "name", "aconv",
2198         "use-converters", use_converters, "use-volume", use_volume, NULL);
2199     gst_bin_add (bin, chain->conv);
2200     if (prev) {
2201       if (!gst_element_link_pads_full (prev, "src", chain->conv, "sink",
2202               GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
2203         goto link_failed;
2204     } else {
2205       head = chain->conv;
2206     }
2207     prev = chain->conv;
2208
2209     if (!have_volume && (playsink->flags & GST_PLAY_FLAG_SOFT_VOLUME)) {
2210       GstPlaySinkAudioConvert *conv =
2211           GST_PLAY_SINK_AUDIO_CONVERT_CAST (chain->conv);
2212
2213       if (conv->volume) {
2214         chain->volume = conv->volume;
2215         have_volume = TRUE;
2216
2217         g_signal_connect (chain->volume, "notify::volume",
2218             G_CALLBACK (notify_volume_cb), playsink);
2219
2220         /* volume also has the mute property */
2221         chain->mute = chain->volume;
2222         g_signal_connect (chain->mute, "notify::mute",
2223             G_CALLBACK (notify_mute_cb), playsink);
2224
2225         /* configure with the latest volume and mute */
2226         g_object_set (G_OBJECT (chain->volume), "volume", playsink->volume,
2227             NULL);
2228         g_object_set (G_OBJECT (chain->mute), "mute", playsink->mute, NULL);
2229       }
2230     }
2231   }
2232
2233   if (prev) {
2234     /* we only have to link to the previous element if we have something in
2235      * front of the sink */
2236     GST_DEBUG_OBJECT (playsink, "linking to sink");
2237     if (!gst_element_link_pads_full (prev, "src", chain->sink, NULL,
2238             GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
2239       goto link_failed;
2240   }
2241
2242   /* post a warning if we have no way to configure the volume */
2243   if (!have_volume) {
2244     GST_ELEMENT_WARNING (playsink, STREAM, NOT_IMPLEMENTED,
2245         (_("No volume control found")), ("Volume/mute is not available"));
2246   }
2247
2248   /* and ghost the sinkpad of the headmost element */
2249   GST_DEBUG_OBJECT (playsink, "ghosting sink pad");
2250   pad = gst_element_get_static_pad (head, "sink");
2251   chain->sinkpad = gst_ghost_pad_new ("sink", pad);
2252   gst_object_unref (pad);
2253   gst_element_add_pad (chain->chain.bin, chain->sinkpad);
2254
2255   return chain;
2256
2257   /* ERRORS */
2258 no_sinks:
2259   {
2260     if (!elem && !playsink->audio_sink) {
2261       post_missing_element_message (playsink, "autoaudiosink");
2262       if (strcmp (DEFAULT_AUDIOSINK, "autoaudiosink")) {
2263         post_missing_element_message (playsink, DEFAULT_AUDIOSINK);
2264         GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
2265             (_("Both autoaudiosink and %s elements are missing."),
2266                 DEFAULT_AUDIOSINK), (NULL));
2267       } else {
2268         GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
2269             (_("The autoaudiosink element is missing.")), (NULL));
2270       }
2271     } else {
2272       if (playsink->audio_sink) {
2273         GST_ELEMENT_ERROR (playsink, CORE, STATE_CHANGE,
2274             (_("Configured audiosink %s is not working."),
2275                 GST_ELEMENT_NAME (playsink->audio_sink)), (NULL));
2276       } else if (strcmp (DEFAULT_AUDIOSINK, "autoaudiosink")) {
2277         GST_ELEMENT_ERROR (playsink, CORE, STATE_CHANGE,
2278             (_("Both autoaudiosink and %s elements are not working."),
2279                 DEFAULT_AUDIOSINK), (NULL));
2280       } else {
2281         GST_ELEMENT_ERROR (playsink, CORE, STATE_CHANGE,
2282             (_("The autoaudiosink element is not working.")), (NULL));
2283       }
2284     }
2285     free_chain ((GstPlayChain *) chain);
2286     return NULL;
2287   }
2288 link_failed:
2289   {
2290     GST_ELEMENT_ERROR (playsink, CORE, PAD,
2291         (NULL), ("Failed to configure the audio sink."));
2292     /* checking sink made it READY */
2293     gst_element_set_state (chain->sink, GST_STATE_NULL);
2294     /* Remove chain from the bin to allow reuse later */
2295     gst_bin_remove (bin, chain->sink);
2296     free_chain ((GstPlayChain *) chain);
2297     return NULL;
2298   }
2299 }
2300
2301 static gboolean
2302 setup_audio_chain (GstPlaySink * playsink, gboolean raw)
2303 {
2304   GstElement *elem;
2305   GstPlayAudioChain *chain;
2306   GstStateChangeReturn ret;
2307   GstPlaySinkAudioConvert *conv;
2308
2309   chain = playsink->audiochain;
2310   conv = GST_PLAY_SINK_AUDIO_CONVERT_CAST (chain->conv);
2311
2312   chain->chain.raw = raw;
2313
2314   /* if the chain was active we don't do anything */
2315   if (GST_PLAY_CHAIN (chain)->activated == TRUE)
2316     return TRUE;
2317
2318   /* try to set the sink element to READY again */
2319   ret = gst_element_set_state (chain->sink, GST_STATE_READY);
2320   if (ret == GST_STATE_CHANGE_FAILURE)
2321     return FALSE;
2322
2323   /* find ts-offset element */
2324   gst_object_replace ((GstObject **) & chain->ts_offset, (GstObject *)
2325       gst_play_sink_find_property_sinks (playsink, chain->sink, "ts-offset",
2326           G_TYPE_INT64));
2327
2328   /* check if the sink, or something within the sink, has the volume property.
2329    * If it does we don't need to add a volume element.  */
2330   elem =
2331       gst_play_sink_find_property_sinks (playsink, chain->sink, "volume",
2332       G_TYPE_DOUBLE);
2333   if (elem) {
2334     chain->volume = elem;
2335
2336     if (playsink->volume_changed) {
2337       GST_DEBUG_OBJECT (playsink, "the sink has a volume property, setting %f",
2338           playsink->volume);
2339       /* use the sink to control the volume */
2340       g_object_set (G_OBJECT (chain->volume), "volume", playsink->volume, NULL);
2341       playsink->volume_changed = FALSE;
2342     }
2343
2344     g_signal_connect (chain->volume, "notify::volume",
2345         G_CALLBACK (notify_volume_cb), playsink);
2346     /* if the sink also has a mute property we can use this as well. We'll only
2347      * use the mute property if there is a volume property. We can simulate the
2348      * mute with the volume otherwise. */
2349     chain->mute =
2350         gst_play_sink_find_property_sinks (playsink, chain->sink, "mute",
2351         G_TYPE_BOOLEAN);
2352     if (chain->mute) {
2353       GST_DEBUG_OBJECT (playsink, "the sink has a mute property");
2354       g_signal_connect (chain->mute, "notify::mute",
2355           G_CALLBACK (notify_mute_cb), playsink);
2356     }
2357
2358     g_object_set (chain->conv, "use-volume", FALSE, NULL);
2359   } else if (conv) {
2360     /* no volume, we need to add a volume element when we can */
2361     g_object_set (chain->conv, "use-volume",
2362         ! !(playsink->flags & GST_PLAY_FLAG_SOFT_VOLUME), NULL);
2363     GST_DEBUG_OBJECT (playsink, "the sink has no volume property");
2364
2365     /* Disconnect signals */
2366     disconnect_chain (chain, playsink);
2367
2368     if (conv->volume && (playsink->flags & GST_PLAY_FLAG_SOFT_VOLUME)) {
2369       chain->volume = conv->volume;
2370       chain->mute = chain->volume;
2371
2372       g_signal_connect (chain->volume, "notify::volume",
2373           G_CALLBACK (notify_volume_cb), playsink);
2374
2375       g_signal_connect (chain->mute, "notify::mute",
2376           G_CALLBACK (notify_mute_cb), playsink);
2377
2378       /* configure with the latest volume and mute */
2379       g_object_set (G_OBJECT (chain->volume), "volume", playsink->volume, NULL);
2380       g_object_set (G_OBJECT (chain->mute), "mute", playsink->mute, NULL);
2381     }
2382
2383     GST_DEBUG_OBJECT (playsink, "reusing existing volume element");
2384   }
2385   return TRUE;
2386 }
2387
2388 /*
2389  *  +-------------------------------------------------------------------+
2390  *  | visbin                                                            |
2391  *  |      +----------+   +------------+   +----------+   +-------+     |
2392  *  |      | visqueue |   | audioconv  |   | audiores |   |  vis  |     |
2393  *  |   +-sink       src-sink + samp  src-sink       src-sink    src-+  |
2394  *  |   |  +----------+   +------------+   +----------+   +-------+  |  |
2395  * sink-+                                                            +-src
2396  *  +-------------------------------------------------------------------+
2397  *
2398  */
2399 static GstPlayVisChain *
2400 gen_vis_chain (GstPlaySink * playsink)
2401 {
2402   GstPlayVisChain *chain;
2403   GstBin *bin;
2404   gboolean res;
2405   GstPad *pad;
2406   GstElement *elem;
2407
2408   chain = g_new0 (GstPlayVisChain, 1);
2409   chain->chain.playsink = playsink;
2410
2411   GST_DEBUG_OBJECT (playsink, "making vis chain %p", chain);
2412
2413   chain->chain.bin = gst_bin_new ("visbin");
2414   bin = GST_BIN_CAST (chain->chain.bin);
2415   gst_object_ref_sink (bin);
2416
2417   /* we're queuing raw audio here, we can remove this queue when we can disable
2418    * async behaviour in the video sink. */
2419   chain->queue = gst_element_factory_make ("queue", "visqueue");
2420   if (chain->queue == NULL)
2421     goto no_queue;
2422   g_object_set (chain->queue, "silent", TRUE, NULL);
2423   gst_bin_add (bin, chain->queue);
2424
2425   chain->conv = gst_element_factory_make ("audioconvert", "aconv");
2426   if (chain->conv == NULL)
2427     goto no_audioconvert;
2428   gst_bin_add (bin, chain->conv);
2429
2430   chain->resample = gst_element_factory_make ("audioresample", "aresample");
2431   if (chain->resample == NULL)
2432     goto no_audioresample;
2433   gst_bin_add (bin, chain->resample);
2434
2435   /* this pad will be used for blocking the dataflow and switching the vis
2436    * plugin */
2437   chain->blockpad = gst_element_get_static_pad (chain->resample, "src");
2438
2439   if (playsink->visualisation) {
2440     GST_DEBUG_OBJECT (playsink, "trying configure vis");
2441     chain->vis = try_element (playsink, playsink->visualisation, FALSE);
2442   }
2443   if (chain->vis == NULL) {
2444     GST_DEBUG_OBJECT (playsink, "trying goom");
2445     elem = gst_element_factory_make ("goom", "vis");
2446     chain->vis = try_element (playsink, elem, TRUE);
2447   }
2448   if (chain->vis == NULL)
2449     goto no_goom;
2450
2451   gst_bin_add (bin, chain->vis);
2452
2453   res = gst_element_link_pads_full (chain->queue, "src", chain->conv, "sink",
2454       GST_PAD_LINK_CHECK_NOTHING);
2455   res &=
2456       gst_element_link_pads_full (chain->conv, "src", chain->resample, "sink",
2457       GST_PAD_LINK_CHECK_NOTHING);
2458   res &=
2459       gst_element_link_pads_full (chain->resample, "src", chain->vis, "sink",
2460       GST_PAD_LINK_CHECK_NOTHING);
2461   if (!res)
2462     goto link_failed;
2463
2464   chain->vissinkpad = gst_element_get_static_pad (chain->vis, "sink");
2465   chain->vissrcpad = gst_element_get_static_pad (chain->vis, "src");
2466
2467   pad = gst_element_get_static_pad (chain->queue, "sink");
2468   chain->sinkpad = gst_ghost_pad_new ("sink", pad);
2469   gst_object_unref (pad);
2470   gst_element_add_pad (chain->chain.bin, chain->sinkpad);
2471
2472   chain->srcpad = gst_ghost_pad_new ("src", chain->vissrcpad);
2473   gst_element_add_pad (chain->chain.bin, chain->srcpad);
2474
2475   return chain;
2476
2477   /* ERRORS */
2478 no_queue:
2479   {
2480     post_missing_element_message (playsink, "queue");
2481     GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
2482         (_("Missing element '%s' - check your GStreamer installation."),
2483             "queue"), (NULL));
2484     free_chain ((GstPlayChain *) chain);
2485     return NULL;
2486   }
2487 no_audioconvert:
2488   {
2489     post_missing_element_message (playsink, "audioconvert");
2490     GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
2491         (_("Missing element '%s' - check your GStreamer installation."),
2492             "audioconvert"), ("possibly a liboil version mismatch?"));
2493     free_chain ((GstPlayChain *) chain);
2494     return NULL;
2495   }
2496 no_audioresample:
2497   {
2498     post_missing_element_message (playsink, "audioresample");
2499     GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
2500         (_("Missing element '%s' - check your GStreamer installation."),
2501             "audioresample"), (NULL));
2502     free_chain ((GstPlayChain *) chain);
2503     return NULL;
2504   }
2505 no_goom:
2506   {
2507     post_missing_element_message (playsink, "goom");
2508     GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
2509         (_("Missing element '%s' - check your GStreamer installation."),
2510             "goom"), (NULL));
2511     free_chain ((GstPlayChain *) chain);
2512     return NULL;
2513   }
2514 link_failed:
2515   {
2516     GST_ELEMENT_ERROR (playsink, CORE, PAD,
2517         (NULL), ("Failed to configure the visualisation element."));
2518     /* element made it to READY */
2519     gst_element_set_state (chain->vis, GST_STATE_NULL);
2520     free_chain ((GstPlayChain *) chain);
2521     return NULL;
2522   }
2523 }
2524
2525 /* this function is called when all the request pads are requested and when we
2526  * have to construct the final pipeline. Based on the flags we construct the
2527  * final output pipelines.
2528  */
2529 gboolean
2530 gst_play_sink_reconfigure (GstPlaySink * playsink)
2531 {
2532   GstPlayFlags flags;
2533   gboolean need_audio, need_video, need_deinterlace, need_vis, need_text;
2534
2535   GST_DEBUG_OBJECT (playsink, "reconfiguring");
2536
2537   /* assume we need nothing */
2538   need_audio = need_video = need_deinterlace = need_vis = need_text = FALSE;
2539
2540   GST_PLAY_SINK_LOCK (playsink);
2541   GST_OBJECT_LOCK (playsink);
2542   /* get flags, there are protected with the object lock */
2543   flags = playsink->flags;
2544   GST_OBJECT_UNLOCK (playsink);
2545
2546   /* figure out which components we need */
2547   if (flags & GST_PLAY_FLAG_TEXT && playsink->text_pad) {
2548     /* we have subtitles and we are requested to show it */
2549     need_text = TRUE;
2550   }
2551
2552   GST_OBJECT_LOCK (playsink);
2553   if (playsink->overlay_element)
2554     gst_object_unref (playsink->overlay_element);
2555   playsink->overlay_element = NULL;
2556
2557   if (playsink->colorbalance_element) {
2558     g_signal_handlers_disconnect_by_func (playsink->colorbalance_element,
2559         G_CALLBACK (colorbalance_value_changed_cb), playsink);
2560     gst_object_unref (playsink->colorbalance_element);
2561   }
2562   playsink->colorbalance_element = NULL;
2563   GST_OBJECT_UNLOCK (playsink);
2564
2565   if (((flags & GST_PLAY_FLAG_VIDEO)
2566           || (flags & GST_PLAY_FLAG_NATIVE_VIDEO)) && playsink->video_pad) {
2567     /* we have video and we are requested to show it */
2568     need_video = TRUE;
2569
2570     /* we only deinterlace if native video is not requested and
2571      * we have raw video */
2572     if ((flags & GST_PLAY_FLAG_DEINTERLACE)
2573         && !(flags & GST_PLAY_FLAG_NATIVE_VIDEO) && playsink->video_pad_raw)
2574       need_deinterlace = TRUE;
2575   }
2576
2577   if (playsink->audio_pad) {
2578     if ((flags & GST_PLAY_FLAG_AUDIO) || (flags & GST_PLAY_FLAG_NATIVE_AUDIO)) {
2579       need_audio = TRUE;
2580     }
2581     if (playsink->audio_pad_raw) {
2582       /* only can do vis with raw uncompressed audio */
2583       if (flags & GST_PLAY_FLAG_VIS && !need_video) {
2584         /* also add video when we add visualisation */
2585         need_video = TRUE;
2586         need_vis = TRUE;
2587       }
2588     }
2589   }
2590
2591   /* we have a text_pad and we need text rendering, in this case we need a
2592    * video_pad to combine the video with the text or visualizations */
2593   if (need_text && !need_video && !playsink->text_sink) {
2594     if (playsink->video_pad) {
2595       need_video = TRUE;
2596     } else if (need_audio) {
2597       GST_ELEMENT_WARNING (playsink, STREAM, FORMAT,
2598           (_("Can't play a text file without video or visualizations.")),
2599           ("Have text pad but no video pad or visualizations"));
2600       need_text = FALSE;
2601     } else {
2602       GST_ELEMENT_ERROR (playsink, STREAM, FORMAT,
2603           (_("Can't play a text file without video or visualizations.")),
2604           ("Have text pad but no video pad or visualizations"));
2605       GST_PLAY_SINK_UNLOCK (playsink);
2606       return FALSE;
2607     }
2608   }
2609
2610   GST_DEBUG_OBJECT (playsink, "audio:%d, video:%d, vis:%d, text:%d", need_audio,
2611       need_video, need_vis, need_text);
2612
2613   /* set up video pipeline */
2614   if (need_video) {
2615     gboolean raw, async;
2616
2617     /* we need a raw sink when we do vis or when we have a raw pad */
2618     raw = need_vis ? TRUE : playsink->video_pad_raw;
2619     /* we try to set the sink async=FALSE when we need vis, this way we can
2620      * avoid a queue in the audio chain. */
2621     async = !need_vis;
2622
2623     GST_DEBUG_OBJECT (playsink, "adding video, raw %d",
2624         playsink->video_pad_raw);
2625
2626     if (playsink->videochain) {
2627       /* try to reactivate the chain */
2628       if (!setup_video_chain (playsink, raw, async)) {
2629         if (playsink->video_sinkpad_stream_synchronizer) {
2630           gst_element_release_request_pad (GST_ELEMENT_CAST
2631               (playsink->stream_synchronizer),
2632               playsink->video_sinkpad_stream_synchronizer);
2633           gst_object_unref (playsink->video_sinkpad_stream_synchronizer);
2634           playsink->video_sinkpad_stream_synchronizer = NULL;
2635           gst_object_unref (playsink->video_srcpad_stream_synchronizer);
2636           playsink->video_srcpad_stream_synchronizer = NULL;
2637         }
2638
2639         add_chain (GST_PLAY_CHAIN (playsink->videochain), FALSE);
2640
2641         /* Remove the sink from the bin to keep its state
2642          * and unparent it to allow reuse */
2643         if (playsink->videochain->sink)
2644           gst_bin_remove (GST_BIN_CAST (playsink->videochain->chain.bin),
2645               playsink->videochain->sink);
2646
2647         activate_chain (GST_PLAY_CHAIN (playsink->videochain), FALSE);
2648         free_chain ((GstPlayChain *) playsink->videochain);
2649         playsink->videochain = NULL;
2650       }
2651     }
2652
2653     if (!playsink->videochain)
2654       playsink->videochain = gen_video_chain (playsink, raw, async);
2655     if (!playsink->videochain)
2656       goto no_chain;
2657
2658     if (!playsink->video_sinkpad_stream_synchronizer) {
2659       GValue item = { 0, };
2660       GstIterator *it;
2661
2662       playsink->video_sinkpad_stream_synchronizer =
2663           gst_element_get_request_pad (GST_ELEMENT_CAST
2664           (playsink->stream_synchronizer), "sink_%u");
2665       it = gst_pad_iterate_internal_links
2666           (playsink->video_sinkpad_stream_synchronizer);
2667       g_assert (it);
2668       gst_iterator_next (it, &item);
2669       playsink->video_srcpad_stream_synchronizer = g_value_dup_object (&item);
2670       g_value_unset (&item);
2671       g_assert (playsink->video_srcpad_stream_synchronizer);
2672       gst_iterator_free (it);
2673     }
2674
2675     if (playsink->video_pad)
2676       gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (playsink->video_pad),
2677           playsink->video_sinkpad_stream_synchronizer);
2678
2679     if (need_deinterlace) {
2680       if (!playsink->videodeinterlacechain)
2681         playsink->videodeinterlacechain =
2682             gen_video_deinterlace_chain (playsink);
2683       if (!playsink->videodeinterlacechain)
2684         goto no_chain;
2685
2686       GST_DEBUG_OBJECT (playsink, "adding video deinterlace chain");
2687
2688       GST_DEBUG_OBJECT (playsink, "setting up deinterlacing chain");
2689
2690       add_chain (GST_PLAY_CHAIN (playsink->videodeinterlacechain), TRUE);
2691       activate_chain (GST_PLAY_CHAIN (playsink->videodeinterlacechain), TRUE);
2692
2693       gst_pad_link_full (playsink->video_srcpad_stream_synchronizer,
2694           playsink->videodeinterlacechain->sinkpad, GST_PAD_LINK_CHECK_NOTHING);
2695     } else {
2696       if (playsink->videodeinterlacechain) {
2697         add_chain (GST_PLAY_CHAIN (playsink->videodeinterlacechain), FALSE);
2698         activate_chain (GST_PLAY_CHAIN (playsink->videodeinterlacechain),
2699             FALSE);
2700       }
2701     }
2702
2703     GST_DEBUG_OBJECT (playsink, "adding video chain");
2704     add_chain (GST_PLAY_CHAIN (playsink->videochain), TRUE);
2705     activate_chain (GST_PLAY_CHAIN (playsink->videochain), TRUE);
2706     /* if we are not part of vis or subtitles, set the ghostpad target */
2707     if (!need_vis && !need_text && (!playsink->textchain
2708             || !playsink->text_pad)) {
2709       GST_DEBUG_OBJECT (playsink, "ghosting video sinkpad");
2710       if (need_deinterlace)
2711         gst_pad_link_full (playsink->videodeinterlacechain->srcpad,
2712             playsink->videochain->sinkpad, GST_PAD_LINK_CHECK_NOTHING);
2713       else
2714         gst_pad_link_full (playsink->video_srcpad_stream_synchronizer,
2715             playsink->videochain->sinkpad, GST_PAD_LINK_CHECK_NOTHING);
2716     }
2717   } else {
2718     GST_DEBUG_OBJECT (playsink, "no video needed");
2719     if (playsink->videochain) {
2720       GST_DEBUG_OBJECT (playsink, "removing video chain");
2721       if (playsink->vischain) {
2722         GstPad *srcpad;
2723
2724         GST_DEBUG_OBJECT (playsink, "unlinking vis chain");
2725
2726         /* also had visualisation, release the tee srcpad before we then
2727          * unlink the video from it */
2728         if (playsink->audio_tee_vissrc) {
2729           gst_element_release_request_pad (playsink->audio_tee,
2730               playsink->audio_tee_vissrc);
2731           gst_object_unref (playsink->audio_tee_vissrc);
2732           playsink->audio_tee_vissrc = NULL;
2733         }
2734         srcpad =
2735             gst_element_get_static_pad (playsink->vischain->chain.bin, "src");
2736         gst_pad_unlink (srcpad, playsink->videochain->sinkpad);
2737       }
2738
2739       if (playsink->video_sinkpad_stream_synchronizer) {
2740         gst_element_release_request_pad (GST_ELEMENT_CAST
2741             (playsink->stream_synchronizer),
2742             playsink->video_sinkpad_stream_synchronizer);
2743         gst_object_unref (playsink->video_sinkpad_stream_synchronizer);
2744         playsink->video_sinkpad_stream_synchronizer = NULL;
2745         gst_object_unref (playsink->video_srcpad_stream_synchronizer);
2746         playsink->video_srcpad_stream_synchronizer = NULL;
2747       }
2748
2749       add_chain (GST_PLAY_CHAIN (playsink->videochain), FALSE);
2750       activate_chain (GST_PLAY_CHAIN (playsink->videochain), FALSE);
2751       if (playsink->videochain->ts_offset)
2752         gst_object_unref (playsink->videochain->ts_offset);
2753       playsink->videochain->ts_offset = NULL;
2754     }
2755
2756     if (playsink->videodeinterlacechain) {
2757       add_chain (GST_PLAY_CHAIN (playsink->videodeinterlacechain), FALSE);
2758       activate_chain (GST_PLAY_CHAIN (playsink->videodeinterlacechain), FALSE);
2759     }
2760
2761     if (playsink->video_pad)
2762       gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (playsink->video_pad), NULL);
2763   }
2764
2765   if (need_audio) {
2766     gboolean raw;
2767
2768     GST_DEBUG_OBJECT (playsink, "adding audio");
2769
2770     /* get a raw sink if we are asked for a raw pad */
2771     raw = playsink->audio_pad_raw;
2772
2773     if (playsink->audiochain) {
2774       /* try to reactivate the chain */
2775       if (!setup_audio_chain (playsink, raw)) {
2776         GST_DEBUG_OBJECT (playsink, "removing current audio chain");
2777         if (playsink->audio_tee_asrc) {
2778           gst_element_release_request_pad (playsink->audio_tee,
2779               playsink->audio_tee_asrc);
2780           gst_object_unref (playsink->audio_tee_asrc);
2781           playsink->audio_tee_asrc = NULL;
2782         }
2783
2784         if (playsink->audio_sinkpad_stream_synchronizer) {
2785           gst_element_release_request_pad (GST_ELEMENT_CAST
2786               (playsink->stream_synchronizer),
2787               playsink->audio_sinkpad_stream_synchronizer);
2788           gst_object_unref (playsink->audio_sinkpad_stream_synchronizer);
2789           playsink->audio_sinkpad_stream_synchronizer = NULL;
2790           gst_object_unref (playsink->audio_srcpad_stream_synchronizer);
2791           playsink->audio_srcpad_stream_synchronizer = NULL;
2792         }
2793
2794         add_chain (GST_PLAY_CHAIN (playsink->audiochain), FALSE);
2795
2796         /* Remove the sink from the bin to keep its state
2797          * and unparent it to allow reuse */
2798         if (playsink->audiochain->sink)
2799           gst_bin_remove (GST_BIN_CAST (playsink->audiochain->chain.bin),
2800               playsink->audiochain->sink);
2801
2802         activate_chain (GST_PLAY_CHAIN (playsink->audiochain), FALSE);
2803         disconnect_chain (playsink->audiochain, playsink);
2804         playsink->audiochain->volume = NULL;
2805         playsink->audiochain->mute = NULL;
2806         if (playsink->audiochain->ts_offset)
2807           gst_object_unref (playsink->audiochain->ts_offset);
2808         playsink->audiochain->ts_offset = NULL;
2809         free_chain ((GstPlayChain *) playsink->audiochain);
2810         playsink->audiochain = NULL;
2811         playsink->volume_changed = playsink->mute_changed = FALSE;
2812       }
2813     }
2814
2815     if (!playsink->audiochain) {
2816       GST_DEBUG_OBJECT (playsink, "creating new audio chain");
2817       playsink->audiochain = gen_audio_chain (playsink, raw);
2818     }
2819
2820     if (!playsink->audio_sinkpad_stream_synchronizer) {
2821       GValue item = { 0, };
2822       GstIterator *it;
2823
2824       playsink->audio_sinkpad_stream_synchronizer =
2825           gst_element_get_request_pad (GST_ELEMENT_CAST
2826           (playsink->stream_synchronizer), "sink_%u");
2827       it = gst_pad_iterate_internal_links
2828           (playsink->audio_sinkpad_stream_synchronizer);
2829       g_assert (it);
2830       gst_iterator_next (it, &item);
2831       playsink->audio_srcpad_stream_synchronizer = g_value_dup_object (&item);
2832       g_value_unset (&item);
2833       g_assert (playsink->audio_srcpad_stream_synchronizer);
2834       gst_iterator_free (it);
2835     }
2836
2837     if (playsink->audiochain) {
2838       GST_DEBUG_OBJECT (playsink, "adding audio chain");
2839       if (playsink->audio_tee_asrc == NULL) {
2840         playsink->audio_tee_asrc =
2841             gst_element_get_request_pad (playsink->audio_tee, "src_%u");
2842       }
2843       add_chain (GST_PLAY_CHAIN (playsink->audiochain), TRUE);
2844       activate_chain (GST_PLAY_CHAIN (playsink->audiochain), TRUE);
2845       gst_pad_link_full (playsink->audio_tee_asrc,
2846           playsink->audio_sinkpad_stream_synchronizer,
2847           GST_PAD_LINK_CHECK_NOTHING);
2848       gst_pad_link_full (playsink->audio_srcpad_stream_synchronizer,
2849           playsink->audiochain->sinkpad, GST_PAD_LINK_CHECK_NOTHING);
2850     }
2851   } else {
2852     GST_DEBUG_OBJECT (playsink, "no audio needed");
2853     /* we have no audio or we are requested to not play audio */
2854     if (playsink->audiochain) {
2855       GST_DEBUG_OBJECT (playsink, "removing audio chain");
2856       /* release the audio pad */
2857       if (playsink->audio_tee_asrc) {
2858         gst_element_release_request_pad (playsink->audio_tee,
2859             playsink->audio_tee_asrc);
2860         gst_object_unref (playsink->audio_tee_asrc);
2861         playsink->audio_tee_asrc = NULL;
2862       }
2863
2864       if (playsink->audio_sinkpad_stream_synchronizer) {
2865         gst_element_release_request_pad (GST_ELEMENT_CAST
2866             (playsink->stream_synchronizer),
2867             playsink->audio_sinkpad_stream_synchronizer);
2868         gst_object_unref (playsink->audio_sinkpad_stream_synchronizer);
2869         playsink->audio_sinkpad_stream_synchronizer = NULL;
2870         gst_object_unref (playsink->audio_srcpad_stream_synchronizer);
2871         playsink->audio_srcpad_stream_synchronizer = NULL;
2872       }
2873
2874       if (playsink->audiochain->sink_volume) {
2875         disconnect_chain (playsink->audiochain, playsink);
2876         playsink->audiochain->volume = NULL;
2877         playsink->audiochain->mute = NULL;
2878         if (playsink->audiochain->ts_offset)
2879           gst_object_unref (playsink->audiochain->ts_offset);
2880         playsink->audiochain->ts_offset = NULL;
2881       }
2882       add_chain (GST_PLAY_CHAIN (playsink->audiochain), FALSE);
2883       activate_chain (GST_PLAY_CHAIN (playsink->audiochain), FALSE);
2884     }
2885   }
2886
2887   if (need_vis) {
2888     GstPad *srcpad;
2889
2890     if (!playsink->vischain)
2891       playsink->vischain = gen_vis_chain (playsink);
2892
2893     GST_DEBUG_OBJECT (playsink, "adding visualisation");
2894
2895     if (playsink->vischain) {
2896       GST_DEBUG_OBJECT (playsink, "setting up vis chain");
2897       srcpad =
2898           gst_element_get_static_pad (playsink->vischain->chain.bin, "src");
2899       add_chain (GST_PLAY_CHAIN (playsink->vischain), TRUE);
2900       activate_chain (GST_PLAY_CHAIN (playsink->vischain), TRUE);
2901       if (playsink->audio_tee_vissrc == NULL) {
2902         playsink->audio_tee_vissrc =
2903             gst_element_get_request_pad (playsink->audio_tee, "src_%u");
2904       }
2905       gst_pad_link_full (playsink->audio_tee_vissrc,
2906           playsink->vischain->sinkpad, GST_PAD_LINK_CHECK_NOTHING);
2907       gst_pad_link_full (srcpad, playsink->video_sinkpad_stream_synchronizer,
2908           GST_PAD_LINK_CHECK_NOTHING);
2909       gst_pad_link_full (playsink->video_srcpad_stream_synchronizer,
2910           playsink->videochain->sinkpad, GST_PAD_LINK_CHECK_NOTHING);
2911       gst_object_unref (srcpad);
2912     }
2913   } else {
2914     GST_DEBUG_OBJECT (playsink, "no vis needed");
2915     if (playsink->vischain) {
2916       if (playsink->audio_tee_vissrc) {
2917         gst_element_release_request_pad (playsink->audio_tee,
2918             playsink->audio_tee_vissrc);
2919         gst_object_unref (playsink->audio_tee_vissrc);
2920         playsink->audio_tee_vissrc = NULL;
2921       }
2922       GST_DEBUG_OBJECT (playsink, "removing vis chain");
2923       add_chain (GST_PLAY_CHAIN (playsink->vischain), FALSE);
2924       activate_chain (GST_PLAY_CHAIN (playsink->vischain), FALSE);
2925     }
2926   }
2927
2928   if (need_text) {
2929     GST_DEBUG_OBJECT (playsink, "adding text");
2930     if (!playsink->textchain) {
2931       GST_DEBUG_OBJECT (playsink, "creating text chain");
2932       playsink->textchain = gen_text_chain (playsink);
2933     }
2934     if (playsink->textchain) {
2935       GstIterator *it;
2936
2937       GST_DEBUG_OBJECT (playsink, "adding text chain");
2938       if (playsink->textchain->overlay)
2939         g_object_set (G_OBJECT (playsink->textchain->overlay), "silent", FALSE,
2940             NULL);
2941       add_chain (GST_PLAY_CHAIN (playsink->textchain), TRUE);
2942
2943       if (!playsink->text_sinkpad_stream_synchronizer) {
2944         GValue item = { 0, };
2945
2946         playsink->text_sinkpad_stream_synchronizer =
2947             gst_element_get_request_pad (GST_ELEMENT_CAST
2948             (playsink->stream_synchronizer), "sink_%u");
2949         it = gst_pad_iterate_internal_links
2950             (playsink->text_sinkpad_stream_synchronizer);
2951         g_assert (it);
2952         gst_iterator_next (it, &item);
2953         playsink->text_srcpad_stream_synchronizer = g_value_dup_object (&item);
2954         g_value_unset (&item);
2955         g_assert (playsink->text_srcpad_stream_synchronizer);
2956         gst_iterator_free (it);
2957
2958         gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (playsink->text_pad),
2959             playsink->text_sinkpad_stream_synchronizer);
2960         gst_pad_link_full (playsink->text_srcpad_stream_synchronizer,
2961             playsink->textchain->textsinkpad, GST_PAD_LINK_CHECK_NOTHING);
2962       }
2963
2964       if (need_vis || need_video) {
2965         if (need_vis) {
2966           GstPad *srcpad;
2967
2968           srcpad =
2969               gst_element_get_static_pad (playsink->vischain->chain.bin, "src");
2970           gst_pad_unlink (srcpad, playsink->videochain->sinkpad);
2971           gst_pad_link_full (srcpad, playsink->textchain->videosinkpad,
2972               GST_PAD_LINK_CHECK_NOTHING);
2973           gst_object_unref (srcpad);
2974         } else {
2975           if (need_deinterlace)
2976             gst_pad_link_full (playsink->videodeinterlacechain->srcpad,
2977                 playsink->textchain->videosinkpad, GST_PAD_LINK_CHECK_NOTHING);
2978           else
2979             gst_pad_link_full (playsink->video_srcpad_stream_synchronizer,
2980                 playsink->textchain->videosinkpad, GST_PAD_LINK_CHECK_NOTHING);
2981         }
2982         gst_pad_link_full (playsink->textchain->srcpad,
2983             playsink->videochain->sinkpad, GST_PAD_LINK_CHECK_NOTHING);
2984       }
2985
2986       activate_chain (GST_PLAY_CHAIN (playsink->textchain), TRUE);
2987     }
2988   } else {
2989     GST_DEBUG_OBJECT (playsink, "no text needed");
2990     /* we have no subtitles/text or we are requested to not show them */
2991
2992     if (playsink->text_sinkpad_stream_synchronizer) {
2993       gst_element_release_request_pad (GST_ELEMENT_CAST
2994           (playsink->stream_synchronizer),
2995           playsink->text_sinkpad_stream_synchronizer);
2996       gst_object_unref (playsink->text_sinkpad_stream_synchronizer);
2997       playsink->text_sinkpad_stream_synchronizer = NULL;
2998       gst_object_unref (playsink->text_srcpad_stream_synchronizer);
2999       playsink->text_srcpad_stream_synchronizer = NULL;
3000     }
3001
3002     if (playsink->textchain) {
3003       if (playsink->text_pad == NULL) {
3004         /* no text pad, remove the chain entirely */
3005         GST_DEBUG_OBJECT (playsink, "removing text chain");
3006         add_chain (GST_PLAY_CHAIN (playsink->textchain), FALSE);
3007         activate_chain (GST_PLAY_CHAIN (playsink->textchain), FALSE);
3008       } else {
3009         /* we have a chain and a textpad, turn the subtitles off */
3010         GST_DEBUG_OBJECT (playsink, "turning off the text");
3011         if (playsink->textchain->overlay)
3012           g_object_set (G_OBJECT (playsink->textchain->overlay), "silent", TRUE,
3013               NULL);
3014       }
3015     }
3016     if (!need_video && playsink->video_pad) {
3017       if (playsink->video_sinkpad_stream_synchronizer) {
3018         gst_element_release_request_pad (GST_ELEMENT_CAST
3019             (playsink->stream_synchronizer),
3020             playsink->video_sinkpad_stream_synchronizer);
3021         gst_object_unref (playsink->video_sinkpad_stream_synchronizer);
3022         playsink->video_sinkpad_stream_synchronizer = NULL;
3023         gst_object_unref (playsink->video_srcpad_stream_synchronizer);
3024         playsink->video_srcpad_stream_synchronizer = NULL;
3025       }
3026
3027       gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (playsink->video_pad), NULL);
3028     }
3029
3030     if (playsink->text_pad && !playsink->textchain)
3031       gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (playsink->text_pad), NULL);
3032   }
3033   update_av_offset (playsink);
3034   do_async_done (playsink);
3035   GST_PLAY_SINK_UNLOCK (playsink);
3036
3037   return TRUE;
3038
3039   /* ERRORS */
3040 no_chain:
3041   {
3042     /* gen_ chain already posted error */
3043     GST_DEBUG_OBJECT (playsink, "failed to setup chain");
3044     GST_PLAY_SINK_UNLOCK (playsink);
3045     return FALSE;
3046   }
3047 }
3048
3049 /**
3050  * gst_play_sink_set_flags:
3051  * @playsink: a #GstPlaySink
3052  * @flags: #GstPlayFlags
3053  *
3054  * Configure @flags on @playsink. The flags control the behaviour of @playsink
3055  * when constructing the sink pipelins.
3056  *
3057  * Returns: TRUE if the flags could be configured.
3058  */
3059 gboolean
3060 gst_play_sink_set_flags (GstPlaySink * playsink, GstPlayFlags flags)
3061 {
3062   g_return_val_if_fail (GST_IS_PLAY_SINK (playsink), FALSE);
3063
3064   GST_OBJECT_LOCK (playsink);
3065   playsink->flags = flags;
3066   GST_OBJECT_UNLOCK (playsink);
3067
3068   return TRUE;
3069 }
3070
3071 /**
3072  * gst_play_sink_get_flags:
3073  * @playsink: a #GstPlaySink
3074  *
3075  * Get the flags of @playsink. That flags control the behaviour of the sink when
3076  * it constructs the sink pipelines.
3077  *
3078  * Returns: the currently configured #GstPlayFlags.
3079  */
3080 GstPlayFlags
3081 gst_play_sink_get_flags (GstPlaySink * playsink)
3082 {
3083   GstPlayFlags res;
3084
3085   g_return_val_if_fail (GST_IS_PLAY_SINK (playsink), 0);
3086
3087   GST_OBJECT_LOCK (playsink);
3088   res = playsink->flags;
3089   GST_OBJECT_UNLOCK (playsink);
3090
3091   return res;
3092 }
3093
3094 void
3095 gst_play_sink_set_font_desc (GstPlaySink * playsink, const gchar * desc)
3096 {
3097   GstPlayTextChain *chain;
3098
3099   GST_PLAY_SINK_LOCK (playsink);
3100   chain = (GstPlayTextChain *) playsink->textchain;
3101   g_free (playsink->font_desc);
3102   playsink->font_desc = g_strdup (desc);
3103   if (chain && chain->overlay) {
3104     g_object_set (chain->overlay, "font-desc", desc, NULL);
3105   }
3106   GST_PLAY_SINK_UNLOCK (playsink);
3107 }
3108
3109 gchar *
3110 gst_play_sink_get_font_desc (GstPlaySink * playsink)
3111 {
3112   gchar *result = NULL;
3113   GstPlayTextChain *chain;
3114
3115   GST_PLAY_SINK_LOCK (playsink);
3116   chain = (GstPlayTextChain *) playsink->textchain;
3117   if (chain && chain->overlay) {
3118     g_object_get (chain->overlay, "font-desc", &result, NULL);
3119     playsink->font_desc = g_strdup (result);
3120   } else {
3121     result = g_strdup (playsink->font_desc);
3122   }
3123   GST_PLAY_SINK_UNLOCK (playsink);
3124
3125   return result;
3126 }
3127
3128 void
3129 gst_play_sink_set_subtitle_encoding (GstPlaySink * playsink,
3130     const gchar * encoding)
3131 {
3132   GstPlayTextChain *chain;
3133
3134   GST_PLAY_SINK_LOCK (playsink);
3135   chain = (GstPlayTextChain *) playsink->textchain;
3136   g_free (playsink->subtitle_encoding);
3137   playsink->subtitle_encoding = g_strdup (encoding);
3138   if (chain && chain->overlay) {
3139     g_object_set (chain->overlay, "subtitle-encoding", encoding, NULL);
3140   }
3141   GST_PLAY_SINK_UNLOCK (playsink);
3142 }
3143
3144 gchar *
3145 gst_play_sink_get_subtitle_encoding (GstPlaySink * playsink)
3146 {
3147   gchar *result = NULL;
3148   GstPlayTextChain *chain;
3149
3150   GST_PLAY_SINK_LOCK (playsink);
3151   chain = (GstPlayTextChain *) playsink->textchain;
3152   if (chain && chain->overlay) {
3153     g_object_get (chain->overlay, "subtitle-encoding", &result, NULL);
3154     playsink->subtitle_encoding = g_strdup (result);
3155   } else {
3156     result = g_strdup (playsink->subtitle_encoding);
3157   }
3158   GST_PLAY_SINK_UNLOCK (playsink);
3159
3160   return result;
3161 }
3162
3163 static void
3164 update_av_offset (GstPlaySink * playsink)
3165 {
3166   gint64 av_offset;
3167   GstPlayAudioChain *achain;
3168   GstPlayVideoChain *vchain;
3169
3170   av_offset = playsink->av_offset;
3171   achain = (GstPlayAudioChain *) playsink->audiochain;
3172   vchain = (GstPlayVideoChain *) playsink->videochain;
3173
3174   if (achain && vchain && achain->ts_offset && vchain->ts_offset) {
3175     g_object_set (achain->ts_offset, "ts-offset", MAX (0, -av_offset), NULL);
3176     g_object_set (vchain->ts_offset, "ts-offset", MAX (0, av_offset), NULL);
3177   } else {
3178     GST_LOG_OBJECT (playsink, "no ts_offset elements");
3179   }
3180 }
3181
3182 void
3183 gst_play_sink_set_av_offset (GstPlaySink * playsink, gint64 av_offset)
3184 {
3185   GST_PLAY_SINK_LOCK (playsink);
3186   playsink->av_offset = av_offset;
3187   update_av_offset (playsink);
3188   GST_PLAY_SINK_UNLOCK (playsink);
3189 }
3190
3191 gint64
3192 gst_play_sink_get_av_offset (GstPlaySink * playsink)
3193 {
3194   gint64 result;
3195
3196   GST_PLAY_SINK_LOCK (playsink);
3197   result = playsink->av_offset;
3198   GST_PLAY_SINK_UNLOCK (playsink);
3199
3200   return result;
3201 }
3202
3203 /**
3204  * gst_play_sink_get_last_sample:
3205  * @playsink: a #GstPlaySink
3206  *
3207  * Get the last displayed sample from @playsink. This sample is in the native
3208  * format of the sink element, the caps in the result sample contain the format
3209  * of the frame data.
3210  *
3211  * Returns: a #GstSample with the frame data or %NULL when no video frame is
3212  * available.
3213  */
3214 GstSample *
3215 gst_play_sink_get_last_sample (GstPlaySink * playsink)
3216 {
3217   GstSample *result = NULL;
3218   GstPlayVideoChain *chain;
3219
3220   GST_PLAY_SINK_LOCK (playsink);
3221   GST_DEBUG_OBJECT (playsink, "taking last sample");
3222   /* get the video chain if we can */
3223   if ((chain = (GstPlayVideoChain *) playsink->videochain)) {
3224     GST_DEBUG_OBJECT (playsink, "found video chain");
3225     /* see if the chain is active */
3226     if (chain->chain.activated && chain->sink) {
3227       GstElement *elem;
3228
3229       GST_DEBUG_OBJECT (playsink, "video chain active and has a sink");
3230
3231       /* find and get the last-buffer property now */
3232       if ((elem =
3233               gst_play_sink_find_property (playsink, chain->sink,
3234                   "last-sample", GST_TYPE_SAMPLE))) {
3235         GST_DEBUG_OBJECT (playsink, "getting last-sample property");
3236         g_object_get (elem, "last-sample", &result, NULL);
3237         gst_object_unref (elem);
3238       }
3239     }
3240   }
3241   GST_PLAY_SINK_UNLOCK (playsink);
3242
3243   return result;
3244 }
3245
3246 /**
3247  * gst_play_sink_convert_sample:
3248  * @playsink: a #GstPlaySink
3249  * @caps: a #GstCaps
3250  *
3251  * Get the last displayed frame from @playsink. If caps is %NULL, the video will
3252  * be in the native format of the sink element and the caps on the buffer
3253  * describe the format of the frame. If @caps is not %NULL, the video
3254  * frame will be converted to the format of the caps.
3255  *
3256  * Returns: a #GstBuffer with the frame data or %NULL when no video frame is
3257  * available or when the conversion failed.
3258  */
3259 GstSample *
3260 gst_play_sink_convert_sample (GstPlaySink * playsink, GstCaps * caps)
3261 {
3262   GstSample *result;
3263   GError *err = NULL;
3264
3265   result = gst_play_sink_get_last_sample (playsink);
3266   if (result != NULL && caps != NULL) {
3267     GstSample *temp;
3268
3269     temp = gst_video_convert_sample (result, caps, 25 * GST_SECOND, &err);
3270     if (temp == NULL && err)
3271       goto error;
3272
3273     gst_sample_unref (result);
3274     result = temp;
3275   }
3276   return result;
3277
3278   /* ERRORS */
3279 error:
3280   {
3281     /* I'm really uncertain whether we should make playsink post an error
3282      * on the bus or not. It's not like it's a critical issue regarding
3283      * playsink behaviour. */
3284     GST_ERROR ("Error converting frame: %s", err->message);
3285     gst_sample_unref (result);
3286     g_error_free (err);
3287     return NULL;
3288   }
3289 }
3290
3291 static gboolean
3292 is_raw_structure (GstStructure * s)
3293 {
3294   const gchar *name;
3295
3296   name = gst_structure_get_name (s);
3297
3298   if (g_str_equal (name, "video/x-raw") || g_str_equal (name, "audio/x-raw"))
3299     return TRUE;
3300   return FALSE;
3301 }
3302
3303 static gboolean
3304 is_raw_pad (GstPad * pad)
3305 {
3306   GstPad *peer = gst_pad_get_peer (pad);
3307   GstCaps *caps;
3308   gboolean raw = TRUE;
3309
3310   if (!peer)
3311     return raw;
3312
3313   caps = gst_pad_get_current_caps (peer);
3314   if (!caps) {
3315     guint i, n;
3316
3317     caps = gst_pad_query_caps (peer, NULL);
3318
3319     n = gst_caps_get_size (caps);
3320     for (i = 0; i < n; i++) {
3321       gboolean r = is_raw_structure (gst_caps_get_structure (caps, i));
3322
3323       if (i == 0) {
3324         raw = r;
3325       } else if (raw != r) {
3326         GST_ERROR_OBJECT (pad,
3327             "Caps contains raw and non-raw structures: %" GST_PTR_FORMAT, caps);
3328         raw = FALSE;
3329         break;
3330       }
3331     }
3332   } else {
3333     raw = is_raw_structure (gst_caps_get_structure (caps, 0));
3334   }
3335   gst_caps_unref (caps);
3336   gst_object_unref (peer);
3337
3338   return raw;
3339 }
3340
3341 static GstPadProbeReturn
3342 sinkpad_blocked_cb (GstPad * blockedpad, GstPadProbeInfo * info,
3343     gpointer user_data);
3344
3345 static void
3346 video_set_blocked (GstPlaySink * playsink, gboolean blocked)
3347 {
3348   if (playsink->video_pad) {
3349     GstPad *opad =
3350         GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD
3351             (playsink->video_pad)));
3352     if (blocked && playsink->video_block_id == 0) {
3353       playsink->video_block_id =
3354           gst_pad_add_probe (opad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
3355           sinkpad_blocked_cb, playsink, NULL);
3356     } else if (!blocked && playsink->video_block_id) {
3357       gst_pad_remove_probe (opad, playsink->video_block_id);
3358       PENDING_FLAG_UNSET (playsink, GST_PLAY_SINK_TYPE_VIDEO_RAW);
3359       PENDING_FLAG_UNSET (playsink, GST_PLAY_SINK_TYPE_VIDEO);
3360       playsink->video_block_id = 0;
3361       playsink->video_pad_blocked = FALSE;
3362     }
3363     gst_object_unref (opad);
3364   }
3365 }
3366
3367 static void
3368 audio_set_blocked (GstPlaySink * playsink, gboolean blocked)
3369 {
3370   if (playsink->audio_pad) {
3371     GstPad *opad =
3372         GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD
3373             (playsink->audio_pad)));
3374     if (blocked && playsink->audio_block_id == 0) {
3375       playsink->audio_block_id =
3376           gst_pad_add_probe (opad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
3377           sinkpad_blocked_cb, playsink, NULL);
3378     } else if (!blocked && playsink->audio_block_id) {
3379       gst_pad_remove_probe (opad, playsink->audio_block_id);
3380       PENDING_FLAG_UNSET (playsink, GST_PLAY_SINK_TYPE_AUDIO_RAW);
3381       PENDING_FLAG_UNSET (playsink, GST_PLAY_SINK_TYPE_AUDIO);
3382       playsink->audio_block_id = 0;
3383       playsink->audio_pad_blocked = FALSE;
3384     }
3385     gst_object_unref (opad);
3386   }
3387 }
3388
3389 static void
3390 text_set_blocked (GstPlaySink * playsink, gboolean blocked)
3391 {
3392   if (playsink->text_pad) {
3393     GstPad *opad =
3394         GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD
3395             (playsink->text_pad)));
3396     if (blocked && playsink->text_block_id == 0) {
3397       playsink->text_block_id =
3398           gst_pad_add_probe (opad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
3399           sinkpad_blocked_cb, playsink, NULL);
3400     } else if (!blocked && playsink->text_block_id) {
3401       gst_pad_remove_probe (opad, playsink->text_block_id);
3402       PENDING_FLAG_UNSET (playsink, GST_PLAY_SINK_TYPE_TEXT);
3403       playsink->text_block_id = 0;
3404       playsink->text_pad_blocked = FALSE;
3405     }
3406     gst_object_unref (opad);
3407   }
3408 }
3409
3410 static GstPadProbeReturn
3411 sinkpad_blocked_cb (GstPad * blockedpad, GstPadProbeInfo * info,
3412     gpointer user_data)
3413 {
3414   GstPlaySink *playsink = (GstPlaySink *) user_data;
3415   GstPad *pad;
3416
3417   GST_PLAY_SINK_LOCK (playsink);
3418
3419   pad = GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD (blockedpad)));
3420   if (pad == playsink->video_pad) {
3421     playsink->video_pad_blocked = TRUE;
3422     GST_DEBUG_OBJECT (pad, "Video pad blocked");
3423   } else if (pad == playsink->audio_pad) {
3424     playsink->audio_pad_blocked = TRUE;
3425     GST_DEBUG_OBJECT (pad, "Audio pad blocked");
3426   } else if (pad == playsink->text_pad) {
3427     playsink->text_pad_blocked = TRUE;
3428     GST_DEBUG_OBJECT (pad, "Text pad blocked");
3429   }
3430
3431   /* We reconfigure when for ALL streams:
3432    * * there isn't a pad
3433    * * OR the pad is blocked
3434    * * OR there are no pending blocks on that pad
3435    */
3436
3437   if ((!playsink->video_pad || playsink->video_pad_blocked
3438           || !PENDING_VIDEO_BLOCK (playsink)) && (!playsink->audio_pad
3439           || playsink->audio_pad_blocked || !PENDING_AUDIO_BLOCK (playsink))
3440       && (!playsink->text_pad || playsink->text_pad_blocked
3441           || !PENDING_TEXT_BLOCK (playsink))) {
3442     GST_DEBUG_OBJECT (playsink, "All pads blocked -- reconfiguring");
3443
3444     if (playsink->video_pad) {
3445       playsink->video_pad_raw = is_raw_pad (playsink->video_pad);
3446       GST_DEBUG_OBJECT (playsink, "Video pad is raw: %d",
3447           playsink->video_pad_raw);
3448     }
3449
3450     if (playsink->audio_pad) {
3451       playsink->audio_pad_raw = is_raw_pad (playsink->audio_pad);
3452       GST_DEBUG_OBJECT (playsink, "Audio pad is raw: %d",
3453           playsink->audio_pad_raw);
3454     }
3455
3456     gst_play_sink_reconfigure (playsink);
3457
3458     video_set_blocked (playsink, FALSE);
3459     audio_set_blocked (playsink, FALSE);
3460     text_set_blocked (playsink, FALSE);
3461   }
3462
3463   gst_object_unref (pad);
3464
3465   GST_PLAY_SINK_UNLOCK (playsink);
3466
3467   return GST_PAD_PROBE_OK;
3468 }
3469
3470 static void
3471 caps_notify_cb (GstPad * pad, GParamSpec * unused, GstPlaySink * playsink)
3472 {
3473   gboolean reconfigure = FALSE;
3474   GstCaps *caps;
3475   gboolean raw;
3476
3477   g_object_get (pad, "caps", &caps, NULL);
3478   if (!caps)
3479     return;
3480
3481   if (pad == playsink->audio_pad) {
3482     raw = is_raw_pad (pad);
3483     reconfigure = (! !playsink->audio_pad_raw != ! !raw)
3484         && playsink->audiochain;
3485     GST_DEBUG_OBJECT (pad,
3486         "Audio caps changed: raw %d reconfigure %d caps %" GST_PTR_FORMAT, raw,
3487         reconfigure, caps);
3488   } else if (pad == playsink->video_pad) {
3489     raw = is_raw_pad (pad);
3490     reconfigure = (! !playsink->video_pad_raw != ! !raw)
3491         && playsink->videochain;
3492     GST_DEBUG_OBJECT (pad,
3493         "Video caps changed: raw %d reconfigure %d caps %" GST_PTR_FORMAT, raw,
3494         reconfigure, caps);
3495   }
3496
3497   gst_caps_unref (caps);
3498
3499   if (reconfigure) {
3500     GST_PLAY_SINK_LOCK (playsink);
3501     video_set_blocked (playsink, TRUE);
3502     audio_set_blocked (playsink, TRUE);
3503     text_set_blocked (playsink, TRUE);
3504     GST_PLAY_SINK_UNLOCK (playsink);
3505   }
3506 }
3507
3508 /**
3509  * gst_play_sink_request_pad
3510  * @playsink: a #GstPlaySink
3511  * @type: a #GstPlaySinkType
3512  *
3513  * Create or return a pad of @type.
3514  *
3515  * Returns: a #GstPad of @type or %NULL when the pad could not be created.
3516  */
3517 GstPad *
3518 gst_play_sink_request_pad (GstPlaySink * playsink, GstPlaySinkType type)
3519 {
3520   GstPad *res = NULL;
3521   gboolean created = FALSE;
3522   gboolean activate = TRUE;
3523   const gchar *pad_name = NULL;
3524   gulong *block_id = NULL;
3525
3526   GST_DEBUG_OBJECT (playsink, "request pad type %d", type);
3527
3528   GST_PLAY_SINK_LOCK (playsink);
3529   switch (type) {
3530     case GST_PLAY_SINK_TYPE_AUDIO_RAW:
3531     case GST_PLAY_SINK_TYPE_AUDIO:
3532       pad_name = "audio_sink";
3533       if (!playsink->audio_tee) {
3534         GST_LOG_OBJECT (playsink, "creating tee");
3535         /* create tee when needed. This element will feed the audio sink chain
3536          * and the vis chain. */
3537         playsink->audio_tee = gst_element_factory_make ("tee", "audiotee");
3538         if (playsink->audio_tee == NULL) {
3539           post_missing_element_message (playsink, "tee");
3540           GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
3541               (_("Missing element '%s' - check your GStreamer installation."),
3542                   "tee"), (NULL));
3543           res = NULL;
3544           break;
3545         } else {
3546           playsink->audio_tee_sink =
3547               gst_element_get_static_pad (playsink->audio_tee, "sink");
3548           gst_bin_add (GST_BIN_CAST (playsink), playsink->audio_tee);
3549           gst_element_set_state (playsink->audio_tee, GST_STATE_PAUSED);
3550         }
3551       } else {
3552         gst_element_set_state (playsink->audio_tee, GST_STATE_PAUSED);
3553       }
3554       if (!playsink->audio_pad) {
3555         GST_LOG_OBJECT (playsink, "ghosting tee sinkpad");
3556         playsink->audio_pad =
3557             gst_ghost_pad_new (pad_name, playsink->audio_tee_sink);
3558         g_signal_connect (G_OBJECT (playsink->audio_pad), "notify::caps",
3559             G_CALLBACK (caps_notify_cb), playsink);
3560         created = TRUE;
3561       }
3562       playsink->audio_pad_raw = FALSE;
3563       res = playsink->audio_pad;
3564       block_id = &playsink->audio_block_id;
3565       break;
3566     case GST_PLAY_SINK_TYPE_VIDEO_RAW:
3567     case GST_PLAY_SINK_TYPE_VIDEO:
3568       pad_name = "video_sink";
3569       if (!playsink->video_pad) {
3570         GST_LOG_OBJECT (playsink, "ghosting videosink");
3571         playsink->video_pad =
3572             gst_ghost_pad_new_no_target (pad_name, GST_PAD_SINK);
3573         g_signal_connect (G_OBJECT (playsink->video_pad), "notify::caps",
3574             G_CALLBACK (caps_notify_cb), playsink);
3575         created = TRUE;
3576       }
3577       playsink->video_pad_raw = FALSE;
3578       res = playsink->video_pad;
3579       block_id = &playsink->video_block_id;
3580       break;
3581     case GST_PLAY_SINK_TYPE_TEXT:
3582       GST_LOG_OBJECT (playsink, "ghosting text");
3583       if (!playsink->text_pad) {
3584         playsink->text_pad =
3585             gst_ghost_pad_new_no_target ("text_sink", GST_PAD_SINK);
3586         created = TRUE;
3587       }
3588       res = playsink->text_pad;
3589       block_id = &playsink->text_block_id;
3590       break;
3591     case GST_PLAY_SINK_TYPE_FLUSHING:
3592     {
3593       gchar *padname;
3594
3595       /* we need a unique padname for the flushing pad. */
3596       padname = g_strdup_printf ("flushing_%u", playsink->count);
3597       res = gst_ghost_pad_new_no_target (padname, GST_PAD_SINK);
3598       g_free (padname);
3599       playsink->count++;
3600       activate = FALSE;
3601       created = TRUE;
3602       break;
3603     }
3604     default:
3605       res = NULL;
3606       break;
3607   }
3608   GST_PLAY_SINK_UNLOCK (playsink);
3609
3610   if (created && res) {
3611     /* we have to add the pad when it's active or we get an error when the
3612      * element is 'running' */
3613     gst_pad_set_active (res, TRUE);
3614     gst_element_add_pad (GST_ELEMENT_CAST (playsink), res);
3615     if (block_id && *block_id == 0) {
3616       GstPad *blockpad =
3617           GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD (res)));
3618
3619       *block_id =
3620           gst_pad_add_probe (blockpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
3621           sinkpad_blocked_cb, playsink, NULL);
3622       PENDING_FLAG_SET (playsink, type);
3623       gst_object_unref (blockpad);
3624     }
3625     if (!activate)
3626       gst_pad_set_active (res, activate);
3627   }
3628
3629   return res;
3630 }
3631
3632 static GstPad *
3633 gst_play_sink_request_new_pad (GstElement * element, GstPadTemplate * templ,
3634     const gchar * name, const GstCaps * caps)
3635 {
3636   GstPlaySink *psink;
3637   GstPad *pad;
3638   GstPlaySinkType type;
3639   const gchar *tplname;
3640
3641   g_return_val_if_fail (templ != NULL, NULL);
3642
3643   GST_DEBUG_OBJECT (element, "name:%s", name);
3644
3645   psink = GST_PLAY_SINK (element);
3646   tplname = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
3647
3648   /* Figure out the GstPlaySinkType based on the template */
3649   if (!strcmp (tplname, "audio_sink"))
3650     type = GST_PLAY_SINK_TYPE_AUDIO;
3651   else if (!strcmp (tplname, "audio_raw_sink"))
3652     type = GST_PLAY_SINK_TYPE_AUDIO_RAW;
3653   else if (!strcmp (tplname, "video_sink"))
3654     type = GST_PLAY_SINK_TYPE_VIDEO;
3655   else if (!strcmp (tplname, "video_raw_sink"))
3656     type = GST_PLAY_SINK_TYPE_VIDEO_RAW;
3657   else if (!strcmp (tplname, "text_sink"))
3658     type = GST_PLAY_SINK_TYPE_TEXT;
3659   else
3660     goto unknown_template;
3661
3662   pad = gst_play_sink_request_pad (psink, type);
3663   return pad;
3664
3665 unknown_template:
3666   GST_WARNING_OBJECT (element, "Unknown pad template");
3667   return NULL;
3668 }
3669
3670 void
3671 gst_play_sink_release_pad (GstPlaySink * playsink, GstPad * pad)
3672 {
3673   GstPad **res = NULL;
3674   gboolean untarget = TRUE;
3675
3676   GST_DEBUG_OBJECT (playsink, "release pad %" GST_PTR_FORMAT, pad);
3677
3678   GST_PLAY_SINK_LOCK (playsink);
3679   if (pad == playsink->video_pad) {
3680     res = &playsink->video_pad;
3681     g_signal_handlers_disconnect_by_func (playsink->video_pad, caps_notify_cb,
3682         playsink);
3683   } else if (pad == playsink->audio_pad) {
3684     res = &playsink->audio_pad;
3685     g_signal_handlers_disconnect_by_func (playsink->audio_pad, caps_notify_cb,
3686         playsink);
3687   } else if (pad == playsink->text_pad) {
3688     res = &playsink->text_pad;
3689   } else {
3690     /* try to release the given pad anyway, these could be the FLUSHING pads. */
3691     res = &pad;
3692     untarget = FALSE;
3693   }
3694   GST_PLAY_SINK_UNLOCK (playsink);
3695
3696   if (*res) {
3697     GST_DEBUG_OBJECT (playsink, "deactivate pad %" GST_PTR_FORMAT, *res);
3698     gst_pad_set_active (*res, FALSE);
3699     if (untarget) {
3700       GST_DEBUG_OBJECT (playsink, "untargeting pad %" GST_PTR_FORMAT, *res);
3701       gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (*res), NULL);
3702     }
3703     GST_DEBUG_OBJECT (playsink, "remove pad %" GST_PTR_FORMAT, *res);
3704     gst_element_remove_pad (GST_ELEMENT_CAST (playsink), *res);
3705     *res = NULL;
3706   }
3707 }
3708
3709 static void
3710 gst_play_sink_release_request_pad (GstElement * element, GstPad * pad)
3711 {
3712   GstPlaySink *psink = GST_PLAY_SINK (element);
3713
3714   gst_play_sink_release_pad (psink, pad);
3715 }
3716
3717 static void
3718 gst_play_sink_handle_message (GstBin * bin, GstMessage * message)
3719 {
3720   GstPlaySink *playsink;
3721
3722   playsink = GST_PLAY_SINK_CAST (bin);
3723
3724   switch (GST_MESSAGE_TYPE (message)) {
3725     case GST_MESSAGE_STEP_DONE:
3726     {
3727       GstFormat format;
3728       guint64 amount;
3729       gdouble rate;
3730       gboolean flush, intermediate, eos;
3731       guint64 duration;
3732
3733       GST_INFO_OBJECT (playsink, "Handling step-done message");
3734       gst_message_parse_step_done (message, &format, &amount, &rate, &flush,
3735           &intermediate, &duration, &eos);
3736
3737       if (format == GST_FORMAT_BUFFERS) {
3738         /* for the buffer format, we align the other streams */
3739         if (playsink->audiochain) {
3740           GstEvent *event;
3741
3742           event =
3743               gst_event_new_step (GST_FORMAT_TIME, duration, rate, flush,
3744               intermediate);
3745
3746           if (!gst_element_send_event (playsink->audiochain->chain.bin, event)) {
3747             GST_DEBUG_OBJECT (playsink, "Event failed when sent to audio sink");
3748           }
3749         }
3750       }
3751       GST_BIN_CLASS (gst_play_sink_parent_class)->handle_message (bin, message);
3752       break;
3753     }
3754     case GST_MESSAGE_ELEMENT:{
3755       if (gst_is_video_overlay_prepare_window_handle_message (message)) {
3756         GstVideoOverlay *overlay;
3757
3758         GST_OBJECT_LOCK (playsink);
3759         if (playsink->overlay_element
3760             && GST_OBJECT_CAST (playsink->overlay_element) !=
3761             GST_MESSAGE_SRC (message)) {
3762           gst_object_unref (playsink->overlay_element);
3763           playsink->overlay_element = NULL;
3764         }
3765
3766         if (!playsink->overlay_element)
3767           playsink->overlay_element =
3768               GST_VIDEO_OVERLAY (gst_object_ref (GST_MESSAGE_SRC (message)));
3769         overlay =
3770             GST_VIDEO_OVERLAY (gst_object_ref (playsink->overlay_element));
3771         GST_OBJECT_UNLOCK (playsink);
3772
3773         GST_DEBUG_OBJECT (playsink, "Got prepare-xwindow-id message");
3774
3775         if (playsink->overlay_handle_set)
3776           gst_video_overlay_set_window_handle (playsink->overlay_element,
3777               playsink->overlay_handle);
3778         if (playsink->overlay_handle_events_set)
3779           gst_video_overlay_handle_events (playsink->overlay_element,
3780               playsink->overlay_handle_events);
3781         if (playsink->overlay_render_rectangle_set)
3782           gst_video_overlay_set_render_rectangle (playsink->overlay_element,
3783               playsink->overlay_x, playsink->overlay_y,
3784               playsink->overlay_width, playsink->overlay_height);
3785
3786         gst_object_unref (overlay);
3787         gst_message_unref (message);
3788         gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (playsink));
3789       } else {
3790         GST_BIN_CLASS (gst_play_sink_parent_class)->handle_message (bin,
3791             message);
3792       }
3793       break;
3794     }
3795     default:
3796       GST_BIN_CLASS (gst_play_sink_parent_class)->handle_message (bin, message);
3797       break;
3798   }
3799 }
3800
3801 /* Send an event to our sinks until one of them works; don't then send to the
3802  * remaining sinks (unlike GstBin)
3803  * Special case: If a text sink is set we need to send the event
3804  * to them in case it's source is different from the a/v stream's source.
3805  */
3806 static gboolean
3807 gst_play_sink_send_event_to_sink (GstPlaySink * playsink, GstEvent * event)
3808 {
3809   gboolean res = TRUE;
3810   if (playsink->send_event_mode == MODE_FIRST) {
3811     if (playsink->textchain && playsink->textchain->sink) {
3812       gst_event_ref (event);
3813       if ((res =
3814               gst_element_send_event (playsink->textchain->chain.bin, event))) {
3815         GST_DEBUG_OBJECT (playsink, "Sent event successfully to text sink");
3816       } else {
3817         GST_DEBUG_OBJECT (playsink, "Event failed when sent to text sink");
3818       }
3819     }
3820
3821     if (playsink->videochain) {
3822       gst_event_ref (event);
3823       if ((res =
3824               gst_element_send_event (playsink->videochain->chain.bin,
3825                   event))) {
3826         GST_DEBUG_OBJECT (playsink, "Sent event successfully to video sink");
3827         goto done;
3828       }
3829       GST_DEBUG_OBJECT (playsink, "Event failed when sent to video sink");
3830     }
3831     if (playsink->audiochain) {
3832       gst_event_ref (event);
3833       if ((res =
3834               gst_element_send_event (playsink->audiochain->chain.bin,
3835                   event))) {
3836         GST_DEBUG_OBJECT (playsink, "Sent event successfully to audio sink");
3837         goto done;
3838       }
3839       GST_DEBUG_OBJECT (playsink, "Event failed when sent to audio sink");
3840     }
3841   } else {
3842     return
3843         GST_ELEMENT_CLASS (gst_play_sink_parent_class)->send_event
3844         (GST_ELEMENT_CAST (playsink), event);
3845   }
3846
3847 done:
3848   gst_event_unref (event);
3849   return res;
3850 }
3851
3852 /* We only want to send the event to a single sink (overriding GstBin's
3853  * behaviour), but we want to keep GstPipeline's behaviour - wrapping seek
3854  * events appropriately. So, this is a messy duplication of code. */
3855 static gboolean
3856 gst_play_sink_send_event (GstElement * element, GstEvent * event)
3857 {
3858   gboolean res = FALSE;
3859   GstEventType event_type = GST_EVENT_TYPE (event);
3860   GstPlaySink *playsink;
3861   playsink = GST_PLAY_SINK_CAST (element);
3862   switch (event_type) {
3863     case GST_EVENT_SEEK:
3864       GST_DEBUG_OBJECT (element, "Sending event to a sink");
3865       res = gst_play_sink_send_event_to_sink (playsink, event);
3866       break;
3867     case GST_EVENT_STEP:
3868     {
3869       GstFormat format;
3870       guint64 amount;
3871       gdouble rate;
3872       gboolean flush, intermediate;
3873       gst_event_parse_step (event, &format, &amount, &rate, &flush,
3874           &intermediate);
3875       if (format == GST_FORMAT_BUFFERS) {
3876         /* for buffers, we will try to step video frames, for other formats we
3877          * send the step to all sinks */
3878         res = gst_play_sink_send_event_to_sink (playsink, event);
3879       } else {
3880         res =
3881             GST_ELEMENT_CLASS (gst_play_sink_parent_class)->send_event (element,
3882             event);
3883       }
3884       break;
3885     }
3886     default:
3887       res =
3888           GST_ELEMENT_CLASS (gst_play_sink_parent_class)->send_event (element,
3889           event);
3890       break;
3891   }
3892   return res;
3893 }
3894
3895 static GstStateChangeReturn
3896 gst_play_sink_change_state (GstElement * element, GstStateChange transition)
3897 {
3898   GstStateChangeReturn ret;
3899   GstStateChangeReturn bret;
3900   GstPlaySink *playsink;
3901   playsink = GST_PLAY_SINK (element);
3902   switch (transition) {
3903     case GST_STATE_CHANGE_READY_TO_PAUSED:
3904       playsink->need_async_start = TRUE;
3905       /* we want to go async to PAUSED until we managed to configure and add the
3906        * sinks */
3907       do_async_start (playsink);
3908       ret = GST_STATE_CHANGE_ASYNC;
3909       break;
3910     case GST_STATE_CHANGE_PAUSED_TO_READY:
3911       /* unblock all pads here */
3912       GST_PLAY_SINK_LOCK (playsink);
3913       video_set_blocked (playsink, FALSE);
3914       audio_set_blocked (playsink, FALSE);
3915       text_set_blocked (playsink, FALSE);
3916       GST_PLAY_SINK_UNLOCK (playsink);
3917       /* fall through */
3918     case GST_STATE_CHANGE_READY_TO_NULL:
3919       if (playsink->audiochain && playsink->audiochain->sink_volume) {
3920         /* remove our links to the mute and volume elements when they were
3921          * provided by a sink */
3922         disconnect_chain (playsink->audiochain, playsink);
3923         playsink->audiochain->volume = NULL;
3924         playsink->audiochain->mute = NULL;
3925       }
3926
3927       if (playsink->audiochain && playsink->audiochain->ts_offset) {
3928         gst_object_unref (playsink->audiochain->ts_offset);
3929         playsink->audiochain->ts_offset = NULL;
3930       }
3931
3932       if (playsink->videochain && playsink->videochain->ts_offset) {
3933         gst_object_unref (playsink->videochain->ts_offset);
3934         playsink->videochain->ts_offset = NULL;
3935       }
3936
3937       GST_OBJECT_LOCK (playsink);
3938       if (playsink->overlay_element)
3939         gst_object_unref (playsink->overlay_element);
3940       playsink->overlay_element = NULL;
3941
3942       if (playsink->colorbalance_element) {
3943         g_signal_handlers_disconnect_by_func (playsink->colorbalance_element,
3944             G_CALLBACK (colorbalance_value_changed_cb), playsink);
3945         gst_object_unref (playsink->colorbalance_element);
3946       }
3947       playsink->colorbalance_element = NULL;
3948       GST_OBJECT_UNLOCK (playsink);
3949
3950       ret = GST_STATE_CHANGE_SUCCESS;
3951       break;
3952     default:
3953       /* all other state changes return SUCCESS by default, this value can be
3954        * overridden by the result of the children */
3955       ret = GST_STATE_CHANGE_SUCCESS;
3956       break;
3957   }
3958
3959   /* do the state change of the children */
3960   bret =
3961       GST_ELEMENT_CLASS (gst_play_sink_parent_class)->change_state (element,
3962       transition);
3963   /* now look at the result of our children and adjust the return value */
3964   switch (bret) {
3965     case GST_STATE_CHANGE_FAILURE:
3966       /* failure, we stop */
3967       goto activate_failed;
3968     case GST_STATE_CHANGE_NO_PREROLL:
3969       /* some child returned NO_PREROLL. This is strange but we never know. We
3970        * commit our async state change (if any) and return the NO_PREROLL */
3971       do_async_done (playsink);
3972       ret = bret;
3973       break;
3974     case GST_STATE_CHANGE_ASYNC:
3975       /* some child was async, return this */
3976       ret = bret;
3977       break;
3978     default:
3979       /* return our previously configured return value */
3980       break;
3981   }
3982
3983   switch (transition) {
3984     case GST_STATE_CHANGE_READY_TO_PAUSED:
3985       break;
3986     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3987       /* FIXME Release audio device when we implement that */
3988       playsink->need_async_start = TRUE;
3989       break;
3990     case GST_STATE_CHANGE_PAUSED_TO_READY:{
3991       if (playsink->video_sinkpad_stream_synchronizer) {
3992         gst_element_release_request_pad (GST_ELEMENT_CAST
3993             (playsink->stream_synchronizer),
3994             playsink->video_sinkpad_stream_synchronizer);
3995         gst_object_unref (playsink->video_sinkpad_stream_synchronizer);
3996         playsink->video_sinkpad_stream_synchronizer = NULL;
3997         gst_object_unref (playsink->video_srcpad_stream_synchronizer);
3998         playsink->video_srcpad_stream_synchronizer = NULL;
3999       }
4000       if (playsink->audio_sinkpad_stream_synchronizer) {
4001         gst_element_release_request_pad (GST_ELEMENT_CAST
4002             (playsink->stream_synchronizer),
4003             playsink->audio_sinkpad_stream_synchronizer);
4004         gst_object_unref (playsink->audio_sinkpad_stream_synchronizer);
4005         playsink->audio_sinkpad_stream_synchronizer = NULL;
4006         gst_object_unref (playsink->audio_srcpad_stream_synchronizer);
4007         playsink->audio_srcpad_stream_synchronizer = NULL;
4008       }
4009       if (playsink->text_sinkpad_stream_synchronizer) {
4010         gst_element_release_request_pad (GST_ELEMENT_CAST
4011             (playsink->stream_synchronizer),
4012             playsink->text_sinkpad_stream_synchronizer);
4013         gst_object_unref (playsink->text_sinkpad_stream_synchronizer);
4014         playsink->text_sinkpad_stream_synchronizer = NULL;
4015         gst_object_unref (playsink->text_srcpad_stream_synchronizer);
4016         playsink->text_srcpad_stream_synchronizer = NULL;
4017       }
4018     }
4019       /* fall through */
4020     case GST_STATE_CHANGE_READY_TO_NULL:
4021       /* remove sinks we added */
4022       if (playsink->videodeinterlacechain) {
4023         activate_chain (GST_PLAY_CHAIN (playsink->videodeinterlacechain),
4024             FALSE);
4025         add_chain (GST_PLAY_CHAIN (playsink->videodeinterlacechain), FALSE);
4026       }
4027       if (playsink->videochain) {
4028         activate_chain (GST_PLAY_CHAIN (playsink->videochain), FALSE);
4029         add_chain (GST_PLAY_CHAIN (playsink->videochain), FALSE);
4030       }
4031       if (playsink->audiochain) {
4032         activate_chain (GST_PLAY_CHAIN (playsink->audiochain), FALSE);
4033         add_chain (GST_PLAY_CHAIN (playsink->audiochain), FALSE);
4034       }
4035       if (playsink->vischain) {
4036         activate_chain (GST_PLAY_CHAIN (playsink->vischain), FALSE);
4037         add_chain (GST_PLAY_CHAIN (playsink->vischain), FALSE);
4038       }
4039       if (playsink->textchain) {
4040         activate_chain (GST_PLAY_CHAIN (playsink->textchain), FALSE);
4041         add_chain (GST_PLAY_CHAIN (playsink->textchain), FALSE);
4042       }
4043       do_async_done (playsink);
4044       /* when going to READY, keep elements around as long as possible,
4045        * so they may be re-used faster next time/url around.
4046        * when really going to NULL, clean up everything completely. */
4047       if (transition == GST_STATE_CHANGE_READY_TO_NULL) {
4048
4049         /* Unparent the sinks to allow reuse */
4050         if (playsink->videochain && playsink->videochain->sink)
4051           gst_bin_remove (GST_BIN_CAST (playsink->videochain->chain.bin),
4052               playsink->videochain->sink);
4053         if (playsink->audiochain && playsink->audiochain->sink)
4054           gst_bin_remove (GST_BIN_CAST (playsink->audiochain->chain.bin),
4055               playsink->audiochain->sink);
4056         if (playsink->textchain && playsink->textchain->sink)
4057           gst_bin_remove (GST_BIN_CAST (playsink->textchain->chain.bin),
4058               playsink->textchain->sink);
4059         if (playsink->audio_sink != NULL)
4060           gst_element_set_state (playsink->audio_sink, GST_STATE_NULL);
4061         if (playsink->video_sink != NULL)
4062           gst_element_set_state (playsink->video_sink, GST_STATE_NULL);
4063         if (playsink->visualisation != NULL)
4064           gst_element_set_state (playsink->visualisation, GST_STATE_NULL);
4065         if (playsink->text_sink != NULL)
4066           gst_element_set_state (playsink->text_sink, GST_STATE_NULL);
4067         free_chain ((GstPlayChain *) playsink->videodeinterlacechain);
4068         playsink->videodeinterlacechain = NULL;
4069         free_chain ((GstPlayChain *) playsink->videochain);
4070         playsink->videochain = NULL;
4071         free_chain ((GstPlayChain *) playsink->audiochain);
4072         playsink->audiochain = NULL;
4073         free_chain ((GstPlayChain *) playsink->vischain);
4074         playsink->vischain = NULL;
4075         free_chain ((GstPlayChain *) playsink->textchain);
4076         playsink->textchain = NULL;
4077       }
4078       break;
4079     default:
4080       break;
4081   }
4082   return ret;
4083   /* ERRORS */
4084 activate_failed:
4085   {
4086     GST_DEBUG_OBJECT (element,
4087         "element failed to change states -- activation problem?");
4088     return GST_STATE_CHANGE_FAILURE;
4089   }
4090 }
4091
4092 static void
4093 gst_play_sink_set_property (GObject * object, guint prop_id,
4094     const GValue * value, GParamSpec * spec)
4095 {
4096   GstPlaySink *playsink = GST_PLAY_SINK (object);
4097   switch (prop_id) {
4098     case PROP_FLAGS:
4099       gst_play_sink_set_flags (playsink, g_value_get_flags (value));
4100       break;
4101     case PROP_VOLUME:
4102       gst_play_sink_set_volume (playsink, g_value_get_double (value));
4103       break;
4104     case PROP_MUTE:
4105       gst_play_sink_set_mute (playsink, g_value_get_boolean (value));
4106       break;
4107     case PROP_FONT_DESC:
4108       gst_play_sink_set_font_desc (playsink, g_value_get_string (value));
4109       break;
4110     case PROP_SUBTITLE_ENCODING:
4111       gst_play_sink_set_subtitle_encoding (playsink,
4112           g_value_get_string (value));
4113       break;
4114     case PROP_VIS_PLUGIN:
4115       gst_play_sink_set_vis_plugin (playsink, g_value_get_object (value));
4116       break;
4117     case PROP_AV_OFFSET:
4118       gst_play_sink_set_av_offset (playsink, g_value_get_int64 (value));
4119       break;
4120     case PROP_VIDEO_SINK:
4121       gst_play_sink_set_sink (playsink, GST_PLAY_SINK_TYPE_VIDEO,
4122           g_value_get_object (value));
4123       break;
4124     case PROP_AUDIO_SINK:
4125       gst_play_sink_set_sink (playsink, GST_PLAY_SINK_TYPE_AUDIO,
4126           g_value_get_object (value));
4127       break;
4128     case PROP_TEXT_SINK:
4129       gst_play_sink_set_sink (playsink, GST_PLAY_SINK_TYPE_TEXT,
4130           g_value_get_object (value));
4131       break;
4132     case PROP_SEND_EVENT_MODE:
4133       playsink->send_event_mode = g_value_get_enum (value);
4134       break;
4135     default:
4136       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
4137       break;
4138   }
4139 }
4140
4141 static void
4142 gst_play_sink_get_property (GObject * object, guint prop_id,
4143     GValue * value, GParamSpec * spec)
4144 {
4145   GstPlaySink *playsink = GST_PLAY_SINK (object);
4146   switch (prop_id) {
4147     case PROP_FLAGS:
4148       g_value_set_flags (value, gst_play_sink_get_flags (playsink));
4149       break;
4150     case PROP_VOLUME:
4151       g_value_set_double (value, gst_play_sink_get_volume (playsink));
4152       break;
4153     case PROP_MUTE:
4154       g_value_set_boolean (value, gst_play_sink_get_mute (playsink));
4155       break;
4156     case PROP_FONT_DESC:
4157       g_value_take_string (value, gst_play_sink_get_font_desc (playsink));
4158       break;
4159     case PROP_SUBTITLE_ENCODING:
4160       g_value_take_string (value,
4161           gst_play_sink_get_subtitle_encoding (playsink));
4162       break;
4163     case PROP_VIS_PLUGIN:
4164       g_value_take_object (value, gst_play_sink_get_vis_plugin (playsink));
4165       break;
4166     case PROP_SAMPLE:
4167       gst_value_take_sample (value, gst_play_sink_get_last_sample (playsink));
4168       break;
4169     case PROP_AV_OFFSET:
4170       g_value_set_int64 (value, gst_play_sink_get_av_offset (playsink));
4171       break;
4172     case PROP_VIDEO_SINK:
4173       g_value_take_object (value, gst_play_sink_get_sink (playsink,
4174               GST_PLAY_SINK_TYPE_VIDEO));
4175       break;
4176     case PROP_AUDIO_SINK:
4177       g_value_take_object (value, gst_play_sink_get_sink (playsink,
4178               GST_PLAY_SINK_TYPE_AUDIO));
4179       break;
4180     case PROP_TEXT_SINK:
4181       g_value_take_object (value, gst_play_sink_get_sink (playsink,
4182               GST_PLAY_SINK_TYPE_TEXT));
4183       break;
4184     case PROP_SEND_EVENT_MODE:
4185       g_value_set_enum (value, playsink->send_event_mode);
4186       break;
4187     default:
4188       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
4189       break;
4190   }
4191 }
4192
4193 static void
4194 gst_play_sink_overlay_expose (GstVideoOverlay * overlay)
4195 {
4196   GstPlaySink *playsink = GST_PLAY_SINK (overlay);
4197   GstVideoOverlay *overlay_element;
4198
4199   GST_OBJECT_LOCK (playsink);
4200   if (playsink->overlay_element)
4201     overlay_element =
4202         GST_VIDEO_OVERLAY (gst_object_ref (playsink->overlay_element));
4203   else
4204     overlay_element = NULL;
4205   GST_OBJECT_UNLOCK (playsink);
4206
4207   if (overlay_element) {
4208     gst_video_overlay_expose (overlay_element);
4209     gst_object_unref (overlay_element);
4210   }
4211 }
4212
4213 static void
4214 gst_play_sink_overlay_handle_events (GstVideoOverlay * overlay,
4215     gboolean handle_events)
4216 {
4217   GstPlaySink *playsink = GST_PLAY_SINK (overlay);
4218   GstVideoOverlay *overlay_element;
4219
4220   GST_OBJECT_LOCK (playsink);
4221   if (playsink->overlay_element)
4222     overlay_element =
4223         GST_VIDEO_OVERLAY (gst_object_ref (playsink->overlay_element));
4224   else
4225     overlay_element = NULL;
4226   GST_OBJECT_UNLOCK (playsink);
4227
4228   playsink->overlay_handle_events_set = TRUE;
4229   playsink->overlay_handle_events = handle_events;
4230
4231   if (overlay_element) {
4232     gst_video_overlay_handle_events (overlay_element, handle_events);
4233     gst_object_unref (overlay_element);
4234   }
4235 }
4236
4237 static void
4238 gst_play_sink_overlay_set_render_rectangle (GstVideoOverlay * overlay, gint x,
4239     gint y, gint width, gint height)
4240 {
4241   GstPlaySink *playsink = GST_PLAY_SINK (overlay);
4242   GstVideoOverlay *overlay_element;
4243
4244   GST_OBJECT_LOCK (playsink);
4245   if (playsink->overlay_element)
4246     overlay_element =
4247         GST_VIDEO_OVERLAY (gst_object_ref (playsink->overlay_element));
4248   else
4249     overlay_element = NULL;
4250   GST_OBJECT_UNLOCK (playsink);
4251
4252   playsink->overlay_render_rectangle_set = TRUE;
4253   playsink->overlay_x = x;
4254   playsink->overlay_y = y;
4255   playsink->overlay_width = width;
4256   playsink->overlay_height = height;
4257
4258   if (overlay_element) {
4259     gst_video_overlay_set_render_rectangle (overlay_element, x, y, width,
4260         height);
4261     gst_object_unref (overlay_element);
4262   }
4263 }
4264
4265 static void
4266 gst_play_sink_overlay_set_window_handle (GstVideoOverlay * overlay,
4267     guintptr handle)
4268 {
4269   GstPlaySink *playsink = GST_PLAY_SINK (overlay);
4270   GstVideoOverlay *overlay_element;
4271
4272   GST_OBJECT_LOCK (playsink);
4273   if (playsink->overlay_element)
4274     overlay_element =
4275         GST_VIDEO_OVERLAY (gst_object_ref (playsink->overlay_element));
4276   else
4277     overlay_element = NULL;
4278   GST_OBJECT_UNLOCK (playsink);
4279
4280   playsink->overlay_handle_set = TRUE;
4281   playsink->overlay_handle = handle;
4282
4283   if (overlay_element) {
4284     gst_video_overlay_set_window_handle (overlay_element, handle);
4285     gst_object_unref (overlay_element);
4286   }
4287 }
4288
4289 static void
4290 gst_play_sink_overlay_init (gpointer g_iface, gpointer g_iface_data)
4291 {
4292   GstVideoOverlayInterface *iface = (GstVideoOverlayInterface *) g_iface;
4293   iface->expose = gst_play_sink_overlay_expose;
4294   iface->handle_events = gst_play_sink_overlay_handle_events;
4295   iface->set_render_rectangle = gst_play_sink_overlay_set_render_rectangle;
4296   iface->set_window_handle = gst_play_sink_overlay_set_window_handle;
4297 }
4298
4299 static void
4300 gst_play_sink_navigation_send_event (GstNavigation * navigation,
4301     GstStructure * structure)
4302 {
4303   GstPlaySink *playsink = GST_PLAY_SINK (navigation);
4304   GstBin *bin = NULL;
4305
4306   GST_PLAY_SINK_LOCK (playsink);
4307   if (playsink->videochain && playsink->videochain->chain.bin)
4308     bin = GST_BIN (gst_object_ref (playsink->videochain->chain.bin));
4309   GST_PLAY_SINK_UNLOCK (playsink);
4310
4311   if (bin) {
4312     GstElement *nav = gst_bin_get_by_interface (bin, GST_TYPE_NAVIGATION);
4313
4314     if (nav) {
4315       gst_navigation_send_event (GST_NAVIGATION (nav), structure);
4316       structure = NULL;
4317       gst_object_unref (nav);
4318     } else {
4319       GstEvent *event = gst_event_new_navigation (structure);
4320       structure = NULL;
4321       gst_element_send_event (GST_ELEMENT (bin), event);
4322     }
4323
4324     gst_object_unref (bin);
4325   }
4326
4327   if (structure)
4328     gst_structure_free (structure);
4329 }
4330
4331 static void
4332 gst_play_sink_navigation_init (gpointer g_iface, gpointer g_iface_data)
4333 {
4334   GstNavigationInterface *iface = (GstNavigationInterface *) g_iface;
4335
4336   iface->send_event = gst_play_sink_navigation_send_event;
4337 }
4338
4339 static const GList *
4340 gst_play_sink_colorbalance_list_channels (GstColorBalance * balance)
4341 {
4342   GstPlaySink *playsink = GST_PLAY_SINK (balance);
4343
4344   return playsink->colorbalance_channels;
4345 }
4346
4347 static void
4348 gst_play_sink_colorbalance_set_value (GstColorBalance * balance,
4349     GstColorBalanceChannel * proxy, gint value)
4350 {
4351   GstPlaySink *playsink = GST_PLAY_SINK (balance);
4352   GList *l;
4353   gint i;
4354   GstColorBalance *balance_element = NULL;
4355
4356   GST_OBJECT_LOCK (playsink);
4357   if (playsink->colorbalance_element)
4358     balance_element =
4359         GST_COLOR_BALANCE (gst_object_ref (playsink->colorbalance_element));
4360   GST_OBJECT_UNLOCK (playsink);
4361
4362   for (i = 0, l = playsink->colorbalance_channels; l; l = l->next, i++) {
4363     GstColorBalanceChannel *proxy_tmp = l->data;
4364     gdouble new_val;
4365
4366     if (proxy_tmp != proxy)
4367       continue;
4368
4369     playsink->colorbalance_values[i] = value;
4370
4371     if (balance_element) {
4372       GstColorBalanceChannel *channel = NULL;
4373       const GList *channels, *k;
4374
4375       channels = gst_color_balance_list_channels (balance_element);
4376       for (k = channels; k; k = k->next) {
4377         GstColorBalanceChannel *tmp = l->data;
4378
4379         if (g_strrstr (tmp->label, proxy->label)) {
4380           channel = tmp;
4381           break;
4382         }
4383       }
4384
4385       g_assert (channel);
4386
4387       /* Convert to [0, 1] range */
4388       new_val =
4389           ((gdouble) value -
4390           (gdouble) proxy->min_value) / ((gdouble) proxy->max_value -
4391           (gdouble) proxy->min_value);
4392       /* Convert to channel range */
4393       new_val =
4394           channel->min_value + new_val * ((gdouble) channel->max_value -
4395           (gdouble) channel->min_value);
4396
4397       gst_color_balance_set_value (balance_element, channel,
4398           (gint) (new_val + 0.5));
4399
4400       gst_object_unref (balance_element);
4401     }
4402
4403     gst_color_balance_value_changed (balance, proxy, value);
4404     break;
4405   }
4406 }
4407
4408 static gint
4409 gst_play_sink_colorbalance_get_value (GstColorBalance * balance,
4410     GstColorBalanceChannel * proxy)
4411 {
4412   GstPlaySink *playsink = GST_PLAY_SINK (balance);
4413   GList *l;
4414   gint i;
4415
4416   for (i = 0, l = playsink->colorbalance_channels; l; l = l->next, i++) {
4417     GstColorBalanceChannel *proxy_tmp = l->data;
4418
4419     if (proxy_tmp != proxy)
4420       continue;
4421
4422     return playsink->colorbalance_values[i];
4423   }
4424
4425   g_return_val_if_reached (0);
4426 }
4427
4428 static GstColorBalanceType
4429 gst_play_sink_colorbalance_get_balance_type (GstColorBalance * balance)
4430 {
4431   GstPlaySink *playsink = GST_PLAY_SINK (balance);
4432   GstColorBalance *balance_element = NULL;
4433   GstColorBalanceType t = GST_COLOR_BALANCE_SOFTWARE;
4434
4435   GST_OBJECT_LOCK (playsink);
4436   if (playsink->colorbalance_element)
4437     balance_element =
4438         GST_COLOR_BALANCE (gst_object_ref (playsink->colorbalance_element));
4439   GST_OBJECT_UNLOCK (playsink);
4440
4441   if (balance_element) {
4442     t = gst_color_balance_get_balance_type (balance_element);
4443     gst_object_unref (balance_element);
4444   }
4445
4446   return t;
4447 }
4448
4449 static void
4450 gst_play_sink_colorbalance_init (gpointer g_iface, gpointer g_iface_data)
4451 {
4452   GstColorBalanceInterface *iface = (GstColorBalanceInterface *) g_iface;
4453
4454   iface->list_channels = gst_play_sink_colorbalance_list_channels;
4455   iface->set_value = gst_play_sink_colorbalance_set_value;
4456   iface->get_value = gst_play_sink_colorbalance_get_value;
4457   iface->get_balance_type = gst_play_sink_colorbalance_get_balance_type;
4458 }
4459
4460 gboolean
4461 gst_play_sink_plugin_init (GstPlugin * plugin)
4462 {
4463   GST_DEBUG_CATEGORY_INIT (gst_play_sink_debug, "playsink", 0, "play bin");
4464   return gst_element_register (plugin, "playsink", GST_RANK_NONE,
4465       GST_TYPE_PLAY_SINK);
4466 }