gstplaysink.c 87.9 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* GStreamer
 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <gst/gst.h>

#include <gst/gst-i18n-plugin.h>
#include <gst/pbutils/pbutils.h>

#include "gstplaysink.h"

GST_DEBUG_CATEGORY_STATIC (gst_play_sink_debug);
#define GST_CAT_DEFAULT gst_play_sink_debug

#define VOLUME_MAX_DOUBLE 10.0

37
38
39
#define DEFAULT_FLAGS             GST_PLAY_FLAG_AUDIO | GST_PLAY_FLAG_VIDEO | GST_PLAY_FLAG_TEXT | \
                                  GST_PLAY_FLAG_SOFT_VOLUME

40
#define GST_PLAY_CHAIN(c) ((GstPlayChain *)(c))
41

Wim Taymans's avatar
Wim Taymans committed
42
43
44
45
46
47
48
49
/* holds the common data fields for the audio and video pipelines. We keep them
 * in a structure to more easily have all the info available. */
typedef struct
{
  GstPlaySink *playsink;
  GstElement *bin;
  gboolean added;
  gboolean activated;
50
  gboolean raw;
Wim Taymans's avatar
Wim Taymans committed
51
52
53
54
55
} GstPlayChain;

typedef struct
{
  GstPlayChain chain;
56
  GstPad *sinkpad;
57
  GstElement *queue;
Wim Taymans's avatar
Wim Taymans committed
58
59
  GstElement *conv;
  GstElement *resample;
60
  GstElement *volume;           /* element with the volume property */
61
  gboolean sink_volume;         /* if the volume was provided by the sink */
62
  GstElement *mute;             /* element with the mute property */
Wim Taymans's avatar
Wim Taymans committed
63
64
65
66
67
68
  GstElement *sink;
} GstPlayAudioChain;

typedef struct
{
  GstPlayChain chain;
69
  GstPad *sinkpad;
Wim Taymans's avatar
Wim Taymans committed
70
  GstElement *queue;
71
  GstElement *conv;
Wim Taymans's avatar
Wim Taymans committed
72
73
  GstElement *scale;
  GstElement *sink;
74
  gboolean async;
Wim Taymans's avatar
Wim Taymans committed
75
76
} GstPlayVideoChain;

77
78
79
typedef struct
{
  GstPlayChain chain;
80
  GstPad *sinkpad;
81
82
83
  GstElement *queue;
  GstElement *conv;
  GstElement *resample;
84
85
  GstPad *blockpad;             /* srcpad of resample, used for switching the vis */
  GstPad *vissinkpad;           /* visualisation sinkpad, */
86
  GstElement *vis;
87
88
89
  GstPad *vissrcpad;            /* visualisation srcpad, */
  GstPad *srcpad;               /* outgoing srcpad, used to connect to the next
                                 * chain */
90
91
} GstPlayVisChain;

92
93
94
95
typedef struct
{
  GstPlayChain chain;
  GstPad *sinkpad;
96
97
  GstElement *queue;
  GstElement *identity;
98
99
100
101
102
  GstElement *overlay;
  GstPad *videosinkpad;
  GstPad *textsinkpad;
  GstPad *srcpad;               /* outgoing srcpad, used to connect to the next
                                 * chain */
103
  GstElement *sink;             /* custom sink to receive subtitle buffers */
104
105
} GstPlayTextChain;

106
107
108
#define GST_PLAY_SINK_GET_LOCK(playsink) (&((GstPlaySink *)playsink)->lock)
#define GST_PLAY_SINK_LOCK(playsink)     g_static_rec_mutex_lock (GST_PLAY_SINK_GET_LOCK (playsink))
#define GST_PLAY_SINK_UNLOCK(playsink)   g_static_rec_mutex_unlock (GST_PLAY_SINK_GET_LOCK (playsink))
109

Wim Taymans's avatar
Wim Taymans committed
110
111
112
113
struct _GstPlaySink
{
  GstBin bin;

114
  GStaticRecMutex lock;
115

116
  gboolean async_pending;
Jan Schmidt's avatar
Jan Schmidt committed
117
  gboolean need_async_start;
118

119
  GstPlayFlags flags;
Wim Taymans's avatar
Wim Taymans committed
120

121
  /* chains */
122
123
124
125
  GstPlayAudioChain *audiochain;
  GstPlayVideoChain *videochain;
  GstPlayVisChain *vischain;
  GstPlayTextChain *textchain;
Wim Taymans's avatar
Wim Taymans committed
126

127
  /* audio */
Wim Taymans's avatar
Wim Taymans committed
128
  GstPad *audio_pad;
129
  gboolean audio_pad_raw;
130
  /* audio tee */
131
132
133
134
  GstElement *audio_tee;
  GstPad *audio_tee_sink;
  GstPad *audio_tee_asrc;
  GstPad *audio_tee_vissrc;
135
  /* video */
Wim Taymans's avatar
Wim Taymans committed
136
  GstPad *video_pad;
137
  gboolean video_pad_raw;
138
  /* text */
Wim Taymans's avatar
Wim Taymans committed
139
  GstPad *text_pad;
140

Wim Taymans's avatar
Wim Taymans committed
141
142
143
144
  /* properties */
  GstElement *audio_sink;
  GstElement *video_sink;
  GstElement *visualisation;
145
  GstElement *text_sink;
146
  gdouble volume;
147
  gboolean mute;
Wim Taymans's avatar
Wim Taymans committed
148
  gchar *font_desc;             /* font description */
149
  gchar *subtitle_encoding;     /* subtitle encoding */
Wim Taymans's avatar
Wim Taymans committed
150
  guint connection_speed;       /* connection speed in bits/sec (0 = unknown) */
Wim Taymans's avatar
Wim Taymans committed
151
  gint count;
152
  gboolean volume_changed;      /* volume/mute changed while no audiochain */
153
  gboolean mute_changed;        /* ... has been created yet */
Wim Taymans's avatar
Wim Taymans committed
154
155
156
157
158
};

struct _GstPlaySinkClass
{
  GstBinClass parent_class;
159
160

    gboolean (*reconfigure) (GstPlaySink * playsink);
Wim Taymans's avatar
Wim Taymans committed
161
162
};

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
static GstStaticPadTemplate audiorawtemplate =
GST_STATIC_PAD_TEMPLATE ("audio_raw_sink",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS_ANY);
static GstStaticPadTemplate audiotemplate =
GST_STATIC_PAD_TEMPLATE ("audio_sink",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS_ANY);
static GstStaticPadTemplate videorawtemplate =
GST_STATIC_PAD_TEMPLATE ("video_raw_sink",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS_ANY);
static GstStaticPadTemplate videotemplate =
GST_STATIC_PAD_TEMPLATE ("video_sink",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS_ANY);
static GstStaticPadTemplate texttemplate = GST_STATIC_PAD_TEMPLATE ("text_sink",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS_ANY);
Wim Taymans's avatar
Wim Taymans committed
187
188
189
190
191

