2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
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.
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.
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.
26 #include "gstdisksink.h"
29 GstElementDetails gst_disksink_details = {
34 "Thomas <thomas@apestaart.org>",
39 /* DiskSink signals and args */
53 static void gst_disksink_class_init (GstDiskSinkClass *klass);
54 static void gst_disksink_init (GstDiskSink *disksink);
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);
61 static gboolean gst_disksink_open_file (GstDiskSink *sink);
62 static void gst_disksink_close_file (GstDiskSink *sink);
64 static gboolean gst_disksink_handle_event (GstPad *pad, GstEvent *event);
65 static void gst_disksink_chain (GstPad *pad,GstBuffer *buf);
67 static GstElementStateReturn gst_disksink_change_state (GstElement *element);
69 static GstElementClass *parent_class = NULL;
70 static guint gst_disksink_signals[LAST_SIGNAL] = { 0 };
73 gst_disksink_get_type (void)
75 static GType disksink_type = 0;
78 static const GTypeInfo disksink_info = {
79 sizeof(GstDiskSinkClass), NULL,
81 (GClassInitFunc)gst_disksink_class_init,
86 (GInstanceInitFunc)gst_disksink_init,
88 disksink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstDiskSink", &disksink_info, 0);
94 gst_disksink_class_init (GstDiskSinkClass *klass)
96 GObjectClass *gobject_class;
97 GstElementClass *gstelement_class;
99 gobject_class = (GObjectClass*)klass;
100 gstelement_class = (GstElementClass*)klass;
102 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
104 gst_element_install_std_props (
105 GST_ELEMENT_CLASS (klass),
106 "location", ARG_LOCATION, G_PARAM_READWRITE,
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));
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);
118 gobject_class->set_property = gst_disksink_set_property;
119 gobject_class->get_property = gst_disksink_get_property;
121 gstelement_class->change_state = gst_disksink_change_state;
125 gst_disksink_init (GstDiskSink *disksink)
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);
133 GST_FLAG_SET (GST_ELEMENT(disksink), GST_ELEMENT_EVENT_AWARE);
134 gst_pad_set_event_function(pad, gst_disksink_handle_event);
136 disksink->filename = NULL;
137 disksink->file = NULL;
138 disksink->filenum = 0;
140 disksink->maxfilesize = -1;
144 gst_disksink_getcurrentfilename (GstDiskSink *disksink)
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);
151 if (!strstr(disksink->filename, "%"))
153 if (!disksink->filenum)
154 return g_strdup(disksink->filename);
159 return g_strdup_printf(disksink->filename, disksink->filenum);
163 gst_disksink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
167 /* it's not null if we got it, but it might not be ours */
168 sink = GST_DISKSINK (object);
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));
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))
181 gst_disksink_close_file (sink);
182 gst_disksink_open_file (sink);
186 case ARG_MAXFILESIZE:
187 sink->maxfilesize = g_value_get_int(value);
195 gst_disksink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
199 /* it's not null if we got it, but it might not be ours */
200 g_return_if_fail (GST_IS_DISKSINK (object));
202 sink = GST_DISKSINK (object);
206 g_value_set_string (value, gst_disksink_getcurrentfilename(sink));
208 case ARG_MAXFILESIZE:
209 g_value_set_int (value, sink->maxfilesize);
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218 gst_disksink_open_file (GstDiskSink *sink)
220 g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_DISKSINK_OPEN), FALSE);
223 if (!gst_disksink_getcurrentfilename(sink))
225 gst_element_error(GST_ELEMENT(sink), "Out of files");
228 sink->file = fopen (gst_disksink_getcurrentfilename(sink), "w");
229 if (sink->file == NULL) {
231 gst_element_error (GST_ELEMENT (sink), g_strconcat("Error opening file \"",
232 gst_disksink_getcurrentfilename(sink), "\": ", sys_errlist[errno], NULL));
236 GST_FLAG_SET (sink, GST_DISKSINK_OPEN);
238 sink->data_written = 0;
244 gst_disksink_close_file (GstDiskSink *sink)
246 g_return_if_fail (GST_FLAG_IS_SET (sink, GST_DISKSINK_OPEN));
248 if (fclose (sink->file) != 0)
251 gst_element_error (GST_ELEMENT (sink), g_strconcat("Error closing file \"",
252 gst_disksink_getcurrentfilename(sink), "\": ", sys_errlist[errno], NULL));
255 GST_FLAG_UNSET (sink, GST_DISKSINK_OPEN);
259 /* handle events (search) */
261 gst_disksink_handle_event (GstPad *pad, GstEvent *event)
264 GstDiskSink *disksink;
266 disksink = GST_DISKSINK (gst_pad_get_parent (pad));
268 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
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))
280 case GST_SEEK_BYTEOFFSET_SET:
281 fseek(disksink->file, GST_EVENT_SEEK_OFFSET(event), SEEK_SET);
283 case GST_SEEK_BYTEOFFSET_CUR:
284 fseek(disksink->file, GST_EVENT_SEEK_OFFSET(event), SEEK_CUR);
286 case GST_SEEK_BYTEOFFSET_END:
287 fseek(disksink->file, GST_EVENT_SEEK_OFFSET(event), SEEK_END);
290 g_warning("Any other then byte-offset seeking is not supported!\n");
294 case GST_EVENT_NEW_MEDIA:
295 /* we need to open a new file! */
296 gst_disksink_close_file(disksink);
298 if (!gst_disksink_open_file(disksink)) return FALSE;
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]);
307 g_warning("Unhandled event %d\n", type);
315 * gst_disksink_chain:
316 * @pad: the pad this disksink is connected to
317 * @buf: the buffer that has to be absorbed
319 * take the buffer from the pad and write to file if it's open
322 gst_disksink_chain (GstPad *pad, GstBuffer *buf)
324 GstDiskSink *disksink;
325 gint bytes_written = 0;
327 g_return_if_fail (pad != NULL);
328 g_return_if_fail (GST_IS_PAD (pad));
329 g_return_if_fail (buf != NULL);
331 disksink = GST_DISKSINK (gst_pad_get_parent (pad));
333 if (GST_IS_EVENT(buf))
335 gst_disksink_handle_event(pad, GST_EVENT(buf));
339 if (disksink->maxfilesize > 0)
341 if ((disksink->data_written + GST_BUFFER_SIZE(buf))/(1024*1024) > disksink->maxfilesize)
343 if (GST_ELEMENT_IS_EVENT_AWARE(GST_ELEMENT(disksink)))
346 event = gst_event_new(GST_EVENT_NEW_MEDIA);
347 gst_pad_send_event(pad, event);
352 if (GST_FLAG_IS_SET (disksink, GST_DISKSINK_OPEN))
354 bytes_written = fwrite (GST_BUFFER_DATA (buf), 1, GST_BUFFER_SIZE (buf), disksink->file);
355 if (bytes_written < GST_BUFFER_SIZE (buf))
357 printf ("disksink : Warning : %d bytes should be written, only %d bytes written\n",
358 GST_BUFFER_SIZE (buf), bytes_written);
361 disksink->data_written += GST_BUFFER_SIZE(buf);
362 gst_buffer_unref (buf);
364 g_signal_emit (G_OBJECT (disksink), gst_disksink_signals[SIGNAL_HANDOFF], 0,
368 static GstElementStateReturn
369 gst_disksink_change_state (GstElement *element)
371 g_return_val_if_fail (GST_IS_DISKSINK (element), GST_STATE_FAILURE);
373 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
374 if (GST_FLAG_IS_SET (element, GST_DISKSINK_OPEN))
375 gst_disksink_close_file (GST_DISKSINK (element));
377 if (!GST_FLAG_IS_SET (element, GST_DISKSINK_OPEN)) {
378 if (!gst_disksink_open_file (GST_DISKSINK (element)))
379 return GST_STATE_FAILURE;
383 if (GST_ELEMENT_CLASS (parent_class)->change_state)
384 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
386 return GST_STATE_SUCCESS;