summaryrefslogtreecommitdiff
path: root/cras/src/tests/dsp_unittest.cc
blob: b1f4c4ee6726b858ad582f942834d2377fed848e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <gtest/gtest.h>

#include "cras_dsp.h"
#include "cras_dsp_module.h"

#define FILENAME_TEMPLATE "DspTest.XXXXXX"

namespace {

extern "C" {
struct dsp_module* cras_dsp_module_load_ladspa(struct plugin* plugin) {
  return NULL;
}
}

class DspTestSuite : public testing::Test {
 protected:
  virtual void SetUp() {
    strcpy(filename, FILENAME_TEMPLATE);
    int fd = mkstemp(filename);
    fp = fdopen(fd, "w");
  }

  virtual void TearDown() {
    CloseFile();
    unlink(filename);
  }

  virtual void CloseFile() {
    if (fp) {
      fclose(fp);
      fp = NULL;
    }
  }

  char filename[sizeof(FILENAME_TEMPLATE) + 1];
  FILE* fp;
};

TEST_F(DspTestSuite, Simple) {
  const char* content =
      "[M1]\n"
      "library=builtin\n"
      "label=source\n"
      "purpose=capture\n"
      "output_0={audio}\n"
      "disable=(not (equal? variable \"foo\"))\n"
      "[M2]\n"
      "library=builtin\n"
      "label=sink\n"
      "purpose=capture\n"
      "input_0={audio}\n"
      "\n";
  fprintf(fp, "%s", content);
  CloseFile();

  cras_dsp_init(filename);
  struct cras_dsp_context *ctx1, *ctx3, *ctx4;
  ctx1 = cras_dsp_context_new(44100, "playback"); /* wrong purpose */
  ctx3 = cras_dsp_context_new(44100, "capture");
  ctx4 = cras_dsp_context_new(44100, "capture");

  cras_dsp_set_variable_string(ctx1, "variable", "foo");
  cras_dsp_set_variable_string(ctx3, "variable", "bar"); /* wrong value */
  cras_dsp_set_variable_string(ctx4, "variable", "foo");

  cras_dsp_load_pipeline(ctx1);
  cras_dsp_load_pipeline(ctx3);
  cras_dsp_load_pipeline(ctx4);

  /* only ctx4 should load the pipeline successfully */
  ASSERT_EQ(NULL, cras_dsp_get_pipeline(ctx1));
  ASSERT_EQ(NULL, cras_dsp_get_pipeline(ctx3));

  struct pipeline* pipeline = cras_dsp_get_pipeline(ctx4);
  ASSERT_TRUE(pipeline);
  cras_dsp_put_pipeline(ctx4);

  /* change the variable to a wrong value, and we should fail to reload. */
  cras_dsp_set_variable_string(ctx4, "variable", "bar");
  cras_dsp_load_pipeline(ctx4);
  ASSERT_EQ(NULL, cras_dsp_get_pipeline(ctx4));

  /* change the variable back, and we should reload successfully. */
  cras_dsp_set_variable_string(ctx4, "variable", "foo");
  cras_dsp_reload_ini();
  ASSERT_TRUE(cras_dsp_get_pipeline(ctx4));

  cras_dsp_context_free(ctx1);
  cras_dsp_context_free(ctx3);
  cras_dsp_context_free(ctx4);
  cras_dsp_stop();
}

static int empty_instantiate(struct dsp_module* module,
                             unsigned long sample_rate) {
  return 0;
}

static void empty_connect_port(struct dsp_module* module,
                               unsigned long port,
                               float* data_location) {}

static int empty_get_delay(struct dsp_module* module) {
  return 0;
}

static void empty_run(struct dsp_module* module, unsigned long sample_count) {}

static void empty_deinstantiate(struct dsp_module* module) {}

static void empty_free_module(struct dsp_module* module) {
  free(module);
}

static int empty_get_properties(struct dsp_module* module) {
  return 0;
}

static void empty_dump(struct dsp_module* module, struct dumper* d) {
  dumpf(d, "built-in module\n");
}

static void empty_init_module(struct dsp_module* module) {
  module->instantiate = &empty_instantiate;
  module->connect_port = &empty_connect_port;
  module->get_delay = &empty_get_delay;
  module->run = &empty_run;
  module->deinstantiate = &empty_deinstantiate;
  module->free_module = &empty_free_module;
  module->get_properties = &empty_get_properties;
  module->dump = &empty_dump;
}

}  //  namespace

extern "C" {
struct dsp_module* cras_dsp_module_load_builtin(struct plugin* plugin) {
  struct dsp_module* module;
  module = (struct dsp_module*)calloc(1, sizeof(struct dsp_module));
  empty_init_module(module);
  return module;
}
void cras_dsp_module_set_sink_ext_module(struct dsp_module* module,
                                         struct ext_dsp_module* ext_module) {}
}  // extern "C"

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}