Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / ext / gio / gstgio.c
1 /* GStreamer
2  *
3  * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
4  * Copyright (C) 2007 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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "gstgio.h"
27 #include "gstgiosink.h"
28 #include "gstgiosrc.h"
29 #include "gstgiostreamsink.h"
30 #include "gstgiostreamsrc.h"
31
32 #include <string.h>
33
34 GST_DEBUG_CATEGORY_STATIC (gst_gio_debug);
35 #define GST_CAT_DEFAULT gst_gio_debug
36
37 /* @func_name: Name of the GIO function, for debugging messages.
38  * @err: Error location.  *err may be NULL, but err must be non-NULL.
39  * @ret: Flow return location.  May be NULL.  Is set to either #GST_FLOW_ERROR
40  * or #GST_FLOW_WRONG_STATE.
41  *
42  * Returns: TRUE to indicate a handled error.  Error at given location err will
43  * be freed and *err will be set to NULL.  A FALSE return indicates an unhandled
44  * error: The err location is unchanged and guaranteed to be != NULL.  ret, if
45  * given, is set to GST_FLOW_ERROR.
46  */
47 gboolean
48 gst_gio_error (gpointer element, const gchar * func_name, GError ** err,
49     GstFlowReturn * ret)
50 {
51   gboolean handled = TRUE;
52
53   if (ret)
54     *ret = GST_FLOW_ERROR;
55
56   if (GST_GIO_ERROR_MATCHES (*err, CANCELLED)) {
57     GST_DEBUG_OBJECT (element, "blocking I/O call cancelled (%s)", func_name);
58     if (ret)
59       *ret = GST_FLOW_WRONG_STATE;
60   } else if (*err != NULL) {
61     handled = FALSE;
62   } else {
63     GST_ELEMENT_ERROR (element, LIBRARY, FAILED, (NULL),
64         ("%s call failed without error set", func_name));
65   }
66
67   if (handled)
68     g_clear_error (err);
69
70   return handled;
71 }
72
73 GstFlowReturn
74 gst_gio_seek (gpointer element, GSeekable * stream, guint64 offset,
75     GCancellable * cancel)
76 {
77   gboolean success;
78   GstFlowReturn ret;
79   GError *err = NULL;
80
81   GST_LOG_OBJECT (element, "seeking to offset %" G_GINT64_FORMAT, offset);
82
83   success = g_seekable_seek (stream, offset, G_SEEK_SET, cancel, &err);
84
85   if (success)
86     ret = GST_FLOW_OK;
87   else if (!gst_gio_error (element, "g_seekable_seek", &err, &ret)) {
88     GST_ELEMENT_ERROR (element, RESOURCE, SEEK, (NULL),
89         ("Could not seek: %s", err->message));
90     g_clear_error (&err);
91   }
92
93   return ret;
94 }
95
96 static gpointer
97 _internal_get_supported_protocols (gpointer data)
98 {
99   const gchar *const *schemes;
100   gchar **our_schemes;
101   guint num;
102   gint i, j;
103
104   schemes = g_vfs_get_supported_uri_schemes (g_vfs_get_default ());
105   num = g_strv_length ((gchar **) schemes);
106
107   if (num == 0) {
108     GST_WARNING ("No GIO supported URI schemes found");
109     return NULL;
110   }
111
112   our_schemes = g_new0 (gchar *, num + 1);
113
114   /* - Filter http/https as we can't support the icy stuff with GIO.
115    *   Use souphttpsrc if you need that.
116    * - Filter cdda as it doesn't support musicbrainz stuff and everything
117    *   else one expects from a cdda source. Use cdparanoiasrc or cdiosrc
118    *   for cdda.
119    */
120   for (i = 0, j = 0; i < num; i++) {
121     if (strcmp (schemes[i], "http") == 0 || strcmp (schemes[i], "https") == 0
122         || strcmp (schemes[i], "cdda") == 0)
123       continue;
124
125     our_schemes[j] = g_strdup (schemes[i]);
126     j++;
127   }
128
129   return our_schemes;
130 }
131
132 static gchar **
133 gst_gio_get_supported_protocols (void)
134 {
135   static GOnce once = G_ONCE_INIT;
136
137   g_once (&once, _internal_get_supported_protocols, NULL);
138   return (gchar **) once.retval;
139 }
140
141 static GstURIType
142 gst_gio_uri_handler_get_type_sink (void)
143 {
144   return GST_URI_SINK;
145 }
146
147 static GstURIType
148 gst_gio_uri_handler_get_type_src (void)
149 {
150   return GST_URI_SRC;
151 }
152
153 static gchar **
154 gst_gio_uri_handler_get_protocols (void)
155 {
156   static gchar **protocols = NULL;
157
158   if (!protocols)
159     protocols = gst_gio_get_supported_protocols ();
160
161   return protocols;
162 }
163
164 static const gchar *
165 gst_gio_uri_handler_get_uri (GstURIHandler * handler)
166 {
167   GstElement *element = GST_ELEMENT (handler);
168   const gchar *uri;
169
170   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
171
172   g_object_get (G_OBJECT (element), "location", &uri, NULL);
173
174   return uri;
175 }
176
177 static gboolean
178 gst_gio_uri_handler_set_uri (GstURIHandler * handler, const gchar * uri)
179 {
180   GstElement *element = GST_ELEMENT (handler);
181
182   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
183
184   if (GST_STATE (element) == GST_STATE_PLAYING ||
185       GST_STATE (element) == GST_STATE_PAUSED)
186     return FALSE;
187
188   g_object_set (G_OBJECT (element), "location", uri, NULL);
189
190   return TRUE;
191 }
192
193 static void
194 gst_gio_uri_handler_init (gpointer g_iface, gpointer iface_data)
195 {
196   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
197   gboolean sink = GPOINTER_TO_INT (iface_data); /* See in do_init below. */
198
199   if (sink)
200     iface->get_type = gst_gio_uri_handler_get_type_sink;
201   else
202     iface->get_type = gst_gio_uri_handler_get_type_src;
203   iface->get_protocols = gst_gio_uri_handler_get_protocols;
204   iface->get_uri = gst_gio_uri_handler_get_uri;
205   iface->set_uri = gst_gio_uri_handler_set_uri;
206 }
207
208 void
209 gst_gio_uri_handler_do_init (GType type)
210 {
211   GInterfaceInfo uri_handler_info = {
212     gst_gio_uri_handler_init,
213     NULL,
214     NULL
215   };
216
217   /* Store information for uri_handler_init to use for distinguishing the
218    * element types.  This lets us use a single interface implementation for both
219    * classes. */
220   uri_handler_info.interface_data = GINT_TO_POINTER (g_type_is_a (type,
221           GST_TYPE_BASE_SINK));
222
223   g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &uri_handler_info);
224 }
225
226 static gboolean
227 plugin_init (GstPlugin * plugin)
228 {
229   gboolean ret = TRUE;
230
231   GST_DEBUG_CATEGORY_INIT (gst_gio_debug, "gio", 0, "GIO elements");
232
233   gst_plugin_add_dependency_simple (plugin, NULL, GIO_MODULE_DIR, NULL,
234       GST_PLUGIN_DEPENDENCY_FLAG_NONE);
235   gst_plugin_add_dependency_simple (plugin, "LD_LIBRARY_PATH", GIO_LIBDIR,
236       "gvfsd", GST_PLUGIN_DEPENDENCY_FLAG_NONE);
237
238   /* FIXME: Rank is MARGINAL for now, should be at least SECONDARY+1 in the future
239    * to replace gnomevfssink/src. For testing purposes PRIMARY+1 one makes sense
240    * so it gets autoplugged and preferred over filesrc/sink. */
241
242   ret &= gst_element_register (plugin, "giosink", GST_RANK_SECONDARY,
243       GST_TYPE_GIO_SINK);
244
245   ret &= gst_element_register (plugin, "giosrc", GST_RANK_SECONDARY,
246       GST_TYPE_GIO_SRC);
247
248   ret &= gst_element_register (plugin, "giostreamsink", GST_RANK_NONE,
249       GST_TYPE_GIO_STREAM_SINK);
250
251   ret &= gst_element_register (plugin, "giostreamsrc", GST_RANK_NONE,
252       GST_TYPE_GIO_STREAM_SRC);
253
254   return ret;
255 }
256
257 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR, "gio",
258     "GIO elements", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
259     GST_PACKAGE_ORIGIN)