3 * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
4 * Copyright (C) 2007-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-giosrc
24 * @see_also: #GstFileSrc, #GstGnomeVFSSrc, #GstGioSink
26 * This plugin reads data from a local or remote location specified
27 * by an URI. This location can be specified using any protocol supported by
28 * the GIO library or it's VFS backends. Common protocols are 'file', 'http',
31 * If an URI or #GFile is not mounted giosrc will post a message of type
32 * %GST_MESSAGE_ELEMENT with name "not-mounted" on the bus. The message
33 * also contains the #GFile and the corresponding URI.
34 * Applications can use the "not-mounted" message to mount the #GFile
35 * by calling g_file_mount_enclosing_volume() and then restart the
36 * pipeline after the mounting has succeeded. Note that right after the
37 * "not-mounted" message a normal error message is posted on the bus which
38 * should be ignored if "not-mounted" is handled by the application, for
39 * example by calling gst_bus_set_flushing(bus, TRUE) after the "not-mounted"
40 * message was received and gst_bus_set_flushing(bus, FALSE) after the
41 * mounting was successful.
44 * <title>Example launch lines</title>
46 * gst-launch -v giosrc location=file:///home/joe/foo.xyz ! fakesink
47 * ]| The above pipeline will simply read a local file and do nothing with the
48 * data read. Instead of giosrc, we could just as well have used the
49 * filesrc element here.
51 * gst-launch -v giosrc location=smb://othercomputer/foo.xyz ! filesink location=/home/joe/foo.xyz
52 * ]| The above pipeline will copy a file from a remote host to the local file
53 * system using the Samba protocol.
55 * gst-launch -v giosrc location=http://music.foobar.com/demo.mp3 ! mad ! audioconvert ! audioresample ! alsasink
56 * ]| The above pipeline will read and decode and play an mp3 file from a
57 * web server using the http protocol.
61 /* FIXME: We would like to mount the enclosing volume of an URL
62 * if it isn't mounted yet but this is possible async-only.
63 * Unfortunately this requires a running main loop from the
64 * default context and we can't guarantuee this!
66 * We would also like to do authentication while mounting.
73 #include "gstgiosrc.h"
76 GST_DEBUG_CATEGORY_STATIC (gst_gio_src_debug);
77 #define GST_CAT_DEFAULT gst_gio_src_debug
86 GST_BOILERPLATE_FULL (GstGioSrc, gst_gio_src, GstGioBaseSrc,
87 GST_TYPE_GIO_BASE_SRC, gst_gio_uri_handler_do_init);
89 static void gst_gio_src_finalize (GObject * object);
91 static void gst_gio_src_set_property (GObject * object, guint prop_id,
92 const GValue * value, GParamSpec * pspec);
93 static void gst_gio_src_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec);
96 static GInputStream *gst_gio_src_get_stream (GstGioBaseSrc * bsrc);
98 static gboolean gst_gio_src_check_get_range (GstBaseSrc * base_src);
101 gst_gio_src_base_init (gpointer gclass)
103 GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
105 GST_DEBUG_CATEGORY_INIT (gst_gio_src_debug, "gio_src", 0, "GIO source");
107 gst_element_class_set_details_simple (element_class, "GIO source",
109 "Read from any GIO-supported location",
110 "Ren\xc3\xa9 Stadler <mail@renestadler.de>, "
111 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
115 gst_gio_src_class_init (GstGioSrcClass * klass)
117 GObjectClass *gobject_class = (GObjectClass *) klass;
118 GstBaseSrcClass *gstbasesrc_class = (GstBaseSrcClass *) klass;
119 GstGioBaseSrcClass *gstgiobasesrc_class = (GstGioBaseSrcClass *) klass;
121 gobject_class->finalize = gst_gio_src_finalize;
122 gobject_class->set_property = gst_gio_src_set_property;
123 gobject_class->get_property = gst_gio_src_get_property;
125 g_object_class_install_property (gobject_class, PROP_LOCATION,
126 g_param_spec_string ("location", "Location", "URI location to read from",
127 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
132 * %GFile to read from.
136 g_object_class_install_property (gobject_class, PROP_FILE,
137 g_param_spec_object ("file", "File", "GFile to read from",
138 G_TYPE_FILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
140 gstbasesrc_class->check_get_range =
141 GST_DEBUG_FUNCPTR (gst_gio_src_check_get_range);
143 gstgiobasesrc_class->get_stream = GST_DEBUG_FUNCPTR (gst_gio_src_get_stream);
144 gstgiobasesrc_class->close_on_stop = TRUE;
148 gst_gio_src_init (GstGioSrc * src, GstGioSrcClass * gclass)
153 gst_gio_src_finalize (GObject * object)
155 GstGioSrc *src = GST_GIO_SRC (object);
158 g_object_unref (src->file);
162 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
166 gst_gio_src_set_property (GObject * object, guint prop_id,
167 const GValue * value, GParamSpec * pspec)
169 GstGioSrc *src = GST_GIO_SRC (object);
173 const gchar *uri = NULL;
175 if (GST_STATE (src) == GST_STATE_PLAYING ||
176 GST_STATE (src) == GST_STATE_PAUSED) {
178 ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
182 GST_OBJECT_LOCK (GST_OBJECT (src));
184 g_object_unref (src->file);
186 uri = g_value_get_string (value);
189 src->file = g_file_new_for_uri (uri);
192 GST_ERROR ("Could not create GFile for URI '%s'", uri);
197 GST_OBJECT_UNLOCK (GST_OBJECT (src));
201 if (GST_STATE (src) == GST_STATE_PLAYING ||
202 GST_STATE (src) == GST_STATE_PAUSED) {
204 ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
208 GST_OBJECT_LOCK (GST_OBJECT (src));
210 g_object_unref (src->file);
212 src->file = g_value_dup_object (value);
214 GST_OBJECT_UNLOCK (GST_OBJECT (src));
217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 gst_gio_src_get_property (GObject * object, guint prop_id,
224 GValue * value, GParamSpec * pspec)
226 GstGioSrc *src = GST_GIO_SRC (object);
232 GST_OBJECT_LOCK (GST_OBJECT (src));
234 uri = g_file_get_uri (src->file);
235 g_value_set_string (value, uri);
238 g_value_set_string (value, NULL);
240 GST_OBJECT_UNLOCK (GST_OBJECT (src));
244 GST_OBJECT_LOCK (GST_OBJECT (src));
245 g_value_set_object (value, src->file);
246 GST_OBJECT_UNLOCK (GST_OBJECT (src));
249 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255 gst_gio_src_check_get_range (GstBaseSrc * base_src)
257 GstGioSrc *src = GST_GIO_SRC (base_src);
260 if (src->file == NULL)
263 scheme = g_file_get_uri_scheme (src->file);
267 if (strcmp (scheme, "file") == 0) {
268 GST_LOG_OBJECT (src, "local URI, assuming random access is possible");
271 } else if (strcmp (scheme, "http") == 0 || strcmp (scheme, "https") == 0) {
272 GST_LOG_OBJECT (src, "blacklisted protocol '%s', "
273 "no random access possible", scheme);
282 GST_DEBUG_OBJECT (src, "undecided about random access, asking base class");
284 return GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_SRC_CLASS,
285 check_get_range, (base_src), FALSE);
289 static GInputStream *
290 gst_gio_src_get_stream (GstGioBaseSrc * bsrc)
292 GstGioSrc *src = GST_GIO_SRC (bsrc);
294 GInputStream *stream;
295 GCancellable *cancel = bsrc->cancel;
298 if (src->file == NULL) {
299 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
300 ("No location or GFile given"));
304 uri = g_file_get_uri (src->file);
306 uri = g_strdup ("(null)");
308 stream = G_INPUT_STREAM (g_file_read (src->file, cancel, &err));
310 if (stream == NULL && !gst_gio_error (src, "g_file_read", &err, NULL)) {
311 if (GST_GIO_ERROR_MATCHES (err, NOT_FOUND)) {
312 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
313 ("Could not open location %s for reading: %s", uri, err->message));
314 } else if (GST_GIO_ERROR_MATCHES (err, NOT_MOUNTED)) {
315 gst_element_post_message (GST_ELEMENT_CAST (src),
316 gst_message_new_element (GST_OBJECT_CAST (src),
317 gst_structure_new ("not-mounted", "file", G_TYPE_FILE, src->file,
318 "uri", G_TYPE_STRING, uri, NULL)));
320 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
321 ("Location %s not mounted: %s", uri, err->message));
323 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
324 ("Could not open location %s for reading: %s", uri, err->message));
328 g_clear_error (&err);
330 } else if (stream == NULL) {
335 GST_DEBUG_OBJECT (src, "opened location %s", uri);