1 /* GStreamer
2 * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
3 * 2009 Nokia Corporation
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 /**
22 * SECTION:gstdiscoverer
23 * @title: GstDiscoverer
24 * @short_description: Utility for discovering information on URIs.
25 *
26 * The #GstDiscoverer is a utility object which allows to get as much
27 * information as possible from one or many URIs.
28 *
29 * It provides two APIs, allowing usage in blocking or non-blocking mode.
30 *
31 * The blocking mode just requires calling gst_discoverer_discover_uri()
32 * with the URI one wishes to discover.
33 *
34 * The non-blocking mode requires a running #GMainLoop iterating a
35 * #GMainContext, where one connects to the various signals, appends the
36 * URIs to be processed (through gst_discoverer_discover_uri_async()) and then
37 * asks for the discovery to begin (through gst_discoverer_start()).
38 * By default this will use the GLib default main context unless you have
39 * set a custom context using g_main_context_push_thread_default().
40 *
41 * All the information is returned in a #GstDiscovererInfo structure.
42 */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <gst/video/video.h>
49 #include <gst/audio/audio.h>
50
51 #include <string.h>
52
53 #include "pbutils.h"
54 #include "pbutils-private.h"
55
56 /* For g_stat () */
57 #include <glib/gstdio.h>
58
59 GST_DEBUG_CATEGORY_STATIC (discoverer_debug);
60 #define GST_CAT_DEFAULT discoverer_debug
61 #define CACHE_DIRNAME "discoverer"
62
63 static GQuark _CAPS_QUARK;
64 static GQuark _TAGS_QUARK;
65 static GQuark _ELEMENT_SRCPAD_QUARK;
66 static GQuark _TOC_QUARK;
67 static GQuark _STREAM_ID_QUARK;
68 static GQuark _MISSING_PLUGIN_QUARK;
69 static GQuark _STREAM_TOPOLOGY_QUARK;
70 static GQuark _TOPOLOGY_PAD_QUARK;
71
72
73 typedef struct
74 {
75 GstDiscoverer *dc;
76 GstPad *pad;
77 GstElement *queue;
78 GstElement *sink;
79 GstTagList *tags;
80 GstToc *toc;
81 gchar *stream_id;
82 gulong probe_id;
83 } PrivateStream;
84
85 struct _GstDiscovererPrivate
86 {
87 gboolean async;
88
89 /* allowed time to discover each uri in nanoseconds */
90 GstClockTime timeout;
91
92 /* list of pending URI to process (current excluded) */
93 GList *pending_uris;
94
95 GMutex lock;
96 /* TRUE if cleaning up discoverer */
97 gboolean cleanup;
98
99 /* TRUE if processing a URI */
100 gboolean processing;
101
102 /* TRUE if discoverer has been started */
103 gboolean running;
104
105 /* current items */
106 GstDiscovererInfo *current_info;
107 GError *current_error;
108 GstStructure *current_topology;
109
110 GstTagList *all_tags;
111 GstTagList *global_tags;
112
113 /* List of private streams */
114 GList *streams;
115
116 /* List of these sinks and their handler IDs (to remove the probe) */
117 guint pending_subtitle_pads;
118
119 /* Whether we received no_more_pads */
120 gboolean no_more_pads;
121
122 GstState target_state;
123 GstState current_state;
124
125 /* Global elements */
126 GstBin *pipeline;
127 GstElement *uridecodebin;
128 GstBus *bus;
129
130 GType decodebin_type;
131
132 /* Custom main context variables */
133 GMainContext *ctx;
134 GSource *bus_source;
135 GSource *timeout_source;
136
137 /* reusable queries */
138 GstQuery *seeking_query;
139
140 /* Handler ids for various callbacks */
141 gulong pad_added_id;
142 gulong pad_remove_id;
143 gulong no_more_pads_id;
144 gulong source_chg_id;
145 gulong element_added_id;
146 gulong bus_cb_id;
147
148 gboolean use_cache;
149 };
150
151 #define DISCO_LOCK(dc) g_mutex_lock (&dc->priv->lock);
152 #define DISCO_UNLOCK(dc) g_mutex_unlock (&dc->priv->lock);
153
154 static void
_do_init(void)155 _do_init (void)
156 {
157 GST_DEBUG_CATEGORY_INIT (discoverer_debug, "discoverer", 0, "Discoverer");
158
159 _CAPS_QUARK = g_quark_from_static_string ("caps");
160 _ELEMENT_SRCPAD_QUARK = g_quark_from_static_string ("element-srcpad");
161 _TAGS_QUARK = g_quark_from_static_string ("tags");
162 _TOC_QUARK = g_quark_from_static_string ("toc");
163 _STREAM_ID_QUARK = g_quark_from_static_string ("stream-id");
164 _MISSING_PLUGIN_QUARK = g_quark_from_static_string ("missing-plugin");
165 _STREAM_TOPOLOGY_QUARK = g_quark_from_static_string ("stream-topology");
166 _TOPOLOGY_PAD_QUARK = g_quark_from_static_string ("pad");
167 };
168
169 G_DEFINE_TYPE_EXTENDED (GstDiscoverer, gst_discoverer, G_TYPE_OBJECT, 0,
170 G_ADD_PRIVATE (GstDiscoverer) _do_init ());
171
172 enum
173 {
174 SIGNAL_FINISHED,
175 SIGNAL_STARTING,
176 SIGNAL_DISCOVERED,
177 SIGNAL_SOURCE_SETUP,
178 LAST_SIGNAL
179 };
180
181 #define DEFAULT_PROP_TIMEOUT 15 * GST_SECOND
182 #define DEFAULT_PROP_USE_CACHE FALSE
183
184 enum
185 {
186 PROP_0,
187 PROP_TIMEOUT,
188 PROP_USE_CACHE
189 };
190
191 static guint gst_discoverer_signals[LAST_SIGNAL] = { 0 };
192
193 static void gst_discoverer_set_timeout (GstDiscoverer * dc,
194 GstClockTime timeout);
195 static gboolean async_timeout_cb (GstDiscoverer * dc);
196
197 static void discoverer_bus_cb (GstBus * bus, GstMessage * msg,
198 GstDiscoverer * dc);
199 static void uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
200 GstDiscoverer * dc);
201 static void uridecodebin_pad_removed_cb (GstElement * uridecodebin,
202 GstPad * pad, GstDiscoverer * dc);
203 static void uridecodebin_no_more_pads_cb (GstElement * uridecodebin,
204 GstDiscoverer * dc);
205 static void uridecodebin_source_changed_cb (GstElement * uridecodebin,
206 GParamSpec * pspec, GstDiscoverer * dc);
207
208 static void gst_discoverer_dispose (GObject * dc);
209 static void gst_discoverer_finalize (GObject * dc);
210 static void gst_discoverer_set_property (GObject * object, guint prop_id,
211 const GValue * value, GParamSpec * pspec);
212 static void gst_discoverer_get_property (GObject * object, guint prop_id,
213 GValue * value, GParamSpec * pspec);
214 static gboolean _setup_locked (GstDiscoverer * dc);
215 static void handle_current_async (GstDiscoverer * dc);
216 static gboolean emit_discovererd_and_next (GstDiscoverer * dc);
217 static GVariant *gst_discoverer_info_to_variant_recurse (GstDiscovererStreamInfo
218 * sinfo, GstDiscovererSerializeFlags flags);
219 static GstDiscovererStreamInfo *_parse_discovery (GVariant * variant,
220 GstDiscovererInfo * info);
221
222 static void
gst_discoverer_class_init(GstDiscovererClass * klass)223 gst_discoverer_class_init (GstDiscovererClass * klass)
224 {
225 GObjectClass *gobject_class = (GObjectClass *) klass;
226
227 gobject_class->dispose = gst_discoverer_dispose;
228 gobject_class->finalize = gst_discoverer_finalize;
229
230 gobject_class->set_property = gst_discoverer_set_property;
231 gobject_class->get_property = gst_discoverer_get_property;
232
233
234 /* properties */
235 /**
236 * GstDiscoverer:timeout:
237 *
238 * The duration (in nanoseconds) after which the discovery of an individual
239 * URI will timeout.
240 *
241 * If the discovery of a URI times out, the %GST_DISCOVERER_TIMEOUT will be
242 * set on the result flags.
243 */
244 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
245 g_param_spec_uint64 ("timeout", "timeout", "Timeout",
246 GST_SECOND, 3600 * GST_SECOND, DEFAULT_PROP_TIMEOUT,
247 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
248
249 /**
250 * GstDiscoverer::use-cache:
251 *
252 * Whether to use a serialized version of the discoverer info from our
253 * own cache if accessible. This allows the discovery to be much faster
254 * as when using this option, we do not need to create a #GstPipeline
255 * and run it, but instead, just reload the #GstDiscovererInfo in its
256 * serialized form.
257 *
258 * The cache files are saved in `$XDG_CACHE_DIR/gstreamer-1.0/discoverer/`.
259 *
260 * Since: 1.16
261 */
262 g_object_class_install_property (gobject_class, PROP_USE_CACHE,
263 g_param_spec_boolean ("use-cache", "use cache", "Use cache",
264 DEFAULT_PROP_USE_CACHE,
265 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
266
267 /* signals */
268 /**
269 * GstDiscoverer::finished:
270 * @discoverer: the #GstDiscoverer
271 *
272 * Will be emitted in async mode when all pending URIs have been processed.
273 */
274 gst_discoverer_signals[SIGNAL_FINISHED] =
275 g_signal_new ("finished", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
276 G_STRUCT_OFFSET (GstDiscovererClass, finished), NULL, NULL, NULL,
277 G_TYPE_NONE, 0, G_TYPE_NONE);
278
279 /**
280 * GstDiscoverer::starting:
281 * @discoverer: the #GstDiscoverer
282 *
283 * Will be emitted when the discover starts analyzing the pending URIs
284 */
285 gst_discoverer_signals[SIGNAL_STARTING] =
286 g_signal_new ("starting", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
287 G_STRUCT_OFFSET (GstDiscovererClass, starting), NULL, NULL, NULL,
288 G_TYPE_NONE, 0, G_TYPE_NONE);
289
290 /**
291 * GstDiscoverer::discovered:
292 * @discoverer: the #GstDiscoverer
293 * @info: the results #GstDiscovererInfo
294 * @error: (allow-none) (type GLib.Error): #GError, which will be non-NULL
295 * if an error occurred during
296 * discovery. You must not free
297 * this #GError, it will be freed by
298 * the discoverer.
299 *
300 * Will be emitted in async mode when all information on a URI could be
301 * discovered, or an error occurred.
302 *
303 * When an error occurs, @info might still contain some partial information,
304 * depending on the circumstances of the error.
305 */
306 gst_discoverer_signals[SIGNAL_DISCOVERED] =
307 g_signal_new ("discovered", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
308 G_STRUCT_OFFSET (GstDiscovererClass, discovered), NULL, NULL, NULL,
309 G_TYPE_NONE, 2, GST_TYPE_DISCOVERER_INFO,
310 G_TYPE_ERROR | G_SIGNAL_TYPE_STATIC_SCOPE);
311
312 /**
313 * GstDiscoverer::source-setup:
314 * @discoverer: the #GstDiscoverer
315 * @source: source element
316 *
317 * This signal is emitted after the source element has been created for, so
318 * the URI being discovered, so it can be configured by setting additional
319 * properties (e.g. set a proxy server for an http source, or set the device
320 * and read speed for an audio cd source).
321 *
322 * This signal is usually emitted from the context of a GStreamer streaming
323 * thread.
324 */
325 gst_discoverer_signals[SIGNAL_SOURCE_SETUP] =
326 g_signal_new ("source-setup", G_TYPE_FROM_CLASS (klass),
327 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDiscovererClass, source_setup),
328 NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
329 }
330
331 static void
uridecodebin_element_added_cb(GstElement * uridecodebin,GstElement * child,GstDiscoverer * dc)332 uridecodebin_element_added_cb (GstElement * uridecodebin,
333 GstElement * child, GstDiscoverer * dc)
334 {
335 GST_DEBUG ("New element added to uridecodebin : %s",
336 GST_ELEMENT_NAME (child));
337
338 if (G_OBJECT_TYPE (child) == dc->priv->decodebin_type) {
339 g_object_set (child, "post-stream-topology", TRUE, NULL);
340 }
341 }
342
343 static void
gst_discoverer_init(GstDiscoverer * dc)344 gst_discoverer_init (GstDiscoverer * dc)
345 {
346 GstElement *tmp;
347 GstFormat format = GST_FORMAT_TIME;
348
349 dc->priv = gst_discoverer_get_instance_private (dc);
350
351 dc->priv->timeout = DEFAULT_PROP_TIMEOUT;
352 dc->priv->use_cache = DEFAULT_PROP_USE_CACHE;
353 dc->priv->async = FALSE;
354
355 g_mutex_init (&dc->priv->lock);
356
357 dc->priv->pending_subtitle_pads = 0;
358
359 dc->priv->current_state = GST_STATE_NULL;
360 dc->priv->target_state = GST_STATE_NULL;
361 dc->priv->no_more_pads = FALSE;
362
363 dc->priv->all_tags = NULL;
364 dc->priv->global_tags = NULL;
365
366 GST_LOG ("Creating pipeline");
367 dc->priv->pipeline = (GstBin *) gst_pipeline_new ("Discoverer");
368 GST_LOG_OBJECT (dc, "Creating uridecodebin");
369 dc->priv->uridecodebin =
370 gst_element_factory_make ("uridecodebin", "discoverer-uri");
371 if (G_UNLIKELY (dc->priv->uridecodebin == NULL)) {
372 GST_ERROR ("Can't create uridecodebin");
373 return;
374 }
375 GST_LOG_OBJECT (dc, "Adding uridecodebin to pipeline");
376 gst_bin_add (dc->priv->pipeline, dc->priv->uridecodebin);
377
378 dc->priv->pad_added_id =
379 g_signal_connect_object (dc->priv->uridecodebin, "pad-added",
380 G_CALLBACK (uridecodebin_pad_added_cb), dc, 0);
381 dc->priv->pad_remove_id =
382 g_signal_connect_object (dc->priv->uridecodebin, "pad-removed",
383 G_CALLBACK (uridecodebin_pad_removed_cb), dc, 0);
384 dc->priv->no_more_pads_id =
385 g_signal_connect_object (dc->priv->uridecodebin, "no-more-pads",
386 G_CALLBACK (uridecodebin_no_more_pads_cb), dc, 0);
387 dc->priv->source_chg_id =
388 g_signal_connect_object (dc->priv->uridecodebin, "notify::source",
389 G_CALLBACK (uridecodebin_source_changed_cb), dc, 0);
390
391 GST_LOG_OBJECT (dc, "Getting pipeline bus");
392 dc->priv->bus = gst_pipeline_get_bus ((GstPipeline *) dc->priv->pipeline);
393
394 dc->priv->bus_cb_id =
395 g_signal_connect_object (dc->priv->bus, "message",
396 G_CALLBACK (discoverer_bus_cb), dc, 0);
397
398 GST_DEBUG_OBJECT (dc, "Done initializing Discoverer");
399
400 /* This is ugly. We get the GType of decodebin so we can quickly detect
401 * when a decodebin is added to uridecodebin so we can set the
402 * post-stream-topology setting to TRUE */
403 dc->priv->element_added_id =
404 g_signal_connect_object (dc->priv->uridecodebin, "element-added",
405 G_CALLBACK (uridecodebin_element_added_cb), dc, 0);
406 tmp = gst_element_factory_make ("decodebin", NULL);
407 dc->priv->decodebin_type = G_OBJECT_TYPE (tmp);
408 gst_object_unref (tmp);
409
410 /* create queries */
411 dc->priv->seeking_query = gst_query_new_seeking (format);
412 }
413
414 static void
discoverer_reset(GstDiscoverer * dc)415 discoverer_reset (GstDiscoverer * dc)
416 {
417 GST_DEBUG_OBJECT (dc, "Resetting");
418
419 if (dc->priv->pending_uris) {
420 g_list_foreach (dc->priv->pending_uris, (GFunc) g_free, NULL);
421 g_list_free (dc->priv->pending_uris);
422 dc->priv->pending_uris = NULL;
423 }
424
425 if (dc->priv->pipeline)
426 gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_NULL);
427 }
428
429 #define DISCONNECT_SIGNAL(o,i) G_STMT_START{ \
430 if ((i) && g_signal_handler_is_connected ((o), (i))) \
431 g_signal_handler_disconnect ((o), (i)); \
432 (i) = 0; \
433 }G_STMT_END
434
435 static void
gst_discoverer_dispose(GObject * obj)436 gst_discoverer_dispose (GObject * obj)
437 {
438 GstDiscoverer *dc = (GstDiscoverer *) obj;
439
440 GST_DEBUG_OBJECT (dc, "Disposing");
441
442 discoverer_reset (dc);
443
444 if (G_LIKELY (dc->priv->pipeline)) {
445 /* Workaround for bug #118536 */
446 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_added_id);
447 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_remove_id);
448 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->no_more_pads_id);
449 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->source_chg_id);
450 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->element_added_id);
451 DISCONNECT_SIGNAL (dc->priv->bus, dc->priv->bus_cb_id);
452
453 /* pipeline was set to NULL in _reset */
454 gst_object_unref (dc->priv->pipeline);
455 if (dc->priv->bus)
456 gst_object_unref (dc->priv->bus);
457
458 dc->priv->pipeline = NULL;
459 dc->priv->uridecodebin = NULL;
460 dc->priv->bus = NULL;
461 }
462
463 gst_discoverer_stop (dc);
464
465 if (dc->priv->seeking_query) {
466 gst_query_unref (dc->priv->seeking_query);
467 dc->priv->seeking_query = NULL;
468 }
469
470 G_OBJECT_CLASS (gst_discoverer_parent_class)->dispose (obj);
471 }
472
473 static void
gst_discoverer_finalize(GObject * obj)474 gst_discoverer_finalize (GObject * obj)
475 {
476 GstDiscoverer *dc = (GstDiscoverer *) obj;
477
478 g_mutex_clear (&dc->priv->lock);
479
480 G_OBJECT_CLASS (gst_discoverer_parent_class)->finalize (obj);
481 }
482
483 static void
gst_discoverer_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)484 gst_discoverer_set_property (GObject * object, guint prop_id,
485 const GValue * value, GParamSpec * pspec)
486 {
487 GstDiscoverer *dc = (GstDiscoverer *) object;
488
489 switch (prop_id) {
490 case PROP_TIMEOUT:
491 gst_discoverer_set_timeout (dc, g_value_get_uint64 (value));
492 break;
493 case PROP_USE_CACHE:
494 DISCO_LOCK (dc);
495 dc->priv->use_cache = g_value_get_boolean (value);
496 DISCO_UNLOCK (dc);
497 break;
498 default:
499 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
500 break;
501 }
502 }
503
504 static void
gst_discoverer_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)505 gst_discoverer_get_property (GObject * object, guint prop_id,
506 GValue * value, GParamSpec * pspec)
507 {
508 GstDiscoverer *dc = (GstDiscoverer *) object;
509
510 switch (prop_id) {
511 case PROP_TIMEOUT:
512 DISCO_LOCK (dc);
513 g_value_set_uint64 (value, dc->priv->timeout);
514 DISCO_UNLOCK (dc);
515 break;
516 case PROP_USE_CACHE:
517 DISCO_LOCK (dc);
518 g_value_set_boolean (value, dc->priv->use_cache);
519 DISCO_UNLOCK (dc);
520 break;
521 default:
522 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
523 break;
524 }
525 }
526
527 static void
gst_discoverer_set_timeout(GstDiscoverer * dc,GstClockTime timeout)528 gst_discoverer_set_timeout (GstDiscoverer * dc, GstClockTime timeout)
529 {
530 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (timeout));
531
532 GST_DEBUG_OBJECT (dc, "timeout : %" GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
533
534 /* FIXME : update current pending timeout if we're running */
535 DISCO_LOCK (dc);
536 dc->priv->timeout = timeout;
537 DISCO_UNLOCK (dc);
538 }
539
540 static GstPadProbeReturn
_event_probe(GstPad * pad,GstPadProbeInfo * info,PrivateStream * ps)541 _event_probe (GstPad * pad, GstPadProbeInfo * info, PrivateStream * ps)
542 {
543 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
544
545 switch (GST_EVENT_TYPE (event)) {
546 case GST_EVENT_TOC:{
547 GstToc *tmp;
548
549 gst_event_parse_toc (event, &tmp, NULL);
550 GST_DEBUG_OBJECT (pad, "toc %" GST_PTR_FORMAT, tmp);
551 DISCO_LOCK (ps->dc);
552 ps->toc = tmp;
553 if (G_LIKELY (ps->dc->priv->processing)) {
554 GST_DEBUG_OBJECT (pad, "private stream %p toc %" GST_PTR_FORMAT, ps,
555 tmp);
556 } else
557 GST_DEBUG_OBJECT (pad, "Dropping toc since preroll is done");
558 DISCO_UNLOCK (ps->dc);
559 break;
560 }
561 case GST_EVENT_STREAM_START:{
562 const gchar *stream_id;
563
564 gst_event_parse_stream_start (event, &stream_id);
565
566 g_free (ps->stream_id);
567 ps->stream_id = stream_id ? g_strdup (stream_id) : NULL;
568 break;
569 }
570 default:
571 break;
572 }
573
574 return GST_PAD_PROBE_OK;
575 }
576
577 static GstStaticCaps subtitle_caps =
578 GST_STATIC_CAPS
579 ("application/x-ssa; application/x-ass; application/x-kate");
580
581 static gboolean
is_subtitle_caps(const GstCaps * caps)582 is_subtitle_caps (const GstCaps * caps)
583 {
584 GstCaps *subs_caps;
585 GstStructure *s;
586 const gchar *name;
587 gboolean ret;
588
589 s = gst_caps_get_structure (caps, 0);
590 if (!s)
591 return FALSE;
592
593 name = gst_structure_get_name (s);
594 if (g_str_has_prefix (name, "text/") ||
595 g_str_has_prefix (name, "subpicture/") ||
596 g_str_has_prefix (name, "subtitle/") ||
597 g_str_has_prefix (name, "closedcaption/") ||
598 g_str_has_prefix (name, "application/x-subtitle"))
599 return TRUE;
600
601 subs_caps = gst_static_caps_get (&subtitle_caps);
602 ret = gst_caps_can_intersect (caps, subs_caps);
603 gst_caps_unref (subs_caps);
604
605 return ret;
606 }
607
608 static GstPadProbeReturn
got_subtitle_data(GstPad * pad,GstPadProbeInfo * info,GstDiscoverer * dc)609 got_subtitle_data (GstPad * pad, GstPadProbeInfo * info, GstDiscoverer * dc)
610 {
611 GstMessage *msg;
612
613 if (!(GST_IS_BUFFER (info->data) || (GST_IS_EVENT (info->data)
614 && (GST_EVENT_TYPE ((GstEvent *) info->data) == GST_EVENT_GAP
615 || GST_EVENT_TYPE ((GstEvent *) info->data) ==
616 GST_EVENT_EOS))))
617 return GST_PAD_PROBE_OK;
618
619
620 DISCO_LOCK (dc);
621
622 dc->priv->pending_subtitle_pads--;
623
624 msg = gst_message_new_application (NULL,
625 gst_structure_new_empty ("DiscovererDone"));
626 gst_element_post_message ((GstElement *) dc->priv->pipeline, msg);
627
628 DISCO_UNLOCK (dc);
629
630 return GST_PAD_PROBE_REMOVE;
631
632 }
633
634 static void
uridecodebin_source_changed_cb(GstElement * uridecodebin,GParamSpec * pspec,GstDiscoverer * dc)635 uridecodebin_source_changed_cb (GstElement * uridecodebin,
636 GParamSpec * pspec, GstDiscoverer * dc)
637 {
638 GstElement *src;
639 /* get a handle to the source */
640 g_object_get (uridecodebin, pspec->name, &src, NULL);
641
642 GST_DEBUG_OBJECT (dc, "got a new source %p", src);
643
644 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_SOURCE_SETUP], 0, src);
645 gst_object_unref (src);
646 }
647
648 static void
uridecodebin_pad_added_cb(GstElement * uridecodebin,GstPad * pad,GstDiscoverer * dc)649 uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
650 GstDiscoverer * dc)
651 {
652 PrivateStream *ps;
653 GstPad *sinkpad = NULL;
654 GstCaps *caps;
655 gchar *padname;
656 gchar *tmpname;
657
658 GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
659
660 DISCO_LOCK (dc);
661 if (dc->priv->cleanup) {
662 GST_WARNING_OBJECT (dc, "Cleanup, not adding pad");
663 DISCO_UNLOCK (dc);
664 return;
665 }
666 if (dc->priv->current_error) {
667 GST_WARNING_OBJECT (dc, "Ongoing error, not adding more pads");
668 DISCO_UNLOCK (dc);
669 return;
670 }
671 ps = g_slice_new0 (PrivateStream);
672
673 ps->dc = dc;
674 ps->pad = pad;
675 padname = gst_pad_get_name (pad);
676 tmpname = g_strdup_printf ("discoverer-queue-%s", padname);
677 ps->queue = gst_element_factory_make ("queue", tmpname);
678 g_free (tmpname);
679 tmpname = g_strdup_printf ("discoverer-sink-%s", padname);
680 ps->sink = gst_element_factory_make ("fakesink", tmpname);
681 g_free (tmpname);
682 g_free (padname);
683
684 if (G_UNLIKELY (ps->queue == NULL || ps->sink == NULL))
685 goto error;
686
687 g_object_set (ps->sink, "silent", TRUE, NULL);
688 g_object_set (ps->queue, "max-size-buffers", 1, "silent", TRUE, NULL);
689
690 sinkpad = gst_element_get_static_pad (ps->queue, "sink");
691 if (sinkpad == NULL)
692 goto error;
693
694 caps = gst_pad_get_current_caps (pad);
695 if (!caps) {
696 GST_WARNING ("Couldn't get negotiated caps from %s:%s",
697 GST_DEBUG_PAD_NAME (pad));
698 caps = gst_pad_query_caps (pad, NULL);
699 }
700
701 if (caps && !gst_caps_is_empty (caps) && !gst_caps_is_any (caps)
702 && is_subtitle_caps (caps)) {
703 /* Subtitle streams are sparse and may not provide any information - don't
704 * wait for data to preroll */
705 ps->probe_id =
706 gst_pad_add_probe (sinkpad, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
707 (GstPadProbeCallback) got_subtitle_data, dc, NULL);
708 g_object_set (ps->sink, "async", FALSE, NULL);
709 dc->priv->pending_subtitle_pads++;
710 }
711
712 if (caps)
713 gst_caps_unref (caps);
714
715 gst_bin_add_many (dc->priv->pipeline, ps->queue, ps->sink, NULL);
716
717 if (!gst_element_link_pads_full (ps->queue, "src", ps->sink, "sink",
718 GST_PAD_LINK_CHECK_NOTHING))
719 goto error;
720 if (!gst_element_sync_state_with_parent (ps->sink))
721 goto error;
722 if (!gst_element_sync_state_with_parent (ps->queue))
723 goto error;
724
725 if (gst_pad_link_full (pad, sinkpad,
726 GST_PAD_LINK_CHECK_NOTHING) != GST_PAD_LINK_OK)
727 goto error;
728 gst_object_unref (sinkpad);
729
730 /* Add an event probe */
731 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
732 (GstPadProbeCallback) _event_probe, ps, NULL);
733
734 dc->priv->streams = g_list_append (dc->priv->streams, ps);
735 DISCO_UNLOCK (dc);
736
737 GST_DEBUG_OBJECT (dc, "Done handling pad");
738
739 return;
740
741 error:
742 GST_ERROR_OBJECT (dc, "Error while handling pad");
743 if (sinkpad)
744 gst_object_unref (sinkpad);
745 if (ps->queue)
746 gst_object_unref (ps->queue);
747 if (ps->sink)
748 gst_object_unref (ps->sink);
749 g_slice_free (PrivateStream, ps);
750 DISCO_UNLOCK (dc);
751 return;
752 }
753
754 static void
uridecodebin_no_more_pads_cb(GstElement * uridecodebin,GstDiscoverer * dc)755 uridecodebin_no_more_pads_cb (GstElement * uridecodebin, GstDiscoverer * dc)
756 {
757 GstMessage *msg = gst_message_new_application (NULL,
758 gst_structure_new_empty ("DiscovererDone"));
759
760 DISCO_LOCK (dc);
761 dc->priv->no_more_pads = TRUE;
762 gst_element_post_message ((GstElement *) dc->priv->pipeline, msg);
763 DISCO_UNLOCK (dc);
764 }
765
766 static void
uridecodebin_pad_removed_cb(GstElement * uridecodebin,GstPad * pad,GstDiscoverer * dc)767 uridecodebin_pad_removed_cb (GstElement * uridecodebin, GstPad * pad,
768 GstDiscoverer * dc)
769 {
770 GList *tmp;
771 PrivateStream *ps;
772 GstPad *sinkpad;
773
774 GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
775
776 /* Find the PrivateStream */
777 DISCO_LOCK (dc);
778 for (tmp = dc->priv->streams; tmp; tmp = tmp->next) {
779 ps = (PrivateStream *) tmp->data;
780 if (ps->pad == pad)
781 break;
782 }
783
784 if (tmp == NULL) {
785 DISCO_UNLOCK (dc);
786 GST_DEBUG ("The removed pad wasn't controlled by us !");
787 return;
788 }
789
790 if (ps->probe_id)
791 gst_pad_remove_probe (pad, ps->probe_id);
792
793 dc->priv->streams = g_list_delete_link (dc->priv->streams, tmp);
794
795 gst_element_set_state (ps->sink, GST_STATE_NULL);
796 gst_element_set_state (ps->queue, GST_STATE_NULL);
797 gst_element_unlink (ps->queue, ps->sink);
798
799 sinkpad = gst_element_get_static_pad (ps->queue, "sink");
800 gst_pad_unlink (pad, sinkpad);
801 gst_object_unref (sinkpad);
802
803 /* references removed here */
804 gst_bin_remove_many (dc->priv->pipeline, ps->sink, ps->queue, NULL);
805
806 DISCO_UNLOCK (dc);
807 if (ps->tags) {
808 gst_tag_list_unref (ps->tags);
809 }
810 if (ps->toc) {
811 gst_toc_unref (ps->toc);
812 }
813 g_free (ps->stream_id);
814
815 g_slice_free (PrivateStream, ps);
816
817 GST_DEBUG ("Done handling pad");
818 }
819
820 static GstStructure *
collect_stream_information(GstDiscoverer * dc,PrivateStream * ps,guint idx)821 collect_stream_information (GstDiscoverer * dc, PrivateStream * ps, guint idx)
822 {
823 GstCaps *caps;
824 GstStructure *st;
825 gchar *stname;
826
827 stname = g_strdup_printf ("stream-%02d", idx);
828 st = gst_structure_new_empty (stname);
829 g_free (stname);
830
831 /* Get caps */
832 caps = gst_pad_get_current_caps (ps->pad);
833 if (!caps) {
834 GST_WARNING ("Couldn't get negotiated caps from %s:%s",
835 GST_DEBUG_PAD_NAME (ps->pad));
836 caps = gst_pad_query_caps (ps->pad, NULL);
837 }
838 if (caps) {
839 GST_DEBUG ("stream-%02d, got caps %" GST_PTR_FORMAT, idx, caps);
840 gst_structure_id_set (st, _CAPS_QUARK, GST_TYPE_CAPS, caps, NULL);
841 gst_caps_unref (caps);
842 }
843 if (ps->tags)
844 gst_structure_id_set (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, ps->tags, NULL);
845 if (ps->toc)
846 gst_structure_id_set (st, _TOC_QUARK, GST_TYPE_TOC, ps->toc, NULL);
847 if (ps->stream_id)
848 gst_structure_id_set (st, _STREAM_ID_QUARK, G_TYPE_STRING, ps->stream_id,
849 NULL);
850
851 return st;
852 }
853
854 /* takes ownership of new_tags, may replace *taglist with a new one */
855 static void
gst_discoverer_merge_and_replace_tags(GstTagList ** taglist,GstTagList * new_tags)856 gst_discoverer_merge_and_replace_tags (GstTagList ** taglist,
857 GstTagList * new_tags)
858 {
859 if (new_tags == NULL)
860 return;
861
862 if (*taglist == NULL) {
863 *taglist = new_tags;
864 return;
865 }
866
867 gst_tag_list_insert (*taglist, new_tags, GST_TAG_MERGE_REPLACE);
868 gst_tag_list_unref (new_tags);
869 }
870
871 static void
collect_common_information(GstDiscovererStreamInfo * info,const GstStructure * st)872 collect_common_information (GstDiscovererStreamInfo * info,
873 const GstStructure * st)
874 {
875 if (gst_structure_id_has_field (st, _TOC_QUARK)) {
876 gst_structure_id_get (st, _TOC_QUARK, GST_TYPE_TOC, &info->toc, NULL);
877 }
878
879 if (gst_structure_id_has_field (st, _STREAM_ID_QUARK)) {
880 gst_structure_id_get (st, _STREAM_ID_QUARK, G_TYPE_STRING, &info->stream_id,
881 NULL);
882 }
883 }
884
885 static GstDiscovererStreamInfo *
make_info(GstDiscovererStreamInfo * parent,GType type,GstCaps * caps)886 make_info (GstDiscovererStreamInfo * parent, GType type, GstCaps * caps)
887 {
888 GstDiscovererStreamInfo *info;
889
890 if (parent)
891 info = gst_discoverer_stream_info_ref (parent);
892 else {
893 info = g_object_new (type, NULL);
894 if (caps)
895 info->caps = gst_caps_ref (caps);
896 }
897 return info;
898 }
899
900 /* Parses a set of caps and tags in st and populates a GstDiscovererStreamInfo
901 * structure (parent, if !NULL, otherwise it allocates one)
902 */
903 static GstDiscovererStreamInfo *
collect_information(GstDiscoverer * dc,const GstStructure * st,GstDiscovererStreamInfo * parent)904 collect_information (GstDiscoverer * dc, const GstStructure * st,
905 GstDiscovererStreamInfo * parent)
906 {
907 GstPad *srcpad;
908 GstCaps *caps = NULL;
909 GstStructure *caps_st;
910 GstTagList *tags_st;
911 const gchar *name;
912 gint tmp, tmp2;
913 guint utmp;
914
915 if (!st || (!gst_structure_id_has_field (st, _CAPS_QUARK)
916 && !gst_structure_id_has_field (st, _ELEMENT_SRCPAD_QUARK))) {
917 GST_WARNING ("Couldn't find caps !");
918 return make_info (parent, GST_TYPE_DISCOVERER_STREAM_INFO, NULL);
919 }
920
921 if (gst_structure_id_get (st, _ELEMENT_SRCPAD_QUARK, GST_TYPE_PAD, &srcpad,
922 NULL)) {
923 caps = gst_pad_get_current_caps (srcpad);
924 gst_object_unref (srcpad);
925 }
926 if (!caps) {
927 gst_structure_id_get (st, _CAPS_QUARK, GST_TYPE_CAPS, &caps, NULL);
928 }
929
930 if (!caps || gst_caps_is_empty (caps) || gst_caps_is_any (caps)) {
931 GST_WARNING ("Couldn't find caps !");
932 if (caps)
933 gst_caps_unref (caps);
934 return make_info (parent, GST_TYPE_DISCOVERER_STREAM_INFO, NULL);
935 }
936
937 caps_st = gst_caps_get_structure (caps, 0);
938 name = gst_structure_get_name (caps_st);
939
940 if (g_str_has_prefix (name, "audio/")) {
941 GstDiscovererAudioInfo *info;
942 const gchar *format_str;
943 guint64 channel_mask;
944
945 info = (GstDiscovererAudioInfo *) make_info (parent,
946 GST_TYPE_DISCOVERER_AUDIO_INFO, caps);
947
948 if (gst_structure_get_int (caps_st, "rate", &tmp))
949 info->sample_rate = (guint) tmp;
950
951 if (gst_structure_get_int (caps_st, "channels", &tmp))
952 info->channels = (guint) tmp;
953
954 if (gst_structure_get (caps_st, "channel-mask", GST_TYPE_BITMASK,
955 &channel_mask, NULL)) {
956 info->channel_mask = channel_mask;
957 } else if (info->channels) {
958 info->channel_mask = gst_audio_channel_get_fallback_mask (info->channels);
959 }
960
961 /* FIXME: we only want to extract depth if raw audio is what's in the
962 * container (i.e. not if there is a decoder involved) */
963 format_str = gst_structure_get_string (caps_st, "format");
964 if (format_str != NULL) {
965 const GstAudioFormatInfo *finfo;
966 GstAudioFormat format;
967
968 format = gst_audio_format_from_string (format_str);
969 finfo = gst_audio_format_get_info (format);
970 if (finfo)
971 info->depth = GST_AUDIO_FORMAT_INFO_DEPTH (finfo);
972 }
973
974 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
975 gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st, NULL);
976 if (gst_tag_list_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
977 gst_tag_list_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
978 info->bitrate = utmp;
979
980 if (gst_tag_list_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
981 info->max_bitrate = utmp;
982
983 /* FIXME: Is it worth it to remove the tags we've parsed? */
984 gst_discoverer_merge_and_replace_tags (&info->parent.tags, tags_st);
985 }
986
987 collect_common_information (&info->parent, st);
988
989 if (!info->language && ((GstDiscovererStreamInfo *) info)->tags) {
990 gchar *language;
991 if (gst_tag_list_get_string (((GstDiscovererStreamInfo *) info)->tags,
992 GST_TAG_LANGUAGE_CODE, &language)) {
993 info->language = language;
994 }
995 }
996
997 gst_caps_unref (caps);
998 return (GstDiscovererStreamInfo *) info;
999
1000 } else if (g_str_has_prefix (name, "video/") ||
1001 g_str_has_prefix (name, "image/")) {
1002 GstDiscovererVideoInfo *info;
1003 const gchar *caps_str;
1004
1005 info = (GstDiscovererVideoInfo *) make_info (parent,
1006 GST_TYPE_DISCOVERER_VIDEO_INFO, caps);
1007
1008 if (gst_structure_get_int (caps_st, "width", &tmp))
1009 info->width = (guint) tmp;
1010 if (gst_structure_get_int (caps_st, "height", &tmp))
1011 info->height = (guint) tmp;
1012
1013 if (gst_structure_get_fraction (caps_st, "framerate", &tmp, &tmp2)) {
1014 info->framerate_num = (guint) tmp;
1015 info->framerate_denom = (guint) tmp2;
1016 } else {
1017 info->framerate_num = 0;
1018 info->framerate_denom = 1;
1019 }
1020
1021 if (gst_structure_get_fraction (caps_st, "pixel-aspect-ratio", &tmp, &tmp2)) {
1022 info->par_num = (guint) tmp;
1023 info->par_denom = (guint) tmp2;
1024 } else {
1025 info->par_num = 1;
1026 info->par_denom = 1;
1027 }
1028
1029 /* FIXME: we only want to extract depth if raw video is what's in the
1030 * container (i.e. not if there is a decoder involved) */
1031 caps_str = gst_structure_get_string (caps_st, "format");
1032 if (caps_str != NULL) {
1033 const GstVideoFormatInfo *finfo;
1034 GstVideoFormat format;
1035
1036 format = gst_video_format_from_string (caps_str);
1037 finfo = gst_video_format_get_info (format);
1038 if (finfo)
1039 info->depth = finfo->bits * finfo->n_components;
1040 }
1041
1042 caps_str = gst_structure_get_string (caps_st, "interlace-mode");
1043 if (!caps_str || strcmp (caps_str, "progressive") == 0)
1044 info->interlaced = FALSE;
1045 else
1046 info->interlaced = TRUE;
1047
1048 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
1049 gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st, NULL);
1050 if (gst_tag_list_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
1051 gst_tag_list_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
1052 info->bitrate = utmp;
1053
1054 if (gst_tag_list_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
1055 info->max_bitrate = utmp;
1056
1057 /* FIXME: Is it worth it to remove the tags we've parsed? */
1058 gst_discoverer_merge_and_replace_tags (&info->parent.tags, tags_st);
1059 }
1060
1061 collect_common_information (&info->parent, st);
1062
1063 gst_caps_unref (caps);
1064 return (GstDiscovererStreamInfo *) info;
1065
1066 } else if (is_subtitle_caps (caps)) {
1067 GstDiscovererSubtitleInfo *info;
1068
1069 info = (GstDiscovererSubtitleInfo *) make_info (parent,
1070 GST_TYPE_DISCOVERER_SUBTITLE_INFO, caps);
1071
1072 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
1073 const gchar *language;
1074
1075 gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st, NULL);
1076
1077 language = gst_structure_get_string (caps_st, GST_TAG_LANGUAGE_CODE);
1078 if (language)
1079 info->language = g_strdup (language);
1080
1081 /* FIXME: Is it worth it to remove the tags we've parsed? */
1082 gst_discoverer_merge_and_replace_tags (&info->parent.tags, tags_st);
1083 }
1084
1085 collect_common_information (&info->parent, st);
1086
1087 if (!info->language && ((GstDiscovererStreamInfo *) info)->tags) {
1088 gchar *language;
1089 if (gst_tag_list_get_string (((GstDiscovererStreamInfo *) info)->tags,
1090 GST_TAG_LANGUAGE_CODE, &language)) {
1091 info->language = language;
1092 }
1093 }
1094
1095 gst_caps_unref (caps);
1096 return (GstDiscovererStreamInfo *) info;
1097
1098 } else {
1099 /* None of the above - populate what information we can */
1100 GstDiscovererStreamInfo *info;
1101
1102 info = make_info (parent, GST_TYPE_DISCOVERER_STREAM_INFO, caps);
1103
1104 if (gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st,
1105 NULL)) {
1106 gst_discoverer_merge_and_replace_tags (&info->tags, tags_st);
1107 }
1108
1109 collect_common_information (info, st);
1110
1111 gst_caps_unref (caps);
1112 return info;
1113 }
1114
1115 }
1116
1117 static GstStructure *
find_stream_for_node(GstDiscoverer * dc,const GstStructure * topology)1118 find_stream_for_node (GstDiscoverer * dc, const GstStructure * topology)
1119 {
1120 GstPad *pad;
1121 GstPad *target_pad = NULL;
1122 GstStructure *st = NULL;
1123 PrivateStream *ps;
1124 guint i;
1125 GList *tmp;
1126
1127 if (!dc->priv->streams) {
1128 return NULL;
1129 }
1130
1131 if (!gst_structure_id_has_field (topology, _TOPOLOGY_PAD_QUARK)) {
1132 GST_DEBUG ("Could not find pad for node %" GST_PTR_FORMAT, topology);
1133 return NULL;
1134 }
1135
1136 gst_structure_id_get (topology, _TOPOLOGY_PAD_QUARK,
1137 GST_TYPE_PAD, &pad, NULL);
1138
1139 for (i = 0, tmp = dc->priv->streams; tmp; tmp = tmp->next, i++) {
1140 ps = (PrivateStream *) tmp->data;
1141
1142 target_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (ps->pad));
1143 if (target_pad == NULL)
1144 continue;
1145 gst_object_unref (target_pad);
1146
1147 if (target_pad == pad)
1148 break;
1149 }
1150
1151 if (tmp)
1152 st = collect_stream_information (dc, ps, i);
1153
1154 gst_object_unref (pad);
1155
1156 return st;
1157 }
1158
1159 /* this can fail due to {framed,parsed}={TRUE,FALSE} differences, thus we filter
1160 * the parent */
1161 static gboolean
child_is_same_stream(const GstCaps * _parent,const GstCaps * child)1162 child_is_same_stream (const GstCaps * _parent, const GstCaps * child)
1163 {
1164 GstCaps *parent;
1165 gboolean res;
1166
1167 if (_parent == child)
1168 return TRUE;
1169 if (!_parent)
1170 return FALSE;
1171 if (!child)
1172 return FALSE;
1173
1174 parent = copy_and_clean_caps (_parent);
1175 res = gst_caps_can_intersect (parent, child);
1176 gst_caps_unref (parent);
1177 return res;
1178 }
1179
1180
1181 static gboolean
child_is_raw_stream(const GstCaps * parent,const GstCaps * child)1182 child_is_raw_stream (const GstCaps * parent, const GstCaps * child)
1183 {
1184 const GstStructure *st1, *st2;
1185 const gchar *name1, *name2;
1186
1187 if (parent == child)
1188 return TRUE;
1189 if (!parent)
1190 return FALSE;
1191 if (!child)
1192 return FALSE;
1193
1194 st1 = gst_caps_get_structure (parent, 0);
1195 name1 = gst_structure_get_name (st1);
1196 st2 = gst_caps_get_structure (child, 0);
1197 name2 = gst_structure_get_name (st2);
1198
1199 if ((g_str_has_prefix (name1, "audio/") &&
1200 g_str_has_prefix (name2, "audio/x-raw")) ||
1201 ((g_str_has_prefix (name1, "video/") ||
1202 g_str_has_prefix (name1, "image/")) &&
1203 g_str_has_prefix (name2, "video/x-raw"))) {
1204 /* child is the "raw" sub-stream corresponding to parent */
1205 return TRUE;
1206 }
1207
1208 if (is_subtitle_caps (parent))
1209 return TRUE;
1210
1211 return FALSE;
1212 }
1213
1214 /* If a parent is non-NULL, collected stream information will be appended to it
1215 * (and where the information exists, it will be overridden)
1216 */
1217 static GstDiscovererStreamInfo *
parse_stream_topology(GstDiscoverer * dc,const GstStructure * topology,GstDiscovererStreamInfo * parent)1218 parse_stream_topology (GstDiscoverer * dc, const GstStructure * topology,
1219 GstDiscovererStreamInfo * parent)
1220 {
1221 GstDiscovererStreamInfo *res = NULL;
1222 GstCaps *caps = NULL;
1223 const GValue *nval = NULL;
1224
1225 GST_DEBUG ("parsing: %" GST_PTR_FORMAT, topology);
1226
1227 nval = gst_structure_get_value (topology, "next");
1228
1229 if (nval == NULL || GST_VALUE_HOLDS_STRUCTURE (nval)) {
1230 GstStructure *st = find_stream_for_node (dc, topology);
1231 gboolean add_to_list = TRUE;
1232
1233 if (st) {
1234 res = collect_information (dc, st, parent);
1235 gst_structure_free (st);
1236 } else {
1237 /* Didn't find a stream structure, so let's just use the caps we have */
1238 res = collect_information (dc, topology, parent);
1239 }
1240
1241 if (nval == NULL) {
1242 /* FIXME : aggregate with information from main streams */
1243 GST_DEBUG ("Couldn't find 'next' ! might be the last entry");
1244 } else {
1245 GstPad *srcpad;
1246
1247 st = (GstStructure *) gst_value_get_structure (nval);
1248
1249 GST_DEBUG ("next is a structure %" GST_PTR_FORMAT, st);
1250
1251 if (!parent)
1252 parent = res;
1253
1254 if (gst_structure_id_get (st, _ELEMENT_SRCPAD_QUARK, GST_TYPE_PAD,
1255 &srcpad, NULL)) {
1256 caps = gst_pad_get_current_caps (srcpad);
1257 gst_object_unref (srcpad);
1258 }
1259 if (!caps) {
1260 gst_structure_id_get (st, _CAPS_QUARK, GST_TYPE_CAPS, &caps, NULL);
1261 }
1262
1263 if (caps) {
1264 if (child_is_same_stream (parent->caps, caps)) {
1265 /* We sometimes get an extra sub-stream from the parser. If this is
1266 * the case, we just replace the parent caps with this stream's caps
1267 * since they might contain more information */
1268 gst_caps_replace (&parent->caps, caps);
1269
1270 parse_stream_topology (dc, st, parent);
1271 add_to_list = FALSE;
1272 } else if (child_is_raw_stream (parent->caps, caps)) {
1273 /* This is the "raw" stream corresponding to the parent. This
1274 * contains more information than the parent, tags etc. */
1275 parse_stream_topology (dc, st, parent);
1276 add_to_list = FALSE;
1277 } else {
1278 GstDiscovererStreamInfo *next = parse_stream_topology (dc, st, NULL);
1279 res->next = next;
1280 next->previous = res;
1281 }
1282 gst_caps_unref (caps);
1283 }
1284 }
1285
1286 if (add_to_list) {
1287 res->stream_number = dc->priv->current_info->stream_count++;
1288 dc->priv->current_info->stream_list =
1289 g_list_append (dc->priv->current_info->stream_list, res);
1290 } else {
1291 gst_discoverer_stream_info_unref (res);
1292 }
1293
1294 } else if (GST_VALUE_HOLDS_LIST (nval)) {
1295 guint i, len;
1296 GstDiscovererContainerInfo *cont;
1297 GstPad *srcpad;
1298
1299 if (gst_structure_id_get (topology, _ELEMENT_SRCPAD_QUARK, GST_TYPE_PAD,
1300 &srcpad, NULL)) {
1301 caps = gst_pad_get_current_caps (srcpad);
1302 gst_object_unref (srcpad);
1303 }
1304 if (!caps) {
1305 gst_structure_id_get (topology, _CAPS_QUARK, GST_TYPE_CAPS, &caps, NULL);
1306 }
1307
1308 if (!caps)
1309 GST_WARNING ("Couldn't find caps !");
1310
1311 len = gst_value_list_get_size (nval);
1312 GST_DEBUG ("next is a list of %d entries", len);
1313
1314 cont = (GstDiscovererContainerInfo *)
1315 g_object_new (GST_TYPE_DISCOVERER_CONTAINER_INFO, NULL);
1316 cont->parent.caps = caps;
1317 if (dc->priv->global_tags)
1318 cont->tags = gst_tag_list_ref (dc->priv->global_tags);
1319 res = (GstDiscovererStreamInfo *) cont;
1320
1321 for (i = 0; i < len; i++) {
1322 const GValue *subv = gst_value_list_get_value (nval, i);
1323 const GstStructure *subst = gst_value_get_structure (subv);
1324 GstDiscovererStreamInfo *substream;
1325
1326 GST_DEBUG ("%d %" GST_PTR_FORMAT, i, subst);
1327
1328 substream = parse_stream_topology (dc, subst, NULL);
1329
1330 substream->previous = res;
1331 cont->streams =
1332 g_list_append (cont->streams,
1333 gst_discoverer_stream_info_ref (substream));
1334 }
1335 }
1336
1337 return res;
1338 }
1339
1340 /* Required DISCO_LOCK to be taken, and will release it */
1341 static void
setup_next_uri_locked(GstDiscoverer * dc)1342 setup_next_uri_locked (GstDiscoverer * dc)
1343 {
1344 if (dc->priv->pending_uris != NULL) {
1345 gboolean ready = _setup_locked (dc);
1346 DISCO_UNLOCK (dc);
1347
1348 if (!ready) {
1349 /* Start timeout */
1350 handle_current_async (dc);
1351 } else {
1352 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
1353 (GSourceFunc) emit_discovererd_and_next, gst_object_ref (dc),
1354 gst_object_unref);
1355 }
1356 } else {
1357 /* We're done ! */
1358 DISCO_UNLOCK (dc);
1359 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_FINISHED], 0);
1360 }
1361 }
1362
1363 static GstDiscovererInfo *
_ensure_info_tags(GstDiscoverer * dc)1364 _ensure_info_tags (GstDiscoverer * dc)
1365 {
1366 GstDiscovererInfo *info = dc->priv->current_info;
1367
1368 if (dc->priv->all_tags)
1369 info->tags = dc->priv->all_tags;
1370 dc->priv->all_tags = NULL;
1371 return info;
1372 }
1373
1374 static void
emit_discovererd(GstDiscoverer * dc)1375 emit_discovererd (GstDiscoverer * dc)
1376 {
1377 GstDiscovererInfo *info = _ensure_info_tags (dc);
1378 GST_DEBUG_OBJECT (dc, "Emitting 'discoverered' %s", info->uri);
1379 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_DISCOVERED], 0,
1380 info, dc->priv->current_error);
1381 /* Clients get a copy of current_info since it is a boxed type */
1382 gst_discoverer_info_unref (dc->priv->current_info);
1383 dc->priv->current_info = NULL;
1384 }
1385
1386 static gboolean
emit_discovererd_and_next(GstDiscoverer * dc)1387 emit_discovererd_and_next (GstDiscoverer * dc)
1388 {
1389 emit_discovererd (dc);
1390
1391 DISCO_LOCK (dc);
1392 setup_next_uri_locked (dc);
1393
1394 return G_SOURCE_REMOVE;
1395 }
1396
1397 /* Called when pipeline is pre-rolled */
1398 static void
discoverer_collect(GstDiscoverer * dc)1399 discoverer_collect (GstDiscoverer * dc)
1400 {
1401 GST_DEBUG ("Collecting information");
1402
1403 /* Stop the timeout handler if present */
1404 if (dc->priv->timeout_source) {
1405 g_source_destroy (dc->priv->timeout_source);
1406 g_source_unref (dc->priv->timeout_source);
1407 dc->priv->timeout_source = NULL;
1408 }
1409
1410 if (dc->priv->use_cache && dc->priv->current_info
1411 && dc->priv->current_info->from_cache) {
1412 GST_DEBUG_OBJECT (dc,
1413 "Nothing to collect as the info was built from" " the cache");
1414 return;
1415 }
1416
1417 if (dc->priv->streams) {
1418 /* FIXME : Make this querying optional */
1419 if (TRUE) {
1420 GstElement *pipeline = (GstElement *) dc->priv->pipeline;
1421 gint64 dur;
1422
1423 GST_DEBUG ("Attempting to query duration");
1424
1425 if (gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur)) {
1426 GST_DEBUG ("Got duration %" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
1427 dc->priv->current_info->duration = (guint64) dur;
1428 } else if (dc->priv->current_info->result != GST_DISCOVERER_ERROR) {
1429 GstStateChangeReturn sret;
1430 /* Note: We don't switch to PLAYING if we previously saw an ERROR since
1431 * the state of various element isn't guaranteed anymore */
1432
1433 /* Some parsers may not even return a rough estimate right away, e.g.
1434 * because they've only processed a single frame so far, so if we
1435 * didn't get a duration the first time, spin a bit and try again.
1436 * Ugly, but still better than making parsers or other elements return
1437 * completely bogus values. We need some API extensions to solve this
1438 * better. */
1439 GST_INFO ("No duration yet, try a bit harder..");
1440 /* Make sure we don't add/remove elements while switching to PLAYING itself */
1441 DISCO_LOCK (dc);
1442 sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
1443 DISCO_UNLOCK (dc);
1444 if (sret != GST_STATE_CHANGE_FAILURE) {
1445 int i;
1446
1447 for (i = 0; i < 2; ++i) {
1448 g_usleep (G_USEC_PER_SEC / 20);
1449 if (gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur)
1450 && dur > 0) {
1451 GST_DEBUG ("Got duration %" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
1452 dc->priv->current_info->duration = (guint64) dur;
1453 break;
1454 }
1455 }
1456 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1457 }
1458 }
1459
1460 if (dc->priv->seeking_query) {
1461 if (gst_element_query (pipeline, dc->priv->seeking_query)) {
1462 GstFormat format;
1463 gboolean seekable;
1464
1465 gst_query_parse_seeking (dc->priv->seeking_query, &format,
1466 &seekable, NULL, NULL);
1467 if (format == GST_FORMAT_TIME) {
1468 GST_DEBUG ("Got seekable %d", seekable);
1469 dc->priv->current_info->seekable = seekable;
1470 }
1471 }
1472 }
1473 }
1474
1475 if (dc->priv->target_state == GST_STATE_PAUSED)
1476 dc->priv->current_info->live = FALSE;
1477 else
1478 dc->priv->current_info->live = TRUE;
1479
1480 if (dc->priv->current_topology) {
1481 dc->priv->current_info->stream_count = 1;
1482 dc->priv->current_info->stream_info = parse_stream_topology (dc,
1483 dc->priv->current_topology, NULL);
1484 if (dc->priv->current_info->stream_info)
1485 dc->priv->current_info->stream_info->stream_number = 0;
1486 }
1487
1488 /*
1489 * Images need some special handling. They do not have a duration, have
1490 * caps named image/<foo> (th exception being MJPEG video which is also
1491 * type image/jpeg), and should consist of precisely one stream (actually
1492 * initially there are 2, the image and raw stream, but we squash these
1493 * while parsing the stream topology). At some point, if we find that these
1494 * conditions are not sufficient, we can count the number of decoders and
1495 * parsers in the chain, and if there's more than one decoder, or any
1496 * parser at all, we should not mark this as an image.
1497 */
1498 if (dc->priv->current_info->duration == 0 &&
1499 dc->priv->current_info->stream_info != NULL &&
1500 dc->priv->current_info->stream_info->next == NULL) {
1501 GstDiscovererStreamInfo *stream_info;
1502 GstStructure *st;
1503
1504 stream_info = dc->priv->current_info->stream_info;
1505 st = gst_caps_get_structure (stream_info->caps, 0);
1506
1507 if (g_str_has_prefix (gst_structure_get_name (st), "image/"))
1508 ((GstDiscovererVideoInfo *) stream_info)->is_image = TRUE;
1509 }
1510 }
1511
1512 if (dc->priv->use_cache && dc->priv->current_info->cachefile &&
1513 dc->priv->current_info->result == GST_DISCOVERER_OK) {
1514 GVariant *variant = gst_discoverer_info_to_variant (dc->priv->current_info,
1515 GST_DISCOVERER_SERIALIZE_ALL);
1516
1517 g_file_set_contents (dc->priv->current_info->cachefile,
1518 g_variant_get_data (variant), g_variant_get_size (variant), NULL);
1519 g_variant_unref (variant);
1520 }
1521
1522 if (dc->priv->async)
1523 emit_discovererd (dc);
1524 }
1525
1526 static void
get_async_cb(gpointer cb_data,GSource * source,GSourceFunc * func,gpointer * data)1527 get_async_cb (gpointer cb_data, GSource * source, GSourceFunc * func,
1528 gpointer * data)
1529 {
1530 *func = (GSourceFunc) async_timeout_cb;
1531 *data = cb_data;
1532 }
1533
1534 /* Wrapper since GSourceCallbackFuncs don't expect a return value from ref() */
1535 static void
_void_g_object_ref(gpointer object)1536 _void_g_object_ref (gpointer object)
1537 {
1538 g_object_ref (G_OBJECT (object));
1539 }
1540
1541 static void
handle_current_async(GstDiscoverer * dc)1542 handle_current_async (GstDiscoverer * dc)
1543 {
1544 GSource *source;
1545 static GSourceCallbackFuncs cb_funcs = {
1546 _void_g_object_ref,
1547 g_object_unref,
1548 get_async_cb,
1549 };
1550
1551 /* Attach a timeout to the main context */
1552 source = g_timeout_source_new (dc->priv->timeout / GST_MSECOND);
1553 g_source_set_callback_indirect (source, g_object_ref (dc), &cb_funcs);
1554 g_source_attach (source, dc->priv->ctx);
1555 dc->priv->timeout_source = source;
1556 }
1557
1558
1559 /* Returns TRUE if processing should stop */
1560 static gboolean
handle_message(GstDiscoverer * dc,GstMessage * msg)1561 handle_message (GstDiscoverer * dc, GstMessage * msg)
1562 {
1563 gboolean done = FALSE;
1564 const gchar *dump_name = NULL;
1565
1566 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "got a %s message",
1567 GST_MESSAGE_TYPE_NAME (msg));
1568
1569 switch (GST_MESSAGE_TYPE (msg)) {
1570 case GST_MESSAGE_ERROR:{
1571 GError *gerr;
1572 gchar *debug;
1573
1574 gst_message_parse_error (msg, &gerr, &debug);
1575 GST_WARNING_OBJECT (GST_MESSAGE_SRC (msg),
1576 "Got an error [debug:%s], [message:%s]", debug, gerr->message);
1577 dc->priv->current_error = gerr;
1578 g_free (debug);
1579
1580 /* We need to stop */
1581 done = TRUE;
1582 dump_name = "gst-discoverer-error";
1583
1584 /* Don't override missing plugin result code for missing plugin errors */
1585 if (dc->priv->current_info->result != GST_DISCOVERER_MISSING_PLUGINS ||
1586 (!g_error_matches (gerr, GST_CORE_ERROR,
1587 GST_CORE_ERROR_MISSING_PLUGIN) &&
1588 !g_error_matches (gerr, GST_STREAM_ERROR,
1589 GST_STREAM_ERROR_CODEC_NOT_FOUND))) {
1590 GST_DEBUG ("Setting result to ERROR");
1591 dc->priv->current_info->result = GST_DISCOVERER_ERROR;
1592 }
1593 }
1594 break;
1595
1596 case GST_MESSAGE_WARNING:{
1597 GError *err;
1598 gchar *debug = NULL;
1599
1600 gst_message_parse_warning (msg, &err, &debug);
1601 GST_WARNING_OBJECT (GST_MESSAGE_SRC (msg),
1602 "Got a warning [debug:%s], [message:%s]", debug, err->message);
1603 g_clear_error (&err);
1604 g_free (debug);
1605 dump_name = "gst-discoverer-warning";
1606 break;
1607 }
1608
1609 case GST_MESSAGE_EOS:
1610 GST_DEBUG ("Got EOS !");
1611 done = TRUE;
1612 dump_name = "gst-discoverer-eos";
1613 break;
1614
1615 case GST_MESSAGE_APPLICATION:{
1616 const gchar *name =
1617 gst_structure_get_name (gst_message_get_structure (msg));
1618
1619 if (g_strcmp0 (name, "DiscovererDone"))
1620 break;
1621
1622 /* Maybe we already reached the target state, and all we're waiting for
1623 * is either the subtitle tags or no_more_pads
1624 */
1625 DISCO_LOCK (dc);
1626 if (dc->priv->pending_subtitle_pads == 0)
1627 done = dc->priv->no_more_pads
1628 && dc->priv->target_state == dc->priv->current_state;
1629 DISCO_UNLOCK (dc);
1630
1631 if (done)
1632 dump_name = "gst-discoverer-application-message";
1633 }
1634 break;
1635
1636 case GST_MESSAGE_STATE_CHANGED:{
1637 GstState old, new, pending;
1638
1639 gst_message_parse_state_changed (msg, &old, &new, &pending);
1640 if (GST_MESSAGE_SRC (msg) == (GstObject *) dc->priv->pipeline) {
1641 DISCO_LOCK (dc);
1642 dc->priv->current_state = new;
1643
1644 if (dc->priv->pending_subtitle_pads == 0)
1645 done = dc->priv->no_more_pads
1646 && dc->priv->target_state == dc->priv->current_state;
1647 /* Else we should get unblocked in GST_MESSAGE_APPLICATION */
1648
1649 DISCO_UNLOCK (dc);
1650 }
1651
1652 if (done)
1653 dump_name = "gst-discoverer-target-state";
1654 }
1655 break;
1656
1657 case GST_MESSAGE_ELEMENT:
1658 {
1659 GQuark sttype;
1660 const GstStructure *structure;
1661
1662 structure = gst_message_get_structure (msg);
1663 sttype = gst_structure_get_name_id (structure);
1664 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1665 "structure %" GST_PTR_FORMAT, structure);
1666 if (sttype == _MISSING_PLUGIN_QUARK) {
1667 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1668 "Setting result to MISSING_PLUGINS");
1669 dc->priv->current_info->result = GST_DISCOVERER_MISSING_PLUGINS;
1670 /* FIXME 2.0 Remove completely the ->misc
1671 * Keep the old behaviour for now.
1672 */
1673 if (dc->priv->current_info->misc)
1674 gst_structure_free (dc->priv->current_info->misc);
1675 dc->priv->current_info->misc = gst_structure_copy (structure);
1676 g_ptr_array_add (dc->priv->current_info->missing_elements_details,
1677 gst_missing_plugin_message_get_installer_detail (msg));
1678 } else if (sttype == _STREAM_TOPOLOGY_QUARK) {
1679 if (dc->priv->current_topology)
1680 gst_structure_free (dc->priv->current_topology);
1681 dc->priv->current_topology = gst_structure_copy (structure);
1682 }
1683 }
1684 break;
1685
1686 case GST_MESSAGE_TAG:
1687 {
1688 GstTagList *tl, *tmp = NULL;
1689 GstTagScope scope;
1690
1691 gst_message_parse_tag (msg, &tl);
1692 scope = gst_tag_list_get_scope (tl);
1693 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Got tags %" GST_PTR_FORMAT, tl);
1694
1695 tmp = gst_tag_list_merge (dc->priv->all_tags, tl, GST_TAG_MERGE_APPEND);
1696 if (dc->priv->all_tags)
1697 gst_tag_list_unref (dc->priv->all_tags);
1698 dc->priv->all_tags = tmp;
1699
1700 if (scope == GST_TAG_SCOPE_STREAM) {
1701 for (GList * curr = dc->priv->streams; curr; curr = curr->next) {
1702 PrivateStream *ps = (PrivateStream *) curr->data;
1703 if (GST_MESSAGE_SRC (msg) == GST_OBJECT_CAST (ps->sink)) {
1704 tmp = gst_tag_list_merge (ps->tags, tl, GST_TAG_MERGE_APPEND);
1705 if (ps->tags)
1706 gst_tag_list_unref (ps->tags);
1707 ps->tags = tmp;
1708 GST_DEBUG_OBJECT (ps->pad, "tags %" GST_PTR_FORMAT, ps->tags);
1709 break;
1710 }
1711 }
1712 } else {
1713 tmp =
1714 gst_tag_list_merge (dc->priv->global_tags, tl,
1715 GST_TAG_MERGE_APPEND);
1716 if (dc->priv->global_tags)
1717 gst_tag_list_unref (dc->priv->global_tags);
1718 dc->priv->global_tags = tmp;
1719 }
1720 gst_tag_list_unref (tl);
1721 }
1722 break;
1723 case GST_MESSAGE_TOC:
1724 {
1725 GstToc *tmp;
1726
1727 gst_message_parse_toc (msg, &tmp, NULL);
1728 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Got toc %" GST_PTR_FORMAT, tmp);
1729 if (dc->priv->current_info->toc)
1730 gst_toc_unref (dc->priv->current_info->toc);
1731 dc->priv->current_info->toc = tmp; /* transfer ownership */
1732 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Current info %p, toc %"
1733 GST_PTR_FORMAT, dc->priv->current_info, tmp);
1734 }
1735 break;
1736
1737 default:
1738 break;
1739 }
1740
1741 if (dump_name != NULL) {
1742 /* dump graph when done or for warnings */
1743 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (dc->priv->pipeline),
1744 GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
1745 }
1746 return done;
1747 }
1748
1749 static void
handle_current_sync(GstDiscoverer * dc)1750 handle_current_sync (GstDiscoverer * dc)
1751 {
1752 GTimer *timer;
1753 gdouble deadline = ((gdouble) dc->priv->timeout) / GST_SECOND;
1754 GstMessage *msg;
1755 gboolean done = FALSE;
1756
1757 timer = g_timer_new ();
1758 g_timer_start (timer);
1759
1760 do {
1761 /* poll bus with timeout */
1762 /* FIXME : make the timeout more fine-tuned */
1763 if ((msg = gst_bus_timed_pop (dc->priv->bus, GST_SECOND / 2))) {
1764 done = handle_message (dc, msg);
1765 gst_message_unref (msg);
1766 }
1767 } while (!done && (g_timer_elapsed (timer, NULL) < deadline));
1768
1769 /* return result */
1770 if (!done) {
1771 GST_DEBUG ("we timed out! Setting result to TIMEOUT");
1772 dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
1773 }
1774
1775 DISCO_LOCK (dc);
1776 dc->priv->processing = FALSE;
1777 DISCO_UNLOCK (dc);
1778
1779
1780 GST_DEBUG ("Done");
1781
1782 g_timer_stop (timer);
1783 g_timer_destroy (timer);
1784 }
1785
1786 static gchar *
_serialized_info_get_path(GstDiscoverer * dc,gchar * uri)1787 _serialized_info_get_path (GstDiscoverer * dc, gchar * uri)
1788 {
1789 GChecksum *cs = NULL;
1790 GStatBuf file_status;
1791 gchar *location = NULL, *res = NULL, *cache_dir = NULL, *tmp = NULL,
1792 *protocol = gst_uri_get_protocol (uri), hash_dirname[3] = "00";
1793 const gchar *checksum;
1794
1795 if (g_ascii_strcasecmp (protocol, "file") != 0) {
1796 GST_DEBUG_OBJECT (dc, "Can not work with serialized DiscovererInfo"
1797 " on non local files - protocol: %s", protocol);
1798
1799 goto done;
1800 }
1801
1802 location = gst_uri_get_location (uri);
1803 if (g_stat (location, &file_status) < 0) {
1804 GST_DEBUG_OBJECT (dc, "Could not get stat for file: %s", location);
1805
1806 goto done;
1807 }
1808
1809 tmp = g_strdup_printf ("%s-%" G_GSIZE_FORMAT "-%" G_GINT64_FORMAT,
1810 location, (gsize) file_status.st_size, (gint64) file_status.st_mtime);
1811 cs = g_checksum_new (G_CHECKSUM_SHA1);
1812 g_checksum_update (cs, (const guchar *) tmp, strlen (tmp));
1813 checksum = g_checksum_get_string (cs);
1814
1815 hash_dirname[0] = checksum[0];
1816 hash_dirname[1] = checksum[1];
1817 cache_dir =
1818 g_build_filename (g_get_user_cache_dir (), "gstreamer-" GST_API_VERSION,
1819 CACHE_DIRNAME, hash_dirname, NULL);
1820 g_mkdir_with_parents (cache_dir, 0777);
1821
1822 res = g_build_filename (cache_dir, &checksum[2], NULL);
1823
1824 done:
1825 g_checksum_free (cs);
1826 g_free (cache_dir);
1827 g_free (location);
1828 g_free (tmp);
1829 g_free (protocol);
1830
1831 return res;
1832 }
1833
1834 static GstDiscovererInfo *
_get_info_from_cachefile(GstDiscoverer * dc,gchar * cachefile)1835 _get_info_from_cachefile (GstDiscoverer * dc, gchar * cachefile)
1836 {
1837 gchar *data;
1838 gsize length;
1839
1840 if (g_file_get_contents (cachefile, &data, &length, NULL)) {
1841 GstDiscovererInfo *info = NULL;
1842 GVariant *variant =
1843 g_variant_new_from_data (G_VARIANT_TYPE ("v"), data, length,
1844 TRUE, NULL, NULL);
1845
1846 info = gst_discoverer_info_from_variant (variant);
1847 g_variant_unref (variant);
1848
1849 if (info) {
1850 info->cachefile = cachefile;
1851 info->from_cache = (gpointer) 0x01;
1852 }
1853
1854 GST_INFO_OBJECT (dc, "Got info from cache: %p", info);
1855 g_free (data);
1856
1857 return info;
1858 }
1859
1860 return NULL;
1861 }
1862
1863 static gboolean
_setup_locked(GstDiscoverer * dc)1864 _setup_locked (GstDiscoverer * dc)
1865 {
1866 GstStateChangeReturn ret;
1867 gchar *uri = (gchar *) dc->priv->pending_uris->data;
1868 gchar *cachefile = NULL;
1869
1870 dc->priv->pending_uris =
1871 g_list_delete_link (dc->priv->pending_uris, dc->priv->pending_uris);
1872
1873 if (dc->priv->use_cache) {
1874 cachefile = _serialized_info_get_path (dc, uri);
1875 if (cachefile)
1876 dc->priv->current_info = _get_info_from_cachefile (dc, cachefile);
1877
1878 if (dc->priv->current_info) {
1879 /* Make sure the URI is exactly what the user passed in */
1880 g_free (dc->priv->current_info->uri);
1881 dc->priv->current_info->uri = uri;
1882
1883 dc->priv->current_info->cachefile = cachefile;
1884 dc->priv->processing = FALSE;
1885 dc->priv->target_state = GST_STATE_NULL;
1886
1887 return TRUE;
1888 }
1889 }
1890
1891 GST_DEBUG ("Setting up");
1892
1893 /* Pop URI off the pending URI list */
1894 dc->priv->current_info =
1895 (GstDiscovererInfo *) g_object_new (GST_TYPE_DISCOVERER_INFO, NULL);
1896 dc->priv->current_info->cachefile = cachefile;
1897 dc->priv->current_info->uri = uri;
1898
1899 /* set uri on uridecodebin */
1900 g_object_set (dc->priv->uridecodebin, "uri", dc->priv->current_info->uri,
1901 NULL);
1902
1903 GST_DEBUG ("Current is now %s", dc->priv->current_info->uri);
1904
1905 dc->priv->processing = TRUE;
1906
1907 dc->priv->target_state = GST_STATE_PAUSED;
1908
1909 /* set pipeline to PAUSED */
1910 DISCO_UNLOCK (dc);
1911 GST_DEBUG ("Setting pipeline to PAUSED");
1912 ret =
1913 gst_element_set_state ((GstElement *) dc->priv->pipeline,
1914 dc->priv->target_state);
1915
1916 if (ret == GST_STATE_CHANGE_NO_PREROLL) {
1917 GST_DEBUG ("Source is live, switching to PLAYING");
1918 dc->priv->target_state = GST_STATE_PLAYING;
1919 ret =
1920 gst_element_set_state ((GstElement *) dc->priv->pipeline,
1921 dc->priv->target_state);
1922 }
1923 DISCO_LOCK (dc);
1924
1925
1926 GST_DEBUG_OBJECT (dc, "Pipeline going to PAUSED : %s",
1927 gst_element_state_change_return_get_name (ret));
1928
1929 return FALSE;
1930 }
1931
1932 static void
discoverer_cleanup(GstDiscoverer * dc)1933 discoverer_cleanup (GstDiscoverer * dc)
1934 {
1935 GST_DEBUG ("Cleaning up");
1936
1937 DISCO_LOCK (dc);
1938 dc->priv->cleanup = TRUE;
1939 DISCO_UNLOCK (dc);
1940
1941 gst_bus_set_flushing (dc->priv->bus, TRUE);
1942
1943 DISCO_LOCK (dc);
1944 if (dc->priv->current_error) {
1945 g_error_free (dc->priv->current_error);
1946 DISCO_UNLOCK (dc);
1947 gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_NULL);
1948 } else {
1949 DISCO_UNLOCK (dc);
1950 }
1951
1952 gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_READY);
1953 gst_bus_set_flushing (dc->priv->bus, FALSE);
1954
1955 DISCO_LOCK (dc);
1956 dc->priv->current_error = NULL;
1957 if (dc->priv->current_topology) {
1958 gst_structure_free (dc->priv->current_topology);
1959 dc->priv->current_topology = NULL;
1960 }
1961
1962 dc->priv->current_info = NULL;
1963
1964 if (dc->priv->all_tags) {
1965 gst_tag_list_unref (dc->priv->all_tags);
1966 dc->priv->all_tags = NULL;
1967 }
1968
1969 if (dc->priv->global_tags) {
1970 gst_tag_list_unref (dc->priv->global_tags);
1971 dc->priv->global_tags = NULL;
1972 }
1973
1974 dc->priv->pending_subtitle_pads = 0;
1975
1976 dc->priv->current_state = GST_STATE_NULL;
1977 dc->priv->target_state = GST_STATE_NULL;
1978 dc->priv->no_more_pads = FALSE;
1979 dc->priv->cleanup = FALSE;
1980
1981
1982 /* Try popping the next uri */
1983 if (dc->priv->async) {
1984 setup_next_uri_locked (dc);
1985 } else
1986 DISCO_UNLOCK (dc);
1987
1988 GST_DEBUG ("out");
1989 }
1990
1991 static void
discoverer_bus_cb(GstBus * bus,GstMessage * msg,GstDiscoverer * dc)1992 discoverer_bus_cb (GstBus * bus, GstMessage * msg, GstDiscoverer * dc)
1993 {
1994 if (dc->priv->processing) {
1995 if (handle_message (dc, msg)) {
1996 GST_DEBUG ("Stopping asynchronously");
1997 /* Serialise with _event_probe() */
1998 DISCO_LOCK (dc);
1999 dc->priv->processing = FALSE;
2000 DISCO_UNLOCK (dc);
2001 discoverer_collect (dc);
2002 discoverer_cleanup (dc);
2003 }
2004 }
2005 }
2006
2007 static gboolean
async_timeout_cb(GstDiscoverer * dc)2008 async_timeout_cb (GstDiscoverer * dc)
2009 {
2010 if (!g_source_is_destroyed (g_main_current_source ())) {
2011 GST_DEBUG ("Setting result to TIMEOUT");
2012 dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
2013 dc->priv->processing = FALSE;
2014 discoverer_collect (dc);
2015 discoverer_cleanup (dc);
2016 }
2017 return FALSE;
2018 }
2019
2020 /* If there is a pending URI, it will pop it from the list of pending
2021 * URIs and start the discovery on it.
2022 *
2023 * Returns GST_DISCOVERER_OK if the next URI was popped and is processing,
2024 * else a error flag.
2025 */
2026 static GstDiscovererResult
start_discovering(GstDiscoverer * dc)2027 start_discovering (GstDiscoverer * dc)
2028 {
2029 gboolean ready;
2030 GstDiscovererResult res = GST_DISCOVERER_OK;
2031
2032 GST_DEBUG ("Starting");
2033
2034 DISCO_LOCK (dc);
2035 if (dc->priv->pending_uris == NULL) {
2036 GST_WARNING ("No URI to process");
2037 res = GST_DISCOVERER_URI_INVALID;
2038 DISCO_UNLOCK (dc);
2039 goto beach;
2040 }
2041
2042 if (dc->priv->current_info != NULL) {
2043 GST_WARNING ("Already processing a file");
2044 res = GST_DISCOVERER_BUSY;
2045 DISCO_UNLOCK (dc);
2046 goto beach;
2047 }
2048
2049 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_STARTING], 0);
2050
2051 ready = _setup_locked (dc);
2052
2053 DISCO_UNLOCK (dc);
2054
2055 if (dc->priv->async) {
2056 if (ready) {
2057 GSource *source;
2058
2059 source = g_idle_source_new ();
2060 g_source_set_callback (source,
2061 (GSourceFunc) emit_discovererd_and_next, gst_object_ref (dc),
2062 gst_object_unref);
2063 g_source_attach (source, dc->priv->ctx);
2064 goto beach;
2065 }
2066
2067 handle_current_async (dc);
2068 } else {
2069 if (!ready)
2070 handle_current_sync (dc);
2071 }
2072
2073 beach:
2074 return res;
2075 }
2076
2077 /* Serializing code */
2078
2079 static GVariant *
_serialize_common_stream_info(GstDiscovererStreamInfo * sinfo,GstDiscovererSerializeFlags flags)2080 _serialize_common_stream_info (GstDiscovererStreamInfo * sinfo,
2081 GstDiscovererSerializeFlags flags)
2082 {
2083 GVariant *common;
2084 GVariant *nextv = NULL;
2085 gchar *caps_str = NULL, *tags_str = NULL, *misc_str = NULL;
2086
2087 if (sinfo->caps && (flags & GST_DISCOVERER_SERIALIZE_CAPS))
2088 caps_str = gst_caps_to_string (sinfo->caps);
2089
2090 if (sinfo->tags && (flags & GST_DISCOVERER_SERIALIZE_TAGS))
2091 tags_str = gst_tag_list_to_string (sinfo->tags);
2092
2093 if (sinfo->misc && (flags & GST_DISCOVERER_SERIALIZE_MISC))
2094 misc_str = gst_structure_to_string (sinfo->misc);
2095
2096
2097 if (sinfo->next)
2098 nextv = gst_discoverer_info_to_variant_recurse (sinfo->next, flags);
2099 else
2100 nextv = g_variant_new ("()");
2101
2102 common =
2103 g_variant_new ("(msmsmsmsv)", sinfo->stream_id, caps_str, tags_str,
2104 misc_str, nextv);
2105
2106 g_free (caps_str);
2107 g_free (tags_str);
2108 g_free (misc_str);
2109
2110 return common;
2111 }
2112
2113 static GVariant *
_serialize_info(GstDiscovererInfo * info,GstDiscovererSerializeFlags flags)2114 _serialize_info (GstDiscovererInfo * info, GstDiscovererSerializeFlags flags)
2115 {
2116 gchar *tags_str = NULL;
2117 GVariant *ret;
2118
2119 if (info->tags && (flags & GST_DISCOVERER_SERIALIZE_TAGS))
2120 tags_str = gst_tag_list_to_string (info->tags);
2121
2122 ret =
2123 g_variant_new ("(mstbmsb)", info->uri, info->duration, info->seekable,
2124 tags_str, info->live);
2125
2126 g_free (tags_str);
2127
2128 return ret;
2129 }
2130
2131 static GVariant *
_serialize_audio_stream_info(GstDiscovererAudioInfo * ainfo)2132 _serialize_audio_stream_info (GstDiscovererAudioInfo * ainfo)
2133 {
2134 return g_variant_new ("(uuuuumst)",
2135 ainfo->channels,
2136 ainfo->sample_rate,
2137 ainfo->bitrate, ainfo->max_bitrate, ainfo->depth, ainfo->language,
2138 ainfo->channel_mask);
2139 }
2140
2141 static GVariant *
_serialize_video_stream_info(GstDiscovererVideoInfo * vinfo)2142 _serialize_video_stream_info (GstDiscovererVideoInfo * vinfo)
2143 {
2144 return g_variant_new ("(uuuuuuubuub)",
2145 vinfo->width,
2146 vinfo->height,
2147 vinfo->depth,
2148 vinfo->framerate_num,
2149 vinfo->framerate_denom,
2150 vinfo->par_num,
2151 vinfo->par_denom,
2152 vinfo->interlaced, vinfo->bitrate, vinfo->max_bitrate, vinfo->is_image);
2153 }
2154
2155 static GVariant *
_serialize_subtitle_stream_info(GstDiscovererSubtitleInfo * sinfo)2156 _serialize_subtitle_stream_info (GstDiscovererSubtitleInfo * sinfo)
2157 {
2158 return g_variant_new ("ms", sinfo->language);
2159 }
2160
2161 static GVariant *
gst_discoverer_info_to_variant_recurse(GstDiscovererStreamInfo * sinfo,GstDiscovererSerializeFlags flags)2162 gst_discoverer_info_to_variant_recurse (GstDiscovererStreamInfo * sinfo,
2163 GstDiscovererSerializeFlags flags)
2164 {
2165 GVariant *stream_variant = NULL;
2166 GVariant *common_stream_variant =
2167 _serialize_common_stream_info (sinfo, flags);
2168
2169 if (GST_IS_DISCOVERER_CONTAINER_INFO (sinfo)) {
2170 GList *tmp;
2171 GList *streams =
2172 gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO
2173 (sinfo));
2174
2175 if (g_list_length (streams) > 0) {
2176 GVariantBuilder children;
2177 GVariant *child_variant;
2178 g_variant_builder_init (&children, G_VARIANT_TYPE_ARRAY);
2179
2180 for (tmp = streams; tmp; tmp = tmp->next) {
2181 child_variant =
2182 gst_discoverer_info_to_variant_recurse (tmp->data, flags);
2183 g_variant_builder_add (&children, "v", child_variant);
2184 }
2185 stream_variant =
2186 g_variant_new ("(yvav)", 'c', common_stream_variant, &children);
2187 } else {
2188 stream_variant =
2189 g_variant_new ("(yvav)", 'c', common_stream_variant, NULL);
2190 }
2191
2192 gst_discoverer_stream_info_list_free (streams);
2193 } else if (GST_IS_DISCOVERER_AUDIO_INFO (sinfo)) {
2194 GVariant *audio_stream_info =
2195 _serialize_audio_stream_info (GST_DISCOVERER_AUDIO_INFO (sinfo));
2196 stream_variant =
2197 g_variant_new ("(yvv)", 'a', common_stream_variant, audio_stream_info);
2198 } else if (GST_IS_DISCOVERER_VIDEO_INFO (sinfo)) {
2199 GVariant *video_stream_info =
2200 _serialize_video_stream_info (GST_DISCOVERER_VIDEO_INFO (sinfo));
2201 stream_variant =
2202 g_variant_new ("(yvv)", 'v', common_stream_variant, video_stream_info);
2203 } else if (GST_IS_DISCOVERER_SUBTITLE_INFO (sinfo)) {
2204 GVariant *subtitle_stream_info =
2205 _serialize_subtitle_stream_info (GST_DISCOVERER_SUBTITLE_INFO (sinfo));
2206 stream_variant =
2207 g_variant_new ("(yvv)", 's', common_stream_variant,
2208 subtitle_stream_info);
2209 } else {
2210 GVariant *nextv = NULL;
2211 GstDiscovererStreamInfo *ninfo =
2212 gst_discoverer_stream_info_get_next (sinfo);
2213
2214 nextv = gst_discoverer_info_to_variant_recurse (ninfo, flags);
2215
2216 stream_variant =
2217 g_variant_new ("(yvv)", 'n', common_stream_variant,
2218 g_variant_new ("v", nextv));
2219 }
2220
2221 return stream_variant;
2222 }
2223
2224 /* Parsing code */
2225
2226 #define GET_FROM_TUPLE(v, t, n, val) G_STMT_START{ \
2227 GVariant *child = g_variant_get_child_value (v, n); \
2228 *val = g_variant_get_##t(child); \
2229 g_variant_unref (child); \
2230 }G_STMT_END
2231
2232 static const gchar *
_maybe_get_string_from_tuple(GVariant * tuple,guint index)2233 _maybe_get_string_from_tuple (GVariant * tuple, guint index)
2234 {
2235 const gchar *ret = NULL;
2236 GVariant *maybe;
2237 GET_FROM_TUPLE (tuple, maybe, index, &maybe);
2238 if (maybe) {
2239 ret = g_variant_get_string (maybe, NULL);
2240 g_variant_unref (maybe);
2241 }
2242
2243 return ret;
2244 }
2245
2246 static void
_parse_info(GstDiscovererInfo * info,GVariant * info_variant)2247 _parse_info (GstDiscovererInfo * info, GVariant * info_variant)
2248 {
2249 const gchar *str;
2250
2251 str = _maybe_get_string_from_tuple (info_variant, 0);
2252 if (str)
2253 info->uri = g_strdup (str);
2254
2255 GET_FROM_TUPLE (info_variant, uint64, 1, &info->duration);
2256 GET_FROM_TUPLE (info_variant, boolean, 2, &info->seekable);
2257
2258 str = _maybe_get_string_from_tuple (info_variant, 3);
2259 if (str)
2260 info->tags = gst_tag_list_new_from_string (str);
2261
2262 GET_FROM_TUPLE (info_variant, boolean, 4, &info->live);
2263 }
2264
2265 static void
_parse_common_stream_info(GstDiscovererStreamInfo * sinfo,GVariant * common,GstDiscovererInfo * info)2266 _parse_common_stream_info (GstDiscovererStreamInfo * sinfo, GVariant * common,
2267 GstDiscovererInfo * info)
2268 {
2269 const gchar *str;
2270
2271 str = _maybe_get_string_from_tuple (common, 0);
2272 if (str)
2273 sinfo->stream_id = g_strdup (str);
2274
2275 str = _maybe_get_string_from_tuple (common, 1);
2276 if (str)
2277 sinfo->caps = gst_caps_from_string (str);
2278
2279 str = _maybe_get_string_from_tuple (common, 2);
2280 if (str)
2281 sinfo->tags = gst_tag_list_new_from_string (str);
2282
2283 str = _maybe_get_string_from_tuple (common, 3);
2284 if (str)
2285 sinfo->misc = gst_structure_new_from_string (str);
2286
2287 if (g_variant_n_children (common) > 4) {
2288 GVariant *nextv;
2289
2290 GET_FROM_TUPLE (common, variant, 4, &nextv);
2291 if (g_variant_n_children (nextv) > 0) {
2292 sinfo->next = _parse_discovery (nextv, info);
2293 }
2294 g_variant_unref (nextv);
2295 }
2296
2297 g_variant_unref (common);
2298 }
2299
2300 static void
_parse_audio_stream_info(GstDiscovererAudioInfo * ainfo,GVariant * specific)2301 _parse_audio_stream_info (GstDiscovererAudioInfo * ainfo, GVariant * specific)
2302 {
2303 const gchar *str;
2304
2305 GET_FROM_TUPLE (specific, uint32, 0, &ainfo->channels);
2306 GET_FROM_TUPLE (specific, uint32, 1, &ainfo->sample_rate);
2307 GET_FROM_TUPLE (specific, uint32, 2, &ainfo->bitrate);
2308 GET_FROM_TUPLE (specific, uint32, 3, &ainfo->max_bitrate);
2309 GET_FROM_TUPLE (specific, uint32, 4, &ainfo->depth);
2310
2311 str = _maybe_get_string_from_tuple (specific, 5);
2312
2313 if (str)
2314 ainfo->language = g_strdup (str);
2315
2316 GET_FROM_TUPLE (specific, uint64, 6, &ainfo->channel_mask);
2317
2318 g_variant_unref (specific);
2319 }
2320
2321 static void
_parse_video_stream_info(GstDiscovererVideoInfo * vinfo,GVariant * specific)2322 _parse_video_stream_info (GstDiscovererVideoInfo * vinfo, GVariant * specific)
2323 {
2324 GET_FROM_TUPLE (specific, uint32, 0, &vinfo->width);
2325 GET_FROM_TUPLE (specific, uint32, 1, &vinfo->height);
2326 GET_FROM_TUPLE (specific, uint32, 2, &vinfo->depth);
2327 GET_FROM_TUPLE (specific, uint32, 3, &vinfo->framerate_num);
2328 GET_FROM_TUPLE (specific, uint32, 4, &vinfo->framerate_denom);
2329 GET_FROM_TUPLE (specific, uint32, 5, &vinfo->par_num);
2330 GET_FROM_TUPLE (specific, uint32, 6, &vinfo->par_denom);
2331 GET_FROM_TUPLE (specific, boolean, 7, &vinfo->interlaced);
2332 GET_FROM_TUPLE (specific, uint32, 8, &vinfo->bitrate);
2333 GET_FROM_TUPLE (specific, uint32, 9, &vinfo->max_bitrate);
2334 GET_FROM_TUPLE (specific, boolean, 10, &vinfo->is_image);
2335
2336 g_variant_unref (specific);
2337 }
2338
2339 static void
_parse_subtitle_stream_info(GstDiscovererSubtitleInfo * sinfo,GVariant * specific)2340 _parse_subtitle_stream_info (GstDiscovererSubtitleInfo * sinfo,
2341 GVariant * specific)
2342 {
2343 GVariant *maybe;
2344
2345 maybe = g_variant_get_maybe (specific);
2346 if (maybe) {
2347 sinfo->language = g_strdup (g_variant_get_string (maybe, NULL));
2348 g_variant_unref (maybe);
2349 }
2350
2351 g_variant_unref (specific);
2352 }
2353
2354 static GstDiscovererStreamInfo *
_parse_discovery(GVariant * variant,GstDiscovererInfo * info)2355 _parse_discovery (GVariant * variant, GstDiscovererInfo * info)
2356 {
2357 gchar type;
2358 GVariant *common = g_variant_get_child_value (variant, 1);
2359 GVariant *specific = g_variant_get_child_value (variant, 2);
2360 GstDiscovererStreamInfo *sinfo = NULL;
2361
2362 GET_FROM_TUPLE (variant, byte, 0, &type);
2363 switch (type) {
2364 case 'c':
2365 sinfo = g_object_new (GST_TYPE_DISCOVERER_CONTAINER_INFO, NULL);
2366 break;
2367 case 'a':
2368 sinfo = g_object_new (GST_TYPE_DISCOVERER_AUDIO_INFO, NULL);
2369 _parse_audio_stream_info (GST_DISCOVERER_AUDIO_INFO (sinfo),
2370 g_variant_get_child_value (specific, 0));
2371 break;
2372 case 'v':
2373 sinfo = g_object_new (GST_TYPE_DISCOVERER_VIDEO_INFO, NULL);
2374 _parse_video_stream_info (GST_DISCOVERER_VIDEO_INFO (sinfo),
2375 g_variant_get_child_value (specific, 0));
2376 break;
2377 case 's':
2378 sinfo = g_object_new (GST_TYPE_DISCOVERER_SUBTITLE_INFO, NULL);
2379 _parse_subtitle_stream_info (GST_DISCOVERER_SUBTITLE_INFO (sinfo),
2380 g_variant_get_child_value (specific, 0));
2381 break;
2382 case 'n':
2383 sinfo = g_object_new (GST_TYPE_DISCOVERER_STREAM_INFO, NULL);
2384 break;
2385 default:
2386 GST_WARNING ("Unexpected discoverer info type %d", type);
2387 goto out;
2388 }
2389
2390 _parse_common_stream_info (sinfo, g_variant_get_child_value (common, 0),
2391 info);
2392
2393 if (!GST_IS_DISCOVERER_CONTAINER_INFO (sinfo)) {
2394 info->stream_list = g_list_append (info->stream_list, sinfo);
2395 }
2396
2397 if (!info->stream_info) {
2398 info->stream_info = sinfo;
2399 }
2400
2401 if (GST_IS_DISCOVERER_CONTAINER_INFO (sinfo)) {
2402 GVariantIter iter;
2403 GVariant *child;
2404
2405 GstDiscovererContainerInfo *cinfo = GST_DISCOVERER_CONTAINER_INFO (sinfo);
2406 g_variant_iter_init (&iter, specific);
2407 cinfo->tags = sinfo->tags;
2408 while ((child = g_variant_iter_next_value (&iter))) {
2409 GstDiscovererStreamInfo *child_info;
2410 child_info = _parse_discovery (g_variant_get_variant (child), info);
2411 if (child_info != NULL) {
2412 cinfo->streams =
2413 g_list_append (cinfo->streams,
2414 gst_discoverer_stream_info_ref (child_info));
2415 }
2416 g_variant_unref (child);
2417 }
2418 }
2419
2420 out:
2421
2422 g_variant_unref (common);
2423 g_variant_unref (specific);
2424 g_variant_unref (variant);
2425 return sinfo;
2426 }
2427
2428 /**
2429 * gst_discoverer_start:
2430 * @discoverer: A #GstDiscoverer
2431 *
2432 * Allow asynchronous discovering of URIs to take place.
2433 * A #GMainLoop must be available for #GstDiscoverer to properly work in
2434 * asynchronous mode.
2435 */
2436 void
gst_discoverer_start(GstDiscoverer * discoverer)2437 gst_discoverer_start (GstDiscoverer * discoverer)
2438 {
2439 GSource *source;
2440 GMainContext *ctx = NULL;
2441
2442 g_return_if_fail (GST_IS_DISCOVERER (discoverer));
2443
2444 GST_DEBUG_OBJECT (discoverer, "Starting...");
2445
2446 if (discoverer->priv->async) {
2447 GST_DEBUG_OBJECT (discoverer, "We were already started");
2448 return;
2449 }
2450
2451 discoverer->priv->async = TRUE;
2452 discoverer->priv->running = TRUE;
2453
2454 ctx = g_main_context_get_thread_default ();
2455
2456 /* Connect to bus signals */
2457 if (ctx == NULL)
2458 ctx = g_main_context_default ();
2459
2460 source = gst_bus_create_watch (discoverer->priv->bus);
2461 g_source_set_callback (source, (GSourceFunc) gst_bus_async_signal_func,
2462 NULL, NULL);
2463 g_source_attach (source, ctx);
2464 discoverer->priv->bus_source = source;
2465 discoverer->priv->ctx = g_main_context_ref (ctx);
2466
2467 start_discovering (discoverer);
2468 GST_DEBUG_OBJECT (discoverer, "Started");
2469 }
2470
2471 /**
2472 * gst_discoverer_stop:
2473 * @discoverer: A #GstDiscoverer
2474 *
2475 * Stop the discovery of any pending URIs and clears the list of
2476 * pending URIS (if any).
2477 */
2478 void
gst_discoverer_stop(GstDiscoverer * discoverer)2479 gst_discoverer_stop (GstDiscoverer * discoverer)
2480 {
2481 g_return_if_fail (GST_IS_DISCOVERER (discoverer));
2482
2483 GST_DEBUG_OBJECT (discoverer, "Stopping...");
2484
2485 if (!discoverer->priv->async) {
2486 GST_DEBUG_OBJECT (discoverer,
2487 "We were already stopped, or running synchronously");
2488 return;
2489 }
2490
2491 DISCO_LOCK (discoverer);
2492 if (discoverer->priv->processing) {
2493 /* We prevent any further processing by setting the bus to
2494 * flushing and setting the pipeline to READY.
2495 * _reset() will take care of the rest of the cleanup */
2496 if (discoverer->priv->bus)
2497 gst_bus_set_flushing (discoverer->priv->bus, TRUE);
2498 if (discoverer->priv->pipeline)
2499 gst_element_set_state ((GstElement *) discoverer->priv->pipeline,
2500 GST_STATE_READY);
2501 }
2502 discoverer->priv->running = FALSE;
2503 DISCO_UNLOCK (discoverer);
2504
2505 /* Remove timeout handler */
2506 if (discoverer->priv->timeout_source) {
2507 g_source_destroy (discoverer->priv->timeout_source);
2508 g_source_unref (discoverer->priv->timeout_source);
2509 discoverer->priv->timeout_source = NULL;
2510 }
2511 /* Remove signal watch */
2512 if (discoverer->priv->bus_source) {
2513 g_source_destroy (discoverer->priv->bus_source);
2514 g_source_unref (discoverer->priv->bus_source);
2515 discoverer->priv->bus_source = NULL;
2516 }
2517 /* Unref main context */
2518 if (discoverer->priv->ctx) {
2519 g_main_context_unref (discoverer->priv->ctx);
2520 discoverer->priv->ctx = NULL;
2521 }
2522 discoverer_reset (discoverer);
2523
2524 discoverer->priv->async = FALSE;
2525
2526 GST_DEBUG_OBJECT (discoverer, "Stopped");
2527 }
2528
2529 /**
2530 * gst_discoverer_discover_uri_async:
2531 * @discoverer: A #GstDiscoverer
2532 * @uri: the URI to add.
2533 *
2534 * Appends the given @uri to the list of URIs to discoverer. The actual
2535 * discovery of the @uri will only take place if gst_discoverer_start() has
2536 * been called.
2537 *
2538 * A copy of @uri will be made internally, so the caller can safely g_free()
2539 * afterwards.
2540 *
2541 * Returns: %TRUE if the @uri was successfully appended to the list of pending
2542 * uris, else %FALSE
2543 */
2544 gboolean
gst_discoverer_discover_uri_async(GstDiscoverer * discoverer,const gchar * uri)2545 gst_discoverer_discover_uri_async (GstDiscoverer * discoverer,
2546 const gchar * uri)
2547 {
2548 gboolean can_run;
2549
2550 g_return_val_if_fail (GST_IS_DISCOVERER (discoverer), FALSE);
2551
2552 GST_DEBUG_OBJECT (discoverer, "uri : %s", uri);
2553
2554 DISCO_LOCK (discoverer);
2555 can_run = (discoverer->priv->pending_uris == NULL);
2556 discoverer->priv->pending_uris =
2557 g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
2558 DISCO_UNLOCK (discoverer);
2559
2560 if (can_run)
2561 start_discovering (discoverer);
2562
2563 return TRUE;
2564 }
2565
2566
2567 /* Synchronous mode */
2568 /**
2569 * gst_discoverer_discover_uri:
2570 * @discoverer: A #GstDiscoverer
2571 * @uri: The URI to run on.
2572 * @err: (out) (allow-none): If an error occurred, this field will be filled in.
2573 *
2574 * Synchronously discovers the given @uri.
2575 *
2576 * A copy of @uri will be made internally, so the caller can safely g_free()
2577 * afterwards.
2578 *
2579 * Returns: (transfer full): the result of the scanning. Can be %NULL if an
2580 * error occurred.
2581 */
2582 GstDiscovererInfo *
gst_discoverer_discover_uri(GstDiscoverer * discoverer,const gchar * uri,GError ** err)2583 gst_discoverer_discover_uri (GstDiscoverer * discoverer, const gchar * uri,
2584 GError ** err)
2585 {
2586 GstDiscovererResult res = 0;
2587 GstDiscovererInfo *info;
2588
2589 g_return_val_if_fail (GST_IS_DISCOVERER (discoverer), NULL);
2590 g_return_val_if_fail (uri, NULL);
2591
2592 GST_DEBUG_OBJECT (discoverer, "uri:%s", uri);
2593
2594 DISCO_LOCK (discoverer);
2595 if (G_UNLIKELY (discoverer->priv->current_info)) {
2596 DISCO_UNLOCK (discoverer);
2597 GST_WARNING_OBJECT (discoverer, "Already handling a uri");
2598 if (err)
2599 *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
2600 "Already handling a uri");
2601 return NULL;
2602 }
2603
2604 discoverer->priv->pending_uris =
2605 g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
2606 DISCO_UNLOCK (discoverer);
2607
2608 res = start_discovering (discoverer);
2609 discoverer_collect (discoverer);
2610
2611 /* Get results */
2612 if (err) {
2613 if (discoverer->priv->current_error)
2614 *err = g_error_copy (discoverer->priv->current_error);
2615 else
2616 *err = NULL;
2617 }
2618 if (res != GST_DISCOVERER_OK) {
2619 GST_DEBUG ("Setting result to %d (was %d)", res,
2620 discoverer->priv->current_info->result);
2621 discoverer->priv->current_info->result = res;
2622 }
2623 info = _ensure_info_tags (discoverer);
2624
2625 discoverer_cleanup (discoverer);
2626
2627 return info;
2628 }
2629
2630 /**
2631 * gst_discoverer_new:
2632 * @timeout: timeout per file, in nanoseconds. Allowed are values between
2633 * one second (#GST_SECOND) and one hour (3600 * #GST_SECOND)
2634 * @err: a pointer to a #GError. can be %NULL
2635 *
2636 * Creates a new #GstDiscoverer with the provided timeout.
2637 *
2638 * Returns: (transfer full): The new #GstDiscoverer.
2639 * If an error occurred when creating the discoverer, @err will be set
2640 * accordingly and %NULL will be returned. If @err is set, the caller must
2641 * free it when no longer needed using g_error_free().
2642 */
2643 GstDiscoverer *
gst_discoverer_new(GstClockTime timeout,GError ** err)2644 gst_discoverer_new (GstClockTime timeout, GError ** err)
2645 {
2646 GstDiscoverer *res;
2647
2648 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timeout), NULL);
2649
2650 res = g_object_new (GST_TYPE_DISCOVERER, "timeout", timeout, NULL);
2651 if (res->priv->uridecodebin == NULL) {
2652 if (err)
2653 *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
2654 "Couldn't create 'uridecodebin' element");
2655 gst_object_unref (res);
2656 res = NULL;
2657 }
2658 return res;
2659 }
2660
2661 /**
2662 * gst_discoverer_info_to_variant:
2663 * @info: A #GstDiscovererInfo
2664 * @flags: A combination of #GstDiscovererSerializeFlags to specify
2665 * what needs to be serialized.
2666 *
2667 * Serializes @info to a #GVariant that can be parsed again
2668 * through gst_discoverer_info_from_variant().
2669 *
2670 * Note that any #GstToc (s) that might have been discovered will not be serialized
2671 * for now.
2672 *
2673 * Returns: (transfer full): A newly-allocated #GVariant representing @info.
2674 *
2675 * Since: 1.6
2676 */
2677 GVariant *
gst_discoverer_info_to_variant(GstDiscovererInfo * info,GstDiscovererSerializeFlags flags)2678 gst_discoverer_info_to_variant (GstDiscovererInfo * info,
2679 GstDiscovererSerializeFlags flags)
2680 {
2681 /* FIXME: implement TOC support */
2682 GVariant *stream_variant;
2683 GVariant *variant, *info_variant;
2684 GstDiscovererStreamInfo *sinfo;
2685 GVariant *wrapper;
2686
2687 g_return_val_if_fail (GST_IS_DISCOVERER_INFO (info), NULL);
2688 g_return_val_if_fail (gst_discoverer_info_get_result (info) ==
2689 GST_DISCOVERER_OK, NULL);
2690
2691 sinfo = gst_discoverer_info_get_stream_info (info);
2692 stream_variant = gst_discoverer_info_to_variant_recurse (sinfo, flags);
2693 info_variant = _serialize_info (info, flags);
2694
2695 variant = g_variant_new ("(vv)", info_variant, stream_variant);
2696
2697 /* Returning a wrapper implies some small overhead, but simplifies
2698 * deserializing from bytes */
2699 wrapper = g_variant_new_variant (variant);
2700
2701 gst_discoverer_stream_info_unref (sinfo);
2702 return wrapper;
2703 }
2704
2705 /**
2706 * gst_discoverer_info_from_variant:
2707 * @variant: A #GVariant to deserialize into a #GstDiscovererInfo.
2708 *
2709 * Parses a #GVariant as produced by gst_discoverer_info_to_variant()
2710 * back to a #GstDiscovererInfo.
2711 *
2712 * Returns: (transfer full): A newly-allocated #GstDiscovererInfo.
2713 *
2714 * Since: 1.6
2715 */
2716 GstDiscovererInfo *
gst_discoverer_info_from_variant(GVariant * variant)2717 gst_discoverer_info_from_variant (GVariant * variant)
2718 {
2719 GstDiscovererInfo *info = g_object_new (GST_TYPE_DISCOVERER_INFO, NULL);
2720 GVariant *info_variant = g_variant_get_variant (variant);
2721 GVariant *info_specific_variant;
2722 GVariant *wrapped;
2723
2724 GET_FROM_TUPLE (info_variant, variant, 0, &info_specific_variant);
2725 GET_FROM_TUPLE (info_variant, variant, 1, &wrapped);
2726
2727 _parse_info (info, info_specific_variant);
2728 _parse_discovery (wrapped, info);
2729 g_variant_unref (info_specific_variant);
2730 g_variant_unref (info_variant);
2731
2732 return info;
2733 }
2734