1 /*
2  * GStreamer
3  *
4  * unit test for (audio) parser
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., 51 Franklin St, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  */
25 
26 #include <gst/check/gstcheck.h>
27 #include "elements/parser.h"
28 
29 
30 /* context state variables */
31 const gchar *ctx_factory;
32 GstStaticPadTemplate *ctx_sink_template;
33 GstStaticPadTemplate *ctx_src_template;
34 GstCaps *ctx_input_caps;
35 GstCaps *ctx_output_caps;
36 guint ctx_discard = 0;
37 datablob ctx_headers[MAX_HEADERS] = { {NULL, 0}, };
38 
39 gboolean ctx_no_metadata = FALSE;
40 
41 /* helper variables */
42 GList *current_buf = NULL;
43 
44 GstPad *srcpad, *sinkpad;
45 guint dataoffset = 0;
46 GstClockTime ts_counter = 0;
47 gint64 offset_counter = 0;
48 guint buffer_counter = 0;
49 
50 typedef struct
51 {
52   guint discard;
53   guint buffers_before_offset_skip;
54   guint offset_skip_amount;
55   const guint8 *data_to_verify;
56   guint data_to_verify_size;
57   GstCaps *caps;
58   gboolean no_metadata;
59 } buffer_verify_data_s;
60 
61 /* takes a copy of the passed buffer data */
62 static GstBuffer *
buffer_new(const unsigned char * buffer_data,guint size)63 buffer_new (const unsigned char *buffer_data, guint size)
64 {
65   GstBuffer *buffer;
66 
67   buffer = gst_buffer_new_and_alloc (size);
68   if (buffer_data) {
69     gst_buffer_fill (buffer, 0, buffer_data, size);
70   } else {
71     guint i;
72     GstMapInfo map;
73 
74     gst_buffer_map (buffer, &map, GST_MAP_WRITE);
75     /* Create a recognizable pattern (loop 0x00 -> 0xff) in the data block */
76     for (i = 0; i < map.size; i++) {
77       map.data[i] = i % 0x100;
78     }
79     gst_buffer_unmap (buffer, &map);
80   }
81 
82   /* gst_buffer_set_caps (buffer, GST_PAD_CAPS (srcpad)); */
83   GST_BUFFER_OFFSET (buffer) = dataoffset;
84   dataoffset += size;
85   return buffer;
86 }
87 
88 /*
89  * Adds buffer sizes together.
90  */
91 static void
buffer_count_size(void * buffer,void * user_data)92 buffer_count_size (void *buffer, void *user_data)
93 {
94   guint *sum = (guint *) user_data;
95   *sum += gst_buffer_get_size (buffer);
96 }
97 
98 /*
99  * Verify that given buffer contains predefined ADTS frame.
100  */
101 static void
buffer_verify_data(void * buffer,void * user_data)102 buffer_verify_data (void *buffer, void *user_data)
103 {
104   buffer_verify_data_s *vdata;
105 
106   if (!user_data) {
107     return;
108   }
109 
110   vdata = (buffer_verify_data_s *) user_data;
111 
112   GST_DEBUG ("discard: %d", vdata->discard);
113   if (vdata->discard) {
114     buffer_counter++;
115     if (buffer_counter == vdata->discard) {
116       buffer_counter = 0;
117       vdata->discard = 0;
118     }
119     return;
120   }
121 
122   fail_unless (gst_buffer_get_size (buffer) == vdata->data_to_verify_size);
123   fail_unless (gst_buffer_memcmp (buffer, 0, vdata->data_to_verify,
124           vdata->data_to_verify_size) == 0);
125 
126   if (vdata->buffers_before_offset_skip) {
127     /* This is for skipping the garbage in some test cases */
128     if (buffer_counter == vdata->buffers_before_offset_skip) {
129       offset_counter += vdata->offset_skip_amount;
130     }
131   }
132   if (!vdata->no_metadata) {
133     fail_unless (GST_BUFFER_TIMESTAMP (buffer) == ts_counter);
134     fail_unless (GST_BUFFER_DURATION (buffer) != 0);
135     fail_unless (GST_BUFFER_OFFSET (buffer) == offset_counter);
136   }
137 
138   ts_counter += GST_BUFFER_DURATION (buffer);
139   offset_counter += gst_buffer_get_size (buffer);
140   buffer_counter++;
141 }
142 
143 static GstElement *
setup_element(const gchar * factory,GstStaticPadTemplate * sink_template,GstCaps * sink_caps,GstStaticPadTemplate * src_template,GstCaps * src_caps)144 setup_element (const gchar * factory, GstStaticPadTemplate * sink_template,
145     GstCaps * sink_caps, GstStaticPadTemplate * src_template,
146     GstCaps * src_caps)
147 {
148   GstElement *element;
149   GstBus *bus;
150   gchar *caps_str = NULL;
151 
152   element = gst_check_setup_element (factory);
153   srcpad = gst_check_setup_src_pad (element, src_template);
154   if (sink_caps) {
155     caps_str = gst_caps_to_string (sink_caps);
156     sink_template->static_caps.string = caps_str;
157   }
158   sinkpad = gst_check_setup_sink_pad (element, sink_template);
159   gst_pad_set_active (srcpad, TRUE);
160   gst_check_setup_events (srcpad, element, src_caps, GST_FORMAT_BYTES);
161   gst_pad_set_active (sinkpad, TRUE);
162 
163   bus = gst_bus_new ();
164   gst_element_set_bus (element, bus);
165 
166   fail_unless (gst_element_set_state (element,
167           GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
168       "could not set to playing");
169 
170   ts_counter = offset_counter = buffer_counter = 0;
171   buffers = NULL;
172   g_free (caps_str);
173   return element;
174 }
175 
176 static void
cleanup_element(GstElement * element)177 cleanup_element (GstElement * element)
178 {
179   GstBus *bus;
180 
181   /* Free parsed buffers */
182   gst_check_drop_buffers ();
183 
184   bus = GST_ELEMENT_BUS (element);
185   gst_bus_set_flushing (bus, TRUE);
186   gst_object_unref (bus);
187 
188   gst_pad_set_active (srcpad, FALSE);
189   gst_pad_set_active (sinkpad, FALSE);
190   gst_check_teardown_src_pad (element);
191   gst_check_teardown_sink_pad (element);
192   gst_check_teardown_element (element);
193 }
194 
195 /* inits a standard test */
196 void
gst_parser_test_init(GstParserTest * ptest,guint8 * data,guint size,guint num)197 gst_parser_test_init (GstParserTest * ptest, guint8 * data, guint size,
198     guint num)
199 {
200   /* need these */
201   fail_unless (ctx_factory != NULL);
202   fail_unless (ctx_src_template != NULL);
203   fail_unless (ctx_sink_template != NULL);
204 
205   /* basics */
206   memset (ptest, 0, sizeof (*ptest));
207   ptest->factory = ctx_factory;
208   ptest->sink_template = ctx_sink_template;
209   ptest->src_template = ctx_src_template;
210   ptest->framed = TRUE;
211   /* could be NULL if not relevant/needed */
212   ptest->src_caps = ctx_input_caps;
213   ptest->sink_caps = ctx_output_caps;
214   memcpy (ptest->headers, ctx_headers, sizeof (ptest->headers));
215   ptest->discard = ctx_discard;
216   /* some data that pleases caller */
217   ptest->series[0].data = data;
218   ptest->series[0].size = size;
219   ptest->series[0].num = num;
220   ptest->series[0].fpb = 1;
221   ptest->series[1].fpb = 1;
222   ptest->series[2].fpb = 1;
223   ptest->no_metadata = ctx_no_metadata;
224 }
225 
226 /*
227  * Test if the parser pushes clean data properly.
228  */
229 void
gst_parser_test_run(GstParserTest * test,GstCaps ** out_caps)230 gst_parser_test_run (GstParserTest * test, GstCaps ** out_caps)
231 {
232   buffer_verify_data_s vdata = { 0, 0, 0, NULL, 0, NULL, FALSE };
233   GstElement *element;
234   GstBuffer *buffer = NULL;
235   GstCaps *src_caps;
236   guint i, j, k;
237   guint frames = 0, size = 0;
238 
239   element = setup_element (test->factory, test->sink_template, NULL,
240       test->src_template, test->src_caps);
241 
242   /* push some setup headers */
243   for (j = 0; j < G_N_ELEMENTS (test->headers) && test->headers[j].data; j++) {
244     buffer = buffer_new (test->headers[j].data, test->headers[j].size);
245     fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
246   }
247 
248   for (j = 0; j < 3; j++) {
249     for (i = 0; i < test->series[j].num; i++) {
250       /* sanity enforcing */
251       for (k = 0; k < MAX (1, test->series[j].fpb); k++) {
252         if (!k)
253           buffer = buffer_new (test->series[j].data, test->series[j].size);
254         else {
255           buffer = gst_buffer_append (buffer,
256               buffer_new (test->series[j].data, test->series[j].size));
257         }
258       }
259       fail_unless_equals_int (gst_pad_push (srcpad, buffer), GST_FLOW_OK);
260       if (j == 0)
261         vdata.buffers_before_offset_skip++;
262       else if (j == 1)
263         vdata.offset_skip_amount += test->series[j].size * test->series[j].fpb;
264       if (j != 1) {
265         frames += test->series[j].fpb;
266         size += test->series[j].size * test->series[j].fpb;
267       }
268     }
269   }
270   gst_pad_push_event (srcpad, gst_event_new_eos ());
271 
272   if (G_LIKELY (test->framed))
273     fail_unless_equals_int (g_list_length (buffers) - test->discard, frames);
274 
275   /* if all frames are identical, do extended test,
276    * otherwise only verify total data size */
277   if (test->series[0].data && (!test->series[2].size ||
278           (test->series[0].size == test->series[2].size && test->series[2].data
279               && !memcmp (test->series[0].data, test->series[2].data,
280                   test->series[0].size)))) {
281     vdata.data_to_verify = test->series[0].data;
282     vdata.data_to_verify_size = test->series[0].size;
283     vdata.caps = test->sink_caps;
284     vdata.discard = test->discard;
285     vdata.no_metadata = test->no_metadata;
286     g_list_foreach (buffers, buffer_verify_data, &vdata);
287   } else {
288     guint datasum = 0;
289 
290     g_list_foreach (buffers, buffer_count_size, &datasum);
291     size -= test->dropped;
292     fail_unless_equals_int (datasum, size);
293   }
294 
295   src_caps = gst_pad_get_current_caps (sinkpad);
296   GST_LOG ("output caps: %" GST_PTR_FORMAT, src_caps);
297 
298   if (test->sink_caps) {
299     GST_LOG ("%" GST_PTR_FORMAT " = %" GST_PTR_FORMAT " ?", src_caps,
300         test->sink_caps);
301     fail_unless (gst_caps_is_equal (src_caps, test->sink_caps));
302   }
303 
304   if (out_caps)
305     *out_caps = src_caps;
306   else
307     gst_caps_unref (src_caps);
308 
309   cleanup_element (element);
310 }
311 
312 /*
313  * Test if the parser pushes clean data properly.
314  */
315 void
gst_parser_test_normal(guint8 * data,guint size)316 gst_parser_test_normal (guint8 * data, guint size)
317 {
318   GstParserTest ptest;
319 
320   gst_parser_test_init (&ptest, data, size, 10);
321   gst_parser_test_run (&ptest, NULL);
322 }
323 
324 /*
325  * Test if parser drains its buffers properly. Even one single frame
326  * should be drained and pushed forward when EOS occurs. This single frame
327  * case is special, since normally the parser needs more data to be sure
328  * about stream format. But it should still push the frame forward in EOS.
329  */
330 void
gst_parser_test_drain_single(guint8 * data,guint size)331 gst_parser_test_drain_single (guint8 * data, guint size)
332 {
333   GstParserTest ptest;
334 
335   gst_parser_test_init (&ptest, data, size, 1);
336   gst_parser_test_run (&ptest, NULL);
337 }
338 
339 /*
340  * Make sure that parser does not drain garbage when EOS occurs.
341  */
342 void
gst_parser_test_drain_garbage(guint8 * data,guint size,guint8 * garbage,guint gsize)343 gst_parser_test_drain_garbage (guint8 * data, guint size, guint8 * garbage,
344     guint gsize)
345 {
346   GstParserTest ptest;
347 
348   /* provide enough initial frames since it may take some parsers some
349    * time to be convinced of proper sync */
350   gst_parser_test_init (&ptest, data, size, 10);
351   ptest.series[1].data = garbage;
352   ptest.series[1].size = gsize;
353   ptest.series[1].num = 1;
354   gst_parser_test_run (&ptest, NULL);
355 }
356 
357 /*
358  * Test if parser splits a buffer that contains two frames into two
359  * separate buffers properly.
360  */
361 void
gst_parser_test_split(guint8 * data,guint size)362 gst_parser_test_split (guint8 * data, guint size)
363 {
364   GstParserTest ptest;
365 
366   gst_parser_test_init (&ptest, data, size, 10);
367   ptest.series[0].fpb = 2;
368   gst_parser_test_run (&ptest, NULL);
369 }
370 
371 /*
372  * Test if the parser skips garbage between frames properly.
373  */
374 void
gst_parser_test_skip_garbage(guint8 * data,guint size,guint8 * garbage,guint gsize)375 gst_parser_test_skip_garbage (guint8 * data, guint size, guint8 * garbage,
376     guint gsize)
377 {
378   GstParserTest ptest;
379 
380   gst_parser_test_init (&ptest, data, size, 10);
381   ptest.series[1].data = garbage;
382   ptest.series[1].size = gsize;
383   ptest.series[1].num = 1;
384   ptest.series[2].data = data;
385   ptest.series[2].size = size;
386   ptest.series[2].num = 10;
387   gst_parser_test_run (&ptest, NULL);
388 }
389 
390 /*
391  * Test if the src caps are set according to stream format.
392  */
393 void
gst_parser_test_output_caps(guint8 * data,guint size,const gchar * input_caps,const gchar * output_caps)394 gst_parser_test_output_caps (guint8 * data, guint size,
395     const gchar * input_caps, const gchar * output_caps)
396 {
397   GstParserTest ptest;
398 
399   gst_parser_test_init (&ptest, data, size, 10);
400   if (input_caps) {
401     ptest.src_caps = gst_caps_from_string (input_caps);
402     fail_unless (ptest.src_caps != NULL);
403   }
404   if (output_caps) {
405     ptest.sink_caps = gst_caps_from_string (output_caps);
406     fail_unless (ptest.sink_caps != NULL);
407   }
408   gst_parser_test_run (&ptest, NULL);
409   if (ptest.sink_caps)
410     gst_caps_unref (ptest.sink_caps);
411   if (ptest.src_caps)
412     gst_caps_unref (ptest.src_caps);
413 }
414 
415 /*
416  * Test if the src caps are set according to stream format.
417  */
418 GstCaps *
gst_parser_test_get_output_caps(guint8 * data,guint size,const gchar * input_caps)419 gst_parser_test_get_output_caps (guint8 * data, guint size,
420     const gchar * input_caps)
421 {
422   GstParserTest ptest;
423   GstCaps *out_caps;
424 
425   gst_parser_test_init (&ptest, data, size, 10);
426   if (input_caps) {
427     ptest.src_caps = gst_caps_from_string (input_caps);
428     fail_unless (ptest.src_caps != NULL);
429   }
430   gst_parser_test_run (&ptest, &out_caps);
431   if (ptest.src_caps)
432     gst_caps_unref (ptest.src_caps);
433 
434   return out_caps;
435 }
436