2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * @short_description: write stream to a file
25 * @see_also: #GstFileSrc
27 * Wrtite incomming data to a file in the local file system.
34 #include "../gst-i18n-lib.h"
38 #include "gstfilesink.h"
41 #include <sys/types.h>
47 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
52 GST_DEBUG_CATEGORY_STATIC (gst_file_sink_debug);
53 #define GST_CAT_DEFAULT gst_file_sink_debug
55 static GstElementDetails gst_file_sink_details =
56 GST_ELEMENT_DETAILS ("File Sink",
58 "Write stream to a file",
59 "Thomas <thomas@apestaart.org>");
62 /* FileSink signals and args */
76 static void gst_file_sink_dispose (GObject * object);
78 static void gst_file_sink_set_property (GObject * object, guint prop_id,
79 const GValue * value, GParamSpec * pspec);
80 static void gst_file_sink_get_property (GObject * object, guint prop_id,
81 GValue * value, GParamSpec * pspec);
83 static gboolean gst_file_sink_open_file (GstFileSink * sink);
84 static void gst_file_sink_close_file (GstFileSink * sink);
86 static gboolean gst_file_sink_event (GstBaseSink * sink, GstEvent * event);
87 static GstFlowReturn gst_file_sink_render (GstBaseSink * sink,
90 static gboolean gst_file_sink_query (GstPad * pad, GstQuery * query);
92 static void gst_file_sink_uri_handler_init (gpointer g_iface,
95 static GstStateChangeReturn gst_file_sink_change_state (GstElement * element,
96 GstStateChange transition);
98 //static guint gst_file_sink_signals[LAST_SIGNAL] = { 0 };
101 _do_init (GType filesink_type)
103 static const GInterfaceInfo urihandler_info = {
104 gst_file_sink_uri_handler_init,
109 g_type_add_interface_static (filesink_type, GST_TYPE_URI_HANDLER,
111 GST_DEBUG_CATEGORY_INIT (gst_file_sink_debug, "filesink", 0,
115 GST_BOILERPLATE_FULL (GstFileSink, gst_file_sink, GstBaseSink,
116 GST_TYPE_BASE_SINK, _do_init);
119 gst_file_sink_base_init (gpointer g_class)
121 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
123 gstelement_class->change_state = gst_file_sink_change_state;
124 gst_element_class_add_pad_template (gstelement_class,
125 gst_static_pad_template_get (&sinktemplate));
126 gst_element_class_set_details (gstelement_class, &gst_file_sink_details);
130 gst_file_sink_class_init (GstFileSinkClass * klass)
132 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
133 GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
135 gobject_class->set_property = gst_file_sink_set_property;
136 gobject_class->get_property = gst_file_sink_get_property;
138 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATION,
139 g_param_spec_string ("location", "File Location",
140 "Location of the file to write", NULL, G_PARAM_READWRITE));
142 gobject_class->dispose = gst_file_sink_dispose;
144 gstbasesink_class->get_times = NULL;
145 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_file_sink_render);
146 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_file_sink_event);
148 if (sizeof (off_t) < 8) {
149 GST_LOG ("No large file support, sizeof (off_t) = %u", sizeof (off_t));
154 gst_file_sink_init (GstFileSink * filesink, GstFileSinkClass * g_class)
158 pad = GST_BASE_SINK_PAD (filesink);
160 gst_pad_set_query_function (pad, gst_file_sink_query);
162 filesink->filename = NULL;
163 filesink->file = NULL;
165 GST_BASE_SINK (filesink)->sync = FALSE;
169 gst_file_sink_dispose (GObject * object)
171 GstFileSink *sink = GST_FILE_SINK (object);
173 G_OBJECT_CLASS (parent_class)->dispose (object);
177 g_free (sink->filename);
178 sink->filename = NULL;
182 gst_file_sink_set_location (GstFileSink * sink, const gchar * location)
184 /* the element must be stopped or paused in order to do this */
185 if (GST_STATE (sink) >= GST_STATE_PAUSED)
188 g_free (sink->filename);
190 if (location != NULL) {
191 sink->filename = g_strdup (location);
192 sink->uri = gst_uri_construct ("file", location);
194 sink->filename = NULL;
201 gst_file_sink_set_property (GObject * object, guint prop_id,
202 const GValue * value, GParamSpec * pspec)
206 sink = GST_FILE_SINK (object);
210 gst_file_sink_set_location (sink, g_value_get_string (value));
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219 gst_file_sink_get_property (GObject * object, guint prop_id, GValue * value,
224 g_return_if_fail (GST_IS_FILE_SINK (object));
226 sink = GST_FILE_SINK (object);
230 g_value_set_string (value, sink->filename);
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
239 gst_file_sink_open_file (GstFileSink * sink)
242 if (sink->filename == NULL || sink->filename[0] == '\0') {
243 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
244 (_("No file name specified for writing.")), (NULL));
248 sink->file = fopen (sink->filename, "wb");
249 if (sink->file == NULL) {
250 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
251 (_("Could not open file \"%s\" for writing."), sink->filename),
256 sink->data_written = 0;
262 gst_file_sink_close_file (GstFileSink * sink)
265 if (fclose (sink->file) != 0) {
266 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
267 (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
273 gst_file_sink_query (GstPad * pad, GstQuery * query)
278 self = GST_FILE_SINK (GST_PAD_PARENT (pad));
280 switch (GST_QUERY_TYPE (query)) {
281 case GST_QUERY_POSITION:
282 gst_query_parse_position (query, &format, NULL, NULL);
284 case GST_FORMAT_DEFAULT:
285 case GST_FORMAT_BYTES:
286 gst_query_set_position (query, GST_FORMAT_BYTES,
287 self->data_written, self->data_written);
293 case GST_QUERY_FORMATS:
294 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
298 return gst_pad_query_default (pad, query);
303 gst_file_sink_do_seek (GstFileSink * filesink, guint64 new_offset)
305 GST_DEBUG_OBJECT (filesink, "Seeking to offset %" G_GUINT64_FORMAT,
309 if (lseek (fileno (filesink->file), (off_t) new_offset,
310 SEEK_SET) != (off_t) - 1)
313 if (fseek (filesink->file, (long) new_offset, SEEK_SET) == 0)
317 GST_DEBUG_OBJECT (filesink, "Seeking failed: %s", g_strerror (errno));
320 /* handle events (search) */
322 gst_file_sink_event (GstBaseSink * sink, GstEvent * event)
325 GstFileSink *filesink;
327 filesink = GST_FILE_SINK (sink);
329 type = GST_EVENT_TYPE (event);
332 case GST_EVENT_NEWSEGMENT:
334 gint64 soffset, eoffset;
337 gst_event_parse_newsegment (event, NULL, &format, &soffset, &eoffset,
340 if (format == GST_FORMAT_BYTES) {
341 gst_file_sink_do_seek (filesink, (guint64) soffset);
343 GST_DEBUG ("Ignored NEWSEGMENT event of format %u", (guint) format);
348 if (fflush (filesink->file)) {
349 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
350 (_("Error while writing to file \"%s\"."), filesink->filename),
362 gst_file_sink_get_current_offset (GstFileSink * filesink, guint64 * p_pos)
367 ret = lseek (fileno (filesink->file), 0, SEEK_CUR);
369 ret = (off_t) ftell (filesink->file);
372 *p_pos = (guint64) ret;
374 return (ret != (off_t) - 1);
378 gst_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
380 GstFileSink *filesink;
382 guint size, back_pending = 0;
384 size = GST_BUFFER_SIZE (buffer);
386 filesink = GST_FILE_SINK (sink);
388 if (!gst_file_sink_get_current_offset (filesink, &cur_pos))
391 if (cur_pos < filesink->data_written)
392 back_pending = filesink->data_written - cur_pos;
394 if (fwrite (GST_BUFFER_DATA (buffer), size, 1, filesink->file) != 1)
397 filesink->data_written += size - back_pending;
403 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
404 (_("Error while writing to file \"%s\"."), filesink->filename),
405 ("%s", g_strerror (errno)));
407 return GST_FLOW_ERROR;
410 static GstStateChangeReturn
411 gst_file_sink_change_state (GstElement * element, GstStateChange transition)
413 GstStateChangeReturn ret;
415 switch (transition) {
416 case GST_STATE_CHANGE_NULL_TO_READY:
418 case GST_STATE_CHANGE_READY_TO_PAUSED:
419 if (!gst_file_sink_open_file (GST_FILE_SINK (element)))
422 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
428 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
430 switch (transition) {
431 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
433 case GST_STATE_CHANGE_PAUSED_TO_READY:
434 gst_file_sink_close_file (GST_FILE_SINK (element));
436 case GST_STATE_CHANGE_READY_TO_NULL:
446 return GST_STATE_CHANGE_FAILURE;
450 /*** GSTURIHANDLER INTERFACE *************************************************/
453 gst_file_sink_uri_get_type (void)
458 gst_file_sink_uri_get_protocols (void)
460 static gchar *protocols[] = { "file", NULL };
465 gst_file_sink_uri_get_uri (GstURIHandler * handler)
467 GstFileSink *sink = GST_FILE_SINK (handler);
473 gst_file_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
475 gchar *protocol, *location;
477 GstFileSink *sink = GST_FILE_SINK (handler);
479 protocol = gst_uri_get_protocol (uri);
480 if (strcmp (protocol, "file") != 0) {
485 location = gst_uri_get_location (uri);
486 ret = gst_file_sink_set_location (sink, location);
493 gst_file_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
495 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
497 iface->get_type = gst_file_sink_uri_get_type;
498 iface->get_protocols = gst_file_sink_uri_get_protocols;
499 iface->get_uri = gst_file_sink_uri_get_uri;
500 iface->set_uri = gst_file_sink_uri_set_uri;