645d321c2d1aeb28afd9a3a472cdcdd7e585c6d6
[platform/upstream/gstreamer.git] / tests / check / libs / mixer.c
1 /* GStreamer
2  *
3  * unit tests for audio support library
4  *
5  * Copyright (C) 2007 Jan Schmidt <thaytan@noraisin.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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gst/check/gstcheck.h>
28
29 #include <gst/audio/mixer.h>
30
31 #include <string.h>
32
33 #define TEST_ELEMENT_TYPE (test_element_get_type())
34
35 /* FIXME 0.11: possibly remove mixer interface entirely, or simplify, or
36  * radically change */
37 typedef struct TestElement TestElement;
38 typedef struct TestElementClass TestElementClass;
39
40 struct TestElement
41 {
42   GstElement parent;
43 };
44
45 struct TestElementClass
46 {
47   GstElementClass parent_class;
48 };
49
50 GType test_element_get_type (void);
51
52 static void init_interface (GType type);
53
54 G_DEFINE_TYPE_WITH_CODE (TestElement, test_element, GST_TYPE_ELEMENT,
55     init_interface (g_define_type_id));
56
57 static void
58 test_element_mixer_interface_init (GstMixerInterface * iface)
59 {
60   /* Not actually implementing any interfaces for this test atm */
61 }
62
63 static void
64 init_interface (GType type)
65 {
66   static const GInterfaceInfo mixer_iface_info = {
67     (GInterfaceInitFunc) test_element_mixer_interface_init,
68     NULL,
69     NULL,
70   };
71
72   g_type_add_interface_static (type, GST_TYPE_MIXER, &mixer_iface_info);
73 }
74
75 static void
76 test_element_class_init (TestElementClass * klass)
77 {
78 }
79
80 static void
81 test_element_init (TestElement * this)
82 {
83 }
84
85 GST_START_TEST (test_messages)
86 {
87   /* Create an empty GstElement that has a GstMixer interface and then
88    * send some notifications and validate them */
89   GstElement *test_element =
90       (GstElement *) g_object_new (TEST_ELEMENT_TYPE, NULL);
91   GstBus *bus = gst_bus_new ();
92   GstMixerTrack *mtrack = g_object_new (GST_TYPE_MIXER_TRACK, NULL);
93   GstMixerOptions *mopts = g_object_new (GST_TYPE_MIXER_OPTIONS, NULL);
94   GstMixerTrack *t;
95   GstMixerOptions *o;
96   gint vols_in[2] = { 50, 75 };
97   gboolean mute, record;
98   gint *vols_out;
99   gint n_chans, i;
100   const gchar *val;
101   GstMessage *message;
102
103   mtrack->num_channels = 2;
104   mtrack->flags = GST_MIXER_TRACK_MUTE | GST_MIXER_TRACK_RECORD;
105
106   gst_element_set_bus (test_element, bus);
107
108   /* Test mute-toggled */
109   gst_mixer_mute_toggled (GST_MIXER (test_element), mtrack, TRUE);
110   message = gst_bus_poll (bus, GST_MESSAGE_ELEMENT, GST_CLOCK_TIME_NONE);
111   fail_if (message == NULL);
112   fail_unless (gst_mixer_message_get_type (message) ==
113       GST_MIXER_MESSAGE_MUTE_TOGGLED);
114   /* Test that we can pass NULL args */
115   gst_mixer_message_parse_mute_toggled (message, NULL, NULL);
116   /* Test the parsing */
117   gst_mixer_message_parse_mute_toggled (message, &t, &mute);
118   fail_unless (t == mtrack);
119   fail_unless (mute == TRUE);
120   gst_message_unref (message);
121
122   /* Test record-toggled */
123   gst_mixer_record_toggled (GST_MIXER (test_element), mtrack, TRUE);
124   message = gst_bus_poll (bus, GST_MESSAGE_ELEMENT, GST_CLOCK_TIME_NONE);
125   fail_if (message == NULL);
126   fail_unless (gst_mixer_message_get_type (message) ==
127       GST_MIXER_MESSAGE_RECORD_TOGGLED);
128   gst_mixer_message_parse_record_toggled (message, NULL, NULL);
129   gst_mixer_message_parse_record_toggled (message, &t, &record);
130   fail_unless (t == mtrack);
131   fail_unless (record == TRUE);
132   gst_message_unref (message);
133
134   /* Test volume-changed */
135   gst_mixer_volume_changed (GST_MIXER (test_element), mtrack, vols_in);
136   message = gst_bus_poll (bus, GST_MESSAGE_ELEMENT, GST_CLOCK_TIME_NONE);
137   fail_if (message == NULL);
138   fail_unless (gst_mixer_message_get_type (message) ==
139       GST_MIXER_MESSAGE_VOLUME_CHANGED);
140   gst_mixer_message_parse_volume_changed (message, NULL, NULL, NULL);
141   gst_mixer_message_parse_volume_changed (message, NULL, NULL, &n_chans);
142   fail_unless (n_chans == 2);
143   gst_mixer_message_parse_volume_changed (message, &t, &vols_out, &n_chans);
144   fail_unless (mtrack == t);
145   for (i = 0; i < n_chans; i++)
146     fail_unless (vols_out[i] == vols_in[i]);
147
148   gst_message_unref (message);
149   g_free (vols_out);
150
151   /* Test option-changed */
152   gst_mixer_option_changed (GST_MIXER (test_element), mopts, "TESTING");
153   message = gst_bus_poll (bus, GST_MESSAGE_ELEMENT, GST_CLOCK_TIME_NONE);
154   fail_if (message == NULL);
155   fail_unless (gst_mixer_message_get_type (message) ==
156       GST_MIXER_MESSAGE_OPTION_CHANGED);
157   gst_mixer_message_parse_option_changed (message, NULL, NULL);
158   gst_mixer_message_parse_option_changed (message, &o, &val);
159   fail_unless (o == mopts);
160   fail_unless (g_str_equal (val, "TESTING"));
161   gst_message_unref (message);
162
163   /* Test options-list-changed */
164   gst_mixer_options_list_changed (GST_MIXER (test_element), mopts);
165   message = gst_bus_poll (bus, GST_MESSAGE_ELEMENT, GST_CLOCK_TIME_NONE);
166   fail_if (message == NULL);
167   fail_unless (gst_mixer_message_get_type (message) ==
168       GST_MIXER_MESSAGE_OPTIONS_LIST_CHANGED);
169   gst_mixer_message_parse_options_list_changed (message, &o);
170   fail_unless (o == mopts);
171   gst_message_unref (message);
172
173   /* Test mixer-changed */
174   gst_mixer_mixer_changed (GST_MIXER (test_element));
175   message = gst_bus_poll (bus, GST_MESSAGE_ELEMENT, GST_CLOCK_TIME_NONE);
176   fail_if (message == NULL);
177   fail_unless (gst_mixer_message_get_type (message) ==
178       GST_MIXER_MESSAGE_MIXER_CHANGED);
179   gst_message_unref (message);
180
181   gst_object_unref (mtrack);
182   gst_object_unref (mopts);
183   gst_object_unref (bus);
184   gst_object_unref (test_element);
185 }
186
187 GST_END_TEST;
188
189 static Suite *
190 mixer_suite (void)
191 {
192   Suite *s = suite_create ("mixer interface");
193   TCase *tc_chain = tcase_create ("notifications");
194
195   suite_add_tcase (s, tc_chain);
196   tcase_add_test (tc_chain, test_messages);
197
198   return s;
199 }
200
201 int
202 main (int argc, char **argv)
203 {
204   int nf;
205
206   Suite *s = mixer_suite ();
207   SRunner *sr = srunner_create (s);
208
209   gst_check_init (&argc, &argv);
210
211   srunner_run_all (sr, CK_NORMAL);
212   nf = srunner_ntests_failed (sr);
213   srunner_free (sr);
214
215   return nf;
216 }