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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, 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 #define gst_gio_src_parent_class parent_class
87 G_DEFINE_TYPE_WITH_CODE (GstGioSrc, gst_gio_src,
88 GST_TYPE_GIO_BASE_SRC, gst_gio_uri_handler_do_init (g_define_type_id));
90 static void gst_gio_src_finalize (GObject * object);
92 static void gst_gio_src_set_property (GObject * object, guint prop_id,
93 const GValue * value, GParamSpec * pspec);
94 static void gst_gio_src_get_property (GObject * object, guint prop_id,
95 GValue * value, GParamSpec * pspec);
97 static GInputStream *gst_gio_src_get_stream (GstGioBaseSrc * bsrc);
99 static gboolean gst_gio_src_query (GstBaseSrc * base_src, GstQuery * query);
102 gst_gio_src_class_init (GstGioSrcClass * klass)
104 GObjectClass *gobject_class = (GObjectClass *) klass;
105 GstElementClass *gstelement_class = (GstElementClass *) klass;
106 GstBaseSrcClass *gstbasesrc_class = (GstBaseSrcClass *) klass;
107 GstGioBaseSrcClass *gstgiobasesrc_class = (GstGioBaseSrcClass *) klass;
109 GST_DEBUG_CATEGORY_INIT (gst_gio_src_debug, "gio_src", 0, "GIO source");
111 gobject_class->finalize = gst_gio_src_finalize;
112 gobject_class->set_property = gst_gio_src_set_property;
113 gobject_class->get_property = gst_gio_src_get_property;
115 g_object_class_install_property (gobject_class, PROP_LOCATION,
116 g_param_spec_string ("location", "Location", "URI location to read from",
117 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
122 * %GFile to read from.
126 g_object_class_install_property (gobject_class, PROP_FILE,
127 g_param_spec_object ("file", "File", "GFile to read from",
128 G_TYPE_FILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
130 gst_element_class_set_static_metadata (gstelement_class, "GIO source",
132 "Read from any GIO-supported location",
133 "Ren\xc3\xa9 Stadler <mail@renestadler.de>, "
134 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
136 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_gio_src_query);
138 gstgiobasesrc_class->get_stream = GST_DEBUG_FUNCPTR (gst_gio_src_get_stream);
139 gstgiobasesrc_class->close_on_stop = TRUE;
143 gst_gio_src_init (GstGioSrc * src)
148 gst_gio_src_finalize (GObject * object)
150 GstGioSrc *src = GST_GIO_SRC (object);
153 g_object_unref (src->file);
157 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
161 gst_gio_src_set_property (GObject * object, guint prop_id,
162 const GValue * value, GParamSpec * pspec)
164 GstGioSrc *src = GST_GIO_SRC (object);
168 const gchar *uri = NULL;
170 if (GST_STATE (src) == GST_STATE_PLAYING ||
171 GST_STATE (src) == GST_STATE_PAUSED) {
173 ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
177 GST_OBJECT_LOCK (GST_OBJECT (src));
179 g_object_unref (src->file);
181 uri = g_value_get_string (value);
184 src->file = g_file_new_for_uri (uri);
187 GST_ERROR ("Could not create GFile for URI '%s'", uri);
192 GST_OBJECT_UNLOCK (GST_OBJECT (src));
196 if (GST_STATE (src) == GST_STATE_PLAYING ||
197 GST_STATE (src) == GST_STATE_PAUSED) {
199 ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
203 GST_OBJECT_LOCK (GST_OBJECT (src));
205 g_object_unref (src->file);
207 src->file = g_value_dup_object (value);
209 GST_OBJECT_UNLOCK (GST_OBJECT (src));
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218 gst_gio_src_get_property (GObject * object, guint prop_id,
219 GValue * value, GParamSpec * pspec)
221 GstGioSrc *src = GST_GIO_SRC (object);
227 GST_OBJECT_LOCK (GST_OBJECT (src));
229 uri = g_file_get_uri (src->file);
230 g_value_set_string (value, uri);
233 g_value_set_string (value, NULL);
235 GST_OBJECT_UNLOCK (GST_OBJECT (src));
239 GST_OBJECT_LOCK (GST_OBJECT (src));
240 g_value_set_object (value, src->file);
241 GST_OBJECT_UNLOCK (GST_OBJECT (src));
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
250 gst_gio_src_query (GstBaseSrc * base_src, GstQuery * query)
253 GstGioSrc *src = GST_GIO_SRC (base_src);
255 switch (GST_QUERY_TYPE (query)) {
256 case GST_QUERY_SCHEDULING:
259 GstSchedulingFlags flags;
262 if (src->file == NULL)
265 scheme = g_file_get_uri_scheme (src->file);
269 if (strcmp (scheme, "file") == 0) {
270 GST_LOG_OBJECT (src, "local URI, assuming random access is possible");
271 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
272 } else if (strcmp (scheme, "http") == 0 || strcmp (scheme, "https") == 0) {
273 GST_LOG_OBJECT (src, "blacklisted protocol '%s', "
274 "no random access possible", scheme);
276 GST_LOG_OBJECT (src, "unhandled protocol '%s', asking parent", scheme);
281 gst_query_set_scheduling (query, flags, 1, -1, 0);
282 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
283 if (flags & GST_SCHEDULING_FLAG_SEEKABLE)
284 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
291 res = GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_SRC_CLASS,
292 query, (base_src, query), FALSE);
299 static GInputStream *
300 gst_gio_src_get_stream (GstGioBaseSrc * bsrc)
302 GstGioSrc *src = GST_GIO_SRC (bsrc);
304 GInputStream *stream;
305 GCancellable *cancel = bsrc->cancel;
308 if (src->file == NULL) {
309 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
310 ("No location or GFile given"));
314 uri = g_file_get_uri (src->file);
316 uri = g_strdup ("(null)");
318 stream = G_INPUT_STREAM (g_file_read (src->file, cancel, &err));
320 if (stream == NULL && !gst_gio_error (src, "g_file_read", &err, NULL)) {
321 if (GST_GIO_ERROR_MATCHES (err, NOT_FOUND)) {
322 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
323 ("Could not open location %s for reading: %s", uri, err->message));
324 } else if (GST_GIO_ERROR_MATCHES (err, NOT_MOUNTED)) {
325 gst_element_post_message (GST_ELEMENT_CAST (src),
326 gst_message_new_element (GST_OBJECT_CAST (src),
327 gst_structure_new ("not-mounted", "file", G_TYPE_FILE, src->file,
328 "uri", G_TYPE_STRING, uri, NULL)));
330 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
331 ("Location %s not mounted: %s", uri, err->message));
333 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
334 ("Could not open location %s for reading: %s", uri, err->message));
338 g_clear_error (&err);
340 } else if (stream == NULL) {
345 GST_DEBUG_OBJECT (src, "opened location %s", uri);