d0a24d9c8c6c209ad6ec16c949dbfbdf4a2f0761
[platform/upstream/gstreamer.git] / gst / gio / gstgiosrc.c
1 /* GStreamer
2  *
3  * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
4  * Copyright (C) 2007-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
5  * 
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.
10  *
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.
15  *
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.
20  */
21
22 /**
23  * SECTION:element-giosrc
24  * @see_also: #GstFileSrc, #GstGnomeVFSSrc, #GstGioSink
25  *
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',
29  * 'ftp', or 'smb'.
30  *
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.
42  *
43  * <refsect2>
44  * <title>Example launch lines</title>
45  * |[
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.
50  * |[
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.
54  * |[
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.
58  * </refsect2>
59  */
60
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!
65  *
66  *        We would also like to do authentication while mounting.
67  */
68
69 #ifdef HAVE_CONFIG_H
70 #include <config.h>
71 #endif
72
73 #include "gstgiosrc.h"
74 #include <string.h>
75
76 GST_DEBUG_CATEGORY_STATIC (gst_gio_src_debug);
77 #define GST_CAT_DEFAULT gst_gio_src_debug
78
79 enum
80 {
81   PROP_0,
82   PROP_LOCATION,
83   PROP_FILE
84 };
85
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));
89
90 static void gst_gio_src_finalize (GObject * object);
91
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);
96
97 static GInputStream *gst_gio_src_get_stream (GstGioBaseSrc * bsrc);
98
99 static gboolean gst_gio_src_query (GstBaseSrc * base_src, GstQuery * query);
100
101 static void
102 gst_gio_src_class_init (GstGioSrcClass * klass)
103 {
104   GObjectClass *gobject_class = (GObjectClass *) klass;
105   GstElementClass *gstelement_class = (GstElementClass *) klass;
106   GstBaseSrcClass *gstbasesrc_class = (GstBaseSrcClass *) klass;
107   GstGioBaseSrcClass *gstgiobasesrc_class = (GstGioBaseSrcClass *) klass;
108
109   GST_DEBUG_CATEGORY_INIT (gst_gio_src_debug, "gio_src", 0, "GIO source");
110
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;
114
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));
118
119   /**
120    * GstGioSrc:file
121    * 
122    * %GFile to read from.
123    * 
124    * Since: 0.10.20
125    **/
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));
129
130   gst_element_class_set_static_metadata (gstelement_class, "GIO source",
131       "Source/File",
132       "Read from any GIO-supported location",
133       "Ren\xc3\xa9 Stadler <mail@renestadler.de>, "
134       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
135
136   gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_gio_src_query);
137
138   gstgiobasesrc_class->get_stream = GST_DEBUG_FUNCPTR (gst_gio_src_get_stream);
139   gstgiobasesrc_class->close_on_stop = TRUE;
140 }
141
142 static void
143 gst_gio_src_init (GstGioSrc * src)
144 {
145 }
146
147 static void
148 gst_gio_src_finalize (GObject * object)
149 {
150   GstGioSrc *src = GST_GIO_SRC (object);
151
152   if (src->file) {
153     g_object_unref (src->file);
154     src->file = NULL;
155   }
156
157   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
158 }
159
160 static void
161 gst_gio_src_set_property (GObject * object, guint prop_id,
162     const GValue * value, GParamSpec * pspec)
163 {
164   GstGioSrc *src = GST_GIO_SRC (object);
165
166   switch (prop_id) {
167     case PROP_LOCATION:{
168       const gchar *uri = NULL;
169
170       if (GST_STATE (src) == GST_STATE_PLAYING ||
171           GST_STATE (src) == GST_STATE_PAUSED) {
172         GST_WARNING
173             ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
174         break;
175       }
176
177       GST_OBJECT_LOCK (GST_OBJECT (src));
178       if (src->file)
179         g_object_unref (src->file);
180
181       uri = g_value_get_string (value);
182
183       if (uri) {
184         src->file = g_file_new_for_uri (uri);
185
186         if (!src->file) {
187           GST_ERROR ("Could not create GFile for URI '%s'", uri);
188         }
189       } else {
190         src->file = NULL;
191       }
192       GST_OBJECT_UNLOCK (GST_OBJECT (src));
193       break;
194     }
195     case PROP_FILE:
196       if (GST_STATE (src) == GST_STATE_PLAYING ||
197           GST_STATE (src) == GST_STATE_PAUSED) {
198         GST_WARNING
199             ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
200         break;
201       }
202
203       GST_OBJECT_LOCK (GST_OBJECT (src));
204       if (src->file)
205         g_object_unref (src->file);
206
207       src->file = g_value_dup_object (value);
208
209       GST_OBJECT_UNLOCK (GST_OBJECT (src));
210       break;
211     default:
212       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213       break;
214   }
215 }
216
217 static void
218 gst_gio_src_get_property (GObject * object, guint prop_id,
219     GValue * value, GParamSpec * pspec)
220 {
221   GstGioSrc *src = GST_GIO_SRC (object);
222
223   switch (prop_id) {
224     case PROP_LOCATION:{
225       gchar *uri;
226
227       GST_OBJECT_LOCK (GST_OBJECT (src));
228       if (src->file) {
229         uri = g_file_get_uri (src->file);
230         g_value_set_string (value, uri);
231         g_free (uri);
232       } else {
233         g_value_set_string (value, NULL);
234       }
235       GST_OBJECT_UNLOCK (GST_OBJECT (src));
236       break;
237     }
238     case PROP_FILE:
239       GST_OBJECT_LOCK (GST_OBJECT (src));
240       g_value_set_object (value, src->file);
241       GST_OBJECT_UNLOCK (GST_OBJECT (src));
242       break;
243     default:
244       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245       break;
246   }
247 }
248
249 static gboolean
250 gst_gio_src_query (GstBaseSrc * base_src, GstQuery * query)
251 {
252   gboolean res;
253   GstGioSrc *src = GST_GIO_SRC (base_src);
254
255   switch (GST_QUERY_TYPE (query)) {
256     case GST_QUERY_SCHEDULING:
257     {
258       gchar *scheme;
259       GstSchedulingFlags flags;
260
261       flags = 0;
262       if (src->file == NULL)
263         goto forward_parent;
264
265       scheme = g_file_get_uri_scheme (src->file);
266       if (scheme == NULL)
267         goto forward_parent;
268
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);
275       } else {
276         GST_LOG_OBJECT (src, "unhandled protocol '%s', asking parent", scheme);
277         goto forward_parent;
278       }
279       g_free (scheme);
280
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);
285
286       res = TRUE;
287       break;
288     }
289     default:
290     forward_parent:
291       res = GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_SRC_CLASS,
292           query, (base_src, query), FALSE);
293       break;
294   }
295
296   return res;
297 }
298
299 static GInputStream *
300 gst_gio_src_get_stream (GstGioBaseSrc * bsrc)
301 {
302   GstGioSrc *src = GST_GIO_SRC (bsrc);
303   GError *err = NULL;
304   GInputStream *stream;
305   GCancellable *cancel = bsrc->cancel;
306   gchar *uri = NULL;
307
308   if (src->file == NULL) {
309     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
310         ("No location or GFile given"));
311     return NULL;
312   }
313
314   uri = g_file_get_uri (src->file);
315   if (!uri)
316     uri = g_strdup ("(null)");
317
318   stream = G_INPUT_STREAM (g_file_read (src->file, cancel, &err));
319
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)));
329
330       GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
331           ("Location %s not mounted: %s", uri, err->message));
332     } else {
333       GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
334           ("Could not open location %s for reading: %s", uri, err->message));
335     }
336
337     g_free (uri);
338     g_clear_error (&err);
339     return NULL;
340   } else if (stream == NULL) {
341     g_free (uri);
342     return NULL;
343   }
344
345   GST_DEBUG_OBJECT (src, "opened location %s", uri);
346   g_free (uri);
347
348   return stream;
349 }