70f358669cf211e8db28649d179b1a882e692b02
[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 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include "gstfdsink.h"
28 #include <unistd.h>
29
30 GST_DEBUG_CATEGORY_STATIC (gst_fdsink_debug);
31 #define GST_CAT_DEFAULT gst_fdsink_debug
32
33 GstElementDetails gst_fdsink_details = GST_ELEMENT_DETAILS (
34   "Filedescriptor Sink",
35   "Sink/File",
36   "Write data to a file descriptor",
37   "Erik Walthinsen <omega@cse.ogi.edu>"
38 );
39
40
41 /* FdSink signals and args */
42 enum {
43   /* FILL ME */
44   LAST_SIGNAL
45 };
46
47 enum {
48   ARG_0,
49   ARG_FD
50 };
51
52
53 #define _do_init(bla) \
54     GST_DEBUG_CATEGORY_INIT (gst_fdsink_debug, "fdsink", 0, "fdsink element");
55
56 GST_BOILERPLATE_FULL (GstFdSink, gst_fdsink, GstElement, GST_TYPE_ELEMENT, _do_init);
57
58 static void     gst_fdsink_set_property (GObject *object, guint prop_id, 
59                                          const GValue *value, GParamSpec *pspec);
60 static void     gst_fdsink_get_property (GObject *object, guint prop_id, 
61                                          GValue *value, GParamSpec *pspec);
62
63 static void     gst_fdsink_chain        (GstPad *pad,GstData *_data);
64
65
66 static void
67 gst_fdsink_base_init (gpointer g_class)
68 {
69   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
70   
71   gst_element_class_set_details (gstelement_class, &gst_fdsink_details);
72 }
73 static void
74 gst_fdsink_class_init (GstFdSinkClass *klass) 
75 {
76   GObjectClass *gobject_class;
77
78   gobject_class = G_OBJECT_CLASS (klass);
79
80
81   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FD,
82     g_param_spec_int ("fd", "fd", "An open file descriptor to write to",
83                       0, G_MAXINT, 1, G_PARAM_READWRITE));
84
85   gobject_class->set_property = gst_fdsink_set_property;
86   gobject_class->get_property = gst_fdsink_get_property;
87 }
88
89 static void 
90 gst_fdsink_init (GstFdSink *fdsink) 
91 {
92   fdsink->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
93   gst_element_add_pad (GST_ELEMENT (fdsink), fdsink->sinkpad);
94   gst_pad_set_chain_function (fdsink->sinkpad, gst_fdsink_chain);
95
96   fdsink->fd = 1;
97 }
98
99 static void 
100 gst_fdsink_chain (GstPad *pad, GstData *_data) 
101 {
102   GstBuffer *buf = GST_BUFFER (_data);
103   GstFdSink *fdsink;
104
105   g_return_if_fail (pad != NULL);
106   g_return_if_fail (GST_IS_PAD (pad));
107   g_return_if_fail (buf != NULL);
108
109   fdsink = GST_FDSINK (gst_pad_get_parent (pad));
110   
111   g_return_if_fail (fdsink->fd >= 0);
112   
113   if (GST_BUFFER_DATA (buf)) {
114     GST_DEBUG ("writing %d bytes to file descriptor %d",GST_BUFFER_SIZE (buf), fdsink->fd);
115     write (fdsink->fd, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
116   }
117   
118   gst_buffer_unref (buf);
119 }
120
121 static void 
122 gst_fdsink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) 
123 {
124   GstFdSink *fdsink;
125    
126   /* it's not null if we got it, but it might not be ours */
127   g_return_if_fail (GST_IS_FDSINK (object));
128   
129   fdsink = GST_FDSINK (object);
130
131   switch (prop_id) {
132     case ARG_FD:
133       fdsink->fd = g_value_get_int (value);
134       break;
135     default:
136       break;
137   }
138 }
139
140 static void 
141 gst_fdsink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) 
142 {
143   GstFdSink *fdsink;
144    
145   /* it's not null if we got it, but it might not be ours */
146   g_return_if_fail (GST_IS_FDSINK (object));
147   
148   fdsink = GST_FDSINK (object);
149
150   switch (prop_id) {
151     case ARG_FD:
152       g_value_set_int (value, fdsink->fd);
153       break;
154     default:
155       break;
156   }
157 }