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