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