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.
26 #include "gstfilesink.h"
29 GstElementDetails gst_filesink_details = {
33 "Write stream to a file",
35 "Thomas <thomas@apestaart.org>",
40 /* FileSink signals and args */
52 GST_PAD_EVENT_MASK_FUNCTION (gst_filesink_get_event_mask,
53 { GST_EVENT_SEEK, GST_SEEK_METHOD_CUR |
56 GST_SEEK_FLAG_FLUSH },
57 { GST_EVENT_FLUSH, 0 },
58 { GST_EVENT_DISCONTINUOUS, 0 }
61 GST_PAD_QUERY_TYPE_FUNCTION (gst_filesink_get_query_types,
66 GST_PAD_FORMATS_FUNCTION (gst_filesink_get_formats,
71 static void gst_filesink_class_init (GstFileSinkClass *klass);
72 static void gst_filesink_init (GstFileSink *filesink);
74 static void gst_filesink_set_property (GObject *object, guint prop_id,
75 const GValue *value, GParamSpec *pspec);
76 static void gst_filesink_get_property (GObject *object, guint prop_id,
77 GValue *value, GParamSpec *pspec);
79 static gboolean gst_filesink_open_file (GstFileSink *sink);
80 static void gst_filesink_close_file (GstFileSink *sink);
82 static gboolean gst_filesink_handle_event (GstPad *pad, GstEvent *event);
83 static gboolean gst_filesink_pad_query (GstPad *pad, GstQueryType type,
84 GstFormat *format, gint64 *value);
85 static void gst_filesink_chain (GstPad *pad,GstBuffer *buf);
87 static GstElementStateReturn gst_filesink_change_state (GstElement *element);
89 static GstElementClass *parent_class = NULL;
90 static guint gst_filesink_signals[LAST_SIGNAL] = { 0 };
93 gst_filesink_get_type (void)
95 static GType filesink_type = 0;
98 static const GTypeInfo filesink_info = {
99 sizeof(GstFileSinkClass), NULL,
101 (GClassInitFunc)gst_filesink_class_init,
106 (GInstanceInitFunc)gst_filesink_init,
108 filesink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFileSink", &filesink_info, 0);
110 return filesink_type;
114 gst_filesink_class_init (GstFileSinkClass *klass)
116 GObjectClass *gobject_class;
117 GstElementClass *gstelement_class;
119 gobject_class = (GObjectClass*)klass;
120 gstelement_class = (GstElementClass*)klass;
122 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
124 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATION,
125 g_param_spec_string ("location", "File Location", "Location of the file to write",
126 NULL, G_PARAM_READWRITE));
128 gst_filesink_signals[SIGNAL_HANDOFF] =
129 g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
130 G_STRUCT_OFFSET (GstFileSinkClass, handoff), NULL, NULL,
131 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
133 gobject_class->set_property = gst_filesink_set_property;
134 gobject_class->get_property = gst_filesink_get_property;
136 gstelement_class->change_state = gst_filesink_change_state;
140 gst_filesink_init (GstFileSink *filesink)
144 pad = gst_pad_new ("sink", GST_PAD_SINK);
145 gst_element_add_pad (GST_ELEMENT (filesink), pad);
146 gst_pad_set_chain_function (pad, gst_filesink_chain);
148 GST_FLAG_SET (GST_ELEMENT(filesink), GST_ELEMENT_EVENT_AWARE);
149 gst_pad_set_event_function(pad, gst_filesink_handle_event);
150 gst_pad_set_event_mask_function(pad, gst_filesink_get_event_mask);
152 gst_pad_set_query_function (pad, gst_filesink_pad_query);
153 gst_pad_set_query_type_function (pad, gst_filesink_get_query_types);
154 gst_pad_set_formats_function (pad, gst_filesink_get_formats);
156 filesink->filename = NULL;
157 filesink->file = NULL;
161 gst_filesink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
165 /* it's not null if we got it, but it might not be ours */
166 sink = GST_FILESINK (object);
170 /* the element must be stopped or paused in order to do this */
171 g_return_if_fail (GST_STATE (sink) < GST_STATE_PLAYING);
173 g_free (sink->filename);
174 sink->filename = g_strdup (g_value_get_string (value));
175 if ((GST_STATE (sink) == GST_STATE_PAUSED) &&
176 (sink->filename != NULL)) {
177 gst_filesink_close_file (sink);
178 gst_filesink_open_file (sink);
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188 gst_filesink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
192 /* it's not null if we got it, but it might not be ours */
193 g_return_if_fail (GST_IS_FILESINK (object));
195 sink = GST_FILESINK (object);
199 g_value_set_string (value, sink->filename);
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
208 gst_filesink_open_file (GstFileSink *sink)
210 g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN), FALSE);
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("unkown seek method!\n");
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, GstBuffer *buf)
367 GstFileSink *filesink;
368 gint bytes_written = 0;
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 bytes_written = fwrite (GST_BUFFER_DATA (buf), 1,
385 GST_BUFFER_SIZE (buf), filesink->file);
386 if (bytes_written < GST_BUFFER_SIZE (buf))
388 g_warning ("filesink: %d bytes should be written, only %d bytes written\n",
389 GST_BUFFER_SIZE (buf), bytes_written);
391 filesink->data_written += bytes_written;
394 gst_buffer_unref (buf);
396 g_signal_emit (G_OBJECT (filesink),
397 gst_filesink_signals[SIGNAL_HANDOFF], 0,
401 static GstElementStateReturn
402 gst_filesink_change_state (GstElement *element)
404 g_return_val_if_fail (GST_IS_FILESINK (element), GST_STATE_FAILURE);
406 switch (GST_STATE_TRANSITION (element)) {
407 case GST_STATE_PAUSED_TO_READY:
408 if (GST_FLAG_IS_SET (element, GST_FILESINK_OPEN))
409 gst_filesink_close_file (GST_FILESINK (element));
412 case GST_STATE_READY_TO_PAUSED:
413 if (!GST_FLAG_IS_SET (element, GST_FILESINK_OPEN)) {
414 if (!gst_filesink_open_file (GST_FILESINK (element)))
415 return GST_STATE_FAILURE;
420 if (GST_ELEMENT_CLASS (parent_class)->change_state)
421 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
423 return GST_STATE_SUCCESS;