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.
25 #include "gstdisksink.h"
28 GstElementDetails gst_disksink_details = {
33 "Thomas <thomas@apestaart.org>",
38 /* DiskSink signals and args */
51 static void gst_disksink_class_init (GstDiskSinkClass *klass);
52 static void gst_disksink_init (GstDiskSink *disksink);
54 static void gst_disksink_set_arg (GtkObject *object, GtkArg *arg, guint id);
55 static void gst_disksink_get_arg (GtkObject *object, GtkArg *arg, guint id);
57 static gboolean gst_disksink_open_file (GstDiskSink *sink);
58 static void gst_disksink_close_file (GstDiskSink *sink);
60 static void gst_disksink_chain (GstPad *pad,GstBuffer *buf);
62 static GstElementStateReturn gst_disksink_change_state (GstElement *element);
64 static GstElementClass *parent_class = NULL;
65 static guint gst_disksink_signals[LAST_SIGNAL] = { 0 };
68 gst_disksink_get_type (void)
70 static GtkType disksink_type = 0;
73 static const GtkTypeInfo disksink_info = {
76 sizeof(GstDiskSinkClass),
77 (GtkClassInitFunc)gst_disksink_class_init,
78 (GtkObjectInitFunc)gst_disksink_init,
79 (GtkArgSetFunc)gst_disksink_set_arg,
80 (GtkArgGetFunc)gst_disksink_get_arg,
81 (GtkClassInitFunc)NULL, /* deprecated, do not use ! */
83 disksink_type = gtk_type_unique (GST_TYPE_ELEMENT, &disksink_info);
89 gst_disksink_class_init (GstDiskSinkClass *klass)
91 GtkObjectClass *gtkobject_class;
92 GstElementClass *gstelement_class;
94 gtkobject_class = (GtkObjectClass*)klass;
95 gstelement_class = (GstElementClass*)klass;
97 parent_class = gtk_type_class (GST_TYPE_ELEMENT);
99 gtk_object_add_arg_type ("GstDiskSink::location", GST_TYPE_FILENAME,
100 GTK_ARG_READWRITE, ARG_LOCATION);
102 gst_disksink_signals[SIGNAL_HANDOFF] =
103 gtk_signal_new ("handoff", GTK_RUN_LAST, gtkobject_class->type,
104 GTK_SIGNAL_OFFSET (GstDiskSinkClass, handoff),
105 gtk_marshal_NONE__NONE, GTK_TYPE_NONE, 0);
107 gtk_object_class_add_signals (gtkobject_class, gst_disksink_signals,
110 gtkobject_class->set_arg = gst_disksink_set_arg;
111 gtkobject_class->get_arg = gst_disksink_get_arg;
113 gstelement_class->change_state = gst_disksink_change_state;
117 gst_disksink_init (GstDiskSink *disksink)
120 pad = gst_pad_new ("sink", GST_PAD_SINK);
121 gst_element_add_pad (GST_ELEMENT (disksink), pad);
122 gst_pad_set_chain_function (pad, gst_disksink_chain);
124 disksink->filename = NULL;
125 disksink->file = NULL;
129 gst_disksink_set_arg (GtkObject *object, GtkArg *arg, guint id)
133 /* it's not null if we got it, but it might not be ours */
134 sink = GST_DISKSINK (object);
138 /* the element must be stopped or paused in order to do this */
139 g_return_if_fail ((GST_STATE (sink) < GST_STATE_PLAYING)
140 || (GST_STATE (sink) == GST_STATE_PAUSED));
142 g_free (sink->filename);
143 sink->filename = g_strdup (GTK_VALUE_STRING (*arg));
144 if ( (GST_STATE (sink) == GST_STATE_PAUSED)
145 && (sink->filename != NULL))
147 gst_disksink_close_file (sink);
148 gst_disksink_open_file (sink);
158 gst_disksink_get_arg (GtkObject *object, GtkArg *arg, guint id)
162 /* it's not null if we got it, but it might not be ours */
163 g_return_if_fail (GST_IS_DISKSINK (object));
165 sink = GST_DISKSINK (object);
169 GTK_VALUE_STRING (*arg) = sink->filename;
172 arg->type = GTK_TYPE_INVALID;
178 gst_disksink_open_file (GstDiskSink *sink)
180 g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_DISKSINK_OPEN), FALSE);
183 sink->file = fopen (sink->filename, "w");
184 if (sink->file == NULL) {
186 gst_element_error (GST_ELEMENT (sink), g_strconcat("opening file \"", sink->filename, "\"", NULL));
190 GST_FLAG_SET (sink, GST_DISKSINK_OPEN);
196 gst_disksink_close_file (GstDiskSink *sink)
198 g_return_if_fail (GST_FLAG_IS_SET (sink, GST_DISKSINK_OPEN));
200 if (fclose (sink->file) != 0)
203 gst_element_error (GST_ELEMENT (sink), g_strconcat("closing file \"", sink->filename, "\"", NULL));
206 GST_FLAG_UNSET (sink, GST_DISKSINK_OPEN);
211 * gst_disksink_chain:
212 * @pad: the pad this disksink is connected to
213 * @buf: the buffer that has to be absorbed
215 * take the buffer from the pad and write to file if it's open
218 gst_disksink_chain (GstPad *pad, GstBuffer *buf)
220 GstDiskSink *disksink;
221 guint16 bytes_written = 0;
223 g_return_if_fail (pad != NULL);
224 g_return_if_fail (GST_IS_PAD (pad));
225 g_return_if_fail (buf != NULL);
227 disksink = GST_DISKSINK (gst_pad_get_parent (pad));
229 if (GST_FLAG_IS_SET (disksink, GST_DISKSINK_OPEN))
231 bytes_written = fwrite (GST_BUFFER_DATA (buf), 1, GST_BUFFER_SIZE (buf), disksink->file);
232 if (bytes_written < GST_BUFFER_SIZE (buf))
234 printf ("disksink : Warning : %d bytes should be written, only %d bytes written\n",
235 GST_BUFFER_SIZE (buf), bytes_written);
238 gst_buffer_unref (buf);
240 gtk_signal_emit (GTK_OBJECT (disksink), gst_disksink_signals[SIGNAL_HANDOFF],
244 static GstElementStateReturn
245 gst_disksink_change_state (GstElement *element)
247 g_return_val_if_fail (GST_IS_DISKSINK (element), GST_STATE_FAILURE);
249 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
250 if (GST_FLAG_IS_SET (element, GST_DISKSINK_OPEN))
251 gst_disksink_close_file (GST_DISKSINK (element));
253 if (!GST_FLAG_IS_SET (element, GST_DISKSINK_OPEN)) {
254 if (!gst_disksink_open_file (GST_DISKSINK (element)))
255 return GST_STATE_FAILURE;
259 if (GST_ELEMENT_CLASS (parent_class)->change_state)
260 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
262 return GST_STATE_SUCCESS;