5ba637ac40413abff3482532c7b6b699857b6d8f
[platform/upstream/gstreamer.git] / gst / playback / gstplaysink.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <string.h>
25 #include <gst/gst.h>
26
27 #include <gst/gst-i18n-plugin.h>
28 #include <gst/pbutils/pbutils.h>
29 #include <gst/video/video.h>
30
31 #include "gstplaysink.h"
32 #include "gststreamsynchronizer.h"
33
34 GST_DEBUG_CATEGORY_STATIC (gst_play_sink_debug);
35 #define GST_CAT_DEFAULT gst_play_sink_debug
36
37 #define VOLUME_MAX_DOUBLE 10.0
38
39 #define DEFAULT_FLAGS             GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_TEXT | \
40                                   GST_PLAY_FLAG_SOFT_VOLUME
41
42 #define GST_PLAY_CHAIN(c) ((GstPlayChain *)(c))
43
44 /* holds the common data fields for the audio and video pipelines. We keep them
45  * in a structure to more easily have all the info available. */
46 typedef struct
47 {
48   GstPlaySink *playsink;
49   GstElement *bin;
50   gboolean added;
51   gboolean activated;
52   gboolean raw;
53 } GstPlayChain;
54
55 typedef struct
56 {
57   GstPlayChain chain;
58   GstPad *sinkpad;
59   GstElement *queue;
60   GstElement *conv;
61   GstElement *resample;
62   GstElement *volume;           /* element with the volume property */
63   gboolean sink_volume;         /* if the volume was provided by the sink */
64   GstElement *mute;             /* element with the mute property */
65   GstElement *sink;
66   GstElement *ts_offset;
67 } GstPlayAudioChain;
68
69 typedef struct
70 {
71   GstPlayChain chain;
72   GstPad *sinkpad, *srcpad;
73   GstElement *conv;
74   GstElement *deinterlace;
75 } GstPlayVideoDeinterlaceChain;
76
77 typedef struct
78 {
79   GstPlayChain chain;
80   GstPad *sinkpad;
81   GstElement *queue;
82   GstElement *conv;
83   GstElement *scale;
84   GstElement *sink;
85   gboolean async;
86   GstElement *ts_offset;
87 } GstPlayVideoChain;
88
89 typedef struct
90 {
91   GstPlayChain chain;
92   GstPad *sinkpad;
93   GstElement *queue;
94   GstElement *conv;
95   GstElement *resample;
96   GstPad *blockpad;             /* srcpad of resample, used for switching the vis */
97   GstPad *vissinkpad;           /* visualisation sinkpad, */
98   GstElement *vis;
99   GstPad *vissrcpad;            /* visualisation srcpad, */
100   GstPad *srcpad;               /* outgoing srcpad, used to connect to the next
101                                  * chain */
102 } GstPlayVisChain;
103
104 typedef struct
105 {
106   GstPlayChain chain;
107   GstPad *sinkpad;
108   GstElement *queue;
109   GstElement *identity;
110   GstElement *overlay;
111   GstPad *videosinkpad;
112   GstPad *textsinkpad;
113   GstPad *srcpad;               /* outgoing srcpad, used to connect to the next
114                                  * chain */
115   GstElement *sink;             /* custom sink to receive subtitle buffers */
116 } GstPlayTextChain;
117
118 #define GST_PLAY_SINK_GET_LOCK(playsink) (&((GstPlaySink *)playsink)->lock)
119 #define GST_PLAY_SINK_LOCK(playsink)     G_STMT_START { \
120   GST_LOG_OBJECT (playsink, "locking from thread %p", g_thread_self ()); \
121   g_static_rec_mutex_lock (GST_PLAY_SINK_GET_LOCK (playsink)); \
122   GST_LOG_OBJECT (playsink, "locked from thread %p", g_thread_self ()); \
123 } G_STMT_END
124 #define GST_PLAY_SINK_UNLOCK(playsink)   G_STMT_START { \
125   GST_LOG_OBJECT (playsink, "unlocking from thread %p", g_thread_self ()); \
126   g_static_rec_mutex_unlock (GST_PLAY_SINK_GET_LOCK (playsink)); \
127 } G_STMT_END
128
129 struct _GstPlaySink
130 {
131   GstBin bin;
132
133   GStaticRecMutex lock;
134
135   gboolean async_pending;
136   gboolean need_async_start;
137
138   GstPlayFlags flags;
139
140   GstStreamSynchronizer *stream_synchronizer;
141
142   /* chains */
143   GstPlayAudioChain *audiochain;
144   GstPlayVideoDeinterlaceChain *videodeinterlacechain;
145   GstPlayVideoChain *videochain;
146   GstPlayVisChain *vischain;
147   GstPlayTextChain *textchain;
148
149   /* audio */
150   GstPad *audio_pad;
151   gboolean audio_pad_raw;
152   GstPad *audio_srcpad_stream_synchronizer;
153   GstPad *audio_sinkpad_stream_synchronizer;
154   /* audio tee */
155   GstElement *audio_tee;
156   GstPad *audio_tee_sink;
157   GstPad *audio_tee_asrc;
158   GstPad *audio_tee_vissrc;
159   /* video */
160   GstPad *video_pad;
161   gboolean video_pad_raw;
162   GstPad *video_srcpad_stream_synchronizer;
163   GstPad *video_sinkpad_stream_synchronizer;
164   /* text */
165   GstPad *text_pad;
166   GstPad *text_srcpad_stream_synchronizer;
167   GstPad *text_sinkpad_stream_synchronizer;
168
169   /* properties */
170   GstElement *audio_sink;
171   GstElement *video_sink;
172   GstElement *visualisation;
173   GstElement *text_sink;
174   gdouble volume;
175   gboolean mute;
176   gchar *font_desc;             /* font description */
177   gchar *subtitle_encoding;     /* subtitle encoding */
178   guint connection_speed;       /* connection speed in bits/sec (0 = unknown) */
179   gint count;
180   gboolean volume_changed;      /* volume/mute changed while no audiochain */
181   gboolean mute_changed;        /* ... has been created yet */
182   gint64 av_offset;
183 };
184
185 struct _GstPlaySinkClass
186 {
187   GstBinClass parent_class;
188
189     gboolean (*reconfigure) (GstPlaySink * playsink);
190
191   GstBuffer *(*convert_frame) (GstPlaySink * playsink, GstCaps * caps);
192 };
193
194 static GstStaticPadTemplate audiorawtemplate =
195 GST_STATIC_PAD_TEMPLATE ("audio_raw_sink",
196     GST_PAD_SINK,
197     GST_PAD_REQUEST,
198     GST_STATIC_CAPS_ANY);
199 static GstStaticPadTemplate audiotemplate =
200 GST_STATIC_PAD_TEMPLATE ("audio_sink",
201     GST_PAD_SINK,
202     GST_PAD_REQUEST,
203     GST_STATIC_CAPS_ANY);
204 static GstStaticPadTemplate videorawtemplate =
205 GST_STATIC_PAD_TEMPLATE ("video_raw_sink",
206     GST_PAD_SINK,
207     GST_PAD_REQUEST,
208     GST_STATIC_CAPS_ANY);
209 static GstStaticPadTemplate videotemplate =
210 GST_STATIC_PAD_TEMPLATE ("video_sink",
211     GST_PAD_SINK,
212     GST_PAD_REQUEST,
213     GST_STATIC_CAPS_ANY);
214 static GstStaticPadTemplate texttemplate = GST_STATIC_PAD_TEMPLATE ("text_sink",
215     GST_PAD_SINK,
216     GST_PAD_REQUEST,
217     GST_STATIC_CAPS_ANY);
218
219 /* props */
220 enum
221 {
222   PROP_0,
223   PROP_FLAGS,
224   PROP_MUTE,
225   PROP_VOLUME,
226   PROP_FONT_DESC,
227   PROP_SUBTITLE_ENCODING,
228   PROP_VIS_PLUGIN,
229   PROP_FRAME,
230   PROP_AV_OFFSET,
231   PROP_LAST
232 };
233
234 /* signals */
235 enum
236 {
237   LAST_SIGNAL
238 };
239
240 static void gst_play_sink_dispose (GObject * object);
241 static void gst_play_sink_finalize (GObject * object);
242 static void gst_play_sink_set_property (GObject * object, guint prop_id,
243     const GValue * value, GParamSpec * spec);
244 static void gst_play_sink_get_property (GObject * object, guint prop_id,
245     GValue * value, GParamSpec * spec);
246
247 static GstPad *gst_play_sink_request_new_pad (GstElement * element,
248     GstPadTemplate * templ, const gchar * name);
249 static void gst_play_sink_release_request_pad (GstElement * element,
250     GstPad * pad);
251 static gboolean gst_play_sink_send_event (GstElement * element,
252     GstEvent * event);
253 static GstStateChangeReturn gst_play_sink_change_state (GstElement * element,
254     GstStateChange transition);
255
256 static void gst_play_sink_handle_message (GstBin * bin, GstMessage * message);
257
258 static void notify_volume_cb (GObject * object, GParamSpec * pspec,
259     GstPlaySink * playsink);
260 static void notify_mute_cb (GObject * object, GParamSpec * pspec,
261     GstPlaySink * playsink);
262
263 static void update_av_offset (GstPlaySink * playsink);
264
265 void
266 gst_play_marshal_BUFFER__BOXED (GClosure * closure,
267     GValue * return_value G_GNUC_UNUSED,
268     guint n_param_values,
269     const GValue * param_values,
270     gpointer invocation_hint G_GNUC_UNUSED, gpointer marshal_data)
271 {
272   typedef GstBuffer *(*GMarshalFunc_OBJECT__BOXED) (gpointer data1,
273       gpointer arg_1, gpointer data2);
274   register GMarshalFunc_OBJECT__BOXED callback;
275   register GCClosure *cc = (GCClosure *) closure;
276   register gpointer data1, data2;
277   GstBuffer *v_return;
278   g_return_if_fail (return_value != NULL);
279   g_return_if_fail (n_param_values == 2);
280
281   if (G_CCLOSURE_SWAP_DATA (closure)) {
282     data1 = closure->data;
283     data2 = g_value_peek_pointer (param_values + 0);
284   } else {
285     data1 = g_value_peek_pointer (param_values + 0);
286     data2 = closure->data;
287   }
288   callback =
289       (GMarshalFunc_OBJECT__BOXED) (marshal_data ? marshal_data : cc->callback);
290
291   v_return = callback (data1, g_value_get_boxed (param_values + 1), data2);
292
293   gst_value_take_buffer (return_value, v_return);
294 }
295
296 /* static guint gst_play_sink_signals[LAST_SIGNAL] = { 0 }; */
297
298 G_DEFINE_TYPE (GstPlaySink, gst_play_sink, GST_TYPE_BIN);
299
300 static void
301 gst_play_sink_class_init (GstPlaySinkClass * klass)
302 {
303   GObjectClass *gobject_klass;
304   GstElementClass *gstelement_klass;
305   GstBinClass *gstbin_klass;
306
307   gobject_klass = (GObjectClass *) klass;
308   gstelement_klass = (GstElementClass *) klass;
309   gstbin_klass = (GstBinClass *) klass;
310
311   gobject_klass->dispose = gst_play_sink_dispose;
312   gobject_klass->finalize = gst_play_sink_finalize;
313   gobject_klass->set_property = gst_play_sink_set_property;
314   gobject_klass->get_property = gst_play_sink_get_property;
315
316
317   /**
318    * GstPlaySink:flags
319    *
320    * Control the behaviour of playsink.
321    */
322   g_object_class_install_property (gobject_klass, PROP_FLAGS,
323       g_param_spec_flags ("flags", "Flags", "Flags to control behaviour",
324           GST_TYPE_PLAY_FLAGS, DEFAULT_FLAGS,
325           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326
327   /**
328    * GstPlaySink:volume:
329    *
330    * Get or set the current audio stream volume. 1.0 means 100%,
331    * 0.0 means mute. This uses a linear volume scale.
332    *
333    */
334   g_object_class_install_property (gobject_klass, PROP_VOLUME,
335       g_param_spec_double ("volume", "Volume", "The audio volume, 1.0=100%",
336           0.0, VOLUME_MAX_DOUBLE, 1.0,
337           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
338   g_object_class_install_property (gobject_klass, PROP_MUTE,
339       g_param_spec_boolean ("mute", "Mute",
340           "Mute the audio channel without changing the volume", FALSE,
341           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
342   g_object_class_install_property (gobject_klass, PROP_FONT_DESC,
343       g_param_spec_string ("subtitle-font-desc",
344           "Subtitle font description",
345           "Pango font description of font "
346           "to be used for subtitle rendering", NULL,
347           G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
348   g_object_class_install_property (gobject_klass, PROP_SUBTITLE_ENCODING,
349       g_param_spec_string ("subtitle-encoding", "subtitle encoding",
350           "Encoding to assume if input subtitles are not in UTF-8 encoding. "
351           "If not set, the GST_SUBTITLE_ENCODING environment variable will "
352           "be checked for an encoding to use. If that is not set either, "
353           "ISO-8859-15 will be assumed.", NULL,
354           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
355   g_object_class_install_property (gobject_klass, PROP_VIS_PLUGIN,
356       g_param_spec_object ("vis-plugin", "Vis plugin",
357           "the visualization element to use (NULL = default)",
358           GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359   /**
360    * GstPlaySink:frame:
361    *
362    * Get the currently rendered or prerolled frame in the video sink.
363    * The #GstCaps on the buffer will describe the format of the buffer.
364    *
365    * Since: 0.10.30
366    */
367   g_object_class_install_property (gobject_klass, PROP_FRAME,
368       gst_param_spec_mini_object ("frame", "Frame",
369           "The last frame (NULL = no video available)",
370           GST_TYPE_BUFFER, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
371   /**
372    * GstPlaySink:av-offset:
373    *
374    * Control the synchronisation offset between the audio and video streams.
375    * Positive values make the audio ahead of the video and negative values make
376    * the audio go behind the video.
377    *
378    * Since: 0.10.30
379    */
380   g_object_class_install_property (gobject_klass, PROP_AV_OFFSET,
381       g_param_spec_int64 ("av-offset", "AV Offset",
382           "The synchronisation offset between audio and video in nanoseconds",
383           G_MININT64, G_MAXINT64, 0,
384           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
385
386   g_signal_new ("reconfigure", G_TYPE_FROM_CLASS (klass),
387       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstPlaySinkClass,
388           reconfigure), NULL, NULL, gst_marshal_BOOLEAN__VOID, G_TYPE_BOOLEAN,
389       0, G_TYPE_NONE);
390   /**
391    * GstPlaySink::convert-frame
392    * @playsink: a #GstPlaySink
393    * @caps: the target format of the frame
394    *
395    * Action signal to retrieve the currently playing video frame in the format
396    * specified by @caps.
397    * If @caps is %NULL, no conversion will be performed and this function is
398    * equivalent to the #GstPlaySink::frame property.
399    *
400    * Returns: a #GstBuffer of the current video frame converted to #caps.
401    * The caps on the buffer will describe the final layout of the buffer data.
402    * %NULL is returned when no current buffer can be retrieved or when the
403    * conversion failed.
404    *
405    * Since: 0.10.30
406    */
407   g_signal_new ("convert-frame", G_TYPE_FROM_CLASS (klass),
408       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
409       G_STRUCT_OFFSET (GstPlaySinkClass, convert_frame), NULL, NULL,
410       gst_play_marshal_BUFFER__BOXED, GST_TYPE_BUFFER, 1, GST_TYPE_CAPS);
411
412   gst_element_class_add_pad_template (gstelement_klass,
413       gst_static_pad_template_get (&audiorawtemplate));
414   gst_element_class_add_pad_template (gstelement_klass,
415       gst_static_pad_template_get (&audiotemplate));
416   gst_element_class_add_pad_template (gstelement_klass,
417       gst_static_pad_template_get (&videorawtemplate));
418   gst_element_class_add_pad_template (gstelement_klass,
419       gst_static_pad_template_get (&videotemplate));
420   gst_element_class_add_pad_template (gstelement_klass,
421       gst_static_pad_template_get (&texttemplate));
422   gst_element_class_set_details_simple (gstelement_klass, "Player Sink",
423       "Generic/Bin/Sink",
424       "Convenience sink for multiple streams",
425       "Wim Taymans <wim.taymans@gmail.com>");
426
427   gstelement_klass->change_state =
428       GST_DEBUG_FUNCPTR (gst_play_sink_change_state);
429   gstelement_klass->send_event = GST_DEBUG_FUNCPTR (gst_play_sink_send_event);
430   gstelement_klass->request_new_pad =
431       GST_DEBUG_FUNCPTR (gst_play_sink_request_new_pad);
432   gstelement_klass->release_pad =
433       GST_DEBUG_FUNCPTR (gst_play_sink_release_request_pad);
434
435   gstbin_klass->handle_message =
436       GST_DEBUG_FUNCPTR (gst_play_sink_handle_message);
437
438   klass->reconfigure = GST_DEBUG_FUNCPTR (gst_play_sink_reconfigure);
439   klass->convert_frame = GST_DEBUG_FUNCPTR (gst_play_sink_convert_frame);
440 }
441
442 static void
443 gst_play_sink_init (GstPlaySink * playsink)
444 {
445   /* init groups */
446   playsink->video_sink = NULL;
447   playsink->audio_sink = NULL;
448   playsink->visualisation = NULL;
449   playsink->text_sink = NULL;
450   playsink->volume = 1.0;
451   playsink->font_desc = NULL;
452   playsink->subtitle_encoding = NULL;
453   playsink->flags = DEFAULT_FLAGS;
454
455   playsink->stream_synchronizer =
456       g_object_new (GST_TYPE_STREAM_SYNCHRONIZER, NULL);
457   gst_bin_add (GST_BIN_CAST (playsink),
458       GST_ELEMENT_CAST (playsink->stream_synchronizer));
459
460   g_static_rec_mutex_init (&playsink->lock);
461   GST_OBJECT_FLAG_SET (playsink, GST_ELEMENT_IS_SINK);
462 }
463
464 static void
465 disconnect_chain (GstPlayAudioChain * chain, GstPlaySink * playsink)
466 {
467   if (chain) {
468     if (chain->volume)
469       g_signal_handlers_disconnect_by_func (chain->volume, notify_volume_cb,
470           playsink);
471     if (chain->mute)
472       g_signal_handlers_disconnect_by_func (chain->mute, notify_mute_cb,
473           playsink);
474   }
475 }
476
477 static void
478 free_chain (GstPlayChain * chain)
479 {
480   if (chain) {
481     if (chain->bin)
482       gst_object_unref (chain->bin);
483     g_free (chain);
484   }
485 }
486
487 static void
488 gst_play_sink_dispose (GObject * object)
489 {
490   GstPlaySink *playsink;
491
492   playsink = GST_PLAY_SINK (object);
493
494   if (playsink->audio_sink != NULL) {
495     gst_element_set_state (playsink->audio_sink, GST_STATE_NULL);
496     gst_object_unref (playsink->audio_sink);
497     playsink->audio_sink = NULL;
498   }
499   if (playsink->video_sink != NULL) {
500     gst_element_set_state (playsink->video_sink, GST_STATE_NULL);
501     gst_object_unref (playsink->video_sink);
502     playsink->video_sink = NULL;
503   }
504   if (playsink->visualisation != NULL) {
505     gst_element_set_state (playsink->visualisation, GST_STATE_NULL);
506     gst_object_unref (playsink->visualisation);
507     playsink->visualisation = NULL;
508   }
509   if (playsink->text_sink != NULL) {
510     gst_element_set_state (playsink->text_sink, GST_STATE_NULL);
511     gst_object_unref (playsink->text_sink);
512     playsink->text_sink = NULL;
513   }
514
515   free_chain ((GstPlayChain *) playsink->videodeinterlacechain);
516   playsink->videodeinterlacechain = NULL;
517   free_chain ((GstPlayChain *) playsink->videochain);
518   playsink->videochain = NULL;
519   free_chain ((GstPlayChain *) playsink->audiochain);
520   playsink->audiochain = NULL;
521   free_chain ((GstPlayChain *) playsink->vischain);
522   playsink->vischain = NULL;
523   free_chain ((GstPlayChain *) playsink->textchain);
524   playsink->textchain = NULL;
525
526   if (playsink->audio_tee_sink) {
527     gst_object_unref (playsink->audio_tee_sink);
528     playsink->audio_tee_sink = NULL;
529   }
530
531   if (playsink->audio_tee_vissrc) {
532     gst_element_release_request_pad (playsink->audio_tee,
533         playsink->audio_tee_vissrc);
534     gst_object_unref (playsink->audio_tee_vissrc);
535     playsink->audio_tee_vissrc = NULL;
536   }
537
538   if (playsink->audio_tee_asrc) {
539     gst_element_release_request_pad (playsink->audio_tee,
540         playsink->audio_tee_asrc);
541     gst_object_unref (playsink->audio_tee_asrc);
542     playsink->audio_tee_asrc = NULL;
543   }
544
545   g_free (playsink->font_desc);
546   playsink->font_desc = NULL;
547
548   g_free (playsink->subtitle_encoding);
549   playsink->subtitle_encoding = NULL;
550
551   playsink->stream_synchronizer = NULL;
552
553   G_OBJECT_CLASS (gst_play_sink_parent_class)->dispose (object);
554 }
555
556 static void
557 gst_play_sink_finalize (GObject * object)
558 {
559   GstPlaySink *playsink;
560
561   playsink = GST_PLAY_SINK (object);
562
563   g_static_rec_mutex_free (&playsink->lock);
564
565   G_OBJECT_CLASS (gst_play_sink_parent_class)->finalize (object);
566 }
567
568 void
569 gst_play_sink_set_sink (GstPlaySink * playsink, GstPlaySinkType type,
570     GstElement * sink)
571 {
572   GstElement **elem = NULL, *old = NULL;
573
574   GST_LOG ("Setting sink %" GST_PTR_FORMAT " as sink type %d", sink, type);
575
576   GST_PLAY_SINK_LOCK (playsink);
577   switch (type) {
578     case GST_PLAY_SINK_TYPE_AUDIO:
579     case GST_PLAY_SINK_TYPE_AUDIO_RAW:
580       elem = &playsink->audio_sink;
581       break;
582     case GST_PLAY_SINK_TYPE_VIDEO:
583     case GST_PLAY_SINK_TYPE_VIDEO_RAW:
584       elem = &playsink->video_sink;
585       break;
586     case GST_PLAY_SINK_TYPE_TEXT:
587       elem = &playsink->text_sink;
588       break;
589     default:
590       break;
591   }
592   if (elem) {
593     old = *elem;
594     if (sink)
595       gst_object_ref (sink);
596     *elem = sink;
597   }
598   GST_PLAY_SINK_UNLOCK (playsink);
599
600   if (old) {
601     if (old != sink)
602       gst_element_set_state (old, GST_STATE_NULL);
603     gst_object_unref (old);
604   }
605 }
606
607 GstElement *
608 gst_play_sink_get_sink (GstPlaySink * playsink, GstPlaySinkType type)
609 {
610   GstElement *result = NULL;
611   GstElement *elem = NULL, *chainp = NULL;
612
613   GST_PLAY_SINK_LOCK (playsink);
614   switch (type) {
615     case GST_PLAY_SINK_TYPE_AUDIO:
616     {
617       GstPlayAudioChain *chain;
618       if ((chain = (GstPlayAudioChain *) playsink->audiochain))
619         chainp = chain->sink;
620       elem = playsink->audio_sink;
621       break;
622     }
623     case GST_PLAY_SINK_TYPE_VIDEO:
624     {
625       GstPlayVideoChain *chain;
626       if ((chain = (GstPlayVideoChain *) playsink->videochain))
627         chainp = chain->sink;
628       elem = playsink->video_sink;
629       break;
630     }
631     case GST_PLAY_SINK_TYPE_TEXT:
632     {
633       GstPlayTextChain *chain;
634       if ((chain = (GstPlayTextChain *) playsink->textchain))
635         chainp = chain->sink;
636       elem = playsink->text_sink;
637       break;
638     }
639     default:
640       break;
641   }
642   if (chainp) {
643     /* we have an active chain with a sink, get the sink */
644     result = gst_object_ref (chainp);
645   }
646   /* nothing found, return last configured sink */
647   if (result == NULL && elem)
648     result = gst_object_ref (elem);
649   GST_PLAY_SINK_UNLOCK (playsink);
650
651   return result;
652 }
653
654 static void
655 gst_play_sink_vis_unblocked (GstPad * tee_pad, gboolean blocked,
656     gpointer user_data)
657 {
658   GstPlaySink *playsink;
659
660   playsink = GST_PLAY_SINK (user_data);
661   /* nothing to do here, we need a dummy callback here to make the async call
662    * non-blocking. */
663   GST_DEBUG_OBJECT (playsink, "vis pad unblocked");
664 }
665
666 static void
667 gst_play_sink_vis_blocked (GstPad * tee_pad, gboolean blocked,
668     gpointer user_data)
669 {
670   GstPlaySink *playsink;
671   GstPlayVisChain *chain;
672
673   playsink = GST_PLAY_SINK (user_data);
674
675   GST_PLAY_SINK_LOCK (playsink);
676   GST_DEBUG_OBJECT (playsink, "vis pad blocked");
677   /* now try to change the plugin in the running vis chain */
678   if (!(chain = (GstPlayVisChain *) playsink->vischain))
679     goto done;
680
681   /* unlink the old plugin and unghost the pad */
682   gst_pad_unlink (chain->blockpad, chain->vissinkpad);
683   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (chain->srcpad), NULL);
684
685   /* set the old plugin to NULL and remove */
686   gst_element_set_state (chain->vis, GST_STATE_NULL);
687   gst_bin_remove (GST_BIN_CAST (chain->chain.bin), chain->vis);
688
689   /* add new plugin and set state to playing */
690   chain->vis = playsink->visualisation;
691   gst_bin_add (GST_BIN_CAST (chain->chain.bin), chain->vis);
692   gst_element_set_state (chain->vis, GST_STATE_PLAYING);
693
694   /* get pads */
695   chain->vissinkpad = gst_element_get_static_pad (chain->vis, "sink");
696   chain->vissrcpad = gst_element_get_static_pad (chain->vis, "src");
697
698   /* link pads */
699   gst_pad_link_full (chain->blockpad, chain->vissinkpad,
700       GST_PAD_LINK_CHECK_NOTHING);
701   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (chain->srcpad),
702       chain->vissrcpad);
703
704 done:
705   /* Unblock the pad */
706   gst_pad_set_blocked_async (tee_pad, FALSE, gst_play_sink_vis_unblocked,
707       playsink);
708   GST_PLAY_SINK_UNLOCK (playsink);
709 }
710
711 void
712 gst_play_sink_set_vis_plugin (GstPlaySink * playsink, GstElement * vis)
713 {
714   GstPlayVisChain *chain;
715
716   /* setting NULL means creating the default vis plugin */
717   if (vis == NULL)
718     vis = gst_element_factory_make ("goom", "vis");
719
720   /* simply return if we don't have a vis plugin here */
721   if (vis == NULL)
722     return;
723
724   GST_PLAY_SINK_LOCK (playsink);
725   /* first store the new vis */
726   if (playsink->visualisation)
727     gst_object_unref (playsink->visualisation);
728   /* take ownership */
729   gst_object_ref_sink (vis);
730   playsink->visualisation = vis;
731
732   /* now try to change the plugin in the running vis chain, if we have no chain,
733    * we don't bother, any future vis chain will be created with the new vis
734    * plugin. */
735   if (!(chain = (GstPlayVisChain *) playsink->vischain))
736     goto done;
737
738   /* block the pad, the next time the callback is called we can change the
739    * visualisation. It's possible that this never happens or that the pad was
740    * already blocked. If the callback never happens, we don't have new data so
741    * we don't need the new vis plugin. If the pad was already blocked, the
742    * function returns FALSE but the previous pad block will do the right thing
743    * anyway. */
744   GST_DEBUG_OBJECT (playsink, "blocking vis pad");
745   gst_pad_set_blocked_async (chain->blockpad, TRUE, gst_play_sink_vis_blocked,
746       playsink);
747 done:
748   GST_PLAY_SINK_UNLOCK (playsink);
749
750   return;
751 }
752
753 GstElement *
754 gst_play_sink_get_vis_plugin (GstPlaySink * playsink)
755 {
756   GstElement *result = NULL;
757   GstPlayVisChain *chain;
758
759   GST_PLAY_SINK_LOCK (playsink);
760   if ((chain = (GstPlayVisChain *) playsink->vischain)) {
761     /* we have an active chain, get the sink */
762     if (chain->vis)
763       result = gst_object_ref (chain->vis);
764   }
765   /* nothing found, return last configured sink */
766   if (result == NULL && playsink->visualisation)
767     result = gst_object_ref (playsink->visualisation);
768   GST_PLAY_SINK_UNLOCK (playsink);
769
770   return result;
771 }
772
773 void
774 gst_play_sink_set_volume (GstPlaySink * playsink, gdouble volume)
775 {
776   GstPlayAudioChain *chain;
777
778   GST_PLAY_SINK_LOCK (playsink);
779   playsink->volume = volume;
780   chain = (GstPlayAudioChain *) playsink->audiochain;
781   if (chain && chain->volume) {
782     GST_LOG_OBJECT (playsink, "elements: volume=%" GST_PTR_FORMAT ", mute=%"
783         GST_PTR_FORMAT "; new volume=%.03f, mute=%d", chain->volume,
784         chain->mute, volume, playsink->mute);
785     /* if there is a mute element or we are not muted, set the volume */
786     if (chain->mute || !playsink->mute)
787       g_object_set (chain->volume, "volume", volume, NULL);
788   } else {
789     GST_LOG_OBJECT (playsink, "no volume element");
790     playsink->volume_changed = TRUE;
791   }
792   GST_PLAY_SINK_UNLOCK (playsink);
793 }
794
795 gdouble
796 gst_play_sink_get_volume (GstPlaySink * playsink)
797 {
798   gdouble result;
799   GstPlayAudioChain *chain;
800
801   GST_PLAY_SINK_LOCK (playsink);
802   chain = (GstPlayAudioChain *) playsink->audiochain;
803   result = playsink->volume;
804   if (chain && chain->volume) {
805     if (chain->mute || !playsink->mute) {
806       g_object_get (chain->volume, "volume", &result, NULL);
807       playsink->volume = result;
808     }
809   }
810   GST_PLAY_SINK_UNLOCK (playsink);
811
812   return result;
813 }
814
815 void
816 gst_play_sink_set_mute (GstPlaySink * playsink, gboolean mute)
817 {
818   GstPlayAudioChain *chain;
819
820   GST_PLAY_SINK_LOCK (playsink);
821   playsink->mute = mute;
822   chain = (GstPlayAudioChain *) playsink->audiochain;
823   if (chain) {
824     if (chain->mute) {
825       g_object_set (chain->mute, "mute", mute, NULL);
826     } else if (chain->volume) {
827       if (mute)
828         g_object_set (chain->volume, "volume", (gdouble) 0.0, NULL);
829       else
830         g_object_set (chain->volume, "volume", (gdouble) playsink->volume,
831             NULL);
832     }
833   } else {
834     playsink->mute_changed = TRUE;
835   }
836   GST_PLAY_SINK_UNLOCK (playsink);
837 }
838
839 gboolean
840 gst_play_sink_get_mute (GstPlaySink * playsink)
841 {
842   gboolean result;
843   GstPlayAudioChain *chain;
844
845   GST_PLAY_SINK_LOCK (playsink);
846   chain = (GstPlayAudioChain *) playsink->audiochain;
847   if (chain && chain->mute) {
848     g_object_get (chain->mute, "mute", &result, NULL);
849     playsink->mute = result;
850   } else {
851     result = playsink->mute;
852   }
853   GST_PLAY_SINK_UNLOCK (playsink);
854
855   return result;
856 }
857
858 static void
859 post_missing_element_message (GstPlaySink * playsink, const gchar * name)
860 {
861   GstMessage *msg;
862
863   msg = gst_missing_element_message_new (GST_ELEMENT_CAST (playsink), name);
864   gst_element_post_message (GST_ELEMENT_CAST (playsink), msg);
865 }
866
867 static gboolean
868 add_chain (GstPlayChain * chain, gboolean add)
869 {
870   if (chain->added == add)
871     return TRUE;
872
873   if (add)
874     gst_bin_add (GST_BIN_CAST (chain->playsink), chain->bin);
875   else {
876     gst_bin_remove (GST_BIN_CAST (chain->playsink), chain->bin);
877     /* we don't want to lose our sink status */
878     GST_OBJECT_FLAG_SET (chain->playsink, GST_ELEMENT_IS_SINK);
879   }
880
881   chain->added = add;
882
883   return TRUE;
884 }
885
886 static gboolean
887 activate_chain (GstPlayChain * chain, gboolean activate)
888 {
889   GstState state;
890
891   if (chain->activated == activate)
892     return TRUE;
893
894   GST_OBJECT_LOCK (chain->playsink);
895   state = GST_STATE_TARGET (chain->playsink);
896   GST_OBJECT_UNLOCK (chain->playsink);
897
898   if (activate)
899     gst_element_set_state (chain->bin, state);
900   else
901     gst_element_set_state (chain->bin, GST_STATE_NULL);
902
903   chain->activated = activate;
904
905   return TRUE;
906 }
907
908 static gboolean
909 element_is_sink (GstElement * element)
910 {
911   gboolean is_sink;
912
913   GST_OBJECT_LOCK (element);
914   is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_IS_SINK);
915   GST_OBJECT_UNLOCK (element);
916
917   GST_DEBUG_OBJECT (element, "is a sink: %s", (is_sink) ? "yes" : "no");
918   return is_sink;
919 }
920
921 static gboolean
922 element_has_property (GstElement * element, const gchar * pname, GType type)
923 {
924   GParamSpec *pspec;
925
926   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), pname);
927
928   if (pspec == NULL) {
929     GST_DEBUG_OBJECT (element, "no %s property", pname);
930     return FALSE;
931   }
932
933   if (type == G_TYPE_INVALID || type == pspec->value_type ||
934       g_type_is_a (pspec->value_type, type)) {
935     GST_DEBUG_OBJECT (element, "has %s property of type %s", pname,
936         (type == G_TYPE_INVALID) ? "any type" : g_type_name (type));
937     return TRUE;
938   }
939
940   GST_WARNING_OBJECT (element, "has %s property, but property is of type %s "
941       "and we expected it to be of type %s", pname,
942       g_type_name (pspec->value_type), g_type_name (type));
943
944   return FALSE;
945 }
946
947 typedef struct
948 {
949   const gchar *prop_name;
950   GType prop_type;
951   gboolean need_sink;
952 } FindPropertyHelper;
953
954 static gint
955 find_property (GstElement * element, FindPropertyHelper * helper)
956 {
957   if (helper->need_sink && !element_is_sink (element)) {
958     gst_object_unref (element);
959     return 1;
960   }
961
962   if (!element_has_property (element, helper->prop_name, helper->prop_type)) {
963     gst_object_unref (element);
964     return 1;
965   }
966
967   GST_INFO_OBJECT (element, "found %s with %s property", helper->prop_name,
968       (helper->need_sink) ? "sink" : "element");
969   return 0;                     /* keep it */
970 }
971
972 /* FIXME: why not move these functions into core? */
973 /* find a sink in the hierarchy with a property named @name. This function does
974  * not increase the refcount of the returned object and thus remains valid as
975  * long as the bin is valid. */
976 static GstElement *
977 gst_play_sink_find_property_sinks (GstPlaySink * playsink, GstElement * obj,
978     const gchar * name, GType expected_type)
979 {
980   GstElement *result = NULL;
981   GstIterator *it;
982
983   if (element_has_property (obj, name, expected_type)) {
984     result = obj;
985   } else if (GST_IS_BIN (obj)) {
986     FindPropertyHelper helper = { name, expected_type, TRUE };
987
988     it = gst_bin_iterate_recurse (GST_BIN_CAST (obj));
989     result = gst_iterator_find_custom (it,
990         (GCompareFunc) find_property, &helper);
991     gst_iterator_free (it);
992     /* we don't need the extra ref */
993     if (result)
994       gst_object_unref (result);
995   }
996   return result;
997 }
998
999 /* find an object in the hierarchy with a property named @name */
1000 static GstElement *
1001 gst_play_sink_find_property (GstPlaySink * playsink, GstElement * obj,
1002     const gchar * name, GType expected_type)
1003 {
1004   GstElement *result = NULL;
1005   GstIterator *it;
1006
1007   if (GST_IS_BIN (obj)) {
1008     FindPropertyHelper helper = { name, expected_type, FALSE };
1009
1010     it = gst_bin_iterate_recurse (GST_BIN_CAST (obj));
1011     result = gst_iterator_find_custom (it,
1012         (GCompareFunc) find_property, &helper);
1013     gst_iterator_free (it);
1014   } else {
1015     if (element_has_property (obj, name, expected_type)) {
1016       result = obj;
1017       gst_object_ref (obj);
1018     }
1019   }
1020   return result;
1021 }
1022
1023 static void
1024 do_async_start (GstPlaySink * playsink)
1025 {
1026   GstMessage *message;
1027
1028   if (!playsink->need_async_start) {
1029     GST_INFO_OBJECT (playsink, "no async_start needed");
1030     return;
1031   }
1032
1033   playsink->async_pending = TRUE;
1034
1035   GST_INFO_OBJECT (playsink, "Sending async_start message");
1036   message = gst_message_new_async_start (GST_OBJECT_CAST (playsink), FALSE);
1037   GST_BIN_CLASS (gst_play_sink_parent_class)->handle_message (GST_BIN_CAST
1038       (playsink), message);
1039 }
1040
1041 static void
1042 do_async_done (GstPlaySink * playsink)
1043 {
1044   GstMessage *message;
1045
1046   if (playsink->async_pending) {
1047     GST_INFO_OBJECT (playsink, "Sending async_done message");
1048     message = gst_message_new_async_done (GST_OBJECT_CAST (playsink));
1049     GST_BIN_CLASS (gst_play_sink_parent_class)->handle_message (GST_BIN_CAST
1050         (playsink), message);
1051
1052     playsink->async_pending = FALSE;
1053   }
1054
1055   playsink->need_async_start = FALSE;
1056 }
1057
1058 /* try to change the state of an element. This function returns the element when
1059  * the state change could be performed. When this function returns NULL an error
1060  * occured and the element is unreffed if @unref is TRUE. */
1061 static GstElement *
1062 try_element (GstPlaySink * playsink, GstElement * element, gboolean unref)
1063 {
1064   GstStateChangeReturn ret;
1065
1066   if (element) {
1067     ret = gst_element_set_state (element, GST_STATE_READY);
1068     if (ret == GST_STATE_CHANGE_FAILURE) {
1069       GST_DEBUG_OBJECT (playsink, "failed state change..");
1070       gst_element_set_state (element, GST_STATE_NULL);
1071       if (unref)
1072         gst_object_unref (element);
1073       element = NULL;
1074     }
1075   }
1076   return element;
1077 }
1078
1079 /* make the element (bin) that contains the elements needed to perform
1080  * video display.
1081  *
1082  *  +------------------------------------------------------------+
1083  *  | vbin                                                       |
1084  *  |      +-------+   +----------+   +----------+   +---------+ |
1085  *  |      | queue |   |colorspace|   |videoscale|   |videosink| |
1086  *  |   +-sink    src-sink       src-sink       src-sink       | |
1087  *  |   |  +-------+   +----------+   +----------+   +---------+ |
1088  * sink-+                                                        |
1089  *  +------------------------------------------------------------+
1090  *
1091  */
1092 static GstPlayVideoDeinterlaceChain *
1093 gen_video_deinterlace_chain (GstPlaySink * playsink)
1094 {
1095   GstPlayVideoDeinterlaceChain *chain;
1096   GstBin *bin;
1097   GstPad *pad;
1098   GstElement *head = NULL, *prev = NULL;
1099
1100   chain = g_new0 (GstPlayVideoDeinterlaceChain, 1);
1101   chain->chain.playsink = playsink;
1102
1103   GST_DEBUG_OBJECT (playsink, "making video deinterlace chain %p", chain);
1104
1105   /* create a bin to hold objects, as we create them we add them to this bin so
1106    * that when something goes wrong we only need to unref the bin */
1107   chain->chain.bin = gst_bin_new ("vdbin");
1108   bin = GST_BIN_CAST (chain->chain.bin);
1109   gst_object_ref_sink (bin);
1110
1111   GST_DEBUG_OBJECT (playsink, "creating ffmpegcolorspace");
1112   chain->conv = gst_element_factory_make ("ffmpegcolorspace", "vdconv");
1113   if (chain->conv == NULL) {
1114     post_missing_element_message (playsink, "ffmpegcolorspace");
1115     GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1116         (_("Missing element '%s' - check your GStreamer installation."),
1117             "ffmpegcolorspace"), ("video rendering might fail"));
1118   } else {
1119     gst_bin_add (bin, chain->conv);
1120     head = chain->conv;
1121     prev = chain->conv;
1122   }
1123
1124   GST_DEBUG_OBJECT (playsink, "creating deinterlace");
1125   chain->deinterlace = gst_element_factory_make ("deinterlace", "deinterlace");
1126   if (chain->deinterlace == NULL) {
1127     post_missing_element_message (playsink, "deinterlace");
1128     GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1129         (_("Missing element '%s' - check your GStreamer installation."),
1130             "deinterlace"), ("deinterlacing won't work"));
1131   } else {
1132     gst_bin_add (bin, chain->deinterlace);
1133     if (prev) {
1134       if (!gst_element_link_pads_full (prev, "src", chain->deinterlace, "sink",
1135               GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
1136         goto link_failed;
1137     } else {
1138       head = chain->deinterlace;
1139     }
1140     prev = chain->deinterlace;
1141   }
1142
1143   if (head) {
1144     pad = gst_element_get_static_pad (head, "sink");
1145     chain->sinkpad = gst_ghost_pad_new ("sink", pad);
1146     gst_object_unref (pad);
1147   } else {
1148     chain->sinkpad = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK);
1149   }
1150
1151   if (prev) {
1152     pad = gst_element_get_static_pad (prev, "src");
1153     chain->srcpad = gst_ghost_pad_new ("src", pad);
1154     gst_object_unref (pad);
1155   } else {
1156     chain->srcpad = gst_ghost_pad_new ("src", chain->sinkpad);
1157   }
1158
1159   gst_element_add_pad (chain->chain.bin, chain->sinkpad);
1160   gst_element_add_pad (chain->chain.bin, chain->srcpad);
1161
1162   return chain;
1163
1164 link_failed:
1165   {
1166     GST_ELEMENT_ERROR (playsink, CORE, PAD,
1167         (NULL), ("Failed to configure the video deinterlace chain."));
1168     free_chain ((GstPlayChain *) chain);
1169     return NULL;
1170   }
1171 }
1172
1173 /* make the element (bin) that contains the elements needed to perform
1174  * video display.
1175  *
1176  *  +------------------------------------------------------------+
1177  *  | vbin                                                       |
1178  *  |      +-------+   +----------+   +----------+   +---------+ |
1179  *  |      | queue |   |colorspace|   |videoscale|   |videosink| |
1180  *  |   +-sink    src-sink       src-sink       src-sink       | |
1181  *  |   |  +-------+   +----------+   +----------+   +---------+ |
1182  * sink-+                                                        |
1183  *  +------------------------------------------------------------+
1184  *
1185  */
1186 static GstPlayVideoChain *
1187 gen_video_chain (GstPlaySink * playsink, gboolean raw, gboolean async)
1188 {
1189   GstPlayVideoChain *chain;
1190   GstBin *bin;
1191   GstPad *pad;
1192   GstElement *head = NULL, *prev = NULL, *elem = NULL;
1193
1194   chain = g_new0 (GstPlayVideoChain, 1);
1195   chain->chain.playsink = playsink;
1196   chain->chain.raw = raw;
1197
1198   GST_DEBUG_OBJECT (playsink, "making video chain %p", chain);
1199
1200   if (playsink->video_sink) {
1201     GST_DEBUG_OBJECT (playsink, "trying configured videosink");
1202     chain->sink = try_element (playsink, playsink->video_sink, FALSE);
1203   } else {
1204     /* only try fallback if no specific sink was chosen */
1205     if (chain->sink == NULL) {
1206       GST_DEBUG_OBJECT (playsink, "trying autovideosink");
1207       elem = gst_element_factory_make ("autovideosink", "videosink");
1208       chain->sink = try_element (playsink, elem, TRUE);
1209     }
1210     if (chain->sink == NULL) {
1211       /* if default sink from config.h is different then try it too */
1212       if (strcmp (DEFAULT_VIDEOSINK, "autovideosink")) {
1213         GST_DEBUG_OBJECT (playsink, "trying " DEFAULT_VIDEOSINK);
1214         elem = gst_element_factory_make (DEFAULT_VIDEOSINK, "videosink");
1215         chain->sink = try_element (playsink, elem, TRUE);
1216       }
1217     }
1218   }
1219   if (chain->sink == NULL)
1220     goto no_sinks;
1221   head = chain->sink;
1222
1223   /* if we can disable async behaviour of the sink, we can avoid adding a
1224    * queue for the audio chain. */
1225   elem =
1226       gst_play_sink_find_property_sinks (playsink, chain->sink, "async",
1227       G_TYPE_BOOLEAN);
1228   if (elem) {
1229     GST_DEBUG_OBJECT (playsink, "setting async property to %d on element %s",
1230         async, GST_ELEMENT_NAME (elem));
1231     g_object_set (elem, "async", async, NULL);
1232     chain->async = async;
1233   } else {
1234     GST_DEBUG_OBJECT (playsink, "no async property on the sink");
1235     chain->async = TRUE;
1236   }
1237
1238   /* find ts-offset element */
1239   chain->ts_offset =
1240       gst_play_sink_find_property_sinks (playsink, chain->sink, "ts-offset",
1241       G_TYPE_INT64);
1242
1243   /* create a bin to hold objects, as we create them we add them to this bin so
1244    * that when something goes wrong we only need to unref the bin */
1245   chain->chain.bin = gst_bin_new ("vbin");
1246   bin = GST_BIN_CAST (chain->chain.bin);
1247   gst_object_ref_sink (bin);
1248   gst_bin_add (bin, chain->sink);
1249
1250   /* decouple decoder from sink, this improves playback quite a lot since the
1251    * decoder can continue while the sink blocks for synchronisation. We don't
1252    * need a lot of buffers as this consumes a lot of memory and we don't want
1253    * too little because else we would be context switching too quickly. */
1254   chain->queue = gst_element_factory_make ("queue", "vqueue");
1255   if (chain->queue == NULL) {
1256     post_missing_element_message (playsink, "queue");
1257     GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1258         (_("Missing element '%s' - check your GStreamer installation."),
1259             "queue"), ("video rendering might be suboptimal"));
1260     head = chain->sink;
1261     prev = NULL;
1262   } else {
1263     g_object_set (G_OBJECT (chain->queue), "max-size-buffers", 3,
1264         "max-size-bytes", 0, "max-size-time", (gint64) 0, "silent", TRUE, NULL);
1265     gst_bin_add (bin, chain->queue);
1266     head = prev = chain->queue;
1267   }
1268
1269   if (raw && !(playsink->flags & GST_PLAY_FLAG_NATIVE_VIDEO)) {
1270     GST_DEBUG_OBJECT (playsink, "creating ffmpegcolorspace");
1271     chain->conv = gst_element_factory_make ("ffmpegcolorspace", "vconv");
1272     if (chain->conv == NULL) {
1273       post_missing_element_message (playsink, "ffmpegcolorspace");
1274       GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1275           (_("Missing element '%s' - check your GStreamer installation."),
1276               "ffmpegcolorspace"), ("video rendering might fail"));
1277     } else {
1278       gst_bin_add (bin, chain->conv);
1279       if (prev) {
1280         if (!gst_element_link_pads_full (prev, "src", chain->conv, "sink",
1281                 GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
1282           goto link_failed;
1283       } else {
1284         head = chain->conv;
1285       }
1286       prev = chain->conv;
1287     }
1288
1289     GST_DEBUG_OBJECT (playsink, "creating videoscale");
1290     chain->scale = gst_element_factory_make ("videoscale", "vscale");
1291     if (chain->scale == NULL) {
1292       post_missing_element_message (playsink, "videoscale");
1293       GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1294           (_("Missing element '%s' - check your GStreamer installation."),
1295               "videoscale"), ("possibly a liboil version mismatch?"));
1296     } else {
1297       /* Add black borders if necessary to keep the DAR */
1298       g_object_set (chain->scale, "add-borders", TRUE, NULL);
1299       gst_bin_add (bin, chain->scale);
1300       if (prev) {
1301         if (!gst_element_link_pads_full (prev, "src", chain->scale, "sink",
1302                 GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
1303           goto link_failed;
1304       } else {
1305         head = chain->scale;
1306       }
1307       prev = chain->scale;
1308     }
1309   }
1310
1311   if (prev) {
1312     GST_DEBUG_OBJECT (playsink, "linking to sink");
1313     if (!gst_element_link_pads_full (prev, "src", chain->sink, NULL,
1314             GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
1315       goto link_failed;
1316   }
1317
1318   pad = gst_element_get_static_pad (head, "sink");
1319   chain->sinkpad = gst_ghost_pad_new ("sink", pad);
1320   gst_object_unref (pad);
1321
1322   gst_element_add_pad (chain->chain.bin, chain->sinkpad);
1323
1324   return chain;
1325
1326   /* ERRORS */
1327 no_sinks:
1328   {
1329     if (!elem && !playsink->video_sink) {
1330       post_missing_element_message (playsink, "autovideosink");
1331       if (strcmp (DEFAULT_VIDEOSINK, "autovideosink")) {
1332         post_missing_element_message (playsink, DEFAULT_VIDEOSINK);
1333         GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
1334             (_("Both autovideosink and %s elements are missing."),
1335                 DEFAULT_VIDEOSINK), (NULL));
1336       } else {
1337         GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
1338             (_("The autovideosink element is missing.")), (NULL));
1339       }
1340     } else {
1341       if (playsink->video_sink) {
1342         GST_ELEMENT_ERROR (playsink, CORE, STATE_CHANGE,
1343             (_("Configured videosink %s is not working."),
1344                 GST_ELEMENT_NAME (playsink->video_sink)), (NULL));
1345       } else if (strcmp (DEFAULT_VIDEOSINK, "autovideosink")) {
1346         GST_ELEMENT_ERROR (playsink, CORE, STATE_CHANGE,
1347             (_("Both autovideosink and %s elements are not working."),
1348                 DEFAULT_VIDEOSINK), (NULL));
1349       } else {
1350         GST_ELEMENT_ERROR (playsink, CORE, STATE_CHANGE,
1351             (_("The autovideosink element is not working.")), (NULL));
1352       }
1353     }
1354     free_chain ((GstPlayChain *) chain);
1355     return NULL;
1356   }
1357 link_failed:
1358   {
1359     GST_ELEMENT_ERROR (playsink, CORE, PAD,
1360         (NULL), ("Failed to configure the video sink."));
1361     /* checking sink made it READY */
1362     gst_element_set_state (chain->sink, GST_STATE_NULL);
1363     free_chain ((GstPlayChain *) chain);
1364     return NULL;
1365   }
1366 }
1367
1368 static gboolean
1369 setup_video_chain (GstPlaySink * playsink, gboolean raw, gboolean async)
1370 {
1371   GstElement *elem;
1372   GstPlayVideoChain *chain;
1373   GstStateChangeReturn ret;
1374
1375   chain = playsink->videochain;
1376
1377   /* if the chain was active we don't do anything */
1378   if (GST_PLAY_CHAIN (chain)->activated == TRUE)
1379     return TRUE;
1380
1381   if (chain->chain.raw != raw)
1382     return FALSE;
1383
1384   /* try to set the sink element to READY again */
1385   ret = gst_element_set_state (chain->sink, GST_STATE_READY);
1386   if (ret == GST_STATE_CHANGE_FAILURE)
1387     return FALSE;
1388
1389   /* find ts-offset element */
1390   chain->ts_offset =
1391       gst_play_sink_find_property_sinks (playsink, chain->sink, "ts-offset",
1392       G_TYPE_INT64);
1393
1394   /* if we can disable async behaviour of the sink, we can avoid adding a
1395    * queue for the audio chain. */
1396   elem =
1397       gst_play_sink_find_property_sinks (playsink, chain->sink, "async",
1398       G_TYPE_BOOLEAN);
1399   if (elem) {
1400     GST_DEBUG_OBJECT (playsink, "setting async property to %d on element %s",
1401         async, GST_ELEMENT_NAME (elem));
1402     g_object_set (elem, "async", async, NULL);
1403     chain->async = async;
1404   } else {
1405     GST_DEBUG_OBJECT (playsink, "no async property on the sink");
1406     chain->async = TRUE;
1407   }
1408   return TRUE;
1409 }
1410
1411 /* make an element for playback of video with subtitles embedded.
1412  *
1413  *  +--------------------------------------------+
1414  *  | tbin                                       |
1415  *  |     +--------+      +-----------------+    |
1416  *  |     | queue  |      | subtitleoverlay |    |
1417  * video--src     sink---video_sink         |    |
1418  *  |     +--------+     |                src--src
1419  * text------------------text_sink          |    |
1420  *  |                     +-----------------+    |
1421  *  +--------------------------------------------+
1422  *
1423  */
1424 static GstPlayTextChain *
1425 gen_text_chain (GstPlaySink * playsink)
1426 {
1427   GstPlayTextChain *chain;
1428   GstBin *bin;
1429   GstElement *elem;
1430   GstPad *videosinkpad, *textsinkpad, *srcpad;
1431
1432   chain = g_new0 (GstPlayTextChain, 1);
1433   chain->chain.playsink = playsink;
1434
1435   GST_DEBUG_OBJECT (playsink, "making text chain %p", chain);
1436
1437   chain->chain.bin = gst_bin_new ("tbin");
1438   bin = GST_BIN_CAST (chain->chain.bin);
1439   gst_object_ref_sink (bin);
1440
1441   videosinkpad = textsinkpad = srcpad = NULL;
1442
1443   /* first try to hook the text pad to the custom sink */
1444   if (playsink->text_sink) {
1445     GST_DEBUG_OBJECT (playsink, "trying configured textsink");
1446     chain->sink = try_element (playsink, playsink->text_sink, FALSE);
1447     if (chain->sink) {
1448       elem =
1449           gst_play_sink_find_property_sinks (playsink, chain->sink, "async",
1450           G_TYPE_BOOLEAN);
1451       if (elem) {
1452         /* make sure the sparse subtitles don't participate in the preroll */
1453         g_object_set (elem, "async", FALSE, NULL);
1454         GST_DEBUG_OBJECT (playsink, "adding custom text sink");
1455         gst_bin_add (bin, chain->sink);
1456         /* NOTE streamsynchronizer needs streams decoupled */
1457         /* make a little queue */
1458         chain->queue = gst_element_factory_make ("queue", "subqueue");
1459         if (chain->queue == NULL) {
1460           post_missing_element_message (playsink, "queue");
1461           GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1462               (_("Missing element '%s' - check your GStreamer installation."),
1463                   "queue"), ("rendering might be suboptimal"));
1464         } else {
1465           g_object_set (G_OBJECT (chain->queue), "max-size-buffers", 3,
1466               "max-size-bytes", 0, "max-size-time", (gint64) 0,
1467               "silent", TRUE, NULL);
1468           gst_bin_add (bin, chain->queue);
1469         }
1470         /* we have a custom sink, this will be our textsinkpad */
1471         if (gst_element_link_pads_full (chain->queue, "src", chain->sink,
1472                 "sink", GST_PAD_LINK_CHECK_TEMPLATE_CAPS)) {
1473           /* we're all fine now and we can add the sink to the chain */
1474           GST_DEBUG_OBJECT (playsink, "using custom text sink");
1475           textsinkpad = gst_element_get_static_pad (chain->queue, "sink");
1476         } else {
1477           GST_WARNING_OBJECT (playsink,
1478               "can't find a sink pad on custom text sink");
1479           gst_bin_remove (bin, chain->sink);
1480           gst_bin_remove (bin, chain->queue);
1481           chain->sink = NULL;
1482           chain->queue = NULL;
1483         }
1484         /* try to set sync to true but it's no biggie when we can't */
1485         if ((elem =
1486                 gst_play_sink_find_property_sinks (playsink, chain->sink,
1487                     "sync", G_TYPE_BOOLEAN)))
1488           g_object_set (elem, "sync", TRUE, NULL);
1489       } else {
1490         GST_WARNING_OBJECT (playsink,
1491             "can't find async property in custom text sink");
1492       }
1493     }
1494     if (textsinkpad == NULL) {
1495       GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1496           (_("Custom text sink element is not usable.")),
1497           ("fallback to default textoverlay"));
1498     }
1499   }
1500
1501   if (textsinkpad == NULL) {
1502     if (!(playsink->flags & GST_PLAY_FLAG_NATIVE_VIDEO)) {
1503       /* make a little queue */
1504       chain->queue = gst_element_factory_make ("queue", "vqueue");
1505       if (chain->queue == NULL) {
1506         post_missing_element_message (playsink, "queue");
1507         GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1508             (_("Missing element '%s' - check your GStreamer installation."),
1509                 "queue"), ("video rendering might be suboptimal"));
1510       } else {
1511         g_object_set (G_OBJECT (chain->queue), "max-size-buffers", 3,
1512             "max-size-bytes", 0, "max-size-time", (gint64) 0,
1513             "silent", TRUE, NULL);
1514         gst_bin_add (bin, chain->queue);
1515         videosinkpad = gst_element_get_static_pad (chain->queue, "sink");
1516       }
1517
1518       chain->overlay =
1519           gst_element_factory_make ("subtitleoverlay", "suboverlay");
1520       if (chain->overlay == NULL) {
1521         post_missing_element_message (playsink, "subtitleoverlay");
1522         GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1523             (_("Missing element '%s' - check your GStreamer installation."),
1524                 "subtitleoverlay"), ("subtitle rendering disabled"));
1525       } else {
1526         GstElement *element;
1527
1528         gst_bin_add (bin, chain->overlay);
1529
1530         g_object_set (G_OBJECT (chain->overlay), "silent", FALSE, NULL);
1531         if (playsink->font_desc) {
1532           g_object_set (G_OBJECT (chain->overlay), "font-desc",
1533               playsink->font_desc, NULL);
1534         }
1535         if (playsink->subtitle_encoding) {
1536           g_object_set (G_OBJECT (chain->overlay), "subtitle-encoding",
1537               playsink->subtitle_encoding, NULL);
1538         }
1539
1540         gst_element_link_pads_full (chain->queue, "src", chain->overlay,
1541             "video_sink", GST_PAD_LINK_CHECK_TEMPLATE_CAPS);
1542
1543         /* make another little queue to decouple streams */
1544         element = gst_element_factory_make ("queue", "subqueue");
1545         if (element == NULL) {
1546           post_missing_element_message (playsink, "queue");
1547           GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1548               (_("Missing element '%s' - check your GStreamer installation."),
1549                   "queue"), ("rendering might be suboptimal"));
1550         } else {
1551           g_object_set (G_OBJECT (element), "max-size-buffers", 3,
1552               "max-size-bytes", 0, "max-size-time", (gint64) 0,
1553               "silent", TRUE, NULL);
1554           gst_bin_add (bin, element);
1555           gst_element_link_pads_full (element, "src", chain->overlay,
1556               "subtitle_sink", GST_PAD_LINK_CHECK_TEMPLATE_CAPS);
1557           textsinkpad = gst_element_get_static_pad (element, "sink");
1558           srcpad = gst_element_get_static_pad (chain->overlay, "src");
1559         }
1560       }
1561     }
1562   }
1563
1564   if (videosinkpad == NULL) {
1565     /* if we still don't have a videosink, we don't have an overlay. the only
1566      * thing we can do is insert an identity and ghost the src
1567      * and sink pads. */
1568     chain->identity = gst_element_factory_make ("identity", "tidentity");
1569     if (chain->identity == NULL) {
1570       post_missing_element_message (playsink, "identity");
1571       GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
1572           (_("Missing element '%s' - check your GStreamer installation."),
1573               "identity"), (NULL));
1574     } else {
1575       g_object_set (chain->identity, "signal-handoffs", FALSE, NULL);
1576       g_object_set (chain->identity, "silent", TRUE, NULL);
1577       gst_bin_add (bin, chain->identity);
1578       srcpad = gst_element_get_static_pad (chain->identity, "src");
1579       videosinkpad = gst_element_get_static_pad (chain->identity, "sink");
1580     }
1581   }
1582
1583   /* expose the ghostpads */
1584   if (videosinkpad) {
1585     chain->videosinkpad = gst_ghost_pad_new ("sink", videosinkpad);
1586     gst_object_unref (videosinkpad);
1587     gst_element_add_pad (chain->chain.bin, chain->videosinkpad);
1588   }
1589   if (textsinkpad) {
1590     chain->textsinkpad = gst_ghost_pad_new ("text_sink", textsinkpad);
1591     gst_object_unref (textsinkpad);
1592     gst_element_add_pad (chain->chain.bin, chain->textsinkpad);
1593   }
1594   if (srcpad) {
1595     chain->srcpad = gst_ghost_pad_new ("src", srcpad);
1596     gst_object_unref (srcpad);
1597     gst_element_add_pad (chain->chain.bin, chain->srcpad);
1598   }
1599
1600   return chain;
1601 }
1602
1603 static void
1604 notify_volume_cb (GObject * object, GParamSpec * pspec, GstPlaySink * playsink)
1605 {
1606   gdouble vol;
1607
1608   g_object_get (object, "volume", &vol, NULL);
1609   playsink->volume = vol;
1610
1611   g_object_notify (G_OBJECT (playsink), "volume");
1612 }
1613
1614 static void
1615 notify_mute_cb (GObject * object, GParamSpec * pspec, GstPlaySink * playsink)
1616 {
1617   gboolean mute;
1618
1619   g_object_get (object, "mute", &mute, NULL);
1620   playsink->mute = mute;
1621
1622   g_object_notify (G_OBJECT (playsink), "mute");
1623 }
1624
1625 /* make the chain that contains the elements needed to perform
1626  * audio playback.
1627  *
1628  * We add a tee as the first element so that we can link the visualisation chain
1629  * to it when requested.
1630  *
1631  *  +-------------------------------------------------------------+
1632  *  | abin                                                        |
1633  *  |      +---------+   +----------+   +---------+   +---------+ |
1634  *  |      |audioconv|   |audioscale|   | volume  |   |audiosink| |
1635  *  |   +-srck      src-sink       src-sink      src-sink       | |
1636  *  |   |  +---------+   +----------+   +---------+   +---------+ |
1637  * sink-+                                                         |
1638  *  +-------------------------------------------------------------+
1639  */
1640 static GstPlayAudioChain *
1641 gen_audio_chain (GstPlaySink * playsink, gboolean raw)
1642 {
1643   GstPlayAudioChain *chain;
1644   GstBin *bin;
1645   gboolean have_volume;
1646   GstPad *pad;
1647   GstElement *head, *prev, *elem = NULL;
1648
1649   chain = g_new0 (GstPlayAudioChain, 1);
1650   chain->chain.playsink = playsink;
1651   chain->chain.raw = raw;
1652
1653   GST_DEBUG_OBJECT (playsink, "making audio chain %p", chain);
1654
1655   if (playsink->audio_sink) {
1656     GST_DEBUG_OBJECT (playsink, "trying configured audiosink %" GST_PTR_FORMAT,
1657         playsink->audio_sink);
1658     chain->sink = try_element (playsink, playsink->audio_sink, FALSE);
1659   } else {
1660     /* only try fallback if no specific sink was chosen */
1661     if (chain->sink == NULL) {
1662       GST_DEBUG_OBJECT (playsink, "trying autoaudiosink");
1663       elem = gst_element_factory_make ("autoaudiosink", "audiosink");
1664       chain->sink = try_element (playsink, elem, TRUE);
1665     }
1666     if (chain->sink == NULL) {
1667       /* if default sink from config.h is different then try it too */
1668       if (strcmp (DEFAULT_AUDIOSINK, "autoaudiosink")) {
1669         GST_DEBUG_OBJECT (playsink, "trying " DEFAULT_AUDIOSINK);
1670         elem = gst_element_factory_make (DEFAULT_AUDIOSINK, "audiosink");
1671         chain->sink = try_element (playsink, elem, TRUE);
1672       }
1673     }
1674   }
1675   if (chain->sink == NULL)
1676     goto no_sinks;
1677
1678   chain->chain.bin = gst_bin_new ("abin");
1679   bin = GST_BIN_CAST (chain->chain.bin);
1680   gst_object_ref_sink (bin);
1681   gst_bin_add (bin, chain->sink);
1682
1683   /* we have to add a queue when we need to decouple for the video sink in
1684    * visualisations */
1685   GST_DEBUG_OBJECT (playsink, "adding audio queue");
1686   chain->queue = gst_element_factory_make ("queue", "aqueue");
1687   if (chain->queue == NULL) {
1688     post_missing_element_message (playsink, "queue");
1689     GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1690         (_("Missing element '%s' - check your GStreamer installation."),
1691             "queue"), ("audio playback and visualizations might not work"));
1692     head = chain->sink;
1693     prev = NULL;
1694   } else {
1695     g_object_set (chain->queue, "silent", TRUE, NULL);
1696     gst_bin_add (bin, chain->queue);
1697     prev = head = chain->queue;
1698   }
1699
1700   /* find ts-offset element */
1701   chain->ts_offset =
1702       gst_play_sink_find_property_sinks (playsink, chain->sink, "ts-offset",
1703       G_TYPE_INT64);
1704
1705   /* check if the sink, or something within the sink, has the volume property.
1706    * If it does we don't need to add a volume element.  */
1707   elem =
1708       gst_play_sink_find_property_sinks (playsink, chain->sink, "volume",
1709       G_TYPE_DOUBLE);
1710   if (elem) {
1711     chain->volume = elem;
1712
1713     g_signal_connect (chain->volume, "notify::volume",
1714         G_CALLBACK (notify_volume_cb), playsink);
1715
1716     GST_DEBUG_OBJECT (playsink, "the sink has a volume property");
1717     have_volume = TRUE;
1718     chain->sink_volume = TRUE;
1719     /* if the sink also has a mute property we can use this as well. We'll only
1720      * use the mute property if there is a volume property. We can simulate the
1721      * mute with the volume otherwise. */
1722     chain->mute =
1723         gst_play_sink_find_property_sinks (playsink, chain->sink, "mute",
1724         G_TYPE_BOOLEAN);
1725     if (chain->mute) {
1726       GST_DEBUG_OBJECT (playsink, "the sink has a mute property");
1727       g_signal_connect (chain->mute, "notify::mute",
1728           G_CALLBACK (notify_mute_cb), playsink);
1729     }
1730     /* use the sink to control the volume and mute */
1731     if (playsink->volume_changed) {
1732       g_object_set (G_OBJECT (chain->volume), "volume", playsink->volume, NULL);
1733       playsink->volume_changed = FALSE;
1734     }
1735     if (playsink->mute_changed) {
1736       if (chain->mute) {
1737         g_object_set (chain->mute, "mute", playsink->mute, NULL);
1738       } else {
1739         if (playsink->mute)
1740           g_object_set (chain->volume, "volume", (gdouble) 0.0, NULL);
1741       }
1742       playsink->mute_changed = FALSE;
1743     }
1744   } else {
1745     /* no volume, we need to add a volume element when we can */
1746     GST_DEBUG_OBJECT (playsink, "the sink has no volume property");
1747     have_volume = FALSE;
1748     chain->sink_volume = FALSE;
1749   }
1750
1751   if (raw && !(playsink->flags & GST_PLAY_FLAG_NATIVE_AUDIO)) {
1752     GST_DEBUG_OBJECT (playsink, "creating audioconvert");
1753     chain->conv = gst_element_factory_make ("audioconvert", "aconv");
1754     if (chain->conv == NULL) {
1755       post_missing_element_message (playsink, "audioconvert");
1756       GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1757           (_("Missing element '%s' - check your GStreamer installation."),
1758               "audioconvert"), ("possibly a liboil version mismatch?"));
1759     } else {
1760       gst_bin_add (bin, chain->conv);
1761       if (prev) {
1762         if (!gst_element_link_pads_full (prev, "src", chain->conv, "sink",
1763                 GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
1764           goto link_failed;
1765       } else {
1766         head = chain->conv;
1767       }
1768       prev = chain->conv;
1769     }
1770
1771     GST_DEBUG_OBJECT (playsink, "creating audioresample");
1772     chain->resample = gst_element_factory_make ("audioresample", "aresample");
1773     if (chain->resample == NULL) {
1774       post_missing_element_message (playsink, "audioresample");
1775       GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1776           (_("Missing element '%s' - check your GStreamer installation."),
1777               "audioresample"), ("possibly a liboil version mismatch?"));
1778     } else {
1779       gst_bin_add (bin, chain->resample);
1780       if (prev) {
1781         if (!gst_element_link_pads_full (prev, "src", chain->resample, "sink",
1782                 GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
1783           goto link_failed;
1784       } else {
1785         head = chain->resample;
1786       }
1787       prev = chain->resample;
1788     }
1789
1790     if (!have_volume && playsink->flags & GST_PLAY_FLAG_SOFT_VOLUME) {
1791       GST_DEBUG_OBJECT (playsink, "creating volume");
1792       chain->volume = gst_element_factory_make ("volume", "volume");
1793       if (chain->volume == NULL) {
1794         post_missing_element_message (playsink, "volume");
1795         GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
1796             (_("Missing element '%s' - check your GStreamer installation."),
1797                 "volume"), ("possibly a liboil version mismatch?"));
1798       } else {
1799         have_volume = TRUE;
1800
1801         g_signal_connect (chain->volume, "notify::volume",
1802             G_CALLBACK (notify_volume_cb), playsink);
1803
1804         /* volume also has the mute property */
1805         chain->mute = chain->volume;
1806         g_signal_connect (chain->mute, "notify::mute",
1807             G_CALLBACK (notify_mute_cb), playsink);
1808
1809         /* configure with the latest volume and mute */
1810         g_object_set (G_OBJECT (chain->volume), "volume", playsink->volume,
1811             NULL);
1812         g_object_set (G_OBJECT (chain->mute), "mute", playsink->mute, NULL);
1813         gst_bin_add (bin, chain->volume);
1814
1815         if (prev) {
1816           if (!gst_element_link_pads_full (prev, "src", chain->volume, "sink",
1817                   GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
1818             goto link_failed;
1819         } else {
1820           head = chain->volume;
1821         }
1822         prev = chain->volume;
1823       }
1824     }
1825   }
1826
1827   if (prev) {
1828     /* we only have to link to the previous element if we have something in
1829      * front of the sink */
1830     GST_DEBUG_OBJECT (playsink, "linking to sink");
1831     if (!gst_element_link_pads_full (prev, "src", chain->sink, NULL,
1832             GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
1833       goto link_failed;
1834   }
1835
1836   /* post a warning if we have no way to configure the volume */
1837   if (!have_volume) {
1838     GST_ELEMENT_WARNING (playsink, STREAM, NOT_IMPLEMENTED,
1839         (_("No volume control found")), ("Volume/mute is not available"));
1840   }
1841
1842   /* and ghost the sinkpad of the headmost element */
1843   GST_DEBUG_OBJECT (playsink, "ghosting sink pad");
1844   pad = gst_element_get_static_pad (head, "sink");
1845   chain->sinkpad = gst_ghost_pad_new ("sink", pad);
1846   gst_object_unref (pad);
1847   gst_element_add_pad (chain->chain.bin, chain->sinkpad);
1848
1849   return chain;
1850
1851   /* ERRORS */
1852 no_sinks:
1853   {
1854     if (!elem && !playsink->audio_sink) {
1855       post_missing_element_message (playsink, "autoaudiosink");
1856       if (strcmp (DEFAULT_AUDIOSINK, "autoaudiosink")) {
1857         post_missing_element_message (playsink, DEFAULT_AUDIOSINK);
1858         GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
1859             (_("Both autoaudiosink and %s elements are missing."),
1860                 DEFAULT_AUDIOSINK), (NULL));
1861       } else {
1862         GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
1863             (_("The autoaudiosink element is missing.")), (NULL));
1864       }
1865     } else {
1866       if (playsink->audio_sink) {
1867         GST_ELEMENT_ERROR (playsink, CORE, STATE_CHANGE,
1868             (_("Configured audiosink %s is not working."),
1869                 GST_ELEMENT_NAME (playsink->audio_sink)), (NULL));
1870       } else if (strcmp (DEFAULT_AUDIOSINK, "autoaudiosink")) {
1871         GST_ELEMENT_ERROR (playsink, CORE, STATE_CHANGE,
1872             (_("Both autoaudiosink and %s elements are not working."),
1873                 DEFAULT_AUDIOSINK), (NULL));
1874       } else {
1875         GST_ELEMENT_ERROR (playsink, CORE, STATE_CHANGE,
1876             (_("The autoaudiosink element is not working.")), (NULL));
1877       }
1878     }
1879     free_chain ((GstPlayChain *) chain);
1880     return NULL;
1881   }
1882 link_failed:
1883   {
1884     GST_ELEMENT_ERROR (playsink, CORE, PAD,
1885         (NULL), ("Failed to configure the audio sink."));
1886     /* checking sink made it READY */
1887     gst_element_set_state (chain->sink, GST_STATE_NULL);
1888     free_chain ((GstPlayChain *) chain);
1889     return NULL;
1890   }
1891 }
1892
1893 static gboolean
1894 setup_audio_chain (GstPlaySink * playsink, gboolean raw)
1895 {
1896   GstElement *elem;
1897   GstPlayAudioChain *chain;
1898   GstStateChangeReturn ret;
1899
1900   chain = playsink->audiochain;
1901
1902   /* if the chain was active we don't do anything */
1903   if (GST_PLAY_CHAIN (chain)->activated == TRUE)
1904     return TRUE;
1905
1906   if (chain->chain.raw != raw)
1907     return FALSE;
1908
1909   /* try to set the sink element to READY again */
1910   ret = gst_element_set_state (chain->sink, GST_STATE_READY);
1911   if (ret == GST_STATE_CHANGE_FAILURE)
1912     return FALSE;
1913
1914   /* find ts-offset element */
1915   chain->ts_offset =
1916       gst_play_sink_find_property_sinks (playsink, chain->sink, "ts-offset",
1917       G_TYPE_INT64);
1918
1919   /* check if the sink, or something within the sink, has the volume property.
1920    * If it does we don't need to add a volume element.  */
1921   elem =
1922       gst_play_sink_find_property_sinks (playsink, chain->sink, "volume",
1923       G_TYPE_DOUBLE);
1924   if (elem) {
1925     chain->volume = elem;
1926
1927     if (playsink->volume_changed) {
1928       GST_DEBUG_OBJECT (playsink, "the sink has a volume property, setting %f",
1929           playsink->volume);
1930       /* use the sink to control the volume */
1931       g_object_set (G_OBJECT (chain->volume), "volume", playsink->volume, NULL);
1932       playsink->volume_changed = FALSE;
1933     }
1934
1935     g_signal_connect (chain->volume, "notify::volume",
1936         G_CALLBACK (notify_volume_cb), playsink);
1937     /* if the sink also has a mute property we can use this as well. We'll only
1938      * use the mute property if there is a volume property. We can simulate the
1939      * mute with the volume otherwise. */
1940     chain->mute =
1941         gst_play_sink_find_property_sinks (playsink, chain->sink, "mute",
1942         G_TYPE_BOOLEAN);
1943     if (chain->mute) {
1944       GST_DEBUG_OBJECT (playsink, "the sink has a mute property");
1945       g_signal_connect (chain->mute, "notify::mute",
1946           G_CALLBACK (notify_mute_cb), playsink);
1947     }
1948   } else {
1949     /* no volume, we need to add a volume element when we can */
1950     GST_DEBUG_OBJECT (playsink, "the sink has no volume property");
1951     if (!raw) {
1952       GST_LOG_OBJECT (playsink, "non-raw format, can't do soft volume control");
1953
1954       disconnect_chain (chain, playsink);
1955       chain->volume = NULL;
1956       chain->mute = NULL;
1957     } else {
1958       /* both last and current chain are raw audio, there should be a volume
1959        * element already, unless the sink changed from one with a volume
1960        * property to one that hasn't got a volume property, in which case we
1961        * re-generate the chain */
1962       if (chain->volume == NULL) {
1963         GST_DEBUG_OBJECT (playsink, "no existing volume element to re-use");
1964         /* undo background state change done earlier */
1965         gst_element_set_state (chain->sink, GST_STATE_NULL);
1966         return FALSE;
1967       }
1968
1969       GST_DEBUG_OBJECT (playsink, "reusing existing volume element");
1970     }
1971   }
1972   return TRUE;
1973 }
1974
1975 /*
1976  *  +-------------------------------------------------------------------+
1977  *  | visbin                                                            |
1978  *  |      +----------+   +------------+   +----------+   +-------+     |
1979  *  |      | visqueue |   | audioconv  |   | audiores |   |  vis  |     |
1980  *  |   +-sink       src-sink + samp  src-sink       src-sink    src-+  |
1981  *  |   |  +----------+   +------------+   +----------+   +-------+  |  |
1982  * sink-+                                                            +-src
1983  *  +-------------------------------------------------------------------+
1984  *
1985  */
1986 static GstPlayVisChain *
1987 gen_vis_chain (GstPlaySink * playsink)
1988 {
1989   GstPlayVisChain *chain;
1990   GstBin *bin;
1991   gboolean res;
1992   GstPad *pad;
1993   GstElement *elem;
1994
1995   chain = g_new0 (GstPlayVisChain, 1);
1996   chain->chain.playsink = playsink;
1997
1998   GST_DEBUG_OBJECT (playsink, "making vis chain %p", chain);
1999
2000   chain->chain.bin = gst_bin_new ("visbin");
2001   bin = GST_BIN_CAST (chain->chain.bin);
2002   gst_object_ref_sink (bin);
2003
2004   /* we're queuing raw audio here, we can remove this queue when we can disable
2005    * async behaviour in the video sink. */
2006   chain->queue = gst_element_factory_make ("queue", "visqueue");
2007   if (chain->queue == NULL)
2008     goto no_queue;
2009   g_object_set (chain->queue, "silent", TRUE, NULL);
2010   gst_bin_add (bin, chain->queue);
2011
2012   chain->conv = gst_element_factory_make ("audioconvert", "aconv");
2013   if (chain->conv == NULL)
2014     goto no_audioconvert;
2015   gst_bin_add (bin, chain->conv);
2016
2017   chain->resample = gst_element_factory_make ("audioresample", "aresample");
2018   if (chain->resample == NULL)
2019     goto no_audioresample;
2020   gst_bin_add (bin, chain->resample);
2021
2022   /* this pad will be used for blocking the dataflow and switching the vis
2023    * plugin */
2024   chain->blockpad = gst_element_get_static_pad (chain->resample, "src");
2025
2026   if (playsink->visualisation) {
2027     GST_DEBUG_OBJECT (playsink, "trying configure vis");
2028     chain->vis = try_element (playsink, playsink->visualisation, FALSE);
2029   }
2030   if (chain->vis == NULL) {
2031     GST_DEBUG_OBJECT (playsink, "trying goom");
2032     elem = gst_element_factory_make ("goom", "vis");
2033     chain->vis = try_element (playsink, elem, TRUE);
2034   }
2035   if (chain->vis == NULL)
2036     goto no_goom;
2037
2038   gst_bin_add (bin, chain->vis);
2039
2040   res = gst_element_link_pads_full (chain->queue, "src", chain->conv, "sink",
2041       GST_PAD_LINK_CHECK_NOTHING);
2042   res &=
2043       gst_element_link_pads_full (chain->conv, "src", chain->resample, "sink",
2044       GST_PAD_LINK_CHECK_NOTHING);
2045   res &=
2046       gst_element_link_pads_full (chain->resample, "src", chain->vis, "sink",
2047       GST_PAD_LINK_CHECK_NOTHING);
2048   if (!res)
2049     goto link_failed;
2050
2051   chain->vissinkpad = gst_element_get_static_pad (chain->vis, "sink");
2052   chain->vissrcpad = gst_element_get_static_pad (chain->vis, "src");
2053
2054   pad = gst_element_get_static_pad (chain->queue, "sink");
2055   chain->sinkpad = gst_ghost_pad_new ("sink", pad);
2056   gst_object_unref (pad);
2057   gst_element_add_pad (chain->chain.bin, chain->sinkpad);
2058
2059   chain->srcpad = gst_ghost_pad_new ("src", chain->vissrcpad);
2060   gst_element_add_pad (chain->chain.bin, chain->srcpad);
2061
2062   return chain;
2063
2064   /* ERRORS */
2065 no_queue:
2066   {
2067     post_missing_element_message (playsink, "queue");
2068     GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
2069         (_("Missing element '%s' - check your GStreamer installation."),
2070             "queue"), (NULL));
2071     free_chain ((GstPlayChain *) chain);
2072     return NULL;
2073   }
2074 no_audioconvert:
2075   {
2076     post_missing_element_message (playsink, "audioconvert");
2077     GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
2078         (_("Missing element '%s' - check your GStreamer installation."),
2079             "audioconvert"), ("possibly a liboil version mismatch?"));
2080     free_chain ((GstPlayChain *) chain);
2081     return NULL;
2082   }
2083 no_audioresample:
2084   {
2085     post_missing_element_message (playsink, "audioresample");
2086     GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
2087         (_("Missing element '%s' - check your GStreamer installation."),
2088             "audioresample"), (NULL));
2089     free_chain ((GstPlayChain *) chain);
2090     return NULL;
2091   }
2092 no_goom:
2093   {
2094     post_missing_element_message (playsink, "goom");
2095     GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
2096         (_("Missing element '%s' - check your GStreamer installation."),
2097             "goom"), (NULL));
2098     free_chain ((GstPlayChain *) chain);
2099     return NULL;
2100   }
2101 link_failed:
2102   {
2103     GST_ELEMENT_ERROR (playsink, CORE, PAD,
2104         (NULL), ("Failed to configure the visualisation element."));
2105     /* element made it to READY */
2106     gst_element_set_state (chain->vis, GST_STATE_NULL);
2107     free_chain ((GstPlayChain *) chain);
2108     return NULL;
2109   }
2110 }
2111
2112 /* this function is called when all the request pads are requested and when we
2113  * have to construct the final pipeline. Based on the flags we construct the
2114  * final output pipelines.
2115  */
2116 gboolean
2117 gst_play_sink_reconfigure (GstPlaySink * playsink)
2118 {
2119   GstPlayFlags flags;
2120   gboolean need_audio, need_video, need_deinterlace, need_vis, need_text;
2121
2122   GST_DEBUG_OBJECT (playsink, "reconfiguring");
2123
2124   /* assume we need nothing */
2125   need_audio = need_video = need_deinterlace = need_vis = need_text = FALSE;
2126
2127   GST_PLAY_SINK_LOCK (playsink);
2128   GST_OBJECT_LOCK (playsink);
2129   /* get flags, there are protected with the object lock */
2130   flags = playsink->flags;
2131   GST_OBJECT_UNLOCK (playsink);
2132
2133   /* figure out which components we need */
2134   if (flags & GST_PLAY_FLAG_TEXT && playsink->text_pad) {
2135     /* we have subtitles and we are requested to show it */
2136     need_text = TRUE;
2137   }
2138
2139   if (((flags & GST_PLAY_FLAG_VIDEO)
2140           || (flags & GST_PLAY_FLAG_NATIVE_VIDEO)) && playsink->video_pad) {
2141     /* we have video and we are requested to show it */
2142     need_video = TRUE;
2143
2144     /* we only deinterlace if native video is not requested and
2145      * we have raw video */
2146     if ((flags & GST_PLAY_FLAG_DEINTERLACE)
2147         && !(flags & GST_PLAY_FLAG_NATIVE_VIDEO) && playsink->video_pad_raw)
2148       need_deinterlace = TRUE;
2149   }
2150
2151   if (playsink->audio_pad) {
2152     if ((flags & GST_PLAY_FLAG_AUDIO) || (flags & GST_PLAY_FLAG_NATIVE_AUDIO)) {
2153       need_audio = TRUE;
2154     }
2155     if (playsink->audio_pad_raw) {
2156       /* only can do vis with raw uncompressed audio */
2157       if (flags & GST_PLAY_FLAG_VIS && !need_video) {
2158         /* also add video when we add visualisation */
2159         need_video = TRUE;
2160         need_vis = TRUE;
2161       }
2162     }
2163   }
2164
2165   /* we have a text_pad and we need text rendering, in this case we need a
2166    * video_pad to combine the video with the text or visualizations */
2167   if (need_text && !need_video) {
2168     if (playsink->video_pad) {
2169       need_video = TRUE;
2170     } else if (need_audio) {
2171       GST_ELEMENT_WARNING (playsink, STREAM, FORMAT,
2172           (_("Can't play a text file without video or visualizations.")),
2173           ("Have text pad but no video pad or visualizations"));
2174       need_text = FALSE;
2175     } else {
2176       GST_ELEMENT_ERROR (playsink, STREAM, FORMAT,
2177           (_("Can't play a text file without video or visualizations.")),
2178           ("Have text pad but no video pad or visualizations"));
2179       GST_PLAY_SINK_UNLOCK (playsink);
2180       return FALSE;
2181     }
2182   }
2183
2184   GST_DEBUG_OBJECT (playsink, "audio:%d, video:%d, vis:%d, text:%d", need_audio,
2185       need_video, need_vis, need_text);
2186
2187   /* set up video pipeline */
2188   if (need_video) {
2189     gboolean raw, async;
2190
2191     /* we need a raw sink when we do vis or when we have a raw pad */
2192     raw = need_vis ? TRUE : playsink->video_pad_raw;
2193     /* we try to set the sink async=FALSE when we need vis, this way we can
2194      * avoid a queue in the audio chain. */
2195     async = !need_vis;
2196
2197     GST_DEBUG_OBJECT (playsink, "adding video, raw %d",
2198         playsink->video_pad_raw);
2199
2200     if (playsink->videochain) {
2201       /* try to reactivate the chain */
2202       if (!setup_video_chain (playsink, raw, async)) {
2203         if (playsink->video_sinkpad_stream_synchronizer) {
2204           gst_element_release_request_pad (GST_ELEMENT_CAST
2205               (playsink->stream_synchronizer),
2206               playsink->video_sinkpad_stream_synchronizer);
2207           gst_object_unref (playsink->video_sinkpad_stream_synchronizer);
2208           playsink->video_sinkpad_stream_synchronizer = NULL;
2209           gst_object_unref (playsink->video_srcpad_stream_synchronizer);
2210           playsink->video_srcpad_stream_synchronizer = NULL;
2211         }
2212
2213         add_chain (GST_PLAY_CHAIN (playsink->videochain), FALSE);
2214         activate_chain (GST_PLAY_CHAIN (playsink->videochain), FALSE);
2215         free_chain ((GstPlayChain *) playsink->videochain);
2216         playsink->videochain = NULL;
2217       }
2218     }
2219
2220     if (!playsink->videochain)
2221       playsink->videochain = gen_video_chain (playsink, raw, async);
2222     if (!playsink->videochain)
2223       goto no_chain;
2224
2225     if (!playsink->video_sinkpad_stream_synchronizer) {
2226       GstIterator *it;
2227
2228       playsink->video_sinkpad_stream_synchronizer =
2229           gst_element_get_request_pad (GST_ELEMENT_CAST
2230           (playsink->stream_synchronizer), "sink_%d");
2231       it = gst_pad_iterate_internal_links
2232           (playsink->video_sinkpad_stream_synchronizer);
2233       g_assert (it);
2234       gst_iterator_next (it,
2235           (gpointer *) & playsink->video_srcpad_stream_synchronizer);
2236       g_assert (playsink->video_srcpad_stream_synchronizer);
2237       gst_iterator_free (it);
2238     }
2239
2240     if (playsink->video_pad)
2241       gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (playsink->video_pad),
2242           playsink->video_sinkpad_stream_synchronizer);
2243
2244     if (need_deinterlace) {
2245       if (!playsink->videodeinterlacechain)
2246         playsink->videodeinterlacechain =
2247             gen_video_deinterlace_chain (playsink);
2248       if (!playsink->videodeinterlacechain)
2249         goto no_chain;
2250
2251       GST_DEBUG_OBJECT (playsink, "adding video deinterlace chain");
2252
2253       GST_DEBUG_OBJECT (playsink, "setting up deinterlacing chain");
2254
2255       add_chain (GST_PLAY_CHAIN (playsink->videodeinterlacechain), TRUE);
2256       activate_chain (GST_PLAY_CHAIN (playsink->videodeinterlacechain), TRUE);
2257
2258       gst_pad_link_full (playsink->video_srcpad_stream_synchronizer,
2259           playsink->videodeinterlacechain->sinkpad, GST_PAD_LINK_CHECK_NOTHING);
2260     } else {
2261       if (playsink->videodeinterlacechain) {
2262         add_chain (GST_PLAY_CHAIN (playsink->videodeinterlacechain), FALSE);
2263         activate_chain (GST_PLAY_CHAIN (playsink->videodeinterlacechain),
2264             FALSE);
2265       }
2266     }
2267
2268     GST_DEBUG_OBJECT (playsink, "adding video chain");
2269     add_chain (GST_PLAY_CHAIN (playsink->videochain), TRUE);
2270     activate_chain (GST_PLAY_CHAIN (playsink->videochain), TRUE);
2271     /* if we are not part of vis or subtitles, set the ghostpad target */
2272     if (!need_vis && !need_text && (!playsink->textchain
2273             || !playsink->text_pad)) {
2274       GST_DEBUG_OBJECT (playsink, "ghosting video sinkpad");
2275       if (need_deinterlace)
2276         gst_pad_link_full (playsink->videodeinterlacechain->srcpad,
2277             playsink->videochain->sinkpad, GST_PAD_LINK_CHECK_NOTHING);
2278       else
2279         gst_pad_link_full (playsink->video_srcpad_stream_synchronizer,
2280             playsink->videochain->sinkpad, GST_PAD_LINK_CHECK_NOTHING);
2281     }
2282   } else {
2283     GST_DEBUG_OBJECT (playsink, "no video needed");
2284     if (playsink->videochain) {
2285       GST_DEBUG_OBJECT (playsink, "removing video chain");
2286       if (playsink->vischain) {
2287         GstPad *srcpad;
2288
2289         GST_DEBUG_OBJECT (playsink, "unlinking vis chain");
2290
2291         /* also had visualisation, release the tee srcpad before we then
2292          * unlink the video from it */
2293         if (playsink->audio_tee_vissrc) {
2294           gst_element_release_request_pad (playsink->audio_tee,
2295               playsink->audio_tee_vissrc);
2296           gst_object_unref (playsink->audio_tee_vissrc);
2297           playsink->audio_tee_vissrc = NULL;
2298         }
2299         srcpad =
2300             gst_element_get_static_pad (playsink->vischain->chain.bin, "src");
2301         gst_pad_unlink (srcpad, playsink->videochain->sinkpad);
2302       }
2303
2304       if (playsink->video_sinkpad_stream_synchronizer) {
2305         gst_element_release_request_pad (GST_ELEMENT_CAST
2306             (playsink->stream_synchronizer),
2307             playsink->video_sinkpad_stream_synchronizer);
2308         gst_object_unref (playsink->video_sinkpad_stream_synchronizer);
2309         playsink->video_sinkpad_stream_synchronizer = NULL;
2310         gst_object_unref (playsink->video_srcpad_stream_synchronizer);
2311         playsink->video_srcpad_stream_synchronizer = NULL;
2312       }
2313
2314       add_chain (GST_PLAY_CHAIN (playsink->videochain), FALSE);
2315       activate_chain (GST_PLAY_CHAIN (playsink->videochain), FALSE);
2316       playsink->videochain->ts_offset = NULL;
2317     }
2318
2319     if (playsink->videodeinterlacechain) {
2320       add_chain (GST_PLAY_CHAIN (playsink->videodeinterlacechain), FALSE);
2321       activate_chain (GST_PLAY_CHAIN (playsink->videodeinterlacechain), FALSE);
2322     }
2323
2324     if (playsink->video_pad)
2325       gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (playsink->video_pad), NULL);
2326   }
2327
2328   if (need_audio) {
2329     gboolean raw;
2330
2331     GST_DEBUG_OBJECT (playsink, "adding audio");
2332
2333     /* get a raw sink if we are asked for a raw pad */
2334     raw = playsink->audio_pad_raw;
2335
2336     if (playsink->audiochain) {
2337       /* try to reactivate the chain */
2338       if (!setup_audio_chain (playsink, raw)) {
2339         GST_DEBUG_OBJECT (playsink, "removing current audio chain");
2340         if (playsink->audio_tee_asrc) {
2341           gst_element_release_request_pad (playsink->audio_tee,
2342               playsink->audio_tee_asrc);
2343           gst_object_unref (playsink->audio_tee_asrc);
2344           playsink->audio_tee_asrc = NULL;
2345         }
2346
2347         if (playsink->audio_sinkpad_stream_synchronizer) {
2348           gst_element_release_request_pad (GST_ELEMENT_CAST
2349               (playsink->stream_synchronizer),
2350               playsink->audio_sinkpad_stream_synchronizer);
2351           gst_object_unref (playsink->audio_sinkpad_stream_synchronizer);
2352           playsink->audio_sinkpad_stream_synchronizer = NULL;
2353           gst_object_unref (playsink->audio_srcpad_stream_synchronizer);
2354           playsink->audio_srcpad_stream_synchronizer = NULL;
2355         }
2356
2357         add_chain (GST_PLAY_CHAIN (playsink->audiochain), FALSE);
2358         activate_chain (GST_PLAY_CHAIN (playsink->audiochain), FALSE);
2359         disconnect_chain (playsink->audiochain, playsink);
2360         playsink->audiochain->volume = NULL;
2361         playsink->audiochain->mute = NULL;
2362         playsink->audiochain->ts_offset = NULL;
2363         free_chain ((GstPlayChain *) playsink->audiochain);
2364         playsink->audiochain = NULL;
2365         playsink->volume_changed = playsink->mute_changed = FALSE;
2366       }
2367     }
2368
2369     if (!playsink->audiochain) {
2370       GST_DEBUG_OBJECT (playsink, "creating new audio chain");
2371       playsink->audiochain = gen_audio_chain (playsink, raw);
2372     }
2373
2374     if (!playsink->audio_sinkpad_stream_synchronizer) {
2375       GstIterator *it;
2376
2377       playsink->audio_sinkpad_stream_synchronizer =
2378           gst_element_get_request_pad (GST_ELEMENT_CAST
2379           (playsink->stream_synchronizer), "sink_%d");
2380       it = gst_pad_iterate_internal_links
2381           (playsink->audio_sinkpad_stream_synchronizer);
2382       g_assert (it);
2383       gst_iterator_next (it,
2384           (gpointer *) & playsink->audio_srcpad_stream_synchronizer);
2385       g_assert (playsink->audio_srcpad_stream_synchronizer);
2386       gst_iterator_free (it);
2387     }
2388
2389     if (playsink->audiochain) {
2390       GST_DEBUG_OBJECT (playsink, "adding audio chain");
2391       if (playsink->audio_tee_asrc == NULL) {
2392         playsink->audio_tee_asrc =
2393             gst_element_get_request_pad (playsink->audio_tee, "src%d");
2394       }
2395       add_chain (GST_PLAY_CHAIN (playsink->audiochain), TRUE);
2396       activate_chain (GST_PLAY_CHAIN (playsink->audiochain), TRUE);
2397       gst_pad_link_full (playsink->audio_tee_asrc,
2398           playsink->audio_sinkpad_stream_synchronizer,
2399           GST_PAD_LINK_CHECK_NOTHING);
2400       gst_pad_link_full (playsink->audio_srcpad_stream_synchronizer,
2401           playsink->audiochain->sinkpad, GST_PAD_LINK_CHECK_NOTHING);
2402     }
2403   } else {
2404     GST_DEBUG_OBJECT (playsink, "no audio needed");
2405     /* we have no audio or we are requested to not play audio */
2406     if (playsink->audiochain) {
2407       GST_DEBUG_OBJECT (playsink, "removing audio chain");
2408       /* release the audio pad */
2409       if (playsink->audio_tee_asrc) {
2410         gst_element_release_request_pad (playsink->audio_tee,
2411             playsink->audio_tee_asrc);
2412         gst_object_unref (playsink->audio_tee_asrc);
2413         playsink->audio_tee_asrc = NULL;
2414       }
2415
2416       if (playsink->audio_sinkpad_stream_synchronizer) {
2417         gst_element_release_request_pad (GST_ELEMENT_CAST
2418             (playsink->stream_synchronizer),
2419             playsink->audio_sinkpad_stream_synchronizer);
2420         gst_object_unref (playsink->audio_sinkpad_stream_synchronizer);
2421         playsink->audio_sinkpad_stream_synchronizer = NULL;
2422         gst_object_unref (playsink->audio_srcpad_stream_synchronizer);
2423         playsink->audio_srcpad_stream_synchronizer = NULL;
2424       }
2425
2426       if (playsink->audiochain->sink_volume) {
2427         disconnect_chain (playsink->audiochain, playsink);
2428         playsink->audiochain->volume = NULL;
2429         playsink->audiochain->mute = NULL;
2430         playsink->audiochain->ts_offset = NULL;
2431       }
2432       add_chain (GST_PLAY_CHAIN (playsink->audiochain), FALSE);
2433       activate_chain (GST_PLAY_CHAIN (playsink->audiochain), FALSE);
2434     }
2435   }
2436
2437   if (need_vis) {
2438     GstPad *srcpad;
2439
2440     if (!playsink->vischain)
2441       playsink->vischain = gen_vis_chain (playsink);
2442
2443     GST_DEBUG_OBJECT (playsink, "adding visualisation");
2444
2445     if (playsink->vischain) {
2446       GST_DEBUG_OBJECT (playsink, "setting up vis chain");
2447       srcpad =
2448           gst_element_get_static_pad (playsink->vischain->chain.bin, "src");
2449       add_chain (GST_PLAY_CHAIN (playsink->vischain), TRUE);
2450       activate_chain (GST_PLAY_CHAIN (playsink->vischain), TRUE);
2451       if (playsink->audio_tee_vissrc == NULL) {
2452         playsink->audio_tee_vissrc =
2453             gst_element_get_request_pad (playsink->audio_tee, "src%d");
2454       }
2455       gst_pad_link_full (playsink->audio_tee_vissrc,
2456           playsink->vischain->sinkpad, GST_PAD_LINK_CHECK_NOTHING);
2457       gst_pad_link_full (srcpad, playsink->video_sinkpad_stream_synchronizer,
2458           GST_PAD_LINK_CHECK_NOTHING);
2459       gst_pad_link_full (playsink->video_srcpad_stream_synchronizer,
2460           playsink->videochain->sinkpad, GST_PAD_LINK_CHECK_NOTHING);
2461       gst_object_unref (srcpad);
2462     }
2463   } else {
2464     GST_DEBUG_OBJECT (playsink, "no vis needed");
2465     if (playsink->vischain) {
2466       if (playsink->audio_tee_vissrc) {
2467         gst_element_release_request_pad (playsink->audio_tee,
2468             playsink->audio_tee_vissrc);
2469         gst_object_unref (playsink->audio_tee_vissrc);
2470         playsink->audio_tee_vissrc = NULL;
2471       }
2472       GST_DEBUG_OBJECT (playsink, "removing vis chain");
2473       add_chain (GST_PLAY_CHAIN (playsink->vischain), FALSE);
2474       activate_chain (GST_PLAY_CHAIN (playsink->vischain), FALSE);
2475     }
2476   }
2477
2478   if (need_text) {
2479     GST_DEBUG_OBJECT (playsink, "adding text");
2480     if (!playsink->textchain) {
2481       GST_DEBUG_OBJECT (playsink, "creating text chain");
2482       playsink->textchain = gen_text_chain (playsink);
2483     }
2484     if (playsink->textchain) {
2485       GstIterator *it;
2486
2487       GST_DEBUG_OBJECT (playsink, "adding text chain");
2488       if (playsink->textchain->overlay)
2489         g_object_set (G_OBJECT (playsink->textchain->overlay), "silent", FALSE,
2490             NULL);
2491       add_chain (GST_PLAY_CHAIN (playsink->textchain), TRUE);
2492
2493       if (!playsink->text_sinkpad_stream_synchronizer) {
2494         playsink->text_sinkpad_stream_synchronizer =
2495             gst_element_get_request_pad (GST_ELEMENT_CAST
2496             (playsink->stream_synchronizer), "sink_%d");
2497         it = gst_pad_iterate_internal_links
2498             (playsink->text_sinkpad_stream_synchronizer);
2499         g_assert (it);
2500         gst_iterator_next (it,
2501             (gpointer *) & playsink->text_srcpad_stream_synchronizer);
2502         g_assert (playsink->text_srcpad_stream_synchronizer);
2503         gst_iterator_free (it);
2504
2505         gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (playsink->text_pad),
2506             playsink->text_sinkpad_stream_synchronizer);
2507         gst_pad_link_full (playsink->text_srcpad_stream_synchronizer,
2508             playsink->textchain->textsinkpad, GST_PAD_LINK_CHECK_NOTHING);
2509       }
2510
2511       if (need_vis) {
2512         GstPad *srcpad;
2513
2514         srcpad =
2515             gst_element_get_static_pad (playsink->vischain->chain.bin, "src");
2516         gst_pad_unlink (srcpad, playsink->videochain->sinkpad);
2517         gst_pad_link_full (srcpad, playsink->textchain->videosinkpad,
2518             GST_PAD_LINK_CHECK_NOTHING);
2519         gst_object_unref (srcpad);
2520       } else {
2521         if (need_deinterlace)
2522           gst_pad_link_full (playsink->videodeinterlacechain->srcpad,
2523               playsink->textchain->videosinkpad, GST_PAD_LINK_CHECK_NOTHING);
2524         else
2525           gst_pad_link_full (playsink->video_srcpad_stream_synchronizer,
2526               playsink->textchain->videosinkpad, GST_PAD_LINK_CHECK_NOTHING);
2527       }
2528       gst_pad_link_full (playsink->textchain->srcpad,
2529           playsink->videochain->sinkpad, GST_PAD_LINK_CHECK_NOTHING);
2530
2531       activate_chain (GST_PLAY_CHAIN (playsink->textchain), TRUE);
2532     }
2533   } else {
2534     GST_DEBUG_OBJECT (playsink, "no text needed");
2535     /* we have no subtitles/text or we are requested to not show them */
2536
2537     if (playsink->text_sinkpad_stream_synchronizer) {
2538       gst_element_release_request_pad (GST_ELEMENT_CAST
2539           (playsink->stream_synchronizer),
2540           playsink->text_sinkpad_stream_synchronizer);
2541       gst_object_unref (playsink->text_sinkpad_stream_synchronizer);
2542       playsink->text_sinkpad_stream_synchronizer = NULL;
2543       gst_object_unref (playsink->text_srcpad_stream_synchronizer);
2544       playsink->text_srcpad_stream_synchronizer = NULL;
2545     }
2546
2547     if (playsink->textchain) {
2548       if (playsink->text_pad == NULL) {
2549         /* no text pad, remove the chain entirely */
2550         GST_DEBUG_OBJECT (playsink, "removing text chain");
2551         add_chain (GST_PLAY_CHAIN (playsink->textchain), FALSE);
2552         activate_chain (GST_PLAY_CHAIN (playsink->textchain), FALSE);
2553       } else {
2554         /* we have a chain and a textpad, turn the subtitles off */
2555         GST_DEBUG_OBJECT (playsink, "turning off the text");
2556         if (playsink->textchain->overlay)
2557           g_object_set (G_OBJECT (playsink->textchain->overlay), "silent", TRUE,
2558               NULL);
2559       }
2560     }
2561     if (!need_video && playsink->video_pad) {
2562       if (playsink->video_sinkpad_stream_synchronizer) {
2563         gst_element_release_request_pad (GST_ELEMENT_CAST
2564             (playsink->stream_synchronizer),
2565             playsink->video_sinkpad_stream_synchronizer);
2566         gst_object_unref (playsink->video_sinkpad_stream_synchronizer);
2567         playsink->video_sinkpad_stream_synchronizer = NULL;
2568         gst_object_unref (playsink->video_srcpad_stream_synchronizer);
2569         playsink->video_srcpad_stream_synchronizer = NULL;
2570       }
2571
2572       gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (playsink->video_pad), NULL);
2573     }
2574
2575     if (playsink->text_pad && !playsink->textchain)
2576       gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (playsink->text_pad), NULL);
2577   }
2578   update_av_offset (playsink);
2579   do_async_done (playsink);
2580   GST_PLAY_SINK_UNLOCK (playsink);
2581
2582   return TRUE;
2583
2584   /* ERRORS */
2585 no_chain:
2586   {
2587     /* gen_ chain already posted error */
2588     GST_DEBUG_OBJECT (playsink, "failed to setup chain");
2589     GST_PLAY_SINK_UNLOCK (playsink);
2590     return FALSE;
2591   }
2592 }
2593
2594 /**
2595  * gst_play_sink_set_flags:
2596  * @playsink: a #GstPlaySink
2597  * @flags: #GstPlayFlags
2598  *
2599  * Configure @flags on @playsink. The flags control the behaviour of @playsink
2600  * when constructing the sink pipelins.
2601  *
2602  * Returns: TRUE if the flags could be configured.
2603  */
2604 gboolean
2605 gst_play_sink_set_flags (GstPlaySink * playsink, GstPlayFlags flags)
2606 {
2607   g_return_val_if_fail (GST_IS_PLAY_SINK (playsink), FALSE);
2608
2609   GST_OBJECT_LOCK (playsink);
2610   playsink->flags = flags;
2611   GST_OBJECT_UNLOCK (playsink);
2612
2613   return TRUE;
2614 }
2615
2616 /**
2617  * gst_play_sink_get_flags:
2618  * @playsink: a #GstPlaySink
2619  *
2620  * Get the flags of @playsink. That flags control the behaviour of the sink when
2621  * it constructs the sink pipelines.
2622  *
2623  * Returns: the currently configured #GstPlayFlags.
2624  */
2625 GstPlayFlags
2626 gst_play_sink_get_flags (GstPlaySink * playsink)
2627 {
2628   GstPlayFlags res;
2629
2630   g_return_val_if_fail (GST_IS_PLAY_SINK (playsink), 0);
2631
2632   GST_OBJECT_LOCK (playsink);
2633   res = playsink->flags;
2634   GST_OBJECT_UNLOCK (playsink);
2635
2636   return res;
2637 }
2638
2639 void
2640 gst_play_sink_set_font_desc (GstPlaySink * playsink, const gchar * desc)
2641 {
2642   GstPlayTextChain *chain;
2643
2644   GST_PLAY_SINK_LOCK (playsink);
2645   chain = (GstPlayTextChain *) playsink->textchain;
2646   g_free (playsink->font_desc);
2647   playsink->font_desc = g_strdup (desc);
2648   if (chain && chain->overlay) {
2649     g_object_set (chain->overlay, "font-desc", desc, NULL);
2650   }
2651   GST_PLAY_SINK_UNLOCK (playsink);
2652 }
2653
2654 gchar *
2655 gst_play_sink_get_font_desc (GstPlaySink * playsink)
2656 {
2657   gchar *result = NULL;
2658   GstPlayTextChain *chain;
2659
2660   GST_PLAY_SINK_LOCK (playsink);
2661   chain = (GstPlayTextChain *) playsink->textchain;
2662   if (chain && chain->overlay) {
2663     g_object_get (chain->overlay, "font-desc", &result, NULL);
2664     playsink->font_desc = g_strdup (result);
2665   } else {
2666     result = g_strdup (playsink->font_desc);
2667   }
2668   GST_PLAY_SINK_UNLOCK (playsink);
2669
2670   return result;
2671 }
2672
2673 void
2674 gst_play_sink_set_subtitle_encoding (GstPlaySink * playsink,
2675     const gchar * encoding)
2676 {
2677   GstPlayTextChain *chain;
2678
2679   GST_PLAY_SINK_LOCK (playsink);
2680   chain = (GstPlayTextChain *) playsink->textchain;
2681   g_free (playsink->subtitle_encoding);
2682   playsink->subtitle_encoding = g_strdup (encoding);
2683   if (chain && chain->overlay) {
2684     g_object_set (chain->overlay, "subtitle-encoding", encoding, NULL);
2685   }
2686   GST_PLAY_SINK_UNLOCK (playsink);
2687 }
2688
2689 gchar *
2690 gst_play_sink_get_subtitle_encoding (GstPlaySink * playsink)
2691 {
2692   gchar *result = NULL;
2693   GstPlayTextChain *chain;
2694
2695   GST_PLAY_SINK_LOCK (playsink);
2696   chain = (GstPlayTextChain *) playsink->textchain;
2697   if (chain && chain->overlay) {
2698     g_object_get (chain->overlay, "subtitle-encoding", &result, NULL);
2699     playsink->subtitle_encoding = g_strdup (result);
2700   } else {
2701     result = g_strdup (playsink->subtitle_encoding);
2702   }
2703   GST_PLAY_SINK_UNLOCK (playsink);
2704
2705   return result;
2706 }
2707
2708 static void
2709 update_av_offset (GstPlaySink * playsink)
2710 {
2711   gint64 av_offset;
2712   GstPlayAudioChain *achain;
2713   GstPlayVideoChain *vchain;
2714
2715   av_offset = playsink->av_offset;
2716   achain = (GstPlayAudioChain *) playsink->audiochain;
2717   vchain = (GstPlayVideoChain *) playsink->videochain;
2718
2719   if (achain && vchain && achain->ts_offset && vchain->ts_offset) {
2720     g_object_set (achain->ts_offset, "ts-offset", MAX (0, -av_offset), NULL);
2721     g_object_set (vchain->ts_offset, "ts-offset", MAX (0, av_offset), NULL);
2722   } else {
2723     GST_LOG_OBJECT (playsink, "no ts_offset elements");
2724   }
2725 }
2726
2727 void
2728 gst_play_sink_set_av_offset (GstPlaySink * playsink, gint64 av_offset)
2729 {
2730   GST_PLAY_SINK_LOCK (playsink);
2731   playsink->av_offset = av_offset;
2732   update_av_offset (playsink);
2733   GST_PLAY_SINK_UNLOCK (playsink);
2734 }
2735
2736 gint64
2737 gst_play_sink_get_av_offset (GstPlaySink * playsink)
2738 {
2739   gint64 result;
2740
2741   GST_PLAY_SINK_LOCK (playsink);
2742   result = playsink->av_offset;
2743   GST_PLAY_SINK_UNLOCK (playsink);
2744
2745   return result;
2746 }
2747
2748 /**
2749  * gst_play_sink_get_last_frame:
2750  * @playsink: a #GstPlaySink
2751  *
2752  * Get the last displayed frame from @playsink. This frame is in the native
2753  * format of the sink element, the caps on the result buffer contain the format
2754  * of the frame data.
2755  *
2756  * Returns: a #GstBuffer with the frame data or %NULL when no video frame is
2757  * available.
2758  */
2759 GstBuffer *
2760 gst_play_sink_get_last_frame (GstPlaySink * playsink)
2761 {
2762   GstBuffer *result = NULL;
2763   GstPlayVideoChain *chain;
2764
2765   GST_PLAY_SINK_LOCK (playsink);
2766   GST_DEBUG_OBJECT (playsink, "taking last frame");
2767   /* get the video chain if we can */
2768   if ((chain = (GstPlayVideoChain *) playsink->videochain)) {
2769     GST_DEBUG_OBJECT (playsink, "found video chain");
2770     /* see if the chain is active */
2771     if (chain->chain.activated && chain->sink) {
2772       GstElement *elem;
2773
2774       GST_DEBUG_OBJECT (playsink, "video chain active and has a sink");
2775
2776       /* find and get the last-buffer property now */
2777       if ((elem =
2778               gst_play_sink_find_property (playsink, chain->sink,
2779                   "last-buffer", GST_TYPE_BUFFER))) {
2780         GST_DEBUG_OBJECT (playsink, "getting last-buffer property");
2781         g_object_get (elem, "last-buffer", &result, NULL);
2782         gst_object_unref (elem);
2783       }
2784     }
2785   }
2786   GST_PLAY_SINK_UNLOCK (playsink);
2787
2788   return result;
2789 }
2790
2791 /**
2792  * gst_play_sink_convert_frame:
2793  * @playsink: a #GstPlaySink
2794  * @caps: a #GstCaps
2795  *
2796  * Get the last displayed frame from @playsink. If caps is %NULL, the video will
2797  * be in the native format of the sink element and the caps on the buffer
2798  * describe the format of the frame. If @caps is not %NULL, the video
2799  * frame will be converted to the format of the caps.
2800  *
2801  * Returns: a #GstBuffer with the frame data or %NULL when no video frame is
2802  * available or when the conversion failed.
2803  */
2804 GstBuffer *
2805 gst_play_sink_convert_frame (GstPlaySink * playsink, GstCaps * caps)
2806 {
2807   GstBuffer *result;
2808
2809   result = gst_play_sink_get_last_frame (playsink);
2810   if (result != NULL && caps != NULL) {
2811     GstBuffer *temp;
2812     GError *err = NULL;
2813
2814     temp = gst_video_convert_frame (result, caps, 25 * GST_SECOND, &err);
2815     gst_buffer_unref (result);
2816     if (temp == NULL && err) {
2817       /* I'm really uncertain whether we should make playsink post an error
2818        * on the bus or not. It's not like it's a critical issue regarding
2819        * playsink behaviour. */
2820       GST_ERROR ("Error converting frame: %s", err->message);
2821     }
2822     result = temp;
2823   }
2824   return result;
2825 }
2826
2827 /**
2828  * gst_play_sink_request_pad
2829  * @playsink: a #GstPlaySink
2830  * @type: a #GstPlaySinkType
2831  *
2832  * Create or return a pad of @type.
2833  *
2834  * Returns: a #GstPad of @type or %NULL when the pad could not be created.
2835  */
2836 GstPad *
2837 gst_play_sink_request_pad (GstPlaySink * playsink, GstPlaySinkType type)
2838 {
2839   GstPad *res = NULL;
2840   gboolean created = FALSE;
2841   gboolean raw = FALSE;
2842   gboolean activate = TRUE;
2843   const gchar *pad_name = NULL;
2844
2845   GST_DEBUG_OBJECT (playsink, "request pad type %d", type);
2846
2847   GST_PLAY_SINK_LOCK (playsink);
2848   switch (type) {
2849     case GST_PLAY_SINK_TYPE_AUDIO_RAW:
2850       pad_name = "audio_raw_sink";
2851       raw = TRUE;
2852     case GST_PLAY_SINK_TYPE_AUDIO:
2853       if (pad_name == NULL)
2854         pad_name = "audio_sink";
2855       if (!playsink->audio_tee) {
2856         GST_LOG_OBJECT (playsink, "creating tee");
2857         /* create tee when needed. This element will feed the audio sink chain
2858          * and the vis chain. */
2859         playsink->audio_tee = gst_element_factory_make ("tee", "audiotee");
2860         if (playsink->audio_tee == NULL) {
2861           post_missing_element_message (playsink, "tee");
2862           GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
2863               (_("Missing element '%s' - check your GStreamer installation."),
2864                   "tee"), (NULL));
2865           res = NULL;
2866           break;
2867         } else {
2868           playsink->audio_tee_sink =
2869               gst_element_get_static_pad (playsink->audio_tee, "sink");
2870           gst_bin_add (GST_BIN_CAST (playsink), playsink->audio_tee);
2871           gst_element_set_state (playsink->audio_tee, GST_STATE_PAUSED);
2872         }
2873       } else {
2874         gst_element_set_state (playsink->audio_tee, GST_STATE_PAUSED);
2875       }
2876       if (!playsink->audio_pad) {
2877         GST_LOG_OBJECT (playsink, "ghosting tee sinkpad");
2878         playsink->audio_pad =
2879             gst_ghost_pad_new (pad_name, playsink->audio_tee_sink);
2880         created = TRUE;
2881       }
2882       playsink->audio_pad_raw = raw;
2883       res = playsink->audio_pad;
2884       break;
2885     case GST_PLAY_SINK_TYPE_VIDEO_RAW:
2886       pad_name = "video_raw_sink";
2887       raw = TRUE;
2888     case GST_PLAY_SINK_TYPE_VIDEO:
2889       if (pad_name == NULL)
2890         pad_name = "video_sink";
2891       if (!playsink->video_pad) {
2892         GST_LOG_OBJECT (playsink, "ghosting videosink");
2893         playsink->video_pad =
2894             gst_ghost_pad_new_no_target (pad_name, GST_PAD_SINK);
2895         created = TRUE;
2896       }
2897       playsink->video_pad_raw = raw;
2898       res = playsink->video_pad;
2899       break;
2900     case GST_PLAY_SINK_TYPE_TEXT:
2901       GST_LOG_OBJECT (playsink, "ghosting text");
2902       if (!playsink->text_pad) {
2903         playsink->text_pad =
2904             gst_ghost_pad_new_no_target ("text_sink", GST_PAD_SINK);
2905         created = TRUE;
2906       }
2907       res = playsink->text_pad;
2908       break;
2909     case GST_PLAY_SINK_TYPE_FLUSHING:
2910     {
2911       gchar *padname;
2912
2913       /* we need a unique padname for the flushing pad. */
2914       padname = g_strdup_printf ("flushing_%d", playsink->count);
2915       res = gst_ghost_pad_new_no_target (padname, GST_PAD_SINK);
2916       g_free (padname);
2917       playsink->count++;
2918       activate = FALSE;
2919       created = TRUE;
2920       break;
2921     }
2922     default:
2923       res = NULL;
2924       break;
2925   }
2926   GST_PLAY_SINK_UNLOCK (playsink);
2927
2928   if (created && res) {
2929     /* we have to add the pad when it's active or we get an error when the
2930      * element is 'running' */
2931     gst_pad_set_active (res, TRUE);
2932     gst_element_add_pad (GST_ELEMENT_CAST (playsink), res);
2933     if (!activate)
2934       gst_pad_set_active (res, activate);
2935   }
2936
2937   return res;
2938 }
2939
2940 static GstPad *
2941 gst_play_sink_request_new_pad (GstElement * element, GstPadTemplate * templ,
2942     const gchar * name)
2943 {
2944   GstPlaySink *psink;
2945   GstPad *pad;
2946   GstPlaySinkType type;
2947   const gchar *tplname;
2948
2949   g_return_val_if_fail (templ != NULL, NULL);
2950
2951   GST_DEBUG_OBJECT (element, "name:%s", name);
2952
2953   psink = GST_PLAY_SINK (element);
2954   tplname = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
2955
2956   /* Figure out the GstPlaySinkType based on the template */
2957   if (!strcmp (tplname, "audio_sink"))
2958     type = GST_PLAY_SINK_TYPE_AUDIO;
2959   else if (!strcmp (tplname, "audio_raw_sink"))
2960     type = GST_PLAY_SINK_TYPE_AUDIO_RAW;
2961   else if (!strcmp (tplname, "video_sink"))
2962     type = GST_PLAY_SINK_TYPE_VIDEO;
2963   else if (!strcmp (tplname, "video_raw_sink"))
2964     type = GST_PLAY_SINK_TYPE_VIDEO_RAW;
2965   else if (!strcmp (tplname, "text_sink"))
2966     type = GST_PLAY_SINK_TYPE_TEXT;
2967   else
2968     goto unknown_template;
2969
2970   pad = gst_play_sink_request_pad (psink, type);
2971   return pad;
2972
2973 unknown_template:
2974   GST_WARNING_OBJECT (element, "Unknown pad template");
2975   return NULL;
2976 }
2977
2978 void
2979 gst_play_sink_release_pad (GstPlaySink * playsink, GstPad * pad)
2980 {
2981   GstPad **res = NULL;
2982   gboolean untarget = TRUE;
2983
2984   GST_DEBUG_OBJECT (playsink, "release pad %" GST_PTR_FORMAT, pad);
2985
2986   GST_PLAY_SINK_LOCK (playsink);
2987   if (pad == playsink->video_pad) {
2988     res = &playsink->video_pad;
2989   } else if (pad == playsink->audio_pad) {
2990     res = &playsink->audio_pad;
2991   } else if (pad == playsink->text_pad) {
2992     res = &playsink->text_pad;
2993   } else {
2994     /* try to release the given pad anyway, these could be the FLUSHING pads. */
2995     res = &pad;
2996     untarget = FALSE;
2997   }
2998   GST_PLAY_SINK_UNLOCK (playsink);
2999
3000   if (*res) {
3001     GST_DEBUG_OBJECT (playsink, "deactivate pad %" GST_PTR_FORMAT, *res);
3002     gst_pad_set_active (*res, FALSE);
3003     if (untarget) {
3004       GST_DEBUG_OBJECT (playsink, "untargeting pad %" GST_PTR_FORMAT, *res);
3005       gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (*res), NULL);
3006     }
3007     GST_DEBUG_OBJECT (playsink, "remove pad %" GST_PTR_FORMAT, *res);
3008     gst_element_remove_pad (GST_ELEMENT_CAST (playsink), *res);
3009     *res = NULL;
3010   }
3011 }
3012
3013 static void
3014 gst_play_sink_release_request_pad (GstElement * element, GstPad * pad)
3015 {
3016   GstPlaySink *psink = GST_PLAY_SINK (element);
3017
3018   gst_play_sink_release_pad (psink, pad);
3019 }
3020
3021 static void
3022 gst_play_sink_handle_message (GstBin * bin, GstMessage * message)
3023 {
3024   GstPlaySink *playsink;
3025
3026   playsink = GST_PLAY_SINK_CAST (bin);
3027
3028   switch (GST_MESSAGE_TYPE (message)) {
3029     case GST_MESSAGE_STEP_DONE:
3030     {
3031       GstFormat format;
3032       guint64 amount;
3033       gdouble rate;
3034       gboolean flush, intermediate, eos;
3035       guint64 duration;
3036
3037       GST_INFO_OBJECT (playsink, "Handling step-done message");
3038       gst_message_parse_step_done (message, &format, &amount, &rate, &flush,
3039           &intermediate, &duration, &eos);
3040
3041       if (format == GST_FORMAT_BUFFERS) {
3042         /* for the buffer format, we align the other streams */
3043         if (playsink->audiochain) {
3044           GstEvent *event;
3045
3046           event =
3047               gst_event_new_step (GST_FORMAT_TIME, duration, rate, flush,
3048               intermediate);
3049
3050           if (!gst_element_send_event (playsink->audiochain->chain.bin, event)) {
3051             GST_DEBUG_OBJECT (playsink, "Event failed when sent to audio sink");
3052           }
3053         }
3054       }
3055       GST_BIN_CLASS (gst_play_sink_parent_class)->handle_message (bin, message);
3056       break;
3057     }
3058     default:
3059       GST_BIN_CLASS (gst_play_sink_parent_class)->handle_message (bin, message);
3060       break;
3061   }
3062 }
3063
3064 /* Send an event to our sinks until one of them works; don't then send to the
3065  * remaining sinks (unlike GstBin)
3066  * Special case: If a text sink is set we need to send the event
3067  * to them in case it's source is different from the a/v stream's source.
3068  */
3069 static gboolean
3070 gst_play_sink_send_event_to_sink (GstPlaySink * playsink, GstEvent * event)
3071 {
3072   gboolean res = TRUE;
3073
3074   if (playsink->textchain && playsink->textchain->sink) {
3075     gst_event_ref (event);
3076     if ((res = gst_element_send_event (playsink->textchain->chain.bin, event))) {
3077       GST_DEBUG_OBJECT (playsink, "Sent event succesfully to text sink");
3078     } else {
3079       GST_DEBUG_OBJECT (playsink, "Event failed when sent to text sink");
3080     }
3081   }
3082
3083   if (playsink->videochain) {
3084     gst_event_ref (event);
3085     if ((res = gst_element_send_event (playsink->videochain->chain.bin, event))) {
3086       GST_DEBUG_OBJECT (playsink, "Sent event succesfully to video sink");
3087       goto done;
3088     }
3089     GST_DEBUG_OBJECT (playsink, "Event failed when sent to video sink");
3090   }
3091   if (playsink->audiochain) {
3092     gst_event_ref (event);
3093     if ((res = gst_element_send_event (playsink->audiochain->chain.bin, event))) {
3094       GST_DEBUG_OBJECT (playsink, "Sent event succesfully to audio sink");
3095       goto done;
3096     }
3097     GST_DEBUG_OBJECT (playsink, "Event failed when sent to audio sink");
3098   }
3099
3100 done:
3101   gst_event_unref (event);
3102   return res;
3103 }
3104
3105 /* We only want to send the event to a single sink (overriding GstBin's
3106  * behaviour), but we want to keep GstPipeline's behaviour - wrapping seek
3107  * events appropriately. So, this is a messy duplication of code. */
3108 static gboolean
3109 gst_play_sink_send_event (GstElement * element, GstEvent * event)
3110 {
3111   gboolean res = FALSE;
3112   GstEventType event_type = GST_EVENT_TYPE (event);
3113   GstPlaySink *playsink;
3114
3115   playsink = GST_PLAY_SINK_CAST (element);
3116
3117   switch (event_type) {
3118     case GST_EVENT_SEEK:
3119       GST_DEBUG_OBJECT (element, "Sending event to a sink");
3120       res = gst_play_sink_send_event_to_sink (playsink, event);
3121       break;
3122     case GST_EVENT_STEP:
3123     {
3124       GstFormat format;
3125       guint64 amount;
3126       gdouble rate;
3127       gboolean flush, intermediate;
3128
3129       gst_event_parse_step (event, &format, &amount, &rate, &flush,
3130           &intermediate);
3131
3132       if (format == GST_FORMAT_BUFFERS) {
3133         /* for buffers, we will try to step video frames, for other formats we
3134          * send the step to all sinks */
3135         res = gst_play_sink_send_event_to_sink (playsink, event);
3136       } else {
3137         res =
3138             GST_ELEMENT_CLASS (gst_play_sink_parent_class)->send_event (element,
3139             event);
3140       }
3141       break;
3142     }
3143     default:
3144       res =
3145           GST_ELEMENT_CLASS (gst_play_sink_parent_class)->send_event (element,
3146           event);
3147       break;
3148   }
3149   return res;
3150 }
3151
3152 static GstStateChangeReturn
3153 gst_play_sink_change_state (GstElement * element, GstStateChange transition)
3154 {
3155   GstStateChangeReturn ret;
3156   GstStateChangeReturn bret;
3157
3158   GstPlaySink *playsink;
3159
3160   playsink = GST_PLAY_SINK (element);
3161
3162   switch (transition) {
3163     case GST_STATE_CHANGE_READY_TO_PAUSED:
3164       playsink->need_async_start = TRUE;
3165       /* we want to go async to PAUSED until we managed to configure and add the
3166        * sinks */
3167       do_async_start (playsink);
3168       ret = GST_STATE_CHANGE_ASYNC;
3169       break;
3170     case GST_STATE_CHANGE_PAUSED_TO_READY:
3171     case GST_STATE_CHANGE_READY_TO_NULL:
3172       if (playsink->audiochain && playsink->audiochain->sink_volume) {
3173         /* remove our links to the mute and volume elements when they were
3174          * provided by a sink */
3175         disconnect_chain (playsink->audiochain, playsink);
3176         playsink->audiochain->volume = NULL;
3177         playsink->audiochain->mute = NULL;
3178         playsink->audiochain->ts_offset = NULL;
3179       }
3180       ret = GST_STATE_CHANGE_SUCCESS;
3181       break;
3182     default:
3183       /* all other state changes return SUCCESS by default, this value can be
3184        * overridden by the result of the children */
3185       ret = GST_STATE_CHANGE_SUCCESS;
3186       break;
3187   }
3188
3189   /* do the state change of the children */
3190   bret =
3191       GST_ELEMENT_CLASS (gst_play_sink_parent_class)->change_state (element,
3192       transition);
3193   /* now look at the result of our children and adjust the return value */
3194   switch (bret) {
3195     case GST_STATE_CHANGE_FAILURE:
3196       /* failure, we stop */
3197       goto activate_failed;
3198     case GST_STATE_CHANGE_NO_PREROLL:
3199       /* some child returned NO_PREROLL. This is strange but we never know. We
3200        * commit our async state change (if any) and return the NO_PREROLL */
3201       do_async_done (playsink);
3202       ret = bret;
3203       break;
3204     case GST_STATE_CHANGE_ASYNC:
3205       /* some child was async, return this */
3206       ret = bret;
3207       break;
3208     default:
3209       /* return our previously configured return value */
3210       break;
3211   }
3212
3213   switch (transition) {
3214     case GST_STATE_CHANGE_READY_TO_PAUSED:
3215       break;
3216     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3217       /* FIXME Release audio device when we implement that */
3218       playsink->need_async_start = TRUE;
3219       break;
3220     case GST_STATE_CHANGE_PAUSED_TO_READY:{
3221       if (playsink->video_sinkpad_stream_synchronizer) {
3222         gst_element_release_request_pad (GST_ELEMENT_CAST
3223             (playsink->stream_synchronizer),
3224             playsink->video_sinkpad_stream_synchronizer);
3225         gst_object_unref (playsink->video_sinkpad_stream_synchronizer);
3226         playsink->video_sinkpad_stream_synchronizer = NULL;
3227         gst_object_unref (playsink->video_srcpad_stream_synchronizer);
3228         playsink->video_srcpad_stream_synchronizer = NULL;
3229       }
3230       if (playsink->audio_sinkpad_stream_synchronizer) {
3231         gst_element_release_request_pad (GST_ELEMENT_CAST
3232             (playsink->stream_synchronizer),
3233             playsink->audio_sinkpad_stream_synchronizer);
3234         gst_object_unref (playsink->audio_sinkpad_stream_synchronizer);
3235         playsink->audio_sinkpad_stream_synchronizer = NULL;
3236         gst_object_unref (playsink->audio_srcpad_stream_synchronizer);
3237         playsink->audio_srcpad_stream_synchronizer = NULL;
3238       }
3239       if (playsink->text_sinkpad_stream_synchronizer) {
3240         gst_element_release_request_pad (GST_ELEMENT_CAST
3241             (playsink->stream_synchronizer),
3242             playsink->text_sinkpad_stream_synchronizer);
3243         gst_object_unref (playsink->text_sinkpad_stream_synchronizer);
3244         playsink->text_sinkpad_stream_synchronizer = NULL;
3245         gst_object_unref (playsink->text_srcpad_stream_synchronizer);
3246         playsink->text_srcpad_stream_synchronizer = NULL;
3247       }
3248     }
3249       /* fall through */
3250     case GST_STATE_CHANGE_READY_TO_NULL:
3251       /* remove sinks we added */
3252       if (playsink->videodeinterlacechain) {
3253         activate_chain (GST_PLAY_CHAIN (playsink->videodeinterlacechain),
3254             FALSE);
3255         add_chain (GST_PLAY_CHAIN (playsink->videodeinterlacechain), FALSE);
3256       }
3257       if (playsink->videochain) {
3258         activate_chain (GST_PLAY_CHAIN (playsink->videochain), FALSE);
3259         add_chain (GST_PLAY_CHAIN (playsink->videochain), FALSE);
3260       }
3261       if (playsink->audiochain) {
3262         activate_chain (GST_PLAY_CHAIN (playsink->audiochain), FALSE);
3263         add_chain (GST_PLAY_CHAIN (playsink->audiochain), FALSE);
3264       }
3265       if (playsink->vischain) {
3266         activate_chain (GST_PLAY_CHAIN (playsink->vischain), FALSE);
3267         add_chain (GST_PLAY_CHAIN (playsink->vischain), FALSE);
3268       }
3269       if (playsink->textchain) {
3270         activate_chain (GST_PLAY_CHAIN (playsink->textchain), FALSE);
3271         add_chain (GST_PLAY_CHAIN (playsink->textchain), FALSE);
3272       }
3273       do_async_done (playsink);
3274       /* when going to READY, keep elements around as long as possible,
3275        * so they may be re-used faster next time/url around.
3276        * when really going to NULL, clean up everything completely. */
3277       if (transition == GST_STATE_CHANGE_READY_TO_NULL) {
3278         free_chain ((GstPlayChain *) playsink->videodeinterlacechain);
3279         playsink->videodeinterlacechain = NULL;
3280         free_chain ((GstPlayChain *) playsink->videochain);
3281         playsink->videochain = NULL;
3282         free_chain ((GstPlayChain *) playsink->audiochain);
3283         playsink->audiochain = NULL;
3284         free_chain ((GstPlayChain *) playsink->vischain);
3285         playsink->vischain = NULL;
3286         free_chain ((GstPlayChain *) playsink->textchain);
3287         playsink->textchain = NULL;
3288       }
3289       break;
3290     default:
3291       break;
3292   }
3293   return ret;
3294
3295   /* ERRORS */
3296 activate_failed:
3297   {
3298     GST_DEBUG_OBJECT (element,
3299         "element failed to change states -- activation problem?");
3300     return GST_STATE_CHANGE_FAILURE;
3301   }
3302 }
3303
3304 static void
3305 gst_play_sink_set_property (GObject * object, guint prop_id,
3306     const GValue * value, GParamSpec * spec)
3307 {
3308   GstPlaySink *playsink = GST_PLAY_SINK (object);
3309
3310   switch (prop_id) {
3311     case PROP_FLAGS:
3312       gst_play_sink_set_flags (playsink, g_value_get_flags (value));
3313       break;
3314     case PROP_VOLUME:
3315       gst_play_sink_set_volume (playsink, g_value_get_double (value));
3316       break;
3317     case PROP_MUTE:
3318       gst_play_sink_set_mute (playsink, g_value_get_boolean (value));
3319       break;
3320     case PROP_FONT_DESC:
3321       gst_play_sink_set_font_desc (playsink, g_value_get_string (value));
3322       break;
3323     case PROP_SUBTITLE_ENCODING:
3324       gst_play_sink_set_subtitle_encoding (playsink,
3325           g_value_get_string (value));
3326       break;
3327     case PROP_VIS_PLUGIN:
3328       gst_play_sink_set_vis_plugin (playsink, g_value_get_object (value));
3329       break;
3330     case PROP_AV_OFFSET:
3331       gst_play_sink_set_av_offset (playsink, g_value_get_int64 (value));
3332       break;
3333     default:
3334       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
3335       break;
3336   }
3337 }
3338
3339 static void
3340 gst_play_sink_get_property (GObject * object, guint prop_id,
3341     GValue * value, GParamSpec * spec)
3342 {
3343   GstPlaySink *playsink = GST_PLAY_SINK (object);
3344
3345   switch (prop_id) {
3346     case PROP_FLAGS:
3347       g_value_set_flags (value, gst_play_sink_get_flags (playsink));
3348       break;
3349     case PROP_VOLUME:
3350       g_value_set_double (value, gst_play_sink_get_volume (playsink));
3351       break;
3352     case PROP_MUTE:
3353       g_value_set_boolean (value, gst_play_sink_get_mute (playsink));
3354       break;
3355     case PROP_FONT_DESC:
3356       g_value_take_string (value, gst_play_sink_get_font_desc (playsink));
3357       break;
3358     case PROP_SUBTITLE_ENCODING:
3359       g_value_take_string (value,
3360           gst_play_sink_get_subtitle_encoding (playsink));
3361       break;
3362     case PROP_VIS_PLUGIN:
3363       g_value_take_object (value, gst_play_sink_get_vis_plugin (playsink));
3364       break;
3365     case PROP_FRAME:
3366       gst_value_take_buffer (value, gst_play_sink_get_last_frame (playsink));
3367       break;
3368     case PROP_AV_OFFSET:
3369       g_value_set_int64 (value, gst_play_sink_get_av_offset (playsink));
3370       break;
3371     default:
3372       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
3373       break;
3374   }
3375 }
3376
3377
3378 gboolean
3379 gst_play_sink_plugin_init (GstPlugin * plugin)
3380 {
3381   GST_DEBUG_CATEGORY_INIT (gst_play_sink_debug, "playsink", 0, "play bin");
3382
3383   return gst_element_register (plugin, "playsink", GST_RANK_NONE,
3384       GST_TYPE_PLAY_SINK);
3385 }