1 /* GStreamer
2 *
3 * unit test for faac
4 *
5 * Copyright (C) <2009> Mark Nauwelaerts <mnauw@users.sf.net>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #include <unistd.h>
24
25 #include <gst/check/gstcheck.h>
26 #include <gst/audio/audio.h>
27
28 /* For ease of programming we use globals to keep refs for our floating
29 * src and sink pads we create; otherwise we always have to do get_pad,
30 * get_peer, and then remove references in every test function */
31 static GstPad *mysrcpad, *mysinkpad;
32
33 #define AUDIO_CAPS_STRING "audio/x-raw, " \
34 "format = (string) " GST_AUDIO_NE (S16) ", "\
35 "layout = (string) interleaved, " \
36 "rate = (int) 48000, " \
37 "channels = (int) 2, " \
38 "channel-mask = (bitmask) 3"
39
40 #define AAC_RAW_CAPS_STRING "audio/mpeg, " \
41 "mpegversion = (int) 4, " \
42 "rate = (int) 48000, " \
43 "channels = (int) 2, " \
44 "stream-format = \"raw\"," \
45 "base-profile = \"lc\""
46
47 #define AAC_ADTS_CAPS_STRING "audio/mpeg, " \
48 "mpegversion = (int) 4, " \
49 "rate = (int) 48000, " \
50 "channels = (int) 2, " \
51 "stream-format = \"adts\"," \
52 "base-profile = \"lc\""
53
54 static GstStaticPadTemplate sinktemplate_adts = GST_STATIC_PAD_TEMPLATE ("sink",
55 GST_PAD_SINK,
56 GST_PAD_ALWAYS,
57 GST_STATIC_CAPS (AAC_ADTS_CAPS_STRING));
58
59 static GstStaticPadTemplate sinktemplate_raw = GST_STATIC_PAD_TEMPLATE ("sink",
60 GST_PAD_SINK,
61 GST_PAD_ALWAYS,
62 GST_STATIC_CAPS (AAC_RAW_CAPS_STRING));
63
64 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
65 GST_PAD_SRC,
66 GST_PAD_ALWAYS,
67 GST_STATIC_CAPS (AUDIO_CAPS_STRING));
68
69
70 static GstElement *
setup_faac(gboolean adts)71 setup_faac (gboolean adts)
72 {
73 GstElement *faac;
74
75 GST_DEBUG ("setup_faac");
76 faac = gst_check_setup_element ("faac");
77 mysrcpad = gst_check_setup_src_pad (faac, &srctemplate);
78
79 if (adts)
80 mysinkpad = gst_check_setup_sink_pad (faac, &sinktemplate_adts);
81 else
82 mysinkpad = gst_check_setup_sink_pad (faac, &sinktemplate_raw);
83
84 gst_pad_set_active (mysrcpad, TRUE);
85 gst_pad_set_active (mysinkpad, TRUE);
86
87 return faac;
88 }
89
90 static void
cleanup_faac(GstElement * faac)91 cleanup_faac (GstElement * faac)
92 {
93 GST_DEBUG ("cleanup_faac");
94 gst_element_set_state (faac, GST_STATE_NULL);
95
96 gst_pad_set_active (mysrcpad, FALSE);
97 gst_pad_set_active (mysinkpad, FALSE);
98 gst_check_teardown_src_pad (faac);
99 gst_check_teardown_sink_pad (faac);
100 gst_check_teardown_element (faac);
101 }
102
103 static void
do_test(gboolean adts)104 do_test (gboolean adts)
105 {
106 GstElement *faac;
107 GstBuffer *inbuffer, *outbuffer;
108 GstCaps *caps;
109 gint i, num_buffers;
110 const gint nbuffers = 10;
111
112 faac = setup_faac (adts);
113 fail_unless (gst_element_set_state (faac,
114 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
115 "could not set to playing");
116
117 /* corresponds to audio buffer mentioned in the caps */
118 inbuffer = gst_buffer_new_and_alloc (1024 * nbuffers * 2 * 2);
119 /* makes valgrind's memcheck happier */
120 gst_buffer_memset (inbuffer, 0, 0, 1024 * nbuffers * 2 * 2);
121 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
122 gst_check_setup_events (mysrcpad, faac, caps, GST_FORMAT_TIME);
123 gst_caps_unref (caps);
124 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
125 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
126 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
127
128 /* send eos to have all flushed if needed */
129 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()) == TRUE);
130
131 num_buffers = g_list_length (buffers);
132 fail_unless_equals_int (num_buffers, nbuffers + 1);
133
134 /* clean up buffers */
135 for (i = 0; i < num_buffers; ++i) {
136 gint header = 0, id;
137 GstMapInfo map;
138 gsize size;
139 guint8 *data;
140
141 outbuffer = GST_BUFFER (buffers->data);
142 fail_if (outbuffer == NULL);
143
144 gst_buffer_map (outbuffer, &map, GST_MAP_READ);
145 data = map.data;
146 size = map.size;
147
148 if (adts) {
149 gboolean protection;
150 gint k;
151
152 fail_if (size < 7);
153 protection = !(data[1] & 0x1);
154 /* expect only 1 raw data block */
155 k = (data[6] & 0x3) + 1;
156 fail_if (k != 1);
157
158 header = 7;
159 if (protection)
160 header += (k - 1) * 2 + 2;
161
162 /* check header */
163 k = GST_READ_UINT16_BE (data) & 0xFFF6;
164 /* sync */
165 fail_unless (k == 0xFFF0);
166 k = data[2];
167 /* profile */
168 fail_unless ((k >> 6) == 0x1);
169 /* rate */
170 fail_unless (((k >> 2) & 0xF) == 0x3);
171 /* channels */
172 fail_unless ((k & 0x1) == 0);
173 k = data[3];
174 fail_unless ((k >> 6) == 0x2);
175 } else {
176 GstCaps *caps;
177 GstStructure *s;
178 const GValue *value;
179 GstBuffer *buf;
180 gint k;
181 GstMapInfo cmap;
182
183 caps = gst_pad_get_current_caps (mysinkpad);
184 fail_if (caps == NULL);
185 s = gst_caps_get_structure (caps, 0);
186 fail_if (s == NULL);
187 value = gst_structure_get_value (s, "codec_data");
188 fail_if (value == NULL);
189 buf = gst_value_get_buffer (value);
190 fail_if (buf == NULL);
191 gst_buffer_map (buf, &cmap, GST_MAP_READ);
192 fail_if (cmap.size < 2);
193 k = GST_READ_UINT16_BE (cmap.data);
194 gst_buffer_unmap (buf, &cmap);
195 /* profile, rate, channels */
196 fail_unless ((k & 0xFFF8) == ((0x02 << 11) | (0x3 << 7) | (0x02 << 3)));
197 gst_caps_unref (caps);
198 }
199
200 fail_if (size <= header);
201 id = data[header] & (0x7 << 5);
202 /* allow all but ID_END or ID_LFE */
203 fail_if (id == 7 || id == 3);
204 gst_buffer_unmap (outbuffer, &map);
205
206 buffers = g_list_remove (buffers, outbuffer);
207
208 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
209 gst_buffer_unref (outbuffer);
210 outbuffer = NULL;
211 }
212
213 cleanup_faac (faac);
214 g_list_free (buffers);
215 buffers = NULL;
216 }
217
GST_START_TEST(test_adts)218 GST_START_TEST (test_adts)
219 {
220 do_test (TRUE);
221 }
222
223 GST_END_TEST;
224
GST_START_TEST(test_raw)225 GST_START_TEST (test_raw)
226 {
227 do_test (FALSE);
228 }
229
230 GST_END_TEST;
231
232 static Suite *
faac_suite(void)233 faac_suite (void)
234 {
235 Suite *s = suite_create ("faac");
236 TCase *tc_chain = tcase_create ("general");
237
238 suite_add_tcase (s, tc_chain);
239 tcase_add_test (tc_chain, test_adts);
240 tcase_add_test (tc_chain, test_raw);
241
242 return s;
243 }
244
245 GST_CHECK_MAIN (faac);
246