xref: /aosp_15_r20/external/webrtc/modules/audio_processing/logging/apm_data_dumper.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 #include "modules/audio_processing/logging/apm_data_dumper.h"
12 
13 #include "absl/strings/string_view.h"
14 #include "rtc_base/strings/string_builder.h"
15 
16 // Check to verify that the define is properly set.
17 #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"
20 #endif
21 
22 namespace webrtc {
23 namespace {
24 
25 #if WEBRTC_APM_DEBUG_DUMP == 1
26 
27 #if defined(WEBRTC_WIN)
28 constexpr char kPathDelimiter = '\\';
29 #else
30 constexpr char kPathDelimiter = '/';
31 #endif
32 
FormFileName(absl::string_view output_dir,absl::string_view name,int instance_index,int reinit_index,absl::string_view suffix)33 std::string FormFileName(absl::string_view output_dir,
34                          absl::string_view name,
35                          int instance_index,
36                          int reinit_index,
37                          absl::string_view suffix) {
38   char buf[1024];
39   rtc::SimpleStringBuilder ss(buf);
40   if (!output_dir.empty()) {
41     ss << output_dir;
42     if (output_dir.back() != kPathDelimiter) {
43       ss << kPathDelimiter;
44     }
45   }
46   ss << name << "_" << instance_index << "-" << reinit_index << suffix;
47   return ss.str();
48 }
49 #endif
50 
51 }  // namespace
52 
53 #if WEBRTC_APM_DEBUG_DUMP == 1
ApmDataDumper(int instance_index)54 ApmDataDumper::ApmDataDumper(int instance_index)
55     : instance_index_(instance_index) {}
56 #else
ApmDataDumper(int instance_index)57 ApmDataDumper::ApmDataDumper(int instance_index) {}
58 #endif
59 
60 ApmDataDumper::~ApmDataDumper() = default;
61 
62 #if WEBRTC_APM_DEBUG_DUMP == 1
63 bool ApmDataDumper::recording_activated_ = false;
64 absl::optional<int> ApmDataDumper::dump_set_to_use_;
65 char ApmDataDumper::output_dir_[] = "";
66 
GetRawFile(absl::string_view name)67 FILE* ApmDataDumper::GetRawFile(absl::string_view name) {
68   std::string filename = FormFileName(output_dir_, name, instance_index_,
69                                       recording_set_index_, ".dat");
70   auto& f = raw_files_[filename];
71   if (!f) {
72     f.reset(fopen(filename.c_str(), "wb"));
73     RTC_CHECK(f.get()) << "Cannot write to " << filename << ".";
74   }
75   return f.get();
76 }
77 
GetWavFile(absl::string_view name,int sample_rate_hz,int num_channels,WavFile::SampleFormat format)78 WavWriter* ApmDataDumper::GetWavFile(absl::string_view name,
79                                      int sample_rate_hz,
80                                      int num_channels,
81                                      WavFile::SampleFormat format) {
82   std::string filename = FormFileName(output_dir_, name, instance_index_,
83                                       recording_set_index_, ".wav");
84   auto& f = wav_files_[filename];
85   if (!f) {
86     f.reset(
87         new WavWriter(filename.c_str(), sample_rate_hz, num_channels, format));
88   }
89   return f.get();
90 }
91 #endif
92 
93 }  // namespace webrtc
94