Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / tests / check / elements / audioecho.c
1 /* GStreamer
2  *
3  * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <gst/check/gstcheck.h>
22
23 gboolean have_eos = FALSE;
24
25 /* For ease of programming we use globals to keep refs for our floating
26  * src and sink pads we create; otherwise we always have to do get_pad,
27  * get_peer, and then remove references in every test function */
28 GstPad *mysrcpad, *mysinkpad;
29
30 #define ECHO_CAPS_STRING    \
31     "audio/x-raw-float, "               \
32     "channels = (int) 2, "              \
33     "rate = (int) 100000, "             \
34     "endianness = (int) BYTE_ORDER, "   \
35     "width = (int) 64"
36
37 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
38     GST_PAD_SINK,
39     GST_PAD_ALWAYS,
40     GST_STATIC_CAPS ("audio/x-raw-float, "
41         "channels = (int) [ 1, 2 ], "
42         "rate = (int) [ 1,  MAX ], "
43         "endianness = (int) BYTE_ORDER, " "width = (int) { 32, 64 }")
44     );
45 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
46     GST_PAD_SRC,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("audio/x-raw-float, "
49         "channels = (int) [ 1, 2 ], "
50         "rate = (int) [ 1,  MAX ], "
51         "endianness = (int) BYTE_ORDER, " "width = (int) { 32, 64 }")
52     );
53
54 static GstElement *
55 setup_echo (void)
56 {
57   GstElement *echo;
58
59   GST_DEBUG ("setup_echo");
60   echo = gst_check_setup_element ("audioecho");
61   mysrcpad = gst_check_setup_src_pad (echo, &srctemplate, NULL);
62   mysinkpad = gst_check_setup_sink_pad (echo, &sinktemplate, NULL);
63   gst_pad_set_active (mysrcpad, TRUE);
64   gst_pad_set_active (mysinkpad, TRUE);
65
66   return echo;
67 }
68
69 static void
70 cleanup_echo (GstElement * echo)
71 {
72   GST_DEBUG ("cleanup_echo");
73
74   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
75   g_list_free (buffers);
76   buffers = NULL;
77
78   gst_pad_set_active (mysrcpad, FALSE);
79   gst_pad_set_active (mysinkpad, FALSE);
80   gst_check_teardown_src_pad (echo);
81   gst_check_teardown_sink_pad (echo);
82   gst_check_teardown_element (echo);
83 }
84
85 GST_START_TEST (test_passthrough)
86 {
87   GstElement *echo;
88   GstBuffer *inbuffer, *outbuffer;
89   GstCaps *caps;
90   gdouble in[] = { 1.0, -1.0, 0.0, 0.5, -0.5, 0.0 };
91   gdouble *res;
92
93   echo = setup_echo ();
94   g_object_set (G_OBJECT (echo), "delay", (GstClockTime) 1, "intensity", 0.0,
95       "feedback", 0.0, NULL);
96   fail_unless (gst_element_set_state (echo,
97           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
98       "could not set to playing");
99
100   inbuffer = gst_buffer_new_and_alloc (sizeof (in));
101   memcpy (GST_BUFFER_DATA (inbuffer), in, sizeof (in));
102   fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, sizeof (in)) == 0);
103   caps = gst_caps_from_string (ECHO_CAPS_STRING);
104   gst_buffer_set_caps (inbuffer, caps);
105   gst_caps_unref (caps);
106   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
107
108   /* pushing gives away my reference ... */
109   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
110   /* ... but it ends up being collected on the global buffer list */
111   fail_unless_equals_int (g_list_length (buffers), 1);
112   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
113
114   res = (gdouble *) GST_BUFFER_DATA (outbuffer);
115   GST_INFO
116       ("expected %+lf %+lf %+lf %+lf %+lf %+lf real %+lf %+lf %+lf %+lf %+lf %+lf",
117       in[0], in[1], in[2], in[3], in[4], in[5], res[0], res[1], res[2], res[3],
118       res[4], res[5]);
119   fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), in, sizeof (in)) == 0);
120
121   /* cleanup */
122   cleanup_echo (echo);
123 }
124
125 GST_END_TEST;
126
127 GST_START_TEST (test_echo)
128 {
129   GstElement *echo;
130   GstBuffer *inbuffer, *outbuffer;
131   GstCaps *caps;
132   gdouble in[] = { 1.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, };
133   gdouble out[] = { 1.0, -1.0, 0.0, 0.0, 1.0, -1.0, 0.0, 0.0, 0.0, 0.0 };
134   gdouble *res;
135
136   echo = setup_echo ();
137   g_object_set (G_OBJECT (echo), "delay", (GstClockTime) 20000, "intensity",
138       1.0, "feedback", 0.0, NULL);
139   fail_unless (gst_element_set_state (echo,
140           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
141       "could not set to playing");
142
143   inbuffer = gst_buffer_new_and_alloc (sizeof (in));
144   memcpy (GST_BUFFER_DATA (inbuffer), in, sizeof (in));
145   fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, sizeof (in)) == 0);
146   caps = gst_caps_from_string (ECHO_CAPS_STRING);
147   gst_buffer_set_caps (inbuffer, caps);
148   gst_caps_unref (caps);
149   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
150
151   /* pushing gives away my reference ... */
152   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
153   /* ... but it ends up being collected on the global buffer list */
154   fail_unless_equals_int (g_list_length (buffers), 1);
155   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
156
157   res = (gdouble *) GST_BUFFER_DATA (outbuffer);
158   GST_INFO
159       ("expected %+lf %+lf %+lf %+lf %+lf %+lf %+lf %+lf %+lf %+lf real %+lf %+lf %+lf %+lf %+lf %+lf %+lf %+lf %+lf %+lf",
160       out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7], out[8],
161       out[9], res[0], res[1], res[2], res[3], res[4], res[5], res[6], res[7],
162       res[8], res[9]);
163   fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, sizeof (out)) == 0);
164
165   /* cleanup */
166   cleanup_echo (echo);
167 }
168
169 GST_END_TEST;
170
171 GST_START_TEST (test_feedback)
172 {
173   GstElement *echo;
174   GstBuffer *inbuffer, *outbuffer;
175   GstCaps *caps;
176   gdouble in[] = { 1.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, };
177   gdouble out[] = { 1.0, -1.0, 0.0, 0.0, 1.0, -1.0, 0.0, 0.0, 1.0, -1.0 };
178   gdouble *res;
179
180   echo = setup_echo ();
181   g_object_set (G_OBJECT (echo), "delay", (GstClockTime) 20000, "intensity",
182       1.0, "feedback", 1.0, NULL);
183   fail_unless (gst_element_set_state (echo,
184           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
185       "could not set to playing");
186
187   inbuffer = gst_buffer_new_and_alloc (sizeof (in));
188   memcpy (GST_BUFFER_DATA (inbuffer), in, sizeof (in));
189   fail_unless (memcmp (GST_BUFFER_DATA (inbuffer), in, sizeof (in)) == 0);
190   caps = gst_caps_from_string (ECHO_CAPS_STRING);
191   gst_buffer_set_caps (inbuffer, caps);
192   gst_caps_unref (caps);
193   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
194
195   /* pushing gives away my reference ... */
196   fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
197   /* ... but it ends up being collected on the global buffer list */
198   fail_unless_equals_int (g_list_length (buffers), 1);
199   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
200
201   res = (gdouble *) GST_BUFFER_DATA (outbuffer);
202   GST_INFO
203       ("expected %+lf %+lf %+lf %+lf %+lf %+lf %+lf %+lf %+lf %+lf real %+lf %+lf %+lf %+lf %+lf %+lf %+lf %+lf %+lf %+lf",
204       out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7], out[8],
205       out[9], res[0], res[1], res[2], res[3], res[4], res[5], res[6], res[7],
206       res[8], res[9]);
207   fail_unless (memcmp (GST_BUFFER_DATA (outbuffer), out, sizeof (out)) == 0);
208
209   /* cleanup */
210   cleanup_echo (echo);
211 }
212
213 GST_END_TEST;
214
215 static Suite *
216 audioecho_suite (void)
217 {
218   Suite *s = suite_create ("audioecho");
219   TCase *tc_chain = tcase_create ("general");
220
221   suite_add_tcase (s, tc_chain);
222   tcase_add_test (tc_chain, test_passthrough);
223   tcase_add_test (tc_chain, test_echo);
224   tcase_add_test (tc_chain, test_feedback);
225
226   return s;
227 }
228
229 GST_CHECK_MAIN (audioecho);