check: Don't re-declare 'GList *buffers' in the tests
[platform/upstream/gst-plugins-good.git] / tests / check / elements / aacparse.c
1 /*
2  * GStreamer
3  *
4  * unit test for aacparse
5  *
6  * Copyright (C) 2008 Nokia Corporation. All rights reserved.
7  *
8  * Contact: Stefan Kost <stefan.kost@nokia.com>
9  *
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.
14  *
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.
19  *
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.
24  */
25
26 #include <gst/check/gstcheck.h>
27 #include "aacparse_data.h"
28
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}"
31
32 #define SINK_CAPS \
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}"
39
40 GList *current_buf = NULL;
41
42 GstPad *srcpad, *sinkpad;
43 guint dataoffset = 0;
44 GstClockTime ts_counter = 0;
45 gint64 offset_counter = 0;
46 guint buffer_counter = 0;
47
48 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
49     GST_PAD_SINK,
50     GST_PAD_ALWAYS,
51     GST_STATIC_CAPS (SINK_CAPS_TMPL)
52     );
53
54 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
55     GST_PAD_SRC,
56     GST_PAD_ALWAYS,
57     GST_STATIC_CAPS (SRC_CAPS_TMPL)
58     );
59
60 typedef struct
61 {
62   guint buffers_before_offset_skip;
63   guint offset_skip_amount;
64   const unsigned char *data_to_verify;
65   GstCaps *caps;
66 } buffer_verify_data_s;
67
68 /* takes a copy of the passed buffer data */
69 static GstBuffer *
70 buffer_new (const unsigned char *buffer_data, guint size)
71 {
72   GstBuffer *buffer;
73
74   buffer = gst_buffer_new_and_alloc (size);
75   if (buffer_data) {
76     memcpy (GST_BUFFER_DATA (buffer), buffer_data, size);
77   } else {
78     guint i;
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;
82     }
83   }
84
85   gst_buffer_set_caps (buffer, GST_PAD_CAPS (srcpad));
86   GST_BUFFER_OFFSET (buffer) = dataoffset;
87   dataoffset += size;
88   return buffer;
89 }
90
91
92 /*
93  * Count buffer sizes together.
94  */
95 static void
96 buffer_count_size (void *buffer, void *user_data)
97 {
98   guint *sum = (guint *) user_data;
99   *sum += GST_BUFFER_SIZE (buffer);
100 }
101
102
103 /*
104  * Verify that given buffer contains predefined ADTS frame.
105  */
106 static void
107 buffer_verify_adts (void *buffer, void *user_data)
108 {
109   buffer_verify_data_s *vdata;
110
111   if (!user_data) {
112     return;
113   }
114
115   vdata = (buffer_verify_data_s *) user_data;
116
117   fail_unless (memcmp (GST_BUFFER_DATA (buffer), vdata->data_to_verify,
118           ADTS_FRAME_LEN) == 0);
119
120   fail_unless (GST_BUFFER_TIMESTAMP (buffer) == ts_counter);
121   fail_unless (GST_BUFFER_DURATION (buffer) != 0);
122
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;
127     }
128   }
129   fail_unless (GST_BUFFER_OFFSET (buffer) == offset_counter);
130
131   if (vdata->caps) {
132     gchar *bcaps = gst_caps_to_string (GST_BUFFER_CAPS (buffer));
133     g_free (bcaps);
134
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));
138   }
139
140   ts_counter += GST_BUFFER_DURATION (buffer);
141   offset_counter += ADTS_FRAME_LEN;
142   buffer_counter++;
143 }
144
145 static GstElement *
146 setup_aacparse (const gchar * src_caps_str)
147 {
148   GstElement *aacparse;
149   GstCaps *srccaps = NULL;
150   GstBus *bus;
151
152   if (src_caps_str) {
153     srccaps = gst_caps_from_string (src_caps_str);
154     fail_unless (srccaps != NULL);
155   }
156
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);
162
163   bus = gst_bus_new ();
164   gst_element_set_bus (aacparse, bus);
165
166   fail_unless (gst_element_set_state (aacparse,
167           GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
168       "could not set to playing");
169
170   if (srccaps) {
171     gst_caps_unref (srccaps);
172   }
173   ts_counter = offset_counter = buffer_counter = 0;
174   buffers = NULL;
175   return aacparse;
176 }
177
178 static void
179 cleanup_aacparse (GstElement * aacparse)
180 {
181   GstBus *bus;
182
183   /* Free parsed buffers */
184   gst_check_drop_buffers ();
185
186   bus = GST_ELEMENT_BUS (aacparse);
187   gst_bus_set_flushing (bus, TRUE);
188   gst_object_unref (bus);
189
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);
195 }
196
197
198 /*
199  * Test if the parser pushes data with ADIF header properly and detects the
200  * stream to MPEG4 properly.
201  */
202 GST_START_TEST (test_parse_adif_normal)
203 {
204   GstElement *aacparse;
205   GstBuffer *buffer;
206   GstCaps *scaps, *sinkcaps;
207   guint datasum = 0;
208   guint i;
209
210   aacparse = setup_aacparse (NULL);
211
212   buffer = buffer_new (adif_header, ADIF_HEADER_LEN);
213   fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
214
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);
218   }
219   gst_pad_push_event (srcpad, gst_event_new_eos ());
220
221   /* Calculate the outputted buffer sizes */
222   g_list_foreach (buffers, buffer_count_size, &datasum);
223
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);
228
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);
233
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);
238
239   cleanup_aacparse (aacparse);
240 }
241
242 GST_END_TEST;
243
244
245 /*
246  * Test if the parser pushes data with ADTS frames properly.
247  */
248 GST_START_TEST (test_parse_adts_normal)
249 {
250   buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg4, NULL };
251   GstElement *aacparse;
252   GstBuffer *buffer;
253   guint i;
254
255   aacparse = setup_aacparse (NULL);
256
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);
260   }
261   gst_pad_push_event (srcpad, gst_event_new_eos ());
262
263   fail_unless_equals_int (g_list_length (buffers), 10);
264   g_list_foreach (buffers, buffer_verify_adts, &vdata);
265
266   cleanup_aacparse (aacparse);
267 }
268
269 GST_END_TEST;
270
271
272 /*
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.
277  */
278 GST_START_TEST (test_parse_adts_drain_single)
279 {
280   buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg4, NULL };
281   GstElement *aacparse;
282   GstBuffer *buffer;
283
284   aacparse = setup_aacparse (NULL);
285
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 ());
289
290   fail_unless_equals_int (g_list_length (buffers), 1);
291   g_list_foreach (buffers, buffer_verify_adts, &vdata);
292
293   cleanup_aacparse (aacparse);
294 }
295
296 GST_END_TEST;
297
298
299 /*
300  * Make sure that parser does not drain garbage when EOS occurs.
301  */
302 GST_START_TEST (test_parse_adts_drain_garbage)
303 {
304   buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg4, NULL };
305   GstElement *aacparse;
306   GstBuffer *buffer;
307   guint i;
308
309   aacparse = setup_aacparse (NULL);
310
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);
314   }
315
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 ());
320
321   fail_unless_equals_int (g_list_length (buffers), 10);
322   g_list_foreach (buffers, buffer_verify_adts, &vdata);
323
324   cleanup_aacparse (aacparse);
325 }
326
327 GST_END_TEST;
328
329
330 /*
331  * Test if ADTS parser splits a buffer that contains two frames into two
332  * separate buffers properly.
333  */
334 GST_START_TEST (test_parse_adts_split)
335 {
336   buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg4, NULL };
337   GstElement *aacparse;
338   GstBuffer *buffer;
339   guint i;
340
341   aacparse = setup_aacparse (NULL);
342
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);
348   }
349   gst_pad_push_event (srcpad, gst_event_new_eos ());
350
351   fail_unless_equals_int (g_list_length (buffers), 10);
352   g_list_foreach (buffers, buffer_verify_adts, &vdata);
353
354   cleanup_aacparse (aacparse);
355 }
356
357 GST_END_TEST;
358
359
360 /*
361  * Test if the ADTS parser skips garbage between frames properly.
362  */
363 GST_START_TEST (test_parse_adts_skip_garbage)
364 {
365   buffer_verify_data_s vdata =
366       { 10, GARBAGE_FRAME_LEN, adts_frame_mpeg4, NULL };
367   GstElement *aacparse;
368   GstBuffer *buffer;
369   guint i;
370
371   aacparse = setup_aacparse (NULL);
372
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);
376   }
377
378   /* push garbage */
379   buffer = buffer_new (garbage_frame, GARBAGE_FRAME_LEN);
380   fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
381
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);
385   }
386   gst_pad_push_event (srcpad, gst_event_new_eos ());
387
388   fail_unless_equals_int (g_list_length (buffers), 20);
389   g_list_foreach (buffers, buffer_verify_adts, &vdata);
390
391   cleanup_aacparse (aacparse);
392 }
393
394 GST_END_TEST;
395
396
397 /*
398  * Test if the src caps are set according to stream format (MPEG version).
399  */
400 GST_START_TEST (test_parse_adts_detect_mpeg_version)
401 {
402   buffer_verify_data_s vdata = { 0, 0, adts_frame_mpeg2, NULL };
403   GstElement *aacparse;
404   GstBuffer *buffer;
405   GstCaps *sinkcaps;
406   guint i;
407
408   aacparse = setup_aacparse (NULL);
409
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");
413
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);
418   }
419   gst_pad_push_event (srcpad, gst_event_new_eos ());
420
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);
426
427   fail_unless_equals_int (g_list_length (buffers), 10);
428   g_list_foreach (buffers, buffer_verify_adts, &vdata);
429
430   gst_caps_unref (vdata.caps);
431   cleanup_aacparse (aacparse);
432 }
433
434 GST_END_TEST;
435
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)
440 /*
441  * Test if the parser handles raw stream and codec_data info properly.
442  */
443 GST_START_TEST (test_parse_handle_codec_data)
444 {
445   GstElement *aacparse;
446   GstBuffer *buffer;
447   GstCaps *sinkcaps;
448   GstStructure *s;
449   guint datasum = 0;
450   guint i;
451   const gchar *stream_format;
452
453   aacparse = setup_aacparse (SRC_CAPS_CDATA);
454
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);
460   }
461   gst_pad_push_event (srcpad, gst_event_new_eos ());
462
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);
476
477   gst_caps_unref (sinkcaps);
478
479   g_list_foreach (buffers, buffer_count_size, &datasum);
480   fail_unless_equals_int (datasum, 10 * 100);
481
482   cleanup_aacparse (aacparse);
483 }
484
485 GST_END_TEST;
486
487
488 static Suite *
489 aacparse_suite (void)
490 {
491   Suite *s = suite_create ("aacparse");
492   TCase *tc_chain = tcase_create ("general");
493
494   suite_add_tcase (s, tc_chain);
495   /* ADIF tests */
496   tcase_add_test (tc_chain, test_parse_adif_normal);
497
498   /* ADTS tests */
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);
505
506   /* Other tests */
507   tcase_add_test (tc_chain, test_parse_handle_codec_data);
508
509   return s;
510 }
511
512
513 /*
514  * TODO:
515  *   - Both push- and pull-modes need to be tested
516  *      * Pull-mode & EOS
517  */
518
519 GST_CHECK_MAIN (aacparse);