/* props */
enum
{
  PROP_0,
192
  PROP_FLAGS,
193
194
195
  PROP_MUTE,
  PROP_VOLUME,
  PROP_FONT_DESC,
196
  PROP_SUBTITLE_ENCODING,
197
  PROP_VIS_PLUGIN,
Wim Taymans's avatar
Wim Taymans committed
198
199
200
201
202
203
204
205
206
207
  PROP_LAST
};

/* signals */
enum
{
  LAST_SIGNAL
};

static void gst_play_sink_dispose (GObject * object);
208
static void gst_play_sink_finalize (GObject * object);
209
210
211
212
static void gst_play_sink_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * spec);
static void gst_play_sink_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * spec);
Wim Taymans's avatar
Wim Taymans committed
213

214
215
216
217
static GstPad *gst_play_sink_request_new_pad (GstElement * element,
    GstPadTemplate * templ, const gchar * name);
static void gst_play_sink_release_request_pad (GstElement * element,
    GstPad * pad);
Wim Taymans's avatar
Wim Taymans committed
218
219
220
221
222
static gboolean gst_play_sink_send_event (GstElement * element,
    GstEvent * event);
static GstStateChangeReturn gst_play_sink_change_state (GstElement * element,
    GstStateChange transition);

223
224
static void gst_play_sink_handle_message (GstBin * bin, GstMessage * message);

Wim Taymans's avatar
Wim Taymans committed
225
226
/* static guint gst_play_sink_signals[LAST_SIGNAL] = { 0 }; */

227
G_DEFINE_TYPE (GstPlaySink, gst_play_sink, GST_TYPE_BIN);
Wim Taymans's avatar
Wim Taymans committed
228
229
230
231
232
233
234
235
236
237
238
239

