summaryrefslogtreecommitdiff
path: root/cras/src/tests/input_data_unittest.cc
blob: 3c6ae9f180f3d9f47f7630ad94e0207e9f44f69d (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
// Copyright 2019 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>

extern "C" {
#include "buffer_share.c"
#include "cras_audio_area.h"
#include "cras_rstream.h"
#include "input_data.h"
}

namespace {

#define FAKE_CRAS_APM_PTR reinterpret_cast<struct cras_apm*>(0x99)

#ifdef HAVE_WEBRTC_APM
static struct cras_audio_area apm_area;
static unsigned int cras_apm_list_process_offset_val;
static unsigned int cras_apm_list_process_called;
static struct cras_apm* cras_apm_list_get_active_ret = NULL;
static bool cras_apm_list_get_use_tuned_settings_val;
#endif  // HAVE_WEBRTC_APM
static float cras_rstream_get_volume_scaler_val;

TEST(InputData, GetForInputStream) {
  void* dev_ptr = reinterpret_cast<void*>(0x123);
  struct input_data* data;
  struct cras_rstream stream;
  struct buffer_share* offsets;
  struct cras_audio_area* area;
  struct cras_audio_area dev_area;
  unsigned int offset;

#ifdef HAVE_WEBRTC_APM
  cras_apm_list_process_called = 0;
#endif  // HAVE_WEBRTC_APM
  stream.stream_id = 111;

  data = input_data_create(dev_ptr);
  data->ext.configure(&data->ext, 8192, 2, 48000);

  // Prepare offsets data for 2 streams.
  offsets = buffer_share_create(8192);
  buffer_share_add_id(offsets, 111, NULL);
  buffer_share_add_id(offsets, 222, NULL);
  buffer_share_offset_update(offsets, 111, 2048);

  dev_area.frames = 600;
  data->area = &dev_area;

  stream.apm_list = NULL;
  input_data_get_for_stream(data, &stream, offsets, &area, &offset);

  // Assert offset is clipped by area->frames
  EXPECT_EQ(600, area->frames);
  EXPECT_EQ(600, offset);
#ifdef HAVE_WEBRTC_APM
  EXPECT_EQ(0, cras_apm_list_process_called);
  cras_apm_list_get_active_ret = FAKE_CRAS_APM_PTR;
#endif  // HAVE_WEBRTC_APM

  input_data_get_for_stream(data, &stream, offsets, &area, &offset);

#ifdef HAVE_WEBRTC_APM
  // Assert APM process uses correct stream offset not the clipped one
  // used for audio area.
  EXPECT_EQ(1, cras_apm_list_process_called);
  EXPECT_EQ(2048, cras_apm_list_process_offset_val);
  EXPECT_EQ(0, offset);
#else
  // Without the APM, the offset shouldn't be changed.
  EXPECT_EQ(600, offset);
#endif  // HAVE_WEBRTC_APM

  input_data_destroy(&data);
  buffer_share_destroy(offsets);
}

TEST(InputData, GetSWCaptureGain) {
  void* dev_ptr = reinterpret_cast<void*>(0x123);
  struct input_data* data = NULL;
  struct cras_rstream stream;
  float gain;

  cras_rstream_get_volume_scaler_val = 0.8f;
  stream.stream_id = 123;

#ifdef HAVE_WEBRTC_APM
  data = input_data_create(dev_ptr);

  cras_apm_list_get_active_ret = FAKE_CRAS_APM_PTR;
  cras_apm_list_get_use_tuned_settings_val = 1;
  gain = input_data_get_software_gain_scaler(data, 0.7f, &stream);
  EXPECT_FLOAT_EQ(1.0f, gain);

  cras_apm_list_get_active_ret = NULL;
  gain = input_data_get_software_gain_scaler(data, 0.7f, &stream);
  EXPECT_FLOAT_EQ(0.56f, gain);

  cras_apm_list_get_active_ret = FAKE_CRAS_APM_PTR;
  cras_apm_list_get_use_tuned_settings_val = 0;
  gain = input_data_get_software_gain_scaler(data, 0.6f, &stream);
  EXPECT_FLOAT_EQ(0.48f, gain);
  input_data_destroy(&data);
#endif  // HAVE_WEBRTC_APM

  data = input_data_create(dev_ptr);
  gain = input_data_get_software_gain_scaler(data, 0.6f, &stream);
  EXPECT_FLOAT_EQ(0.48f, gain);
  input_data_destroy(&data);
}

extern "C" {
#ifdef HAVE_WEBRTC_APM
struct cras_apm* cras_apm_list_get_active_apm(void* stream_ptr, void* dev_ptr) {
  return cras_apm_list_get_active_ret;
}
int cras_apm_list_process(struct cras_apm* apm,
                          struct float_buffer* input,
                          unsigned int offset) {
  cras_apm_list_process_called++;
  cras_apm_list_process_offset_val = offset;
  return 0;
}

struct cras_audio_area* cras_apm_list_get_processed(struct cras_apm* apm) {
  return &apm_area;
}
void cras_apm_list_remove_apm(struct cras_apm_list* list, void* dev_ptr) {}
void cras_apm_list_put_processed(struct cras_apm* apm, unsigned int frames) {}
bool cras_apm_list_get_use_tuned_settings(struct cras_apm* apm) {
  return cras_apm_list_get_use_tuned_settings_val;
}
#endif  // HAVE_WEBRTC_APM

float cras_rstream_get_volume_scaler(struct cras_rstream* rstream) {
  return cras_rstream_get_volume_scaler_val;
}
}  // extern "C"
}  // namespace

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