1 | /* GStreamer |
---|
2 | * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu> |
---|
3 | * 2000 Wim Taymans <wtay@chello.be> |
---|
4 | * |
---|
5 | * gstbstest.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 | #include <stdlib.h> |
---|
27 | #include <string.h> |
---|
28 | |
---|
29 | #include <gst/gst.h> |
---|
30 | #include <gst/bytestream/bytestream.h> |
---|
31 | |
---|
32 | #define GST_TYPE_BSTEST (gst_bstest_get_type()) |
---|
33 | #define GST_BSTEST(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BSTEST,GstBsTest)) |
---|
34 | #define GST_BSTEST_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_BSTEST,GstBsTestClass)) |
---|
35 | #define GST_IS_BSTEST(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BSTEST)) |
---|
36 | #define GST_IS_BSTEST_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_BSTEST)) |
---|
37 | |
---|
38 | typedef struct _GstBsTest GstBsTest; |
---|
39 | typedef struct _GstBsTestClass GstBsTestClass; |
---|
40 | |
---|
41 | struct _GstBsTest |
---|
42 | { |
---|
43 | GstElement element; |
---|
44 | |
---|
45 | GstPad *sinkpad; |
---|
46 | GstPad *srcpad; |
---|
47 | |
---|
48 | GstByteStream *bs; |
---|
49 | |
---|
50 | gchar *accesspattern; |
---|
51 | guint num_patterns; |
---|
52 | gchar **patterns; |
---|
53 | guint sizemin; |
---|
54 | guint sizemax; |
---|
55 | gint count; |
---|
56 | gboolean silent; |
---|
57 | }; |
---|
58 | |
---|
59 | struct _GstBsTestClass |
---|
60 | { |
---|
61 | GstElementClass parent_class; |
---|
62 | }; |
---|
63 | |
---|
64 | GType gst_bstest_get_type (void); |
---|
65 | |
---|
66 | |
---|
67 | GstElementDetails gst_bstest_details = GST_ELEMENT_DETAILS ("ByteStreamTest", |
---|
68 | "Filter", |
---|
69 | "Test for the GstByteStream code", |
---|
70 | "Erik Walthinsen <omega@temple-baptist.com>, " |
---|
71 | "Wim Taymans <wim.taymans@chello.be>"); |
---|
72 | |
---|
73 | |
---|
74 | /* BsTest signals and args */ |
---|
75 | enum |
---|
76 | { |
---|
77 | /* FILL ME */ |
---|
78 | LAST_SIGNAL |
---|
79 | }; |
---|
80 | |
---|
81 | enum |
---|
82 | { |
---|
83 | ARG_0, |
---|
84 | ARG_SIZEMIN, |
---|
85 | ARG_SIZEMAX, |
---|
86 | ARG_COUNT, |
---|
87 | ARG_SILENT, |
---|
88 | ARG_ACCESSPATTERN, |
---|
89 | }; |
---|
90 | |
---|
91 | |
---|
92 | static void gst_bstest_base_init (gpointer g_class); |
---|
93 | static void gst_bstest_class_init (GstBsTestClass * klass); |
---|
94 | static void gst_bstest_init (GstBsTest * bstest); |
---|
95 | |
---|
96 | static void gst_bstest_set_property (GObject * object, guint prop_id, |
---|
97 | const GValue * value, GParamSpec * pspec); |
---|
98 | static void gst_bstest_get_property (GObject * object, guint prop_id, |
---|
99 | GValue * value, GParamSpec * pspec); |
---|
100 | |
---|
101 | static GstElementStateReturn gst_bstest_change_state (GstElement * element); |
---|
102 | static void gst_bstest_loop (GstElement * element); |
---|
103 | |
---|
104 | static GstElementClass *parent_class = NULL; |
---|
105 | |
---|
106 | /* static guint gst_bstest_signals[LAST_SIGNAL] = { 0 }; */ |
---|
107 | |
---|
108 | GType |
---|
109 | gst_bstest_get_type (void) |
---|
110 | { |
---|
111 | static GType bstest_type = 0; |
---|
112 | |
---|
113 | if (!bstest_type) { |
---|
114 | static const GTypeInfo bstest_info = { |
---|
115 | sizeof (GstBsTestClass), |
---|
116 | gst_bstest_base_init, |
---|
117 | NULL, |
---|
118 | (GClassInitFunc) gst_bstest_class_init, |
---|
119 | NULL, |
---|
120 | NULL, |
---|
121 | sizeof (GstBsTest), |
---|
122 | 0, |
---|
123 | (GInstanceInitFunc) gst_bstest_init, |
---|
124 | }; |
---|
125 | |
---|
126 | bstest_type = |
---|
127 | g_type_register_static (GST_TYPE_ELEMENT, "BSTest", &bstest_info, 0); |
---|
128 | } |
---|
129 | return bstest_type; |
---|
130 | } |
---|
131 | static void |
---|
132 | gst_bstest_base_init (gpointer g_class) |
---|
133 | { |
---|
134 | GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class); |
---|
135 | |
---|
136 | gst_element_class_set_details (gstelement_class, &gst_bstest_details); |
---|
137 | } |
---|
138 | |
---|
139 | static void |
---|
140 | gst_bstest_class_init (GstBsTestClass * klass) |
---|
141 | { |
---|
142 | GObjectClass *gobject_class; |
---|
143 | GstElementClass *gstelement_class; |
---|
144 | |
---|
145 | gobject_class = (GObjectClass *) klass; |
---|
146 | gstelement_class = (GstElementClass *) klass; |
---|
147 | |
---|
148 | parent_class = g_type_class_ref (GST_TYPE_ELEMENT); |
---|
149 | |
---|
150 | g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SIZEMIN, |
---|
151 | g_param_spec_int ("sizemin", "sizemin", "sizemin", 0, G_MAXINT, |
---|
152 | 0, G_PARAM_READWRITE)); |
---|
153 | g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SIZEMAX, |
---|
154 | g_param_spec_int ("sizemax", "sizemax", "sizemax", 0, G_MAXINT, |
---|
155 | 384, G_PARAM_READWRITE)); |
---|
156 | g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ACCESSPATTERN, |
---|
157 | g_param_spec_string ("accesspattern", "accesspattern", "accesspattern", |
---|
158 | "r", G_PARAM_READWRITE)); |
---|
159 | g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_COUNT, |
---|
160 | g_param_spec_uint ("count", "count", "count", |
---|
161 | 0, G_MAXUINT, 0, G_PARAM_READWRITE)); |
---|
162 | g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SILENT, |
---|
163 | g_param_spec_boolean ("silent", "silent", "silent", |
---|
164 | FALSE, G_PARAM_READWRITE)); |
---|
165 | |
---|
166 | gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_bstest_set_property); |
---|
167 | gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_bstest_get_property); |
---|
168 | |
---|
169 | gstelement_class->change_state = gst_bstest_change_state; |
---|
170 | |
---|
171 | } |
---|
172 | |
---|
173 | static GstCaps * |
---|
174 | gst_bstest_getcaps (GstPad * pad) |
---|
175 | { |
---|
176 | GstBsTest *bstest = GST_BSTEST (gst_pad_get_parent (pad)); |
---|
177 | GstPad *otherpad; |
---|
178 | |
---|
179 | otherpad = (pad == bstest->srcpad) ? bstest->sinkpad : bstest->srcpad; |
---|
180 | |
---|
181 | return gst_pad_get_allowed_caps (otherpad); |
---|
182 | } |
---|
183 | |
---|
184 | GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src", |
---|
185 | GST_PAD_SRC, |
---|
186 | GST_PAD_ALWAYS, |
---|
187 | GST_STATIC_CAPS_ANY); |
---|
188 | |
---|
189 | GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", |
---|
190 | GST_PAD_SINK, |
---|
191 | GST_PAD_ALWAYS, |
---|
192 | GST_STATIC_CAPS_ANY); |
---|
193 | |
---|
194 | static void |
---|
195 | gst_bstest_init (GstBsTest * bstest) |
---|
196 | { |
---|
197 | bstest->sinkpad = |
---|
198 | gst_pad_new_from_template (gst_static_pad_template_get (&sinktemplate), |
---|
199 | "sink"); |
---|
200 | gst_element_add_pad (GST_ELEMENT (bstest), bstest->sinkpad); |
---|
201 | gst_pad_set_getcaps_function (bstest->sinkpad, gst_bstest_getcaps); |
---|
202 | |
---|
203 | bstest->srcpad = |
---|
204 | gst_pad_new_from_template (gst_static_pad_template_get (&srctemplate), |
---|
205 | "src"); |
---|
206 | gst_element_add_pad (GST_ELEMENT (bstest), bstest->srcpad); |
---|
207 | gst_pad_set_getcaps_function (bstest->srcpad, gst_bstest_getcaps); |
---|
208 | |
---|
209 | gst_element_set_loop_function (GST_ELEMENT (bstest), gst_bstest_loop); |
---|
210 | |
---|
211 | bstest->sizemin = 0; |
---|
212 | bstest->sizemax = 384; |
---|
213 | bstest->accesspattern = g_strdup ("r"); |
---|
214 | bstest->patterns = g_strsplit (bstest->accesspattern, ":", 0); |
---|
215 | bstest->count = 5; |
---|
216 | bstest->silent = FALSE; |
---|
217 | bstest->bs = NULL; |
---|
218 | } |
---|
219 | |
---|
220 | static guint |
---|
221 | gst_bstest_get_size (GstBsTest * bstest, gchar * sizestring, guint prevsize) |
---|
222 | { |
---|
223 | guint size; |
---|
224 | |
---|
225 | if (sizestring[0] == 0) { |
---|
226 | size = bstest->sizemax; |
---|
227 | } else if (sizestring[0] == 'r') { |
---|
228 | size = |
---|
229 | bstest->sizemin + |
---|
230 | (guint8) (((gfloat) bstest->sizemax) * rand () / (RAND_MAX + |
---|
231 | (gfloat) bstest->sizemin)); |
---|
232 | } else if (sizestring[0] == '<') { |
---|
233 | size = prevsize; |
---|
234 | } else { |
---|
235 | size = atoi (sizestring); |
---|
236 | } |
---|
237 | |
---|
238 | if (size == 0) |
---|
239 | size++; |
---|
240 | |
---|
241 | return size; |
---|
242 | } |
---|
243 | |
---|
244 | static void |
---|
245 | gst_bstest_loop (GstElement * element) |
---|
246 | { |
---|
247 | GstBsTest *bstest; |
---|
248 | GstBuffer *buf = NULL; |
---|
249 | |
---|
250 | g_return_if_fail (element != NULL); |
---|
251 | g_return_if_fail (GST_IS_BSTEST (element)); |
---|
252 | |
---|
253 | bstest = GST_BSTEST (element); |
---|
254 | |
---|
255 | do { |
---|
256 | guint size = 0; |
---|
257 | guint i = 0; |
---|
258 | guint8 *ptr; |
---|
259 | |
---|
260 | while (i < bstest->num_patterns) { |
---|
261 | buf = NULL; |
---|
262 | |
---|
263 | if (bstest->patterns[i][0] == 'r') { |
---|
264 | size = gst_bstest_get_size (bstest, &bstest->patterns[i][1], size); |
---|
265 | if (!bstest->silent) |
---|
266 | g_print ("bstest: ***** read %d bytes\n", size); |
---|
267 | gst_bytestream_read (bstest->bs, &buf, size); |
---|
268 | } else if (bstest->patterns[i][0] == 'f') { |
---|
269 | size = gst_bstest_get_size (bstest, &bstest->patterns[i][1], size); |
---|
270 | if (!bstest->silent) |
---|
271 | g_print ("bstest: ***** flush %d bytes\n", size); |
---|
272 | gst_bytestream_flush (bstest->bs, size); |
---|
273 | } else if (!strncmp (bstest->patterns[i], "pb", 2)) { |
---|
274 | size = gst_bstest_get_size (bstest, &bstest->patterns[i][2], size); |
---|
275 | if (!bstest->silent) |
---|
276 | g_print ("bstest: ***** peek bytes %d bytes\n", size); |
---|
277 | gst_bytestream_peek_bytes (bstest->bs, &ptr, size); |
---|
278 | } else if (bstest->patterns[i][0] == 'p') { |
---|
279 | size = gst_bstest_get_size (bstest, &bstest->patterns[i][1], size); |
---|
280 | if (!bstest->silent) |
---|
281 | g_print ("bstest: ***** peek %d bytes\n", size); |
---|
282 | gst_bytestream_peek (bstest->bs, &buf, size); |
---|
283 | gst_buffer_unref (buf); |
---|
284 | buf = NULL; |
---|
285 | } |
---|
286 | |
---|
287 | if (buf) |
---|
288 | gst_pad_push (bstest->srcpad, GST_DATA (buf)); |
---|
289 | |
---|
290 | i++; |
---|
291 | } |
---|
292 | /* } while (!GST_ELEMENT_IS_COTHREAD_STOPPING (element)); */ |
---|
293 | |
---|
294 | } while (0); |
---|
295 | } |
---|
296 | |
---|
297 | static void |
---|
298 | gst_bstest_set_property (GObject * object, guint prop_id, const GValue * value, |
---|
299 | GParamSpec * pspec) |
---|
300 | { |
---|
301 | GstBsTest *bstest; |
---|
302 | |
---|
303 | /* it's not null if we got it, but it might not be ours */ |
---|
304 | g_return_if_fail (GST_IS_BSTEST (object)); |
---|
305 | |
---|
306 | bstest = GST_BSTEST (object); |
---|
307 | |
---|
308 | switch (prop_id) { |
---|
309 | case ARG_SIZEMIN: |
---|
310 | bstest->sizemin = g_value_get_int (value); |
---|
311 | break; |
---|
312 | case ARG_SIZEMAX: |
---|
313 | bstest->sizemax = g_value_get_int (value); |
---|
314 | break; |
---|
315 | case ARG_ACCESSPATTERN: |
---|
316 | if (bstest->accesspattern) { |
---|
317 | g_free (bstest->accesspattern); |
---|
318 | g_strfreev (bstest->patterns); |
---|
319 | } |
---|
320 | if (g_value_get_string (value) == NULL) { |
---|
321 | gst_element_set_state (GST_ELEMENT (object), GST_STATE_NULL); |
---|
322 | bstest->accesspattern = NULL; |
---|
323 | bstest->num_patterns = 0; |
---|
324 | } else { |
---|
325 | guint i = 0; |
---|
326 | |
---|
327 | bstest->accesspattern = g_strdup (g_value_get_string (value)); |
---|
328 | bstest->patterns = g_strsplit (bstest->accesspattern, ":", 0); |
---|
329 | while (bstest->patterns[i++]); |
---|
330 | bstest->num_patterns = i - 1; |
---|
331 | } |
---|
332 | break; |
---|
333 | case ARG_COUNT: |
---|
334 | bstest->count = g_value_get_uint (value); |
---|
335 | break; |
---|
336 | case ARG_SILENT: |
---|
337 | bstest->silent = g_value_get_boolean (value); |
---|
338 | break; |
---|
339 | default: |
---|
340 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
---|
341 | break; |
---|
342 | } |
---|
343 | } |
---|
344 | |
---|
345 | static void |
---|
346 | gst_bstest_get_property (GObject * object, guint prop_id, GValue * value, |
---|
347 | GParamSpec * pspec) |
---|
348 | { |
---|
349 | GstBsTest *bstest; |
---|
350 | |
---|
351 | /* it's not null if we got it, but it might not be ours */ |
---|
352 | g_return_if_fail (GST_IS_BSTEST (object)); |
---|
353 | |
---|
354 | bstest = GST_BSTEST (object); |
---|
355 | |
---|
356 | switch (prop_id) { |
---|
357 | case ARG_SIZEMIN: |
---|
358 | g_value_set_int (value, bstest->sizemin); |
---|
359 | break; |
---|
360 | case ARG_SIZEMAX: |
---|
361 | g_value_set_int (value, bstest->sizemax); |
---|
362 | break; |
---|
363 | case ARG_ACCESSPATTERN: |
---|
364 | g_value_set_string (value, bstest->accesspattern); |
---|
365 | break; |
---|
366 | case ARG_COUNT: |
---|
367 | g_value_set_uint (value, bstest->count); |
---|
368 | break; |
---|
369 | case ARG_SILENT: |
---|
370 | g_value_set_boolean (value, bstest->silent); |
---|
371 | break; |
---|
372 | default: |
---|
373 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
---|
374 | break; |
---|
375 | } |
---|
376 | } |
---|
377 | |
---|
378 | static GstElementStateReturn |
---|
379 | gst_bstest_change_state (GstElement * element) |
---|
380 | { |
---|
381 | GstBsTest *bstest; |
---|
382 | |
---|
383 | g_return_val_if_fail (GST_IS_BSTEST (element), GST_STATE_FAILURE); |
---|
384 | |
---|
385 | bstest = GST_BSTEST (element); |
---|
386 | |
---|
387 | if (GST_STATE_PENDING (element) == GST_STATE_NULL) { |
---|
388 | if (bstest->bs) { |
---|
389 | gst_bytestream_destroy (bstest->bs); |
---|
390 | bstest->bs = NULL; |
---|
391 | } |
---|
392 | } else { |
---|
393 | if (!bstest->bs) { |
---|
394 | bstest->bs = gst_bytestream_new (bstest->sinkpad); |
---|
395 | } |
---|
396 | } |
---|
397 | |
---|
398 | if (GST_ELEMENT_CLASS (parent_class)->change_state) |
---|
399 | return GST_ELEMENT_CLASS (parent_class)->change_state (element); |
---|
400 | |
---|
401 | return GST_STATE_SUCCESS; |
---|
402 | } |
---|
403 | |
---|
404 | static gboolean |
---|
405 | plugin_init (GstPlugin * plugin) |
---|
406 | { |
---|
407 | /* We need to create an ElementFactory for each element we provide. |
---|
408 | * This consists of the name of the element, the GType identifier, |
---|
409 | * and a pointer to the details structure at the top of the file. |
---|
410 | */ |
---|
411 | return gst_element_register (plugin, "bstest", GST_RANK_PRIMARY, |
---|
412 | GST_TYPE_BSTEST); |
---|
413 | } |
---|
414 | |
---|
415 | GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, |
---|
416 | GST_VERSION_MINOR, |
---|
417 | "bstest", |
---|
418 | "test for the bytestream element", |
---|
419 | plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN) |
---|