gst_plugins_base/tests/check/libs/mixer.c
changeset 2 5505e8908944
parent 0 0e761a78d257
equal deleted inserted replaced
1:4c282e7dd6d3 2:5505e8908944
       
     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/interfaces/mixer.h>
       
    30 
       
    31 #include <string.h>
       
    32 
       
    33 #define TEST_ELEMENT_TYPE (test_element_get_type())
       
    34 
       
    35 typedef struct TestElement TestElement;
       
    36 typedef struct TestElementClass TestElementClass;
       
    37 
       
    38 struct TestElement
       
    39 {
       
    40   GstElement parent;
       
    41 };
       
    42 
       
    43 struct TestElementClass
       
    44 {
       
    45   GstElementClass parent_class;
       
    46 };
       
    47 
       
    48 static void init_interface (GType type);
       
    49 static void gst_implements_interface_init (GstImplementsInterfaceClass * klass);
       
    50 
       
    51 GST_BOILERPLATE_FULL (TestElement, test_element, GstElement, GST_TYPE_ELEMENT,
       
    52     init_interface);
       
    53 
       
    54 static void
       
    55 test_element_mixer_interface_init (GstMixerClass * klass)
       
    56 {
       
    57   /* Not actually implementing any interfaces for this test atm */
       
    58 }
       
    59 
       
    60 static void
       
    61 init_interface (GType type)
       
    62 {
       
    63   static const GInterfaceInfo mixer_iface_info = {
       
    64     (GInterfaceInitFunc) test_element_mixer_interface_init,
       
    65     NULL,
       
    66     NULL,
       
    67   };
       
    68   static const GInterfaceInfo implements_iface_info = {
       
    69     (GInterfaceInitFunc) gst_implements_interface_init,
       
    70     NULL,
       
    71     NULL,
       
    72   };
       
    73 
       
    74   g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,
       
    75       &implements_iface_info);
       
    76   g_type_add_interface_static (type, GST_TYPE_MIXER, &mixer_iface_info);
       
    77 }
       
    78 
       
    79 static void
       
    80 test_element_base_init (gpointer klass)
       
    81 {
       
    82 }
       
    83 
       
    84 static void
       
    85 test_element_class_init (TestElementClass * klass)
       
    86 {
       
    87 }
       
    88 
       
    89 static gboolean
       
    90 test_element_interface_supported (GstImplementsInterface * ifacE,
       
    91     GType interface_type)
       
    92 {
       
    93   if (interface_type == GST_TYPE_MIXER)
       
    94     return TRUE;
       
    95 
       
    96   return FALSE;
       
    97 }
       
    98 
       
    99 static void
       
   100 gst_implements_interface_init (GstImplementsInterfaceClass * klass)
       
   101 {
       
   102   klass->supported = test_element_interface_supported;
       
   103 }
       
   104 
       
   105 static void
       
   106 test_element_init (TestElement * this, TestElementClass * klass)
       
   107 {
       
   108 }
       
   109 
       
   110 GST_START_TEST (test_messages)
       
   111 {
       
   112   /* Create an empty GstElement that has a GstMixer interface and then
       
   113    * send some notifications and validate them */
       
   114   GstElement *test_element =
       
   115       (GstElement *) g_object_new (TEST_ELEMENT_TYPE, NULL);
       
   116   GstBus *bus = gst_bus_new ();
       
   117   GstMixerTrack *mtrack = g_object_new (GST_TYPE_MIXER_TRACK, NULL);
       
   118   GstMixerOptions *mopts = g_object_new (GST_TYPE_MIXER_OPTIONS, NULL);
       
   119   GstMixerTrack *t;
       
   120   GstMixerOptions *o;
       
   121   gint vols_in[2] = { 50, 75 };
       
   122   gboolean mute, record;
       
   123   gint *vols_out;
       
   124   gint n_chans, i;
       
   125   const gchar *val;
       
   126   GstMessage *message;
       
   127 
       
   128   mtrack->num_channels = 2;
       
   129   mtrack->flags = GST_MIXER_TRACK_MUTE | GST_MIXER_TRACK_RECORD;
       
   130 
       
   131   gst_element_set_bus (test_element, bus);
       
   132 
       
   133   /* Test mute-toggled */
       
   134   gst_mixer_mute_toggled (GST_MIXER (test_element), mtrack, TRUE);
       
   135   message = gst_bus_poll (bus, GST_MESSAGE_ELEMENT, GST_CLOCK_TIME_NONE);
       
   136   fail_if (message == NULL);
       
   137   fail_unless (gst_mixer_message_get_type (message) ==
       
   138       GST_MIXER_MESSAGE_MUTE_TOGGLED);
       
   139   /* Test that we can pass NULL args */
       
   140   gst_mixer_message_parse_mute_toggled (message, NULL, NULL);
       
   141   /* Test the parsing */
       
   142   gst_mixer_message_parse_mute_toggled (message, &t, &mute);
       
   143   fail_unless (t == mtrack);
       
   144   fail_unless (mute == TRUE);
       
   145   gst_message_unref (message);
       
   146 
       
   147   /* Test record-toggled */
       
   148   gst_mixer_record_toggled (GST_MIXER (test_element), mtrack, TRUE);
       
   149   message = gst_bus_poll (bus, GST_MESSAGE_ELEMENT, GST_CLOCK_TIME_NONE);
       
   150   fail_if (message == NULL);
       
   151   fail_unless (gst_mixer_message_get_type (message) ==
       
   152       GST_MIXER_MESSAGE_RECORD_TOGGLED);
       
   153   gst_mixer_message_parse_record_toggled (message, NULL, NULL);
       
   154   gst_mixer_message_parse_record_toggled (message, &t, &record);
       
   155   fail_unless (t == mtrack);
       
   156   fail_unless (record == TRUE);
       
   157   gst_message_unref (message);
       
   158 
       
   159   /* Test volume-changed */
       
   160   gst_mixer_volume_changed (GST_MIXER (test_element), mtrack, vols_in);
       
   161   message = gst_bus_poll (bus, GST_MESSAGE_ELEMENT, GST_CLOCK_TIME_NONE);
       
   162   fail_if (message == NULL);
       
   163   fail_unless (gst_mixer_message_get_type (message) ==
       
   164       GST_MIXER_MESSAGE_VOLUME_CHANGED);
       
   165   gst_mixer_message_parse_volume_changed (message, NULL, NULL, NULL);
       
   166   gst_mixer_message_parse_volume_changed (message, NULL, NULL, &n_chans);
       
   167   fail_unless (n_chans == 2);
       
   168   gst_mixer_message_parse_volume_changed (message, &t, &vols_out, &n_chans);
       
   169   fail_unless (mtrack == t);
       
   170   for (i = 0; i < n_chans; i++)
       
   171     fail_unless (vols_out[i] == vols_in[i]);
       
   172 
       
   173   gst_message_unref (message);
       
   174   g_free (vols_out);
       
   175 
       
   176   /* Test option-changed */
       
   177   gst_mixer_option_changed (GST_MIXER (test_element), mopts, "TESTING");
       
   178   message = gst_bus_poll (bus, GST_MESSAGE_ELEMENT, GST_CLOCK_TIME_NONE);
       
   179   fail_if (message == NULL);
       
   180   fail_unless (gst_mixer_message_get_type (message) ==
       
   181       GST_MIXER_MESSAGE_OPTION_CHANGED);
       
   182   gst_mixer_message_parse_option_changed (message, NULL, NULL);
       
   183   gst_mixer_message_parse_option_changed (message, &o, &val);
       
   184   fail_unless (o == mopts);
       
   185   fail_unless (g_str_equal (val, "TESTING"));
       
   186   gst_message_unref (message);
       
   187 
       
   188   /* Test options-list-changed */
       
   189   gst_mixer_options_list_changed (GST_MIXER (test_element), mopts);
       
   190   message = gst_bus_poll (bus, GST_MESSAGE_ELEMENT, GST_CLOCK_TIME_NONE);
       
   191   fail_if (message == NULL);
       
   192   fail_unless (gst_mixer_message_get_type (message) ==
       
   193       GST_MIXER_MESSAGE_OPTIONS_LIST_CHANGED);
       
   194   gst_mixer_message_parse_options_list_changed (message, &o);
       
   195   fail_unless (o == mopts);
       
   196   gst_message_unref (message);
       
   197 
       
   198   /* Test mixer-changed */
       
   199   gst_mixer_mixer_changed (GST_MIXER (test_element));
       
   200   message = gst_bus_poll (bus, GST_MESSAGE_ELEMENT, GST_CLOCK_TIME_NONE);
       
   201   fail_if (message == NULL);
       
   202   fail_unless (gst_mixer_message_get_type (message) ==
       
   203       GST_MIXER_MESSAGE_MIXER_CHANGED);
       
   204   gst_message_unref (message);
       
   205 
       
   206   gst_object_unref (mtrack);
       
   207   gst_object_unref (mopts);
       
   208   gst_object_unref (bus);
       
   209   gst_object_unref (test_element);
       
   210 }
       
   211 
       
   212 GST_END_TEST;
       
   213 
       
   214 static Suite *
       
   215 mixer_suite (void)
       
   216 {
       
   217   Suite *s = suite_create ("mixer interface");
       
   218   TCase *tc_chain = tcase_create ("notifications");
       
   219 
       
   220   suite_add_tcase (s, tc_chain);
       
   221   tcase_add_test (tc_chain, test_messages);
       
   222 
       
   223   return s;
       
   224 }
       
   225 
       
   226 int
       
   227 main (int argc, char **argv)
       
   228 {
       
   229   int nf;
       
   230 
       
   231   Suite *s = mixer_suite ();
       
   232   SRunner *sr = srunner_create (s);
       
   233 
       
   234   gst_check_init (&argc, &argv);
       
   235 
       
   236   srunner_run_all (sr, CK_NORMAL);
       
   237   nf = srunner_ntests_failed (sr);
       
   238   srunner_free (sr);
       
   239 
       
   240   return nf;
       
   241 }