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-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 #ifdef __BIONIC__ /* Android */
80 #include "gstelements_private.h"
81 #include "gstfilesink.h"
83 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
88 #define GST_TYPE_FILE_SINK_BUFFER_MODE (gst_file_sink_buffer_mode_get_type ())
90 gst_file_sink_buffer_mode_get_type (void)
92 static GType buffer_mode_type = 0;
93 static const GEnumValue buffer_mode[] = {
94 {GST_FILE_SINK_BUFFER_MODE_DEFAULT, "Default buffering", "default"},
95 {GST_FILE_SINK_BUFFER_MODE_FULL, "Fully buffered", "full"},
96 {GST_FILE_SINK_BUFFER_MODE_LINE, "Line buffered", "line"},
97 {GST_FILE_SINK_BUFFER_MODE_UNBUFFERED, "Unbuffered", "unbuffered"},
101 if (!buffer_mode_type) {
103 g_enum_register_static ("GstFileSinkBufferMode", buffer_mode);
105 return buffer_mode_type;
108 GST_DEBUG_CATEGORY_STATIC (gst_file_sink_debug);
109 #define GST_CAT_DEFAULT gst_file_sink_debug
111 #define DEFAULT_LOCATION NULL
112 #define DEFAULT_BUFFER_MODE GST_FILE_SINK_BUFFER_MODE_DEFAULT
113 #define DEFAULT_BUFFER_SIZE 64 * 1024
114 #define DEFAULT_APPEND FALSE
126 /* Copy of glib's g_fopen due to win32 libc/cross-DLL brokenness: we can't
127 * use the 'file pointer' opened in glib (and returned from this function)
128 * in this library, as they may have unrelated C runtimes. */
130 gst_fopen (const gchar * filename, const gchar * mode)
133 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
138 if (wfilename == NULL) {
143 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
151 retval = _wfopen (wfilename, wmode);
160 return fopen (filename, mode);
164 static void gst_file_sink_dispose (GObject * object);
166 static void gst_file_sink_set_property (GObject * object, guint prop_id,
167 const GValue * value, GParamSpec * pspec);
168 static void gst_file_sink_get_property (GObject * object, guint prop_id,
169 GValue * value, GParamSpec * pspec);
171 static gboolean gst_file_sink_open_file (GstFileSink * sink);
172 static void gst_file_sink_close_file (GstFileSink * sink);
174 static gboolean gst_file_sink_start (GstBaseSink * sink);
175 static gboolean gst_file_sink_stop (GstBaseSink * sink);
176 static gboolean gst_file_sink_event (GstBaseSink * sink, GstEvent * event);
177 static GstFlowReturn gst_file_sink_render (GstBaseSink * sink,
179 static GstFlowReturn gst_file_sink_render_list (GstBaseSink * sink,
180 GstBufferList * list);
182 static gboolean gst_file_sink_do_seek (GstFileSink * filesink,
184 static gboolean gst_file_sink_get_current_offset (GstFileSink * filesink,
187 static gboolean gst_file_sink_query (GstBaseSink * bsink, GstQuery * query);
189 static void gst_file_sink_uri_handler_init (gpointer g_iface,
190 gpointer iface_data);
193 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_file_sink_uri_handler_init); \
194 GST_DEBUG_CATEGORY_INIT (gst_file_sink_debug, "filesink", 0, "filesink element");
195 #define gst_file_sink_parent_class parent_class
196 G_DEFINE_TYPE_WITH_CODE (GstFileSink, gst_file_sink, GST_TYPE_BASE_SINK,
200 gst_file_sink_class_init (GstFileSinkClass * klass)
202 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
203 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
204 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
206 gobject_class->dispose = gst_file_sink_dispose;
208 gobject_class->set_property = gst_file_sink_set_property;
209 gobject_class->get_property = gst_file_sink_get_property;
211 g_object_class_install_property (gobject_class, PROP_LOCATION,
212 g_param_spec_string ("location", "File Location",
213 "Location of the file to write", NULL,
214 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
217 g_param_spec_enum ("buffer-mode", "Buffering mode",
218 "The buffering mode to use", GST_TYPE_FILE_SINK_BUFFER_MODE,
219 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
222 g_param_spec_uint ("buffer-size", "Buffering size",
223 "Size of buffer in number of bytes for line or full buffer-mode", 0,
224 G_MAXUINT, DEFAULT_BUFFER_SIZE,
225 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230 * Append to an already existing file.
232 g_object_class_install_property (gobject_class, PROP_APPEND,
233 g_param_spec_boolean ("append", "Append",
234 "Append to an already existing file", DEFAULT_APPEND,
235 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
237 gst_element_class_set_static_metadata (gstelement_class,
239 "Sink/File", "Write stream to a file",
240 "Thomas Vander Stichele <thomas at apestaart dot org>");
241 gst_element_class_add_pad_template (gstelement_class,
242 gst_static_pad_template_get (&sinktemplate));
244 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_file_sink_start);
245 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_file_sink_stop);
246 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_file_sink_query);
247 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_file_sink_render);
248 gstbasesink_class->render_list =
249 GST_DEBUG_FUNCPTR (gst_file_sink_render_list);
250 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_file_sink_event);
252 if (sizeof (off_t) < 8) {
253 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
259 gst_file_sink_init (GstFileSink * filesink)
261 filesink->filename = NULL;
262 filesink->file = NULL;
263 filesink->current_pos = 0;
264 filesink->buffer_mode = DEFAULT_BUFFER_MODE;
265 filesink->buffer_size = DEFAULT_BUFFER_SIZE;
266 filesink->buffer = NULL;
267 filesink->append = FALSE;
269 gst_base_sink_set_sync (GST_BASE_SINK (filesink), FALSE);
273 gst_file_sink_dispose (GObject * object)
275 GstFileSink *sink = GST_FILE_SINK (object);
277 G_OBJECT_CLASS (parent_class)->dispose (object);
281 g_free (sink->filename);
282 sink->filename = NULL;
283 g_free (sink->buffer);
285 sink->buffer_size = 0;
289 gst_file_sink_set_location (GstFileSink * sink, const gchar * location,
295 g_free (sink->filename);
297 if (location != NULL) {
298 /* we store the filename as we received it from the application. On Windows
299 * this should be in UTF8 */
300 sink->filename = g_strdup (location);
301 sink->uri = gst_filename_to_uri (location, NULL);
302 GST_INFO_OBJECT (sink, "filename : %s", sink->filename);
303 GST_INFO_OBJECT (sink, "uri : %s", sink->uri);
305 sink->filename = NULL;
314 g_warning ("Changing the `location' property on filesink when a file is "
315 "open is not supported.");
316 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
317 "Changing the 'location' property on filesink when a file is "
318 "open is not supported");
324 gst_file_sink_set_property (GObject * object, guint prop_id,
325 const GValue * value, GParamSpec * pspec)
327 GstFileSink *sink = GST_FILE_SINK (object);
331 gst_file_sink_set_location (sink, g_value_get_string (value), NULL);
333 case PROP_BUFFER_MODE:
334 sink->buffer_mode = g_value_get_enum (value);
336 case PROP_BUFFER_SIZE:
337 sink->buffer_size = g_value_get_uint (value);
340 sink->append = g_value_get_boolean (value);
343 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
349 gst_file_sink_get_property (GObject * object, guint prop_id, GValue * value,
352 GstFileSink *sink = GST_FILE_SINK (object);
356 g_value_set_string (value, sink->filename);
358 case PROP_BUFFER_MODE:
359 g_value_set_enum (value, sink->buffer_mode);
361 case PROP_BUFFER_SIZE:
362 g_value_set_uint (value, sink->buffer_size);
365 g_value_set_boolean (value, sink->append);
368 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
374 gst_file_sink_open_file (GstFileSink * sink)
379 if (sink->filename == NULL || sink->filename[0] == '\0')
383 sink->file = gst_fopen (sink->filename, "ab");
385 sink->file = gst_fopen (sink->filename, "wb");
386 if (sink->file == NULL)
389 /* see if we are asked to perform a specific kind of buffering */
390 if ((mode = sink->buffer_mode) != -1) {
393 /* free previous buffer if any */
394 g_free (sink->buffer);
396 if (mode == _IONBF) {
401 /* allocate buffer */
402 sink->buffer = g_malloc (sink->buffer_size);
403 buffer_size = sink->buffer_size;
405 /* Cygwin does not have __fbufsize */
406 #if defined(HAVE_STDIO_EXT_H) && !defined(__CYGWIN__)
407 GST_DEBUG_OBJECT (sink, "change buffer size %u to %u, mode %d",
408 (guint) __fbufsize (sink->file), buffer_size, mode);
410 GST_DEBUG_OBJECT (sink, "change buffer size to %u, mode %d",
411 sink->buffer_size, mode);
413 if (setvbuf (sink->file, sink->buffer, mode, buffer_size) != 0) {
414 GST_WARNING_OBJECT (sink, "warning: setvbuf failed: %s",
419 sink->current_pos = 0;
420 /* try to seek in the file to figure out if it is seekable */
421 sink->seekable = gst_file_sink_do_seek (sink, 0);
423 GST_DEBUG_OBJECT (sink, "opened file %s, seekable %d",
424 sink->filename, sink->seekable);
431 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
432 (_("No file name specified for writing.")), (NULL));
437 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
438 (_("Could not open file \"%s\" for writing."), sink->filename),
445 gst_file_sink_close_file (GstFileSink * sink)
448 if (fclose (sink->file) != 0)
449 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
450 (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
452 GST_DEBUG_OBJECT (sink, "closed file");
455 g_free (sink->buffer);
461 gst_file_sink_query (GstBaseSink * bsink, GstQuery * query)
467 self = GST_FILE_SINK (bsink);
469 switch (GST_QUERY_TYPE (query)) {
470 case GST_QUERY_POSITION:
471 gst_query_parse_position (query, &format, NULL);
474 case GST_FORMAT_DEFAULT:
475 case GST_FORMAT_BYTES:
476 gst_query_set_position (query, GST_FORMAT_BYTES, self->current_pos);
485 case GST_QUERY_FORMATS:
486 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
491 gst_query_set_uri (query, self->uri);
495 case GST_QUERY_SEEKING:
496 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
497 if (format == GST_FORMAT_BYTES || format == GST_FORMAT_DEFAULT) {
498 gst_query_set_seeking (query, GST_FORMAT_BYTES, self->seekable, 0, -1);
500 gst_query_set_seeking (query, format, FALSE, 0, -1);
506 res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
513 # define __GST_STDIO_SEEK_FUNCTION "fseeko"
514 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
515 # define __GST_STDIO_SEEK_FUNCTION "lseek"
517 # define __GST_STDIO_SEEK_FUNCTION "fseek"
521 gst_file_sink_do_seek (GstFileSink * filesink, guint64 new_offset)
523 GST_DEBUG_OBJECT (filesink, "Seeking to offset %" G_GUINT64_FORMAT
524 " using " __GST_STDIO_SEEK_FUNCTION, new_offset);
526 if (fflush (filesink->file))
530 if (fseeko (filesink->file, (off_t) new_offset, SEEK_SET) != 0)
532 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
533 if (lseek (fileno (filesink->file), (off_t) new_offset,
534 SEEK_SET) == (off_t) - 1)
537 if (fseek (filesink->file, (long) new_offset, SEEK_SET) != 0)
541 /* adjust position reporting after seek;
542 * presumably this should basically yield new_offset */
543 gst_file_sink_get_current_offset (filesink, &filesink->current_pos);
550 GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
555 GST_DEBUG_OBJECT (filesink, "Seeking failed: %s", g_strerror (errno));
560 /* handle events (search) */
562 gst_file_sink_event (GstBaseSink * sink, GstEvent * event)
565 GstFileSink *filesink;
567 filesink = GST_FILE_SINK (sink);
569 type = GST_EVENT_TYPE (event);
572 case GST_EVENT_SEGMENT:
574 const GstSegment *segment;
576 gst_event_parse_segment (event, &segment);
578 if (segment->format == GST_FORMAT_BYTES) {
579 /* only try to seek and fail when we are going to a different
581 if (filesink->current_pos != segment->start) {
582 /* FIXME, the seek should be performed on the pos field, start/stop are
583 * just boundaries for valid bytes offsets. We should also fill the file
584 * with zeroes if the new position extends the current EOF (sparse streams
585 * and segment accumulation). */
586 if (!gst_file_sink_do_seek (filesink, (guint64) segment->start))
589 GST_DEBUG_OBJECT (filesink, "Ignored SEGMENT, no seek needed");
592 GST_DEBUG_OBJECT (filesink,
593 "Ignored SEGMENT event of format %u (%s)", (guint) segment->format,
594 gst_format_get_name (segment->format));
598 case GST_EVENT_FLUSH_STOP:
599 if (filesink->current_pos != 0 && filesink->seekable) {
600 gst_file_sink_do_seek (filesink, 0);
601 if (ftruncate (fileno (filesink->file), 0))
606 if (fflush (filesink->file))
613 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
618 GST_ELEMENT_ERROR (filesink, RESOURCE, SEEK,
619 (_("Error while seeking in file \"%s\"."), filesink->filename),
621 gst_event_unref (event);
626 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
627 (_("Error while writing to file \"%s\"."), filesink->filename),
629 gst_event_unref (event);
635 gst_file_sink_get_current_offset (GstFileSink * filesink, guint64 * p_pos)
640 ret = ftello (filesink->file);
641 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
642 if (fflush (filesink->file)) {
643 GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
644 /* ignore and continue */
646 ret = lseek (fileno (filesink->file), 0, SEEK_CUR);
648 ret = (off_t) ftell (filesink->file);
651 if (ret != (off_t) - 1)
652 *p_pos = (guint64) ret;
654 return (ret != (off_t) - 1);
658 gst_file_sink_render_buffers (GstFileSink * sink, GstBuffer ** buffers,
659 guint num_buffers, guint8 * mem_nums, guint total_mems)
661 GST_DEBUG_OBJECT (sink,
662 "writing %u buffers (%u memories) at position %" G_GUINT64_FORMAT,
663 num_buffers, total_mems, sink->current_pos);
665 return gst_writev_buffers (GST_OBJECT_CAST (sink), fileno (sink->file), NULL,
666 buffers, num_buffers, mem_nums, total_mems, NULL, &sink->current_pos);
670 gst_file_sink_render_list (GstBaseSink * bsink, GstBufferList * buffer_list)
677 guint i, num_buffers;
678 gboolean sync_after = FALSE;
680 sink = GST_FILE_SINK_CAST (bsink);
682 num_buffers = gst_buffer_list_length (buffer_list);
683 if (num_buffers == 0)
686 /* extract buffers from list and count memories */
687 buffers = g_newa (GstBuffer *, num_buffers);
688 mem_nums = g_newa (guint8, num_buffers);
689 for (i = 0, total_mems = 0; i < num_buffers; ++i) {
690 buffers[i] = gst_buffer_list_get (buffer_list, i);
691 mem_nums[i] = gst_buffer_n_memory (buffers[i]);
692 total_mems += mem_nums[i];
693 if (GST_BUFFER_FLAG_IS_SET (buffers[i], GST_BUFFER_FLAG_SYNC_AFTER))
698 gst_file_sink_render_buffers (sink, buffers, num_buffers, mem_nums,
701 if (flow == GST_FLOW_OK && sync_after) {
702 if (fflush (sink->file) || fsync (fileno (sink->file))) {
703 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
704 (_("Error while writing to file \"%s\"."), sink->filename),
705 ("%s", g_strerror (errno)));
706 flow = GST_FLOW_ERROR;
714 GST_LOG_OBJECT (sink, "empty buffer list");
720 gst_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
722 GstFileSink *filesink;
726 filesink = GST_FILE_SINK_CAST (sink);
728 n_mem = gst_buffer_n_memory (buffer);
731 flow = gst_file_sink_render_buffers (filesink, &buffer, 1, &n_mem, n_mem);
735 if (flow == GST_FLOW_OK &&
736 GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_SYNC_AFTER)) {
737 if (fflush (filesink->file) || fsync (fileno (filesink->file))) {
738 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
739 (_("Error while writing to file \"%s\"."), filesink->filename),
740 ("%s", g_strerror (errno)));
741 flow = GST_FLOW_ERROR;
749 gst_file_sink_start (GstBaseSink * basesink)
751 return gst_file_sink_open_file (GST_FILE_SINK (basesink));
755 gst_file_sink_stop (GstBaseSink * basesink)
757 gst_file_sink_close_file (GST_FILE_SINK (basesink));
761 /*** GSTURIHANDLER INTERFACE *************************************************/
764 gst_file_sink_uri_get_type (GType type)
769 static const gchar *const *
770 gst_file_sink_uri_get_protocols (GType type)
772 static const gchar *protocols[] = { "file", NULL };
778 gst_file_sink_uri_get_uri (GstURIHandler * handler)
780 GstFileSink *sink = GST_FILE_SINK (handler);
782 /* FIXME: make thread-safe */
783 return g_strdup (sink->uri);
787 gst_file_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
792 GstFileSink *sink = GST_FILE_SINK (handler);
794 /* allow file://localhost/foo/bar by stripping localhost but fail
795 * for every other hostname */
796 if (g_str_has_prefix (uri, "file://localhost/")) {
799 /* 16 == strlen ("file://localhost") */
800 tmp = g_strconcat ("file://", uri + 16, NULL);
801 /* we use gst_uri_get_location() although we already have the
802 * "location" with uri + 16 because it provides unescaping */
803 location = gst_uri_get_location (tmp);
805 } else if (strcmp (uri, "file://") == 0) {
806 /* Special case for "file://" as this is used by some applications
807 * to test with gst_element_make_from_uri if there's an element
808 * that supports the URI protocol. */
809 gst_file_sink_set_location (sink, NULL, NULL);
812 location = gst_uri_get_location (uri);
816 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
817 "File URI without location");
821 if (!g_path_is_absolute (location)) {
822 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
823 "File URI location must be an absolute path");
828 ret = gst_file_sink_set_location (sink, location, error);
835 gst_file_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
837 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
839 iface->get_type = gst_file_sink_uri_get_type;
840 iface->get_protocols = gst_file_sink_uri_get_protocols;
841 iface->get_uri = gst_file_sink_uri_get_uri;
842 iface->set_uri = gst_file_sink_uri_set_uri;