blob: 65d2167d375e9c9e6ec43d13a5488bfc2a3a34ff [file] [log] [blame]
peahb46083e2016-05-03 07:01:18 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/logging/apm_data_dumper.h"
peahb46083e2016-05-03 07:01:18 -070012
Ali Tofighf3592cb2022-08-16 14:44:38 +020013#include "absl/strings/string_view.h"
Per Åhgrenf0c449e2018-10-23 20:17:18 +020014#include "rtc_base/strings/string_builder.h"
15
peahb46083e2016-05-03 07:01:18 -070016// Check to verify that the define is properly set.
peahf28a3892016-09-01 08:58:21 -070017#if !defined(WEBRTC_APM_DEBUG_DUMP) || \
18 (WEBRTC_APM_DEBUG_DUMP != 0 && WEBRTC_APM_DEBUG_DUMP != 1)
19#error "Set WEBRTC_APM_DEBUG_DUMP to either 0 or 1"
peahb46083e2016-05-03 07:01:18 -070020#endif
21
22namespace webrtc {
peahb46083e2016-05-03 07:01:18 -070023namespace {
24
peahf28a3892016-09-01 08:58:21 -070025#if WEBRTC_APM_DEBUG_DUMP == 1
Alessio Bazzica4bc60452018-11-20 12:44:15 +010026
27#if defined(WEBRTC_WIN)
28constexpr char kPathDelimiter = '\\';
29#else
30constexpr char kPathDelimiter = '/';
31#endif
32
Ali Tofighf3592cb2022-08-16 14:44:38 +020033std::string FormFileName(absl::string_view output_dir,
34 absl::string_view name,
peahb46083e2016-05-03 07:01:18 -070035 int instance_index,
36 int reinit_index,
Ali Tofighf3592cb2022-08-16 14:44:38 +020037 absl::string_view suffix) {
Jonas Olsson18f151a2018-04-05 11:34:56 +020038 char buf[1024];
39 rtc::SimpleStringBuilder ss(buf);
Ali Tofighf3592cb2022-08-16 14:44:38 +020040 if (!output_dir.empty()) {
Alessio Bazzica4bc60452018-11-20 12:44:15 +010041 ss << output_dir;
Ali Tofighf3592cb2022-08-16 14:44:38 +020042 if (output_dir.back() != kPathDelimiter) {
Alessio Bazzica4bc60452018-11-20 12:44:15 +010043 ss << kPathDelimiter;
44 }
45 }
peahb46083e2016-05-03 07:01:18 -070046 ss << name << "_" << instance_index << "-" << reinit_index << suffix;
47 return ss.str();
48}
49#endif
50
51} // namespace
52
peahf28a3892016-09-01 08:58:21 -070053#if WEBRTC_APM_DEBUG_DUMP == 1
peahf8f754a2016-08-30 10:31:45 -070054ApmDataDumper::ApmDataDumper(int instance_index)
55 : instance_index_(instance_index) {}
56#else
Nico Weber22f99252019-02-20 10:13:16 -050057ApmDataDumper::ApmDataDumper(int instance_index) {}
peahf8f754a2016-08-30 10:31:45 -070058#endif
59
Alessio Bazzica4bc60452018-11-20 12:44:15 +010060ApmDataDumper::~ApmDataDumper() = default;
peahf8f754a2016-08-30 10:31:45 -070061
peahf28a3892016-09-01 08:58:21 -070062#if WEBRTC_APM_DEBUG_DUMP == 1
Per Åhgren7a95e0f2018-10-25 09:56:49 +020063bool ApmDataDumper::recording_activated_ = false;
Per Åhgrenc2ae4c82021-01-20 15:42:13 +010064absl::optional<int> ApmDataDumper::dump_set_to_use_;
Alessio Bazzica4bc60452018-11-20 12:44:15 +010065char ApmDataDumper::output_dir_[] = "";
66
Ali Tofighf3592cb2022-08-16 14:44:38 +020067FILE* ApmDataDumper::GetRawFile(absl::string_view name) {
Alessio Bazzica4bc60452018-11-20 12:44:15 +010068 std::string filename = FormFileName(output_dir_, name, instance_index_,
69 recording_set_index_, ".dat");
peahb46083e2016-05-03 07:01:18 -070070 auto& f = raw_files_[filename];
71 if (!f) {
72 f.reset(fopen(filename.c_str(), "wb"));
Alessio Bazzica4bc60452018-11-20 12:44:15 +010073 RTC_CHECK(f.get()) << "Cannot write to " << filename << ".";
peahb46083e2016-05-03 07:01:18 -070074 }
75 return f.get();
76}
77
Ali Tofighf3592cb2022-08-16 14:44:38 +020078WavWriter* ApmDataDumper::GetWavFile(absl::string_view name,
peahb46083e2016-05-03 07:01:18 -070079 int sample_rate_hz,
Per Åhgren5dca3f12020-01-28 09:08:11 +010080 int num_channels,
81 WavFile::SampleFormat format) {
Alessio Bazzica4bc60452018-11-20 12:44:15 +010082 std::string filename = FormFileName(output_dir_, name, instance_index_,
83 recording_set_index_, ".wav");
peahb46083e2016-05-03 07:01:18 -070084 auto& f = wav_files_[filename];
85 if (!f) {
Per Åhgren5dca3f12020-01-28 09:08:11 +010086 f.reset(
87 new WavWriter(filename.c_str(), sample_rate_hz, num_channels, format));
peahb46083e2016-05-03 07:01:18 -070088 }
89 return f.get();
90}
peahb46083e2016-05-03 07:01:18 -070091#endif
92
93} // namespace webrtc