• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <gtest/gtest.h>
6 
7 #include "cras_dsp.h"
8 #include "cras_dsp_module.h"
9 
10 #define FILENAME_TEMPLATE "DspTest.XXXXXX"
11 
12 namespace {
13 
14 extern "C" {
cras_dsp_module_load_ladspa(struct plugin * plugin)15 struct dsp_module* cras_dsp_module_load_ladspa(struct plugin* plugin) {
16   return NULL;
17 }
18 }
19 
20 class DspTestSuite : public testing::Test {
21  protected:
SetUp()22   virtual void SetUp() {
23     strcpy(filename, FILENAME_TEMPLATE);
24     int fd = mkstemp(filename);
25     fp = fdopen(fd, "w");
26   }
27 
TearDown()28   virtual void TearDown() {
29     CloseFile();
30     unlink(filename);
31   }
32 
CloseFile()33   virtual void CloseFile() {
34     if (fp) {
35       fclose(fp);
36       fp = NULL;
37     }
38   }
39 
40   char filename[sizeof(FILENAME_TEMPLATE) + 1];
41   FILE* fp;
42 };
43 
TEST_F(DspTestSuite,Simple)44 TEST_F(DspTestSuite, Simple) {
45   const char* content =
46       "[M1]\n"
47       "library=builtin\n"
48       "label=source\n"
49       "purpose=capture\n"
50       "output_0={audio}\n"
51       "disable=(not (equal? variable \"foo\"))\n"
52       "[M2]\n"
53       "library=builtin\n"
54       "label=sink\n"
55       "purpose=capture\n"
56       "input_0={audio}\n"
57       "\n";
58   fprintf(fp, "%s", content);
59   CloseFile();
60 
61   cras_dsp_init(filename);
62   struct cras_dsp_context *ctx1, *ctx3, *ctx4;
63   ctx1 = cras_dsp_context_new(44100, "playback"); /* wrong purpose */
64   ctx3 = cras_dsp_context_new(44100, "capture");
65   ctx4 = cras_dsp_context_new(44100, "capture");
66 
67   cras_dsp_set_variable_string(ctx1, "variable", "foo");
68   cras_dsp_set_variable_string(ctx3, "variable", "bar"); /* wrong value */
69   cras_dsp_set_variable_string(ctx4, "variable", "foo");
70 
71   cras_dsp_load_pipeline(ctx1);
72   cras_dsp_load_pipeline(ctx3);
73   cras_dsp_load_pipeline(ctx4);
74 
75   /* only ctx4 should load the pipeline successfully */
76   ASSERT_EQ(NULL, cras_dsp_get_pipeline(ctx1));
77   ASSERT_EQ(NULL, cras_dsp_get_pipeline(ctx3));
78 
79   struct pipeline* pipeline = cras_dsp_get_pipeline(ctx4);
80   ASSERT_TRUE(pipeline);
81   cras_dsp_put_pipeline(ctx4);
82 
83   /* change the variable to a wrong value, and we should fail to reload. */
84   cras_dsp_set_variable_string(ctx4, "variable", "bar");
85   cras_dsp_load_pipeline(ctx4);
86   ASSERT_EQ(NULL, cras_dsp_get_pipeline(ctx4));
87 
88   /* change the variable back, and we should reload successfully. */
89   cras_dsp_set_variable_string(ctx4, "variable", "foo");
90   cras_dsp_reload_ini();
91   ASSERT_TRUE(cras_dsp_get_pipeline(ctx4));
92 
93   cras_dsp_context_free(ctx1);
94   cras_dsp_context_free(ctx3);
95   cras_dsp_context_free(ctx4);
96   cras_dsp_stop();
97 }
98 
empty_instantiate(struct dsp_module * module,unsigned long sample_rate)99 static int empty_instantiate(struct dsp_module* module,
100                              unsigned long sample_rate) {
101   return 0;
102 }
103 
empty_connect_port(struct dsp_module * module,unsigned long port,float * data_location)104 static void empty_connect_port(struct dsp_module* module,
105                                unsigned long port,
106                                float* data_location) {}
107 
empty_get_delay(struct dsp_module * module)108 static int empty_get_delay(struct dsp_module* module) {
109   return 0;
110 }
111 
empty_run(struct dsp_module * module,unsigned long sample_count)112 static void empty_run(struct dsp_module* module, unsigned long sample_count) {}
113 
empty_deinstantiate(struct dsp_module * module)114 static void empty_deinstantiate(struct dsp_module* module) {}
115 
empty_free_module(struct dsp_module * module)116 static void empty_free_module(struct dsp_module* module) {
117   free(module);
118 }
119 
empty_get_properties(struct dsp_module * module)120 static int empty_get_properties(struct dsp_module* module) {
121   return 0;
122 }
123 
empty_dump(struct dsp_module * module,struct dumper * d)124 static void empty_dump(struct dsp_module* module, struct dumper* d) {
125   dumpf(d, "built-in module\n");
126 }
127 
empty_init_module(struct dsp_module * module)128 static void empty_init_module(struct dsp_module* module) {
129   module->instantiate = &empty_instantiate;
130   module->connect_port = &empty_connect_port;
131   module->get_delay = &empty_get_delay;
132   module->run = &empty_run;
133   module->deinstantiate = &empty_deinstantiate;
134   module->free_module = &empty_free_module;
135   module->get_properties = &empty_get_properties;
136   module->dump = &empty_dump;
137 }
138 
139 }  //  namespace
140 
141 extern "C" {
cras_dsp_module_load_builtin(struct plugin * plugin)142 struct dsp_module* cras_dsp_module_load_builtin(struct plugin* plugin) {
143   struct dsp_module* module;
144   module = (struct dsp_module*)calloc(1, sizeof(struct dsp_module));
145   empty_init_module(module);
146   return module;
147 }
cras_dsp_module_set_sink_ext_module(struct dsp_module * module,struct ext_dsp_module * ext_module)148 void cras_dsp_module_set_sink_ext_module(struct dsp_module* module,
149                                          struct ext_dsp_module* ext_module) {}
150 }  // extern "C"
151 
main(int argc,char ** argv)152 int main(int argc, char** argv) {
153   ::testing::InitGoogleTest(&argc, argv);
154   return RUN_ALL_TESTS();
155 }
156