first stab at a disk sink element no optimization, is it necessary ? basic error...
[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   ARG_CLOSED/*,
49   ARG_SILENT,
50   ARG_BYTESPERWRITE,
51   ARG_NUM_SOURCES,
52   */
53 };
54
55
56 static void     gst_disksink_class_init (GstDiskSinkClass *klass);
57 static void     gst_disksink_init       (GstDiskSink *disksink);
58
59 static void     gst_disksink_set_arg    (GtkObject *object, GtkArg *arg, guint id);
60 static void     gst_disksink_get_arg    (GtkObject *object, GtkArg *arg, guint id);
61
62 static void     gst_disksink_chain      (GstPad *pad,GstBuffer *buf);
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,           /* QUESTION : why null ? otherwise coredump */
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
93   gtkobject_class = (GtkObjectClass*)klass;
94
95   parent_class = gtk_type_class (GST_TYPE_ELEMENT);
96
97
98   gtk_object_add_arg_type ("GstDiskSink::location", GST_TYPE_FILENAME,
99                            GTK_ARG_READWRITE, ARG_LOCATION);
100 /*
101   gtk_object_add_arg_type ("GstDiskSink::silent", GTK_TYPE_BOOL,
102                            GTK_ARG_READWRITE, ARG_SILENT);
103 */
104   gtk_object_add_arg_type ("GstDiskSink::closed", GTK_TYPE_BOOL,
105                            GTK_ARG_READWRITE, ARG_CLOSED);
106
107
108   gst_disksink_signals[SIGNAL_HANDOFF] =
109     gtk_signal_new ("handoff", GTK_RUN_LAST, gtkobject_class->type,
110                     GTK_SIGNAL_OFFSET (GstDiskSinkClass, handoff),
111                     gtk_marshal_NONE__NONE, GTK_TYPE_NONE, 0);
112
113   gtk_object_class_add_signals (gtkobject_class, gst_disksink_signals,
114                                     LAST_SIGNAL);
115
116
117   gtkobject_class->set_arg = gst_disksink_set_arg;
118   gtkobject_class->get_arg = gst_disksink_get_arg;
119 }
120
121 static void 
122 gst_disksink_init (GstDiskSink *disksink) 
123 {
124   GstPad *pad;
125   pad = gst_pad_new ("sink", GST_PAD_SINK);
126   gst_element_add_pad (GST_ELEMENT (disksink), pad);
127   gst_pad_set_chain_function (pad, gst_disksink_chain);
128   disksink->opened = FALSE;
129   disksink->filename = NULL;
130   disksink->file = NULL;
131   
132 //  disksink->silent = FALSE;  ? what's this ? it's for output !
133 }
134
135 static void
136 gst_disksink_set_arg (GtkObject *object, GtkArg *arg, guint id)
137 {
138   GstDiskSink *sink;
139
140   /* it's not null if we got it, but it might not be ours */
141   sink = GST_DISKSINK (object);
142
143   switch(id) {
144     case ARG_LOCATION:
145      /* the element must be stopped in order to do this */
146       g_return_if_fail (GST_STATE (sink) < GST_STATE_PLAYING);  
147
148       if (sink->filename) g_free (sink->filename);
149       
150       sink->filename = g_strdup (GTK_VALUE_STRING (*arg));
151       sink->file = fopen (GTK_VALUE_STRING (*arg), "w");
152       if (sink->file == NULL)
153       {
154         g_error ("Cannot open %s for writing !\n", GTK_VALUE_STRING (*arg));
155                 //exit (-2);
156       }
157       else sink->opened = TRUE;
158       gst_element_set_state(GST_ELEMENT(sink),GST_STATE_READY);
159       break;
160       /*
161     case ARG_SILENT:
162       sink->silent = GTK_VALUE_BOOL (*arg);
163       break;
164       */
165     case ARG_CLOSED:
166       if (GTK_VALUE_BOOL (*arg) == TRUE)
167       {
168         /* close the file descriptor */
169         sink->opened = FALSE;
170         if (! (fclose (sink->file)))
171         {
172           g_warning ("Cannot close file !\n");
173         }
174       }
175       break;
176       
177     default:
178       break;
179   }
180 }
181
182 static void   
183 gst_disksink_get_arg (GtkObject *object, GtkArg *arg, guint id)
184 {
185   GstDiskSink *sink;
186  
187   /* it's not null if we got it, but it might not be ours */
188   g_return_if_fail (GST_IS_DISKSINK (object));
189  
190   sink = GST_DISKSINK (object);
191   
192   switch (id) {
193   /*
194     case ARG_SILENT:
195       GTK_VALUE_BOOL (*arg) = sink->silent;
196       break;
197       */
198     case ARG_CLOSED:
199       GTK_VALUE_BOOL (*arg) = !sink->opened;
200       break;
201     case ARG_LOCATION:
202       GTK_VALUE_STRING (*arg) = sink->filename;
203       break;
204     default:
205       arg->type = GTK_TYPE_INVALID;
206       break;
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   guint16 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 (!disksink->silent)
230     g_print("disksink: ******* (%s:%s)< \n",GST_DEBUG_PAD_NAME(pad));
231 */
232   if (disksink->opened)
233   {
234     bytes_written = fwrite (GST_BUFFER_DATA (buf), 1, GST_BUFFER_SIZE (buf), disksink->file);
235     if (bytes_written < GST_BUFFER_SIZE (buf))
236     {
237       printf ("disksink : Warning : %d bytes should be written, only %d bytes written\n",
238                   GST_BUFFER_SIZE (buf), bytes_written);
239     }
240   }
241   gst_buffer_unref (buf);
242
243   gtk_signal_emit (GTK_OBJECT (disksink), gst_disksink_signals[SIGNAL_HANDOFF],
244                               disksink);
245 }