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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, 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 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
59 #ifdef _MSC_VER /* Check if we are using MSVC, fileno is deprecated in favour */
60 #define fileno _fileno /* of _fileno */
69 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
74 #define GST_TYPE_BUFFER_MODE (buffer_mode_get_type ())
76 buffer_mode_get_type (void)
78 static GType buffer_mode_type = 0;
79 static const GEnumValue buffer_mode[] = {
80 {-1, "Default buffering", "default"},
81 {_IOFBF, "Fully buffered", "full"},
82 {_IOLBF, "Line buffered", "line"},
83 {_IONBF, "Unbuffered", "unbuffered"},
87 if (!buffer_mode_type) {
89 g_enum_register_static ("GstFileSinkBufferMode", buffer_mode);
91 return buffer_mode_type;
94 GST_DEBUG_CATEGORY_STATIC (gst_file_sink_debug);
95 #define GST_CAT_DEFAULT gst_file_sink_debug
97 #define DEFAULT_LOCATION NULL
98 #define DEFAULT_BUFFER_MODE -1
99 #define DEFAULT_BUFFER_SIZE 64 * 1024
100 #define DEFAULT_APPEND FALSE
112 /* Copy of glib's g_fopen due to win32 libc/cross-DLL brokenness: we can't
113 * use the 'file pointer' opened in glib (and returned from this function)
114 * in this library, as they may have unrelated C runtimes. */
116 gst_fopen (const gchar * filename, const gchar * mode)
119 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
124 if (wfilename == NULL) {
129 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
137 retval = _wfopen (wfilename, wmode);
146 return fopen (filename, mode);
150 static void gst_file_sink_dispose (GObject * object);
152 static void gst_file_sink_set_property (GObject * object, guint prop_id,
153 const GValue * value, GParamSpec * pspec);
154 static void gst_file_sink_get_property (GObject * object, guint prop_id,
155 GValue * value, GParamSpec * pspec);
157 static gboolean gst_file_sink_open_file (GstFileSink * sink);
158 static void gst_file_sink_close_file (GstFileSink * sink);
160 static gboolean gst_file_sink_start (GstBaseSink * sink);
161 static gboolean gst_file_sink_stop (GstBaseSink * sink);
162 static gboolean gst_file_sink_event (GstBaseSink * sink, GstEvent * event);
163 static GstFlowReturn gst_file_sink_render (GstBaseSink * sink,
166 static gboolean gst_file_sink_do_seek (GstFileSink * filesink,
168 static gboolean gst_file_sink_get_current_offset (GstFileSink * filesink,
171 static gboolean gst_file_sink_query (GstPad * pad, GstQuery * query);
173 static void gst_file_sink_uri_handler_init (gpointer g_iface,
174 gpointer iface_data);
178 _do_init (GType filesink_type)
180 static const GInterfaceInfo urihandler_info = {
181 gst_file_sink_uri_handler_init,
186 g_type_add_interface_static (filesink_type, GST_TYPE_URI_HANDLER,
188 GST_DEBUG_CATEGORY_INIT (gst_file_sink_debug, "filesink", 0,
192 GST_BOILERPLATE_FULL (GstFileSink, gst_file_sink, GstBaseSink,
193 GST_TYPE_BASE_SINK, _do_init);
196 gst_file_sink_base_init (gpointer g_class)
198 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
200 gst_element_class_set_details_simple (gstelement_class,
202 "Sink/File", "Write stream to a file",
203 "Thomas Vander Stichele <thomas at apestaart dot org>");
204 gst_element_class_add_pad_template (gstelement_class,
205 gst_static_pad_template_get (&sinktemplate));
209 gst_file_sink_class_init (GstFileSinkClass * klass)
211 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
212 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
214 gobject_class->dispose = gst_file_sink_dispose;
216 gobject_class->set_property = gst_file_sink_set_property;
217 gobject_class->get_property = gst_file_sink_get_property;
219 g_object_class_install_property (gobject_class, PROP_LOCATION,
220 g_param_spec_string ("location", "File Location",
221 "Location of the file to write", NULL,
222 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
225 g_param_spec_enum ("buffer-mode", "Buffering mode",
226 "The buffering mode to use", GST_TYPE_BUFFER_MODE,
227 DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
230 g_param_spec_uint ("buffer-size", "Buffering size",
231 "Size of buffer in number of bytes for line or full buffer-mode", 0,
232 G_MAXUINT, DEFAULT_BUFFER_SIZE,
233 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
238 * 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 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_file_sink_start);
248 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_file_sink_stop);
249 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_file_sink_render);
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, GstFileSinkClass * g_class)
263 pad = GST_BASE_SINK_PAD (filesink);
265 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_file_sink_query));
267 filesink->filename = NULL;
268 filesink->file = NULL;
269 filesink->buffer_mode = DEFAULT_BUFFER_MODE;
270 filesink->buffer_size = DEFAULT_BUFFER_SIZE;
271 filesink->buffer = NULL;
272 filesink->append = FALSE;
274 gst_base_sink_set_sync (GST_BASE_SINK (filesink), FALSE);
278 gst_file_sink_dispose (GObject * object)
280 GstFileSink *sink = GST_FILE_SINK (object);
282 G_OBJECT_CLASS (parent_class)->dispose (object);
286 g_free (sink->filename);
287 sink->filename = NULL;
288 g_free (sink->buffer);
290 sink->buffer_size = 0;
294 gst_file_sink_set_location (GstFileSink * sink, const gchar * location)
299 g_free (sink->filename);
301 if (location != NULL) {
302 /* we store the filename as we received it from the application. On Windows
303 * this should be in UTF8 */
304 sink->filename = g_strdup (location);
305 sink->uri = gst_filename_to_uri (location, NULL);
306 GST_INFO ("filename : %s", sink->filename);
307 GST_INFO ("uri : %s", sink->uri);
309 sink->filename = NULL;
318 g_warning ("Changing the `location' property on filesink when a file is "
319 "open is not supported.");
325 gst_file_sink_set_property (GObject * object, guint prop_id,
326 const GValue * value, GParamSpec * pspec)
328 GstFileSink *sink = GST_FILE_SINK (object);
332 gst_file_sink_set_location (sink, g_value_get_string (value));
334 case PROP_BUFFER_MODE:
335 sink->buffer_mode = g_value_get_enum (value);
337 case PROP_BUFFER_SIZE:
338 sink->buffer_size = g_value_get_uint (value);
341 sink->append = g_value_get_boolean (value);
344 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
350 gst_file_sink_get_property (GObject * object, guint prop_id, GValue * value,
353 GstFileSink *sink = GST_FILE_SINK (object);
357 g_value_set_string (value, sink->filename);
359 case PROP_BUFFER_MODE:
360 g_value_set_enum (value, sink->buffer_mode);
362 case PROP_BUFFER_SIZE:
363 g_value_set_uint (value, sink->buffer_size);
366 g_value_set_boolean (value, sink->append);
369 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
375 gst_file_sink_open_file (GstFileSink * sink)
380 if (sink->filename == NULL || sink->filename[0] == '\0')
384 sink->file = gst_fopen (sink->filename, "ab");
386 sink->file = gst_fopen (sink->filename, "wb");
387 if (sink->file == NULL)
390 /* see if we are asked to perform a specific kind of buffering */
391 if ((mode = sink->buffer_mode) != -1) {
394 /* free previous buffer if any */
395 g_free (sink->buffer);
397 if (mode == _IONBF) {
402 /* allocate buffer */
403 sink->buffer = g_malloc (sink->buffer_size);
404 buffer_size = sink->buffer_size;
406 #ifdef HAVE_STDIO_EXT_H
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)
451 GST_DEBUG_OBJECT (sink, "closed file");
454 g_free (sink->buffer);
462 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
463 (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
469 gst_file_sink_query (GstPad * pad, GstQuery * query)
474 self = GST_FILE_SINK (GST_PAD_PARENT (pad));
476 switch (GST_QUERY_TYPE (query)) {
477 case GST_QUERY_POSITION:
478 gst_query_parse_position (query, &format, NULL);
480 case GST_FORMAT_DEFAULT:
481 case GST_FORMAT_BYTES:
482 gst_query_set_position (query, GST_FORMAT_BYTES, self->current_pos);
488 case GST_QUERY_FORMATS:
489 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
493 gst_query_set_uri (query, self->uri);
497 return gst_pad_query_default (pad, query);
502 # define __GST_STDIO_SEEK_FUNCTION "fseeko"
503 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
504 # define __GST_STDIO_SEEK_FUNCTION "lseek"
506 # define __GST_STDIO_SEEK_FUNCTION "fseek"
510 gst_file_sink_do_seek (GstFileSink * filesink, guint64 new_offset)
512 GST_DEBUG_OBJECT (filesink, "Seeking to offset %" G_GUINT64_FORMAT
513 " using " __GST_STDIO_SEEK_FUNCTION, new_offset);
515 if (fflush (filesink->file))
519 if (fseeko (filesink->file, (off_t) new_offset, SEEK_SET) != 0)
521 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
522 if (lseek (fileno (filesink->file), (off_t) new_offset,
523 SEEK_SET) == (off_t) - 1)
526 if (fseek (filesink->file, (long) new_offset, SEEK_SET) != 0)
530 /* adjust position reporting after seek;
531 * presumably this should basically yield new_offset */
532 gst_file_sink_get_current_offset (filesink, &filesink->current_pos);
539 GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
544 GST_DEBUG_OBJECT (filesink, "Seeking failed: %s", g_strerror (errno));
549 /* handle events (search) */
551 gst_file_sink_event (GstBaseSink * sink, GstEvent * event)
554 GstFileSink *filesink;
556 filesink = GST_FILE_SINK (sink);
558 type = GST_EVENT_TYPE (event);
561 case GST_EVENT_NEWSEGMENT:
563 gint64 start, stop, pos;
566 gst_event_parse_new_segment (event, NULL, NULL, &format, &start,
569 if (format == GST_FORMAT_BYTES) {
570 /* only try to seek and fail when we are going to a different
572 if (filesink->current_pos != start) {
573 /* FIXME, the seek should be performed on the pos field, start/stop are
574 * just boundaries for valid bytes offsets. We should also fill the file
575 * with zeroes if the new position extends the current EOF (sparse streams
576 * and segment accumulation). */
577 if (!gst_file_sink_do_seek (filesink, (guint64) start))
580 GST_DEBUG_OBJECT (filesink, "Ignored NEWSEGMENT, no seek needed");
583 GST_DEBUG_OBJECT (filesink,
584 "Ignored NEWSEGMENT event of format %u (%s)", (guint) format,
585 gst_format_get_name (format));
590 if (fflush (filesink->file))
602 GST_ELEMENT_ERROR (filesink, RESOURCE, SEEK,
603 (_("Error while seeking in file \"%s\"."), filesink->filename),
609 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
610 (_("Error while writing to file \"%s\"."), filesink->filename),
617 gst_file_sink_get_current_offset (GstFileSink * filesink, guint64 * p_pos)
622 ret = ftello (filesink->file);
623 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
624 if (fflush (filesink->file)) {
625 GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
626 /* ignore and continue */
628 ret = lseek (fileno (filesink->file), 0, SEEK_CUR);
630 ret = (off_t) ftell (filesink->file);
633 if (ret != (off_t) - 1)
634 *p_pos = (guint64) ret;
636 return (ret != (off_t) - 1);
640 gst_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
642 GstFileSink *filesink;
646 filesink = GST_FILE_SINK (sink);
648 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
650 GST_DEBUG_OBJECT (filesink, "writing %u bytes at %" G_GUINT64_FORMAT,
651 size, filesink->current_pos);
653 if (size > 0 && data != NULL) {
654 if (fwrite (data, size, 1, filesink->file) != 1)
657 filesink->current_pos += size;
659 gst_buffer_unmap (buffer, data, size);
667 GST_ELEMENT_ERROR (filesink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
671 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
672 (_("Error while writing to file \"%s\"."), filesink->filename),
673 ("%s", g_strerror (errno)));
676 gst_buffer_unmap (buffer, data, size);
677 return GST_FLOW_ERROR;
682 gst_file_sink_start (GstBaseSink * basesink)
684 return gst_file_sink_open_file (GST_FILE_SINK (basesink));
688 gst_file_sink_stop (GstBaseSink * basesink)
690 gst_file_sink_close_file (GST_FILE_SINK (basesink));
694 /*** GSTURIHANDLER INTERFACE *************************************************/
697 gst_file_sink_uri_get_type (void)
703 gst_file_sink_uri_get_protocols (void)
705 static gchar *protocols[] = { (char *) "file", NULL };
711 gst_file_sink_uri_get_uri (GstURIHandler * handler)
713 GstFileSink *sink = GST_FILE_SINK (handler);
719 gst_file_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
721 gchar *protocol, *location;
723 GstFileSink *sink = GST_FILE_SINK (handler);
725 protocol = gst_uri_get_protocol (uri);
726 if (strcmp (protocol, "file") != 0) {
732 /* allow file://localhost/foo/bar by stripping localhost but fail
733 * for every other hostname */
734 if (g_str_has_prefix (uri, "file://localhost/")) {
737 /* 16 == strlen ("file://localhost") */
738 tmp = g_strconcat ("file://", uri + 16, NULL);
739 /* we use gst_uri_get_location() although we already have the
740 * "location" with uri + 16 because it provides unescaping */
741 location = gst_uri_get_location (tmp);
743 } else if (strcmp (uri, "file://") == 0) {
744 /* Special case for "file://" as this is used by some applications
745 * to test with gst_element_make_from_uri if there's an element
746 * that supports the URI protocol. */
747 gst_file_sink_set_location (sink, NULL);
750 location = gst_uri_get_location (uri);
755 if (!g_path_is_absolute (location)) {
760 ret = gst_file_sink_set_location (sink, location);
767 gst_file_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
769 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
771 iface->get_type = gst_file_sink_uri_get_type;
772 iface->get_protocols = gst_file_sink_uri_get_protocols;
773 iface->get_uri = gst_file_sink_uri_get_uri;
774 iface->set_uri = gst_file_sink_uri_set_uri;