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
26 * @see_also: #GstFileSrc
28 * Write incoming data to a file in the local file system.
30 * ## Example launch line
32 * gst-launch-1.0 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
63 #ifdef _MSC_VER /* Check if we are using MSVC, fileno is deprecated in favour */
64 #define fileno _fileno /* of _fileno */
73 #include "gstelements_private.h"
74 #include "gstfilesink.h"
76 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
81 #define GST_TYPE_FILE_SINK_BUFFER_MODE (gst_file_sink_buffer_mode_get_type ())
83 gst_file_sink_buffer_mode_get_type (void)
85 static GType buffer_mode_type = 0;
86 static const GEnumValue buffer_mode[] = {
87 {GST_FILE_SINK_BUFFER_MODE_DEFAULT, "Default buffering", "default"},
88 {GST_FILE_SINK_BUFFER_MODE_FULL, "Fully buffered", "full"},
89 {GST_FILE_SINK_BUFFER_MODE_LINE, "Line buffered", "line"},
90 {GST_FILE_SINK_BUFFER_MODE_UNBUFFERED, "Unbuffered", "unbuffered"},
94 if (!buffer_mode_type) {
96 g_enum_register_static ("GstFileSinkBufferMode", buffer_mode);
98 return buffer_mode_type;
101 GST_DEBUG_CATEGORY_STATIC (gst_file_sink_debug);
102 #define GST_CAT_DEFAULT gst_file_sink_debug
104 #define DEFAULT_LOCATION NULL
105 #define DEFAULT_BUFFER_MODE GST_FILE_SINK_BUFFER_MODE_DEFAULT
106 #define DEFAULT_BUFFER_SIZE 64 * 1024
107 #define DEFAULT_APPEND FALSE
119 /* Copy of glib's g_fopen due to win32 libc/cross-DLL brokenness: we can't
120 * use the 'file pointer' opened in glib (and returned from this function)
121 * in this library, as they may have unrelated C runtimes. */
123 gst_fopen (const gchar * filename, const gchar * mode)
126 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
131 if (wfilename == NULL) {
136 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
144 retval = _wfopen (wfilename, wmode);
153 return fopen (filename, mode);
157 static void gst_file_sink_dispose (GObject * object);
159 static void gst_file_sink_set_property (GObject * object, guint prop_id,
160 const GValue * value, GParamSpec * pspec);
161 static void gst_file_sink_get_property (GObject * object, guint prop_id,
162 GValue * value, GParamSpec * pspec);
164 static gboolean gst_file_sink_open_file (GstFileSink * sink);
165 static void gst_file_sink_close_file (GstFileSink * sink);
167 static gboolean gst_file_sink_start (GstBaseSink * sink);
168 static gboolean gst_file_sink_stop (GstBaseSink * sink);
169 static gboolean gst_file_sink_event (GstBaseSink * sink, GstEvent * event);
170 static GstFlowReturn gst_file_sink_render (GstBaseSink * sink,
172 static GstFlowReturn gst_file_sink_render_list (GstBaseSink * sink,
173 GstBufferList * list);
175 static gboolean gst_file_sink_do_seek (GstFileSink * filesink,
177 static gboolean gst_file_sink_get_current_offset (GstFileSink * filesink,
180 static gboolean gst_file_sink_query (GstBaseSink * bsink, GstQuery * query);
182 static void gst_file_sink_uri_handler_init (gpointer g_iface,
183 gpointer iface_data);
186 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_file_sink_uri_handler_init); \
187 GST_DEBUG_CATEGORY_INIT (gst_file_sink_debug, "filesink", 0, "filesink element");
188 #define gst_file_sink_parent_class parent_class
189 G_DEFINE_TYPE_WITH_CODE (GstFileSink, gst_file_sink, GST_TYPE_BASE_SINK,
193 gst_file_sink_class_init (GstFileSinkClass * klass)
195 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
196 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
197 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
199 gobject_class->dispose = gst_file_sink_dispose;
201 gobject_class->set_property = gst_file_sink_set_property;
202 gobject_class->get_property = gst_file_sink_get_property;
204 g_object_class_install_property (gobject_class, PROP_LOCATION,
205 g_param_spec_string ("location", "File Location",
206 "Location of the file to write", NULL,
207 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
209 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
210 g_param_spec_enum ("buffer-mode", "Buffering mode",
211 "The buffering mode to use", GST_TYPE_FILE_SINK_BUFFER_MODE,
212 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
215 g_param_spec_uint ("buffer-size", "Buffering size",
216 "Size of buffer in number of bytes for line or full buffer-mode", 0,
217 G_MAXUINT, DEFAULT_BUFFER_SIZE,
218 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223 * Append to an already existing file.
225 g_object_class_install_property (gobject_class, PROP_APPEND,
226 g_param_spec_boolean ("append", "Append",
227 "Append to an already existing file", DEFAULT_APPEND,
228 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230 gst_element_class_set_static_metadata (gstelement_class,
232 "Sink/File", "Write stream to a file",
233 "Thomas Vander Stichele <thomas at apestaart dot org>");
234 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
236 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_file_sink_start);
237 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_file_sink_stop);
238 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_file_sink_query);
239 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_file_sink_render);
240 gstbasesink_class->render_list =
241 GST_DEBUG_FUNCPTR (gst_file_sink_render_list);
242 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_file_sink_event);
244 if (sizeof (off_t) < 8) {
245 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
251 gst_file_sink_init (GstFileSink * filesink)
253 filesink->filename = NULL;
254 filesink->file = NULL;
255 filesink->current_pos = 0;
256 filesink->buffer_mode = DEFAULT_BUFFER_MODE;
257 filesink->buffer_size = DEFAULT_BUFFER_SIZE;
258 filesink->buffer = NULL;
259 filesink->append = FALSE;
261 gst_base_sink_set_sync (GST_BASE_SINK (filesink), FALSE);
265 gst_file_sink_dispose (GObject * object)
267 GstFileSink *sink = GST_FILE_SINK (object);
269 G_OBJECT_CLASS (parent_class)->dispose (object);
273 g_free (sink->filename);
274 sink->filename = NULL;
275 g_free (sink->buffer);
277 sink->buffer_size = 0;
281 gst_file_sink_set_location (GstFileSink * sink, const gchar * location,
287 g_free (sink->filename);
289 if (location != NULL) {
290 /* we store the filename as we received it from the application. On Windows
291 * this should be in UTF8 */
292 sink->filename = g_strdup (location);
293 sink->uri = gst_filename_to_uri (location, NULL);
294 GST_INFO_OBJECT (sink, "filename : %s", sink->filename);
295 GST_INFO_OBJECT (sink, "uri : %s", sink->uri);
297 sink->filename = NULL;
306 g_warning ("Changing the `location' property on filesink when a file is "
307 "open is not supported.");
308 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
309 "Changing the 'location' property on filesink when a file is "
310 "open is not supported");
316 gst_file_sink_set_property (GObject * object, guint prop_id,
317 const GValue * value, GParamSpec * pspec)
319 GstFileSink *sink = GST_FILE_SINK (object);
323 gst_file_sink_set_location (sink, g_value_get_string (value), NULL);
325 case PROP_BUFFER_MODE:
326 sink->buffer_mode = g_value_get_enum (value);
328 case PROP_BUFFER_SIZE:
329 sink->buffer_size = g_value_get_uint (value);
332 sink->append = g_value_get_boolean (value);
335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
341 gst_file_sink_get_property (GObject * object, guint prop_id, GValue * value,
344 GstFileSink *sink = GST_FILE_SINK (object);
348 g_value_set_string (value, sink->filename);
350 case PROP_BUFFER_MODE:
351 g_value_set_enum (value, sink->buffer_mode);
353 case PROP_BUFFER_SIZE:
354 g_value_set_uint (value, sink->buffer_size);
357 g_value_set_boolean (value, sink->append);
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
366 gst_file_sink_open_file (GstFileSink * sink)
371 if (sink->filename == NULL || sink->filename[0] == '\0')
375 sink->file = gst_fopen (sink->filename, "ab");
377 sink->file = gst_fopen (sink->filename, "wb");
378 if (sink->file == NULL)
381 /* see if we are asked to perform a specific kind of buffering */
382 if ((mode = sink->buffer_mode) != -1) {
385 /* free previous buffer if any */
386 g_free (sink->buffer);
388 if (mode == _IONBF) {
393 /* allocate buffer */
394 sink->buffer = g_malloc (sink->buffer_size);
395 buffer_size = sink->buffer_size;
397 /* Cygwin does not have __fbufsize, android adds it in API 23 */
398 #if defined(HAVE_STDIO_EXT_H) && (!defined(__CYGWIN__) && (!defined(__ANDROID_API__) || __ANDROID_API__ >= 23))
399 GST_DEBUG_OBJECT (sink, "change buffer size %u to %u, mode %d",
400 (guint) __fbufsize (sink->file), buffer_size, mode);
402 GST_DEBUG_OBJECT (sink, "change buffer size to %u, mode %d",
403 sink->buffer_size, mode);
405 if (setvbuf (sink->file, sink->buffer, mode, buffer_size) != 0) {
406 GST_WARNING_OBJECT (sink, "warning: setvbuf failed: %s",
411 sink->current_pos = 0;
412 /* try to seek in the file to figure out if it is seekable */
413 sink->seekable = gst_file_sink_do_seek (sink, 0);
415 GST_DEBUG_OBJECT (sink, "opened file %s, seekable %d",
416 sink->filename, sink->seekable);
423 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
424 (_("No file name specified for writing.")), (NULL));
429 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
430 (_("Could not open file \"%s\" for writing."), sink->filename),
437 gst_file_sink_close_file (GstFileSink * sink)
440 if (fclose (sink->file) != 0)
441 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
442 (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
444 GST_DEBUG_OBJECT (sink, "closed file");
447 g_free (sink->buffer);
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 if (ftruncate (fileno (filesink->file), 0))
598 if (fflush (filesink->file))
605 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
610 GST_ELEMENT_ERROR (filesink, RESOURCE, SEEK,
611 (_("Error while seeking in file \"%s\"."), filesink->filename),
613 gst_event_unref (event);
618 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
619 (_("Error while writing to file \"%s\"."), filesink->filename),
621 gst_event_unref (event);
627 gst_file_sink_get_current_offset (GstFileSink * filesink, guint64 * p_pos)
632 ret = ftello (filesink->file);
633 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
634 if (fflush (filesink->file)) {
635 GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
636 /* ignore and continue */
638 ret = lseek (fileno (filesink->file), 0, SEEK_CUR);
640 ret = (off_t) ftell (filesink->file);
643 if (ret != (off_t) - 1)
644 *p_pos = (guint64) ret;
646 return (ret != (off_t) - 1);
650 gst_file_sink_render_buffers (GstFileSink * sink, GstBuffer ** buffers,
651 guint num_buffers, guint8 * mem_nums, guint total_mems)
653 GST_DEBUG_OBJECT (sink,
654 "writing %u buffers (%u memories) at position %" G_GUINT64_FORMAT,
655 num_buffers, total_mems, sink->current_pos);
657 return gst_writev_buffers (GST_OBJECT_CAST (sink), fileno (sink->file), NULL,
658 buffers, num_buffers, mem_nums, total_mems, &sink->current_pos, 0);
662 gst_file_sink_render_list (GstBaseSink * bsink, GstBufferList * buffer_list)
669 guint i, num_buffers;
670 gboolean sync_after = FALSE;
672 sink = GST_FILE_SINK_CAST (bsink);
674 num_buffers = gst_buffer_list_length (buffer_list);
675 if (num_buffers == 0)
678 /* extract buffers from list and count memories */
679 buffers = g_newa (GstBuffer *, num_buffers);
680 mem_nums = g_newa (guint8, num_buffers);
681 for (i = 0, total_mems = 0; i < num_buffers; ++i) {
682 buffers[i] = gst_buffer_list_get (buffer_list, i);
683 mem_nums[i] = gst_buffer_n_memory (buffers[i]);
684 total_mems += mem_nums[i];
685 if (GST_BUFFER_FLAG_IS_SET (buffers[i], GST_BUFFER_FLAG_SYNC_AFTER))
690 gst_file_sink_render_buffers (sink, buffers, num_buffers, mem_nums,
693 if (flow == GST_FLOW_OK && sync_after) {
694 if (fflush (sink->file) || fsync (fileno (sink->file))) {
695 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
696 (_("Error while writing to file \"%s\"."), sink->filename),
697 ("%s", g_strerror (errno)));
698 flow = GST_FLOW_ERROR;
706 GST_LOG_OBJECT (sink, "empty buffer list");
712 gst_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
714 GstFileSink *filesink;
718 filesink = GST_FILE_SINK_CAST (sink);
720 n_mem = gst_buffer_n_memory (buffer);
723 flow = gst_file_sink_render_buffers (filesink, &buffer, 1, &n_mem, n_mem);
727 if (flow == GST_FLOW_OK &&
728 GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_SYNC_AFTER)) {
729 if (fflush (filesink->file) || fsync (fileno (filesink->file))) {
730 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
731 (_("Error while writing to file \"%s\"."), filesink->filename),
732 ("%s", g_strerror (errno)));
733 flow = GST_FLOW_ERROR;
741 gst_file_sink_start (GstBaseSink * basesink)
743 return gst_file_sink_open_file (GST_FILE_SINK (basesink));
747 gst_file_sink_stop (GstBaseSink * basesink)
749 gst_file_sink_close_file (GST_FILE_SINK (basesink));
753 /*** GSTURIHANDLER INTERFACE *************************************************/
756 gst_file_sink_uri_get_type (GType type)
761 static const gchar *const *
762 gst_file_sink_uri_get_protocols (GType type)
764 static const gchar *protocols[] = { "file", NULL };
770 gst_file_sink_uri_get_uri (GstURIHandler * handler)
772 GstFileSink *sink = GST_FILE_SINK (handler);
774 /* FIXME: make thread-safe */
775 return g_strdup (sink->uri);
779 gst_file_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
784 GstFileSink *sink = GST_FILE_SINK (handler);
786 /* allow file://localhost/foo/bar by stripping localhost but fail
787 * for every other hostname */
788 if (g_str_has_prefix (uri, "file://localhost/")) {
791 /* 16 == strlen ("file://localhost") */
792 tmp = g_strconcat ("file://", uri + 16, NULL);
793 /* we use gst_uri_get_location() although we already have the
794 * "location" with uri + 16 because it provides unescaping */
795 location = gst_uri_get_location (tmp);
797 } else if (strcmp (uri, "file://") == 0) {
798 /* Special case for "file://" as this is used by some applications
799 * to test with gst_element_make_from_uri if there's an element
800 * that supports the URI protocol. */
801 gst_file_sink_set_location (sink, NULL, NULL);
804 location = gst_uri_get_location (uri);
808 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
809 "File URI without location");
813 if (!g_path_is_absolute (location)) {
814 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
815 "File URI location must be an absolute path");
820 ret = gst_file_sink_set_location (sink, location, error);
827 gst_file_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
829 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
831 iface->get_type = gst_file_sink_uri_get_type;
832 iface->get_protocols = gst_file_sink_uri_get_protocols;
833 iface->get_uri = gst_file_sink_uri_get_uri;
834 iface->set_uri = gst_file_sink_uri_set_uri;