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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, 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_FLUSHING.
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
gst_gio_error(gpointer element,const gchar * func_name,GError ** err,GstFlowReturn * ret)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_FLUSHING;
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
gst_gio_seek(gpointer element,GSeekable * stream,guint64 offset,GCancellable * cancel)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
_internal_get_supported_protocols(gpointer data)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
106 if (schemes != NULL)
107 num = g_strv_length ((gchar **) schemes);
108 else
109 num = 0;
110
111 if (num == 0) {
112 GST_WARNING ("No GIO supported URI schemes found");
113 return NULL;
114 }
115
116 our_schemes = g_new0 (gchar *, num + 1);
117
118 /* - Filter http/https as we can't support the icy stuff with GIO.
119 * Use souphttpsrc if you need that.
120 * - Filter cdda as it doesn't support musicbrainz stuff and everything
121 * else one expects from a cdda source. Use cdparanoiasrc or cdiosrc
122 * for cdda.
123 */
124 for (i = 0, j = 0; i < num; i++) {
125 if (strcmp (schemes[i], "http") == 0 || strcmp (schemes[i], "https") == 0
126 || strcmp (schemes[i], "cdda") == 0)
127 continue;
128
129 our_schemes[j] = g_strdup (schemes[i]);
130 j++;
131 }
132
133 return our_schemes;
134 }
135
136 static gchar **
gst_gio_get_supported_protocols(void)137 gst_gio_get_supported_protocols (void)
138 {
139 static GOnce once = G_ONCE_INIT;
140
141 g_once (&once, _internal_get_supported_protocols, NULL);
142 return (gchar **) once.retval;
143 }
144
145 static GstURIType
gst_gio_uri_handler_get_type_sink(GType type)146 gst_gio_uri_handler_get_type_sink (GType type)
147 {
148 return GST_URI_SINK;
149 }
150
151 static GstURIType
gst_gio_uri_handler_get_type_src(GType type)152 gst_gio_uri_handler_get_type_src (GType type)
153 {
154 return GST_URI_SRC;
155 }
156
157 static const gchar *const *
gst_gio_uri_handler_get_protocols(GType type)158 gst_gio_uri_handler_get_protocols (GType type)
159 {
160 static const gchar *const *protocols = NULL;
161
162 if (!protocols)
163 protocols = (const gchar * const *) gst_gio_get_supported_protocols ();
164
165 return protocols;
166 }
167
168 static gchar *
gst_gio_uri_handler_get_uri(GstURIHandler * handler)169 gst_gio_uri_handler_get_uri (GstURIHandler * handler)
170 {
171 GstElement *element = GST_ELEMENT (handler);
172 gchar *uri;
173
174 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
175
176 g_object_get (G_OBJECT (element), "location", &uri, NULL);
177
178 return uri;
179 }
180
181 static gboolean
gst_gio_uri_handler_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)182 gst_gio_uri_handler_set_uri (GstURIHandler * handler, const gchar * uri,
183 GError ** error)
184 {
185 GstElement *element = GST_ELEMENT (handler);
186
187 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
188
189 if (GST_STATE (element) == GST_STATE_PLAYING ||
190 GST_STATE (element) == GST_STATE_PAUSED) {
191 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
192 "Changing the 'location' property while the element is running is "
193 "not supported");
194 return FALSE;
195 }
196
197 g_object_set (G_OBJECT (element), "location", uri, NULL);
198 return TRUE;
199 }
200
201 static void
gst_gio_uri_handler_init(gpointer g_iface,gpointer iface_data)202 gst_gio_uri_handler_init (gpointer g_iface, gpointer iface_data)
203 {
204 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
205 gboolean sink = GPOINTER_TO_INT (iface_data); /* See in do_init below. */
206
207 if (sink)
208 iface->get_type = gst_gio_uri_handler_get_type_sink;
209 else
210 iface->get_type = gst_gio_uri_handler_get_type_src;
211 iface->get_protocols = gst_gio_uri_handler_get_protocols;
212 iface->get_uri = gst_gio_uri_handler_get_uri;
213 iface->set_uri = gst_gio_uri_handler_set_uri;
214 }
215
216 void
gst_gio_uri_handler_do_init(GType type)217 gst_gio_uri_handler_do_init (GType type)
218 {
219 GInterfaceInfo uri_handler_info = {
220 gst_gio_uri_handler_init,
221 NULL,
222 NULL
223 };
224
225 /* Store information for uri_handler_init to use for distinguishing the
226 * element types. This lets us use a single interface implementation for both
227 * classes. */
228 uri_handler_info.interface_data = GINT_TO_POINTER (g_type_is_a (type,
229 GST_TYPE_BASE_SINK));
230
231 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &uri_handler_info);
232 }
233
234 #define GIO_GVFS_MOUNTS_DIR GIO_PREFIX \
235 G_DIR_SEPARATOR_S "share" \
236 G_DIR_SEPARATOR_S "gvfs" \
237 G_DIR_SEPARATOR_S "mounts"
238
239 static gboolean
plugin_init(GstPlugin * plugin)240 plugin_init (GstPlugin * plugin)
241 {
242 gboolean ret = TRUE;
243
244 GST_DEBUG_CATEGORY_INIT (gst_gio_debug, "gio", 0, "GIO elements");
245
246 gst_plugin_add_dependency_simple (plugin, NULL, GIO_MODULE_DIR, NULL,
247 GST_PLUGIN_DEPENDENCY_FLAG_NONE);
248 gst_plugin_add_dependency_simple (plugin, NULL, GIO_GVFS_MOUNTS_DIR, NULL,
249 GST_PLUGIN_DEPENDENCY_FLAG_NONE);
250
251 /* FIXME: Rank is MARGINAL for now, should be at least SECONDARY+1 in the future
252 * to replace gnomevfssink/src. For testing purposes PRIMARY+1 one makes sense
253 * so it gets autoplugged and preferred over filesrc/sink. */
254
255 ret &= gst_element_register (plugin, "giosink", GST_RANK_SECONDARY,
256 GST_TYPE_GIO_SINK);
257
258 ret &= gst_element_register (plugin, "giosrc", GST_RANK_SECONDARY,
259 GST_TYPE_GIO_SRC);
260
261 ret &= gst_element_register (plugin, "giostreamsink", GST_RANK_NONE,
262 GST_TYPE_GIO_STREAM_SINK);
263
264 ret &= gst_element_register (plugin, "giostreamsrc", GST_RANK_NONE,
265 GST_TYPE_GIO_STREAM_SRC);
266
267 return ret;
268 }
269
270 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR, gio,
271 "GIO elements", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
272 GST_PACKAGE_ORIGIN)
273