make filesink handle events only in the chainfunction
[platform/upstream/gstreamer.git] / libs / gst / bytestream / bstest.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstidentity.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 <stdlib.h>
28 #include <gst/gst.h>
29 #include "bytestream.h"
30
31 #define GST_TYPE_IDENTITY \
32   (gst_identity_get_type())
33 #define GST_IDENTITY(obj) \
34   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_IDENTITY,GstIdentity))
35 #define GST_IDENTITY_CLASS(klass) \
36   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_IDENTITY,GstIdentityClass))
37 #define GST_IS_IDENTITY(obj) \
38   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_IDENTITY))
39 #define GST_IS_IDENTITY_CLASS(obj) \
40   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_IDENTITY))
41
42 typedef struct _GstIdentity GstIdentity;
43 typedef struct _GstIdentityClass GstIdentityClass;
44
45 struct _GstIdentity {
46   GstElement element;
47
48   GstPad *sinkpad;
49   GstPad *srcpad;
50
51   GstByteStream *bs;
52   gint byte_size;
53   gint count;
54 };
55
56 struct _GstIdentityClass {
57   GstElementClass parent_class;
58 };
59
60 GType gst_identity_get_type(void);
61
62
63 GstElementDetails gst_identity_details = {
64   "ByteStreamTest",
65   "Filter",
66   "Test for the GstByteStream code",
67   VERSION,
68   "Erik Walthinsen <omega@temple-baptist.com>",
69   "(C) 2001",
70 };
71
72
73 /* Identity signals and args */
74 enum {
75   /* FILL ME */
76   LAST_SIGNAL
77 };
78
79 enum {
80   ARG_0,
81   ARG_BYTE_SIZE,
82   ARG_COUNT,
83 };
84
85
86 static void gst_identity_class_init     (GstIdentityClass *klass);
87 static void gst_identity_init           (GstIdentity *identity);
88
89 static void gst_identity_set_property   (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
90 static void gst_identity_get_property   (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
91
92 static void gst_identity_loop           (GstElement *element);
93
94 static GstElementClass *parent_class = NULL;
95 /* static guint gst_identity_signals[LAST_SIGNAL] = { 0 }; */
96
97 GType
98 gst_identity_get_type (void) 
99 {
100   static GType identity_type = 0;
101
102   if (!identity_type) {
103     static const GTypeInfo identity_info = {
104       sizeof(GstIdentityClass),      NULL,
105       NULL,
106       (GClassInitFunc)gst_identity_class_init,
107       NULL,
108       NULL,
109       sizeof(GstIdentity),
110       0,
111       (GInstanceInitFunc)gst_identity_init,
112     };
113     identity_type = g_type_register_static (GST_TYPE_ELEMENT, "GstBSTest", &identity_info, 0);
114   }
115   return identity_type;
116 }
117
118 static void 
119 gst_identity_class_init (GstIdentityClass *klass) 
120 {
121   GObjectClass *gobject_class;
122
123   gobject_class = (GObjectClass*)klass;
124
125   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
126
127   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BYTE_SIZE,
128     g_param_spec_uint ("byte_size", "byte_size", "byte_size",
129                        0, G_MAXUINT, 0, G_PARAM_READWRITE));
130   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_COUNT,
131     g_param_spec_uint ("count", "count", "count",
132                        0, G_MAXUINT, 0, G_PARAM_READWRITE));
133
134   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_identity_set_property);  
135   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_identity_get_property);
136 }
137
138 /*
139 static GstPadNegotiateReturn
140 gst_identity_negotiate_src (GstPad *pad, GstCaps **caps, gpointer *data)
141 {
142   GstIdentity *identity;
143
144   identity = GST_IDENTITY (gst_pad_get_parent (pad));
145
146   return gst_pad_negotiate_proxy (pad, identity->sinkpad, caps);
147 }
148
149 static GstPadNegotiateReturn
150 gst_identity_negotiate_sink (GstPad *pad, GstCaps **caps, gpointer *data)
151 {
152   GstIdentity *identity;
153
154   identity = GST_IDENTITY (gst_pad_get_parent (pad));
155
156   return gst_pad_negotiate_proxy (pad, identity->srcpad, caps);
157 }
158 */
159
160 static void 
161 gst_identity_init (GstIdentity *identity) 
162 {
163   identity->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
164   gst_element_add_pad (GST_ELEMENT (identity), identity->sinkpad);
165   /*gst_pad_set_negotiate_function (identity->sinkpad, gst_identity_negotiate_sink); */
166   
167   identity->srcpad = gst_pad_new ("src", GST_PAD_SRC);
168   gst_element_add_pad (GST_ELEMENT (identity), identity->srcpad);
169   /*gst_pad_set_negotiate_function (identity->srcpad, gst_identity_negotiate_src); */
170
171   gst_element_set_loop_function (GST_ELEMENT (identity), gst_identity_loop);
172
173   identity->byte_size = 384;
174   identity->count = 5;
175
176   identity->bs = gst_bytestream_new(identity->sinkpad);
177 }
178
179 static void 
180 gst_identity_loop (GstElement *element) 
181 {
182   GstIdentity *identity;
183   GstBuffer *buf;
184   int i;
185
186   g_return_if_fail (element != NULL);
187   g_return_if_fail (GST_IS_IDENTITY (element));
188
189   identity = GST_IDENTITY (element);
190
191 /* THIS IS THE BUFFER BASED ONE
192   do {
193 *    g_print("\n"); *
194
195     for (i=0;i<identity->count;i++) {
196 *      g_print("bstest: getting a buffer of %d bytes\n",identity->byte_size); *
197       buf = gst_bytestream_read(identity->bs,identity->byte_size);
198       if (!buf) g_print("BUFFER IS BOGUS\n");
199 *      g_print("pushing the buffer, %d bytes at %d\n",GST_BUFFER_SIZE(buf),GST_BUFFER_OFFSET(buf)); *
200       gst_pad_push(identity->srcpad,buf);
201 *      g_print("\n"); *
202       gst_bytestream_print_status(identity->bs);
203 *      g_print("\n\n"); *
204     }
205
206     exit(1);
207   } while (!GST_ELEMENT_IS_COTHREAD_STOPPING(element));
208 */
209
210 /* THIS IS THE BYTE BASED ONE*/
211   do {
212     for (i=0;i<identity->count;i++) {
213       guint8 *data;
214       buf = gst_buffer_new();
215       /* note that this is dangerous, as it does *NOT* refcount the data, it can go away!!! */
216       GST_BUFFER_SIZE(buf) = gst_bytestream_peek_bytes(identity->bs, &data, identity->byte_size);
217       GST_BUFFER_DATA(buf) = data;
218       GST_BUFFER_FLAG_SET(buf,GST_BUFFER_DONTFREE);
219       gst_pad_push(identity->srcpad,buf);
220       gst_bytestream_flush(identity->bs,identity->byte_size);
221     }
222
223     exit(1);
224     /* implicit declaration of function `GST_ELEMENT_IS_COTHREAD_STOPPING' */
225     #define GST_ELEMENT_IS_COTHREAD_STOPPING(element) GST_FLAG_IS_SET((element), GST_ELEMENT_SCHEDULER_PRIVATE1)
226   } while (!GST_ELEMENT_IS_COTHREAD_STOPPING(element));
227 /**/
228 }
229
230 static void 
231 gst_identity_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) 
232 {
233   GstIdentity *identity;
234
235   /* it's not null if we got it, but it might not be ours */
236   g_return_if_fail (GST_IS_IDENTITY (object));
237   
238   identity = GST_IDENTITY (object);
239
240   switch (prop_id) {
241     case ARG_BYTE_SIZE:
242       identity->byte_size = g_value_get_uint (value);
243       break;
244     case ARG_COUNT:
245       identity->count = g_value_get_uint (value);
246       break;
247     default:
248       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
249       break;
250   }
251 }
252
253 static void gst_identity_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) {
254   GstIdentity *identity;
255
256   /* it's not null if we got it, but it might not be ours */
257   g_return_if_fail (GST_IS_IDENTITY (object));
258   
259   identity = GST_IDENTITY (object);
260
261   switch (prop_id) {
262     case ARG_BYTE_SIZE:
263       g_value_set_uint (value, identity->byte_size);
264       break;
265     case ARG_COUNT:
266       g_value_set_uint (value, identity->count);
267       break;
268     default:
269       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
270       break;
271   }
272 }
273
274 static gboolean
275 plugin_init (GModule *module, GstPlugin *plugin)
276 {
277   GstElementFactory *factory;
278
279   /* we need gstbytestream */
280   if (!gst_library_load ("gstbytestream")) {
281     g_print("can't load bytestream\n");
282     return FALSE;
283   }
284   
285   /* We need to create an ElementFactory for each element we provide.
286    * This consists of the name of the element, the GType identifier,
287    * and a pointer to the details structure at the top of the file.
288    */
289   factory = gst_element_factory_new("gstbstest", GST_TYPE_IDENTITY, &gst_identity_details);
290   g_return_val_if_fail(factory != NULL, FALSE);
291
292   /* The very last thing is to register the elementfactory with the plugin. */
293   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
294  
295   return TRUE;
296 }
297
298 GstPluginDesc plugin_desc = {
299   GST_VERSION_MAJOR,
300   GST_VERSION_MINOR,
301   "gstbstest",
302   plugin_init
303 };
304