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 * @short_description: write stream to a file
26 * @see_also: #GstFileSrc
28 * Write incoming data to a file in the local file system.
35 #include "../../gst/gst-i18n-lib.h"
38 #include <stdio.h> /* for fseeko() */
40 #include "gstfilesink.h"
43 #include <sys/types.h>
48 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
53 GST_DEBUG_CATEGORY_STATIC (gst_file_sink_debug);
54 #define GST_CAT_DEFAULT gst_file_sink_debug
56 static const GstElementDetails gst_file_sink_details =
57 GST_ELEMENT_DETAILS ("File Sink",
59 "Write stream to a file",
60 "Thomas <thomas@apestaart.org>");
68 static void gst_file_sink_dispose (GObject * object);
70 static void gst_file_sink_set_property (GObject * object, guint prop_id,
71 const GValue * value, GParamSpec * pspec);
72 static void gst_file_sink_get_property (GObject * object, guint prop_id,
73 GValue * value, GParamSpec * pspec);
75 static gboolean gst_file_sink_open_file (GstFileSink * sink);
76 static void gst_file_sink_close_file (GstFileSink * sink);
78 static gboolean gst_file_sink_start (GstBaseSink * sink);
79 static gboolean gst_file_sink_stop (GstBaseSink * sink);
80 static gboolean gst_file_sink_event (GstBaseSink * sink, GstEvent * event);
81 static GstFlowReturn gst_file_sink_render (GstBaseSink * sink,
84 static gboolean gst_file_sink_do_seek (GstFileSink * filesink,
86 static gboolean gst_file_sink_get_current_offset (GstFileSink * filesink,
89 static gboolean gst_file_sink_query (GstPad * pad, GstQuery * query);
91 static void gst_file_sink_uri_handler_init (gpointer g_iface,
96 _do_init (GType filesink_type)
98 static const GInterfaceInfo urihandler_info = {
99 gst_file_sink_uri_handler_init,
104 g_type_add_interface_static (filesink_type, GST_TYPE_URI_HANDLER,
106 GST_DEBUG_CATEGORY_INIT (gst_file_sink_debug, "filesink", 0,
110 GST_BOILERPLATE_FULL (GstFileSink, gst_file_sink, GstBaseSink,
111 GST_TYPE_BASE_SINK, _do_init);
114 gst_file_sink_base_init (gpointer g_class)
116 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
118 gst_element_class_add_pad_template (gstelement_class,
119 gst_static_pad_template_get (&sinktemplate));
120 gst_element_class_set_details (gstelement_class, &gst_file_sink_details);
124 gst_file_sink_class_init (GstFileSinkClass * klass)
126 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
127 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
129 gobject_class->set_property = gst_file_sink_set_property;
130 gobject_class->get_property = gst_file_sink_get_property;
132 g_object_class_install_property (gobject_class, ARG_LOCATION,
133 g_param_spec_string ("location", "File Location",
134 "Location of the file to write", NULL, G_PARAM_READWRITE));
136 gobject_class->dispose = gst_file_sink_dispose;
138 gstbasesink_class->get_times = NULL;
139 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_file_sink_start);
140 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_file_sink_stop);
141 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_file_sink_render);
142 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_file_sink_event);
144 if (sizeof (off_t) < 8) {
145 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
151 gst_file_sink_init (GstFileSink * filesink, GstFileSinkClass * g_class)
155 pad = GST_BASE_SINK_PAD (filesink);
157 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_file_sink_query));
159 filesink->filename = NULL;
160 filesink->file = NULL;
162 gst_base_sink_set_sync (GST_BASE_SINK (filesink), FALSE);
166 gst_file_sink_dispose (GObject * object)
168 GstFileSink *sink = GST_FILE_SINK (object);
170 G_OBJECT_CLASS (parent_class)->dispose (object);
174 g_free (sink->filename);
175 sink->filename = NULL;
179 gst_file_sink_set_location (GstFileSink * sink, const gchar * location)
184 g_free (sink->filename);
186 if (location != NULL) {
187 sink->filename = g_strdup (location);
188 sink->uri = gst_uri_construct ("file", location);
190 sink->filename = NULL;
199 g_warning ("Changing the `location' property on filesink when "
200 "a file is open not supported.");
205 gst_file_sink_set_property (GObject * object, guint prop_id,
206 const GValue * value, GParamSpec * pspec)
208 GstFileSink *sink = GST_FILE_SINK (object);
212 gst_file_sink_set_location (sink, g_value_get_string (value));
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221 gst_file_sink_get_property (GObject * object, guint prop_id, GValue * value,
224 GstFileSink *sink = GST_FILE_SINK (object);
228 g_value_set_string (value, sink->filename);
231 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
237 gst_file_sink_open_file (GstFileSink * sink)
240 if (sink->filename == NULL || sink->filename[0] == '\0')
243 sink->file = fopen (sink->filename, "wb");
244 if (sink->file == NULL)
247 sink->current_pos = 0;
248 /* try to seek in the file to figure out if it is seekable */
249 sink->seekable = gst_file_sink_do_seek (sink, 0);
251 GST_DEBUG_OBJECT (sink, "opened file %s, seekable %d",
252 sink->filename, sink->seekable);
259 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
260 (_("No file name specified for writing.")), (NULL));
265 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
266 (_("Could not open file \"%s\" for writing."), sink->filename),
273 gst_file_sink_close_file (GstFileSink * sink)
276 if (fclose (sink->file) != 0)
279 GST_DEBUG_OBJECT (sink, "closed file");
287 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
288 (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
294 gst_file_sink_query (GstPad * pad, GstQuery * query)
299 self = GST_FILE_SINK (GST_PAD_PARENT (pad));
301 switch (GST_QUERY_TYPE (query)) {
302 case GST_QUERY_POSITION:
303 gst_query_parse_position (query, &format, NULL);
305 case GST_FORMAT_DEFAULT:
306 case GST_FORMAT_BYTES:
307 gst_query_set_position (query, GST_FORMAT_BYTES, self->current_pos);
313 case GST_QUERY_FORMATS:
314 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
318 return gst_pad_query_default (pad, query);
323 # define __GST_STDIO_SEEK_FUNCTION "fseeko"
325 # define __GST_STDIO_SEEK_FUNCTION "lseek"
327 # define __GST_STDIO_SEEK_FUNCTION "fseek"
331 gst_file_sink_do_seek (GstFileSink * filesink, guint64 new_offset)
333 GST_DEBUG_OBJECT (filesink, "Seeking to offset %" G_GUINT64_FORMAT
334 " using " __GST_STDIO_SEEK_FUNCTION, new_offset);
336 if (fflush (filesink->file))
340 if (fseeko (filesink->file, (off_t) new_offset, SEEK_SET) != 0)
343 if (lseek (fileno (filesink->file), (off_t) new_offset,
344 SEEK_SET) == (off_t) - 1)
347 if (fseek (filesink->file, (long) new_offset, SEEK_SET) != 0)
351 /* adjust position reporting after seek;
352 * presumably this should basically yield new_offset */
353 gst_file_sink_get_current_offset (filesink, &filesink->current_pos);
360 GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
365 GST_DEBUG_OBJECT (filesink, "Seeking failed: %s", g_strerror (errno));
370 /* handle events (search) */
372 gst_file_sink_event (GstBaseSink * sink, GstEvent * event)
375 GstFileSink *filesink;
377 filesink = GST_FILE_SINK (sink);
379 type = GST_EVENT_TYPE (event);
382 case GST_EVENT_NEWSEGMENT:
384 gint64 start, stop, pos;
387 gst_event_parse_new_segment (event, NULL, NULL, &format, &start,
390 if (format == GST_FORMAT_BYTES) {
391 /* FIXME, the seek should be performed on the pos field, start/stop are
392 * just boundaries for valid bytes offsets. We should also fill the file
393 * with zeroes if the new position extends the current EOF (sparse streams
394 * and segment accumulation). */
395 if (!gst_file_sink_do_seek (filesink, (guint64) start))
398 GST_DEBUG ("Ignored NEWSEGMENT event of format %u (%s)",
399 (guint) format, gst_format_get_name (format));
404 if (fflush (filesink->file))
416 GST_ELEMENT_ERROR (filesink, RESOURCE, SEEK,
417 (_("Error while seeking in file \"%s\"."), filesink->filename),
423 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
424 (_("Error while writing to file \"%s\"."), filesink->filename),
431 gst_file_sink_get_current_offset (GstFileSink * filesink, guint64 * p_pos)
436 ret = ftello (filesink->file);
438 if (fflush (filesink->file)) {
439 GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
440 /* ignore and continue */
442 ret = lseek (fileno (filesink->file), 0, SEEK_CUR);
444 ret = (off_t) ftell (filesink->file);
447 if (ret != (off_t) - 1)
448 *p_pos = (guint64) ret;
450 return (ret != (off_t) - 1);
454 gst_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
456 GstFileSink *filesink;
459 size = GST_BUFFER_SIZE (buffer);
461 filesink = GST_FILE_SINK (sink);
463 GST_DEBUG_OBJECT (filesink, "writing %u bytes at %" G_GUINT64_FORMAT,
464 size, filesink->current_pos);
466 if (size > 0 && GST_BUFFER_DATA (buffer) != NULL) {
467 if (fwrite (GST_BUFFER_DATA (buffer), size, 1, filesink->file) != 1)
470 filesink->current_pos += size;
479 GST_ELEMENT_ERROR (filesink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
483 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
484 (_("Error while writing to file \"%s\"."), filesink->filename),
485 ("%s", g_strerror (errno)));
488 return GST_FLOW_ERROR;
493 gst_file_sink_start (GstBaseSink * basesink)
495 return gst_file_sink_open_file (GST_FILE_SINK (basesink));
499 gst_file_sink_stop (GstBaseSink * basesink)
501 gst_file_sink_close_file (GST_FILE_SINK (basesink));
505 /*** GSTURIHANDLER INTERFACE *************************************************/
508 gst_file_sink_uri_get_type (void)
513 gst_file_sink_uri_get_protocols (void)
515 static gchar *protocols[] = { "file", NULL };
520 gst_file_sink_uri_get_uri (GstURIHandler * handler)
522 GstFileSink *sink = GST_FILE_SINK (handler);
528 gst_file_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
530 gchar *protocol, *location;
532 GstFileSink *sink = GST_FILE_SINK (handler);
534 protocol = gst_uri_get_protocol (uri);
535 if (strcmp (protocol, "file") != 0) {
541 /* allow file://localhost/foo/bar by stripping localhost but fail
542 * for every other hostname */
543 if (g_str_has_prefix (uri, "file://localhost/")) {
546 /* 16 == strlen ("file://localhost") */
547 tmp = g_strconcat ("file://", uri + 16, NULL);
548 /* we use gst_uri_get_location() although we already have the
549 * "location" with uri + 16 because it provides unescaping */
550 location = gst_uri_get_location (tmp);
552 } else if (strcmp (uri, "file://") == 0) {
553 /* Special case for "file://" as this is used by some applications
554 * to test with gst_element_make_from_uri if there's an element
555 * that supports the URI protocol. */
556 gst_file_sink_set_location (sink, NULL);
559 location = gst_uri_get_location (uri);
564 if (!g_path_is_absolute (location)) {
569 ret = gst_file_sink_set_location (sink, location);
576 gst_file_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
578 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
580 iface->get_type = gst_file_sink_uri_get_type;
581 iface->get_protocols = gst_file_sink_uri_get_protocols;
582 iface->get_uri = gst_file_sink_uri_get_uri;
583 iface->set_uri = gst_file_sink_uri_set_uri;