1 /* GStreamer
2 * Copyright (C) 2008 Jan Schmidt <thaytan@noraisin.net>
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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <string.h>
25
26 #include <gst/gst.h>
27 #include <gst/glib-compat-private.h>
28 #include <gst/pbutils/missing-plugins.h>
29 #include <gst/video/video.h>
30 #include <gst/audio/audio.h>
31 #include <gst/gst-i18n-plugin.h>
32
33 #include "resindvdbin.h"
34 #include "resindvdsrc.h"
35 #include "rsninputselector.h"
36 #include "rsndec.h"
37 #include "rsnparsetter.h"
38
39 #include "gstmpegdemux.h"
40
41 #define RSN_TYPE_INPUT_SELECTOR GST_TYPE_INPUT_SELECTOR
42
43 GST_DEBUG_CATEGORY_EXTERN (resindvd_debug);
44 #define GST_CAT_DEFAULT resindvd_debug
45
46 #define DVDBIN_LOCK(d) g_mutex_lock(&(d)->dvd_lock)
47 #define DVDBIN_UNLOCK(d) g_mutex_unlock(&(d)->dvd_lock)
48
49 #define DVDBIN_PREROLL_LOCK(d) g_mutex_lock(&(d)->preroll_lock)
50 #define DVDBIN_PREROLL_UNLOCK(d) g_mutex_unlock(&(d)->preroll_lock)
51
52 #define DEFAULT_DEVICE "/dev/dvd"
53 enum
54 {
55 /* FILL ME */
56 LAST_SIGNAL
57 };
58
59 enum
60 {
61 ARG_0,
62 ARG_DEVICE
63 };
64
65 static GstStaticPadTemplate video_src_template =
66 GST_STATIC_PAD_TEMPLATE ("video",
67 GST_PAD_SRC,
68 GST_PAD_SOMETIMES,
69 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL))
70 );
71
72 static GstStaticPadTemplate audio_src_template =
73 GST_STATIC_PAD_TEMPLATE ("audio",
74 GST_PAD_SRC,
75 GST_PAD_SOMETIMES,
76 GST_STATIC_CAPS (GST_AUDIO_CAPS_MAKE (GST_AUDIO_FORMATS_ALL))
77 );
78
79 static GstStaticPadTemplate subpicture_src_template =
80 GST_STATIC_PAD_TEMPLATE ("subpicture",
81 GST_PAD_SRC,
82 GST_PAD_SOMETIMES,
83 GST_STATIC_CAPS ("subpicture/x-dvd")
84 );
85
86 static void rsn_dvdbin_finalize (GObject * object);
87 static void rsn_dvdbin_uri_handler_init (gpointer g_iface, gpointer iface_data);
88 static gboolean rsndvdbin_element_init (GstPlugin * plugin);
89
90 #define rsn_dvdbin_parent_class parent_class
91 G_DEFINE_TYPE_WITH_CODE (RsnDvdBin, rsn_dvdbin, GST_TYPE_BIN,
92 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, rsn_dvdbin_uri_handler_init));
93 GST_ELEMENT_REGISTER_DEFINE_CUSTOM (rsndvdbin, rsndvdbin_element_init);
94
95 static void demux_pad_added (GstElement * element, GstPad * pad,
96 RsnDvdBin * dvdbin);
97 static void demux_no_more_pads (GstElement * element, RsnDvdBin * dvdbin);
98 static void rsn_dvdbin_set_property (GObject * object, guint prop_id,
99 const GValue * value, GParamSpec * pspec);
100 static void rsn_dvdbin_get_property (GObject * object, guint prop_id,
101 GValue * value, GParamSpec * pspec);
102 static GstStateChangeReturn rsn_dvdbin_change_state (GstElement * element,
103 GstStateChange transition);
104 static void rsn_dvdbin_no_more_pads (RsnDvdBin * dvdbin);
105
106
107 GST_DEBUG_CATEGORY (resindvd_debug);
108 #define GST_CAT_DEFAULT resindvd_debug
109
110 static void
rsn_dvdbin_class_init(RsnDvdBinClass * klass)111 rsn_dvdbin_class_init (RsnDvdBinClass * klass)
112 {
113 GObjectClass *gobject_class;
114 GstElementClass *element_class;
115
116 gobject_class = (GObjectClass *) klass;
117 element_class = (GstElementClass *) klass;
118
119 gobject_class->finalize = rsn_dvdbin_finalize;
120 gobject_class->set_property = rsn_dvdbin_set_property;
121 gobject_class->get_property = rsn_dvdbin_get_property;
122
123 g_object_class_install_property (gobject_class, ARG_DEVICE,
124 g_param_spec_string ("device", "Device", "DVD device location",
125 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
126
127 gst_element_class_add_static_pad_template (element_class,
128 &video_src_template);
129 gst_element_class_add_static_pad_template (element_class,
130 &audio_src_template);
131 gst_element_class_add_static_pad_template (element_class,
132 &subpicture_src_template);
133
134 element_class->change_state = GST_DEBUG_FUNCPTR (rsn_dvdbin_change_state);
135
136 gst_element_class_set_static_metadata (element_class, "rsndvdbin",
137 "Generic/Bin/Player",
138 "DVD playback element", "Jan Schmidt <thaytan@noraisin.net>");
139 }
140
141 static void
rsn_dvdbin_init(RsnDvdBin * dvdbin)142 rsn_dvdbin_init (RsnDvdBin * dvdbin)
143 {
144 g_mutex_init (&dvdbin->dvd_lock);
145 g_mutex_init (&dvdbin->preroll_lock);
146 }
147
148 static void
rsn_dvdbin_finalize(GObject * object)149 rsn_dvdbin_finalize (GObject * object)
150 {
151 RsnDvdBin *dvdbin = RESINDVDBIN (object);
152
153 g_mutex_clear (&dvdbin->dvd_lock);
154 g_mutex_clear (&dvdbin->preroll_lock);
155 g_free (dvdbin->last_uri);
156 g_free (dvdbin->device);
157
158 G_OBJECT_CLASS (parent_class)->finalize (object);
159 }
160
161 /* URI interface */
162 static GstURIType
rsn_dvdbin_uri_get_type(GType type)163 rsn_dvdbin_uri_get_type (GType type)
164 {
165 return GST_URI_SRC;
166 }
167
168 static const gchar *const *
rsn_dvdbin_uri_get_protocols(GType type)169 rsn_dvdbin_uri_get_protocols (GType type)
170 {
171 static const gchar *protocols[] = { "dvd", NULL };
172
173 return protocols;
174 }
175
176 static gchar *
rsn_dvdbin_uri_get_uri(GstURIHandler * handler)177 rsn_dvdbin_uri_get_uri (GstURIHandler * handler)
178 {
179 RsnDvdBin *dvdbin = RESINDVDBIN (handler);
180
181 DVDBIN_LOCK (dvdbin);
182 g_free (dvdbin->last_uri);
183 if (dvdbin->device)
184 dvdbin->last_uri = g_strdup_printf ("dvd://%s", dvdbin->device);
185 else
186 dvdbin->last_uri = g_strdup ("dvd://");
187 DVDBIN_UNLOCK (dvdbin);
188
189 return g_strdup (dvdbin->last_uri);
190 }
191
192 static gboolean
rsn_dvdbin_uri_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)193 rsn_dvdbin_uri_set_uri (GstURIHandler * handler, const gchar * uri,
194 GError ** error)
195 {
196 RsnDvdBin *dvdbin = RESINDVDBIN (handler);
197 gboolean ret;
198 gchar *protocol, *location;
199
200 protocol = gst_uri_get_protocol (uri);
201
202 ret = (protocol && !strcmp (protocol, "dvd")) ? TRUE : FALSE;
203
204 g_free (protocol);
205 protocol = NULL;
206
207 if (!ret)
208 return ret;
209
210 location = gst_uri_get_location (uri);
211 if (!location)
212 return ret;
213
214 /*
215 * URI structure: dvd:///path/to/device
216 */
217 if (g_str_has_prefix (uri, "dvd://")) {
218 g_free (dvdbin->device);
219 if (strlen (uri) > 6)
220 dvdbin->device = g_strdup (uri + 6);
221 else
222 dvdbin->device = g_strdup (DEFAULT_DEVICE);
223 }
224 #if 0
225 /*
226 * Parse out the new t/c/a and seek to them
227 */
228 {
229 gchar **strs;
230 gchar **strcur;
231 gint pos = 0;
232
233 strcur = strs = g_strsplit (location, ",", 0);
234 while (strcur && *strcur) {
235 gint val;
236
237 if (!sscanf (*strcur, "%d", &val))
238 break;
239
240 switch (pos) {
241 case 0:
242 if (val != dvdbin->uri_title) {
243 dvdbin->uri_title = val;
244 dvdbin->new_seek = TRUE;
245 }
246 break;
247 case 1:
248 if (val != dvdbin->uri_chapter) {
249 dvdbin->uri_chapter = val;
250 dvdbin->new_seek = TRUE;
251 }
252 break;
253 case 2:
254 dvdbin->uri_angle = val;
255 break;
256 }
257
258 strcur++;
259 pos++;
260 }
261
262 g_strfreev (strs);
263 }
264 #endif
265
266 g_free (location);
267
268 return ret;
269 }
270
271 static void
rsn_dvdbin_uri_handler_init(gpointer g_iface,gpointer iface_data)272 rsn_dvdbin_uri_handler_init (gpointer g_iface, gpointer iface_data)
273 {
274 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
275
276 iface->get_type = rsn_dvdbin_uri_get_type;
277 iface->get_protocols = rsn_dvdbin_uri_get_protocols;
278 iface->get_uri = rsn_dvdbin_uri_get_uri;
279 iface->set_uri = rsn_dvdbin_uri_set_uri;
280 }
281
282 static void
rsn_dvdbin_no_more_pads(RsnDvdBin * dvdbin)283 rsn_dvdbin_no_more_pads (RsnDvdBin * dvdbin)
284 {
285 if (dvdbin->did_no_more_pads)
286 return;
287 dvdbin->did_no_more_pads = TRUE;
288
289 GST_DEBUG_OBJECT (dvdbin, "Firing no more pads");
290 /* Shrink subpicture queue to smaller size */
291 g_object_set (dvdbin->pieces[DVD_ELEM_SPUQ],
292 "max-size-time", G_GUINT64_CONSTANT (0), "max-size-bytes", 0,
293 "max-size-buffers", 1, NULL);
294 gst_element_no_more_pads (GST_ELEMENT (dvdbin));
295 }
296
297 static gboolean
try_create_piece(RsnDvdBin * dvdbin,gint index,const gchar * factory,GType type,const gchar * name,const gchar * descr)298 try_create_piece (RsnDvdBin * dvdbin, gint index,
299 const gchar * factory, GType type, const gchar * name, const gchar * descr)
300 {
301 GstElement *e;
302
303 DVDBIN_LOCK (dvdbin);
304 if (dvdbin->pieces[index] != NULL) {
305 DVDBIN_UNLOCK (dvdbin);
306 return TRUE; /* Already exists */
307 }
308 DVDBIN_UNLOCK (dvdbin);
309
310 if (factory != NULL) {
311 e = gst_element_factory_make (factory, name);
312 } else {
313 if (name)
314 e = g_object_new (type, "name", name, NULL);
315 else
316 e = g_object_new (type, NULL);
317 }
318 if (e == NULL)
319 goto create_failed;
320
321 if (!gst_bin_add (GST_BIN (dvdbin), e))
322 goto add_failed;
323
324 GST_DEBUG_OBJECT (dvdbin, "Added %s element: %" GST_PTR_FORMAT, descr, e);
325
326 DVDBIN_LOCK (dvdbin);
327 dvdbin->pieces[index] = e;
328 DVDBIN_UNLOCK (dvdbin);
329
330 return TRUE;
331 create_failed:
332 gst_element_post_message (GST_ELEMENT_CAST (dvdbin),
333 gst_missing_element_message_new (GST_ELEMENT_CAST (dvdbin), factory));
334 GST_ELEMENT_ERROR (dvdbin, CORE, MISSING_PLUGIN, (NULL),
335 ("Could not create %s element '%s'", descr, factory));
336 return FALSE;
337 add_failed:
338 gst_object_unref (e);
339 GST_ELEMENT_ERROR (dvdbin, CORE, FAILED, (NULL),
340 ("Could not add %s element to bin", descr));
341 return FALSE;
342 }
343
344 typedef struct
345 {
346 RsnDvdBin *dvdbin;
347 GstPad *pad;
348 gulong pad_block_id;
349 } RsnDvdBinPadBlockCtx;
350
351 static GstPadProbeReturn dvdbin_pad_blocked_cb (GstPad * pad,
352 GstPadProbeInfo * info, RsnDvdBinPadBlockCtx * ctx);
353
354 static void
_pad_block_destroy_notify(RsnDvdBinPadBlockCtx * ctx)355 _pad_block_destroy_notify (RsnDvdBinPadBlockCtx * ctx)
356 {
357 gst_object_unref (ctx->dvdbin);
358 gst_object_unref (ctx->pad);
359 g_slice_free (RsnDvdBinPadBlockCtx, ctx);
360 }
361
362 #if DEBUG_TIMING
363 static GstPadProbeReturn
dvdbin_dump_timing_info(GstPad * opad,GstPadProbeInfo * info,gpointer userdata)364 dvdbin_dump_timing_info (GstPad * opad,
365 GstPadProbeInfo * info, gpointer userdata)
366 {
367 if (GST_PAD_PROBE_INFO_TYPE (info) & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
368 GST_PAD_PROBE_TYPE_EVENT_FLUSH)) {
369 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
370 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
371 const GstSegment *seg;
372
373 gst_event_parse_segment (event, &seg);
374
375 g_print ("%s:%s segment: rate %g format %d, start: %"
376 GST_TIME_FORMAT ", stop: %" GST_TIME_FORMAT ", time: %"
377 GST_TIME_FORMAT " base: %" GST_TIME_FORMAT "\n",
378 GST_DEBUG_PAD_NAME (opad),
379 seg->rate, seg->format, GST_TIME_ARGS (seg->start),
380 GST_TIME_ARGS (seg->stop), GST_TIME_ARGS (seg->time),
381 GST_TIME_ARGS (seg->base));
382 } else if (GST_EVENT_TYPE (event) == GST_EVENT_GAP) {
383 GstClockTime ts, dur, end;
384 gst_event_parse_gap (event, &ts, &dur);
385 end = ts;
386 if (ts != GST_CLOCK_TIME_NONE && dur != GST_CLOCK_TIME_NONE)
387 end += dur;
388 g_print ("%s:%s Gap TS: %" GST_TIME_FORMAT " dur %" GST_TIME_FORMAT
389 " (to %" GST_TIME_FORMAT ")\n", GST_DEBUG_PAD_NAME (opad),
390 GST_TIME_ARGS (ts), GST_TIME_ARGS (dur), GST_TIME_ARGS (end));
391 } else if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
392 g_print ("%s:%s FLUSHED\n", GST_DEBUG_PAD_NAME (opad));
393 }
394 }
395 if (GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_BUFFER) {
396 GstBuffer *buf = GST_PAD_PROBE_INFO_BUFFER (info);
397 g_print ("%s:%s Buffer PTS %" GST_TIME_FORMAT " duration %" GST_TIME_FORMAT
398 "\n", GST_DEBUG_PAD_NAME (opad), GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
399 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
400 }
401 return GST_PAD_PROBE_OK;
402 }
403 #endif
404
405 static gboolean
try_link_pieces(GstElement * e1,const gchar * pad1,GstElement * e2,const gchar * pad2)406 try_link_pieces (GstElement * e1, const gchar * pad1, GstElement * e2,
407 const gchar * pad2)
408 {
409 GstPad *src = gst_element_get_static_pad (e1, pad1);
410 GstPad *sink = gst_element_get_static_pad (e2, pad2);
411 gboolean ret = FALSE;
412
413 if (src == NULL || sink == NULL)
414 goto done;
415
416 if (GST_PAD_LINK_FAILED (gst_pad_link (src, sink)))
417 goto done;
418
419 ret = TRUE;
420 done:
421 if (src)
422 gst_object_unref (src);
423 if (sink)
424 gst_object_unref (sink);
425 return ret;
426 }
427
428 static gboolean
create_elements(RsnDvdBin * dvdbin)429 create_elements (RsnDvdBin * dvdbin)
430 {
431 GstPadTemplate *src_templ = NULL;
432 GstPad *src = NULL;
433 GstPad *sink = NULL;
434 RsnDvdBinPadBlockCtx *bctx = NULL;
435
436 if (!try_create_piece (dvdbin, DVD_ELEM_SOURCE, NULL,
437 RESIN_TYPE_DVDSRC, "dvdsrc", "DVD source")) {
438 return FALSE;
439 }
440
441 /* FIXME: Locking */
442 if (dvdbin->device) {
443 g_object_set (G_OBJECT (dvdbin->pieces[DVD_ELEM_SOURCE]),
444 "device", dvdbin->device, NULL);
445 }
446
447 /* FIXME: Import and use local copy of mpeg PS demuxer */
448 if (!try_create_piece (dvdbin, DVD_ELEM_DEMUX,
449 NULL, GST_TYPE_FLUPS_DEMUX, "dvddemux", "DVD demuxer"))
450 return FALSE;
451
452 if (gst_element_link (dvdbin->pieces[DVD_ELEM_SOURCE],
453 dvdbin->pieces[DVD_ELEM_DEMUX]) == FALSE)
454 goto failed_connect;
455
456 /* Listen for new pads from the demuxer */
457 g_signal_connect (G_OBJECT (dvdbin->pieces[DVD_ELEM_DEMUX]), "pad-added",
458 G_CALLBACK (demux_pad_added), dvdbin);
459
460 g_signal_connect (G_OBJECT (dvdbin->pieces[DVD_ELEM_DEMUX]), "no-more-pads",
461 G_CALLBACK (demux_no_more_pads), dvdbin);
462
463 if (!try_create_piece (dvdbin, DVD_ELEM_MQUEUE, "multiqueue", 0, "rsnmq",
464 "multiqueue"))
465 return FALSE;
466
467 g_object_set (dvdbin->pieces[DVD_ELEM_MQUEUE],
468 "max-size-time", (7 * GST_SECOND / 10), "max-size-bytes", 0,
469 "max-size-buffers", 0, NULL);
470
471 if (!try_create_piece (dvdbin, DVD_ELEM_VIDPARSE, "mpegvideoparse", 0,
472 "rsnvidparse", "video parser"))
473 return FALSE;
474
475 /* Decodebin will throw a missing element message to find an MPEG decoder */
476 if (!try_create_piece (dvdbin, DVD_ELEM_VIDDEC, NULL, RSN_TYPE_VIDEODEC,
477 "rsnviddec", "video decoder"))
478 return FALSE;
479
480 /* FIXME: Replace identity */
481 if (!try_create_piece (dvdbin, DVD_ELEM_PARSET, NULL, RSN_TYPE_RSNPARSETTER,
482 "rsnparsetter", "Aspect ratio adjustment"))
483 return FALSE;
484
485 if (!try_link_pieces (dvdbin->pieces[DVD_ELEM_VIDPARSE], "src",
486 dvdbin->pieces[DVD_ELEM_VIDDEC], "sink"))
487 goto failed_vidparse_connect;
488
489 src = gst_element_get_static_pad (dvdbin->pieces[DVD_ELEM_VIDDEC], "src");
490 sink = gst_element_get_static_pad (dvdbin->pieces[DVD_ELEM_PARSET], "sink");
491 if (src == NULL || sink == NULL)
492 goto failed_viddec_connect;
493 if (GST_PAD_LINK_FAILED (gst_pad_link (src, sink)))
494 goto failed_viddec_connect;
495 gst_object_unref (src);
496 gst_object_unref (sink);
497 src = sink = NULL;
498
499 src = gst_element_get_static_pad (dvdbin->pieces[DVD_ELEM_PARSET], "src");
500 if (src == NULL)
501 goto failed_video_ghost;
502 src_templ = gst_static_pad_template_get (&video_src_template);
503 dvdbin->video_pad = gst_ghost_pad_new_from_template ("video", src, src_templ);
504 gst_object_unref (src_templ);
505 if (dvdbin->video_pad == NULL)
506 goto failed_video_ghost;
507 gst_pad_set_active (dvdbin->video_pad, TRUE);
508 bctx = g_slice_new (RsnDvdBinPadBlockCtx);
509 bctx->dvdbin = gst_object_ref (dvdbin);
510 bctx->pad = gst_object_ref (dvdbin->video_pad);
511 bctx->pad_block_id =
512 gst_pad_add_probe (src, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
513 (GstPadProbeCallback) dvdbin_pad_blocked_cb, bctx, (GDestroyNotify)
514 _pad_block_destroy_notify);
515 gst_object_unref (src);
516 src = NULL;
517
518 #if DEBUG_TIMING
519 gst_pad_add_probe (dvdbin->video_pad,
520 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM | GST_PAD_PROBE_TYPE_BUFFER |
521 GST_PAD_PROBE_TYPE_EVENT_FLUSH,
522 (GstPadProbeCallback) dvdbin_dump_timing_info, NULL, NULL);
523 #endif
524
525 /* FIXME: Merge stream-selection logic to core and switch back */
526 if (!try_create_piece (dvdbin, DVD_ELEM_SPU_SELECT, NULL,
527 RSN_TYPE_INPUT_SELECTOR, "subpselect", "Subpicture stream selector"))
528 return FALSE;
529
530 g_object_set (G_OBJECT (dvdbin->pieces[DVD_ELEM_SPU_SELECT]),
531 "sync-streams", FALSE, NULL);
532
533 /* Add a single standalone queue to hold a single buffer of SPU data */
534 if (!try_create_piece (dvdbin, DVD_ELEM_SPUQ, "queue", 0, "spu_q",
535 "subpicture decoder buffer"))
536 return FALSE;
537 /* Allow a lot more while pre-rolling */
538 g_object_set (dvdbin->pieces[DVD_ELEM_SPUQ],
539 "max-size-time", G_GUINT64_CONSTANT (0), "max-size-bytes", 0,
540 "max-size-buffers", 100, NULL);
541
542 src = gst_element_get_static_pad (dvdbin->pieces[DVD_ELEM_SPU_SELECT], "src");
543 sink = gst_element_get_static_pad (dvdbin->pieces[DVD_ELEM_SPUQ], "sink");
544 if (src == NULL || sink == NULL)
545 goto failed_spuq_connect;
546 if (GST_PAD_LINK_FAILED (gst_pad_link (src, sink)))
547 goto failed_spuq_connect;
548 gst_object_unref (src);
549 gst_object_unref (sink);
550 src = sink = NULL;
551
552 src = gst_element_get_static_pad (dvdbin->pieces[DVD_ELEM_SPUQ], "src");
553 if (src == NULL)
554 goto failed_spu_ghost;
555 src_templ = gst_static_pad_template_get (&subpicture_src_template);
556 dvdbin->subpicture_pad =
557 gst_ghost_pad_new_from_template ("subpicture", src, src_templ);
558 gst_object_unref (src_templ);
559 if (dvdbin->subpicture_pad == NULL)
560 goto failed_spu_ghost;
561 gst_pad_set_active (dvdbin->subpicture_pad, TRUE);
562 bctx = g_slice_new (RsnDvdBinPadBlockCtx);
563 bctx->dvdbin = gst_object_ref (dvdbin);
564 bctx->pad = gst_object_ref (dvdbin->subpicture_pad);
565 bctx->pad_block_id =
566 gst_pad_add_probe (src, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
567 (GstPadProbeCallback) dvdbin_pad_blocked_cb, bctx, (GDestroyNotify)
568 _pad_block_destroy_notify);
569 gst_object_unref (src);
570 src = NULL;
571
572 if (!try_create_piece (dvdbin, DVD_ELEM_AUD_SELECT, NULL,
573 RSN_TYPE_INPUT_SELECTOR, "audioselect", "Audio stream selector"))
574 return FALSE;
575 g_object_set (G_OBJECT (dvdbin->pieces[DVD_ELEM_AUD_SELECT]),
576 "sync-streams", FALSE, NULL);
577
578 if (!try_create_piece (dvdbin, DVD_ELEM_AUDDEC, NULL,
579 RSN_TYPE_AUDIODEC, "auddec", "audio decoder"))
580 return FALSE;
581
582 src = gst_element_get_static_pad (dvdbin->pieces[DVD_ELEM_AUD_SELECT], "src");
583 sink = gst_element_get_static_pad (dvdbin->pieces[DVD_ELEM_AUDDEC], "sink");
584 if (src == NULL || sink == NULL)
585 goto failed_aud_connect;
586 if (GST_PAD_LINK_FAILED (gst_pad_link (src, sink)))
587 goto failed_aud_connect;
588 gst_object_unref (sink);
589 gst_object_unref (src);
590 src = sink = NULL;
591
592 /* ghost audio munge output pad onto bin */
593 src = gst_element_get_static_pad (dvdbin->pieces[DVD_ELEM_AUDDEC], "src");
594 if (src == NULL)
595 goto failed_aud_ghost;
596 src_templ = gst_static_pad_template_get (&audio_src_template);
597 dvdbin->audio_pad = gst_ghost_pad_new_from_template ("audio", src, src_templ);
598 gst_object_unref (src_templ);
599 if (dvdbin->audio_pad == NULL)
600 goto failed_aud_ghost;
601 gst_pad_set_active (dvdbin->audio_pad, TRUE);
602 bctx = g_slice_new (RsnDvdBinPadBlockCtx);
603 bctx->dvdbin = gst_object_ref (dvdbin);
604 bctx->pad = gst_object_ref (dvdbin->audio_pad);
605 bctx->pad_block_id =
606 gst_pad_add_probe (src, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
607 (GstPadProbeCallback) dvdbin_pad_blocked_cb, bctx, (GDestroyNotify)
608 _pad_block_destroy_notify);
609 gst_object_unref (src);
610 src = NULL;
611
612 if (dvdbin->video_added && (dvdbin->audio_added || dvdbin->audio_broken)
613 && dvdbin->subpicture_added) {
614 rsn_dvdbin_no_more_pads (dvdbin);
615 }
616
617 return TRUE;
618
619 failed_connect:
620 GST_ELEMENT_ERROR (dvdbin, CORE, FAILED, (NULL),
621 ("Could not connect DVD source and demuxer elements"));
622 goto error_out;
623 failed_vidparse_connect:
624 GST_ELEMENT_ERROR (dvdbin, CORE, FAILED, (NULL),
625 ("Could not connect DVD video parser and video decoder"));
626 goto error_out;
627 failed_viddec_connect:
628 GST_ELEMENT_ERROR (dvdbin, CORE, FAILED, (NULL),
629 ("Could not connect DVD video decoder and aspect ratio adjuster"));
630 goto error_out;
631 failed_video_ghost:
632 GST_ELEMENT_ERROR (dvdbin, CORE, FAILED, (NULL),
633 ("Could not ghost video output pad"));
634 goto error_out;
635 failed_spuq_connect:
636 GST_ELEMENT_ERROR (dvdbin, CORE, FAILED, (NULL),
637 ("Could not connect DVD subpicture selector and buffer elements"));
638 goto error_out;
639 failed_spu_ghost:
640 GST_ELEMENT_ERROR (dvdbin, CORE, FAILED, (NULL),
641 ("Could not ghost SPU output pad"));
642 goto error_out;
643 failed_aud_connect:
644 GST_ELEMENT_ERROR (dvdbin, CORE, FAILED, (NULL),
645 ("Could not connect DVD audio decoder"));
646 goto error_out;
647 failed_aud_ghost:
648 GST_ELEMENT_ERROR (dvdbin, CORE, FAILED, (NULL),
649 ("Could not ghost audio output pad"));
650 goto error_out;
651 error_out:
652 if (src != NULL)
653 gst_object_unref (src);
654 if (sink != NULL)
655 gst_object_unref (sink);
656 return FALSE;
657 }
658
659 static void
remove_elements(RsnDvdBin * dvdbin)660 remove_elements (RsnDvdBin * dvdbin)
661 {
662 gint i;
663 GList *tmp;
664
665 if (dvdbin->pieces[DVD_ELEM_MQUEUE] != NULL) {
666 for (tmp = dvdbin->mq_req_pads; tmp; tmp = g_list_next (tmp)) {
667 gst_element_release_request_pad (dvdbin->pieces[DVD_ELEM_MQUEUE],
668 GST_PAD (tmp->data));
669 }
670 }
671 g_list_free (dvdbin->mq_req_pads);
672 dvdbin->mq_req_pads = NULL;
673
674 for (i = 0; i < DVD_ELEM_LAST; i++) {
675 DVDBIN_LOCK (dvdbin);
676 if (dvdbin->pieces[i] != NULL) {
677 GstElement *piece = dvdbin->pieces[i];
678
679 dvdbin->pieces[i] = NULL;
680 DVDBIN_UNLOCK (dvdbin);
681
682 gst_element_set_state (piece, GST_STATE_NULL);
683 gst_bin_remove (GST_BIN (dvdbin), piece);
684 } else
685 DVDBIN_UNLOCK (dvdbin);
686 }
687 if (dvdbin->video_pad) {
688 if (dvdbin->video_added)
689 gst_element_remove_pad (GST_ELEMENT (dvdbin), dvdbin->video_pad);
690 else
691 gst_object_unref (dvdbin->video_pad);
692 }
693 if (dvdbin->audio_pad) {
694 if (dvdbin->audio_added)
695 gst_element_remove_pad (GST_ELEMENT (dvdbin), dvdbin->audio_pad);
696 else
697 gst_object_unref (dvdbin->audio_pad);
698 }
699 if (dvdbin->subpicture_pad) {
700 if (dvdbin->subpicture_added)
701 gst_element_remove_pad (GST_ELEMENT (dvdbin), dvdbin->subpicture_pad);
702 else
703 gst_object_unref (dvdbin->subpicture_pad);
704 }
705
706 dvdbin->video_added = dvdbin->audio_added = dvdbin->subpicture_added = FALSE;
707 dvdbin->audio_broken = FALSE;
708 dvdbin->video_pad = dvdbin->audio_pad = dvdbin->subpicture_pad = NULL;
709 dvdbin->did_no_more_pads = FALSE;
710 }
711
712 static GstPad *
connect_thru_mq(RsnDvdBin * dvdbin,GstPad * pad)713 connect_thru_mq (RsnDvdBin * dvdbin, GstPad * pad)
714 {
715 GstPad *mq_sink;
716 GstPad *mq_src;
717 gchar *tmp, *sinkname, *srcname;
718
719 /* Request a pad from multiqueue, then connect this one, then
720 * discover the corresponding output pad and return it */
721 mq_sink = gst_element_request_pad_simple (dvdbin->pieces[DVD_ELEM_MQUEUE],
722 "sink_%u");
723 if (mq_sink == NULL)
724 return FALSE;
725 dvdbin->mq_req_pads = g_list_prepend (dvdbin->mq_req_pads, mq_sink);
726
727 if (gst_pad_link (pad, mq_sink) != GST_PAD_LINK_OK)
728 return FALSE;
729
730 sinkname = gst_pad_get_name (mq_sink);
731 tmp = sinkname + 5;
732 srcname = g_strdup_printf ("src_%s", tmp);
733
734 mq_src = gst_element_get_static_pad (dvdbin->pieces[DVD_ELEM_MQUEUE],
735 srcname);
736
737 g_free (sinkname);
738 g_free (srcname);
739
740 return mq_src;
741 }
742
743 static gboolean
can_sink_caps(GstElement * e,GstCaps * caps)744 can_sink_caps (GstElement * e, GstCaps * caps)
745 {
746 gboolean res = FALSE;
747 GstPad *sink = gst_element_get_static_pad (e, "sink");
748
749 if (sink) {
750 GstCaps *sink_caps = gst_pad_query_caps (sink, caps);
751 if (sink_caps) {
752 res = !gst_caps_is_empty (sink_caps);
753 gst_caps_unref (sink_caps);
754 }
755 gst_object_unref (sink);
756 }
757
758 return res;
759 }
760
761 static void
demux_pad_added(GstElement * element,GstPad * pad,RsnDvdBin * dvdbin)762 demux_pad_added (GstElement * element, GstPad * pad, RsnDvdBin * dvdbin)
763 {
764 gboolean skip_mq = FALSE;
765 GstPad *mq_pad = NULL;
766 GstPad *dest_pad = NULL;
767 GstCaps *caps;
768 GstStructure *s;
769
770 GST_DEBUG_OBJECT (dvdbin, "New pad: %" GST_PTR_FORMAT, pad);
771
772 caps = gst_pad_query_caps (pad, NULL);
773 if (caps == NULL) {
774 GST_WARNING_OBJECT (dvdbin, "NULL caps from pad %" GST_PTR_FORMAT, pad);
775 return;
776 }
777 if (!gst_caps_is_fixed (caps)) {
778 GST_WARNING_OBJECT (dvdbin, "Unfixed caps %" GST_PTR_FORMAT
779 " on pad %" GST_PTR_FORMAT, caps, pad);
780 gst_caps_unref (caps);
781 return;
782 }
783
784 GST_DEBUG_OBJECT (dvdbin,
785 "Pad %" GST_PTR_FORMAT " has caps: %" GST_PTR_FORMAT, pad, caps);
786
787 s = gst_caps_get_structure (caps, 0);
788 g_return_if_fail (s != NULL);
789
790 if (can_sink_caps (dvdbin->pieces[DVD_ELEM_VIDPARSE], caps)) {
791 GST_LOG_OBJECT (dvdbin, "Found video pad w/ caps %" GST_PTR_FORMAT, caps);
792 dest_pad =
793 gst_element_get_static_pad (dvdbin->pieces[DVD_ELEM_VIDPARSE], "sink");
794 } else if (g_str_equal (gst_structure_get_name (s), "subpicture/x-dvd")) {
795 GST_LOG_OBJECT (dvdbin, "Found subpicture pad w/ caps %" GST_PTR_FORMAT,
796 caps);
797 dest_pad =
798 gst_element_request_pad_simple (dvdbin->pieces[DVD_ELEM_SPU_SELECT],
799 "sink_%u");
800 skip_mq = TRUE;
801 } else if (can_sink_caps (dvdbin->pieces[DVD_ELEM_AUDDEC], caps)) {
802 GST_LOG_OBJECT (dvdbin, "Found audio pad w/ caps %" GST_PTR_FORMAT, caps);
803 dest_pad =
804 gst_element_request_pad_simple (dvdbin->pieces[DVD_ELEM_AUD_SELECT],
805 "sink_%u");
806 } else {
807 GstStructure *s;
808
809 GST_DEBUG_OBJECT (dvdbin, "Ignoring unusable pad w/ caps %" GST_PTR_FORMAT,
810 caps);
811 gst_element_post_message (GST_ELEMENT_CAST (dvdbin),
812 gst_missing_decoder_message_new (GST_ELEMENT_CAST (dvdbin), caps));
813
814 s = gst_caps_get_structure (caps, 0);
815 if (g_str_has_prefix ("video/", gst_structure_get_name (s))) {
816 GST_ELEMENT_ERROR (dvdbin, STREAM, CODEC_NOT_FOUND, (NULL),
817 ("No MPEG video decoder found"));
818 } else {
819 GST_ELEMENT_WARNING (dvdbin, STREAM, CODEC_NOT_FOUND, (NULL),
820 ("No audio decoder found"));
821 }
822 }
823
824 gst_caps_unref (caps);
825
826 if (dest_pad == NULL) {
827 GST_DEBUG_OBJECT (dvdbin, "Don't know how to handle pad. Ignoring");
828 return;
829 }
830
831 if (skip_mq) {
832 mq_pad = gst_object_ref (pad);
833 } else {
834 mq_pad = connect_thru_mq (dvdbin, pad);
835 if (mq_pad == NULL)
836 goto failed;
837 GST_DEBUG_OBJECT (dvdbin, "Linking new pad %" GST_PTR_FORMAT
838 " through multiqueue to %" GST_PTR_FORMAT, pad, dest_pad);
839 }
840
841 gst_pad_link (mq_pad, dest_pad);
842
843 gst_object_unref (mq_pad);
844 gst_object_unref (dest_pad);
845
846 return;
847 failed:
848 GST_ELEMENT_ERROR (dvdbin, CORE, FAILED, (NULL),
849 ("Failed to handle new demuxer pad %s", GST_PAD_NAME (pad)));
850 if (mq_pad)
851 gst_object_unref (mq_pad);
852 if (dest_pad)
853 gst_object_unref (dest_pad);
854 return;
855 }
856
857 static void
demux_no_more_pads(GstElement * element,RsnDvdBin * dvdbin)858 demux_no_more_pads (GstElement * element, RsnDvdBin * dvdbin)
859 {
860 gboolean no_more_pads = FALSE;
861 guint n_audio_pads = 0;
862
863 GST_DEBUG_OBJECT (dvdbin, "Received no more pads from demuxer");
864 DVDBIN_PREROLL_LOCK (dvdbin);
865
866 g_object_get (dvdbin->pieces[DVD_ELEM_AUD_SELECT], "n-pads", &n_audio_pads,
867 NULL);
868 if (n_audio_pads == 0) {
869 no_more_pads = dvdbin->video_added && dvdbin->subpicture_added;
870 dvdbin->audio_broken = TRUE;
871 }
872
873 DVDBIN_PREROLL_UNLOCK (dvdbin);
874
875 if (no_more_pads) {
876 GST_DEBUG_OBJECT (dvdbin,
877 "Firing no more pads from demuxer no-more-pads cb");
878 rsn_dvdbin_no_more_pads (dvdbin);
879 }
880 }
881
882 static GstPadProbeReturn
dvdbin_pad_blocked_cb(GstPad * opad,GstPadProbeInfo * info,RsnDvdBinPadBlockCtx * ctx)883 dvdbin_pad_blocked_cb (GstPad * opad,
884 GstPadProbeInfo * info, RsnDvdBinPadBlockCtx * ctx)
885 {
886 RsnDvdBin *dvdbin;
887 GstPad *pad;
888 gboolean added_last_pad = FALSE;
889 gboolean added = FALSE;
890 guint pad_block_id = 0;
891
892 dvdbin = ctx->dvdbin;
893 pad = ctx->pad;
894
895 if (pad == dvdbin->subpicture_pad) {
896 GST_DEBUG_OBJECT (opad, "Pad block -> subpicture pad");
897 DVDBIN_PREROLL_LOCK (dvdbin);
898 added = dvdbin->subpicture_added;
899 dvdbin->subpicture_added = TRUE;
900
901 if (!added) {
902 gst_element_add_pad (GST_ELEMENT (dvdbin), dvdbin->subpicture_pad);
903 added_last_pad = ((dvdbin->audio_broken || dvdbin->audio_added)
904 && dvdbin->video_added);
905 }
906 pad_block_id = ctx->pad_block_id;
907 ctx->pad_block_id = 0;
908 DVDBIN_PREROLL_UNLOCK (dvdbin);
909
910 if (pad_block_id)
911 gst_pad_remove_probe (opad, pad_block_id);
912 } else if (pad == dvdbin->audio_pad) {
913 GST_DEBUG_OBJECT (opad, "Pad block -> audio pad");
914 DVDBIN_PREROLL_LOCK (dvdbin);
915 added = dvdbin->audio_added;
916 dvdbin->audio_added = TRUE;
917
918 if (!added) {
919 gst_element_add_pad (GST_ELEMENT (dvdbin), dvdbin->audio_pad);
920 added_last_pad = (dvdbin->subpicture_added && dvdbin->video_added);
921 }
922 pad_block_id = ctx->pad_block_id;
923 ctx->pad_block_id = 0;
924 DVDBIN_PREROLL_UNLOCK (dvdbin);
925
926 if (pad_block_id)
927 gst_pad_remove_probe (opad, pad_block_id);
928 } else if (pad == dvdbin->video_pad) {
929 GST_DEBUG_OBJECT (opad, "Pad block -> video pad");
930
931 DVDBIN_PREROLL_LOCK (dvdbin);
932 added = dvdbin->video_added;
933 dvdbin->video_added = TRUE;
934
935 if (!added) {
936 gst_element_add_pad (GST_ELEMENT (dvdbin), dvdbin->video_pad);
937 added_last_pad = (dvdbin->subpicture_added && (dvdbin->audio_added
938 || dvdbin->audio_broken));
939 }
940 pad_block_id = ctx->pad_block_id;
941 ctx->pad_block_id = 0;
942 DVDBIN_PREROLL_UNLOCK (dvdbin);
943
944 if (pad_block_id)
945 gst_pad_remove_probe (opad, pad_block_id);
946 }
947
948 if (added_last_pad) {
949 GST_DEBUG_OBJECT (dvdbin, "Firing no more pads from pad-blocked cb");
950 rsn_dvdbin_no_more_pads (dvdbin);
951 }
952
953 return GST_PAD_PROBE_OK;
954 }
955
956 static void
rsn_dvdbin_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)957 rsn_dvdbin_set_property (GObject * object, guint prop_id,
958 const GValue * value, GParamSpec * pspec)
959 {
960 RsnDvdBin *dvdbin = RESINDVDBIN (object);
961
962 switch (prop_id) {
963 case ARG_DEVICE:
964 DVDBIN_LOCK (dvdbin);
965 g_free (dvdbin->device);
966 if (g_value_get_string (value) == NULL)
967 dvdbin->device = g_strdup (DEFAULT_DEVICE);
968 else
969 dvdbin->device = g_value_dup_string (value);
970
971 if (dvdbin->pieces[DVD_ELEM_SOURCE]) {
972 g_object_set_property (G_OBJECT (dvdbin->pieces[DVD_ELEM_SOURCE]),
973 "device", value);
974 }
975 DVDBIN_UNLOCK (dvdbin);
976 break;
977 default:
978 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
979 break;
980 }
981 }
982
983 static void
rsn_dvdbin_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)984 rsn_dvdbin_get_property (GObject * object, guint prop_id,
985 GValue * value, GParamSpec * pspec)
986 {
987 RsnDvdBin *dvdbin = RESINDVDBIN (object);
988
989 switch (prop_id) {
990 case ARG_DEVICE:
991 DVDBIN_LOCK (dvdbin);
992 if (dvdbin->device)
993 g_value_set_string (value, dvdbin->device);
994 else if (dvdbin->pieces[DVD_ELEM_SOURCE])
995 g_object_get_property (G_OBJECT (dvdbin->pieces[DVD_ELEM_SOURCE]),
996 "device", value);
997 else
998 g_value_set_string (value, DEFAULT_DEVICE);
999 DVDBIN_UNLOCK (dvdbin);
1000 break;
1001 default:
1002 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1003 break;
1004 }
1005 }
1006
1007 static GstStateChangeReturn
rsn_dvdbin_change_state(GstElement * element,GstStateChange transition)1008 rsn_dvdbin_change_state (GstElement * element, GstStateChange transition)
1009 {
1010 GstStateChangeReturn ret;
1011 RsnDvdBin *dvdbin = RESINDVDBIN (element);
1012
1013 switch (transition) {
1014 case GST_STATE_CHANGE_NULL_TO_READY:
1015 if (!create_elements (dvdbin)) {
1016 remove_elements (dvdbin);
1017 return GST_STATE_CHANGE_FAILURE;
1018 }
1019 break;
1020 default:
1021 break;
1022 }
1023
1024 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1025 if (ret == GST_STATE_CHANGE_FAILURE)
1026 return ret;
1027
1028 switch (transition) {
1029 case GST_STATE_CHANGE_PAUSED_TO_READY:
1030 case GST_STATE_CHANGE_READY_TO_NULL:
1031 remove_elements (dvdbin);
1032 break;
1033 default:
1034 break;
1035 }
1036
1037 return ret;
1038 }
1039
1040 static gboolean
rsndvdbin_element_init(GstPlugin * plugin)1041 rsndvdbin_element_init (GstPlugin * plugin)
1042 {
1043 gboolean result = TRUE;
1044
1045 GST_DEBUG_CATEGORY_INIT (resindvd_debug, "resindvd",
1046 0, "DVD playback elements from resindvd");
1047
1048 #ifdef ENABLE_NLS
1049 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
1050 LOCALEDIR);
1051 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1052 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1053 #endif
1054
1055 result &= gst_element_register (plugin, "rsndvdbin",
1056 GST_RANK_PRIMARY, RESIN_TYPE_DVDBIN);
1057
1058 result &= gst_flups_demux_plugin_init (plugin);
1059
1060 return result;
1061 }
1062