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