Allow multiple files to be created with disksink and integrate a new event for that
[platform/upstream/gstreamer.git] / gst / elements / gstdisksink.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstdisksink.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 "gstdisksink.h"
27 #include <string.h>
28
29 GstElementDetails gst_disksink_details = {
30   "Disk Sink",
31   "Sink",
32   "Disk hole for data",
33   VERSION,
34   "Thomas <thomas@apestaart.org>",
35   "(C) 2001"
36 };
37
38
39 /* DiskSink 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_disksink_class_init         (GstDiskSinkClass *klass);
54 static void     gst_disksink_init               (GstDiskSink *disksink);
55
56 static void     gst_disksink_set_property       (GObject *object, guint prop_id, 
57                                                  const GValue *value, GParamSpec *pspec);
58 static void     gst_disksink_get_property       (GObject *object, guint prop_id, 
59                                                  GValue *value, GParamSpec *pspec);
60
61 static gboolean gst_disksink_open_file          (GstDiskSink *sink);
62 static void     gst_disksink_close_file         (GstDiskSink *sink);
63
64 static gboolean gst_disksink_handle_event       (GstPad *pad, GstEvent *event);
65 static void     gst_disksink_chain              (GstPad *pad,GstBuffer *buf);
66
67 static GstElementStateReturn gst_disksink_change_state (GstElement *element);
68
69 static GstElementClass *parent_class = NULL;
70 static guint gst_disksink_signals[LAST_SIGNAL] = { 0 };
71
72 GType
73 gst_disksink_get_type (void) 
74 {
75   static GType disksink_type = 0;
76
77   if (!disksink_type) {
78     static const GTypeInfo disksink_info = {
79       sizeof(GstDiskSinkClass),      NULL,
80       NULL,
81       (GClassInitFunc)gst_disksink_class_init,
82       NULL,
83       NULL,
84       sizeof(GstDiskSink),
85       0,
86       (GInstanceInitFunc)gst_disksink_init,
87     };
88     disksink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstDiskSink", &disksink_info, 0);
89   }
90   return disksink_type;
91 }
92
93 static void
94 gst_disksink_class_init (GstDiskSinkClass *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_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_disksink_signals[SIGNAL_HANDOFF] =
114     g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
115                     G_STRUCT_OFFSET (GstDiskSinkClass, handoff), NULL, NULL,
116                     g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
117
118   gobject_class->set_property = gst_disksink_set_property;
119   gobject_class->get_property = gst_disksink_get_property;
120
121   gstelement_class->change_state = gst_disksink_change_state;
122 }
123
124 static void 
125 gst_disksink_init (GstDiskSink *disksink) 
126 {
127   GstPad *pad;
128
129   pad = gst_pad_new ("sink", GST_PAD_SINK);
130   gst_element_add_pad (GST_ELEMENT (disksink), pad);
131   gst_pad_set_chain_function (pad, gst_disksink_chain);
132
133   GST_FLAG_SET (GST_ELEMENT(disksink), GST_ELEMENT_EVENT_AWARE);
134   gst_pad_set_event_function(pad, gst_disksink_handle_event);
135
136   disksink->filename = NULL;
137   disksink->file = NULL;
138   disksink->filenum = 0;
139
140   disksink->maxfilesize = -1;
141 }
142
143 static char *
144 gst_disksink_getcurrentfilename (GstDiskSink *disksink)
145 {
146   g_return_val_if_fail(disksink != NULL, NULL);
147   g_return_val_if_fail(GST_IS_DISKSINK(disksink), NULL);
148   g_return_val_if_fail(disksink->filename != NULL, NULL);
149   g_return_val_if_fail(disksink->filenum >= 0, NULL);
150
151   if (!strstr(disksink->filename, "%"))
152   {
153     if (!disksink->filenum)
154       return g_strdup(disksink->filename);
155     else
156       return NULL;
157   }
158
159   return g_strdup_printf(disksink->filename, disksink->filenum);
160 }
161
162 static void
163 gst_disksink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
164 {
165   GstDiskSink *sink;
166
167   /* it's not null if we got it, but it might not be ours */
168   sink = GST_DISKSINK (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_disksink_close_file (sink);
182               gst_disksink_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_disksink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
196 {
197   GstDiskSink *sink;
198  
199   /* it's not null if we got it, but it might not be ours */
200   g_return_if_fail (GST_IS_DISKSINK (object));
201  
202   sink = GST_DISKSINK (object);
203   
204   switch (prop_id) {
205     case ARG_LOCATION:
206       g_value_set_string (value, gst_disksink_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_disksink_open_file (GstDiskSink *sink)
219 {
220   g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_DISKSINK_OPEN), FALSE);
221
222   /* open the file */
223   if (!gst_disksink_getcurrentfilename(sink))
224   {
225     gst_element_error(GST_ELEMENT(sink), "Out of files");
226     return FALSE;
227   }
228   sink->file = fopen (gst_disksink_getcurrentfilename(sink), "w");
229   if (sink->file == NULL) {
230     perror ("open");
231     gst_element_error (GST_ELEMENT (sink), g_strconcat("Error opening file \"",
232       gst_disksink_getcurrentfilename(sink), "\": ", sys_errlist[errno], NULL));
233     return FALSE;
234   } 
235
236   GST_FLAG_SET (sink, GST_DISKSINK_OPEN);
237
238   sink->data_written = 0;
239
240   return TRUE;
241 }
242
243 static void
244 gst_disksink_close_file (GstDiskSink *sink)
245 {
246   g_return_if_fail (GST_FLAG_IS_SET (sink, GST_DISKSINK_OPEN));
247
248   if (fclose (sink->file) != 0)
249   {
250     perror ("close");
251     gst_element_error (GST_ELEMENT (sink), g_strconcat("Error closing file \"",
252       gst_disksink_getcurrentfilename(sink), "\": ", sys_errlist[errno], NULL));
253   }
254   else {
255     GST_FLAG_UNSET (sink, GST_DISKSINK_OPEN);
256   }
257 }
258
259 /* handle events (search) */
260 static gboolean
261 gst_disksink_handle_event (GstPad *pad, GstEvent *event)
262 {
263   GstEventType type;
264   GstDiskSink *disksink;
265
266   disksink = GST_DISKSINK (gst_pad_get_parent (pad));
267
268   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
269
270   switch (type) {
271     case GST_EVENT_SEEK:
272       /* we need to seek */
273       if (GST_EVENT_SEEK_FLUSH(event))
274         if (fflush(disksink->file))
275           gst_element_error(GST_ELEMENT(disksink),
276             "Error flushing the buffer cache of file \'%s\' to disk: %s",
277             gst_disksink_getcurrentfilename(disksink), sys_errlist[errno]);
278       switch (GST_EVENT_SEEK_TYPE(event))
279       {
280         case GST_SEEK_BYTEOFFSET_SET:
281           fseek(disksink->file, GST_EVENT_SEEK_OFFSET(event), SEEK_SET);
282           break;
283         case GST_SEEK_BYTEOFFSET_CUR:
284           fseek(disksink->file, GST_EVENT_SEEK_OFFSET(event), SEEK_CUR);
285           break;
286         case GST_SEEK_BYTEOFFSET_END:
287           fseek(disksink->file, GST_EVENT_SEEK_OFFSET(event), SEEK_END);
288           break;
289         default:
290           g_warning("Any other then byte-offset seeking is not supported!\n");
291           break;
292       }
293       break;
294     case GST_EVENT_NEW_MEDIA:
295       /* we need to open a new file! */
296       gst_disksink_close_file(disksink);
297       disksink->filenum++;
298       if (!gst_disksink_open_file(disksink)) return FALSE;
299       break;
300     case GST_EVENT_FLUSH:
301       if (fflush(disksink->file))
302         gst_element_error(GST_ELEMENT(disksink),
303           "Error flushing the buffer cache of file \'%s\' to disk: %s",
304           gst_disksink_getcurrentfilename(disksink), sys_errlist[errno]);
305       break;
306     default:
307       g_warning("Unhandled event %d\n", type);
308       break;
309   }
310
311   return TRUE;
312 }
313
314 /**
315  * gst_disksink_chain:
316  * @pad: the pad this disksink is connected to
317  * @buf: the buffer that has to be absorbed
318  *
319  * take the buffer from the pad and write to file if it's open
320  */
321 static void 
322 gst_disksink_chain (GstPad *pad, GstBuffer *buf) 
323 {
324   GstDiskSink *disksink;
325   gint bytes_written = 0;
326
327   g_return_if_fail (pad != NULL);
328   g_return_if_fail (GST_IS_PAD (pad));
329   g_return_if_fail (buf != NULL);
330
331   disksink = GST_DISKSINK (gst_pad_get_parent (pad));
332
333   if (GST_IS_EVENT(buf))
334   {
335     gst_disksink_handle_event(pad, GST_EVENT(buf));
336     return;
337   }
338
339   if (disksink->maxfilesize > 0)
340   {
341     if ((disksink->data_written + GST_BUFFER_SIZE(buf))/(1024*1024) > disksink->maxfilesize)
342     {
343       GstEvent *event;
344       event = gst_event_new(GST_EVENT_NEW_MEDIA);
345       gst_pad_send_event(pad, event);
346
347       /* if the event wasn't handled, we probably need to open a new file ourselves */
348       if (disksink->data_written)
349       {
350         gst_disksink_close_file(disksink);
351         disksink->filenum++;
352         if (!gst_disksink_open_file(disksink)) return;
353       }
354     }
355   }
356
357   if (GST_FLAG_IS_SET (disksink, GST_DISKSINK_OPEN))
358   {
359     bytes_written = fwrite (GST_BUFFER_DATA (buf), 1, GST_BUFFER_SIZE (buf), disksink->file);
360     if (bytes_written < GST_BUFFER_SIZE (buf))
361     {
362       printf ("disksink : Warning : %d bytes should be written, only %d bytes written\n",
363                   GST_BUFFER_SIZE (buf), bytes_written);
364     }
365   }
366   disksink->data_written += GST_BUFFER_SIZE(buf);
367   gst_buffer_unref (buf);
368
369   g_signal_emit (G_OBJECT (disksink), gst_disksink_signals[SIGNAL_HANDOFF], 0,
370                               disksink);
371 }
372
373 static GstElementStateReturn
374 gst_disksink_change_state (GstElement *element)
375 {
376   g_return_val_if_fail (GST_IS_DISKSINK (element), GST_STATE_FAILURE);
377
378   if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
379     if (GST_FLAG_IS_SET (element, GST_DISKSINK_OPEN))
380       gst_disksink_close_file (GST_DISKSINK (element));
381   } else {
382     if (!GST_FLAG_IS_SET (element, GST_DISKSINK_OPEN)) {
383       if (!gst_disksink_open_file (GST_DISKSINK (element)))
384         return GST_STATE_FAILURE;
385     }
386   }
387
388   if (GST_ELEMENT_CLASS (parent_class)->change_state)
389     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
390
391   return GST_STATE_SUCCESS;
392 }
393