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>
55 #include <io.h> /* lseek, open, close, read */
57 #define lseek _lseeki64
61 #define ftruncate _chsize
64 #ifdef _MSC_VER /* Check if we are using MSVC, fileno is deprecated in favour */
65 #define fileno _fileno /* of _fileno */
74 #include "gstelements_private.h"
75 #include "gstfilesink.h"
77 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
82 #define GST_TYPE_FILE_SINK_BUFFER_MODE (gst_file_sink_buffer_mode_get_type ())
84 gst_file_sink_buffer_mode_get_type (void)
86 static GType buffer_mode_type = 0;
87 static const GEnumValue buffer_mode[] = {
88 {GST_FILE_SINK_BUFFER_MODE_DEFAULT, "Default buffering", "default"},
89 {GST_FILE_SINK_BUFFER_MODE_FULL, "Fully buffered", "full"},
90 {GST_FILE_SINK_BUFFER_MODE_LINE, "Line buffered (deprecated, like full)",
92 {GST_FILE_SINK_BUFFER_MODE_UNBUFFERED, "Unbuffered", "unbuffered"},
96 if (!buffer_mode_type) {
98 g_enum_register_static ("GstFileSinkBufferMode", buffer_mode);
100 return buffer_mode_type;
103 GST_DEBUG_CATEGORY_STATIC (gst_file_sink_debug);
104 #define GST_CAT_DEFAULT gst_file_sink_debug
106 #define DEFAULT_LOCATION NULL
107 #define DEFAULT_BUFFER_MODE GST_FILE_SINK_BUFFER_MODE_DEFAULT
108 #define DEFAULT_BUFFER_SIZE 64 * 1024
109 #define DEFAULT_APPEND FALSE
110 #define DEFAULT_O_SYNC FALSE
111 #define DEFAULT_MAX_TRANSIENT_ERROR_TIMEOUT 0
121 PROP_MAX_TRANSIENT_ERROR_TIMEOUT,
125 /* Copy of glib's g_fopen due to win32 libc/cross-DLL brokenness: we can't
126 * use the 'file pointer' opened in glib (and returned from this function)
127 * in this library, as they may have unrelated C runtimes. */
129 gst_fopen (const gchar * filename, const gchar * mode, gboolean o_sync)
133 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
137 if (wfilename == NULL) {
142 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
150 retval = _wfopen (wfilename, wmode);
160 int flags = O_CREAT | O_WRONLY;
162 if (strcmp (mode, "wb") == 0)
164 else if (strcmp (mode, "ab") == 0)
167 g_assert_not_reached ();
172 fd = open (filename, flags, 0666);
174 retval = fdopen (fd, mode);
179 static void gst_file_sink_dispose (GObject * object);
181 static void gst_file_sink_set_property (GObject * object, guint prop_id,
182 const GValue * value, GParamSpec * pspec);
183 static void gst_file_sink_get_property (GObject * object, guint prop_id,
184 GValue * value, GParamSpec * pspec);
186 static gboolean gst_file_sink_open_file (GstFileSink * sink);
187 static void gst_file_sink_close_file (GstFileSink * sink);
189 static gboolean gst_file_sink_start (GstBaseSink * sink);
190 static gboolean gst_file_sink_stop (GstBaseSink * sink);
191 static gboolean gst_file_sink_event (GstBaseSink * sink, GstEvent * event);
192 static GstFlowReturn gst_file_sink_render (GstBaseSink * sink,
194 static GstFlowReturn gst_file_sink_render_list (GstBaseSink * sink,
195 GstBufferList * list);
196 static gboolean gst_file_sink_unlock (GstBaseSink * sink);
197 static gboolean gst_file_sink_unlock_stop (GstBaseSink * sink);
199 static gboolean gst_file_sink_do_seek (GstFileSink * filesink,
201 static gboolean gst_file_sink_get_current_offset (GstFileSink * filesink,
204 static gboolean gst_file_sink_query (GstBaseSink * bsink, GstQuery * query);
206 static void gst_file_sink_uri_handler_init (gpointer g_iface,
207 gpointer iface_data);
209 static GstFlowReturn gst_file_sink_flush_buffer (GstFileSink * filesink);
212 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_file_sink_uri_handler_init); \
213 GST_DEBUG_CATEGORY_INIT (gst_file_sink_debug, "filesink", 0, "filesink element");
214 #define gst_file_sink_parent_class parent_class
215 G_DEFINE_TYPE_WITH_CODE (GstFileSink, gst_file_sink, GST_TYPE_BASE_SINK,
219 gst_file_sink_class_init (GstFileSinkClass * klass)
221 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
222 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
223 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
225 gobject_class->dispose = gst_file_sink_dispose;
227 gobject_class->set_property = gst_file_sink_set_property;
228 gobject_class->get_property = gst_file_sink_get_property;
230 g_object_class_install_property (gobject_class, PROP_LOCATION,
231 g_param_spec_string ("location", "File Location",
232 "Location of the file to write", NULL,
233 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
235 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
236 g_param_spec_enum ("buffer-mode", "Buffering mode",
237 "The buffering mode to use", GST_TYPE_FILE_SINK_BUFFER_MODE,
238 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
241 g_param_spec_uint ("buffer-size", "Buffering size",
242 "Size of buffer in number of bytes for line or full buffer-mode", 0,
243 G_MAXUINT, DEFAULT_BUFFER_SIZE,
244 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
249 * Append to an already existing file.
251 g_object_class_install_property (gobject_class, PROP_APPEND,
252 g_param_spec_boolean ("append", "Append",
253 "Append to an already existing file", DEFAULT_APPEND,
254 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256 g_object_class_install_property (gobject_class, PROP_O_SYNC,
257 g_param_spec_boolean ("o-sync", "Synchronous IO",
258 "Open the file with O_SYNC for enabling synchronous IO",
259 DEFAULT_O_SYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
261 g_object_class_install_property (gobject_class,
262 PROP_MAX_TRANSIENT_ERROR_TIMEOUT,
263 g_param_spec_int ("max-transient-error-timeout",
264 "Max Transient Error Timeout",
265 "Retry up to this many ms on transient errors (currently EACCES)", 0,
266 G_MAXINT, DEFAULT_MAX_TRANSIENT_ERROR_TIMEOUT,
267 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
269 gst_element_class_set_static_metadata (gstelement_class,
271 "Sink/File", "Write stream to a file",
272 "Thomas Vander Stichele <thomas at apestaart dot org>");
273 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
275 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_file_sink_start);
276 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_file_sink_stop);
277 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_file_sink_query);
278 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_file_sink_render);
279 gstbasesink_class->render_list =
280 GST_DEBUG_FUNCPTR (gst_file_sink_render_list);
281 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_file_sink_event);
282 gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_file_sink_unlock);
283 gstbasesink_class->unlock_stop =
284 GST_DEBUG_FUNCPTR (gst_file_sink_unlock_stop);
286 if (sizeof (off_t) < 8) {
287 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
293 gst_file_sink_init (GstFileSink * filesink)
295 filesink->filename = NULL;
296 filesink->file = NULL;
297 filesink->current_pos = 0;
298 filesink->buffer_mode = DEFAULT_BUFFER_MODE;
299 filesink->buffer_size = DEFAULT_BUFFER_SIZE;
300 filesink->append = FALSE;
302 gst_base_sink_set_sync (GST_BASE_SINK (filesink), FALSE);
306 gst_file_sink_dispose (GObject * object)
308 GstFileSink *sink = GST_FILE_SINK (object);
310 G_OBJECT_CLASS (parent_class)->dispose (object);
314 g_free (sink->filename);
315 sink->filename = NULL;
319 gst_file_sink_set_location (GstFileSink * sink, const gchar * location,
325 g_free (sink->filename);
327 if (location != NULL) {
328 /* we store the filename as we received it from the application. On Windows
329 * this should be in UTF8 */
330 sink->filename = g_strdup (location);
331 sink->uri = gst_filename_to_uri (location, NULL);
332 GST_INFO_OBJECT (sink, "filename : %s", sink->filename);
333 GST_INFO_OBJECT (sink, "uri : %s", sink->uri);
335 sink->filename = NULL;
344 g_warning ("Changing the `location' property on filesink when a file is "
345 "open is not supported.");
346 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
347 "Changing the 'location' property on filesink when a file is "
348 "open is not supported");
354 gst_file_sink_set_property (GObject * object, guint prop_id,
355 const GValue * value, GParamSpec * pspec)
357 GstFileSink *sink = GST_FILE_SINK (object);
361 gst_file_sink_set_location (sink, g_value_get_string (value), NULL);
363 case PROP_BUFFER_MODE:
364 sink->buffer_mode = g_value_get_enum (value);
366 case PROP_BUFFER_SIZE:
367 sink->buffer_size = g_value_get_uint (value);
370 sink->append = g_value_get_boolean (value);
373 sink->o_sync = g_value_get_boolean (value);
375 case PROP_MAX_TRANSIENT_ERROR_TIMEOUT:
376 sink->max_transient_error_timeout = g_value_get_int (value);
379 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
385 gst_file_sink_get_property (GObject * object, guint prop_id, GValue * value,
388 GstFileSink *sink = GST_FILE_SINK (object);
392 g_value_set_string (value, sink->filename);
394 case PROP_BUFFER_MODE:
395 g_value_set_enum (value, sink->buffer_mode);
397 case PROP_BUFFER_SIZE:
398 g_value_set_uint (value, sink->buffer_size);
401 g_value_set_boolean (value, sink->append);
404 g_value_set_boolean (value, sink->o_sync);
406 case PROP_MAX_TRANSIENT_ERROR_TIMEOUT:
407 g_value_set_int (value, sink->max_transient_error_timeout);
410 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
416 gst_file_sink_open_file (GstFileSink * sink)
419 if (sink->filename == NULL || sink->filename[0] == '\0')
423 sink->file = gst_fopen (sink->filename, "ab", sink->o_sync);
425 sink->file = gst_fopen (sink->filename, "wb", sink->o_sync);
426 if (sink->file == NULL)
429 sink->current_pos = 0;
430 /* try to seek in the file to figure out if it is seekable */
431 sink->seekable = gst_file_sink_do_seek (sink, 0);
434 gst_buffer_list_unref (sink->buffer);
436 if (sink->buffer_mode != GST_FILE_SINK_BUFFER_MODE_UNBUFFERED) {
437 if (sink->buffer_size == 0) {
438 sink->buffer_size = DEFAULT_BUFFER_SIZE;
439 g_object_notify (G_OBJECT (sink), "buffer-size");
442 sink->buffer = gst_buffer_list_new ();
443 sink->current_buffer_size = 0;
446 GST_DEBUG_OBJECT (sink, "opened file %s, seekable %d",
447 sink->filename, sink->seekable);
454 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
455 (_("No file name specified for writing.")), (NULL));
460 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
461 (_("Could not open file \"%s\" for writing."), sink->filename),
468 gst_file_sink_close_file (GstFileSink * sink)
471 if (gst_file_sink_flush_buffer (sink) != GST_FLOW_OK)
472 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
473 (_("Error closing file \"%s\"."), sink->filename), NULL);
475 if (fclose (sink->file) != 0)
476 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
477 (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
479 GST_DEBUG_OBJECT (sink, "closed file");
484 gst_buffer_list_unref (sink->buffer);
487 sink->current_buffer_size = 0;
491 gst_file_sink_query (GstBaseSink * bsink, GstQuery * query)
497 self = GST_FILE_SINK (bsink);
499 switch (GST_QUERY_TYPE (query)) {
500 case GST_QUERY_POSITION:
501 gst_query_parse_position (query, &format, NULL);
504 case GST_FORMAT_DEFAULT:
505 case GST_FORMAT_BYTES:
506 gst_query_set_position (query, GST_FORMAT_BYTES,
507 self->current_pos + self->current_buffer_size);
516 case GST_QUERY_FORMATS:
517 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
522 gst_query_set_uri (query, self->uri);
526 case GST_QUERY_SEEKING:
527 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
528 if (format == GST_FORMAT_BYTES || format == GST_FORMAT_DEFAULT) {
529 gst_query_set_seeking (query, GST_FORMAT_BYTES, self->seekable, 0, -1);
531 gst_query_set_seeking (query, format, FALSE, 0, -1);
537 res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
544 # define __GST_STDIO_SEEK_FUNCTION "fseeko"
545 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
546 # define __GST_STDIO_SEEK_FUNCTION "lseek"
548 # define __GST_STDIO_SEEK_FUNCTION "fseek"
552 gst_file_sink_do_seek (GstFileSink * filesink, guint64 new_offset)
554 GST_DEBUG_OBJECT (filesink, "Seeking to offset %" G_GUINT64_FORMAT
555 " using " __GST_STDIO_SEEK_FUNCTION, new_offset);
557 if (gst_file_sink_flush_buffer (filesink) != GST_FLOW_OK)
558 goto flush_buffer_failed;
561 if (fseeko (filesink->file, (off_t) new_offset, SEEK_SET) != 0)
563 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
564 if (lseek (fileno (filesink->file), (off_t) new_offset,
565 SEEK_SET) == (off_t) - 1)
568 if (fseek (filesink->file, (long) new_offset, SEEK_SET) != 0)
572 /* adjust position reporting after seek;
573 * presumably this should basically yield new_offset */
574 gst_file_sink_get_current_offset (filesink, &filesink->current_pos);
581 GST_DEBUG_OBJECT (filesink, "Flushing buffer failed");
586 GST_DEBUG_OBJECT (filesink, "Seeking failed: %s", g_strerror (errno));
591 /* handle events (search) */
593 gst_file_sink_event (GstBaseSink * sink, GstEvent * event)
596 GstFileSink *filesink;
598 filesink = GST_FILE_SINK (sink);
600 type = GST_EVENT_TYPE (event);
603 case GST_EVENT_SEGMENT:
605 const GstSegment *segment;
607 gst_event_parse_segment (event, &segment);
609 if (segment->format == GST_FORMAT_BYTES) {
610 /* only try to seek and fail when we are going to a different
612 if (filesink->current_pos + filesink->current_buffer_size !=
614 /* FIXME, the seek should be performed on the pos field, start/stop are
615 * just boundaries for valid bytes offsets. We should also fill the file
616 * with zeroes if the new position extends the current EOF (sparse streams
617 * and segment accumulation). */
618 if (!gst_file_sink_do_seek (filesink, (guint64) segment->start))
621 GST_DEBUG_OBJECT (filesink, "Ignored SEGMENT, no seek needed");
624 GST_DEBUG_OBJECT (filesink,
625 "Ignored SEGMENT event of format %u (%s)", (guint) segment->format,
626 gst_format_get_name (segment->format));
630 case GST_EVENT_FLUSH_STOP:
631 if (filesink->current_pos != 0 && filesink->seekable) {
632 gst_file_sink_do_seek (filesink, 0);
633 if (ftruncate (fileno (filesink->file), 0))
634 goto truncate_failed;
636 if (filesink->buffer) {
637 gst_buffer_list_unref (filesink->buffer);
638 filesink->buffer = gst_buffer_list_new ();
639 filesink->current_buffer_size = 0;
643 if (gst_file_sink_flush_buffer (filesink) != GST_FLOW_OK)
644 goto flush_buffer_failed;
650 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
655 GST_ELEMENT_ERROR (filesink, RESOURCE, SEEK,
656 (_("Error while seeking in file \"%s\"."), filesink->filename),
658 gst_event_unref (event);
663 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
664 (_("Error while writing to file \"%s\"."), filesink->filename), NULL);
665 gst_event_unref (event);
670 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
671 (_("Error while writing to file \"%s\"."), filesink->filename),
673 gst_event_unref (event);
679 gst_file_sink_get_current_offset (GstFileSink * filesink, guint64 * p_pos)
683 /* no need to flush internal buffer here as this is only called right
684 * after a seek. If this changes then the buffer should be flushed here
689 ret = ftello (filesink->file);
690 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
691 ret = lseek (fileno (filesink->file), 0, SEEK_CUR);
693 ret = (off_t) ftell (filesink->file);
696 if (ret != (off_t) - 1)
697 *p_pos = (guint64) ret;
699 return (ret != (off_t) - 1);
703 gst_file_sink_render_buffers (GstFileSink * sink, GstBuffer ** buffers,
704 guint num_buffers, guint8 * mem_nums, guint total_mems, gsize size)
707 guint64 bytes_written = 0;
709 GST_DEBUG_OBJECT (sink,
710 "writing %u buffers (%u memories, %" G_GSIZE_FORMAT
711 " bytes) at position %" G_GUINT64_FORMAT, num_buffers, total_mems, size,
714 ret = gst_writev_buffers (GST_OBJECT_CAST (sink), fileno (sink->file), NULL,
715 buffers, num_buffers, mem_nums, total_mems, &bytes_written, 0,
716 sink->max_transient_error_timeout, sink->current_pos, &sink->flushing);
718 sink->current_pos += bytes_written;
724 gst_file_sink_render_list_internal (GstFileSink * sink,
725 GstBufferList * buffer_list)
731 gsize total_size = 0;
732 guint i, num_buffers;
734 num_buffers = gst_buffer_list_length (buffer_list);
735 if (num_buffers == 0)
738 /* extract buffers from list and count memories */
739 buffers = g_newa (GstBuffer *, num_buffers);
740 mem_nums = g_newa (guint8, num_buffers);
741 for (i = 0, total_mems = 0; i < num_buffers; ++i) {
742 buffers[i] = gst_buffer_list_get (buffer_list, i);
743 mem_nums[i] = gst_buffer_n_memory (buffers[i]);
744 total_mems += mem_nums[i];
745 total_size += gst_buffer_get_size (buffers[i]);
749 gst_file_sink_render_buffers (sink, buffers, num_buffers, mem_nums,
750 total_mems, total_size);
756 GST_LOG_OBJECT (sink, "empty buffer list");
762 gst_file_sink_flush_buffer (GstFileSink * filesink)
764 GstFlowReturn flow_ret = GST_FLOW_OK;
766 if (filesink->buffer) {
769 length = gst_buffer_list_length (filesink->buffer);
772 GST_DEBUG_OBJECT (filesink, "Flushing out buffer of size %u",
773 filesink->current_buffer_size);
775 gst_file_sink_render_list_internal (filesink, filesink->buffer);
776 /* Remove all buffers from the list but keep the list. This ensures that
777 * we don't re-allocate the array storing the buffers all the time */
778 gst_buffer_list_remove (filesink->buffer, 0, length);
779 filesink->current_buffer_size = 0;
787 has_sync_after_buffer (GstBuffer ** buffer, guint idx, gpointer user_data)
789 if (GST_BUFFER_FLAG_IS_SET (*buffer, GST_BUFFER_FLAG_SYNC_AFTER)) {
790 gboolean *sync_after = user_data;
800 accumulate_size (GstBuffer ** buffer, guint idx, gpointer user_data)
802 guint *size = user_data;
804 *size += gst_buffer_get_size (*buffer);
810 gst_file_sink_render_list (GstBaseSink * bsink, GstBufferList * buffer_list)
814 guint i, num_buffers;
815 gboolean sync_after = FALSE;
817 sink = GST_FILE_SINK_CAST (bsink);
819 num_buffers = gst_buffer_list_length (buffer_list);
820 if (num_buffers == 0)
823 gst_buffer_list_foreach (buffer_list, has_sync_after_buffer, &sync_after);
825 if (sync_after || !sink->buffer) {
826 flow = gst_file_sink_flush_buffer (sink);
827 if (flow == GST_FLOW_OK)
828 flow = gst_file_sink_render_list_internal (sink, buffer_list);
831 gst_buffer_list_foreach (buffer_list, accumulate_size, &size);
833 GST_DEBUG_OBJECT (sink,
834 "Queueing buffer list of %u bytes (%u buffers) at offset %"
835 G_GUINT64_FORMAT, size, num_buffers,
836 sink->current_pos + sink->current_buffer_size);
838 for (i = 0; i < num_buffers; ++i)
839 gst_buffer_list_add (sink->buffer,
840 gst_buffer_ref (gst_buffer_list_get (buffer_list, i)));
841 sink->current_buffer_size += size;
843 if (sink->current_buffer_size > sink->buffer_size)
844 flow = gst_file_sink_flush_buffer (sink);
849 if (flow == GST_FLOW_OK && sync_after) {
850 if (fsync (fileno (sink->file))) {
851 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
852 (_("Error while writing to file \"%s\"."), sink->filename),
853 ("%s", g_strerror (errno)));
854 flow = GST_FLOW_ERROR;
862 GST_LOG_OBJECT (sink, "empty buffer list");
868 gst_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
870 GstFileSink *filesink;
875 filesink = GST_FILE_SINK_CAST (sink);
877 sync_after = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_SYNC_AFTER);
879 n_mem = gst_buffer_n_memory (buffer);
881 if (n_mem > 0 && (sync_after || !filesink->buffer)) {
882 flow = gst_file_sink_flush_buffer (filesink);
883 if (flow == GST_FLOW_OK)
885 gst_file_sink_render_buffers (filesink, &buffer, 1, &n_mem, n_mem,
886 gst_buffer_get_size (buffer));
887 } else if (n_mem > 0) {
888 GST_DEBUG_OBJECT (filesink,
889 "Queueing buffer of %" G_GSIZE_FORMAT " bytes at offset %"
890 G_GUINT64_FORMAT, gst_buffer_get_size (buffer),
891 filesink->current_pos + filesink->current_buffer_size);
893 filesink->current_buffer_size += gst_buffer_get_size (buffer);
894 gst_buffer_list_add (filesink->buffer, gst_buffer_ref (buffer));
896 if (filesink->current_buffer_size > filesink->buffer_size)
897 flow = gst_file_sink_flush_buffer (filesink);
904 if (flow == GST_FLOW_OK && sync_after) {
905 if (fsync (fileno (filesink->file))) {
906 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
907 (_("Error while writing to file \"%s\"."), filesink->filename),
908 ("%s", g_strerror (errno)));
909 flow = GST_FLOW_ERROR;
917 gst_file_sink_start (GstBaseSink * basesink)
919 GstFileSink *filesink;
921 filesink = GST_FILE_SINK_CAST (basesink);
923 g_atomic_int_set (&filesink->flushing, FALSE);
924 return gst_file_sink_open_file (filesink);
928 gst_file_sink_stop (GstBaseSink * basesink)
930 GstFileSink *filesink;
932 filesink = GST_FILE_SINK_CAST (basesink);
934 gst_file_sink_close_file (filesink);
935 g_atomic_int_set (&filesink->flushing, TRUE);
940 gst_file_sink_unlock (GstBaseSink * basesink)
942 GstFileSink *filesink;
944 filesink = GST_FILE_SINK_CAST (basesink);
945 g_atomic_int_set (&filesink->flushing, TRUE);
951 gst_file_sink_unlock_stop (GstBaseSink * basesink)
953 GstFileSink *filesink;
955 filesink = GST_FILE_SINK_CAST (basesink);
956 g_atomic_int_set (&filesink->flushing, FALSE);
961 /*** GSTURIHANDLER INTERFACE *************************************************/
964 gst_file_sink_uri_get_type (GType type)
969 static const gchar *const *
970 gst_file_sink_uri_get_protocols (GType type)
972 static const gchar *protocols[] = { "file", NULL };
978 gst_file_sink_uri_get_uri (GstURIHandler * handler)
980 GstFileSink *sink = GST_FILE_SINK (handler);
982 /* FIXME: make thread-safe */
983 return g_strdup (sink->uri);
987 gst_file_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
992 GstFileSink *sink = GST_FILE_SINK (handler);
994 /* allow file://localhost/foo/bar by stripping localhost but fail
995 * for every other hostname */
996 if (g_str_has_prefix (uri, "file://localhost/")) {
999 /* 16 == strlen ("file://localhost") */
1000 tmp = g_strconcat ("file://", uri + 16, NULL);
1001 /* we use gst_uri_get_location() although we already have the
1002 * "location" with uri + 16 because it provides unescaping */
1003 location = gst_uri_get_location (tmp);
1005 } else if (strcmp (uri, "file://") == 0) {
1006 /* Special case for "file://" as this is used by some applications
1007 * to test with gst_element_make_from_uri if there's an element
1008 * that supports the URI protocol. */
1009 gst_file_sink_set_location (sink, NULL, NULL);
1012 location = gst_uri_get_location (uri);
1016 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
1017 "File URI without location");
1021 if (!g_path_is_absolute (location)) {
1022 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
1023 "File URI location must be an absolute path");
1028 ret = gst_file_sink_set_location (sink, location, error);
1035 gst_file_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
1037 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1039 iface->get_type = gst_file_sink_uri_get_type;
1040 iface->get_protocols = gst_file_sink_uri_get_protocols;
1041 iface->get_uri = gst_file_sink_uri_get_uri;
1042 iface->set_uri = gst_file_sink_uri_set_uri;