tizen 2.3 release
[framework/multimedia/gst-plugins-base0.10.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 GST_BOILERPLATE_FULL (GstGioSink, gst_gio_sink, GstGioBaseSink,
96     GST_TYPE_GIO_BASE_SINK, gst_gio_uri_handler_do_init);
97
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);
104
105 static void
106 gst_gio_sink_base_init (gpointer gclass)
107 {
108   GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
109
110   GST_DEBUG_CATEGORY_INIT (gst_gio_sink_debug, "gio_sink", 0, "GIO sink");
111
112   gst_element_class_set_details_simple (element_class, "GIO sink",
113       "Sink/File",
114       "Write to any GIO-supported location",
115       "Ren\xc3\xa9 Stadler <mail@renestadler.de>, "
116       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
117 }
118
119 static void
120 gst_gio_sink_class_init (GstGioSinkClass * klass)
121 {
122   GObjectClass *gobject_class = (GObjectClass *) klass;
123   GstGioBaseSinkClass *gstgiobasesink_class = (GstGioBaseSinkClass *) klass;
124
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;
128
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));
132
133   /**
134    * GstGioSink:file
135    *
136    * %GFile to write to.
137    * 
138    * Since: 0.10.20
139    **/
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));
143
144   gstgiobasesink_class->get_stream =
145       GST_DEBUG_FUNCPTR (gst_gio_sink_get_stream);
146   gstgiobasesink_class->close_on_stop = TRUE;
147 }
148
149 static void
150 gst_gio_sink_init (GstGioSink * sink, GstGioSinkClass * gclass)
151 {
152 }
153
154 static void
155 gst_gio_sink_finalize (GObject * object)
156 {
157   GstGioSink *sink = GST_GIO_SINK (object);
158
159   if (sink->file) {
160     g_object_unref (sink->file);
161     sink->file = NULL;
162   }
163
164   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
165 }
166
167 static void
168 gst_gio_sink_set_property (GObject * object, guint prop_id,
169     const GValue * value, GParamSpec * pspec)
170 {
171   GstGioSink *sink = GST_GIO_SINK (object);
172
173   switch (prop_id) {
174     case PROP_LOCATION:{
175       const gchar *uri = NULL;
176
177       if (GST_STATE (sink) == GST_STATE_PLAYING ||
178           GST_STATE (sink) == GST_STATE_PAUSED) {
179         GST_WARNING
180             ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
181         break;
182       }
183
184       GST_OBJECT_LOCK (GST_OBJECT (sink));
185       if (sink->file)
186         g_object_unref (sink->file);
187
188       uri = g_value_get_string (value);
189
190       if (uri) {
191         sink->file = g_file_new_for_uri (uri);
192
193         if (!sink->file) {
194           GST_ERROR ("Could not create GFile for URI '%s'", uri);
195         }
196       } else {
197         sink->file = NULL;
198       }
199       GST_OBJECT_UNLOCK (GST_OBJECT (sink));
200       break;
201     }
202     case PROP_FILE:
203       if (GST_STATE (sink) == GST_STATE_PLAYING ||
204           GST_STATE (sink) == GST_STATE_PAUSED) {
205         GST_WARNING
206             ("Setting a new location or GFile not supported in PLAYING or PAUSED state");
207         break;
208       }
209
210       GST_OBJECT_LOCK (GST_OBJECT (sink));
211       if (sink->file)
212         g_object_unref (sink->file);
213
214       sink->file = g_value_dup_object (value);
215
216       GST_OBJECT_UNLOCK (GST_OBJECT (sink));
217       break;
218     default:
219       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220       break;
221   }
222 }
223
224 static void
225 gst_gio_sink_get_property (GObject * object, guint prop_id,
226     GValue * value, GParamSpec * pspec)
227 {
228   GstGioSink *sink = GST_GIO_SINK (object);
229
230   switch (prop_id) {
231     case PROP_LOCATION:{
232       gchar *uri;
233
234       GST_OBJECT_LOCK (GST_OBJECT (sink));
235       if (sink->file) {
236         uri = g_file_get_uri (sink->file);
237         g_value_set_string (value, uri);
238         g_free (uri);
239       } else {
240         g_value_set_string (value, NULL);
241       }
242       GST_OBJECT_UNLOCK (GST_OBJECT (sink));
243       break;
244     }
245     case PROP_FILE:
246       GST_OBJECT_LOCK (GST_OBJECT (sink));
247       g_value_set_object (value, sink->file);
248       GST_OBJECT_UNLOCK (GST_OBJECT (sink));
249       break;
250     default:
251       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252       break;
253   }
254 }
255
256 static GOutputStream *
257 gst_gio_sink_get_stream (GstGioBaseSink * bsink)
258 {
259   GstGioSink *sink = GST_GIO_SINK (bsink);
260   GOutputStream *stream;
261   GCancellable *cancel = GST_GIO_BASE_SINK (sink)->cancel;
262   GError *err = NULL;
263   gchar *uri;
264
265   if (sink->file == NULL) {
266     GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
267         ("No location or GFile given"));
268     return NULL;
269   }
270
271   uri = g_file_get_uri (sink->file);
272   if (!uri)
273     uri = g_strdup ("(null)");
274
275   stream =
276       G_OUTPUT_STREAM (g_file_create (sink->file, G_FILE_CREATE_NONE, cancel,
277           &err));
278
279   if (!stream) {
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! */
283
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)));
292
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)));
300
301         GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
302             ("Location %s not mounted: %s", uri, err->message));
303       } else {
304         GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
305             ("Could not open location %s for writing: %s", uri, err->message));
306       }
307
308       g_clear_error (&err);
309     }
310     g_free (uri);
311     return NULL;
312   }
313
314   GST_DEBUG_OBJECT (sink, "opened location %s", uri);
315
316   g_free (uri);
317
318   return stream;
319 }