1 // Copyright 2019 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 #include <stdio.h>
7
8 extern "C" {
9 #include "buffer_share.c"
10 #include "cras_audio_area.h"
11 #include "cras_rstream.h"
12 #include "input_data.h"
13 }
14
15 namespace {
16
17 #define FAKE_CRAS_APM_PTR reinterpret_cast<struct cras_apm*>(0x99)
18
19 #ifdef HAVE_WEBRTC_APM
20 static struct cras_audio_area apm_area;
21 static unsigned int cras_apm_list_process_offset_val;
22 static unsigned int cras_apm_list_process_called;
23 static struct cras_apm* cras_apm_list_get_active_ret = NULL;
24 static bool cras_apm_list_get_use_tuned_settings_val;
25 #endif // HAVE_WEBRTC_APM
26 static float cras_rstream_get_volume_scaler_val;
27
TEST(InputData,GetForInputStream)28 TEST(InputData, GetForInputStream) {
29 void* dev_ptr = reinterpret_cast<void*>(0x123);
30 struct input_data* data;
31 struct cras_rstream stream;
32 struct buffer_share* offsets;
33 struct cras_audio_area* area;
34 struct cras_audio_area dev_area;
35 unsigned int offset;
36
37 #ifdef HAVE_WEBRTC_APM
38 cras_apm_list_process_called = 0;
39 #endif // HAVE_WEBRTC_APM
40 stream.stream_id = 111;
41
42 data = input_data_create(dev_ptr);
43 data->ext.configure(&data->ext, 8192, 2, 48000);
44
45 // Prepare offsets data for 2 streams.
46 offsets = buffer_share_create(8192);
47 buffer_share_add_id(offsets, 111, NULL);
48 buffer_share_add_id(offsets, 222, NULL);
49 buffer_share_offset_update(offsets, 111, 2048);
50
51 dev_area.frames = 600;
52 data->area = &dev_area;
53
54 stream.apm_list = NULL;
55 input_data_get_for_stream(data, &stream, offsets, &area, &offset);
56
57 // Assert offset is clipped by area->frames
58 EXPECT_EQ(600, area->frames);
59 EXPECT_EQ(600, offset);
60 #ifdef HAVE_WEBRTC_APM
61 EXPECT_EQ(0, cras_apm_list_process_called);
62 cras_apm_list_get_active_ret = FAKE_CRAS_APM_PTR;
63 #endif // HAVE_WEBRTC_APM
64
65 input_data_get_for_stream(data, &stream, offsets, &area, &offset);
66
67 #ifdef HAVE_WEBRTC_APM
68 // Assert APM process uses correct stream offset not the clipped one
69 // used for audio area.
70 EXPECT_EQ(1, cras_apm_list_process_called);
71 EXPECT_EQ(2048, cras_apm_list_process_offset_val);
72 EXPECT_EQ(0, offset);
73 #else
74 // Without the APM, the offset shouldn't be changed.
75 EXPECT_EQ(600, offset);
76 #endif // HAVE_WEBRTC_APM
77
78 input_data_destroy(&data);
79 buffer_share_destroy(offsets);
80 }
81
TEST(InputData,GetSWCaptureGain)82 TEST(InputData, GetSWCaptureGain) {
83 void* dev_ptr = reinterpret_cast<void*>(0x123);
84 struct input_data* data = NULL;
85 struct cras_rstream stream;
86 float gain;
87
88 cras_rstream_get_volume_scaler_val = 0.8f;
89 stream.stream_id = 123;
90
91 #ifdef HAVE_WEBRTC_APM
92 data = input_data_create(dev_ptr);
93
94 cras_apm_list_get_active_ret = FAKE_CRAS_APM_PTR;
95 cras_apm_list_get_use_tuned_settings_val = 1;
96 gain = input_data_get_software_gain_scaler(data, 0.7f, &stream);
97 EXPECT_FLOAT_EQ(1.0f, gain);
98
99 cras_apm_list_get_active_ret = NULL;
100 gain = input_data_get_software_gain_scaler(data, 0.7f, &stream);
101 EXPECT_FLOAT_EQ(0.56f, gain);
102
103 cras_apm_list_get_active_ret = FAKE_CRAS_APM_PTR;
104 cras_apm_list_get_use_tuned_settings_val = 0;
105 gain = input_data_get_software_gain_scaler(data, 0.6f, &stream);
106 EXPECT_FLOAT_EQ(0.48f, gain);
107 input_data_destroy(&data);
108 #endif // HAVE_WEBRTC_APM
109
110 data = input_data_create(dev_ptr);
111 gain = input_data_get_software_gain_scaler(data, 0.6f, &stream);
112 EXPECT_FLOAT_EQ(0.48f, gain);
113 input_data_destroy(&data);
114 }
115
116 extern "C" {
117 #ifdef HAVE_WEBRTC_APM
cras_apm_list_get_active_apm(void * stream_ptr,void * dev_ptr)118 struct cras_apm* cras_apm_list_get_active_apm(void* stream_ptr, void* dev_ptr) {
119 return cras_apm_list_get_active_ret;
120 }
cras_apm_list_process(struct cras_apm * apm,struct float_buffer * input,unsigned int offset)121 int cras_apm_list_process(struct cras_apm* apm,
122 struct float_buffer* input,
123 unsigned int offset) {
124 cras_apm_list_process_called++;
125 cras_apm_list_process_offset_val = offset;
126 return 0;
127 }
128
cras_apm_list_get_processed(struct cras_apm * apm)129 struct cras_audio_area* cras_apm_list_get_processed(struct cras_apm* apm) {
130 return &apm_area;
131 }
cras_apm_list_remove_apm(struct cras_apm_list * list,void * dev_ptr)132 void cras_apm_list_remove_apm(struct cras_apm_list* list, void* dev_ptr) {}
cras_apm_list_put_processed(struct cras_apm * apm,unsigned int frames)133 void cras_apm_list_put_processed(struct cras_apm* apm, unsigned int frames) {}
cras_apm_list_get_use_tuned_settings(struct cras_apm * apm)134 bool cras_apm_list_get_use_tuned_settings(struct cras_apm* apm) {
135 return cras_apm_list_get_use_tuned_settings_val;
136 }
137 #endif // HAVE_WEBRTC_APM
138
cras_rstream_get_volume_scaler(struct cras_rstream * rstream)139 float cras_rstream_get_volume_scaler(struct cras_rstream* rstream) {
140 return cras_rstream_get_volume_scaler_val;
141 }
142 } // extern "C"
143 } // namespace
144
main(int argc,char ** argv)145 int main(int argc, char** argv) {
146 ::testing::InitGoogleTest(&argc, argv);
147 return RUN_ALL_TESTS();
148 }
149