merge from EVENTS1 on 20011016
[platform/upstream/gstreamer.git] / plugins / elements / gstfakesink.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstfakesink.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 <gstfakesink.h>
25
26
27 GstElementDetails gst_fakesink_details = {
28   "Fake Sink",
29   "Sink",
30   "Black hole for data",
31   VERSION,
32   "Erik Walthinsen <omega@cse.ogi.edu>",
33   "(C) 1999",
34 };
35
36
37 /* FakeSink signals and args */
38 enum {
39   /* FILL ME */
40   SIGNAL_HANDOFF,
41   LAST_SIGNAL
42 };
43
44 enum {
45   ARG_0,
46   ARG_NUM_SINKS,
47   ARG_SILENT,
48   ARG_DUMP,
49 };
50
51 GST_PADTEMPLATE_FACTORY (fakesink_sink_factory,
52   "sink%d",
53   GST_PAD_SINK,
54   GST_PAD_REQUEST,
55   NULL                  /* no caps */
56 );
57
58
59 static void     gst_fakesink_class_init         (GstFakeSinkClass *klass);
60 static void     gst_fakesink_init               (GstFakeSink *fakesink);
61
62 static GstPad*  gst_fakesink_request_new_pad    (GstElement *element, GstPadTemplate *templ, const
63                                                  gchar *unused);
64
65 static void     gst_fakesink_set_property       (GObject *object, guint prop_id, 
66                                                  const GValue *value, GParamSpec *pspec);
67 static void     gst_fakesink_get_property       (GObject *object, guint prop_id, 
68                                                  GValue *value, GParamSpec *pspec);
69
70 static void     gst_fakesink_chain              (GstPad *pad, GstBuffer *buf);
71
72 static GstElementClass *parent_class = NULL;
73 static guint gst_fakesink_signals[LAST_SIGNAL] = { 0 };
74
75 GType
76 gst_fakesink_get_type (void) 
77 {
78   static GType fakesink_type = 0;
79
80   if (!fakesink_type) {
81     static const GTypeInfo fakesink_info = {
82       sizeof(GstFakeSinkClass),      NULL,
83       NULL,
84       (GClassInitFunc)gst_fakesink_class_init,
85       NULL,
86       NULL,
87       sizeof(GstFakeSink),
88       0,
89       (GInstanceInitFunc)gst_fakesink_init,
90     };
91     fakesink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFakeSink", &fakesink_info, 0);
92   }
93   return fakesink_type;
94 }
95
96 static void
97 gst_fakesink_class_init (GstFakeSinkClass *klass) 
98 {
99   GObjectClass *gobject_class;
100   GstElementClass *gstelement_class;
101
102   gobject_class = (GObjectClass*)klass;
103   gstelement_class = (GstElementClass*)klass;
104
105   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
106
107   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NUM_SINKS,
108     g_param_spec_int ("num_sinks", "num_sinks", "num_sinks",
109                       1, G_MAXINT, 1, G_PARAM_READABLE)); 
110   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SILENT,
111     g_param_spec_boolean ("silent", "silent", "silent",
112                           FALSE, G_PARAM_READWRITE)); 
113   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DUMP,
114     g_param_spec_boolean ("dump", "dump", "dump",
115                           FALSE, G_PARAM_READWRITE)); 
116
117   gst_fakesink_signals[SIGNAL_HANDOFF] =
118     g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
119                     G_STRUCT_OFFSET (GstFakeSinkClass, handoff), NULL, NULL,
120                     g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
121                     G_TYPE_POINTER);
122
123   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_fakesink_set_property);
124   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_fakesink_get_property);
125
126   gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR (gst_fakesink_request_new_pad);
127 }
128
129 static void 
130 gst_fakesink_init (GstFakeSink *fakesink) 
131 {
132   GstPad *pad;
133   pad = gst_pad_new ("sink", GST_PAD_SINK);
134   gst_element_add_pad (GST_ELEMENT (fakesink), pad);
135   gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_fakesink_chain));
136
137   fakesink->sinkpads = g_slist_prepend (NULL, pad);
138   fakesink->numsinkpads = 1;
139   fakesink->silent = FALSE;
140   fakesink->dump = FALSE;
141 }
142
143 static GstPad*
144 gst_fakesink_request_new_pad (GstElement *element, GstPadTemplate *templ, const gchar *unused)
145 {
146   gchar *name;
147   GstPad *sinkpad;
148   GstFakeSink *fakesink;
149
150   g_return_val_if_fail (GST_IS_FAKESINK (element), NULL);
151
152   if (templ->direction != GST_PAD_SINK) {
153     g_warning ("gstfakesink: request new pad that is not a SINK pad\n");
154     return NULL;
155   }
156
157   fakesink = GST_FAKESINK (element);
158
159   name = g_strdup_printf ("sink%d", fakesink->numsinkpads);
160
161   sinkpad = gst_pad_new_from_template (templ, name);
162   gst_element_add_pad (GST_ELEMENT (fakesink), sinkpad);
163
164   fakesink->sinkpads = g_slist_prepend (fakesink->sinkpads, sinkpad);
165   fakesink->numsinkpads++;
166
167   return sinkpad;
168 }
169
170 static void
171 gst_fakesink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
172 {
173   GstFakeSink *sink;
174
175   /* it's not null if we got it, but it might not be ours */
176   sink = GST_FAKESINK (object);
177
178   switch (prop_id) {
179     case ARG_SILENT:
180       sink->silent = g_value_get_boolean (value);
181       break;
182     case ARG_DUMP:
183       sink->dump = g_value_get_boolean (value);
184       break;
185     default:
186       break;
187   }
188 }
189
190 static void   
191 gst_fakesink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
192 {
193   GstFakeSink *sink;
194  
195   /* it's not null if we got it, but it might not be ours */
196   g_return_if_fail (GST_IS_FAKESINK (object));
197  
198   sink = GST_FAKESINK (object);
199   
200   switch (prop_id) {
201     case ARG_NUM_SINKS:
202       g_value_set_int (value, sink->numsinkpads);
203       break;
204     case ARG_SILENT:
205       g_value_set_boolean (value, sink->silent);
206       break;
207     case ARG_DUMP:
208       g_value_set_boolean (value, sink->dump);
209       break;
210     default:
211       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212       break;
213   }
214 }
215
216 /**
217  * gst_fakesink_chain:
218  * @pad: the pad this faksink is connected to
219  * @buffer: the buffer or event that has to be absorbed
220  *
221  * Take the buffer or event from the pad and unref it without doing
222  * anything with it.
223  */
224 static void 
225 gst_fakesink_chain (GstPad *pad, GstBuffer *buf) 
226 {
227   GstFakeSink *fakesink;
228
229   g_return_if_fail (pad != NULL);
230   g_return_if_fail (GST_IS_PAD (pad));
231   g_return_if_fail (buf != NULL);
232
233   fakesink = GST_FAKESINK (gst_pad_get_parent (pad));
234
235   if (GST_IS_EVENT(buf)) {
236     GstEvent *event = GST_EVENT (buf);
237
238     switch (GST_EVENT_TYPE (event)) {
239       case GST_EVENT_EOS:
240         g_print("fakesink: have EOS event!\n");
241         gst_element_set_state (GST_ELEMENT (fakesink), GST_STATE_PAUSED);
242         break;
243       default:
244         g_print("fakesink: have unhandled event!\n");
245         break;
246     }
247     gst_event_free (event);
248     return;
249   }
250
251   if (!fakesink->silent)
252     g_print("fakesink: chain   ******* (%s:%s)< (%d bytes, %lld) %p\n",
253                 GST_DEBUG_PAD_NAME (pad), GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf), buf);
254
255   g_signal_emit (G_OBJECT (fakesink), gst_fakesink_signals[SIGNAL_HANDOFF], 0, buf, pad);
256
257   if (fakesink->dump)
258   {
259     gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
260   }
261
262   gst_buffer_unref (buf);
263 }
264
265 gboolean
266 gst_fakesink_factory_init (GstElementFactory *factory)
267 {
268   gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (fakesink_sink_factory));
269
270   return TRUE;
271 }