Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-base.git] / ext / gio / gstgiosink.c
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:element-giosink
24  * @see_also: #GstFileSink, #GstGnomeVFSSink, #GstGioSrc
25  *
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',
29  * or 'smb'.
30  *
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
41  * resolved.
42  *
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.
47  * 
48  * <refsect2>
49  * <title>Example pipelines</title>
50  * |[
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.
54  * |[
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.
58  * |[
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.
62  * </refsect2>
63  */
64
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!
69  *
70  *        We would also like to do authentication while mounting.
71  */
72
73 #ifdef HAVE_CONFIG_H
74 #include <config.h>
75 #endif
76
77 #include "gstgiosink.h"
78
79 GST_DEBUG_CATEGORY_STATIC (gst_gio_sink_debug);
80 #define GST_CAT_DEFAULT gst_gio_sink_debug
81
82 /* Filter signals and args */
83 enum
84 {
85   LAST_SIGNAL
86 };
87
88 enum
89 {
90   PROP_0,
91   PROP_LOCATION,
92   PROP_FILE
93 };
94
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));
98
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);
105
106 static void
107 gst_gio_sink_class_init (GstGioSinkClass * klass)
108 {
109   GObjectClass *gobject_class = (GObjectClass *) klass;
110   GstElementClass *gstelement_class = (GstElementClass *) klass;
111   GstGioBaseSinkClass *gstgiobasesink_class = (GstGioBaseSinkClass *) klass;
112
113   GST_DEBUG_CATEGORY_INIT (gst_gio_sink_debug, "gio_sink", 0, "GIO sink");
114
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;
118
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));
122
123   /**
124    * GstGioSink:file
125    *
126    * %GFile to write to.
127    * 
128    * Since: 0.10.20
129    **/
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));
133
134   gst_element_class_set_details_simple (gstelement_class, "GIO sink",
135       "Sink/File",
136       "Write to any GIO-supported location",
137       "Ren\xc3\xa9 Stadler <mail@renestadler.de>, "
138       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
139
140   gstgiobasesink_class->get_stream =
141       GST_DEBUG_FUNCPTR (gst_gio_sink_get_stream);
142   gstgiobasesink_class->close_on_stop = TRUE;
143 }
144
145 static void
146 gst_gio_sink_init (GstGioSink * sink)
147 {
148 }
149
150 static void
151 gst_gio_sink_finalize (GObject * object)
152 {
153   GstGioSink *sink = GST_GIO_SINK (object);
154
155   if (sink->file) {
156     g_object_unref (sink->file);
157     sink->file = NULL;
158   }
159
160   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
161 }
162
163 static void
164 gst_gio_sink_set_property (GObject * object, guint prop_id,
165     const GValue * value, GParamSpec * pspec)
166 {
167   GstGioSink *sink = GST_GIO_SINK (object);
168
169   switch (prop_id) {
170     case PROP_LOCATION:{
171       const gchar *uri = NULL;
172
173       if (GST_STATE (sink) == GST_STATE_PLAYING ||
174           GST_STATE (sink) == GST_STATE_PAUSED) {
175         GST_WARNING
176             ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
177         break;
178       }
179
180       GST_OBJECT_LOCK (GST_OBJECT (sink));
181       if (sink->file)
182         g_object_unref (sink->file);
183
184       uri = g_value_get_string (value);
185
186       if (uri) {
187         sink->file = g_file_new_for_uri (uri);
188
189         if (!sink->file) {
190           GST_ERROR ("Could not create GFile for URI '%s'", uri);
191         }
192       } else {
193         sink->file = NULL;
194       }
195       GST_OBJECT_UNLOCK (GST_OBJECT (sink));
196       break;
197     }
198     case PROP_FILE:
199       if (GST_STATE (sink) == GST_STATE_PLAYING ||
200           GST_STATE (sink) == GST_STATE_PAUSED) {
201         GST_WARNING
202             ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
203         break;
204       }
205
206       GST_OBJECT_LOCK (GST_OBJECT (sink));
207       if (sink->file)
208         g_object_unref (sink->file);
209
210       sink->file = g_value_dup_object (value);
211
212       GST_OBJECT_UNLOCK (GST_OBJECT (sink));
213       break;
214     default:
215       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216       break;
217   }
218 }
219
220 static void
221 gst_gio_sink_get_property (GObject * object, guint prop_id,
222     GValue * value, GParamSpec * pspec)
223 {
224   GstGioSink *sink = GST_GIO_SINK (object);
225
226   switch (prop_id) {
227     case PROP_LOCATION:{
228       gchar *uri;
229
230       GST_OBJECT_LOCK (GST_OBJECT (sink));
231       if (sink->file) {
232         uri = g_file_get_uri (sink->file);
233         g_value_set_string (value, uri);
234         g_free (uri);
235       } else {
236         g_value_set_string (value, NULL);
237       }
238       GST_OBJECT_UNLOCK (GST_OBJECT (sink));
239       break;
240     }
241     case PROP_FILE:
242       GST_OBJECT_LOCK (GST_OBJECT (sink));
243       g_value_set_object (value, sink->file);
244       GST_OBJECT_UNLOCK (GST_OBJECT (sink));
245       break;
246     default:
247       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
248       break;
249   }
250 }
251
252 static GOutputStream *
253 gst_gio_sink_get_stream (GstGioBaseSink * bsink)
254 {
255   GstGioSink *sink = GST_GIO_SINK (bsink);
256   GOutputStream *stream;
257   GCancellable *cancel = GST_GIO_BASE_SINK (sink)->cancel;
258   GError *err = NULL;
259   gchar *uri;
260
261   if (sink->file == NULL) {
262     GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
263         ("No location or GFile given"));
264     return NULL;
265   }
266
267   uri = g_file_get_uri (sink->file);
268   if (!uri)
269     uri = g_strdup ("(null)");
270
271   stream =
272       G_OUTPUT_STREAM (g_file_create (sink->file, G_FILE_CREATE_NONE, cancel,
273           &err));
274
275   if (!stream) {
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! */
279
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)));
288
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)));
296
297         GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
298             ("Location %s not mounted: %s", uri, err->message));
299       } else {
300         GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
301             ("Could not open location %s for writing: %s", uri, err->message));
302       }
303
304       g_clear_error (&err);
305     }
306     g_free (uri);
307     return NULL;
308   }
309
310   GST_DEBUG_OBJECT (sink, "opened location %s", uri);
311
312   g_free (uri);
313
314   return stream;
315 }