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.
30 #include "gstfilesink.h"
33 #include <sys/types.h>
36 GST_DEBUG_CATEGORY_STATIC (gst_filesink_debug);
37 #define GST_CAT_DEFAULT gst_filesink_debug
39 GstElementDetails gst_filesink_details = GST_ELEMENT_DETAILS (
42 "Write stream to a file",
43 "Thomas <thomas@apestaart.org>"
47 /* FileSink signals and args */
59 GST_PAD_QUERY_TYPE_FUNCTION (gst_filesink_get_query_types,
64 GST_PAD_FORMATS_FUNCTION (gst_filesink_get_formats,
69 static void gst_filesink_base_init (gpointer g_class);
70 static void gst_filesink_class_init (GstFileSinkClass *klass);
71 static void gst_filesink_init (GstFileSink *filesink);
73 static void gst_filesink_set_property (GObject *object, guint prop_id,
74 const GValue *value, GParamSpec *pspec);
75 static void gst_filesink_get_property (GObject *object, guint prop_id,
76 GValue *value, GParamSpec *pspec);
78 static gboolean gst_filesink_open_file (GstFileSink *sink);
79 static void gst_filesink_close_file (GstFileSink *sink);
81 static gboolean gst_filesink_handle_event (GstPad *pad, GstEvent *event);
82 static gboolean gst_filesink_pad_query (GstPad *pad, GstQueryType type,
83 GstFormat *format, gint64 *value);
84 static void gst_filesink_chain (GstPad *pad,GstData *_data);
86 static GstElementStateReturn gst_filesink_change_state (GstElement *element);
88 static GstElementClass *parent_class = NULL;
89 static guint gst_filesink_signals[LAST_SIGNAL] = { 0 };
92 gst_filesink_get_type (void)
94 static GType filesink_type = 0;
97 static const GTypeInfo filesink_info = {
98 sizeof(GstFileSinkClass),
99 gst_filesink_base_init,
101 (GClassInitFunc)gst_filesink_class_init,
106 (GInstanceInitFunc)gst_filesink_init,
108 filesink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFileSink", &filesink_info, 0);
110 GST_DEBUG_CATEGORY_INIT (gst_filesink_debug, "filesink", 0, "filesink element");
112 return filesink_type;
116 gst_filesink_base_init (gpointer g_class)
118 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
120 gstelement_class->change_state = gst_filesink_change_state;
121 gst_element_class_set_details (gstelement_class, &gst_filesink_details);
124 gst_filesink_class_init (GstFileSinkClass *klass)
126 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
128 parent_class = g_type_class_peek_parent (klass);
130 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATION,
131 g_param_spec_string ("location", "File Location", "Location of the file to write",
132 NULL, G_PARAM_READWRITE));
134 gst_filesink_signals[SIGNAL_HANDOFF] =
135 g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
136 G_STRUCT_OFFSET (GstFileSinkClass, handoff), NULL, NULL,
137 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
139 gobject_class->set_property = gst_filesink_set_property;
140 gobject_class->get_property = gst_filesink_get_property;
144 gst_filesink_init (GstFileSink *filesink)
148 pad = gst_pad_new ("sink", GST_PAD_SINK);
149 gst_element_add_pad (GST_ELEMENT (filesink), pad);
150 gst_pad_set_chain_function (pad, gst_filesink_chain);
152 GST_FLAG_SET (GST_ELEMENT(filesink), GST_ELEMENT_EVENT_AWARE);
154 gst_pad_set_query_function (pad, gst_filesink_pad_query);
155 gst_pad_set_query_type_function (pad, gst_filesink_get_query_types);
156 gst_pad_set_formats_function (pad, gst_filesink_get_formats);
158 filesink->filename = NULL;
159 filesink->file = NULL;
163 gst_filesink_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_FILESINK (object);
172 /* the element must be stopped or paused in order to do this */
173 g_return_if_fail (GST_STATE (sink) <= GST_STATE_PAUSED);
174 if (GST_STATE (sink) == GST_STATE_PAUSED)
175 g_return_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
178 g_free (sink->filename);
179 sink->filename = g_strdup (g_value_get_string (value));
180 if (GST_STATE (sink) == GST_STATE_PAUSED)
181 gst_filesink_open_file (sink);
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
190 gst_filesink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
194 /* it's not null if we got it, but it might not be ours */
195 g_return_if_fail (GST_IS_FILESINK (object));
197 sink = GST_FILESINK (object);
201 g_value_set_string (value, sink->filename);
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
210 gst_filesink_open_file (GstFileSink *sink)
212 g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN), FALSE);
217 gst_element_error (GST_ELEMENT (sink),
218 "Error opening file: no file given");
222 sink->file = fopen (sink->filename, "w");
223 if (sink->file == NULL) {
224 gst_element_error (GST_ELEMENT (sink),
225 "Error opening file %s: %s",
226 sink->filename, g_strerror(errno));
230 GST_FLAG_SET (sink, GST_FILESINK_OPEN);
232 sink->data_written = 0;
238 gst_filesink_close_file (GstFileSink *sink)
240 g_return_if_fail (GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
242 if (fclose (sink->file) != 0)
244 gst_element_error (GST_ELEMENT (sink),
245 "Error closing file %s: %s",
246 sink->filename, g_strerror(errno));
249 GST_FLAG_UNSET (sink, GST_FILESINK_OPEN);
254 gst_filesink_pad_query (GstPad *pad, GstQueryType type,
255 GstFormat *format, gint64 *value)
257 GstFileSink *sink = GST_FILESINK (GST_PAD_PARENT (pad));
260 case GST_QUERY_TOTAL:
262 case GST_FORMAT_BYTES:
263 if (GST_FLAG_IS_SET (GST_ELEMENT(sink), GST_FILESINK_OPEN)) {
264 *value = sink->data_written; /* FIXME - doesn't the kernel provide
272 case GST_QUERY_POSITION:
274 case GST_FORMAT_BYTES:
275 if (GST_FLAG_IS_SET (GST_ELEMENT(sink), GST_FILESINK_OPEN)) {
276 *value = ftell (sink->file);
290 /* handle events (search) */
292 gst_filesink_handle_event (GstPad *pad, GstEvent *event)
295 GstFileSink *filesink;
297 filesink = GST_FILESINK (gst_pad_get_parent (pad));
299 g_return_val_if_fail (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN),
302 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
306 g_return_val_if_fail (GST_EVENT_SEEK_FORMAT (event) == GST_FORMAT_BYTES,
309 if (GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH)
310 if (fflush (filesink->file))
311 gst_element_error (GST_ELEMENT (filesink),
312 "Error flushing file %s: %s",
313 filesink->filename, g_strerror(errno));
315 switch (GST_EVENT_SEEK_METHOD(event))
317 case GST_SEEK_METHOD_SET:
318 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_SET);
320 case GST_SEEK_METHOD_CUR:
321 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_CUR);
323 case GST_SEEK_METHOD_END:
324 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_END);
327 g_warning ("unknown seek method!");
331 case GST_EVENT_DISCONTINUOUS:
335 if (gst_event_discont_get_value (event, GST_FORMAT_BYTES, &offset))
336 fseek (filesink->file, offset, SEEK_SET);
338 gst_event_unref (event);
341 case GST_EVENT_FLUSH:
342 if (fflush (filesink->file)) {
343 gst_element_error (GST_ELEMENT (filesink),
344 "Error flushing file %s: %s",
345 filesink->filename, g_strerror(errno));
349 gst_filesink_close_file (filesink);
350 gst_element_set_eos (GST_ELEMENT (filesink));
353 gst_pad_event_default (pad, event);
361 * gst_filesink_chain:
362 * @pad: the pad this filesink is connected to
363 * @buf: the buffer that has to be absorbed
365 * take the buffer from the pad and write to file if it's open
368 gst_filesink_chain (GstPad *pad, GstData *_data)
370 GstBuffer *buf = GST_BUFFER (_data);
371 GstFileSink *filesink;
373 g_return_if_fail (pad != NULL);
374 g_return_if_fail (GST_IS_PAD (pad));
375 g_return_if_fail (buf != NULL);
377 filesink = GST_FILESINK (gst_pad_get_parent (pad));
379 if (GST_IS_EVENT(buf))
381 gst_filesink_handle_event(pad, GST_EVENT(buf));
385 if (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN))
387 guint bytes_written = 0, back_pending = 0;
388 if (ftell(filesink->file) < filesink->data_written)
389 back_pending = filesink->data_written - ftell(filesink->file);
390 while (bytes_written < GST_BUFFER_SIZE (buf)) {
391 size_t wrote = fwrite (GST_BUFFER_DATA (buf) + bytes_written, 1,
392 GST_BUFFER_SIZE (buf) - bytes_written,
395 gst_element_error (GST_ELEMENT (filesink),
396 "Only %d of %d bytes written: %s",
397 bytes_written, GST_BUFFER_SIZE (buf),
401 bytes_written += wrote;
404 filesink->data_written += bytes_written - back_pending;
407 gst_buffer_unref (buf);
409 g_signal_emit (G_OBJECT (filesink),
410 gst_filesink_signals[SIGNAL_HANDOFF], 0,
414 static GstElementStateReturn
415 gst_filesink_change_state (GstElement *element)
417 g_return_val_if_fail (GST_IS_FILESINK (element), GST_STATE_FAILURE);
419 switch (GST_STATE_TRANSITION (element)) {
420 case GST_STATE_PAUSED_TO_READY:
421 if (GST_FLAG_IS_SET (element, GST_FILESINK_OPEN))
422 gst_filesink_close_file (GST_FILESINK (element));
425 case GST_STATE_READY_TO_PAUSED:
426 if (!GST_FLAG_IS_SET (element, GST_FILESINK_OPEN)) {
427 if (!gst_filesink_open_file (GST_FILESINK (element)))
428 return GST_STATE_FAILURE;
433 if (GST_ELEMENT_CLASS (parent_class)->change_state)
434 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
436 return GST_STATE_SUCCESS;