static void
gst_play_sink_class_init (GstPlaySinkClass * klass)
{
  GObjectClass *gobject_klass;
  GstElementClass *gstelement_klass;
  GstBinClass *gstbin_klass;

  gobject_klass = (GObjectClass *) klass;
  gstelement_klass = (GstElementClass *) klass;
  gstbin_klass = (GstBinClass *) klass;

240
241
242
243
  gobject_klass->dispose = gst_play_sink_dispose;
  gobject_klass->finalize = gst_play_sink_finalize;
  gobject_klass->set_property = gst_play_sink_set_property;
  gobject_klass->get_property = gst_play_sink_get_property;
244
245
246
247
248
249
250
251
252
253
254


  /**
   * GstPlaySink:flags
   *
   * Control the behaviour of playsink.
   */
  g_object_class_install_property (gobject_klass, PROP_FLAGS,
      g_param_spec_flags ("flags", "Flags", "Flags to control behaviour",
          GST_TYPE_PLAY_FLAGS, DEFAULT_FLAGS,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
255

256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
  /**
   * GstPlaySink:volume:
   *
   * Get or set the current audio stream volume. 1.0 means 100%,
   * 0.0 means mute. This uses a linear volume scale.
   *
   */
  g_object_class_install_property (gobject_klass, PROP_VOLUME,
      g_param_spec_double ("volume", "Volume", "The audio volume, 1.0=100%",
          0.0, VOLUME_MAX_DOUBLE, 1.0,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_klass, PROP_MUTE,
      g_param_spec_boolean ("mute", "Mute",
          "Mute the audio channel without changing the volume", FALSE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_klass, PROP_FONT_DESC,
      g_param_spec_string ("subtitle-font-desc",
          "Subtitle font description",
          "Pango font description of font "
          "to be used for subtitle rendering", NULL,
          G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
277
278
279
280
281
282
283
  g_object_class_install_property (gobject_klass, PROP_SUBTITLE_ENCODING,
      g_param_spec_string ("subtitle-encoding", "subtitle encoding",
          "Encoding to assume if input subtitles are not in UTF-8 encoding. "
          "If not set, the GST_SUBTITLE_ENCODING environment variable will "
          "be checked for an encoding to use. If that is not set either, "
          "ISO-8859-15 will be assumed.", NULL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
284
285
286
287
288
  g_object_class_install_property (gobject_klass, PROP_VIS_PLUGIN,
      g_param_spec_object ("vis-plugin", "Vis plugin",
          "the visualization element to use (NULL = default)",
          GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

289
290
291
292
293
  g_signal_new ("reconfigure", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstPlaySinkClass,
          reconfigure), NULL, NULL, gst_marshal_BOOLEAN__VOID, G_TYPE_BOOLEAN,
      0, G_TYPE_NONE);

294
295
296
297
298
299
300
301
302
303
  gst_element_class_add_pad_template (gstelement_klass,
      gst_static_pad_template_get (&audiorawtemplate));
  gst_element_class_add_pad_template (gstelement_klass,
      gst_static_pad_template_get (&audiotemplate));
  gst_element_class_add_pad_template (gstelement_klass,
      gst_static_pad_template_get (&videorawtemplate));
  gst_element_class_add_pad_template (gstelement_klass,
      gst_static_pad_template_get (&videotemplate));
  gst_element_class_add_pad_template (gstelement_klass,
      gst_static_pad_template_get (&texttemplate));
304
305
306
307
  gst_element_class_set_details_simple (gstelement_klass, "Player Sink",
      "Generic/Bin/Sink",
      "Convenience sink for multiple streams",
      "Wim Taymans <wim.taymans@gmail.com>");
Wim Taymans's avatar
Wim Taymans committed
308
309
310
311

  gstelement_klass->change_state =
      GST_DEBUG_FUNCPTR (gst_play_sink_change_state);
  gstelement_klass->send_event = GST_DEBUG_FUNCPTR (gst_play_sink_send_event);
312
313
314
315
  gstelement_klass->request_new_pad =
      GST_DEBUG_FUNCPTR (gst_play_sink_request_new_pad);
  gstelement_klass->release_pad =
      GST_DEBUG_FUNCPTR (gst_play_sink_release_request_pad);
Wim Taymans's avatar
Wim Taymans committed
316

317
318
  gstbin_klass->handle_message =
      GST_DEBUG_FUNCPTR (gst_play_sink_handle_message);
319
320

  klass->reconfigure = GST_DEBUG_FUNCPTR (gst_play_sink_reconfigure);
Wim Taymans's avatar
Wim Taymans committed
321
322
323
}

static void
324
gst_play_sink_init (GstPlaySink * playsink)
Wim Taymans's avatar
Wim Taymans committed
325
326
{
  /* init groups */
327
328
329
  playsink->video_sink = NULL;
  playsink->audio_sink = NULL;
  playsink->visualisation = NULL;
330
  playsink->text_sink = NULL;
331
332
  playsink->volume = 1.0;
  playsink->font_desc = NULL;
333
  playsink->subtitle_encoding = NULL;
334
  playsink->flags = DEFAULT_FLAGS;
335

336
  g_static_rec_mutex_init (&playsink->lock);
337
  GST_OBJECT_FLAG_SET (playsink, GST_ELEMENT_IS_SINK);
Wim Taymans's avatar
Wim Taymans committed
338
339
}

340
341
342
343
344
345
346
347
348
349
static void
free_chain (GstPlayChain * chain)
{
  if (chain) {
    if (chain->bin)
      gst_object_unref (chain->bin);
    g_free (chain);
  }
}

Wim Taymans's avatar
Wim Taymans committed
350
351
352
static void
gst_play_sink_dispose (GObject * object)
{
353
  GstPlaySink *playsink;
Wim Taymans's avatar
Wim Taymans committed
354

355
  playsink = GST_PLAY_SINK (object);
Wim Taymans's avatar
Wim Taymans committed
356

357
358
359
360
  if (playsink->audio_sink != NULL) {
    gst_element_set_state (playsink->audio_sink, GST_STATE_NULL);
    gst_object_unref (playsink->audio_sink);
    playsink->audio_sink = NULL;
Wim Taymans's avatar
Wim Taymans committed
361
  }
362
363
364
365
  if (playsink->video_sink != NULL) {
    gst_element_set_state (playsink->video_sink, GST_STATE_NULL);
    gst_object_unref (playsink->video_sink);
    playsink->video_sink = NULL;
Wim Taymans's avatar
Wim Taymans committed
366
  }
367
368
369
370
  if (playsink->visualisation != NULL) {
    gst_element_set_state (playsink->visualisation, GST_STATE_NULL);
    gst_object_unref (playsink->visualisation);
    playsink->visualisation = NULL;
Wim Taymans's avatar
Wim Taymans committed
371
  }
372
373
374
375
376
  if (playsink->text_sink != NULL) {
    gst_element_set_state (playsink->text_sink, GST_STATE_NULL);
    gst_object_unref (playsink->text_sink);
    playsink->text_sink = NULL;
  }
377
378
379
380
381
382
383
384
385
386

  free_chain ((GstPlayChain *) playsink->videochain);
  playsink->videochain = NULL;
  free_chain ((GstPlayChain *) playsink->audiochain);
  playsink->audiochain = NULL;
  free_chain ((GstPlayChain *) playsink->vischain);
  playsink->vischain = NULL;
  free_chain ((GstPlayChain *) playsink->textchain);
  playsink->textchain = NULL;

387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
  if (playsink->audio_tee_sink) {
    gst_object_unref (playsink->audio_tee_sink);
    playsink->audio_tee_sink = NULL;
  }

  if (playsink->audio_tee_vissrc) {
    gst_element_release_request_pad (playsink->audio_tee,
        playsink->audio_tee_vissrc);
    gst_object_unref (playsink->audio_tee_vissrc);
    playsink->audio_tee_vissrc = NULL;
  }

  if (playsink->audio_tee_asrc) {
    gst_element_release_request_pad (playsink->audio_tee,
        playsink->audio_tee_asrc);
    gst_object_unref (playsink->audio_tee_asrc);
    playsink->audio_tee_asrc = NULL;
  }

406
407
  g_free (playsink->font_desc);
  playsink->font_desc = NULL;
Wim Taymans's avatar
Wim Taymans committed
408

409
410
411
  g_free (playsink->subtitle_encoding);
  playsink->subtitle_encoding = NULL;

412
  G_OBJECT_CLASS (gst_play_sink_parent_class)->dispose (object);
Wim Taymans's avatar
Wim Taymans committed
413
414
}

415
416
417
418
419
420
421
static void
gst_play_sink_finalize (GObject * object)
{
  GstPlaySink *playsink;

  playsink = GST_PLAY_SINK (object);

422
  g_static_rec_mutex_free (&playsink->lock);
423

424
  G_OBJECT_CLASS (gst_play_sink_parent_class)->finalize (object);
425
426
}

427
void
428
429
gst_play_sink_set_sink (GstPlaySink * playsink, GstPlaySinkType type,
    GstElement * sink)
Wim Taymans's avatar
Wim Taymans committed
430
{
431
  GstElement **elem = NULL, *old = NULL;
432

433
434
  GST_LOG ("Setting sink %" GST_PTR_FORMAT " as sink type %d", sink, type);

435
  GST_PLAY_SINK_LOCK (playsink);
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
  switch (type) {
    case GST_PLAY_SINK_TYPE_AUDIO:
    case GST_PLAY_SINK_TYPE_AUDIO_RAW:
      elem = &playsink->audio_sink;
      break;
    case GST_PLAY_SINK_TYPE_VIDEO:
    case GST_PLAY_SINK_TYPE_VIDEO_RAW:
      elem = &playsink->video_sink;
      break;
    case GST_PLAY_SINK_TYPE_TEXT:
      elem = &playsink->text_sink;
      break;
    default:
      break;
  }
  if (elem) {
    old = *elem;
453
    if (sink)
454
455
      gst_object_ref (sink);
    *elem = sink;
456
457
458
  }
  GST_PLAY_SINK_UNLOCK (playsink);

459
460
461
  if (old) {
    if (old != sink)
      gst_element_set_state (old, GST_STATE_NULL);
462
    gst_object_unref (old);
463
  }
464
465
466
}

GstElement *
467
gst_play_sink_get_sink (GstPlaySink * playsink, GstPlaySinkType type)
468
469
{
  GstElement *result = NULL;
470
  GstElement *elem = NULL, *chainp = NULL;
471
472

  GST_PLAY_SINK_LOCK (playsink);
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
  switch (type) {
    case GST_PLAY_SINK_TYPE_AUDIO:
    {
      GstPlayAudioChain *chain;
      if ((chain = (GstPlayAudioChain *) playsink->audiochain))
        chainp = chain->sink;
      elem = playsink->audio_sink;
      break;
    }
    case GST_PLAY_SINK_TYPE_VIDEO:
    {
      GstPlayVideoChain *chain;
      if ((chain = (GstPlayVideoChain *) playsink->videochain))
        chainp = chain->sink;
      elem = playsink->video_sink;
      break;
    }
    case GST_PLAY_SINK_TYPE_TEXT:
    {
      GstPlayTextChain *chain;
      if ((chain = (GstPlayTextChain *) playsink->textchain))
        chainp = chain->sink;
      elem = playsink->text_sink;
      break;
    }
    default:
      break;
  }
  if (chainp) {
    /* we have an active chain with a sink, get the sink */
    result = gst_object_ref (chainp);
504
505
  }
  /* nothing found, return last configured sink */
506
507
  if (result == NULL && elem)
    result = gst_object_ref (elem);
508
509
510
  GST_PLAY_SINK_UNLOCK (playsink);

  return result;
Wim Taymans's avatar
Wim Taymans committed
511
512
}

513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
static void
gst_play_sink_vis_unblocked (GstPad * tee_pad, gboolean blocked,
    gpointer user_data)
{
  GstPlaySink *playsink;

  playsink = GST_PLAY_SINK (user_data);
  /* nothing to do here, we need a dummy callback here to make the async call
   * non-blocking. */
  GST_DEBUG_OBJECT (playsink, "vis pad unblocked");
}

static void
gst_play_sink_vis_blocked (GstPad * tee_pad, gboolean blocked,
    gpointer user_data)
{
  GstPlaySink *playsink;
  GstPlayVisChain *chain;

  playsink = GST_PLAY_SINK (user_data);

  GST_PLAY_SINK_LOCK (playsink);
  GST_DEBUG_OBJECT (playsink, "vis pad blocked");
  /* now try to change the plugin in the running vis chain */
  if (!(chain = (GstPlayVisChain *) playsink->vischain))
    goto done;

  /* unlink the old plugin and unghost the pad */
  gst_pad_unlink (chain->blockpad, chain->vissinkpad);
  gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (chain->srcpad), NULL);

  /* set the old plugin to NULL and remove */
  gst_element_set_state (chain->vis, GST_STATE_NULL);
  gst_bin_remove (GST_BIN_CAST (chain->chain.bin), chain->vis);

  /* add new plugin and set state to playing */
549
  chain->vis = playsink->visualisation;
550
551
552
553
  gst_bin_add (GST_BIN_CAST (chain->chain.bin), chain->vis);
  gst_element_set_state (chain->vis, GST_STATE_PLAYING);

  /* get pads */
554
555
  chain->vissinkpad = gst_element_get_static_pad (chain->vis, "sink");
  chain->vissrcpad = gst_element_get_static_pad (chain->vis, "src");
556
557
558
559
560
561
562
563
564
565
566
567
568

  /* link pads */
  gst_pad_link (chain->blockpad, chain->vissinkpad);
  gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (chain->srcpad),
      chain->vissrcpad);

done:
  /* Unblock the pad */
  gst_pad_set_blocked_async (tee_pad, FALSE, gst_play_sink_vis_unblocked,
      playsink);
  GST_PLAY_SINK_UNLOCK (playsink);
}

569
void
570
gst_play_sink_set_vis_plugin (GstPlaySink * playsink, GstElement * vis)
Wim Taymans's avatar
Wim Taymans committed
571
{
572
  GstPlayVisChain *chain;
Wim Taymans's avatar
Wim Taymans committed
573

574
575
576
577
578
579
580
581
  /* setting NULL means creating the default vis plugin */
  if (vis == NULL)
    vis = gst_element_factory_make ("goom", "vis");

  /* simply return if we don't have a vis plugin here */
  if (vis == NULL)
    return;

582
583
584
585
  GST_PLAY_SINK_LOCK (playsink);
  /* first store the new vis */
  if (playsink->visualisation)
    gst_object_unref (playsink->visualisation);
586
  /* take ownership */
587
  gst_object_ref_sink (vis);
588
  playsink->visualisation = vis;
589
590
591
592
593
594
595
596
597

  /* now try to change the plugin in the running vis chain, if we have no chain,
   * we don't bother, any future vis chain will be created with the new vis
   * plugin. */
  if (!(chain = (GstPlayVisChain *) playsink->vischain))
    goto done;

  /* block the pad, the next time the callback is called we can change the
   * visualisation. It's possible that this never happens or that the pad was
598
599
600
601
   * already blocked. If the callback never happens, we don't have new data so
   * we don't need the new vis plugin. If the pad was already blocked, the
   * function returns FALSE but the previous pad block will do the right thing
   * anyway. */
602
603
604
605
606
607
608
  GST_DEBUG_OBJECT (playsink, "blocking vis pad");
  gst_pad_set_blocked_async (chain->blockpad, TRUE, gst_play_sink_vis_blocked,
      playsink);
done:
  GST_PLAY_SINK_UNLOCK (playsink);

  return;
Wim Taymans's avatar
Wim Taymans committed
609
610
}

611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
GstElement *
gst_play_sink_get_vis_plugin (GstPlaySink * playsink)
{
  GstElement *result = NULL;
  GstPlayVisChain *chain;

  GST_PLAY_SINK_LOCK (playsink);
  if ((chain = (GstPlayVisChain *) playsink->vischain)) {
    /* we have an active chain, get the sink */
    if (chain->vis)
      result = gst_object_ref (chain->vis);
  }
  /* nothing found, return last configured sink */
  if (result == NULL && playsink->visualisation)
    result = gst_object_ref (playsink->visualisation);
  GST_PLAY_SINK_UNLOCK (playsink);

  return result;
}

631
632
633
634
635
636
637
638
639
void
gst_play_sink_set_volume (GstPlaySink * playsink, gdouble volume)
{
  GstPlayAudioChain *chain;

  GST_PLAY_SINK_LOCK (playsink);
  playsink->volume = volume;
  chain = (GstPlayAudioChain *) playsink->audiochain;
  if (chain && chain->volume) {
640
641
642
    GST_LOG_OBJECT (playsink, "elements: volume=%" GST_PTR_FORMAT ", mute=%"
        GST_PTR_FORMAT "; new volume=%.03f, mute=%d", chain->volume,
        chain->mute, volume, playsink->mute);
643
644
645
    /* if there is a mute element or we are not muted, set the volume */
    if (chain->mute || !playsink->mute)
      g_object_set (chain->volume, "volume", volume, NULL);
646
  } else {
647
    GST_LOG_OBJECT (playsink, "no volume element");
648
    playsink->volume_changed = TRUE;
649
650
651
652
653
654
655
656
657
658
659
660
  }
  GST_PLAY_SINK_UNLOCK (playsink);
}

gdouble
gst_play_sink_get_volume (GstPlaySink * playsink)
{
  gdouble result;
  GstPlayAudioChain *chain;

  GST_PLAY_SINK_LOCK (playsink);
  chain = (GstPlayAudioChain *) playsink->audiochain;
661
  result = playsink->volume;
662
  if (chain && chain->volume) {
663
664
665
666
    if (chain->mute || !playsink->mute) {
      g_object_get (chain->volume, "volume", &result, NULL);
      playsink->volume = result;
    }
667
668
669
670
671
672
  }
  GST_PLAY_SINK_UNLOCK (playsink);

  return result;
}

673
674
675
676
677
678
679
680
void
gst_play_sink_set_mute (GstPlaySink * playsink, gboolean mute)
{
  GstPlayAudioChain *chain;

  GST_PLAY_SINK_LOCK (playsink);
  playsink->mute = mute;
  chain = (GstPlayAudioChain *) playsink->audiochain;
681
682
683
684
685
686
687
688
689
690
  if (chain) {
    if (chain->mute) {
      g_object_set (chain->mute, "mute", mute, NULL);
    } else if (chain->volume) {
      if (mute)
        g_object_set (chain->volume, "volume", (gdouble) 0.0, NULL);
      else
        g_object_set (chain->volume, "volume", (gdouble) playsink->volume,
            NULL);
    }
691
692
  } else {
    playsink->mute_changed = TRUE;
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
  }
  GST_PLAY_SINK_UNLOCK (playsink);
}

gboolean
gst_play_sink_get_mute (GstPlaySink * playsink)
{
  gboolean result;
  GstPlayAudioChain *chain;

  GST_PLAY_SINK_LOCK (playsink);
  chain = (GstPlayAudioChain *) playsink->audiochain;
  if (chain && chain->mute) {
    g_object_get (chain->mute, "mute", &result, NULL);
    playsink->mute = result;
  } else {
    result = playsink->mute;
  }
  GST_PLAY_SINK_UNLOCK (playsink);

  return result;
}

Wim Taymans's avatar
Wim Taymans committed
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
static void
post_missing_element_message (GstPlaySink * playsink, const gchar * name)
{
  GstMessage *msg;

  msg = gst_missing_element_message_new (GST_ELEMENT_CAST (playsink), name);
  gst_element_post_message (GST_ELEMENT_CAST (playsink), msg);
}

static gboolean
add_chain (GstPlayChain * chain, gboolean add)
{
  if (chain->added == add)
    return TRUE;

  if (add)
    gst_bin_add (GST_BIN_CAST (chain->playsink), chain->bin);
733
  else {
Wim Taymans's avatar
Wim Taymans committed
734
    gst_bin_remove (GST_BIN_CAST (chain->playsink), chain->bin);
735
736
737
    /* we don't want to lose our sink status */
    GST_OBJECT_FLAG_SET (chain->playsink, GST_ELEMENT_IS_SINK);
  }
Wim Taymans's avatar
Wim Taymans committed
738
739
740
741
742
743
744
745
746

  chain->added = add;

  return TRUE;
}

static gboolean
activate_chain (GstPlayChain * chain, gboolean activate)
{
747
748
  GstState state;

Wim Taymans's avatar
Wim Taymans committed
749
750
751
  if (chain->activated == activate)
    return TRUE;

752
753
754
755
  GST_OBJECT_LOCK (chain->playsink);
  state = GST_STATE_TARGET (chain->playsink);
  GST_OBJECT_UNLOCK (chain->playsink);

Wim Taymans's avatar
Wim Taymans committed
756
  if (activate)
757
    gst_element_set_state (chain->bin, state);
Wim Taymans's avatar
Wim Taymans committed
758
759
760
761
762
763
764
765
  else
    gst_element_set_state (chain->bin, GST_STATE_NULL);

  chain->activated = activate;

  return TRUE;
}

766
767
static gboolean
element_is_sink (GstElement * element)
768
769
770
771
772
773
774
{
  gboolean is_sink;

  GST_OBJECT_LOCK (element);
  is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_IS_SINK);
  GST_OBJECT_UNLOCK (element);

775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
  GST_DEBUG_OBJECT (element, "is a sink: %s", (is_sink) ? "yes" : "no");
  return is_sink;
}

static gboolean
element_has_property (GstElement * element, const gchar * pname, GType type)
{
  GParamSpec *pspec;

  pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), pname);

  if (pspec == NULL) {
    GST_DEBUG_OBJECT (element, "no %s property", pname);
    return FALSE;
  }

  if (type == G_TYPE_INVALID || type == pspec->value_type ||
      g_type_is_a (pspec->value_type, type)) {
    GST_DEBUG_OBJECT (element, "has %s property of type %s", pname,
        (type == G_TYPE_INVALID) ? "any type" : g_type_name (type));
    return TRUE;
  }

  GST_WARNING_OBJECT (element, "has %s property, but property is of type %s "
      "and we expected it to be of type %s", pname,
      g_type_name (pspec->value_type), g_type_name (type));

  return FALSE;
}

typedef struct
{
  const gchar *prop_name;
  GType prop_type;
809
  gboolean need_sink;
810
811
812
} FindPropertyHelper;

static gint
813
find_property (GstElement * element, FindPropertyHelper * helper)
814
{
815
  if (helper->need_sink && !element_is_sink (element)) {
816
    gst_object_unref (element);
817
    return 1;
818
  }
819
820
821
822
823
824

  if (!element_has_property (element, helper->prop_name, helper->prop_type)) {
    gst_object_unref (element);
    return 1;
  }

825
826
  GST_INFO_OBJECT (element, "found %s with %s property", helper->prop_name,
      (helper->need_sink) ? "sink" : "element");
827
  return 0;                     /* keep it */
828
829
}

830
/* FIXME: why not move these functions into core? */
831
832
833
/* find a sink in the hierarchy with a property named @name. This function does
 * not increase the refcount of the returned object and thus remains valid as
 * long as the bin is valid. */
834
835
static GstElement *
gst_play_sink_find_property_sinks (GstPlaySink * playsink, GstElement * obj,
836
    const gchar * name, GType expected_type)
837
838
839
840
{
  GstElement *result = NULL;
  GstIterator *it;

841
  if (element_has_property (obj, name, expected_type)) {
842
843
    result = obj;
  } else if (GST_IS_BIN (obj)) {
844
    FindPropertyHelper helper = { name, expected_type, TRUE };
845

846
847
    it = gst_bin_iterate_recurse (GST_BIN_CAST (obj));
    result = gst_iterator_find_custom (it,
848
        (GCompareFunc) find_property, &helper);
849
    gst_iterator_free (it);
850
851
852
    /* we don't need the extra ref */
    if (result)
      gst_object_unref (result);
853
854
855
856
  }
  return result;
}

857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
/* find an object in the hierarchy with a property named @name */
static GstElement *
gst_play_sink_find_property (GstPlaySink * playsink, GstElement * obj,
    const gchar * name, GType expected_type)
{
  GstElement *result = NULL;
  GstIterator *it;

  if (GST_IS_BIN (obj)) {
    FindPropertyHelper helper = { name, expected_type, FALSE };

    it = gst_bin_iterate_recurse (GST_BIN_CAST (obj));
    result = gst_iterator_find_custom (it,
        (GCompareFunc) find_property, &helper);
    gst_iterator_free (it);
  } else {
    if (element_has_property (obj, name, expected_type)) {
      result = obj;
      gst_object_ref (obj);
    }
  }
  return result;
}

881
882
883
884
885
static void
do_async_start (GstPlaySink * playsink)
{
  GstMessage *message;

886
887
  if (!playsink->need_async_start) {
    GST_INFO_OBJECT (playsink, "no async_start needed");
Jan Schmidt's avatar
Jan Schmidt committed
888
    return;
889
  }
Jan Schmidt's avatar
Jan Schmidt committed
890

891
892
  playsink->async_pending = TRUE;

Jan Schmidt's avatar
Jan Schmidt committed
893
  GST_INFO_OBJECT (playsink, "Sending async_start message");
894
895
896
897
898
899
900
901
902
903
904
  message = gst_message_new_async_start (GST_OBJECT_CAST (playsink), FALSE);
  GST_BIN_CLASS (gst_play_sink_parent_class)->handle_message (GST_BIN_CAST
      (playsink), message);
}

static void
do_async_done (GstPlaySink * playsink)
{
  GstMessage *message;

  if (playsink->async_pending) {
Jan Schmidt's avatar
Jan Schmidt committed
905
    GST_INFO_OBJECT (playsink, "Sending async_done message");
906
907
908
909
910
911
    message = gst_message_new_async_done (GST_OBJECT_CAST (playsink));
    GST_BIN_CLASS (gst_play_sink_parent_class)->handle_message (GST_BIN_CAST
        (playsink), message);

    playsink->async_pending = FALSE;
  }
Jan Schmidt's avatar
Jan Schmidt committed
912
913

  playsink->need_async_start = FALSE;
914
915
}

916
917
/* try to change the state of an element. This function returns the element when
 * the state change could be performed. When this function returns NULL an error
918
 * occured and the element is unreffed if @unref is TRUE. */
919
static GstElement *
920
try_element (GstPlaySink * playsink, GstElement * element, gboolean unref)
921
922
923
924
925
926
927
928
{
  GstStateChangeReturn ret;

  if (element) {
    ret = gst_element_set_state (element, GST_STATE_READY);
    if (ret == GST_STATE_CHANGE_FAILURE) {
      GST_DEBUG_OBJECT (playsink, "failed state change..");
      gst_element_set_state (element, GST_STATE_NULL);
929
930
      if (unref)
        gst_object_unref (element);
931
932
933
934
935
936
      element = NULL;
    }
  }
  return element;
}

Wim Taymans's avatar
Wim Taymans committed
937
/* make the element (bin) that contains the elements needed to perform
Wim Taymans's avatar
Wim Taymans committed
938
 * video display.
Wim Taymans's avatar
Wim Taymans committed
939
 *
940
941
942
943
944
945
946
947
 *  +------------------------------------------------------------+
 *  | vbin                                                       |
 *  |      +-------+   +----------+   +----------+   +---------+ |
 *  |      | queue |   |colorspace|   |videoscale|   |videosink| |
 *  |   +-sink    src-sink       src-sink       src-sink       | |
 *  |   |  +-------+   +----------+   +----------+   +---------+ |
 * sink-+                                                        |
 *  +------------------------------------------------------------+
Wim Taymans's avatar
Wim Taymans committed
948
 *
Wim Taymans's avatar
Wim Taymans committed
949
 */
950
static GstPlayVideoChain *
951
952
gen_video_chain (GstPlaySink * playsink, gboolean raw, gboolean async,
    gboolean queue)
Wim Taymans's avatar
Wim Taymans committed
953
954
955
956
{
  GstPlayVideoChain *chain;
  GstBin *bin;
  GstPad *pad;
957
  GstElement *head = NULL, *prev = NULL, *elem = NULL;
Wim Taymans's avatar
Wim Taymans committed
958
959

  chain = g_new0 (GstPlayVideoChain, 1);
960
  chain->chain.playsink = playsink;
961
  chain->chain.raw = raw;
Wim Taymans's avatar
Wim Taymans committed
962

963
964
  GST_DEBUG_OBJECT (playsink, "making video chain %p", chain);

965
  if (playsink->video_sink) {
966
    GST_DEBUG_OBJECT (playsink, "trying configured videosink");
967
    chain->sink = try_element (playsink, playsink->video_sink, FALSE);
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
  } else {
    /* only try fallback if no specific sink was chosen */
    if (chain->sink == NULL) {
      GST_DEBUG_OBJECT (playsink, "trying autovideosink");
      elem = gst_element_factory_make ("autovideosink", "videosink");
      chain->sink = try_element (playsink, elem, TRUE);
    }
    if (chain->sink == NULL) {
      /* if default sink from config.h is different then try it too */
      if (strcmp (DEFAULT_VIDEOSINK, "autovideosink")) {
        GST_DEBUG_OBJECT (playsink, "trying " DEFAULT_VIDEOSINK);
        elem = gst_element_factory_make (DEFAULT_VIDEOSINK, "videosink");
        chain->sink = try_element (playsink, elem, TRUE);
      }
    }
Wim Taymans's avatar
Wim Taymans committed
983
  }
984
985
  if (chain->sink == NULL)
    goto no_sinks;
986
  head = chain->sink;
Wim Taymans's avatar
Wim Taymans committed
987

988
  /* if we can disable async behaviour of the sink, we can avoid adding a
989
   * queue for the audio chain. */
990
991
992
  elem =
      gst_play_sink_find_property_sinks (playsink, chain->sink, "async",
      G_TYPE_BOOLEAN);
993
994
995
996
  if (elem) {
    GST_DEBUG_OBJECT (playsink, "setting async property to %d on element %s",
        async, GST_ELEMENT_NAME (elem));
    g_object_set (elem, "async", async, NULL);
997
    chain->async = async;
998
999
  } else {
    GST_DEBUG_OBJECT (playsink, "no async property on the sink");
1000
    chain->async = TRUE;
1001
  }
1002

Wim Taymans's avatar
Wim Taymans committed
1003
1004
1005
1006
  /* create a bin to hold objects, as we create them we add them to this bin so
   * that when something goes wrong we only need to unref the bin */
  chain->chain.bin = gst_bin_new ("vbin");
  bin = GST_BIN_CAST (chain->chain.bin);
1007
  gst_object_ref_sink (bin);
Wim Taymans's avatar
Wim Taymans committed
1008
1009
  gst_bin_add (bin, chain->sink);

1010
1011
1012
1013
1014
1015
  if (queue) {
    /* decouple decoder from sink, this improves playback quite a lot since the
     * decoder can continue while the sink blocks for synchronisation. We don't
     * need a lot of buffers as this consumes a lot of memory and we don't want
     * too little because else we would be context switching too quickly. */
    chain->queue = gst_element_factory_make ("queue", "vqueue");
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
    if (chain->queue == NULL) {
      post_missing_element_message (playsink, "queue");
      GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
          (_("Missing element '%s' - check your GStreamer installation."),
              "queue"), ("video rendering might be suboptimal"));
      head = chain->sink;
      prev = NULL;
    } else {
      g_object_set (G_OBJECT (chain->queue), "max-size-buffers", 3,
          "max-size-bytes", 0, "max-size-time", (gint64) 0, NULL);
      gst_bin_add (bin, chain->queue);
      head = prev = chain->queue;
    }
1029
1030
1031
1032
  } else {
    head = chain->sink;
    prev = NULL;
  }
Wim Taymans's avatar
Wim Taymans committed
1033

1034
  if (raw && !(playsink->flags & GST_PLAY_FLAG_NATIVE_VIDEO)) {
1035
1036
1037
1038
1039
1040
    GST_DEBUG_OBJECT (playsink, "creating ffmpegcolorspace");
    chain->conv = gst_element_factory_make ("ffmpegcolorspace", "vconv");
    if (chain->conv == NULL) {
      post_missing_element_message (playsink, "ffmpegcolorspace");
      GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
          (_("Missing element '%s' - check your GStreamer installation."),
1041
              "ffmpegcolorspace"), ("video rendering might fail"));
1042
1043
    } else {
      gst_bin_add (bin, chain->conv);
1044
1045
1046
1047
1048
1049
      if (prev) {
        if (!gst_element_link_pads (prev, "src", chain->conv, "sink"))
          goto link_failed;
      } else {
        head = chain->conv;
      }
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
      prev = chain->conv;
    }

    GST_DEBUG_OBJECT (playsink, "creating videoscale");
    chain->scale = gst_element_factory_make ("videoscale", "vscale");
    if (chain->scale == NULL) {
      post_missing_element_message (playsink, "videoscale");
      GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
          (_("Missing element '%s' - check your GStreamer installation."),
              "videoscale"), ("possibly a liboil version mismatch?"));
    } else {
      gst_bin_add (bin, chain->scale);
1062
1063
1064
1065
1066
1067
      if (prev) {
        if (!gst_element_link_pads (prev, "src", chain->scale, "sink"))
          goto link_failed;
      } else {
        head = chain->scale;
      }
1068
1069
      prev = chain->scale;
    }
1070
  }
Wim Taymans's avatar
Wim Taymans committed
1071

1072
1073
1074
1075
1076
  if (prev) {
    GST_DEBUG_OBJECT (playsink, "linking to sink");
    if (!gst_element_link_pads (prev, "src", chain->sink, NULL))
      goto link_failed;
  }
1077

1078
  pad = gst_element_get_static_pad (head, "sink");
1079
  chain->sinkpad = gst_ghost_pad_new ("sink", pad);
Wim Taymans's avatar
Wim Taymans committed
1080
  gst_object_unref (pad);
1081

1082
  gst_element_add_pad (chain->chain.bin, chain->sinkpad);
Wim Taymans's avatar
Wim Taymans committed
1083

1084
  return chain;
Wim Taymans's avatar
Wim Taymans committed
1085
1086
1087
1088

  /* ERRORS */
no_sinks:
  {
1089
    if (!elem && !playsink->video_sink) {
1090
      post_missing_element_message (playsink, "autovideosink");
1091
1092
1093
1094
1095
1096
1097
1098
1099
      if (strcmp (DEFAULT_VIDEOSINK, "autovideosink")) {
        post_missing_element_message (playsink, DEFAULT_VIDEOSINK);
        GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
            (_("Both autovideosink and %s elements are missing."),
                DEFAULT_VIDEOSINK), (NULL));
      } else {
        GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
            (_("The autovideosink element is missing.")), (NULL));
      }
1100
    } else {
1101
1102
1103
1104
1105
      if (playsink->video_sink) {
        GST_ELEMENT_ERROR (playsink, CORE, STATE_CHANGE,
            (_("Configured videosink %s is not working."),
                GST_ELEMENT_NAME (playsink->video_sink)), (NULL));
      } else if (strcmp (DEFAULT_VIDEOSINK, "autovideosink")) {
1106
1107
1108
1109
1110
1111
1112
        GST_ELEMENT_ERROR (playsink, CORE, STATE_CHANGE,
            (_("Both autovideosink and %s elements are not working."),
                DEFAULT_VIDEOSINK), (NULL));
      } else {
        GST_ELEMENT_ERROR (playsink, CORE, MISSING_PLUGIN,
            (_("The autovideosink element is not working.")), (NULL));
      }
1113
    }
Wim Taymans's avatar
Wim Taymans committed
1114
1115
1116
1117
1118
    free_chain ((GstPlayChain *) chain);
    return NULL;
  }
