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., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
24 * SECTION:element-filesink
25 * @see_also: #GstFileSrc
27 * Write incoming data to a file in the local file system.
30 * <title>Example launch line</title>
32 * gst-launch v4l2src num-buffers=1 ! jpegenc ! filesink location=capture1.jpeg
33 * ]| Capture one frame from a v4l2 camera and save as jpeg image.
41 #include "../../gst/gst-i18n-lib.h"
44 #include <stdio.h> /* for fseeko() */
45 #ifdef HAVE_STDIO_EXT_H
46 #include <stdio_ext.h> /* for __fbufsize, for debugging */
49 #include "gstfilesink.h"
51 #include <sys/types.h>
54 #include <io.h> /* lseek, open, close, read */
56 #define lseek _lseeki64
60 #define ftruncate _chsize
61 #ifdef _MSC_VER /* Check if we are using MSVC, fileno is deprecated in favour */
62 #define fileno _fileno /* of _fileno */
71 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
76 #define GST_TYPE_BUFFER_MODE (buffer_mode_get_type ())
78 buffer_mode_get_type (void)
80 static GType buffer_mode_type = 0;
81 static const GEnumValue buffer_mode[] = {
82 {-1, "Default buffering", "default"},
83 {_IOFBF, "Fully buffered", "full"},
84 {_IOLBF, "Line buffered", "line"},
85 {_IONBF, "Unbuffered", "unbuffered"},
89 if (!buffer_mode_type) {
91 g_enum_register_static ("GstFileSinkBufferMode", buffer_mode);
93 return buffer_mode_type;
96 GST_DEBUG_CATEGORY_STATIC (gst_file_sink_debug);
97 #define GST_CAT_DEFAULT gst_file_sink_debug
99 #define DEFAULT_LOCATION NULL
100 #define DEFAULT_BUFFER_MODE -1
101 #define DEFAULT_BUFFER_SIZE 64 * 1024
102 #define DEFAULT_APPEND FALSE
114 /* Copy of glib's g_fopen due to win32 libc/cross-DLL brokenness: we can't
115 * use the 'file pointer' opened in glib (and returned from this function)
116 * in this library, as they may have unrelated C runtimes. */
118 gst_fopen (const gchar * filename, const gchar * mode)
121 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
126 if (wfilename == NULL) {
131 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
139 retval = _wfopen (wfilename, wmode);
148 return fopen (filename, mode);
152 static void gst_file_sink_dispose (GObject * object);
154 static void gst_file_sink_set_property (GObject * object, guint prop_id,
155 const GValue * value, GParamSpec * pspec);
156 static void gst_file_sink_get_property (GObject * object, guint prop_id,
157 GValue * value, GParamSpec * pspec);
159 static gboolean gst_file_sink_open_file (GstFileSink * sink);
160 static void gst_file_sink_close_file (GstFileSink * sink);
162 static gboolean gst_file_sink_start (GstBaseSink * sink);
163 static gboolean gst_file_sink_stop (GstBaseSink * sink);
164 static gboolean gst_file_sink_event (GstBaseSink * sink, GstEvent * event);
165 static GstFlowReturn gst_file_sink_render (GstBaseSink * sink,
168 static gboolean gst_file_sink_do_seek (GstFileSink * filesink,
170 static gboolean gst_file_sink_get_current_offset (GstFileSink * filesink,
173 static gboolean gst_file_sink_query (GstBaseSink * bsink, GstQuery * query);
175 static void gst_file_sink_uri_handler_init (gpointer g_iface,
176 gpointer iface_data);
179 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_file_sink_uri_handler_init); \
180 GST_DEBUG_CATEGORY_INIT (gst_file_sink_debug, "filesink", 0, "filesink element");
181 #define gst_file_sink_parent_class parent_class
182 G_DEFINE_TYPE_WITH_CODE (GstFileSink, gst_file_sink, GST_TYPE_BASE_SINK,
186 gst_file_sink_class_init (GstFileSinkClass * klass)
188 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
189 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
190 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
192 gobject_class->dispose = gst_file_sink_dispose;
194 gobject_class->set_property = gst_file_sink_set_property;
195 gobject_class->get_property = gst_file_sink_get_property;
197 g_object_class_install_property (gobject_class, PROP_LOCATION,
198 g_param_spec_string ("location", "File Location",
199 "Location of the file to write", NULL,
200 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
202 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
203 g_param_spec_enum ("buffer-mode", "Buffering mode",
204 "The buffering mode to use", GST_TYPE_BUFFER_MODE,
205 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
207 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
208 g_param_spec_uint ("buffer-size", "Buffering size",
209 "Size of buffer in number of bytes for line or full buffer-mode", 0,
210 G_MAXUINT, DEFAULT_BUFFER_SIZE,
211 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216 * Append to an already existing file.
218 g_object_class_install_property (gobject_class, PROP_APPEND,
219 g_param_spec_boolean ("append", "Append",
220 "Append to an already existing file", DEFAULT_APPEND,
221 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223 gst_element_class_set_static_metadata (gstelement_class,
225 "Sink/File", "Write stream to a file",
226 "Thomas Vander Stichele <thomas at apestaart dot org>");
227 gst_element_class_add_pad_template (gstelement_class,
228 gst_static_pad_template_get (&sinktemplate));
230 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_file_sink_start);
231 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_file_sink_stop);
232 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_file_sink_query);
233 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_file_sink_render);
234 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_file_sink_event);
236 if (sizeof (off_t) < 8) {
237 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
243 gst_file_sink_init (GstFileSink * filesink)
245 filesink->filename = NULL;
246 filesink->file = NULL;
247 filesink->current_pos = 0;
248 filesink->buffer_mode = DEFAULT_BUFFER_MODE;
249 filesink->buffer_size = DEFAULT_BUFFER_SIZE;
250 filesink->buffer = NULL;
251 filesink->append = FALSE;
253 gst_base_sink_set_sync (GST_BASE_SINK (filesink), FALSE);
257 gst_file_sink_dispose (GObject * object)
259 GstFileSink *sink = GST_FILE_SINK (object);
261 G_OBJECT_CLASS (parent_class)->dispose (object);
265 g_free (sink->filename);
266 sink->filename = NULL;
267 g_free (sink->buffer);
269 sink->buffer_size = 0;
273 gst_file_sink_set_location (GstFileSink * sink, const gchar * location,
279 g_free (sink->filename);
281 if (location != NULL) {
282 /* we store the filename as we received it from the application. On Windows
283 * this should be in UTF8 */
284 sink->filename = g_strdup (location);
285 sink->uri = gst_filename_to_uri (location, NULL);
286 GST_INFO ("filename : %s", sink->filename);
287 GST_INFO ("uri : %s", sink->uri);
289 sink->filename = NULL;
298 g_warning ("Changing the `location' property on filesink when a file is "
299 "open is not supported.");
300 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
301 "Changing the 'location' property on filesink when a file is "
302 "open is not supported");
308 gst_file_sink_set_property (GObject * object, guint prop_id,
309 const GValue * value, GParamSpec * pspec)
311 GstFileSink *sink = GST_FILE_SINK (object);
315 gst_file_sink_set_location (sink, g_value_get_string (value), NULL);
317 case PROP_BUFFER_MODE:
318 sink->buffer_mode = g_value_get_enum (value);
320 case PROP_BUFFER_SIZE:
321 sink->buffer_size = g_value_get_uint (value);
324 sink->append = g_value_get_boolean (value);
327 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
333 gst_file_sink_get_property (GObject * object, guint prop_id, GValue * value,
336 GstFileSink *sink = GST_FILE_SINK (object);
340 g_value_set_string (value, sink->filename);
342 case PROP_BUFFER_MODE:
343 g_value_set_enum (value, sink->buffer_mode);
345 case PROP_BUFFER_SIZE:
346 g_value_set_uint (value, sink->buffer_size);
349 g_value_set_boolean (value, sink->append);
352 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
358 gst_file_sink_open_file (GstFileSink * sink)
363 if (sink->filename == NULL || sink->filename[0] == '\0')
367 sink->file = gst_fopen (sink->filename, "ab");
369 sink->file = gst_fopen (sink->filename, "wb");
370 if (sink->file == NULL)
373 /* see if we are asked to perform a specific kind of buffering */
374 if ((mode = sink->buffer_mode) != -1) {
377 /* free previous buffer if any */
378 g_free (sink->buffer);
380 if (mode == _IONBF) {
385 /* allocate buffer */
386 sink->buffer = g_malloc (sink->buffer_size);
387 buffer_size = sink->buffer_size;
389 /* Cygwin does not have __fbufsize */
390 #if defined(HAVE_STDIO_EXT_H) && !defined(__CYGWIN__)
391 GST_DEBUG_OBJECT (sink, "change buffer size %u to %u, mode %d",
392 (guint) __fbufsize (sink->file), buffer_size, mode);
394 GST_DEBUG_OBJECT (sink, "change buffer size to %u, mode %d",
395 sink->buffer_size, mode);
397 if (setvbuf (sink->file, sink->buffer, mode, buffer_size) != 0) {
398 GST_WARNING_OBJECT (sink, "warning: setvbuf failed: %s",
403 sink->current_pos = 0;
404 /* try to seek in the file to figure out if it is seekable */
405 sink->seekable = gst_file_sink_do_seek (sink, 0);
407 GST_DEBUG_OBJECT (sink, "opened file %s, seekable %d",
408 sink->filename, sink->seekable);
415 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
416 (_("No file name specified for writing.")), (NULL));
421 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
422 (_("Could not open file \"%s\" for writing."), sink->filename),
429 gst_file_sink_close_file (GstFileSink * sink)
432 if (fclose (sink->file) != 0)
435 GST_DEBUG_OBJECT (sink, "closed file");
438 g_free (sink->buffer);
446 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
447 (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
453 gst_file_sink_query (GstBaseSink * bsink, GstQuery * query)
459 self = GST_FILE_SINK (bsink);
461 switch (GST_QUERY_TYPE (query)) {
462 case GST_QUERY_POSITION:
463 gst_query_parse_position (query, &format, NULL);
466 case GST_FORMAT_DEFAULT:
467 case GST_FORMAT_BYTES:
468 gst_query_set_position (query, GST_FORMAT_BYTES, self->current_pos);
477 case GST_QUERY_FORMATS:
478 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
483 gst_query_set_uri (query, self->uri);
487 case GST_QUERY_SEEKING:
488 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
489 if (format == GST_FORMAT_BYTES || format == GST_FORMAT_DEFAULT) {
490 gst_query_set_seeking (query, GST_FORMAT_BYTES, self->seekable, 0, -1);
492 gst_query_set_seeking (query, format, FALSE, 0, -1);
498 res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
505 # define __GST_STDIO_SEEK_FUNCTION "fseeko"
506 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
507 # define __GST_STDIO_SEEK_FUNCTION "lseek"
509 # define __GST_STDIO_SEEK_FUNCTION "fseek"
513 gst_file_sink_do_seek (GstFileSink * filesink, guint64 new_offset)
515 GST_DEBUG_OBJECT (filesink, "Seeking to offset %" G_GUINT64_FORMAT
516 " using " __GST_STDIO_SEEK_FUNCTION, new_offset);
518 if (fflush (filesink->file))
522 if (fseeko (filesink->file, (off_t) new_offset, SEEK_SET) != 0)
524 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
525 if (lseek (fileno (filesink->file), (off_t) new_offset,
526 SEEK_SET) == (off_t) - 1)
529 if (fseek (filesink->file, (long) new_offset, SEEK_SET) != 0)
533 /* adjust position reporting after seek;
534 * presumably this should basically yield new_offset */
535 gst_file_sink_get_current_offset (filesink, &filesink->current_pos);
542 GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
547 GST_DEBUG_OBJECT (filesink, "Seeking failed: %s", g_strerror (errno));
552 /* handle events (search) */
554 gst_file_sink_event (GstBaseSink * sink, GstEvent * event)
557 GstFileSink *filesink;
559 filesink = GST_FILE_SINK (sink);
561 type = GST_EVENT_TYPE (event);
564 case GST_EVENT_SEGMENT:
566 const GstSegment *segment;
568 gst_event_parse_segment (event, &segment);
570 if (segment->format == GST_FORMAT_BYTES) {
571 /* only try to seek and fail when we are going to a different
573 if (filesink->current_pos != segment->start) {
574 /* FIXME, the seek should be performed on the pos field, start/stop are
575 * just boundaries for valid bytes offsets. We should also fill the file
576 * with zeroes if the new position extends the current EOF (sparse streams
577 * and segment accumulation). */
578 if (!gst_file_sink_do_seek (filesink, (guint64) segment->start))
581 GST_DEBUG_OBJECT (filesink, "Ignored SEGMENT, no seek needed");
584 GST_DEBUG_OBJECT (filesink,
585 "Ignored SEGMENT event of format %u (%s)", (guint) segment->format,
586 gst_format_get_name (segment->format));
590 case GST_EVENT_FLUSH_STOP:
591 if (filesink->current_pos != 0 && filesink->seekable) {
592 gst_file_sink_do_seek (filesink, 0);
593 ftruncate (fileno (filesink->file), 0);
597 if (fflush (filesink->file))
604 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
609 GST_ELEMENT_ERROR (filesink, RESOURCE, SEEK,
610 (_("Error while seeking in file \"%s\"."), filesink->filename),
612 gst_event_unref (event);
617 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
618 (_("Error while writing to file \"%s\"."), filesink->filename),
620 gst_event_unref (event);
626 gst_file_sink_get_current_offset (GstFileSink * filesink, guint64 * p_pos)
631 ret = ftello (filesink->file);
632 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
633 if (fflush (filesink->file)) {
634 GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
635 /* ignore and continue */
637 ret = lseek (fileno (filesink->file), 0, SEEK_CUR);
639 ret = (off_t) ftell (filesink->file);
642 if (ret != (off_t) - 1)
643 *p_pos = (guint64) ret;
645 return (ret != (off_t) - 1);
649 gst_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
651 GstFileSink *filesink;
654 filesink = GST_FILE_SINK (sink);
656 gst_buffer_map (buffer, &info, GST_MAP_READ);
658 GST_DEBUG_OBJECT (filesink,
659 "writing %" G_GSIZE_FORMAT " bytes at %" G_GUINT64_FORMAT,
660 info.size, filesink->current_pos);
662 if (info.size > 0 && info.data != NULL) {
663 if (fwrite (info.data, info.size, 1, filesink->file) != 1)
666 filesink->current_pos += info.size;
668 gst_buffer_unmap (buffer, &info);
676 GST_ELEMENT_ERROR (filesink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
680 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
681 (_("Error while writing to file \"%s\"."), filesink->filename),
682 ("%s", g_strerror (errno)));
685 gst_buffer_unmap (buffer, &info);
686 return GST_FLOW_ERROR;
691 gst_file_sink_start (GstBaseSink * basesink)
693 return gst_file_sink_open_file (GST_FILE_SINK (basesink));
697 gst_file_sink_stop (GstBaseSink * basesink)
699 gst_file_sink_close_file (GST_FILE_SINK (basesink));
703 /*** GSTURIHANDLER INTERFACE *************************************************/
706 gst_file_sink_uri_get_type (GType type)
711 static const gchar *const *
712 gst_file_sink_uri_get_protocols (GType type)
714 static const gchar *protocols[] = { "file", NULL };
720 gst_file_sink_uri_get_uri (GstURIHandler * handler)
722 GstFileSink *sink = GST_FILE_SINK (handler);
724 /* FIXME: make thread-safe */
725 return g_strdup (sink->uri);
729 gst_file_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
734 GstFileSink *sink = GST_FILE_SINK (handler);
736 /* allow file://localhost/foo/bar by stripping localhost but fail
737 * for every other hostname */
738 if (g_str_has_prefix (uri, "file://localhost/")) {
741 /* 16 == strlen ("file://localhost") */
742 tmp = g_strconcat ("file://", uri + 16, NULL);
743 /* we use gst_uri_get_location() although we already have the
744 * "location" with uri + 16 because it provides unescaping */
745 location = gst_uri_get_location (tmp);
747 } else if (strcmp (uri, "file://") == 0) {
748 /* Special case for "file://" as this is used by some applications
749 * to test with gst_element_make_from_uri if there's an element
750 * that supports the URI protocol. */
751 gst_file_sink_set_location (sink, NULL, NULL);
754 location = gst_uri_get_location (uri);
758 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
759 "File URI without location");
763 if (!g_path_is_absolute (location)) {
764 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
765 "File URI location must be an absolute path");
770 ret = gst_file_sink_set_location (sink, location, error);
777 gst_file_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
779 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
781 iface->get_type = gst_file_sink_uri_get_type;
782 iface->get_protocols = gst_file_sink_uri_get_protocols;
783 iface->get_uri = gst_file_sink_uri_get_uri;
784 iface->set_uri = gst_file_sink_uri_set_uri;