• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Copyright (C) 2020 Huawei Technologies Co., Ltd.
6  *   @Author: Stéphane Cerveau <scerveau@collabora.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include "gstgioelements.h"
28 
29 GST_DEBUG_CATEGORY_STATIC (gst_gio_debug);
30 #define GST_CAT_DEFAULT gst_gio_debug
31 
32 /* @func_name: Name of the GIO function, for debugging messages.
33  * @err: Error location.  *err may be NULL, but err must be non-NULL.
34  * @ret: Flow return location.  May be NULL.  Is set to either #GST_FLOW_ERROR
35  * or #GST_FLOW_FLUSHING.
36  *
37  * Returns: TRUE to indicate a handled error.  Error at given location err will
38  * be freed and *err will be set to NULL.  A FALSE return indicates an unhandled
39  * error: The err location is unchanged and guaranteed to be != NULL.  ret, if
40  * given, is set to GST_FLOW_ERROR.
41  */
42 gboolean
gst_gio_error(gpointer element,const gchar * func_name,GError ** err,GstFlowReturn * ret)43 gst_gio_error (gpointer element, const gchar * func_name, GError ** err,
44     GstFlowReturn * ret)
45 {
46   gboolean handled = TRUE;
47 
48   if (ret)
49     *ret = GST_FLOW_ERROR;
50 
51   if (GST_GIO_ERROR_MATCHES (*err, CANCELLED)) {
52     GST_DEBUG_OBJECT (element, "blocking I/O call cancelled (%s)", func_name);
53     if (ret)
54       *ret = GST_FLOW_FLUSHING;
55   } else if (*err != NULL) {
56     handled = FALSE;
57   } else {
58     GST_ELEMENT_ERROR (element, LIBRARY, FAILED, (NULL),
59         ("%s call failed without error set", func_name));
60   }
61 
62   if (handled)
63     g_clear_error (err);
64 
65   return handled;
66 }
67 
68 GstFlowReturn
gst_gio_seek(gpointer element,GSeekable * stream,guint64 offset,GCancellable * cancel)69 gst_gio_seek (gpointer element, GSeekable * stream, guint64 offset,
70     GCancellable * cancel)
71 {
72   gboolean success;
73   GstFlowReturn ret;
74   GError *err = NULL;
75 
76   GST_LOG_OBJECT (element, "seeking to offset %" G_GINT64_FORMAT, offset);
77 
78   success = g_seekable_seek (stream, offset, G_SEEK_SET, cancel, &err);
79 
80   if (success)
81     ret = GST_FLOW_OK;
82   else if (!gst_gio_error (element, "g_seekable_seek", &err, &ret)) {
83     GST_ELEMENT_ERROR (element, RESOURCE, SEEK, (NULL),
84         ("Could not seek: %s", err->message));
85     g_clear_error (&err);
86   }
87 
88   return ret;
89 }
90 
91 static gpointer
_internal_get_supported_protocols(gpointer data)92 _internal_get_supported_protocols (gpointer data)
93 {
94   const gchar *const *schemes;
95   gchar **our_schemes;
96   guint num;
97   gint i, j;
98 
99   schemes = g_vfs_get_supported_uri_schemes (g_vfs_get_default ());
100 
101   if (schemes != NULL)
102     num = g_strv_length ((gchar **) schemes);
103   else
104     num = 0;
105 
106   if (num == 0) {
107     GST_WARNING ("No GIO supported URI schemes found");
108     return NULL;
109   }
110 
111   our_schemes = g_new0 (gchar *, num + 1);
112 
113   /* - Filter http/https as we can't support the icy stuff with GIO.
114    *   Use souphttpsrc if you need that.
115    * - Filter cdda as it doesn't support musicbrainz stuff and everything
116    *   else one expects from a cdda source. Use cdparanoiasrc or cdiosrc
117    *   for cdda.
118    */
119   for (i = 0, j = 0; i < num; i++) {
120     if (strcmp (schemes[i], "http") == 0 || strcmp (schemes[i], "https") == 0
121         || strcmp (schemes[i], "cdda") == 0)
122       continue;
123 
124     our_schemes[j] = g_strdup (schemes[i]);
125     j++;
126   }
127 
128   return our_schemes;
129 }
130 
131 static gchar **
gst_gio_get_supported_protocols(void)132 gst_gio_get_supported_protocols (void)
133 {
134   static GOnce once = G_ONCE_INIT;
135 
136   g_once (&once, _internal_get_supported_protocols, NULL);
137   return (gchar **) once.retval;
138 }
139 
140 static GstURIType
gst_gio_uri_handler_get_type_sink(GType type)141 gst_gio_uri_handler_get_type_sink (GType type)
142 {
143   return GST_URI_SINK;
144 }
145 
146 static GstURIType
gst_gio_uri_handler_get_type_src(GType type)147 gst_gio_uri_handler_get_type_src (GType type)
148 {
149   return GST_URI_SRC;
150 }
151 
152 static const gchar *const *
gst_gio_uri_handler_get_protocols(GType type)153 gst_gio_uri_handler_get_protocols (GType type)
154 {
155   static const gchar *const *protocols = NULL;
156 
157   if (!protocols)
158     protocols = (const gchar * const *) gst_gio_get_supported_protocols ();
159 
160   return protocols;
161 }
162 
163 static gchar *
gst_gio_uri_handler_get_uri(GstURIHandler * handler)164 gst_gio_uri_handler_get_uri (GstURIHandler * handler)
165 {
166   GstElement *element = GST_ELEMENT (handler);
167   gchar *uri;
168 
169   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
170 
171   g_object_get (G_OBJECT (element), "location", &uri, NULL);
172 
173   return uri;
174 }
175 
176 static gboolean
gst_gio_uri_handler_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)177 gst_gio_uri_handler_set_uri (GstURIHandler * handler, const gchar * uri,
178     GError ** error)
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     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
187         "Changing the 'location' property while the element is running is "
188         "not supported");
189     return FALSE;
190   }
191 
192   g_object_set (G_OBJECT (element), "location", uri, NULL);
193   return TRUE;
194 }
195 
196 static void
gst_gio_uri_handler_init(gpointer g_iface,gpointer iface_data)197 gst_gio_uri_handler_init (gpointer g_iface, gpointer iface_data)
198 {
199   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
200   gboolean sink = GPOINTER_TO_INT (iface_data); /* See in do_init below. */
201 
202   if (sink)
203     iface->get_type = gst_gio_uri_handler_get_type_sink;
204   else
205     iface->get_type = gst_gio_uri_handler_get_type_src;
206   iface->get_protocols = gst_gio_uri_handler_get_protocols;
207   iface->get_uri = gst_gio_uri_handler_get_uri;
208   iface->set_uri = gst_gio_uri_handler_set_uri;
209 }
210 
211 void
gst_gio_uri_handler_do_init(GType type)212 gst_gio_uri_handler_do_init (GType type)
213 {
214   GInterfaceInfo uri_handler_info = {
215     gst_gio_uri_handler_init,
216     NULL,
217     NULL
218   };
219 
220   /* Store information for uri_handler_init to use for distinguishing the
221    * element types.  This lets us use a single interface implementation for both
222    * classes. */
223   uri_handler_info.interface_data = GINT_TO_POINTER (g_type_is_a (type,
224           GST_TYPE_BASE_SINK));
225 
226   g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &uri_handler_info);
227 }
228 
229 #define GIO_GVFS_MOUNTS_DIR GIO_PREFIX \
230     G_DIR_SEPARATOR_S "share" \
231     G_DIR_SEPARATOR_S "gvfs" \
232     G_DIR_SEPARATOR_S "mounts"
233 
234 void
gio_element_init(GstPlugin * plugin)235 gio_element_init (GstPlugin * plugin)
236 {
237   static gsize res = FALSE;
238 
239   if (g_once_init_enter (&res)) {
240     GST_DEBUG_CATEGORY_INIT (gst_gio_debug, "gio", 0, "GIO elements");
241 
242     gst_plugin_add_dependency_simple (plugin, NULL, GIO_MODULE_DIR, NULL,
243         GST_PLUGIN_DEPENDENCY_FLAG_NONE);
244     gst_plugin_add_dependency_simple (plugin, NULL, GIO_GVFS_MOUNTS_DIR, NULL,
245         GST_PLUGIN_DEPENDENCY_FLAG_NONE);
246     g_once_init_leave (&res, TRUE);
247   }
248 }
249