1 /* GStreamer
2 *
3 * unit test for voamrwbenc
4 *
5 * Copyright (C) <2011> Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
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
27 /* For ease of programming we use globals to keep refs for our floating
28 * src and sink pads we create; otherwise we always have to do get_pad,
29 * get_peer, and then remove references in every test function */
30 static GstPad *mysrcpad, *mysinkpad;
31
32 #if G_BYTE_ORDER == G_BIG_ENDIAN
33 #define AFORMAT "S16BE"
34 #else
35 #define AFORMAT "S16LE"
36 #endif
37
38 #define AUDIO_CAPS_STRING "audio/x-raw, " \
39 "format = (string) " AFORMAT ", "\
40 "layout = (string) interleaved, " \
41 "rate = (int) 16000, " \
42 "channels = (int) 1 "
43
44
45 #define AMRWB_CAPS_STRING "audio/AMR-WB"
46
47
48 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
49 GST_PAD_SINK,
50 GST_PAD_ALWAYS,
51 GST_STATIC_CAPS (AMRWB_CAPS_STRING));
52
53
54 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
55 GST_PAD_SRC,
56 GST_PAD_ALWAYS,
57 GST_STATIC_CAPS (AUDIO_CAPS_STRING));
58
59
60 static GstElement *
setup_voamrwbenc(void)61 setup_voamrwbenc (void)
62 {
63 GstElement *voamrwbenc;
64
65 GST_DEBUG ("setup_voamrwbenc");
66 voamrwbenc = gst_check_setup_element ("voamrwbenc");
67 /* ensure mode as expected */
68 g_object_set (voamrwbenc, "band-mode", 0, NULL);
69 mysrcpad = gst_check_setup_src_pad (voamrwbenc, &srctemplate);
70 mysinkpad = gst_check_setup_sink_pad (voamrwbenc, &sinktemplate);
71 gst_pad_set_active (mysrcpad, TRUE);
72 gst_pad_set_active (mysinkpad, TRUE);
73
74 return voamrwbenc;
75 }
76
77 static void
cleanup_voamrwbenc(GstElement * voamrwbenc)78 cleanup_voamrwbenc (GstElement * voamrwbenc)
79 {
80 GST_DEBUG ("cleanup_aacenc");
81 gst_element_set_state (voamrwbenc, GST_STATE_NULL);
82
83 gst_pad_set_active (mysrcpad, FALSE);
84 gst_pad_set_active (mysinkpad, FALSE);
85 gst_check_teardown_src_pad (voamrwbenc);
86 gst_check_teardown_sink_pad (voamrwbenc);
87 gst_check_teardown_element (voamrwbenc);
88 }
89
90 static void
do_test(void)91 do_test (void)
92 {
93 GstElement *voamrwbenc;
94 GstBuffer *inbuffer, *outbuffer;
95 GstCaps *caps;
96 gint i, num_buffers;
97 const gint nbuffers = 10;
98
99 voamrwbenc = setup_voamrwbenc ();
100 fail_unless (gst_element_set_state (voamrwbenc,
101 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
102 "could not set to playing");
103
104 /* corresponds to audio buffer mentioned in the caps */
105 inbuffer = gst_buffer_new_and_alloc (320 * nbuffers * 2);
106 /* makes valgrind's memcheck happier */
107 gst_buffer_memset (inbuffer, 0, 0, 1024 * nbuffers * 2 * 2);
108 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
109
110 gst_check_setup_events (mysrcpad, voamrwbenc, caps, GST_FORMAT_TIME);
111 gst_caps_unref (caps);
112 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
113 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
114 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
115
116 /* send eos to have all flushed if needed */
117 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()) == TRUE);
118
119 num_buffers = g_list_length (buffers);
120 fail_unless_equals_int (num_buffers, nbuffers);
121
122 /* clean up buffers */
123 for (i = 0; i < num_buffers; ++i) {
124 GstMapInfo map;
125 gsize size;
126 guint8 *data;
127 GstClockTime time, dur;
128
129 outbuffer = GST_BUFFER (buffers->data);
130 fail_if (outbuffer == NULL);
131
132 gst_buffer_map (outbuffer, &map, GST_MAP_READ);
133 data = map.data;
134 size = map.size;
135
136 /* at least for mode 0 */
137 fail_unless (size == 18);
138 fail_unless ((data[0] & 0x83) == 0);
139 fail_unless (((data[0] >> 3) & 0xF) == 0);
140
141 time = GST_BUFFER_TIMESTAMP (outbuffer);
142 dur = GST_BUFFER_DURATION (outbuffer);
143 fail_unless (time == 20 * GST_MSECOND * i);
144 fail_unless (dur == 20 * GST_MSECOND);
145 gst_buffer_unmap (outbuffer, &map);
146
147 buffers = g_list_remove (buffers, outbuffer);
148
149 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
150 gst_buffer_unref (outbuffer);
151 outbuffer = NULL;
152 }
153
154 cleanup_voamrwbenc (voamrwbenc);
155 g_list_free (buffers);
156 buffers = NULL;
157 }
158
GST_START_TEST(test_enc)159 GST_START_TEST (test_enc)
160 {
161 do_test ();
162 }
163
164 GST_END_TEST;
165
166
167 static Suite *
voamrwbenc_suite(void)168 voamrwbenc_suite (void)
169 {
170 Suite *s = suite_create ("voamrwbenc");
171 TCase *tc_chain = tcase_create ("general");
172
173 suite_add_tcase (s, tc_chain);
174 tcase_add_test (tc_chain, test_enc);
175
176 return s;
177 }
178
179 GST_CHECK_MAIN (voamrwbenc);
180