blob: 445248b0bfb770feaae09fdc6e47a70829e4b306 [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
Per Åhgrenf0c449e2018-10-23 20:17:18 +020013#include "rtc_base/strings/string_builder.h"
14
peahb46083e2016-05-03 07:01:18 -070015// Check to verify that the define is properly set.
peahf28a3892016-09-01 08:58:21 -070016#if !defined(WEBRTC_APM_DEBUG_DUMP) || \
17 (WEBRTC_APM_DEBUG_DUMP != 0 && WEBRTC_APM_DEBUG_DUMP != 1)
18#error "Set WEBRTC_APM_DEBUG_DUMP to either 0 or 1"
peahb46083e2016-05-03 07:01:18 -070019#endif
20
21namespace webrtc {
peahb46083e2016-05-03 07:01:18 -070022namespace {
23
peahf28a3892016-09-01 08:58:21 -070024#if WEBRTC_APM_DEBUG_DUMP == 1
Alessio Bazzica4bc60452018-11-20 12:44:15 +010025
26#if defined(WEBRTC_WIN)
27constexpr char kPathDelimiter = '\\';
28#else
29constexpr char kPathDelimiter = '/';
30#endif
31
32std::string FormFileName(const char* output_dir,
33 const char* name,
peahb46083e2016-05-03 07:01:18 -070034 int instance_index,
35 int reinit_index,
36 const std::string& suffix) {
Jonas Olsson18f151a2018-04-05 11:34:56 +020037 char buf[1024];
38 rtc::SimpleStringBuilder ss(buf);
Alessio Bazzica4bc60452018-11-20 12:44:15 +010039 const size_t output_dir_size = strlen(output_dir);
40 if (output_dir_size > 0) {
41 ss << output_dir;
42 if (output_dir[output_dir_size - 1] != kPathDelimiter) {
43 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
peahb46083e2016-05-03 07:01:18 -070067FILE* ApmDataDumper::GetRawFile(const char* 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
78WavWriter* ApmDataDumper::GetWavFile(const char* name,
79 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