s/gst_element_install_std_props/gst_element_class_install_std_props/ -- it just makes...
[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_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_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     /* Out of files */
226     gst_element_set_eos(GST_ELEMENT(sink));
227     return FALSE;
228   }
229   sink->file = fopen (gst_disksink_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_disksink_getcurrentfilename(sink), "\": ", sys_errlist[errno], NULL));
234     return FALSE;
235   } 
236
237   GST_FLAG_SET (sink, GST_DISKSINK_OPEN);
238
239   sink->data_written = 0;
240
241   return TRUE;
242 }
243
244 static void
245 gst_disksink_close_file (GstDiskSink *sink)
246 {
247   g_return_if_fail (GST_FLAG_IS_SET (sink, GST_DISKSINK_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_disksink_getcurrentfilename(sink), "\": ", sys_errlist[errno], NULL));
254   }
255   else {
256     GST_FLAG_UNSET (sink, GST_DISKSINK_OPEN);
257   }
258 }
259
260 /* handle events (search) */
261 static gboolean
262 gst_disksink_handle_event (GstPad *pad, GstEvent *event)
263 {
264   GstEventType type;
265   GstDiskSink *disksink;
266
267   disksink = GST_DISKSINK (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_FLUSH(event))
275         if (fflush(disksink->file))
276           gst_element_error(GST_ELEMENT(disksink),
277             "Error flushing the buffer cache of file \'%s\' to disk: %s",
278             gst_disksink_getcurrentfilename(disksink), sys_errlist[errno]);
279       switch (GST_EVENT_SEEK_TYPE(event))
280       {
281         case GST_SEEK_BYTEOFFSET_SET:
282           fseek(disksink->file, GST_EVENT_SEEK_OFFSET(event), SEEK_SET);
283           break;
284         case GST_SEEK_BYTEOFFSET_CUR:
285           fseek(disksink->file, GST_EVENT_SEEK_OFFSET(event), SEEK_CUR);
286           break;
287         case GST_SEEK_BYTEOFFSET_END:
288           fseek(disksink->file, GST_EVENT_SEEK_OFFSET(event), SEEK_END);
289           break;
290         default:
291           g_warning("Any other then byte-offset seeking is not supported!\n");
292           break;
293       }
294       break;
295     case GST_EVENT_NEW_MEDIA:
296       /* we need to open a new file! */
297       gst_disksink_close_file(disksink);
298       disksink->filenum++;
299       if (!gst_disksink_open_file(disksink)) return FALSE;
300       break;
301     case GST_EVENT_FLUSH:
302       if (fflush(disksink->file))
303         gst_element_error(GST_ELEMENT(disksink),
304           "Error flushing the buffer cache of file \'%s\' to disk: %s",
305           gst_disksink_getcurrentfilename(disksink), sys_errlist[errno]);
306       break;
307     default:
308       gst_pad_event_default (pad, event);
309       break;
310   }
311
312   return TRUE;
313 }
314
315 /**
316  * gst_disksink_chain:
317  * @pad: the pad this disksink is connected to
318  * @buf: the buffer that has to be absorbed
319  *
320  * take the buffer from the pad and write to file if it's open
321  */
322 static void 
323 gst_disksink_chain (GstPad *pad, GstBuffer *buf) 
324 {
325   GstDiskSink *disksink;
326   gint bytes_written = 0;
327
328   g_return_if_fail (pad != NULL);
329   g_return_if_fail (GST_IS_PAD (pad));
330   g_return_if_fail (buf != NULL);
331
332   disksink = GST_DISKSINK (gst_pad_get_parent (pad));
333
334   if (GST_IS_EVENT(buf))
335   {
336     gst_disksink_handle_event(pad, GST_EVENT(buf));
337     return;
338   }
339
340   if (disksink->maxfilesize > 0)
341   {
342     if ((disksink->data_written + GST_BUFFER_SIZE(buf))/(1024*1024) > disksink->maxfilesize)
343     {
344       if (GST_ELEMENT_IS_EVENT_AWARE(GST_ELEMENT(disksink)))
345       {
346         GstEvent *event;
347         event = gst_event_new(GST_EVENT_NEW_MEDIA);
348         gst_pad_send_event(pad, event);
349       }
350     }
351   }
352
353   if (GST_FLAG_IS_SET (disksink, GST_DISKSINK_OPEN))
354   {
355     bytes_written = fwrite (GST_BUFFER_DATA (buf), 1, GST_BUFFER_SIZE (buf), disksink->file);
356     if (bytes_written < GST_BUFFER_SIZE (buf))
357     {
358       printf ("disksink : Warning : %d bytes should be written, only %d bytes written\n",
359                   GST_BUFFER_SIZE (buf), bytes_written);
360     }
361   }
362   disksink->data_written += GST_BUFFER_SIZE(buf);
363
364   gst_buffer_unref (buf);
365
366   g_signal_emit (G_OBJECT (disksink), gst_disksink_signals[SIGNAL_HANDOFF], 0,
367                               disksink);
368 }
369
370 static GstElementStateReturn
371 gst_disksink_change_state (GstElement *element)
372 {
373   g_return_val_if_fail (GST_IS_DISKSINK (element), GST_STATE_FAILURE);
374
375   if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
376     if (GST_FLAG_IS_SET (element, GST_DISKSINK_OPEN))
377       gst_disksink_close_file (GST_DISKSINK (element));
378   } else {
379     if (!GST_FLAG_IS_SET (element, GST_DISKSINK_OPEN)) {
380       if (!gst_disksink_open_file (GST_DISKSINK (element)))
381         return GST_STATE_FAILURE;
382     }
383   }
384
385   if (GST_ELEMENT_CLASS (parent_class)->change_state)
386     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
387
388   return GST_STATE_SUCCESS;
389 }
390