1 /*
2 * Copyright (c) 2011 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 // Commandline tool to unpack audioproc debug files.
12 //
13 // The debug files are dumped as protobuf blobs. For analysis, it's necessary
14 // to unpack the file into its component parts: audio and other data.
15
16 #include <inttypes.h>
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20
21 #include <memory>
22 #include <string>
23 #include <vector>
24
25 #include "absl/flags/flag.h"
26 #include "absl/flags/parse.h"
27 #include "api/function_view.h"
28 #include "common_audio/wav_file.h"
29 #include "modules/audio_processing/test/protobuf_utils.h"
30 #include "modules/audio_processing/test/test_utils.h"
31 #include "rtc_base/format_macros.h"
32 #include "rtc_base/ignore_wundef.h"
33 #include "rtc_base/strings/string_builder.h"
34
35 RTC_PUSH_IGNORING_WUNDEF()
36 #include "modules/audio_processing/debug.pb.h"
37 RTC_POP_IGNORING_WUNDEF()
38
39 ABSL_FLAG(std::string,
40 input_file,
41 "input",
42 "The name of the input stream file.");
43 ABSL_FLAG(std::string,
44 output_file,
45 "ref_out",
46 "The name of the reference output stream file.");
47 ABSL_FLAG(std::string,
48 reverse_file,
49 "reverse",
50 "The name of the reverse input stream file.");
51 ABSL_FLAG(std::string,
52 delay_file,
53 "delay.int32",
54 "The name of the delay file.");
55 ABSL_FLAG(std::string,
56 drift_file,
57 "drift.int32",
58 "The name of the drift file.");
59 ABSL_FLAG(std::string,
60 level_file,
61 "level.int32",
62 "The name of the level file.");
63 ABSL_FLAG(std::string,
64 keypress_file,
65 "keypress.bool",
66 "The name of the keypress file.");
67 ABSL_FLAG(std::string,
68 callorder_file,
69 "callorder",
70 "The name of the render/capture call order file.");
71 ABSL_FLAG(std::string,
72 settings_file,
73 "settings.txt",
74 "The name of the settings file.");
75 ABSL_FLAG(bool,
76 full,
77 false,
78 "Unpack the full set of files (normally not needed).");
79 ABSL_FLAG(bool, raw, false, "Write raw data instead of a WAV file.");
80 ABSL_FLAG(bool,
81 text,
82 false,
83 "Write non-audio files as text files instead of binary files.");
84
85 #define PRINT_CONFIG(field_name) \
86 if (msg.has_##field_name()) { \
87 fprintf(settings_file, " " #field_name ": %d\n", msg.field_name()); \
88 }
89
90 #define PRINT_CONFIG_FLOAT(field_name) \
91 if (msg.has_##field_name()) { \
92 fprintf(settings_file, " " #field_name ": %f\n", msg.field_name()); \
93 }
94
95 namespace webrtc {
96
97 using audioproc::Event;
98 using audioproc::Init;
99 using audioproc::ReverseStream;
100 using audioproc::Stream;
101
102 namespace {
103
WriteData(const void * data,size_t size,FILE * file,const std::string & filename)104 void WriteData(const void* data,
105 size_t size,
106 FILE* file,
107 const std::string& filename) {
108 if (fwrite(data, size, 1, file) != 1) {
109 printf("Error when writing to %s\n", filename.c_str());
110 exit(1);
111 }
112 }
113
WriteCallOrderData(const bool render_call,FILE * file,const std::string & filename)114 void WriteCallOrderData(const bool render_call,
115 FILE* file,
116 const std::string& filename) {
117 const char call_type = render_call ? 'r' : 'c';
118 WriteData(&call_type, sizeof(call_type), file, filename.c_str());
119 }
120
WritingCallOrderFile()121 bool WritingCallOrderFile() {
122 return absl::GetFlag(FLAGS_full);
123 }
124
WritingRuntimeSettingFiles()125 bool WritingRuntimeSettingFiles() {
126 return absl::GetFlag(FLAGS_full);
127 }
128
129 // Exports RuntimeSetting AEC dump events to Audacity-readable files.
130 // This class is not RAII compliant.
131 class RuntimeSettingWriter {
132 public:
RuntimeSettingWriter(std::string name,rtc::FunctionView<bool (const Event)> is_exporter_for,rtc::FunctionView<std::string (const Event)> get_timeline_label)133 RuntimeSettingWriter(
134 std::string name,
135 rtc::FunctionView<bool(const Event)> is_exporter_for,
136 rtc::FunctionView<std::string(const Event)> get_timeline_label)
137 : setting_name_(std::move(name)),
138 is_exporter_for_(is_exporter_for),
139 get_timeline_label_(get_timeline_label) {}
~RuntimeSettingWriter()140 ~RuntimeSettingWriter() { Flush(); }
141
IsExporterFor(const Event & event) const142 bool IsExporterFor(const Event& event) const {
143 return is_exporter_for_(event);
144 }
145
146 // Writes to file the payload of |event| using |frame_count| to calculate
147 // timestamp.
WriteEvent(const Event & event,int frame_count)148 void WriteEvent(const Event& event, int frame_count) {
149 RTC_DCHECK(is_exporter_for_(event));
150 if (file_ == nullptr) {
151 rtc::StringBuilder file_name;
152 file_name << setting_name_ << frame_offset_ << ".txt";
153 file_ = OpenFile(file_name.str(), "wb");
154 }
155
156 // Time in the current WAV file, in seconds.
157 double time = (frame_count - frame_offset_) / 100.0;
158 std::string label = get_timeline_label_(event);
159 // In Audacity, all annotations are encoded as intervals.
160 fprintf(file_, "%.6f\t%.6f\t%s \n", time, time, label.c_str());
161 }
162
163 // Handles an AEC dump initialization event, occurring at frame
164 // |frame_offset|.
HandleInitEvent(int frame_offset)165 void HandleInitEvent(int frame_offset) {
166 Flush();
167 frame_offset_ = frame_offset;
168 }
169
170 private:
Flush()171 void Flush() {
172 if (file_ != nullptr) {
173 fclose(file_);
174 file_ = nullptr;
175 }
176 }
177
178 FILE* file_ = nullptr;
179 int frame_offset_ = 0;
180 const std::string setting_name_;
181 const rtc::FunctionView<bool(Event)> is_exporter_for_;
182 const rtc::FunctionView<std::string(Event)> get_timeline_label_;
183 };
184
185 // Returns RuntimeSetting exporters for runtime setting types defined in
186 // debug.proto.
RuntimeSettingWriters()187 std::vector<RuntimeSettingWriter> RuntimeSettingWriters() {
188 return {
189 RuntimeSettingWriter(
190 "CapturePreGain",
191 [](const Event& event) -> bool {
192 return event.runtime_setting().has_capture_pre_gain();
193 },
194 [](const Event& event) -> std::string {
195 return std::to_string(event.runtime_setting().capture_pre_gain());
196 }),
197 RuntimeSettingWriter(
198 "CustomRenderProcessingRuntimeSetting",
199 [](const Event& event) -> bool {
200 return event.runtime_setting()
201 .has_custom_render_processing_setting();
202 },
203 [](const Event& event) -> std::string {
204 return std::to_string(
205 event.runtime_setting().custom_render_processing_setting());
206 }),
207 RuntimeSettingWriter(
208 "CaptureFixedPostGain",
209 [](const Event& event) -> bool {
210 return event.runtime_setting().has_capture_fixed_post_gain();
211 },
212 [](const Event& event) -> std::string {
213 return std::to_string(
214 event.runtime_setting().capture_fixed_post_gain());
215 }),
216 RuntimeSettingWriter(
217 "PlayoutVolumeChange",
218 [](const Event& event) -> bool {
219 return event.runtime_setting().has_playout_volume_change();
220 },
221 [](const Event& event) -> std::string {
222 return std::to_string(
223 event.runtime_setting().playout_volume_change());
224 })};
225 }
226
227 } // namespace
228
do_main(int argc,char * argv[])229 int do_main(int argc, char* argv[]) {
230 std::vector<char*> args = absl::ParseCommandLine(argc, argv);
231 std::string program_name = args[0];
232 std::string usage =
233 "Commandline tool to unpack audioproc debug files.\n"
234 "Example usage:\n" +
235 program_name + " debug_dump.pb\n";
236
237 if (args.size() < 2) {
238 printf("%s", usage.c_str());
239 return 1;
240 }
241
242 FILE* debug_file = OpenFile(args[1], "rb");
243
244 Event event_msg;
245 int frame_count = 0;
246 size_t reverse_samples_per_channel = 0;
247 size_t input_samples_per_channel = 0;
248 size_t output_samples_per_channel = 0;
249 size_t num_reverse_channels = 0;
250 size_t num_input_channels = 0;
251 size_t num_output_channels = 0;
252 std::unique_ptr<WavWriter> reverse_wav_file;
253 std::unique_ptr<WavWriter> input_wav_file;
254 std::unique_ptr<WavWriter> output_wav_file;
255 std::unique_ptr<RawFile> reverse_raw_file;
256 std::unique_ptr<RawFile> input_raw_file;
257 std::unique_ptr<RawFile> output_raw_file;
258
259 rtc::StringBuilder callorder_raw_name;
260 callorder_raw_name << absl::GetFlag(FLAGS_callorder_file) << ".char";
261 FILE* callorder_char_file = WritingCallOrderFile()
262 ? OpenFile(callorder_raw_name.str(), "wb")
263 : nullptr;
264 FILE* settings_file = OpenFile(absl::GetFlag(FLAGS_settings_file), "wb");
265
266 std::vector<RuntimeSettingWriter> runtime_setting_writers =
267 RuntimeSettingWriters();
268
269 while (ReadMessageFromFile(debug_file, &event_msg)) {
270 if (event_msg.type() == Event::REVERSE_STREAM) {
271 if (!event_msg.has_reverse_stream()) {
272 printf("Corrupt input file: ReverseStream missing.\n");
273 return 1;
274 }
275
276 const ReverseStream msg = event_msg.reverse_stream();
277 if (msg.has_data()) {
278 if (absl::GetFlag(FLAGS_raw) && !reverse_raw_file) {
279 reverse_raw_file.reset(
280 new RawFile(absl::GetFlag(FLAGS_reverse_file) + ".pcm"));
281 }
282 // TODO(aluebs): Replace "num_reverse_channels *
283 // reverse_samples_per_channel" with "msg.data().size() /
284 // sizeof(int16_t)" and so on when this fix in audio_processing has made
285 // it into stable: https://webrtc-codereview.appspot.com/15299004/
286 WriteIntData(reinterpret_cast<const int16_t*>(msg.data().data()),
287 num_reverse_channels * reverse_samples_per_channel,
288 reverse_wav_file.get(), reverse_raw_file.get());
289 } else if (msg.channel_size() > 0) {
290 if (absl::GetFlag(FLAGS_raw) && !reverse_raw_file) {
291 reverse_raw_file.reset(
292 new RawFile(absl::GetFlag(FLAGS_reverse_file) + ".float"));
293 }
294 std::unique_ptr<const float*[]> data(
295 new const float*[num_reverse_channels]);
296 for (size_t i = 0; i < num_reverse_channels; ++i) {
297 data[i] = reinterpret_cast<const float*>(msg.channel(i).data());
298 }
299 WriteFloatData(data.get(), reverse_samples_per_channel,
300 num_reverse_channels, reverse_wav_file.get(),
301 reverse_raw_file.get());
302 }
303 if (absl::GetFlag(FLAGS_full)) {
304 if (WritingCallOrderFile()) {
305 WriteCallOrderData(true /* render_call */, callorder_char_file,
306 absl::GetFlag(FLAGS_callorder_file));
307 }
308 }
309 } else if (event_msg.type() == Event::STREAM) {
310 frame_count++;
311 if (!event_msg.has_stream()) {
312 printf("Corrupt input file: Stream missing.\n");
313 return 1;
314 }
315
316 const Stream msg = event_msg.stream();
317 if (msg.has_input_data()) {
318 if (absl::GetFlag(FLAGS_raw) && !input_raw_file) {
319 input_raw_file.reset(
320 new RawFile(absl::GetFlag(FLAGS_input_file) + ".pcm"));
321 }
322 WriteIntData(reinterpret_cast<const int16_t*>(msg.input_data().data()),
323 num_input_channels * input_samples_per_channel,
324 input_wav_file.get(), input_raw_file.get());
325 } else if (msg.input_channel_size() > 0) {
326 if (absl::GetFlag(FLAGS_raw) && !input_raw_file) {
327 input_raw_file.reset(
328 new RawFile(absl::GetFlag(FLAGS_input_file) + ".float"));
329 }
330 std::unique_ptr<const float*[]> data(
331 new const float*[num_input_channels]);
332 for (size_t i = 0; i < num_input_channels; ++i) {
333 data[i] = reinterpret_cast<const float*>(msg.input_channel(i).data());
334 }
335 WriteFloatData(data.get(), input_samples_per_channel,
336 num_input_channels, input_wav_file.get(),
337 input_raw_file.get());
338 }
339
340 if (msg.has_output_data()) {
341 if (absl::GetFlag(FLAGS_raw) && !output_raw_file) {
342 output_raw_file.reset(
343 new RawFile(absl::GetFlag(FLAGS_output_file) + ".pcm"));
344 }
345 WriteIntData(reinterpret_cast<const int16_t*>(msg.output_data().data()),
346 num_output_channels * output_samples_per_channel,
347 output_wav_file.get(), output_raw_file.get());
348 } else if (msg.output_channel_size() > 0) {
349 if (absl::GetFlag(FLAGS_raw) && !output_raw_file) {
350 output_raw_file.reset(
351 new RawFile(absl::GetFlag(FLAGS_output_file) + ".float"));
352 }
353 std::unique_ptr<const float*[]> data(
354 new const float*[num_output_channels]);
355 for (size_t i = 0; i < num_output_channels; ++i) {
356 data[i] =
357 reinterpret_cast<const float*>(msg.output_channel(i).data());
358 }
359 WriteFloatData(data.get(), output_samples_per_channel,
360 num_output_channels, output_wav_file.get(),
361 output_raw_file.get());
362 }
363
364 if (absl::GetFlag(FLAGS_full)) {
365 if (WritingCallOrderFile()) {
366 WriteCallOrderData(false /* render_call */, callorder_char_file,
367 absl::GetFlag(FLAGS_callorder_file));
368 }
369 if (msg.has_delay()) {
370 static FILE* delay_file =
371 OpenFile(absl::GetFlag(FLAGS_delay_file), "wb");
372 int32_t delay = msg.delay();
373 if (absl::GetFlag(FLAGS_text)) {
374 fprintf(delay_file, "%d\n", delay);
375 } else {
376 WriteData(&delay, sizeof(delay), delay_file,
377 absl::GetFlag(FLAGS_delay_file));
378 }
379 }
380
381 if (msg.has_drift()) {
382 static FILE* drift_file =
383 OpenFile(absl::GetFlag(FLAGS_drift_file), "wb");
384 int32_t drift = msg.drift();
385 if (absl::GetFlag(FLAGS_text)) {
386 fprintf(drift_file, "%d\n", drift);
387 } else {
388 WriteData(&drift, sizeof(drift), drift_file,
389 absl::GetFlag(FLAGS_drift_file));
390 }
391 }
392
393 if (msg.has_level()) {
394 static FILE* level_file =
395 OpenFile(absl::GetFlag(FLAGS_level_file), "wb");
396 int32_t level = msg.level();
397 if (absl::GetFlag(FLAGS_text)) {
398 fprintf(level_file, "%d\n", level);
399 } else {
400 WriteData(&level, sizeof(level), level_file,
401 absl::GetFlag(FLAGS_level_file));
402 }
403 }
404
405 if (msg.has_keypress()) {
406 static FILE* keypress_file =
407 OpenFile(absl::GetFlag(FLAGS_keypress_file), "wb");
408 bool keypress = msg.keypress();
409 if (absl::GetFlag(FLAGS_text)) {
410 fprintf(keypress_file, "%d\n", keypress);
411 } else {
412 WriteData(&keypress, sizeof(keypress), keypress_file,
413 absl::GetFlag(FLAGS_keypress_file));
414 }
415 }
416 }
417 } else if (event_msg.type() == Event::CONFIG) {
418 if (!event_msg.has_config()) {
419 printf("Corrupt input file: Config missing.\n");
420 return 1;
421 }
422 const audioproc::Config msg = event_msg.config();
423
424 fprintf(settings_file, "APM re-config at frame: %d\n", frame_count);
425
426 PRINT_CONFIG(aec_enabled);
427 PRINT_CONFIG(aec_delay_agnostic_enabled);
428 PRINT_CONFIG(aec_drift_compensation_enabled);
429 PRINT_CONFIG(aec_extended_filter_enabled);
430 PRINT_CONFIG(aec_suppression_level);
431 PRINT_CONFIG(aecm_enabled);
432 PRINT_CONFIG(aecm_comfort_noise_enabled);
433 PRINT_CONFIG(aecm_routing_mode);
434 PRINT_CONFIG(agc_enabled);
435 PRINT_CONFIG(agc_mode);
436 PRINT_CONFIG(agc_limiter_enabled);
437 PRINT_CONFIG(noise_robust_agc_enabled);
438 PRINT_CONFIG(hpf_enabled);
439 PRINT_CONFIG(ns_enabled);
440 PRINT_CONFIG(ns_level);
441 PRINT_CONFIG(transient_suppression_enabled);
442 PRINT_CONFIG(pre_amplifier_enabled);
443 PRINT_CONFIG_FLOAT(pre_amplifier_fixed_gain_factor);
444
445 if (msg.has_experiments_description()) {
446 fprintf(settings_file, " experiments_description: %s\n",
447 msg.experiments_description().c_str());
448 }
449 } else if (event_msg.type() == Event::INIT) {
450 if (!event_msg.has_init()) {
451 printf("Corrupt input file: Init missing.\n");
452 return 1;
453 }
454
455 const Init msg = event_msg.init();
456 // These should print out zeros if they're missing.
457 fprintf(settings_file, "Init at frame: %d\n", frame_count);
458 int input_sample_rate = msg.sample_rate();
459 fprintf(settings_file, " Input sample rate: %d\n", input_sample_rate);
460 int output_sample_rate = msg.output_sample_rate();
461 fprintf(settings_file, " Output sample rate: %d\n", output_sample_rate);
462 int reverse_sample_rate = msg.reverse_sample_rate();
463 fprintf(settings_file, " Reverse sample rate: %d\n",
464 reverse_sample_rate);
465 num_input_channels = msg.num_input_channels();
466 fprintf(settings_file, " Input channels: %" RTC_PRIuS "\n",
467 num_input_channels);
468 num_output_channels = msg.num_output_channels();
469 fprintf(settings_file, " Output channels: %" RTC_PRIuS "\n",
470 num_output_channels);
471 num_reverse_channels = msg.num_reverse_channels();
472 fprintf(settings_file, " Reverse channels: %" RTC_PRIuS "\n",
473 num_reverse_channels);
474 if (msg.has_timestamp_ms()) {
475 const int64_t timestamp = msg.timestamp_ms();
476 fprintf(settings_file, " Timestamp in millisecond: %" PRId64 "\n",
477 timestamp);
478 }
479
480 fprintf(settings_file, "\n");
481
482 if (reverse_sample_rate == 0) {
483 reverse_sample_rate = input_sample_rate;
484 }
485 if (output_sample_rate == 0) {
486 output_sample_rate = input_sample_rate;
487 }
488
489 reverse_samples_per_channel =
490 static_cast<size_t>(reverse_sample_rate / 100);
491 input_samples_per_channel = static_cast<size_t>(input_sample_rate / 100);
492 output_samples_per_channel =
493 static_cast<size_t>(output_sample_rate / 100);
494
495 if (!absl::GetFlag(FLAGS_raw)) {
496 // The WAV files need to be reset every time, because they cant change
497 // their sample rate or number of channels.
498 rtc::StringBuilder reverse_name;
499 reverse_name << absl::GetFlag(FLAGS_reverse_file) << frame_count
500 << ".wav";
501 reverse_wav_file.reset(new WavWriter(
502 reverse_name.str(), reverse_sample_rate, num_reverse_channels));
503 rtc::StringBuilder input_name;
504 input_name << absl::GetFlag(FLAGS_input_file) << frame_count << ".wav";
505 input_wav_file.reset(new WavWriter(input_name.str(), input_sample_rate,
506 num_input_channels));
507 rtc::StringBuilder output_name;
508 output_name << absl::GetFlag(FLAGS_output_file) << frame_count
509 << ".wav";
510 output_wav_file.reset(new WavWriter(
511 output_name.str(), output_sample_rate, num_output_channels));
512
513 if (WritingCallOrderFile()) {
514 rtc::StringBuilder callorder_name;
515 callorder_name << absl::GetFlag(FLAGS_callorder_file) << frame_count
516 << ".char";
517 callorder_char_file = OpenFile(callorder_name.str(), "wb");
518 }
519
520 if (WritingRuntimeSettingFiles()) {
521 for (RuntimeSettingWriter& writer : runtime_setting_writers) {
522 writer.HandleInitEvent(frame_count);
523 }
524 }
525 }
526 } else if (event_msg.type() == Event::RUNTIME_SETTING) {
527 if (WritingRuntimeSettingFiles()) {
528 for (RuntimeSettingWriter& writer : runtime_setting_writers) {
529 if (writer.IsExporterFor(event_msg)) {
530 writer.WriteEvent(event_msg, frame_count);
531 }
532 }
533 }
534 }
535 }
536
537 return 0;
538 }
539
540 } // namespace webrtc
541
main(int argc,char * argv[])542 int main(int argc, char* argv[]) {
543 return webrtc::do_main(argc, argv);
544 }
545