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/modules/audio_processing/aec_dump/aec_dump_factory.h" |
| 18 |
| 19 namespace webrtc { |
| 20 |
| 21 class WriteToFileTask : public rtc::QueuedTask { |
| 22 public: |
| 23 WriteToFileTask(webrtc::FileWrapper* debug_file, |
| 24 std::unique_ptr<audioproc::Event> event, |
| 25 int64_t* num_bytes_left_for_log) |
| 26 : debug_file_(debug_file), |
| 27 event_(std::move(event)), |
| 28 num_bytes_left_for_log_(num_bytes_left_for_log) {} |
| 29 |
| 30 private: |
| 31 bool IsRoomForNextEvent(size_t event_byte_size) const { |
| 32 int64_t next_message_size = event_byte_size + sizeof(int32_t); |
| 33 return (*num_bytes_left_for_log_ < 0) || |
| 34 (*num_bytes_left_for_log_ >= next_message_size); |
| 35 } |
| 36 |
| 37 void UpdateBytesLeft(size_t event_byte_size) { |
| 38 RTC_DCHECK(IsRoomForNextEvent(event_byte_size)); |
| 39 if (*num_bytes_left_for_log_ >= 0) { |
| 40 *num_bytes_left_for_log_ -= (sizeof(int32_t) + event_byte_size); |
| 41 } |
| 42 } |
| 43 |
| 44 bool Run() override { |
| 45 if (!debug_file_->is_open()) { |
| 46 return true; |
| 47 } |
| 48 |
| 49 std::string event_string; |
| 50 event_->SerializeToString(&event_string); |
| 51 |
| 52 const size_t event_byte_size = event_->ByteSize(); |
| 53 |
| 54 if (!IsRoomForNextEvent(event_byte_size)) { |
| 55 debug_file_->CloseFile(); |
| 56 return true; |
| 57 } |
| 58 |
| 59 UpdateBytesLeft(event_byte_size); |
| 60 |
| 61 // Write message preceded by its size. |
| 62 if (!debug_file_->Write(&event_byte_size, sizeof(int32_t))) { |
| 63 RTC_NOTREACHED(); |
| 64 } |
| 65 if (!debug_file_->Write(event_string.data(), event_string.length())) { |
| 66 RTC_NOTREACHED(); |
| 67 } |
| 68 return true; // Delete task from queue at once. TODO(aleloi): |
| 69 // instead consider a 'mega-task' that returns |
| 70 // 'false', checks if there is something in a |
| 71 // swap-queue and reposts itself periodically. |
| 72 } |
| 73 |
| 74 webrtc::FileWrapper* debug_file_; |
| 75 std::unique_ptr<audioproc::Event> event_; |
| 76 int64_t* num_bytes_left_for_log_; |
| 77 }; |
| 78 |
| 79 AecDumpImpl::AecDumpImpl(std::string file_name, |
| 80 int64_t max_log_size_bytes, |
| 81 rtc::TaskQueue* worker_queue) |
| 82 : debug_file_(FileWrapper::Create()), worker_queue_(worker_queue) { |
| 83 RTC_DCHECK(debug_file_); |
| 84 worker_queue_->PostTask([this, file_name, max_log_size_bytes]() { |
| 85 num_bytes_left_for_log_ = max_log_size_bytes; |
| 86 debug_file_->OpenFile(file_name.c_str(), false); |
| 87 }); |
| 88 } |
| 89 |
| 90 AecDumpImpl::AecDumpImpl(FILE* handle, |
| 91 int64_t max_log_size_bytes, |
| 92 rtc::TaskQueue* worker_queue) |
| 93 : debug_file_(FileWrapper::Create()), worker_queue_(worker_queue) { |
| 94 RTC_DCHECK(debug_file_); |
| 95 worker_queue_->PostTask([this, handle, max_log_size_bytes]() { |
| 96 num_bytes_left_for_log_ = max_log_size_bytes; |
| 97 debug_file_->OpenFromFileHandle(handle); |
| 98 }); |
| 99 } |
| 100 |
| 101 AecDumpImpl::~AecDumpImpl() { |
| 102 // Block until all tasks have finished running. |
| 103 rtc::Event thread_sync_event(false /* manual_reset */, false); |
| 104 worker_queue_->PostTask([&thread_sync_event] { thread_sync_event.Set(); }); |
| 105 thread_sync_event.Wait(rtc::Event::kForever); |
| 106 } |
| 107 |
| 108 std::unique_ptr<AecDump::CaptureStreamInfo> |
| 109 AecDumpImpl::GetCaptureStreamInfo() { |
| 110 return std::unique_ptr<CaptureStreamInfoImpl>(new CaptureStreamInfoImpl( |
| 111 std::unique_ptr<audioproc::Event>(new audioproc::Event()))); |
| 112 } |
| 113 |
| 114 void AecDumpImpl::WriteInitMessage(const ProcessingConfig& api_format) { |
| 115 auto event = std::unique_ptr<audioproc::Event>(new audioproc::Event()); |
| 116 event->set_type(audioproc::Event::INIT); |
| 117 audioproc::Init* msg = event->mutable_init(); |
| 118 |
| 119 msg->set_sample_rate(api_format.input_stream().sample_rate_hz()); |
| 120 msg->set_num_input_channels(static_cast<google::protobuf::int32>( |
| 121 api_format.input_stream().num_channels())); |
| 122 msg->set_num_output_channels(static_cast<google::protobuf::int32>( |
| 123 api_format.output_stream().num_channels())); |
| 124 msg->set_num_reverse_channels(static_cast<google::protobuf::int32>( |
| 125 api_format.reverse_input_stream().num_channels())); |
| 126 msg->set_reverse_sample_rate( |
| 127 api_format.reverse_input_stream().sample_rate_hz()); |
| 128 msg->set_output_sample_rate(api_format.output_stream().sample_rate_hz()); |
| 129 msg->set_reverse_output_sample_rate( |
| 130 api_format.reverse_output_stream().sample_rate_hz()); |
| 131 msg->set_num_reverse_output_channels( |
| 132 api_format.reverse_output_stream().num_channels()); |
| 133 |
| 134 PostTask(std::move(event)); |
| 135 } |
| 136 |
| 137 void AecDumpImpl::WriteRenderStreamMessage(const AudioFrame& frame) { |
| 138 auto event = std::unique_ptr<audioproc::Event>(new audioproc::Event()); |
| 139 |
| 140 event->set_type(audioproc::Event::REVERSE_STREAM); |
| 141 audioproc::ReverseStream* msg = event->mutable_reverse_stream(); |
| 142 const size_t data_size = |
| 143 sizeof(int16_t) * frame.samples_per_channel_ * frame.num_channels_; |
| 144 msg->set_data(frame.data_, data_size); |
| 145 |
| 146 PostTask(std::move(event)); |
| 147 } |
| 148 |
| 149 void AecDumpImpl::WriteRenderStreamMessage( |
| 150 std::vector<rtc::ArrayView<const float>> src) { |
| 151 auto event = std::unique_ptr<audioproc::Event>(new audioproc::Event()); |
| 152 event->set_type(audioproc::Event::REVERSE_STREAM); |
| 153 |
| 154 audioproc::ReverseStream* msg = event->mutable_reverse_stream(); |
| 155 |
| 156 for (const auto& frame : src) { |
| 157 msg->add_channel(frame.begin(), frame.size()); |
| 158 } |
| 159 |
| 160 PostTask(std::move(event)); |
| 161 } |
| 162 |
| 163 void AecDumpImpl::WriteCaptureStreamMessage( |
| 164 std::unique_ptr<CaptureStreamInfo> capture_stream_info) { |
| 165 // Really ugly, how is it done better? |
| 166 auto event_ptr = |
| 167 static_cast<CaptureStreamInfoImpl*>(capture_stream_info.get()) |
| 168 ->GetEventMsg(); |
| 169 if (event_ptr) { |
| 170 PostTask(std::move(event_ptr)); |
| 171 } |
| 172 } |
| 173 |
| 174 void CopyFromConfigToEvent(const webrtc::InternalAPMConfig& config, |
| 175 webrtc::audioproc::Config* pb_cfg) { |
| 176 pb_cfg->set_aec_enabled(config.aec_enabled); |
| 177 pb_cfg->set_aec_delay_agnostic_enabled(config.aec_delay_agnostic_enabled); |
| 178 pb_cfg->set_aec_drift_compensation_enabled( |
| 179 config.aec_drift_compensation_enabled); |
| 180 pb_cfg->set_aec_extended_filter_enabled(config.aec_extended_filter_enabled); |
| 181 pb_cfg->set_aec_suppression_level(config.aec_suppression_level); |
| 182 |
| 183 pb_cfg->set_aecm_enabled(config.aecm_enabled); |
| 184 pb_cfg->set_aecm_comfort_noise_enabled(config.aecm_comfort_noise_enabled); |
| 185 pb_cfg->set_aecm_routing_mode(config.aecm_routing_mode); |
| 186 |
| 187 pb_cfg->set_agc_enabled(config.agc_enabled); |
| 188 pb_cfg->set_agc_mode(config.agc_mode); |
| 189 pb_cfg->set_agc_limiter_enabled(config.agc_limiter_enabled); |
| 190 pb_cfg->set_noise_robust_agc_enabled(config.noise_robust_agc_enabled); |
| 191 |
| 192 pb_cfg->set_hpf_enabled(config.hpf_enabled); |
| 193 |
| 194 pb_cfg->set_ns_enabled(config.ns_enabled); |
| 195 pb_cfg->set_ns_level(config.ns_level); |
| 196 |
| 197 pb_cfg->set_transient_suppression_enabled( |
| 198 config.transient_suppression_enabled); |
| 199 pb_cfg->set_intelligibility_enhancer_enabled( |
| 200 config.intelligibility_enhancer_enabled); |
| 201 |
| 202 pb_cfg->set_experiments_description(config.experiments_description); |
| 203 } |
| 204 |
| 205 void AecDumpImpl::WriteConfig(const InternalAPMConfig& config, bool forced) { |
| 206 auto event = std::unique_ptr<audioproc::Event>(new audioproc::Event()); |
| 207 event->set_type(audioproc::Event::CONFIG); |
| 208 CopyFromConfigToEvent(config, event->mutable_config()); |
| 209 |
| 210 std::string serialized_config = event->mutable_config()->SerializeAsString(); |
| 211 { |
| 212 rtc::CritScope cs(&config_string_lock_); |
| 213 if (!forced && serialized_config == last_serialized_capture_config_) { |
| 214 return; |
| 215 } |
| 216 last_serialized_capture_config_ = serialized_config; |
| 217 } |
| 218 |
| 219 PostTask(std::move(event)); |
| 220 } |
| 221 |
| 222 void AecDumpImpl::PostTask(std::unique_ptr<audioproc::Event> event) { |
| 223 RTC_DCHECK(event); |
| 224 worker_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(new WriteToFileTask( |
| 225 debug_file_.get(), std::move(event), &num_bytes_left_for_log_))); |
| 226 } |
| 227 |
| 228 std::unique_ptr<AecDump> AecDumpFactory::Create(std::string file_name, |
| 229 int64_t max_log_size_bytes, |
| 230 rtc::TaskQueue* worker_queue) { |
| 231 return std::unique_ptr<AecDumpImpl>( |
| 232 new AecDumpImpl(file_name, max_log_size_bytes, worker_queue)); |
| 233 } |
| 234 |
| 235 std::unique_ptr<AecDump> AecDumpFactory::Create(FILE* handle, |
| 236 int64_t max_log_size_bytes, |
| 237 rtc::TaskQueue* worker_queue) { |
| 238 return std::unique_ptr<AecDumpImpl>( |
| 239 new AecDumpImpl(handle, max_log_size_bytes, worker_queue)); |
| 240 } |
| 241 } // namespace webrtc |
OLD | NEW |