Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(935)

Side by Side Diff: webrtc/modules/audio_processing/aec_dumper/aec_dumper_impl.cc

Issue 2747123007: Test submission of complete AEC-dump refactoring. (Closed)
Patch Set: Most of Karl's comments addressed. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2017 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 <string>
12 #include <utility>
13 #include <vector>
14
15 #include "webrtc/modules/audio_processing/aec_dumper/aec_dumper.h"
16
17 #include "webrtc/base/checks.h"
18 #include "webrtc/base/event.h"
19 #include "webrtc/base/ignore_wundef.h"
20 #include "webrtc/base/task_queue.h"
21 #include "webrtc/base/thread_checker.h"
22 #include "webrtc/modules/audio_processing/aec_dumper/capture_stream_info_impl.h"
23 #include "webrtc/modules/include/module_common_types.h"
24 #include "webrtc/system_wrappers/include/file_wrapper.h"
25
26 // Files generated at build-time by the protobuf compiler.
27 RTC_PUSH_IGNORING_WUNDEF()
28 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD
29 #include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
30 #else
31 #include "webrtc/modules/audio_processing/debug.pb.h"
32 #endif
33 RTC_POP_IGNORING_WUNDEF()
34
35 namespace webrtc {
36
37 namespace {
38
39 // Task-queue based implementation of AecDumper. It is thread safe by
40 // relying on locks in TaskQueue.
41 class AecDumperImpl : public AecDumper {
42 public:
43 AecDumperImpl(std::string file_name,
44 int64_t max_log_size_bytes,
45 rtc::TaskQueue* worker_queue);
46 AecDumperImpl(FILE* handle,
47 int64_t max_log_size_bytes,
48 rtc::TaskQueue* worker_queue);
49 ~AecDumperImpl() override;
50
51 std::unique_ptr<CaptureStreamInfo> GetCaptureStreamInfo() override;
52
53 void WriteInitMessage(const ProcessingConfig& api_format) override;
54 void WriteReverseStreamMessage(const AudioFrame& frame) override;
55 void WriteReverseStreamMessage(
56 std::vector<rtc::ArrayView<const float>> src) override;
57 void WriteCaptureStreamMessage(
58 std::unique_ptr<CaptureStreamInfo> capture_stream_info) override;
59 void WriteConfig(const InternalAPMConfig& config, bool forced) override;
60
61 private:
62 void PostTask(std::unique_ptr<audioproc::Event> event);
63
64 // Implementation detail of WriteConfig: If not |forced|, only
65 // writes the current config if it is different from the last saved
66 // one; if |forced|, writes the config regardless of the last saved.
67 std::string last_serialized_capture_config_ GUARDED_BY(config_string_lock_) =
68 "";
69 std::unique_ptr<FileWrapper> debug_file_;
70 int64_t num_bytes_left_for_log_ = 0;
71
72 rtc::TaskQueue* worker_queue_;
73 rtc::CriticalSection config_string_lock_;
74 };
75
76 class WriteToFileTask : public rtc::QueuedTask {
77 public:
78 WriteToFileTask(webrtc::FileWrapper* debug_file,
79 std::unique_ptr<audioproc::Event> event,
80 int64_t* num_bytes_left_for_log)
81 : debug_file_(debug_file),
82 event_(std::move(event)),
83 num_bytes_left_for_log_(num_bytes_left_for_log) {}
84
85 private:
86 bool IsRoomForNextEvent(size_t event_byte_size) const {
87 int64_t next_message_size = event_byte_size + sizeof(int32_t);
88 return (*num_bytes_left_for_log_ < 0) ||
89 (*num_bytes_left_for_log_ >= next_message_size);
90 }
91
92 void UpdateBytesLeft(size_t event_byte_size) {
93 RTC_DCHECK(IsRoomForNextEvent(event_byte_size));
94 if (*num_bytes_left_for_log_ >= 0) {
95 *num_bytes_left_for_log_ -= (sizeof(int32_t) + event_byte_size);
96 }
97 }
98
99 bool Run() override {
100 if (!debug_file_->is_open()) {
101 return true;
102 }
103
104 std::string event_string;
105 event_->SerializeToString(&event_string);
106
107 const size_t event_byte_size = event_->ByteSize();
108
109 if (!IsRoomForNextEvent(event_byte_size)) {
110 debug_file_->CloseFile();
111 return true;
112 }
113
114 UpdateBytesLeft(event_byte_size);
115
116 // Write message preceded by its size.
117 if (!debug_file_->Write(&event_byte_size, sizeof(int32_t))) {
118 RTC_NOTREACHED();
119 }
120 if (!debug_file_->Write(event_string.data(), event_string.length())) {
121 RTC_NOTREACHED();
122 }
123 return true; // Delete task from queue at once. TODO(aleloi):
124 // instead consider a 'mega-task' that returns
125 // 'false', checks if there is something in a
126 // swap-queue and reposts itself periodically.
127 }
128
129 webrtc::FileWrapper* debug_file_;
130 std::unique_ptr<audioproc::Event> event_;
131 int64_t* num_bytes_left_for_log_;
132 };
133
134 AecDumperImpl::AecDumperImpl(std::string file_name,
135 int64_t max_log_size_bytes,
136 rtc::TaskQueue* worker_queue)
137 : debug_file_(FileWrapper::Create()), worker_queue_(worker_queue) {
138 RTC_DCHECK(debug_file_);
139 worker_queue_->PostTask([this, file_name, max_log_size_bytes]() {
140 num_bytes_left_for_log_ = max_log_size_bytes;
141 debug_file_->OpenFile(file_name.c_str(), false);
142 });
143 }
144
145 AecDumperImpl::AecDumperImpl(FILE* handle,
146 int64_t max_log_size_bytes,
147 rtc::TaskQueue* worker_queue)
148 : debug_file_(FileWrapper::Create()), worker_queue_(worker_queue) {
149 RTC_DCHECK(debug_file_);
150 worker_queue_->PostTask([this, handle, max_log_size_bytes]() {
151 num_bytes_left_for_log_ = max_log_size_bytes;
152 debug_file_->OpenFromFileHandle(handle);
153 });
154 }
155
156 AecDumperImpl::~AecDumperImpl() {
157 // Block until all tasks have finished running.
158 rtc::Event thread_sync_event(false /* manual_reset */, false);
159 worker_queue_->PostTask([&thread_sync_event] { thread_sync_event.Set(); });
160 thread_sync_event.Wait(rtc::Event::kForever);
161 }
162
163 std::unique_ptr<AecDumper::CaptureStreamInfo>
164 AecDumperImpl::GetCaptureStreamInfo() {
165 return std::unique_ptr<CaptureStreamInfoImpl>(new CaptureStreamInfoImpl(
166 std::unique_ptr<audioproc::Event>(new audioproc::Event())));
167 }
168
169 void AecDumperImpl::WriteInitMessage(const ProcessingConfig& api_format) {
170 auto event = std::unique_ptr<audioproc::Event>(new audioproc::Event());
171 event->set_type(audioproc::Event::INIT);
172 audioproc::Init* msg = event->mutable_init();
173
174 msg->set_sample_rate(api_format.input_stream().sample_rate_hz());
175 msg->set_num_input_channels(static_cast<google::protobuf::int32>(
176 api_format.input_stream().num_channels()));
177 msg->set_num_output_channels(static_cast<google::protobuf::int32>(
178 api_format.output_stream().num_channels()));
179 msg->set_num_reverse_channels(static_cast<google::protobuf::int32>(
180 api_format.reverse_input_stream().num_channels()));
181 msg->set_reverse_sample_rate(
182 api_format.reverse_input_stream().sample_rate_hz());
183 msg->set_output_sample_rate(api_format.output_stream().sample_rate_hz());
184 msg->set_reverse_output_sample_rate(
185 api_format.reverse_output_stream().sample_rate_hz());
186 msg->set_num_reverse_output_channels(
187 api_format.reverse_output_stream().num_channels());
188
189 PostTask(std::move(event));
190 }
191
192 void AecDumperImpl::WriteReverseStreamMessage(const AudioFrame& frame) {
193 auto event = std::unique_ptr<audioproc::Event>(new audioproc::Event());
194
195 event->set_type(audioproc::Event::REVERSE_STREAM);
196 audioproc::ReverseStream* msg = event->mutable_reverse_stream();
197 const size_t data_size =
198 sizeof(int16_t) * frame.samples_per_channel_ * frame.num_channels_;
199 msg->set_data(frame.data_, data_size);
200
201 PostTask(std::move(event));
202 }
203
204 void AecDumperImpl::WriteReverseStreamMessage(
205 std::vector<rtc::ArrayView<const float>> src) {
206 auto event = std::unique_ptr<audioproc::Event>(new audioproc::Event());
207 event->set_type(audioproc::Event::REVERSE_STREAM);
208
209 audioproc::ReverseStream* msg = event->mutable_reverse_stream();
210
211 for (const auto& frame : src) {
212 msg->add_channel(frame.begin(), frame.size());
213 }
214
215 PostTask(std::move(event));
216 }
217
218 void AecDumperImpl::WriteCaptureStreamMessage(
219 std::unique_ptr<CaptureStreamInfo> capture_stream_info) {
220 // Really ugly, how is it done better?
221 auto event_ptr =
222 static_cast<CaptureStreamInfoImpl*>(capture_stream_info.get())
223 ->GetEventMsg();
224 if (event_ptr) {
225 PostTask(std::move(event_ptr));
226 }
227 }
228
229 void CopyFromConfigToEvent(const webrtc::InternalAPMConfig& config,
230 webrtc::audioproc::Config* pb_cfg) {
231 pb_cfg->set_aec_enabled(config.aec_enabled);
232 pb_cfg->set_aec_delay_agnostic_enabled(config.aec_delay_agnostic_enabled);
233 pb_cfg->set_aec_drift_compensation_enabled(
234 config.aec_drift_compensation_enabled);
235 pb_cfg->set_aec_extended_filter_enabled(config.aec_extended_filter_enabled);
236 pb_cfg->set_aec_suppression_level(config.aec_suppression_level);
237
238 pb_cfg->set_aecm_enabled(config.aecm_enabled);
239 pb_cfg->set_aecm_comfort_noise_enabled(config.aecm_comfort_noise_enabled);
240 pb_cfg->set_aecm_routing_mode(config.aecm_routing_mode);
241
242 pb_cfg->set_agc_enabled(config.agc_enabled);
243 pb_cfg->set_agc_mode(config.agc_mode);
244 pb_cfg->set_agc_limiter_enabled(config.agc_limiter_enabled);
245 pb_cfg->set_noise_robust_agc_enabled(config.noise_robust_agc_enabled);
246
247 pb_cfg->set_hpf_enabled(config.hpf_enabled);
248
249 pb_cfg->set_ns_enabled(config.ns_enabled);
250 pb_cfg->set_ns_level(config.ns_level);
251
252 pb_cfg->set_transient_suppression_enabled(
253 config.transient_suppression_enabled);
254 pb_cfg->set_intelligibility_enhancer_enabled(
255 config.intelligibility_enhancer_enabled);
256
257 pb_cfg->set_experiments_description(config.experiments_description);
258 }
259
260 void AecDumperImpl::WriteConfig(const InternalAPMConfig& config, bool forced) {
261 auto event = std::unique_ptr<audioproc::Event>(new audioproc::Event());
262 event->set_type(audioproc::Event::CONFIG);
263 CopyFromConfigToEvent(config, event->mutable_config());
264
265 std::string serialized_config = event->mutable_config()->SerializeAsString();
266 {
267 rtc::CritScope cs(&config_string_lock_);
268 if (!forced && serialized_config == last_serialized_capture_config_) {
269 return;
270 }
271 last_serialized_capture_config_ = serialized_config;
272 }
273
274 PostTask(std::move(event));
275 }
276
277 void AecDumperImpl::PostTask(std::unique_ptr<audioproc::Event> event) {
278 RTC_DCHECK(event);
279 worker_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(new WriteToFileTask(
280 debug_file_.get(), std::move(event), &num_bytes_left_for_log_)));
281 }
282 } // namespace
283
284 std::unique_ptr<AecDumper> AecDumper::Create(std::string file_name,
285 int64_t max_log_size_bytes,
286 rtc::TaskQueue* worker_queue) {
287 return std::unique_ptr<AecDumperImpl>(
288 new AecDumperImpl(file_name, max_log_size_bytes, worker_queue));
289 }
290
291 std::unique_ptr<AecDumper> AecDumper::Create(FILE* handle,
292 int64_t max_log_size_bytes,
293 rtc::TaskQueue* worker_queue) {
294 return std::unique_ptr<AecDumperImpl>(
295 new AecDumperImpl(handle, max_log_size_bytes, worker_queue));
296 }
297 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698