Convert %lld and %llu in printf formats to G_G[U]INT64_FORMAT. Fix pointer<->int...
[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   "LGPL",
31   "Black hole for data",
32   VERSION,
33   "Erik Walthinsen <omega@cse.ogi.edu>",
34   "(C) 1999",
35 };
36
37
38 /* FakeSink signals and args */
39 enum {
40   /* FILL ME */
41   SIGNAL_HANDOFF,
42   LAST_SIGNAL
43 };
44
45 enum {
46   ARG_0,
47   ARG_NUM_SINKS,
48   ARG_SILENT,
49   ARG_DUMP,
50   ARG_SYNC,
51   ARG_LAST_MESSAGE,
52 };
53
54 GST_PAD_TEMPLATE_FACTORY (fakesink_sink_factory,
55   "sink%d",
56   GST_PAD_SINK,
57   GST_PAD_REQUEST,
58   NULL                  /* no caps */
59 );
60
61
62 static void     gst_fakesink_class_init         (GstFakeSinkClass *klass);
63 static void     gst_fakesink_init               (GstFakeSink *fakesink);
64
65 static void     gst_fakesink_set_clock          (GstElement *element, GstClock *clock);
66 static GstPad*  gst_fakesink_request_new_pad    (GstElement *element, GstPadTemplate *templ, const
67                                                  gchar *unused);
68
69 static void     gst_fakesink_set_property       (GObject *object, guint prop_id, 
70                                                  const GValue *value, GParamSpec *pspec);
71 static void     gst_fakesink_get_property       (GObject *object, guint prop_id, 
72                                                  GValue *value, GParamSpec *pspec);
73
74 static void     gst_fakesink_chain              (GstPad *pad, GstBuffer *buf);
75
76 static GstElementClass *parent_class = NULL;
77 static guint gst_fakesink_signals[LAST_SIGNAL] = { 0 };
78
79 GType
80 gst_fakesink_get_type (void) 
81 {
82   static GType fakesink_type = 0;
83
84   if (!fakesink_type) {
85     static const GTypeInfo fakesink_info = {
86       sizeof(GstFakeSinkClass),      NULL,
87       NULL,
88       (GClassInitFunc)gst_fakesink_class_init,
89       NULL,
90       NULL,
91       sizeof(GstFakeSink),
92       0,
93       (GInstanceInitFunc)gst_fakesink_init,
94     };
95     fakesink_type = g_type_register_static (GST_TYPE_ELEMENT, "GstFakeSink", &fakesink_info, 0);
96   }
97   return fakesink_type;
98 }
99
100 static void
101 gst_fakesink_class_init (GstFakeSinkClass *klass) 
102 {
103   GObjectClass *gobject_class;
104   GstElementClass *gstelement_class;
105
106   gobject_class = (GObjectClass*)klass;
107   gstelement_class = (GstElementClass*)klass;
108
109   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
110
111   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NUM_SINKS,
112     g_param_spec_int ("num_sinks", "Number of sinks", "The number of sinkpads",
113                       1, G_MAXINT, 1, G_PARAM_READABLE)); 
114   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LAST_MESSAGE,
115     g_param_spec_string ("last_message", "Last Message", "The message describing current status",
116                          NULL, G_PARAM_READABLE));
117   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SYNC,
118     g_param_spec_boolean ("sync", "Sync", "Sync on the clock",
119                           FALSE, G_PARAM_READWRITE)); 
120   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SILENT,
121     g_param_spec_boolean ("silent", "Silent", "Don't produce last_message events",
122                           FALSE, G_PARAM_READWRITE)); 
123   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DUMP,
124     g_param_spec_boolean ("dump", "Dump", "Dump received bytes to stdout",
125                           FALSE, G_PARAM_READWRITE)); 
126
127   gst_fakesink_signals[SIGNAL_HANDOFF] =
128     g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
129                     G_STRUCT_OFFSET (GstFakeSinkClass, handoff), NULL, NULL,
130                     g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
131                     G_TYPE_POINTER);
132
133   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_fakesink_set_property);
134   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_fakesink_get_property);
135
136   gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR (gst_fakesink_request_new_pad);
137   gstelement_class->set_clock       = GST_DEBUG_FUNCPTR (gst_fakesink_set_clock);
138 }
139
140 static void 
141 gst_fakesink_init (GstFakeSink *fakesink) 
142 {
143   GstPad *pad;
144
145   pad = gst_pad_new ("sink", GST_PAD_SINK);
146   gst_element_add_pad (GST_ELEMENT (fakesink), pad);
147   gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_fakesink_chain));
148
149   fakesink->silent = FALSE;
150   fakesink->dump = FALSE;
151   fakesink->sync = FALSE;
152   fakesink->last_message = NULL;
153
154   GST_FLAG_SET (fakesink, GST_ELEMENT_EVENT_AWARE);
155 }
156
157 static void
158 gst_fakesink_set_clock (GstElement *element, GstClock *clock)
159
160   GstFakeSink *sink;
161
162   sink = GST_FAKESINK (element);
163
164   sink->clock = clock;
165
166
167 static GstPad*
168 gst_fakesink_request_new_pad (GstElement *element, GstPadTemplate *templ, const gchar *unused)
169 {
170   gchar *name;
171   GstPad *sinkpad;
172   GstFakeSink *fakesink;
173
174   g_return_val_if_fail (GST_IS_FAKESINK (element), NULL);
175
176   if (templ->direction != GST_PAD_SINK) {
177     g_warning ("gstfakesink: request new pad that is not a SINK pad\n");
178     return NULL;
179   }
180
181   fakesink = GST_FAKESINK (element);
182
183   name = g_strdup_printf ("sink%d", GST_ELEMENT (fakesink)->numsinkpads);
184
185   sinkpad = gst_pad_new_from_template (templ, name);
186   g_free (name);
187   
188   gst_element_add_pad (GST_ELEMENT (fakesink), sinkpad);
189
190   return sinkpad;
191 }
192
193 static void
194 gst_fakesink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
195 {
196   GstFakeSink *sink;
197
198   /* it's not null if we got it, but it might not be ours */
199   sink = GST_FAKESINK (object);
200
201   switch (prop_id) {
202     case ARG_SILENT:
203       sink->silent = g_value_get_boolean (value);
204       break;
205     case ARG_DUMP:
206       sink->dump = g_value_get_boolean (value);
207       break;
208     case ARG_SYNC:
209       sink->sync = g_value_get_boolean (value);
210       break;
211     default:
212       break;
213   }
214 }
215
216 static void   
217 gst_fakesink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
218 {
219   GstFakeSink *sink;
220  
221   /* it's not null if we got it, but it might not be ours */
222   g_return_if_fail (GST_IS_FAKESINK (object));
223  
224   sink = GST_FAKESINK (object);
225   
226   switch (prop_id) {
227     case ARG_NUM_SINKS:
228       g_value_set_int (value, GST_ELEMENT (sink)->numsinkpads);
229       break;
230     case ARG_SILENT:
231       g_value_set_boolean (value, sink->silent);
232       break;
233     case ARG_DUMP:
234       g_value_set_boolean (value, sink->dump);
235       break;
236     case ARG_SYNC:
237       g_value_set_boolean (value, sink->sync);
238       break;
239     case ARG_LAST_MESSAGE:
240       g_value_set_string (value, sink->last_message);
241       break;
242     default:
243       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
244       break;
245   }
246 }
247
248 static void 
249 gst_fakesink_chain (GstPad *pad, GstBuffer *buf) 
250 {
251   GstFakeSink *fakesink;
252
253   g_return_if_fail (pad != NULL);
254   g_return_if_fail (GST_IS_PAD (pad));
255   g_return_if_fail (buf != NULL);
256
257   fakesink = GST_FAKESINK (gst_pad_get_parent (pad));
258
259   if (GST_IS_EVENT (buf)) {
260     GstEvent *event = GST_EVENT (buf);
261     
262     if (!fakesink->silent) { 
263       g_free (fakesink->last_message);
264
265       fakesink->last_message = g_strdup_printf ("chain   ******* (%s:%s)E (type: %d) %p",
266                 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE (event), event);
267     
268       g_object_notify (G_OBJECT (fakesink), "last_message");
269     }
270           
271     switch (GST_EVENT_TYPE (event)) {
272       case GST_EVENT_DISCONTINUOUS:
273         if (fakesink->sync && fakesink->clock) { 
274           gint64 value = GST_EVENT_DISCONT_OFFSET (event, 0).value;
275           gst_clock_handle_discont (fakesink->clock, value);
276         }
277       default:
278         gst_pad_event_default (pad, event);
279         break;
280     }
281     return;
282   }
283
284   if (fakesink->sync && fakesink->clock) { 
285     GstClockID id = gst_clock_new_single_shot_id (fakesink->clock, GST_BUFFER_TIMESTAMP (buf));
286
287     gst_element_clock_wait (GST_ELEMENT (fakesink), id, NULL);
288     gst_clock_id_free (id);
289   }
290
291   if (!fakesink->silent) { 
292     g_free (fakesink->last_message);
293
294     fakesink->last_message = g_strdup_printf ("chain   ******* (%s:%s)< (%d bytes, %"
295                                               G_GINT64_FORMAT ", %d) %p",
296                 GST_DEBUG_PAD_NAME (pad), GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf), 
297                 GST_BUFFER_FLAGS (buf), buf);
298     
299     g_object_notify (G_OBJECT (fakesink), "last_message");
300   }
301
302   g_signal_emit (G_OBJECT (fakesink), gst_fakesink_signals[SIGNAL_HANDOFF], 0, buf, pad);
303
304   if (fakesink->dump) {
305     gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
306   }
307
308   gst_buffer_unref (buf);
309 }
310
311 gboolean
312 gst_fakesink_factory_init (GstElementFactory *factory)
313 {
314   gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (fakesink_sink_factory));
315
316   return TRUE;
317 }