OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2017 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 <utility> |
| 12 |
| 13 #include "webrtc/modules/audio_processing/aec_dump/aec_dump_impl.h" |
| 14 |
| 15 #include "webrtc/base/checks.h" |
| 16 #include "webrtc/base/event.h" |
| 17 #include "webrtc/base/ptr_util.h" |
| 18 #include "webrtc/modules/audio_processing/aec_dump/aec_dump_factory.h" |
| 19 |
| 20 namespace webrtc { |
| 21 |
| 22 AecDumpImpl::AecDumpImpl(int64_t max_log_size_bytes, |
| 23 rtc::TaskQueue* worker_queue) |
| 24 : debug_file_(FileWrapper::Create()), |
| 25 num_bytes_left_for_log_(max_log_size_bytes), |
| 26 worker_queue_(worker_queue), |
| 27 capture_stream_info_(CreateWriteToFileTask()) {} |
| 28 |
| 29 AecDumpImpl::AecDumpImpl(rtc::PlatformFile file, |
| 30 int64_t max_log_size_bytes, |
| 31 rtc::TaskQueue* worker_queue) |
| 32 : AecDumpImpl(max_log_size_bytes, worker_queue) { |
| 33 FILE* handle = rtc::FdopenPlatformFileForWriting(file); |
| 34 RTC_DCHECK(handle); |
| 35 debug_file_->OpenFromFileHandle(handle); |
| 36 } |
| 37 |
| 38 AecDumpImpl::AecDumpImpl(std::string file_name, |
| 39 int64_t max_log_size_bytes, |
| 40 rtc::TaskQueue* worker_queue) |
| 41 : AecDumpImpl(max_log_size_bytes, worker_queue) { |
| 42 RTC_DCHECK(debug_file_); |
| 43 debug_file_->OpenFile(file_name.c_str(), false); |
| 44 } |
| 45 |
| 46 AecDumpImpl::AecDumpImpl(FILE* handle, |
| 47 int64_t max_log_size_bytes, |
| 48 rtc::TaskQueue* worker_queue) |
| 49 : AecDumpImpl(max_log_size_bytes, worker_queue) { |
| 50 RTC_DCHECK(debug_file_); |
| 51 debug_file_->OpenFromFileHandle(handle); |
| 52 } |
| 53 |
| 54 AecDumpImpl::~AecDumpImpl() { |
| 55 // Block until all tasks have finished running. |
| 56 rtc::Event thread_sync_event(false /* manual_reset */, false); |
| 57 worker_queue_->PostTask([&thread_sync_event] { thread_sync_event.Set(); }); |
| 58 // Wait until the event has been signaled with .Set(). By then all |
| 59 // pending tasks will have finished. |
| 60 thread_sync_event.Wait(rtc::Event::kForever); |
| 61 } |
| 62 |
| 63 AecDump::CaptureStreamInfo* AecDumpImpl::GetCaptureStreamInfo() { |
| 64 return &capture_stream_info_; |
| 65 } |
| 66 |
| 67 void AecDumpImpl::WriteInitMessage( |
| 68 const InternalAPMStreamsConfig& streams_config) { |
| 69 auto task = CreateWriteToFileTask(); |
| 70 auto* event = task->GetEvent(); |
| 71 event->set_type(audioproc::Event::INIT); |
| 72 audioproc::Init* msg = event->mutable_init(); |
| 73 |
| 74 msg->set_sample_rate(streams_config.input_sample_rate); |
| 75 msg->set_output_sample_rate(streams_config.output_sample_rate); |
| 76 msg->set_reverse_sample_rate(streams_config.render_input_sample_rate); |
| 77 msg->set_reverse_output_sample_rate(streams_config.render_output_sample_rate); |
| 78 |
| 79 msg->set_num_input_channels( |
| 80 static_cast<int32_t>(streams_config.input_num_channels)); |
| 81 msg->set_num_output_channels( |
| 82 static_cast<int32_t>(streams_config.output_num_channels)); |
| 83 msg->set_num_reverse_channels( |
| 84 static_cast<int32_t>(streams_config.render_input_num_channels)); |
| 85 msg->set_num_reverse_output_channels( |
| 86 streams_config.render_output_num_channels); |
| 87 |
| 88 worker_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(std::move(task))); |
| 89 } |
| 90 |
| 91 void AecDumpImpl::WriteRenderStreamMessage(const AudioFrame& frame) { |
| 92 auto task = CreateWriteToFileTask(); |
| 93 auto* event = task->GetEvent(); |
| 94 |
| 95 event->set_type(audioproc::Event::REVERSE_STREAM); |
| 96 audioproc::ReverseStream* msg = event->mutable_reverse_stream(); |
| 97 const size_t data_size = |
| 98 sizeof(int16_t) * frame.samples_per_channel_ * frame.num_channels_; |
| 99 msg->set_data(frame.data_, data_size); |
| 100 |
| 101 worker_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(std::move(task))); |
| 102 } |
| 103 |
| 104 void AecDumpImpl::WriteRenderStreamMessage(const FloatAudioFrame& src) { |
| 105 auto task = CreateWriteToFileTask(); |
| 106 auto* event = task->GetEvent(); |
| 107 |
| 108 // auto event = std::unique_ptr<audioproc::Event>(new audioproc::Event()); |
| 109 event->set_type(audioproc::Event::REVERSE_STREAM); |
| 110 |
| 111 audioproc::ReverseStream* msg = event->mutable_reverse_stream(); |
| 112 |
| 113 for (size_t i = 0; i < src.num_channels(); ++i) { |
| 114 const auto& channel_view = src.channel(i); |
| 115 msg->add_channel(channel_view.begin(), sizeof(float) * channel_view.size()); |
| 116 } |
| 117 |
| 118 worker_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(std::move(task))); |
| 119 } |
| 120 |
| 121 void AecDumpImpl::WriteCaptureStreamMessage() { |
| 122 auto task = capture_stream_info_.GetTask(); |
| 123 RTC_DCHECK(task); |
| 124 worker_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(std::move(task))); |
| 125 capture_stream_info_.SetTask(CreateWriteToFileTask()); |
| 126 } |
| 127 |
| 128 void CopyFromConfigToEvent(const webrtc::InternalAPMConfig& config, |
| 129 webrtc::audioproc::Config* pb_cfg) { |
| 130 pb_cfg->set_aec_enabled(config.aec_enabled); |
| 131 pb_cfg->set_aec_delay_agnostic_enabled(config.aec_delay_agnostic_enabled); |
| 132 pb_cfg->set_aec_drift_compensation_enabled( |
| 133 config.aec_drift_compensation_enabled); |
| 134 pb_cfg->set_aec_extended_filter_enabled(config.aec_extended_filter_enabled); |
| 135 pb_cfg->set_aec_suppression_level(config.aec_suppression_level); |
| 136 |
| 137 pb_cfg->set_aecm_enabled(config.aecm_enabled); |
| 138 pb_cfg->set_aecm_comfort_noise_enabled(config.aecm_comfort_noise_enabled); |
| 139 pb_cfg->set_aecm_routing_mode(config.aecm_routing_mode); |
| 140 |
| 141 pb_cfg->set_agc_enabled(config.agc_enabled); |
| 142 pb_cfg->set_agc_mode(config.agc_mode); |
| 143 pb_cfg->set_agc_limiter_enabled(config.agc_limiter_enabled); |
| 144 pb_cfg->set_noise_robust_agc_enabled(config.noise_robust_agc_enabled); |
| 145 |
| 146 pb_cfg->set_hpf_enabled(config.hpf_enabled); |
| 147 |
| 148 pb_cfg->set_ns_enabled(config.ns_enabled); |
| 149 pb_cfg->set_ns_level(config.ns_level); |
| 150 |
| 151 pb_cfg->set_transient_suppression_enabled( |
| 152 config.transient_suppression_enabled); |
| 153 pb_cfg->set_intelligibility_enhancer_enabled( |
| 154 config.intelligibility_enhancer_enabled); |
| 155 |
| 156 pb_cfg->set_experiments_description(config.experiments_description); |
| 157 } |
| 158 |
| 159 void AecDumpImpl::WriteConfig(const InternalAPMConfig& config, bool forced) { |
| 160 auto task = CreateWriteToFileTask(); |
| 161 auto* event = task->GetEvent(); |
| 162 event->set_type(audioproc::Event::CONFIG); |
| 163 CopyFromConfigToEvent(config, event->mutable_config()); |
| 164 |
| 165 ProtoString serialized_config = event->mutable_config()->SerializeAsString(); |
| 166 { |
| 167 rtc::CritScope cs(&config_string_lock_); |
| 168 if (!forced && serialized_config == last_serialized_capture_config_) { |
| 169 return; |
| 170 } |
| 171 last_serialized_capture_config_ = serialized_config; |
| 172 } |
| 173 |
| 174 worker_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(std::move(task))); |
| 175 } |
| 176 |
| 177 std::unique_ptr<WriteToFileTask> AecDumpImpl::CreateWriteToFileTask() { |
| 178 return rtc::MakeUnique<WriteToFileTask>(debug_file_.get(), |
| 179 &num_bytes_left_for_log_); |
| 180 } |
| 181 |
| 182 std::unique_ptr<AecDump> AecDumpFactory::Create(rtc::PlatformFile file, |
| 183 int64_t max_log_size_bytes, |
| 184 rtc::TaskQueue* worker_queue) { |
| 185 return std::unique_ptr<AecDumpImpl>( |
| 186 new AecDumpImpl(file, max_log_size_bytes, worker_queue)); |
| 187 } |
| 188 |
| 189 std::unique_ptr<AecDump> AecDumpFactory::Create(std::string file_name, |
| 190 int64_t max_log_size_bytes, |
| 191 rtc::TaskQueue* worker_queue) { |
| 192 return std::unique_ptr<AecDumpImpl>( |
| 193 new AecDumpImpl(file_name, max_log_size_bytes, worker_queue)); |
| 194 } |
| 195 |
| 196 std::unique_ptr<AecDump> AecDumpFactory::Create(FILE* handle, |
| 197 int64_t max_log_size_bytes, |
| 198 rtc::TaskQueue* worker_queue) { |
| 199 return std::unique_ptr<AecDumpImpl>( |
| 200 new AecDumpImpl(handle, max_log_size_bytes, worker_queue)); |
| 201 } |
| 202 } // namespace webrtc |
OLD | NEW |