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 (gst_filesink_debug);
37 #define GST_CAT_DEFAULT gst_filesink_debug
39 GstElementDetails gst_filesink_details = {
43 "Write stream to a file",
45 "Thomas <thomas@apestaart.org>",
50 /* FileSink signals and args */
62 GST_PAD_QUERY_TYPE_FUNCTION (gst_filesink_get_query_types,
67 GST_PAD_FORMATS_FUNCTION (gst_filesink_get_formats,
72 static void gst_filesink_class_init (GstFileSinkClass *klass);
73 static void gst_filesink_init (GstFileSink *filesink);
75 static void gst_filesink_set_property (GObject *object, guint prop_id,
76 const GValue *value, GParamSpec *pspec);
77 static void gst_filesink_get_property (GObject *object, guint prop_id,
78 GValue *value, GParamSpec *pspec);
80 static gboolean gst_filesink_open_file (GstFileSink *sink);
81 static void gst_filesink_close_file (GstFileSink *sink);
83 static const GstEventMask *
84 gst_filesink_get_event_mask (GstPad *pad);
86 static gboolean gst_filesink_handle_event (GstPad *pad, GstEvent *event);
87 static gboolean gst_filesink_pad_query (GstPad *pad, GstQueryType type,
88 GstFormat *format, gint64 *value);
89 static void gst_filesink_chain (GstPad *pad,GstBuffer *buf);
91 static GstElementStateReturn gst_filesink_change_state (GstElement *element);
93 static GstElementClass *parent_class = NULL;
94 static guint gst_filesink_signals[LAST_SIGNAL] = { 0 };
97 gst_filesink_get_type (void)
99 static GType filesink_type = 0;
101 if (!filesink_type) {
102 static const GTypeInfo filesink_info = {
103 sizeof(GstFileSinkClass), NULL,
105 (GClassInitFunc)gst_filesink_class_init,
110 (GInstanceInitFunc)gst_filesink_init,
112 filesink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFileSink", &filesink_info, 0);
114 return filesink_type;
118 gst_filesink_class_init (GstFileSinkClass *klass)
120 GObjectClass *gobject_class;
121 GstElementClass *gstelement_class;
123 gobject_class = (GObjectClass*)klass;
124 gstelement_class = (GstElementClass*)klass;
126 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
128 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATION,
129 g_param_spec_string ("location", "File Location", "Location of the file to write",
130 NULL, G_PARAM_READWRITE));
132 gst_filesink_signals[SIGNAL_HANDOFF] =
133 g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
134 G_STRUCT_OFFSET (GstFileSinkClass, handoff), NULL, NULL,
135 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
137 gobject_class->set_property = gst_filesink_set_property;
138 gobject_class->get_property = gst_filesink_get_property;
140 gstelement_class->change_state = gst_filesink_change_state;
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);
153 gst_pad_set_event_function(pad, gst_filesink_handle_event);
154 gst_pad_set_event_mask_function(pad, gst_filesink_get_event_mask);
156 gst_pad_set_query_function (pad, gst_filesink_pad_query);
157 gst_pad_set_query_type_function (pad, gst_filesink_get_query_types);
158 gst_pad_set_formats_function (pad, gst_filesink_get_formats);
160 filesink->filename = NULL;
161 filesink->file = NULL;
165 gst_filesink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
169 /* it's not null if we got it, but it might not be ours */
170 sink = GST_FILESINK (object);
174 /* the element must be stopped or paused in order to do this */
175 g_return_if_fail (GST_STATE (sink) <= GST_STATE_PAUSED);
176 if (GST_STATE (sink) == GST_STATE_PAUSED)
177 g_return_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
180 g_free (sink->filename);
181 sink->filename = g_strdup (g_value_get_string (value));
182 if (GST_STATE (sink) == GST_STATE_PAUSED)
183 gst_filesink_open_file (sink);
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
192 gst_filesink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
196 /* it's not null if we got it, but it might not be ours */
197 g_return_if_fail (GST_IS_FILESINK (object));
199 sink = GST_FILESINK (object);
203 g_value_set_string (value, sink->filename);
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212 gst_filesink_open_file (GstFileSink *sink)
214 g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN), FALSE);
223 sink->file = fopen (sink->filename, "w");
224 if (sink->file == NULL) {
225 gst_element_error (GST_ELEMENT (sink),
226 "Error opening file %s: %s",
227 sink->filename, g_strerror(errno));
231 GST_FLAG_SET (sink, GST_FILESINK_OPEN);
233 sink->data_written = 0;
239 gst_filesink_close_file (GstFileSink *sink)
241 g_return_if_fail (GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
243 if (fclose (sink->file) != 0)
245 gst_element_error (GST_ELEMENT (sink),
246 "Error closing file %s: %s",
247 sink->filename, g_strerror(errno));
250 GST_FLAG_UNSET (sink, GST_FILESINK_OPEN);
255 gst_filesink_pad_query (GstPad *pad, GstQueryType type,
256 GstFormat *format, gint64 *value)
258 GstFileSink *sink = GST_FILESINK (GST_PAD_PARENT (pad));
261 case GST_QUERY_TOTAL:
263 case GST_FORMAT_BYTES:
264 if (GST_FLAG_IS_SET (GST_ELEMENT(sink), GST_FILESINK_OPEN)) {
265 *value = sink->data_written; /* FIXME - doesn't the kernel provide
273 case GST_QUERY_POSITION:
275 case GST_FORMAT_BYTES:
276 if (GST_FLAG_IS_SET (GST_ELEMENT(sink), GST_FILESINK_OPEN)) {
277 *value = ftell (sink->file);
291 /* supported events */
292 static const GstEventMask *
293 gst_filesink_get_event_mask (GstPad *pad)
295 GstFileSink *filesink = GST_FILESINK (gst_pad_get_parent (pad));
296 struct stat filestat;
297 static const GstEventMask seek_masks[] = {
298 { GST_EVENT_SEEK, GST_SEEK_METHOD_CUR |
299 GST_SEEK_METHOD_SET |
300 GST_SEEK_METHOD_END |
301 GST_SEEK_FLAG_FLUSH },
302 { GST_EVENT_FLUSH, 0 },
303 { GST_EVENT_DISCONTINUOUS, 0 },
305 }, noseek_masks[] = {
306 { GST_EVENT_FLUSH, 0 },
308 }, *selected = seek_masks;
310 if (filesink->file != NULL) {
311 if (fstat (fileno (filesink->file), &filestat) == 0) {
312 if (S_ISFIFO (filestat.st_mode) ||
313 S_ISSOCK (filestat.st_mode)) {
314 selected = noseek_masks;
322 /* handle events (search) */
324 gst_filesink_handle_event (GstPad *pad, GstEvent *event)
327 GstFileSink *filesink;
329 filesink = GST_FILESINK (gst_pad_get_parent (pad));
331 g_return_val_if_fail (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN),
334 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
338 g_return_val_if_fail (GST_EVENT_SEEK_FORMAT (event) == GST_FORMAT_BYTES,
341 if (GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_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));
347 switch (GST_EVENT_SEEK_METHOD(event))
349 case GST_SEEK_METHOD_SET:
350 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_SET);
352 case GST_SEEK_METHOD_CUR:
353 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_CUR);
355 case GST_SEEK_METHOD_END:
356 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_END);
359 g_warning("unkown seek method!\n");
363 case GST_EVENT_DISCONTINUOUS:
367 if (gst_event_discont_get_value (event, GST_FORMAT_BYTES, &offset))
368 fseek (filesink->file, offset, SEEK_SET);
370 gst_event_unref (event);
373 case GST_EVENT_FLUSH:
374 if (fflush (filesink->file)) {
375 gst_element_error (GST_ELEMENT (filesink),
376 "Error flushing file %s: %s",
377 filesink->filename, g_strerror(errno));
381 gst_filesink_close_file (filesink);
382 gst_element_set_eos (GST_ELEMENT (filesink));
385 gst_pad_event_default (pad, event);
393 * gst_filesink_chain:
394 * @pad: the pad this filesink is connected to
395 * @buf: the buffer that has to be absorbed
397 * take the buffer from the pad and write to file if it's open
400 gst_filesink_chain (GstPad *pad, GstBuffer *buf)
402 GstFileSink *filesink;
404 g_return_if_fail (pad != NULL);
405 g_return_if_fail (GST_IS_PAD (pad));
406 g_return_if_fail (buf != NULL);
408 filesink = GST_FILESINK (gst_pad_get_parent (pad));
410 if (GST_IS_EVENT(buf))
412 gst_filesink_handle_event(pad, GST_EVENT(buf));
416 if (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN))
418 guint bytes_written = 0, back_pending = 0;
419 if (ftell(filesink->file) < filesink->data_written)
420 back_pending = filesink->data_written - ftell(filesink->file);
421 while (bytes_written < GST_BUFFER_SIZE (buf)) {
422 size_t wrote = fwrite (GST_BUFFER_DATA (buf) + bytes_written, 1,
423 GST_BUFFER_SIZE (buf) - bytes_written,
426 gst_element_error (GST_ELEMENT (filesink),
427 "Only %d of %d bytes written: %s",
428 bytes_written, GST_BUFFER_SIZE (buf),
432 bytes_written += wrote;
435 filesink->data_written += bytes_written - back_pending;
438 gst_buffer_unref (buf);
440 g_signal_emit (G_OBJECT (filesink),
441 gst_filesink_signals[SIGNAL_HANDOFF], 0,
445 static GstElementStateReturn
446 gst_filesink_change_state (GstElement *element)
448 g_return_val_if_fail (GST_IS_FILESINK (element), GST_STATE_FAILURE);
450 switch (GST_STATE_TRANSITION (element)) {
451 case GST_STATE_PAUSED_TO_READY:
452 if (GST_FLAG_IS_SET (element, GST_FILESINK_OPEN))
453 gst_filesink_close_file (GST_FILESINK (element));
456 case GST_STATE_READY_TO_PAUSED:
457 if (!GST_FLAG_IS_SET (element, GST_FILESINK_OPEN)) {
458 if (!gst_filesink_open_file (GST_FILESINK (element)))
459 return GST_STATE_FAILURE;
464 if (GST_ELEMENT_CLASS (parent_class)->change_state)
465 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
467 return GST_STATE_SUCCESS;