1 /* GStreamer
2  *
3  * unit test for wavenc
4  *
5  * Copyright (C) <2010> Stefan Kost <ensonic@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 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
24  * with newer GLib versions (>= 2.31.0) */
25 #define GLIB_DISABLE_DEPRECATION_WARNINGS
26 
27 #include <gst/check/gstcheck.h>
28 #include <gst/audio/audio.h>
29 #include <gst/audio/audio-enumtypes.h>
30 
31 static gboolean
bus_handler(GstBus * bus,GstMessage * message,gpointer data)32 bus_handler (GstBus * bus, GstMessage * message, gpointer data)
33 {
34   GMainLoop *loop = (GMainLoop *) data;
35 
36   switch (message->type) {
37     case GST_MESSAGE_EOS:
38       g_main_loop_quit (loop);
39       break;
40     case GST_MESSAGE_WARNING:
41     case GST_MESSAGE_ERROR:{
42       GError *gerror;
43       gchar *debug;
44 
45       gst_message_parse_error (message, &gerror, &debug);
46       gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
47       gst_message_unref (message);
48       g_error_free (gerror);
49       g_free (debug);
50       g_main_loop_quit (loop);
51       break;
52     }
53     default:
54       break;
55   }
56 
57   return TRUE;
58 }
59 
60 /*
61  * gst-launch-1.0 \
62  * audiotestsrc freq=440 num-buffers=100 ! interleave name=i ! audioconvert ! wavenc ! filesink location=/tmp/mc.wav \
63  * audiotestsrc freq=880 num-buffers=100 ! i.
64  * ...
65  *
66  * http://www.microsoft.com/whdc/device/audio/multichaud.mspx
67  */
68 
69 static void
make_n_channel_wav(const gint channels,const GValueArray * arr)70 make_n_channel_wav (const gint channels, const GValueArray * arr)
71 {
72   GstElement *pipeline;
73   GstElement **audiotestsrc, *interleave, *wavenc, *conv, *fakesink;
74   GstBus *bus;
75   GMainLoop *loop;
76   guint i;
77   guint bus_watch = 0;
78 
79   audiotestsrc = g_new0 (GstElement *, channels);
80 
81   pipeline = gst_pipeline_new ("pipeline");
82   fail_unless (pipeline != NULL);
83 
84   interleave = gst_element_factory_make ("interleave", NULL);
85   fail_unless (interleave != NULL);
86   g_object_set (interleave, "channel-positions", arr, NULL);
87   gst_bin_add (GST_BIN (pipeline), interleave);
88 
89   if (G_BYTE_ORDER == G_BIG_ENDIAN) {
90     /* we're not here to test orc; audioconvert misbehaves on ppc32 */
91     g_setenv ("ORC_CODE", "backup", 1);
92     conv = gst_element_factory_make ("audioconvert", NULL);
93   } else {
94     conv = gst_element_factory_make ("identity", NULL);
95   }
96   fail_unless (conv != NULL);
97   gst_bin_add (GST_BIN (pipeline), conv);
98   fail_unless (gst_element_link (interleave, conv));
99 
100   wavenc = gst_element_factory_make ("wavenc", NULL);
101   fail_unless (wavenc != NULL);
102   gst_bin_add (GST_BIN (pipeline), wavenc);
103   fail_unless (gst_element_link (conv, wavenc));
104 
105   fakesink = gst_element_factory_make ("fakesink", NULL);
106   fail_unless (fakesink != NULL);
107   gst_bin_add (GST_BIN (pipeline), fakesink);
108   fail_unless (gst_element_link (wavenc, fakesink));
109 
110   for (i = 0; i < channels; i++) {
111     audiotestsrc[i] = gst_element_factory_make ("audiotestsrc", NULL);
112     fail_unless (audiotestsrc[i] != NULL);
113     g_object_set (G_OBJECT (audiotestsrc[i]), "wave", 0, "freq",
114         440.0 * (i + 1), "num-buffers", 100, NULL);
115     gst_bin_add (GST_BIN (pipeline), audiotestsrc[i]);
116     fail_unless (gst_element_link (audiotestsrc[i], interleave));
117   }
118 
119   loop = g_main_loop_new (NULL, TRUE);
120   fail_unless (loop != NULL);
121 
122   bus = gst_element_get_bus (pipeline);
123   fail_unless (bus != NULL);
124   bus_watch = gst_bus_add_watch (bus, bus_handler, loop);
125   gst_object_unref (bus);
126 
127   gst_element_set_state (pipeline, GST_STATE_PLAYING);
128   g_main_loop_run (loop);
129   gst_element_set_state (pipeline, GST_STATE_NULL);
130 
131   gst_object_unref (pipeline);
132   g_free (audiotestsrc);
133 
134   g_main_loop_unref (loop);
135   g_source_remove (bus_watch);
136 }
137 
GST_START_TEST(test_encode_stereo)138 GST_START_TEST (test_encode_stereo)
139 {
140   GValueArray *arr;
141   GValue val = { 0, };
142 
143   arr = g_value_array_new (2);
144   g_value_init (&val, GST_TYPE_AUDIO_CHANNEL_POSITION);
145   g_value_set_enum (&val, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT);
146   g_value_array_append (arr, &val);
147   g_value_reset (&val);
148   g_value_set_enum (&val, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT);
149   g_value_array_append (arr, &val);
150   g_value_unset (&val);
151 
152   make_n_channel_wav (2, arr);
153   g_value_array_free (arr);
154 }
155 
156 GST_END_TEST;
157 
158 #if 0
159 GST_START_TEST (test_encode_multichannel)
160 {
161   GValueArray *arr;
162   GValue val = { 0, };
163 
164   arr = g_value_array_new (6);
165   g_value_init (&val, GST_TYPE_AUDIO_CHANNEL_POSITION);
166   g_value_set_enum (&val, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT);
167   g_value_array_append (arr, &val);
168   g_value_reset (&val);
169   g_value_set_enum (&val, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT);
170   g_value_array_append (arr, &val);
171   g_value_reset (&val);
172   g_value_set_enum (&val, GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER);
173   g_value_array_append (arr, &val);
174   g_value_reset (&val);
175   g_value_set_enum (&val, GST_AUDIO_CHANNEL_POSITION_LFE);
176   g_value_array_append (arr, &val);
177   g_value_reset (&val);
178   g_value_set_enum (&val, GST_AUDIO_CHANNEL_POSITION_REAR_LEFT);
179   g_value_array_append (arr, &val);
180   g_value_reset (&val);
181   g_value_set_enum (&val, GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT);
182   g_value_array_append (arr, &val);
183   g_value_unset (&val);
184 
185   make_n_channel_wav (6);
186 }
187 
188 GST_END_TEST;
189 #endif
190 
191 static Suite *
wavenc_suite(void)192 wavenc_suite (void)
193 {
194   Suite *s = suite_create ("wavenc");
195   TCase *tc_chain = tcase_create ("general");
196 
197   suite_add_tcase (s, tc_chain);
198   tcase_add_test (tc_chain, test_encode_stereo);
199   /* FIXME: improve wavenc
200      tcase_add_test (tc_chain, test_encode_multichannel);
201    */
202 
203   return s;
204 }
205 
206 GST_CHECK_MAIN (wavenc);
207