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 _MISSING_PLUGIN_QUARK;
61 static GQuark _STREAM_TOPOLOGY_QUARK;
62 static GQuark _TOPOLOGY_PAD_QUARK;
74 struct _GstDiscovererPrivate
78 /* allowed time to discover each uri in nanoseconds */
81 /* list of pending URI to process (current excluded) */
86 /* TRUE if processing a URI */
89 /* TRUE if discoverer has been started */
93 GstDiscovererInfo *current_info;
94 GError *current_error;
95 GstStructure *current_topology;
97 /* List of private streams */
100 /* Global elements */
102 GstElement *uridecodebin;
105 GType decodebin_type;
107 /* Custom main context variables */
112 /* reusable queries */
113 GstQuery *seeking_query;
115 /* Handler ids for various callbacks */
117 gulong pad_remove_id;
118 gulong element_added_id;
122 #define DISCO_LOCK(dc) g_mutex_lock (dc->priv->lock);
123 #define DISCO_UNLOCK(dc) g_mutex_unlock (dc->priv->lock);
128 GST_DEBUG_CATEGORY_INIT (discoverer_debug, "discoverer", 0, "Discoverer");
130 _CAPS_QUARK = g_quark_from_static_string ("caps");
131 _TAGS_QUARK = g_quark_from_static_string ("tags");
132 _MISSING_PLUGIN_QUARK = g_quark_from_static_string ("missing-plugin");
133 _STREAM_TOPOLOGY_QUARK = g_quark_from_static_string ("stream-topology");
134 _TOPOLOGY_PAD_QUARK = g_quark_from_static_string ("pad");
137 G_DEFINE_TYPE_EXTENDED (GstDiscoverer, gst_discoverer, G_TYPE_OBJECT, 0,
148 #define DEFAULT_PROP_TIMEOUT 15 * GST_SECOND
156 static guint gst_discoverer_signals[LAST_SIGNAL] = { 0 };
158 static void gst_discoverer_set_timeout (GstDiscoverer * dc,
159 GstClockTime timeout);
160 static gboolean async_timeout_cb (GstDiscoverer * dc);
162 static void discoverer_bus_cb (GstBus * bus, GstMessage * msg,
164 static void uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
166 static void uridecodebin_pad_removed_cb (GstElement * uridecodebin,
167 GstPad * pad, GstDiscoverer * dc);
169 static void gst_discoverer_dispose (GObject * dc);
170 static void gst_discoverer_set_property (GObject * object, guint prop_id,
171 const GValue * value, GParamSpec * pspec);
172 static void gst_discoverer_get_property (GObject * object, guint prop_id,
173 GValue * value, GParamSpec * pspec);
176 gst_discoverer_class_init (GstDiscovererClass * klass)
178 GObjectClass *gobject_class = (GObjectClass *) klass;
180 gobject_class->dispose = gst_discoverer_dispose;
182 gobject_class->set_property = gst_discoverer_set_property;
183 gobject_class->get_property = gst_discoverer_get_property;
185 g_type_class_add_private (klass, sizeof (GstDiscovererPrivate));
189 * GstDiscoverer:timeout
191 * The duration (in nanoseconds) after which the discovery of an individual
194 * If the discovery of a URI times out, the %GST_DISCOVERER_TIMEOUT will be
195 * set on the result flags.
197 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
198 g_param_spec_uint64 ("timeout", "timeout", "Timeout",
199 GST_SECOND, 3600 * GST_SECOND, DEFAULT_PROP_TIMEOUT,
200 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
204 * GstDiscoverer::finished:
205 * @discoverer: the #GstDiscoverer
207 * Will be emitted when all pending URIs have been processed.
209 gst_discoverer_signals[SIGNAL_FINISHED] =
210 g_signal_new ("finished", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
211 G_STRUCT_OFFSET (GstDiscovererClass, finished),
212 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
215 * GstDiscoverer::starting:
216 * @discoverer: the #GstDiscoverer
218 * Will be emitted when the discover starts analyzing the pending URIs
220 gst_discoverer_signals[SIGNAL_STARTING] =
221 g_signal_new ("starting", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
222 G_STRUCT_OFFSET (GstDiscovererClass, starting),
223 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
226 * GstDiscoverer::discovered:
227 * @discoverer: the #GstDiscoverer
228 * @info: the results #GstDiscovererInfo
229 * @error: (type GLib.Error): #GError, which will be non-NULL if an error
230 * occurred during discovery
232 * Will be emitted when all information on a URI could be discovered.
234 gst_discoverer_signals[SIGNAL_DISCOVERED] =
235 g_signal_new ("discovered", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
236 G_STRUCT_OFFSET (GstDiscovererClass, discovered),
237 NULL, NULL, pbutils_marshal_VOID__POINTER_BOXED,
238 G_TYPE_NONE, 2, GST_TYPE_DISCOVERER_INFO, GST_TYPE_G_ERROR);
242 uridecodebin_element_added_cb (GstElement * uridecodebin,
243 GstElement * child, GstDiscoverer * dc)
245 GST_DEBUG ("New element added to uridecodebin : %s",
246 GST_ELEMENT_NAME (child));
248 if (G_OBJECT_TYPE (child) == dc->priv->decodebin_type) {
249 g_object_set (child, "post-stream-topology", TRUE, NULL);
254 gst_discoverer_init (GstDiscoverer * dc)
257 GstFormat format = GST_FORMAT_TIME;
259 dc->priv = G_TYPE_INSTANCE_GET_PRIVATE (dc, GST_TYPE_DISCOVERER,
260 GstDiscovererPrivate);
262 dc->priv->timeout = DEFAULT_PROP_TIMEOUT;
263 dc->priv->async = FALSE;
265 dc->priv->lock = g_mutex_new ();
267 GST_LOG ("Creating pipeline");
268 dc->priv->pipeline = (GstBin *) gst_pipeline_new ("Discoverer");
269 GST_LOG_OBJECT (dc, "Creating uridecodebin");
270 dc->priv->uridecodebin =
271 gst_element_factory_make ("uridecodebin", "discoverer-uri");
272 if (G_UNLIKELY (dc->priv->uridecodebin == NULL)) {
273 GST_ERROR ("Can't create uridecodebin");
276 GST_LOG_OBJECT (dc, "Adding uridecodebin to pipeline");
277 gst_bin_add (dc->priv->pipeline, dc->priv->uridecodebin);
279 dc->priv->pad_added_id =
280 g_signal_connect_object (dc->priv->uridecodebin, "pad-added",
281 G_CALLBACK (uridecodebin_pad_added_cb), dc, 0);
282 dc->priv->pad_remove_id =
283 g_signal_connect_object (dc->priv->uridecodebin, "pad-removed",
284 G_CALLBACK (uridecodebin_pad_removed_cb), dc, 0);
286 GST_LOG_OBJECT (dc, "Getting pipeline bus");
287 dc->priv->bus = gst_pipeline_get_bus ((GstPipeline *) dc->priv->pipeline);
289 dc->priv->bus_cb_id =
290 g_signal_connect_object (dc->priv->bus, "message",
291 G_CALLBACK (discoverer_bus_cb), dc, 0);
293 GST_DEBUG_OBJECT (dc, "Done initializing Discoverer");
295 /* This is ugly. We get the GType of decodebin so we can quickly detect
296 * when a decodebin is added to uridecodebin so we can set the
297 * post-stream-topology setting to TRUE */
298 dc->priv->element_added_id =
299 g_signal_connect_object (dc->priv->uridecodebin, "element-added",
300 G_CALLBACK (uridecodebin_element_added_cb), dc, 0);
301 tmp = gst_element_factory_make ("decodebin", NULL);
302 dc->priv->decodebin_type = G_OBJECT_TYPE (tmp);
303 gst_object_unref (tmp);
306 dc->priv->seeking_query = gst_query_new_seeking (format);
310 discoverer_reset (GstDiscoverer * dc)
312 GST_DEBUG_OBJECT (dc, "Resetting");
314 if (dc->priv->pending_uris) {
315 g_list_foreach (dc->priv->pending_uris, (GFunc) g_free, NULL);
316 g_list_free (dc->priv->pending_uris);
317 dc->priv->pending_uris = NULL;
320 if (dc->priv->pipeline)
321 gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_NULL);
324 #define DISCONNECT_SIGNAL(o,i) G_STMT_START{ \
325 if ((i) && g_signal_handler_is_connected ((o), (i))) \
326 g_signal_handler_disconnect ((o), (i)); \
331 gst_discoverer_dispose (GObject * obj)
333 GstDiscoverer *dc = (GstDiscoverer *) obj;
335 GST_DEBUG_OBJECT (dc, "Disposing");
337 discoverer_reset (dc);
339 if (G_LIKELY (dc->priv->pipeline)) {
340 /* Workaround for bug #118536 */
341 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_added_id);
342 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_remove_id);
343 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->element_added_id);
344 DISCONNECT_SIGNAL (dc->priv->bus, dc->priv->bus_cb_id);
346 /* pipeline was set to NULL in _reset */
347 gst_object_unref (dc->priv->pipeline);
348 gst_object_unref (dc->priv->bus);
350 dc->priv->pipeline = NULL;
351 dc->priv->uridecodebin = NULL;
352 dc->priv->bus = NULL;
355 gst_discoverer_stop (dc);
357 if (dc->priv->lock) {
358 g_mutex_free (dc->priv->lock);
359 dc->priv->lock = NULL;
362 if (dc->priv->seeking_query) {
363 gst_query_unref (dc->priv->seeking_query);
364 dc->priv->seeking_query = NULL;
367 G_OBJECT_CLASS (gst_discoverer_parent_class)->dispose (obj);
371 gst_discoverer_set_property (GObject * object, guint prop_id,
372 const GValue * value, GParamSpec * pspec)
374 GstDiscoverer *dc = (GstDiscoverer *) object;
378 gst_discoverer_set_timeout (dc, g_value_get_uint64 (value));
381 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
387 gst_discoverer_get_property (GObject * object, guint prop_id,
388 GValue * value, GParamSpec * pspec)
390 GstDiscoverer *dc = (GstDiscoverer *) object;
395 g_value_set_uint64 (value, dc->priv->timeout);
399 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
405 gst_discoverer_set_timeout (GstDiscoverer * dc, GstClockTime timeout)
407 GST_DEBUG_OBJECT (dc, "timeout : %" GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
409 /* FIXME : update current pending timeout if we're running */
411 dc->priv->timeout = timeout;
415 static GstPadProbeReturn
416 _event_probe (GstPad * pad, GstPadProbeInfo * info, PrivateStream * ps)
418 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
420 if (GST_EVENT_TYPE (event) == GST_EVENT_TAG) {
421 GstTagList *tl = NULL, *tmp;
423 gst_event_parse_tag (event, &tl);
424 GST_DEBUG_OBJECT (pad, "tags %" GST_PTR_FORMAT, tl);
426 /* If preroll is complete, drop these tags - the collected information is
427 * possibly already being processed and adding more tags would be racy */
428 if (G_LIKELY (ps->dc->priv->processing)) {
429 GST_DEBUG_OBJECT (pad, "private stream %p old tags %" GST_PTR_FORMAT, ps,
431 tmp = gst_tag_list_merge (ps->tags, tl, GST_TAG_MERGE_APPEND);
433 gst_tag_list_free (ps->tags);
435 GST_DEBUG_OBJECT (pad, "private stream %p new tags %" GST_PTR_FORMAT, ps,
438 GST_DEBUG_OBJECT (pad, "Dropping tags since preroll is done");
439 DISCO_UNLOCK (ps->dc);
442 return GST_PAD_PROBE_OK;
446 is_subtitle_caps (const GstCaps * caps)
448 static GstCaps *subs_caps = NULL;
451 subs_caps = gst_caps_from_string ("text/plain; text/x-pango-markup; "
452 "subpicture/x-pgs; subpicture/x-dvb; application/x-subtitle-unknown; "
453 "application/x-ssa; application/x-ass; subtitle/x-kate; "
454 "application/x-kate; video/x-dvd-subpicture; ");
457 return gst_caps_can_intersect (caps, subs_caps);
461 uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
465 GstPad *sinkpad = NULL;
468 GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
470 ps = g_slice_new0 (PrivateStream);
474 ps->queue = gst_element_factory_make ("queue", NULL);
475 ps->sink = gst_element_factory_make ("fakesink", NULL);
477 if (G_UNLIKELY (ps->queue == NULL || ps->sink == NULL))
480 g_object_set (ps->sink, "silent", TRUE, NULL);
481 g_object_set (ps->queue, "max-size-buffers", 1, "silent", TRUE, NULL);
483 caps = gst_pad_query_caps (pad, NULL);
485 if (is_subtitle_caps (caps)) {
486 /* Subtitle streams are sparse and may not provide any information - don't
487 * wait for data to preroll */
488 g_object_set (ps->sink, "async", FALSE, NULL);
491 gst_caps_unref (caps);
493 gst_bin_add_many (dc->priv->pipeline, ps->queue, ps->sink, NULL);
495 if (!gst_element_link_pads_full (ps->queue, "src", ps->sink, "sink",
496 GST_PAD_LINK_CHECK_NOTHING))
498 if (!gst_element_sync_state_with_parent (ps->sink))
500 if (!gst_element_sync_state_with_parent (ps->queue))
503 sinkpad = gst_element_get_static_pad (ps->queue, "sink");
506 if (gst_pad_link_full (pad, sinkpad,
507 GST_PAD_LINK_CHECK_NOTHING) != GST_PAD_LINK_OK)
509 gst_object_unref (sinkpad);
511 /* Add an event probe */
512 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
513 (GstPadProbeCallback) _event_probe, ps, NULL);
516 dc->priv->streams = g_list_append (dc->priv->streams, ps);
519 GST_DEBUG_OBJECT (dc, "Done handling pad");
524 GST_ERROR_OBJECT (dc, "Error while handling pad");
526 gst_object_unref (sinkpad);
528 gst_object_unref (ps->queue);
530 gst_object_unref (ps->sink);
531 g_slice_free (PrivateStream, ps);
536 uridecodebin_pad_removed_cb (GstElement * uridecodebin, GstPad * pad,
543 GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
545 /* Find the PrivateStream */
547 for (tmp = dc->priv->streams; tmp; tmp = tmp->next) {
548 ps = (PrivateStream *) tmp->data;
555 GST_DEBUG ("The removed pad wasn't controlled by us !");
559 dc->priv->streams = g_list_delete_link (dc->priv->streams, tmp);
562 gst_element_set_state (ps->sink, GST_STATE_NULL);
563 gst_element_set_state (ps->queue, GST_STATE_NULL);
564 gst_element_unlink (ps->queue, ps->sink);
566 sinkpad = gst_element_get_static_pad (ps->queue, "sink");
567 gst_pad_unlink (pad, sinkpad);
568 gst_object_unref (sinkpad);
570 /* references removed here */
571 gst_bin_remove_many (dc->priv->pipeline, ps->sink, ps->queue, NULL);
574 gst_tag_list_free (ps->tags);
577 g_slice_free (PrivateStream, ps);
579 GST_DEBUG ("Done handling pad");
582 static GstStructure *
583 collect_stream_information (GstDiscoverer * dc, PrivateStream * ps, guint idx)
589 stname = g_strdup_printf ("stream-%02d", idx);
590 st = gst_structure_new_empty (stname);
594 caps = gst_pad_get_current_caps (ps->pad);
596 GST_WARNING ("Couldn't get negotiated caps from %s:%s",
597 GST_DEBUG_PAD_NAME (ps->pad));
598 caps = gst_pad_query_caps (ps->pad, NULL);
601 GST_DEBUG ("Got caps %" GST_PTR_FORMAT, caps);
602 gst_structure_id_set (st, _CAPS_QUARK, GST_TYPE_CAPS, caps, NULL);
604 gst_caps_unref (caps);
607 gst_structure_id_set (st, _TAGS_QUARK, GST_TYPE_STRUCTURE, ps->tags, NULL);
612 /* Parses a set of caps and tags in st and populates a GstDiscovererStreamInfo
613 * structure (parent, if !NULL, otherwise it allocates one)
615 static GstDiscovererStreamInfo *
616 collect_information (GstDiscoverer * dc, const GstStructure * st,
617 GstDiscovererStreamInfo * parent)
620 GstStructure *caps_st, *tags_st;
625 if (!st || !gst_structure_id_has_field (st, _CAPS_QUARK)) {
626 GST_WARNING ("Couldn't find caps !");
630 return (GstDiscovererStreamInfo *)
631 g_object_new (GST_TYPE_DISCOVERER_STREAM_INFO, NULL);
634 gst_structure_id_get (st, _CAPS_QUARK, GST_TYPE_CAPS, &caps, NULL);
635 caps_st = gst_caps_get_structure (caps, 0);
636 name = gst_structure_get_name (caps_st);
638 if (g_str_has_prefix (name, "audio/")) {
639 GstDiscovererAudioInfo *info;
642 info = (GstDiscovererAudioInfo *) parent;
644 info = (GstDiscovererAudioInfo *)
645 g_object_new (GST_TYPE_DISCOVERER_AUDIO_INFO, NULL);
646 info->parent.caps = caps;
649 if (gst_structure_get_int (caps_st, "rate", &tmp))
650 info->sample_rate = (guint) tmp;
652 if (gst_structure_get_int (caps_st, "channels", &tmp))
653 info->channels = (guint) tmp;
655 if (gst_structure_get_int (caps_st, "depth", &tmp))
656 info->depth = (guint) tmp;
658 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
659 gst_structure_id_get (st, _TAGS_QUARK,
660 GST_TYPE_STRUCTURE, &tags_st, NULL);
661 if (gst_structure_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
662 gst_structure_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
663 info->bitrate = utmp;
665 if (gst_structure_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
666 info->max_bitrate = utmp;
668 /* FIXME: Is it worth it to remove the tags we've parsed? */
669 info->parent.tags = gst_tag_list_merge (info->parent.tags,
670 (GstTagList *) tags_st, GST_TAG_MERGE_REPLACE);
672 gst_structure_free (tags_st);
675 if (!info->language && ((GstDiscovererStreamInfo *) info)->tags) {
677 if (gst_tag_list_get_string (((GstDiscovererStreamInfo *) info)->tags,
678 GST_TAG_LANGUAGE_CODE, &language)) {
679 info->language = language;
683 return (GstDiscovererStreamInfo *) info;
685 } else if (g_str_has_prefix (name, "video/") ||
686 g_str_has_prefix (name, "image/")) {
687 GstDiscovererVideoInfo *info;
691 info = (GstDiscovererVideoInfo *) parent;
693 info = (GstDiscovererVideoInfo *)
694 g_object_new (GST_TYPE_DISCOVERER_VIDEO_INFO, NULL);
695 info->parent.caps = caps;
698 /* FIXME : gst_video_info_from_caps only works with raw caps,
699 * wouldn't we want to get all the info below for non-raw caps ?
701 if (g_str_has_prefix (name, "video/x-raw") &&
702 gst_video_info_from_caps (&vinfo, caps)) {
703 info->width = (guint) vinfo.width;
704 info->height = (guint) vinfo.height;
706 info->depth = (guint) 0;
708 info->par_num = vinfo.par_n;
709 info->par_denom = vinfo.par_d;
711 info->framerate_num = vinfo.fps_n;
712 info->framerate_denom = vinfo.fps_d;
714 info->interlaced = (vinfo.flags & GST_VIDEO_FLAG_INTERLACED) != 0;
717 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
718 gst_structure_id_get (st, _TAGS_QUARK,
719 GST_TYPE_STRUCTURE, &tags_st, NULL);
720 if (gst_structure_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
721 gst_structure_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
722 info->bitrate = utmp;
724 if (gst_structure_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
725 info->max_bitrate = utmp;
727 /* FIXME: Is it worth it to remove the tags we've parsed? */
728 info->parent.tags = gst_tag_list_merge (info->parent.tags,
729 (GstTagList *) tags_st, GST_TAG_MERGE_REPLACE);
730 gst_structure_free (tags_st);
733 return (GstDiscovererStreamInfo *) info;
735 } else if (is_subtitle_caps (caps)) {
736 GstDiscovererSubtitleInfo *info;
739 info = (GstDiscovererSubtitleInfo *) parent;
741 info = (GstDiscovererSubtitleInfo *)
742 g_object_new (GST_TYPE_DISCOVERER_SUBTITLE_INFO, NULL);
743 info->parent.caps = caps;
746 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
747 const gchar *language;
749 gst_structure_id_get (st, _TAGS_QUARK,
750 GST_TYPE_STRUCTURE, &tags_st, NULL);
752 language = gst_structure_get_string (caps_st, GST_TAG_LANGUAGE_CODE);
754 info->language = g_strdup (language);
756 /* FIXME: Is it worth it to remove the tags we've parsed? */
757 info->parent.tags = gst_tag_list_merge (info->parent.tags,
758 (GstTagList *) tags_st, GST_TAG_MERGE_REPLACE);
759 gst_structure_free (tags_st);
763 if (!info->language && ((GstDiscovererStreamInfo *) info)->tags) {
765 if (gst_tag_list_get_string (((GstDiscovererStreamInfo *) info)->tags,
766 GST_TAG_LANGUAGE_CODE, &language)) {
767 info->language = language;
771 return (GstDiscovererStreamInfo *) info;
774 /* None of the above - populate what information we can */
775 GstDiscovererStreamInfo *info;
780 info = (GstDiscovererStreamInfo *)
781 g_object_new (GST_TYPE_DISCOVERER_STREAM_INFO, NULL);
785 if (gst_structure_id_get (st, _TAGS_QUARK,
786 GST_TYPE_STRUCTURE, &tags_st, NULL)) {
787 info->tags = gst_tag_list_merge (info->tags, (GstTagList *) tags_st,
788 GST_TAG_MERGE_REPLACE);
789 gst_structure_free (tags_st);
797 static GstStructure *
798 find_stream_for_node (GstDiscoverer * dc, const GstStructure * topology)
801 GstPad *target_pad = NULL;
802 GstStructure *st = NULL;
807 if (!gst_structure_id_has_field (topology, _TOPOLOGY_PAD_QUARK)) {
808 GST_DEBUG ("Could not find pad for node %" GST_PTR_FORMAT "\n", topology);
812 gst_structure_id_get (topology, _TOPOLOGY_PAD_QUARK,
813 GST_TYPE_PAD, &pad, NULL);
815 if (!dc->priv->streams)
818 for (i = 0, tmp = dc->priv->streams; tmp; tmp = tmp->next, i++) {
819 ps = (PrivateStream *) tmp->data;
821 target_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (ps->pad));
822 gst_object_unref (target_pad);
824 if (target_pad == pad)
829 st = collect_stream_information (dc, ps, i);
831 gst_object_unref (pad);
837 child_is_raw_stream (GstCaps * parent, GstCaps * child)
839 GstStructure *st1, *st2;
840 const gchar *name1, *name2;
842 st1 = gst_caps_get_structure (parent, 0);
843 name1 = gst_structure_get_name (st1);
844 st2 = gst_caps_get_structure (child, 0);
845 name2 = gst_structure_get_name (st2);
847 if ((g_str_has_prefix (name1, "audio/") &&
848 g_str_has_prefix (name2, "audio/x-raw")) ||
849 ((g_str_has_prefix (name1, "video/") ||
850 g_str_has_prefix (name1, "image/")) &&
851 g_str_has_prefix (name2, "video/x-raw"))) {
852 /* child is the "raw" sub-stream corresponding to parent */
856 if (is_subtitle_caps (parent))
862 /* If a parent is non-NULL, collected stream information will be appended to it
863 * (and where the information exists, it will be overriden)
865 static GstDiscovererStreamInfo *
866 parse_stream_topology (GstDiscoverer * dc, const GstStructure * topology,
867 GstDiscovererStreamInfo * parent)
869 GstDiscovererStreamInfo *res = NULL;
870 GstCaps *caps = NULL;
871 const GValue *nval = NULL;
873 GST_DEBUG ("parsing: %" GST_PTR_FORMAT, topology);
875 nval = gst_structure_get_value (topology, "next");
877 if (nval == NULL || GST_VALUE_HOLDS_STRUCTURE (nval)) {
878 GstStructure *st = find_stream_for_node (dc, topology);
879 gboolean add_to_list = TRUE;
882 res = collect_information (dc, st, parent);
883 gst_structure_free (st);
885 /* Didn't find a stream structure, so let's just use the caps we have */
886 res = collect_information (dc, topology, parent);
890 /* FIXME : aggregate with information from main streams */
891 GST_DEBUG ("Coudn't find 'next' ! might be the last entry");
894 const GstStructure *st;
896 st = gst_value_get_structure (nval);
898 GST_DEBUG ("next is a structure %" GST_PTR_FORMAT, st);
903 if (gst_structure_id_get (st, _CAPS_QUARK, GST_TYPE_CAPS, &caps, NULL)) {
904 if (gst_caps_can_intersect (parent->caps, caps)) {
905 /* We sometimes get an extra sub-stream from the parser. If this is
906 * the case, we just replace the parent caps with this stream's caps
907 * since they might contain more information */
908 gst_caps_unref (parent->caps);
911 parse_stream_topology (dc, st, parent);
914 } else if (child_is_raw_stream (parent->caps, caps)) {
915 /* This is the "raw" stream corresponding to the parent. This
916 * contains more information than the parent, tags etc. */
917 parse_stream_topology (dc, st, parent);
919 gst_caps_unref (caps);
922 GstDiscovererStreamInfo *next = parse_stream_topology (dc, st, NULL);
924 next->previous = res;
930 dc->priv->current_info->stream_list =
931 g_list_append (dc->priv->current_info->stream_list, res);
934 } else if (GST_VALUE_HOLDS_LIST (nval)) {
936 GstDiscovererContainerInfo *cont;
939 if (!gst_structure_id_get (topology, _CAPS_QUARK,
940 GST_TYPE_CAPS, &caps, NULL))
941 GST_WARNING ("Couldn't find caps !");
943 len = gst_value_list_get_size (nval);
944 GST_DEBUG ("next is a list of %d entries", len);
946 cont = (GstDiscovererContainerInfo *)
947 g_object_new (GST_TYPE_DISCOVERER_CONTAINER_INFO, NULL);
948 cont->parent.caps = caps;
949 res = (GstDiscovererStreamInfo *) cont;
951 if (gst_structure_id_has_field (topology, _TAGS_QUARK)) {
954 gst_structure_id_get (topology, _TAGS_QUARK,
955 GST_TYPE_STRUCTURE, &tags, NULL);
957 GST_DEBUG ("Merge tags %" GST_PTR_FORMAT, tags);
960 gst_tag_list_merge (cont->parent.tags, (GstTagList *) tags,
961 GST_TAG_MERGE_APPEND);
962 gst_tag_list_free (tags);
963 if (cont->parent.tags)
964 gst_tag_list_free (cont->parent.tags);
965 cont->parent.tags = tmp;
966 GST_DEBUG ("Container info tags %" GST_PTR_FORMAT, tmp);
969 for (i = 0; i < len; i++) {
970 const GValue *subv = gst_value_list_get_value (nval, i);
971 const GstStructure *subst = gst_value_get_structure (subv);
972 GstDiscovererStreamInfo *substream;
974 GST_DEBUG ("%d %" GST_PTR_FORMAT, i, subst);
976 substream = parse_stream_topology (dc, subst, NULL);
978 substream->previous = res;
980 g_list_append (cont->streams,
981 gst_discoverer_stream_info_ref (substream));
988 /* Called when pipeline is pre-rolled */
990 discoverer_collect (GstDiscoverer * dc)
992 GST_DEBUG ("Collecting information");
994 /* Stop the timeout handler if present */
995 if (dc->priv->timeoutid) {
996 g_source_remove (dc->priv->timeoutid);
997 dc->priv->timeoutid = 0;
1000 if (dc->priv->streams) {
1001 /* FIXME : Make this querying optional */
1003 GstElement *pipeline = (GstElement *) dc->priv->pipeline;
1006 GST_DEBUG ("Attempting to query duration");
1008 if (gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur)) {
1009 GST_DEBUG ("Got duration %" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
1010 dc->priv->current_info->duration = (guint64) dur;
1013 if (dc->priv->seeking_query) {
1014 if (gst_element_query (pipeline, dc->priv->seeking_query)) {
1018 gst_query_parse_seeking (dc->priv->seeking_query, &format,
1019 &seekable, NULL, NULL);
1020 if (format == GST_FORMAT_TIME) {
1021 GST_DEBUG ("Got seekable %d", seekable);
1022 dc->priv->current_info->seekable = seekable;
1028 if (dc->priv->current_topology)
1029 dc->priv->current_info->stream_info = parse_stream_topology (dc,
1030 dc->priv->current_topology, NULL);
1033 * Images need some special handling. They do not have a duration, have
1034 * caps named image/<foo> (th exception being MJPEG video which is also
1035 * type image/jpeg), and should consist of precisely one stream (actually
1036 * initially there are 2, the image and raw stream, but we squash these
1037 * while parsing the stream topology). At some point, if we find that these
1038 * conditions are not sufficient, we can count the number of decoders and
1039 * parsers in the chain, and if there's more than one decoder, or any
1040 * parser at all, we should not mark this as an image.
1042 if (dc->priv->current_info->duration == 0 &&
1043 dc->priv->current_info->stream_info != NULL &&
1044 dc->priv->current_info->stream_info->next == NULL) {
1046 gst_caps_get_structure (dc->priv->current_info->stream_info->caps, 0);
1048 if (g_str_has_prefix (gst_structure_get_name (st), "image/"))
1049 ((GstDiscovererVideoInfo *) dc->priv->current_info->
1050 stream_info)->is_image = TRUE;
1054 if (dc->priv->async) {
1055 GST_DEBUG ("Emitting 'discoverered'");
1056 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_DISCOVERED], 0,
1057 dc->priv->current_info, dc->priv->current_error);
1058 /* Clients get a copy of current_info since it is a boxed type */
1059 gst_discoverer_info_unref (dc->priv->current_info);
1064 get_async_cb (gpointer cb_data, GSource * source, GSourceFunc * func,
1067 *func = (GSourceFunc) async_timeout_cb;
1071 /* Wrapper since GSourceCallbackFuncs don't expect a return value from ref() */
1073 _void_g_object_ref (gpointer object)
1075 g_object_ref (G_OBJECT (object));
1079 handle_current_async (GstDiscoverer * dc)
1082 static GSourceCallbackFuncs cb_funcs = {
1088 /* Attach a timeout to the main context */
1089 source = g_timeout_source_new (dc->priv->timeout / GST_MSECOND);
1090 g_source_set_callback_indirect (source, g_object_ref (dc), &cb_funcs);
1091 dc->priv->timeoutid = g_source_attach (source, dc->priv->ctx);
1092 g_source_unref (source);
1096 /* Returns TRUE if processing should stop */
1098 handle_message (GstDiscoverer * dc, GstMessage * msg)
1100 gboolean done = FALSE;
1102 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "got a %s message",
1103 GST_MESSAGE_TYPE_NAME (msg));
1105 switch (GST_MESSAGE_TYPE (msg)) {
1106 case GST_MESSAGE_ERROR:{
1110 gst_message_parse_error (msg, &gerr, &debug);
1111 GST_WARNING_OBJECT (GST_MESSAGE_SRC (msg),
1112 "Got an error [debug:%s], [message:%s]", debug, gerr->message);
1113 dc->priv->current_error = gerr;
1116 /* We need to stop */
1119 GST_DEBUG ("Setting result to ERROR");
1120 dc->priv->current_info->result = GST_DISCOVERER_ERROR;
1124 case GST_MESSAGE_EOS:
1125 GST_DEBUG ("Got EOS !");
1129 case GST_MESSAGE_ASYNC_DONE:
1130 if (GST_MESSAGE_SRC (msg) == (GstObject *) dc->priv->pipeline) {
1131 GST_DEBUG ("Finished changing state asynchronously");
1137 case GST_MESSAGE_ELEMENT:
1140 const GstStructure *structure;
1142 structure = gst_message_get_structure (msg);
1143 sttype = gst_structure_get_name_id (structure);
1144 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1145 "structure %" GST_PTR_FORMAT, structure);
1146 if (sttype == _MISSING_PLUGIN_QUARK) {
1147 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1148 "Setting result to MISSING_PLUGINS");
1149 dc->priv->current_info->result = GST_DISCOVERER_MISSING_PLUGINS;
1150 dc->priv->current_info->misc = gst_structure_copy (structure);
1151 } else if (sttype == _STREAM_TOPOLOGY_QUARK) {
1152 dc->priv->current_topology = gst_structure_copy (structure);
1157 case GST_MESSAGE_TAG:
1159 GstTagList *tl, *tmp;
1161 gst_message_parse_tag (msg, &tl);
1162 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Got tags %" GST_PTR_FORMAT, tl);
1163 /* Merge with current tags */
1165 gst_tag_list_merge (dc->priv->current_info->tags, tl,
1166 GST_TAG_MERGE_APPEND);
1167 gst_tag_list_free (tl);
1168 if (dc->priv->current_info->tags)
1169 gst_tag_list_free (dc->priv->current_info->tags);
1170 dc->priv->current_info->tags = tmp;
1171 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Current info %p, tags %"
1172 GST_PTR_FORMAT, dc->priv->current_info, tmp);
1185 handle_current_sync (GstDiscoverer * dc)
1188 gdouble deadline = ((gdouble) dc->priv->timeout) / GST_SECOND;
1190 gboolean done = FALSE;
1192 timer = g_timer_new ();
1193 g_timer_start (timer);
1196 /* poll bus with timeout */
1197 /* FIXME : make the timeout more fine-tuned */
1198 if ((msg = gst_bus_timed_pop (dc->priv->bus, GST_SECOND / 2))) {
1199 done = handle_message (dc, msg);
1200 gst_message_unref (msg);
1203 } while (!done && (g_timer_elapsed (timer, NULL) < deadline));
1207 GST_DEBUG ("we timed out! Setting result to TIMEOUT");
1208 dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
1213 g_timer_stop (timer);
1214 g_timer_destroy (timer);
1218 _setup_locked (GstDiscoverer * dc)
1220 GstStateChangeReturn ret;
1222 GST_DEBUG ("Setting up");
1224 /* Pop URI off the pending URI list */
1225 dc->priv->current_info =
1226 (GstDiscovererInfo *) g_object_new (GST_TYPE_DISCOVERER_INFO, NULL);
1227 dc->priv->current_info->uri = (gchar *) dc->priv->pending_uris->data;
1228 dc->priv->pending_uris =
1229 g_list_delete_link (dc->priv->pending_uris, dc->priv->pending_uris);
1231 /* set uri on uridecodebin */
1232 g_object_set (dc->priv->uridecodebin, "uri", dc->priv->current_info->uri,
1235 GST_DEBUG ("Current is now %s", dc->priv->current_info->uri);
1237 dc->priv->processing = TRUE;
1239 /* set pipeline to PAUSED */
1241 GST_DEBUG ("Setting pipeline to PAUSED");
1243 gst_element_set_state ((GstElement *) dc->priv->pipeline,
1247 GST_DEBUG_OBJECT (dc, "Pipeline going to PAUSED : %s",
1248 gst_element_state_change_return_get_name (ret));
1252 discoverer_cleanup (GstDiscoverer * dc)
1254 GST_DEBUG ("Cleaning up");
1256 gst_bus_set_flushing (dc->priv->bus, TRUE);
1257 gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_READY);
1258 gst_bus_set_flushing (dc->priv->bus, FALSE);
1261 if (dc->priv->current_error)
1262 g_error_free (dc->priv->current_error);
1263 dc->priv->current_error = NULL;
1264 if (dc->priv->current_topology) {
1265 gst_structure_free (dc->priv->current_topology);
1266 dc->priv->current_topology = NULL;
1269 dc->priv->current_info = NULL;
1271 /* Try popping the next uri */
1272 if (dc->priv->async) {
1273 if (dc->priv->pending_uris != NULL) {
1277 handle_current_async (dc);
1281 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_FINISHED], 0);
1290 discoverer_bus_cb (GstBus * bus, GstMessage * msg, GstDiscoverer * dc)
1292 if (dc->priv->processing) {
1293 if (handle_message (dc, msg)) {
1294 GST_DEBUG ("Stopping asynchronously");
1295 /* Serialise with _event_probe() */
1297 dc->priv->processing = FALSE;
1299 discoverer_collect (dc);
1300 discoverer_cleanup (dc);
1306 async_timeout_cb (GstDiscoverer * dc)
1308 if (!g_source_is_destroyed (g_main_current_source ())) {
1309 dc->priv->timeoutid = 0;
1310 GST_DEBUG ("Setting result to TIMEOUT");
1311 dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
1312 dc->priv->processing = FALSE;
1313 discoverer_collect (dc);
1314 discoverer_cleanup (dc);
1319 /* If there is a pending URI, it will pop it from the list of pending
1320 * URIs and start the discovery on it.
1322 * Returns GST_DISCOVERER_OK if the next URI was popped and is processing,
1323 * else a error flag.
1325 static GstDiscovererResult
1326 start_discovering (GstDiscoverer * dc)
1328 GstDiscovererResult res = GST_DISCOVERER_OK;
1330 GST_DEBUG ("Starting");
1333 if (dc->priv->pending_uris == NULL) {
1334 GST_WARNING ("No URI to process");
1335 res = GST_DISCOVERER_URI_INVALID;
1340 if (dc->priv->current_info != NULL) {
1341 GST_WARNING ("Already processing a file");
1342 res = GST_DISCOVERER_BUSY;
1347 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_STARTING], 0);
1353 if (dc->priv->async)
1354 handle_current_async (dc);
1356 handle_current_sync (dc);
1364 * gst_discoverer_start:
1365 * @discoverer: A #GstDiscoverer
1367 * Allow asynchronous discovering of URIs to take place.
1368 * A #GMainLoop must be available for #GstDiscoverer to properly work in
1369 * asynchronous mode.
1374 gst_discoverer_start (GstDiscoverer * discoverer)
1377 GMainContext *ctx = NULL;
1379 GST_DEBUG_OBJECT (discoverer, "Starting...");
1381 if (discoverer->priv->async) {
1382 GST_DEBUG_OBJECT (discoverer, "We were already started");
1386 discoverer->priv->async = TRUE;
1387 discoverer->priv->running = TRUE;
1389 ctx = g_main_context_get_thread_default ();
1391 /* Connect to bus signals */
1393 ctx = g_main_context_default ();
1395 source = gst_bus_create_watch (discoverer->priv->bus);
1396 g_source_set_callback (source, (GSourceFunc) gst_bus_async_signal_func,
1398 discoverer->priv->sourceid = g_source_attach (source, ctx);
1399 g_source_unref (source);
1400 discoverer->priv->ctx = g_main_context_ref (ctx);
1402 start_discovering (discoverer);
1403 GST_DEBUG_OBJECT (discoverer, "Started");
1407 * gst_discoverer_stop:
1408 * @discoverer: A #GstDiscoverer
1410 * Stop the discovery of any pending URIs and clears the list of
1411 * pending URIS (if any).
1416 gst_discoverer_stop (GstDiscoverer * discoverer)
1418 GST_DEBUG_OBJECT (discoverer, "Stopping...");
1420 if (!discoverer->priv->async) {
1421 GST_DEBUG_OBJECT (discoverer,
1422 "We were already stopped, or running synchronously");
1426 DISCO_LOCK (discoverer);
1427 if (discoverer->priv->processing) {
1428 /* We prevent any further processing by setting the bus to
1429 * flushing and setting the pipeline to READY.
1430 * _reset() will take care of the rest of the cleanup */
1431 if (discoverer->priv->bus)
1432 gst_bus_set_flushing (discoverer->priv->bus, TRUE);
1433 if (discoverer->priv->pipeline)
1434 gst_element_set_state ((GstElement *) discoverer->priv->pipeline,
1437 discoverer->priv->running = FALSE;
1438 DISCO_UNLOCK (discoverer);
1440 /* Remove timeout handler */
1441 if (discoverer->priv->timeoutid) {
1442 g_source_remove (discoverer->priv->timeoutid);
1443 discoverer->priv->timeoutid = 0;
1445 /* Remove signal watch */
1446 if (discoverer->priv->sourceid) {
1447 g_source_remove (discoverer->priv->sourceid);
1448 discoverer->priv->sourceid = 0;
1450 /* Unref main context */
1451 if (discoverer->priv->ctx) {
1452 g_main_context_unref (discoverer->priv->ctx);
1453 discoverer->priv->ctx = NULL;
1455 discoverer_reset (discoverer);
1457 discoverer->priv->async = FALSE;
1459 GST_DEBUG_OBJECT (discoverer, "Stopped");
1463 * gst_discoverer_discover_uri_async:
1464 * @discoverer: A #GstDiscoverer
1465 * @uri: the URI to add.
1467 * Appends the given @uri to the list of URIs to discoverer. The actual
1468 * discovery of the @uri will only take place if gst_discoverer_start() has
1471 * A copy of @uri will be made internally, so the caller can safely g_free()
1474 * Returns: %TRUE if the @uri was successfully appended to the list of pending
1480 gst_discoverer_discover_uri_async (GstDiscoverer * discoverer,
1485 GST_DEBUG_OBJECT (discoverer, "uri : %s", uri);
1487 DISCO_LOCK (discoverer);
1488 can_run = (discoverer->priv->pending_uris == NULL);
1489 discoverer->priv->pending_uris =
1490 g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
1491 DISCO_UNLOCK (discoverer);
1494 start_discovering (discoverer);
1500 /* Synchronous mode */
1502 * gst_discoverer_discover_uri:
1503 * @discoverer: A #GstDiscoverer
1504 * @uri: The URI to run on.
1505 * @err: (out) (allow-none): If an error occurred, this field will be filled in.
1507 * Synchronously discovers the given @uri.
1509 * A copy of @uri will be made internally, so the caller can safely g_free()
1512 * Returns: (transfer full): the result of the scanning. Can be %NULL if an
1518 gst_discoverer_discover_uri (GstDiscoverer * discoverer, const gchar * uri,
1521 GstDiscovererResult res = 0;
1522 GstDiscovererInfo *info;
1524 GST_DEBUG_OBJECT (discoverer, "uri:%s", uri);
1526 DISCO_LOCK (discoverer);
1527 if (G_UNLIKELY (discoverer->priv->current_info)) {
1528 DISCO_UNLOCK (discoverer);
1529 GST_WARNING_OBJECT (discoverer, "Already handling a uri");
1533 discoverer->priv->pending_uris =
1534 g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
1535 DISCO_UNLOCK (discoverer);
1537 res = start_discovering (discoverer);
1538 discoverer_collect (discoverer);
1542 if (discoverer->priv->current_error)
1543 *err = g_error_copy (discoverer->priv->current_error);
1547 if (res != GST_DISCOVERER_OK) {
1548 GST_DEBUG ("Setting result to %d (was %d)", res,
1549 discoverer->priv->current_info->result);
1550 discoverer->priv->current_info->result = res;
1552 info = discoverer->priv->current_info;
1554 discoverer_cleanup (discoverer);
1560 * gst_discoverer_new:
1561 * @timeout: timeout per file, in nanoseconds. Allowed are values between
1562 * one second (#GST_SECOND) and one hour (3600 * #GST_SECOND)
1563 * @err: a pointer to a #GError. can be %NULL
1565 * Creates a new #GstDiscoverer with the provided timeout.
1567 * Returns: (transfer full): The new #GstDiscoverer.
1568 * If an error occurred when creating the discoverer, @err will be set
1569 * accordingly and %NULL will be returned. If @err is set, the caller must
1570 * free it when no longer needed using g_error_free().
1575 gst_discoverer_new (GstClockTime timeout, GError ** err)
1579 res = g_object_new (GST_TYPE_DISCOVERER, "timeout", timeout, NULL);
1580 if (res->priv->uridecodebin == NULL) {
1582 *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
1583 "Couldn't create 'uridecodebin' element");
1584 gst_object_unref (res);