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>
49 #include "pbutils-marshal.h"
50 #include "pbutils-private.h"
52 GST_DEBUG_CATEGORY_STATIC (discoverer_debug);
53 #define GST_CAT_DEFAULT discoverer_debug
55 static GQuark _CAPS_QUARK;
56 static GQuark _TAGS_QUARK;
57 static GQuark _MISSING_PLUGIN_QUARK;
58 static GQuark _STREAM_TOPOLOGY_QUARK;
59 static GQuark _TOPOLOGY_PAD_QUARK;
71 struct _GstDiscovererPrivate
75 /* allowed time to discover each uri in nanoseconds */
78 /* list of pending URI to process (current excluded) */
83 /* TRUE if processing a URI */
86 /* TRUE if discoverer has been started */
90 GstDiscovererInfo *current_info;
91 GError *current_error;
92 GstStructure *current_topology;
94 /* List of private streams */
99 GstElement *uridecodebin;
102 GType decodebin2_type;
104 /* Custom main context variables */
109 /* reusable queries */
110 GstQuery *seeking_query;
112 /* Handler ids for various callbacks */
114 gulong pad_remove_id;
115 gulong element_added_id;
119 #define DISCO_LOCK(dc) g_mutex_lock (dc->priv->lock);
120 #define DISCO_UNLOCK(dc) g_mutex_unlock (dc->priv->lock);
125 GST_DEBUG_CATEGORY_INIT (discoverer_debug, "discoverer", 0, "Discoverer");
127 _CAPS_QUARK = g_quark_from_static_string ("caps");
128 _TAGS_QUARK = g_quark_from_static_string ("tags");
129 _MISSING_PLUGIN_QUARK = g_quark_from_static_string ("missing-plugin");
130 _STREAM_TOPOLOGY_QUARK = g_quark_from_static_string ("stream-topology");
131 _TOPOLOGY_PAD_QUARK = g_quark_from_static_string ("pad");
134 G_DEFINE_TYPE_EXTENDED (GstDiscoverer, gst_discoverer, G_TYPE_OBJECT, 0,
145 #define DEFAULT_PROP_TIMEOUT 15 * GST_SECOND
153 static guint gst_discoverer_signals[LAST_SIGNAL] = { 0 };
155 static void gst_discoverer_set_timeout (GstDiscoverer * dc,
156 GstClockTime timeout);
157 static gboolean async_timeout_cb (GstDiscoverer * dc);
159 static void discoverer_bus_cb (GstBus * bus, GstMessage * msg,
161 static void uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
163 static void uridecodebin_pad_removed_cb (GstElement * uridecodebin,
164 GstPad * pad, GstDiscoverer * dc);
166 static void gst_discoverer_dispose (GObject * dc);
167 static void gst_discoverer_set_property (GObject * object, guint prop_id,
168 const GValue * value, GParamSpec * pspec);
169 static void gst_discoverer_get_property (GObject * object, guint prop_id,
170 GValue * value, GParamSpec * pspec);
173 gst_discoverer_class_init (GstDiscovererClass * klass)
175 GObjectClass *gobject_class = (GObjectClass *) klass;
177 gobject_class->dispose = gst_discoverer_dispose;
179 gobject_class->set_property = gst_discoverer_set_property;
180 gobject_class->get_property = gst_discoverer_get_property;
182 g_type_class_add_private (klass, sizeof (GstDiscovererPrivate));
186 * GstDiscoverer:timeout
188 * The duration (in nanoseconds) after which the discovery of an individual
191 * If the discovery of a URI times out, the %GST_DISCOVERER_TIMEOUT will be
192 * set on the result flags.
194 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
195 g_param_spec_uint64 ("timeout", "timeout", "Timeout",
196 GST_SECOND, 3600 * GST_SECOND, DEFAULT_PROP_TIMEOUT,
197 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
201 * GstDiscoverer::finished:
202 * @discoverer: the #GstDiscoverer
204 * Will be emitted when all pending URIs have been processed.
206 gst_discoverer_signals[SIGNAL_FINISHED] =
207 g_signal_new ("finished", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
208 G_STRUCT_OFFSET (GstDiscovererClass, finished),
209 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
212 * GstDiscoverer::starting:
213 * @discoverer: the #GstDiscoverer
215 * Will be emitted when the discover starts analyzing the pending URIs
217 gst_discoverer_signals[SIGNAL_STARTING] =
218 g_signal_new ("starting", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
219 G_STRUCT_OFFSET (GstDiscovererClass, starting),
220 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
223 * GstDiscoverer::discovered:
224 * @discoverer: the #GstDiscoverer
225 * @info: the results #GstDiscovererInfo
226 * @error: (type GLib.Error): #GError, which will be non-NULL if an error
227 * occurred during discovery
229 * Will be emitted when all information on a URI could be discovered.
231 gst_discoverer_signals[SIGNAL_DISCOVERED] =
232 g_signal_new ("discovered", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
233 G_STRUCT_OFFSET (GstDiscovererClass, discovered),
234 NULL, NULL, pbutils_marshal_VOID__POINTER_BOXED,
235 G_TYPE_NONE, 2, GST_TYPE_DISCOVERER_INFO, GST_TYPE_G_ERROR);
239 uridecodebin_element_added_cb (GstElement * uridecodebin,
240 GstElement * child, GstDiscoverer * dc)
242 GST_DEBUG ("New element added to uridecodebin : %s",
243 GST_ELEMENT_NAME (child));
245 if (G_OBJECT_TYPE (child) == dc->priv->decodebin2_type) {
246 g_object_set (child, "post-stream-topology", TRUE, NULL);
251 gst_discoverer_init (GstDiscoverer * dc)
254 GstFormat format = GST_FORMAT_TIME;
256 dc->priv = G_TYPE_INSTANCE_GET_PRIVATE (dc, GST_TYPE_DISCOVERER,
257 GstDiscovererPrivate);
259 dc->priv->timeout = DEFAULT_PROP_TIMEOUT;
260 dc->priv->async = FALSE;
262 dc->priv->lock = g_mutex_new ();
264 GST_LOG ("Creating pipeline");
265 dc->priv->pipeline = (GstBin *) gst_pipeline_new ("Discoverer");
266 GST_LOG_OBJECT (dc, "Creating uridecodebin");
267 dc->priv->uridecodebin =
268 gst_element_factory_make ("uridecodebin", "discoverer-uri");
269 if (G_UNLIKELY (dc->priv->uridecodebin == NULL)) {
270 GST_ERROR ("Can't create uridecodebin");
273 GST_LOG_OBJECT (dc, "Adding uridecodebin to pipeline");
274 gst_bin_add (dc->priv->pipeline, dc->priv->uridecodebin);
276 dc->priv->pad_added_id =
277 g_signal_connect_object (dc->priv->uridecodebin, "pad-added",
278 G_CALLBACK (uridecodebin_pad_added_cb), dc, 0);
279 dc->priv->pad_remove_id =
280 g_signal_connect_object (dc->priv->uridecodebin, "pad-removed",
281 G_CALLBACK (uridecodebin_pad_removed_cb), dc, 0);
283 GST_LOG_OBJECT (dc, "Getting pipeline bus");
284 dc->priv->bus = gst_pipeline_get_bus ((GstPipeline *) dc->priv->pipeline);
286 dc->priv->bus_cb_id =
287 g_signal_connect_object (dc->priv->bus, "message",
288 G_CALLBACK (discoverer_bus_cb), dc, 0);
290 GST_DEBUG_OBJECT (dc, "Done initializing Discoverer");
292 /* This is ugly. We get the GType of decodebin2 so we can quickly detect
293 * when a decodebin2 is added to uridecodebin so we can set the
294 * post-stream-topology setting to TRUE */
295 dc->priv->element_added_id =
296 g_signal_connect_object (dc->priv->uridecodebin, "element-added",
297 G_CALLBACK (uridecodebin_element_added_cb), dc, 0);
298 tmp = gst_element_factory_make ("decodebin2", NULL);
299 dc->priv->decodebin2_type = G_OBJECT_TYPE (tmp);
300 gst_object_unref (tmp);
303 dc->priv->seeking_query = gst_query_new_seeking (format);
307 discoverer_reset (GstDiscoverer * dc)
309 GST_DEBUG_OBJECT (dc, "Resetting");
311 if (dc->priv->pending_uris) {
312 g_list_foreach (dc->priv->pending_uris, (GFunc) g_free, NULL);
313 g_list_free (dc->priv->pending_uris);
314 dc->priv->pending_uris = NULL;
317 if (dc->priv->pipeline)
318 gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_NULL);
321 #define DISCONNECT_SIGNAL(o,i) G_STMT_START{ \
322 if ((i) && g_signal_handler_is_connected ((o), (i))) \
323 g_signal_handler_disconnect ((o), (i)); \
328 gst_discoverer_dispose (GObject * obj)
330 GstDiscoverer *dc = (GstDiscoverer *) obj;
332 GST_DEBUG_OBJECT (dc, "Disposing");
334 discoverer_reset (dc);
336 if (G_LIKELY (dc->priv->pipeline)) {
337 /* Workaround for bug #118536 */
338 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_added_id);
339 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_remove_id);
340 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->element_added_id);
341 DISCONNECT_SIGNAL (dc->priv->bus, dc->priv->bus_cb_id);
343 /* pipeline was set to NULL in _reset */
344 gst_object_unref (dc->priv->pipeline);
345 gst_object_unref (dc->priv->bus);
347 dc->priv->pipeline = NULL;
348 dc->priv->uridecodebin = NULL;
349 dc->priv->bus = NULL;
352 gst_discoverer_stop (dc);
354 if (dc->priv->lock) {
355 g_mutex_free (dc->priv->lock);
356 dc->priv->lock = NULL;
359 if (dc->priv->seeking_query) {
360 gst_query_unref (dc->priv->seeking_query);
361 dc->priv->seeking_query = NULL;
364 G_OBJECT_CLASS (gst_discoverer_parent_class)->dispose (obj);
368 gst_discoverer_set_property (GObject * object, guint prop_id,
369 const GValue * value, GParamSpec * pspec)
371 GstDiscoverer *dc = (GstDiscoverer *) object;
375 gst_discoverer_set_timeout (dc, g_value_get_uint64 (value));
378 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
384 gst_discoverer_get_property (GObject * object, guint prop_id,
385 GValue * value, GParamSpec * pspec)
387 GstDiscoverer *dc = (GstDiscoverer *) object;
392 g_value_set_uint64 (value, dc->priv->timeout);
396 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
402 gst_discoverer_set_timeout (GstDiscoverer * dc, GstClockTime timeout)
404 GST_DEBUG_OBJECT (dc, "timeout : %" GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
406 /* FIXME : update current pending timeout if we're running */
408 dc->priv->timeout = timeout;
413 _event_probe (GstPad * pad, GstEvent * event, PrivateStream * ps)
415 if (GST_EVENT_TYPE (event) == GST_EVENT_TAG) {
416 GstTagList *tl = NULL, *tmp;
418 gst_event_parse_tag (event, &tl);
419 GST_DEBUG_OBJECT (pad, "tags %" GST_PTR_FORMAT, tl);
421 /* If preroll is complete, drop these tags - the collected information is
422 * possibly already being processed and adding more tags would be racy */
423 if (G_LIKELY (ps->dc->priv->processing)) {
424 GST_DEBUG_OBJECT (pad, "private stream %p old tags %" GST_PTR_FORMAT, ps,
426 tmp = gst_tag_list_merge (ps->tags, tl, GST_TAG_MERGE_APPEND);
428 gst_tag_list_free (ps->tags);
430 GST_DEBUG_OBJECT (pad, "private stream %p new tags %" GST_PTR_FORMAT, ps,
433 GST_DEBUG_OBJECT (pad, "Dropping tags since preroll is done");
434 DISCO_UNLOCK (ps->dc);
441 uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
445 GstPad *sinkpad = NULL;
447 static GstCaps *subs_caps = NULL;
450 subs_caps = gst_caps_from_string ("text/plain; text/x-pango-markup; "
451 "subpicture/x-pgs; subpicture/x-dvb; application/x-subtitle-unknown; "
452 "application/x-ssa; application/x-ass; subtitle/x-kate; "
453 "video/x-dvd-subpicture; ");
456 GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
458 ps = g_slice_new0 (PrivateStream);
462 ps->queue = gst_element_factory_make ("queue", NULL);
463 ps->sink = gst_element_factory_make ("fakesink", NULL);
465 if (G_UNLIKELY (ps->queue == NULL || ps->sink == NULL))
468 g_object_set (ps->sink, "silent", TRUE, NULL);
469 g_object_set (ps->queue, "max-size-buffers", 1, "silent", TRUE, NULL);
471 caps = gst_pad_get_caps_reffed (pad);
473 if (gst_caps_can_intersect (caps, subs_caps)) {
474 /* Subtitle streams are sparse and don't provide any information - don't
475 * wait for data to preroll */
476 g_object_set (ps->sink, "async", FALSE, NULL);
479 gst_caps_unref (caps);
481 gst_bin_add_many (dc->priv->pipeline, ps->queue, ps->sink, NULL);
483 if (!gst_element_link_pads_full (ps->queue, "src", ps->sink, "sink",
484 GST_PAD_LINK_CHECK_NOTHING))
486 if (!gst_element_sync_state_with_parent (ps->sink))
488 if (!gst_element_sync_state_with_parent (ps->queue))
491 sinkpad = gst_element_get_static_pad (ps->queue, "sink");
494 if (gst_pad_link_full (pad, sinkpad,
495 GST_PAD_LINK_CHECK_NOTHING) != GST_PAD_LINK_OK)
497 gst_object_unref (sinkpad);
499 /* Add an event probe */
500 gst_pad_add_event_probe (pad, G_CALLBACK (_event_probe), ps);
503 dc->priv->streams = g_list_append (dc->priv->streams, ps);
506 GST_DEBUG_OBJECT (dc, "Done handling pad");
511 GST_ERROR_OBJECT (dc, "Error while handling pad");
513 gst_object_unref (sinkpad);
515 gst_object_unref (ps->queue);
517 gst_object_unref (ps->sink);
518 g_slice_free (PrivateStream, ps);
523 uridecodebin_pad_removed_cb (GstElement * uridecodebin, GstPad * pad,
530 GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
532 /* Find the PrivateStream */
534 for (tmp = dc->priv->streams; tmp; tmp = tmp->next) {
535 ps = (PrivateStream *) tmp->data;
542 GST_DEBUG ("The removed pad wasn't controlled by us !");
546 dc->priv->streams = g_list_delete_link (dc->priv->streams, tmp);
549 gst_element_set_state (ps->sink, GST_STATE_NULL);
550 gst_element_set_state (ps->queue, GST_STATE_NULL);
551 gst_element_unlink (ps->queue, ps->sink);
553 sinkpad = gst_element_get_static_pad (ps->queue, "sink");
554 gst_pad_unlink (pad, sinkpad);
555 gst_object_unref (sinkpad);
557 /* references removed here */
558 gst_bin_remove_many (dc->priv->pipeline, ps->sink, ps->queue, NULL);
561 gst_tag_list_free (ps->tags);
564 g_slice_free (PrivateStream, ps);
566 GST_DEBUG ("Done handling pad");
569 static GstStructure *
570 collect_stream_information (GstDiscoverer * dc, PrivateStream * ps, guint idx)
576 stname = g_strdup_printf ("stream-%02d", idx);
577 st = gst_structure_empty_new (stname);
581 caps = gst_pad_get_negotiated_caps (ps->pad);
583 GST_WARNING ("Couldn't get negotiated caps from %s:%s",
584 GST_DEBUG_PAD_NAME (ps->pad));
585 caps = gst_pad_get_caps (ps->pad);
588 GST_DEBUG ("Got caps %" GST_PTR_FORMAT, caps);
589 gst_structure_id_set (st, _CAPS_QUARK, GST_TYPE_CAPS, caps, NULL);
591 gst_caps_unref (caps);
594 gst_structure_id_set (st, _TAGS_QUARK, GST_TYPE_STRUCTURE, ps->tags, NULL);
599 /* Parses a set of caps and tags in st and populates a GstDiscovererStreamInfo
600 * structure (parent, if !NULL, otherwise it allocates one)
602 static GstDiscovererStreamInfo *
603 collect_information (GstDiscoverer * dc, const GstStructure * st,
604 GstDiscovererStreamInfo * parent)
607 GstStructure *caps_st, *tags_st;
613 if (!st || !gst_structure_id_has_field (st, _CAPS_QUARK)) {
614 GST_WARNING ("Couldn't find caps !");
618 return (GstDiscovererStreamInfo *)
619 gst_mini_object_new (GST_TYPE_DISCOVERER_STREAM_INFO);
622 gst_structure_id_get (st, _CAPS_QUARK, GST_TYPE_CAPS, &caps, NULL);
623 caps_st = gst_caps_get_structure (caps, 0);
624 name = gst_structure_get_name (caps_st);
626 if (g_str_has_prefix (name, "audio/")) {
627 GstDiscovererAudioInfo *info;
630 info = (GstDiscovererAudioInfo *) parent;
632 info = (GstDiscovererAudioInfo *)
633 gst_mini_object_new (GST_TYPE_DISCOVERER_AUDIO_INFO);
634 info->parent.caps = caps;
637 if (gst_structure_get_int (caps_st, "rate", &tmp))
638 info->sample_rate = (guint) tmp;
640 if (gst_structure_get_int (caps_st, "channels", &tmp))
641 info->channels = (guint) tmp;
643 if (gst_structure_get_int (caps_st, "depth", &tmp))
644 info->depth = (guint) tmp;
646 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
647 gst_structure_id_get (st, _TAGS_QUARK,
648 GST_TYPE_STRUCTURE, &tags_st, NULL);
649 if (gst_structure_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
650 gst_structure_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
651 info->bitrate = utmp;
653 if (gst_structure_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
654 info->max_bitrate = utmp;
656 /* FIXME: Is it worth it to remove the tags we've parsed? */
657 info->parent.tags = gst_tag_list_merge (info->parent.tags,
658 (GstTagList *) tags_st, GST_TAG_MERGE_REPLACE);
660 gst_structure_free (tags_st);
663 return (GstDiscovererStreamInfo *) info;
665 } else if (g_str_has_prefix (name, "video/") ||
666 g_str_has_prefix (name, "image/")) {
667 GstDiscovererVideoInfo *info;
668 GstVideoFormat format;
671 info = (GstDiscovererVideoInfo *) parent;
673 info = (GstDiscovererVideoInfo *)
674 gst_mini_object_new (GST_TYPE_DISCOVERER_VIDEO_INFO);
675 info->parent.caps = caps;
678 if (gst_video_format_parse_caps (caps, &format, &tmp, &tmp2)) {
679 info->width = (guint) tmp;
680 info->height = (guint) tmp2;
683 if (gst_structure_get_int (caps_st, "depth", &tmp))
684 info->depth = (guint) tmp;
686 if (gst_video_parse_caps_pixel_aspect_ratio (caps, &tmp, &tmp2)) {
688 info->par_denom = tmp2;
691 if (gst_video_parse_caps_framerate (caps, &tmp, &tmp2)) {
692 info->framerate_num = tmp;
693 info->framerate_denom = tmp2;
696 if (gst_video_format_parse_caps_interlaced (caps, &btmp))
697 info->interlaced = btmp;
699 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
700 gst_structure_id_get (st, _TAGS_QUARK,
701 GST_TYPE_STRUCTURE, &tags_st, NULL);
702 if (gst_structure_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
703 gst_structure_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
704 info->bitrate = utmp;
706 if (gst_structure_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
707 info->max_bitrate = utmp;
709 /* FIXME: Is it worth it to remove the tags we've parsed? */
710 info->parent.tags = gst_tag_list_merge (info->parent.tags,
711 (GstTagList *) tags_st, GST_TAG_MERGE_REPLACE);
712 gst_structure_free (tags_st);
715 return (GstDiscovererStreamInfo *) info;
718 /* None of the above - populate what information we can */
719 GstDiscovererStreamInfo *info;
724 info = (GstDiscovererStreamInfo *)
725 gst_mini_object_new (GST_TYPE_DISCOVERER_STREAM_INFO);
729 if (gst_structure_id_get (st, _TAGS_QUARK,
730 GST_TYPE_STRUCTURE, &tags_st, NULL)) {
731 info->tags = gst_tag_list_merge (info->tags, (GstTagList *) tags_st,
732 GST_TAG_MERGE_REPLACE);
733 gst_structure_free (tags_st);
741 static GstStructure *
742 find_stream_for_node (GstDiscoverer * dc, const GstStructure * topology)
745 GstPad *target_pad = NULL;
746 GstStructure *st = NULL;
751 if (!gst_structure_id_has_field (topology, _TOPOLOGY_PAD_QUARK)) {
752 GST_DEBUG ("Could not find pad for node %" GST_PTR_FORMAT "\n", topology);
756 gst_structure_id_get (topology, _TOPOLOGY_PAD_QUARK,
757 GST_TYPE_PAD, &pad, NULL);
759 if (!dc->priv->streams)
762 for (i = 0, tmp = dc->priv->streams; tmp; tmp = tmp->next, i++) {
763 ps = (PrivateStream *) tmp->data;
765 target_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (ps->pad));
766 gst_object_unref (target_pad);
768 if (target_pad == pad)
773 st = collect_stream_information (dc, ps, i);
775 gst_object_unref (pad);
781 child_is_raw_stream (GstCaps * parent, GstCaps * child)
783 GstStructure *st1, *st2;
784 const gchar *name1, *name2;
786 st1 = gst_caps_get_structure (parent, 0);
787 name1 = gst_structure_get_name (st1);
788 st2 = gst_caps_get_structure (child, 0);
789 name2 = gst_structure_get_name (st2);
791 if ((g_str_has_prefix (name1, "audio/") &&
792 g_str_has_prefix (name2, "audio/x-raw")) ||
793 ((g_str_has_prefix (name1, "video/") ||
794 g_str_has_prefix (name1, "image/")) &&
795 g_str_has_prefix (name2, "video/x-raw"))) {
796 /* child is the "raw" sub-stream corresponding to parent */
803 /* If a parent is non-NULL, collected stream information will be appended to it
804 * (and where the information exists, it will be overriden)
806 static GstDiscovererStreamInfo *
807 parse_stream_topology (GstDiscoverer * dc, const GstStructure * topology,
808 GstDiscovererStreamInfo * parent)
810 GstDiscovererStreamInfo *res = NULL;
811 GstCaps *caps = NULL;
812 const GValue *nval = NULL;
814 GST_DEBUG ("parsing: %" GST_PTR_FORMAT, topology);
816 nval = gst_structure_get_value (topology, "next");
818 if (nval == NULL || GST_VALUE_HOLDS_STRUCTURE (nval)) {
819 GstStructure *st = find_stream_for_node (dc, topology);
820 gboolean add_to_list = TRUE;
823 res = collect_information (dc, st, parent);
824 gst_structure_free (st);
826 /* Didn't find a stream structure, so let's just use the caps we have */
827 res = collect_information (dc, topology, parent);
831 /* FIXME : aggregate with information from main streams */
832 GST_DEBUG ("Coudn't find 'next' ! might be the last entry");
835 const GstStructure *st;
837 st = gst_value_get_structure (nval);
839 GST_DEBUG ("next is a structure %" GST_PTR_FORMAT, st);
844 if (gst_structure_id_get (st, _CAPS_QUARK, GST_TYPE_CAPS, &caps, NULL)) {
845 if (gst_caps_can_intersect (parent->caps, caps)) {
846 /* We sometimes get an extra sub-stream from the parser. If this is
847 * the case, we just replace the parent caps with this stream's caps
848 * since they might contain more information */
849 gst_caps_unref (parent->caps);
852 parse_stream_topology (dc, st, parent);
855 } else if (child_is_raw_stream (parent->caps, caps)) {
856 /* This is the "raw" stream corresponding to the parent. This
857 * contains more information than the parent, tags etc. */
858 parse_stream_topology (dc, st, parent);
860 gst_caps_unref (caps);
863 GstDiscovererStreamInfo *next = parse_stream_topology (dc, st, NULL);
865 next->previous = res;
871 dc->priv->current_info->stream_list =
872 g_list_append (dc->priv->current_info->stream_list, res);
875 } else if (GST_VALUE_HOLDS_LIST (nval)) {
877 GstDiscovererContainerInfo *cont;
880 if (!gst_structure_id_get (topology, _CAPS_QUARK,
881 GST_TYPE_CAPS, &caps, NULL))
882 GST_WARNING ("Couldn't find caps !");
884 len = gst_value_list_get_size (nval);
885 GST_DEBUG ("next is a list of %d entries", len);
887 cont = (GstDiscovererContainerInfo *)
888 gst_mini_object_new (GST_TYPE_DISCOVERER_CONTAINER_INFO);
889 cont->parent.caps = caps;
890 res = (GstDiscovererStreamInfo *) cont;
892 if (gst_structure_id_has_field (topology, _TAGS_QUARK)) {
895 gst_structure_id_get (topology, _TAGS_QUARK,
896 GST_TYPE_STRUCTURE, &tags, NULL);
898 GST_DEBUG ("Merge tags %" GST_PTR_FORMAT, tags);
901 gst_tag_list_merge (cont->parent.tags, (GstTagList *) tags,
902 GST_TAG_MERGE_APPEND);
903 gst_tag_list_free (tags);
904 if (cont->parent.tags)
905 gst_tag_list_free (cont->parent.tags);
906 cont->parent.tags = tmp;
907 GST_DEBUG ("Container info tags %" GST_PTR_FORMAT, tmp);
910 for (i = 0; i < len; i++) {
911 const GValue *subv = gst_value_list_get_value (nval, i);
912 const GstStructure *subst = gst_value_get_structure (subv);
913 GstDiscovererStreamInfo *substream;
915 GST_DEBUG ("%d %" GST_PTR_FORMAT, i, subst);
917 substream = parse_stream_topology (dc, subst, NULL);
919 substream->previous = res;
921 g_list_append (cont->streams,
922 gst_discoverer_stream_info_ref (substream));
929 /* Called when pipeline is pre-rolled */
931 discoverer_collect (GstDiscoverer * dc)
933 GST_DEBUG ("Collecting information");
935 /* Stop the timeout handler if present */
936 if (dc->priv->timeoutid) {
937 g_source_remove (dc->priv->timeoutid);
938 dc->priv->timeoutid = 0;
941 if (dc->priv->streams) {
942 /* FIXME : Make this querying optional */
944 GstElement *pipeline = (GstElement *) dc->priv->pipeline;
945 GstFormat format = GST_FORMAT_TIME;
948 GST_DEBUG ("Attempting to query duration");
950 if (gst_element_query_duration (pipeline, &format, &dur)) {
951 if (format == GST_FORMAT_TIME) {
952 GST_DEBUG ("Got duration %" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
953 dc->priv->current_info->duration = (guint64) dur;
957 if (dc->priv->seeking_query) {
958 if (gst_element_query (pipeline, dc->priv->seeking_query)) {
961 gst_query_parse_seeking (dc->priv->seeking_query, &format,
962 &seekable, NULL, NULL);
963 if (format == GST_FORMAT_TIME) {
964 GST_DEBUG ("Got seekable %d", seekable);
965 dc->priv->current_info->seekable = seekable;
971 if (dc->priv->current_topology)
972 dc->priv->current_info->stream_info = parse_stream_topology (dc,
973 dc->priv->current_topology, NULL);
976 * Images need some special handling. They do not have a duration, have
977 * caps named image/<foo> (th exception being MJPEG video which is also
978 * type image/jpeg), and should consist of precisely one stream (actually
979 * initially there are 2, the image and raw stream, but we squash these
980 * while parsing the stream topology). At some ponit, if we find that these
981 * conditions are not sufficient, we can count the number of decoders and
982 * parsers in the chain, and if there's more than one decoder, or any
983 * parser at all, we should not mark this as an image.
985 if (dc->priv->current_info->duration == 0 &&
986 dc->priv->current_info->stream_info != NULL &&
987 dc->priv->current_info->stream_info->next == NULL) {
989 gst_caps_get_structure (dc->priv->current_info->stream_info->caps, 0);
991 if (g_str_has_prefix (gst_structure_get_name (st), "image/"))
992 ((GstDiscovererVideoInfo *) dc->priv->current_info->
993 stream_info)->is_image = TRUE;
997 if (dc->priv->async) {
998 GST_DEBUG ("Emitting 'discoverered'");
999 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_DISCOVERED], 0,
1000 dc->priv->current_info, dc->priv->current_error);
1001 /* Clients get a copy of current_info since it is a boxed type */
1002 gst_discoverer_info_unref (dc->priv->current_info);
1007 get_async_cb (gpointer cb_data, GSource * source, GSourceFunc * func,
1010 *func = (GSourceFunc) async_timeout_cb;
1014 /* Wrapper since GSourceCallbackFuncs don't expect a return value from ref() */
1016 _void_g_object_ref (gpointer object)
1018 g_object_ref (G_OBJECT (object));
1022 handle_current_async (GstDiscoverer * dc)
1025 static GSourceCallbackFuncs cb_funcs = {
1031 /* Attach a timeout to the main context */
1032 source = g_timeout_source_new (dc->priv->timeout / GST_MSECOND);
1033 g_source_set_callback_indirect (source, g_object_ref (dc), &cb_funcs);
1034 dc->priv->timeoutid = g_source_attach (source, dc->priv->ctx);
1035 g_source_unref (source);
1039 /* Returns TRUE if processing should stop */
1041 handle_message (GstDiscoverer * dc, GstMessage * msg)
1043 gboolean done = FALSE;
1045 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "got a %s message",
1046 GST_MESSAGE_TYPE_NAME (msg));
1048 switch (GST_MESSAGE_TYPE (msg)) {
1049 case GST_MESSAGE_ERROR:{
1053 gst_message_parse_error (msg, &gerr, &debug);
1054 GST_WARNING_OBJECT (GST_MESSAGE_SRC (msg),
1055 "Got an error [debug:%s], [message:%s]", debug, gerr->message);
1056 dc->priv->current_error = gerr;
1059 /* We need to stop */
1062 GST_DEBUG ("Setting result to ERROR");
1063 dc->priv->current_info->result = GST_DISCOVERER_ERROR;
1067 case GST_MESSAGE_EOS:
1068 GST_DEBUG ("Got EOS !");
1072 case GST_MESSAGE_ASYNC_DONE:
1073 if (GST_MESSAGE_SRC (msg) == (GstObject *) dc->priv->pipeline) {
1074 GST_DEBUG ("Finished changing state asynchronously");
1080 case GST_MESSAGE_ELEMENT:
1082 GQuark sttype = gst_structure_get_name_id (msg->structure);
1083 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1084 "structure %" GST_PTR_FORMAT, msg->structure);
1085 if (sttype == _MISSING_PLUGIN_QUARK) {
1086 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1087 "Setting result to MISSING_PLUGINS");
1088 dc->priv->current_info->result = GST_DISCOVERER_MISSING_PLUGINS;
1089 dc->priv->current_info->misc = gst_structure_copy (msg->structure);
1090 } else if (sttype == _STREAM_TOPOLOGY_QUARK) {
1091 dc->priv->current_topology = gst_structure_copy (msg->structure);
1096 case GST_MESSAGE_TAG:
1098 GstTagList *tl, *tmp;
1100 gst_message_parse_tag (msg, &tl);
1101 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Got tags %" GST_PTR_FORMAT, tl);
1102 /* Merge with current tags */
1104 gst_tag_list_merge (dc->priv->current_info->tags, tl,
1105 GST_TAG_MERGE_APPEND);
1106 gst_tag_list_free (tl);
1107 if (dc->priv->current_info->tags)
1108 gst_tag_list_free (dc->priv->current_info->tags);
1109 dc->priv->current_info->tags = tmp;
1110 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Current info %p, tags %"
1111 GST_PTR_FORMAT, dc->priv->current_info, tmp);
1124 handle_current_sync (GstDiscoverer * dc)
1127 gdouble deadline = ((gdouble) dc->priv->timeout) / GST_SECOND;
1129 gboolean done = FALSE;
1131 timer = g_timer_new ();
1132 g_timer_start (timer);
1135 /* poll bus with timeout */
1136 /* FIXME : make the timeout more fine-tuned */
1137 if ((msg = gst_bus_timed_pop (dc->priv->bus, GST_SECOND / 2))) {
1138 done = handle_message (dc, msg);
1139 gst_message_unref (msg);
1142 } while (!done && (g_timer_elapsed (timer, NULL) < deadline));
1146 GST_DEBUG ("we timed out! Setting result to TIMEOUT");
1147 dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
1152 g_timer_stop (timer);
1153 g_timer_destroy (timer);
1157 _setup_locked (GstDiscoverer * dc)
1159 GstStateChangeReturn ret;
1161 GST_DEBUG ("Setting up");
1163 /* Pop URI off the pending URI list */
1164 dc->priv->current_info =
1165 (GstDiscovererInfo *) gst_mini_object_new (GST_TYPE_DISCOVERER_INFO);
1166 dc->priv->current_info->uri = (gchar *) dc->priv->pending_uris->data;
1167 dc->priv->pending_uris =
1168 g_list_delete_link (dc->priv->pending_uris, dc->priv->pending_uris);
1170 /* set uri on uridecodebin */
1171 g_object_set (dc->priv->uridecodebin, "uri", dc->priv->current_info->uri,
1174 GST_DEBUG ("Current is now %s", dc->priv->current_info->uri);
1176 dc->priv->processing = TRUE;
1178 /* set pipeline to PAUSED */
1180 GST_DEBUG ("Setting pipeline to PAUSED");
1182 gst_element_set_state ((GstElement *) dc->priv->pipeline,
1186 GST_DEBUG_OBJECT (dc, "Pipeline going to PAUSED : %s",
1187 gst_element_state_change_return_get_name (ret));
1191 discoverer_cleanup (GstDiscoverer * dc)
1193 GST_DEBUG ("Cleaning up");
1195 gst_bus_set_flushing (dc->priv->bus, TRUE);
1196 gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_READY);
1197 gst_bus_set_flushing (dc->priv->bus, FALSE);
1200 if (dc->priv->current_error)
1201 g_error_free (dc->priv->current_error);
1202 dc->priv->current_error = NULL;
1203 if (dc->priv->current_topology) {
1204 gst_structure_free (dc->priv->current_topology);
1205 dc->priv->current_topology = NULL;
1208 dc->priv->current_info = NULL;
1210 /* Try popping the next uri */
1211 if (dc->priv->async) {
1212 if (dc->priv->pending_uris != NULL) {
1216 handle_current_async (dc);
1220 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_FINISHED], 0);
1229 discoverer_bus_cb (GstBus * bus, GstMessage * msg, GstDiscoverer * dc)
1231 if (dc->priv->processing) {
1232 if (handle_message (dc, msg)) {
1233 GST_DEBUG ("Stopping asynchronously");
1234 /* Serialise with _event_probe() */
1236 dc->priv->processing = FALSE;
1238 discoverer_collect (dc);
1239 discoverer_cleanup (dc);
1245 async_timeout_cb (GstDiscoverer * dc)
1247 if (!g_source_is_destroyed (g_main_current_source ())) {
1248 dc->priv->timeoutid = 0;
1249 GST_DEBUG ("Setting result to TIMEOUT");
1250 dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
1251 dc->priv->processing = FALSE;
1252 discoverer_collect (dc);
1253 discoverer_cleanup (dc);
1258 /* If there is a pending URI, it will pop it from the list of pending
1259 * URIs and start the discovery on it.
1261 * Returns GST_DISCOVERER_OK if the next URI was popped and is processing,
1262 * else a error flag.
1264 static GstDiscovererResult
1265 start_discovering (GstDiscoverer * dc)
1267 GstDiscovererResult res = GST_DISCOVERER_OK;
1269 GST_DEBUG ("Starting");
1272 if (dc->priv->pending_uris == NULL) {
1273 GST_WARNING ("No URI to process");
1274 res = GST_DISCOVERER_URI_INVALID;
1279 if (dc->priv->current_info != NULL) {
1280 GST_WARNING ("Already processing a file");
1281 res = GST_DISCOVERER_BUSY;
1286 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_STARTING], 0);
1292 if (dc->priv->async)
1293 handle_current_async (dc);
1295 handle_current_sync (dc);
1303 * gst_discoverer_start:
1304 * @discoverer: A #GstDiscoverer
1306 * Allow asynchronous discovering of URIs to take place.
1307 * A #GMainLoop must be available for #GstDiscoverer to properly work in
1308 * asynchronous mode.
1313 gst_discoverer_start (GstDiscoverer * discoverer)
1316 GMainContext *ctx = NULL;
1318 GST_DEBUG_OBJECT (discoverer, "Starting...");
1320 if (discoverer->priv->async) {
1321 GST_DEBUG_OBJECT (discoverer, "We were already started");
1325 discoverer->priv->async = TRUE;
1326 discoverer->priv->running = TRUE;
1328 ctx = g_main_context_get_thread_default ();
1330 /* Connect to bus signals */
1332 ctx = g_main_context_default ();
1334 source = gst_bus_create_watch (discoverer->priv->bus);
1335 g_source_set_callback (source, (GSourceFunc) gst_bus_async_signal_func,
1337 discoverer->priv->sourceid = g_source_attach (source, ctx);
1338 g_source_unref (source);
1339 discoverer->priv->ctx = g_main_context_ref (ctx);
1341 start_discovering (discoverer);
1342 GST_DEBUG_OBJECT (discoverer, "Started");
1346 * gst_discoverer_stop:
1347 * @discoverer: A #GstDiscoverer
1349 * Stop the discovery of any pending URIs and clears the list of
1350 * pending URIS (if any).
1355 gst_discoverer_stop (GstDiscoverer * discoverer)
1357 GST_DEBUG_OBJECT (discoverer, "Stopping...");
1359 if (!discoverer->priv->async) {
1360 GST_DEBUG_OBJECT (discoverer,
1361 "We were already stopped, or running synchronously");
1365 DISCO_LOCK (discoverer);
1366 if (discoverer->priv->processing) {
1367 /* We prevent any further processing by setting the bus to
1368 * flushing and setting the pipeline to READY.
1369 * _reset() will take care of the rest of the cleanup */
1370 if (discoverer->priv->bus)
1371 gst_bus_set_flushing (discoverer->priv->bus, TRUE);
1372 if (discoverer->priv->pipeline)
1373 gst_element_set_state ((GstElement *) discoverer->priv->pipeline,
1376 discoverer->priv->running = FALSE;
1377 DISCO_UNLOCK (discoverer);
1379 /* Remove timeout handler */
1380 if (discoverer->priv->timeoutid) {
1381 g_source_remove (discoverer->priv->timeoutid);
1382 discoverer->priv->timeoutid = 0;
1384 /* Remove signal watch */
1385 if (discoverer->priv->sourceid) {
1386 g_source_remove (discoverer->priv->sourceid);
1387 discoverer->priv->sourceid = 0;
1389 /* Unref main context */
1390 if (discoverer->priv->ctx) {
1391 g_main_context_unref (discoverer->priv->ctx);
1392 discoverer->priv->ctx = NULL;
1394 discoverer_reset (discoverer);
1396 discoverer->priv->async = FALSE;
1398 GST_DEBUG_OBJECT (discoverer, "Stopped");
1402 * gst_discoverer_discover_uri_async:
1403 * @discoverer: A #GstDiscoverer
1404 * @uri: the URI to add.
1406 * Appends the given @uri to the list of URIs to discoverer. The actual
1407 * discovery of the @uri will only take place if gst_discoverer_start() has
1410 * A copy of @uri will be made internally, so the caller can safely g_free()
1413 * Returns: %TRUE if the @uri was succesfully appended to the list of pending
1419 gst_discoverer_discover_uri_async (GstDiscoverer * discoverer,
1424 GST_DEBUG_OBJECT (discoverer, "uri : %s", uri);
1426 DISCO_LOCK (discoverer);
1427 can_run = (discoverer->priv->pending_uris == NULL);
1428 discoverer->priv->pending_uris =
1429 g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
1430 DISCO_UNLOCK (discoverer);
1433 start_discovering (discoverer);
1439 /* Synchronous mode */
1441 * gst_discoverer_discover_uri:
1442 * @discoverer: A #GstDiscoverer
1443 * @uri: The URI to run on.
1444 * @err: (out) (allow-none): If an error occurred, this field will be filled in.
1446 * Synchronously discovers the given @uri.
1448 * A copy of @uri will be made internally, so the caller can safely g_free()
1451 * Returns: (transfer full): the result of the scanning. Can be %NULL if an
1457 gst_discoverer_discover_uri (GstDiscoverer * discoverer, const gchar * uri,
1460 GstDiscovererResult res = 0;
1461 GstDiscovererInfo *info;
1463 GST_DEBUG_OBJECT (discoverer, "uri:%s", uri);
1465 DISCO_LOCK (discoverer);
1466 if (G_UNLIKELY (discoverer->priv->current_info)) {
1467 DISCO_UNLOCK (discoverer);
1468 GST_WARNING_OBJECT (discoverer, "Already handling a uri");
1472 discoverer->priv->pending_uris =
1473 g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
1474 DISCO_UNLOCK (discoverer);
1476 res = start_discovering (discoverer);
1477 discoverer_collect (discoverer);
1481 if (discoverer->priv->current_error)
1482 *err = g_error_copy (discoverer->priv->current_error);
1486 if (res != GST_DISCOVERER_OK) {
1487 GST_DEBUG ("Setting result to %d (was %d)", res,
1488 discoverer->priv->current_info->result);
1489 discoverer->priv->current_info->result = res;
1491 info = discoverer->priv->current_info;
1493 discoverer_cleanup (discoverer);
1499 * gst_discoverer_new:
1500 * @timeout: timeout per file, in nanoseconds. Allowed are values between
1501 * one second (#GST_SECOND) and one hour (3600 * #GST_SECOND)
1502 * @err: a pointer to a #GError. can be %NULL
1504 * Creates a new #GstDiscoverer with the provided timeout.
1506 * Returns: (transfer full): The new #GstDiscoverer.
1507 * If an error occurred when creating the discoverer, @err will be set
1508 * accordingly and %NULL will be returned. If @err is set, the caller must
1509 * free it when no longer needed using g_error_free().
1514 gst_discoverer_new (GstClockTime timeout, GError ** err)
1518 res = g_object_new (GST_TYPE_DISCOVERER, "timeout", timeout, NULL);
1519 if (res->priv->uridecodebin == NULL) {
1521 *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
1522 "Couldn't create 'uridecodebin' element");
1523 gst_object_unref (res);