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 GST_DEBUG_CATEGORY_STATIC (discoverer_debug);
54 #define GST_CAT_DEFAULT discoverer_debug
56 static GQuark _CAPS_QUARK;
57 static GQuark _TAGS_QUARK;
58 static GQuark _MISSING_PLUGIN_QUARK;
59 static GQuark _STREAM_TOPOLOGY_QUARK;
60 static GQuark _TOPOLOGY_PAD_QUARK;
72 struct _GstDiscovererPrivate
76 /* allowed time to discover each uri in nanoseconds */
79 /* list of pending URI to process (current excluded) */
84 /* TRUE if processing a URI */
87 /* TRUE if discoverer has been started */
91 GstDiscovererInfo *current_info;
92 GError *current_error;
93 GstStructure *current_topology;
95 /* List of private streams */
100 GstElement *uridecodebin;
103 GType decodebin_type;
105 /* Custom main context variables */
110 /* reusable queries */
111 GstQuery *seeking_query;
113 /* Handler ids for various callbacks */
115 gulong pad_remove_id;
116 gulong element_added_id;
120 #define DISCO_LOCK(dc) g_mutex_lock (dc->priv->lock);
121 #define DISCO_UNLOCK(dc) g_mutex_unlock (dc->priv->lock);
126 GST_DEBUG_CATEGORY_INIT (discoverer_debug, "discoverer", 0, "Discoverer");
128 _CAPS_QUARK = g_quark_from_static_string ("caps");
129 _TAGS_QUARK = g_quark_from_static_string ("tags");
130 _MISSING_PLUGIN_QUARK = g_quark_from_static_string ("missing-plugin");
131 _STREAM_TOPOLOGY_QUARK = g_quark_from_static_string ("stream-topology");
132 _TOPOLOGY_PAD_QUARK = g_quark_from_static_string ("pad");
135 G_DEFINE_TYPE_EXTENDED (GstDiscoverer, gst_discoverer, G_TYPE_OBJECT, 0,
146 #define DEFAULT_PROP_TIMEOUT 15 * GST_SECOND
154 static guint gst_discoverer_signals[LAST_SIGNAL] = { 0 };
156 static void gst_discoverer_set_timeout (GstDiscoverer * dc,
157 GstClockTime timeout);
158 static gboolean async_timeout_cb (GstDiscoverer * dc);
160 static void discoverer_bus_cb (GstBus * bus, GstMessage * msg,
162 static void uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
164 static void uridecodebin_pad_removed_cb (GstElement * uridecodebin,
165 GstPad * pad, GstDiscoverer * dc);
167 static void gst_discoverer_dispose (GObject * dc);
168 static void gst_discoverer_set_property (GObject * object, guint prop_id,
169 const GValue * value, GParamSpec * pspec);
170 static void gst_discoverer_get_property (GObject * object, guint prop_id,
171 GValue * value, GParamSpec * pspec);
174 gst_discoverer_class_init (GstDiscovererClass * klass)
176 GObjectClass *gobject_class = (GObjectClass *) klass;
178 gobject_class->dispose = gst_discoverer_dispose;
180 gobject_class->set_property = gst_discoverer_set_property;
181 gobject_class->get_property = gst_discoverer_get_property;
183 g_type_class_add_private (klass, sizeof (GstDiscovererPrivate));
187 * GstDiscoverer:timeout
189 * The duration (in nanoseconds) after which the discovery of an individual
192 * If the discovery of a URI times out, the %GST_DISCOVERER_TIMEOUT will be
193 * set on the result flags.
195 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
196 g_param_spec_uint64 ("timeout", "timeout", "Timeout",
197 GST_SECOND, 3600 * GST_SECOND, DEFAULT_PROP_TIMEOUT,
198 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
202 * GstDiscoverer::finished:
203 * @discoverer: the #GstDiscoverer
205 * Will be emitted when all pending URIs have been processed.
207 gst_discoverer_signals[SIGNAL_FINISHED] =
208 g_signal_new ("finished", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
209 G_STRUCT_OFFSET (GstDiscovererClass, finished),
210 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
213 * GstDiscoverer::starting:
214 * @discoverer: the #GstDiscoverer
216 * Will be emitted when the discover starts analyzing the pending URIs
218 gst_discoverer_signals[SIGNAL_STARTING] =
219 g_signal_new ("starting", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
220 G_STRUCT_OFFSET (GstDiscovererClass, starting),
221 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
224 * GstDiscoverer::discovered:
225 * @discoverer: the #GstDiscoverer
226 * @info: the results #GstDiscovererInfo
227 * @error: (type GLib.Error): #GError, which will be non-NULL if an error
228 * occurred during discovery
230 * Will be emitted when all information on a URI could be discovered.
232 gst_discoverer_signals[SIGNAL_DISCOVERED] =
233 g_signal_new ("discovered", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
234 G_STRUCT_OFFSET (GstDiscovererClass, discovered),
235 NULL, NULL, pbutils_marshal_VOID__POINTER_BOXED,
236 G_TYPE_NONE, 2, GST_TYPE_DISCOVERER_INFO, GST_TYPE_G_ERROR);
240 uridecodebin_element_added_cb (GstElement * uridecodebin,
241 GstElement * child, GstDiscoverer * dc)
243 GST_DEBUG ("New element added to uridecodebin : %s",
244 GST_ELEMENT_NAME (child));
246 if (G_OBJECT_TYPE (child) == dc->priv->decodebin_type) {
247 g_object_set (child, "post-stream-topology", TRUE, NULL);
252 gst_discoverer_init (GstDiscoverer * dc)
255 GstFormat format = GST_FORMAT_TIME;
257 dc->priv = G_TYPE_INSTANCE_GET_PRIVATE (dc, GST_TYPE_DISCOVERER,
258 GstDiscovererPrivate);
260 dc->priv->timeout = DEFAULT_PROP_TIMEOUT;
261 dc->priv->async = FALSE;
263 dc->priv->lock = g_mutex_new ();
265 GST_LOG ("Creating pipeline");
266 dc->priv->pipeline = (GstBin *) gst_pipeline_new ("Discoverer");
267 GST_LOG_OBJECT (dc, "Creating uridecodebin");
268 dc->priv->uridecodebin =
269 gst_element_factory_make ("uridecodebin", "discoverer-uri");
270 if (G_UNLIKELY (dc->priv->uridecodebin == NULL)) {
271 GST_ERROR ("Can't create uridecodebin");
274 GST_LOG_OBJECT (dc, "Adding uridecodebin to pipeline");
275 gst_bin_add (dc->priv->pipeline, dc->priv->uridecodebin);
277 dc->priv->pad_added_id =
278 g_signal_connect_object (dc->priv->uridecodebin, "pad-added",
279 G_CALLBACK (uridecodebin_pad_added_cb), dc, 0);
280 dc->priv->pad_remove_id =
281 g_signal_connect_object (dc->priv->uridecodebin, "pad-removed",
282 G_CALLBACK (uridecodebin_pad_removed_cb), dc, 0);
284 GST_LOG_OBJECT (dc, "Getting pipeline bus");
285 dc->priv->bus = gst_pipeline_get_bus ((GstPipeline *) dc->priv->pipeline);
287 dc->priv->bus_cb_id =
288 g_signal_connect_object (dc->priv->bus, "message",
289 G_CALLBACK (discoverer_bus_cb), dc, 0);
291 GST_DEBUG_OBJECT (dc, "Done initializing Discoverer");
293 /* This is ugly. We get the GType of decodebin so we can quickly detect
294 * when a decodebin is added to uridecodebin so we can set the
295 * post-stream-topology setting to TRUE */
296 dc->priv->element_added_id =
297 g_signal_connect_object (dc->priv->uridecodebin, "element-added",
298 G_CALLBACK (uridecodebin_element_added_cb), dc, 0);
299 tmp = gst_element_factory_make ("decodebin", NULL);
300 dc->priv->decodebin_type = G_OBJECT_TYPE (tmp);
301 gst_object_unref (tmp);
304 dc->priv->seeking_query = gst_query_new_seeking (format);
308 discoverer_reset (GstDiscoverer * dc)
310 GST_DEBUG_OBJECT (dc, "Resetting");
312 if (dc->priv->pending_uris) {
313 g_list_foreach (dc->priv->pending_uris, (GFunc) g_free, NULL);
314 g_list_free (dc->priv->pending_uris);
315 dc->priv->pending_uris = NULL;
318 if (dc->priv->pipeline)
319 gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_NULL);
322 #define DISCONNECT_SIGNAL(o,i) G_STMT_START{ \
323 if ((i) && g_signal_handler_is_connected ((o), (i))) \
324 g_signal_handler_disconnect ((o), (i)); \
329 gst_discoverer_dispose (GObject * obj)
331 GstDiscoverer *dc = (GstDiscoverer *) obj;
333 GST_DEBUG_OBJECT (dc, "Disposing");
335 discoverer_reset (dc);
337 if (G_LIKELY (dc->priv->pipeline)) {
338 /* Workaround for bug #118536 */
339 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_added_id);
340 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_remove_id);
341 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->element_added_id);
342 DISCONNECT_SIGNAL (dc->priv->bus, dc->priv->bus_cb_id);
344 /* pipeline was set to NULL in _reset */
345 gst_object_unref (dc->priv->pipeline);
346 gst_object_unref (dc->priv->bus);
348 dc->priv->pipeline = NULL;
349 dc->priv->uridecodebin = NULL;
350 dc->priv->bus = NULL;
353 gst_discoverer_stop (dc);
355 if (dc->priv->lock) {
356 g_mutex_free (dc->priv->lock);
357 dc->priv->lock = NULL;
360 if (dc->priv->seeking_query) {
361 gst_query_unref (dc->priv->seeking_query);
362 dc->priv->seeking_query = NULL;
365 G_OBJECT_CLASS (gst_discoverer_parent_class)->dispose (obj);
369 gst_discoverer_set_property (GObject * object, guint prop_id,
370 const GValue * value, GParamSpec * pspec)
372 GstDiscoverer *dc = (GstDiscoverer *) object;
376 gst_discoverer_set_timeout (dc, g_value_get_uint64 (value));
379 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
385 gst_discoverer_get_property (GObject * object, guint prop_id,
386 GValue * value, GParamSpec * pspec)
388 GstDiscoverer *dc = (GstDiscoverer *) object;
393 g_value_set_uint64 (value, dc->priv->timeout);
397 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
403 gst_discoverer_set_timeout (GstDiscoverer * dc, GstClockTime timeout)
405 GST_DEBUG_OBJECT (dc, "timeout : %" GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
407 /* FIXME : update current pending timeout if we're running */
409 dc->priv->timeout = timeout;
413 static GstProbeReturn
414 _event_probe (GstPad * pad, GstProbeType type, GstEvent * event,
417 if (GST_EVENT_TYPE (event) == GST_EVENT_TAG) {
418 GstTagList *tl = NULL, *tmp;
420 gst_event_parse_tag (event, &tl);
421 GST_DEBUG_OBJECT (pad, "tags %" GST_PTR_FORMAT, tl);
423 /* If preroll is complete, drop these tags - the collected information is
424 * possibly already being processed and adding more tags would be racy */
425 if (G_LIKELY (ps->dc->priv->processing)) {
426 GST_DEBUG_OBJECT (pad, "private stream %p old tags %" GST_PTR_FORMAT, ps,
428 tmp = gst_tag_list_merge (ps->tags, tl, GST_TAG_MERGE_APPEND);
430 gst_tag_list_free (ps->tags);
432 GST_DEBUG_OBJECT (pad, "private stream %p new tags %" GST_PTR_FORMAT, ps,
435 GST_DEBUG_OBJECT (pad, "Dropping tags since preroll is done");
436 DISCO_UNLOCK (ps->dc);
443 is_subtitle_caps (const GstCaps * caps)
445 static GstCaps *subs_caps = NULL;
448 subs_caps = gst_caps_from_string ("text/plain; text/x-pango-markup; "
449 "subpicture/x-pgs; subpicture/x-dvb; application/x-subtitle-unknown; "
450 "application/x-ssa; application/x-ass; subtitle/x-kate; "
451 "application/x-kate; video/x-dvd-subpicture; ");
454 return gst_caps_can_intersect (caps, subs_caps);
458 uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
462 GstPad *sinkpad = NULL;
465 GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
467 ps = g_slice_new0 (PrivateStream);
471 ps->queue = gst_element_factory_make ("queue", NULL);
472 ps->sink = gst_element_factory_make ("fakesink", NULL);
474 if (G_UNLIKELY (ps->queue == NULL || ps->sink == NULL))
477 g_object_set (ps->sink, "silent", TRUE, NULL);
478 g_object_set (ps->queue, "max-size-buffers", 1, "silent", TRUE, NULL);
480 caps = gst_pad_get_caps (pad, NULL);
482 if (is_subtitle_caps (caps)) {
483 /* Subtitle streams are sparse and may not provide any information - don't
484 * wait for data to preroll */
485 g_object_set (ps->sink, "async", FALSE, NULL);
488 gst_caps_unref (caps);
490 gst_bin_add_many (dc->priv->pipeline, ps->queue, ps->sink, NULL);
492 if (!gst_element_link_pads_full (ps->queue, "src", ps->sink, "sink",
493 GST_PAD_LINK_CHECK_NOTHING))
495 if (!gst_element_sync_state_with_parent (ps->sink))
497 if (!gst_element_sync_state_with_parent (ps->queue))
500 sinkpad = gst_element_get_static_pad (ps->queue, "sink");
503 if (gst_pad_link_full (pad, sinkpad,
504 GST_PAD_LINK_CHECK_NOTHING) != GST_PAD_LINK_OK)
506 gst_object_unref (sinkpad);
508 /* Add an event probe */
509 gst_pad_add_probe (pad, GST_PROBE_TYPE_EVENT,
510 (GstPadProbeCallback) _event_probe, ps, NULL);
513 dc->priv->streams = g_list_append (dc->priv->streams, ps);
516 GST_DEBUG_OBJECT (dc, "Done handling pad");
521 GST_ERROR_OBJECT (dc, "Error while handling pad");
523 gst_object_unref (sinkpad);
525 gst_object_unref (ps->queue);
527 gst_object_unref (ps->sink);
528 g_slice_free (PrivateStream, ps);
533 uridecodebin_pad_removed_cb (GstElement * uridecodebin, GstPad * pad,
540 GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
542 /* Find the PrivateStream */
544 for (tmp = dc->priv->streams; tmp; tmp = tmp->next) {
545 ps = (PrivateStream *) tmp->data;
552 GST_DEBUG ("The removed pad wasn't controlled by us !");
556 dc->priv->streams = g_list_delete_link (dc->priv->streams, tmp);
559 gst_element_set_state (ps->sink, GST_STATE_NULL);
560 gst_element_set_state (ps->queue, GST_STATE_NULL);
561 gst_element_unlink (ps->queue, ps->sink);
563 sinkpad = gst_element_get_static_pad (ps->queue, "sink");
564 gst_pad_unlink (pad, sinkpad);
565 gst_object_unref (sinkpad);
567 /* references removed here */
568 gst_bin_remove_many (dc->priv->pipeline, ps->sink, ps->queue, NULL);
571 gst_tag_list_free (ps->tags);
574 g_slice_free (PrivateStream, ps);
576 GST_DEBUG ("Done handling pad");
579 static GstStructure *
580 collect_stream_information (GstDiscoverer * dc, PrivateStream * ps, guint idx)
586 stname = g_strdup_printf ("stream-%02d", idx);
587 st = gst_structure_empty_new (stname);
591 caps = gst_pad_get_current_caps (ps->pad);
593 GST_WARNING ("Couldn't get negotiated caps from %s:%s",
594 GST_DEBUG_PAD_NAME (ps->pad));
595 caps = gst_pad_get_caps (ps->pad, NULL);
598 GST_DEBUG ("Got caps %" GST_PTR_FORMAT, caps);
599 gst_structure_id_set (st, _CAPS_QUARK, GST_TYPE_CAPS, caps, NULL);
601 gst_caps_unref (caps);
604 gst_structure_id_set (st, _TAGS_QUARK, GST_TYPE_STRUCTURE, ps->tags, NULL);
609 /* Parses a set of caps and tags in st and populates a GstDiscovererStreamInfo
610 * structure (parent, if !NULL, otherwise it allocates one)
612 static GstDiscovererStreamInfo *
613 collect_information (GstDiscoverer * dc, const GstStructure * st,
614 GstDiscovererStreamInfo * parent)
617 GstStructure *caps_st, *tags_st;
622 if (!st || !gst_structure_id_has_field (st, _CAPS_QUARK)) {
623 GST_WARNING ("Couldn't find caps !");
627 return (GstDiscovererStreamInfo *)
628 g_object_new (GST_TYPE_DISCOVERER_STREAM_INFO, NULL);
631 gst_structure_id_get (st, _CAPS_QUARK, GST_TYPE_CAPS, &caps, NULL);
632 caps_st = gst_caps_get_structure (caps, 0);
633 name = gst_structure_get_name (caps_st);
635 if (g_str_has_prefix (name, "audio/")) {
636 GstDiscovererAudioInfo *info;
639 info = (GstDiscovererAudioInfo *) parent;
641 info = (GstDiscovererAudioInfo *)
642 g_object_new (GST_TYPE_DISCOVERER_AUDIO_INFO, NULL);
643 info->parent.caps = caps;
646 if (gst_structure_get_int (caps_st, "rate", &tmp))
647 info->sample_rate = (guint) tmp;
649 if (gst_structure_get_int (caps_st, "channels", &tmp))
650 info->channels = (guint) tmp;
652 if (gst_structure_get_int (caps_st, "depth", &tmp))
653 info->depth = (guint) tmp;
655 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
656 gst_structure_id_get (st, _TAGS_QUARK,
657 GST_TYPE_STRUCTURE, &tags_st, NULL);
658 if (gst_structure_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
659 gst_structure_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
660 info->bitrate = utmp;
662 if (gst_structure_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
663 info->max_bitrate = utmp;
665 /* FIXME: Is it worth it to remove the tags we've parsed? */
666 info->parent.tags = gst_tag_list_merge (info->parent.tags,
667 (GstTagList *) tags_st, GST_TAG_MERGE_REPLACE);
669 gst_structure_free (tags_st);
672 if (!info->language && ((GstDiscovererStreamInfo *) info)->tags) {
674 if (gst_tag_list_get_string (((GstDiscovererStreamInfo *) info)->tags,
675 GST_TAG_LANGUAGE_CODE, &language)) {
676 info->language = language;
680 return (GstDiscovererStreamInfo *) info;
682 } else if (g_str_has_prefix (name, "video/") ||
683 g_str_has_prefix (name, "image/")) {
684 GstDiscovererVideoInfo *info;
688 info = (GstDiscovererVideoInfo *) parent;
690 info = (GstDiscovererVideoInfo *)
691 g_object_new (GST_TYPE_DISCOVERER_VIDEO_INFO, NULL);
692 info->parent.caps = caps;
695 if (gst_video_info_from_caps (&vinfo, caps)) {
696 info->width = (guint) vinfo.width;
697 info->height = (guint) vinfo.height;
699 info->depth = (guint) 0;
701 info->par_num = vinfo.par_n;
702 info->par_denom = vinfo.par_d;
704 info->framerate_num = vinfo.fps_n;
705 info->framerate_denom = vinfo.fps_d;
707 info->interlaced = (vinfo.flags & GST_VIDEO_FLAG_INTERLACED) != 0;
710 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
711 gst_structure_id_get (st, _TAGS_QUARK,
712 GST_TYPE_STRUCTURE, &tags_st, NULL);
713 if (gst_structure_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
714 gst_structure_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
715 info->bitrate = utmp;
717 if (gst_structure_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
718 info->max_bitrate = utmp;
720 /* FIXME: Is it worth it to remove the tags we've parsed? */
721 info->parent.tags = gst_tag_list_merge (info->parent.tags,
722 (GstTagList *) tags_st, GST_TAG_MERGE_REPLACE);
723 gst_structure_free (tags_st);
726 return (GstDiscovererStreamInfo *) info;
728 } else if (is_subtitle_caps (caps)) {
729 GstDiscovererSubtitleInfo *info;
732 info = (GstDiscovererSubtitleInfo *) parent;
734 info = (GstDiscovererSubtitleInfo *)
735 g_object_new (GST_TYPE_DISCOVERER_SUBTITLE_INFO, NULL);
736 info->parent.caps = caps;
739 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
740 const gchar *language;
742 gst_structure_id_get (st, _TAGS_QUARK,
743 GST_TYPE_STRUCTURE, &tags_st, NULL);
745 language = gst_structure_get_string (caps_st, GST_TAG_LANGUAGE_CODE);
747 info->language = g_strdup (language);
749 /* FIXME: Is it worth it to remove the tags we've parsed? */
750 info->parent.tags = gst_tag_list_merge (info->parent.tags,
751 (GstTagList *) tags_st, GST_TAG_MERGE_REPLACE);
752 gst_structure_free (tags_st);
756 if (!info->language && ((GstDiscovererStreamInfo *) info)->tags) {
758 if (gst_tag_list_get_string (((GstDiscovererStreamInfo *) info)->tags,
759 GST_TAG_LANGUAGE_CODE, &language)) {
760 info->language = language;
764 return (GstDiscovererStreamInfo *) info;
767 /* None of the above - populate what information we can */
768 GstDiscovererStreamInfo *info;
773 info = (GstDiscovererStreamInfo *)
774 g_object_new (GST_TYPE_DISCOVERER_STREAM_INFO, NULL);
778 if (gst_structure_id_get (st, _TAGS_QUARK,
779 GST_TYPE_STRUCTURE, &tags_st, NULL)) {
780 info->tags = gst_tag_list_merge (info->tags, (GstTagList *) tags_st,
781 GST_TAG_MERGE_REPLACE);
782 gst_structure_free (tags_st);
790 static GstStructure *
791 find_stream_for_node (GstDiscoverer * dc, const GstStructure * topology)
794 GstPad *target_pad = NULL;
795 GstStructure *st = NULL;
800 if (!gst_structure_id_has_field (topology, _TOPOLOGY_PAD_QUARK)) {
801 GST_DEBUG ("Could not find pad for node %" GST_PTR_FORMAT "\n", topology);
805 gst_structure_id_get (topology, _TOPOLOGY_PAD_QUARK,
806 GST_TYPE_PAD, &pad, NULL);
808 if (!dc->priv->streams)
811 for (i = 0, tmp = dc->priv->streams; tmp; tmp = tmp->next, i++) {
812 ps = (PrivateStream *) tmp->data;
814 target_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (ps->pad));
815 gst_object_unref (target_pad);
817 if (target_pad == pad)
822 st = collect_stream_information (dc, ps, i);
824 gst_object_unref (pad);
830 child_is_raw_stream (GstCaps * parent, GstCaps * child)
832 GstStructure *st1, *st2;
833 const gchar *name1, *name2;
835 st1 = gst_caps_get_structure (parent, 0);
836 name1 = gst_structure_get_name (st1);
837 st2 = gst_caps_get_structure (child, 0);
838 name2 = gst_structure_get_name (st2);
840 if ((g_str_has_prefix (name1, "audio/") &&
841 g_str_has_prefix (name2, "audio/x-raw")) ||
842 ((g_str_has_prefix (name1, "video/") ||
843 g_str_has_prefix (name1, "image/")) &&
844 g_str_has_prefix (name2, "video/x-raw"))) {
845 /* child is the "raw" sub-stream corresponding to parent */
849 if (is_subtitle_caps (parent))
855 /* If a parent is non-NULL, collected stream information will be appended to it
856 * (and where the information exists, it will be overriden)
858 static GstDiscovererStreamInfo *
859 parse_stream_topology (GstDiscoverer * dc, const GstStructure * topology,
860 GstDiscovererStreamInfo * parent)
862 GstDiscovererStreamInfo *res = NULL;
863 GstCaps *caps = NULL;
864 const GValue *nval = NULL;
866 GST_DEBUG ("parsing: %" GST_PTR_FORMAT, topology);
868 nval = gst_structure_get_value (topology, "next");
870 if (nval == NULL || GST_VALUE_HOLDS_STRUCTURE (nval)) {
871 GstStructure *st = find_stream_for_node (dc, topology);
872 gboolean add_to_list = TRUE;
875 res = collect_information (dc, st, parent);
876 gst_structure_free (st);
878 /* Didn't find a stream structure, so let's just use the caps we have */
879 res = collect_information (dc, topology, parent);
883 /* FIXME : aggregate with information from main streams */
884 GST_DEBUG ("Coudn't find 'next' ! might be the last entry");
887 const GstStructure *st;
889 st = gst_value_get_structure (nval);
891 GST_DEBUG ("next is a structure %" GST_PTR_FORMAT, st);
896 if (gst_structure_id_get (st, _CAPS_QUARK, GST_TYPE_CAPS, &caps, NULL)) {
897 if (gst_caps_can_intersect (parent->caps, caps)) {
898 /* We sometimes get an extra sub-stream from the parser. If this is
899 * the case, we just replace the parent caps with this stream's caps
900 * since they might contain more information */
901 gst_caps_unref (parent->caps);
904 parse_stream_topology (dc, st, parent);
907 } else if (child_is_raw_stream (parent->caps, caps)) {
908 /* This is the "raw" stream corresponding to the parent. This
909 * contains more information than the parent, tags etc. */
910 parse_stream_topology (dc, st, parent);
912 gst_caps_unref (caps);
915 GstDiscovererStreamInfo *next = parse_stream_topology (dc, st, NULL);
917 next->previous = res;
923 dc->priv->current_info->stream_list =
924 g_list_append (dc->priv->current_info->stream_list, res);
927 } else if (GST_VALUE_HOLDS_LIST (nval)) {
929 GstDiscovererContainerInfo *cont;
932 if (!gst_structure_id_get (topology, _CAPS_QUARK,
933 GST_TYPE_CAPS, &caps, NULL))
934 GST_WARNING ("Couldn't find caps !");
936 len = gst_value_list_get_size (nval);
937 GST_DEBUG ("next is a list of %d entries", len);
939 cont = (GstDiscovererContainerInfo *)
940 g_object_new (GST_TYPE_DISCOVERER_CONTAINER_INFO, NULL);
941 cont->parent.caps = caps;
942 res = (GstDiscovererStreamInfo *) cont;
944 if (gst_structure_id_has_field (topology, _TAGS_QUARK)) {
947 gst_structure_id_get (topology, _TAGS_QUARK,
948 GST_TYPE_STRUCTURE, &tags, NULL);
950 GST_DEBUG ("Merge tags %" GST_PTR_FORMAT, tags);
953 gst_tag_list_merge (cont->parent.tags, (GstTagList *) tags,
954 GST_TAG_MERGE_APPEND);
955 gst_tag_list_free (tags);
956 if (cont->parent.tags)
957 gst_tag_list_free (cont->parent.tags);
958 cont->parent.tags = tmp;
959 GST_DEBUG ("Container info tags %" GST_PTR_FORMAT, tmp);
962 for (i = 0; i < len; i++) {
963 const GValue *subv = gst_value_list_get_value (nval, i);
964 const GstStructure *subst = gst_value_get_structure (subv);
965 GstDiscovererStreamInfo *substream;
967 GST_DEBUG ("%d %" GST_PTR_FORMAT, i, subst);
969 substream = parse_stream_topology (dc, subst, NULL);
971 substream->previous = res;
973 g_list_append (cont->streams,
974 gst_discoverer_stream_info_ref (substream));
981 /* Called when pipeline is pre-rolled */
983 discoverer_collect (GstDiscoverer * dc)
985 GST_DEBUG ("Collecting information");
987 /* Stop the timeout handler if present */
988 if (dc->priv->timeoutid) {
989 g_source_remove (dc->priv->timeoutid);
990 dc->priv->timeoutid = 0;
993 if (dc->priv->streams) {
994 /* FIXME : Make this querying optional */
996 GstElement *pipeline = (GstElement *) dc->priv->pipeline;
999 GST_DEBUG ("Attempting to query duration");
1001 if (gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur)) {
1002 GST_DEBUG ("Got duration %" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
1003 dc->priv->current_info->duration = (guint64) dur;
1006 if (dc->priv->seeking_query) {
1007 if (gst_element_query (pipeline, dc->priv->seeking_query)) {
1011 gst_query_parse_seeking (dc->priv->seeking_query, &format,
1012 &seekable, NULL, NULL);
1013 if (format == GST_FORMAT_TIME) {
1014 GST_DEBUG ("Got seekable %d", seekable);
1015 dc->priv->current_info->seekable = seekable;
1021 if (dc->priv->current_topology)
1022 dc->priv->current_info->stream_info = parse_stream_topology (dc,
1023 dc->priv->current_topology, NULL);
1026 * Images need some special handling. They do not have a duration, have
1027 * caps named image/<foo> (th exception being MJPEG video which is also
1028 * type image/jpeg), and should consist of precisely one stream (actually
1029 * initially there are 2, the image and raw stream, but we squash these
1030 * while parsing the stream topology). At some point, if we find that these
1031 * conditions are not sufficient, we can count the number of decoders and
1032 * parsers in the chain, and if there's more than one decoder, or any
1033 * parser at all, we should not mark this as an image.
1035 if (dc->priv->current_info->duration == 0 &&
1036 dc->priv->current_info->stream_info != NULL &&
1037 dc->priv->current_info->stream_info->next == NULL) {
1039 gst_caps_get_structure (dc->priv->current_info->stream_info->caps, 0);
1041 if (g_str_has_prefix (gst_structure_get_name (st), "image/"))
1042 ((GstDiscovererVideoInfo *) dc->priv->current_info->stream_info)->
1047 if (dc->priv->async) {
1048 GST_DEBUG ("Emitting 'discoverered'");
1049 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_DISCOVERED], 0,
1050 dc->priv->current_info, dc->priv->current_error);
1051 /* Clients get a copy of current_info since it is a boxed type */
1052 gst_discoverer_info_unref (dc->priv->current_info);
1057 get_async_cb (gpointer cb_data, GSource * source, GSourceFunc * func,
1060 *func = (GSourceFunc) async_timeout_cb;
1064 /* Wrapper since GSourceCallbackFuncs don't expect a return value from ref() */
1066 _void_g_object_ref (gpointer object)
1068 g_object_ref (G_OBJECT (object));
1072 handle_current_async (GstDiscoverer * dc)
1075 static GSourceCallbackFuncs cb_funcs = {
1081 /* Attach a timeout to the main context */
1082 source = g_timeout_source_new (dc->priv->timeout / GST_MSECOND);
1083 g_source_set_callback_indirect (source, g_object_ref (dc), &cb_funcs);
1084 dc->priv->timeoutid = g_source_attach (source, dc->priv->ctx);
1085 g_source_unref (source);
1089 /* Returns TRUE if processing should stop */
1091 handle_message (GstDiscoverer * dc, GstMessage * msg)
1093 gboolean done = FALSE;
1095 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "got a %s message",
1096 GST_MESSAGE_TYPE_NAME (msg));
1098 switch (GST_MESSAGE_TYPE (msg)) {
1099 case GST_MESSAGE_ERROR:{
1103 gst_message_parse_error (msg, &gerr, &debug);
1104 GST_WARNING_OBJECT (GST_MESSAGE_SRC (msg),
1105 "Got an error [debug:%s], [message:%s]", debug, gerr->message);
1106 dc->priv->current_error = gerr;
1109 /* We need to stop */
1112 GST_DEBUG ("Setting result to ERROR");
1113 dc->priv->current_info->result = GST_DISCOVERER_ERROR;
1117 case GST_MESSAGE_EOS:
1118 GST_DEBUG ("Got EOS !");
1122 case GST_MESSAGE_ASYNC_DONE:
1123 if (GST_MESSAGE_SRC (msg) == (GstObject *) dc->priv->pipeline) {
1124 GST_DEBUG ("Finished changing state asynchronously");
1130 case GST_MESSAGE_ELEMENT:
1133 const GstStructure *structure;
1135 structure = gst_message_get_structure (msg);
1136 sttype = gst_structure_get_name_id (structure);
1137 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1138 "structure %" GST_PTR_FORMAT, structure);
1139 if (sttype == _MISSING_PLUGIN_QUARK) {
1140 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1141 "Setting result to MISSING_PLUGINS");
1142 dc->priv->current_info->result = GST_DISCOVERER_MISSING_PLUGINS;
1143 dc->priv->current_info->misc = gst_structure_copy (structure);
1144 } else if (sttype == _STREAM_TOPOLOGY_QUARK) {
1145 dc->priv->current_topology = gst_structure_copy (structure);
1150 case GST_MESSAGE_TAG:
1152 GstTagList *tl, *tmp;
1154 gst_message_parse_tag (msg, &tl);
1155 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Got tags %" GST_PTR_FORMAT, tl);
1156 /* Merge with current tags */
1158 gst_tag_list_merge (dc->priv->current_info->tags, tl,
1159 GST_TAG_MERGE_APPEND);
1160 gst_tag_list_free (tl);
1161 if (dc->priv->current_info->tags)
1162 gst_tag_list_free (dc->priv->current_info->tags);
1163 dc->priv->current_info->tags = tmp;
1164 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Current info %p, tags %"
1165 GST_PTR_FORMAT, dc->priv->current_info, tmp);
1178 handle_current_sync (GstDiscoverer * dc)
1181 gdouble deadline = ((gdouble) dc->priv->timeout) / GST_SECOND;
1183 gboolean done = FALSE;
1185 timer = g_timer_new ();
1186 g_timer_start (timer);
1189 /* poll bus with timeout */
1190 /* FIXME : make the timeout more fine-tuned */
1191 if ((msg = gst_bus_timed_pop (dc->priv->bus, GST_SECOND / 2))) {
1192 done = handle_message (dc, msg);
1193 gst_message_unref (msg);
1196 } while (!done && (g_timer_elapsed (timer, NULL) < deadline));
1200 GST_DEBUG ("we timed out! Setting result to TIMEOUT");
1201 dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
1206 g_timer_stop (timer);
1207 g_timer_destroy (timer);
1211 _setup_locked (GstDiscoverer * dc)
1213 GstStateChangeReturn ret;
1215 GST_DEBUG ("Setting up");
1217 /* Pop URI off the pending URI list */
1218 dc->priv->current_info =
1219 (GstDiscovererInfo *) g_object_new (GST_TYPE_DISCOVERER_INFO, NULL);
1220 dc->priv->current_info->uri = (gchar *) dc->priv->pending_uris->data;
1221 dc->priv->pending_uris =
1222 g_list_delete_link (dc->priv->pending_uris, dc->priv->pending_uris);
1224 /* set uri on uridecodebin */
1225 g_object_set (dc->priv->uridecodebin, "uri", dc->priv->current_info->uri,
1228 GST_DEBUG ("Current is now %s", dc->priv->current_info->uri);
1230 dc->priv->processing = TRUE;
1232 /* set pipeline to PAUSED */
1234 GST_DEBUG ("Setting pipeline to PAUSED");
1236 gst_element_set_state ((GstElement *) dc->priv->pipeline,
1240 GST_DEBUG_OBJECT (dc, "Pipeline going to PAUSED : %s",
1241 gst_element_state_change_return_get_name (ret));
1245 discoverer_cleanup (GstDiscoverer * dc)
1247 GST_DEBUG ("Cleaning up");
1249 gst_bus_set_flushing (dc->priv->bus, TRUE);
1250 gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_READY);
1251 gst_bus_set_flushing (dc->priv->bus, FALSE);
1254 if (dc->priv->current_error)
1255 g_error_free (dc->priv->current_error);
1256 dc->priv->current_error = NULL;
1257 if (dc->priv->current_topology) {
1258 gst_structure_free (dc->priv->current_topology);
1259 dc->priv->current_topology = NULL;
1262 dc->priv->current_info = NULL;
1264 /* Try popping the next uri */
1265 if (dc->priv->async) {
1266 if (dc->priv->pending_uris != NULL) {
1270 handle_current_async (dc);
1274 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_FINISHED], 0);
1283 discoverer_bus_cb (GstBus * bus, GstMessage * msg, GstDiscoverer * dc)
1285 if (dc->priv->processing) {
1286 if (handle_message (dc, msg)) {
1287 GST_DEBUG ("Stopping asynchronously");
1288 /* Serialise with _event_probe() */
1290 dc->priv->processing = FALSE;
1292 discoverer_collect (dc);
1293 discoverer_cleanup (dc);
1299 async_timeout_cb (GstDiscoverer * dc)
1301 if (!g_source_is_destroyed (g_main_current_source ())) {
1302 dc->priv->timeoutid = 0;
1303 GST_DEBUG ("Setting result to TIMEOUT");
1304 dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
1305 dc->priv->processing = FALSE;
1306 discoverer_collect (dc);
1307 discoverer_cleanup (dc);
1312 /* If there is a pending URI, it will pop it from the list of pending
1313 * URIs and start the discovery on it.
1315 * Returns GST_DISCOVERER_OK if the next URI was popped and is processing,
1316 * else a error flag.
1318 static GstDiscovererResult
1319 start_discovering (GstDiscoverer * dc)
1321 GstDiscovererResult res = GST_DISCOVERER_OK;
1323 GST_DEBUG ("Starting");
1326 if (dc->priv->pending_uris == NULL) {
1327 GST_WARNING ("No URI to process");
1328 res = GST_DISCOVERER_URI_INVALID;
1333 if (dc->priv->current_info != NULL) {
1334 GST_WARNING ("Already processing a file");
1335 res = GST_DISCOVERER_BUSY;
1340 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_STARTING], 0);
1346 if (dc->priv->async)
1347 handle_current_async (dc);
1349 handle_current_sync (dc);
1357 * gst_discoverer_start:
1358 * @discoverer: A #GstDiscoverer
1360 * Allow asynchronous discovering of URIs to take place.
1361 * A #GMainLoop must be available for #GstDiscoverer to properly work in
1362 * asynchronous mode.
1367 gst_discoverer_start (GstDiscoverer * discoverer)
1370 GMainContext *ctx = NULL;
1372 GST_DEBUG_OBJECT (discoverer, "Starting...");
1374 if (discoverer->priv->async) {
1375 GST_DEBUG_OBJECT (discoverer, "We were already started");
1379 discoverer->priv->async = TRUE;
1380 discoverer->priv->running = TRUE;
1382 ctx = g_main_context_get_thread_default ();
1384 /* Connect to bus signals */
1386 ctx = g_main_context_default ();
1388 source = gst_bus_create_watch (discoverer->priv->bus);
1389 g_source_set_callback (source, (GSourceFunc) gst_bus_async_signal_func,
1391 discoverer->priv->sourceid = g_source_attach (source, ctx);
1392 g_source_unref (source);
1393 discoverer->priv->ctx = g_main_context_ref (ctx);
1395 start_discovering (discoverer);
1396 GST_DEBUG_OBJECT (discoverer, "Started");
1400 * gst_discoverer_stop:
1401 * @discoverer: A #GstDiscoverer
1403 * Stop the discovery of any pending URIs and clears the list of
1404 * pending URIS (if any).
1409 gst_discoverer_stop (GstDiscoverer * discoverer)
1411 GST_DEBUG_OBJECT (discoverer, "Stopping...");
1413 if (!discoverer->priv->async) {
1414 GST_DEBUG_OBJECT (discoverer,
1415 "We were already stopped, or running synchronously");
1419 DISCO_LOCK (discoverer);
1420 if (discoverer->priv->processing) {
1421 /* We prevent any further processing by setting the bus to
1422 * flushing and setting the pipeline to READY.
1423 * _reset() will take care of the rest of the cleanup */
1424 if (discoverer->priv->bus)
1425 gst_bus_set_flushing (discoverer->priv->bus, TRUE);
1426 if (discoverer->priv->pipeline)
1427 gst_element_set_state ((GstElement *) discoverer->priv->pipeline,
1430 discoverer->priv->running = FALSE;
1431 DISCO_UNLOCK (discoverer);
1433 /* Remove timeout handler */
1434 if (discoverer->priv->timeoutid) {
1435 g_source_remove (discoverer->priv->timeoutid);
1436 discoverer->priv->timeoutid = 0;
1438 /* Remove signal watch */
1439 if (discoverer->priv->sourceid) {
1440 g_source_remove (discoverer->priv->sourceid);
1441 discoverer->priv->sourceid = 0;
1443 /* Unref main context */
1444 if (discoverer->priv->ctx) {
1445 g_main_context_unref (discoverer->priv->ctx);
1446 discoverer->priv->ctx = NULL;
1448 discoverer_reset (discoverer);
1450 discoverer->priv->async = FALSE;
1452 GST_DEBUG_OBJECT (discoverer, "Stopped");
1456 * gst_discoverer_discover_uri_async:
1457 * @discoverer: A #GstDiscoverer
1458 * @uri: the URI to add.
1460 * Appends the given @uri to the list of URIs to discoverer. The actual
1461 * discovery of the @uri will only take place if gst_discoverer_start() has
1464 * A copy of @uri will be made internally, so the caller can safely g_free()
1467 * Returns: %TRUE if the @uri was succesfully appended to the list of pending
1473 gst_discoverer_discover_uri_async (GstDiscoverer * discoverer,
1478 GST_DEBUG_OBJECT (discoverer, "uri : %s", uri);
1480 DISCO_LOCK (discoverer);
1481 can_run = (discoverer->priv->pending_uris == NULL);
1482 discoverer->priv->pending_uris =
1483 g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
1484 DISCO_UNLOCK (discoverer);
1487 start_discovering (discoverer);
1493 /* Synchronous mode */
1495 * gst_discoverer_discover_uri:
1496 * @discoverer: A #GstDiscoverer
1497 * @uri: The URI to run on.
1498 * @err: (out) (allow-none): If an error occurred, this field will be filled in.
1500 * Synchronously discovers the given @uri.
1502 * A copy of @uri will be made internally, so the caller can safely g_free()
1505 * Returns: (transfer full): the result of the scanning. Can be %NULL if an
1511 gst_discoverer_discover_uri (GstDiscoverer * discoverer, const gchar * uri,
1514 GstDiscovererResult res = 0;
1515 GstDiscovererInfo *info;
1517 GST_DEBUG_OBJECT (discoverer, "uri:%s", uri);
1519 DISCO_LOCK (discoverer);
1520 if (G_UNLIKELY (discoverer->priv->current_info)) {
1521 DISCO_UNLOCK (discoverer);
1522 GST_WARNING_OBJECT (discoverer, "Already handling a uri");
1526 discoverer->priv->pending_uris =
1527 g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
1528 DISCO_UNLOCK (discoverer);
1530 res = start_discovering (discoverer);
1531 discoverer_collect (discoverer);
1535 if (discoverer->priv->current_error)
1536 *err = g_error_copy (discoverer->priv->current_error);
1540 if (res != GST_DISCOVERER_OK) {
1541 GST_DEBUG ("Setting result to %d (was %d)", res,
1542 discoverer->priv->current_info->result);
1543 discoverer->priv->current_info->result = res;
1545 info = discoverer->priv->current_info;
1547 discoverer_cleanup (discoverer);
1553 * gst_discoverer_new:
1554 * @timeout: timeout per file, in nanoseconds. Allowed are values between
1555 * one second (#GST_SECOND) and one hour (3600 * #GST_SECOND)
1556 * @err: a pointer to a #GError. can be %NULL
1558 * Creates a new #GstDiscoverer with the provided timeout.
1560 * Returns: (transfer full): The new #GstDiscoverer.
1561 * If an error occurred when creating the discoverer, @err will be set
1562 * accordingly and %NULL will be returned. If @err is set, the caller must
1563 * free it when no longer needed using g_error_free().
1568 gst_discoverer_new (GstClockTime timeout, GError ** err)
1572 res = g_object_new (GST_TYPE_DISCOVERER, "timeout", timeout, NULL);
1573 if (res->priv->uridecodebin == NULL) {
1575 *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
1576 "Couldn't create 'uridecodebin' element");
1577 gst_object_unref (res);