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.
47 #include <gst/video/video.h>
50 #include "pbutils-marshal.h"
51 #include "pbutils-private.h"
53 #include "gst/glib-compat-private.h"
55 GST_DEBUG_CATEGORY_STATIC (discoverer_debug);
56 #define GST_CAT_DEFAULT discoverer_debug
58 static GQuark _CAPS_QUARK;
59 static GQuark _TAGS_QUARK;
60 static GQuark _TOC_QUARK;
61 static GQuark _MISSING_PLUGIN_QUARK;
62 static GQuark _STREAM_TOPOLOGY_QUARK;
63 static GQuark _TOPOLOGY_PAD_QUARK;
76 struct _GstDiscovererPrivate
80 /* allowed time to discover each uri in nanoseconds */
83 /* list of pending URI to process (current excluded) */
88 /* TRUE if processing a URI */
91 /* TRUE if discoverer has been started */
94 /* TRUE if ASYNC_DONE has been received (need to check for subtitle tags) */
98 GstDiscovererInfo *current_info;
99 GError *current_error;
100 GstStructure *current_topology;
102 /* List of private streams */
105 /* List of these sinks and their handler IDs (to remove the probe) */
106 guint pending_subtitle_pads;
108 /* Global elements */
110 GstElement *uridecodebin;
113 GType decodebin_type;
115 /* Custom main context variables */
120 /* reusable queries */
121 GstQuery *seeking_query;
123 /* Handler ids for various callbacks */
125 gulong pad_remove_id;
126 gulong element_added_id;
130 #define DISCO_LOCK(dc) g_mutex_lock (dc->priv->lock);
131 #define DISCO_UNLOCK(dc) g_mutex_unlock (dc->priv->lock);
136 GST_DEBUG_CATEGORY_INIT (discoverer_debug, "discoverer", 0, "Discoverer");
138 _CAPS_QUARK = g_quark_from_static_string ("caps");
139 _TAGS_QUARK = g_quark_from_static_string ("tags");
140 _TOC_QUARK = g_quark_from_static_string ("toc");
141 _MISSING_PLUGIN_QUARK = g_quark_from_static_string ("missing-plugin");
142 _STREAM_TOPOLOGY_QUARK = g_quark_from_static_string ("stream-topology");
143 _TOPOLOGY_PAD_QUARK = g_quark_from_static_string ("pad");
146 G_DEFINE_TYPE_EXTENDED (GstDiscoverer, gst_discoverer, G_TYPE_OBJECT, 0,
157 #define DEFAULT_PROP_TIMEOUT 15 * GST_SECOND
165 static guint gst_discoverer_signals[LAST_SIGNAL] = { 0 };
167 static void gst_discoverer_set_timeout (GstDiscoverer * dc,
168 GstClockTime timeout);
169 static gboolean async_timeout_cb (GstDiscoverer * dc);
171 static void discoverer_bus_cb (GstBus * bus, GstMessage * msg,
173 static void uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
175 static void uridecodebin_pad_removed_cb (GstElement * uridecodebin,
176 GstPad * pad, 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
241 * Will be emitted when all information on a URI could be discovered.
243 gst_discoverer_signals[SIGNAL_DISCOVERED] =
244 g_signal_new ("discovered", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
245 G_STRUCT_OFFSET (GstDiscovererClass, discovered),
246 NULL, NULL, pbutils_marshal_VOID__POINTER_BOXED,
247 G_TYPE_NONE, 2, GST_TYPE_DISCOVERER_INFO,
248 G_TYPE_ERROR | G_SIGNAL_TYPE_STATIC_SCOPE);
252 uridecodebin_element_added_cb (GstElement * uridecodebin,
253 GstElement * child, GstDiscoverer * dc)
255 GST_DEBUG ("New element added to uridecodebin : %s",
256 GST_ELEMENT_NAME (child));
258 if (G_OBJECT_TYPE (child) == dc->priv->decodebin_type) {
259 g_object_set (child, "post-stream-topology", TRUE, NULL);
264 gst_discoverer_init (GstDiscoverer * dc)
267 GstFormat format = GST_FORMAT_TIME;
269 dc->priv = G_TYPE_INSTANCE_GET_PRIVATE (dc, GST_TYPE_DISCOVERER,
270 GstDiscovererPrivate);
272 dc->priv->timeout = DEFAULT_PROP_TIMEOUT;
273 dc->priv->async = FALSE;
274 dc->priv->async_done = FALSE;
276 dc->priv->lock = g_mutex_new ();
278 dc->priv->pending_subtitle_pads = 0;
280 GST_LOG ("Creating pipeline");
281 dc->priv->pipeline = (GstBin *) gst_pipeline_new ("Discoverer");
282 GST_LOG_OBJECT (dc, "Creating uridecodebin");
283 dc->priv->uridecodebin =
284 gst_element_factory_make ("uridecodebin", "discoverer-uri");
285 if (G_UNLIKELY (dc->priv->uridecodebin == NULL)) {
286 GST_ERROR ("Can't create uridecodebin");
289 GST_LOG_OBJECT (dc, "Adding uridecodebin to pipeline");
290 gst_bin_add (dc->priv->pipeline, dc->priv->uridecodebin);
292 dc->priv->pad_added_id =
293 g_signal_connect_object (dc->priv->uridecodebin, "pad-added",
294 G_CALLBACK (uridecodebin_pad_added_cb), dc, 0);
295 dc->priv->pad_remove_id =
296 g_signal_connect_object (dc->priv->uridecodebin, "pad-removed",
297 G_CALLBACK (uridecodebin_pad_removed_cb), dc, 0);
299 GST_LOG_OBJECT (dc, "Getting pipeline bus");
300 dc->priv->bus = gst_pipeline_get_bus ((GstPipeline *) dc->priv->pipeline);
302 dc->priv->bus_cb_id =
303 g_signal_connect_object (dc->priv->bus, "message",
304 G_CALLBACK (discoverer_bus_cb), dc, 0);
306 GST_DEBUG_OBJECT (dc, "Done initializing Discoverer");
308 /* This is ugly. We get the GType of decodebin so we can quickly detect
309 * when a decodebin is added to uridecodebin so we can set the
310 * post-stream-topology setting to TRUE */
311 dc->priv->element_added_id =
312 g_signal_connect_object (dc->priv->uridecodebin, "element-added",
313 G_CALLBACK (uridecodebin_element_added_cb), dc, 0);
314 tmp = gst_element_factory_make ("decodebin", NULL);
315 dc->priv->decodebin_type = G_OBJECT_TYPE (tmp);
316 gst_object_unref (tmp);
319 dc->priv->seeking_query = gst_query_new_seeking (format);
323 discoverer_reset (GstDiscoverer * dc)
325 GST_DEBUG_OBJECT (dc, "Resetting");
327 if (dc->priv->pending_uris) {
328 g_list_foreach (dc->priv->pending_uris, (GFunc) g_free, NULL);
329 g_list_free (dc->priv->pending_uris);
330 dc->priv->pending_uris = NULL;
333 if (dc->priv->pipeline)
334 gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_NULL);
337 #define DISCONNECT_SIGNAL(o,i) G_STMT_START{ \
338 if ((i) && g_signal_handler_is_connected ((o), (i))) \
339 g_signal_handler_disconnect ((o), (i)); \
344 gst_discoverer_dispose (GObject * obj)
346 GstDiscoverer *dc = (GstDiscoverer *) obj;
348 GST_DEBUG_OBJECT (dc, "Disposing");
350 discoverer_reset (dc);
352 if (G_LIKELY (dc->priv->pipeline)) {
353 /* Workaround for bug #118536 */
354 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_added_id);
355 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_remove_id);
356 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->element_added_id);
357 DISCONNECT_SIGNAL (dc->priv->bus, dc->priv->bus_cb_id);
359 /* pipeline was set to NULL in _reset */
360 gst_object_unref (dc->priv->pipeline);
361 gst_object_unref (dc->priv->bus);
363 dc->priv->pipeline = NULL;
364 dc->priv->uridecodebin = NULL;
365 dc->priv->bus = NULL;
368 gst_discoverer_stop (dc);
370 if (dc->priv->lock) {
371 g_mutex_free (dc->priv->lock);
372 dc->priv->lock = NULL;
375 if (dc->priv->seeking_query) {
376 gst_query_unref (dc->priv->seeking_query);
377 dc->priv->seeking_query = NULL;
380 G_OBJECT_CLASS (gst_discoverer_parent_class)->dispose (obj);
384 gst_discoverer_set_property (GObject * object, guint prop_id,
385 const GValue * value, GParamSpec * pspec)
387 GstDiscoverer *dc = (GstDiscoverer *) object;
391 gst_discoverer_set_timeout (dc, g_value_get_uint64 (value));
394 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
400 gst_discoverer_get_property (GObject * object, guint prop_id,
401 GValue * value, GParamSpec * pspec)
403 GstDiscoverer *dc = (GstDiscoverer *) object;
408 g_value_set_uint64 (value, dc->priv->timeout);
412 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
418 gst_discoverer_set_timeout (GstDiscoverer * dc, GstClockTime timeout)
420 GST_DEBUG_OBJECT (dc, "timeout : %" GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
422 /* FIXME : update current pending timeout if we're running */
424 dc->priv->timeout = timeout;
428 static GstPadProbeReturn
429 _event_probe (GstPad * pad, GstPadProbeInfo * info, PrivateStream * ps)
431 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
433 if (GST_EVENT_TYPE (event) == GST_EVENT_TAG) {
434 GstTagList *tl = NULL, *tmp;
436 gst_event_parse_tag (event, &tl);
437 GST_DEBUG_OBJECT (pad, "tags %" GST_PTR_FORMAT, tl);
439 /* If preroll is complete, drop these tags - the collected information is
440 * possibly already being processed and adding more tags would be racy */
441 if (G_LIKELY (ps->dc->priv->processing)) {
442 GST_DEBUG_OBJECT (pad, "private stream %p old tags %" GST_PTR_FORMAT, ps,
444 tmp = gst_tag_list_merge (ps->tags, tl, GST_TAG_MERGE_APPEND);
446 gst_tag_list_free (ps->tags);
448 GST_DEBUG_OBJECT (pad, "private stream %p new tags %" GST_PTR_FORMAT, ps,
451 GST_DEBUG_OBJECT (pad, "Dropping tags since preroll is done");
452 DISCO_UNLOCK (ps->dc);
455 if (GST_EVENT_TYPE (event) == GST_EVENT_TOC) {
458 gst_event_parse_toc (event, &tmp, NULL);
459 GST_DEBUG_OBJECT (pad, "toc %" GST_PTR_FORMAT, tmp);
462 if (G_LIKELY (ps->dc->priv->processing)) {
463 GST_DEBUG_OBJECT (pad, "private stream %p toc %" GST_PTR_FORMAT, ps, tmp);
465 GST_DEBUG_OBJECT (pad, "Dropping toc since preroll is done");
466 DISCO_UNLOCK (ps->dc);
469 return GST_PAD_PROBE_OK;
472 static GstStaticCaps subtitle_caps = GST_STATIC_CAPS ("text/plain; "
473 "text/x-pango-markup; subpicture/x-pgs; subpicture/x-dvb; "
474 "application/x-subtitle-unknown; application/x-ssa; application/x-ass; "
475 "subtitle/x-kate; application/x-kate; video/x-dvd-subpicture");
478 is_subtitle_caps (const GstCaps * caps)
483 subs_caps = gst_static_caps_get (&subtitle_caps);
484 ret = gst_caps_can_intersect (caps, subs_caps);
485 gst_caps_unref (subs_caps);
490 static GstPadProbeReturn
491 got_subtitle_data (GstPad * pad, GstPadProbeInfo * info, GstDiscoverer * dc)
494 if (!(GST_IS_BUFFER (info->data) || (GST_IS_EVENT (info->data)
495 && GST_EVENT_TYPE ((GstEvent *) info->data) == GST_EVENT_GAP)))
496 return GST_PAD_PROBE_OK;
501 dc->priv->pending_subtitle_pads--;
503 if (dc->priv->pending_subtitle_pads == 0) {
504 GstMessage *msg = gst_message_new_application (NULL,
505 gst_structure_new_empty ("DiscovererDone"));
506 gst_element_post_message ((GstElement *) dc->priv->pipeline, msg);
510 return GST_PAD_PROBE_REMOVE;
515 uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
519 GstPad *sinkpad = NULL;
522 GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
524 ps = g_slice_new0 (PrivateStream);
528 ps->queue = gst_element_factory_make ("queue", NULL);
529 ps->sink = gst_element_factory_make ("fakesink", NULL);
531 if (G_UNLIKELY (ps->queue == NULL || ps->sink == NULL))
534 g_object_set (ps->sink, "silent", TRUE, NULL);
535 g_object_set (ps->queue, "max-size-buffers", 1, "silent", TRUE, NULL);
537 caps = gst_pad_query_caps (pad, NULL);
539 sinkpad = gst_element_get_static_pad (ps->queue, "sink");
543 if (is_subtitle_caps (caps)) {
544 /* Subtitle streams are sparse and may not provide any information - don't
545 * wait for data to preroll */
546 gst_pad_add_probe (sinkpad, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
547 (GstPadProbeCallback) got_subtitle_data, dc, NULL);
548 g_object_set (ps->sink, "async", FALSE, NULL);
550 dc->priv->pending_subtitle_pads++;
554 gst_caps_unref (caps);
556 gst_bin_add_many (dc->priv->pipeline, ps->queue, ps->sink, NULL);
558 if (!gst_element_link_pads_full (ps->queue, "src", ps->sink, "sink",
559 GST_PAD_LINK_CHECK_NOTHING))
561 if (!gst_element_sync_state_with_parent (ps->sink))
563 if (!gst_element_sync_state_with_parent (ps->queue))
566 if (gst_pad_link_full (pad, sinkpad,
567 GST_PAD_LINK_CHECK_NOTHING) != GST_PAD_LINK_OK)
569 gst_object_unref (sinkpad);
571 /* Add an event probe */
572 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
573 (GstPadProbeCallback) _event_probe, ps, NULL);
576 dc->priv->streams = g_list_append (dc->priv->streams, ps);
579 GST_DEBUG_OBJECT (dc, "Done handling pad");
584 GST_ERROR_OBJECT (dc, "Error while handling pad");
586 gst_object_unref (sinkpad);
588 gst_object_unref (ps->queue);
590 gst_object_unref (ps->sink);
591 g_slice_free (PrivateStream, ps);
596 uridecodebin_pad_removed_cb (GstElement * uridecodebin, GstPad * pad,
603 GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
605 /* Find the PrivateStream */
607 for (tmp = dc->priv->streams; tmp; tmp = tmp->next) {
608 ps = (PrivateStream *) tmp->data;
615 GST_DEBUG ("The removed pad wasn't controlled by us !");
619 dc->priv->streams = g_list_delete_link (dc->priv->streams, tmp);
622 gst_element_set_state (ps->sink, GST_STATE_NULL);
623 gst_element_set_state (ps->queue, GST_STATE_NULL);
624 gst_element_unlink (ps->queue, ps->sink);
626 sinkpad = gst_element_get_static_pad (ps->queue, "sink");
627 gst_pad_unlink (pad, sinkpad);
628 gst_object_unref (sinkpad);
630 /* references removed here */
631 gst_bin_remove_many (dc->priv->pipeline, ps->sink, ps->queue, NULL);
634 gst_tag_list_free (ps->tags);
637 gst_toc_free (ps->toc);
640 g_slice_free (PrivateStream, ps);
642 GST_DEBUG ("Done handling pad");
645 static GstStructure *
646 collect_stream_information (GstDiscoverer * dc, PrivateStream * ps, guint idx)
652 stname = g_strdup_printf ("stream-%02d", idx);
653 st = gst_structure_new_empty (stname);
657 caps = gst_pad_get_current_caps (ps->pad);
659 GST_WARNING ("Couldn't get negotiated caps from %s:%s",
660 GST_DEBUG_PAD_NAME (ps->pad));
661 caps = gst_pad_query_caps (ps->pad, NULL);
664 GST_DEBUG ("Got caps %" GST_PTR_FORMAT, caps);
665 gst_structure_id_set (st, _CAPS_QUARK, GST_TYPE_CAPS, caps, NULL);
667 gst_caps_unref (caps);
670 gst_structure_id_set (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, ps->tags, NULL);
672 gst_structure_id_set (st, _TOC_QUARK, GST_TYPE_TOC, ps->toc, NULL);
677 /* takes ownership of new_tags, may replace *taglist with a new one */
679 gst_discoverer_merge_and_replace_tags (GstTagList ** taglist,
680 GstTagList * new_tags)
682 if (new_tags == NULL)
685 if (*taglist == NULL) {
690 gst_tag_list_insert (*taglist, new_tags, GST_TAG_MERGE_REPLACE);
691 gst_tag_list_free (new_tags);
694 /* Parses a set of caps and tags in st and populates a GstDiscovererStreamInfo
695 * structure (parent, if !NULL, otherwise it allocates one)
697 static GstDiscovererStreamInfo *
698 collect_information (GstDiscoverer * dc, const GstStructure * st,
699 GstDiscovererStreamInfo * parent)
702 GstStructure *caps_st;
709 if (!st || !gst_structure_id_has_field (st, _CAPS_QUARK)) {
710 GST_WARNING ("Couldn't find caps !");
712 return gst_discoverer_stream_info_ref (parent);
714 return (GstDiscovererStreamInfo *)
715 g_object_new (GST_TYPE_DISCOVERER_STREAM_INFO, NULL);
718 gst_structure_id_get (st, _CAPS_QUARK, GST_TYPE_CAPS, &caps, NULL);
719 caps_st = gst_caps_get_structure (caps, 0);
720 name = gst_structure_get_name (caps_st);
722 if (g_str_has_prefix (name, "audio/")) {
723 GstDiscovererAudioInfo *info;
726 info = (GstDiscovererAudioInfo *) gst_discoverer_stream_info_ref (parent);
728 info = (GstDiscovererAudioInfo *)
729 g_object_new (GST_TYPE_DISCOVERER_AUDIO_INFO, NULL);
730 info->parent.caps = gst_caps_ref (caps);
733 if (gst_structure_get_int (caps_st, "rate", &tmp))
734 info->sample_rate = (guint) tmp;
736 if (gst_structure_get_int (caps_st, "channels", &tmp))
737 info->channels = (guint) tmp;
739 if (gst_structure_get_int (caps_st, "depth", &tmp))
740 info->depth = (guint) tmp;
742 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
743 gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st, NULL);
744 if (gst_tag_list_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
745 gst_tag_list_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
746 info->bitrate = utmp;
748 if (gst_tag_list_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
749 info->max_bitrate = utmp;
751 /* FIXME: Is it worth it to remove the tags we've parsed? */
752 gst_discoverer_merge_and_replace_tags (&info->parent.tags, tags_st);
755 if (gst_structure_id_has_field (st, _TOC_QUARK)) {
756 gst_structure_id_get (st, _TOC_QUARK, GST_TYPE_TOC, &toc_st, NULL);
757 info->parent.toc = toc_st;
760 if (!info->language && ((GstDiscovererStreamInfo *) info)->tags) {
762 if (gst_tag_list_get_string (((GstDiscovererStreamInfo *) info)->tags,
763 GST_TAG_LANGUAGE_CODE, &language)) {
764 info->language = language;
768 gst_caps_unref (caps);
769 return (GstDiscovererStreamInfo *) info;
771 } else if (g_str_has_prefix (name, "video/") ||
772 g_str_has_prefix (name, "image/")) {
773 GstDiscovererVideoInfo *info;
777 info = (GstDiscovererVideoInfo *) gst_discoverer_stream_info_ref (parent);
779 info = (GstDiscovererVideoInfo *)
780 g_object_new (GST_TYPE_DISCOVERER_VIDEO_INFO, NULL);
781 info->parent.caps = gst_caps_ref (caps);
784 /* FIXME : gst_video_info_from_caps only works with raw caps,
785 * wouldn't we want to get all the info below for non-raw caps ?
787 if (g_str_has_prefix (name, "video/x-raw") &&
788 gst_video_info_from_caps (&vinfo, caps)) {
789 info->width = (guint) vinfo.width;
790 info->height = (guint) vinfo.height;
792 info->depth = (guint) 0;
794 info->par_num = vinfo.par_n;
795 info->par_denom = vinfo.par_d;
797 info->framerate_num = vinfo.fps_n;
798 info->framerate_denom = vinfo.fps_d;
801 vinfo.interlace_mode != GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
804 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
805 gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st, NULL);
806 if (gst_tag_list_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
807 gst_tag_list_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
808 info->bitrate = utmp;
810 if (gst_tag_list_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
811 info->max_bitrate = utmp;
813 /* FIXME: Is it worth it to remove the tags we've parsed? */
814 gst_discoverer_merge_and_replace_tags (&info->parent.tags,
815 (GstTagList *) tags_st);
818 if (gst_structure_id_has_field (st, _TOC_QUARK)) {
819 gst_structure_id_get (st, _TOC_QUARK, GST_TYPE_TOC, &toc_st, NULL);
820 info->parent.toc = toc_st;
823 gst_caps_unref (caps);
824 return (GstDiscovererStreamInfo *) info;
826 } else if (is_subtitle_caps (caps)) {
827 GstDiscovererSubtitleInfo *info;
831 (GstDiscovererSubtitleInfo *) gst_discoverer_stream_info_ref (parent);
833 info = (GstDiscovererSubtitleInfo *)
834 g_object_new (GST_TYPE_DISCOVERER_SUBTITLE_INFO, NULL);
835 info->parent.caps = gst_caps_ref (caps);
838 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
839 const gchar *language;
841 gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st, NULL);
843 language = gst_structure_get_string (caps_st, GST_TAG_LANGUAGE_CODE);
845 info->language = g_strdup (language);
847 /* FIXME: Is it worth it to remove the tags we've parsed? */
848 gst_discoverer_merge_and_replace_tags (&info->parent.tags, tags_st);
851 if (gst_structure_id_has_field (st, _TOC_QUARK)) {
852 gst_structure_id_get (st, _TOC_QUARK, GST_TYPE_TOC, &toc_st, NULL);
853 info->parent.toc = toc_st;
856 if (!info->language && ((GstDiscovererStreamInfo *) info)->tags) {
858 if (gst_tag_list_get_string (((GstDiscovererStreamInfo *) info)->tags,
859 GST_TAG_LANGUAGE_CODE, &language)) {
860 info->language = language;
864 gst_caps_unref (caps);
865 return (GstDiscovererStreamInfo *) info;
868 /* None of the above - populate what information we can */
869 GstDiscovererStreamInfo *info;
872 info = gst_discoverer_stream_info_ref (parent);
874 info = (GstDiscovererStreamInfo *)
875 g_object_new (GST_TYPE_DISCOVERER_STREAM_INFO, NULL);
876 info->caps = gst_caps_ref (caps);
879 if (gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st,
881 gst_discoverer_merge_and_replace_tags (&info->tags, tags_st);
884 if (gst_structure_id_get (st, _TOC_QUARK, GST_TYPE_TOC, &toc_st, NULL)) {
888 gst_caps_unref (caps);
894 static GstStructure *
895 find_stream_for_node (GstDiscoverer * dc, const GstStructure * topology)
898 GstPad *target_pad = NULL;
899 GstStructure *st = NULL;
904 if (!gst_structure_id_has_field (topology, _TOPOLOGY_PAD_QUARK)) {
905 GST_DEBUG ("Could not find pad for node %" GST_PTR_FORMAT "\n", topology);
909 gst_structure_id_get (topology, _TOPOLOGY_PAD_QUARK,
910 GST_TYPE_PAD, &pad, NULL);
912 if (!dc->priv->streams) {
913 gst_object_unref (pad);
917 for (i = 0, tmp = dc->priv->streams; tmp; tmp = tmp->next, i++) {
918 ps = (PrivateStream *) tmp->data;
920 target_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (ps->pad));
921 gst_object_unref (target_pad);
923 if (target_pad == pad)
928 st = collect_stream_information (dc, ps, i);
930 gst_object_unref (pad);
936 child_is_raw_stream (const GstCaps * parent, const GstCaps * child)
938 const GstStructure *st1, *st2;
939 const gchar *name1, *name2;
941 st1 = gst_caps_get_structure (parent, 0);
942 name1 = gst_structure_get_name (st1);
943 st2 = gst_caps_get_structure (child, 0);
944 name2 = gst_structure_get_name (st2);
946 if ((g_str_has_prefix (name1, "audio/") &&
947 g_str_has_prefix (name2, "audio/x-raw")) ||
948 ((g_str_has_prefix (name1, "video/") ||
949 g_str_has_prefix (name1, "image/")) &&
950 g_str_has_prefix (name2, "video/x-raw"))) {
951 /* child is the "raw" sub-stream corresponding to parent */
955 if (is_subtitle_caps (parent))
961 /* If a parent is non-NULL, collected stream information will be appended to it
962 * (and where the information exists, it will be overriden)
964 static GstDiscovererStreamInfo *
965 parse_stream_topology (GstDiscoverer * dc, const GstStructure * topology,
966 GstDiscovererStreamInfo * parent)
968 GstDiscovererStreamInfo *res = NULL;
969 GstCaps *caps = NULL;
970 const GValue *nval = NULL;
972 GST_DEBUG ("parsing: %" GST_PTR_FORMAT, topology);
974 nval = gst_structure_get_value (topology, "next");
976 if (nval == NULL || GST_VALUE_HOLDS_STRUCTURE (nval)) {
977 GstStructure *st = find_stream_for_node (dc, topology);
978 gboolean add_to_list = TRUE;
981 res = collect_information (dc, st, parent);
982 gst_structure_free (st);
984 /* Didn't find a stream structure, so let's just use the caps we have */
985 res = collect_information (dc, topology, parent);
989 /* FIXME : aggregate with information from main streams */
990 GST_DEBUG ("Coudn't find 'next' ! might be the last entry");
993 const GstStructure *st;
995 st = gst_value_get_structure (nval);
997 GST_DEBUG ("next is a structure %" GST_PTR_FORMAT, st);
1002 if (gst_structure_id_get (st, _CAPS_QUARK, GST_TYPE_CAPS, &caps, NULL)) {
1003 if (gst_caps_can_intersect (parent->caps, caps)) {
1004 /* We sometimes get an extra sub-stream from the parser. If this is
1005 * the case, we just replace the parent caps with this stream's caps
1006 * since they might contain more information */
1007 gst_caps_replace (&parent->caps, caps);
1009 parse_stream_topology (dc, st, parent);
1010 add_to_list = FALSE;
1011 } else if (child_is_raw_stream (parent->caps, caps)) {
1012 /* This is the "raw" stream corresponding to the parent. This
1013 * contains more information than the parent, tags etc. */
1014 parse_stream_topology (dc, st, parent);
1015 add_to_list = FALSE;
1017 GstDiscovererStreamInfo *next = parse_stream_topology (dc, st, NULL);
1019 next->previous = res;
1021 gst_caps_unref (caps);
1026 dc->priv->current_info->stream_list =
1027 g_list_append (dc->priv->current_info->stream_list, res);
1029 gst_discoverer_stream_info_unref (res);
1032 } else if (GST_VALUE_HOLDS_LIST (nval)) {
1034 GstDiscovererContainerInfo *cont;
1037 if (!gst_structure_id_get (topology, _CAPS_QUARK,
1038 GST_TYPE_CAPS, &caps, NULL))
1039 GST_WARNING ("Couldn't find caps !");
1041 len = gst_value_list_get_size (nval);
1042 GST_DEBUG ("next is a list of %d entries", len);
1044 cont = (GstDiscovererContainerInfo *)
1045 g_object_new (GST_TYPE_DISCOVERER_CONTAINER_INFO, NULL);
1046 cont->parent.caps = caps;
1047 res = (GstDiscovererStreamInfo *) cont;
1049 if (gst_structure_id_has_field (topology, _TAGS_QUARK)) {
1052 gst_structure_id_get (topology, _TAGS_QUARK,
1053 GST_TYPE_TAG_LIST, &tags, NULL);
1055 GST_DEBUG ("Merge tags %" GST_PTR_FORMAT, tags);
1058 gst_tag_list_merge (cont->parent.tags, (GstTagList *) tags,
1059 GST_TAG_MERGE_APPEND);
1060 gst_tag_list_free (tags);
1061 if (cont->parent.tags)
1062 gst_tag_list_free (cont->parent.tags);
1063 cont->parent.tags = tmp;
1064 GST_DEBUG ("Container info tags %" GST_PTR_FORMAT, tmp);
1067 for (i = 0; i < len; i++) {
1068 const GValue *subv = gst_value_list_get_value (nval, i);
1069 const GstStructure *subst = gst_value_get_structure (subv);
1070 GstDiscovererStreamInfo *substream;
1072 GST_DEBUG ("%d %" GST_PTR_FORMAT, i, subst);
1074 substream = parse_stream_topology (dc, subst, NULL);
1076 substream->previous = res;
1078 g_list_append (cont->streams,
1079 gst_discoverer_stream_info_ref (substream));
1086 /* Called when pipeline is pre-rolled */
1088 discoverer_collect (GstDiscoverer * dc)
1090 GST_DEBUG ("Collecting information");
1092 /* Stop the timeout handler if present */
1093 if (dc->priv->timeoutid) {
1094 g_source_remove (dc->priv->timeoutid);
1095 dc->priv->timeoutid = 0;
1098 if (dc->priv->streams) {
1099 /* FIXME : Make this querying optional */
1101 GstElement *pipeline = (GstElement *) dc->priv->pipeline;
1104 GST_DEBUG ("Attempting to query duration");
1106 if (gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur)) {
1107 GST_DEBUG ("Got duration %" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
1108 dc->priv->current_info->duration = (guint64) dur;
1110 GstStateChangeReturn sret;
1112 /* Some parsers may not even return a rough estimate right away, e.g.
1113 * because they've only processed a single frame so far, so if we
1114 * didn't get a duration the first time, spin a bit and try again.
1115 * Ugly, but still better than making parsers or other elements return
1116 * completely bogus values. We need some API extensions to solve this
1118 GST_INFO ("No duration yet, try a bit harder..");
1119 sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
1120 if (sret != GST_STATE_CHANGE_FAILURE) {
1123 for (i = 0; i < 2; ++i) {
1124 g_usleep (G_USEC_PER_SEC / 20);
1125 if (gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur)
1127 GST_DEBUG ("Got duration %" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
1128 dc->priv->current_info->duration = (guint64) dur;
1132 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1136 if (dc->priv->seeking_query) {
1137 if (gst_element_query (pipeline, dc->priv->seeking_query)) {
1141 gst_query_parse_seeking (dc->priv->seeking_query, &format,
1142 &seekable, NULL, NULL);
1143 if (format == GST_FORMAT_TIME) {
1144 GST_DEBUG ("Got seekable %d", seekable);
1145 dc->priv->current_info->seekable = seekable;
1151 if (dc->priv->current_topology)
1152 dc->priv->current_info->stream_info = parse_stream_topology (dc,
1153 dc->priv->current_topology, NULL);
1156 * Images need some special handling. They do not have a duration, have
1157 * caps named image/<foo> (th exception being MJPEG video which is also
1158 * type image/jpeg), and should consist of precisely one stream (actually
1159 * initially there are 2, the image and raw stream, but we squash these
1160 * while parsing the stream topology). At some point, if we find that these
1161 * conditions are not sufficient, we can count the number of decoders and
1162 * parsers in the chain, and if there's more than one decoder, or any
1163 * parser at all, we should not mark this as an image.
1165 if (dc->priv->current_info->duration == 0 &&
1166 dc->priv->current_info->stream_info != NULL &&
1167 dc->priv->current_info->stream_info->next == NULL) {
1169 gst_caps_get_structure (dc->priv->current_info->stream_info->caps, 0);
1171 if (g_str_has_prefix (gst_structure_get_name (st), "image/"))
1172 ((GstDiscovererVideoInfo *) dc->priv->current_info->stream_info)->
1177 if (dc->priv->async) {
1178 GST_DEBUG ("Emitting 'discoverered'");
1179 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_DISCOVERED], 0,
1180 dc->priv->current_info, dc->priv->current_error);
1181 /* Clients get a copy of current_info since it is a boxed type */
1182 gst_discoverer_info_unref (dc->priv->current_info);
1187 get_async_cb (gpointer cb_data, GSource * source, GSourceFunc * func,
1190 *func = (GSourceFunc) async_timeout_cb;
1194 /* Wrapper since GSourceCallbackFuncs don't expect a return value from ref() */
1196 _void_g_object_ref (gpointer object)
1198 g_object_ref (G_OBJECT (object));
1202 handle_current_async (GstDiscoverer * dc)
1205 static GSourceCallbackFuncs cb_funcs = {
1211 /* Attach a timeout to the main context */
1212 source = g_timeout_source_new (dc->priv->timeout / GST_MSECOND);
1213 g_source_set_callback_indirect (source, g_object_ref (dc), &cb_funcs);
1214 dc->priv->timeoutid = g_source_attach (source, dc->priv->ctx);
1215 g_source_unref (source);
1219 /* Returns TRUE if processing should stop */
1221 handle_message (GstDiscoverer * dc, GstMessage * msg)
1223 gboolean done = FALSE;
1225 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "got a %s message",
1226 GST_MESSAGE_TYPE_NAME (msg));
1228 switch (GST_MESSAGE_TYPE (msg)) {
1229 case GST_MESSAGE_ERROR:{
1233 gst_message_parse_error (msg, &gerr, &debug);
1234 GST_WARNING_OBJECT (GST_MESSAGE_SRC (msg),
1235 "Got an error [debug:%s], [message:%s]", debug, gerr->message);
1236 dc->priv->current_error = gerr;
1239 /* We need to stop */
1242 /* Don't override missing plugin result code for missing plugin errors */
1243 if (dc->priv->current_info->result != GST_DISCOVERER_MISSING_PLUGINS ||
1244 (!g_error_matches (gerr, GST_CORE_ERROR,
1245 GST_CORE_ERROR_MISSING_PLUGIN) &&
1246 !g_error_matches (gerr, GST_STREAM_ERROR,
1247 GST_STREAM_ERROR_CODEC_NOT_FOUND))) {
1248 GST_DEBUG ("Setting result to ERROR");
1249 dc->priv->current_info->result = GST_DISCOVERER_ERROR;
1254 case GST_MESSAGE_EOS:
1255 GST_DEBUG ("Got EOS !");
1259 case GST_MESSAGE_APPLICATION:{
1261 gboolean async_done;
1262 name = gst_structure_get_name (gst_message_get_structure (msg));
1263 /* Maybe ASYNC_DONE is received & we're just waiting for subtitle tags */
1265 async_done = dc->priv->async_done;
1267 if (g_str_equal (name, "DiscovererDone") && async_done)
1272 case GST_MESSAGE_ASYNC_DONE:
1273 if (GST_MESSAGE_SRC (msg) == (GstObject *) dc->priv->pipeline) {
1274 GST_DEBUG ("Finished changing state asynchronously");
1276 if (dc->priv->pending_subtitle_pads == 0) {
1279 /* Remember that ASYNC_DONE has been received, wait for subtitles */
1280 dc->priv->async_done = TRUE;
1287 case GST_MESSAGE_ELEMENT:
1290 const GstStructure *structure;
1292 structure = gst_message_get_structure (msg);
1293 sttype = gst_structure_get_name_id (structure);
1294 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1295 "structure %" GST_PTR_FORMAT, structure);
1296 if (sttype == _MISSING_PLUGIN_QUARK) {
1297 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1298 "Setting result to MISSING_PLUGINS");
1299 dc->priv->current_info->result = GST_DISCOVERER_MISSING_PLUGINS;
1300 if (dc->priv->current_info->misc)
1301 gst_structure_free (dc->priv->current_info->misc);
1302 dc->priv->current_info->misc = gst_structure_copy (structure);
1303 } else if (sttype == _STREAM_TOPOLOGY_QUARK) {
1304 if (dc->priv->current_topology)
1305 gst_structure_free (dc->priv->current_topology);
1306 dc->priv->current_topology = gst_structure_copy (structure);
1311 case GST_MESSAGE_TAG:
1313 GstTagList *tl, *tmp;
1315 gst_message_parse_tag (msg, &tl);
1316 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Got tags %" GST_PTR_FORMAT, tl);
1317 /* Merge with current tags */
1319 gst_tag_list_merge (dc->priv->current_info->tags, tl,
1320 GST_TAG_MERGE_APPEND);
1321 gst_tag_list_free (tl);
1322 if (dc->priv->current_info->tags)
1323 gst_tag_list_free (dc->priv->current_info->tags);
1324 dc->priv->current_info->tags = tmp;
1325 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Current info %p, tags %"
1326 GST_PTR_FORMAT, dc->priv->current_info, tmp);
1330 case GST_MESSAGE_TOC:
1334 gst_message_parse_toc (msg, &tmp, NULL);
1335 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Got toc %" GST_PTR_FORMAT, tmp);
1336 dc->priv->current_info->toc = tmp;
1337 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Current info %p, toc %"
1338 GST_PTR_FORMAT, dc->priv->current_info, tmp);
1350 handle_current_sync (GstDiscoverer * dc)
1353 gdouble deadline = ((gdouble) dc->priv->timeout) / GST_SECOND;
1355 gboolean done = FALSE;
1357 timer = g_timer_new ();
1358 g_timer_start (timer);
1361 /* poll bus with timeout */
1362 /* FIXME : make the timeout more fine-tuned */
1363 if ((msg = gst_bus_timed_pop (dc->priv->bus, GST_SECOND / 2))) {
1364 done = handle_message (dc, msg);
1365 gst_message_unref (msg);
1367 } while (!done && (g_timer_elapsed (timer, NULL) < deadline));
1371 GST_DEBUG ("we timed out! Setting result to TIMEOUT");
1372 dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
1377 g_timer_stop (timer);
1378 g_timer_destroy (timer);
1382 _setup_locked (GstDiscoverer * dc)
1384 GstStateChangeReturn ret;
1386 GST_DEBUG ("Setting up");
1388 /* Pop URI off the pending URI list */
1389 dc->priv->current_info =
1390 (GstDiscovererInfo *) g_object_new (GST_TYPE_DISCOVERER_INFO, NULL);
1391 dc->priv->current_info->uri = (gchar *) dc->priv->pending_uris->data;
1392 dc->priv->pending_uris =
1393 g_list_delete_link (dc->priv->pending_uris, dc->priv->pending_uris);
1395 /* set uri on uridecodebin */
1396 g_object_set (dc->priv->uridecodebin, "uri", dc->priv->current_info->uri,
1399 GST_DEBUG ("Current is now %s", dc->priv->current_info->uri);
1401 dc->priv->processing = TRUE;
1403 /* set pipeline to PAUSED */
1405 GST_DEBUG ("Setting pipeline to PAUSED");
1407 gst_element_set_state ((GstElement *) dc->priv->pipeline,
1411 GST_DEBUG_OBJECT (dc, "Pipeline going to PAUSED : %s",
1412 gst_element_state_change_return_get_name (ret));
1416 discoverer_cleanup (GstDiscoverer * dc)
1418 GST_DEBUG ("Cleaning up");
1420 gst_bus_set_flushing (dc->priv->bus, TRUE);
1421 gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_READY);
1422 gst_bus_set_flushing (dc->priv->bus, FALSE);
1425 if (dc->priv->current_error)
1426 g_error_free (dc->priv->current_error);
1427 dc->priv->current_error = NULL;
1428 if (dc->priv->current_topology) {
1429 gst_structure_free (dc->priv->current_topology);
1430 dc->priv->current_topology = NULL;
1433 dc->priv->current_info = NULL;
1435 dc->priv->pending_subtitle_pads = 0;
1436 dc->priv->async_done = FALSE;
1438 /* Try popping the next uri */
1439 if (dc->priv->async) {
1440 if (dc->priv->pending_uris != NULL) {
1444 handle_current_async (dc);
1448 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_FINISHED], 0);
1457 discoverer_bus_cb (GstBus * bus, GstMessage * msg, GstDiscoverer * dc)
1459 if (dc->priv->processing) {
1460 if (handle_message (dc, msg)) {
1461 GST_DEBUG ("Stopping asynchronously");
1462 /* Serialise with _event_probe() */
1464 dc->priv->processing = FALSE;
1466 discoverer_collect (dc);
1467 discoverer_cleanup (dc);
1473 async_timeout_cb (GstDiscoverer * dc)
1475 if (!g_source_is_destroyed (g_main_current_source ())) {
1476 dc->priv->timeoutid = 0;
1477 GST_DEBUG ("Setting result to TIMEOUT");
1478 dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
1479 dc->priv->processing = FALSE;
1480 discoverer_collect (dc);
1481 discoverer_cleanup (dc);
1486 /* If there is a pending URI, it will pop it from the list of pending
1487 * URIs and start the discovery on it.
1489 * Returns GST_DISCOVERER_OK if the next URI was popped and is processing,
1490 * else a error flag.
1492 static GstDiscovererResult
1493 start_discovering (GstDiscoverer * dc)
1495 GstDiscovererResult res = GST_DISCOVERER_OK;
1497 GST_DEBUG ("Starting");
1500 if (dc->priv->pending_uris == NULL) {
1501 GST_WARNING ("No URI to process");
1502 res = GST_DISCOVERER_URI_INVALID;
1507 if (dc->priv->current_info != NULL) {
1508 GST_WARNING ("Already processing a file");
1509 res = GST_DISCOVERER_BUSY;
1514 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_STARTING], 0);
1520 if (dc->priv->async)
1521 handle_current_async (dc);
1523 handle_current_sync (dc);
1531 * gst_discoverer_start:
1532 * @discoverer: A #GstDiscoverer
1534 * Allow asynchronous discovering of URIs to take place.
1535 * A #GMainLoop must be available for #GstDiscoverer to properly work in
1536 * asynchronous mode.
1541 gst_discoverer_start (GstDiscoverer * discoverer)
1544 GMainContext *ctx = NULL;
1546 GST_DEBUG_OBJECT (discoverer, "Starting...");
1548 if (discoverer->priv->async) {
1549 GST_DEBUG_OBJECT (discoverer, "We were already started");
1553 discoverer->priv->async = TRUE;
1554 discoverer->priv->running = TRUE;
1556 ctx = g_main_context_get_thread_default ();
1558 /* Connect to bus signals */
1560 ctx = g_main_context_default ();
1562 source = gst_bus_create_watch (discoverer->priv->bus);
1563 g_source_set_callback (source, (GSourceFunc) gst_bus_async_signal_func,
1565 discoverer->priv->sourceid = g_source_attach (source, ctx);
1566 g_source_unref (source);
1567 discoverer->priv->ctx = g_main_context_ref (ctx);
1569 start_discovering (discoverer);
1570 GST_DEBUG_OBJECT (discoverer, "Started");
1574 * gst_discoverer_stop:
1575 * @discoverer: A #GstDiscoverer
1577 * Stop the discovery of any pending URIs and clears the list of
1578 * pending URIS (if any).
1583 gst_discoverer_stop (GstDiscoverer * discoverer)
1585 GST_DEBUG_OBJECT (discoverer, "Stopping...");
1587 if (!discoverer->priv->async) {
1588 GST_DEBUG_OBJECT (discoverer,
1589 "We were already stopped, or running synchronously");
1593 DISCO_LOCK (discoverer);
1594 if (discoverer->priv->processing) {
1595 /* We prevent any further processing by setting the bus to
1596 * flushing and setting the pipeline to READY.
1597 * _reset() will take care of the rest of the cleanup */
1598 if (discoverer->priv->bus)
1599 gst_bus_set_flushing (discoverer->priv->bus, TRUE);
1600 if (discoverer->priv->pipeline)
1601 gst_element_set_state ((GstElement *) discoverer->priv->pipeline,
1604 discoverer->priv->running = FALSE;
1605 DISCO_UNLOCK (discoverer);
1607 /* Remove timeout handler */
1608 if (discoverer->priv->timeoutid) {
1609 g_source_remove (discoverer->priv->timeoutid);
1610 discoverer->priv->timeoutid = 0;
1612 /* Remove signal watch */
1613 if (discoverer->priv->sourceid) {
1614 g_source_remove (discoverer->priv->sourceid);
1615 discoverer->priv->sourceid = 0;
1617 /* Unref main context */
1618 if (discoverer->priv->ctx) {
1619 g_main_context_unref (discoverer->priv->ctx);
1620 discoverer->priv->ctx = NULL;
1622 discoverer_reset (discoverer);
1624 discoverer->priv->async = FALSE;
1626 GST_DEBUG_OBJECT (discoverer, "Stopped");
1630 * gst_discoverer_discover_uri_async:
1631 * @discoverer: A #GstDiscoverer
1632 * @uri: the URI to add.
1634 * Appends the given @uri to the list of URIs to discoverer. The actual
1635 * discovery of the @uri will only take place if gst_discoverer_start() has
1638 * A copy of @uri will be made internally, so the caller can safely g_free()
1641 * Returns: %TRUE if the @uri was successfully appended to the list of pending
1647 gst_discoverer_discover_uri_async (GstDiscoverer * discoverer,
1652 GST_DEBUG_OBJECT (discoverer, "uri : %s", uri);
1654 DISCO_LOCK (discoverer);
1655 can_run = (discoverer->priv->pending_uris == NULL);
1656 discoverer->priv->pending_uris =
1657 g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
1658 DISCO_UNLOCK (discoverer);
1661 start_discovering (discoverer);
1667 /* Synchronous mode */
1669 * gst_discoverer_discover_uri:
1670 * @discoverer: A #GstDiscoverer
1671 * @uri: The URI to run on.
1672 * @err: (out) (allow-none): If an error occurred, this field will be filled in.
1674 * Synchronously discovers the given @uri.
1676 * A copy of @uri will be made internally, so the caller can safely g_free()
1679 * Returns: (transfer full): the result of the scanning. Can be %NULL if an
1685 gst_discoverer_discover_uri (GstDiscoverer * discoverer, const gchar * uri,
1688 GstDiscovererResult res = 0;
1689 GstDiscovererInfo *info;
1691 GST_DEBUG_OBJECT (discoverer, "uri:%s", uri);
1693 DISCO_LOCK (discoverer);
1694 if (G_UNLIKELY (discoverer->priv->current_info)) {
1695 DISCO_UNLOCK (discoverer);
1696 GST_WARNING_OBJECT (discoverer, "Already handling a uri");
1700 discoverer->priv->pending_uris =
1701 g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
1702 DISCO_UNLOCK (discoverer);
1704 res = start_discovering (discoverer);
1705 discoverer_collect (discoverer);
1709 if (discoverer->priv->current_error)
1710 *err = g_error_copy (discoverer->priv->current_error);
1714 if (res != GST_DISCOVERER_OK) {
1715 GST_DEBUG ("Setting result to %d (was %d)", res,
1716 discoverer->priv->current_info->result);
1717 discoverer->priv->current_info->result = res;
1719 info = discoverer->priv->current_info;
1721 discoverer_cleanup (discoverer);
1727 * gst_discoverer_new:
1728 * @timeout: timeout per file, in nanoseconds. Allowed are values between
1729 * one second (#GST_SECOND) and one hour (3600 * #GST_SECOND)
1730 * @err: a pointer to a #GError. can be %NULL
1732 * Creates a new #GstDiscoverer with the provided timeout.
1734 * Returns: (transfer full): The new #GstDiscoverer.
1735 * If an error occurred when creating the discoverer, @err will be set
1736 * accordingly and %NULL will be returned. If @err is set, the caller must
1737 * free it when no longer needed using g_error_free().
1742 gst_discoverer_new (GstClockTime timeout, GError ** err)
1746 res = g_object_new (GST_TYPE_DISCOVERER, "timeout", timeout, NULL);
1747 if (res->priv->uridecodebin == NULL) {
1749 *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
1750 "Couldn't create 'uridecodebin' element");
1751 gst_object_unref (res);