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