Small cleanups
[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   ARG_SYNC,
50   ARG_LAST_MESSAGE,
51 };
52
53 GST_PAD_TEMPLATE_FACTORY (fakesink_sink_factory,
54   "sink%d",
55   GST_PAD_SINK,
56   GST_PAD_REQUEST,
57   NULL                  /* no caps */
58 );
59
60
61 static void     gst_fakesink_class_init         (GstFakeSinkClass *klass);
62 static void     gst_fakesink_init               (GstFakeSink *fakesink);
63
64 static void     gst_fakesink_set_clock          (GstElement *element, GstClock *clock);
65 static GstPad*  gst_fakesink_request_new_pad    (GstElement *element, GstPadTemplate *templ, const
66                                                  gchar *unused);
67
68 static void     gst_fakesink_set_property       (GObject *object, guint prop_id, 
69                                                  const GValue *value, GParamSpec *pspec);
70 static void     gst_fakesink_get_property       (GObject *object, guint prop_id, 
71                                                  GValue *value, GParamSpec *pspec);
72
73 static void     gst_fakesink_chain              (GstPad *pad, GstBuffer *buf);
74
75 static GstElementClass *parent_class = NULL;
76 static guint gst_fakesink_signals[LAST_SIGNAL] = { 0 };
77
78 GType
79 gst_fakesink_get_type (void) 
80 {
81   static GType fakesink_type = 0;
82
83   if (!fakesink_type) {
84     static const GTypeInfo fakesink_info = {
85       sizeof(GstFakeSinkClass),      NULL,
86       NULL,
87       (GClassInitFunc)gst_fakesink_class_init,
88       NULL,
89       NULL,
90       sizeof(GstFakeSink),
91       0,
92       (GInstanceInitFunc)gst_fakesink_init,
93     };
94     fakesink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFakeSink", &fakesink_info, 0);
95   }
96   return fakesink_type;
97 }
98
99 static void
100 gst_fakesink_class_init (GstFakeSinkClass *klass) 
101 {
102   GObjectClass *gobject_class;
103   GstElementClass *gstelement_class;
104
105   gobject_class = (GObjectClass*)klass;
106   gstelement_class = (GstElementClass*)klass;
107
108   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
109
110   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NUM_SINKS,
111     g_param_spec_int ("num_sinks", "Number of sinks", "The number of sinkpads",
112                       1, G_MAXINT, 1, G_PARAM_READABLE)); 
113   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LAST_MESSAGE,
114     g_param_spec_string ("last_message", "Last Message", "The message describing current status",
115                          NULL, G_PARAM_READABLE));
116   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_SYNC,
117     g_param_spec_boolean("sync","Sync","Sync on the clock",
118                          FALSE, G_PARAM_READWRITE)); /* CHECKME */
119
120   gst_element_class_install_std_props (
121           GST_ELEMENT_CLASS (klass),
122           "silent", ARG_SILENT, G_PARAM_READWRITE,
123           "dump",   ARG_DUMP,   G_PARAM_READWRITE,
124           NULL);
125
126   gst_fakesink_signals[SIGNAL_HANDOFF] =
127     g_signal_new ("handoff", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
128                     G_STRUCT_OFFSET (GstFakeSinkClass, handoff), NULL, NULL,
129                     g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
130                     G_TYPE_POINTER);
131
132   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_fakesink_set_property);
133   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_fakesink_get_property);
134
135   gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR (gst_fakesink_request_new_pad);
136 }
137
138 static void 
139 gst_fakesink_init (GstFakeSink *fakesink) 
140 {
141   GstPad *pad;
142   pad = gst_pad_new ("sink", GST_PAD_SINK);
143   gst_element_add_pad (GST_ELEMENT (fakesink), pad);
144   gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_fakesink_chain));
145
146   fakesink->silent = FALSE;
147   fakesink->dump = FALSE;
148   fakesink->sync = FALSE;
149   fakesink->last_message = NULL;
150
151   GST_ELEMENT (fakesink)->setclockfunc    = gst_fakesink_set_clock;
152 }
153
154 static void
155 gst_fakesink_set_clock (GstElement *element, GstClock *clock)
156
157   GstFakeSink *sink;
158
159   sink = GST_FAKESINK (element);
160
161   sink->clock = clock;
162
163
164 static GstPad*
165 gst_fakesink_request_new_pad (GstElement *element, GstPadTemplate *templ, const gchar *unused)
166 {
167   gchar *name;
168   GstPad *sinkpad;
169   GstFakeSink *fakesink;
170
171   g_return_val_if_fail (GST_IS_FAKESINK (element), NULL);
172
173   if (templ->direction != GST_PAD_SINK) {
174     g_warning ("gstfakesink: request new pad that is not a SINK pad\n");
175     return NULL;
176   }
177
178   fakesink = GST_FAKESINK (element);
179
180   name = g_strdup_printf ("sink%d", GST_ELEMENT (fakesink)->numsinkpads);
181
182   sinkpad = gst_pad_new_from_template (templ, name);
183   gst_element_add_pad (GST_ELEMENT (fakesink), sinkpad);
184
185   return sinkpad;
186 }
187
188 static void
189 gst_fakesink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
190 {
191   GstFakeSink *sink;
192
193   /* it's not null if we got it, but it might not be ours */
194   sink = GST_FAKESINK (object);
195
196   switch (prop_id) {
197     case ARG_SILENT:
198       sink->silent = g_value_get_boolean (value);
199       break;
200     case ARG_DUMP:
201       sink->dump = g_value_get_boolean (value);
202       break;
203     case ARG_SYNC:
204       sink->sync = g_value_get_boolean (value);
205       break;
206     default:
207       break;
208   }
209 }
210
211 static void   
212 gst_fakesink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
213 {
214   GstFakeSink *sink;
215  
216   /* it's not null if we got it, but it might not be ours */
217   g_return_if_fail (GST_IS_FAKESINK (object));
218  
219   sink = GST_FAKESINK (object);
220   
221   switch (prop_id) {
222     case ARG_NUM_SINKS:
223       g_value_set_int (value, GST_ELEMENT (sink)->numsinkpads);
224       break;
225     case ARG_SILENT:
226       g_value_set_boolean (value, sink->silent);
227       break;
228     case ARG_DUMP:
229       g_value_set_boolean (value, sink->dump);
230       break;
231     case ARG_SYNC:
232       g_value_set_boolean (value, sink->sync);
233       break;
234     case ARG_LAST_MESSAGE:
235       g_value_set_string (value, sink->last_message);
236       break;
237     default:
238       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
239       break;
240   }
241 }
242
243 static void 
244 gst_fakesink_chain (GstPad *pad, GstBuffer *buf) 
245 {
246   GstFakeSink *fakesink;
247
248   g_return_if_fail (pad != NULL);
249   g_return_if_fail (GST_IS_PAD (pad));
250   g_return_if_fail (buf != NULL);
251
252   fakesink = GST_FAKESINK (gst_pad_get_parent (pad));
253
254   if (fakesink->sync) { 
255     gst_element_clock_wait (GST_ELEMENT (fakesink), fakesink->clock, GST_BUFFER_TIMESTAMP (buf));
256   }
257
258   if (!fakesink->silent) { 
259     g_free (fakesink->last_message);
260
261     fakesink->last_message = g_strdup_printf ("chain   ******* (%s:%s)< (%d bytes, %lld) %p",
262                 GST_DEBUG_PAD_NAME (pad), GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf), buf);
263     
264     g_object_notify (G_OBJECT (fakesink), "last_message");
265   }
266
267   g_signal_emit (G_OBJECT (fakesink), gst_fakesink_signals[SIGNAL_HANDOFF], 0, buf, pad);
268
269   if (fakesink->dump) {
270     gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
271   }
272
273   gst_buffer_unref (buf);
274 }
275
276 gboolean
277 gst_fakesink_factory_init (GstElementFactory *factory)
278 {
279   gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (fakesink_sink_factory));
280
281   return TRUE;
282 }