1 /* GStreamer
2  *
3  * unit test for voaacenc
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 
46 #define AAC_ADTS_CAPS_STRING "audio/mpeg, " \
47                           "mpegversion = (int) 4, " \
48                           "rate = (int) 48000, " \
49                           "channels = (int) 2, " \
50 													"stream-format = \"adts\""
51 
52 
53 static GstStaticPadTemplate sinktemplate_adts = GST_STATIC_PAD_TEMPLATE ("sink",
54     GST_PAD_SINK,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS (AAC_ADTS_CAPS_STRING));
57 
58 static GstStaticPadTemplate sinktemplate_raw = GST_STATIC_PAD_TEMPLATE ("sink",
59     GST_PAD_SINK,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS (AAC_RAW_CAPS_STRING));
62 
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_voaacenc(gboolean adts)71 setup_voaacenc (gboolean adts)
72 {
73   GstElement *voaacenc;
74 
75   GST_DEBUG ("setup_voaacenc");
76   voaacenc = gst_check_setup_element ("voaacenc");
77   mysrcpad = gst_check_setup_src_pad (voaacenc, &srctemplate);
78 
79   if (adts)
80     mysinkpad = gst_check_setup_sink_pad (voaacenc, &sinktemplate_adts);
81   else
82     mysinkpad = gst_check_setup_sink_pad (voaacenc, &sinktemplate_raw);
83 
84   gst_pad_set_active (mysrcpad, TRUE);
85   gst_pad_set_active (mysinkpad, TRUE);
86 
87   return voaacenc;
88 }
89 
90 static void
cleanup_voaacenc(GstElement * voaacenc)91 cleanup_voaacenc (GstElement * voaacenc)
92 {
93   GST_DEBUG ("cleanup_aacenc");
94   gst_element_set_state (voaacenc, GST_STATE_NULL);
95 
96   gst_pad_set_active (mysrcpad, FALSE);
97   gst_pad_set_active (mysinkpad, FALSE);
98   gst_check_teardown_src_pad (voaacenc);
99   gst_check_teardown_sink_pad (voaacenc);
100   gst_check_teardown_element (voaacenc);
101 }
102 
103 static void
do_test(gboolean adts)104 do_test (gboolean adts)
105 {
106   GstElement *voaacenc;
107   GstBuffer *inbuffer, *outbuffer;
108   GstCaps *caps;
109   gint i, num_buffers;
110   const gint nbuffers = 10;
111 
112   voaacenc = setup_voaacenc (adts);
113   fail_unless (gst_element_set_state (voaacenc,
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 
123   gst_check_setup_events (mysrcpad, voaacenc, caps, GST_FORMAT_TIME);
124   gst_caps_unref (caps);
125   GST_BUFFER_TIMESTAMP (inbuffer) = 0;
126   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
127   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
128 
129   /* send eos to have all flushed if needed */
130   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()) == TRUE);
131 
132   num_buffers = g_list_length (buffers);
133   fail_unless_equals_int (num_buffers, nbuffers);
134 
135   /* clean up buffers */
136   for (i = 0; i < num_buffers; ++i) {
137     gint header = 0, id;
138     GstMapInfo map;
139     gsize size;
140     guint8 *data;
141 
142     outbuffer = GST_BUFFER (buffers->data);
143     fail_if (outbuffer == NULL);
144 
145     gst_buffer_map (outbuffer, &map, GST_MAP_READ);
146     data = map.data;
147     size = map.size;
148 
149     if (adts) {
150       gboolean protection;
151       gint k;
152 
153       fail_if (size < 7);
154       protection = !(data[1] & 0x1);
155       /* expect only 1 raw data block */
156       k = (data[6] & 0x3) + 1;
157       fail_if (k != 1);
158 
159       header = 7;
160       if (protection)
161         header += (k - 1) * 2 + 2;
162 
163       /* check header */
164       k = GST_READ_UINT16_BE (data) & 0xFFF6;
165       /* sync */
166       fail_unless (k == 0xFFF0);
167       k = data[2];
168       /* profile */
169       fail_unless ((k >> 6) == 0x1);
170       /* rate */
171       fail_unless (((k >> 2) & 0xF) == 0x3);
172       /* channels */
173       fail_unless ((k & 0x1) == 0);
174       k = data[3];
175       fail_unless ((k >> 6) == 0x2);
176 
177     } else {
178       GstCaps *caps;
179       GstStructure *s;
180       const GValue *value;
181       GstBuffer *buf;
182       gint k;
183       GstMapInfo cmap;
184 
185       caps = gst_pad_get_current_caps (mysinkpad);
186       fail_if (caps == NULL);
187       s = gst_caps_get_structure (caps, 0);
188       fail_if (s == NULL);
189       value = gst_structure_get_value (s, "codec_data");
190       fail_if (value == NULL);
191       buf = gst_value_get_buffer (value);
192       fail_if (buf == NULL);
193       gst_buffer_map (buf, &cmap, GST_MAP_READ);
194       fail_if (cmap.size < 2);
195       k = GST_READ_UINT16_BE (cmap.data);
196       gst_buffer_unmap (buf, &cmap);
197       /* profile, rate, channels */
198       fail_unless ((k & 0xFFF8) == ((0x02 << 11) | (0x3 << 7) | (0x02 << 3)));
199       gst_caps_unref (caps);
200 
201     }
202 
203     fail_if (size <= header);
204     id = data[header] & (0x7 << 5);
205     /* allow all but ID_END or ID_LFE */
206     fail_if (id == 7 || id == 3);
207     gst_buffer_unmap (outbuffer, &map);
208 
209     buffers = g_list_remove (buffers, outbuffer);
210 
211     ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
212     gst_buffer_unref (outbuffer);
213     outbuffer = NULL;
214   }
215 
216   cleanup_voaacenc (voaacenc);
217   g_list_free (buffers);
218   buffers = NULL;
219 }
220 
GST_START_TEST(test_adts)221 GST_START_TEST (test_adts)
222 {
223   do_test (TRUE);
224 }
225 
226 GST_END_TEST;
227 
GST_START_TEST(test_raw)228 GST_START_TEST (test_raw)
229 {
230   do_test (FALSE);
231 }
232 
233 GST_END_TEST;
234 
235 static Suite *
voaacenc_suite(void)236 voaacenc_suite (void)
237 {
238   Suite *s = suite_create ("voaacenc");
239   TCase *tc_chain = tcase_create ("general");
240 
241   suite_add_tcase (s, tc_chain);
242   tcase_add_test (tc_chain, test_adts);
243   tcase_add_test (tc_chain, test_raw);
244 
245   return s;
246 }
247 
248 GST_CHECK_MAIN (voaacenc);
249