OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 28 matching lines...) Loading... | |
39 | 39 |
40 AudioDeviceBuffer::AudioDeviceBuffer() | 40 AudioDeviceBuffer::AudioDeviceBuffer() |
41 : audio_transport_cb_(nullptr), | 41 : audio_transport_cb_(nullptr), |
42 task_queue_(kTimerQueueName), | 42 task_queue_(kTimerQueueName), |
43 playing_(false), | 43 playing_(false), |
44 recording_(false), | 44 recording_(false), |
45 rec_sample_rate_(0), | 45 rec_sample_rate_(0), |
46 play_sample_rate_(0), | 46 play_sample_rate_(0), |
47 rec_channels_(0), | 47 rec_channels_(0), |
48 play_channels_(0), | 48 play_channels_(0), |
49 rec_bytes_per_sample_(0), | |
50 play_bytes_per_sample_(0), | |
51 current_mic_level_(0), | 49 current_mic_level_(0), |
52 new_mic_level_(0), | 50 new_mic_level_(0), |
53 typing_status_(false), | 51 typing_status_(false), |
54 play_delay_ms_(0), | 52 play_delay_ms_(0), |
55 rec_delay_ms_(0), | 53 rec_delay_ms_(0), |
56 clock_drift_(0), | 54 clock_drift_(0), |
57 num_stat_reports_(0), | 55 num_stat_reports_(0), |
58 rec_callbacks_(0), | 56 rec_callbacks_(0), |
59 last_rec_callbacks_(0), | 57 last_rec_callbacks_(0), |
60 play_callbacks_(0), | 58 play_callbacks_(0), |
61 last_play_callbacks_(0), | 59 last_play_callbacks_(0), |
62 rec_samples_(0), | 60 rec_samples_(0), |
63 last_rec_samples_(0), | 61 last_rec_samples_(0), |
64 play_samples_(0), | 62 play_samples_(0), |
65 last_play_samples_(0), | 63 last_play_samples_(0), |
66 last_timer_task_time_(0), | |
67 max_rec_level_(0), | 64 max_rec_level_(0), |
68 max_play_level_(0), | 65 max_play_level_(0), |
66 last_timer_task_time_(0), | |
67 last_playout_time_(0), | |
69 rec_stat_count_(0), | 68 rec_stat_count_(0), |
70 play_stat_count_(0), | 69 play_stat_count_(0), |
71 play_start_time_(0), | 70 play_start_time_(0), |
72 rec_start_time_(0), | 71 rec_start_time_(0), |
73 only_silence_recorded_(true) { | 72 only_silence_recorded_(true) { |
74 LOG(INFO) << "AudioDeviceBuffer::ctor"; | 73 LOG(INFO) << "AudioDeviceBuffer::ctor"; |
75 } | 74 } |
76 | 75 |
77 AudioDeviceBuffer::~AudioDeviceBuffer() { | 76 AudioDeviceBuffer::~AudioDeviceBuffer() { |
78 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 77 RTC_DCHECK_RUN_ON(&thread_checker_); |
79 RTC_DCHECK(!playing_); | 78 RTC_DCHECK(!playing_); |
80 RTC_DCHECK(!recording_); | 79 RTC_DCHECK(!recording_); |
81 LOG(INFO) << "AudioDeviceBuffer::~dtor"; | 80 LOG(INFO) << "AudioDeviceBuffer::~dtor"; |
82 } | 81 } |
83 | 82 |
84 int32_t AudioDeviceBuffer::RegisterAudioCallback( | 83 int32_t AudioDeviceBuffer::RegisterAudioCallback( |
85 AudioTransport* audio_callback) { | 84 AudioTransport* audio_callback) { |
85 RTC_DCHECK_RUN_ON(&thread_checker_); | |
86 LOG(INFO) << __FUNCTION__; | 86 LOG(INFO) << __FUNCTION__; |
87 rtc::CritScope lock(&lock_cb_); | 87 if (playing_ || recording_) { |
88 LOG(LS_ERROR) << "Failed to set audio transport since media was active"; | |
89 return -1; | |
90 } | |
88 audio_transport_cb_ = audio_callback; | 91 audio_transport_cb_ = audio_callback; |
89 return 0; | 92 return 0; |
90 } | 93 } |
91 | 94 |
92 void AudioDeviceBuffer::StartPlayout() { | 95 void AudioDeviceBuffer::StartPlayout() { |
93 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 96 RTC_DCHECK_RUN_ON(&thread_checker_); |
94 // TODO(henrika): allow for usage of DCHECK(!playing_) here instead. Today the | 97 // TODO(henrika): allow for usage of DCHECK(!playing_) here instead. Today the |
95 // ADM allows calling Start(), Start() by ignoring the second call but it | 98 // ADM allows calling Start(), Start() by ignoring the second call but it |
96 // makes more sense to only allow one call. | 99 // makes more sense to only allow one call. |
97 if (playing_) { | 100 if (playing_) { |
98 return; | 101 return; |
99 } | 102 } |
100 LOG(INFO) << __FUNCTION__; | 103 LOG(INFO) << __FUNCTION__; |
104 playout_thread_checker_.DetachFromThread(); | |
101 // Clear members tracking playout stats and do it on the task queue. | 105 // Clear members tracking playout stats and do it on the task queue. |
102 task_queue_.PostTask([this] { ResetPlayStats(); }); | 106 task_queue_.PostTask([this] { ResetPlayStats(); }); |
103 // Start a periodic timer based on task queue if not already done by the | 107 // Start a periodic timer based on task queue if not already done by the |
104 // recording side. | 108 // recording side. |
105 if (!recording_) { | 109 if (!recording_) { |
106 StartPeriodicLogging(); | 110 StartPeriodicLogging(); |
107 } | 111 } |
108 const uint64_t now_time = rtc::TimeMillis(); | 112 const uint64_t now_time = rtc::TimeMillis(); |
109 // Clear members that are only touched on the main (creating) thread. | 113 // Clear members that are only touched on the main (creating) thread. |
110 play_start_time_ = now_time; | 114 play_start_time_ = now_time; |
115 playing_ = true; | |
116 // This member is updated on the audio thread but it safe to initialize | |
117 // here since the media has not started yet (known by design). | |
kwiberg-webrtc
2016/11/02 14:14:57
So RTC_DCHECK_RUN_ON the audio thread, and detach
henrika_webrtc
2016/11/02 16:23:24
Not sure if I understand; care to elaborate?
| |
111 last_playout_time_ = now_time; | 118 last_playout_time_ = now_time; |
112 playing_ = true; | |
113 } | 119 } |
114 | 120 |
115 void AudioDeviceBuffer::StartRecording() { | 121 void AudioDeviceBuffer::StartRecording() { |
116 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 122 RTC_DCHECK_RUN_ON(&thread_checker_); |
117 if (recording_) { | 123 if (recording_) { |
118 return; | 124 return; |
119 } | 125 } |
120 LOG(INFO) << __FUNCTION__; | 126 LOG(INFO) << __FUNCTION__; |
127 recording_thread_checker_.DetachFromThread(); | |
121 // Clear members tracking recording stats and do it on the task queue. | 128 // Clear members tracking recording stats and do it on the task queue. |
122 task_queue_.PostTask([this] { ResetRecStats(); }); | 129 task_queue_.PostTask([this] { ResetRecStats(); }); |
123 // Start a periodic timer based on task queue if not already done by the | 130 // Start a periodic timer based on task queue if not already done by the |
124 // playout side. | 131 // playout side. |
125 if (!playing_) { | 132 if (!playing_) { |
126 StartPeriodicLogging(); | 133 StartPeriodicLogging(); |
127 } | 134 } |
128 // Clear members that will be touched on the main (creating) thread. | 135 // Clear members that will be touched on the main (creating) thread. |
129 rec_start_time_ = rtc::TimeMillis(); | 136 rec_start_time_ = rtc::TimeMillis(); |
130 recording_ = true; | 137 recording_ = true; |
131 // And finally a member which can be modified on the native audio thread. | 138 // And finally a member which can be modified on the native audio thread. |
132 // It is safe to do so since we know by design that the owning ADM has not | 139 // It is safe to do so since we know by design that the owning ADM has not |
133 // yet started the native audio recording. | 140 // yet started the native audio recording. |
134 only_silence_recorded_ = true; | 141 only_silence_recorded_ = true; |
135 } | 142 } |
136 | 143 |
137 void AudioDeviceBuffer::StopPlayout() { | 144 void AudioDeviceBuffer::StopPlayout() { |
138 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 145 RTC_DCHECK_RUN_ON(&thread_checker_); |
139 if (!playing_) { | 146 if (!playing_) { |
140 return; | 147 return; |
141 } | 148 } |
142 LOG(INFO) << __FUNCTION__; | 149 LOG(INFO) << __FUNCTION__; |
143 playing_ = false; | 150 playing_ = false; |
144 // Stop periodic logging if no more media is active. | 151 // Stop periodic logging if no more media is active. |
145 if (!recording_) { | 152 if (!recording_) { |
146 StopPeriodicLogging(); | 153 StopPeriodicLogging(); |
147 } | 154 } |
148 // Add diagnostic logging of delta times for playout callbacks. We are doing | 155 // Add diagnostic logging of delta times for playout callbacks. We are doing |
(...skipping 16 matching lines...) Loading... | |
165 LOG(INFO) << "total_diff_time: " << total_diff_time << ", " | 172 LOG(INFO) << "total_diff_time: " << total_diff_time << ", " |
166 << "num_measurements: " << num_measurements << ", " | 173 << "num_measurements: " << num_measurements << ", " |
167 << "average: " | 174 << "average: " |
168 << static_cast<float>(total_diff_time) / num_measurements; | 175 << static_cast<float>(total_diff_time) / num_measurements; |
169 } | 176 } |
170 } | 177 } |
171 LOG(INFO) << "total playout time: " << time_since_start; | 178 LOG(INFO) << "total playout time: " << time_since_start; |
172 } | 179 } |
173 | 180 |
174 void AudioDeviceBuffer::StopRecording() { | 181 void AudioDeviceBuffer::StopRecording() { |
175 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 182 RTC_DCHECK_RUN_ON(&thread_checker_); |
176 if (!recording_) { | 183 if (!recording_) { |
177 return; | 184 return; |
178 } | 185 } |
179 LOG(INFO) << __FUNCTION__; | 186 LOG(INFO) << __FUNCTION__; |
180 recording_ = false; | 187 recording_ = false; |
181 // Stop periodic logging if no more media is active. | 188 // Stop periodic logging if no more media is active. |
182 if (!playing_) { | 189 if (!playing_) { |
183 StopPeriodicLogging(); | 190 StopPeriodicLogging(); |
184 } | 191 } |
185 // Add UMA histogram to keep track of the case when only zeros have been | 192 // Add UMA histogram to keep track of the case when only zeros have been |
(...skipping 11 matching lines...) Loading... | |
197 const int only_zeros = static_cast<int>(only_silence_recorded_); | 204 const int only_zeros = static_cast<int>(only_silence_recorded_); |
198 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", only_zeros); | 205 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", only_zeros); |
199 LOG(INFO) << "HISTOGRAM(WebRTC.Audio.RecordedOnlyZeros): " << only_zeros; | 206 LOG(INFO) << "HISTOGRAM(WebRTC.Audio.RecordedOnlyZeros): " << only_zeros; |
200 } | 207 } |
201 LOG(INFO) << "total recording time: " << time_since_start; | 208 LOG(INFO) << "total recording time: " << time_since_start; |
202 } | 209 } |
203 | 210 |
204 int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) { | 211 int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) { |
205 LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")"; | 212 LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")"; |
206 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 213 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
214 rtc::CritScope lock(&lock_); | |
207 rec_sample_rate_ = fsHz; | 215 rec_sample_rate_ = fsHz; |
208 return 0; | 216 return 0; |
209 } | 217 } |
210 | 218 |
211 int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) { | 219 int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) { |
212 LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")"; | 220 LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")"; |
213 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 221 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
222 rtc::CritScope lock(&lock_); | |
214 play_sample_rate_ = fsHz; | 223 play_sample_rate_ = fsHz; |
215 return 0; | 224 return 0; |
216 } | 225 } |
217 | 226 |
218 int32_t AudioDeviceBuffer::RecordingSampleRate() const { | 227 int32_t AudioDeviceBuffer::RecordingSampleRate() const { |
228 rtc::CritScope lock(&lock_); | |
219 return rec_sample_rate_; | 229 return rec_sample_rate_; |
220 } | 230 } |
221 | 231 |
222 int32_t AudioDeviceBuffer::PlayoutSampleRate() const { | 232 int32_t AudioDeviceBuffer::PlayoutSampleRate() const { |
233 rtc::CritScope lock(&lock_); | |
223 return play_sample_rate_; | 234 return play_sample_rate_; |
224 } | 235 } |
225 | 236 |
226 int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) { | 237 int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) { |
227 LOG(INFO) << "SetRecordingChannels(" << channels << ")"; | 238 LOG(INFO) << "SetRecordingChannels(" << channels << ")"; |
239 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
228 rtc::CritScope lock(&lock_); | 240 rtc::CritScope lock(&lock_); |
229 rec_channels_ = channels; | 241 rec_channels_ = channels; |
230 rec_bytes_per_sample_ = sizeof(int16_t) * channels; | |
231 return 0; | 242 return 0; |
232 } | 243 } |
233 | 244 |
234 int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) { | 245 int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) { |
235 LOG(INFO) << "SetPlayoutChannels(" << channels << ")"; | 246 LOG(INFO) << "SetPlayoutChannels(" << channels << ")"; |
247 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
236 rtc::CritScope lock(&lock_); | 248 rtc::CritScope lock(&lock_); |
237 play_channels_ = channels; | 249 play_channels_ = channels; |
238 play_bytes_per_sample_ = sizeof(int16_t) * channels; | |
239 return 0; | 250 return 0; |
240 } | 251 } |
241 | 252 |
242 int32_t AudioDeviceBuffer::SetRecordingChannel( | 253 int32_t AudioDeviceBuffer::SetRecordingChannel( |
243 const AudioDeviceModule::ChannelType channel) { | 254 const AudioDeviceModule::ChannelType channel) { |
244 LOG(INFO) << "SetRecordingChannel(" << channel << ")"; | 255 LOG(INFO) << "SetRecordingChannel(" << channel << ")"; |
245 LOG(LS_WARNING) << "Not implemented"; | 256 LOG(LS_WARNING) << "Not implemented"; |
246 // Add DCHECK to ensure that user does not try to use this API with a non- | 257 // Add DCHECK to ensure that user does not try to use this API with a non- |
247 // default parameter. | 258 // default parameter. |
248 RTC_DCHECK_EQ(channel, AudioDeviceModule::kChannelBoth); | 259 RTC_DCHECK_EQ(channel, AudioDeviceModule::kChannelBoth); |
249 return -1; | 260 return -1; |
250 } | 261 } |
251 | 262 |
252 int32_t AudioDeviceBuffer::RecordingChannel( | 263 int32_t AudioDeviceBuffer::RecordingChannel( |
253 AudioDeviceModule::ChannelType& channel) const { | 264 AudioDeviceModule::ChannelType& channel) const { |
254 LOG(LS_WARNING) << "Not implemented"; | 265 LOG(LS_WARNING) << "Not implemented"; |
255 return -1; | 266 return -1; |
256 } | 267 } |
257 | 268 |
258 size_t AudioDeviceBuffer::RecordingChannels() const { | 269 size_t AudioDeviceBuffer::RecordingChannels() const { |
270 rtc::CritScope lock(&lock_); | |
259 return rec_channels_; | 271 return rec_channels_; |
260 } | 272 } |
261 | 273 |
262 size_t AudioDeviceBuffer::PlayoutChannels() const { | 274 size_t AudioDeviceBuffer::PlayoutChannels() const { |
275 rtc::CritScope lock(&lock_); | |
263 return play_channels_; | 276 return play_channels_; |
264 } | 277 } |
265 | 278 |
266 int32_t AudioDeviceBuffer::SetCurrentMicLevel(uint32_t level) { | 279 int32_t AudioDeviceBuffer::SetCurrentMicLevel(uint32_t level) { |
280 RTC_DCHECK_RUN_ON(&recording_thread_checker_); | |
267 current_mic_level_ = level; | 281 current_mic_level_ = level; |
268 return 0; | 282 return 0; |
269 } | 283 } |
270 | 284 |
271 int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) { | 285 int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) { |
286 RTC_DCHECK_RUN_ON(&recording_thread_checker_); | |
272 typing_status_ = typing_status; | 287 typing_status_ = typing_status; |
273 return 0; | 288 return 0; |
274 } | 289 } |
275 | 290 |
276 uint32_t AudioDeviceBuffer::NewMicLevel() const { | 291 uint32_t AudioDeviceBuffer::NewMicLevel() const { |
292 RTC_DCHECK_RUN_ON(&recording_thread_checker_); | |
277 return new_mic_level_; | 293 return new_mic_level_; |
278 } | 294 } |
279 | 295 |
280 void AudioDeviceBuffer::SetVQEData(int play_delay_ms, | 296 void AudioDeviceBuffer::SetVQEData(int play_delay_ms, |
281 int rec_delay_ms, | 297 int rec_delay_ms, |
282 int clock_drift) { | 298 int clock_drift) { |
299 RTC_DCHECK_RUN_ON(&recording_thread_checker_); | |
283 play_delay_ms_ = play_delay_ms; | 300 play_delay_ms_ = play_delay_ms; |
284 rec_delay_ms_ = rec_delay_ms; | 301 rec_delay_ms_ = rec_delay_ms; |
285 clock_drift_ = clock_drift; | 302 clock_drift_ = clock_drift; |
286 } | 303 } |
287 | 304 |
288 int32_t AudioDeviceBuffer::StartInputFileRecording( | 305 int32_t AudioDeviceBuffer::StartInputFileRecording( |
289 const char fileName[kAdmMaxFileNameSize]) { | 306 const char fileName[kAdmMaxFileNameSize]) { |
290 LOG(LS_WARNING) << "Not implemented"; | 307 LOG(LS_WARNING) << "Not implemented"; |
291 return 0; | 308 return 0; |
292 } | 309 } |
293 | 310 |
294 int32_t AudioDeviceBuffer::StopInputFileRecording() { | 311 int32_t AudioDeviceBuffer::StopInputFileRecording() { |
295 LOG(LS_WARNING) << "Not implemented"; | 312 LOG(LS_WARNING) << "Not implemented"; |
296 return 0; | 313 return 0; |
297 } | 314 } |
298 | 315 |
299 int32_t AudioDeviceBuffer::StartOutputFileRecording( | 316 int32_t AudioDeviceBuffer::StartOutputFileRecording( |
300 const char fileName[kAdmMaxFileNameSize]) { | 317 const char fileName[kAdmMaxFileNameSize]) { |
301 LOG(LS_WARNING) << "Not implemented"; | 318 LOG(LS_WARNING) << "Not implemented"; |
302 return 0; | 319 return 0; |
303 } | 320 } |
304 | 321 |
305 int32_t AudioDeviceBuffer::StopOutputFileRecording() { | 322 int32_t AudioDeviceBuffer::StopOutputFileRecording() { |
306 LOG(LS_WARNING) << "Not implemented"; | 323 LOG(LS_WARNING) << "Not implemented"; |
307 return 0; | 324 return 0; |
308 } | 325 } |
309 | 326 |
310 int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer, | 327 int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer, |
311 size_t num_samples) { | 328 size_t num_samples) { |
329 RTC_DCHECK_RUN_ON(&recording_thread_checker_); | |
312 const size_t rec_channels = [&] { | 330 const size_t rec_channels = [&] { |
313 rtc::CritScope lock(&lock_); | 331 rtc::CritScope lock(&lock_); |
314 return rec_channels_; | 332 return rec_channels_; |
315 }(); | 333 }(); |
316 // Copy the complete input buffer to the local buffer. | 334 // Copy the complete input buffer to the local buffer. |
317 const size_t size_in_bytes = num_samples * rec_channels * sizeof(int16_t); | 335 const size_t size_in_bytes = num_samples * rec_channels * sizeof(int16_t); |
318 const size_t old_size = rec_buffer_.size(); | 336 const size_t old_size = rec_buffer_.size(); |
319 rec_buffer_.SetData(static_cast<const uint8_t*>(audio_buffer), size_in_bytes); | 337 rec_buffer_.SetData(static_cast<const uint8_t*>(audio_buffer), size_in_bytes); |
320 // Keep track of the size of the recording buffer. Only updated when the | 338 // Keep track of the size of the recording buffer. Only updated when the |
321 // size changes, which is a rare event. | 339 // size changes, which is a rare event. |
(...skipping 19 matching lines...) Loading... | |
341 // Update some stats but do it on the task queue to ensure that the members | 359 // Update some stats but do it on the task queue to ensure that the members |
342 // are modified and read on the same thread. Note that |max_abs| will be | 360 // are modified and read on the same thread. Note that |max_abs| will be |
343 // zero in most calls and then have no effect of the stats. It is only updated | 361 // zero in most calls and then have no effect of the stats. It is only updated |
344 // approximately two times per second and can then change the stats. | 362 // approximately two times per second and can then change the stats. |
345 task_queue_.PostTask( | 363 task_queue_.PostTask( |
346 [this, max_abs, num_samples] { UpdateRecStats(max_abs, num_samples); }); | 364 [this, max_abs, num_samples] { UpdateRecStats(max_abs, num_samples); }); |
347 return 0; | 365 return 0; |
348 } | 366 } |
349 | 367 |
350 int32_t AudioDeviceBuffer::DeliverRecordedData() { | 368 int32_t AudioDeviceBuffer::DeliverRecordedData() { |
351 rtc::CritScope lock(&lock_cb_); | 369 RTC_DCHECK_RUN_ON(&recording_thread_checker_); |
352 if (!audio_transport_cb_) { | 370 if (!audio_transport_cb_) { |
353 LOG(LS_WARNING) << "Invalid audio transport"; | 371 LOG(LS_WARNING) << "Invalid audio transport"; |
354 return 0; | 372 return 0; |
355 } | 373 } |
356 const size_t rec_bytes_per_sample = [&] { | 374 size_t rec_channels = 0; |
375 size_t rec_sample_rate = 0; | |
kwiberg-webrtc
2016/11/02 14:14:57
Better to not initialize. That way, the reader can
henrika_webrtc
2016/11/02 16:23:24
Done.
| |
376 { | |
357 rtc::CritScope lock(&lock_); | 377 rtc::CritScope lock(&lock_); |
358 return rec_bytes_per_sample_; | 378 rec_channels = rec_channels_; |
359 }(); | 379 rec_sample_rate = rec_sample_rate_; |
380 } | |
381 const size_t rec_bytes_per_sample = rec_channels * sizeof(int16_t); | |
360 uint32_t new_mic_level(0); | 382 uint32_t new_mic_level(0); |
361 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_; | 383 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_; |
362 size_t num_samples = rec_buffer_.size() / rec_bytes_per_sample; | 384 size_t num_samples = rec_buffer_.size() / rec_bytes_per_sample; |
363 int32_t res = audio_transport_cb_->RecordedDataIsAvailable( | 385 int32_t res = audio_transport_cb_->RecordedDataIsAvailable( |
364 rec_buffer_.data(), num_samples, rec_bytes_per_sample_, rec_channels_, | 386 rec_buffer_.data(), num_samples, rec_bytes_per_sample, rec_channels, |
365 rec_sample_rate_, total_delay_ms, clock_drift_, current_mic_level_, | 387 rec_sample_rate, total_delay_ms, clock_drift_, current_mic_level_, |
366 typing_status_, new_mic_level); | 388 typing_status_, new_mic_level); |
367 if (res != -1) { | 389 if (res != -1) { |
368 new_mic_level_ = new_mic_level; | 390 new_mic_level_ = new_mic_level; |
369 } else { | 391 } else { |
370 LOG(LS_ERROR) << "RecordedDataIsAvailable() failed"; | 392 LOG(LS_ERROR) << "RecordedDataIsAvailable() failed"; |
371 } | 393 } |
372 return 0; | 394 return 0; |
373 } | 395 } |
374 | 396 |
375 int32_t AudioDeviceBuffer::RequestPlayoutData(size_t num_samples) { | 397 int32_t AudioDeviceBuffer::RequestPlayoutData(size_t num_samples) { |
398 RTC_DCHECK_RUN_ON(&playout_thread_checker_); | |
376 // Measure time since last function call and update an array where the | 399 // Measure time since last function call and update an array where the |
377 // position/index corresponds to time differences (in milliseconds) between | 400 // position/index corresponds to time differences (in milliseconds) between |
378 // two successive playout callbacks, and the stored value is the number of | 401 // two successive playout callbacks, and the stored value is the number of |
379 // times a given time difference was found. | 402 // times a given time difference was found. |
380 int64_t now_time = rtc::TimeMillis(); | 403 int64_t now_time = rtc::TimeMillis(); |
381 size_t diff_time = rtc::TimeDiff(now_time, last_playout_time_); | 404 size_t diff_time = rtc::TimeDiff(now_time, last_playout_time_); |
382 // Truncate at 500ms to limit the size of the array. | 405 // Truncate at 500ms to limit the size of the array. |
383 diff_time = std::min(kMaxDeltaTimeInMs, diff_time); | 406 diff_time = std::min(kMaxDeltaTimeInMs, diff_time); |
384 last_playout_time_ = now_time; | 407 last_playout_time_ = now_time; |
385 playout_diff_times_[diff_time]++; | 408 playout_diff_times_[diff_time]++; |
386 | 409 |
387 const size_t play_channels = [&] { | 410 size_t play_channels = 0; |
411 size_t play_sample_rate = 0; | |
kwiberg-webrtc
2016/11/02 14:14:57
Same here.
henrika_webrtc
2016/11/02 16:23:24
Done.
| |
412 { | |
388 rtc::CritScope lock(&lock_); | 413 rtc::CritScope lock(&lock_); |
389 return play_channels_; | 414 play_channels = play_channels_; |
390 }(); | 415 play_sample_rate = play_sample_rate_; |
416 } | |
391 | 417 |
392 // The consumer can change the request size on the fly and we therefore | 418 // The consumer can change the request size on the fly and we therefore |
393 // resize the buffer accordingly. Also takes place at the first call to this | 419 // resize the buffer accordingly. Also takes place at the first call to this |
394 // method. | 420 // method. |
395 const size_t play_bytes_per_sample = play_channels * sizeof(int16_t); | 421 const size_t play_bytes_per_sample = play_channels * sizeof(int16_t); |
396 const size_t size_in_bytes = num_samples * play_bytes_per_sample; | 422 const size_t size_in_bytes = num_samples * play_bytes_per_sample; |
397 if (play_buffer_.size() != size_in_bytes) { | 423 if (play_buffer_.size() != size_in_bytes) { |
398 play_buffer_.SetSize(size_in_bytes); | 424 play_buffer_.SetSize(size_in_bytes); |
399 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size(); | 425 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size(); |
400 } | 426 } |
401 | 427 |
402 size_t num_samples_out(0); | 428 size_t num_samples_out(0); |
403 { | 429 // It is currently supported to start playout without a valid audio |
404 rtc::CritScope lock(&lock_cb_); | 430 // transport object. Leads to warning and silence. |
431 if (!audio_transport_cb_) { | |
432 LOG(LS_WARNING) << "Invalid audio transport"; | |
433 return 0; | |
434 } | |
405 | 435 |
406 // It is currently supported to start playout without a valid audio | 436 // Retrieve new 16-bit PCM audio data using the audio transport instance. |
407 // transport object. Leads to warning and silence. | 437 int64_t elapsed_time_ms = -1; |
408 if (!audio_transport_cb_) { | 438 int64_t ntp_time_ms = -1; |
409 LOG(LS_WARNING) << "Invalid audio transport"; | 439 uint32_t res = audio_transport_cb_->NeedMorePlayData( |
410 return 0; | 440 num_samples, play_bytes_per_sample, play_channels, play_sample_rate, |
411 } | 441 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms); |
412 | 442 if (res != 0) { |
413 // Retrieve new 16-bit PCM audio data using the audio transport instance. | 443 LOG(LS_ERROR) << "NeedMorePlayData() failed"; |
414 int64_t elapsed_time_ms = -1; | |
415 int64_t ntp_time_ms = -1; | |
416 uint32_t res = audio_transport_cb_->NeedMorePlayData( | |
417 num_samples, play_bytes_per_sample_, play_channels, play_sample_rate_, | |
418 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms); | |
419 if (res != 0) { | |
420 LOG(LS_ERROR) << "NeedMorePlayData() failed"; | |
421 } | |
422 } | 444 } |
423 | 445 |
424 // Derive a new level value twice per second. | 446 // Derive a new level value twice per second. |
425 int16_t max_abs = 0; | 447 int16_t max_abs = 0; |
426 RTC_DCHECK_LT(play_stat_count_, 50); | 448 RTC_DCHECK_LT(play_stat_count_, 50); |
427 if (++play_stat_count_ >= 50) { | 449 if (++play_stat_count_ >= 50) { |
428 const size_t size = num_samples * play_channels; | 450 const size_t size = num_samples * play_channels; |
429 // Returns the largest absolute value in a signed 16-bit vector. | 451 // Returns the largest absolute value in a signed 16-bit vector. |
430 max_abs = WebRtcSpl_MaxAbsValueW16( | 452 max_abs = WebRtcSpl_MaxAbsValueW16( |
431 reinterpret_cast<const int16_t*>(play_buffer_.data()), size); | 453 reinterpret_cast<const int16_t*>(play_buffer_.data()), size); |
432 play_stat_count_ = 0; | 454 play_stat_count_ = 0; |
433 } | 455 } |
434 // Update some stats but do it on the task queue to ensure that the members | 456 // Update some stats but do it on the task queue to ensure that the members |
435 // are modified and read on the same thread. Note that |max_abs| will be | 457 // are modified and read on the same thread. Note that |max_abs| will be |
436 // zero in most calls and then have no effect of the stats. It is only updated | 458 // zero in most calls and then have no effect of the stats. It is only updated |
437 // approximately two times per second and can then change the stats. | 459 // approximately two times per second and can then change the stats. |
438 task_queue_.PostTask([this, max_abs, num_samples_out] { | 460 task_queue_.PostTask([this, max_abs, num_samples_out] { |
439 UpdatePlayStats(max_abs, num_samples_out); | 461 UpdatePlayStats(max_abs, num_samples_out); |
440 }); | 462 }); |
441 return static_cast<int32_t>(num_samples_out); | 463 return static_cast<int32_t>(num_samples_out); |
442 } | 464 } |
443 | 465 |
444 int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) { | 466 int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) { |
467 RTC_DCHECK_RUN_ON(&playout_thread_checker_); | |
445 RTC_DCHECK_GT(play_buffer_.size(), 0u); | 468 RTC_DCHECK_GT(play_buffer_.size(), 0u); |
446 const size_t play_bytes_per_sample = [&] { | 469 const size_t play_channels = [&] { |
447 rtc::CritScope lock(&lock_); | 470 rtc::CritScope lock(&lock_); |
448 return play_bytes_per_sample_; | 471 return play_channels_; |
449 }(); | 472 }(); |
473 const size_t play_bytes_per_sample = play_channels * sizeof(int16_t); | |
450 memcpy(audio_buffer, play_buffer_.data(), play_buffer_.size()); | 474 memcpy(audio_buffer, play_buffer_.data(), play_buffer_.size()); |
451 return static_cast<int32_t>(play_buffer_.size() / play_bytes_per_sample); | 475 return static_cast<int32_t>(play_buffer_.size() / play_bytes_per_sample); |
452 } | 476 } |
453 | 477 |
454 void AudioDeviceBuffer::StartPeriodicLogging() { | 478 void AudioDeviceBuffer::StartPeriodicLogging() { |
455 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, | 479 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, |
456 AudioDeviceBuffer::LOG_START)); | 480 AudioDeviceBuffer::LOG_START)); |
457 } | 481 } |
458 | 482 |
459 void AudioDeviceBuffer::StopPeriodicLogging() { | 483 void AudioDeviceBuffer::StopPeriodicLogging() { |
460 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, | 484 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, |
461 AudioDeviceBuffer::LOG_STOP)); | 485 AudioDeviceBuffer::LOG_STOP)); |
462 } | 486 } |
463 | 487 |
464 void AudioDeviceBuffer::LogStats(LogState state) { | 488 void AudioDeviceBuffer::LogStats(LogState state) { |
465 RTC_DCHECK(task_queue_.IsCurrent()); | 489 RTC_DCHECK_RUN_ON(&task_queue_); |
466 int64_t now_time = rtc::TimeMillis(); | 490 int64_t now_time = rtc::TimeMillis(); |
467 if (state == AudioDeviceBuffer::LOG_START) { | 491 if (state == AudioDeviceBuffer::LOG_START) { |
468 // Reset counters at start. We will not add any logging in this state but | 492 // Reset counters at start. We will not add any logging in this state but |
469 // the timer will started by posting a new (delayed) task. | 493 // the timer will started by posting a new (delayed) task. |
470 num_stat_reports_ = 0; | 494 num_stat_reports_ = 0; |
471 last_timer_task_time_ = now_time; | 495 last_timer_task_time_ = now_time; |
472 } else if (state == AudioDeviceBuffer::LOG_STOP) { | 496 } else if (state == AudioDeviceBuffer::LOG_STOP) { |
473 // Stop logging and posting new tasks. | 497 // Stop logging and posting new tasks. |
474 return; | 498 return; |
475 } else if (state == AudioDeviceBuffer::LOG_ACTIVE) { | 499 } else if (state == AudioDeviceBuffer::LOG_ACTIVE) { |
476 // Default state. Just keep on logging. | 500 // Default state. Just keep on logging. |
477 } | 501 } |
478 | 502 |
479 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds; | 503 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds; |
480 int64_t time_since_last = rtc::TimeDiff(now_time, last_timer_task_time_); | 504 int64_t time_since_last = rtc::TimeDiff(now_time, last_timer_task_time_); |
481 last_timer_task_time_ = now_time; | 505 last_timer_task_time_ = now_time; |
482 | 506 |
507 size_t play_sample_rate = 0; | |
508 size_t rec_sample_rate = 0; | |
kwiberg-webrtc
2016/11/02 14:14:57
Same here.
henrika_webrtc
2016/11/02 16:23:24
Done.
| |
509 { | |
510 rtc::CritScope lock(&lock_); | |
511 play_sample_rate = play_sample_rate_; | |
512 rec_sample_rate = rec_sample_rate_; | |
513 }; | |
514 | |
483 // Log the latest statistics but skip the first round just after state was | 515 // Log the latest statistics but skip the first round just after state was |
484 // set to LOG_START. Hence, first printed log will be after ~10 seconds. | 516 // set to LOG_START. Hence, first printed log will be after ~10 seconds. |
485 if (++num_stat_reports_ > 1 && time_since_last > 0) { | 517 if (++num_stat_reports_ > 1 && time_since_last > 0) { |
486 uint32_t diff_samples = rec_samples_ - last_rec_samples_; | 518 uint32_t diff_samples = rec_samples_ - last_rec_samples_; |
487 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); | 519 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); |
488 LOG(INFO) << "[REC : " << time_since_last << "msec, " | 520 LOG(INFO) << "[REC : " << time_since_last << "msec, " |
489 << rec_sample_rate_ / 1000 | 521 << rec_sample_rate / 1000 |
490 << "kHz] callbacks: " << rec_callbacks_ - last_rec_callbacks_ | 522 << "kHz] callbacks: " << rec_callbacks_ - last_rec_callbacks_ |
491 << ", " | 523 << ", " |
492 << "samples: " << diff_samples << ", " | 524 << "samples: " << diff_samples << ", " |
493 << "rate: " << static_cast<int>(rate + 0.5) << ", " | 525 << "rate: " << static_cast<int>(rate + 0.5) << ", " |
494 << "level: " << max_rec_level_; | 526 << "level: " << max_rec_level_; |
495 | 527 |
496 diff_samples = play_samples_ - last_play_samples_; | 528 diff_samples = play_samples_ - last_play_samples_; |
497 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); | 529 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); |
498 LOG(INFO) << "[PLAY: " << time_since_last << "msec, " | 530 LOG(INFO) << "[PLAY: " << time_since_last << "msec, " |
499 << play_sample_rate_ / 1000 | 531 << play_sample_rate / 1000 |
500 << "kHz] callbacks: " << play_callbacks_ - last_play_callbacks_ | 532 << "kHz] callbacks: " << play_callbacks_ - last_play_callbacks_ |
501 << ", " | 533 << ", " |
502 << "samples: " << diff_samples << ", " | 534 << "samples: " << diff_samples << ", " |
503 << "rate: " << static_cast<int>(rate + 0.5) << ", " | 535 << "rate: " << static_cast<int>(rate + 0.5) << ", " |
504 << "level: " << max_play_level_; | 536 << "level: " << max_play_level_; |
505 } | 537 } |
506 | 538 |
507 last_rec_callbacks_ = rec_callbacks_; | 539 last_rec_callbacks_ = rec_callbacks_; |
508 last_play_callbacks_ = play_callbacks_; | 540 last_play_callbacks_ = play_callbacks_; |
509 last_rec_samples_ = rec_samples_; | 541 last_rec_samples_ = rec_samples_; |
510 last_play_samples_ = play_samples_; | 542 last_play_samples_ = play_samples_; |
511 max_rec_level_ = 0; | 543 max_rec_level_ = 0; |
512 max_play_level_ = 0; | 544 max_play_level_ = 0; |
513 | 545 |
514 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis(); | 546 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis(); |
515 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval"; | 547 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval"; |
516 | 548 |
517 // Keep posting new (delayed) tasks until state is changed to kLogStop. | 549 // Keep posting new (delayed) tasks until state is changed to kLogStop. |
518 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, | 550 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, |
519 AudioDeviceBuffer::LOG_ACTIVE), | 551 AudioDeviceBuffer::LOG_ACTIVE), |
520 time_to_wait_ms); | 552 time_to_wait_ms); |
521 } | 553 } |
522 | 554 |
523 void AudioDeviceBuffer::ResetRecStats() { | 555 void AudioDeviceBuffer::ResetRecStats() { |
524 RTC_DCHECK(task_queue_.IsCurrent()); | 556 RTC_DCHECK_RUN_ON(&task_queue_); |
525 rec_callbacks_ = 0; | 557 rec_callbacks_ = 0; |
526 last_rec_callbacks_ = 0; | 558 last_rec_callbacks_ = 0; |
527 rec_samples_ = 0; | 559 rec_samples_ = 0; |
528 last_rec_samples_ = 0; | 560 last_rec_samples_ = 0; |
529 max_rec_level_ = 0; | 561 max_rec_level_ = 0; |
530 } | 562 } |
531 | 563 |
532 void AudioDeviceBuffer::ResetPlayStats() { | 564 void AudioDeviceBuffer::ResetPlayStats() { |
533 RTC_DCHECK(task_queue_.IsCurrent()); | 565 RTC_DCHECK_RUN_ON(&task_queue_); |
534 play_callbacks_ = 0; | 566 play_callbacks_ = 0; |
535 last_play_callbacks_ = 0; | 567 last_play_callbacks_ = 0; |
536 play_samples_ = 0; | 568 play_samples_ = 0; |
537 last_play_samples_ = 0; | 569 last_play_samples_ = 0; |
538 max_play_level_ = 0; | 570 max_play_level_ = 0; |
539 } | 571 } |
540 | 572 |
541 void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs, size_t num_samples) { | 573 void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs, size_t num_samples) { |
542 RTC_DCHECK(task_queue_.IsCurrent()); | 574 RTC_DCHECK_RUN_ON(&task_queue_); |
543 ++rec_callbacks_; | 575 ++rec_callbacks_; |
544 rec_samples_ += num_samples; | 576 rec_samples_ += num_samples; |
545 if (max_abs > max_rec_level_) { | 577 if (max_abs > max_rec_level_) { |
546 max_rec_level_ = max_abs; | 578 max_rec_level_ = max_abs; |
547 } | 579 } |
548 } | 580 } |
549 | 581 |
550 void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs, size_t num_samples) { | 582 void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs, size_t num_samples) { |
551 RTC_DCHECK(task_queue_.IsCurrent()); | 583 RTC_DCHECK_RUN_ON(&task_queue_); |
552 ++play_callbacks_; | 584 ++play_callbacks_; |
553 play_samples_ += num_samples; | 585 play_samples_ += num_samples; |
554 if (max_abs > max_play_level_) { | 586 if (max_abs > max_play_level_) { |
555 max_play_level_ = max_abs; | 587 max_play_level_ = max_abs; |
556 } | 588 } |
557 } | 589 } |
558 | 590 |
559 } // namespace webrtc | 591 } // namespace webrtc |
OLD | NEW |