Merged from GOBJECT1 to HEAD at 200106241
[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, const GValue *value, GParamSpec *pspec);
55 static void     gst_disksink_get_property       (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
56
57 static gboolean gst_disksink_open_file  (GstDiskSink *sink);
58 static void     gst_disksink_close_file (GstDiskSink *sink);
59
60 static void     gst_disksink_chain      (GstPad *pad,GstBuffer *buf);
61
62 static GstElementStateReturn gst_disksink_change_state (GstElement *element);
63
64 static GstElementClass *parent_class = NULL;
65 static guint gst_disksink_signals[LAST_SIGNAL] = { 0 };
66
67 GType
68 gst_disksink_get_type (void) 
69 {
70   static GType disksink_type = 0;
71
72   if (!disksink_type) {
73     static const GTypeInfo disksink_info = {
74       sizeof(GstDiskSinkClass),      NULL,
75       NULL,
76       (GClassInitFunc)gst_disksink_class_init,
77       NULL,
78       NULL,
79       sizeof(GstDiskSink),
80       0,
81       (GInstanceInitFunc)gst_disksink_init,
82     };
83     disksink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstDiskSink", &disksink_info, 0);
84   }
85   return disksink_type;
86 }
87
88 static void
89 gst_disksink_class_init (GstDiskSinkClass *klass) 
90 {
91   GObjectClass *gobject_class;
92   GstElementClass *gstelement_class;
93
94   gobject_class = (GObjectClass*)klass;
95   gstelement_class = (GstElementClass*)klass;
96
97   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
98
99   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LOCATION,
100     g_param_spec_string("location","location","location",
101                         NULL,G_PARAM_READWRITE)); // CHECKME!
102
103   gst_disksink_signals[SIGNAL_HANDOFF] =
104     g_signal_newc ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
105                     G_STRUCT_OFFSET (GstDiskSinkClass, handoff), NULL, NULL,
106                     g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
107
108   gobject_class->set_property = gst_disksink_set_property;
109   gobject_class->get_property = gst_disksink_get_property;
110
111   gstelement_class->change_state = gst_disksink_change_state;
112 }
113
114 static void 
115 gst_disksink_init (GstDiskSink *disksink) 
116 {
117   GstPad *pad;
118   pad = gst_pad_new ("sink", GST_PAD_SINK);
119   gst_element_add_pad (GST_ELEMENT (disksink), pad);
120   gst_pad_set_chain_function (pad, gst_disksink_chain);
121
122   disksink->filename = NULL;
123   disksink->file = NULL;
124 }
125
126 static void
127 gst_disksink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
128 {
129   GstDiskSink *sink;
130
131   /* it's not null if we got it, but it might not be ours */
132   sink = GST_DISKSINK (object);
133
134   switch (prop_id) {
135     case ARG_LOCATION:
136       /* the element must be stopped or paused in order to do this */
137       g_return_if_fail ((GST_STATE (sink) < GST_STATE_PLAYING)
138                       || (GST_STATE (sink) == GST_STATE_PAUSED));
139       if (sink->filename)
140         g_free (sink->filename);
141       sink->filename = g_strdup (g_value_get_string (value));
142       if ( (GST_STATE (sink) == GST_STATE_PAUSED) 
143         && (sink->filename != NULL))
144       {
145               gst_disksink_close_file (sink);
146               gst_disksink_open_file (sink);   
147       }
148  
149       break;
150     default:
151       break;
152   }
153 }
154
155 static void   
156 gst_disksink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
157 {
158   GstDiskSink *sink;
159  
160   /* it's not null if we got it, but it might not be ours */
161   g_return_if_fail (GST_IS_DISKSINK (object));
162  
163   sink = GST_DISKSINK (object);
164   
165   switch (prop_id) {
166     case ARG_LOCATION:
167       g_value_set_string (value, sink->filename);
168       break;
169     default:
170       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
171       break;
172   }
173 }
174
175 static gboolean
176 gst_disksink_open_file (GstDiskSink *sink)
177 {
178   g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_DISKSINK_OPEN), FALSE);
179
180   /* open the file */
181   sink->file = fopen (sink->filename, "w");
182   if (sink->file == NULL) {
183     perror ("open");
184     gst_element_error (GST_ELEMENT (sink), g_strconcat("opening file \"", sink->filename, "\"", NULL));
185     return FALSE;
186   } 
187
188   GST_FLAG_SET (sink, GST_DISKSINK_OPEN);
189
190   return TRUE;
191 }
192
193 static void
194 gst_disksink_close_file (GstDiskSink *sink)
195 {
196   g_return_if_fail (GST_FLAG_IS_SET (sink, GST_DISKSINK_OPEN));
197
198   if (fclose (sink->file) != 0)
199   {
200     perror ("close");
201     gst_element_error (GST_ELEMENT (sink), g_strconcat("closing file \"", sink->filename, "\"", NULL));
202   }
203   else {
204     GST_FLAG_UNSET (sink, GST_DISKSINK_OPEN);
205   }
206 }
207
208 /**
209  * gst_disksink_chain:
210  * @pad: the pad this disksink is connected to
211  * @buf: the buffer that has to be absorbed
212  *
213  * take the buffer from the pad and write to file if it's open
214  */
215 static void 
216 gst_disksink_chain (GstPad *pad, GstBuffer *buf) 
217 {
218   GstDiskSink *disksink;
219   gint bytes_written = 0;
220
221   g_return_if_fail (pad != NULL);
222   g_return_if_fail (GST_IS_PAD (pad));
223   g_return_if_fail (buf != NULL);
224
225   disksink = GST_DISKSINK (gst_pad_get_parent (pad));
226
227   if (GST_FLAG_IS_SET (disksink, GST_DISKSINK_OPEN))
228   {
229     bytes_written = fwrite (GST_BUFFER_DATA (buf), 1, GST_BUFFER_SIZE (buf), disksink->file);
230     if (bytes_written < GST_BUFFER_SIZE (buf))
231     {
232       printf ("disksink : Warning : %d bytes should be written, only %d bytes written\n",
233                   GST_BUFFER_SIZE (buf), bytes_written);
234     }
235   }
236   gst_buffer_unref (buf);
237
238   g_signal_emit (G_OBJECT (disksink), gst_disksink_signals[SIGNAL_HANDOFF], 0,
239                               disksink);
240 }
241
242 static GstElementStateReturn
243 gst_disksink_change_state (GstElement *element)
244 {
245   g_return_val_if_fail (GST_IS_DISKSINK (element), GST_STATE_FAILURE);
246
247   if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
248     if (GST_FLAG_IS_SET (element, GST_DISKSINK_OPEN))
249       gst_disksink_close_file (GST_DISKSINK (element));
250   } else {
251     if (!GST_FLAG_IS_SET (element, GST_DISKSINK_OPEN)) {
252       if (!gst_disksink_open_file (GST_DISKSINK (element)))
253         return GST_STATE_FAILURE;
254     }
255   }
256
257   if (GST_ELEMENT_CLASS (parent_class)->change_state)
258     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
259
260   return GST_STATE_SUCCESS;
261 }
262