blob: cc879c8b08fdc5d7d6cbff9949166e2ad6894125 [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
Alessio Bazzica4bc60452018-11-20 12:44:15 +010057ApmDataDumper::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;
Alessio Bazzica4bc60452018-11-20 12:44:15 +010064char ApmDataDumper::output_dir_[] = "";
65
peahb46083e2016-05-03 07:01:18 -070066FILE* ApmDataDumper::GetRawFile(const char* name) {
Alessio Bazzica4bc60452018-11-20 12:44:15 +010067 std::string filename = FormFileName(output_dir_, name, instance_index_,
68 recording_set_index_, ".dat");
peahb46083e2016-05-03 07:01:18 -070069 auto& f = raw_files_[filename];
70 if (!f) {
71 f.reset(fopen(filename.c_str(), "wb"));
Alessio Bazzica4bc60452018-11-20 12:44:15 +010072 RTC_CHECK(f.get()) << "Cannot write to " << filename << ".";
peahb46083e2016-05-03 07:01:18 -070073 }
74 return f.get();
75}
76
77WavWriter* ApmDataDumper::GetWavFile(const char* name,
78 int sample_rate_hz,
79 int num_channels) {
Alessio Bazzica4bc60452018-11-20 12:44:15 +010080 std::string filename = FormFileName(output_dir_, name, instance_index_,
81 recording_set_index_, ".wav");
peahb46083e2016-05-03 07:01:18 -070082 auto& f = wav_files_[filename];
83 if (!f) {
84 f.reset(new WavWriter(filename.c_str(), sample_rate_hz, num_channels));
85 }
86 return f.get();
87}
peahb46083e2016-05-03 07:01:18 -070088#endif
89
90} // namespace webrtc