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 #include "gstgioelements.h"
82
83 GST_DEBUG_CATEGORY_STATIC (gst_gio_sink_debug);
84 #define GST_CAT_DEFAULT gst_gio_sink_debug
85
86 /* Filter signals and args */
87 enum
88 {
89 LAST_SIGNAL
90 };
91
92 enum
93 {
94 PROP_0,
95 PROP_LOCATION,
96 PROP_FILE
97 };
98
99 #define gst_gio_sink_parent_class parent_class
100 G_DEFINE_TYPE_WITH_CODE (GstGioSink, gst_gio_sink, GST_TYPE_GIO_BASE_SINK,
101 gst_gio_uri_handler_do_init (g_define_type_id));
102 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (giosink, "giosink",
103 GST_RANK_SECONDARY, GST_TYPE_GIO_SINK, gio_element_init (plugin));
104
105
106 static void gst_gio_sink_finalize (GObject * object);
107 static void gst_gio_sink_set_property (GObject * object, guint prop_id,
108 const GValue * value, GParamSpec * pspec);
109 static void gst_gio_sink_get_property (GObject * object, guint prop_id,
110 GValue * value, GParamSpec * pspec);
111 static GOutputStream *gst_gio_sink_get_stream (GstGioBaseSink * base_sink);
112
113 static void
gst_gio_sink_class_init(GstGioSinkClass * klass)114 gst_gio_sink_class_init (GstGioSinkClass * klass)
115 {
116 GObjectClass *gobject_class = (GObjectClass *) klass;
117 GstElementClass *gstelement_class = (GstElementClass *) klass;
118 GstGioBaseSinkClass *gstgiobasesink_class = (GstGioBaseSinkClass *) klass;
119
120 GST_DEBUG_CATEGORY_INIT (gst_gio_sink_debug, "gio_sink", 0, "GIO sink");
121
122 gobject_class->finalize = gst_gio_sink_finalize;
123 gobject_class->set_property = gst_gio_sink_set_property;
124 gobject_class->get_property = gst_gio_sink_get_property;
125
126 g_object_class_install_property (gobject_class, PROP_LOCATION,
127 g_param_spec_string ("location", "Location", "URI location to write to",
128 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
129
130 /**
131 * GstGioSink:file:
132 *
133 * %GFile to write to.
134 */
135 g_object_class_install_property (gobject_class, PROP_FILE,
136 g_param_spec_object ("file", "File", "GFile to write to",
137 G_TYPE_FILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
138
139 gst_element_class_set_static_metadata (gstelement_class, "GIO sink",
140 "Sink/File",
141 "Write to any GIO-supported location",
142 "Ren\xc3\xa9 Stadler <mail@renestadler.de>, "
143 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
144
145 gstgiobasesink_class->get_stream =
146 GST_DEBUG_FUNCPTR (gst_gio_sink_get_stream);
147 gstgiobasesink_class->close_on_stop = TRUE;
148 }
149
150 static void
gst_gio_sink_init(GstGioSink * sink)151 gst_gio_sink_init (GstGioSink * sink)
152 {
153 }
154
155 static void
gst_gio_sink_finalize(GObject * object)156 gst_gio_sink_finalize (GObject * object)
157 {
158 GstGioSink *sink = GST_GIO_SINK (object);
159
160 if (sink->file) {
161 g_object_unref (sink->file);
162 sink->file = NULL;
163 }
164
165 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
166 }
167
168 static void
gst_gio_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)169 gst_gio_sink_set_property (GObject * object, guint prop_id,
170 const GValue * value, GParamSpec * pspec)
171 {
172 GstGioSink *sink = GST_GIO_SINK (object);
173
174 switch (prop_id) {
175 case PROP_LOCATION:{
176 const gchar *uri = NULL;
177
178 if (GST_STATE (sink) == GST_STATE_PLAYING ||
179 GST_STATE (sink) == GST_STATE_PAUSED) {
180 GST_WARNING
181 ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
182 break;
183 }
184
185 GST_OBJECT_LOCK (GST_OBJECT (sink));
186 if (sink->file)
187 g_object_unref (sink->file);
188
189 uri = g_value_get_string (value);
190
191 if (uri) {
192 sink->file = g_file_new_for_uri (uri);
193
194 if (!sink->file) {
195 GST_ERROR ("Could not create GFile for URI '%s'", uri);
196 }
197 } else {
198 sink->file = NULL;
199 }
200 GST_OBJECT_UNLOCK (GST_OBJECT (sink));
201 break;
202 }
203 case PROP_FILE:
204 if (GST_STATE (sink) == GST_STATE_PLAYING ||
205 GST_STATE (sink) == GST_STATE_PAUSED) {
206 GST_WARNING
207 ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
208 break;
209 }
210
211 GST_OBJECT_LOCK (GST_OBJECT (sink));
212 if (sink->file)
213 g_object_unref (sink->file);
214
215 sink->file = g_value_dup_object (value);
216
217 GST_OBJECT_UNLOCK (GST_OBJECT (sink));
218 break;
219 default:
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221 break;
222 }
223 }
224
225 static void
gst_gio_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)226 gst_gio_sink_get_property (GObject * object, guint prop_id,
227 GValue * value, GParamSpec * pspec)
228 {
229 GstGioSink *sink = GST_GIO_SINK (object);
230
231 switch (prop_id) {
232 case PROP_LOCATION:{
233 gchar *uri;
234
235 GST_OBJECT_LOCK (GST_OBJECT (sink));
236 if (sink->file) {
237 uri = g_file_get_uri (sink->file);
238 g_value_set_string (value, uri);
239 g_free (uri);
240 } else {
241 g_value_set_string (value, NULL);
242 }
243 GST_OBJECT_UNLOCK (GST_OBJECT (sink));
244 break;
245 }
246 case PROP_FILE:
247 GST_OBJECT_LOCK (GST_OBJECT (sink));
248 g_value_set_object (value, sink->file);
249 GST_OBJECT_UNLOCK (GST_OBJECT (sink));
250 break;
251 default:
252 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
253 break;
254 }
255 }
256
257 static GOutputStream *
gst_gio_sink_get_stream(GstGioBaseSink * bsink)258 gst_gio_sink_get_stream (GstGioBaseSink * bsink)
259 {
260 GstGioSink *sink = GST_GIO_SINK (bsink);
261 GOutputStream *stream;
262 GCancellable *cancel = GST_GIO_BASE_SINK (sink)->cancel;
263 GError *err = NULL;
264 gchar *uri;
265
266 if (sink->file == NULL) {
267 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
268 ("No location or GFile given"));
269 return NULL;
270 }
271
272 uri = g_file_get_uri (sink->file);
273 if (!uri)
274 uri = g_strdup ("(null)");
275
276 stream =
277 G_OUTPUT_STREAM (g_file_create (sink->file, G_FILE_CREATE_NONE, cancel,
278 &err));
279
280 if (!stream) {
281 if (!gst_gio_error (sink, "g_file_create", &err, NULL)) {
282 /*if (GST_GIO_ERROR_MATCHES (err, EXISTS)) */
283 /* FIXME: Retry with replace if overwrite == TRUE! */
284
285 if (GST_GIO_ERROR_MATCHES (err, NOT_FOUND)) {
286 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, (NULL),
287 ("Could not open location %s for writing: %s", uri, err->message));
288 } else if (GST_GIO_ERROR_MATCHES (err, EXISTS)) {
289 gst_element_post_message (GST_ELEMENT_CAST (sink),
290 gst_message_new_element (GST_OBJECT_CAST (sink),
291 gst_structure_new ("file-exists", "file", G_TYPE_FILE,
292 sink->file, "uri", G_TYPE_STRING, uri, NULL)));
293
294 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
295 ("Location %s already exists: %s", uri, err->message));
296 } else if (GST_GIO_ERROR_MATCHES (err, NOT_MOUNTED)) {
297 gst_element_post_message (GST_ELEMENT_CAST (sink),
298 gst_message_new_element (GST_OBJECT_CAST (sink),
299 gst_structure_new ("not-mounted", "file", G_TYPE_FILE,
300 sink->file, "uri", G_TYPE_STRING, uri, NULL)));
301
302 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
303 ("Location %s not mounted: %s", uri, err->message));
304 } else {
305 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
306 ("Could not open location %s for writing: %s", uri, err->message));
307 }
308
309 g_clear_error (&err);
310 }
311 g_free (uri);
312 return NULL;
313 }
314
315 GST_DEBUG_OBJECT (sink, "opened location %s", uri);
316
317 g_free (uri);
318
319 return stream;
320 }
321