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_property (GObject *object, guint prop_id,
55 const GValue *value, GParamSpec *pspec);
56 static void gst_disksink_get_property (GObject *object, guint prop_id,
57 GValue *value, GParamSpec *pspec);
59 static gboolean gst_disksink_open_file (GstDiskSink *sink);
60 static void gst_disksink_close_file (GstDiskSink *sink);
62 static void gst_disksink_chain (GstPad *pad,GstBuffer *buf);
64 static GstElementStateReturn gst_disksink_change_state (GstElement *element);
66 static GstElementClass *parent_class = NULL;
67 static guint gst_disksink_signals[LAST_SIGNAL] = { 0 };
70 gst_disksink_get_type (void)
72 static GType disksink_type = 0;
75 static const GTypeInfo disksink_info = {
76 sizeof(GstDiskSinkClass), NULL,
78 (GClassInitFunc)gst_disksink_class_init,
83 (GInstanceInitFunc)gst_disksink_init,
85 disksink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstDiskSink", &disksink_info, 0);
91 gst_disksink_class_init (GstDiskSinkClass *klass)
93 GObjectClass *gobject_class;
94 GstElementClass *gstelement_class;
96 gobject_class = (GObjectClass*)klass;
97 gstelement_class = (GstElementClass*)klass;
99 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
101 gst_element_install_std_props (
102 GST_ELEMENT_CLASS (klass),
103 "location", ARG_LOCATION, G_PARAM_READWRITE,
106 gst_disksink_signals[SIGNAL_HANDOFF] =
107 g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
108 G_STRUCT_OFFSET (GstDiskSinkClass, handoff), NULL, NULL,
109 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
111 gobject_class->set_property = gst_disksink_set_property;
112 gobject_class->get_property = gst_disksink_get_property;
114 gstelement_class->change_state = gst_disksink_change_state;
118 gst_disksink_init (GstDiskSink *disksink)
121 pad = gst_pad_new ("sink", GST_PAD_SINK);
122 gst_element_add_pad (GST_ELEMENT (disksink), pad);
123 gst_pad_set_chain_function (pad, gst_disksink_chain);
125 disksink->filename = NULL;
126 disksink->file = NULL;
130 gst_disksink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
134 /* it's not null if we got it, but it might not be ours */
135 sink = GST_DISKSINK (object);
139 /* the element must be stopped or paused in order to do this */
140 g_return_if_fail ((GST_STATE (sink) < GST_STATE_PLAYING)
141 || (GST_STATE (sink) == GST_STATE_PAUSED));
143 g_free (sink->filename);
144 sink->filename = g_strdup (g_value_get_string (value));
145 if ( (GST_STATE (sink) == GST_STATE_PAUSED)
146 && (sink->filename != NULL))
148 gst_disksink_close_file (sink);
149 gst_disksink_open_file (sink);
159 gst_disksink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
163 /* it's not null if we got it, but it might not be ours */
164 g_return_if_fail (GST_IS_DISKSINK (object));
166 sink = GST_DISKSINK (object);
170 g_value_set_string (value, sink->filename);
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 gst_disksink_open_file (GstDiskSink *sink)
181 g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_DISKSINK_OPEN), FALSE);
184 sink->file = fopen (sink->filename, "w");
185 if (sink->file == NULL) {
187 gst_element_error (GST_ELEMENT (sink), g_strconcat("opening file \"", sink->filename, "\"", NULL));
191 GST_FLAG_SET (sink, GST_DISKSINK_OPEN);
197 gst_disksink_close_file (GstDiskSink *sink)
199 g_return_if_fail (GST_FLAG_IS_SET (sink, GST_DISKSINK_OPEN));
201 if (fclose (sink->file) != 0)
204 gst_element_error (GST_ELEMENT (sink), g_strconcat("closing file \"", sink->filename, "\"", NULL));
207 GST_FLAG_UNSET (sink, GST_DISKSINK_OPEN);
212 * gst_disksink_chain:
213 * @pad: the pad this disksink is connected to
214 * @buf: the buffer that has to be absorbed
216 * take the buffer from the pad and write to file if it's open
219 gst_disksink_chain (GstPad *pad, GstBuffer *buf)
221 GstDiskSink *disksink;
222 gint bytes_written = 0;
224 g_return_if_fail (pad != NULL);
225 g_return_if_fail (GST_IS_PAD (pad));
226 g_return_if_fail (buf != NULL);
228 disksink = GST_DISKSINK (gst_pad_get_parent (pad));
230 if (GST_FLAG_IS_SET (disksink, GST_DISKSINK_OPEN))
232 bytes_written = fwrite (GST_BUFFER_DATA (buf), 1, GST_BUFFER_SIZE (buf), disksink->file);
233 if (bytes_written < GST_BUFFER_SIZE (buf))
235 printf ("disksink : Warning : %d bytes should be written, only %d bytes written\n",
236 GST_BUFFER_SIZE (buf), bytes_written);
239 gst_buffer_unref (buf);
241 g_signal_emit (G_OBJECT (disksink), gst_disksink_signals[SIGNAL_HANDOFF], 0,
245 static GstElementStateReturn
246 gst_disksink_change_state (GstElement *element)
248 g_return_val_if_fail (GST_IS_DISKSINK (element), GST_STATE_FAILURE);
250 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
251 if (GST_FLAG_IS_SET (element, GST_DISKSINK_OPEN))
252 gst_disksink_close_file (GST_DISKSINK (element));
254 if (!GST_FLAG_IS_SET (element, GST_DISKSINK_OPEN)) {
255 if (!gst_disksink_open_file (GST_DISKSINK (element)))
256 return GST_STATE_FAILURE;
260 if (GST_ELEMENT_CLASS (parent_class)->change_state)
261 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
263 return GST_STATE_SUCCESS;