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 "gstfilesink.h"
29 GstElementDetails gst_filesink_details = {
32 "Write stream to a file",
34 "Thomas <thomas@apestaart.org>",
39 /* FileSink signals and args */
53 static void gst_filesink_class_init (GstFileSinkClass *klass);
54 static void gst_filesink_init (GstFileSink *filesink);
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);
61 static gboolean gst_filesink_open_file (GstFileSink *sink);
62 static void gst_filesink_close_file (GstFileSink *sink);
64 static gboolean gst_filesink_handle_event (GstPad *pad, GstEvent *event);
65 static void gst_filesink_chain (GstPad *pad,GstBuffer *buf);
67 static GstElementStateReturn gst_filesink_change_state (GstElement *element);
69 static GstElementClass *parent_class = NULL;
70 static guint gst_filesink_signals[LAST_SIGNAL] = { 0 };
73 gst_filesink_get_type (void)
75 static GType filesink_type = 0;
78 static const GTypeInfo filesink_info = {
79 sizeof(GstFileSinkClass), NULL,
81 (GClassInitFunc)gst_filesink_class_init,
86 (GInstanceInitFunc)gst_filesink_init,
88 filesink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFileSink", &filesink_info, 0);
94 gst_filesink_class_init (GstFileSinkClass *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_class_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_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);
118 gobject_class->set_property = gst_filesink_set_property;
119 gobject_class->get_property = gst_filesink_get_property;
121 gstelement_class->change_state = gst_filesink_change_state;
124 static const GstEventMask*
125 gst_filesink_get_event_mask (GstPad *pad)
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 },
138 return gst_filesink_event_mask;
142 gst_filesink_init (GstFileSink *filesink)
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);
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);
154 filesink->filename = NULL;
155 filesink->file = NULL;
156 filesink->filenum = 0;
158 filesink->maxfilesize = -1;
162 gst_filesink_getcurrentfilename (GstFileSink *filesink)
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);
169 if (!strstr(filesink->filename, "%"))
171 if (!filesink->filenum)
172 return g_strdup(filesink->filename);
177 return g_strdup_printf(filesink->filename, filesink->filenum);
181 gst_filesink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
185 /* it's not null if we got it, but it might not be ours */
186 sink = GST_FILESINK (object);
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));
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))
199 gst_filesink_close_file (sink);
200 gst_filesink_open_file (sink);
204 case ARG_MAXFILESIZE:
205 sink->maxfilesize = g_value_get_int(value);
213 gst_filesink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
217 /* it's not null if we got it, but it might not be ours */
218 g_return_if_fail (GST_IS_FILESINK (object));
220 sink = GST_FILESINK (object);
224 g_value_set_string (value, gst_filesink_getcurrentfilename(sink));
226 case ARG_MAXFILESIZE:
227 g_value_set_int (value, sink->maxfilesize);
230 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236 gst_filesink_open_file (GstFileSink *sink)
238 g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN), FALSE);
241 if (!gst_filesink_getcurrentfilename(sink))
244 gst_element_set_eos(GST_ELEMENT(sink));
247 sink->file = fopen (gst_filesink_getcurrentfilename(sink), "w");
248 if (sink->file == NULL) {
250 gst_element_error (GST_ELEMENT (sink), g_strconcat("Error opening file \"",
251 gst_filesink_getcurrentfilename(sink), "\": ", sys_errlist[errno], NULL));
255 GST_FLAG_SET (sink, GST_FILESINK_OPEN);
257 sink->data_written = 0;
263 gst_filesink_close_file (GstFileSink *sink)
265 g_return_if_fail (GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
267 if (fclose (sink->file) != 0)
270 gst_element_error (GST_ELEMENT (sink), g_strconcat("Error closing file \"",
271 gst_filesink_getcurrentfilename(sink), "\": ", sys_errlist[errno], NULL));
274 GST_FLAG_UNSET (sink, GST_FILESINK_OPEN);
278 /* handle events (search) */
280 gst_filesink_handle_event (GstPad *pad, GstEvent *event)
283 GstFileSink *filesink;
285 filesink = GST_FILESINK (gst_pad_get_parent (pad));
287 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
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]);
298 if (GST_EVENT_SEEK_FORMAT (event) != GST_FORMAT_BYTES) {
299 g_warning("Any other then byte-offset seeking is not supported!\n");
302 switch (GST_EVENT_SEEK_METHOD(event))
304 case GST_SEEK_METHOD_SET:
305 fseek(filesink->file, GST_EVENT_SEEK_OFFSET(event), SEEK_SET);
307 case GST_SEEK_METHOD_CUR:
308 fseek(filesink->file, GST_EVENT_SEEK_OFFSET(event), SEEK_CUR);
310 case GST_SEEK_METHOD_END:
311 fseek(filesink->file, GST_EVENT_SEEK_OFFSET(event), SEEK_END);
314 g_warning("unkown seek method!\n");
318 case GST_EVENT_DISCONTINUOUS:
322 if (gst_event_discont_get_value (event, GST_FORMAT_BYTES, &offset))
323 fseek(filesink->file, offset, SEEK_SET);
325 gst_event_unref (event);
328 case GST_EVENT_NEW_MEDIA:
329 /* we need to open a new file! */
330 gst_filesink_close_file(filesink);
332 if (!gst_filesink_open_file(filesink)) return FALSE;
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]);
341 gst_pad_event_default (pad, event);
349 * gst_filesink_chain:
350 * @pad: the pad this filesink is connected to
351 * @buf: the buffer that has to be absorbed
353 * take the buffer from the pad and write to file if it's open
356 gst_filesink_chain (GstPad *pad, GstBuffer *buf)
358 GstFileSink *filesink;
359 gint bytes_written = 0;
361 g_return_if_fail (pad != NULL);
362 g_return_if_fail (GST_IS_PAD (pad));
363 g_return_if_fail (buf != NULL);
365 filesink = GST_FILESINK (gst_pad_get_parent (pad));
367 if (GST_IS_EVENT(buf))
369 gst_filesink_handle_event(pad, GST_EVENT(buf));
373 if (filesink->maxfilesize > 0)
375 if ((filesink->data_written + GST_BUFFER_SIZE(buf))/(1024*1024) > filesink->maxfilesize)
377 if (GST_ELEMENT_IS_EVENT_AWARE(GST_ELEMENT(filesink)))
380 event = gst_event_new(GST_EVENT_NEW_MEDIA);
381 gst_pad_send_event(pad, event);
386 if (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN))
388 bytes_written = fwrite (GST_BUFFER_DATA (buf), 1, GST_BUFFER_SIZE (buf), filesink->file);
389 if (bytes_written < GST_BUFFER_SIZE (buf))
391 printf ("filesink : Warning : %d bytes should be written, only %d bytes written\n",
392 GST_BUFFER_SIZE (buf), bytes_written);
395 filesink->data_written += GST_BUFFER_SIZE(buf);
397 gst_buffer_unref (buf);
399 g_signal_emit (G_OBJECT (filesink), gst_filesink_signals[SIGNAL_HANDOFF], 0,
403 static GstElementStateReturn
404 gst_filesink_change_state (GstElement *element)
406 g_return_val_if_fail (GST_IS_FILESINK (element), GST_STATE_FAILURE);
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));
412 if (!GST_FLAG_IS_SET (element, GST_FILESINK_OPEN)) {
413 if (!gst_filesink_open_file (GST_FILESINK (element)))
414 return GST_STATE_FAILURE;
418 if (GST_ELEMENT_CLASS (parent_class)->change_state)
419 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
421 return GST_STATE_SUCCESS;