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