3 * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
4 * Copyright (C) 2007-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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.
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.
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.
23 * SECTION:element-giosink
24 * @see_also: #GstFileSink, #GstGnomeVFSSink, #GstGioSrc
26 * This plugin writes incoming data to a local or remote location specified
27 * by an URI. This location can be specified using any protocol supported by
28 * the GIO library or it's VFS backends. Common protocols are 'file', 'ftp',
31 * If the URI or #GFile already exists giosink will post a message of
32 * type %GST_MESSAGE_ELEMENT with name "file-exists" on the bus. The message
33 * also contains the #GFile and the corresponding URI.
34 * Applications can use the "file-exists" message to notify the user about
35 * the problem and to set a different target location or to remove the
36 * existing file. Note that right after the "file-exists" message a normal
37 * error message is posted on the bus which should be ignored if "file-exists"
38 * is handled by the application, for example by calling
39 * gst_bus_set_flushing(bus, TRUE) after the "file-exists" message was
40 * received and gst_bus_set_flushing(bus, FALSE) after the problem is
43 * Similar to the "file-exist" message a "not-mounted" message is posted
44 * on the bus if the target location is not mounted yet and needs to be
45 * mounted. This message can be used by application to mount the location
46 * and retry after the location was mounted successfully.
49 * <title>Example pipelines</title>
51 * gst-launch -v filesrc location=input.xyz ! giosink location=file:///home/joe/out.xyz
52 * ]| The above pipeline will simply copy a local file. Instead of giosink,
53 * we could just as well have used the filesink element here.
55 * gst-launch -v filesrc location=foo.mp3 ! mad ! flacenc ! giosink location=smb://othercomputer/foo.flac
56 * ]| The above pipeline will re-encode an mp3 file into FLAC format and store
57 * it on a remote host using the Samba protocol.
59 * gst-launch -v audiotestsrc num-buffers=100 ! vorbisenc ! oggmux ! giosink location=file:///home/foo/bar.ogg
60 * ]| The above pipeline will encode a 440Hz sine wave to Ogg Vorbis and stores
61 * it in the home directory of user foo.
65 /* FIXME: We would like to mount the enclosing volume of an URL
66 * if it isn't mounted yet but this is possible async-only.
67 * Unfortunately this requires a running main loop from the
68 * default context and we can't guarantuee this!
70 * We would also like to do authentication while mounting.
77 #include "gstgiosink.h"
79 GST_DEBUG_CATEGORY_STATIC (gst_gio_sink_debug);
80 #define GST_CAT_DEFAULT gst_gio_sink_debug
82 /* Filter signals and args */
95 #define gst_gio_sink_parent_class parent_class
96 G_DEFINE_TYPE_WITH_CODE (GstGioSink, gst_gio_sink, GST_TYPE_GIO_BASE_SINK,
97 gst_gio_uri_handler_do_init (g_define_type_id));
99 static void gst_gio_sink_finalize (GObject * object);
100 static void gst_gio_sink_set_property (GObject * object, guint prop_id,
101 const GValue * value, GParamSpec * pspec);
102 static void gst_gio_sink_get_property (GObject * object, guint prop_id,
103 GValue * value, GParamSpec * pspec);
104 static GOutputStream *gst_gio_sink_get_stream (GstGioBaseSink * base_sink);
107 gst_gio_sink_class_init (GstGioSinkClass * klass)
109 GObjectClass *gobject_class = (GObjectClass *) klass;
110 GstElementClass *gstelement_class = (GstElementClass *) klass;
111 GstGioBaseSinkClass *gstgiobasesink_class = (GstGioBaseSinkClass *) klass;
113 GST_DEBUG_CATEGORY_INIT (gst_gio_sink_debug, "gio_sink", 0, "GIO sink");
115 gobject_class->finalize = gst_gio_sink_finalize;
116 gobject_class->set_property = gst_gio_sink_set_property;
117 gobject_class->get_property = gst_gio_sink_get_property;
119 g_object_class_install_property (gobject_class, PROP_LOCATION,
120 g_param_spec_string ("location", "Location", "URI location to write to",
121 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
126 * %GFile to write to.
130 g_object_class_install_property (gobject_class, PROP_FILE,
131 g_param_spec_object ("file", "File", "GFile to write to",
132 G_TYPE_FILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
134 gst_element_class_set_static_metadata (gstelement_class, "GIO sink",
136 "Write to any GIO-supported location",
137 "Ren\xc3\xa9 Stadler <mail@renestadler.de>, "
138 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
140 gstgiobasesink_class->get_stream =
141 GST_DEBUG_FUNCPTR (gst_gio_sink_get_stream);
142 gstgiobasesink_class->close_on_stop = TRUE;
146 gst_gio_sink_init (GstGioSink * sink)
151 gst_gio_sink_finalize (GObject * object)
153 GstGioSink *sink = GST_GIO_SINK (object);
156 g_object_unref (sink->file);
160 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
164 gst_gio_sink_set_property (GObject * object, guint prop_id,
165 const GValue * value, GParamSpec * pspec)
167 GstGioSink *sink = GST_GIO_SINK (object);
171 const gchar *uri = NULL;
173 if (GST_STATE (sink) == GST_STATE_PLAYING ||
174 GST_STATE (sink) == GST_STATE_PAUSED) {
176 ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
180 GST_OBJECT_LOCK (GST_OBJECT (sink));
182 g_object_unref (sink->file);
184 uri = g_value_get_string (value);
187 sink->file = g_file_new_for_uri (uri);
190 GST_ERROR ("Could not create GFile for URI '%s'", uri);
195 GST_OBJECT_UNLOCK (GST_OBJECT (sink));
199 if (GST_STATE (sink) == GST_STATE_PLAYING ||
200 GST_STATE (sink) == GST_STATE_PAUSED) {
202 ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
206 GST_OBJECT_LOCK (GST_OBJECT (sink));
208 g_object_unref (sink->file);
210 sink->file = g_value_dup_object (value);
212 GST_OBJECT_UNLOCK (GST_OBJECT (sink));
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221 gst_gio_sink_get_property (GObject * object, guint prop_id,
222 GValue * value, GParamSpec * pspec)
224 GstGioSink *sink = GST_GIO_SINK (object);
230 GST_OBJECT_LOCK (GST_OBJECT (sink));
232 uri = g_file_get_uri (sink->file);
233 g_value_set_string (value, uri);
236 g_value_set_string (value, NULL);
238 GST_OBJECT_UNLOCK (GST_OBJECT (sink));
242 GST_OBJECT_LOCK (GST_OBJECT (sink));
243 g_value_set_object (value, sink->file);
244 GST_OBJECT_UNLOCK (GST_OBJECT (sink));
247 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252 static GOutputStream *
253 gst_gio_sink_get_stream (GstGioBaseSink * bsink)
255 GstGioSink *sink = GST_GIO_SINK (bsink);
256 GOutputStream *stream;
257 GCancellable *cancel = GST_GIO_BASE_SINK (sink)->cancel;
261 if (sink->file == NULL) {
262 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
263 ("No location or GFile given"));
267 uri = g_file_get_uri (sink->file);
269 uri = g_strdup ("(null)");
272 G_OUTPUT_STREAM (g_file_create (sink->file, G_FILE_CREATE_NONE, cancel,
276 if (!gst_gio_error (sink, "g_file_create", &err, NULL)) {
277 /*if (GST_GIO_ERROR_MATCHES (err, EXISTS)) */
278 /* FIXME: Retry with replace if overwrite == TRUE! */
280 if (GST_GIO_ERROR_MATCHES (err, NOT_FOUND)) {
281 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, (NULL),
282 ("Could not open location %s for writing: %s", uri, err->message));
283 } else if (GST_GIO_ERROR_MATCHES (err, EXISTS)) {
284 gst_element_post_message (GST_ELEMENT_CAST (sink),
285 gst_message_new_element (GST_OBJECT_CAST (sink),
286 gst_structure_new ("file-exists", "file", G_TYPE_FILE,
287 sink->file, "uri", G_TYPE_STRING, uri, NULL)));
289 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
290 ("Location %s already exists: %s", uri, err->message));
291 } else if (GST_GIO_ERROR_MATCHES (err, NOT_MOUNTED)) {
292 gst_element_post_message (GST_ELEMENT_CAST (sink),
293 gst_message_new_element (GST_OBJECT_CAST (sink),
294 gst_structure_new ("not-mounted", "file", G_TYPE_FILE,
295 sink->file, "uri", G_TYPE_STRING, uri, NULL)));
297 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
298 ("Location %s not mounted: %s", uri, err->message));
300 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
301 ("Could not open location %s for writing: %s", uri, err->message));
304 g_clear_error (&err);
310 GST_DEBUG_OBJECT (sink, "opened location %s", uri);