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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, 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 iterating a
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()).
37 * By default this will use the GLib default main context unless you have
38 * set a custom context using g_main_context_push_thread_default().
40 * All the information is returned in a #GstDiscovererInfo structure.
47 #include <gst/video/video.h>
48 #include <gst/audio/audio.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 _TOC_QUARK;
59 static GQuark _STREAM_ID_QUARK;
60 static GQuark _MISSING_PLUGIN_QUARK;
61 static GQuark _STREAM_TOPOLOGY_QUARK;
62 static GQuark _TOPOLOGY_PAD_QUARK;
76 struct _GstDiscovererPrivate
80 /* allowed time to discover each uri in nanoseconds */
83 /* list of pending URI to process (current excluded) */
88 /* TRUE if processing a URI */
91 /* TRUE if discoverer has been started */
94 /* TRUE if ASYNC_DONE has been received (need to check for subtitle tags) */
98 GstDiscovererInfo *current_info;
99 GError *current_error;
100 GstStructure *current_topology;
102 /* List of private streams */
105 /* List of these sinks and their handler IDs (to remove the probe) */
106 guint pending_subtitle_pads;
108 /* Global elements */
110 GstElement *uridecodebin;
113 GType decodebin_type;
115 /* Custom main context variables */
120 /* reusable queries */
121 GstQuery *seeking_query;
123 /* Handler ids for various callbacks */
125 gulong pad_remove_id;
126 gulong source_chg_id;
127 gulong element_added_id;
131 #define DISCO_LOCK(dc) g_mutex_lock (&dc->priv->lock);
132 #define DISCO_UNLOCK(dc) g_mutex_unlock (&dc->priv->lock);
137 GST_DEBUG_CATEGORY_INIT (discoverer_debug, "discoverer", 0, "Discoverer");
139 _CAPS_QUARK = g_quark_from_static_string ("caps");
140 _TAGS_QUARK = g_quark_from_static_string ("tags");
141 _TOC_QUARK = g_quark_from_static_string ("toc");
142 _STREAM_ID_QUARK = g_quark_from_static_string ("stream-id");
143 _MISSING_PLUGIN_QUARK = g_quark_from_static_string ("missing-plugin");
144 _STREAM_TOPOLOGY_QUARK = g_quark_from_static_string ("stream-topology");
145 _TOPOLOGY_PAD_QUARK = g_quark_from_static_string ("pad");
148 G_DEFINE_TYPE_EXTENDED (GstDiscoverer, gst_discoverer, G_TYPE_OBJECT, 0,
160 #define DEFAULT_PROP_TIMEOUT 15 * GST_SECOND
168 static guint gst_discoverer_signals[LAST_SIGNAL] = { 0 };
170 static void gst_discoverer_set_timeout (GstDiscoverer * dc,
171 GstClockTime timeout);
172 static gboolean async_timeout_cb (GstDiscoverer * dc);
174 static void discoverer_bus_cb (GstBus * bus, GstMessage * msg,
176 static void uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
178 static void uridecodebin_pad_removed_cb (GstElement * uridecodebin,
179 GstPad * pad, GstDiscoverer * dc);
180 static void uridecodebin_source_changed_cb (GstElement * uridecodebin,
181 GParamSpec * pspec, GstDiscoverer * dc);
183 static void gst_discoverer_dispose (GObject * dc);
184 static void gst_discoverer_finalize (GObject * dc);
185 static void gst_discoverer_set_property (GObject * object, guint prop_id,
186 const GValue * value, GParamSpec * pspec);
187 static void gst_discoverer_get_property (GObject * object, guint prop_id,
188 GValue * value, GParamSpec * pspec);
191 gst_discoverer_class_init (GstDiscovererClass * klass)
193 GObjectClass *gobject_class = (GObjectClass *) klass;
195 gobject_class->dispose = gst_discoverer_dispose;
196 gobject_class->finalize = gst_discoverer_finalize;
198 gobject_class->set_property = gst_discoverer_set_property;
199 gobject_class->get_property = gst_discoverer_get_property;
201 g_type_class_add_private (klass, sizeof (GstDiscovererPrivate));
205 * GstDiscoverer:timeout:
207 * The duration (in nanoseconds) after which the discovery of an individual
210 * If the discovery of a URI times out, the %GST_DISCOVERER_TIMEOUT will be
211 * set on the result flags.
213 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
214 g_param_spec_uint64 ("timeout", "timeout", "Timeout",
215 GST_SECOND, 3600 * GST_SECOND, DEFAULT_PROP_TIMEOUT,
216 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
220 * GstDiscoverer::finished:
221 * @discoverer: the #GstDiscoverer
223 * Will be emitted in async mode when all pending URIs have been processed.
225 gst_discoverer_signals[SIGNAL_FINISHED] =
226 g_signal_new ("finished", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
227 G_STRUCT_OFFSET (GstDiscovererClass, finished),
228 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
231 * GstDiscoverer::starting:
232 * @discoverer: the #GstDiscoverer
234 * Will be emitted when the discover starts analyzing the pending URIs
236 gst_discoverer_signals[SIGNAL_STARTING] =
237 g_signal_new ("starting", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
238 G_STRUCT_OFFSET (GstDiscovererClass, starting),
239 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
242 * GstDiscoverer::discovered:
243 * @discoverer: the #GstDiscoverer
244 * @info: the results #GstDiscovererInfo
245 * @error: (type GLib.Error): #GError, which will be non-NULL if an error
246 * occurred during discovery. You must not
247 * free this #GError, it will be freed by
250 * Will be emitted in async mode when all information on a URI could be
251 * discovered, or an error occurred.
253 * When an error occurs, @info might still contain some partial information,
254 * depending on the circumstances of the error.
256 gst_discoverer_signals[SIGNAL_DISCOVERED] =
257 g_signal_new ("discovered", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
258 G_STRUCT_OFFSET (GstDiscovererClass, discovered),
259 NULL, NULL, g_cclosure_marshal_generic,
260 G_TYPE_NONE, 2, GST_TYPE_DISCOVERER_INFO,
261 G_TYPE_ERROR | G_SIGNAL_TYPE_STATIC_SCOPE);
264 * GstDiscoverer::source-setup:
265 * @discoverer: the #GstDiscoverer
266 * @source: source element
268 * This signal is emitted after the source element has been created for, so
269 * the URI being discovered, so it can be configured by setting additional
270 * properties (e.g. set a proxy server for an http source, or set the device
271 * and read speed for an audio cd source).
273 * This signal is usually emitted from the context of a GStreamer streaming
276 gst_discoverer_signals[SIGNAL_SOURCE_SETUP] =
277 g_signal_new ("source-setup", G_TYPE_FROM_CLASS (klass),
278 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDiscovererClass, source_setup),
279 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
283 uridecodebin_element_added_cb (GstElement * uridecodebin,
284 GstElement * child, GstDiscoverer * dc)
286 GST_DEBUG ("New element added to uridecodebin : %s",
287 GST_ELEMENT_NAME (child));
289 if (G_OBJECT_TYPE (child) == dc->priv->decodebin_type) {
290 g_object_set (child, "post-stream-topology", TRUE, NULL);
295 gst_discoverer_init (GstDiscoverer * dc)
298 GstFormat format = GST_FORMAT_TIME;
300 dc->priv = G_TYPE_INSTANCE_GET_PRIVATE (dc, GST_TYPE_DISCOVERER,
301 GstDiscovererPrivate);
303 dc->priv->timeout = DEFAULT_PROP_TIMEOUT;
304 dc->priv->async = FALSE;
305 dc->priv->async_done = FALSE;
307 g_mutex_init (&dc->priv->lock);
309 dc->priv->pending_subtitle_pads = 0;
311 GST_LOG ("Creating pipeline");
312 dc->priv->pipeline = (GstBin *) gst_pipeline_new ("Discoverer");
313 GST_LOG_OBJECT (dc, "Creating uridecodebin");
314 dc->priv->uridecodebin =
315 gst_element_factory_make ("uridecodebin", "discoverer-uri");
316 if (G_UNLIKELY (dc->priv->uridecodebin == NULL)) {
317 GST_ERROR ("Can't create uridecodebin");
320 GST_LOG_OBJECT (dc, "Adding uridecodebin to pipeline");
321 gst_bin_add (dc->priv->pipeline, dc->priv->uridecodebin);
323 dc->priv->pad_added_id =
324 g_signal_connect_object (dc->priv->uridecodebin, "pad-added",
325 G_CALLBACK (uridecodebin_pad_added_cb), dc, 0);
326 dc->priv->pad_remove_id =
327 g_signal_connect_object (dc->priv->uridecodebin, "pad-removed",
328 G_CALLBACK (uridecodebin_pad_removed_cb), dc, 0);
329 dc->priv->source_chg_id =
330 g_signal_connect_object (dc->priv->uridecodebin, "notify::source",
331 G_CALLBACK (uridecodebin_source_changed_cb), dc, 0);
333 GST_LOG_OBJECT (dc, "Getting pipeline bus");
334 dc->priv->bus = gst_pipeline_get_bus ((GstPipeline *) dc->priv->pipeline);
336 dc->priv->bus_cb_id =
337 g_signal_connect_object (dc->priv->bus, "message",
338 G_CALLBACK (discoverer_bus_cb), dc, 0);
340 GST_DEBUG_OBJECT (dc, "Done initializing Discoverer");
342 /* This is ugly. We get the GType of decodebin so we can quickly detect
343 * when a decodebin is added to uridecodebin so we can set the
344 * post-stream-topology setting to TRUE */
345 dc->priv->element_added_id =
346 g_signal_connect_object (dc->priv->uridecodebin, "element-added",
347 G_CALLBACK (uridecodebin_element_added_cb), dc, 0);
348 tmp = gst_element_factory_make ("decodebin", NULL);
349 dc->priv->decodebin_type = G_OBJECT_TYPE (tmp);
350 gst_object_unref (tmp);
353 dc->priv->seeking_query = gst_query_new_seeking (format);
357 discoverer_reset (GstDiscoverer * dc)
359 GST_DEBUG_OBJECT (dc, "Resetting");
361 if (dc->priv->pending_uris) {
362 g_list_foreach (dc->priv->pending_uris, (GFunc) g_free, NULL);
363 g_list_free (dc->priv->pending_uris);
364 dc->priv->pending_uris = NULL;
367 if (dc->priv->pipeline)
368 gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_NULL);
371 #define DISCONNECT_SIGNAL(o,i) G_STMT_START{ \
372 if ((i) && g_signal_handler_is_connected ((o), (i))) \
373 g_signal_handler_disconnect ((o), (i)); \
378 gst_discoverer_dispose (GObject * obj)
380 GstDiscoverer *dc = (GstDiscoverer *) obj;
382 GST_DEBUG_OBJECT (dc, "Disposing");
384 discoverer_reset (dc);
386 if (G_LIKELY (dc->priv->pipeline)) {
387 /* Workaround for bug #118536 */
388 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_added_id);
389 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_remove_id);
390 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->source_chg_id);
391 DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->element_added_id);
392 DISCONNECT_SIGNAL (dc->priv->bus, dc->priv->bus_cb_id);
394 /* pipeline was set to NULL in _reset */
395 gst_object_unref (dc->priv->pipeline);
396 gst_object_unref (dc->priv->bus);
398 dc->priv->pipeline = NULL;
399 dc->priv->uridecodebin = NULL;
400 dc->priv->bus = NULL;
403 gst_discoverer_stop (dc);
405 if (dc->priv->seeking_query) {
406 gst_query_unref (dc->priv->seeking_query);
407 dc->priv->seeking_query = NULL;
410 G_OBJECT_CLASS (gst_discoverer_parent_class)->dispose (obj);
414 gst_discoverer_finalize (GObject * obj)
416 GstDiscoverer *dc = (GstDiscoverer *) obj;
418 g_mutex_clear (&dc->priv->lock);
420 G_OBJECT_CLASS (gst_discoverer_parent_class)->finalize (obj);
424 gst_discoverer_set_property (GObject * object, guint prop_id,
425 const GValue * value, GParamSpec * pspec)
427 GstDiscoverer *dc = (GstDiscoverer *) object;
431 gst_discoverer_set_timeout (dc, g_value_get_uint64 (value));
434 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
440 gst_discoverer_get_property (GObject * object, guint prop_id,
441 GValue * value, GParamSpec * pspec)
443 GstDiscoverer *dc = (GstDiscoverer *) object;
448 g_value_set_uint64 (value, dc->priv->timeout);
452 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
458 gst_discoverer_set_timeout (GstDiscoverer * dc, GstClockTime timeout)
460 GST_DEBUG_OBJECT (dc, "timeout : %" GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
462 /* FIXME : update current pending timeout if we're running */
464 dc->priv->timeout = timeout;
468 static GstPadProbeReturn
469 _event_probe (GstPad * pad, GstPadProbeInfo * info, PrivateStream * ps)
471 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
473 switch (GST_EVENT_TYPE (event)) {
475 GstTagList *tl = NULL, *tmp;
477 gst_event_parse_tag (event, &tl);
478 GST_DEBUG_OBJECT (pad, "tags %" GST_PTR_FORMAT, tl);
480 /* If preroll is complete, drop these tags - the collected information is
481 * possibly already being processed and adding more tags would be racy */
482 if (G_LIKELY (ps->dc->priv->processing)) {
483 GST_DEBUG_OBJECT (pad, "private stream %p old tags %" GST_PTR_FORMAT,
485 tmp = gst_tag_list_merge (ps->tags, tl, GST_TAG_MERGE_APPEND);
487 gst_tag_list_unref (ps->tags);
489 GST_DEBUG_OBJECT (pad, "private stream %p new tags %" GST_PTR_FORMAT,
492 GST_DEBUG_OBJECT (pad, "Dropping tags since preroll is done");
493 DISCO_UNLOCK (ps->dc);
499 gst_event_parse_toc (event, &tmp, NULL);
500 GST_DEBUG_OBJECT (pad, "toc %" GST_PTR_FORMAT, tmp);
503 if (G_LIKELY (ps->dc->priv->processing)) {
504 GST_DEBUG_OBJECT (pad, "private stream %p toc %" GST_PTR_FORMAT, ps,
507 GST_DEBUG_OBJECT (pad, "Dropping toc since preroll is done");
508 DISCO_UNLOCK (ps->dc);
511 case GST_EVENT_STREAM_START:{
512 const gchar *stream_id;
514 gst_event_parse_stream_start (event, &stream_id);
516 g_free (ps->stream_id);
517 ps->stream_id = stream_id ? g_strdup (stream_id) : NULL;
524 return GST_PAD_PROBE_OK;
527 static GstStaticCaps subtitle_caps = GST_STATIC_CAPS ("text/x-raw; "
528 "subpicture/x-pgs; subpicture/x-dvb; subpicture/x-dvd; "
529 "application/x-subtitle-unknown; application/x-ssa; application/x-ass; "
530 "subtitle/x-kate; application/x-kate");
533 is_subtitle_caps (const GstCaps * caps)
538 subs_caps = gst_static_caps_get (&subtitle_caps);
539 ret = gst_caps_can_intersect (caps, subs_caps);
540 gst_caps_unref (subs_caps);
545 static GstPadProbeReturn
546 got_subtitle_data (GstPad * pad, GstPadProbeInfo * info, GstDiscoverer * dc)
549 if (!(GST_IS_BUFFER (info->data) || (GST_IS_EVENT (info->data)
550 && GST_EVENT_TYPE ((GstEvent *) info->data) == GST_EVENT_GAP)))
551 return GST_PAD_PROBE_OK;
556 dc->priv->pending_subtitle_pads--;
558 if (dc->priv->pending_subtitle_pads == 0) {
559 GstMessage *msg = gst_message_new_application (NULL,
560 gst_structure_new_empty ("DiscovererDone"));
561 gst_element_post_message ((GstElement *) dc->priv->pipeline, msg);
565 return GST_PAD_PROBE_REMOVE;
570 uridecodebin_source_changed_cb (GstElement * uridecodebin,
571 GParamSpec * pspec, GstDiscoverer * dc)
574 /* get a handle to the source */
575 g_object_get (uridecodebin, pspec->name, &src, NULL);
577 GST_DEBUG_OBJECT (dc, "got a new source %p", src);
579 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_SOURCE_SETUP], 0, src);
580 gst_object_unref (src);
584 uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
588 GstPad *sinkpad = NULL;
591 GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
593 ps = g_slice_new0 (PrivateStream);
597 ps->queue = gst_element_factory_make ("queue", NULL);
598 ps->sink = gst_element_factory_make ("fakesink", NULL);
600 if (G_UNLIKELY (ps->queue == NULL || ps->sink == NULL))
603 g_object_set (ps->sink, "silent", TRUE, NULL);
604 g_object_set (ps->queue, "max-size-buffers", 1, "silent", TRUE, NULL);
606 caps = gst_pad_query_caps (pad, NULL);
608 sinkpad = gst_element_get_static_pad (ps->queue, "sink");
612 if (is_subtitle_caps (caps)) {
613 /* Subtitle streams are sparse and may not provide any information - don't
614 * wait for data to preroll */
615 gst_pad_add_probe (sinkpad, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
616 (GstPadProbeCallback) got_subtitle_data, dc, NULL);
617 g_object_set (ps->sink, "async", FALSE, NULL);
619 dc->priv->pending_subtitle_pads++;
623 gst_caps_unref (caps);
625 gst_bin_add_many (dc->priv->pipeline, ps->queue, ps->sink, NULL);
627 if (!gst_element_link_pads_full (ps->queue, "src", ps->sink, "sink",
628 GST_PAD_LINK_CHECK_NOTHING))
630 if (!gst_element_sync_state_with_parent (ps->sink))
632 if (!gst_element_sync_state_with_parent (ps->queue))
635 if (gst_pad_link_full (pad, sinkpad,
636 GST_PAD_LINK_CHECK_NOTHING) != GST_PAD_LINK_OK)
638 gst_object_unref (sinkpad);
640 /* Add an event probe */
641 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
642 (GstPadProbeCallback) _event_probe, ps, NULL);
645 dc->priv->streams = g_list_append (dc->priv->streams, ps);
648 GST_DEBUG_OBJECT (dc, "Done handling pad");
653 GST_ERROR_OBJECT (dc, "Error while handling pad");
655 gst_object_unref (sinkpad);
657 gst_object_unref (ps->queue);
659 gst_object_unref (ps->sink);
660 g_slice_free (PrivateStream, ps);
665 uridecodebin_pad_removed_cb (GstElement * uridecodebin, GstPad * pad,
672 GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
674 /* Find the PrivateStream */
676 for (tmp = dc->priv->streams; tmp; tmp = tmp->next) {
677 ps = (PrivateStream *) tmp->data;
684 GST_DEBUG ("The removed pad wasn't controlled by us !");
688 dc->priv->streams = g_list_delete_link (dc->priv->streams, tmp);
691 gst_element_set_state (ps->sink, GST_STATE_NULL);
692 gst_element_set_state (ps->queue, GST_STATE_NULL);
693 gst_element_unlink (ps->queue, ps->sink);
695 sinkpad = gst_element_get_static_pad (ps->queue, "sink");
696 gst_pad_unlink (pad, sinkpad);
697 gst_object_unref (sinkpad);
699 /* references removed here */
700 gst_bin_remove_many (dc->priv->pipeline, ps->sink, ps->queue, NULL);
703 gst_tag_list_unref (ps->tags);
706 gst_toc_unref (ps->toc);
708 g_free (ps->stream_id);
710 g_slice_free (PrivateStream, ps);
712 GST_DEBUG ("Done handling pad");
715 static GstStructure *
716 collect_stream_information (GstDiscoverer * dc, PrivateStream * ps, guint idx)
722 stname = g_strdup_printf ("stream-%02d", idx);
723 st = gst_structure_new_empty (stname);
727 caps = gst_pad_get_current_caps (ps->pad);
729 GST_WARNING ("Couldn't get negotiated caps from %s:%s",
730 GST_DEBUG_PAD_NAME (ps->pad));
731 caps = gst_pad_query_caps (ps->pad, NULL);
734 GST_DEBUG ("stream-%02d, got caps %" GST_PTR_FORMAT, idx, caps);
735 gst_structure_id_set (st, _CAPS_QUARK, GST_TYPE_CAPS, caps, NULL);
736 gst_caps_unref (caps);
739 gst_structure_id_set (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, ps->tags, NULL);
741 gst_structure_id_set (st, _TOC_QUARK, GST_TYPE_TOC, ps->toc, NULL);
743 gst_structure_id_set (st, _STREAM_ID_QUARK, G_TYPE_STRING, ps->stream_id,
749 /* takes ownership of new_tags, may replace *taglist with a new one */
751 gst_discoverer_merge_and_replace_tags (GstTagList ** taglist,
752 GstTagList * new_tags)
754 if (new_tags == NULL)
757 if (*taglist == NULL) {
762 gst_tag_list_insert (*taglist, new_tags, GST_TAG_MERGE_REPLACE);
763 gst_tag_list_unref (new_tags);
767 collect_common_information (GstDiscovererStreamInfo * info,
768 const GstStructure * st)
770 if (gst_structure_id_has_field (st, _TOC_QUARK)) {
771 gst_structure_id_get (st, _TOC_QUARK, GST_TYPE_TOC, &info->toc, NULL);
774 if (gst_structure_id_has_field (st, _STREAM_ID_QUARK)) {
775 gst_structure_id_get (st, _STREAM_ID_QUARK, G_TYPE_STRING, &info->stream_id,
780 static GstDiscovererStreamInfo *
781 make_info (GstDiscovererStreamInfo * parent, GType type, GstCaps * caps)
783 GstDiscovererStreamInfo *info;
786 info = gst_discoverer_stream_info_ref (parent);
788 info = g_object_new (type, NULL);
790 info->caps = gst_caps_ref (caps);
795 /* Parses a set of caps and tags in st and populates a GstDiscovererStreamInfo
796 * structure (parent, if !NULL, otherwise it allocates one)
798 static GstDiscovererStreamInfo *
799 collect_information (GstDiscoverer * dc, const GstStructure * st,
800 GstDiscovererStreamInfo * parent)
803 GstStructure *caps_st;
809 if (!st || !gst_structure_id_has_field (st, _CAPS_QUARK)) {
810 GST_WARNING ("Couldn't find caps !");
811 return make_info (parent, GST_TYPE_DISCOVERER_STREAM_INFO, NULL);
814 gst_structure_id_get (st, _CAPS_QUARK, GST_TYPE_CAPS, &caps, NULL);
815 caps_st = gst_caps_get_structure (caps, 0);
816 name = gst_structure_get_name (caps_st);
818 if (g_str_has_prefix (name, "audio/")) {
819 GstDiscovererAudioInfo *info;
820 const gchar *format_str;
822 info = (GstDiscovererAudioInfo *) make_info (parent,
823 GST_TYPE_DISCOVERER_AUDIO_INFO, caps);
825 if (gst_structure_get_int (caps_st, "rate", &tmp))
826 info->sample_rate = (guint) tmp;
828 if (gst_structure_get_int (caps_st, "channels", &tmp))
829 info->channels = (guint) tmp;
831 /* FIXME: we only want to extract depth if raw audio is what's in the
832 * container (i.e. not if there is a decoder involved) */
833 format_str = gst_structure_get_string (caps_st, "format");
834 if (format_str != NULL) {
835 const GstAudioFormatInfo *finfo;
836 GstAudioFormat format;
838 format = gst_audio_format_from_string (format_str);
839 finfo = gst_audio_format_get_info (format);
840 info->depth = GST_AUDIO_FORMAT_INFO_DEPTH (finfo);
843 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
844 gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st, NULL);
845 if (gst_tag_list_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
846 gst_tag_list_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
847 info->bitrate = utmp;
849 if (gst_tag_list_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
850 info->max_bitrate = utmp;
852 /* FIXME: Is it worth it to remove the tags we've parsed? */
853 gst_discoverer_merge_and_replace_tags (&info->parent.tags, tags_st);
856 collect_common_information (&info->parent, st);
858 if (!info->language && ((GstDiscovererStreamInfo *) info)->tags) {
860 if (gst_tag_list_get_string (((GstDiscovererStreamInfo *) info)->tags,
861 GST_TAG_LANGUAGE_CODE, &language)) {
862 info->language = language;
866 gst_caps_unref (caps);
867 return (GstDiscovererStreamInfo *) info;
869 } else if (g_str_has_prefix (name, "video/") ||
870 g_str_has_prefix (name, "image/")) {
871 GstDiscovererVideoInfo *info;
874 info = (GstDiscovererVideoInfo *) make_info (parent,
875 GST_TYPE_DISCOVERER_VIDEO_INFO, caps);
877 if (gst_video_info_from_caps (&vinfo, caps)) {
878 info->width = (guint) vinfo.width;
879 info->height = (guint) vinfo.height;
881 info->depth = vinfo.finfo->bits * vinfo.finfo->n_components;
883 info->par_num = vinfo.par_n;
884 info->par_denom = vinfo.par_d;
886 info->framerate_num = vinfo.fps_n;
887 info->framerate_denom = vinfo.fps_d;
890 vinfo.interlace_mode != GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
893 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
894 gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st, NULL);
895 if (gst_tag_list_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
896 gst_tag_list_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
897 info->bitrate = utmp;
899 if (gst_tag_list_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
900 info->max_bitrate = utmp;
902 /* FIXME: Is it worth it to remove the tags we've parsed? */
903 gst_discoverer_merge_and_replace_tags (&info->parent.tags, tags_st);
906 collect_common_information (&info->parent, st);
908 gst_caps_unref (caps);
909 return (GstDiscovererStreamInfo *) info;
911 } else if (is_subtitle_caps (caps)) {
912 GstDiscovererSubtitleInfo *info;
914 info = (GstDiscovererSubtitleInfo *) make_info (parent,
915 GST_TYPE_DISCOVERER_SUBTITLE_INFO, caps);
917 if (gst_structure_id_has_field (st, _TAGS_QUARK)) {
918 const gchar *language;
920 gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st, NULL);
922 language = gst_structure_get_string (caps_st, GST_TAG_LANGUAGE_CODE);
924 info->language = g_strdup (language);
926 /* FIXME: Is it worth it to remove the tags we've parsed? */
927 gst_discoverer_merge_and_replace_tags (&info->parent.tags, tags_st);
930 collect_common_information (&info->parent, st);
932 if (!info->language && ((GstDiscovererStreamInfo *) info)->tags) {
934 if (gst_tag_list_get_string (((GstDiscovererStreamInfo *) info)->tags,
935 GST_TAG_LANGUAGE_CODE, &language)) {
936 info->language = language;
940 gst_caps_unref (caps);
941 return (GstDiscovererStreamInfo *) info;
944 /* None of the above - populate what information we can */
945 GstDiscovererStreamInfo *info;
947 info = make_info (parent, GST_TYPE_DISCOVERER_STREAM_INFO, caps);
949 if (gst_structure_id_get (st, _TAGS_QUARK, GST_TYPE_TAG_LIST, &tags_st,
951 gst_discoverer_merge_and_replace_tags (&info->tags, tags_st);
954 collect_common_information (info, st);
956 gst_caps_unref (caps);
962 static GstStructure *
963 find_stream_for_node (GstDiscoverer * dc, const GstStructure * topology)
966 GstPad *target_pad = NULL;
967 GstStructure *st = NULL;
972 if (!dc->priv->streams) {
976 if (!gst_structure_id_has_field (topology, _TOPOLOGY_PAD_QUARK)) {
977 GST_DEBUG ("Could not find pad for node %" GST_PTR_FORMAT, topology);
981 gst_structure_id_get (topology, _TOPOLOGY_PAD_QUARK,
982 GST_TYPE_PAD, &pad, NULL);
984 for (i = 0, tmp = dc->priv->streams; tmp; tmp = tmp->next, i++) {
985 ps = (PrivateStream *) tmp->data;
987 target_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (ps->pad));
988 gst_object_unref (target_pad);
990 if (target_pad == pad)
995 st = collect_stream_information (dc, ps, i);
997 gst_object_unref (pad);
1002 /* this can fail due to {framed,parsed}={TRUE,FALSE} differences, thus we filter
1005 child_is_same_stream (const GstCaps * _parent, const GstCaps * child)
1007 GstCaps *parent = gst_caps_copy (_parent);
1008 guint i, size = gst_caps_get_size (parent);
1011 for (i = 0; i < size; i++) {
1012 gst_structure_remove_field (gst_caps_get_structure (parent, i), "parsed");
1013 gst_structure_remove_field (gst_caps_get_structure (parent, i), "framed");
1015 res = gst_caps_can_intersect (parent, child);
1016 gst_caps_unref (parent);
1022 child_is_raw_stream (const GstCaps * parent, const GstCaps * child)
1024 const GstStructure *st1, *st2;
1025 const gchar *name1, *name2;
1027 st1 = gst_caps_get_structure (parent, 0);
1028 name1 = gst_structure_get_name (st1);
1029 st2 = gst_caps_get_structure (child, 0);
1030 name2 = gst_structure_get_name (st2);
1032 if ((g_str_has_prefix (name1, "audio/") &&
1033 g_str_has_prefix (name2, "audio/x-raw")) ||
1034 ((g_str_has_prefix (name1, "video/") ||
1035 g_str_has_prefix (name1, "image/")) &&
1036 g_str_has_prefix (name2, "video/x-raw"))) {
1037 /* child is the "raw" sub-stream corresponding to parent */
1041 if (is_subtitle_caps (parent))
1047 /* If a parent is non-NULL, collected stream information will be appended to it
1048 * (and where the information exists, it will be overriden)
1050 static GstDiscovererStreamInfo *
1051 parse_stream_topology (GstDiscoverer * dc, const GstStructure * topology,
1052 GstDiscovererStreamInfo * parent)
1054 GstDiscovererStreamInfo *res = NULL;
1055 GstCaps *caps = NULL;
1056 const GValue *nval = NULL;
1058 GST_DEBUG ("parsing: %" GST_PTR_FORMAT, topology);
1060 nval = gst_structure_get_value (topology, "next");
1062 if (nval == NULL || GST_VALUE_HOLDS_STRUCTURE (nval)) {
1063 GstStructure *st = find_stream_for_node (dc, topology);
1064 gboolean add_to_list = TRUE;
1067 res = collect_information (dc, st, parent);
1068 gst_structure_free (st);
1070 /* Didn't find a stream structure, so let's just use the caps we have */
1071 res = collect_information (dc, topology, parent);
1075 /* FIXME : aggregate with information from main streams */
1076 GST_DEBUG ("Coudn't find 'next' ! might be the last entry");
1078 st = (GstStructure *) gst_value_get_structure (nval);
1080 GST_DEBUG ("next is a structure %" GST_PTR_FORMAT, st);
1085 if (gst_structure_id_get (st, _CAPS_QUARK, GST_TYPE_CAPS, &caps, NULL)) {
1086 if (child_is_same_stream (parent->caps, caps)) {
1087 /* We sometimes get an extra sub-stream from the parser. If this is
1088 * the case, we just replace the parent caps with this stream's caps
1089 * since they might contain more information */
1090 gst_caps_replace (&parent->caps, caps);
1092 parse_stream_topology (dc, st, parent);
1093 add_to_list = FALSE;
1094 } else if (child_is_raw_stream (parent->caps, caps)) {
1095 /* This is the "raw" stream corresponding to the parent. This
1096 * contains more information than the parent, tags etc. */
1097 parse_stream_topology (dc, st, parent);
1098 add_to_list = FALSE;
1100 GstDiscovererStreamInfo *next = parse_stream_topology (dc, st, NULL);
1102 next->previous = res;
1104 gst_caps_unref (caps);
1109 dc->priv->current_info->stream_list =
1110 g_list_append (dc->priv->current_info->stream_list, res);
1112 gst_discoverer_stream_info_unref (res);
1115 } else if (GST_VALUE_HOLDS_LIST (nval)) {
1117 GstDiscovererContainerInfo *cont;
1120 if (!gst_structure_id_get (topology, _CAPS_QUARK,
1121 GST_TYPE_CAPS, &caps, NULL))
1122 GST_WARNING ("Couldn't find caps !");
1124 len = gst_value_list_get_size (nval);
1125 GST_DEBUG ("next is a list of %d entries", len);
1127 cont = (GstDiscovererContainerInfo *)
1128 g_object_new (GST_TYPE_DISCOVERER_CONTAINER_INFO, NULL);
1129 cont->parent.caps = caps;
1130 res = (GstDiscovererStreamInfo *) cont;
1132 if (gst_structure_id_has_field (topology, _TAGS_QUARK)) {
1135 gst_structure_id_get (topology, _TAGS_QUARK,
1136 GST_TYPE_TAG_LIST, &tags, NULL);
1138 GST_DEBUG ("Merge tags %" GST_PTR_FORMAT, tags);
1141 gst_tag_list_merge (cont->parent.tags, (GstTagList *) tags,
1142 GST_TAG_MERGE_APPEND);
1143 gst_tag_list_unref (tags);
1144 if (cont->parent.tags)
1145 gst_tag_list_unref (cont->parent.tags);
1146 cont->parent.tags = tmp;
1147 GST_DEBUG ("Container info tags %" GST_PTR_FORMAT, tmp);
1150 for (i = 0; i < len; i++) {
1151 const GValue *subv = gst_value_list_get_value (nval, i);
1152 const GstStructure *subst = gst_value_get_structure (subv);
1153 GstDiscovererStreamInfo *substream;
1155 GST_DEBUG ("%d %" GST_PTR_FORMAT, i, subst);
1157 substream = parse_stream_topology (dc, subst, NULL);
1159 substream->previous = res;
1161 g_list_append (cont->streams,
1162 gst_discoverer_stream_info_ref (substream));
1169 /* Called when pipeline is pre-rolled */
1171 discoverer_collect (GstDiscoverer * dc)
1173 GST_DEBUG ("Collecting information");
1175 /* Stop the timeout handler if present */
1176 if (dc->priv->timeoutid) {
1177 g_source_remove (dc->priv->timeoutid);
1178 dc->priv->timeoutid = 0;
1181 if (dc->priv->streams) {
1182 /* FIXME : Make this querying optional */
1184 GstElement *pipeline = (GstElement *) dc->priv->pipeline;
1187 GST_DEBUG ("Attempting to query duration");
1189 if (gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur)) {
1190 GST_DEBUG ("Got duration %" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
1191 dc->priv->current_info->duration = (guint64) dur;
1193 GstStateChangeReturn sret;
1195 /* Some parsers may not even return a rough estimate right away, e.g.
1196 * because they've only processed a single frame so far, so if we
1197 * didn't get a duration the first time, spin a bit and try again.
1198 * Ugly, but still better than making parsers or other elements return
1199 * completely bogus values. We need some API extensions to solve this
1201 GST_INFO ("No duration yet, try a bit harder..");
1202 sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
1203 if (sret != GST_STATE_CHANGE_FAILURE) {
1206 for (i = 0; i < 2; ++i) {
1207 g_usleep (G_USEC_PER_SEC / 20);
1208 if (gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur)
1210 GST_DEBUG ("Got duration %" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
1211 dc->priv->current_info->duration = (guint64) dur;
1215 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1219 if (dc->priv->seeking_query) {
1220 if (gst_element_query (pipeline, dc->priv->seeking_query)) {
1224 gst_query_parse_seeking (dc->priv->seeking_query, &format,
1225 &seekable, NULL, NULL);
1226 if (format == GST_FORMAT_TIME) {
1227 GST_DEBUG ("Got seekable %d", seekable);
1228 dc->priv->current_info->seekable = seekable;
1234 if (dc->priv->current_topology)
1235 dc->priv->current_info->stream_info = parse_stream_topology (dc,
1236 dc->priv->current_topology, NULL);
1239 * Images need some special handling. They do not have a duration, have
1240 * caps named image/<foo> (th exception being MJPEG video which is also
1241 * type image/jpeg), and should consist of precisely one stream (actually
1242 * initially there are 2, the image and raw stream, but we squash these
1243 * while parsing the stream topology). At some point, if we find that these
1244 * conditions are not sufficient, we can count the number of decoders and
1245 * parsers in the chain, and if there's more than one decoder, or any
1246 * parser at all, we should not mark this as an image.
1248 if (dc->priv->current_info->duration == 0 &&
1249 dc->priv->current_info->stream_info != NULL &&
1250 dc->priv->current_info->stream_info->next == NULL) {
1251 GstDiscovererStreamInfo *stream_info;
1254 stream_info = dc->priv->current_info->stream_info;
1255 st = gst_caps_get_structure (stream_info->caps, 0);
1257 if (g_str_has_prefix (gst_structure_get_name (st), "image/"))
1258 ((GstDiscovererVideoInfo *) stream_info)->is_image = TRUE;
1262 if (dc->priv->async) {
1263 GST_DEBUG ("Emitting 'discoverered'");
1264 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_DISCOVERED], 0,
1265 dc->priv->current_info, dc->priv->current_error);
1266 /* Clients get a copy of current_info since it is a boxed type */
1267 gst_discoverer_info_unref (dc->priv->current_info);
1268 dc->priv->current_info = NULL;
1273 get_async_cb (gpointer cb_data, GSource * source, GSourceFunc * func,
1276 *func = (GSourceFunc) async_timeout_cb;
1280 /* Wrapper since GSourceCallbackFuncs don't expect a return value from ref() */
1282 _void_g_object_ref (gpointer object)
1284 g_object_ref (G_OBJECT (object));
1288 handle_current_async (GstDiscoverer * dc)
1291 static GSourceCallbackFuncs cb_funcs = {
1297 /* Attach a timeout to the main context */
1298 source = g_timeout_source_new (dc->priv->timeout / GST_MSECOND);
1299 g_source_set_callback_indirect (source, g_object_ref (dc), &cb_funcs);
1300 dc->priv->timeoutid = g_source_attach (source, dc->priv->ctx);
1301 g_source_unref (source);
1305 /* Returns TRUE if processing should stop */
1307 handle_message (GstDiscoverer * dc, GstMessage * msg)
1309 gboolean done = FALSE;
1311 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "got a %s message",
1312 GST_MESSAGE_TYPE_NAME (msg));
1314 switch (GST_MESSAGE_TYPE (msg)) {
1315 case GST_MESSAGE_ERROR:{
1319 gst_message_parse_error (msg, &gerr, &debug);
1320 GST_WARNING_OBJECT (GST_MESSAGE_SRC (msg),
1321 "Got an error [debug:%s], [message:%s]", debug, gerr->message);
1322 dc->priv->current_error = gerr;
1325 /* We need to stop */
1328 /* Don't override missing plugin result code for missing plugin errors */
1329 if (dc->priv->current_info->result != GST_DISCOVERER_MISSING_PLUGINS ||
1330 (!g_error_matches (gerr, GST_CORE_ERROR,
1331 GST_CORE_ERROR_MISSING_PLUGIN) &&
1332 !g_error_matches (gerr, GST_STREAM_ERROR,
1333 GST_STREAM_ERROR_CODEC_NOT_FOUND))) {
1334 GST_DEBUG ("Setting result to ERROR");
1335 dc->priv->current_info->result = GST_DISCOVERER_ERROR;
1340 case GST_MESSAGE_EOS:
1341 GST_DEBUG ("Got EOS !");
1345 case GST_MESSAGE_APPLICATION:{
1347 gboolean async_done;
1348 name = gst_structure_get_name (gst_message_get_structure (msg));
1349 /* Maybe ASYNC_DONE is received & we're just waiting for subtitle tags */
1351 async_done = dc->priv->async_done;
1353 if (g_str_equal (name, "DiscovererDone") && async_done)
1358 case GST_MESSAGE_ASYNC_DONE:
1359 if (GST_MESSAGE_SRC (msg) == (GstObject *) dc->priv->pipeline) {
1360 GST_DEBUG ("Finished changing state asynchronously");
1362 if (dc->priv->pending_subtitle_pads == 0) {
1365 /* Remember that ASYNC_DONE has been received, wait for subtitles */
1366 dc->priv->async_done = TRUE;
1373 case GST_MESSAGE_ELEMENT:
1376 const GstStructure *structure;
1378 structure = gst_message_get_structure (msg);
1379 sttype = gst_structure_get_name_id (structure);
1380 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1381 "structure %" GST_PTR_FORMAT, structure);
1382 if (sttype == _MISSING_PLUGIN_QUARK) {
1383 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1384 "Setting result to MISSING_PLUGINS");
1385 dc->priv->current_info->result = GST_DISCOVERER_MISSING_PLUGINS;
1386 if (dc->priv->current_info->misc)
1387 gst_structure_free (dc->priv->current_info->misc);
1388 dc->priv->current_info->misc = gst_structure_copy (structure);
1389 } else if (sttype == _STREAM_TOPOLOGY_QUARK) {
1390 if (dc->priv->current_topology)
1391 gst_structure_free (dc->priv->current_topology);
1392 dc->priv->current_topology = gst_structure_copy (structure);
1397 case GST_MESSAGE_TAG:
1399 GstTagList *tl, *tmp;
1401 gst_message_parse_tag (msg, &tl);
1402 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Got tags %" GST_PTR_FORMAT, tl);
1403 /* Merge with current tags */
1405 gst_tag_list_merge (dc->priv->current_info->tags, tl,
1406 GST_TAG_MERGE_APPEND);
1407 gst_tag_list_unref (tl);
1408 if (dc->priv->current_info->tags)
1409 gst_tag_list_unref (dc->priv->current_info->tags);
1410 dc->priv->current_info->tags = tmp;
1411 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Current info %p, tags %"
1412 GST_PTR_FORMAT, dc->priv->current_info, tmp);
1416 case GST_MESSAGE_TOC:
1420 gst_message_parse_toc (msg, &tmp, NULL);
1421 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Got toc %" GST_PTR_FORMAT, tmp);
1422 dc->priv->current_info->toc = tmp;
1423 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Current info %p, toc %"
1424 GST_PTR_FORMAT, dc->priv->current_info, tmp);
1436 handle_current_sync (GstDiscoverer * dc)
1439 gdouble deadline = ((gdouble) dc->priv->timeout) / GST_SECOND;
1441 gboolean done = FALSE;
1443 timer = g_timer_new ();
1444 g_timer_start (timer);
1447 /* poll bus with timeout */
1448 /* FIXME : make the timeout more fine-tuned */
1449 if ((msg = gst_bus_timed_pop (dc->priv->bus, GST_SECOND / 2))) {
1450 done = handle_message (dc, msg);
1451 gst_message_unref (msg);
1453 } while (!done && (g_timer_elapsed (timer, NULL) < deadline));
1457 GST_DEBUG ("we timed out! Setting result to TIMEOUT");
1458 dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
1463 g_timer_stop (timer);
1464 g_timer_destroy (timer);
1468 _setup_locked (GstDiscoverer * dc)
1470 GstStateChangeReturn ret;
1472 GST_DEBUG ("Setting up");
1474 /* Pop URI off the pending URI list */
1475 dc->priv->current_info =
1476 (GstDiscovererInfo *) g_object_new (GST_TYPE_DISCOVERER_INFO, NULL);
1477 dc->priv->current_info->uri = (gchar *) dc->priv->pending_uris->data;
1478 dc->priv->pending_uris =
1479 g_list_delete_link (dc->priv->pending_uris, dc->priv->pending_uris);
1481 /* set uri on uridecodebin */
1482 g_object_set (dc->priv->uridecodebin, "uri", dc->priv->current_info->uri,
1485 GST_DEBUG ("Current is now %s", dc->priv->current_info->uri);
1487 dc->priv->processing = TRUE;
1489 /* set pipeline to PAUSED */
1491 GST_DEBUG ("Setting pipeline to PAUSED");
1493 gst_element_set_state ((GstElement *) dc->priv->pipeline,
1495 if (ret == GST_STATE_CHANGE_NO_PREROLL) {
1496 GST_DEBUG ("Source is live, switching to PLAYING");
1498 gst_element_set_state ((GstElement *) dc->priv->pipeline,
1503 GST_DEBUG_OBJECT (dc, "Pipeline going to PAUSED : %s",
1504 gst_element_state_change_return_get_name (ret));
1508 discoverer_cleanup (GstDiscoverer * dc)
1510 GST_DEBUG ("Cleaning up");
1512 gst_bus_set_flushing (dc->priv->bus, TRUE);
1513 gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_READY);
1514 gst_bus_set_flushing (dc->priv->bus, FALSE);
1517 if (dc->priv->current_error)
1518 g_error_free (dc->priv->current_error);
1519 dc->priv->current_error = NULL;
1520 if (dc->priv->current_topology) {
1521 gst_structure_free (dc->priv->current_topology);
1522 dc->priv->current_topology = NULL;
1525 dc->priv->current_info = NULL;
1527 dc->priv->pending_subtitle_pads = 0;
1528 dc->priv->async_done = FALSE;
1530 /* Try popping the next uri */
1531 if (dc->priv->async) {
1532 if (dc->priv->pending_uris != NULL) {
1536 handle_current_async (dc);
1540 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_FINISHED], 0);
1549 discoverer_bus_cb (GstBus * bus, GstMessage * msg, GstDiscoverer * dc)
1551 if (dc->priv->processing) {
1552 if (handle_message (dc, msg)) {
1553 GST_DEBUG ("Stopping asynchronously");
1554 /* Serialise with _event_probe() */
1556 dc->priv->processing = FALSE;
1558 discoverer_collect (dc);
1559 discoverer_cleanup (dc);
1565 async_timeout_cb (GstDiscoverer * dc)
1567 if (!g_source_is_destroyed (g_main_current_source ())) {
1568 dc->priv->timeoutid = 0;
1569 GST_DEBUG ("Setting result to TIMEOUT");
1570 dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
1571 dc->priv->processing = FALSE;
1572 discoverer_collect (dc);
1573 discoverer_cleanup (dc);
1578 /* If there is a pending URI, it will pop it from the list of pending
1579 * URIs and start the discovery on it.
1581 * Returns GST_DISCOVERER_OK if the next URI was popped and is processing,
1582 * else a error flag.
1584 static GstDiscovererResult
1585 start_discovering (GstDiscoverer * dc)
1587 GstDiscovererResult res = GST_DISCOVERER_OK;
1589 GST_DEBUG ("Starting");
1592 if (dc->priv->pending_uris == NULL) {
1593 GST_WARNING ("No URI to process");
1594 res = GST_DISCOVERER_URI_INVALID;
1599 if (dc->priv->current_info != NULL) {
1600 GST_WARNING ("Already processing a file");
1601 res = GST_DISCOVERER_BUSY;
1606 g_signal_emit (dc, gst_discoverer_signals[SIGNAL_STARTING], 0);
1612 if (dc->priv->async)
1613 handle_current_async (dc);
1615 handle_current_sync (dc);
1623 * gst_discoverer_start:
1624 * @discoverer: A #GstDiscoverer
1626 * Allow asynchronous discovering of URIs to take place.
1627 * A #GMainLoop must be available for #GstDiscoverer to properly work in
1628 * asynchronous mode.
1631 gst_discoverer_start (GstDiscoverer * discoverer)
1634 GMainContext *ctx = NULL;
1636 GST_DEBUG_OBJECT (discoverer, "Starting...");
1638 if (discoverer->priv->async) {
1639 GST_DEBUG_OBJECT (discoverer, "We were already started");
1643 discoverer->priv->async = TRUE;
1644 discoverer->priv->running = TRUE;
1646 ctx = g_main_context_get_thread_default ();
1648 /* Connect to bus signals */
1650 ctx = g_main_context_default ();
1652 source = gst_bus_create_watch (discoverer->priv->bus);
1653 g_source_set_callback (source, (GSourceFunc) gst_bus_async_signal_func,
1655 discoverer->priv->sourceid = g_source_attach (source, ctx);
1656 g_source_unref (source);
1657 discoverer->priv->ctx = g_main_context_ref (ctx);
1659 start_discovering (discoverer);
1660 GST_DEBUG_OBJECT (discoverer, "Started");
1664 * gst_discoverer_stop:
1665 * @discoverer: A #GstDiscoverer
1667 * Stop the discovery of any pending URIs and clears the list of
1668 * pending URIS (if any).
1671 gst_discoverer_stop (GstDiscoverer * discoverer)
1673 GST_DEBUG_OBJECT (discoverer, "Stopping...");
1675 if (!discoverer->priv->async) {
1676 GST_DEBUG_OBJECT (discoverer,
1677 "We were already stopped, or running synchronously");
1681 DISCO_LOCK (discoverer);
1682 if (discoverer->priv->processing) {
1683 /* We prevent any further processing by setting the bus to
1684 * flushing and setting the pipeline to READY.
1685 * _reset() will take care of the rest of the cleanup */
1686 if (discoverer->priv->bus)
1687 gst_bus_set_flushing (discoverer->priv->bus, TRUE);
1688 if (discoverer->priv->pipeline)
1689 gst_element_set_state ((GstElement *) discoverer->priv->pipeline,
1692 discoverer->priv->running = FALSE;
1693 DISCO_UNLOCK (discoverer);
1695 /* Remove timeout handler */
1696 if (discoverer->priv->timeoutid) {
1697 g_source_remove (discoverer->priv->timeoutid);
1698 discoverer->priv->timeoutid = 0;
1700 /* Remove signal watch */
1701 if (discoverer->priv->sourceid) {
1702 g_source_remove (discoverer->priv->sourceid);
1703 discoverer->priv->sourceid = 0;
1705 /* Unref main context */
1706 if (discoverer->priv->ctx) {
1707 g_main_context_unref (discoverer->priv->ctx);
1708 discoverer->priv->ctx = NULL;
1710 discoverer_reset (discoverer);
1712 discoverer->priv->async = FALSE;
1714 GST_DEBUG_OBJECT (discoverer, "Stopped");
1718 * gst_discoverer_discover_uri_async:
1719 * @discoverer: A #GstDiscoverer
1720 * @uri: the URI to add.
1722 * Appends the given @uri to the list of URIs to discoverer. The actual
1723 * discovery of the @uri will only take place if gst_discoverer_start() has
1726 * A copy of @uri will be made internally, so the caller can safely g_free()
1729 * Returns: %TRUE if the @uri was successfully appended to the list of pending
1733 gst_discoverer_discover_uri_async (GstDiscoverer * discoverer,
1738 GST_DEBUG_OBJECT (discoverer, "uri : %s", uri);
1740 DISCO_LOCK (discoverer);
1741 can_run = (discoverer->priv->pending_uris == NULL);
1742 discoverer->priv->pending_uris =
1743 g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
1744 DISCO_UNLOCK (discoverer);
1747 start_discovering (discoverer);
1753 /* Synchronous mode */
1755 * gst_discoverer_discover_uri:
1756 * @discoverer: A #GstDiscoverer
1757 * @uri: The URI to run on.
1758 * @err: (out) (allow-none): If an error occurred, this field will be filled in.
1760 * Synchronously discovers the given @uri.
1762 * A copy of @uri will be made internally, so the caller can safely g_free()
1765 * Returns: (transfer full): the result of the scanning. Can be %NULL if an
1769 gst_discoverer_discover_uri (GstDiscoverer * discoverer, const gchar * uri,
1772 GstDiscovererResult res = 0;
1773 GstDiscovererInfo *info;
1775 GST_DEBUG_OBJECT (discoverer, "uri:%s", uri);
1777 DISCO_LOCK (discoverer);
1778 if (G_UNLIKELY (discoverer->priv->current_info)) {
1779 DISCO_UNLOCK (discoverer);
1780 GST_WARNING_OBJECT (discoverer, "Already handling a uri");
1784 discoverer->priv->pending_uris =
1785 g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
1786 DISCO_UNLOCK (discoverer);
1788 res = start_discovering (discoverer);
1789 discoverer_collect (discoverer);
1793 if (discoverer->priv->current_error)
1794 *err = g_error_copy (discoverer->priv->current_error);
1798 if (res != GST_DISCOVERER_OK) {
1799 GST_DEBUG ("Setting result to %d (was %d)", res,
1800 discoverer->priv->current_info->result);
1801 discoverer->priv->current_info->result = res;
1803 info = discoverer->priv->current_info;
1805 discoverer_cleanup (discoverer);
1811 * gst_discoverer_new:
1812 * @timeout: timeout per file, in nanoseconds. Allowed are values between
1813 * one second (#GST_SECOND) and one hour (3600 * #GST_SECOND)
1814 * @err: a pointer to a #GError. can be %NULL
1816 * Creates a new #GstDiscoverer with the provided timeout.
1818 * Returns: (transfer full): The new #GstDiscoverer.
1819 * If an error occurred when creating the discoverer, @err will be set
1820 * accordingly and %NULL will be returned. If @err is set, the caller must
1821 * free it when no longer needed using g_error_free().
1824 gst_discoverer_new (GstClockTime timeout, GError ** err)
1828 res = g_object_new (GST_TYPE_DISCOVERER, "timeout", timeout, NULL);
1829 if (res->priv->uridecodebin == NULL) {
1831 *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
1832 "Couldn't create 'uridecodebin' element");
1833 gst_object_unref (res);