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}"
41 GList *current_buf = NULL;
43 GstPad *srcpad, *sinkpad;
45 GstClockTime ts_counter = 0;
46 gint64 offset_counter = 0;
47 guint buffer_counter = 0;
49 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
52 GST_STATIC_CAPS (SINK_CAPS_TMPL)
55 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
58 GST_STATIC_CAPS (SRC_CAPS_TMPL)
63 guint buffers_before_offset_skip;
64 guint offset_skip_amount;
65 const unsigned char *data_to_verify;
67 } buffer_verify_data_s;
69 /* takes a copy of the passed buffer data */
71 buffer_new (const unsigned char *buffer_data, guint size)
75 buffer = gst_buffer_new_and_alloc (size);
77 memcpy (GST_BUFFER_DATA (buffer), buffer_data, size);
80 /* Create a recognizable pattern (loop 0x00 -> 0xff) in the data block */
81 for (i = 0; i < size; i++) {
82 GST_BUFFER_DATA (buffer)[i] = i % 0x100;
86 gst_buffer_set_caps (buffer, GST_PAD_CAPS (srcpad));
87 GST_BUFFER_OFFSET (buffer) = dataoffset;
94 * Count buffer sizes together.
97 buffer_count_size (void *buffer, void *user_data)
99 guint *sum = (guint *) user_data;
100 *sum += GST_BUFFER_SIZE (buffer);
105 * Verify that given buffer contains predefined ADTS frame.
108 buffer_verify_adts (void *buffer, void *user_data)
110 buffer_verify_data_s *vdata;
116 vdata = (buffer_verify_data_s *) user_data;
118 fail_unless (memcmp (GST_BUFFER_DATA (buffer), vdata->data_to_verify,
119 ADTS_FRAME_LEN) == 0);
121 fail_unless (GST_BUFFER_TIMESTAMP (buffer) == ts_counter);
122 fail_unless (GST_BUFFER_DURATION (buffer) != 0);
124 if (vdata->buffers_before_offset_skip) {
125 /* This is for skipping the garbage in some test cases */
126 if (buffer_counter == vdata->buffers_before_offset_skip) {
127 offset_counter += vdata->offset_skip_amount;
130 fail_unless (GST_BUFFER_OFFSET (buffer) == offset_counter);
133 gchar *bcaps = gst_caps_to_string (GST_BUFFER_CAPS (buffer));
136 GST_LOG ("%" GST_PTR_FORMAT " = %" GST_PTR_FORMAT " ?",
137 GST_BUFFER_CAPS (buffer), vdata->caps);
138 fail_unless (gst_caps_is_equal (GST_BUFFER_CAPS (buffer), vdata->caps));
141 ts_counter += GST_BUFFER_DURATION (buffer);
142 offset_counter += ADTS_FRAME_LEN;
147 setup_aacparse (const gchar * src_caps_str)
149 GstElement *aacparse;
150 GstCaps *srccaps = NULL;
154 srccaps = gst_caps_from_string (src_caps_str);
155 fail_unless (srccaps != NULL);
158 aacparse = gst_check_setup_element ("aacparse");
159 srcpad = gst_check_setup_src_pad (aacparse, &srctemplate, srccaps);
160 sinkpad = gst_check_setup_sink_pad (aacparse, &sinktemplate, NULL);
161 gst_pad_set_active (srcpad, TRUE);
162 gst_pad_set_active (sinkpad, TRUE);
164 bus = gst_bus_new ();
165 gst_element_set_bus (aacparse, bus);
167 fail_unless (gst_element_set_state (aacparse,
168 GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
169 "could not set to playing");
172 gst_caps_unref (srccaps);
174 ts_counter = offset_counter = buffer_counter = 0;
180 cleanup_aacparse (GstElement * aacparse)
184 /* Free parsed buffers */
185 gst_check_drop_buffers ();
187 bus = GST_ELEMENT_BUS (aacparse);
188 gst_bus_set_flushing (bus, TRUE);
189 gst_object_unref (bus);
191 gst_pad_set_active (srcpad, FALSE);
192 gst_pad_set_active (sinkpad, FALSE);
193 gst_check_teardown_src_pad (aacparse);
194 gst_check_teardown_sink_pad (aacparse);
195 gst_check_teardown_element (aacparse);
200 * Test if the parser pushes data with ADIF header properly and detects the
201 * stream to MPEG4 properly.
203 GST_START_TEST (test_parse_adif_normal)
205 GstElement *aacparse;
207 GstCaps *scaps, *sinkcaps;
211 aacparse = setup_aacparse (NULL);
213 buffer = buffer_new (adif_header, ADIF_HEADER_LEN);
214 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
216 for (i = 0; i < 3; i++) {
217 buffer = buffer_new (NULL, 100);
218 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
220 gst_pad_push_event (srcpad, gst_event_new_eos ());
222 /* Calculate the outputted buffer sizes */
223 g_list_foreach (buffers, buffer_count_size, &datasum);
225 /* ADIF is not a framed format, and therefore we cannot expect the
226 same amount of output buffers as we pushed. However, all data should
227 still come through, including the header bytes */
228 fail_unless_equals_int (datasum, 3 * 100 + ADIF_HEADER_LEN);
230 /* Check that the negotiated caps are as expected */
231 /* For ADIF parser assumes that data is always version 4 */
232 scaps = gst_caps_from_string (SINK_CAPS_MPEG4 ", stream-format=(string)adif");
233 sinkcaps = gst_pad_get_negotiated_caps (sinkpad);
235 GST_LOG ("%" GST_PTR_FORMAT " = %" GST_PTR_FORMAT " ?", sinkcaps, scaps);
236 fail_unless (gst_caps_is_equal (sinkcaps, scaps));
237 gst_caps_unref (sinkcaps);
238 gst_caps_unref (scaps);
240 cleanup_aacparse (aacparse);
247 * Test if the parser pushes data with ADTS frames properly.
249 GST_START_TEST (test_parse_adts_normal)
251 buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg4, NULL };
252 GstElement *aacparse;
256 aacparse = setup_aacparse (NULL);
258 for (i = 0; i < 10; i++) {
259 buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN);
260 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
262 gst_pad_push_event (srcpad, gst_event_new_eos ());
264 fail_unless_equals_int (g_list_length (buffers), 10);
265 g_list_foreach (buffers, buffer_verify_adts, &vdata);
267 cleanup_aacparse (aacparse);
274 * Test if ADTS parser drains its buffers properly. Even one single frame
275 * should be drained and pushed forward when EOS occurs. This single frame
276 * case is special, since normally the parser needs more data to be sure
277 * about stream format. But it should still push the frame forward in EOS.
279 GST_START_TEST (test_parse_adts_drain_single)
281 buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg4, NULL };
282 GstElement *aacparse;
285 aacparse = setup_aacparse (NULL);
287 buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN);
288 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
289 gst_pad_push_event (srcpad, gst_event_new_eos ());
291 fail_unless_equals_int (g_list_length (buffers), 1);
292 g_list_foreach (buffers, buffer_verify_adts, &vdata);
294 cleanup_aacparse (aacparse);
301 * Make sure that parser does not drain garbage when EOS occurs.
303 GST_START_TEST (test_parse_adts_drain_garbage)
305 buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg4, NULL };
306 GstElement *aacparse;
310 aacparse = setup_aacparse (NULL);
312 for (i = 0; i < 10; i++) {
313 buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN);
314 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
317 /* Push one garbage frame and then EOS */
318 buffer = buffer_new (garbage_frame, GARBAGE_FRAME_LEN);
319 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
320 gst_pad_push_event (srcpad, gst_event_new_eos ());
322 fail_unless_equals_int (g_list_length (buffers), 10);
323 g_list_foreach (buffers, buffer_verify_adts, &vdata);
325 cleanup_aacparse (aacparse);
332 * Test if ADTS parser splits a buffer that contains two frames into two
333 * separate buffers properly.
335 GST_START_TEST (test_parse_adts_split)
337 buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg4, NULL };
338 GstElement *aacparse;
342 aacparse = setup_aacparse (NULL);
344 for (i = 0; i < 5; i++) {
345 buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN * 2);
346 memcpy (GST_BUFFER_DATA (buffer) + ADTS_FRAME_LEN,
347 adts_frame_mpeg4, ADTS_FRAME_LEN);
348 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
350 gst_pad_push_event (srcpad, gst_event_new_eos ());
352 fail_unless_equals_int (g_list_length (buffers), 10);
353 g_list_foreach (buffers, buffer_verify_adts, &vdata);
355 cleanup_aacparse (aacparse);
362 * Test if the ADTS parser skips garbage between frames properly.
364 GST_START_TEST (test_parse_adts_skip_garbage)
366 buffer_verify_data_s vdata =
367 { 10, GARBAGE_FRAME_LEN, adts_frame_mpeg4, NULL };
368 GstElement *aacparse;
372 aacparse = setup_aacparse (NULL);
374 for (i = 0; i < 10; i++) {
375 buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN);
376 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
380 buffer = buffer_new (garbage_frame, GARBAGE_FRAME_LEN);
381 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
383 for (i = 0; i < 10; i++) {
384 buffer = buffer_new (adts_frame_mpeg4, ADTS_FRAME_LEN);
385 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
387 gst_pad_push_event (srcpad, gst_event_new_eos ());
389 fail_unless_equals_int (g_list_length (buffers), 20);
390 g_list_foreach (buffers, buffer_verify_adts, &vdata);
392 cleanup_aacparse (aacparse);
399 * Test if the src caps are set according to stream format (MPEG version).
401 GST_START_TEST (test_parse_adts_detect_mpeg_version)
403 buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg2, NULL };
404 GstElement *aacparse;
409 aacparse = setup_aacparse (NULL);
411 /* buffer_verify_adts will check if the caps are equal */
412 vdata.caps = gst_caps_from_string (SINK_CAPS_MPEG2
413 ", stream-format=(string)adts");
415 for (i = 0; i < 10; i++) {
416 /* Push MPEG version 2 frames. */
417 buffer = buffer_new (adts_frame_mpeg2, ADTS_FRAME_LEN);
418 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
420 gst_pad_push_event (srcpad, gst_event_new_eos ());
422 /* Check that the negotiated caps are as expected */
423 sinkcaps = gst_pad_get_negotiated_caps (sinkpad);
424 GST_LOG ("%" GST_PTR_FORMAT " = %" GST_PTR_FORMAT "?", sinkcaps, vdata.caps);
425 fail_unless (gst_caps_is_equal (sinkcaps, vdata.caps));
426 gst_caps_unref (sinkcaps);
428 fail_unless_equals_int (g_list_length (buffers), 10);
429 g_list_foreach (buffers, buffer_verify_adts, &vdata);
431 gst_caps_unref (vdata.caps);
432 cleanup_aacparse (aacparse);
437 #define structure_get_int(s,f) \
438 (g_value_get_int(gst_structure_get_value(s,f)))
439 #define fail_unless_structure_field_int_equals(s,field,num) \
440 fail_unless_equals_int (structure_get_int(s,field), num)
442 * Test if the parser handles raw stream and codec_data info properly.
444 GST_START_TEST (test_parse_handle_codec_data)
446 GstElement *aacparse;
452 const gchar *stream_format;
454 aacparse = setup_aacparse (SRC_CAPS_CDATA);
456 for (i = 0; i < 10; i++) {
457 /* Push random data. It should get through since the parser should be
458 initialized because it got codec_data in the caps */
459 buffer = buffer_new (NULL, 100);
460 fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
462 gst_pad_push_event (srcpad, gst_event_new_eos ());
464 /* Check that the negotiated caps are as expected */
465 /* When codec_data is present, parser assumes that data is version 4 */
466 sinkcaps = gst_pad_get_negotiated_caps (sinkpad);
467 GST_LOG ("aac output caps: %" GST_PTR_FORMAT, sinkcaps);
468 s = gst_caps_get_structure (sinkcaps, 0);
469 fail_unless (gst_structure_has_name (s, "audio/mpeg"));
470 fail_unless_structure_field_int_equals (s, "mpegversion", 4);
471 fail_unless_structure_field_int_equals (s, "channels", 2);
472 fail_unless_structure_field_int_equals (s, "rate", 48000);
473 fail_unless (gst_structure_has_field (s, "codec_data"));
474 fail_unless (gst_structure_has_field (s, "stream-format"));
475 stream_format = gst_structure_get_string (s, "stream-format");
476 fail_unless (strcmp (stream_format, "raw") == 0);
478 gst_caps_unref (sinkcaps);
480 g_list_foreach (buffers, buffer_count_size, &datasum);
481 fail_unless_equals_int (datasum, 10 * 100);
483 cleanup_aacparse (aacparse);
490 aacparse_suite (void)
492 Suite *s = suite_create ("aacparse");
493 TCase *tc_chain = tcase_create ("general");
495 suite_add_tcase (s, tc_chain);
497 tcase_add_test (tc_chain, test_parse_adif_normal);
500 tcase_add_test (tc_chain, test_parse_adts_normal);
501 tcase_add_test (tc_chain, test_parse_adts_drain_single);
502 tcase_add_test (tc_chain, test_parse_adts_drain_garbage);
503 tcase_add_test (tc_chain, test_parse_adts_split);
504 tcase_add_test (tc_chain, test_parse_adts_skip_garbage);
505 tcase_add_test (tc_chain, test_parse_adts_detect_mpeg_version);
508 tcase_add_test (tc_chain, test_parse_handle_codec_data);
516 * - Both push- and pull-modes need to be tested
520 GST_CHECK_MAIN (aacparse);