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