2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2006 Wim Taymans <wim@fluendo.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 * SECTION:element-filesink
25 * @see_also: #GstFileSrc
27 * Write incoming data to a file in the local file system.
34 #include "../../gst/gst-i18n-lib.h"
37 #include <stdio.h> /* for fseeko() */
38 #ifdef HAVE_STDIO_EXT_H
39 #include <stdio_ext.h> /* for __fbufsize, for debugging */
42 #include "gstfilesink.h"
44 #include <sys/types.h>
47 #include <io.h> /* lseek, open, close, read */
49 #define lseek _lseeki64
52 #ifdef _MSC_VER /* Check if we are using MSVC, fileno is deprecated in favour */
53 #define fileno _fileno /* of _fileno */
62 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
67 #define GST_TYPE_BUFFER_MODE (buffer_mode_get_type ())
69 buffer_mode_get_type (void)
71 static GType buffer_mode_type = 0;
72 static const GEnumValue buffer_mode[] = {
73 {-1, "Default buffering", "default"},
74 {_IOFBF, "Fully buffered", "full"},
75 {_IOLBF, "Line buffered", "line"},
76 {_IONBF, "Unbuffered", "unbuffered"},
80 if (!buffer_mode_type) {
82 g_enum_register_static ("GstFileSinkBufferMode", buffer_mode);
84 return buffer_mode_type;
87 GST_DEBUG_CATEGORY_STATIC (gst_file_sink_debug);
88 #define GST_CAT_DEFAULT gst_file_sink_debug
90 #define DEFAULT_LOCATION NULL
91 #define DEFAULT_BUFFER_MODE -1
92 #define DEFAULT_BUFFER_SIZE 64 * 1024
103 static void gst_file_sink_dispose (GObject * object);
105 static void gst_file_sink_set_property (GObject * object, guint prop_id,
106 const GValue * value, GParamSpec * pspec);
107 static void gst_file_sink_get_property (GObject * object, guint prop_id,
108 GValue * value, GParamSpec * pspec);
110 static gboolean gst_file_sink_open_file (GstFileSink * sink);
111 static void gst_file_sink_close_file (GstFileSink * sink);
113 static gboolean gst_file_sink_start (GstBaseSink * sink);
114 static gboolean gst_file_sink_stop (GstBaseSink * sink);
115 static gboolean gst_file_sink_event (GstBaseSink * sink, GstEvent * event);
116 static GstFlowReturn gst_file_sink_render (GstBaseSink * sink,
119 static gboolean gst_file_sink_do_seek (GstFileSink * filesink,
121 static gboolean gst_file_sink_get_current_offset (GstFileSink * filesink,
124 static gboolean gst_file_sink_query (GstPad * pad, GstQuery * query);
126 static void gst_file_sink_uri_handler_init (gpointer g_iface,
127 gpointer iface_data);
131 _do_init (GType filesink_type)
133 static const GInterfaceInfo urihandler_info = {
134 gst_file_sink_uri_handler_init,
139 g_type_add_interface_static (filesink_type, GST_TYPE_URI_HANDLER,
141 GST_DEBUG_CATEGORY_INIT (gst_file_sink_debug, "filesink", 0,
145 GST_BOILERPLATE_FULL (GstFileSink, gst_file_sink, GstBaseSink,
146 GST_TYPE_BASE_SINK, _do_init);
149 gst_file_sink_base_init (gpointer g_class)
151 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
153 gst_element_class_set_details_simple (gstelement_class,
155 "Sink/File", "Write stream to a file",
156 "Thomas Vander Stichele <thomas at apestaart dot org>");
157 gst_element_class_add_pad_template (gstelement_class,
158 gst_static_pad_template_get (&sinktemplate));
162 gst_file_sink_class_init (GstFileSinkClass * klass)
164 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
165 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
167 gobject_class->dispose = gst_file_sink_dispose;
169 gobject_class->set_property = gst_file_sink_set_property;
170 gobject_class->get_property = gst_file_sink_get_property;
172 g_object_class_install_property (gobject_class, PROP_LOCATION,
173 g_param_spec_string ("location", "File Location",
174 "Location of the file to write", NULL,
175 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
177 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
178 g_param_spec_enum ("buffer-mode", "Buffering mode",
179 "The buffering mode to use", GST_TYPE_BUFFER_MODE,
180 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
183 g_param_spec_uint ("buffer-size", "Buffering size",
184 "Size of buffer in number of bytes for line or full buffer-mode", 0,
185 G_MAXUINT, DEFAULT_BUFFER_SIZE,
186 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188 gstbasesink_class->get_times = NULL;
189 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_file_sink_start);
190 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_file_sink_stop);
191 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_file_sink_render);
192 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_file_sink_event);
194 if (sizeof (off_t) < 8) {
195 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
201 gst_file_sink_init (GstFileSink * filesink, GstFileSinkClass * g_class)
205 pad = GST_BASE_SINK_PAD (filesink);
207 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_file_sink_query));
209 filesink->filename = NULL;
210 filesink->file = NULL;
211 filesink->buffer_mode = DEFAULT_BUFFER_MODE;
212 filesink->buffer_size = DEFAULT_BUFFER_SIZE;
213 filesink->buffer = NULL;
215 gst_base_sink_set_sync (GST_BASE_SINK (filesink), FALSE);
219 gst_file_sink_dispose (GObject * object)
221 GstFileSink *sink = GST_FILE_SINK (object);
223 G_OBJECT_CLASS (parent_class)->dispose (object);
227 g_free (sink->filename);
228 sink->filename = NULL;
229 g_free (sink->buffer);
231 sink->buffer_size = 0;
235 gst_file_sink_set_location (GstFileSink * sink, const gchar * location)
240 g_free (sink->filename);
242 if (location != NULL) {
243 /* we store the filename as we received it from the application. On Windows
244 * this should be in UTF8 */
245 sink->filename = g_strdup (location);
246 sink->uri = gst_uri_construct ("file", sink->filename);
248 sink->filename = NULL;
257 g_warning ("Changing the `location' property on filesink when a file is "
258 "open is not supported.");
264 gst_file_sink_set_property (GObject * object, guint prop_id,
265 const GValue * value, GParamSpec * pspec)
267 GstFileSink *sink = GST_FILE_SINK (object);
271 gst_file_sink_set_location (sink, g_value_get_string (value));
273 case PROP_BUFFER_MODE:
274 sink->buffer_mode = g_value_get_enum (value);
276 case PROP_BUFFER_SIZE:
277 sink->buffer_size = g_value_get_uint (value);
280 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
286 gst_file_sink_get_property (GObject * object, guint prop_id, GValue * value,
289 GstFileSink *sink = GST_FILE_SINK (object);
293 g_value_set_string (value, sink->filename);
295 case PROP_BUFFER_MODE:
296 g_value_set_enum (value, sink->buffer_mode);
298 case PROP_BUFFER_SIZE:
299 g_value_set_uint (value, sink->buffer_size);
302 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
308 gst_file_sink_open_file (GstFileSink * sink)
313 if (sink->filename == NULL || sink->filename[0] == '\0')
316 /* FIXME, can we use g_fopen here? some people say that the FILE object is
317 * local to the .so that performed the fopen call, which would not be us when
319 sink->file = fopen (sink->filename, "wb");
320 if (sink->file == NULL)
323 /* see if we are asked to perform a specific kind of buffering */
324 if ((mode = sink->buffer_mode) != -1) {
327 /* free previous buffer if any */
328 g_free (sink->buffer);
330 if (mode == _IONBF) {
335 /* allocate buffer */
336 sink->buffer = g_malloc (sink->buffer_size);
337 buffer_size = sink->buffer_size;
339 #ifdef HAVE_STDIO_EXT_H
340 GST_DEBUG_OBJECT (sink, "change buffer size %d to %d, mode %d",
341 __fbufsize (sink->file), buffer_size, mode);
343 GST_DEBUG_OBJECT (sink, "change buffer size to %d, mode %d",
344 sink->buffer_size, mode);
346 if (setvbuf (sink->file, sink->buffer, mode, buffer_size) != 0) {
347 GST_WARNING_OBJECT (sink, "warning: setvbuf failed: %s",
352 sink->current_pos = 0;
353 /* try to seek in the file to figure out if it is seekable */
354 sink->seekable = gst_file_sink_do_seek (sink, 0);
356 GST_DEBUG_OBJECT (sink, "opened file %s, seekable %d",
357 sink->filename, sink->seekable);
364 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
365 (_("No file name specified for writing.")), (NULL));
370 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
371 (_("Could not open file \"%s\" for writing."), sink->filename),
378 gst_file_sink_close_file (GstFileSink * sink)
381 if (fclose (sink->file) != 0)
384 GST_DEBUG_OBJECT (sink, "closed file");
387 g_free (sink->buffer);
395 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
396 (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
402 gst_file_sink_query (GstPad * pad, GstQuery * query)
407 self = GST_FILE_SINK (GST_PAD_PARENT (pad));
409 switch (GST_QUERY_TYPE (query)) {
410 case GST_QUERY_POSITION:
411 gst_query_parse_position (query, &format, NULL);
413 case GST_FORMAT_DEFAULT:
414 case GST_FORMAT_BYTES:
415 gst_query_set_position (query, GST_FORMAT_BYTES, self->current_pos);
421 case GST_QUERY_FORMATS:
422 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
426 gst_query_set_uri (query, self->uri);
430 return gst_pad_query_default (pad, query);
435 # define __GST_STDIO_SEEK_FUNCTION "fseeko"
436 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
437 # define __GST_STDIO_SEEK_FUNCTION "lseek"
439 # define __GST_STDIO_SEEK_FUNCTION "fseek"
443 gst_file_sink_do_seek (GstFileSink * filesink, guint64 new_offset)
445 GST_DEBUG_OBJECT (filesink, "Seeking to offset %" G_GUINT64_FORMAT
446 " using " __GST_STDIO_SEEK_FUNCTION, new_offset);
448 if (fflush (filesink->file))
452 if (fseeko (filesink->file, (off_t) new_offset, SEEK_SET) != 0)
454 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
455 if (lseek (fileno (filesink->file), (off_t) new_offset,
456 SEEK_SET) == (off_t) - 1)
459 if (fseek (filesink->file, (long) new_offset, SEEK_SET) != 0)
463 /* adjust position reporting after seek;
464 * presumably this should basically yield new_offset */
465 gst_file_sink_get_current_offset (filesink, &filesink->current_pos);
472 GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
477 GST_DEBUG_OBJECT (filesink, "Seeking failed: %s", g_strerror (errno));
482 /* handle events (search) */
484 gst_file_sink_event (GstBaseSink * sink, GstEvent * event)
487 GstFileSink *filesink;
489 filesink = GST_FILE_SINK (sink);
491 type = GST_EVENT_TYPE (event);
494 case GST_EVENT_NEWSEGMENT:
496 gint64 start, stop, pos;
499 gst_event_parse_new_segment (event, NULL, NULL, &format, &start,
502 if (format == GST_FORMAT_BYTES) {
503 /* only try to seek and fail when we are going to a different
505 if (filesink->current_pos != start) {
506 /* FIXME, the seek should be performed on the pos field, start/stop are
507 * just boundaries for valid bytes offsets. We should also fill the file
508 * with zeroes if the new position extends the current EOF (sparse streams
509 * and segment accumulation). */
510 if (!gst_file_sink_do_seek (filesink, (guint64) start))
513 GST_DEBUG_OBJECT (filesink, "Ignored NEWSEGMENT, no seek needed");
516 GST_DEBUG_OBJECT (filesink,
517 "Ignored NEWSEGMENT event of format %u (%s)", (guint) format,
518 gst_format_get_name (format));
523 if (fflush (filesink->file))
535 GST_ELEMENT_ERROR (filesink, RESOURCE, SEEK,
536 (_("Error while seeking in file \"%s\"."), filesink->filename),
542 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
543 (_("Error while writing to file \"%s\"."), filesink->filename),
550 gst_file_sink_get_current_offset (GstFileSink * filesink, guint64 * p_pos)
555 ret = ftello (filesink->file);
556 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
557 if (fflush (filesink->file)) {
558 GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
559 /* ignore and continue */
561 ret = lseek (fileno (filesink->file), 0, SEEK_CUR);
563 ret = (off_t) ftell (filesink->file);
566 if (ret != (off_t) - 1)
567 *p_pos = (guint64) ret;
569 return (ret != (off_t) - 1);
573 gst_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
575 GstFileSink *filesink;
579 filesink = GST_FILE_SINK (sink);
581 size = GST_BUFFER_SIZE (buffer);
582 data = GST_BUFFER_DATA (buffer);
584 GST_DEBUG_OBJECT (filesink, "writing %u bytes at %" G_GUINT64_FORMAT,
585 size, filesink->current_pos);
587 if (size > 0 && data != NULL) {
588 if (fwrite (data, size, 1, filesink->file) != 1)
591 filesink->current_pos += size;
600 GST_ELEMENT_ERROR (filesink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
604 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
605 (_("Error while writing to file \"%s\"."), filesink->filename),
606 ("%s", g_strerror (errno)));
609 return GST_FLOW_ERROR;
614 gst_file_sink_start (GstBaseSink * basesink)
616 return gst_file_sink_open_file (GST_FILE_SINK (basesink));
620 gst_file_sink_stop (GstBaseSink * basesink)
622 gst_file_sink_close_file (GST_FILE_SINK (basesink));
626 /*** GSTURIHANDLER INTERFACE *************************************************/
629 gst_file_sink_uri_get_type (void)
635 gst_file_sink_uri_get_protocols (void)
637 static gchar *protocols[] = { "file", NULL };
643 gst_file_sink_uri_get_uri (GstURIHandler * handler)
645 GstFileSink *sink = GST_FILE_SINK (handler);
651 gst_file_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
653 gchar *protocol, *location;
655 GstFileSink *sink = GST_FILE_SINK (handler);
657 protocol = gst_uri_get_protocol (uri);
658 if (strcmp (protocol, "file") != 0) {
664 /* allow file://localhost/foo/bar by stripping localhost but fail
665 * for every other hostname */
666 if (g_str_has_prefix (uri, "file://localhost/")) {
669 /* 16 == strlen ("file://localhost") */
670 tmp = g_strconcat ("file://", uri + 16, NULL);
671 /* we use gst_uri_get_location() although we already have the
672 * "location" with uri + 16 because it provides unescaping */
673 location = gst_uri_get_location (tmp);
675 } else if (strcmp (uri, "file://") == 0) {
676 /* Special case for "file://" as this is used by some applications
677 * to test with gst_element_make_from_uri if there's an element
678 * that supports the URI protocol. */
679 gst_file_sink_set_location (sink, NULL);
682 location = gst_uri_get_location (uri);
687 if (!g_path_is_absolute (location)) {
692 ret = gst_file_sink_set_location (sink, location);
699 gst_file_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
701 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
703 iface->get_type = gst_file_sink_uri_get_type;
704 iface->get_protocols = gst_file_sink_uri_get_protocols;
705 iface->get_uri = gst_file_sink_uri_get_uri;
706 iface->set_uri = gst_file_sink_uri_set_uri;