2 * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
3 * 2009 Nokia Corporation
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.
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.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:gstdiscoverer
23 * @short_description: Utility for discovering information on URIs.
25 * The #GstDiscoverer is a utility object which allows to get as much
26 * information as possible from one or many URIs.
28 * It provides two APIs, allowing usage in blocking or non-blocking mode.
30 * The blocking mode just requires calling gst_discoverer_discover_uri()
31 * with the URI one wishes to discover.
33 * The non-blocking mode requires a running #GMainLoop in the default
34 * #GMainContext, where one connects to the various signals, appends the
35 * URIs to be processed (through gst_discoverer_discover_uri_async()) and then
36 * asks for the discovery to begin (through gst_discoverer_start()).
38 * All the information is returned in a #GstDiscovererInfo structure.
45 #include <gst/video/video.h>
46 #include <gst/audio/audio.h>
49 #include "pbutils-private.h"
51 GST_DEBUG_CATEGORY_STATIC (discoverer_debug);
52 #define GST_CAT_DEFAULT discoverer_debug
54 static GQuark _CAPS_QUARK;
55 static GQuark _TAGS_QUARK;
56 static GQuark _TOC_QUARK;
57 static GQuark _MISSING_PLUGIN_QUARK;
58 static GQuark _STREAM_TOPOLOGY_QUARK;
59 static GQuark _TOPOLOGY_PAD_QUARK;
72 struct _GstDiscovererPrivate
76 /* allowed time to discover each uri in nanoseconds */
79 /* list of pending URI to process (current excluded) */
84 /* TRUE if processing a URI */
87 /* TRUE if discoverer has been started */
90 /* TRUE if ASYNC_DONE has been received (need to check for subtitle tags) */
94 GstDiscovererInfo *current_info;
95 GError *current_error;
96 GstStructure *current_topology;
98 /* List of private streams */
101 /* List of these sinks and their handler IDs (to remove the probe) */
102 guint pending_subtitle_pads;
104 /* Global elements */
106 GstElement *uridecodebin;
109 GType decodebin_type;
111 /* Custom main context variables */
116 /* reusable queries */
117 GstQuery *seeking_query;
119 /* Handler ids for various callbacks */
121 gulong pad_remove_id;
122 gulong source_chg_id;
123 gulong element_added_id;
127 #define DISCO_LOCK(dc) g_mutex_lock (dc->priv->lock);
128 #define DISCO_UNLOCK(dc) g_mutex_unlock (dc->priv->lock);
133 GST_DEBUG_CATEGORY_INIT (discoverer_debug, "discoverer", 0, "Discoverer");
135 _CAPS_QUARK = g_quark_from_static_string ("caps");
136 _TAGS_QUARK = g_quark_from_static_string ("tags");
137 _TOC_QUARK = g_quark_from_static_string ("toc");
138 _MISSING_PLUGIN_QUARK = g_quark_from_static_string ("missing-plugin");
139 _STREAM_TOPOLOGY_QUARK = g_quark_from_static_string ("stream-topology");
140 _TOPOLOGY_PAD_QUARK = g_quark_from_static_string ("pad");
143 G_DEFINE_TYPE_EXTENDED (GstDiscoverer, gst_discoverer, G_TYPE_OBJECT, 0,
155 #define DEFAULT_PROP_TIMEOUT 15 * GST_SECOND
163 static guint gst_discoverer_signals[LAST_SIGNAL] = { 0 };
165 static void gst_discoverer_set_timeout (GstDiscoverer * dc,
166 GstClockTime timeout);
167 static gboolean async_timeout_cb (GstDiscoverer * dc);
169 static void discoverer_bus_cb (GstBus * bus, GstMessage * msg,
171 static void uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
173 static void uridecodebin_pad_removed_cb (GstElement * uridecodebin,
174 GstPad * pad, GstDiscoverer * dc);
175 static void uridecodebin_source_changed_cb (GstElement * uridecodebin,
176 GParamSpec * pspec, GstDiscoverer * dc);
178 static void gst_discoverer_dispose (GObject * dc);
179 static void gst_discoverer_set_property (GObject * object, guint prop_id,
180 const GValue * value, GParamSpec * pspec);
181 static void gst_discoverer_get_property (GObject * object, guint prop_id,
182 GValue * value, GParamSpec * pspec);
185 gst_discoverer_class_init (GstDiscovererClass * klass)
187 GObjectClass *gobject_class = (GObjectClass *) klass;
189 gobject_class->dispose = gst_discoverer_dispose;
191 gobject_class->set_property = gst_discoverer_set_property;
192 gobject_class->get_property = gst_discoverer_get_property;
194 g_type_class_add_private (klass, sizeof (GstDiscovererPrivate));
198 * GstDiscoverer:timeout:
200 * The duration (in nanoseconds) after which the discovery of an individual
203 * If the discovery of a URI times out, the %GST_DISCOVERER_TIMEOUT will be
204 * set on the result flags.
206 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
207 g_param_spec_uint64 ("timeout", "timeout", "Timeout",
208 GST_SECOND, 3600 * GST_SECOND, DEFAULT_PROP_TIMEOUT,
209 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
213 * GstDiscoverer::finished:
214 * @discoverer: the #GstDiscoverer
216 * Will be emitted when all pending URIs have been processed.
218 gst_discoverer_signals[SIGNAL_FINISHED] =
219 g_signal_new ("finished", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
220 G_STRUCT_OFFSET (GstDiscovererClass, finished),
221 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
224 * GstDiscoverer::starting:
225 * @discoverer: the #GstDiscoverer
227 * Will be emitted when the discover starts analyzing the pending URIs
229 gst_discoverer_signals[SIGNAL_STARTING] =
230 g_signal_new ("starting", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
231 G_STRUCT_OFFSET (GstDiscovererClass, starting),
232 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
235 * GstDiscoverer::discovered:
236 * @discoverer: the #GstDiscoverer
237 * @info: the results #GstDiscovererInfo
238 * @error: (type GLib.Error): #GError, which will be non-NULL if an error
239 * occurred during discovery. You must not
240 * free this #GError, it will be freed by
243 * Will be emitted when all information on a URI could be discovered, or
246 * When an error occurs, @info might still contain some partial information,
247 * depending on the circumstances of the error.
249 gst_discoverer_signals[SIGNAL_DISCOVERED] =
250 g_signal_new ("discovered", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
251 G_STRUCT_OFFSET (GstDiscovererClass, discovered),
252 NULL, NULL, g_cclosure_marshal_generic,
253 G_TYPE_NONE, 2, GST_TYPE_DISCOVERER_INFO,
254 G_TYPE_ERROR | G_SIGNAL_TYPE_STATIC_SCOPE);
257 * GstDiscoverer::source-setup:
258 * @discoverer: the #GstDiscoverer
259 * @source: source element
261 * This signal is emitted after the source element has been created for, so
262 * the URI being discovered, so it can be configured by setting additional
263 * properties (e.g. set a proxy server for an http source, or set the device
264 * and read speed for an audio cd source).
266 * This signal is usually emitted from the context of a GStreamer streaming
269 gst_discoverer_signals[SIGNAL_SOURCE_SETUP] =
270 g_signal_new ("source-setup", G_TYPE_FROM_CLASS (klass),
271 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDiscovererClass, source_setup),
272 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
276 uridecodebin_element_added_cb (GstElement * uridecodebin,
277 GstElement * child, GstDiscoverer * dc)
279 GST_DEBUG ("New element added to uridecodebin : %s",
280 GST_ELEMENT_NAME (child));
282 if (G_OBJECT_TYPE (child) == dc->priv->decodebin_type) {
283 g_object_set (child, "post-stream-topology", TRUE, NULL);
288 gst_discoverer_init (GstDiscoverer * dc)
291 GstFormat format = GST_FORMAT_TIME;
293 dc->priv = G_TYPE_INSTANCE_GET_PRIVATE (dc, GST_TYPE_DISCOVERER,
294 GstDiscovererPrivate);
296 dc->priv->timeout = DEFAULT_PROP_TIMEOUT;
297 dc->priv->async = FALSE;
298 dc->priv->async_done = FALSE;
300 dc->priv->lock = g_mutex_new ();
302 dc->priv->pending_subtitle_pads = 0;
304 GST_LOG ("Creating pipeline");
305 dc->priv->pipeline = (GstBin *) gst_pipeline_new ("Discoverer");
306 GST_LOG_OBJECT (dc, "Creating uridecodebin");
307 dc->priv->uridecodebin =
308 gst_element_factory_make ("uridecodebin", "discoverer-uri");
309 if (G_UNLIKELY (dc->priv->uridecodebin == NULL)) {
310 GST_ERROR ("Can't create uridecodebin");
313 GST_LOG_OBJECT (dc, "Adding uridecodebin to pipeline");
314 gst_bin_add (dc->priv->pipeline, dc->priv->uridecodebin);
316 dc->priv->pad_added_id =
317 g_signal_connect_object (dc->priv->uridecodebin, "pad-added",
318 G_CALLBACK (uridecodebin_pad_added_cb), dc, 0);
319 dc->priv->pad_remove_id =
320 g_signal_connect_object (dc->priv->uridecodebin, "pad-removed",
321 G_CALLBACK (uridecodebin_pad_removed_cb), dc, 0);
322 dc->priv->source_chg_id =
323 g_signal_connect_object (dc->priv->uridecodebin, "notify::source",
324 G_CALLBACK (uridecodebin_source_changed_cb), dc, 0);
326 GST_LOG_OBJECT (dc, "Getting pipeline bus");
327 dc->priv->bus = gst_pipeline_get_bus ((GstPipeline *) dc->priv->pipeline);
329 dc->priv->bus_cb_id =
330 g_signal_connect_object (dc->priv->bus, "message",
331 G_CALLBACK (discoverer_bus_cb), dc, 0);
333 GST_DEBUG_OBJECT (dc, "Done initializing Discoverer");
335 /* This is ugly. We get the GType of decodebin so we can quickly detect
336 * when a decodebin is added to uridecodebin so we can set the
337 * post-stream-topology setting to TRUE */
338 dc->priv->element_added_id =
339 g_signal_connect_object (dc->priv->uridecodebin, "element-added",
340 G_CALLBACK (uridecodebin_element_added_cb), dc, 0);
341 tmp = gst_element_factory_make ("decodebin", NULL);
342 dc->priv->decodebin_type = G_OBJECT_TYPE (tmp);
343 gst_object_unref (tmp);
346 dc->priv->seeking_query = gst_query_new_seeking (format);
350 discoverer_reset (GstDiscoverer * dc)
352 GST_DEBUG_OBJECT (dc, "Resetting");
354 if (dc->priv->pending_uris) {
355 g_list_foreach (dc->priv->pending_uris, (GFunc) g_free, NULL);
356 g_list_free (dc->priv->pending_uris);
357 dc->priv->pending_uris = NULL;
360 if (dc->priv->pipeline)
361 gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_NULL);
364 #define DISCONNECT_SIGNAL(o,i) G_STMT_START{ \
365 if ((i) && g_signal_handler_is_connected ((o), (i))) \
366 g_signal_handler_disconnect ((o), (i)); \
371 gst_discoverer_dispose (GObject * obj)
373 GstDiscoverer *dc = (GstDiscoverer *) obj;
375 GST_DEBUG_OBJECT (dc, "Disposing");
377 discoverer_reset (dc);
379 if (G_LIKELY (dc->priv->pipeline)) {
380 /* Workaround for bug #118536 */
381 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_added_id);
382 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_remove_id);
383 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->source_chg_id);
384 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->element_added_id);
385 DISCONNECT_SIGNAL (dc->priv->bus, dc->priv->bus_cb_id);
387 /* pipeline was set to NULL in _reset */
388 gst_object_unref (dc->priv->pipeline);
389 gst_object_unref (dc->priv->bus);
391 dc->priv->pipeline = NULL;
392 dc->priv->uridecodebin = NULL;
393 dc->priv->bus = NULL;
396 gst_discoverer_stop (dc);
398 if (dc->priv->lock) {
399 g_mutex_free (dc->priv->lock);
400 dc->priv->lock = NULL;
403 if (dc->priv->seeking_query) {
404 gst_query_unref (dc->priv->seeking_query);
405 dc->priv->seeking_query = NULL;
408 G_OBJECT_CLASS (gst_discoverer_parent_class)->dispose (obj);
412 gst_discoverer_set_property (GObject * object, guint prop_id,
413 const GValue * value, GParamSpec * pspec)
415 GstDiscoverer *dc = (GstDiscoverer *) object;
419 gst_discoverer_set_timeout (dc, g_value_get_uint64 (value));
422 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
428 gst_discoverer_get_property (GObject * object, guint prop_id,
429 GValue * value, GParamSpec * pspec)
431 GstDiscoverer *dc = (GstDiscoverer *) object;
436 g_value_set_uint64 (value, dc->priv->timeout);
440 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
446 gst_discoverer_set_timeout (GstDiscoverer * dc, GstClockTime timeout)
448 GST_DEBUG_OBJECT (dc, "timeout : %" GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
450 /* FIXME : update current pending timeout if we're running */
452 dc->priv->timeout = timeout;
456 static GstPadProbeReturn
457 _event_probe (GstPad * pad, GstPadProbeInfo * info, PrivateStream * ps)
459 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
461 if (GST_EVENT_TYPE (event) == GST_EVENT_TAG) {
462 GstTagList *tl = NULL, *tmp;
464 gst_event_parse_tag (event, &tl);
465 GST_DEBUG_OBJECT (pad, "tags %" GST_PTR_FORMAT, tl);
467 /* If preroll is complete, drop these tags - the collected information is
468 * possibly already being processed and adding more tags would be racy */
469 if (G_LIKELY (ps->dc->priv->processing)) {
470 GST_DEBUG_OBJECT (pad, "private stream %p old tags %" GST_PTR_FORMAT, ps,
472 tmp = gst_tag_list_merge (ps->tags, tl, GST_TAG_MERGE_APPEND);
474 gst_tag_list_free (ps->tags);
476 GST_DEBUG_OBJECT (pad, "private stream %p new tags %" GST_PTR_FORMAT, ps,
479 GST_DEBUG_OBJECT (pad, "Dropping tags since preroll is done");
480 DISCO_UNLOCK (ps->dc);
483 if (GST_EVENT_TYPE (event) == GST_EVENT_TOC) {
486 gst_event_parse_toc (event, &tmp, NULL);
487 GST_DEBUG_OBJECT (pad, "toc %" GST_PTR_FORMAT, tmp);
490 if (G_LIKELY (ps->dc->priv->processing)) {
491 GST_DEBUG_OBJECT (pad, "private stream %p toc %" GST_PTR_FORMAT, ps, tmp);
493 GST_DEBUG_OBJECT (pad, "Dropping toc since preroll is done");
494 DISCO_UNLOCK (ps->dc);
497 return GST_PAD_PROBE_OK;
500 static GstStaticCaps subtitle_caps = GST_STATIC_CAPS ("text/x-raw; "
501 "subpicture/x-pgs; subpicture/x-dvb; subpicture/x-dvd; "
502 "application/x-subtitle-unknown; application/x-ssa; application/x-ass; "
503 "subtitle/x-kate; application/x-kate");
506 is_subtitle_caps (const GstCaps * caps)
511 subs_caps = gst_static_caps_get (&subtitle_caps);
512 ret = gst_caps_can_intersect (caps, subs_caps);
513 gst_caps_unref (subs_caps);
518 static GstPadProbeReturn
519 got_subtitle_data (GstPad * pad, GstPadProbeInfo * info, GstDiscoverer * dc)
522 if (!(GST_IS_BUFFER (info->data) || (GST_IS_EVENT (info->data)
523 && GST_EVENT_TYPE ((GstEvent *) info->data) == GST_EVENT_GAP)))
524 return GST_PAD_PROBE_OK;
529 dc->priv->pending_subtitle_pads--;
531 if (dc->priv->pending_subtitle_pads == 0) {
532 GstMessage *msg = gst_message_new_application (NULL,
533 gst_structure_new_empty ("DiscovererDone"));
534 gst_element_post_message ((GstElement *) dc->priv->pipeline, msg);
538 return GST_PAD_PROBE_REMOVE;
543 uridecodebin_source_changed_cb (GstElement * uridecodebin,
544 GParamSpec * pspec, GstDiscoverer * dc)
547 /* get a handle to the source */
548 g_object_get (uridecodebin, pspec->name, &src, NULL);
550 GST_DEBUG_OBJECT (dc, "got a new source %p", src);
552 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_SOURCE_SETUP], 0, src);
553 gst_object_unref (src);
557 uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
561 GstPad *sinkpad = NULL;
564 GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
566 ps = g_slice_new0 (PrivateStream);
570 ps->queue = gst_element_factory_make ("queue", NULL);
571 ps->sink = gst_element_factory_make ("fakesink", NULL);
573 if (G_UNLIKELY (ps->queue == NULL || ps->sink == NULL))
576 g_object_set (ps->sink, "silent", TRUE, NULL);
577 g_object_set (ps->queue, "max-size-buffers", 1, "silent", TRUE, NULL);
579 caps = gst_pad_query_caps (pad, NULL);
581 sinkpad = gst_element_get_static_pad (ps->queue, "sink");
585 if (is_subtitle_caps (caps)) {
586 /* Subtitle streams are sparse and may not provide any information - don't
587 * wait for data to preroll */
588 gst_pad_add_probe (sinkpad, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
589 (GstPadProbeCallback) got_subtitle_data, dc, NULL);
590 g_object_set (ps->sink, "async", FALSE, NULL);
592 dc->priv->pending_subtitle_pads++;
596 gst_caps_unref (caps);
598 gst_bin_add_many (dc->priv->pipeline, ps->queue, ps->sink, NULL);
600 if (!gst_element_link_pads_full (ps->queue, "src", ps->sink, "sink",
601 GST_PAD_LINK_CHECK_NOTHING))
603 if (!gst_element_sync_state_with_parent (ps->sink))
605 if (!gst_element_sync_state_with_parent (ps->queue))
608 if (gst_pad_link_full (pad, sinkpad,
609 GST_PAD_LINK_CHECK_NOTHING) != GST_PAD_LINK_OK)
611 gst_object_unref (sinkpad);
613 /* Add an event probe */
614 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
615 (GstPadProbeCallback) _event_probe, ps, NULL);
618 dc->priv->streams = g_list_append (dc->priv->streams, ps);
621 GST_DEBUG_OBJECT (dc, "Done handling pad");
626 GST_ERROR_OBJECT (dc, "Error while handling pad");
628 gst_object_unref (sinkpad);
630 gst_object_unref (ps->queue);
632 gst_object_unref (ps->sink);
633 g_slice_free (PrivateStream, ps);
638 uridecodebin_pad_removed_cb (GstElement * uridecodebin, GstPad * pad,
645 GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
647 /* Find the PrivateStream */
649 for (tmp = dc->priv->streams; tmp; tmp = tmp->next) {
650 ps = (PrivateStream *) tmp->data;
657 GST_DEBUG ("The removed pad wasn't controlled by us !");
661 dc->priv->streams = g_list_delete_link (dc->priv->streams, tmp);
664 gst_element_set_state (ps->sink, GST_STATE_NULL);
665 gst_element_set_state (ps->queue, GST_STATE_NULL);
666 gst_element_unlink (ps->queue, ps->sink);
668 sinkpad = gst_element_get_static_pad (ps->queue, "sink");
669 gst_pad_unlink (pad, sinkpad);
670 gst_object_unref (sinkpad);
672 /* references removed here */
673 gst_bin_remove_many (dc->priv->pipeline, ps->sink, ps->queue, NULL);
676 gst_tag_list_free (ps->tags);
679 gst_toc_unref (ps->toc);
682 g_slice_free (PrivateStream, ps);
684 GST_DEBUG ("Done handling pad");
687 static GstStructure *
688 collect_stream_information (GstDiscoverer * dc, PrivateStream * ps, guint idx)
694 stname = g_strdup_printf ("stream-%02d", idx);
695 st = gst_structure_new_empty (stname);
699 caps = gst_pad_get_current_caps (ps->pad);
701 GST_WARNING ("Couldn't get negotiated caps from %s:%s",
702 GST_DEBUG_PAD_NAME (ps->pad));
703 caps = gst_pad_query_caps (ps->pad, NULL);
706 GST_DEBUG ("Got caps %" GST_PTR_FORMAT, caps);
707 gst_structure_id_set (st, _CAPS_QUARK, GST_TYPE_CAPS, caps, NULL);
709 gst_caps_unref (caps);
712 gst_structure_id_set (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, ps->tags, NULL);
714 gst_structure_id_set (st, _TOC_QUARK, GST_TYPE_TOC, ps->toc, NULL);
719 /* takes ownership of new_tags, may replace *taglist with a new one */
721 gst_discoverer_merge_and_replace_tags (GstTagList ** taglist,
722 GstTagList * new_tags)
724 if (new_tags == NULL)
727 if (*taglist == NULL) {
732 gst_tag_list_insert (*taglist, new_tags, GST_TAG_MERGE_REPLACE);
733 gst_tag_list_free (new_tags);
736 /* Parses a set of caps and tags in st and populates a GstDiscovererStreamInfo
737 * structure (parent, if !NULL, otherwise it allocates one)
739 static GstDiscovererStreamInfo *
740 collect_information (GstDiscoverer * dc, const GstStructure * st,
741 GstDiscovererStreamInfo * parent)
744 GstStructure *caps_st;
751 if (!st || !gst_structure_id_has_field (st, _CAPS_QUARK)) {
752 GST_WARNING ("Couldn't find caps !");
754 return gst_discoverer_stream_info_ref (parent);
756 return (GstDiscovererStreamInfo *)
757 g_object_new (GST_TYPE_DISCOVERER_STREAM_INFO, NULL);
760 gst_structure_id_get (st, _CAPS_QUARK, GST_TYPE_CAPS, &caps, NULL);
761 caps_st = gst_caps_get_structure (caps, 0);
762 name = gst_structure_get_name (caps_st);
764 if (g_str_has_prefix (name, "audio/")) {
765 GstDiscovererAudioInfo *info;
766 const gchar *format_str;
769 info = (GstDiscovererAudioInfo *) gst_discoverer_stream_info_ref (parent);
771 info = (GstDiscovererAudioInfo *)
772 g_object_new (GST_TYPE_DISCOVERER_AUDIO_INFO, NULL);
773 info->parent.caps = gst_caps_ref (caps);
776 if (gst_structure_get_int (caps_st, "rate", &tmp))
777 info->sample_rate = (guint) tmp;
779 if (gst_structure_get_int (caps_st, "channels", &tmp))
780 info->channels = (guint) tmp;
782 /* FIXME: we only want to extract depth if raw audio is what's in the
783 * container (i.e. not if there is a decoder involved) */
784 format_str = gst_structure_get_string (caps_st, "format");
785 if (format_str != NULL) {
786 const GstAudioFormatInfo *finfo;
787 GstAudioFormat format;
789 format = gst_audio_format_from_string (format_str);
790 finfo = gst_audio_format_get_info (format);
791 info->depth = GST_AUDIO_FORMAT_INFO_DEPTH (finfo);
794 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
795 gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st, NULL);
796 if (gst_tag_list_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
797 gst_tag_list_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
798 info->bitrate = utmp;
800 if (gst_tag_list_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
801 info->max_bitrate = utmp;
803 /* FIXME: Is it worth it to remove the tags we've parsed? */
804 gst_discoverer_merge_and_replace_tags (&info->parent.tags, tags_st);
807 if (gst_structure_id_has_field (st, _TOC_QUARK)) {
808 gst_structure_id_get (st, _TOC_QUARK, GST_TYPE_TOC, &toc_st, NULL);
809 info->parent.toc = toc_st;
812 if (!info->language && ((GstDiscovererStreamInfo *) info)->tags) {
814 if (gst_tag_list_get_string (((GstDiscovererStreamInfo *) info)->tags,
815 GST_TAG_LANGUAGE_CODE, &language)) {
816 info->language = language;
820 gst_caps_unref (caps);
821 return (GstDiscovererStreamInfo *) info;
823 } else if (g_str_has_prefix (name, "video/") ||
824 g_str_has_prefix (name, "image/")) {
825 GstDiscovererVideoInfo *info;
829 info = (GstDiscovererVideoInfo *) gst_discoverer_stream_info_ref (parent);
831 info = (GstDiscovererVideoInfo *)
832 g_object_new (GST_TYPE_DISCOVERER_VIDEO_INFO, NULL);
833 info->parent.caps = gst_caps_ref (caps);
836 if (gst_video_info_from_caps (&vinfo, caps)) {
837 info->width = (guint) vinfo.width;
838 info->height = (guint) vinfo.height;
840 info->depth = (guint) 0;
842 info->par_num = vinfo.par_n;
843 info->par_denom = vinfo.par_d;
845 info->framerate_num = vinfo.fps_n;
846 info->framerate_denom = vinfo.fps_d;
849 vinfo.interlace_mode != GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
852 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
853 gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st, NULL);
854 if (gst_tag_list_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
855 gst_tag_list_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
856 info->bitrate = utmp;
858 if (gst_tag_list_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
859 info->max_bitrate = utmp;
861 /* FIXME: Is it worth it to remove the tags we've parsed? */
862 gst_discoverer_merge_and_replace_tags (&info->parent.tags,
863 (GstTagList *) tags_st);
866 if (gst_structure_id_has_field (st, _TOC_QUARK)) {
867 gst_structure_id_get (st, _TOC_QUARK, GST_TYPE_TOC, &toc_st, NULL);
868 info->parent.toc = toc_st;
871 gst_caps_unref (caps);
872 return (GstDiscovererStreamInfo *) info;
874 } else if (is_subtitle_caps (caps)) {
875 GstDiscovererSubtitleInfo *info;
879 (GstDiscovererSubtitleInfo *) gst_discoverer_stream_info_ref (parent);
881 info = (GstDiscovererSubtitleInfo *)
882 g_object_new (GST_TYPE_DISCOVERER_SUBTITLE_INFO, NULL);
883 info->parent.caps = gst_caps_ref (caps);
886 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
887 const gchar *language;
889 gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st, NULL);
891 language = gst_structure_get_string (caps_st, GST_TAG_LANGUAGE_CODE);
893 info->language = g_strdup (language);
895 /* FIXME: Is it worth it to remove the tags we've parsed? */
896 gst_discoverer_merge_and_replace_tags (&info->parent.tags, tags_st);
899 if (gst_structure_id_has_field (st, _TOC_QUARK)) {
900 gst_structure_id_get (st, _TOC_QUARK, GST_TYPE_TOC, &toc_st, NULL);
901 info->parent.toc = toc_st;
904 if (!info->language && ((GstDiscovererStreamInfo *) info)->tags) {
906 if (gst_tag_list_get_string (((GstDiscovererStreamInfo *) info)->tags,
907 GST_TAG_LANGUAGE_CODE, &language)) {
908 info->language = language;
912 gst_caps_unref (caps);
913 return (GstDiscovererStreamInfo *) info;
916 /* None of the above - populate what information we can */
917 GstDiscovererStreamInfo *info;
920 info = gst_discoverer_stream_info_ref (parent);
922 info = (GstDiscovererStreamInfo *)
923 g_object_new (GST_TYPE_DISCOVERER_STREAM_INFO, NULL);
924 info->caps = gst_caps_ref (caps);
927 if (gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st,
929 gst_discoverer_merge_and_replace_tags (&info->tags, tags_st);
932 if (gst_structure_id_get (st, _TOC_QUARK, GST_TYPE_TOC, &toc_st, NULL)) {
936 gst_caps_unref (caps);
942 static GstStructure *
943 find_stream_for_node (GstDiscoverer * dc, const GstStructure * topology)
946 GstPad *target_pad = NULL;
947 GstStructure *st = NULL;
952 if (!gst_structure_id_has_field (topology, _TOPOLOGY_PAD_QUARK)) {
953 GST_DEBUG ("Could not find pad for node %" GST_PTR_FORMAT, topology);
957 gst_structure_id_get (topology, _TOPOLOGY_PAD_QUARK,
958 GST_TYPE_PAD, &pad, NULL);
960 if (!dc->priv->streams) {
961 gst_object_unref (pad);
965 for (i = 0, tmp = dc->priv->streams; tmp; tmp = tmp->next, i++) {
966 ps = (PrivateStream *) tmp->data;
968 target_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (ps->pad));
969 gst_object_unref (target_pad);
971 if (target_pad == pad)
976 st = collect_stream_information (dc, ps, i);
978 gst_object_unref (pad);
984 child_is_raw_stream (const GstCaps * parent, const GstCaps * child)
986 const GstStructure *st1, *st2;
987 const gchar *name1, *name2;
989 st1 = gst_caps_get_structure (parent, 0);
990 name1 = gst_structure_get_name (st1);
991 st2 = gst_caps_get_structure (child, 0);
992 name2 = gst_structure_get_name (st2);
994 if ((g_str_has_prefix (name1, "audio/") &&
995 g_str_has_prefix (name2, "audio/x-raw")) ||
996 ((g_str_has_prefix (name1, "video/") ||
997 g_str_has_prefix (name1, "image/")) &&
998 g_str_has_prefix (name2, "video/x-raw"))) {
999 /* child is the "raw" sub-stream corresponding to parent */
1003 if (is_subtitle_caps (parent))
1009 /* If a parent is non-NULL, collected stream information will be appended to it
1010 * (and where the information exists, it will be overriden)
1012 static GstDiscovererStreamInfo *
1013 parse_stream_topology (GstDiscoverer * dc, const GstStructure * topology,
1014 GstDiscovererStreamInfo * parent)
1016 GstDiscovererStreamInfo *res = NULL;
1017 GstCaps *caps = NULL;
1018 const GValue *nval = NULL;
1020 GST_DEBUG ("parsing: %" GST_PTR_FORMAT, topology);
1022 nval = gst_structure_get_value (topology, "next");
1024 if (nval == NULL || GST_VALUE_HOLDS_STRUCTURE (nval)) {
1025 GstStructure *st = find_stream_for_node (dc, topology);
1026 gboolean add_to_list = TRUE;
1029 res = collect_information (dc, st, parent);
1030 gst_structure_free (st);
1032 /* Didn't find a stream structure, so let's just use the caps we have */
1033 res = collect_information (dc, topology, parent);
1037 /* FIXME : aggregate with information from main streams */
1038 GST_DEBUG ("Coudn't find 'next' ! might be the last entry");
1041 const GstStructure *st;
1043 st = gst_value_get_structure (nval);
1045 GST_DEBUG ("next is a structure %" GST_PTR_FORMAT, st);
1050 if (gst_structure_id_get (st, _CAPS_QUARK, GST_TYPE_CAPS, &caps, NULL)) {
1051 if (gst_caps_can_intersect (parent->caps, caps)) {
1052 /* We sometimes get an extra sub-stream from the parser. If this is
1053 * the case, we just replace the parent caps with this stream's caps
1054 * since they might contain more information */
1055 gst_caps_replace (&parent->caps, caps);
1057 parse_stream_topology (dc, st, parent);
1058 add_to_list = FALSE;
1059 } else if (child_is_raw_stream (parent->caps, caps)) {
1060 /* This is the "raw" stream corresponding to the parent. This
1061 * contains more information than the parent, tags etc. */
1062 parse_stream_topology (dc, st, parent);
1063 add_to_list = FALSE;
1065 GstDiscovererStreamInfo *next = parse_stream_topology (dc, st, NULL);
1067 next->previous = res;
1069 gst_caps_unref (caps);
1074 dc->priv->current_info->stream_list =
1075 g_list_append (dc->priv->current_info->stream_list, res);
1077 gst_discoverer_stream_info_unref (res);
1080 } else if (GST_VALUE_HOLDS_LIST (nval)) {
1082 GstDiscovererContainerInfo *cont;
1085 if (!gst_structure_id_get (topology, _CAPS_QUARK,
1086 GST_TYPE_CAPS, &caps, NULL))
1087 GST_WARNING ("Couldn't find caps !");
1089 len = gst_value_list_get_size (nval);
1090 GST_DEBUG ("next is a list of %d entries", len);
1092 cont = (GstDiscovererContainerInfo *)
1093 g_object_new (GST_TYPE_DISCOVERER_CONTAINER_INFO, NULL);
1094 cont->parent.caps = caps;
1095 res = (GstDiscovererStreamInfo *) cont;
1097 if (gst_structure_id_has_field (topology, _TAGS_QUARK)) {
1100 gst_structure_id_get (topology, _TAGS_QUARK,
1101 GST_TYPE_TAG_LIST, &tags, NULL);
1103 GST_DEBUG ("Merge tags %" GST_PTR_FORMAT, tags);
1106 gst_tag_list_merge (cont->parent.tags, (GstTagList *) tags,
1107 GST_TAG_MERGE_APPEND);
1108 gst_tag_list_free (tags);
1109 if (cont->parent.tags)
1110 gst_tag_list_free (cont->parent.tags);
1111 cont->parent.tags = tmp;
1112 GST_DEBUG ("Container info tags %" GST_PTR_FORMAT, tmp);
1115 for (i = 0; i < len; i++) {
1116 const GValue *subv = gst_value_list_get_value (nval, i);
1117 const GstStructure *subst = gst_value_get_structure (subv);
1118 GstDiscovererStreamInfo *substream;
1120 GST_DEBUG ("%d %" GST_PTR_FORMAT, i, subst);
1122 substream = parse_stream_topology (dc, subst, NULL);
1124 substream->previous = res;
1126 g_list_append (cont->streams,
1127 gst_discoverer_stream_info_ref (substream));
1134 /* Called when pipeline is pre-rolled */
1136 discoverer_collect (GstDiscoverer * dc)
1138 GST_DEBUG ("Collecting information");
1140 /* Stop the timeout handler if present */
1141 if (dc->priv->timeoutid) {
1142 g_source_remove (dc->priv->timeoutid);
1143 dc->priv->timeoutid = 0;
1146 if (dc->priv->streams) {
1147 /* FIXME : Make this querying optional */
1149 GstElement *pipeline = (GstElement *) dc->priv->pipeline;
1152 GST_DEBUG ("Attempting to query duration");
1154 if (gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur)) {
1155 GST_DEBUG ("Got duration %" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
1156 dc->priv->current_info->duration = (guint64) dur;
1158 GstStateChangeReturn sret;
1160 /* Some parsers may not even return a rough estimate right away, e.g.
1161 * because they've only processed a single frame so far, so if we
1162 * didn't get a duration the first time, spin a bit and try again.
1163 * Ugly, but still better than making parsers or other elements return
1164 * completely bogus values. We need some API extensions to solve this
1166 GST_INFO ("No duration yet, try a bit harder..");
1167 sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
1168 if (sret != GST_STATE_CHANGE_FAILURE) {
1171 for (i = 0; i < 2; ++i) {
1172 g_usleep (G_USEC_PER_SEC / 20);
1173 if (gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur)
1175 GST_DEBUG ("Got duration %" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
1176 dc->priv->current_info->duration = (guint64) dur;
1180 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1184 if (dc->priv->seeking_query) {
1185 if (gst_element_query (pipeline, dc->priv->seeking_query)) {
1189 gst_query_parse_seeking (dc->priv->seeking_query, &format,
1190 &seekable, NULL, NULL);
1191 if (format == GST_FORMAT_TIME) {
1192 GST_DEBUG ("Got seekable %d", seekable);
1193 dc->priv->current_info->seekable = seekable;
1199 if (dc->priv->current_topology)
1200 dc->priv->current_info->stream_info = parse_stream_topology (dc,
1201 dc->priv->current_topology, NULL);
1204 * Images need some special handling. They do not have a duration, have
1205 * caps named image/<foo> (th exception being MJPEG video which is also
1206 * type image/jpeg), and should consist of precisely one stream (actually
1207 * initially there are 2, the image and raw stream, but we squash these
1208 * while parsing the stream topology). At some point, if we find that these
1209 * conditions are not sufficient, we can count the number of decoders and
1210 * parsers in the chain, and if there's more than one decoder, or any
1211 * parser at all, we should not mark this as an image.
1213 if (dc->priv->current_info->duration == 0 &&
1214 dc->priv->current_info->stream_info != NULL &&
1215 dc->priv->current_info->stream_info->next == NULL) {
1216 GstDiscovererStreamInfo *stream_info;
1219 stream_info = dc->priv->current_info->stream_info;
1220 st = gst_caps_get_structure (stream_info->caps, 0);
1222 if (g_str_has_prefix (gst_structure_get_name (st), "image/"))
1223 ((GstDiscovererVideoInfo *) stream_info)->is_image = TRUE;
1227 if (dc->priv->async) {
1228 GST_DEBUG ("Emitting 'discoverered'");
1229 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_DISCOVERED], 0,
1230 dc->priv->current_info, dc->priv->current_error);
1231 /* Clients get a copy of current_info since it is a boxed type */
1232 gst_discoverer_info_unref (dc->priv->current_info);
1233 dc->priv->current_info = NULL;
1238 get_async_cb (gpointer cb_data, GSource * source, GSourceFunc * func,
1241 *func = (GSourceFunc) async_timeout_cb;
1245 /* Wrapper since GSourceCallbackFuncs don't expect a return value from ref() */
1247 _void_g_object_ref (gpointer object)
1249 g_object_ref (G_OBJECT (object));
1253 handle_current_async (GstDiscoverer * dc)
1256 static GSourceCallbackFuncs cb_funcs = {
1262 /* Attach a timeout to the main context */
1263 source = g_timeout_source_new (dc->priv->timeout / GST_MSECOND);
1264 g_source_set_callback_indirect (source, g_object_ref (dc), &cb_funcs);
1265 dc->priv->timeoutid = g_source_attach (source, dc->priv->ctx);
1266 g_source_unref (source);
1270 /* Returns TRUE if processing should stop */
1272 handle_message (GstDiscoverer * dc, GstMessage * msg)
1274 gboolean done = FALSE;
1276 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "got a %s message",
1277 GST_MESSAGE_TYPE_NAME (msg));
1279 switch (GST_MESSAGE_TYPE (msg)) {
1280 case GST_MESSAGE_ERROR:{
1284 gst_message_parse_error (msg, &gerr, &debug);
1285 GST_WARNING_OBJECT (GST_MESSAGE_SRC (msg),
1286 "Got an error [debug:%s], [message:%s]", debug, gerr->message);
1287 dc->priv->current_error = gerr;
1290 /* We need to stop */
1293 /* Don't override missing plugin result code for missing plugin errors */
1294 if (dc->priv->current_info->result != GST_DISCOVERER_MISSING_PLUGINS ||
1295 (!g_error_matches (gerr, GST_CORE_ERROR,
1296 GST_CORE_ERROR_MISSING_PLUGIN) &&
1297 !g_error_matches (gerr, GST_STREAM_ERROR,
1298 GST_STREAM_ERROR_CODEC_NOT_FOUND))) {
1299 GST_DEBUG ("Setting result to ERROR");
1300 dc->priv->current_info->result = GST_DISCOVERER_ERROR;
1305 case GST_MESSAGE_EOS:
1306 GST_DEBUG ("Got EOS !");
1310 case GST_MESSAGE_APPLICATION:{
1312 gboolean async_done;
1313 name = gst_structure_get_name (gst_message_get_structure (msg));
1314 /* Maybe ASYNC_DONE is received & we're just waiting for subtitle tags */
1316 async_done = dc->priv->async_done;
1318 if (g_str_equal (name, "DiscovererDone") && async_done)
1323 case GST_MESSAGE_ASYNC_DONE:
1324 if (GST_MESSAGE_SRC (msg) == (GstObject *) dc->priv->pipeline) {
1325 GST_DEBUG ("Finished changing state asynchronously");
1327 if (dc->priv->pending_subtitle_pads == 0) {
1330 /* Remember that ASYNC_DONE has been received, wait for subtitles */
1331 dc->priv->async_done = TRUE;
1338 case GST_MESSAGE_ELEMENT:
1341 const GstStructure *structure;
1343 structure = gst_message_get_structure (msg);
1344 sttype = gst_structure_get_name_id (structure);
1345 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1346 "structure %" GST_PTR_FORMAT, structure);
1347 if (sttype == _MISSING_PLUGIN_QUARK) {
1348 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1349 "Setting result to MISSING_PLUGINS");
1350 dc->priv->current_info->result = GST_DISCOVERER_MISSING_PLUGINS;
1351 if (dc->priv->current_info->misc)
1352 gst_structure_free (dc->priv->current_info->misc);
1353 dc->priv->current_info->misc = gst_structure_copy (structure);
1354 } else if (sttype == _STREAM_TOPOLOGY_QUARK) {
1355 if (dc->priv->current_topology)
1356 gst_structure_free (dc->priv->current_topology);
1357 dc->priv->current_topology = gst_structure_copy (structure);
1362 case GST_MESSAGE_TAG:
1364 GstTagList *tl, *tmp;
1366 gst_message_parse_tag (msg, &tl);
1367 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Got tags %" GST_PTR_FORMAT, tl);
1368 /* Merge with current tags */
1370 gst_tag_list_merge (dc->priv->current_info->tags, tl,
1371 GST_TAG_MERGE_APPEND);
1372 gst_tag_list_free (tl);
1373 if (dc->priv->current_info->tags)
1374 gst_tag_list_free (dc->priv->current_info->tags);
1375 dc->priv->current_info->tags = tmp;
1376 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Current info %p, tags %"
1377 GST_PTR_FORMAT, dc->priv->current_info, tmp);
1381 case GST_MESSAGE_TOC:
1385 gst_message_parse_toc (msg, &tmp, NULL);
1386 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Got toc %" GST_PTR_FORMAT, tmp);
1387 dc->priv->current_info->toc = tmp;
1388 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Current info %p, toc %"
1389 GST_PTR_FORMAT, dc->priv->current_info, tmp);
1401 handle_current_sync (GstDiscoverer * dc)
1404 gdouble deadline = ((gdouble) dc->priv->timeout) / GST_SECOND;
1406 gboolean done = FALSE;
1408 timer = g_timer_new ();
1409 g_timer_start (timer);
1412 /* poll bus with timeout */
1413 /* FIXME : make the timeout more fine-tuned */
1414 if ((msg = gst_bus_timed_pop (dc->priv->bus, GST_SECOND / 2))) {
1415 done = handle_message (dc, msg);
1416 gst_message_unref (msg);
1418 } while (!done && (g_timer_elapsed (timer, NULL) < deadline));
1422 GST_DEBUG ("we timed out! Setting result to TIMEOUT");
1423 dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
1428 g_timer_stop (timer);
1429 g_timer_destroy (timer);
1433 _setup_locked (GstDiscoverer * dc)
1435 GstStateChangeReturn ret;
1437 GST_DEBUG ("Setting up");
1439 /* Pop URI off the pending URI list */
1440 dc->priv->current_info =
1441 (GstDiscovererInfo *) g_object_new (GST_TYPE_DISCOVERER_INFO, NULL);
1442 dc->priv->current_info->uri = (gchar *) dc->priv->pending_uris->data;
1443 dc->priv->pending_uris =
1444 g_list_delete_link (dc->priv->pending_uris, dc->priv->pending_uris);
1446 /* set uri on uridecodebin */
1447 g_object_set (dc->priv->uridecodebin, "uri", dc->priv->current_info->uri,
1450 GST_DEBUG ("Current is now %s", dc->priv->current_info->uri);
1452 dc->priv->processing = TRUE;
1454 /* set pipeline to PAUSED */
1456 GST_DEBUG ("Setting pipeline to PAUSED");
1458 gst_element_set_state ((GstElement *) dc->priv->pipeline,
1462 GST_DEBUG_OBJECT (dc, "Pipeline going to PAUSED : %s",
1463 gst_element_state_change_return_get_name (ret));
1467 discoverer_cleanup (GstDiscoverer * dc)
1469 GST_DEBUG ("Cleaning up");
1471 gst_bus_set_flushing (dc->priv->bus, TRUE);
1472 gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_READY);
1473 gst_bus_set_flushing (dc->priv->bus, FALSE);
1476 if (dc->priv->current_error)
1477 g_error_free (dc->priv->current_error);
1478 dc->priv->current_error = NULL;
1479 if (dc->priv->current_topology) {
1480 gst_structure_free (dc->priv->current_topology);
1481 dc->priv->current_topology = NULL;
1484 dc->priv->current_info = NULL;
1486 dc->priv->pending_subtitle_pads = 0;
1487 dc->priv->async_done = FALSE;
1489 /* Try popping the next uri */
1490 if (dc->priv->async) {
1491 if (dc->priv->pending_uris != NULL) {
1495 handle_current_async (dc);
1499 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_FINISHED], 0);
1508 discoverer_bus_cb (GstBus * bus, GstMessage * msg, GstDiscoverer * dc)
1510 if (dc->priv->processing) {
1511 if (handle_message (dc, msg)) {
1512 GST_DEBUG ("Stopping asynchronously");
1513 /* Serialise with _event_probe() */
1515 dc->priv->processing = FALSE;
1517 discoverer_collect (dc);
1518 discoverer_cleanup (dc);
1524 async_timeout_cb (GstDiscoverer * dc)
1526 if (!g_source_is_destroyed (g_main_current_source ())) {
1527 dc->priv->timeoutid = 0;
1528 GST_DEBUG ("Setting result to TIMEOUT");
1529 dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
1530 dc->priv->processing = FALSE;
1531 discoverer_collect (dc);
1532 discoverer_cleanup (dc);
1537 /* If there is a pending URI, it will pop it from the list of pending
1538 * URIs and start the discovery on it.
1540 * Returns GST_DISCOVERER_OK if the next URI was popped and is processing,
1541 * else a error flag.
1543 static GstDiscovererResult
1544 start_discovering (GstDiscoverer * dc)
1546 GstDiscovererResult res = GST_DISCOVERER_OK;
1548 GST_DEBUG ("Starting");
1551 if (dc->priv->pending_uris == NULL) {
1552 GST_WARNING ("No URI to process");
1553 res = GST_DISCOVERER_URI_INVALID;
1558 if (dc->priv->current_info != NULL) {
1559 GST_WARNING ("Already processing a file");
1560 res = GST_DISCOVERER_BUSY;
1565 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_STARTING], 0);
1571 if (dc->priv->async)
1572 handle_current_async (dc);
1574 handle_current_sync (dc);
1582 * gst_discoverer_start:
1583 * @discoverer: A #GstDiscoverer
1585 * Allow asynchronous discovering of URIs to take place.
1586 * A #GMainLoop must be available for #GstDiscoverer to properly work in
1587 * asynchronous mode.
1590 gst_discoverer_start (GstDiscoverer * discoverer)
1593 GMainContext *ctx = NULL;
1595 GST_DEBUG_OBJECT (discoverer, "Starting...");
1597 if (discoverer->priv->async) {
1598 GST_DEBUG_OBJECT (discoverer, "We were already started");
1602 discoverer->priv->async = TRUE;
1603 discoverer->priv->running = TRUE;
1605 ctx = g_main_context_get_thread_default ();
1607 /* Connect to bus signals */
1609 ctx = g_main_context_default ();
1611 source = gst_bus_create_watch (discoverer->priv->bus);
1612 g_source_set_callback (source, (GSourceFunc) gst_bus_async_signal_func,
1614 discoverer->priv->sourceid = g_source_attach (source, ctx);
1615 g_source_unref (source);
1616 discoverer->priv->ctx = g_main_context_ref (ctx);
1618 start_discovering (discoverer);
1619 GST_DEBUG_OBJECT (discoverer, "Started");
1623 * gst_discoverer_stop:
1624 * @discoverer: A #GstDiscoverer
1626 * Stop the discovery of any pending URIs and clears the list of
1627 * pending URIS (if any).
1630 gst_discoverer_stop (GstDiscoverer * discoverer)
1632 GST_DEBUG_OBJECT (discoverer, "Stopping...");
1634 if (!discoverer->priv->async) {
1635 GST_DEBUG_OBJECT (discoverer,
1636 "We were already stopped, or running synchronously");
1640 DISCO_LOCK (discoverer);
1641 if (discoverer->priv->processing) {
1642 /* We prevent any further processing by setting the bus to
1643 * flushing and setting the pipeline to READY.
1644 * _reset() will take care of the rest of the cleanup */
1645 if (discoverer->priv->bus)
1646 gst_bus_set_flushing (discoverer->priv->bus, TRUE);
1647 if (discoverer->priv->pipeline)
1648 gst_element_set_state ((GstElement *) discoverer->priv->pipeline,
1651 discoverer->priv->running = FALSE;
1652 DISCO_UNLOCK (discoverer);
1654 /* Remove timeout handler */
1655 if (discoverer->priv->timeoutid) {
1656 g_source_remove (discoverer->priv->timeoutid);
1657 discoverer->priv->timeoutid = 0;
1659 /* Remove signal watch */
1660 if (discoverer->priv->sourceid) {
1661 g_source_remove (discoverer->priv->sourceid);
1662 discoverer->priv->sourceid = 0;
1664 /* Unref main context */
1665 if (discoverer->priv->ctx) {
1666 g_main_context_unref (discoverer->priv->ctx);
1667 discoverer->priv->ctx = NULL;
1669 discoverer_reset (discoverer);
1671 discoverer->priv->async = FALSE;
1673 GST_DEBUG_OBJECT (discoverer, "Stopped");
1677 * gst_discoverer_discover_uri_async:
1678 * @discoverer: A #GstDiscoverer
1679 * @uri: the URI to add.
1681 * Appends the given @uri to the list of URIs to discoverer. The actual
1682 * discovery of the @uri will only take place if gst_discoverer_start() has
1685 * A copy of @uri will be made internally, so the caller can safely g_free()
1688 * Returns: %TRUE if the @uri was successfully appended to the list of pending
1692 gst_discoverer_discover_uri_async (GstDiscoverer * discoverer,
1697 GST_DEBUG_OBJECT (discoverer, "uri : %s", uri);
1699 DISCO_LOCK (discoverer);
1700 can_run = (discoverer->priv->pending_uris == NULL);
1701 discoverer->priv->pending_uris =
1702 g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
1703 DISCO_UNLOCK (discoverer);
1706 start_discovering (discoverer);
1712 /* Synchronous mode */
1714 * gst_discoverer_discover_uri:
1715 * @discoverer: A #GstDiscoverer
1716 * @uri: The URI to run on.
1717 * @err: (out) (allow-none): If an error occurred, this field will be filled in.
1719 * Synchronously discovers the given @uri.
1721 * A copy of @uri will be made internally, so the caller can safely g_free()
1724 * Returns: (transfer full): the result of the scanning. Can be %NULL if an
1728 gst_discoverer_discover_uri (GstDiscoverer * discoverer, const gchar * uri,
1731 GstDiscovererResult res = 0;
1732 GstDiscovererInfo *info;
1734 GST_DEBUG_OBJECT (discoverer, "uri:%s", uri);
1736 DISCO_LOCK (discoverer);
1737 if (G_UNLIKELY (discoverer->priv->current_info)) {
1738 DISCO_UNLOCK (discoverer);
1739 GST_WARNING_OBJECT (discoverer, "Already handling a uri");
1743 discoverer->priv->pending_uris =
1744 g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
1745 DISCO_UNLOCK (discoverer);
1747 res = start_discovering (discoverer);
1748 discoverer_collect (discoverer);
1752 if (discoverer->priv->current_error)
1753 *err = g_error_copy (discoverer->priv->current_error);
1757 if (res != GST_DISCOVERER_OK) {
1758 GST_DEBUG ("Setting result to %d (was %d)", res,
1759 discoverer->priv->current_info->result);
1760 discoverer->priv->current_info->result = res;
1762 info = discoverer->priv->current_info;
1764 discoverer_cleanup (discoverer);
1770 * gst_discoverer_new:
1771 * @timeout: timeout per file, in nanoseconds. Allowed are values between
1772 * one second (#GST_SECOND) and one hour (3600 * #GST_SECOND)
1773 * @err: a pointer to a #GError. can be %NULL
1775 * Creates a new #GstDiscoverer with the provided timeout.
1777 * Returns: (transfer full): The new #GstDiscoverer.
1778 * If an error occurred when creating the discoverer, @err will be set
1779 * accordingly and %NULL will be returned. If @err is set, the caller must
1780 * free it when no longer needed using g_error_free().
1783 gst_discoverer_new (GstClockTime timeout, GError ** err)
1787 res = g_object_new (GST_TYPE_DISCOVERER, "timeout", timeout, NULL);
1788 if (res->priv->uridecodebin == NULL) {
1790 *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
1791 "Couldn't create 'uridecodebin' element");
1792 gst_object_unref (res);