link_failed:
  {
1119
    GST_ELEMENT_ERROR (playsink, CORE, PAD,
Wim Taymans's avatar
Wim Taymans committed
1120
1121
1122
1123
1124
1125
        (NULL), ("Failed to configure the video sink."));
    free_chain ((GstPlayChain *) chain);
    return NULL;
  }
}

1126
static gboolean
1127
1128
setup_video_chain (GstPlaySink * playsink, gboolean raw, gboolean async,
    gboolean queue)
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
{
  GstElement *elem;
  GstPlayVideoChain *chain;
  GstStateChangeReturn ret;

  chain = playsink->videochain;

  /* if the chain was active we don't do anything */
  if (GST_PLAY_CHAIN (chain)->activated == TRUE)
    return TRUE;

1140
1141
1142
  if (chain->chain.raw != raw)
    return FALSE;

1143
1144
1145
1146
1147
1148
1149
  /* try to set the sink element to READY again */
  ret = gst_element_set_state (chain->sink, GST_STATE_READY);
  if (ret == GST_STATE_CHANGE_FAILURE)
    return FALSE;

  /* if we can disable async behaviour of the sink, we can avoid adding a
   * queue for the audio chain. */
1150
1151
1152
  elem =
      gst_play_sink_find_property_sinks (playsink, chain->sink, "async",
      G_TYPE_BOOLEAN);
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
  if (elem) {
    GST_DEBUG_OBJECT (playsink, "setting async property to %d on element %s",
        async, GST_ELEMENT_NAME (elem));
    g_object_set (elem, "async", async, NULL);
    chain->async = async;
  } else {
    GST_DEBUG_OBJECT (playsink, "no async property on the sink");
    chain->async = TRUE;
  }
  return TRUE;
}

