Initialize Tizen 2.3
[framework/multimedia/gst-plugins-base0.10.git] / tests / check / elements / audiorate.c
1 /* GStreamer unit tests for audiorate
2  *
3  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <gst/check/gstcheck.h>
26
27 /* helper element to insert additional buffers overlapping with previous ones */
28 static gdouble injector_inject_probability = 0.0;
29
30 typedef GstElement TestInjector;
31 typedef GstElementClass TestInjectorClass;
32
33 GType test_injector_get_type (void);
34 GST_BOILERPLATE (TestInjector, test_injector, GstElement, GST_TYPE_ELEMENT);
35
36 #define INJECTOR_CAPS \
37   "audio/x-raw-float, "                                  \
38     "rate = (int) [ 1, MAX ], "                          \
39     "channels = (int) [ 1, 8 ], "                        \
40     "endianness = (int) BYTE_ORDER, "                    \
41     "width = (int) 32;"                                  \
42   "audio/x-raw-int, "                                    \
43     "rate = (int) [ 1, MAX ], "                          \
44     "channels = (int) [ 1, 8 ], "                        \
45     "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
46     "width = (int) { 8, 16, 32 }, "                      \
47     "depth = (int) [ 1, 32 ], "                          \
48     "signed = (boolean) { true, false }"
49
50 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS (INJECTOR_CAPS));
54
55 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
56     GST_PAD_SINK,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS (INJECTOR_CAPS));
59
60 static void
61 test_injector_base_init (gpointer g_class)
62 {
63   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
64
65   gst_element_class_add_static_pad_template (element_class, &src_template);
66   gst_element_class_add_static_pad_template (element_class,
67       &sink_template);
68 }
69
70 static void
71 test_injector_class_init (TestInjectorClass * klass)
72 {
73   /* nothing to do here */
74 }
75
76 static GstFlowReturn
77 test_injector_chain (GstPad * pad, GstBuffer * buf)
78 {
79   GstFlowReturn ret;
80   GstPad *srcpad;
81
82   srcpad =
83       gst_element_get_static_pad (GST_ELEMENT (GST_PAD_PARENT (pad)), "src");
84
85   /* since we're increasing timestamp/offsets, push this one first */
86   GST_LOG (" passing buffer   [t=%" GST_TIME_FORMAT "-%" GST_TIME_FORMAT
87       "], offset=%" G_GINT64_FORMAT ", offset_end=%" G_GINT64_FORMAT,
88       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
89       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf)),
90       GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf));
91
92   gst_buffer_ref (buf);
93
94   ret = gst_pad_push (srcpad, buf);
95
96   if (g_random_double () < injector_inject_probability) {
97     GstBuffer *ibuf;
98
99     ibuf = gst_buffer_copy (buf);
100
101     if (GST_BUFFER_OFFSET_IS_VALID (buf) &&
102         GST_BUFFER_OFFSET_END_IS_VALID (buf)) {
103       guint64 delta;
104
105       delta = GST_BUFFER_OFFSET_END (buf) - GST_BUFFER_OFFSET (buf);
106       GST_BUFFER_OFFSET (ibuf) += delta / 4;
107       GST_BUFFER_OFFSET_END (ibuf) += delta / 4;
108     } else {
109       GST_BUFFER_OFFSET (ibuf) = GST_BUFFER_OFFSET_NONE;
110       GST_BUFFER_OFFSET_END (ibuf) = GST_BUFFER_OFFSET_NONE;
111     }
112
113     if (GST_BUFFER_TIMESTAMP_IS_VALID (buf) &&
114         GST_BUFFER_DURATION_IS_VALID (buf)) {
115       GstClockTime delta;
116
117       delta = GST_BUFFER_DURATION (buf);
118       GST_BUFFER_TIMESTAMP (ibuf) += delta / 4;
119     } else {
120       GST_BUFFER_TIMESTAMP (ibuf) = GST_CLOCK_TIME_NONE;
121       GST_BUFFER_DURATION (ibuf) = GST_CLOCK_TIME_NONE;
122     }
123
124     if (GST_BUFFER_TIMESTAMP_IS_VALID (ibuf) ||
125         GST_BUFFER_OFFSET_IS_VALID (ibuf)) {
126       GST_LOG ("injecting buffer [t=%" GST_TIME_FORMAT "-%" GST_TIME_FORMAT
127           "], offset=%" G_GINT64_FORMAT ", offset_end=%" G_GINT64_FORMAT,
128           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (ibuf)),
129           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (ibuf) +
130               GST_BUFFER_DURATION (ibuf)), GST_BUFFER_OFFSET (ibuf),
131           GST_BUFFER_OFFSET_END (ibuf));
132
133       if (gst_pad_push (srcpad, ibuf) != GST_FLOW_OK) {
134         /* ignore return value */
135       }
136     } else {
137       GST_WARNING ("couldn't inject buffer, no incoming timestamps or offsets");
138       gst_buffer_unref (ibuf);
139     }
140   }
141
142   gst_buffer_unref (buf);
143
144   return ret;
145 }
146
147 static void
148 test_injector_init (TestInjector * injector, TestInjectorClass * klass)
149 {
150   GstPad *pad;
151
152   pad = gst_pad_new_from_static_template (&sink_template, "sink");
153   gst_pad_set_chain_function (pad, test_injector_chain);
154   gst_pad_set_getcaps_function (pad, gst_pad_proxy_getcaps);
155   gst_pad_set_setcaps_function (pad, gst_pad_proxy_setcaps);
156   gst_element_add_pad (GST_ELEMENT (injector), pad);
157
158   pad = gst_pad_new_from_static_template (&src_template, "src");
159   gst_pad_set_getcaps_function (pad, gst_pad_proxy_getcaps);
160   gst_pad_set_setcaps_function (pad, gst_pad_proxy_setcaps);
161   gst_element_add_pad (GST_ELEMENT (injector), pad);
162 }
163
164 static gboolean
165 probe_cb (GstPad * pad, GstBuffer * buf, gdouble * drop_probability)
166 {
167   if (g_random_double () < *drop_probability) {
168     GST_LOG ("dropping buffer [t=%" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "], "
169         "offset=%" G_GINT64_FORMAT ", offset_end=%" G_GINT64_FORMAT,
170         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
171         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf)),
172         GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf));
173     return FALSE;               /* drop buffer */
174   }
175
176   return TRUE;                  /* don't drop buffer */
177 }
178
179 static void
180 got_buf (GstElement * fakesink, GstBuffer * buf, GstPad * pad, GList ** p_bufs)
181 {
182   *p_bufs = g_list_append (*p_bufs, gst_buffer_ref (buf));
183 }
184
185 static void
186 do_perfect_stream_test (guint rate, guint width, gdouble drop_probability,
187     gdouble inject_probability)
188 {
189   GstElement *pipe, *src, *conv, *filter, *injector, *audiorate, *sink;
190   GstMessage *msg;
191   GstCaps *caps;
192   GstPad *srcpad;
193   GList *l, *bufs = NULL;
194   GstClockTime next_time = GST_CLOCK_TIME_NONE;
195   guint64 next_offset = GST_BUFFER_OFFSET_NONE;
196
197   caps = gst_caps_new_simple ("audio/x-raw-int", "rate", G_TYPE_INT,
198       rate, "width", G_TYPE_INT, width, NULL);
199
200   GST_INFO ("-------- drop=%.0f%% caps = %" GST_PTR_FORMAT " ---------- ",
201       drop_probability * 100.0, caps);
202
203   g_assert (drop_probability >= 0.0 && drop_probability <= 1.0);
204   g_assert (inject_probability >= 0.0 && inject_probability <= 1.0);
205   g_assert (width > 0 && (width % 8) == 0);
206
207   pipe = gst_pipeline_new ("pipeline");
208   fail_unless (pipe != NULL);
209
210   src = gst_element_factory_make ("audiotestsrc", "audiotestsrc");
211   fail_unless (src != NULL);
212
213   g_object_set (src, "num-buffers", 100, NULL);
214
215   conv = gst_element_factory_make ("audioconvert", "audioconvert");
216   fail_unless (conv != NULL);
217
218   filter = gst_element_factory_make ("capsfilter", "capsfilter");
219   fail_unless (filter != NULL);
220
221   g_object_set (filter, "caps", caps, NULL);
222
223   injector_inject_probability = inject_probability;
224
225   injector = GST_ELEMENT (g_object_new (test_injector_get_type (), NULL));
226
227   srcpad = gst_element_get_static_pad (injector, "src");
228   fail_unless (srcpad != NULL);
229   gst_pad_add_buffer_probe (srcpad, G_CALLBACK (probe_cb), &drop_probability);
230   gst_object_unref (srcpad);
231
232   audiorate = gst_element_factory_make ("audiorate", "audiorate");
233   fail_unless (audiorate != NULL);
234
235   sink = gst_element_factory_make ("fakesink", "fakesink");
236   fail_unless (sink != NULL);
237
238   g_object_set (sink, "signal-handoffs", TRUE, NULL);
239
240   g_signal_connect (sink, "handoff", G_CALLBACK (got_buf), &bufs);
241
242   gst_bin_add_many (GST_BIN (pipe), src, conv, filter, injector, audiorate,
243       sink, NULL);
244   gst_element_link_many (src, conv, filter, injector, audiorate, sink, NULL);
245
246   fail_unless_equals_int (gst_element_set_state (pipe, GST_STATE_PLAYING),
247       GST_STATE_CHANGE_ASYNC);
248
249   fail_unless_equals_int (gst_element_get_state (pipe, NULL, NULL, -1),
250       GST_STATE_CHANGE_SUCCESS);
251
252   msg = gst_bus_poll (GST_ELEMENT_BUS (pipe),
253       GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
254   fail_unless_equals_string (GST_MESSAGE_TYPE_NAME (msg), "eos");
255
256   for (l = bufs; l != NULL; l = l->next) {
257     GstBuffer *buf = GST_BUFFER (l->data);
258     guint num_samples;
259
260     fail_unless (GST_BUFFER_TIMESTAMP_IS_VALID (buf));
261     fail_unless (GST_BUFFER_DURATION_IS_VALID (buf));
262     fail_unless (GST_BUFFER_OFFSET_IS_VALID (buf));
263     fail_unless (GST_BUFFER_OFFSET_END_IS_VALID (buf));
264
265     GST_LOG ("buffer: ts=%" GST_TIME_FORMAT ", end_ts=%" GST_TIME_FORMAT
266         " off=%" G_GINT64_FORMAT ", end_off=%" G_GINT64_FORMAT,
267         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
268         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf)),
269         GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf));
270
271     if (GST_CLOCK_TIME_IS_VALID (next_time)) {
272       fail_unless_equals_uint64 (next_time, GST_BUFFER_TIMESTAMP (buf));
273     }
274     if (next_offset != GST_BUFFER_OFFSET_NONE) {
275       fail_unless_equals_uint64 (next_offset, GST_BUFFER_OFFSET (buf));
276     }
277
278     /* check buffer size for sanity */
279     fail_unless_equals_int (GST_BUFFER_SIZE (buf) % (width / 8), 0);
280
281     /* check there is actually as much data as there should be */
282     num_samples = GST_BUFFER_OFFSET_END (buf) - GST_BUFFER_OFFSET (buf);
283     fail_unless_equals_int (GST_BUFFER_SIZE (buf), num_samples * (width / 8));
284
285     next_time = GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf);
286     next_offset = GST_BUFFER_OFFSET_END (buf);
287   }
288
289   gst_message_unref (msg);
290   gst_element_set_state (pipe, GST_STATE_NULL);
291   gst_object_unref (pipe);
292
293   g_list_foreach (bufs, (GFunc) gst_mini_object_unref, NULL);
294   g_list_free (bufs);
295
296   gst_caps_unref (caps);
297 }
298
299 static const guint rates[] = { 8000, 11025, 16000, 22050, 32000, 44100,
300   48000, 3333, 33333, 66666, 9999
301 };
302
303 GST_START_TEST (test_perfect_stream_drop0)
304 {
305   guint i;
306
307   for (i = 0; i < G_N_ELEMENTS (rates); ++i) {
308     do_perfect_stream_test (rates[i], 8, 0.0, 0.0);
309     do_perfect_stream_test (rates[i], 16, 0.0, 0.0);
310   }
311 }
312
313 GST_END_TEST;
314
315 GST_START_TEST (test_perfect_stream_drop10)
316 {
317   guint i;
318
319   for (i = 0; i < G_N_ELEMENTS (rates); ++i) {
320     do_perfect_stream_test (rates[i], 8, 0.10, 0.0);
321     do_perfect_stream_test (rates[i], 16, 0.10, 0.0);
322   }
323 }
324
325 GST_END_TEST;
326
327 GST_START_TEST (test_perfect_stream_drop50)
328 {
329   guint i;
330
331   for (i = 0; i < G_N_ELEMENTS (rates); ++i) {
332     do_perfect_stream_test (rates[i], 8, 0.50, 0.0);
333     do_perfect_stream_test (rates[i], 16, 0.50, 0.0);
334   }
335 }
336
337 GST_END_TEST;
338
339 GST_START_TEST (test_perfect_stream_drop90)
340 {
341   guint i;
342
343   for (i = 0; i < G_N_ELEMENTS (rates); ++i) {
344     do_perfect_stream_test (rates[i], 8, 0.90, 0.0);
345     do_perfect_stream_test (rates[i], 16, 0.90, 0.0);
346   }
347 }
348
349 GST_END_TEST;
350
351 GST_START_TEST (test_perfect_stream_inject10)
352 {
353   guint i;
354
355   for (i = 0; i < G_N_ELEMENTS (rates); ++i) {
356     do_perfect_stream_test (rates[i], 8, 0.0, 0.10);
357     do_perfect_stream_test (rates[i], 16, 0.0, 0.10);
358   }
359 }
360
361 GST_END_TEST;
362
363 GST_START_TEST (test_perfect_stream_inject90)
364 {
365   guint i;
366
367   for (i = 0; i < G_N_ELEMENTS (rates); ++i) {
368     do_perfect_stream_test (rates[i], 8, 0.0, 0.90);
369     do_perfect_stream_test (rates[i], 16, 0.0, 0.90);
370   }
371 }
372
373 GST_END_TEST;
374
375 GST_START_TEST (test_perfect_stream_drop45_inject25)
376 {
377   guint i;
378
379   for (i = 0; i < G_N_ELEMENTS (rates); ++i) {
380     do_perfect_stream_test (rates[i], 8, 0.45, 0.25);
381     do_perfect_stream_test (rates[i], 16, 0.45, 0.25);
382   }
383 }
384
385 GST_END_TEST;
386
387 /* TODO: also do all tests with channels=1 and channels=2 */
388
389 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
390     GST_PAD_SRC,
391     GST_PAD_ALWAYS,
392     GST_STATIC_CAPS ("audio/x-raw-float,channels=1,rate=44100,width=32")
393     );
394
395 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
396     GST_PAD_SINK,
397     GST_PAD_ALWAYS,
398     GST_STATIC_CAPS ("audio/x-raw-float,channels=1,rate=44100,width=32")
399     );
400
401 GST_START_TEST (test_large_discont)
402 {
403   GstElement *audiorate;
404   GstCaps *caps;
405   GstPad *srcpad, *sinkpad;
406   GstBuffer *buf;
407
408   audiorate = gst_check_setup_element ("audiorate");
409   caps = gst_caps_new_simple ("audio/x-raw-float",
410       "channels", G_TYPE_INT, 1,
411       "rate", G_TYPE_INT, 44100, "width", G_TYPE_INT, 32,
412       "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
413
414   srcpad = gst_check_setup_src_pad (audiorate, &srctemplate, caps);
415   sinkpad = gst_check_setup_sink_pad (audiorate, &sinktemplate, caps);
416
417   gst_pad_set_active (srcpad, TRUE);
418   gst_pad_set_active (sinkpad, TRUE);
419
420   fail_unless (gst_element_set_state (audiorate,
421           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
422       "failed to set audiorate playing");
423
424   buf = gst_buffer_new_and_alloc (4);
425   gst_buffer_set_caps (buf, caps);
426   GST_BUFFER_TIMESTAMP (buf) = 0;
427   gst_pad_push (srcpad, buf);
428
429   fail_unless_equals_int (g_list_length (buffers), 1);
430
431   buf = gst_buffer_new_and_alloc (4);
432   gst_buffer_set_caps (buf, caps);
433   GST_BUFFER_TIMESTAMP (buf) = 2 * GST_SECOND;
434   gst_pad_push (srcpad, buf);
435   /* Now we should have 3 more buffers: the one we injected, plus _two_ filler
436    * buffers, because the gap is > 1 second (but less than 2 seconds) */
437   fail_unless_equals_int (g_list_length (buffers), 4);
438
439   gst_element_set_state (audiorate, GST_STATE_NULL);
440   gst_caps_unref (caps);
441
442   gst_check_teardown_sink_pad (audiorate);
443   gst_check_teardown_src_pad (audiorate);
444
445   gst_object_unref (audiorate);
446 }
447
448 GST_END_TEST;
449
450 static Suite *
451 audiorate_suite (void)
452 {
453   Suite *s = suite_create ("audiorate");
454   TCase *tc_chain = tcase_create ("general");
455
456   suite_add_tcase (s, tc_chain);
457
458   tcase_add_test (tc_chain, test_perfect_stream_drop0);
459   tcase_add_test (tc_chain, test_perfect_stream_drop10);
460   tcase_add_test (tc_chain, test_perfect_stream_drop50);
461   tcase_add_test (tc_chain, test_perfect_stream_drop90);
462   tcase_add_test (tc_chain, test_perfect_stream_inject10);
463   tcase_add_test (tc_chain, test_perfect_stream_inject90);
464   tcase_add_test (tc_chain, test_perfect_stream_drop45_inject25);
465   tcase_add_test (tc_chain, test_large_discont);
466
467   return s;
468 }
469
470 GST_CHECK_MAIN (audiorate);