implement translatable error messages using gerror.
[platform/upstream/gstreamer.git] / gst / elements / gstfilesink.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstfilesink.c: 
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27 #include "gst/gst_private.h"
28
29 #include <gst/gst.h>
30 #include <errno.h>
31 #include "gstfilesink.h"
32 #include <string.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36
37 GST_DEBUG_CATEGORY (gst_filesink_debug);
38 #define GST_CAT_DEFAULT gst_filesink_debug
39
40 GstElementDetails gst_filesink_details = {
41   "File Sink",
42   "Sink/File",
43   "LGPL",
44   "Write stream to a file",
45   VERSION,
46   "Thomas <thomas@apestaart.org>",
47   "(C) 2001"
48 };
49
50
51 /* FileSink signals and args */
52 enum {
53   /* FILL ME */
54   SIGNAL_HANDOFF,
55   LAST_SIGNAL
56 };
57
58 enum {
59   ARG_0,
60   ARG_LOCATION
61 };
62
63 GST_PAD_QUERY_TYPE_FUNCTION (gst_filesink_get_query_types,
64   GST_QUERY_TOTAL,
65   GST_QUERY_POSITION
66 )
67
68 GST_PAD_FORMATS_FUNCTION (gst_filesink_get_formats,
69   GST_FORMAT_BYTES
70 )
71
72
73 static void     gst_filesink_class_init         (GstFileSinkClass *klass);
74 static void     gst_filesink_init               (GstFileSink *filesink);
75
76 static void     gst_filesink_set_property       (GObject *object, guint prop_id, 
77                                                  const GValue *value, GParamSpec *pspec);
78 static void     gst_filesink_get_property       (GObject *object, guint prop_id, 
79                                                  GValue *value, GParamSpec *pspec);
80
81 static gboolean gst_filesink_open_file          (GstFileSink *sink);
82 static void     gst_filesink_close_file         (GstFileSink *sink);
83
84 static const GstEventMask *
85                 gst_filesink_get_event_mask     (GstPad *pad);
86
87 static gboolean gst_filesink_handle_event       (GstPad *pad, GstEvent *event);
88 static gboolean gst_filesink_pad_query          (GstPad *pad, GstQueryType type,
89                                                  GstFormat *format, gint64 *value);
90 static void     gst_filesink_chain              (GstPad *pad,GstBuffer *buf);
91
92 static GstElementStateReturn gst_filesink_change_state (GstElement *element);
93
94 static GstElementClass *parent_class = NULL;
95 static guint gst_filesink_signals[LAST_SIGNAL] = { 0 };
96
97 GType
98 gst_filesink_get_type (void) 
99 {
100   static GType filesink_type = 0;
101
102   if (!filesink_type) {
103     static const GTypeInfo filesink_info = {
104       sizeof(GstFileSinkClass),      NULL,
105       NULL,
106       (GClassInitFunc)gst_filesink_class_init,
107       NULL,
108       NULL,
109       sizeof(GstFileSink),
110       0,
111       (GInstanceInitFunc)gst_filesink_init,
112     };
113     filesink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFileSink", &filesink_info, 0);
114   }
115   return filesink_type;
116 }
117
118 static void
119 gst_filesink_class_init (GstFileSinkClass *klass) 
120 {
121   GObjectClass *gobject_class;
122   GstElementClass *gstelement_class;
123
124   gobject_class = (GObjectClass*)klass;
125   gstelement_class = (GstElementClass*)klass;
126
127   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
128
129   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATION,
130     g_param_spec_string ("location", "File Location", "Location of the file to write",
131                          NULL, G_PARAM_READWRITE));
132
133   gst_filesink_signals[SIGNAL_HANDOFF] =
134     g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
135                     G_STRUCT_OFFSET (GstFileSinkClass, handoff), NULL, NULL,
136                     g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
137
138   gobject_class->set_property = gst_filesink_set_property;
139   gobject_class->get_property = gst_filesink_get_property;
140
141   gstelement_class->change_state = gst_filesink_change_state;
142 }
143
144 static void 
145 gst_filesink_init (GstFileSink *filesink) 
146 {
147   GstPad *pad;
148
149   pad = gst_pad_new ("sink", GST_PAD_SINK);
150   gst_element_add_pad (GST_ELEMENT (filesink), pad);
151   gst_pad_set_chain_function (pad, gst_filesink_chain);
152
153   GST_FLAG_SET (GST_ELEMENT(filesink), GST_ELEMENT_EVENT_AWARE);
154   gst_pad_set_event_function(pad, gst_filesink_handle_event);
155   gst_pad_set_event_mask_function(pad, gst_filesink_get_event_mask);
156
157   gst_pad_set_query_function (pad, gst_filesink_pad_query);
158   gst_pad_set_query_type_function (pad, gst_filesink_get_query_types);
159   gst_pad_set_formats_function (pad, gst_filesink_get_formats);
160
161   filesink->filename = NULL;
162   filesink->file = NULL;
163 }
164
165 static void
166 gst_filesink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
167 {
168   GstFileSink *sink;
169
170   /* it's not null if we got it, but it might not be ours */
171   sink = GST_FILESINK (object);
172
173   switch (prop_id) {
174     case ARG_LOCATION:
175       /* the element must be stopped or paused in order to do this */
176       g_return_if_fail (GST_STATE (sink) <= GST_STATE_PAUSED);
177       if (GST_STATE (sink) == GST_STATE_PAUSED)
178         g_return_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
179
180       if (sink->filename)
181         g_free (sink->filename);
182       sink->filename = g_strdup (g_value_get_string (value));
183       if (GST_STATE (sink) == GST_STATE_PAUSED)
184         gst_filesink_open_file (sink);   
185       break;
186     default:
187       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188       break;
189   }
190 }
191
192 static void   
193 gst_filesink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
194 {
195   GstFileSink *sink;
196  
197   /* it's not null if we got it, but it might not be ours */
198   g_return_if_fail (GST_IS_FILESINK (object));
199  
200   sink = GST_FILESINK (object);
201   
202   switch (prop_id) {
203     case ARG_LOCATION:
204       g_value_set_string (value, sink->filename);
205       break;
206     default:
207       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
208       break;
209   }
210 }
211
212 static gboolean
213 gst_filesink_open_file (GstFileSink *sink)
214 {
215   g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN), FALSE);
216
217   /* open the file */
218   if (!sink->filename)
219   {
220     /* Out of files */
221     return FALSE;
222   }
223
224   sink->file = fopen (sink->filename, "w");
225   if (sink->file == NULL) {
226     gst_element_gerror (GST_ELEMENT (sink), GST_ERROR_DEVICE,
227                         g_strdup_printf (_("Could not open file \"%s\""), sink->filename),
228                         g_strdup_printf ("Error opening file \"%s\": %s", sink->filename, g_strerror(errno)));
229     return FALSE;
230   } 
231
232   GST_FLAG_SET (sink, GST_FILESINK_OPEN);
233
234   sink->data_written = 0;
235
236   return TRUE;
237 }
238
239 static void
240 gst_filesink_close_file (GstFileSink *sink)
241 {
242   g_return_if_fail (GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
243
244   if (fclose (sink->file) != 0)
245   {
246     gst_element_gerror (GST_ELEMENT (sink), GST_ERROR_DEVICE,
247                         g_strdup_printf (_("Could not open file \"%s\""), sink->filename),
248                         g_strdup_printf ("Error closing file \"%s\": %s", sink->filename, g_strerror(errno)));
249   }
250   else {
251     GST_FLAG_UNSET (sink, GST_FILESINK_OPEN);
252   }
253 }
254
255 static gboolean
256 gst_filesink_pad_query (GstPad *pad, GstQueryType type,
257                         GstFormat *format, gint64 *value)
258 {
259   GstFileSink *sink = GST_FILESINK (GST_PAD_PARENT (pad));
260
261   switch (type) {
262     case GST_QUERY_TOTAL:
263       switch (*format) {
264         case GST_FORMAT_BYTES:
265           if (GST_FLAG_IS_SET (GST_ELEMENT(sink), GST_FILESINK_OPEN)) {
266             *value = sink->data_written; /* FIXME - doesn't the kernel provide
267                                             such a function? */
268             break;
269           }
270         default:
271           return FALSE;
272       }
273       break;
274     case GST_QUERY_POSITION:
275       switch (*format) {
276         case GST_FORMAT_BYTES:
277           if (GST_FLAG_IS_SET (GST_ELEMENT(sink), GST_FILESINK_OPEN)) {
278             *value = ftell (sink->file);
279             break;
280           }
281         default:
282           return FALSE;
283       }
284       break;
285     default:
286       return FALSE;
287   }
288
289   return TRUE;
290 }
291
292 /* supported events */
293 static const GstEventMask *
294 gst_filesink_get_event_mask (GstPad *pad)
295 {
296   GstFileSink *filesink = GST_FILESINK (gst_pad_get_parent (pad));
297   struct stat filestat;
298   static const GstEventMask seek_masks[] = {
299     { GST_EVENT_SEEK, GST_SEEK_METHOD_CUR |
300                       GST_SEEK_METHOD_SET |
301                       GST_SEEK_METHOD_END |
302                       GST_SEEK_FLAG_FLUSH },
303     { GST_EVENT_FLUSH, 0 },
304     { GST_EVENT_DISCONTINUOUS, 0 },
305     { 0, 0 }
306   }, noseek_masks[] = {
307     { GST_EVENT_FLUSH, 0 },
308     { 0, 0 }
309   }, *selected = seek_masks;
310
311   if (filesink->file != NULL) {
312     if (fstat (fileno (filesink->file), &filestat) == 0) {
313       if (S_ISFIFO (filestat.st_mode) ||
314           S_ISSOCK (filestat.st_mode)) {
315         selected = noseek_masks;
316       }
317     }
318   }
319
320   return selected;
321 }
322
323 /* handle events (search) */
324 static gboolean
325 gst_filesink_handle_event (GstPad *pad, GstEvent *event)
326 {
327   GstEventType type;
328   GstFileSink *filesink;
329
330   filesink = GST_FILESINK (gst_pad_get_parent (pad));
331
332   g_return_val_if_fail (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN),
333                         FALSE);
334
335   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
336
337   switch (type) {
338     case GST_EVENT_SEEK:
339       g_return_val_if_fail (GST_EVENT_SEEK_FORMAT (event) == GST_FORMAT_BYTES,
340                             FALSE);
341
342       if (GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH)
343         if (fflush (filesink->file))
344           gst_element_gerror (GST_ELEMENT (filesink), GST_ERROR_DEVICE,
345                               g_strdup_printf (_("Could not write to file \"%s\""), filesink->filename),
346                               g_strdup_printf ("Error flushing file \"%s\": %s",
347                                                filesink->filename, g_strerror(errno)));
348
349       switch (GST_EVENT_SEEK_METHOD(event))
350       {
351         case GST_SEEK_METHOD_SET:
352           fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_SET);
353           break;
354         case GST_SEEK_METHOD_CUR:
355           fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_CUR);
356           break;
357         case GST_SEEK_METHOD_END:
358           fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_END);
359           break;
360         default:
361           g_warning("unkown seek method!\n");
362           break;
363       }
364       break;
365     case GST_EVENT_DISCONTINUOUS:
366     {
367       gint64 offset;
368       
369       if (gst_event_discont_get_value (event, GST_FORMAT_BYTES, &offset))
370         fseek (filesink->file, offset, SEEK_SET);
371
372       gst_event_unref (event);
373       break;
374     }
375     case GST_EVENT_FLUSH:
376       if (fflush (filesink->file)) {
377         gst_element_gerror (GST_ELEMENT (filesink), GST_ERROR_DEVICE,
378                             g_strdup_printf (_("Could not write to file \"%s\""), filesink->filename),
379                             g_strdup_printf ("Error flushing file \"%s\": %s",
380                                              filesink->filename, g_strerror(errno)));
381       }
382       break;
383     case GST_EVENT_EOS:
384       gst_filesink_close_file (filesink);
385       gst_element_set_eos (GST_ELEMENT (filesink));
386       break;
387     default:
388       gst_pad_event_default (pad, event);
389       break;
390   }
391
392   return TRUE;
393 }
394
395 /**
396  * gst_filesink_chain:
397  * @pad: the pad this filesink is connected to
398  * @buf: the buffer that has to be absorbed
399  *
400  * take the buffer from the pad and write to file if it's open
401  */
402 static void 
403 gst_filesink_chain (GstPad *pad, GstBuffer *buf) 
404 {
405   GstFileSink *filesink;
406
407   g_return_if_fail (pad != NULL);
408   g_return_if_fail (GST_IS_PAD (pad));
409   g_return_if_fail (buf != NULL);
410
411   filesink = GST_FILESINK (gst_pad_get_parent (pad));
412
413   if (GST_IS_EVENT(buf))
414   {
415     gst_filesink_handle_event(pad, GST_EVENT(buf));
416     return;
417   }
418
419   if (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN))
420   {
421     guint bytes_written = 0, back_pending = 0;
422     if (ftell(filesink->file) < filesink->data_written)
423       back_pending = filesink->data_written - ftell(filesink->file);
424     while (bytes_written < GST_BUFFER_SIZE (buf)) {
425       size_t wrote = fwrite (GST_BUFFER_DATA (buf) + bytes_written, 1,
426                              GST_BUFFER_SIZE (buf) - bytes_written,
427                              filesink->file);
428       if (wrote <= 0) {
429         gst_element_gerror (GST_ELEMENT (filesink), GST_ERROR_DEVICE,
430                             g_strdup_printf (_("Could not write to file \"%s\""), filesink->filename),
431                             g_strdup_printf ("Only %d of %d bytes written: %s", bytes_written,
432                             GST_BUFFER_SIZE (buf), strerror (errno)));
433         break;
434       }
435       bytes_written += wrote;
436     }
437
438     filesink->data_written += bytes_written - back_pending;
439   }
440
441   gst_buffer_unref (buf);
442
443   g_signal_emit (G_OBJECT (filesink),
444                  gst_filesink_signals[SIGNAL_HANDOFF], 0,
445                  filesink);
446 }
447
448 static GstElementStateReturn
449 gst_filesink_change_state (GstElement *element)
450 {
451   g_return_val_if_fail (GST_IS_FILESINK (element), GST_STATE_FAILURE);
452
453   switch (GST_STATE_TRANSITION (element)) {
454     case GST_STATE_PAUSED_TO_READY:
455       if (GST_FLAG_IS_SET (element, GST_FILESINK_OPEN))
456         gst_filesink_close_file (GST_FILESINK (element));
457       break;
458
459     case GST_STATE_READY_TO_PAUSED:
460       if (!GST_FLAG_IS_SET (element, GST_FILESINK_OPEN)) {
461         if (!gst_filesink_open_file (GST_FILESINK (element)))
462           return GST_STATE_FAILURE;
463       }
464       break;
465   }
466
467   if (GST_ELEMENT_CLASS (parent_class)->change_state)
468     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
469
470   return GST_STATE_SUCCESS;
471 }