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 (
45 "Write stream to a file",
46 "Thomas <thomas@apestaart.org>"
50 /* 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, gpointer iface_data);
100 static GstElementStateReturn gst_filesink_change_state (GstElement *element);
102 static guint gst_filesink_signals[LAST_SIGNAL] = { 0 };
105 _do_init (GType filesink_type)
107 static const GInterfaceInfo urihandler_info = {
108 gst_filesink_uri_handler_init,
112 g_type_add_interface_static (filesink_type, GST_TYPE_URI_HANDLER, &urihandler_info);
113 GST_DEBUG_CATEGORY_INIT (gst_filesink_debug, "filesink", 0, "filesink element");
116 GST_BOILERPLATE_FULL (GstFileSink, gst_filesink, GstElement, GST_TYPE_ELEMENT, _do_init);
120 gst_filesink_base_init (gpointer g_class)
122 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
124 gstelement_class->change_state = gst_filesink_change_state;
125 gst_element_class_set_details (gstelement_class, &gst_filesink_details);
128 gst_filesink_class_init (GstFileSinkClass *klass)
130 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
133 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATION,
134 g_param_spec_string ("location", "File Location", "Location of the file to write",
135 NULL, G_PARAM_READWRITE));
137 gst_filesink_signals[SIGNAL_HANDOFF] =
138 g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
139 G_STRUCT_OFFSET (GstFileSinkClass, handoff), NULL, NULL,
140 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
142 gobject_class->set_property = gst_filesink_set_property;
143 gobject_class->get_property = gst_filesink_get_property;
144 gobject_class->dispose = gst_filesink_dispose;
147 gst_filesink_init (GstFileSink *filesink)
151 pad = gst_pad_new ("sink", GST_PAD_SINK);
152 gst_element_add_pad (GST_ELEMENT (filesink), pad);
153 gst_pad_set_chain_function (pad, gst_filesink_chain);
155 GST_FLAG_SET (GST_ELEMENT(filesink), GST_ELEMENT_EVENT_AWARE);
157 gst_pad_set_query_function (pad, gst_filesink_pad_query);
158 gst_pad_set_query_type_function (pad, gst_filesink_get_query_types);
159 gst_pad_set_formats_function (pad, gst_filesink_get_formats);
161 filesink->filename = NULL;
162 filesink->file = NULL;
165 gst_filesink_dispose (GObject *object)
167 GstFileSink *sink = GST_FILESINK (object);
169 G_OBJECT_CLASS (parent_class)->dispose (object);
173 g_free (sink->filename);
174 sink->filename = NULL;
177 gst_filesink_set_location (GstFileSink *sink, const gchar *location)
179 /* the element must be stopped or paused in order to do this */
180 if (GST_STATE (sink) > GST_STATE_PAUSED)
182 if (GST_STATE (sink) == GST_STATE_PAUSED &&
183 GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN))
186 g_free (sink->filename);
188 if (location != NULL) {
189 sink->filename = g_strdup (location);
190 sink->uri = gst_uri_construct ("file", location);
192 sink->filename = NULL;
196 if (GST_STATE (sink) == GST_STATE_PAUSED)
197 gst_filesink_open_file (sink);
202 gst_filesink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
206 /* it's not null if we got it, but it might not be ours */
207 sink = GST_FILESINK (object);
211 gst_filesink_set_location (sink, g_value_get_string (value));
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 gst_filesink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
224 /* it's not null if we got it, but it might not be ours */
225 g_return_if_fail (GST_IS_FILESINK (object));
227 sink = GST_FILESINK (object);
231 g_value_set_string (value, sink->filename);
234 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240 gst_filesink_open_file (GstFileSink *sink)
242 g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN), FALSE);
247 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
248 (_("No filename specified.")), (NULL));
252 sink->file = fopen (sink->filename, "w");
253 if (sink->file == NULL) {
254 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
255 (_("Could not open file \"%s\" for writing."), sink->filename),
260 GST_FLAG_SET (sink, GST_FILESINK_OPEN);
262 sink->data_written = 0;
268 gst_filesink_close_file (GstFileSink *sink)
270 g_return_if_fail (GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
272 if (fclose (sink->file) != 0)
274 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
275 (_("Error closing file \"%s\"."), sink->filename),
279 GST_FLAG_UNSET (sink, GST_FILESINK_OPEN);
284 gst_filesink_pad_query (GstPad *pad, GstQueryType type,
285 GstFormat *format, gint64 *value)
287 GstFileSink *sink = GST_FILESINK (GST_PAD_PARENT (pad));
290 case GST_QUERY_TOTAL:
292 case GST_FORMAT_BYTES:
293 if (GST_FLAG_IS_SET (GST_ELEMENT(sink), GST_FILESINK_OPEN)) {
294 *value = sink->data_written; /* FIXME - doesn't the kernel provide
302 case GST_QUERY_POSITION:
304 case GST_FORMAT_BYTES:
305 if (GST_FLAG_IS_SET (GST_ELEMENT(sink), GST_FILESINK_OPEN)) {
306 *value = ftell (sink->file);
320 /* handle events (search) */
322 gst_filesink_handle_event (GstPad *pad, GstEvent *event)
325 GstFileSink *filesink;
327 filesink = GST_FILESINK (gst_pad_get_parent (pad));
329 g_return_val_if_fail (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN),
332 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
336 g_return_val_if_fail (GST_EVENT_SEEK_FORMAT (event) == GST_FORMAT_BYTES,
339 if (GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH)
340 if (fflush (filesink->file))
341 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
342 (_("Error while writing to file \"%s\"."), filesink->filename),
345 switch (GST_EVENT_SEEK_METHOD(event))
347 case GST_SEEK_METHOD_SET:
348 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_SET);
350 case GST_SEEK_METHOD_CUR:
351 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_CUR);
353 case GST_SEEK_METHOD_END:
354 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_END);
357 g_warning ("unknown seek method!");
361 case GST_EVENT_DISCONTINUOUS:
365 if (gst_event_discont_get_value (event, GST_FORMAT_BYTES, &offset))
366 fseek (filesink->file, offset, SEEK_SET);
368 gst_event_unref (event);
371 case GST_EVENT_FLUSH:
372 if (fflush (filesink->file)) {
373 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
374 (_("Error while writing to file \"%s\"."), filesink->filename),
379 gst_filesink_close_file (filesink);
380 gst_element_set_eos (GST_ELEMENT (filesink));
383 gst_pad_event_default (pad, event);
391 * gst_filesink_chain:
392 * @pad: the pad this filesink is connected to
393 * @buf: the buffer that has to be absorbed
395 * take the buffer from the pad and write to file if it's open
398 gst_filesink_chain (GstPad *pad, GstData *_data)
400 GstBuffer *buf = GST_BUFFER (_data);
401 GstFileSink *filesink;
403 g_return_if_fail (pad != NULL);
404 g_return_if_fail (GST_IS_PAD (pad));
405 g_return_if_fail (buf != NULL);
407 filesink = GST_FILESINK (gst_pad_get_parent (pad));
409 if (GST_IS_EVENT(buf))
411 gst_filesink_handle_event(pad, GST_EVENT(buf));
415 if (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN))
417 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,
425 GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
426 (_("Error while writing to file \"%s\"."), filesink->filename),
427 ("Only %d of %d bytes written: %s",
428 bytes_written, GST_BUFFER_SIZE (buf),
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,
445 static GstElementStateReturn
446 gst_filesink_change_state (GstElement *element)
448 g_return_val_if_fail (GST_IS_FILESINK (element), GST_STATE_FAILURE);
450 switch (GST_STATE_TRANSITION (element)) {
451 case GST_STATE_PAUSED_TO_READY:
452 if (GST_FLAG_IS_SET (element, GST_FILESINK_OPEN))
453 gst_filesink_close_file (GST_FILESINK (element));
456 case GST_STATE_READY_TO_PAUSED:
457 if (!GST_FLAG_IS_SET (element, GST_FILESINK_OPEN)) {
458 if (!gst_filesink_open_file (GST_FILESINK (element)))
459 return GST_STATE_FAILURE;
464 if (GST_ELEMENT_CLASS (parent_class)->change_state)
465 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
467 return GST_STATE_SUCCESS;
470 /*** GSTURIHANDLER INTERFACE *************************************************/
473 gst_filesink_uri_get_type (void)
478 gst_filesink_uri_get_protocols(void)
480 static gchar *protocols[] = {"file", NULL};
484 gst_filesink_uri_get_uri (GstURIHandler *handler)
486 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;