summaryrefslogtreecommitdiff
path: root/cras/src/tests/loopback_iodev_unittest.cc
blob: fde5037516fee2fe77359dda1b02e46590af9d0f (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright (c) 2013 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 <stdio.h>
#include <stdlib.h>

extern "C" {
// For audio_thread_log.h use.
struct audio_thread_event_log* atlog;
int atlog_rw_shm_fd;
int atlog_ro_shm_fd;
#include "audio_thread_log.h"
#include "cras_audio_area.h"
#include "cras_iodev.h"
#include "cras_iodev_list.h"
#include "cras_loopback_iodev.h"
#include "cras_shm.h"
#include "cras_types.h"
#include "dev_stream.h"
#include "utlist.h"
}

namespace {

static const unsigned int kBufferFrames = 16384;
static const unsigned int kFrameBytes = 4;
static const unsigned int kBufferSize = kBufferFrames * kFrameBytes;

static struct timespec time_now;
static cras_audio_area* mock_audio_area;
static loopback_hook_data_t loop_hook;
static struct cras_iodev* enabled_dev;
static unsigned int cras_iodev_list_add_input_called;
static unsigned int cras_iodev_list_rm_input_called;
static unsigned int cras_iodev_list_set_device_enabled_callback_called;
static device_enabled_callback_t device_enabled_callback_cb;
static device_disabled_callback_t device_disabled_callback_cb;
static void* device_enabled_callback_cb_data;
static int cras_iodev_list_register_loopback_called;
static int cras_iodev_list_unregister_loopback_called;

static char* atlog_name;

class LoopBackTestSuite : public testing::Test {
 protected:
  virtual void SetUp() {
    mock_audio_area = (cras_audio_area*)calloc(
        1, sizeof(*mock_audio_area) + sizeof(cras_channel_area) * 2);
    for (unsigned int i = 0; i < kBufferSize; i++) {
      buf_[i] = rand();
    }
    fmt_.frame_rate = 48000;
    fmt_.num_channels = 2;
    fmt_.format = SND_PCM_FORMAT_S16_LE;

    loop_in_ = loopback_iodev_create(LOOPBACK_POST_MIX_PRE_DSP);
    EXPECT_EQ(1, cras_iodev_list_add_input_called);
    loop_in_->format = &fmt_;

    loop_hook = NULL;
    cras_iodev_list_add_input_called = 0;
    cras_iodev_list_rm_input_called = 0;
    cras_iodev_list_set_device_enabled_callback_called = 0;
    cras_iodev_list_register_loopback_called = 0;
    cras_iodev_list_unregister_loopback_called = 0;

    ASSERT_FALSE(asprintf(&atlog_name, "/ATlog-%d", getpid()) < 0);
    /* To avoid un-used variable warning. */
    atlog_rw_shm_fd = atlog_ro_shm_fd = -1;
    atlog = audio_thread_event_log_init(atlog_name);
  }

  virtual void TearDown() {
    loopback_iodev_destroy(loop_in_);
    EXPECT_EQ(1, cras_iodev_list_rm_input_called);
    EXPECT_EQ(NULL, device_enabled_callback_cb);
    EXPECT_EQ(NULL, device_disabled_callback_cb);
    free(mock_audio_area);
    audio_thread_event_log_deinit(atlog, atlog_name);
    free(atlog_name);
  }

  uint8_t buf_[kBufferSize];
  struct cras_audio_format fmt_;
  struct cras_iodev* loop_in_;
};

TEST_F(LoopBackTestSuite, InstallLoopHook) {
  struct cras_iodev iodev;
  struct timespec tstamp;

  iodev.direction = CRAS_STREAM_OUTPUT;
  iodev.format = &fmt_;
  iodev.streams = NULL;
  iodev.info.idx = 123;
  enabled_dev = &iodev;

  // Open loopback devices.
  EXPECT_EQ(0, loop_in_->configure_dev(loop_in_));
  EXPECT_EQ(1, cras_iodev_list_set_device_enabled_callback_called);
  EXPECT_EQ(1, cras_iodev_list_register_loopback_called);

  // Signal an output device is enabled.
  device_enabled_callback_cb(&iodev, device_enabled_callback_cb_data);

  // Expect that a hook was added to the iodev
  EXPECT_EQ(2, cras_iodev_list_register_loopback_called);
  ASSERT_NE(reinterpret_cast<loopback_hook_data_t>(NULL), loop_hook);

  // Check zero frames queued.
  EXPECT_EQ(0, loop_in_->frames_queued(loop_in_, &tstamp));

  device_disabled_callback_cb(&iodev, device_enabled_callback_cb_data);
  EXPECT_EQ(1, cras_iodev_list_unregister_loopback_called);
  EXPECT_EQ(3, cras_iodev_list_register_loopback_called);

  enabled_dev->info.idx = 456;
  device_enabled_callback_cb(&iodev, device_enabled_callback_cb_data);
  EXPECT_EQ(4, cras_iodev_list_register_loopback_called);

  // Close loopback devices.
  EXPECT_EQ(0, loop_in_->close_dev(loop_in_));
  EXPECT_EQ(2, cras_iodev_list_unregister_loopback_called);
  EXPECT_EQ(2, cras_iodev_list_set_device_enabled_callback_called);
}

TEST_F(LoopBackTestSuite, SelectDevFromAToB) {
  struct cras_iodev iodev1, iodev2;

  iodev1.direction = CRAS_STREAM_OUTPUT;
  iodev2.direction = CRAS_STREAM_OUTPUT;
  enabled_dev = &iodev1;

  enabled_dev->info.idx = 111;
  EXPECT_EQ(0, loop_in_->configure_dev(loop_in_));
  EXPECT_EQ(1, cras_iodev_list_set_device_enabled_callback_called);
  EXPECT_EQ(1, cras_iodev_list_register_loopback_called);

  /* Not the current sender being disabled, assert unregister not called. */
  iodev2.info.idx = 222;
  device_disabled_callback_cb(&iodev2, device_enabled_callback_cb_data);
  EXPECT_EQ(0, cras_iodev_list_unregister_loopback_called);
  EXPECT_EQ(1, cras_iodev_list_register_loopback_called);

  enabled_dev = &iodev2;
  device_disabled_callback_cb(&iodev1, device_enabled_callback_cb_data);
  EXPECT_EQ(1, cras_iodev_list_unregister_loopback_called);
  EXPECT_EQ(2, cras_iodev_list_register_loopback_called);

  EXPECT_EQ(0, loop_in_->close_dev(loop_in_));
}

// Test how loopback works if there isn't any output devices open.
TEST_F(LoopBackTestSuite, OpenIdleSystem) {
  cras_audio_area* area;
  unsigned int nread = 1024;
  struct timespec tstamp;
  int rc;

  // No active output device.
  enabled_dev = NULL;
  time_now.tv_sec = 100;
  time_now.tv_nsec = 0;

  EXPECT_EQ(0, loop_in_->configure_dev(loop_in_));
  EXPECT_EQ(1, cras_iodev_list_set_device_enabled_callback_called);

  // Should be 480 samples after 480/frame rate seconds
  time_now.tv_nsec += 480 * 1e9 / 48000;
  EXPECT_EQ(480, loop_in_->frames_queued(loop_in_, &tstamp));

  // Verify frames from loopback record.
  loop_in_->get_buffer(loop_in_, &area, &nread);
  EXPECT_EQ(480, nread);
  memset(buf_, 0, nread * kFrameBytes);
  rc = memcmp(area->channels[0].buf, buf_, nread * kFrameBytes);
  EXPECT_EQ(0, rc);
  loop_in_->put_buffer(loop_in_, nread);

  // Check zero frames queued.
  EXPECT_EQ(0, loop_in_->frames_queued(loop_in_, &tstamp));

  EXPECT_EQ(0, loop_in_->close_dev(loop_in_));
}

TEST_F(LoopBackTestSuite, SimpleLoopback) {
  cras_audio_area* area;
  unsigned int nframes = 1024;
  unsigned int nread = 1024;
  int rc;
  struct cras_iodev iodev;
  struct dev_stream stream;
  struct timespec tstamp;

  iodev.streams = &stream;
  enabled_dev = &iodev;

  loop_in_->configure_dev(loop_in_);
  ASSERT_NE(reinterpret_cast<void*>(NULL), loop_hook);

  // Loopback callback for the hook.
  loop_hook(buf_, nframes, &fmt_, loop_in_);

  // Verify frames from loopback record.
  loop_in_->get_buffer(loop_in_, &area, &nread);
  EXPECT_EQ(nframes, nread);
  rc = memcmp(area->channels[0].buf, buf_, nframes * kFrameBytes);
  EXPECT_EQ(0, rc);
  loop_in_->put_buffer(loop_in_, nread);

  // Check zero frames queued.
  EXPECT_EQ(0, loop_in_->frames_queued(loop_in_, &tstamp));

  EXPECT_EQ(0, loop_in_->close_dev(loop_in_));
}

// TODO(chinyue): Test closing last iodev while streaming loopback data.

/* Stubs */
extern "C" {

void cras_audio_area_config_buf_pointers(struct cras_audio_area* area,
                                         const struct cras_audio_format* fmt,
                                         uint8_t* base_buffer) {
  mock_audio_area->channels[0].buf = base_buffer;
}

void cras_iodev_free_audio_area(struct cras_iodev* iodev) {}

void cras_iodev_free_format(struct cras_iodev* iodev) {}

void cras_iodev_init_audio_area(struct cras_iodev* iodev, int num_channels) {
  iodev->area = mock_audio_area;
}

void cras_iodev_add_node(struct cras_iodev* iodev, struct cras_ionode* node) {
  DL_APPEND(iodev->nodes, node);
}

void cras_iodev_set_active_node(struct cras_iodev* iodev,
                                struct cras_ionode* node) {}
void cras_iodev_list_register_loopback(enum CRAS_LOOPBACK_TYPE loopback_type,
                                       unsigned int output_dev_idx,
                                       loopback_hook_data_t hook_data,
                                       loopback_hook_control_t hook_start,
                                       unsigned int loopback_dev_idx) {
  cras_iodev_list_register_loopback_called++;
  loop_hook = hook_data;
}

void cras_iodev_list_unregister_loopback(enum CRAS_LOOPBACK_TYPE loopback_type,
                                         unsigned int output_dev_idx,
                                         unsigned int loopback_dev_idx) {
  cras_iodev_list_unregister_loopback_called++;
}

int cras_iodev_list_add_input(struct cras_iodev* input) {
  cras_iodev_list_add_input_called++;
  return 0;
}

int cras_iodev_list_rm_input(struct cras_iodev* input) {
  cras_iodev_list_rm_input_called++;
  return 0;
}

int cras_iodev_list_set_device_enabled_callback(
    device_enabled_callback_t enabled_cb,
    device_disabled_callback_t disabled_cb,
    void* cb_data) {
  cras_iodev_list_set_device_enabled_callback_called++;
  device_enabled_callback_cb = enabled_cb;
  device_disabled_callback_cb = disabled_cb;
  device_enabled_callback_cb_data = cb_data;
  return 0;
}

int clock_gettime(clockid_t clk_id, struct timespec* tp) {
  *tp = time_now;
  return 0;
}

struct cras_iodev* cras_iodev_list_get_first_enabled_iodev(
    enum CRAS_STREAM_DIRECTION direction) {
  return enabled_dev;
}

}  // extern "C"

}  //  namespace

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