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