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