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 #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
123 #ifdef TIZEN_FEATURE_FILESINK_MODIFICATION
129 /* Copy of glib's g_fopen due to win32 libc/cross-DLL brokenness: we can't
130 * use the 'file pointer' opened in glib (and returned from this function)
131 * in this library, as they may have unrelated C runtimes. */
133 gst_fopen (const gchar * filename, const gchar * mode)
136 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
141 if (wfilename == NULL) {
146 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
154 retval = _wfopen (wfilename, wmode);
163 return fopen (filename, mode);
167 static void gst_file_sink_dispose (GObject * object);
169 static void gst_file_sink_set_property (GObject * object, guint prop_id,
170 const GValue * value, GParamSpec * pspec);
171 static void gst_file_sink_get_property (GObject * object, guint prop_id,
172 GValue * value, GParamSpec * pspec);
174 static gboolean gst_file_sink_open_file (GstFileSink * sink);
175 static void gst_file_sink_close_file (GstFileSink * sink);
177 static gboolean gst_file_sink_start (GstBaseSink * sink);
178 static gboolean gst_file_sink_stop (GstBaseSink * sink);
179 static gboolean gst_file_sink_event (GstBaseSink * sink, GstEvent * event);
180 static GstFlowReturn gst_file_sink_render (GstBaseSink * sink,
182 static GstFlowReturn gst_file_sink_render_list (GstBaseSink * sink,
183 GstBufferList * list);
185 static gboolean gst_file_sink_do_seek (GstFileSink * filesink,
187 static gboolean gst_file_sink_get_current_offset (GstFileSink * filesink,
190 static gboolean gst_file_sink_query (GstBaseSink * bsink, GstQuery * query);
192 static void gst_file_sink_uri_handler_init (gpointer g_iface,
193 gpointer iface_data);
196 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_file_sink_uri_handler_init); \
197 GST_DEBUG_CATEGORY_INIT (gst_file_sink_debug, "filesink", 0, "filesink element");
198 #define gst_file_sink_parent_class parent_class
199 G_DEFINE_TYPE_WITH_CODE (GstFileSink, gst_file_sink, GST_TYPE_BASE_SINK,
203 gst_file_sink_class_init (GstFileSinkClass * klass)
205 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
206 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
207 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
209 gobject_class->dispose = gst_file_sink_dispose;
211 gobject_class->set_property = gst_file_sink_set_property;
212 gobject_class->get_property = gst_file_sink_get_property;
214 g_object_class_install_property (gobject_class, PROP_LOCATION,
215 g_param_spec_string ("location", "File Location",
216 "Location of the file to write", NULL,
217 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
220 g_param_spec_enum ("buffer-mode", "Buffering mode",
221 "The buffering mode to use", GST_TYPE_FILE_SINK_BUFFER_MODE,
222 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
225 g_param_spec_uint ("buffer-size", "Buffering size",
226 "Size of buffer in number of bytes for line or full buffer-mode", 0,
227 G_MAXUINT, DEFAULT_BUFFER_SIZE,
228 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 #ifdef TIZEN_FEATURE_FILESINK_MODIFICATION
230 g_object_class_install_property (gobject_class, PROP_CURRENT_BYTES,
231 g_param_spec_uint64 ("current-bytes", "Current bytes",
232 "downloaded bytes so far", 0,
234 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240 * Append to an already existing file.
242 g_object_class_install_property (gobject_class, PROP_APPEND,
243 g_param_spec_boolean ("append", "Append",
244 "Append to an already existing file", DEFAULT_APPEND,
245 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
247 gst_element_class_set_static_metadata (gstelement_class,
249 "Sink/File", "Write stream to a file",
250 "Thomas Vander Stichele <thomas at apestaart dot org>");
251 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
253 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_file_sink_start);
254 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_file_sink_stop);
255 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_file_sink_query);
256 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_file_sink_render);
257 gstbasesink_class->render_list =
258 GST_DEBUG_FUNCPTR (gst_file_sink_render_list);
259 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_file_sink_event);
261 if (sizeof (off_t) < 8) {
262 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
268 gst_file_sink_init (GstFileSink * filesink)
270 filesink->filename = NULL;
271 filesink->file = NULL;
272 filesink->current_pos = 0;
273 filesink->buffer_mode = DEFAULT_BUFFER_MODE;
274 filesink->buffer_size = DEFAULT_BUFFER_SIZE;
275 filesink->buffer = NULL;
276 filesink->append = FALSE;
278 gst_base_sink_set_sync (GST_BASE_SINK (filesink), FALSE);
282 gst_file_sink_dispose (GObject * object)
284 GstFileSink *sink = GST_FILE_SINK (object);
286 G_OBJECT_CLASS (parent_class)->dispose (object);
290 g_free (sink->filename);
291 sink->filename = NULL;
292 g_free (sink->buffer);
294 sink->buffer_size = 0;
298 gst_file_sink_set_location (GstFileSink * sink, const gchar * location,
304 g_free (sink->filename);
306 if (location != NULL) {
307 /* we store the filename as we received it from the application. On Windows
308 * this should be in UTF8 */
309 sink->filename = g_strdup (location);
310 sink->uri = gst_filename_to_uri (location, NULL);
311 GST_INFO_OBJECT (sink, "filename : %s", sink->filename);
312 GST_INFO_OBJECT (sink, "uri : %s", sink->uri);
314 sink->filename = NULL;
323 g_warning ("Changing the `location' property on filesink when a file is "
324 "open is not supported.");
325 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
326 "Changing the 'location' property on filesink when a file is "
327 "open is not supported");
333 gst_file_sink_set_property (GObject * object, guint prop_id,
334 const GValue * value, GParamSpec * pspec)
336 GstFileSink *sink = GST_FILE_SINK (object);
340 gst_file_sink_set_location (sink, g_value_get_string (value), NULL);
342 case PROP_BUFFER_MODE:
343 sink->buffer_mode = g_value_get_enum (value);
345 case PROP_BUFFER_SIZE:
346 sink->buffer_size = g_value_get_uint (value);
349 sink->append = g_value_get_boolean (value);
352 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
358 gst_file_sink_get_property (GObject * object, guint prop_id, GValue * value,
361 GstFileSink *sink = GST_FILE_SINK (object);
365 g_value_set_string (value, sink->filename);
367 case PROP_BUFFER_MODE:
368 g_value_set_enum (value, sink->buffer_mode);
370 case PROP_BUFFER_SIZE:
371 g_value_set_uint (value, sink->buffer_size);
374 g_value_set_boolean (value, sink->append);
376 #ifdef TIZEN_FEATURE_FILESINK_MODIFICATION
377 case PROP_CURRENT_BYTES:
378 g_value_set_uint64(value, sink->current_pos);
382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
388 gst_file_sink_open_file (GstFileSink * sink)
393 if (sink->filename == NULL || sink->filename[0] == '\0')
397 sink->file = gst_fopen (sink->filename, "ab");
399 sink->file = gst_fopen (sink->filename, "wb");
400 if (sink->file == NULL)
403 /* see if we are asked to perform a specific kind of buffering */
404 if ((mode = sink->buffer_mode) != -1) {
407 /* free previous buffer if any */
408 g_free (sink->buffer);
410 if (mode == _IONBF) {
415 /* allocate buffer */
416 sink->buffer = g_malloc (sink->buffer_size);
417 buffer_size = sink->buffer_size;
419 /* Cygwin does not have __fbufsize */
420 #if defined(HAVE_STDIO_EXT_H) && !defined(__CYGWIN__)
421 GST_DEBUG_OBJECT (sink, "change buffer size %u to %u, mode %d",
422 (guint) __fbufsize (sink->file), buffer_size, mode);
424 GST_DEBUG_OBJECT (sink, "change buffer size to %u, mode %d",
425 sink->buffer_size, mode);
427 if (setvbuf (sink->file, sink->buffer, mode, buffer_size) != 0) {
428 GST_WARNING_OBJECT (sink, "warning: setvbuf failed: %s",
433 sink->current_pos = 0;
434 /* try to seek in the file to figure out if it is seekable */
435 sink->seekable = gst_file_sink_do_seek (sink, 0);
437 GST_DEBUG_OBJECT (sink, "opened file %s, seekable %d",
438 sink->filename, sink->seekable);
445 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
446 (_("No file name specified for writing.")), (NULL));
451 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
452 (_("Could not open file \"%s\" for writing."), sink->filename),
459 gst_file_sink_close_file (GstFileSink * sink)
462 if (fclose (sink->file) != 0)
463 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
464 (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
466 GST_DEBUG_OBJECT (sink, "closed file");
469 g_free (sink->buffer);
475 gst_file_sink_query (GstBaseSink * bsink, GstQuery * query)
481 self = GST_FILE_SINK (bsink);
483 switch (GST_QUERY_TYPE (query)) {
484 case GST_QUERY_POSITION:
485 gst_query_parse_position (query, &format, NULL);
488 case GST_FORMAT_DEFAULT:
489 case GST_FORMAT_BYTES:
490 gst_query_set_position (query, GST_FORMAT_BYTES, self->current_pos);
499 case GST_QUERY_FORMATS:
500 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
505 gst_query_set_uri (query, self->uri);
509 case GST_QUERY_SEEKING:
510 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
511 if (format == GST_FORMAT_BYTES || format == GST_FORMAT_DEFAULT) {
512 gst_query_set_seeking (query, GST_FORMAT_BYTES, self->seekable, 0, -1);
514 gst_query_set_seeking (query, format, FALSE, 0, -1);
520 res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
527 # define __GST_STDIO_SEEK_FUNCTION "fseeko"
528 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
529 # define __GST_STDIO_SEEK_FUNCTION "lseek"
531 # define __GST_STDIO_SEEK_FUNCTION "fseek"
535 gst_file_sink_do_seek (GstFileSink * filesink, guint64 new_offset)
537 GST_DEBUG_OBJECT (filesink, "Seeking to offset %" G_GUINT64_FORMAT
538 " using " __GST_STDIO_SEEK_FUNCTION, new_offset);
540 if (fflush (filesink->file))
544 if (fseeko (filesink->file, (off_t) new_offset, SEEK_SET) != 0)
546 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
547 if (lseek (fileno (filesink->file), (off_t) new_offset,
548 SEEK_SET) == (off_t) - 1)
551 if (fseek (filesink->file, (long) new_offset, SEEK_SET) != 0)
555 /* adjust position reporting after seek;
556 * presumably this should basically yield new_offset */
557 gst_file_sink_get_current_offset (filesink, &filesink->current_pos);
564 GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
569 GST_DEBUG_OBJECT (filesink, "Seeking failed: %s", g_strerror (errno));
574 /* handle events (search) */
576 gst_file_sink_event (GstBaseSink * sink, GstEvent * event)
579 GstFileSink *filesink;
581 filesink = GST_FILE_SINK (sink);
583 type = GST_EVENT_TYPE (event);
586 case GST_EVENT_SEGMENT:
588 const GstSegment *segment;
590 gst_event_parse_segment (event, &segment);
592 if (segment->format == GST_FORMAT_BYTES) {
593 /* only try to seek and fail when we are going to a different
595 if (filesink->current_pos != segment->start) {
596 /* FIXME, the seek should be performed on the pos field, start/stop are
597 * just boundaries for valid bytes offsets. We should also fill the file
598 * with zeroes if the new position extends the current EOF (sparse streams
599 * and segment accumulation). */
600 if (!gst_file_sink_do_seek (filesink, (guint64) segment->start))
603 GST_DEBUG_OBJECT (filesink, "Ignored SEGMENT, no seek needed");
606 GST_DEBUG_OBJECT (filesink,
607 "Ignored SEGMENT event of format %u (%s)", (guint) segment->format,
608 gst_format_get_name (segment->format));
612 case GST_EVENT_FLUSH_STOP:
613 if (filesink->current_pos != 0 && filesink->seekable) {
614 gst_file_sink_do_seek (filesink, 0);
615 if (ftruncate (fileno (filesink->file), 0))
620 if (fflush (filesink->file))
627 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
632 GST_ELEMENT_ERROR (filesink, RESOURCE, SEEK,
633 (_("Error while seeking in file \"%s\"."), filesink->filename),
635 gst_event_unref (event);
640 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
641 (_("Error while writing to file \"%s\"."), filesink->filename),
643 gst_event_unref (event);
649 gst_file_sink_get_current_offset (GstFileSink * filesink, guint64 * p_pos)
654 ret = ftello (filesink->file);
655 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
656 if (fflush (filesink->file)) {
657 GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
658 /* ignore and continue */
660 ret = lseek (fileno (filesink->file), 0, SEEK_CUR);
662 ret = (off_t) ftell (filesink->file);
665 if (ret != (off_t) - 1)
666 *p_pos = (guint64) ret;
668 return (ret != (off_t) - 1);
672 gst_file_sink_render_buffers (GstFileSink * sink, GstBuffer ** buffers,
673 guint num_buffers, guint8 * mem_nums, guint total_mems)
675 GST_DEBUG_OBJECT (sink,
676 "writing %u buffers (%u memories) at position %" G_GUINT64_FORMAT,
677 num_buffers, total_mems, sink->current_pos);
679 return gst_writev_buffers (GST_OBJECT_CAST (sink), fileno (sink->file), NULL,
680 buffers, num_buffers, mem_nums, total_mems, &sink->current_pos, 0);
684 gst_file_sink_render_list (GstBaseSink * bsink, GstBufferList * buffer_list)
691 guint i, num_buffers;
692 gboolean sync_after = FALSE;
694 sink = GST_FILE_SINK_CAST (bsink);
696 num_buffers = gst_buffer_list_length (buffer_list);
697 if (num_buffers == 0)
700 /* extract buffers from list and count memories */
701 buffers = g_newa (GstBuffer *, num_buffers);
702 mem_nums = g_newa (guint8, num_buffers);
703 for (i = 0, total_mems = 0; i < num_buffers; ++i) {
704 buffers[i] = gst_buffer_list_get (buffer_list, i);
705 mem_nums[i] = gst_buffer_n_memory (buffers[i]);
706 total_mems += mem_nums[i];
707 if (GST_BUFFER_FLAG_IS_SET (buffers[i], GST_BUFFER_FLAG_SYNC_AFTER))
712 gst_file_sink_render_buffers (sink, buffers, num_buffers, mem_nums,
715 if (flow == GST_FLOW_OK && sync_after) {
716 if (fflush (sink->file) || fsync (fileno (sink->file))) {
717 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
718 (_("Error while writing to file \"%s\"."), sink->filename),
719 ("%s", g_strerror (errno)));
720 flow = GST_FLOW_ERROR;
728 GST_LOG_OBJECT (sink, "empty buffer list");
734 gst_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
736 GstFileSink *filesink;
740 filesink = GST_FILE_SINK_CAST (sink);
742 n_mem = gst_buffer_n_memory (buffer);
745 flow = gst_file_sink_render_buffers (filesink, &buffer, 1, &n_mem, n_mem);
749 if (flow == GST_FLOW_OK &&
750 GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_SYNC_AFTER)) {
751 if (fflush (filesink->file) || fsync (fileno (filesink->file))) {
752 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
753 (_("Error while writing to file \"%s\"."), filesink->filename),
754 ("%s", g_strerror (errno)));
755 flow = GST_FLOW_ERROR;
763 gst_file_sink_start (GstBaseSink * basesink)
765 return gst_file_sink_open_file (GST_FILE_SINK (basesink));
769 gst_file_sink_stop (GstBaseSink * basesink)
771 gst_file_sink_close_file (GST_FILE_SINK (basesink));
775 /*** GSTURIHANDLER INTERFACE *************************************************/
778 gst_file_sink_uri_get_type (GType type)
783 static const gchar *const *
784 gst_file_sink_uri_get_protocols (GType type)
786 static const gchar *protocols[] = { "file", NULL };
792 gst_file_sink_uri_get_uri (GstURIHandler * handler)
794 GstFileSink *sink = GST_FILE_SINK (handler);
796 /* FIXME: make thread-safe */
797 return g_strdup (sink->uri);
801 gst_file_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
806 GstFileSink *sink = GST_FILE_SINK (handler);
808 /* allow file://localhost/foo/bar by stripping localhost but fail
809 * for every other hostname */
810 if (g_str_has_prefix (uri, "file://localhost/")) {
813 /* 16 == strlen ("file://localhost") */
814 tmp = g_strconcat ("file://", uri + 16, NULL);
815 /* we use gst_uri_get_location() although we already have the
816 * "location" with uri + 16 because it provides unescaping */
817 location = gst_uri_get_location (tmp);
819 } else if (strcmp (uri, "file://") == 0) {
820 /* Special case for "file://" as this is used by some applications
821 * to test with gst_element_make_from_uri if there's an element
822 * that supports the URI protocol. */
823 gst_file_sink_set_location (sink, NULL, NULL);
826 location = gst_uri_get_location (uri);
830 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
831 "File URI without location");
835 if (!g_path_is_absolute (location)) {
836 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
837 "File URI location must be an absolute path");
842 ret = gst_file_sink_set_location (sink, location, error);
849 gst_file_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
851 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
853 iface->get_type = gst_file_sink_uri_get_type;
854 iface->get_protocols = gst_file_sink_uri_get_protocols;
855 iface->get_uri = gst_file_sink_uri_get_uri;
856 iface->set_uri = gst_file_sink_uri_set_uri;