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 <glib/gi18n-lib.h>
44 #include <glib/gstdio.h>
45 #include <stdio.h> /* for fseeko() */
46 #ifdef HAVE_STDIO_EXT_H
47 #include <stdio_ext.h> /* for __fbufsize, for debugging */
50 #include "gstfilesink.h"
52 #include <sys/types.h>
56 #include <io.h> /* lseek, open, close, read */
58 #define lseek _lseeki64
62 #define ftruncate _chsize
65 #ifdef _MSC_VER /* Check if we are using MSVC, fileno is deprecated in favour */
66 #define fileno _fileno /* of _fileno */
75 #include "gstelements_private.h"
76 #include "gstfilesink.h"
77 #include "gstcoreelementselements.h"
79 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
84 #define GST_TYPE_FILE_SINK_BUFFER_MODE (gst_file_sink_buffer_mode_get_type ())
86 gst_file_sink_buffer_mode_get_type (void)
88 static GType buffer_mode_type = 0;
89 static const GEnumValue buffer_mode[] = {
90 {GST_FILE_SINK_BUFFER_MODE_DEFAULT, "Default buffering", "default"},
91 {GST_FILE_SINK_BUFFER_MODE_FULL, "Fully buffered", "full"},
92 {GST_FILE_SINK_BUFFER_MODE_LINE, "Line buffered (deprecated, like full)",
94 {GST_FILE_SINK_BUFFER_MODE_UNBUFFERED, "Unbuffered", "unbuffered"},
98 if (!buffer_mode_type) {
100 g_enum_register_static ("GstFileSinkBufferMode", buffer_mode);
102 return buffer_mode_type;
105 GST_DEBUG_CATEGORY_STATIC (gst_file_sink_debug);
106 #define GST_CAT_DEFAULT gst_file_sink_debug
108 #define DEFAULT_LOCATION NULL
109 #define DEFAULT_BUFFER_MODE GST_FILE_SINK_BUFFER_MODE_DEFAULT
110 #define DEFAULT_BUFFER_SIZE 64 * 1024
111 #define DEFAULT_APPEND FALSE
112 #define DEFAULT_O_SYNC FALSE
113 #define DEFAULT_MAX_TRANSIENT_ERROR_TIMEOUT 0
123 PROP_MAX_TRANSIENT_ERROR_TIMEOUT,
127 /* Copy of glib's g_fopen due to win32 libc/cross-DLL brokenness: we can't
128 * use the 'file pointer' opened in glib (and returned from this function)
129 * in this library, as they may have unrelated C runtimes. */
131 gst_fopen (const gchar * filename, const gchar * mode, gboolean o_sync)
135 retval = g_fopen (filename, mode);
139 int flags = O_CREAT | O_WRONLY;
141 /* NOTE: below code is for handing spurious EACCES return on write
142 * See https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/143
144 if (strcmp (mode, "wb") == 0)
146 else if (strcmp (mode, "ab") == 0)
149 g_assert_not_reached ();
154 fd = open (filename, flags, 0666);
159 retval = fdopen (fd, 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);
181 static gboolean gst_file_sink_unlock (GstBaseSink * sink);
182 static gboolean gst_file_sink_unlock_stop (GstBaseSink * sink);
184 static gboolean gst_file_sink_do_seek (GstFileSink * filesink,
186 static gboolean gst_file_sink_get_current_offset (GstFileSink * filesink,
189 static gboolean gst_file_sink_query (GstBaseSink * bsink, GstQuery * query);
191 static void gst_file_sink_uri_handler_init (gpointer g_iface,
192 gpointer iface_data);
194 static GstFlowReturn gst_file_sink_flush_buffer (GstFileSink * filesink);
197 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_file_sink_uri_handler_init); \
198 GST_DEBUG_CATEGORY_INIT (gst_file_sink_debug, "filesink", 0, "filesink element");
199 #define gst_file_sink_parent_class parent_class
200 G_DEFINE_TYPE_WITH_CODE (GstFileSink, gst_file_sink, GST_TYPE_BASE_SINK,
202 GST_ELEMENT_REGISTER_DEFINE (filesink, "filesink", GST_RANK_PRIMARY,
206 gst_file_sink_class_init (GstFileSinkClass * klass)
208 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
209 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
210 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
212 gobject_class->dispose = gst_file_sink_dispose;
214 gobject_class->set_property = gst_file_sink_set_property;
215 gobject_class->get_property = gst_file_sink_get_property;
217 g_object_class_install_property (gobject_class, PROP_LOCATION,
218 g_param_spec_string ("location", "File Location",
219 "Location of the file to write", NULL,
220 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
222 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
223 g_param_spec_enum ("buffer-mode", "Buffering mode",
224 "The buffering mode to use", GST_TYPE_FILE_SINK_BUFFER_MODE,
225 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
228 g_param_spec_uint ("buffer-size", "Buffering size",
229 "Size of buffer in number of bytes for line or full buffer-mode", 0,
230 G_MAXUINT, DEFAULT_BUFFER_SIZE,
231 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
236 * Append to an already existing file.
238 g_object_class_install_property (gobject_class, PROP_APPEND,
239 g_param_spec_boolean ("append", "Append",
240 "Append to an already existing file", DEFAULT_APPEND,
241 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243 g_object_class_install_property (gobject_class, PROP_O_SYNC,
244 g_param_spec_boolean ("o-sync", "Synchronous IO",
245 "Open the file with O_SYNC for enabling synchronous IO",
246 DEFAULT_O_SYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248 g_object_class_install_property (gobject_class,
249 PROP_MAX_TRANSIENT_ERROR_TIMEOUT,
250 g_param_spec_int ("max-transient-error-timeout",
251 "Max Transient Error Timeout",
252 "Retry up to this many ms on transient errors (currently EACCES)", 0,
253 G_MAXINT, DEFAULT_MAX_TRANSIENT_ERROR_TIMEOUT,
254 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256 gst_element_class_set_static_metadata (gstelement_class,
258 "Sink/File", "Write stream to a file",
259 "Thomas Vander Stichele <thomas at apestaart dot org>");
260 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
262 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_file_sink_start);
263 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_file_sink_stop);
264 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_file_sink_query);
265 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_file_sink_render);
266 gstbasesink_class->render_list =
267 GST_DEBUG_FUNCPTR (gst_file_sink_render_list);
268 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_file_sink_event);
269 gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_file_sink_unlock);
270 gstbasesink_class->unlock_stop =
271 GST_DEBUG_FUNCPTR (gst_file_sink_unlock_stop);
273 if (sizeof (off_t) < 8) {
274 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
278 gst_type_mark_as_plugin_api (GST_TYPE_FILE_SINK_BUFFER_MODE, 0);
282 gst_file_sink_init (GstFileSink * filesink)
284 filesink->filename = NULL;
285 filesink->file = NULL;
286 filesink->current_pos = 0;
287 filesink->buffer_mode = DEFAULT_BUFFER_MODE;
288 filesink->buffer_size = DEFAULT_BUFFER_SIZE;
289 filesink->append = FALSE;
291 gst_base_sink_set_sync (GST_BASE_SINK (filesink), FALSE);
295 gst_file_sink_dispose (GObject * object)
297 GstFileSink *sink = GST_FILE_SINK (object);
299 G_OBJECT_CLASS (parent_class)->dispose (object);
303 g_free (sink->filename);
304 sink->filename = NULL;
308 gst_file_sink_set_location (GstFileSink * sink, const gchar * location,
314 g_free (sink->filename);
316 if (location != NULL) {
317 /* we store the filename as we received it from the application. On Windows
318 * this should be in UTF8 */
319 sink->filename = g_strdup (location);
320 sink->uri = gst_filename_to_uri (location, NULL);
321 GST_INFO_OBJECT (sink, "filename : %s", sink->filename);
322 GST_INFO_OBJECT (sink, "uri : %s", sink->uri);
324 sink->filename = NULL;
333 g_warning ("Changing the `location' property on filesink when a file is "
334 "open is not supported.");
335 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
336 "Changing the 'location' property on filesink when a file is "
337 "open is not supported");
343 gst_file_sink_set_property (GObject * object, guint prop_id,
344 const GValue * value, GParamSpec * pspec)
346 GstFileSink *sink = GST_FILE_SINK (object);
350 gst_file_sink_set_location (sink, g_value_get_string (value), NULL);
352 case PROP_BUFFER_MODE:
353 sink->buffer_mode = g_value_get_enum (value);
355 case PROP_BUFFER_SIZE:
356 sink->buffer_size = g_value_get_uint (value);
359 sink->append = g_value_get_boolean (value);
362 sink->o_sync = g_value_get_boolean (value);
364 case PROP_MAX_TRANSIENT_ERROR_TIMEOUT:
365 sink->max_transient_error_timeout = g_value_get_int (value);
368 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
374 gst_file_sink_get_property (GObject * object, guint prop_id, GValue * value,
377 GstFileSink *sink = GST_FILE_SINK (object);
381 g_value_set_string (value, sink->filename);
383 case PROP_BUFFER_MODE:
384 g_value_set_enum (value, sink->buffer_mode);
386 case PROP_BUFFER_SIZE:
387 g_value_set_uint (value, sink->buffer_size);
390 g_value_set_boolean (value, sink->append);
393 g_value_set_boolean (value, sink->o_sync);
395 case PROP_MAX_TRANSIENT_ERROR_TIMEOUT:
396 g_value_set_int (value, sink->max_transient_error_timeout);
399 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
405 gst_file_sink_open_file (GstFileSink * sink)
408 if (sink->filename == NULL || sink->filename[0] == '\0')
412 sink->file = gst_fopen (sink->filename, "ab", sink->o_sync);
414 sink->file = gst_fopen (sink->filename, "wb", sink->o_sync);
415 if (sink->file == NULL)
418 sink->current_pos = 0;
419 /* try to seek in the file to figure out if it is seekable */
420 sink->seekable = gst_file_sink_do_seek (sink, 0);
423 g_free (sink->buffer);
425 if (sink->buffer_list)
426 gst_buffer_list_unref (sink->buffer_list);
427 sink->buffer_list = NULL;
429 if (sink->buffer_mode != GST_FILE_SINK_BUFFER_MODE_UNBUFFERED) {
430 if (sink->buffer_size == 0) {
431 sink->buffer_size = DEFAULT_BUFFER_SIZE;
432 g_object_notify (G_OBJECT (sink), "buffer-size");
435 if (sink->buffer_mode == GST_FILE_SINK_BUFFER_MODE_FULL) {
436 sink->buffer = g_malloc (sink->buffer_size);
437 sink->allocated_buffer_size = sink->buffer_size;
439 sink->buffer_list = gst_buffer_list_new ();
441 sink->current_buffer_size = 0;
444 GST_DEBUG_OBJECT (sink, "opened file %s, seekable %d",
445 sink->filename, sink->seekable);
452 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
453 (_("No file name specified for writing.")), (NULL));
458 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
459 (_("Could not open file \"%s\" for writing."), sink->filename),
466 gst_file_sink_close_file (GstFileSink * sink)
469 if (gst_file_sink_flush_buffer (sink) != GST_FLOW_OK)
470 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
471 (_("Error closing file \"%s\"."), sink->filename), NULL);
473 if (fclose (sink->file) != 0)
474 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
475 (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
477 GST_DEBUG_OBJECT (sink, "closed file");
482 g_free (sink->buffer);
485 sink->allocated_buffer_size = 0;
487 if (sink->buffer_list) {
488 gst_buffer_list_unref (sink->buffer_list);
489 sink->buffer_list = NULL;
491 sink->current_buffer_size = 0;
495 gst_file_sink_query (GstBaseSink * bsink, GstQuery * query)
501 self = GST_FILE_SINK (bsink);
503 switch (GST_QUERY_TYPE (query)) {
504 case GST_QUERY_POSITION:
505 gst_query_parse_position (query, &format, NULL);
508 case GST_FORMAT_DEFAULT:
509 case GST_FORMAT_BYTES:
510 gst_query_set_position (query, GST_FORMAT_BYTES,
511 self->current_pos + self->current_buffer_size);
520 case GST_QUERY_FORMATS:
521 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
526 gst_query_set_uri (query, self->uri);
530 case GST_QUERY_SEEKING:
531 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
532 if (format == GST_FORMAT_BYTES || format == GST_FORMAT_DEFAULT) {
533 gst_query_set_seeking (query, GST_FORMAT_BYTES, self->seekable, 0, -1);
535 gst_query_set_seeking (query, format, FALSE, 0, -1);
541 res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
548 # define __GST_STDIO_SEEK_FUNCTION "fseeko"
549 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
550 # define __GST_STDIO_SEEK_FUNCTION "lseek"
552 # define __GST_STDIO_SEEK_FUNCTION "fseek"
556 gst_file_sink_do_seek (GstFileSink * filesink, guint64 new_offset)
558 GST_DEBUG_OBJECT (filesink, "Seeking to offset %" G_GUINT64_FORMAT
559 " using " __GST_STDIO_SEEK_FUNCTION, new_offset);
561 if (gst_file_sink_flush_buffer (filesink) != GST_FLOW_OK)
562 goto flush_buffer_failed;
565 if (fseeko (filesink->file, (off_t) new_offset, SEEK_SET) != 0)
567 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
568 if (lseek (fileno (filesink->file), (off_t) new_offset,
569 SEEK_SET) == (off_t) - 1)
572 if (fseek (filesink->file, (long) new_offset, SEEK_SET) != 0)
576 /* adjust position reporting after seek;
577 * presumably this should basically yield new_offset */
578 gst_file_sink_get_current_offset (filesink, &filesink->current_pos);
585 GST_DEBUG_OBJECT (filesink, "Flushing buffer failed");
590 GST_DEBUG_OBJECT (filesink, "Seeking failed: %s", g_strerror (errno));
595 /* handle events (search) */
597 gst_file_sink_event (GstBaseSink * sink, GstEvent * event)
600 GstFileSink *filesink;
602 filesink = GST_FILE_SINK (sink);
604 type = GST_EVENT_TYPE (event);
607 case GST_EVENT_SEGMENT:
609 const GstSegment *segment;
611 gst_event_parse_segment (event, &segment);
613 if (segment->format == GST_FORMAT_BYTES) {
614 /* only try to seek and fail when we are going to a different
616 if (filesink->current_pos + filesink->current_buffer_size !=
618 /* FIXME, the seek should be performed on the pos field, start/stop are
619 * just boundaries for valid bytes offsets. We should also fill the file
620 * with zeroes if the new position extends the current EOF (sparse streams
621 * and segment accumulation). */
622 if (!gst_file_sink_do_seek (filesink, (guint64) segment->start))
625 GST_DEBUG_OBJECT (filesink, "Ignored SEGMENT, no seek needed");
628 GST_DEBUG_OBJECT (filesink,
629 "Ignored SEGMENT event of format %u (%s)", (guint) segment->format,
630 gst_format_get_name (segment->format));
634 case GST_EVENT_FLUSH_STOP:
635 if (filesink->current_pos != 0 && filesink->seekable) {
636 gst_file_sink_do_seek (filesink, 0);
637 if (ftruncate (fileno (filesink->file), 0))
638 goto truncate_failed;
640 if (filesink->buffer_list) {
641 gst_buffer_list_unref (filesink->buffer_list);
642 filesink->buffer_list = gst_buffer_list_new ();
644 filesink->current_buffer_size = 0;
647 if (gst_file_sink_flush_buffer (filesink) != GST_FLOW_OK)
648 goto flush_buffer_failed;
654 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
659 GST_ELEMENT_ERROR (filesink, RESOURCE, SEEK,
660 (_("Error while seeking in file \"%s\"."), filesink->filename),
662 gst_event_unref (event);
667 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
668 (_("Error while writing to file \"%s\"."), filesink->filename), NULL);
669 gst_event_unref (event);
674 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
675 (_("Error while writing to file \"%s\"."), filesink->filename),
677 gst_event_unref (event);
683 gst_file_sink_get_current_offset (GstFileSink * filesink, guint64 * p_pos)
687 /* no need to flush internal buffer here as this is only called right
688 * after a seek. If this changes then the buffer should be flushed here
693 ret = ftello (filesink->file);
694 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
695 ret = lseek (fileno (filesink->file), 0, SEEK_CUR);
697 ret = (off_t) ftell (filesink->file);
700 if (ret != (off_t) - 1)
701 *p_pos = (guint64) ret;
703 return (ret != (off_t) - 1);
707 gst_file_sink_render_list_internal (GstFileSink * sink,
708 GstBufferList * buffer_list)
714 num_buffers = gst_buffer_list_length (buffer_list);
715 if (num_buffers == 0)
718 GST_DEBUG_OBJECT (sink,
719 "writing %u buffers at position %" G_GUINT64_FORMAT, num_buffers,
723 guint64 bytes_written = 0;
726 gst_writev_buffer_list (GST_OBJECT_CAST (sink), fileno (sink->file),
727 NULL, buffer_list, &bytes_written, skip,
728 sink->max_transient_error_timeout, sink->current_pos, &sink->flushing);
730 sink->current_pos += bytes_written;
731 skip += bytes_written;
733 if (flow != GST_FLOW_FLUSHING)
736 flow = gst_base_sink_wait_preroll (GST_BASE_SINK (sink));
738 if (flow != GST_FLOW_OK)
746 GST_LOG_OBJECT (sink, "empty buffer list");
752 gst_file_sink_flush_buffer (GstFileSink * filesink)
754 GstFlowReturn flow_ret = GST_FLOW_OK;
756 GST_DEBUG_OBJECT (filesink, "Flushing out buffer of size %" G_GSIZE_FORMAT,
757 filesink->current_buffer_size);
759 if (filesink->buffer && filesink->current_buffer_size) {
763 guint64 bytes_written = 0;
766 gst_writev_mem (GST_OBJECT_CAST (filesink), fileno (filesink->file),
767 NULL, filesink->buffer, filesink->current_buffer_size, &bytes_written,
768 skip, filesink->max_transient_error_timeout, filesink->current_pos,
769 &filesink->flushing);
771 filesink->current_pos += bytes_written;
772 skip += bytes_written;
774 if (flow_ret != GST_FLOW_FLUSHING)
777 flow_ret = gst_base_sink_wait_preroll (GST_BASE_SINK (filesink));
778 if (flow_ret != GST_FLOW_OK)
781 } else if (filesink->buffer_list && filesink->current_buffer_size) {
784 length = gst_buffer_list_length (filesink->buffer_list);
788 gst_file_sink_render_list_internal (filesink, filesink->buffer_list);
789 /* Remove all buffers from the list but keep the list. This ensures that
790 * we don't re-allocate the array storing the buffers all the time */
791 gst_buffer_list_remove (filesink->buffer_list, 0, length);
795 filesink->current_buffer_size = 0;
801 has_sync_after_buffer (GstBuffer ** buffer, guint idx, gpointer user_data)
803 if (GST_BUFFER_FLAG_IS_SET (*buffer, GST_BUFFER_FLAG_SYNC_AFTER)) {
804 gboolean *sync_after = user_data;
814 accumulate_size (GstBuffer ** buffer, guint idx, gpointer user_data)
816 guint *size = user_data;
818 *size += gst_buffer_get_size (*buffer);
824 render_buffer (GstFileSink * filesink, GstBuffer * buffer)
827 guint64 bytes_written = 0;
832 gst_writev_buffer (GST_OBJECT_CAST (filesink),
833 fileno (filesink->file), NULL, buffer, &bytes_written, skip,
834 filesink->max_transient_error_timeout, filesink->current_pos,
835 &filesink->flushing);
837 filesink->current_pos += bytes_written;
838 skip += bytes_written;
840 if (flow != GST_FLOW_FLUSHING)
843 flow = gst_base_sink_wait_preroll (GST_BASE_SINK (filesink));
845 if (flow != GST_FLOW_OK)
853 gst_file_sink_render_list (GstBaseSink * bsink, GstBufferList * buffer_list)
857 guint i, num_buffers;
858 gboolean sync_after = FALSE;
861 sink = GST_FILE_SINK_CAST (bsink);
863 num_buffers = gst_buffer_list_length (buffer_list);
864 if (num_buffers == 0)
867 gst_buffer_list_foreach (buffer_list, has_sync_after_buffer, &sync_after);
869 if (sync_after || (!sink->buffer && !sink->buffer_list)) {
870 flow = gst_file_sink_flush_buffer (sink);
871 if (flow == GST_FLOW_OK)
872 flow = gst_file_sink_render_list_internal (sink, buffer_list);
875 gst_buffer_list_foreach (buffer_list, accumulate_size, &size);
877 GST_DEBUG_OBJECT (sink,
878 "Queueing buffer list of %u bytes (%u buffers) at offset %"
879 G_GUINT64_FORMAT, size, num_buffers,
880 sink->current_pos + sink->current_buffer_size);
884 for (i = 0; i < num_buffers && flow == GST_FLOW_OK; i++) {
885 GstBuffer *buffer = gst_buffer_list_get (buffer_list, i);
886 gsize buffer_size = gst_buffer_get_size (buffer);
888 if (sink->current_buffer_size + buffer_size >
889 sink->allocated_buffer_size) {
890 flow = gst_file_sink_flush_buffer (sink);
891 if (flow != GST_FLOW_OK)
895 if (buffer_size > sink->allocated_buffer_size) {
896 GST_DEBUG_OBJECT (sink,
897 "writing buffer ( %" G_GSIZE_FORMAT
898 " bytes) at position %" G_GUINT64_FORMAT,
899 buffer_size, sink->current_pos);
901 flow = render_buffer (sink, buffer);
903 sink->current_buffer_size +=
904 gst_buffer_extract (buffer, 0,
905 sink->buffer + sink->current_buffer_size, buffer_size);
910 for (i = 0; i < num_buffers; ++i)
911 gst_buffer_list_add (sink->buffer_list,
912 gst_buffer_ref (gst_buffer_list_get (buffer_list, i)));
913 sink->current_buffer_size += size;
915 if (sink->current_buffer_size > sink->buffer_size)
916 flow = gst_file_sink_flush_buffer (sink);
922 if (flow == GST_FLOW_OK && sync_after) {
924 fsync_ret = fsync (fileno (sink->file));
925 } while (fsync_ret < 0 && errno == EINTR);
927 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
928 (_("Error while writing to file \"%s\"."), sink->filename),
929 ("%s", g_strerror (errno)));
930 flow = GST_FLOW_ERROR;
938 GST_LOG_OBJECT (sink, "empty buffer list");
944 gst_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
946 GstFileSink *filesink;
952 filesink = GST_FILE_SINK_CAST (sink);
954 sync_after = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_SYNC_AFTER);
956 n_mem = gst_buffer_n_memory (buffer);
958 if (n_mem > 0 && (sync_after || (!filesink->buffer
959 && !filesink->buffer_list))) {
960 flow = gst_file_sink_flush_buffer (filesink);
961 if (flow == GST_FLOW_OK) {
962 flow = render_buffer (filesink, buffer);
964 } else if (n_mem > 0) {
965 gsize size = gst_buffer_get_size (buffer);
967 GST_DEBUG_OBJECT (filesink,
968 "Queueing buffer of %" G_GSIZE_FORMAT " bytes at offset %"
969 G_GUINT64_FORMAT, size,
970 filesink->current_pos + filesink->current_buffer_size);
972 if (filesink->buffer) {
973 if (filesink->current_buffer_size + size >
974 filesink->allocated_buffer_size) {
975 flow = gst_file_sink_flush_buffer (filesink);
976 if (flow != GST_FLOW_OK)
980 if (size > filesink->allocated_buffer_size) {
981 GST_DEBUG_OBJECT (sink,
982 "writing buffer ( %" G_GSIZE_FORMAT
983 " bytes) at position %" G_GUINT64_FORMAT,
984 size, filesink->current_pos);
986 flow = render_buffer (filesink, buffer);
988 filesink->current_buffer_size +=
989 gst_buffer_extract (buffer, 0,
990 filesink->buffer + filesink->current_buffer_size, size);
994 filesink->current_buffer_size += gst_buffer_get_size (buffer);
995 gst_buffer_list_add (filesink->buffer_list, gst_buffer_ref (buffer));
997 if (filesink->current_buffer_size > filesink->buffer_size)
998 flow = gst_file_sink_flush_buffer (filesink);
1006 if (flow == GST_FLOW_OK && sync_after) {
1008 fsync_ret = fsync (fileno (filesink->file));
1009 } while (fsync_ret < 0 && errno == EINTR);
1011 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
1012 (_("Error while writing to file \"%s\"."), filesink->filename),
1013 ("%s", g_strerror (errno)));
1014 flow = GST_FLOW_ERROR;
1022 gst_file_sink_start (GstBaseSink * basesink)
1024 GstFileSink *filesink;
1026 filesink = GST_FILE_SINK_CAST (basesink);
1028 g_atomic_int_set (&filesink->flushing, FALSE);
1029 return gst_file_sink_open_file (filesink);
1033 gst_file_sink_stop (GstBaseSink * basesink)
1035 GstFileSink *filesink;
1037 filesink = GST_FILE_SINK_CAST (basesink);
1039 gst_file_sink_close_file (filesink);
1044 gst_file_sink_unlock (GstBaseSink * basesink)
1046 GstFileSink *filesink;
1048 filesink = GST_FILE_SINK_CAST (basesink);
1049 g_atomic_int_set (&filesink->flushing, TRUE);
1055 gst_file_sink_unlock_stop (GstBaseSink * basesink)
1057 GstFileSink *filesink;
1059 filesink = GST_FILE_SINK_CAST (basesink);
1060 g_atomic_int_set (&filesink->flushing, FALSE);
1065 /*** GSTURIHANDLER INTERFACE *************************************************/
1068 gst_file_sink_uri_get_type (GType type)
1070 return GST_URI_SINK;
1073 static const gchar *const *
1074 gst_file_sink_uri_get_protocols (GType type)
1076 static const gchar *protocols[] = { "file", NULL };
1082 gst_file_sink_uri_get_uri (GstURIHandler * handler)
1084 GstFileSink *sink = GST_FILE_SINK (handler);
1086 /* FIXME: make thread-safe */
1087 return g_strdup (sink->uri);
1091 gst_file_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
1096 GstFileSink *sink = GST_FILE_SINK (handler);
1098 /* allow file://localhost/foo/bar by stripping localhost but fail
1099 * for every other hostname */
1100 if (g_str_has_prefix (uri, "file://localhost/")) {
1103 /* 16 == strlen ("file://localhost") */
1104 tmp = g_strconcat ("file://", uri + 16, NULL);
1105 /* we use gst_uri_get_location() although we already have the
1106 * "location" with uri + 16 because it provides unescaping */
1107 location = gst_uri_get_location (tmp);
1109 } else if (strcmp (uri, "file://") == 0) {
1110 /* Special case for "file://" as this is used by some applications
1111 * to test with gst_element_make_from_uri if there's an element
1112 * that supports the URI protocol. */
1113 gst_file_sink_set_location (sink, NULL, NULL);
1116 location = gst_uri_get_location (uri);
1120 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
1121 "File URI without location");
1125 if (!g_path_is_absolute (location)) {
1126 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
1127 "File URI location must be an absolute path");
1132 ret = gst_file_sink_set_location (sink, location, error);
1139 gst_file_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
1141 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1143 iface->get_type = gst_file_sink_uri_get_type;
1144 iface->get_protocols = gst_file_sink_uri_get_protocols;
1145 iface->get_uri = gst_file_sink_uri_get_uri;
1146 iface->set_uri = gst_file_sink_uri_set_uri;