Wim Taymans's avatar
Wim Taymans committed
1165
1166
/* make an element for playback of video with subtitles embedded.
 *
1167
1168
1169
1170
1171
1172
1173
1174
1175
 *  +--------------------------------------------+
 *  | tbin                                       |
 *  |     +--------+      +-----------------+    |
 *  |     | queue  |      | subtitleoverlay |    |
 * video--src     sink---video_sink         |    |
 *  |     +--------+     |                src--src
 * text------------------text_sink          |    |
 *  |                     +-----------------+    |
 *  +--------------------------------------------+
1176
 *
Wim Taymans's avatar
Wim Taymans committed
1177
 */
1178
1179
static GstPlayTextChain *
gen_text_chain (GstPlaySink * playsink)
Wim Taymans's avatar
Wim Taymans committed
1180
{
1181
1182
  GstPlayTextChain *chain;
  GstBin *bin;
1183
1184
  GstElement *elem;
  GstPad *videosinkpad, *textsinkpad, *srcpad;
Wim Taymans's avatar
Wim Taymans committed
1185

1186
  chain = g_new0 (GstPlayTextChain, 1);
1187
  chain->chain.playsink = playsink;
Wim Taymans's avatar
Wim Taymans committed
1188

1189
  GST_DEBUG_OBJECT (playsink, "making text chain %p", chain);
Wim Taymans's avatar
Wim Taymans committed
1190

1191
1192
  chain->chain.bin = gst_bin_new ("tbin");
  bin = GST_BIN_CAST (chain->chain.bin);
1193
  gst_object_ref_sink (bin);
1194

1195
1196
1197
1198
1199
  videosinkpad = textsinkpad = srcpad = NULL;

  /* first try to hook the text pad to the custom sink */
  if (playsink->text_sink) {
    GST_DEBUG_OBJECT (playsink, "trying configured textsink");
1200
    chain->sink = try_element (playsink, playsink->text_sink, FALSE);
1201
    if (chain->sink) {
1202
1203
1204
      elem =
          gst_play_sink_find_property_sinks (playsink, chain->sink, "async",
          G_TYPE_BOOLEAN);
1205
      if (elem) {
1206
1207
        /* make sure the sparse subtitles don't participate in the preroll */
        g_object_set (elem, "async", FALSE, NULL);
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
        /* we have a custom sink, this will be our textsinkpad */
        textsinkpad = gst_element_get_static_pad (chain->sink, "sink");
        if (textsinkpad) {
          /* we're all fine now and we can add the sink to the chain */
          GST_DEBUG_OBJECT (playsink, "adding custom text sink");
          gst_bin_add (bin, chain->sink);
        } else {
          GST_WARNING_OBJECT (playsink,
              "can't find a sink pad on custom text sink");
          gst_object_unref (chain->sink);
          chain->sink = NULL;
        }
1220
1221
1222
        /* try to set sync to true but it's no biggie when we can't */
        if ((elem =
                gst_play_sink_find_property_sinks (playsink, chain->sink,
1223
                    "sync", G_TYPE_BOOLEAN)))
1224
          g_object_set (elem, "sync", TRUE, NULL);
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
      } else {
        GST_WARNING_OBJECT (playsink,
            "can't find async property in custom text sink");
      }
    }
    if (textsinkpad == NULL) {
      GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
          (_("Custom text sink element is not usable.")),
          ("fallback to default textoverlay"));
    }
Wim Taymans's avatar
Wim Taymans committed
1235
1236
  }

