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>
48 #include "pbutils-private.h"
50 #include "gst/glib-compat-private.h"
52 GST_DEBUG_CATEGORY_STATIC (discoverer_debug);
53 #define GST_CAT_DEFAULT discoverer_debug
55 static GQuark _CAPS_QUARK;
56 static GQuark _TAGS_QUARK;
57 static GQuark _TOC_QUARK;
58 static GQuark _MISSING_PLUGIN_QUARK;
59 static GQuark _STREAM_TOPOLOGY_QUARK;
60 static GQuark _TOPOLOGY_PAD_QUARK;
73 struct _GstDiscovererPrivate
77 /* allowed time to discover each uri in nanoseconds */
80 /* list of pending URI to process (current excluded) */
85 /* TRUE if processing a URI */
88 /* TRUE if discoverer has been started */
91 /* TRUE if ASYNC_DONE has been received (need to check for subtitle tags) */
95 GstDiscovererInfo *current_info;
96 GError *current_error;
97 GstStructure *current_topology;
99 /* List of private streams */
102 /* List of these sinks and their handler IDs (to remove the probe) */
103 guint pending_subtitle_pads;
105 /* Global elements */
107 GstElement *uridecodebin;
110 GType decodebin_type;
112 /* Custom main context variables */
117 /* reusable queries */
118 GstQuery *seeking_query;
120 /* Handler ids for various callbacks */
122 gulong pad_remove_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,
154 #define DEFAULT_PROP_TIMEOUT 15 * GST_SECOND
162 static guint gst_discoverer_signals[LAST_SIGNAL] = { 0 };
164 static void gst_discoverer_set_timeout (GstDiscoverer * dc,
165 GstClockTime timeout);
166 static gboolean async_timeout_cb (GstDiscoverer * dc);
168 static void discoverer_bus_cb (GstBus * bus, GstMessage * msg,
170 static void uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
172 static void uridecodebin_pad_removed_cb (GstElement * uridecodebin,
173 GstPad * pad, GstDiscoverer * dc);
175 static void gst_discoverer_dispose (GObject * dc);
176 static void gst_discoverer_set_property (GObject * object, guint prop_id,
177 const GValue * value, GParamSpec * pspec);
178 static void gst_discoverer_get_property (GObject * object, guint prop_id,
179 GValue * value, GParamSpec * pspec);
182 gst_discoverer_class_init (GstDiscovererClass * klass)
184 GObjectClass *gobject_class = (GObjectClass *) klass;
186 gobject_class->dispose = gst_discoverer_dispose;
188 gobject_class->set_property = gst_discoverer_set_property;
189 gobject_class->get_property = gst_discoverer_get_property;
191 g_type_class_add_private (klass, sizeof (GstDiscovererPrivate));
195 * GstDiscoverer:timeout:
197 * The duration (in nanoseconds) after which the discovery of an individual
200 * If the discovery of a URI times out, the %GST_DISCOVERER_TIMEOUT will be
201 * set on the result flags.
203 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
204 g_param_spec_uint64 ("timeout", "timeout", "Timeout",
205 GST_SECOND, 3600 * GST_SECOND, DEFAULT_PROP_TIMEOUT,
206 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
210 * GstDiscoverer::finished:
211 * @discoverer: the #GstDiscoverer
213 * Will be emitted when all pending URIs have been processed.
215 gst_discoverer_signals[SIGNAL_FINISHED] =
216 g_signal_new ("finished", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
217 G_STRUCT_OFFSET (GstDiscovererClass, finished),
218 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
221 * GstDiscoverer::starting:
222 * @discoverer: the #GstDiscoverer
224 * Will be emitted when the discover starts analyzing the pending URIs
226 gst_discoverer_signals[SIGNAL_STARTING] =
227 g_signal_new ("starting", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
228 G_STRUCT_OFFSET (GstDiscovererClass, starting),
229 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
232 * GstDiscoverer::discovered:
233 * @discoverer: the #GstDiscoverer
234 * @info: the results #GstDiscovererInfo
235 * @error: (type GLib.Error): #GError, which will be non-NULL if an error
236 * occurred during discovery
238 * Will be emitted when all information on a URI could be discovered.
240 gst_discoverer_signals[SIGNAL_DISCOVERED] =
241 g_signal_new ("discovered", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
242 G_STRUCT_OFFSET (GstDiscovererClass, discovered),
243 NULL, NULL, g_cclosure_marshal_generic,
244 G_TYPE_NONE, 2, GST_TYPE_DISCOVERER_INFO,
245 G_TYPE_ERROR | G_SIGNAL_TYPE_STATIC_SCOPE);
249 uridecodebin_element_added_cb (GstElement * uridecodebin,
250 GstElement * child, GstDiscoverer * dc)
252 GST_DEBUG ("New element added to uridecodebin : %s",
253 GST_ELEMENT_NAME (child));
255 if (G_OBJECT_TYPE (child) == dc->priv->decodebin_type) {
256 g_object_set (child, "post-stream-topology", TRUE, NULL);
261 gst_discoverer_init (GstDiscoverer * dc)
264 GstFormat format = GST_FORMAT_TIME;
266 dc->priv = G_TYPE_INSTANCE_GET_PRIVATE (dc, GST_TYPE_DISCOVERER,
267 GstDiscovererPrivate);
269 dc->priv->timeout = DEFAULT_PROP_TIMEOUT;
270 dc->priv->async = FALSE;
271 dc->priv->async_done = FALSE;
273 dc->priv->lock = g_mutex_new ();
275 dc->priv->pending_subtitle_pads = 0;
277 GST_LOG ("Creating pipeline");
278 dc->priv->pipeline = (GstBin *) gst_pipeline_new ("Discoverer");
279 GST_LOG_OBJECT (dc, "Creating uridecodebin");
280 dc->priv->uridecodebin =
281 gst_element_factory_make ("uridecodebin", "discoverer-uri");
282 if (G_UNLIKELY (dc->priv->uridecodebin == NULL)) {
283 GST_ERROR ("Can't create uridecodebin");
286 GST_LOG_OBJECT (dc, "Adding uridecodebin to pipeline");
287 gst_bin_add (dc->priv->pipeline, dc->priv->uridecodebin);
289 dc->priv->pad_added_id =
290 g_signal_connect_object (dc->priv->uridecodebin, "pad-added",
291 G_CALLBACK (uridecodebin_pad_added_cb), dc, 0);
292 dc->priv->pad_remove_id =
293 g_signal_connect_object (dc->priv->uridecodebin, "pad-removed",
294 G_CALLBACK (uridecodebin_pad_removed_cb), dc, 0);
296 GST_LOG_OBJECT (dc, "Getting pipeline bus");
297 dc->priv->bus = gst_pipeline_get_bus ((GstPipeline *) dc->priv->pipeline);
299 dc->priv->bus_cb_id =
300 g_signal_connect_object (dc->priv->bus, "message",
301 G_CALLBACK (discoverer_bus_cb), dc, 0);
303 GST_DEBUG_OBJECT (dc, "Done initializing Discoverer");
305 /* This is ugly. We get the GType of decodebin so we can quickly detect
306 * when a decodebin is added to uridecodebin so we can set the
307 * post-stream-topology setting to TRUE */
308 dc->priv->element_added_id =
309 g_signal_connect_object (dc->priv->uridecodebin, "element-added",
310 G_CALLBACK (uridecodebin_element_added_cb), dc, 0);
311 tmp = gst_element_factory_make ("decodebin", NULL);
312 dc->priv->decodebin_type = G_OBJECT_TYPE (tmp);
313 gst_object_unref (tmp);
316 dc->priv->seeking_query = gst_query_new_seeking (format);
320 discoverer_reset (GstDiscoverer * dc)
322 GST_DEBUG_OBJECT (dc, "Resetting");
324 if (dc->priv->pending_uris) {
325 g_list_foreach (dc->priv->pending_uris, (GFunc) g_free, NULL);
326 g_list_free (dc->priv->pending_uris);
327 dc->priv->pending_uris = NULL;
330 if (dc->priv->pipeline)
331 gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_NULL);
334 #define DISCONNECT_SIGNAL(o,i) G_STMT_START{ \
335 if ((i) && g_signal_handler_is_connected ((o), (i))) \
336 g_signal_handler_disconnect ((o), (i)); \
341 gst_discoverer_dispose (GObject * obj)
343 GstDiscoverer *dc = (GstDiscoverer *) obj;
345 GST_DEBUG_OBJECT (dc, "Disposing");
347 discoverer_reset (dc);
349 if (G_LIKELY (dc->priv->pipeline)) {
350 /* Workaround for bug #118536 */
351 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_added_id);
352 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_remove_id);
353 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->element_added_id);
354 DISCONNECT_SIGNAL (dc->priv->bus, dc->priv->bus_cb_id);
356 /* pipeline was set to NULL in _reset */
357 gst_object_unref (dc->priv->pipeline);
358 gst_object_unref (dc->priv->bus);
360 dc->priv->pipeline = NULL;
361 dc->priv->uridecodebin = NULL;
362 dc->priv->bus = NULL;
365 gst_discoverer_stop (dc);
367 if (dc->priv->lock) {
368 g_mutex_free (dc->priv->lock);
369 dc->priv->lock = NULL;
372 if (dc->priv->seeking_query) {
373 gst_query_unref (dc->priv->seeking_query);
374 dc->priv->seeking_query = NULL;
377 G_OBJECT_CLASS (gst_discoverer_parent_class)->dispose (obj);
381 gst_discoverer_set_property (GObject * object, guint prop_id,
382 const GValue * value, GParamSpec * pspec)
384 GstDiscoverer *dc = (GstDiscoverer *) object;
388 gst_discoverer_set_timeout (dc, g_value_get_uint64 (value));
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
397 gst_discoverer_get_property (GObject * object, guint prop_id,
398 GValue * value, GParamSpec * pspec)
400 GstDiscoverer *dc = (GstDiscoverer *) object;
405 g_value_set_uint64 (value, dc->priv->timeout);
409 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
415 gst_discoverer_set_timeout (GstDiscoverer * dc, GstClockTime timeout)
417 GST_DEBUG_OBJECT (dc, "timeout : %" GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
419 /* FIXME : update current pending timeout if we're running */
421 dc->priv->timeout = timeout;
425 static GstPadProbeReturn
426 _event_probe (GstPad * pad, GstPadProbeInfo * info, PrivateStream * ps)
428 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
430 if (GST_EVENT_TYPE (event) == GST_EVENT_TAG) {
431 GstTagList *tl = NULL, *tmp;
433 gst_event_parse_tag (event, &tl);
434 GST_DEBUG_OBJECT (pad, "tags %" GST_PTR_FORMAT, tl);
436 /* If preroll is complete, drop these tags - the collected information is
437 * possibly already being processed and adding more tags would be racy */
438 if (G_LIKELY (ps->dc->priv->processing)) {
439 GST_DEBUG_OBJECT (pad, "private stream %p old tags %" GST_PTR_FORMAT, ps,
441 tmp = gst_tag_list_merge (ps->tags, tl, GST_TAG_MERGE_APPEND);
443 gst_tag_list_free (ps->tags);
445 GST_DEBUG_OBJECT (pad, "private stream %p new tags %" GST_PTR_FORMAT, ps,
448 GST_DEBUG_OBJECT (pad, "Dropping tags since preroll is done");
449 DISCO_UNLOCK (ps->dc);
452 if (GST_EVENT_TYPE (event) == GST_EVENT_TOC) {
455 gst_event_parse_toc (event, &tmp, NULL);
456 GST_DEBUG_OBJECT (pad, "toc %" GST_PTR_FORMAT, tmp);
459 if (G_LIKELY (ps->dc->priv->processing)) {
460 GST_DEBUG_OBJECT (pad, "private stream %p toc %" GST_PTR_FORMAT, ps, tmp);
462 GST_DEBUG_OBJECT (pad, "Dropping toc since preroll is done");
463 DISCO_UNLOCK (ps->dc);
466 return GST_PAD_PROBE_OK;
469 static GstStaticCaps subtitle_caps = GST_STATIC_CAPS ("text/plain; "
470 "text/x-pango-markup; subpicture/x-pgs; subpicture/x-dvb; "
471 "application/x-subtitle-unknown; application/x-ssa; application/x-ass; "
472 "subtitle/x-kate; application/x-kate; video/x-dvd-subpicture");
475 is_subtitle_caps (const GstCaps * caps)
480 subs_caps = gst_static_caps_get (&subtitle_caps);
481 ret = gst_caps_can_intersect (caps, subs_caps);
482 gst_caps_unref (subs_caps);
487 static GstPadProbeReturn
488 got_subtitle_data (GstPad * pad, GstPadProbeInfo * info, GstDiscoverer * dc)
491 if (!(GST_IS_BUFFER (info->data) || (GST_IS_EVENT (info->data)
492 && GST_EVENT_TYPE ((GstEvent *) info->data) == GST_EVENT_GAP)))
493 return GST_PAD_PROBE_OK;
498 dc->priv->pending_subtitle_pads--;
500 if (dc->priv->pending_subtitle_pads == 0) {
501 GstMessage *msg = gst_message_new_application (NULL,
502 gst_structure_new_empty ("DiscovererDone"));
503 gst_element_post_message ((GstElement *) dc->priv->pipeline, msg);
507 return GST_PAD_PROBE_REMOVE;
512 uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
516 GstPad *sinkpad = NULL;
519 GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
521 ps = g_slice_new0 (PrivateStream);
525 ps->queue = gst_element_factory_make ("queue", NULL);
526 ps->sink = gst_element_factory_make ("fakesink", NULL);
528 if (G_UNLIKELY (ps->queue == NULL || ps->sink == NULL))
531 g_object_set (ps->sink, "silent", TRUE, NULL);
532 g_object_set (ps->queue, "max-size-buffers", 1, "silent", TRUE, NULL);
534 caps = gst_pad_query_caps (pad, NULL);
536 sinkpad = gst_element_get_static_pad (ps->queue, "sink");
540 if (is_subtitle_caps (caps)) {
541 /* Subtitle streams are sparse and may not provide any information - don't
542 * wait for data to preroll */
543 gst_pad_add_probe (sinkpad, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
544 (GstPadProbeCallback) got_subtitle_data, dc, NULL);
545 g_object_set (ps->sink, "async", FALSE, NULL);
547 dc->priv->pending_subtitle_pads++;
551 gst_caps_unref (caps);
553 gst_bin_add_many (dc->priv->pipeline, ps->queue, ps->sink, NULL);
555 if (!gst_element_link_pads_full (ps->queue, "src", ps->sink, "sink",
556 GST_PAD_LINK_CHECK_NOTHING))
558 if (!gst_element_sync_state_with_parent (ps->sink))
560 if (!gst_element_sync_state_with_parent (ps->queue))
563 if (gst_pad_link_full (pad, sinkpad,
564 GST_PAD_LINK_CHECK_NOTHING) != GST_PAD_LINK_OK)
566 gst_object_unref (sinkpad);
568 /* Add an event probe */
569 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
570 (GstPadProbeCallback) _event_probe, ps, NULL);
573 dc->priv->streams = g_list_append (dc->priv->streams, ps);
576 GST_DEBUG_OBJECT (dc, "Done handling pad");
581 GST_ERROR_OBJECT (dc, "Error while handling pad");
583 gst_object_unref (sinkpad);
585 gst_object_unref (ps->queue);
587 gst_object_unref (ps->sink);
588 g_slice_free (PrivateStream, ps);
593 uridecodebin_pad_removed_cb (GstElement * uridecodebin, GstPad * pad,
600 GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
602 /* Find the PrivateStream */
604 for (tmp = dc->priv->streams; tmp; tmp = tmp->next) {
605 ps = (PrivateStream *) tmp->data;
612 GST_DEBUG ("The removed pad wasn't controlled by us !");
616 dc->priv->streams = g_list_delete_link (dc->priv->streams, tmp);
619 gst_element_set_state (ps->sink, GST_STATE_NULL);
620 gst_element_set_state (ps->queue, GST_STATE_NULL);
621 gst_element_unlink (ps->queue, ps->sink);
623 sinkpad = gst_element_get_static_pad (ps->queue, "sink");
624 gst_pad_unlink (pad, sinkpad);
625 gst_object_unref (sinkpad);
627 /* references removed here */
628 gst_bin_remove_many (dc->priv->pipeline, ps->sink, ps->queue, NULL);
631 gst_tag_list_free (ps->tags);
634 gst_toc_unref (ps->toc);
637 g_slice_free (PrivateStream, ps);
639 GST_DEBUG ("Done handling pad");
642 static GstStructure *
643 collect_stream_information (GstDiscoverer * dc, PrivateStream * ps, guint idx)
649 stname = g_strdup_printf ("stream-%02d", idx);
650 st = gst_structure_new_empty (stname);
654 caps = gst_pad_get_current_caps (ps->pad);
656 GST_WARNING ("Couldn't get negotiated caps from %s:%s",
657 GST_DEBUG_PAD_NAME (ps->pad));
658 caps = gst_pad_query_caps (ps->pad, NULL);
661 GST_DEBUG ("Got caps %" GST_PTR_FORMAT, caps);
662 gst_structure_id_set (st, _CAPS_QUARK, GST_TYPE_CAPS, caps, NULL);
664 gst_caps_unref (caps);
667 gst_structure_id_set (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, ps->tags, NULL);
669 gst_structure_id_set (st, _TOC_QUARK, GST_TYPE_TOC, ps->toc, NULL);
674 /* takes ownership of new_tags, may replace *taglist with a new one */
676 gst_discoverer_merge_and_replace_tags (GstTagList ** taglist,
677 GstTagList * new_tags)
679 if (new_tags == NULL)
682 if (*taglist == NULL) {
687 gst_tag_list_insert (*taglist, new_tags, GST_TAG_MERGE_REPLACE);
688 gst_tag_list_free (new_tags);
691 /* Parses a set of caps and tags in st and populates a GstDiscovererStreamInfo
692 * structure (parent, if !NULL, otherwise it allocates one)
694 static GstDiscovererStreamInfo *
695 collect_information (GstDiscoverer * dc, const GstStructure * st,
696 GstDiscovererStreamInfo * parent)
699 GstStructure *caps_st;
706 if (!st || !gst_structure_id_has_field (st, _CAPS_QUARK)) {
707 GST_WARNING ("Couldn't find caps !");
709 return gst_discoverer_stream_info_ref (parent);
711 return (GstDiscovererStreamInfo *)
712 g_object_new (GST_TYPE_DISCOVERER_STREAM_INFO, NULL);
715 gst_structure_id_get (st, _CAPS_QUARK, GST_TYPE_CAPS, &caps, NULL);
716 caps_st = gst_caps_get_structure (caps, 0);
717 name = gst_structure_get_name (caps_st);
719 if (g_str_has_prefix (name, "audio/")) {
720 GstDiscovererAudioInfo *info;
723 info = (GstDiscovererAudioInfo *) gst_discoverer_stream_info_ref (parent);
725 info = (GstDiscovererAudioInfo *)
726 g_object_new (GST_TYPE_DISCOVERER_AUDIO_INFO, NULL);
727 info->parent.caps = gst_caps_ref (caps);
730 if (gst_structure_get_int (caps_st, "rate", &tmp))
731 info->sample_rate = (guint) tmp;
733 if (gst_structure_get_int (caps_st, "channels", &tmp))
734 info->channels = (guint) tmp;
736 if (gst_structure_get_int (caps_st, "depth", &tmp))
737 info->depth = (guint) tmp;
739 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
740 gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st, NULL);
741 if (gst_tag_list_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
742 gst_tag_list_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
743 info->bitrate = utmp;
745 if (gst_tag_list_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
746 info->max_bitrate = utmp;
748 /* FIXME: Is it worth it to remove the tags we've parsed? */
749 gst_discoverer_merge_and_replace_tags (&info->parent.tags, tags_st);
752 if (gst_structure_id_has_field (st, _TOC_QUARK)) {
753 gst_structure_id_get (st, _TOC_QUARK, GST_TYPE_TOC, &toc_st, NULL);
754 info->parent.toc = toc_st;
757 if (!info->language && ((GstDiscovererStreamInfo *) info)->tags) {
759 if (gst_tag_list_get_string (((GstDiscovererStreamInfo *) info)->tags,
760 GST_TAG_LANGUAGE_CODE, &language)) {
761 info->language = language;
765 gst_caps_unref (caps);
766 return (GstDiscovererStreamInfo *) info;
768 } else if (g_str_has_prefix (name, "video/") ||
769 g_str_has_prefix (name, "image/")) {
770 GstDiscovererVideoInfo *info;
774 info = (GstDiscovererVideoInfo *) gst_discoverer_stream_info_ref (parent);
776 info = (GstDiscovererVideoInfo *)
777 g_object_new (GST_TYPE_DISCOVERER_VIDEO_INFO, NULL);
778 info->parent.caps = gst_caps_ref (caps);
781 /* FIXME : gst_video_info_from_caps only works with raw caps,
782 * wouldn't we want to get all the info below for non-raw caps ?
784 if (g_str_has_prefix (name, "video/x-raw") &&
785 gst_video_info_from_caps (&vinfo, caps)) {
786 info->width = (guint) vinfo.width;
787 info->height = (guint) vinfo.height;
789 info->depth = (guint) 0;
791 info->par_num = vinfo.par_n;
792 info->par_denom = vinfo.par_d;
794 info->framerate_num = vinfo.fps_n;
795 info->framerate_denom = vinfo.fps_d;
798 vinfo.interlace_mode != GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
801 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
802 gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st, NULL);
803 if (gst_tag_list_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
804 gst_tag_list_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
805 info->bitrate = utmp;
807 if (gst_tag_list_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
808 info->max_bitrate = utmp;
810 /* FIXME: Is it worth it to remove the tags we've parsed? */
811 gst_discoverer_merge_and_replace_tags (&info->parent.tags,
812 (GstTagList *) tags_st);
815 if (gst_structure_id_has_field (st, _TOC_QUARK)) {
816 gst_structure_id_get (st, _TOC_QUARK, GST_TYPE_TOC, &toc_st, NULL);
817 info->parent.toc = toc_st;
820 gst_caps_unref (caps);
821 return (GstDiscovererStreamInfo *) info;
823 } else if (is_subtitle_caps (caps)) {
824 GstDiscovererSubtitleInfo *info;
828 (GstDiscovererSubtitleInfo *) gst_discoverer_stream_info_ref (parent);
830 info = (GstDiscovererSubtitleInfo *)
831 g_object_new (GST_TYPE_DISCOVERER_SUBTITLE_INFO, NULL);
832 info->parent.caps = gst_caps_ref (caps);
835 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
836 const gchar *language;
838 gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st, NULL);
840 language = gst_structure_get_string (caps_st, GST_TAG_LANGUAGE_CODE);
842 info->language = g_strdup (language);
844 /* FIXME: Is it worth it to remove the tags we've parsed? */
845 gst_discoverer_merge_and_replace_tags (&info->parent.tags, tags_st);
848 if (gst_structure_id_has_field (st, _TOC_QUARK)) {
849 gst_structure_id_get (st, _TOC_QUARK, GST_TYPE_TOC, &toc_st, NULL);
850 info->parent.toc = toc_st;
853 if (!info->language && ((GstDiscovererStreamInfo *) info)->tags) {
855 if (gst_tag_list_get_string (((GstDiscovererStreamInfo *) info)->tags,
856 GST_TAG_LANGUAGE_CODE, &language)) {
857 info->language = language;
861 gst_caps_unref (caps);
862 return (GstDiscovererStreamInfo *) info;
865 /* None of the above - populate what information we can */
866 GstDiscovererStreamInfo *info;
869 info = gst_discoverer_stream_info_ref (parent);
871 info = (GstDiscovererStreamInfo *)
872 g_object_new (GST_TYPE_DISCOVERER_STREAM_INFO, NULL);
873 info->caps = gst_caps_ref (caps);
876 if (gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st,
878 gst_discoverer_merge_and_replace_tags (&info->tags, tags_st);
881 if (gst_structure_id_get (st, _TOC_QUARK, GST_TYPE_TOC, &toc_st, NULL)) {
885 gst_caps_unref (caps);
891 static GstStructure *
892 find_stream_for_node (GstDiscoverer * dc, const GstStructure * topology)
895 GstPad *target_pad = NULL;
896 GstStructure *st = NULL;
901 if (!gst_structure_id_has_field (topology, _TOPOLOGY_PAD_QUARK)) {
902 GST_DEBUG ("Could not find pad for node %" GST_PTR_FORMAT "\n", topology);
906 gst_structure_id_get (topology, _TOPOLOGY_PAD_QUARK,
907 GST_TYPE_PAD, &pad, NULL);
909 if (!dc->priv->streams) {
910 gst_object_unref (pad);
914 for (i = 0, tmp = dc->priv->streams; tmp; tmp = tmp->next, i++) {
915 ps = (PrivateStream *) tmp->data;
917 target_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (ps->pad));
918 gst_object_unref (target_pad);
920 if (target_pad == pad)
925 st = collect_stream_information (dc, ps, i);
927 gst_object_unref (pad);
933 child_is_raw_stream (const GstCaps * parent, const GstCaps * child)
935 const GstStructure *st1, *st2;
936 const gchar *name1, *name2;
938 st1 = gst_caps_get_structure (parent, 0);
939 name1 = gst_structure_get_name (st1);
940 st2 = gst_caps_get_structure (child, 0);
941 name2 = gst_structure_get_name (st2);
943 if ((g_str_has_prefix (name1, "audio/") &&
944 g_str_has_prefix (name2, "audio/x-raw")) ||
945 ((g_str_has_prefix (name1, "video/") ||
946 g_str_has_prefix (name1, "image/")) &&
947 g_str_has_prefix (name2, "video/x-raw"))) {
948 /* child is the "raw" sub-stream corresponding to parent */
952 if (is_subtitle_caps (parent))
958 /* If a parent is non-NULL, collected stream information will be appended to it
959 * (and where the information exists, it will be overriden)
961 static GstDiscovererStreamInfo *
962 parse_stream_topology (GstDiscoverer * dc, const GstStructure * topology,
963 GstDiscovererStreamInfo * parent)
965 GstDiscovererStreamInfo *res = NULL;
966 GstCaps *caps = NULL;
967 const GValue *nval = NULL;
969 GST_DEBUG ("parsing: %" GST_PTR_FORMAT, topology);
971 nval = gst_structure_get_value (topology, "next");
973 if (nval == NULL || GST_VALUE_HOLDS_STRUCTURE (nval)) {
974 GstStructure *st = find_stream_for_node (dc, topology);
975 gboolean add_to_list = TRUE;
978 res = collect_information (dc, st, parent);
979 gst_structure_free (st);
981 /* Didn't find a stream structure, so let's just use the caps we have */
982 res = collect_information (dc, topology, parent);
986 /* FIXME : aggregate with information from main streams */
987 GST_DEBUG ("Coudn't find 'next' ! might be the last entry");
990 const GstStructure *st;
992 st = gst_value_get_structure (nval);
994 GST_DEBUG ("next is a structure %" GST_PTR_FORMAT, st);
999 if (gst_structure_id_get (st, _CAPS_QUARK, GST_TYPE_CAPS, &caps, NULL)) {
1000 if (gst_caps_can_intersect (parent->caps, caps)) {
1001 /* We sometimes get an extra sub-stream from the parser. If this is
1002 * the case, we just replace the parent caps with this stream's caps
1003 * since they might contain more information */
1004 gst_caps_replace (&parent->caps, caps);
1006 parse_stream_topology (dc, st, parent);
1007 add_to_list = FALSE;
1008 } else if (child_is_raw_stream (parent->caps, caps)) {
1009 /* This is the "raw" stream corresponding to the parent. This
1010 * contains more information than the parent, tags etc. */
1011 parse_stream_topology (dc, st, parent);
1012 add_to_list = FALSE;
1014 GstDiscovererStreamInfo *next = parse_stream_topology (dc, st, NULL);
1016 next->previous = res;
1018 gst_caps_unref (caps);
1023 dc->priv->current_info->stream_list =
1024 g_list_append (dc->priv->current_info->stream_list, res);
1026 gst_discoverer_stream_info_unref (res);
1029 } else if (GST_VALUE_HOLDS_LIST (nval)) {
1031 GstDiscovererContainerInfo *cont;
1034 if (!gst_structure_id_get (topology, _CAPS_QUARK,
1035 GST_TYPE_CAPS, &caps, NULL))
1036 GST_WARNING ("Couldn't find caps !");
1038 len = gst_value_list_get_size (nval);
1039 GST_DEBUG ("next is a list of %d entries", len);
1041 cont = (GstDiscovererContainerInfo *)
1042 g_object_new (GST_TYPE_DISCOVERER_CONTAINER_INFO, NULL);
1043 cont->parent.caps = caps;
1044 res = (GstDiscovererStreamInfo *) cont;
1046 if (gst_structure_id_has_field (topology, _TAGS_QUARK)) {
1049 gst_structure_id_get (topology, _TAGS_QUARK,
1050 GST_TYPE_TAG_LIST, &tags, NULL);
1052 GST_DEBUG ("Merge tags %" GST_PTR_FORMAT, tags);
1055 gst_tag_list_merge (cont->parent.tags, (GstTagList *) tags,
1056 GST_TAG_MERGE_APPEND);
1057 gst_tag_list_free (tags);
1058 if (cont->parent.tags)
1059 gst_tag_list_free (cont->parent.tags);
1060 cont->parent.tags = tmp;
1061 GST_DEBUG ("Container info tags %" GST_PTR_FORMAT, tmp);
1064 for (i = 0; i < len; i++) {
1065 const GValue *subv = gst_value_list_get_value (nval, i);
1066 const GstStructure *subst = gst_value_get_structure (subv);
1067 GstDiscovererStreamInfo *substream;
1069 GST_DEBUG ("%d %" GST_PTR_FORMAT, i, subst);
1071 substream = parse_stream_topology (dc, subst, NULL);
1073 substream->previous = res;
1075 g_list_append (cont->streams,
1076 gst_discoverer_stream_info_ref (substream));
1083 /* Called when pipeline is pre-rolled */
1085 discoverer_collect (GstDiscoverer * dc)
1087 GST_DEBUG ("Collecting information");
1089 /* Stop the timeout handler if present */
1090 if (dc->priv->timeoutid) {
1091 g_source_remove (dc->priv->timeoutid);
1092 dc->priv->timeoutid = 0;
1095 if (dc->priv->streams) {
1096 /* FIXME : Make this querying optional */
1098 GstElement *pipeline = (GstElement *) dc->priv->pipeline;
1101 GST_DEBUG ("Attempting to query duration");
1103 if (gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur)) {
1104 GST_DEBUG ("Got duration %" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
1105 dc->priv->current_info->duration = (guint64) dur;
1107 GstStateChangeReturn sret;
1109 /* Some parsers may not even return a rough estimate right away, e.g.
1110 * because they've only processed a single frame so far, so if we
1111 * didn't get a duration the first time, spin a bit and try again.
1112 * Ugly, but still better than making parsers or other elements return
1113 * completely bogus values. We need some API extensions to solve this
1115 GST_INFO ("No duration yet, try a bit harder..");
1116 sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
1117 if (sret != GST_STATE_CHANGE_FAILURE) {
1120 for (i = 0; i < 2; ++i) {
1121 g_usleep (G_USEC_PER_SEC / 20);
1122 if (gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur)
1124 GST_DEBUG ("Got duration %" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
1125 dc->priv->current_info->duration = (guint64) dur;
1129 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1133 if (dc->priv->seeking_query) {
1134 if (gst_element_query (pipeline, dc->priv->seeking_query)) {
1138 gst_query_parse_seeking (dc->priv->seeking_query, &format,
1139 &seekable, NULL, NULL);
1140 if (format == GST_FORMAT_TIME) {
1141 GST_DEBUG ("Got seekable %d", seekable);
1142 dc->priv->current_info->seekable = seekable;
1148 if (dc->priv->current_topology)
1149 dc->priv->current_info->stream_info = parse_stream_topology (dc,
1150 dc->priv->current_topology, NULL);
1153 * Images need some special handling. They do not have a duration, have
1154 * caps named image/<foo> (th exception being MJPEG video which is also
1155 * type image/jpeg), and should consist of precisely one stream (actually
1156 * initially there are 2, the image and raw stream, but we squash these
1157 * while parsing the stream topology). At some point, if we find that these
1158 * conditions are not sufficient, we can count the number of decoders and
1159 * parsers in the chain, and if there's more than one decoder, or any
1160 * parser at all, we should not mark this as an image.
1162 if (dc->priv->current_info->duration == 0 &&
1163 dc->priv->current_info->stream_info != NULL &&
1164 dc->priv->current_info->stream_info->next == NULL) {
1166 gst_caps_get_structure (dc->priv->current_info->stream_info->caps, 0);
1168 if (g_str_has_prefix (gst_structure_get_name (st), "image/"))
1169 ((GstDiscovererVideoInfo *) dc->priv->current_info->
1170 stream_info)->is_image = TRUE;
1174 if (dc->priv->async) {
1175 GST_DEBUG ("Emitting 'discoverered'");
1176 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_DISCOVERED], 0,
1177 dc->priv->current_info, dc->priv->current_error);
1178 /* Clients get a copy of current_info since it is a boxed type */
1179 gst_discoverer_info_unref (dc->priv->current_info);
1180 dc->priv->current_info = NULL;
1185 get_async_cb (gpointer cb_data, GSource * source, GSourceFunc * func,
1188 *func = (GSourceFunc) async_timeout_cb;
1192 /* Wrapper since GSourceCallbackFuncs don't expect a return value from ref() */
1194 _void_g_object_ref (gpointer object)
1196 g_object_ref (G_OBJECT (object));
1200 handle_current_async (GstDiscoverer * dc)
1203 static GSourceCallbackFuncs cb_funcs = {
1209 /* Attach a timeout to the main context */
1210 source = g_timeout_source_new (dc->priv->timeout / GST_MSECOND);
1211 g_source_set_callback_indirect (source, g_object_ref (dc), &cb_funcs);
1212 dc->priv->timeoutid = g_source_attach (source, dc->priv->ctx);
1213 g_source_unref (source);
1217 /* Returns TRUE if processing should stop */
1219 handle_message (GstDiscoverer * dc, GstMessage * msg)
1221 gboolean done = FALSE;
1223 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "got a %s message",
1224 GST_MESSAGE_TYPE_NAME (msg));
1226 switch (GST_MESSAGE_TYPE (msg)) {
1227 case GST_MESSAGE_ERROR:{
1231 gst_message_parse_error (msg, &gerr, &debug);
1232 GST_WARNING_OBJECT (GST_MESSAGE_SRC (msg),
1233 "Got an error [debug:%s], [message:%s]", debug, gerr->message);
1234 dc->priv->current_error = gerr;
1237 /* We need to stop */
1240 /* Don't override missing plugin result code for missing plugin errors */
1241 if (dc->priv->current_info->result != GST_DISCOVERER_MISSING_PLUGINS ||
1242 (!g_error_matches (gerr, GST_CORE_ERROR,
1243 GST_CORE_ERROR_MISSING_PLUGIN) &&
1244 !g_error_matches (gerr, GST_STREAM_ERROR,
1245 GST_STREAM_ERROR_CODEC_NOT_FOUND))) {
1246 GST_DEBUG ("Setting result to ERROR");
1247 dc->priv->current_info->result = GST_DISCOVERER_ERROR;
1252 case GST_MESSAGE_EOS:
1253 GST_DEBUG ("Got EOS !");
1257 case GST_MESSAGE_APPLICATION:{
1259 gboolean async_done;
1260 name = gst_structure_get_name (gst_message_get_structure (msg));
1261 /* Maybe ASYNC_DONE is received & we're just waiting for subtitle tags */
1263 async_done = dc->priv->async_done;
1265 if (g_str_equal (name, "DiscovererDone") && async_done)
1270 case GST_MESSAGE_ASYNC_DONE:
1271 if (GST_MESSAGE_SRC (msg) == (GstObject *) dc->priv->pipeline) {
1272 GST_DEBUG ("Finished changing state asynchronously");
1274 if (dc->priv->pending_subtitle_pads == 0) {
1277 /* Remember that ASYNC_DONE has been received, wait for subtitles */
1278 dc->priv->async_done = TRUE;
1285 case GST_MESSAGE_ELEMENT:
1288 const GstStructure *structure;
1290 structure = gst_message_get_structure (msg);
1291 sttype = gst_structure_get_name_id (structure);
1292 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1293 "structure %" GST_PTR_FORMAT, structure);
1294 if (sttype == _MISSING_PLUGIN_QUARK) {
1295 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1296 "Setting result to MISSING_PLUGINS");
1297 dc->priv->current_info->result = GST_DISCOVERER_MISSING_PLUGINS;
1298 if (dc->priv->current_info->misc)
1299 gst_structure_free (dc->priv->current_info->misc);
1300 dc->priv->current_info->misc = gst_structure_copy (structure);
1301 } else if (sttype == _STREAM_TOPOLOGY_QUARK) {
1302 if (dc->priv->current_topology)
1303 gst_structure_free (dc->priv->current_topology);
1304 dc->priv->current_topology = gst_structure_copy (structure);
1309 case GST_MESSAGE_TAG:
1311 GstTagList *tl, *tmp;
1313 gst_message_parse_tag (msg, &tl);
1314 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Got tags %" GST_PTR_FORMAT, tl);
1315 /* Merge with current tags */
1317 gst_tag_list_merge (dc->priv->current_info->tags, tl,
1318 GST_TAG_MERGE_APPEND);
1319 gst_tag_list_free (tl);
1320 if (dc->priv->current_info->tags)
1321 gst_tag_list_free (dc->priv->current_info->tags);
1322 dc->priv->current_info->tags = tmp;
1323 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Current info %p, tags %"
1324 GST_PTR_FORMAT, dc->priv->current_info, tmp);
1328 case GST_MESSAGE_TOC:
1332 gst_message_parse_toc (msg, &tmp, NULL);
1333 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Got toc %" GST_PTR_FORMAT, tmp);
1334 dc->priv->current_info->toc = tmp;
1335 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Current info %p, toc %"
1336 GST_PTR_FORMAT, dc->priv->current_info, tmp);
1348 handle_current_sync (GstDiscoverer * dc)
1351 gdouble deadline = ((gdouble) dc->priv->timeout) / GST_SECOND;
1353 gboolean done = FALSE;
1355 timer = g_timer_new ();
1356 g_timer_start (timer);
1359 /* poll bus with timeout */
1360 /* FIXME : make the timeout more fine-tuned */
1361 if ((msg = gst_bus_timed_pop (dc->priv->bus, GST_SECOND / 2))) {
1362 done = handle_message (dc, msg);
1363 gst_message_unref (msg);
1365 } while (!done && (g_timer_elapsed (timer, NULL) < deadline));
1369 GST_DEBUG ("we timed out! Setting result to TIMEOUT");
1370 dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
1375 g_timer_stop (timer);
1376 g_timer_destroy (timer);
1380 _setup_locked (GstDiscoverer * dc)
1382 GstStateChangeReturn ret;
1384 GST_DEBUG ("Setting up");
1386 /* Pop URI off the pending URI list */
1387 dc->priv->current_info =
1388 (GstDiscovererInfo *) g_object_new (GST_TYPE_DISCOVERER_INFO, NULL);
1389 dc->priv->current_info->uri = (gchar *) dc->priv->pending_uris->data;
1390 dc->priv->pending_uris =
1391 g_list_delete_link (dc->priv->pending_uris, dc->priv->pending_uris);
1393 /* set uri on uridecodebin */
1394 g_object_set (dc->priv->uridecodebin, "uri", dc->priv->current_info->uri,
1397 GST_DEBUG ("Current is now %s", dc->priv->current_info->uri);
1399 dc->priv->processing = TRUE;
1401 /* set pipeline to PAUSED */
1403 GST_DEBUG ("Setting pipeline to PAUSED");
1405 gst_element_set_state ((GstElement *) dc->priv->pipeline,
1409 GST_DEBUG_OBJECT (dc, "Pipeline going to PAUSED : %s",
1410 gst_element_state_change_return_get_name (ret));
1414 discoverer_cleanup (GstDiscoverer * dc)
1416 GST_DEBUG ("Cleaning up");
1418 gst_bus_set_flushing (dc->priv->bus, TRUE);
1419 gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_READY);
1420 gst_bus_set_flushing (dc->priv->bus, FALSE);
1423 if (dc->priv->current_error)
1424 g_error_free (dc->priv->current_error);
1425 dc->priv->current_error = NULL;
1426 if (dc->priv->current_topology) {
1427 gst_structure_free (dc->priv->current_topology);
1428 dc->priv->current_topology = NULL;
1431 dc->priv->current_info = NULL;
1433 dc->priv->pending_subtitle_pads = 0;
1434 dc->priv->async_done = FALSE;
1436 /* Try popping the next uri */
1437 if (dc->priv->async) {
1438 if (dc->priv->pending_uris != NULL) {
1442 handle_current_async (dc);
1446 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_FINISHED], 0);
1455 discoverer_bus_cb (GstBus * bus, GstMessage * msg, GstDiscoverer * dc)
1457 if (dc->priv->processing) {
1458 if (handle_message (dc, msg)) {
1459 GST_DEBUG ("Stopping asynchronously");
1460 /* Serialise with _event_probe() */
1462 dc->priv->processing = FALSE;
1464 discoverer_collect (dc);
1465 discoverer_cleanup (dc);
1471 async_timeout_cb (GstDiscoverer * dc)
1473 if (!g_source_is_destroyed (g_main_current_source ())) {
1474 dc->priv->timeoutid = 0;
1475 GST_DEBUG ("Setting result to TIMEOUT");
1476 dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
1477 dc->priv->processing = FALSE;
1478 discoverer_collect (dc);
1479 discoverer_cleanup (dc);
1484 /* If there is a pending URI, it will pop it from the list of pending
1485 * URIs and start the discovery on it.
1487 * Returns GST_DISCOVERER_OK if the next URI was popped and is processing,
1488 * else a error flag.
1490 static GstDiscovererResult
1491 start_discovering (GstDiscoverer * dc)
1493 GstDiscovererResult res = GST_DISCOVERER_OK;
1495 GST_DEBUG ("Starting");
1498 if (dc->priv->pending_uris == NULL) {
1499 GST_WARNING ("No URI to process");
1500 res = GST_DISCOVERER_URI_INVALID;
1505 if (dc->priv->current_info != NULL) {
1506 GST_WARNING ("Already processing a file");
1507 res = GST_DISCOVERER_BUSY;
1512 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_STARTING], 0);
1518 if (dc->priv->async)
1519 handle_current_async (dc);
1521 handle_current_sync (dc);
1529 * gst_discoverer_start:
1530 * @discoverer: A #GstDiscoverer
1532 * Allow asynchronous discovering of URIs to take place.
1533 * A #GMainLoop must be available for #GstDiscoverer to properly work in
1534 * asynchronous mode.
1537 gst_discoverer_start (GstDiscoverer * discoverer)
1540 GMainContext *ctx = NULL;
1542 GST_DEBUG_OBJECT (discoverer, "Starting...");
1544 if (discoverer->priv->async) {
1545 GST_DEBUG_OBJECT (discoverer, "We were already started");
1549 discoverer->priv->async = TRUE;
1550 discoverer->priv->running = TRUE;
1552 ctx = g_main_context_get_thread_default ();
1554 /* Connect to bus signals */
1556 ctx = g_main_context_default ();
1558 source = gst_bus_create_watch (discoverer->priv->bus);
1559 g_source_set_callback (source, (GSourceFunc) gst_bus_async_signal_func,
1561 discoverer->priv->sourceid = g_source_attach (source, ctx);
1562 g_source_unref (source);
1563 discoverer->priv->ctx = g_main_context_ref (ctx);
1565 start_discovering (discoverer);
1566 GST_DEBUG_OBJECT (discoverer, "Started");
1570 * gst_discoverer_stop:
1571 * @discoverer: A #GstDiscoverer
1573 * Stop the discovery of any pending URIs and clears the list of
1574 * pending URIS (if any).
1577 gst_discoverer_stop (GstDiscoverer * discoverer)
1579 GST_DEBUG_OBJECT (discoverer, "Stopping...");
1581 if (!discoverer->priv->async) {
1582 GST_DEBUG_OBJECT (discoverer,
1583 "We were already stopped, or running synchronously");
1587 DISCO_LOCK (discoverer);
1588 if (discoverer->priv->processing) {
1589 /* We prevent any further processing by setting the bus to
1590 * flushing and setting the pipeline to READY.
1591 * _reset() will take care of the rest of the cleanup */
1592 if (discoverer->priv->bus)
1593 gst_bus_set_flushing (discoverer->priv->bus, TRUE);
1594 if (discoverer->priv->pipeline)
1595 gst_element_set_state ((GstElement *) discoverer->priv->pipeline,
1598 discoverer->priv->running = FALSE;
1599 DISCO_UNLOCK (discoverer);
1601 /* Remove timeout handler */
1602 if (discoverer->priv->timeoutid) {
1603 g_source_remove (discoverer->priv->timeoutid);
1604 discoverer->priv->timeoutid = 0;
1606 /* Remove signal watch */
1607 if (discoverer->priv->sourceid) {
1608 g_source_remove (discoverer->priv->sourceid);
1609 discoverer->priv->sourceid = 0;
1611 /* Unref main context */
1612 if (discoverer->priv->ctx) {
1613 g_main_context_unref (discoverer->priv->ctx);
1614 discoverer->priv->ctx = NULL;
1616 discoverer_reset (discoverer);
1618 discoverer->priv->async = FALSE;
1620 GST_DEBUG_OBJECT (discoverer, "Stopped");
1624 * gst_discoverer_discover_uri_async:
1625 * @discoverer: A #GstDiscoverer
1626 * @uri: the URI to add.
1628 * Appends the given @uri to the list of URIs to discoverer. The actual
1629 * discovery of the @uri will only take place if gst_discoverer_start() has
1632 * A copy of @uri will be made internally, so the caller can safely g_free()
1635 * Returns: %TRUE if the @uri was successfully appended to the list of pending
1639 gst_discoverer_discover_uri_async (GstDiscoverer * discoverer,
1644 GST_DEBUG_OBJECT (discoverer, "uri : %s", uri);
1646 DISCO_LOCK (discoverer);
1647 can_run = (discoverer->priv->pending_uris == NULL);
1648 discoverer->priv->pending_uris =
1649 g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
1650 DISCO_UNLOCK (discoverer);
1653 start_discovering (discoverer);
1659 /* Synchronous mode */
1661 * gst_discoverer_discover_uri:
1662 * @discoverer: A #GstDiscoverer
1663 * @uri: The URI to run on.
1664 * @err: (out) (allow-none): If an error occurred, this field will be filled in.
1666 * Synchronously discovers the given @uri.
1668 * A copy of @uri will be made internally, so the caller can safely g_free()
1671 * Returns: (transfer full): the result of the scanning. Can be %NULL if an
1675 gst_discoverer_discover_uri (GstDiscoverer * discoverer, const gchar * uri,
1678 GstDiscovererResult res = 0;
1679 GstDiscovererInfo *info;
1681 GST_DEBUG_OBJECT (discoverer, "uri:%s", uri);
1683 DISCO_LOCK (discoverer);
1684 if (G_UNLIKELY (discoverer->priv->current_info)) {
1685 DISCO_UNLOCK (discoverer);
1686 GST_WARNING_OBJECT (discoverer, "Already handling a uri");
1690 discoverer->priv->pending_uris =
1691 g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
1692 DISCO_UNLOCK (discoverer);
1694 res = start_discovering (discoverer);
1695 discoverer_collect (discoverer);
1699 if (discoverer->priv->current_error)
1700 *err = g_error_copy (discoverer->priv->current_error);
1704 if (res != GST_DISCOVERER_OK) {
1705 GST_DEBUG ("Setting result to %d (was %d)", res,
1706 discoverer->priv->current_info->result);
1707 discoverer->priv->current_info->result = res;
1709 info = discoverer->priv->current_info;
1711 discoverer_cleanup (discoverer);
1717 * gst_discoverer_new:
1718 * @timeout: timeout per file, in nanoseconds. Allowed are values between
1719 * one second (#GST_SECOND) and one hour (3600 * #GST_SECOND)
1720 * @err: a pointer to a #GError. can be %NULL
1722 * Creates a new #GstDiscoverer with the provided timeout.
1724 * Returns: (transfer full): The new #GstDiscoverer.
1725 * If an error occurred when creating the discoverer, @err will be set
1726 * accordingly and %NULL will be returned. If @err is set, the caller must
1727 * free it when no longer needed using g_error_free().
1730 gst_discoverer_new (GstClockTime timeout, GError ** err)
1734 res = g_object_new (GST_TYPE_DISCOVERER, "timeout", timeout, NULL);
1735 if (res->priv->uridecodebin == NULL) {
1737 *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
1738 "Couldn't create 'uridecodebin' element");
1739 gst_object_unref (res);