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 namespace { | |
23 void CopyFromConfigToEvent(const webrtc::InternalAPMConfig& config, | |
24 webrtc::audioproc::Config* pb_cfg) { | |
25 pb_cfg->set_aec_enabled(config.aec_enabled); | |
26 pb_cfg->set_aec_delay_agnostic_enabled(config.aec_delay_agnostic_enabled); | |
27 pb_cfg->set_aec_drift_compensation_enabled( | |
28 config.aec_drift_compensation_enabled); | |
29 pb_cfg->set_aec_extended_filter_enabled(config.aec_extended_filter_enabled); | |
30 pb_cfg->set_aec_suppression_level(config.aec_suppression_level); | |
31 | |
32 pb_cfg->set_aecm_enabled(config.aecm_enabled); | |
33 pb_cfg->set_aecm_comfort_noise_enabled(config.aecm_comfort_noise_enabled); | |
34 pb_cfg->set_aecm_routing_mode(config.aecm_routing_mode); | |
35 | |
36 pb_cfg->set_agc_enabled(config.agc_enabled); | |
37 pb_cfg->set_agc_mode(config.agc_mode); | |
38 pb_cfg->set_agc_limiter_enabled(config.agc_limiter_enabled); | |
39 pb_cfg->set_noise_robust_agc_enabled(config.noise_robust_agc_enabled); | |
40 | |
41 pb_cfg->set_hpf_enabled(config.hpf_enabled); | |
42 | |
43 pb_cfg->set_ns_enabled(config.ns_enabled); | |
44 pb_cfg->set_ns_level(config.ns_level); | |
45 | |
46 pb_cfg->set_transient_suppression_enabled( | |
47 config.transient_suppression_enabled); | |
48 pb_cfg->set_intelligibility_enhancer_enabled( | |
49 config.intelligibility_enhancer_enabled); | |
50 | |
51 pb_cfg->set_experiments_description(config.experiments_description); | |
52 } | |
53 | |
54 } // namespace | |
55 | |
56 AecDumpImpl::AecDumpImpl(std::unique_ptr<FileWrapper> debug_file, | |
57 int64_t max_log_size_bytes, | |
58 rtc::TaskQueue* worker_queue) | |
59 : debug_file_(std::move(debug_file)), | |
60 num_bytes_left_for_log_(max_log_size_bytes), | |
61 worker_queue_(worker_queue), | |
62 capture_stream_info_(CreateWriteToFileTask()) {} | |
63 | |
64 AecDumpImpl::~AecDumpImpl() { | |
65 // Block until all tasks have finished running. | |
peah-webrtc
2017/05/17 05:36:33
It would be so nice if this could be avoided. Plea
aleloi
2017/05/17 10:49:55
Acknowledged.
| |
66 rtc::Event thread_sync_event(false /* manual_reset */, false); | |
67 worker_queue_->PostTask([&thread_sync_event] { thread_sync_event.Set(); }); | |
68 // Wait until the event has been signaled with .Set(). By then all | |
69 // pending tasks will have finished. | |
70 thread_sync_event.Wait(rtc::Event::kForever); | |
71 } | |
72 | |
73 void AecDumpImpl::AddCaptureStreamInput(const FloatAudioFrame& src) { | |
74 capture_stream_info_.AddInput(src); | |
75 } | |
76 | |
77 void AecDumpImpl::AddCaptureStreamOutput(const FloatAudioFrame& src) { | |
78 capture_stream_info_.AddOutput(src); | |
79 } | |
80 | |
81 void AecDumpImpl::AddCaptureStreamInput(const AudioFrame& frame) { | |
82 capture_stream_info_.AddInput(frame); | |
83 } | |
84 | |
85 void AecDumpImpl::AddCaptureStreamOutput(const AudioFrame& frame) { | |
86 capture_stream_info_.AddOutput(frame); | |
87 } | |
88 | |
89 void AecDumpImpl::AddAudioProcessingState(const AudioProcessingState& state) { | |
90 capture_stream_info_.AddAudioProcessingState(state); | |
91 } | |
92 | |
93 void AecDumpImpl::WriteInitMessage( | |
94 const InternalAPMStreamsConfig& streams_config) { | |
95 auto task = CreateWriteToFileTask(); | |
96 auto* event = task->GetEvent(); | |
97 event->set_type(audioproc::Event::INIT); | |
98 audioproc::Init* msg = event->mutable_init(); | |
99 | |
100 msg->set_sample_rate(streams_config.input_sample_rate); | |
101 msg->set_output_sample_rate(streams_config.output_sample_rate); | |
102 msg->set_reverse_sample_rate(streams_config.render_input_sample_rate); | |
103 msg->set_reverse_output_sample_rate(streams_config.render_output_sample_rate); | |
104 | |
105 msg->set_num_input_channels( | |
106 static_cast<int32_t>(streams_config.input_num_channels)); | |
107 msg->set_num_output_channels( | |
108 static_cast<int32_t>(streams_config.output_num_channels)); | |
109 msg->set_num_reverse_channels( | |
110 static_cast<int32_t>(streams_config.render_input_num_channels)); | |
111 msg->set_num_reverse_output_channels( | |
112 streams_config.render_output_num_channels); | |
113 | |
114 worker_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(std::move(task))); | |
peah-webrtc
2017/05/17 05:36:33
Just wondering, is it necessary to explicitly crea
aleloi
2017/05/17 10:49:55
I think it should work, but it doesn't. It seems t
peah-webrtc
2017/05/17 11:13:50
No, I don't think that makes sense either. Let's k
| |
115 } | |
116 | |
117 void AecDumpImpl::WriteRenderStreamMessage(const AudioFrame& frame) { | |
118 auto task = CreateWriteToFileTask(); | |
119 auto* event = task->GetEvent(); | |
120 | |
121 event->set_type(audioproc::Event::REVERSE_STREAM); | |
122 audioproc::ReverseStream* msg = event->mutable_reverse_stream(); | |
123 const size_t data_size = | |
124 sizeof(int16_t) * frame.samples_per_channel_ * frame.num_channels_; | |
125 msg->set_data(frame.data_, data_size); | |
126 | |
127 worker_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(std::move(task))); | |
128 } | |
129 | |
130 void AecDumpImpl::WriteRenderStreamMessage(const FloatAudioFrame& src) { | |
131 auto task = CreateWriteToFileTask(); | |
132 auto* event = task->GetEvent(); | |
133 | |
134 event->set_type(audioproc::Event::REVERSE_STREAM); | |
135 | |
136 audioproc::ReverseStream* msg = event->mutable_reverse_stream(); | |
137 | |
138 for (size_t i = 0; i < src.num_channels(); ++i) { | |
139 const auto& channel_view = src.channel(i); | |
140 msg->add_channel(channel_view.begin(), sizeof(float) * channel_view.size()); | |
141 } | |
142 | |
143 worker_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(std::move(task))); | |
144 } | |
145 | |
146 void AecDumpImpl::WriteCaptureStreamMessage() { | |
147 auto task = capture_stream_info_.GetTask(); | |
148 RTC_DCHECK(task); | |
149 worker_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(std::move(task))); | |
150 capture_stream_info_.SetTask(CreateWriteToFileTask()); | |
151 } | |
152 | |
153 void AecDumpImpl::WriteConfig(const InternalAPMConfig& config, bool forced) { | |
154 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_); | |
155 auto task = CreateWriteToFileTask(); | |
156 auto* event = task->GetEvent(); | |
157 event->set_type(audioproc::Event::CONFIG); | |
158 CopyFromConfigToEvent(config, event->mutable_config()); | |
159 | |
160 ProtoString serialized_config = event->mutable_config()->SerializeAsString(); | |
161 if (!forced && serialized_config == last_serialized_capture_config_) { | |
162 return; | |
163 } | |
164 last_serialized_capture_config_ = serialized_config; | |
165 | |
166 worker_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(std::move(task))); | |
167 } | |
168 | |
169 std::unique_ptr<WriteToFileTask> AecDumpImpl::CreateWriteToFileTask() { | |
170 return rtc::MakeUnique<WriteToFileTask>(debug_file_.get(), | |
171 &num_bytes_left_for_log_); | |
172 } | |
173 | |
174 std::unique_ptr<AecDump> AecDumpFactory::Create(rtc::PlatformFile file, | |
175 int64_t max_log_size_bytes, | |
176 rtc::TaskQueue* worker_queue) { | |
177 RTC_DCHECK(worker_queue); | |
178 std::unique_ptr<FileWrapper> debug_file(FileWrapper::Create()); | |
179 FILE* handle = rtc::FdopenPlatformFileForWriting(file); | |
180 if (!handle) { | |
181 return nullptr; | |
182 } | |
183 if (!debug_file->OpenFromFileHandle(handle)) { | |
184 return nullptr; | |
185 } | |
186 return std::unique_ptr<AecDumpImpl>( | |
peah-webrtc
2017/05/17 05:36:32
MakeUnique?
aleloi
2017/05/17 10:49:56
Done.
| |
187 new AecDumpImpl(std::move(debug_file), max_log_size_bytes, worker_queue)); | |
188 } | |
189 | |
190 std::unique_ptr<AecDump> AecDumpFactory::Create(std::string file_name, | |
191 int64_t max_log_size_bytes, | |
192 rtc::TaskQueue* worker_queue) { | |
193 RTC_DCHECK(worker_queue); | |
194 std::unique_ptr<FileWrapper> debug_file(FileWrapper::Create()); | |
195 if (!debug_file->OpenFile(file_name.c_str(), false)) { | |
196 return nullptr; | |
197 } | |
198 return std::unique_ptr<AecDumpImpl>( | |
peah-webrtc
2017/05/17 05:36:33
MakeUnique?
aleloi
2017/05/17 10:49:56
Done.
| |
199 new AecDumpImpl(std::move(debug_file), max_log_size_bytes, worker_queue)); | |
200 } | |
201 | |
202 std::unique_ptr<AecDump> AecDumpFactory::Create(FILE* handle, | |
203 int64_t max_log_size_bytes, | |
204 rtc::TaskQueue* worker_queue) { | |
205 RTC_DCHECK(worker_queue); | |
206 RTC_DCHECK(handle); | |
207 std::unique_ptr<FileWrapper> debug_file(FileWrapper::Create()); | |
208 if (!debug_file->OpenFromFileHandle(handle)) { | |
209 return nullptr; | |
210 } | |
211 return std::unique_ptr<AecDumpImpl>( | |
peah-webrtc
2017/05/17 05:36:33
MakeUnique?
aleloi
2017/05/17 10:49:55
Done.
| |
212 new AecDumpImpl(std::move(debug_file), max_log_size_bytes, worker_queue)); | |
213 } | |
214 } // namespace webrtc | |
OLD | NEW |