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