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_dispose (GObject *object);
71 static void gst_filesink_set_property (GObject *object, guint prop_id,
72 const GValue *value, GParamSpec *pspec);
73 static void gst_filesink_get_property (GObject *object, guint prop_id,
74 GValue *value, GParamSpec *pspec);
76 static gboolean gst_filesink_open_file (GstFileSink *sink);
77 static void gst_filesink_close_file (GstFileSink *sink);
79 static gboolean gst_filesink_handle_event (GstPad *pad, GstEvent *event);
80 static gboolean gst_filesink_pad_query (GstPad *pad, GstQueryType type,
81 GstFormat *format, gint64 *value);
82 static void gst_filesink_chain (GstPad *pad,GstData *_data);
84 static void gst_filesink_uri_handler_init (gpointer g_iface, gpointer iface_data);
86 static GstElementStateReturn gst_filesink_change_state (GstElement *element);
88 static guint gst_filesink_signals[LAST_SIGNAL] = { 0 };
91 _do_init (GType filesink_type)
93 static const GInterfaceInfo urihandler_info = {
94 gst_filesink_uri_handler_init,
98 g_type_add_interface_static (filesink_type, GST_TYPE_URI_HANDLER, &urihandler_info);
99 GST_DEBUG_CATEGORY_INIT (gst_filesink_debug, "filesink", 0, "filesink element");
102 GST_BOILERPLATE_FULL (GstFileSink, gst_filesink, GstElement, GST_TYPE_ELEMENT, _do_init);
106 gst_filesink_base_init (gpointer g_class)
108 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
110 gstelement_class->change_state = gst_filesink_change_state;
111 gst_element_class_set_details (gstelement_class, &gst_filesink_details);
114 gst_filesink_class_init (GstFileSinkClass *klass)
116 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
119 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATION,
120 g_param_spec_string ("location", "File Location", "Location of the file to write",
121 NULL, G_PARAM_READWRITE));
123 gst_filesink_signals[SIGNAL_HANDOFF] =
124 g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
125 G_STRUCT_OFFSET (GstFileSinkClass, handoff), NULL, NULL,
126 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
128 gobject_class->set_property = gst_filesink_set_property;
129 gobject_class->get_property = gst_filesink_get_property;
130 gobject_class->dispose = gst_filesink_dispose;
133 gst_filesink_init (GstFileSink *filesink)
137 pad = gst_pad_new ("sink", GST_PAD_SINK);
138 gst_element_add_pad (GST_ELEMENT (filesink), pad);
139 gst_pad_set_chain_function (pad, gst_filesink_chain);
141 GST_FLAG_SET (GST_ELEMENT(filesink), GST_ELEMENT_EVENT_AWARE);
143 gst_pad_set_query_function (pad, gst_filesink_pad_query);
144 gst_pad_set_query_type_function (pad, gst_filesink_get_query_types);
145 gst_pad_set_formats_function (pad, gst_filesink_get_formats);
147 filesink->filename = NULL;
148 filesink->file = NULL;
151 gst_filesink_dispose (GObject *object)
153 GstFileSink *sink = GST_FILESINK (object);
155 G_OBJECT_CLASS (parent_class)->dispose (object);
159 g_free (sink->filename);
160 sink->filename = NULL;
163 gst_filesink_set_location (GstFileSink *sink, const gchar *location)
165 /* the element must be stopped or paused in order to do this */
166 if (GST_STATE (sink) > GST_STATE_PAUSED)
168 if (GST_STATE (sink) == GST_STATE_PAUSED &&
169 GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN))
172 g_free (sink->filename);
174 if (location != NULL) {
175 sink->filename = g_strdup (location);
176 sink->uri = gst_uri_construct ("file", location);
178 sink->filename = NULL;
182 if (GST_STATE (sink) == GST_STATE_PAUSED)
183 gst_filesink_open_file (sink);
188 gst_filesink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
192 /* it's not null if we got it, but it might not be ours */
193 sink = GST_FILESINK (object);
197 gst_filesink_set_location (sink, g_value_get_string (value));
200 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
206 gst_filesink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
210 /* it's not null if we got it, but it might not be ours */
211 g_return_if_fail (GST_IS_FILESINK (object));
213 sink = GST_FILESINK (object);
217 g_value_set_string (value, sink->filename);
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226 gst_filesink_open_file (GstFileSink *sink)
228 g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN), FALSE);
233 gst_element_error (GST_ELEMENT (sink),
234 "Error opening file: no file given");
238 sink->file = fopen (sink->filename, "w");
239 if (sink->file == NULL) {
240 gst_element_error (GST_ELEMENT (sink),
241 "Error opening file %s: %s",
242 sink->filename, g_strerror(errno));
246 GST_FLAG_SET (sink, GST_FILESINK_OPEN);
248 sink->data_written = 0;
254 gst_filesink_close_file (GstFileSink *sink)
256 g_return_if_fail (GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
258 if (fclose (sink->file) != 0)
260 gst_element_error (GST_ELEMENT (sink),
261 "Error closing file %s: %s",
262 sink->filename, g_strerror(errno));
265 GST_FLAG_UNSET (sink, GST_FILESINK_OPEN);
270 gst_filesink_pad_query (GstPad *pad, GstQueryType type,
271 GstFormat *format, gint64 *value)
273 GstFileSink *sink = GST_FILESINK (GST_PAD_PARENT (pad));
276 case GST_QUERY_TOTAL:
278 case GST_FORMAT_BYTES:
279 if (GST_FLAG_IS_SET (GST_ELEMENT(sink), GST_FILESINK_OPEN)) {
280 *value = sink->data_written; /* FIXME - doesn't the kernel provide
288 case GST_QUERY_POSITION:
290 case GST_FORMAT_BYTES:
291 if (GST_FLAG_IS_SET (GST_ELEMENT(sink), GST_FILESINK_OPEN)) {
292 *value = ftell (sink->file);
306 /* handle events (search) */
308 gst_filesink_handle_event (GstPad *pad, GstEvent *event)
311 GstFileSink *filesink;
313 filesink = GST_FILESINK (gst_pad_get_parent (pad));
315 g_return_val_if_fail (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN),
318 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
322 g_return_val_if_fail (GST_EVENT_SEEK_FORMAT (event) == GST_FORMAT_BYTES,
325 if (GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH)
326 if (fflush (filesink->file))
327 gst_element_error (GST_ELEMENT (filesink),
328 "Error flushing file %s: %s",
329 filesink->filename, g_strerror(errno));
331 switch (GST_EVENT_SEEK_METHOD(event))
333 case GST_SEEK_METHOD_SET:
334 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_SET);
336 case GST_SEEK_METHOD_CUR:
337 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_CUR);
339 case GST_SEEK_METHOD_END:
340 fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_END);
343 g_warning ("unknown seek method!");
347 case GST_EVENT_DISCONTINUOUS:
351 if (gst_event_discont_get_value (event, GST_FORMAT_BYTES, &offset))
352 fseek (filesink->file, offset, SEEK_SET);
354 gst_event_unref (event);
357 case GST_EVENT_FLUSH:
358 if (fflush (filesink->file)) {
359 gst_element_error (GST_ELEMENT (filesink),
360 "Error flushing file %s: %s",
361 filesink->filename, g_strerror(errno));
365 gst_filesink_close_file (filesink);
366 gst_element_set_eos (GST_ELEMENT (filesink));
369 gst_pad_event_default (pad, event);
377 * gst_filesink_chain:
378 * @pad: the pad this filesink is connected to
379 * @buf: the buffer that has to be absorbed
381 * take the buffer from the pad and write to file if it's open
384 gst_filesink_chain (GstPad *pad, GstData *_data)
386 GstBuffer *buf = GST_BUFFER (_data);
387 GstFileSink *filesink;
389 g_return_if_fail (pad != NULL);
390 g_return_if_fail (GST_IS_PAD (pad));
391 g_return_if_fail (buf != NULL);
393 filesink = GST_FILESINK (gst_pad_get_parent (pad));
395 if (GST_IS_EVENT(buf))
397 gst_filesink_handle_event(pad, GST_EVENT(buf));
401 if (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN))
403 guint bytes_written = 0, back_pending = 0;
404 if (ftell(filesink->file) < filesink->data_written)
405 back_pending = filesink->data_written - ftell(filesink->file);
406 while (bytes_written < GST_BUFFER_SIZE (buf)) {
407 size_t wrote = fwrite (GST_BUFFER_DATA (buf) + bytes_written, 1,
408 GST_BUFFER_SIZE (buf) - bytes_written,
411 gst_element_error (GST_ELEMENT (filesink),
412 "Only %d of %d bytes written: %s",
413 bytes_written, GST_BUFFER_SIZE (buf),
417 bytes_written += wrote;
420 filesink->data_written += bytes_written - back_pending;
423 gst_buffer_unref (buf);
425 g_signal_emit (G_OBJECT (filesink),
426 gst_filesink_signals[SIGNAL_HANDOFF], 0,
430 static GstElementStateReturn
431 gst_filesink_change_state (GstElement *element)
433 g_return_val_if_fail (GST_IS_FILESINK (element), GST_STATE_FAILURE);
435 switch (GST_STATE_TRANSITION (element)) {
436 case GST_STATE_PAUSED_TO_READY:
437 if (GST_FLAG_IS_SET (element, GST_FILESINK_OPEN))
438 gst_filesink_close_file (GST_FILESINK (element));
441 case GST_STATE_READY_TO_PAUSED:
442 if (!GST_FLAG_IS_SET (element, GST_FILESINK_OPEN)) {
443 if (!gst_filesink_open_file (GST_FILESINK (element)))
444 return GST_STATE_FAILURE;
449 if (GST_ELEMENT_CLASS (parent_class)->change_state)
450 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
452 return GST_STATE_SUCCESS;
455 /*** GSTURIHANDLER INTERFACE *************************************************/
458 gst_filesink_uri_get_type (void)
463 gst_filesink_uri_get_protocols(void)
465 static gchar *protocols[] = {"file", NULL};
469 gst_filesink_uri_get_uri (GstURIHandler *handler)
471 GstFileSink *sink = GST_FILESINK (handler);
476 gst_filesink_uri_set_uri (GstURIHandler *handler, const gchar *uri)
478 gchar *protocol, *location;
480 GstFileSink *sink = GST_FILESINK (handler);
482 protocol = gst_uri_get_protocol (uri);
483 if (strcmp (protocol, "file") != 0) {
488 location = gst_uri_get_location (uri);
489 ret = gst_filesink_set_location (sink, location);
496 gst_filesink_uri_handler_init (gpointer g_iface, gpointer iface_data)
498 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
500 iface->get_type = gst_filesink_uri_get_type;
501 iface->get_protocols = gst_filesink_uri_get_protocols;
502 iface->get_uri = gst_filesink_uri_get_uri;
503 iface->set_uri = gst_filesink_uri_set_uri;