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 GST_DEBUG_CATEGORY (gst_filesink_debug);
34 #define GST_CAT_DEFAULT gst_filesink_debug
36 GstElementDetails gst_filesink_details = {
40 "Write stream to a file",
42 "Thomas <thomas@apestaart.org>",
47 /* FileSink signals and args */
59 GST_PAD_EVENT_MASK_FUNCTION (gst_filesink_get_event_mask,
60 { GST_EVENT_SEEK, GST_SEEK_METHOD_CUR |
63 GST_SEEK_FLAG_FLUSH },
64 { GST_EVENT_FLUSH, 0 },
65 { GST_EVENT_DISCONTINUOUS, 0 }
68 GST_PAD_QUERY_TYPE_FUNCTION (gst_filesink_get_query_types,
73 GST_PAD_FORMATS_FUNCTION (gst_filesink_get_formats,
78 static void gst_filesink_class_init (GstFileSinkClass *klass);
79 static void gst_filesink_init (GstFileSink *filesink);
81 static void gst_filesink_set_property (GObject *object, guint prop_id,
82 const GValue *value, GParamSpec *pspec);
83 static void gst_filesink_get_property (GObject *object, guint prop_id,
84 GValue *value, GParamSpec *pspec);
86 static gboolean gst_filesink_open_file (GstFileSink *sink);
87 static void gst_filesink_close_file (GstFileSink *sink);
89 static gboolean gst_filesink_handle_event (GstPad *pad, GstEvent *event);
90 static gboolean gst_filesink_pad_query (GstPad *pad, GstQueryType type,
91 GstFormat *format, gint64 *value);
92 static void gst_filesink_chain (GstPad *pad,GstBuffer *buf);
94 static GstElementStateReturn gst_filesink_change_state (GstElement *element);
96 static GstElementClass *parent_class = NULL;
97 static guint gst_filesink_signals[LAST_SIGNAL] = { 0 };
100 gst_filesink_get_type (void)
102 static GType filesink_type = 0;
104 if (!filesink_type) {
105 static const GTypeInfo filesink_info = {
106 sizeof(GstFileSinkClass), NULL,
108 (GClassInitFunc)gst_filesink_class_init,
113 (GInstanceInitFunc)gst_filesink_init,
115 filesink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFileSink", &filesink_info, 0);
117 return filesink_type;
121 gst_filesink_class_init (GstFileSinkClass *klass)
123 GObjectClass *gobject_class;
124 GstElementClass *gstelement_class;
126 gobject_class = (GObjectClass*)klass;
127 gstelement_class = (GstElementClass*)klass;
129 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
131 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATION,
132 g_param_spec_string ("location", "File Location", "Location of the file to write",
133 NULL, G_PARAM_READWRITE));
135 gst_filesink_signals[SIGNAL_HANDOFF] =
136 g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
137 G_STRUCT_OFFSET (GstFileSinkClass, handoff), NULL, NULL,
138 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
140 gobject_class->set_property = gst_filesink_set_property;
141 gobject_class->get_property = gst_filesink_get_property;
143 gstelement_class->change_state = gst_filesink_change_state;
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);
156 gst_pad_set_event_function(pad, gst_filesink_handle_event);
157 gst_pad_set_event_mask_function(pad, gst_filesink_get_event_mask);
159 gst_pad_set_query_function (pad, gst_filesink_pad_query);
160 gst_pad_set_query_type_function (pad, gst_filesink_get_query_types);
161 gst_pad_set_formats_function (pad, gst_filesink_get_formats);
163 filesink->filename = NULL;
164 filesink->file = NULL;
168 gst_filesink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
172 /* it's not null if we got it, but it might not be ours */
173 sink = GST_FILESINK (object);
177 /* the element must be stopped or paused in order to do this */
178 g_return_if_fail (GST_STATE (sink) <= GST_STATE_PAUSED);
179 if (GST_STATE (sink) == GST_STATE_PAUSED)
180 g_return_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
183 g_free (sink->filename);
184 sink->filename = g_strdup (g_value_get_string (value));
185 if (GST_STATE (sink) == GST_STATE_PAUSED)
186 gst_filesink_open_file (sink);
189 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
195 gst_filesink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
199 /* it's not null if we got it, but it might not be ours */
200 g_return_if_fail (GST_IS_FILESINK (object));
202 sink = GST_FILESINK (object);
206 g_value_set_string (value, sink->filename);
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 gst_filesink_open_file (GstFileSink *sink)
217 g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN), FALSE);
226 sink->file = fopen (sink->filename, "w");
227 if (sink->file == NULL) {
228 gst_element_error (GST_ELEMENT (sink),
229 "Error opening file %s: %s",
230 sink->filename, g_strerror(errno));
234 GST_FLAG_SET (sink, GST_FILESINK_OPEN);
236 sink->data_written = 0;
242 gst_filesink_close_file (GstFileSink *sink)
244 g_return_if_fail (GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
246 if (fclose (sink->file) != 0)
248 gst_element_error (GST_ELEMENT (sink),
249 "Error closing file %s: %s",
250 sink->filename, g_strerror(errno));
253 GST_FLAG_UNSET (sink, GST_FILESINK_OPEN);
258 gst_filesink_pad_query (GstPad *pad, GstQueryType type,
259 GstFormat *format, gint64 *value)
261 GstFileSink *sink = GST_FILESINK (GST_PAD_PARENT (pad));
264 case GST_QUERY_TOTAL:
266 case GST_FORMAT_BYTES:
267 if (GST_FLAG_IS_SET (GST_ELEMENT(sink), GST_FILESINK_OPEN)) {
268 *value = sink->data_written; /* FIXME - doesn't the kernel provide
276 case GST_QUERY_POSITION:
278 case GST_FORMAT_BYTES:
279 if (GST_FLAG_IS_SET (GST_ELEMENT(sink), GST_FILESINK_OPEN)) {
280 *value = ftell (sink->file);
294 /* handle events (search) */
296 gst_filesink_handle_event (GstPad *pad, GstEvent *event)
299 GstFileSink *filesink;
301 filesink = GST_FILESINK (gst_pad_get_parent (pad));
303 g_return_val_if_fail (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN),
306 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
310 g_return_val_if_fail (GST_EVENT_SEEK_FORMAT (event) == GST_FORMAT_BYTES,
313 if (GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH)
314 if (fflush (filesink->file))
315 gst_element_error (GST_ELEMENT (filesink),
316 "Error flushing file %s: %s",
317 filesink->filename, g_strerror(errno));
319 switch (GST_EVENT_SEEK_METHOD(event))
321 case GST_SEEK_METHOD_SET:
322 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_SET);
324 case GST_SEEK_METHOD_CUR:
325 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_CUR);
327 case GST_SEEK_METHOD_END:
328 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_END);
331 g_warning("unkown seek method!\n");
335 case GST_EVENT_DISCONTINUOUS:
339 if (gst_event_discont_get_value (event, GST_FORMAT_BYTES, &offset))
340 fseek (filesink->file, offset, SEEK_SET);
342 gst_event_unref (event);
345 case GST_EVENT_FLUSH:
346 if (fflush (filesink->file)) {
347 gst_element_error (GST_ELEMENT (filesink),
348 "Error flushing file %s: %s",
349 filesink->filename, g_strerror(errno));
353 gst_filesink_close_file (filesink);
354 gst_element_set_eos (GST_ELEMENT (filesink));
357 gst_pad_event_default (pad, event);
365 * gst_filesink_chain:
366 * @pad: the pad this filesink is connected to
367 * @buf: the buffer that has to be absorbed
369 * take the buffer from the pad and write to file if it's open
372 gst_filesink_chain (GstPad *pad, GstBuffer *buf)
374 GstFileSink *filesink;
376 g_return_if_fail (pad != NULL);
377 g_return_if_fail (GST_IS_PAD (pad));
378 g_return_if_fail (buf != NULL);
380 filesink = GST_FILESINK (gst_pad_get_parent (pad));
382 if (GST_IS_EVENT(buf))
384 gst_filesink_handle_event(pad, GST_EVENT(buf));
388 if (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN))
390 guint bytes_written = 0, back_pending = 0;
391 if (ftell(filesink->file) < filesink->data_written)
392 back_pending = filesink->data_written - ftell(filesink->file);
394 size_t wrote = fwrite (GST_BUFFER_DATA (buf) + bytes_written, 1,
395 GST_BUFFER_SIZE (buf) - bytes_written,
398 gst_element_error (GST_ELEMENT (filesink),
399 "Only %d of %d bytes written: %s",
400 bytes_written, GST_BUFFER_SIZE (buf),
404 bytes_written += wrote;
405 } while (bytes_written < GST_BUFFER_SIZE (buf));
407 filesink->data_written += bytes_written - back_pending;
410 gst_buffer_unref (buf);
412 g_signal_emit (G_OBJECT (filesink),
413 gst_filesink_signals[SIGNAL_HANDOFF], 0,
417 static GstElementStateReturn
418 gst_filesink_change_state (GstElement *element)
420 g_return_val_if_fail (GST_IS_FILESINK (element), GST_STATE_FAILURE);
422 switch (GST_STATE_TRANSITION (element)) {
423 case GST_STATE_PAUSED_TO_READY:
424 if (GST_FLAG_IS_SET (element, GST_FILESINK_OPEN))
425 gst_filesink_close_file (GST_FILESINK (element));
428 case GST_STATE_READY_TO_PAUSED:
429 if (!GST_FLAG_IS_SET (element, GST_FILESINK_OPEN)) {
430 if (!gst_filesink_open_file (GST_FILESINK (element)))
431 return GST_STATE_FAILURE;
436 if (GST_ELEMENT_CLASS (parent_class)->change_state)
437 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
439 return GST_STATE_SUCCESS;