API docs updates.
[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_arg    (GtkObject *object, GtkArg *arg, guint id);
55 static void     gst_disksink_get_arg    (GtkObject *object, GtkArg *arg, guint id);
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 GtkType
68 gst_disksink_get_type (void) 
69 {
70   static GtkType disksink_type = 0;
71
72   if (!disksink_type) {
73     static const GtkTypeInfo disksink_info = {
74       "GstDiskSink",
75       sizeof(GstDiskSink),
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 ! */
82     };
83     disksink_type = gtk_type_unique (GST_TYPE_ELEMENT, &disksink_info);
84   }
85   return disksink_type;
86 }
87
88 static void
89 gst_disksink_class_init (GstDiskSinkClass *klass) 
90 {
91   GtkObjectClass *gtkobject_class;
92   GstElementClass *gstelement_class;
93
94   gtkobject_class = (GtkObjectClass*)klass;
95   gstelement_class = (GstElementClass*)klass;
96
97   parent_class = gtk_type_class (GST_TYPE_ELEMENT);
98
99   gtk_object_add_arg_type ("GstDiskSink::location", GST_TYPE_FILENAME,
100                            GTK_ARG_READWRITE, ARG_LOCATION);
101
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);
106
107   gtk_object_class_add_signals (gtkobject_class, gst_disksink_signals,
108                                     LAST_SIGNAL);
109
110   gtkobject_class->set_arg = gst_disksink_set_arg;
111   gtkobject_class->get_arg = gst_disksink_get_arg;
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_arg (GtkObject *object, GtkArg *arg, guint id)
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(id) {
137     case ARG_LOCATION:
138       if (sink->filename)
139         g_free (sink->filename);
140       sink->filename = g_strdup (GTK_VALUE_STRING (*arg));
141       break;
142     default:
143       break;
144   }
145 }
146
147 static void   
148 gst_disksink_get_arg (GtkObject *object, GtkArg *arg, guint id)
149 {
150   GstDiskSink *sink;
151  
152   /* it's not null if we got it, but it might not be ours */
153   g_return_if_fail (GST_IS_DISKSINK (object));
154  
155   sink = GST_DISKSINK (object);
156   
157   switch (id) {
158     case ARG_LOCATION:
159       GTK_VALUE_STRING (*arg) = sink->filename;
160       break;
161     default:
162       arg->type = GTK_TYPE_INVALID;
163       break;
164   }
165 }
166
167 static gboolean
168 gst_disksink_open_file (GstDiskSink *sink)
169 {
170   g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_DISKSINK_OPEN), FALSE);
171
172   /* open the file */
173   sink->file = fopen (sink->filename, "w");
174   if (sink->file == NULL) {
175     perror ("open");
176     gst_element_error (GST_ELEMENT (sink), g_strconcat("opening file \"", sink->filename, "\"", NULL));
177     return FALSE;
178   } 
179
180   GST_FLAG_SET (sink, GST_DISKSINK_OPEN);
181
182   return TRUE;
183 }
184
185 static void
186 gst_disksink_close_file (GstDiskSink *sink)
187 {
188   g_return_if_fail (GST_FLAG_IS_SET (sink, GST_DISKSINK_OPEN));
189
190   if (fclose (sink->file) != 0)
191   {
192     perror ("close");
193     gst_element_error (GST_ELEMENT (sink), g_strconcat("closing file \"", sink->filename, "\"", NULL));
194   }
195   else {
196     GST_FLAG_UNSET (sink, GST_DISKSINK_OPEN);
197   }
198 }
199
200 /**
201  * gst_disksink_chain:
202  * @pad: the pad this disksink is connected to
203  * @buf: the buffer that has to be absorbed
204  *
205  * take the buffer from the pad and write to file if it's open
206  */
207 static void 
208 gst_disksink_chain (GstPad *pad, GstBuffer *buf) 
209 {
210   GstDiskSink *disksink;
211   guint16 bytes_written = 0;
212
213   g_return_if_fail (pad != NULL);
214   g_return_if_fail (GST_IS_PAD (pad));
215   g_return_if_fail (buf != NULL);
216
217   disksink = GST_DISKSINK (gst_pad_get_parent (pad));
218
219   if (GST_FLAG_IS_SET (disksink, GST_DISKSINK_OPEN))
220   {
221     bytes_written = fwrite (GST_BUFFER_DATA (buf), 1, GST_BUFFER_SIZE (buf), disksink->file);
222     if (bytes_written < GST_BUFFER_SIZE (buf))
223     {
224       printf ("disksink : Warning : %d bytes should be written, only %d bytes written\n",
225                   GST_BUFFER_SIZE (buf), bytes_written);
226     }
227   }
228   gst_buffer_unref (buf);
229
230   gtk_signal_emit (GTK_OBJECT (disksink), gst_disksink_signals[SIGNAL_HANDOFF],
231                               disksink);
232 }
233
234 static GstElementStateReturn
235 gst_disksink_change_state (GstElement *element)
236 {
237   g_return_val_if_fail (GST_IS_DISKSINK (element), GST_STATE_FAILURE);
238
239   if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
240     if (GST_FLAG_IS_SET (element, GST_DISKSINK_OPEN))
241       gst_disksink_close_file (GST_DISKSINK (element));
242   } else {
243     if (!GST_FLAG_IS_SET (element, GST_DISKSINK_OPEN)) {
244       if (!gst_disksink_open_file (GST_DISKSINK (element)))
245         return GST_STATE_FAILURE;
246     }
247   }
248
249   if (GST_ELEMENT_CLASS (parent_class)->change_state)
250     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
251
252   return GST_STATE_SUCCESS;
253 }
254