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.
30 #include "gstfilesink.h"
33 #include <sys/types.h>
36 GST_DEBUG_CATEGORY (gst_filesink_debug);
37 #define GST_CAT_DEFAULT gst_filesink_debug
39 GstElementDetails gst_filesink_details = {
43 "Write stream to a file",
45 "Thomas <thomas@apestaart.org>",
50 /* FileSink signals and args */
62 GST_PAD_QUERY_TYPE_FUNCTION (gst_filesink_get_query_types,
67 GST_PAD_FORMATS_FUNCTION (gst_filesink_get_formats,
72 static void gst_filesink_class_init (GstFileSinkClass *klass);
73 static void gst_filesink_init (GstFileSink *filesink);
75 static void gst_filesink_set_property (GObject *object, guint prop_id,
76 const GValue *value, GParamSpec *pspec);
77 static void gst_filesink_get_property (GObject *object, guint prop_id,
78 GValue *value, GParamSpec *pspec);
80 static gboolean gst_filesink_open_file (GstFileSink *sink);
81 static void gst_filesink_close_file (GstFileSink *sink);
83 static gboolean gst_filesink_handle_event (GstPad *pad, GstEvent *event);
84 static gboolean gst_filesink_pad_query (GstPad *pad, GstQueryType type,
85 GstFormat *format, gint64 *value);
86 static void gst_filesink_chain (GstPad *pad,GstData *_data);
88 static GstElementStateReturn gst_filesink_change_state (GstElement *element);
90 static GstElementClass *parent_class = NULL;
91 static guint gst_filesink_signals[LAST_SIGNAL] = { 0 };
94 gst_filesink_get_type (void)
96 static GType filesink_type = 0;
99 static const GTypeInfo filesink_info = {
100 sizeof(GstFileSinkClass), NULL,
102 (GClassInitFunc)gst_filesink_class_init,
107 (GInstanceInitFunc)gst_filesink_init,
109 filesink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFileSink", &filesink_info, 0);
111 return filesink_type;
115 gst_filesink_class_init (GstFileSinkClass *klass)
117 GObjectClass *gobject_class;
118 GstElementClass *gstelement_class;
120 gobject_class = (GObjectClass*)klass;
121 gstelement_class = (GstElementClass*)klass;
123 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
125 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATION,
126 g_param_spec_string ("location", "File Location", "Location of the file to write",
127 NULL, G_PARAM_READWRITE));
129 gst_filesink_signals[SIGNAL_HANDOFF] =
130 g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
131 G_STRUCT_OFFSET (GstFileSinkClass, handoff), NULL, NULL,
132 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
134 gobject_class->set_property = gst_filesink_set_property;
135 gobject_class->get_property = gst_filesink_get_property;
137 gstelement_class->change_state = gst_filesink_change_state;
141 gst_filesink_init (GstFileSink *filesink)
145 pad = gst_pad_new ("sink", GST_PAD_SINK);
146 gst_element_add_pad (GST_ELEMENT (filesink), pad);
147 gst_pad_set_chain_function (pad, gst_filesink_chain);
149 GST_FLAG_SET (GST_ELEMENT(filesink), GST_ELEMENT_EVENT_AWARE);
151 gst_pad_set_query_function (pad, gst_filesink_pad_query);
152 gst_pad_set_query_type_function (pad, gst_filesink_get_query_types);
153 gst_pad_set_formats_function (pad, gst_filesink_get_formats);
155 filesink->filename = NULL;
156 filesink->file = NULL;
160 gst_filesink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
164 /* it's not null if we got it, but it might not be ours */
165 sink = GST_FILESINK (object);
169 /* the element must be stopped or paused in order to do this */
170 g_return_if_fail (GST_STATE (sink) <= GST_STATE_PAUSED);
171 if (GST_STATE (sink) == GST_STATE_PAUSED)
172 g_return_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
175 g_free (sink->filename);
176 sink->filename = g_strdup (g_value_get_string (value));
177 if (GST_STATE (sink) == GST_STATE_PAUSED)
178 gst_filesink_open_file (sink);
181 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187 gst_filesink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
191 /* it's not null if we got it, but it might not be ours */
192 g_return_if_fail (GST_IS_FILESINK (object));
194 sink = GST_FILESINK (object);
198 g_value_set_string (value, sink->filename);
201 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207 gst_filesink_open_file (GstFileSink *sink)
209 g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN), FALSE);
214 gst_element_error (GST_ELEMENT (sink),
215 "Error opening file: no file given");
219 sink->file = fopen (sink->filename, "w");
220 if (sink->file == NULL) {
221 gst_element_error (GST_ELEMENT (sink),
222 "Error opening file %s: %s",
223 sink->filename, g_strerror(errno));
227 GST_FLAG_SET (sink, GST_FILESINK_OPEN);
229 sink->data_written = 0;
235 gst_filesink_close_file (GstFileSink *sink)
237 g_return_if_fail (GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
239 if (fclose (sink->file) != 0)
241 gst_element_error (GST_ELEMENT (sink),
242 "Error closing file %s: %s",
243 sink->filename, g_strerror(errno));
246 GST_FLAG_UNSET (sink, GST_FILESINK_OPEN);
251 gst_filesink_pad_query (GstPad *pad, GstQueryType type,
252 GstFormat *format, gint64 *value)
254 GstFileSink *sink = GST_FILESINK (GST_PAD_PARENT (pad));
257 case GST_QUERY_TOTAL:
259 case GST_FORMAT_BYTES:
260 if (GST_FLAG_IS_SET (GST_ELEMENT(sink), GST_FILESINK_OPEN)) {
261 *value = sink->data_written; /* FIXME - doesn't the kernel provide
269 case GST_QUERY_POSITION:
271 case GST_FORMAT_BYTES:
272 if (GST_FLAG_IS_SET (GST_ELEMENT(sink), GST_FILESINK_OPEN)) {
273 *value = ftell (sink->file);
287 /* handle events (search) */
289 gst_filesink_handle_event (GstPad *pad, GstEvent *event)
292 GstFileSink *filesink;
294 filesink = GST_FILESINK (gst_pad_get_parent (pad));
296 g_return_val_if_fail (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN),
299 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
303 g_return_val_if_fail (GST_EVENT_SEEK_FORMAT (event) == GST_FORMAT_BYTES,
306 if (GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH)
307 if (fflush (filesink->file))
308 gst_element_error (GST_ELEMENT (filesink),
309 "Error flushing file %s: %s",
310 filesink->filename, g_strerror(errno));
312 switch (GST_EVENT_SEEK_METHOD(event))
314 case GST_SEEK_METHOD_SET:
315 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_SET);
317 case GST_SEEK_METHOD_CUR:
318 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_CUR);
320 case GST_SEEK_METHOD_END:
321 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_END);
324 g_warning ("unknown seek method!");
328 case GST_EVENT_DISCONTINUOUS:
332 if (gst_event_discont_get_value (event, GST_FORMAT_BYTES, &offset))
333 fseek (filesink->file, offset, SEEK_SET);
335 gst_event_unref (event);
338 case GST_EVENT_FLUSH:
339 if (fflush (filesink->file)) {
340 gst_element_error (GST_ELEMENT (filesink),
341 "Error flushing file %s: %s",
342 filesink->filename, g_strerror(errno));
346 gst_filesink_close_file (filesink);
347 gst_element_set_eos (GST_ELEMENT (filesink));
350 gst_pad_event_default (pad, event);
358 * gst_filesink_chain:
359 * @pad: the pad this filesink is connected to
360 * @buf: the buffer that has to be absorbed
362 * take the buffer from the pad and write to file if it's open
365 gst_filesink_chain (GstPad *pad, GstData *_data)
367 GstBuffer *buf = GST_BUFFER (_data);
368 GstFileSink *filesink;
370 g_return_if_fail (pad != NULL);
371 g_return_if_fail (GST_IS_PAD (pad));
372 g_return_if_fail (buf != NULL);
374 filesink = GST_FILESINK (gst_pad_get_parent (pad));
376 if (GST_IS_EVENT(buf))
378 gst_filesink_handle_event(pad, GST_EVENT(buf));
382 if (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN))
384 guint bytes_written = 0, back_pending = 0;
385 if (ftell(filesink->file) < filesink->data_written)
386 back_pending = filesink->data_written - ftell(filesink->file);
387 while (bytes_written < GST_BUFFER_SIZE (buf)) {
388 size_t wrote = fwrite (GST_BUFFER_DATA (buf) + bytes_written, 1,
389 GST_BUFFER_SIZE (buf) - bytes_written,
392 gst_element_error (GST_ELEMENT (filesink),
393 "Only %d of %d bytes written: %s",
394 bytes_written, GST_BUFFER_SIZE (buf),
398 bytes_written += wrote;
401 filesink->data_written += bytes_written - back_pending;
404 gst_buffer_unref (buf);
406 g_signal_emit (G_OBJECT (filesink),
407 gst_filesink_signals[SIGNAL_HANDOFF], 0,
411 static GstElementStateReturn
412 gst_filesink_change_state (GstElement *element)
414 g_return_val_if_fail (GST_IS_FILESINK (element), GST_STATE_FAILURE);
416 switch (GST_STATE_TRANSITION (element)) {
417 case GST_STATE_PAUSED_TO_READY:
418 if (GST_FLAG_IS_SET (element, GST_FILESINK_OPEN))
419 gst_filesink_close_file (GST_FILESINK (element));
422 case GST_STATE_READY_TO_PAUSED:
423 if (!GST_FLAG_IS_SET (element, GST_FILESINK_OPEN)) {
424 if (!gst_filesink_open_file (GST_FILESINK (element)))
425 return GST_STATE_FAILURE;
430 if (GST_ELEMENT_CLASS (parent_class)->change_state)
431 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
433 return GST_STATE_SUCCESS;