Some code cleanups.
[platform/upstream/gstreamer.git] / gst / elements / gstdisksink.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstdisksink.c: 
6  *
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.
11  *
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.
16  *
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.
21  */
22
23
24 #include <gst/gst.h>
25 #include "gstdisksink.h"
26
27
28 GstElementDetails gst_disksink_details = {
29   "Disk Sink",
30   "Sink",
31   "Disk hole for data",
32   VERSION,
33   "Thomas <thomas@apestaart.org>",
34   "(C) 2001"
35 };
36
37
38 /* DiskSink signals and args */
39 enum {
40   /* FILL ME */
41   SIGNAL_HANDOFF,
42   LAST_SIGNAL
43 };
44
45 enum {
46   ARG_0,
47   ARG_LOCATION,
48 };
49
50
51 static void     gst_disksink_class_init         (GstDiskSinkClass *klass);
52 static void     gst_disksink_init               (GstDiskSink *disksink);
53
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);
58
59 static gboolean gst_disksink_open_file          (GstDiskSink *sink);
60 static void     gst_disksink_close_file         (GstDiskSink *sink);
61
62 static void     gst_disksink_chain              (GstPad *pad,GstBuffer *buf);
63
64 static GstElementStateReturn gst_disksink_change_state (GstElement *element);
65
66 static GstElementClass *parent_class = NULL;
67 static guint gst_disksink_signals[LAST_SIGNAL] = { 0 };
68
69 GType
70 gst_disksink_get_type (void) 
71 {
72   static GType disksink_type = 0;
73
74   if (!disksink_type) {
75     static const GTypeInfo disksink_info = {
76       sizeof(GstDiskSinkClass),      NULL,
77       NULL,
78       (GClassInitFunc)gst_disksink_class_init,
79       NULL,
80       NULL,
81       sizeof(GstDiskSink),
82       0,
83       (GInstanceInitFunc)gst_disksink_init,
84     };
85     disksink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstDiskSink", &disksink_info, 0);
86   }
87   return disksink_type;
88 }
89
90 static void
91 gst_disksink_class_init (GstDiskSinkClass *klass) 
92 {
93   GObjectClass *gobject_class;
94   GstElementClass *gstelement_class;
95
96   gobject_class = (GObjectClass*)klass;
97   gstelement_class = (GstElementClass*)klass;
98
99   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
100
101   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LOCATION,
102     g_param_spec_string("location","location","location",
103                         NULL,G_PARAM_READWRITE)); // CHECKME!
104
105   gst_disksink_signals[SIGNAL_HANDOFF] =
106     g_signal_newc ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
107                     G_STRUCT_OFFSET (GstDiskSinkClass, handoff), NULL, NULL,
108                     g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
109
110   gobject_class->set_property = gst_disksink_set_property;
111   gobject_class->get_property = gst_disksink_get_property;
112
113   gstelement_class->change_state = gst_disksink_change_state;
114 }
115
116 static void 
117 gst_disksink_init (GstDiskSink *disksink) 
118 {
119   GstPad *pad;
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);
123
124   disksink->filename = NULL;
125   disksink->file = NULL;
126 }
127
128 static void
129 gst_disksink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
130 {
131   GstDiskSink *sink;
132
133   /* it's not null if we got it, but it might not be ours */
134   sink = GST_DISKSINK (object);
135
136   switch (prop_id) {
137     case ARG_LOCATION:
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));
141       if (sink->filename)
142         g_free (sink->filename);
143       sink->filename = g_strdup (g_value_get_string (value));
144       if ( (GST_STATE (sink) == GST_STATE_PAUSED) 
145         && (sink->filename != NULL))
146       {
147               gst_disksink_close_file (sink);
148               gst_disksink_open_file (sink);   
149       }
150  
151       break;
152     default:
153       break;
154   }
155 }
156
157 static void   
158 gst_disksink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
159 {
160   GstDiskSink *sink;
161  
162   /* it's not null if we got it, but it might not be ours */
163   g_return_if_fail (GST_IS_DISKSINK (object));
164  
165   sink = GST_DISKSINK (object);
166   
167   switch (prop_id) {
168     case ARG_LOCATION:
169       g_value_set_string (value, sink->filename);
170       break;
171     default:
172       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
173       break;
174   }
175 }
176
177 static gboolean
178 gst_disksink_open_file (GstDiskSink *sink)
179 {
180   g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_DISKSINK_OPEN), FALSE);
181
182   /* open the file */
183   sink->file = fopen (sink->filename, "w");
184   if (sink->file == NULL) {
185     perror ("open");
186     gst_element_error (GST_ELEMENT (sink), g_strconcat("opening file \"", sink->filename, "\"", NULL));
187     return FALSE;
188   } 
189
190   GST_FLAG_SET (sink, GST_DISKSINK_OPEN);
191
192   return TRUE;
193 }
194
195 static void
196 gst_disksink_close_file (GstDiskSink *sink)
197 {
198   g_return_if_fail (GST_FLAG_IS_SET (sink, GST_DISKSINK_OPEN));
199
200   if (fclose (sink->file) != 0)
201   {
202     perror ("close");
203     gst_element_error (GST_ELEMENT (sink), g_strconcat("closing file \"", sink->filename, "\"", NULL));
204   }
205   else {
206     GST_FLAG_UNSET (sink, GST_DISKSINK_OPEN);
207   }
208 }
209
210 /**
211  * gst_disksink_chain:
212  * @pad: the pad this disksink is connected to
213  * @buf: the buffer that has to be absorbed
214  *
215  * take the buffer from the pad and write to file if it's open
216  */
217 static void 
218 gst_disksink_chain (GstPad *pad, GstBuffer *buf) 
219 {
220   GstDiskSink *disksink;
221   gint bytes_written = 0;
222
223   g_return_if_fail (pad != NULL);
224   g_return_if_fail (GST_IS_PAD (pad));
225   g_return_if_fail (buf != NULL);
226
227   disksink = GST_DISKSINK (gst_pad_get_parent (pad));
228
229   if (GST_FLAG_IS_SET (disksink, GST_DISKSINK_OPEN))
230   {
231     bytes_written = fwrite (GST_BUFFER_DATA (buf), 1, GST_BUFFER_SIZE (buf), disksink->file);
232     if (bytes_written < GST_BUFFER_SIZE (buf))
233     {
234       printf ("disksink : Warning : %d bytes should be written, only %d bytes written\n",
235                   GST_BUFFER_SIZE (buf), bytes_written);
236     }
237   }
238   gst_buffer_unref (buf);
239
240   g_signal_emit (G_OBJECT (disksink), gst_disksink_signals[SIGNAL_HANDOFF], 0,
241                               disksink);
242 }
243
244 static GstElementStateReturn
245 gst_disksink_change_state (GstElement *element)
246 {
247   g_return_val_if_fail (GST_IS_DISKSINK (element), GST_STATE_FAILURE);
248
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));
252   } else {
253     if (!GST_FLAG_IS_SET (element, GST_DISKSINK_OPEN)) {
254       if (!gst_disksink_open_file (GST_DISKSINK (element)))
255         return GST_STATE_FAILURE;
256     }
257   }
258
259   if (GST_ELEMENT_CLASS (parent_class)->change_state)
260     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
261
262   return GST_STATE_SUCCESS;
263 }
264