1237
1238
  if (textsinkpad == NULL) {
    if (!(playsink->flags & GST_PLAY_FLAG_NATIVE_VIDEO)) {
1239
1240
      /* make a little queue */
      chain->queue = gst_element_factory_make ("queue", "vqueue");
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
      if (chain->queue == NULL) {
        post_missing_element_message (playsink, "queue");
        GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
            (_("Missing element '%s' - check your GStreamer installation."),
                "queue"), ("video rendering might be suboptimal"));
      } else {
        g_object_set (G_OBJECT (chain->queue), "max-size-buffers", 3,
            "max-size-bytes", 0, "max-size-time", (gint64) 0, NULL);
        gst_bin_add (bin, chain->queue);
        videosinkpad = gst_element_get_static_pad (chain->queue, "sink");
      }
1252

1253
1254
1255
1256
      chain->overlay =
          gst_element_factory_make ("subtitleoverlay", "suboverlay");
      if (chain->overlay == NULL) {
        post_missing_element_message (playsink, "subtitleoverlay");
1257
1258
        GST_ELEMENT_WARNING (playsink, CORE, MISSING_PLUGIN,
            (_("Missing element '%s' - check your GStreamer installation."),
1259
                "subtitleoverlay"), ("subtitle rendering disabled"));
1260
      } else {
1261
        gst_bin_add (bin, chain->overlay);
Wim Taymans's avatar
Wim Taymans committed
1262

1263
        g_object_set (G_OBJECT (chain->overlay), "silent", FALSE, NULL);
1264
1265
1266
1267
        if (playsink->font_desc) {
          g_object_set (G_OBJECT (chain->overlay), "font-desc",
              playsink->font_desc, NULL);
        }
1268
1269
1270
1271
        if (playsink->subtitle_encoding) {
          g_object_set (G_OBJECT (chain->overlay), "subtitle-encoding",
              playsink->subtitle_encoding, NULL);
        }
Wim Taymans's avatar
Wim Taymans committed
1272

1273
1274
1275
        gst_element_link_pads (chain->queue, "src", chain->overlay,
            "video_sink");

1276
1277
1278
        textsinkpad =
            gst_element_get_static_pad (chain->overlay, "subtitle_sink");
        srcpad = gst_element_get_static_pad (chain->overlay, "src");
1279
1280
1281
      }
    }
  }
Wim Taymans's avatar
Wim Taymans committed
1282

1283
  if (videosinkpad == NULL) {
1284
1285
    /* if we still don't have a videosink, we don't have an overlay. the only
     * thing we can do is insert an identity and ghost the src
1286
     * and sink pads. */
1287
    chain->identity = gst_element_factory_make ("identity", "tidentity");