d888f222e851aedb7bbbb2e10167fdd747aeee7c
[platform/upstream/gstreamer.git] / gst / elements / gstfdsink.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstfdsink.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 #include <gstfdsink.h>
24 #include <unistd.h>
25
26
27 GstElementDetails gst_fdsink_details = {
28   "Filedescriptor Sink",
29   "Sink/File",
30   "LGPL",
31   "Write data to a file descriptor",
32   VERSION,
33   "Erik Walthinsen <omega@cse.ogi.edu>",
34   "(C) 1999",
35 };
36
37
38 /* FdSink signals and args */
39 enum {
40   /* FILL ME */
41   LAST_SIGNAL
42 };
43
44 enum {
45   ARG_0,
46   ARG_FD
47 };
48
49
50 static void     gst_fdsink_class_init   (GstFdSinkClass *klass);
51 static void     gst_fdsink_init         (GstFdSink *fdsink);
52
53 static void     gst_fdsink_set_property (GObject *object, guint prop_id, 
54                                          const GValue *value, GParamSpec *pspec);
55 static void     gst_fdsink_get_property (GObject *object, guint prop_id, 
56                                          GValue *value, GParamSpec *pspec);
57
58 static void     gst_fdsink_chain        (GstPad *pad,GstBuffer *buf);
59
60 static GstElementClass *parent_class = NULL;
61 /*static guint gst_fdsink_signals[LAST_SIGNAL] = { 0 };*/
62
63 GType
64 gst_fdsink_get_type (void) 
65 {
66   static GType fdsink_type = 0;
67
68   if (!fdsink_type) {
69     static const GTypeInfo fdsink_info = {
70       sizeof(GstFdSinkClass),      NULL,
71       NULL,
72       (GClassInitFunc)gst_fdsink_class_init,
73       NULL,
74       NULL,
75       sizeof(GstFdSink),
76       0,
77       (GInstanceInitFunc)gst_fdsink_init,
78     };
79     fdsink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFdSink", &fdsink_info, 0);
80   }
81   return fdsink_type;
82 }
83
84 static void
85 gst_fdsink_class_init (GstFdSinkClass *klass) 
86 {
87   GObjectClass *gobject_class;
88
89   gobject_class = (GObjectClass*)klass;
90
91   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
92
93   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FD,
94     g_param_spec_int ("fd", "fd", "An open file descriptor to write to",
95                       0, G_MAXINT, 1, G_PARAM_READWRITE));
96
97   gobject_class->set_property = gst_fdsink_set_property;
98   gobject_class->get_property = gst_fdsink_get_property;
99 }
100
101 static void 
102 gst_fdsink_init (GstFdSink *fdsink) 
103 {
104   fdsink->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
105   gst_element_add_pad (GST_ELEMENT (fdsink), fdsink->sinkpad);
106   gst_pad_set_chain_function (fdsink->sinkpad, gst_fdsink_chain);
107
108   fdsink->fd = 1;
109 }
110
111 static void 
112 gst_fdsink_chain (GstPad *pad, GstBuffer *buf) 
113 {
114   GstFdSink *fdsink;
115
116   g_return_if_fail (pad != NULL);
117   g_return_if_fail (GST_IS_PAD (pad));
118   g_return_if_fail (buf != NULL);
119
120   fdsink = GST_FDSINK (gst_pad_get_parent (pad));
121   
122   g_return_if_fail (fdsink->fd >= 0);
123   
124   if (GST_BUFFER_DATA (buf)) {
125     GST_DEBUG (0,"writing %d bytes to file descriptor %d",GST_BUFFER_SIZE (buf), fdsink->fd);
126     write (fdsink->fd, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
127   }
128   
129   gst_buffer_unref (buf);
130 }
131
132 static void 
133 gst_fdsink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) 
134 {
135   GstFdSink *fdsink;
136    
137   /* it's not null if we got it, but it might not be ours */
138   g_return_if_fail (GST_IS_FDSINK (object));
139   
140   fdsink = GST_FDSINK (object);
141
142   switch (prop_id) {
143     case ARG_FD:
144       fdsink->fd = g_value_get_int (value);
145       break;
146     default:
147       break;
148   }
149 }
150
151 static void 
152 gst_fdsink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) 
153 {
154   GstFdSink *fdsink;
155    
156   /* it's not null if we got it, but it might not be ours */
157   g_return_if_fail (GST_IS_FDSINK (object));
158   
159   fdsink = GST_FDSINK (object);
160
161   switch (prop_id) {
162     case ARG_FD:
163       g_value_set_int (value, fdsink->fd);
164       break;
165     default:
166       break;
167   }
168 }