4 * unit test for aacparse
6 * Copyright (C) 2008 Nokia Corporation. All rights reserved.
8 * Contact: Stefan Kost <stefan.kost@nokia.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
26 #include <gst/check/gstcheck.h>
27 #include "aacparse_data.h"
29 #define SRC_CAPS_CDATA "audio/mpeg, framed=(boolean)false, codec_data=(buffer)1190"
30 #define SRC_CAPS_TMPL "audio/mpeg, framed=(boolean)false, mpegversion=(int){2,4}"
33 "audio/mpeg, framed=(boolean)true"
34 #define SINK_CAPS_MPEG2 \
35 "audio/mpeg, framed=(boolean)true, mpegversion=2, rate=48000, channels=2"
36 #define SINK_CAPS_MPEG4 \
37 "audio/mpeg, framed=(boolean)true, mpegversion=4, rate=96000, channels=2"
38 #define SINK_CAPS_TMPL "audio/mpeg, framed=(boolean)true, mpegversion=(int){2,4}"
40 GList *current_buf = NULL;
42 GstPad *srcpad, *sinkpad;
44 GstClockTime ts_counter = 0;
45 gint64 offset_counter = 0;
46 guint buffer_counter = 0;
48 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
51 GST_STATIC_CAPS (SINK_CAPS_TMPL)
54 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
57 GST_STATIC_CAPS (SRC_CAPS_TMPL)
62 guint buffers_before_offset_skip;
63 guint offset_skip_amount;
64 const unsigned char *data_to_verify;
66 } buffer_verify_data_s;
68 /* takes a copy of the passed buffer data */
70 buffer_new (const unsigned char *buffer_data, guint size)
74 buffer = gst_buffer_new_and_alloc (size);
76 memcpy (GST_BUFFER_DATA (buffer), buffer_data, size);
79 /* Create a recognizable pattern (loop 0x00 -> 0xff) in the data block */
80 for (i = 0; i < size; i++) {
81 GST_BUFFER_DATA (buffer)[i] = i % 0x100;
85 gst_buffer_set_caps (buffer, GST_PAD_CAPS (srcpad));
86 GST_BUFFER_OFFSET (buffer) = dataoffset;
93 * Count buffer sizes together.
96 buffer_count_size (void *buffer, void *user_data)
98 guint *sum = (guint *) user_data;
99 *sum += GST_BUFFER_SIZE (buffer);
104 * Verify that given buffer contains predefined ADTS frame.
107 buffer_verify_adts (void *buffer, void *user_data)
109 buffer_verify_data_s *vdata;
115 vdata = (buffer_verify_data_s *) user_data;
117 fail_unless (memcmp (GST_BUFFER_DATA (buffer), vdata->data_to_verify,
118 ADTS_FRAME_LEN) == 0);
120 fail_unless (GST_BUFFER_TIMESTAMP (buffer) == ts_counter);
121 fail_unless (GST_BUFFER_DURATION (buffer) != 0);
123 if (vdata->buffers_before_offset_skip) {
124 /* This is for skipping the garbage in some test cases */
125 if (buffer_counter == vdata->buffers_before_offset_skip) {
126 offset_counter += vdata->offset_skip_amount;
129 fail_unless (GST_BUFFER_OFFSET (buffer) == offset_counter);
132 gchar *bcaps = gst_caps_to_string (GST_BUFFER_CAPS (buffer));
135 GST_LOG ("%" GST_PTR_FORMAT " = %" GST_PTR_FORMAT " ?",
136 GST_BUFFER_CAPS (buffer), vdata->caps);
137 fail_unless (gst_caps_is_equal (GST_BUFFER_CAPS (buffer), vdata->caps));
140 ts_counter += GST_BUFFER_DURATION (buffer);
141 offset_counter += ADTS_FRAME_LEN;
146 setup_aacparse (const gchar * src_caps_str)
148 GstElement *aacparse;
149 GstCaps *srccaps = NULL;
153 srccaps = gst_caps_from_string (src_caps_str);
154 fail_unless (srccaps != NULL);
157 aacparse = gst_check_setup_element ("aacparse");
158 srcpad = gst_check_setup_src_pad (aacparse, &srctemplate, srccaps);
159 sinkpad = gst_check_setup_sink_pad (aacparse, &sinktemplate, NULL);
160 gst_pad_set_active (srcpad, TRUE);
161 gst_pad_set_active (sinkpad, TRUE);
163 bus = gst_bus_new ();
164 gst_element_set_bus (aacparse, bus);
166 fail_unless (gst_element_set_state (aacparse,
167 GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
168 "could not set to playing");
171 gst_caps_unref (srccaps);
173 ts_counter = offset_counter = buffer_counter = 0;
179 cleanup_aacparse (GstElement * aacparse)
183 /* Free parsed buffers */
184 gst_check_drop_buffers ();
186 bus = GST_ELEMENT_BUS (aacparse);
187 gst_bus_set_flushing (bus, TRUE);
188 gst_object_unref (bus);
190 gst_pad_set_active (srcpad, FALSE);
191 gst_pad_set_active (sinkpad, FALSE);
192 gst_check_teardown_src_pad (aacparse);
193 gst_check_teardown_sink_pad (aacparse);
194 gst_check_teardown_element (aacparse);
199 * Test if the parser pushes data with ADIF header properly and detects the
200 * stream to MPEG4 properly.
202 GST_START_TEST (test_parse_adif_normal)
204 GstElement *aacparse;
206 GstCaps *scaps, *sinkcaps;
210 aacparse = setup_aacparse (NULL);
212 buffer = buffer_new (adif_header, ADIF_HEADER_LEN);
213 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
215 for (i = 0; i < 3; i++) {
216 buffer = buffer_new (NULL, 100);
217 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
219 gst_pad_push_event (srcpad, gst_event_new_eos ());
221 /* Calculate the outputted buffer sizes */
222 g_list_foreach (buffers, buffer_count_size, &datasum);
224 /* ADIF is not a framed format, and therefore we cannot expect the
225 same amount of output buffers as we pushed. However, all data should
226 still come through, including the header bytes */
227 fail_unless_equals_int (datasum, 3 * 100 + ADIF_HEADER_LEN);
229 /* Check that the negotiated caps are as expected */
230 /* For ADIF parser assumes that data is always version 4 */
231 scaps = gst_caps_from_string (SINK_CAPS_MPEG4 ", stream-format=(string)adif");
232 sinkcaps = gst_pad_get_negotiated_caps (sinkpad);
234 GST_LOG ("%" GST_PTR_FORMAT " = %" GST_PTR_FORMAT " ?", sinkcaps, scaps);
235 fail_unless (gst_caps_is_equal (sinkcaps, scaps));
236 gst_caps_unref (sinkcaps);
237 gst_caps_unref (scaps);
239 cleanup_aacparse (aacparse);
246 * Test if the parser pushes data with ADTS frames properly.
248 GST_START_TEST (test_parse_adts_normal)
250 buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg4, NULL };
251 GstElement *aacparse;
255 aacparse = setup_aacparse (NULL);
257 for (i = 0; i < 10; i++) {
258 buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN);
259 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
261 gst_pad_push_event (srcpad, gst_event_new_eos ());
263 fail_unless_equals_int (g_list_length (buffers), 10);
264 g_list_foreach (buffers, buffer_verify_adts, &vdata);
266 cleanup_aacparse (aacparse);
273 * Test if ADTS parser drains its buffers properly. Even one single frame
274 * should be drained and pushed forward when EOS occurs. This single frame
275 * case is special, since normally the parser needs more data to be sure
276 * about stream format. But it should still push the frame forward in EOS.
278 GST_START_TEST (test_parse_adts_drain_single)
280 buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg4, NULL };
281 GstElement *aacparse;
284 aacparse = setup_aacparse (NULL);
286 buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN);
287 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
288 gst_pad_push_event (srcpad, gst_event_new_eos ());
290 fail_unless_equals_int (g_list_length (buffers), 1);
291 g_list_foreach (buffers, buffer_verify_adts, &vdata);
293 cleanup_aacparse (aacparse);
300 * Make sure that parser does not drain garbage when EOS occurs.
302 GST_START_TEST (test_parse_adts_drain_garbage)
304 buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg4, NULL };
305 GstElement *aacparse;
309 aacparse = setup_aacparse (NULL);
311 for (i = 0; i < 10; i++) {
312 buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN);
313 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
316 /* Push one garbage frame and then EOS */
317 buffer = buffer_new (garbage_frame, GARBAGE_FRAME_LEN);
318 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
319 gst_pad_push_event (srcpad, gst_event_new_eos ());
321 fail_unless_equals_int (g_list_length (buffers), 10);
322 g_list_foreach (buffers, buffer_verify_adts, &vdata);
324 cleanup_aacparse (aacparse);
331 * Test if ADTS parser splits a buffer that contains two frames into two
332 * separate buffers properly.
334 GST_START_TEST (test_parse_adts_split)
336 buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg4, NULL };
337 GstElement *aacparse;
341 aacparse = setup_aacparse (NULL);
343 for (i = 0; i < 5; i++) {
344 buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN * 2);
345 memcpy (GST_BUFFER_DATA (buffer) + ADTS_FRAME_LEN,
346 adts_frame_mpeg4, ADTS_FRAME_LEN);
347 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
349 gst_pad_push_event (srcpad, gst_event_new_eos ());
351 fail_unless_equals_int (g_list_length (buffers), 10);
352 g_list_foreach (buffers, buffer_verify_adts, &vdata);
354 cleanup_aacparse (aacparse);
361 * Test if the ADTS parser skips garbage between frames properly.
363 GST_START_TEST (test_parse_adts_skip_garbage)
365 buffer_verify_data_s vdata =
366 { 10, GARBAGE_FRAME_LEN, adts_frame_mpeg4, NULL };
367 GstElement *aacparse;
371 aacparse = setup_aacparse (NULL);
373 for (i = 0; i < 10; i++) {
374 buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN);
375 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
379 buffer = buffer_new (garbage_frame, GARBAGE_FRAME_LEN);
380 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
382 for (i = 0; i < 10; i++) {
383 buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN);
384 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
386 gst_pad_push_event (srcpad, gst_event_new_eos ());
388 fail_unless_equals_int (g_list_length (buffers), 20);
389 g_list_foreach (buffers, buffer_verify_adts, &vdata);
391 cleanup_aacparse (aacparse);
398 * Test if the src caps are set according to stream format (MPEG version).
400 GST_START_TEST (test_parse_adts_detect_mpeg_version)
402 buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg2, NULL };
403 GstElement *aacparse;
408 aacparse = setup_aacparse (NULL);
410 /* buffer_verify_adts will check if the caps are equal */
411 vdata.caps = gst_caps_from_string (SINK_CAPS_MPEG2
412 ", stream-format=(string)adts");
414 for (i = 0; i < 10; i++) {
415 /* Push MPEG version 2 frames. */
416 buffer = buffer_new (adts_frame_mpeg2, ADTS_FRAME_LEN);
417 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
419 gst_pad_push_event (srcpad, gst_event_new_eos ());
421 /* Check that the negotiated caps are as expected */
422 sinkcaps = gst_pad_get_negotiated_caps (sinkpad);
423 GST_LOG ("%" GST_PTR_FORMAT " = %" GST_PTR_FORMAT "?", sinkcaps, vdata.caps);
424 fail_unless (gst_caps_is_equal (sinkcaps, vdata.caps));
425 gst_caps_unref (sinkcaps);
427 fail_unless_equals_int (g_list_length (buffers), 10);
428 g_list_foreach (buffers, buffer_verify_adts, &vdata);
430 gst_caps_unref (vdata.caps);
431 cleanup_aacparse (aacparse);
436 #define structure_get_int(s,f) \
437 (g_value_get_int(gst_structure_get_value(s,f)))
438 #define fail_unless_structure_field_int_equals(s,field,num) \
439 fail_unless_equals_int (structure_get_int(s,field), num)
441 * Test if the parser handles raw stream and codec_data info properly.
443 GST_START_TEST (test_parse_handle_codec_data)
445 GstElement *aacparse;
451 const gchar *stream_format;
453 aacparse = setup_aacparse (SRC_CAPS_CDATA);
455 for (i = 0; i < 10; i++) {
456 /* Push random data. It should get through since the parser should be
457 initialized because it got codec_data in the caps */
458 buffer = buffer_new (NULL, 100);
459 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
461 gst_pad_push_event (srcpad, gst_event_new_eos ());
463 /* Check that the negotiated caps are as expected */
464 /* When codec_data is present, parser assumes that data is version 4 */
465 sinkcaps = gst_pad_get_negotiated_caps (sinkpad);
466 GST_LOG ("aac output caps: %" GST_PTR_FORMAT, sinkcaps);
467 s = gst_caps_get_structure (sinkcaps, 0);
468 fail_unless (gst_structure_has_name (s, "audio/mpeg"));
469 fail_unless_structure_field_int_equals (s, "mpegversion", 4);
470 fail_unless_structure_field_int_equals (s, "channels", 2);
471 fail_unless_structure_field_int_equals (s, "rate", 48000);
472 fail_unless (gst_structure_has_field (s, "codec_data"));
473 fail_unless (gst_structure_has_field (s, "stream-format"));
474 stream_format = gst_structure_get_string (s, "stream-format");
475 fail_unless (strcmp (stream_format, "raw") == 0);
477 gst_caps_unref (sinkcaps);
479 g_list_foreach (buffers, buffer_count_size, &datasum);
480 fail_unless_equals_int (datasum, 10 * 100);
482 cleanup_aacparse (aacparse);
489 aacparse_suite (void)
491 Suite *s = suite_create ("aacparse");
492 TCase *tc_chain = tcase_create ("general");
494 suite_add_tcase (s, tc_chain);
496 tcase_add_test (tc_chain, test_parse_adif_normal);
499 tcase_add_test (tc_chain, test_parse_adts_normal);
500 tcase_add_test (tc_chain, test_parse_adts_drain_single);
501 tcase_add_test (tc_chain, test_parse_adts_drain_garbage);
502 tcase_add_test (tc_chain, test_parse_adts_split);
503 tcase_add_test (tc_chain, test_parse_adts_skip_garbage);
504 tcase_add_test (tc_chain, test_parse_adts_detect_mpeg_version);
507 tcase_add_test (tc_chain, test_parse_handle_codec_data);
515 * - Both push- and pull-modes need to be tested
519 GST_CHECK_MAIN (aacparse);