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-giosink
24 * @title: giosink
25 * @see_also: #GstFileSink, #GstGnomeVFSSink, #GstGioSrc
26 *
27 * This plugin writes incoming data to a local or remote location specified
28 * by an URI. This location can be specified using any protocol supported by
29 * the GIO library or it's VFS backends. Common protocols are 'file', 'ftp',
30 * or 'smb'.
31 *
32 * If the URI or #GFile already exists giosink will post a message of
33 * type %GST_MESSAGE_ELEMENT with name "file-exists" on the bus. The message
34 * also contains the #GFile and the corresponding URI.
35 * Applications can use the "file-exists" message to notify the user about
36 * the problem and to set a different target location or to remove the
37 * existing file. Note that right after the "file-exists" message a normal
38 * error message is posted on the bus which should be ignored if "file-exists"
39 * is handled by the application, for example by calling
40 * gst_bus_set_flushing(bus, TRUE) after the "file-exists" message was
41 * received and gst_bus_set_flushing(bus, FALSE) after the problem is
42 * resolved.
43 *
44 * Similar to the "file-exist" message a "not-mounted" message is posted
45 * on the bus if the target location is not mounted yet and needs to be
46 * mounted. This message can be used by application to mount the location
47 * and retry after the location was mounted successfully.
48 *
49 * ## Example pipelines
50 * |[
51 * gst-launch-1.0 -v filesrc location=input.xyz ! giosink location=file:///home/joe/out.xyz
52 * ]|
53 * The above pipeline will simply copy a local file. Instead of giosink,
54 * we could just as well have used the filesink element here.
55 * |[
56 * gst-launch-1.0 -v uridecodebin uri=file:///path/to/audio.file ! audioconvert ! flacenc ! giosink location=smb://othercomputer/foo.flac
57 * ]|
58 * The above pipeline will re-encode an audio file into FLAC format and store
59 * it on a remote host using the Samba protocol.
60 * |[
61 * gst-launch-1.0 -v audiotestsrc num-buffers=100 ! vorbisenc ! oggmux ! giosink location=file:///home/foo/bar.ogg
62 * ]|
63 * The above pipeline will encode a 440Hz sine wave to Ogg Vorbis and stores
64 * it in the home directory of user foo.
65 *
66 */
67
68 /* FIXME: We would like to mount the enclosing volume of an URL
69 * if it isn't mounted yet but this is possible async-only.
70 * Unfortunately this requires a running main loop from the
71 * default context and we can't guarantee this!
72 *
73 * We would also like to do authentication while mounting.
74 */
75
76 #ifdef HAVE_CONFIG_H
77 #include <config.h>
78 #endif
79
80 #include "gstgiosink.h"
81
82 GST_DEBUG_CATEGORY_STATIC (gst_gio_sink_debug);
83 #define GST_CAT_DEFAULT gst_gio_sink_debug
84
85 /* Filter signals and args */
86 enum
87 {
88 LAST_SIGNAL
89 };
90
91 enum
92 {
93 PROP_0,
94 PROP_LOCATION,
95 PROP_FILE
96 };
97
98 #define gst_gio_sink_parent_class parent_class
99 G_DEFINE_TYPE_WITH_CODE (GstGioSink, gst_gio_sink, GST_TYPE_GIO_BASE_SINK,
100 gst_gio_uri_handler_do_init (g_define_type_id));
101
102 static void gst_gio_sink_finalize (GObject * object);
103 static void gst_gio_sink_set_property (GObject * object, guint prop_id,
104 const GValue * value, GParamSpec * pspec);
105 static void gst_gio_sink_get_property (GObject * object, guint prop_id,
106 GValue * value, GParamSpec * pspec);
107 static GOutputStream *gst_gio_sink_get_stream (GstGioBaseSink * base_sink);
108
109 static void
gst_gio_sink_class_init(GstGioSinkClass * klass)110 gst_gio_sink_class_init (GstGioSinkClass * klass)
111 {
112 GObjectClass *gobject_class = (GObjectClass *) klass;
113 GstElementClass *gstelement_class = (GstElementClass *) klass;
114 GstGioBaseSinkClass *gstgiobasesink_class = (GstGioBaseSinkClass *) klass;
115
116 GST_DEBUG_CATEGORY_INIT (gst_gio_sink_debug, "gio_sink", 0, "GIO sink");
117
118 gobject_class->finalize = gst_gio_sink_finalize;
119 gobject_class->set_property = gst_gio_sink_set_property;
120 gobject_class->get_property = gst_gio_sink_get_property;
121
122 g_object_class_install_property (gobject_class, PROP_LOCATION,
123 g_param_spec_string ("location", "Location", "URI location to write to",
124 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
125
126 /**
127 * GstGioSink:file:
128 *
129 * %GFile to write to.
130 */
131 g_object_class_install_property (gobject_class, PROP_FILE,
132 g_param_spec_object ("file", "File", "GFile to write to",
133 G_TYPE_FILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
134
135 gst_element_class_set_static_metadata (gstelement_class, "GIO sink",
136 "Sink/File",
137 "Write to any GIO-supported location",
138 "Ren\xc3\xa9 Stadler <mail@renestadler.de>, "
139 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
140
141 gstgiobasesink_class->get_stream =
142 GST_DEBUG_FUNCPTR (gst_gio_sink_get_stream);
143 gstgiobasesink_class->close_on_stop = TRUE;
144 }
145
146 static void
gst_gio_sink_init(GstGioSink * sink)147 gst_gio_sink_init (GstGioSink * sink)
148 {
149 }
150
151 static void
gst_gio_sink_finalize(GObject * object)152 gst_gio_sink_finalize (GObject * object)
153 {
154 GstGioSink *sink = GST_GIO_SINK (object);
155
156 if (sink->file) {
157 g_object_unref (sink->file);
158 sink->file = NULL;
159 }
160
161 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
162 }
163
164 static void
gst_gio_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)165 gst_gio_sink_set_property (GObject * object, guint prop_id,
166 const GValue * value, GParamSpec * pspec)
167 {
168 GstGioSink *sink = GST_GIO_SINK (object);
169
170 switch (prop_id) {
171 case PROP_LOCATION:{
172 const gchar *uri = NULL;
173
174 if (GST_STATE (sink) == GST_STATE_PLAYING ||
175 GST_STATE (sink) == GST_STATE_PAUSED) {
176 GST_WARNING
177 ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
178 break;
179 }
180
181 GST_OBJECT_LOCK (GST_OBJECT (sink));
182 if (sink->file)
183 g_object_unref (sink->file);
184
185 uri = g_value_get_string (value);
186
187 if (uri) {
188 sink->file = g_file_new_for_uri (uri);
189
190 if (!sink->file) {
191 GST_ERROR ("Could not create GFile for URI '%s'", uri);
192 }
193 } else {
194 sink->file = NULL;
195 }
196 GST_OBJECT_UNLOCK (GST_OBJECT (sink));
197 break;
198 }
199 case PROP_FILE:
200 if (GST_STATE (sink) == GST_STATE_PLAYING ||
201 GST_STATE (sink) == GST_STATE_PAUSED) {
202 GST_WARNING
203 ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
204 break;
205 }
206
207 GST_OBJECT_LOCK (GST_OBJECT (sink));
208 if (sink->file)
209 g_object_unref (sink->file);
210
211 sink->file = g_value_dup_object (value);
212
213 GST_OBJECT_UNLOCK (GST_OBJECT (sink));
214 break;
215 default:
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217 break;
218 }
219 }
220
221 static void
gst_gio_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)222 gst_gio_sink_get_property (GObject * object, guint prop_id,
223 GValue * value, GParamSpec * pspec)
224 {
225 GstGioSink *sink = GST_GIO_SINK (object);
226
227 switch (prop_id) {
228 case PROP_LOCATION:{
229 gchar *uri;
230
231 GST_OBJECT_LOCK (GST_OBJECT (sink));
232 if (sink->file) {
233 uri = g_file_get_uri (sink->file);
234 g_value_set_string (value, uri);
235 g_free (uri);
236 } else {
237 g_value_set_string (value, NULL);
238 }
239 GST_OBJECT_UNLOCK (GST_OBJECT (sink));
240 break;
241 }
242 case PROP_FILE:
243 GST_OBJECT_LOCK (GST_OBJECT (sink));
244 g_value_set_object (value, sink->file);
245 GST_OBJECT_UNLOCK (GST_OBJECT (sink));
246 break;
247 default:
248 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
249 break;
250 }
251 }
252
253 static GOutputStream *
gst_gio_sink_get_stream(GstGioBaseSink * bsink)254 gst_gio_sink_get_stream (GstGioBaseSink * bsink)
255 {
256 GstGioSink *sink = GST_GIO_SINK (bsink);
257 GOutputStream *stream;
258 GCancellable *cancel = GST_GIO_BASE_SINK (sink)->cancel;
259 GError *err = NULL;
260 gchar *uri;
261
262 if (sink->file == NULL) {
263 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
264 ("No location or GFile given"));
265 return NULL;
266 }
267
268 uri = g_file_get_uri (sink->file);
269 if (!uri)
270 uri = g_strdup ("(null)");
271
272 stream =
273 G_OUTPUT_STREAM (g_file_create (sink->file, G_FILE_CREATE_NONE, cancel,
274 &err));
275
276 if (!stream) {
277 if (!gst_gio_error (sink, "g_file_create", &err, NULL)) {
278 /*if (GST_GIO_ERROR_MATCHES (err, EXISTS)) */
279 /* FIXME: Retry with replace if overwrite == TRUE! */
280
281 if (GST_GIO_ERROR_MATCHES (err, NOT_FOUND)) {
282 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, (NULL),
283 ("Could not open location %s for writing: %s", uri, err->message));
284 } else if (GST_GIO_ERROR_MATCHES (err, EXISTS)) {
285 gst_element_post_message (GST_ELEMENT_CAST (sink),
286 gst_message_new_element (GST_OBJECT_CAST (sink),
287 gst_structure_new ("file-exists", "file", G_TYPE_FILE,
288 sink->file, "uri", G_TYPE_STRING, uri, NULL)));
289
290 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
291 ("Location %s already exists: %s", uri, err->message));
292 } else if (GST_GIO_ERROR_MATCHES (err, NOT_MOUNTED)) {
293 gst_element_post_message (GST_ELEMENT_CAST (sink),
294 gst_message_new_element (GST_OBJECT_CAST (sink),
295 gst_structure_new ("not-mounted", "file", G_TYPE_FILE,
296 sink->file, "uri", G_TYPE_STRING, uri, NULL)));
297
298 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
299 ("Location %s not mounted: %s", uri, err->message));
300 } else {
301 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
302 ("Could not open location %s for writing: %s", uri, err->message));
303 }
304
305 g_clear_error (&err);
306 }
307 g_free (uri);
308 return NULL;
309 }
310
311 GST_DEBUG_OBJECT (sink, "opened location %s", uri);
312
313 g_free (uri);
314
315 return stream;
316 }
317