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 #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_pad_template (gstelement_class,
235 gst_static_pad_template_get (&sinktemplate));
237 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_file_sink_start);
238 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_file_sink_stop);
239 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_file_sink_query);
240 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_file_sink_render);
241 gstbasesink_class->render_list =
242 GST_DEBUG_FUNCPTR (gst_file_sink_render_list);
243 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_file_sink_event);
245 if (sizeof (off_t) < 8) {
246 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
252 gst_file_sink_init (GstFileSink * filesink)
254 filesink->filename = NULL;
255 filesink->file = NULL;
256 filesink->current_pos = 0;
257 filesink->buffer_mode = DEFAULT_BUFFER_MODE;
258 filesink->buffer_size = DEFAULT_BUFFER_SIZE;
259 filesink->buffer = NULL;
260 filesink->append = FALSE;
262 gst_base_sink_set_sync (GST_BASE_SINK (filesink), FALSE);
266 gst_file_sink_dispose (GObject * object)
268 GstFileSink *sink = GST_FILE_SINK (object);
270 G_OBJECT_CLASS (parent_class)->dispose (object);
274 g_free (sink->filename);
275 sink->filename = NULL;
276 g_free (sink->buffer);
278 sink->buffer_size = 0;
282 gst_file_sink_set_location (GstFileSink * sink, const gchar * location,
288 g_free (sink->filename);
290 if (location != NULL) {
291 /* we store the filename as we received it from the application. On Windows
292 * this should be in UTF8 */
293 sink->filename = g_strdup (location);
294 sink->uri = gst_filename_to_uri (location, NULL);
295 GST_INFO_OBJECT (sink, "filename : %s", sink->filename);
296 GST_INFO_OBJECT (sink, "uri : %s", sink->uri);
298 sink->filename = NULL;
307 g_warning ("Changing the `location' property on filesink when a file is "
308 "open is not supported.");
309 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
310 "Changing the 'location' property on filesink when a file is "
311 "open is not supported");
317 gst_file_sink_set_property (GObject * object, guint prop_id,
318 const GValue * value, GParamSpec * pspec)
320 GstFileSink *sink = GST_FILE_SINK (object);
324 gst_file_sink_set_location (sink, g_value_get_string (value), NULL);
326 case PROP_BUFFER_MODE:
327 sink->buffer_mode = g_value_get_enum (value);
329 case PROP_BUFFER_SIZE:
330 sink->buffer_size = g_value_get_uint (value);
333 sink->append = g_value_get_boolean (value);
336 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342 gst_file_sink_get_property (GObject * object, guint prop_id, GValue * value,
345 GstFileSink *sink = GST_FILE_SINK (object);
349 g_value_set_string (value, sink->filename);
351 case PROP_BUFFER_MODE:
352 g_value_set_enum (value, sink->buffer_mode);
354 case PROP_BUFFER_SIZE:
355 g_value_set_uint (value, sink->buffer_size);
358 g_value_set_boolean (value, sink->append);
361 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
367 gst_file_sink_open_file (GstFileSink * sink)
372 if (sink->filename == NULL || sink->filename[0] == '\0')
376 sink->file = gst_fopen (sink->filename, "ab");
378 sink->file = gst_fopen (sink->filename, "wb");
379 if (sink->file == NULL)
382 /* see if we are asked to perform a specific kind of buffering */
383 if ((mode = sink->buffer_mode) != -1) {
386 /* free previous buffer if any */
387 g_free (sink->buffer);
389 if (mode == _IONBF) {
394 /* allocate buffer */
395 sink->buffer = g_malloc (sink->buffer_size);
396 buffer_size = sink->buffer_size;
398 /* Cygwin does not have __fbufsize */
399 #if defined(HAVE_STDIO_EXT_H) && !defined(__CYGWIN__)
400 GST_DEBUG_OBJECT (sink, "change buffer size %u to %u, mode %d",
401 (guint) __fbufsize (sink->file), buffer_size, mode);
403 GST_DEBUG_OBJECT (sink, "change buffer size to %u, mode %d",
404 sink->buffer_size, mode);
406 if (setvbuf (sink->file, sink->buffer, mode, buffer_size) != 0) {
407 GST_WARNING_OBJECT (sink, "warning: setvbuf failed: %s",
412 sink->current_pos = 0;
413 /* try to seek in the file to figure out if it is seekable */
414 sink->seekable = gst_file_sink_do_seek (sink, 0);
416 GST_DEBUG_OBJECT (sink, "opened file %s, seekable %d",
417 sink->filename, sink->seekable);
424 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
425 (_("No file name specified for writing.")), (NULL));
430 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
431 (_("Could not open file \"%s\" for writing."), sink->filename),
438 gst_file_sink_close_file (GstFileSink * sink)
441 if (fclose (sink->file) != 0)
442 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
443 (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
445 GST_DEBUG_OBJECT (sink, "closed file");
448 g_free (sink->buffer);
454 gst_file_sink_query (GstBaseSink * bsink, GstQuery * query)
460 self = GST_FILE_SINK (bsink);
462 switch (GST_QUERY_TYPE (query)) {
463 case GST_QUERY_POSITION:
464 gst_query_parse_position (query, &format, NULL);
467 case GST_FORMAT_DEFAULT:
468 case GST_FORMAT_BYTES:
469 gst_query_set_position (query, GST_FORMAT_BYTES, self->current_pos);
478 case GST_QUERY_FORMATS:
479 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
484 gst_query_set_uri (query, self->uri);
488 case GST_QUERY_SEEKING:
489 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
490 if (format == GST_FORMAT_BYTES || format == GST_FORMAT_DEFAULT) {
491 gst_query_set_seeking (query, GST_FORMAT_BYTES, self->seekable, 0, -1);
493 gst_query_set_seeking (query, format, FALSE, 0, -1);
499 res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
506 # define __GST_STDIO_SEEK_FUNCTION "fseeko"
507 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
508 # define __GST_STDIO_SEEK_FUNCTION "lseek"
510 # define __GST_STDIO_SEEK_FUNCTION "fseek"
514 gst_file_sink_do_seek (GstFileSink * filesink, guint64 new_offset)
516 GST_DEBUG_OBJECT (filesink, "Seeking to offset %" G_GUINT64_FORMAT
517 " using " __GST_STDIO_SEEK_FUNCTION, new_offset);
519 if (fflush (filesink->file))
523 if (fseeko (filesink->file, (off_t) new_offset, SEEK_SET) != 0)
525 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
526 if (lseek (fileno (filesink->file), (off_t) new_offset,
527 SEEK_SET) == (off_t) - 1)
530 if (fseek (filesink->file, (long) new_offset, SEEK_SET) != 0)
534 /* adjust position reporting after seek;
535 * presumably this should basically yield new_offset */
536 gst_file_sink_get_current_offset (filesink, &filesink->current_pos);
543 GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
548 GST_DEBUG_OBJECT (filesink, "Seeking failed: %s", g_strerror (errno));
553 /* handle events (search) */
555 gst_file_sink_event (GstBaseSink * sink, GstEvent * event)
558 GstFileSink *filesink;
560 filesink = GST_FILE_SINK (sink);
562 type = GST_EVENT_TYPE (event);
565 case GST_EVENT_SEGMENT:
567 const GstSegment *segment;
569 gst_event_parse_segment (event, &segment);
571 if (segment->format == GST_FORMAT_BYTES) {
572 /* only try to seek and fail when we are going to a different
574 if (filesink->current_pos != segment->start) {
575 /* FIXME, the seek should be performed on the pos field, start/stop are
576 * just boundaries for valid bytes offsets. We should also fill the file
577 * with zeroes if the new position extends the current EOF (sparse streams
578 * and segment accumulation). */
579 if (!gst_file_sink_do_seek (filesink, (guint64) segment->start))
582 GST_DEBUG_OBJECT (filesink, "Ignored SEGMENT, no seek needed");
585 GST_DEBUG_OBJECT (filesink,
586 "Ignored SEGMENT event of format %u (%s)", (guint) segment->format,
587 gst_format_get_name (segment->format));
591 case GST_EVENT_FLUSH_STOP:
592 if (filesink->current_pos != 0 && filesink->seekable) {
593 gst_file_sink_do_seek (filesink, 0);
594 if (ftruncate (fileno (filesink->file), 0))
599 if (fflush (filesink->file))
606 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
611 GST_ELEMENT_ERROR (filesink, RESOURCE, SEEK,
612 (_("Error while seeking in file \"%s\"."), filesink->filename),
614 gst_event_unref (event);
619 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
620 (_("Error while writing to file \"%s\"."), filesink->filename),
622 gst_event_unref (event);
628 gst_file_sink_get_current_offset (GstFileSink * filesink, guint64 * p_pos)
633 ret = ftello (filesink->file);
634 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
635 if (fflush (filesink->file)) {
636 GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
637 /* ignore and continue */
639 ret = lseek (fileno (filesink->file), 0, SEEK_CUR);
641 ret = (off_t) ftell (filesink->file);
644 if (ret != (off_t) - 1)
645 *p_pos = (guint64) ret;
647 return (ret != (off_t) - 1);
651 gst_file_sink_render_buffers (GstFileSink * sink, GstBuffer ** buffers,
652 guint num_buffers, guint8 * mem_nums, guint total_mems)
654 GST_DEBUG_OBJECT (sink,
655 "writing %u buffers (%u memories) at position %" G_GUINT64_FORMAT,
656 num_buffers, total_mems, sink->current_pos);
658 return gst_writev_buffers (GST_OBJECT_CAST (sink), fileno (sink->file), NULL,
659 buffers, num_buffers, mem_nums, total_mems, NULL, &sink->current_pos);
663 gst_file_sink_render_list (GstBaseSink * bsink, GstBufferList * buffer_list)
670 guint i, num_buffers;
671 gboolean sync_after = FALSE;
673 sink = GST_FILE_SINK_CAST (bsink);
675 num_buffers = gst_buffer_list_length (buffer_list);
676 if (num_buffers == 0)
679 /* extract buffers from list and count memories */
680 buffers = g_newa (GstBuffer *, num_buffers);
681 mem_nums = g_newa (guint8, num_buffers);
682 for (i = 0, total_mems = 0; i < num_buffers; ++i) {
683 buffers[i] = gst_buffer_list_get (buffer_list, i);
684 mem_nums[i] = gst_buffer_n_memory (buffers[i]);
685 total_mems += mem_nums[i];
686 if (GST_BUFFER_FLAG_IS_SET (buffers[i], GST_BUFFER_FLAG_SYNC_AFTER))
691 gst_file_sink_render_buffers (sink, buffers, num_buffers, mem_nums,
694 if (flow == GST_FLOW_OK && sync_after) {
695 if (fflush (sink->file) || fsync (fileno (sink->file))) {
696 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
697 (_("Error while writing to file \"%s\"."), sink->filename),
698 ("%s", g_strerror (errno)));
699 flow = GST_FLOW_ERROR;
707 GST_LOG_OBJECT (sink, "empty buffer list");
713 gst_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
715 GstFileSink *filesink;
719 filesink = GST_FILE_SINK_CAST (sink);
721 n_mem = gst_buffer_n_memory (buffer);
724 flow = gst_file_sink_render_buffers (filesink, &buffer, 1, &n_mem, n_mem);
728 if (flow == GST_FLOW_OK &&
729 GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_SYNC_AFTER)) {
730 if (fflush (filesink->file) || fsync (fileno (filesink->file))) {
731 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
732 (_("Error while writing to file \"%s\"."), filesink->filename),
733 ("%s", g_strerror (errno)));
734 flow = GST_FLOW_ERROR;
742 gst_file_sink_start (GstBaseSink * basesink)
744 return gst_file_sink_open_file (GST_FILE_SINK (basesink));
748 gst_file_sink_stop (GstBaseSink * basesink)
750 gst_file_sink_close_file (GST_FILE_SINK (basesink));
754 /*** GSTURIHANDLER INTERFACE *************************************************/
757 gst_file_sink_uri_get_type (GType type)
762 static const gchar *const *
763 gst_file_sink_uri_get_protocols (GType type)
765 static const gchar *protocols[] = { "file", NULL };
771 gst_file_sink_uri_get_uri (GstURIHandler * handler)
773 GstFileSink *sink = GST_FILE_SINK (handler);
775 /* FIXME: make thread-safe */
776 return g_strdup (sink->uri);
780 gst_file_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
785 GstFileSink *sink = GST_FILE_SINK (handler);
787 /* allow file://localhost/foo/bar by stripping localhost but fail
788 * for every other hostname */
789 if (g_str_has_prefix (uri, "file://localhost/")) {
792 /* 16 == strlen ("file://localhost") */
793 tmp = g_strconcat ("file://", uri + 16, NULL);
794 /* we use gst_uri_get_location() although we already have the
795 * "location" with uri + 16 because it provides unescaping */
796 location = gst_uri_get_location (tmp);
798 } else if (strcmp (uri, "file://") == 0) {
799 /* Special case for "file://" as this is used by some applications
800 * to test with gst_element_make_from_uri if there's an element
801 * that supports the URI protocol. */
802 gst_file_sink_set_location (sink, NULL, NULL);
805 location = gst_uri_get_location (uri);
809 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
810 "File URI without location");
814 if (!g_path_is_absolute (location)) {
815 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
816 "File URI location must be an absolute path");
821 ret = gst_file_sink_set_location (sink, location, error);
828 gst_file_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
830 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
832 iface->get_type = gst_file_sink_uri_get_type;
833 iface->get_protocols = gst_file_sink_uri_get_protocols;
834 iface->get_uri = gst_file_sink_uri_get_uri;
835 iface->set_uri = gst_file_sink_uri_set_uri;