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 GST_BOILERPLATE_FULL (GstGioSink, gst_gio_sink, GstGioBaseSink,
96 GST_TYPE_GIO_BASE_SINK, gst_gio_uri_handler_do_init);
98 static void gst_gio_sink_finalize (GObject * object);
99 static void gst_gio_sink_set_property (GObject * object, guint prop_id,
100 const GValue * value, GParamSpec * pspec);
101 static void gst_gio_sink_get_property (GObject * object, guint prop_id,
102 GValue * value, GParamSpec * pspec);
103 static GOutputStream *gst_gio_sink_get_stream (GstGioBaseSink * base_sink);
106 gst_gio_sink_base_init (gpointer gclass)
108 GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
110 GST_DEBUG_CATEGORY_INIT (gst_gio_sink_debug, "gio_sink", 0, "GIO sink");
112 gst_element_class_set_details_simple (element_class, "GIO sink",
114 "Write to any GIO-supported location",
115 "Ren\xc3\xa9 Stadler <mail@renestadler.de>, "
116 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
120 gst_gio_sink_class_init (GstGioSinkClass * klass)
122 GObjectClass *gobject_class = (GObjectClass *) klass;
123 GstGioBaseSinkClass *gstgiobasesink_class = (GstGioBaseSinkClass *) klass;
125 gobject_class->finalize = gst_gio_sink_finalize;
126 gobject_class->set_property = gst_gio_sink_set_property;
127 gobject_class->get_property = gst_gio_sink_get_property;
129 g_object_class_install_property (gobject_class, PROP_LOCATION,
130 g_param_spec_string ("location", "Location", "URI location to write to",
131 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
136 * %GFile to write to.
140 g_object_class_install_property (gobject_class, PROP_FILE,
141 g_param_spec_object ("file", "File", "GFile to write to",
142 G_TYPE_FILE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
144 gstgiobasesink_class->get_stream =
145 GST_DEBUG_FUNCPTR (gst_gio_sink_get_stream);
146 gstgiobasesink_class->close_on_stop = TRUE;
150 gst_gio_sink_init (GstGioSink * sink, GstGioSinkClass * gclass)
155 gst_gio_sink_finalize (GObject * object)
157 GstGioSink *sink = GST_GIO_SINK (object);
160 g_object_unref (sink->file);
164 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
168 gst_gio_sink_set_property (GObject * object, guint prop_id,
169 const GValue * value, GParamSpec * pspec)
171 GstGioSink *sink = GST_GIO_SINK (object);
175 const gchar *uri = NULL;
177 if (GST_STATE (sink) == GST_STATE_PLAYING ||
178 GST_STATE (sink) == GST_STATE_PAUSED) {
180 ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
184 GST_OBJECT_LOCK (GST_OBJECT (sink));
186 g_object_unref (sink->file);
188 uri = g_value_get_string (value);
191 sink->file = g_file_new_for_uri (uri);
194 GST_ERROR ("Could not create GFile for URI '%s'", uri);
199 GST_OBJECT_UNLOCK (GST_OBJECT (sink));
203 if (GST_STATE (sink) == GST_STATE_PLAYING ||
204 GST_STATE (sink) == GST_STATE_PAUSED) {
206 ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
210 GST_OBJECT_LOCK (GST_OBJECT (sink));
212 g_object_unref (sink->file);
214 sink->file = g_value_dup_object (value);
216 GST_OBJECT_UNLOCK (GST_OBJECT (sink));
219 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
225 gst_gio_sink_get_property (GObject * object, guint prop_id,
226 GValue * value, GParamSpec * pspec)
228 GstGioSink *sink = GST_GIO_SINK (object);
234 GST_OBJECT_LOCK (GST_OBJECT (sink));
236 uri = g_file_get_uri (sink->file);
237 g_value_set_string (value, uri);
240 g_value_set_string (value, NULL);
242 GST_OBJECT_UNLOCK (GST_OBJECT (sink));
246 GST_OBJECT_LOCK (GST_OBJECT (sink));
247 g_value_set_object (value, sink->file);
248 GST_OBJECT_UNLOCK (GST_OBJECT (sink));
251 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256 static GOutputStream *
257 gst_gio_sink_get_stream (GstGioBaseSink * bsink)
259 GstGioSink *sink = GST_GIO_SINK (bsink);
260 GOutputStream *stream;
261 GCancellable *cancel = GST_GIO_BASE_SINK (sink)->cancel;
265 if (sink->file == NULL) {
266 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
267 ("No location or GFile given"));
271 uri = g_file_get_uri (sink->file);
273 uri = g_strdup ("(null)");
276 G_OUTPUT_STREAM (g_file_create (sink->file, G_FILE_CREATE_NONE, cancel,
280 if (!gst_gio_error (sink, "g_file_create", &err, NULL)) {
281 /*if (GST_GIO_ERROR_MATCHES (err, EXISTS)) */
282 /* FIXME: Retry with replace if overwrite == TRUE! */
284 if (GST_GIO_ERROR_MATCHES (err, NOT_FOUND)) {
285 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, (NULL),
286 ("Could not open location %s for writing: %s", uri, err->message));
287 } else if (GST_GIO_ERROR_MATCHES (err, EXISTS)) {
288 gst_element_post_message (GST_ELEMENT_CAST (sink),
289 gst_message_new_element (GST_OBJECT_CAST (sink),
290 gst_structure_new ("file-exists", "file", G_TYPE_FILE,
291 sink->file, "uri", G_TYPE_STRING, uri, NULL)));
293 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
294 ("Location %s already exists: %s", uri, err->message));
295 } else if (GST_GIO_ERROR_MATCHES (err, NOT_MOUNTED)) {
296 gst_element_post_message (GST_ELEMENT_CAST (sink),
297 gst_message_new_element (GST_OBJECT_CAST (sink),
298 gst_structure_new ("not-mounted", "file", G_TYPE_FILE,
299 sink->file, "uri", G_TYPE_STRING, uri, NULL)));
301 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
302 ("Location %s not mounted: %s", uri, err->message));
304 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
305 ("Could not open location %s for writing: %s", uri, err->message));
308 g_clear_error (&err);
314 GST_DEBUG_OBJECT (sink, "opened location %s", uri);