Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / ext / 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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, 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 GST_BOILERPLATE_FULL (GstGioSrc, gst_gio_src, GstGioBaseSrc,
87     GST_TYPE_GIO_BASE_SRC, gst_gio_uri_handler_do_init);
88
89 static void gst_gio_src_finalize (GObject * object);
90
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);
95
96 static GInputStream *gst_gio_src_get_stream (GstGioBaseSrc * bsrc);
97
98 static gboolean gst_gio_src_check_get_range (GstBaseSrc * base_src);
99
100 static void
101 gst_gio_src_base_init (gpointer gclass)
102 {
103   GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
104
105   GST_DEBUG_CATEGORY_INIT (gst_gio_src_debug, "gio_src", 0, "GIO source");
106
107   gst_element_class_set_details_simple (element_class, "GIO source",
108       "Source/File",
109       "Read from any GIO-supported location",
110       "Ren\xc3\xa9 Stadler <mail@renestadler.de>, "
111       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
112 }
113
114 static void
115 gst_gio_src_class_init (GstGioSrcClass * klass)
116 {
117   GObjectClass *gobject_class = (GObjectClass *) klass;
118   GstBaseSrcClass *gstbasesrc_class = (GstBaseSrcClass *) klass;
119   GstGioBaseSrcClass *gstgiobasesrc_class = (GstGioBaseSrcClass *) klass;
120
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;
124
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));
128
129   /**
130    * GstGioSrc:file
131    * 
132    * %GFile to read from.
133    * 
134    * Since: 0.10.20
135    **/
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));
139
140   gstbasesrc_class->check_get_range =
141       GST_DEBUG_FUNCPTR (gst_gio_src_check_get_range);
142
143   gstgiobasesrc_class->get_stream = GST_DEBUG_FUNCPTR (gst_gio_src_get_stream);
144   gstgiobasesrc_class->close_on_stop = TRUE;
145 }
146
147 static void
148 gst_gio_src_init (GstGioSrc * src, GstGioSrcClass * gclass)
149 {
150 }
151
152 static void
153 gst_gio_src_finalize (GObject * object)
154 {
155   GstGioSrc *src = GST_GIO_SRC (object);
156
157   if (src->file) {
158     g_object_unref (src->file);
159     src->file = NULL;
160   }
161
162   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
163 }
164
165 static void
166 gst_gio_src_set_property (GObject * object, guint prop_id,
167     const GValue * value, GParamSpec * pspec)
168 {
169   GstGioSrc *src = GST_GIO_SRC (object);
170
171   switch (prop_id) {
172     case PROP_LOCATION:{
173       const gchar *uri = NULL;
174
175       if (GST_STATE (src) == GST_STATE_PLAYING ||
176           GST_STATE (src) == GST_STATE_PAUSED) {
177         GST_WARNING
178             ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
179         break;
180       }
181
182       GST_OBJECT_LOCK (GST_OBJECT (src));
183       if (src->file)
184         g_object_unref (src->file);
185
186       uri = g_value_get_string (value);
187
188       if (uri) {
189         src->file = g_file_new_for_uri (uri);
190
191         if (!src->file) {
192           GST_ERROR ("Could not create GFile for URI '%s'", uri);
193         }
194       } else {
195         src->file = NULL;
196       }
197       GST_OBJECT_UNLOCK (GST_OBJECT (src));
198       break;
199     }
200     case PROP_FILE:
201       if (GST_STATE (src) == GST_STATE_PLAYING ||
202           GST_STATE (src) == GST_STATE_PAUSED) {
203         GST_WARNING
204             ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
205         break;
206       }
207
208       GST_OBJECT_LOCK (GST_OBJECT (src));
209       if (src->file)
210         g_object_unref (src->file);
211
212       src->file = g_value_dup_object (value);
213
214       GST_OBJECT_UNLOCK (GST_OBJECT (src));
215       break;
216     default:
217       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218       break;
219   }
220 }
221
222 static void
223 gst_gio_src_get_property (GObject * object, guint prop_id,
224     GValue * value, GParamSpec * pspec)
225 {
226   GstGioSrc *src = GST_GIO_SRC (object);
227
228   switch (prop_id) {
229     case PROP_LOCATION:{
230       gchar *uri;
231
232       GST_OBJECT_LOCK (GST_OBJECT (src));
233       if (src->file) {
234         uri = g_file_get_uri (src->file);
235         g_value_set_string (value, uri);
236         g_free (uri);
237       } else {
238         g_value_set_string (value, NULL);
239       }
240       GST_OBJECT_UNLOCK (GST_OBJECT (src));
241       break;
242     }
243     case PROP_FILE:
244       GST_OBJECT_LOCK (GST_OBJECT (src));
245       g_value_set_object (value, src->file);
246       GST_OBJECT_UNLOCK (GST_OBJECT (src));
247       break;
248     default:
249       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
250       break;
251   }
252 }
253
254 static gboolean
255 gst_gio_src_check_get_range (GstBaseSrc * base_src)
256 {
257   GstGioSrc *src = GST_GIO_SRC (base_src);
258   gchar *scheme;
259
260   if (src->file == NULL)
261     goto done;
262
263   scheme = g_file_get_uri_scheme (src->file);
264   if (scheme == NULL)
265     goto done;
266
267   if (strcmp (scheme, "file") == 0) {
268     GST_LOG_OBJECT (src, "local URI, assuming random access is possible");
269     g_free (scheme);
270     return TRUE;
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);
274     g_free (scheme);
275     return FALSE;
276   }
277
278   g_free (scheme);
279
280 done:
281
282   GST_DEBUG_OBJECT (src, "undecided about random access, asking base class");
283
284   return GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_SRC_CLASS,
285       check_get_range, (base_src), FALSE);
286 }
287
288
289 static GInputStream *
290 gst_gio_src_get_stream (GstGioBaseSrc * bsrc)
291 {
292   GstGioSrc *src = GST_GIO_SRC (bsrc);
293   GError *err = NULL;
294   GInputStream *stream;
295   GCancellable *cancel = bsrc->cancel;
296   gchar *uri = NULL;
297
298   if (src->file == NULL) {
299     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
300         ("No location or GFile given"));
301     return NULL;
302   }
303
304   uri = g_file_get_uri (src->file);
305   if (!uri)
306     uri = g_strdup ("(null)");
307
308   stream = G_INPUT_STREAM (g_file_read (src->file, cancel, &err));
309
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)));
319
320       GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
321           ("Location %s not mounted: %s", uri, err->message));
322     } else {
323       GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
324           ("Could not open location %s for reading: %s", uri, err->message));
325     }
326
327     g_free (uri);
328     g_clear_error (&err);
329     return NULL;
330   } else if (stream == NULL) {
331     g_free (uri);
332     return NULL;
333   }
334
335   GST_DEBUG_OBJECT (src, "opened location %s", uri);
336   g_free (uri);
337
338   return stream;
339 }