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.
28 #include "../gst-i18n-lib.h"
32 #include "gstfilesink.h"
35 #include <sys/types.h>
39 GST_DEBUG_CATEGORY_STATIC (gst_filesink_debug);
40 #define GST_CAT_DEFAULT gst_filesink_debug
42 GstElementDetails gst_filesink_details = GST_ELEMENT_DETAILS ("File Sink",
44 "Write stream to a file",
45 "Thomas <thomas@apestaart.org>");
48 /* FileSink signals and args */
62 static const GstFormat *
63 gst_filesink_get_formats (GstPad * pad)
65 static const GstFormat formats[] = {
72 static const GstQueryType *
73 gst_filesink_get_query_types (GstPad * pad)
75 static const GstQueryType types[] = {
83 static void gst_filesink_dispose (GObject * object);
85 static void gst_filesink_set_property (GObject * object, guint prop_id,
86 const GValue * value, GParamSpec * pspec);
87 static void gst_filesink_get_property (GObject * object, guint prop_id,
88 GValue * value, GParamSpec * pspec);
90 static gboolean gst_filesink_open_file (GstFileSink * sink);
91 static void gst_filesink_close_file (GstFileSink * sink);
93 static gboolean gst_filesink_handle_event (GstPad * pad, GstEvent * event);
94 static gboolean gst_filesink_pad_query (GstPad * pad, GstQueryType type,
95 GstFormat * format, gint64 * value);
96 static void gst_filesink_chain (GstPad * pad, GstData * _data);
98 static void gst_filesink_uri_handler_init (gpointer g_iface,
101 static GstElementStateReturn gst_filesink_change_state (GstElement * element);
103 static guint gst_filesink_signals[LAST_SIGNAL] = { 0 };
106 _do_init (GType filesink_type)
108 static const GInterfaceInfo urihandler_info = {
109 gst_filesink_uri_handler_init,
113 g_type_add_interface_static (filesink_type, GST_TYPE_URI_HANDLER,
115 GST_DEBUG_CATEGORY_INIT (gst_filesink_debug, "filesink", 0,
119 GST_BOILERPLATE_FULL (GstFileSink, gst_filesink, GstElement, GST_TYPE_ELEMENT,
124 gst_filesink_base_init (gpointer g_class)
126 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
128 gstelement_class->change_state = gst_filesink_change_state;
129 gst_element_class_set_details (gstelement_class, &gst_filesink_details);
132 gst_filesink_class_init (GstFileSinkClass * klass)
134 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
137 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATION,
138 g_param_spec_string ("location", "File Location",
139 "Location of the file to write", NULL, G_PARAM_READWRITE));
141 gst_filesink_signals[SIGNAL_HANDOFF] =
142 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
143 G_STRUCT_OFFSET (GstFileSinkClass, handoff), NULL, NULL,
144 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
146 gobject_class->set_property = gst_filesink_set_property;
147 gobject_class->get_property = gst_filesink_get_property;
148 gobject_class->dispose = gst_filesink_dispose;
151 gst_filesink_init (GstFileSink * filesink)
155 pad = gst_pad_new ("sink", GST_PAD_SINK);
156 gst_element_add_pad (GST_ELEMENT (filesink), pad);
157 gst_pad_set_chain_function (pad, gst_filesink_chain);
159 GST_FLAG_SET (GST_ELEMENT (filesink), GST_ELEMENT_EVENT_AWARE);
161 gst_pad_set_query_function (pad, gst_filesink_pad_query);
162 gst_pad_set_query_type_function (pad, gst_filesink_get_query_types);
163 gst_pad_set_formats_function (pad, gst_filesink_get_formats);
165 filesink->filename = NULL;
166 filesink->file = NULL;
169 gst_filesink_dispose (GObject * object)
171 GstFileSink *sink = GST_FILESINK (object);
173 G_OBJECT_CLASS (parent_class)->dispose (object);
177 g_free (sink->filename);
178 sink->filename = NULL;
182 gst_filesink_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)
187 if (GST_STATE (sink) == GST_STATE_PAUSED &&
188 GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN))
191 g_free (sink->filename);
193 if (location != NULL) {
194 sink->filename = g_strdup (location);
195 sink->uri = gst_uri_construct ("file", location);
197 sink->filename = NULL;
201 if (GST_STATE (sink) == GST_STATE_PAUSED)
202 gst_filesink_open_file (sink);
207 gst_filesink_set_property (GObject * object, guint prop_id,
208 const GValue * value, GParamSpec * pspec)
212 /* it's not null if we got it, but it might not be ours */
213 sink = GST_FILESINK (object);
217 gst_filesink_set_location (sink, g_value_get_string (value));
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226 gst_filesink_get_property (GObject * object, guint prop_id, GValue * value,
231 /* it's not null if we got it, but it might not be ours */
232 g_return_if_fail (GST_IS_FILESINK (object));
234 sink = GST_FILESINK (object);
238 g_value_set_string (value, sink->filename);
241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247 gst_filesink_open_file (GstFileSink * sink)
249 g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN), FALSE);
252 if (sink->filename == NULL || sink->filename[0] == '\0') {
253 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
254 (_("No file name specified for writing.")), (NULL));
258 sink->file = fopen (sink->filename, "w");
259 if (sink->file == NULL) {
260 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
261 (_("Could not open file \"%s\" for writing."), sink->filename),
266 GST_FLAG_SET (sink, GST_FILESINK_OPEN);
268 sink->data_written = 0;
274 gst_filesink_close_file (GstFileSink * sink)
276 g_return_if_fail (GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
278 if (fclose (sink->file) != 0) {
279 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
280 (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
282 GST_FLAG_UNSET (sink, GST_FILESINK_OPEN);
287 gst_filesink_pad_query (GstPad * pad, GstQueryType type,
288 GstFormat * format, gint64 * value)
290 GstFileSink *sink = GST_FILESINK (GST_PAD_PARENT (pad));
293 case GST_QUERY_TOTAL:
295 case GST_FORMAT_BYTES:
296 if (GST_FLAG_IS_SET (GST_ELEMENT (sink), GST_FILESINK_OPEN)) {
297 *value = sink->data_written; /* FIXME - doesn't the kernel provide
305 case GST_QUERY_POSITION:
307 case GST_FORMAT_BYTES:
308 if (GST_FLAG_IS_SET (GST_ELEMENT (sink), GST_FILESINK_OPEN)) {
309 *value = ftell (sink->file);
323 /* handle events (search) */
325 gst_filesink_handle_event (GstPad * pad, GstEvent * event)
328 GstFileSink *filesink;
330 filesink = GST_FILESINK (gst_pad_get_parent (pad));
332 g_return_val_if_fail (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN), FALSE);
334 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
338 g_return_val_if_fail (GST_EVENT_SEEK_FORMAT (event) == GST_FORMAT_BYTES,
341 if (GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH)
342 if (fflush (filesink->file))
343 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
344 (_("Error while writing to file \"%s\"."), filesink->filename),
347 switch (GST_EVENT_SEEK_METHOD (event)) {
348 case GST_SEEK_METHOD_SET:
349 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_SET);
351 case GST_SEEK_METHOD_CUR:
352 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_CUR);
354 case GST_SEEK_METHOD_END:
355 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_END);
358 g_warning ("unknown seek method!");
362 case GST_EVENT_DISCONTINUOUS:
366 if (gst_event_discont_get_value (event, GST_FORMAT_BYTES, &offset))
367 fseek (filesink->file, offset, SEEK_SET);
369 gst_event_unref (event);
372 case GST_EVENT_FLUSH:
373 if (fflush (filesink->file)) {
374 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
375 (_("Error while writing to file \"%s\"."), filesink->filename),
380 gst_filesink_close_file (filesink);
381 gst_element_set_eos (GST_ELEMENT (filesink));
384 gst_pad_event_default (pad, event);
392 * gst_filesink_chain:
393 * @pad: the pad this filesink is connected to
394 * @buf: the buffer that has to be absorbed
396 * take the buffer from the pad and write to file if it's open
399 gst_filesink_chain (GstPad * pad, GstData * _data)
401 GstBuffer *buf = GST_BUFFER (_data);
402 GstFileSink *filesink;
404 g_return_if_fail (pad != NULL);
405 g_return_if_fail (GST_IS_PAD (pad));
406 g_return_if_fail (buf != NULL);
408 filesink = GST_FILESINK (gst_pad_get_parent (pad));
410 if (GST_IS_EVENT (buf)) {
411 gst_filesink_handle_event (pad, GST_EVENT (buf));
415 if (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN)) {
416 guint bytes_written = 0, back_pending = 0;
418 if (ftell (filesink->file) < filesink->data_written)
419 back_pending = filesink->data_written - ftell (filesink->file);
420 while (bytes_written < GST_BUFFER_SIZE (buf)) {
421 size_t wrote = fwrite (GST_BUFFER_DATA (buf) + bytes_written, 1,
422 GST_BUFFER_SIZE (buf) - bytes_written,
426 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
427 (_("Error while writing to file \"%s\"."), filesink->filename),
428 ("Only %d of %d bytes written: %s",
429 bytes_written, GST_BUFFER_SIZE (buf), strerror (errno)));
432 bytes_written += wrote;
435 filesink->data_written += bytes_written - back_pending;
438 gst_buffer_unref (buf);
440 g_signal_emit (G_OBJECT (filesink),
441 gst_filesink_signals[SIGNAL_HANDOFF], 0, filesink);
444 static GstElementStateReturn
445 gst_filesink_change_state (GstElement * element)
447 g_return_val_if_fail (GST_IS_FILESINK (element), GST_STATE_FAILURE);
449 switch (GST_STATE_TRANSITION (element)) {
450 case GST_STATE_PAUSED_TO_READY:
451 if (GST_FLAG_IS_SET (element, GST_FILESINK_OPEN))
452 gst_filesink_close_file (GST_FILESINK (element));
455 case GST_STATE_READY_TO_PAUSED:
456 if (!GST_FLAG_IS_SET (element, GST_FILESINK_OPEN)) {
457 if (!gst_filesink_open_file (GST_FILESINK (element)))
458 return GST_STATE_FAILURE;
463 if (GST_ELEMENT_CLASS (parent_class)->change_state)
464 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
466 return GST_STATE_SUCCESS;
469 /*** GSTURIHANDLER INTERFACE *************************************************/
472 gst_filesink_uri_get_type (void)
477 gst_filesink_uri_get_protocols (void)
479 static gchar *protocols[] = { "file", NULL };
483 gst_filesink_uri_get_uri (GstURIHandler * handler)
485 GstFileSink *sink = GST_FILESINK (handler);
491 gst_filesink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
493 gchar *protocol, *location;
495 GstFileSink *sink = GST_FILESINK (handler);
497 protocol = gst_uri_get_protocol (uri);
498 if (strcmp (protocol, "file") != 0) {
503 location = gst_uri_get_location (uri);
504 ret = gst_filesink_set_location (sink, location);
511 gst_filesink_uri_handler_init (gpointer g_iface, gpointer iface_data)
513 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
515 iface->get_type = gst_filesink_uri_get_type;
516 iface->get_protocols = gst_filesink_uri_get_protocols;
517 iface->get_uri = gst_filesink_uri_get_uri;
518 iface->set_uri = gst_filesink_uri_set_uri;