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 = {
33 "Write stream to a file",
35 "Thomas <thomas@apestaart.org>",
40 /* FileSink signals and args */
53 GST_EVENT_MASK_FUNCTION (gst_filesink_get_event_mask,
54 { GST_EVENT_SEEK, GST_SEEK_METHOD_CUR |
57 GST_SEEK_FLAG_FLUSH },
58 { GST_EVENT_FLUSH, 0 },
59 { GST_EVENT_DISCONTINUOUS, 0 },
60 { GST_EVENT_NEW_MEDIA, 0 }
64 static void gst_filesink_class_init (GstFileSinkClass *klass);
65 static void gst_filesink_init (GstFileSink *filesink);
67 static void gst_filesink_set_property (GObject *object, guint prop_id,
68 const GValue *value, GParamSpec *pspec);
69 static void gst_filesink_get_property (GObject *object, guint prop_id,
70 GValue *value, GParamSpec *pspec);
72 static gboolean gst_filesink_open_file (GstFileSink *sink);
73 static void gst_filesink_close_file (GstFileSink *sink);
75 static gboolean gst_filesink_handle_event (GstPad *pad, GstEvent *event);
76 static void gst_filesink_chain (GstPad *pad,GstBuffer *buf);
78 static GstElementStateReturn gst_filesink_change_state (GstElement *element);
80 static GstElementClass *parent_class = NULL;
81 static guint gst_filesink_signals[LAST_SIGNAL] = { 0 };
84 gst_filesink_get_type (void)
86 static GType filesink_type = 0;
89 static const GTypeInfo filesink_info = {
90 sizeof(GstFileSinkClass), NULL,
92 (GClassInitFunc)gst_filesink_class_init,
97 (GInstanceInitFunc)gst_filesink_init,
99 filesink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFileSink", &filesink_info, 0);
101 return filesink_type;
105 gst_filesink_class_init (GstFileSinkClass *klass)
107 GObjectClass *gobject_class;
108 GstElementClass *gstelement_class;
110 gobject_class = (GObjectClass*)klass;
111 gstelement_class = (GstElementClass*)klass;
113 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
115 gst_element_class_install_std_props (
116 GST_ELEMENT_CLASS (klass),
117 "location", ARG_LOCATION, G_PARAM_READWRITE,
120 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_MAXFILESIZE,
121 g_param_spec_int("maxfilesize","MaxFileSize","Maximum Size Per File",
122 G_MININT,G_MAXINT,0,G_PARAM_READWRITE));
124 gst_filesink_signals[SIGNAL_HANDOFF] =
125 g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
126 G_STRUCT_OFFSET (GstFileSinkClass, handoff), NULL, NULL,
127 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
129 gobject_class->set_property = gst_filesink_set_property;
130 gobject_class->get_property = gst_filesink_get_property;
132 gstelement_class->change_state = gst_filesink_change_state;
136 gst_filesink_init (GstFileSink *filesink)
140 pad = gst_pad_new ("sink", GST_PAD_SINK);
141 gst_element_add_pad (GST_ELEMENT (filesink), pad);
142 gst_pad_set_chain_function (pad, gst_filesink_chain);
144 GST_FLAG_SET (GST_ELEMENT(filesink), GST_ELEMENT_EVENT_AWARE);
145 gst_pad_set_event_function(pad, gst_filesink_handle_event);
146 gst_pad_set_event_mask_function(pad, gst_filesink_get_event_mask);
148 filesink->filename = NULL;
149 filesink->file = NULL;
150 filesink->filenum = 0;
152 filesink->maxfilesize = -1;
156 gst_filesink_getcurrentfilename (GstFileSink *filesink)
158 g_return_val_if_fail(filesink != NULL, NULL);
159 g_return_val_if_fail(GST_IS_FILESINK(filesink), NULL);
160 if (filesink->filename == NULL) return NULL;
161 g_return_val_if_fail(filesink->filenum >= 0, NULL);
163 if (!strstr(filesink->filename, "%"))
165 if (!filesink->filenum)
166 return g_strdup(filesink->filename);
171 return g_strdup_printf(filesink->filename, filesink->filenum);
175 gst_filesink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
179 /* it's not null if we got it, but it might not be ours */
180 sink = GST_FILESINK (object);
184 /* the element must be stopped or paused in order to do this */
185 g_return_if_fail ((GST_STATE (sink) < GST_STATE_PLAYING)
186 || (GST_STATE (sink) == GST_STATE_PAUSED));
188 g_free (sink->filename);
189 sink->filename = g_strdup (g_value_get_string (value));
190 if ( (GST_STATE (sink) == GST_STATE_PAUSED)
191 && (sink->filename != NULL))
193 gst_filesink_close_file (sink);
194 gst_filesink_open_file (sink);
198 case ARG_MAXFILESIZE:
199 sink->maxfilesize = g_value_get_int(value);
207 gst_filesink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
211 /* it's not null if we got it, but it might not be ours */
212 g_return_if_fail (GST_IS_FILESINK (object));
214 sink = GST_FILESINK (object);
218 g_value_set_string (value, gst_filesink_getcurrentfilename(sink));
220 case ARG_MAXFILESIZE:
221 g_value_set_int (value, sink->maxfilesize);
224 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230 gst_filesink_open_file (GstFileSink *sink)
232 g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN), FALSE);
235 if (!gst_filesink_getcurrentfilename(sink))
240 sink->file = fopen (gst_filesink_getcurrentfilename(sink), "w");
241 if (sink->file == NULL) {
243 gst_element_error (GST_ELEMENT (sink), g_strconcat("Error opening file \"",
244 gst_filesink_getcurrentfilename(sink), "\": ", sys_errlist[errno], NULL));
248 GST_FLAG_SET (sink, GST_FILESINK_OPEN);
250 sink->data_written = 0;
256 gst_filesink_close_file (GstFileSink *sink)
258 g_return_if_fail (GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
260 if (fclose (sink->file) != 0)
263 gst_element_error (GST_ELEMENT (sink), g_strconcat("Error closing file \"",
264 gst_filesink_getcurrentfilename(sink), "\": ", sys_errlist[errno], NULL));
267 GST_FLAG_UNSET (sink, GST_FILESINK_OPEN);
271 /* handle events (search) */
273 gst_filesink_handle_event (GstPad *pad, GstEvent *event)
276 GstFileSink *filesink;
278 filesink = GST_FILESINK (gst_pad_get_parent (pad));
280 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
284 /* we need to seek */
285 if (GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH)
286 if (fflush(filesink->file))
287 gst_element_error(GST_ELEMENT(filesink),
288 "Error flushing the buffer cache of file \'%s\' to disk: %s",
289 gst_filesink_getcurrentfilename(filesink), sys_errlist[errno]);
291 if (GST_EVENT_SEEK_FORMAT (event) != GST_FORMAT_BYTES) {
292 g_warning("Any other then byte-offset seeking is not supported!\n");
295 switch (GST_EVENT_SEEK_METHOD(event))
297 case GST_SEEK_METHOD_SET:
298 fseek(filesink->file, GST_EVENT_SEEK_OFFSET(event), SEEK_SET);
300 case GST_SEEK_METHOD_CUR:
301 fseek(filesink->file, GST_EVENT_SEEK_OFFSET(event), SEEK_CUR);
303 case GST_SEEK_METHOD_END:
304 fseek(filesink->file, GST_EVENT_SEEK_OFFSET(event), SEEK_END);
307 g_warning("unkown seek method!\n");
311 case GST_EVENT_DISCONTINUOUS:
315 if (gst_event_discont_get_value (event, GST_FORMAT_BYTES, &offset))
316 fseek(filesink->file, offset, SEEK_SET);
318 gst_event_unref (event);
321 case GST_EVENT_NEW_MEDIA:
322 /* we need to open a new file! */
323 gst_filesink_close_file(filesink);
325 if (!gst_filesink_open_file(filesink)) {
326 /* no more files, give EOS */
327 gst_element_set_eos(GST_ELEMENT(filesink));
331 case GST_EVENT_FLUSH:
332 if (fflush(filesink->file))
333 gst_element_error(GST_ELEMENT(filesink),
334 "Error flushing the buffer cache of file \'%s\' to disk: %s",
335 gst_filesink_getcurrentfilename(filesink), sys_errlist[errno]);
338 gst_pad_event_default (pad, event);
346 * gst_filesink_chain:
347 * @pad: the pad this filesink is connected to
348 * @buf: the buffer that has to be absorbed
350 * take the buffer from the pad and write to file if it's open
353 gst_filesink_chain (GstPad *pad, GstBuffer *buf)
355 GstFileSink *filesink;
356 gint bytes_written = 0;
358 g_return_if_fail (pad != NULL);
359 g_return_if_fail (GST_IS_PAD (pad));
360 g_return_if_fail (buf != NULL);
362 filesink = GST_FILESINK (gst_pad_get_parent (pad));
364 if (GST_IS_EVENT(buf))
366 gst_filesink_handle_event(pad, GST_EVENT(buf));
370 if (filesink->maxfilesize > 0)
372 if ((filesink->data_written + GST_BUFFER_SIZE(buf))/(1024*1024) > filesink->maxfilesize)
374 if (GST_ELEMENT_IS_EVENT_AWARE(GST_ELEMENT(filesink)))
377 event = gst_event_new(GST_EVENT_NEW_MEDIA);
378 gst_pad_send_event(pad, event);
383 if (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN))
385 bytes_written = fwrite (GST_BUFFER_DATA (buf), 1, GST_BUFFER_SIZE (buf), filesink->file);
386 if (bytes_written < GST_BUFFER_SIZE (buf))
388 printf ("filesink : Warning : %d bytes should be written, only %d bytes written\n",
389 GST_BUFFER_SIZE (buf), bytes_written);
392 filesink->data_written += GST_BUFFER_SIZE(buf);
394 gst_buffer_unref (buf);
396 g_signal_emit (G_OBJECT (filesink), gst_filesink_signals[SIGNAL_HANDOFF], 0,
400 static GstElementStateReturn
401 gst_filesink_change_state (GstElement *element)
403 g_return_val_if_fail (GST_IS_FILESINK (element), GST_STATE_FAILURE);
405 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
406 if (GST_FLAG_IS_SET (element, GST_FILESINK_OPEN))
407 gst_filesink_close_file (GST_FILESINK (element));
409 if (!GST_FLAG_IS_SET (element, GST_FILESINK_OPEN)) {
410 if (!gst_filesink_open_file (GST_FILESINK (element)))
411 return GST_STATE_FAILURE;
415 if (GST_ELEMENT_CLASS (parent_class)->change_state)
416 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
418 return GST_STATE_SUCCESS;