Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / tests / check / elements / voamrwbenc.c
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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, 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 #define AUDIO_CAPS_STRING "audio/x-raw-int, " \
33                            "rate = (int) 16000, " \
34                            "channels = (int) 1, " \
35                            "width = (int) 16, " \
36                            "depth = (int) 16, " \
37                            "signed = (boolean) true, " \
38                            "endianness = (int) BYTE_ORDER "
39
40 #define AMRWB_CAPS_STRING "audio/AMR-WB"
41
42
43 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
44     GST_PAD_SINK,
45     GST_PAD_ALWAYS,
46     GST_STATIC_CAPS (AMRWB_CAPS_STRING));
47
48
49 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
50     GST_PAD_SRC,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS (AUDIO_CAPS_STRING));
53
54
55 static GstElement *
56 setup_voamrwbenc (void)
57 {
58   GstElement *voamrwbenc;
59
60   GST_DEBUG ("setup_voamrwbenc");
61   voamrwbenc = gst_check_setup_element ("voamrwbenc");
62   /* ensure mode as expected */
63   g_object_set (voamrwbenc, "band-mode", 0, NULL);
64   mysrcpad = gst_check_setup_src_pad (voamrwbenc, &srctemplate, NULL);
65   mysinkpad = gst_check_setup_sink_pad (voamrwbenc, &sinktemplate, NULL);
66   gst_pad_set_active (mysrcpad, TRUE);
67   gst_pad_set_active (mysinkpad, TRUE);
68
69   return voamrwbenc;
70 }
71
72 static void
73 cleanup_voamrwbenc (GstElement * voamrwbenc)
74 {
75   GST_DEBUG ("cleanup_aacenc");
76   gst_element_set_state (voamrwbenc, GST_STATE_NULL);
77
78   gst_pad_set_active (mysrcpad, FALSE);
79   gst_pad_set_active (mysinkpad, FALSE);
80   gst_check_teardown_src_pad (voamrwbenc);
81   gst_check_teardown_sink_pad (voamrwbenc);
82   gst_check_teardown_element (voamrwbenc);
83 }
84
85 static void
86 do_test (void)
87 {
88   GstElement *voamrwbenc;
89   GstBuffer *inbuffer, *outbuffer;
90   GstCaps *caps;
91   gint i, num_buffers;
92   const gint nbuffers = 10;
93
94   voamrwbenc = setup_voamrwbenc ();
95   fail_unless (gst_element_set_state (voamrwbenc,
96           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
97       "could not set to playing");
98
99   /* corresponds to audio buffer mentioned in the caps */
100   inbuffer = gst_buffer_new_and_alloc (320 * nbuffers * 2);
101   /* makes valgrind's memcheck happier */
102   memset (GST_BUFFER_DATA (inbuffer), 0, GST_BUFFER_SIZE (inbuffer));
103   caps = gst_caps_from_string (AUDIO_CAPS_STRING);
104
105   gst_buffer_set_caps (inbuffer, caps);
106   gst_caps_unref (caps);
107   GST_BUFFER_TIMESTAMP (inbuffer) = 0;
108   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
109   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
110
111   /* send eos to have all flushed if needed */
112   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()) == TRUE);
113
114   num_buffers = g_list_length (buffers);
115   fail_unless_equals_int (num_buffers, nbuffers);
116
117   /* clean up buffers */
118   for (i = 0; i < num_buffers; ++i) {
119     gint size;
120     guint8 *data;
121     GstClockTime time, dur;
122
123     outbuffer = GST_BUFFER (buffers->data);
124     fail_if (outbuffer == NULL);
125
126     data = GST_BUFFER_DATA (outbuffer);
127     size = GST_BUFFER_SIZE (outbuffer);
128
129     /* at least for mode 0 */
130     fail_unless (size == 18);
131     fail_unless ((data[0] & 0x83) == 0);
132     fail_unless (((data[0] >> 3) & 0xF) == 0);
133
134     time = GST_BUFFER_TIMESTAMP (outbuffer);
135     dur = GST_BUFFER_DURATION (outbuffer);
136     fail_unless (time == 20 * GST_MSECOND * i);
137     fail_unless (dur == 20 * GST_MSECOND);
138
139     buffers = g_list_remove (buffers, outbuffer);
140
141     ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
142     gst_buffer_unref (outbuffer);
143     outbuffer = NULL;
144   }
145
146   cleanup_voamrwbenc (voamrwbenc);
147   g_list_free (buffers);
148   buffers = NULL;
149 }
150
151 GST_START_TEST (test_enc)
152 {
153   do_test ();
154 }
155
156 GST_END_TEST;
157
158
159 static Suite *
160 voamrwbenc_suite (void)
161 {
162   Suite *s = suite_create ("voamrwbenc");
163   TCase *tc_chain = tcase_create ("general");
164
165   suite_add_tcase (s, tc_chain);
166   tcase_add_test (tc_chain, test_enc);
167
168   return s;
169 }
170
171 int
172 main (int argc, char **argv)
173 {
174   int nf;
175
176   Suite *s = voamrwbenc_suite ();
177   SRunner *sr = srunner_create (s);
178
179   gst_check_init (&argc, &argv);
180
181   srunner_run_all (sr, CK_NORMAL);
182   nf = srunner_ntests_failed (sr);
183   srunner_free (sr);
184
185   return nf;
186 }