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