1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "modules/audio_processing/test/wav_based_simulator.h"
12
13 #include <stdio.h>
14
15 #include <iostream>
16
17 #include "modules/audio_processing/test/test_utils.h"
18 #include "rtc_base/checks.h"
19 #include "rtc_base/system/file_wrapper.h"
20
21 namespace webrtc {
22 namespace test {
23
24 std::vector<WavBasedSimulator::SimulationEventType>
GetCustomEventChain(const std::string & filename)25 WavBasedSimulator::GetCustomEventChain(const std::string& filename) {
26 std::vector<WavBasedSimulator::SimulationEventType> call_chain;
27 FileWrapper file_wrapper = FileWrapper::OpenReadOnly(filename.c_str());
28
29 RTC_CHECK(file_wrapper.is_open())
30 << "Could not open the custom call order file, reverting "
31 "to using the default call order";
32
33 char c;
34 size_t num_read = file_wrapper.Read(&c, sizeof(char));
35 while (num_read > 0) {
36 switch (c) {
37 case 'r':
38 call_chain.push_back(SimulationEventType::kProcessReverseStream);
39 break;
40 case 'c':
41 call_chain.push_back(SimulationEventType::kProcessStream);
42 break;
43 case '\n':
44 break;
45 default:
46 FATAL() << "Incorrect custom call order file, reverting to using the "
47 "default call order";
48 return WavBasedSimulator::GetDefaultEventChain();
49 }
50
51 num_read = file_wrapper.Read(&c, sizeof(char));
52 }
53
54 return call_chain;
55 }
56
WavBasedSimulator(const SimulationSettings & settings,rtc::scoped_refptr<AudioProcessing> audio_processing,std::unique_ptr<AudioProcessingBuilder> ap_builder)57 WavBasedSimulator::WavBasedSimulator(
58 const SimulationSettings& settings,
59 rtc::scoped_refptr<AudioProcessing> audio_processing,
60 std::unique_ptr<AudioProcessingBuilder> ap_builder)
61 : AudioProcessingSimulator(settings,
62 std::move(audio_processing),
63 std::move(ap_builder)) {
64 if (settings_.call_order_input_filename) {
65 call_chain_ = WavBasedSimulator::GetCustomEventChain(
66 *settings_.call_order_input_filename);
67 } else {
68 call_chain_ = WavBasedSimulator::GetDefaultEventChain();
69 }
70 }
71
72 WavBasedSimulator::~WavBasedSimulator() = default;
73
74 std::vector<WavBasedSimulator::SimulationEventType>
GetDefaultEventChain()75 WavBasedSimulator::GetDefaultEventChain() {
76 std::vector<WavBasedSimulator::SimulationEventType> call_chain(2);
77 call_chain[0] = SimulationEventType::kProcessStream;
78 call_chain[1] = SimulationEventType::kProcessReverseStream;
79 return call_chain;
80 }
81
PrepareProcessStreamCall()82 void WavBasedSimulator::PrepareProcessStreamCall() {
83 if (settings_.fixed_interface) {
84 fwd_frame_.CopyFrom(*in_buf_);
85 }
86 ap_->set_stream_key_pressed(settings_.use_ts && (*settings_.use_ts));
87
88 if (!settings_.use_stream_delay || *settings_.use_stream_delay) {
89 RTC_CHECK_EQ(AudioProcessing::kNoError,
90 ap_->set_stream_delay_ms(
91 settings_.stream_delay ? *settings_.stream_delay : 0));
92 }
93 }
94
PrepareReverseProcessStreamCall()95 void WavBasedSimulator::PrepareReverseProcessStreamCall() {
96 if (settings_.fixed_interface) {
97 rev_frame_.CopyFrom(*reverse_in_buf_);
98 }
99 }
100
Process()101 void WavBasedSimulator::Process() {
102 ConfigureAudioProcessor();
103
104 Initialize();
105
106 bool samples_left_to_process = true;
107 int call_chain_index = 0;
108 int num_forward_chunks_processed = 0;
109 while (samples_left_to_process) {
110 switch (call_chain_[call_chain_index]) {
111 case SimulationEventType::kProcessStream:
112 samples_left_to_process = HandleProcessStreamCall();
113 ++num_forward_chunks_processed;
114 break;
115 case SimulationEventType::kProcessReverseStream:
116 if (settings_.reverse_input_filename) {
117 samples_left_to_process = HandleProcessReverseStreamCall();
118 }
119 break;
120 default:
121 RTC_CHECK(false);
122 }
123
124 call_chain_index = (call_chain_index + 1) % call_chain_.size();
125 }
126
127 DetachAecDump();
128 }
129
HandleProcessStreamCall()130 bool WavBasedSimulator::HandleProcessStreamCall() {
131 bool samples_left_to_process = buffer_reader_->Read(in_buf_.get());
132 if (samples_left_to_process) {
133 PrepareProcessStreamCall();
134 ProcessStream(settings_.fixed_interface);
135 }
136 return samples_left_to_process;
137 }
138
HandleProcessReverseStreamCall()139 bool WavBasedSimulator::HandleProcessReverseStreamCall() {
140 bool samples_left_to_process =
141 reverse_buffer_reader_->Read(reverse_in_buf_.get());
142 if (samples_left_to_process) {
143 PrepareReverseProcessStreamCall();
144 ProcessReverseStream(settings_.fixed_interface);
145 }
146 return samples_left_to_process;
147 }
148
Initialize()149 void WavBasedSimulator::Initialize() {
150 std::unique_ptr<WavReader> in_file(
151 new WavReader(settings_.input_filename->c_str()));
152 int input_sample_rate_hz = in_file->sample_rate();
153 int input_num_channels = in_file->num_channels();
154 buffer_reader_.reset(new ChannelBufferWavReader(std::move(in_file)));
155
156 int output_sample_rate_hz = settings_.output_sample_rate_hz
157 ? *settings_.output_sample_rate_hz
158 : input_sample_rate_hz;
159 int output_num_channels = settings_.output_num_channels
160 ? *settings_.output_num_channels
161 : input_num_channels;
162
163 int reverse_sample_rate_hz = 48000;
164 int reverse_num_channels = 1;
165 int reverse_output_sample_rate_hz = 48000;
166 int reverse_output_num_channels = 1;
167 if (settings_.reverse_input_filename) {
168 std::unique_ptr<WavReader> reverse_in_file(
169 new WavReader(settings_.reverse_input_filename->c_str()));
170 reverse_sample_rate_hz = reverse_in_file->sample_rate();
171 reverse_num_channels = reverse_in_file->num_channels();
172 reverse_buffer_reader_.reset(
173 new ChannelBufferWavReader(std::move(reverse_in_file)));
174
175 reverse_output_sample_rate_hz =
176 settings_.reverse_output_sample_rate_hz
177 ? *settings_.reverse_output_sample_rate_hz
178 : reverse_sample_rate_hz;
179 reverse_output_num_channels = settings_.reverse_output_num_channels
180 ? *settings_.reverse_output_num_channels
181 : reverse_num_channels;
182 }
183
184 SetupBuffersConfigsOutputs(
185 input_sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
186 reverse_output_sample_rate_hz, input_num_channels, output_num_channels,
187 reverse_num_channels, reverse_output_num_channels);
188 }
189
190 } // namespace test
191 } // namespace webrtc
192