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_STATIC (gst_filesink_debug);
37 #define GST_CAT_DEFAULT gst_filesink_debug
39 GstElementDetails gst_filesink_details = GST_ELEMENT_DETAILS (
42 "Write stream to a file",
43 "Thomas <thomas@apestaart.org>"
47 /* FileSink signals and args */
59 GST_PAD_QUERY_TYPE_FUNCTION (gst_filesink_get_query_types,
64 GST_PAD_FORMATS_FUNCTION (gst_filesink_get_formats,
69 static void gst_filesink_base_init (gpointer g_class);
70 static void gst_filesink_class_init (GstFileSinkClass *klass);
71 static void gst_filesink_init (GstFileSink *filesink);
72 static void gst_filesink_dispose (GObject *object);
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,GstData *_data);
87 static void gst_filesink_uri_handler_init (gpointer g_iface, gpointer iface_data);
89 static GstElementStateReturn gst_filesink_change_state (GstElement *element);
91 static GstElementClass *parent_class = NULL;
92 static guint gst_filesink_signals[LAST_SIGNAL] = { 0 };
95 gst_filesink_get_type (void)
97 static GType filesink_type = 0;
100 static const GTypeInfo filesink_info = {
101 sizeof(GstFileSinkClass),
102 gst_filesink_base_init,
104 (GClassInitFunc)gst_filesink_class_init,
109 (GInstanceInitFunc)gst_filesink_init,
111 static const GInterfaceInfo urihandler_info = {
112 gst_filesink_uri_handler_init,
116 filesink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFileSink", &filesink_info, 0);
118 g_type_add_interface_static (filesink_type, GST_TYPE_URI_HANDLER, &urihandler_info);
120 GST_DEBUG_CATEGORY_INIT (gst_filesink_debug, "filesink", 0, "filesink element");
122 return filesink_type;
126 gst_filesink_base_init (gpointer g_class)
128 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
130 gstelement_class->change_state = gst_filesink_change_state;
131 gst_element_class_set_details (gstelement_class, &gst_filesink_details);
134 gst_filesink_class_init (GstFileSinkClass *klass)
136 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
138 parent_class = g_type_class_peek_parent (klass);
140 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATION,
141 g_param_spec_string ("location", "File Location", "Location of the file to write",
142 NULL, G_PARAM_READWRITE));
144 gst_filesink_signals[SIGNAL_HANDOFF] =
145 g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
146 G_STRUCT_OFFSET (GstFileSinkClass, handoff), NULL, NULL,
147 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
149 gobject_class->set_property = gst_filesink_set_property;
150 gobject_class->get_property = gst_filesink_get_property;
151 gobject_class->dispose = gst_filesink_dispose;
154 gst_filesink_init (GstFileSink *filesink)
158 pad = gst_pad_new ("sink", GST_PAD_SINK);
159 gst_element_add_pad (GST_ELEMENT (filesink), pad);
160 gst_pad_set_chain_function (pad, gst_filesink_chain);
162 GST_FLAG_SET (GST_ELEMENT(filesink), GST_ELEMENT_EVENT_AWARE);
164 gst_pad_set_query_function (pad, gst_filesink_pad_query);
165 gst_pad_set_query_type_function (pad, gst_filesink_get_query_types);
166 gst_pad_set_formats_function (pad, gst_filesink_get_formats);
168 filesink->filename = NULL;
169 filesink->file = NULL;
172 gst_filesink_dispose (GObject *object)
174 GstFileSink *sink = GST_FILESINK (object);
176 G_OBJECT_CLASS (parent_class)->dispose (object);
180 g_free (sink->filename);
181 sink->filename = NULL;
184 gst_filesink_set_location (GstFileSink *sink, const gchar *location)
186 /* the element must be stopped or paused in order to do this */
187 if (GST_STATE (sink) > GST_STATE_PAUSED)
189 if (GST_STATE (sink) == GST_STATE_PAUSED &&
190 GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN))
193 g_free (sink->filename);
195 if (location != NULL) {
196 sink->filename = g_strdup (location);
197 sink->uri = gst_uri_construct ("file", location);
199 sink->filename = NULL;
203 if (GST_STATE (sink) == GST_STATE_PAUSED)
204 gst_filesink_open_file (sink);
209 gst_filesink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
213 /* it's not null if we got it, but it might not be ours */
214 sink = GST_FILESINK (object);
218 gst_filesink_set_location (sink, g_value_get_string (value));
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227 gst_filesink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
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);
254 gst_element_error (GST_ELEMENT (sink),
255 "Error opening file: no file given");
259 sink->file = fopen (sink->filename, "w");
260 if (sink->file == NULL) {
261 gst_element_error (GST_ELEMENT (sink),
262 "Error opening file %s: %s",
263 sink->filename, g_strerror(errno));
267 GST_FLAG_SET (sink, GST_FILESINK_OPEN);
269 sink->data_written = 0;
275 gst_filesink_close_file (GstFileSink *sink)
277 g_return_if_fail (GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
279 if (fclose (sink->file) != 0)
281 gst_element_error (GST_ELEMENT (sink),
282 "Error closing file %s: %s",
283 sink->filename, g_strerror(errno));
286 GST_FLAG_UNSET (sink, GST_FILESINK_OPEN);
291 gst_filesink_pad_query (GstPad *pad, GstQueryType type,
292 GstFormat *format, gint64 *value)
294 GstFileSink *sink = GST_FILESINK (GST_PAD_PARENT (pad));
297 case GST_QUERY_TOTAL:
299 case GST_FORMAT_BYTES:
300 if (GST_FLAG_IS_SET (GST_ELEMENT(sink), GST_FILESINK_OPEN)) {
301 *value = sink->data_written; /* FIXME - doesn't the kernel provide
309 case GST_QUERY_POSITION:
311 case GST_FORMAT_BYTES:
312 if (GST_FLAG_IS_SET (GST_ELEMENT(sink), GST_FILESINK_OPEN)) {
313 *value = ftell (sink->file);
327 /* handle events (search) */
329 gst_filesink_handle_event (GstPad *pad, GstEvent *event)
332 GstFileSink *filesink;
334 filesink = GST_FILESINK (gst_pad_get_parent (pad));
336 g_return_val_if_fail (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN),
339 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
343 g_return_val_if_fail (GST_EVENT_SEEK_FORMAT (event) == GST_FORMAT_BYTES,
346 if (GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH)
347 if (fflush (filesink->file))
348 gst_element_error (GST_ELEMENT (filesink),
349 "Error flushing file %s: %s",
350 filesink->filename, g_strerror(errno));
352 switch (GST_EVENT_SEEK_METHOD(event))
354 case GST_SEEK_METHOD_SET:
355 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_SET);
357 case GST_SEEK_METHOD_CUR:
358 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_CUR);
360 case GST_SEEK_METHOD_END:
361 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_END);
364 g_warning ("unknown seek method!");
368 case GST_EVENT_DISCONTINUOUS:
372 if (gst_event_discont_get_value (event, GST_FORMAT_BYTES, &offset))
373 fseek (filesink->file, offset, SEEK_SET);
375 gst_event_unref (event);
378 case GST_EVENT_FLUSH:
379 if (fflush (filesink->file)) {
380 gst_element_error (GST_ELEMENT (filesink),
381 "Error flushing file %s: %s",
382 filesink->filename, g_strerror(errno));
386 gst_filesink_close_file (filesink);
387 gst_element_set_eos (GST_ELEMENT (filesink));
390 gst_pad_event_default (pad, event);
398 * gst_filesink_chain:
399 * @pad: the pad this filesink is connected to
400 * @buf: the buffer that has to be absorbed
402 * take the buffer from the pad and write to file if it's open
405 gst_filesink_chain (GstPad *pad, GstData *_data)
407 GstBuffer *buf = GST_BUFFER (_data);
408 GstFileSink *filesink;
410 g_return_if_fail (pad != NULL);
411 g_return_if_fail (GST_IS_PAD (pad));
412 g_return_if_fail (buf != NULL);
414 filesink = GST_FILESINK (gst_pad_get_parent (pad));
416 if (GST_IS_EVENT(buf))
418 gst_filesink_handle_event(pad, GST_EVENT(buf));
422 if (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN))
424 guint bytes_written = 0, back_pending = 0;
425 if (ftell(filesink->file) < filesink->data_written)
426 back_pending = filesink->data_written - ftell(filesink->file);
427 while (bytes_written < GST_BUFFER_SIZE (buf)) {
428 size_t wrote = fwrite (GST_BUFFER_DATA (buf) + bytes_written, 1,
429 GST_BUFFER_SIZE (buf) - bytes_written,
432 gst_element_error (GST_ELEMENT (filesink),
433 "Only %d of %d bytes written: %s",
434 bytes_written, GST_BUFFER_SIZE (buf),
438 bytes_written += wrote;
441 filesink->data_written += bytes_written - back_pending;
444 gst_buffer_unref (buf);
446 g_signal_emit (G_OBJECT (filesink),
447 gst_filesink_signals[SIGNAL_HANDOFF], 0,
451 static GstElementStateReturn
452 gst_filesink_change_state (GstElement *element)
454 g_return_val_if_fail (GST_IS_FILESINK (element), GST_STATE_FAILURE);
456 switch (GST_STATE_TRANSITION (element)) {
457 case GST_STATE_PAUSED_TO_READY:
458 if (GST_FLAG_IS_SET (element, GST_FILESINK_OPEN))
459 gst_filesink_close_file (GST_FILESINK (element));
462 case GST_STATE_READY_TO_PAUSED:
463 if (!GST_FLAG_IS_SET (element, GST_FILESINK_OPEN)) {
464 if (!gst_filesink_open_file (GST_FILESINK (element)))
465 return GST_STATE_FAILURE;
470 if (GST_ELEMENT_CLASS (parent_class)->change_state)
471 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
473 return GST_STATE_SUCCESS;
476 /*** GSTURIHANDLER INTERFACE *************************************************/
479 gst_filesink_uri_get_type (void)
484 gst_filesink_uri_get_protocols(void)
486 static gchar *protocols[] = {"file", NULL};
490 gst_filesink_uri_get_uri (GstURIHandler *handler)
492 GstFileSink *sink = GST_FILESINK (handler);
497 gst_filesink_uri_set_uri (GstURIHandler *handler, const gchar *uri)
499 gchar *protocol, *location;
501 GstFileSink *sink = GST_FILESINK (handler);
503 protocol = gst_uri_get_protocol (uri);
504 if (strcmp (protocol, "file") != 0) {
509 location = gst_uri_get_location (uri);
510 ret = gst_filesink_set_location (sink, location);
517 gst_filesink_uri_handler_init (gpointer g_iface, gpointer iface_data)
519 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
521 iface->get_type = gst_filesink_uri_get_type;
522 iface->get_protocols = gst_filesink_uri_get_protocols;
523 iface->get_uri = gst_filesink_uri_get_uri;
524 iface->set_uri = gst_filesink_uri_set_uri;