blob: d3b7917a5e4b7680a917a53bb1dadc2e8eb64147 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
xians@webrtc.org20aabbb2012-02-20 09:17:41 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
henrika3d7346f2016-07-29 16:20:47 +020011#include <algorithm>
12
pbos@webrtc.org811269d2013-07-11 13:24:38 +000013#include "webrtc/modules/audio_device/audio_device_buffer.h"
andrew@webrtc.org25534502013-09-13 00:02:13 +000014
henrika3d7346f2016-07-29 16:20:47 +020015#include "webrtc/base/arraysize.h"
henrika6c4d0f02016-07-14 05:54:19 -070016#include "webrtc/base/bind.h"
henrika3f33e2a2016-07-06 00:33:57 -070017#include "webrtc/base/checks.h"
18#include "webrtc/base/logging.h"
Peter Kastingdce40cf2015-08-24 14:52:23 -070019#include "webrtc/base/format_macros.h"
henrika6c4d0f02016-07-14 05:54:19 -070020#include "webrtc/base/timeutils.h"
henrikaf06f35a2016-09-09 14:23:11 +020021#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
pbos@webrtc.org811269d2013-07-11 13:24:38 +000022#include "webrtc/modules/audio_device/audio_device_config.h"
henrikaf06f35a2016-09-09 14:23:11 +020023#include "webrtc/system_wrappers/include/metrics.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000024
niklase@google.com470e71d2011-07-07 08:21:25 +000025namespace webrtc {
26
henrika6c4d0f02016-07-14 05:54:19 -070027static const char kTimerQueueName[] = "AudioDeviceBufferTimer";
28
29// Time between two sucessive calls to LogStats().
30static const size_t kTimerIntervalInSeconds = 10;
31static const size_t kTimerIntervalInMilliseconds =
32 kTimerIntervalInSeconds * rtc::kNumMillisecsPerSec;
33
henrika0fd68012016-07-04 13:01:19 +020034AudioDeviceBuffer::AudioDeviceBuffer()
henrika49810512016-08-22 05:56:12 -070035 : audio_transport_cb_(nullptr),
henrika6c4d0f02016-07-14 05:54:19 -070036 task_queue_(kTimerQueueName),
37 timer_has_started_(false),
henrika49810512016-08-22 05:56:12 -070038 rec_sample_rate_(0),
39 play_sample_rate_(0),
40 rec_channels_(0),
41 play_channels_(0),
henrika49810512016-08-22 05:56:12 -070042 rec_bytes_per_sample_(0),
43 play_bytes_per_sample_(0),
henrika49810512016-08-22 05:56:12 -070044 current_mic_level_(0),
45 new_mic_level_(0),
46 typing_status_(false),
47 play_delay_ms_(0),
48 rec_delay_ms_(0),
49 clock_drift_(0),
henrika6c4d0f02016-07-14 05:54:19 -070050 num_stat_reports_(0),
51 rec_callbacks_(0),
52 last_rec_callbacks_(0),
53 play_callbacks_(0),
54 last_play_callbacks_(0),
55 rec_samples_(0),
56 last_rec_samples_(0),
57 play_samples_(0),
58 last_play_samples_(0),
henrikaf06f35a2016-09-09 14:23:11 +020059 last_log_stat_time_(0),
60 max_rec_level_(0),
61 max_play_level_(0),
62 num_rec_level_is_zero_(0) {
henrika3f33e2a2016-07-06 00:33:57 -070063 LOG(INFO) << "AudioDeviceBuffer::ctor";
niklase@google.com470e71d2011-07-07 08:21:25 +000064}
65
henrika0fd68012016-07-04 13:01:19 +020066AudioDeviceBuffer::~AudioDeviceBuffer() {
henrika6c4d0f02016-07-14 05:54:19 -070067 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrika3f33e2a2016-07-06 00:33:57 -070068 LOG(INFO) << "AudioDeviceBuffer::~dtor";
henrika3d7346f2016-07-29 16:20:47 +020069
70 size_t total_diff_time = 0;
71 int num_measurements = 0;
72 LOG(INFO) << "[playout diff time => #measurements]";
73 for (size_t diff = 0; diff < arraysize(playout_diff_times_); ++diff) {
74 uint32_t num_elements = playout_diff_times_[diff];
75 if (num_elements > 0) {
76 total_diff_time += num_elements * diff;
77 num_measurements += num_elements;
78 LOG(INFO) << "[" << diff << " => " << num_elements << "]";
79 }
80 }
81 if (num_measurements > 0) {
82 LOG(INFO) << "total_diff_time: " << total_diff_time;
83 LOG(INFO) << "num_measurements: " << num_measurements;
84 LOG(INFO) << "average: "
85 << static_cast<float>(total_diff_time) / num_measurements;
86 }
henrikaf06f35a2016-09-09 14:23:11 +020087
88 // Add UMA histogram to keep track of the case when only zeros have been
89 // recorded. Ensure that recording callbacks have started and that at least
90 // one timer event has been able to update |num_rec_level_is_zero_|.
91 // I am avoiding use of the task queue here since we are under destruction
92 // and reading these members on the creating thread feels safe.
93 if (rec_callbacks_ > 0 && num_stat_reports_ > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -070094 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros",
henrikaf06f35a2016-09-09 14:23:11 +020095 static_cast<int>(num_stat_reports_ == num_rec_level_is_zero_));
96 }
niklase@google.com470e71d2011-07-07 08:21:25 +000097}
98
henrika0fd68012016-07-04 13:01:19 +020099int32_t AudioDeviceBuffer::RegisterAudioCallback(
henrika49810512016-08-22 05:56:12 -0700100 AudioTransport* audio_callback) {
henrika3f33e2a2016-07-06 00:33:57 -0700101 LOG(INFO) << __FUNCTION__;
henrika5588a132016-10-18 05:14:30 -0700102 rtc::CritScope lock(&lock_cb_);
henrika49810512016-08-22 05:56:12 -0700103 audio_transport_cb_ = audio_callback;
henrika0fd68012016-07-04 13:01:19 +0200104 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000105}
106
henrika0fd68012016-07-04 13:01:19 +0200107int32_t AudioDeviceBuffer::InitPlayout() {
henrikad7a89db2016-08-19 08:09:25 -0700108 LOG(INFO) << __FUNCTION__;
henrika49810512016-08-22 05:56:12 -0700109 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrikaf06f35a2016-09-09 14:23:11 +0200110 ResetPlayStats();
henrika6c4d0f02016-07-14 05:54:19 -0700111 if (!timer_has_started_) {
112 StartTimer();
113 timer_has_started_ = true;
114 }
henrika0fd68012016-07-04 13:01:19 +0200115 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000116}
117
henrika0fd68012016-07-04 13:01:19 +0200118int32_t AudioDeviceBuffer::InitRecording() {
henrikad7a89db2016-08-19 08:09:25 -0700119 LOG(INFO) << __FUNCTION__;
henrika49810512016-08-22 05:56:12 -0700120 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrikaf06f35a2016-09-09 14:23:11 +0200121 ResetRecStats();
henrika6c4d0f02016-07-14 05:54:19 -0700122 if (!timer_has_started_) {
123 StartTimer();
124 timer_has_started_ = true;
125 }
henrika0fd68012016-07-04 13:01:19 +0200126 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000127}
128
henrika0fd68012016-07-04 13:01:19 +0200129int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) {
henrika3f33e2a2016-07-06 00:33:57 -0700130 LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")";
henrika5588a132016-10-18 05:14:30 -0700131 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700132 rec_sample_rate_ = fsHz;
henrika0fd68012016-07-04 13:01:19 +0200133 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000134}
135
henrika0fd68012016-07-04 13:01:19 +0200136int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) {
henrika3f33e2a2016-07-06 00:33:57 -0700137 LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")";
henrika5588a132016-10-18 05:14:30 -0700138 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700139 play_sample_rate_ = fsHz;
henrika0fd68012016-07-04 13:01:19 +0200140 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000141}
142
henrika0fd68012016-07-04 13:01:19 +0200143int32_t AudioDeviceBuffer::RecordingSampleRate() const {
henrika49810512016-08-22 05:56:12 -0700144 return rec_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145}
146
henrika0fd68012016-07-04 13:01:19 +0200147int32_t AudioDeviceBuffer::PlayoutSampleRate() const {
henrika49810512016-08-22 05:56:12 -0700148 return play_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000149}
150
henrika0fd68012016-07-04 13:01:19 +0200151int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) {
henrika49810512016-08-22 05:56:12 -0700152 LOG(INFO) << "SetRecordingChannels(" << channels << ")";
henrika5588a132016-10-18 05:14:30 -0700153 rtc::CritScope lock(&lock_);
henrika49810512016-08-22 05:56:12 -0700154 rec_channels_ = channels;
henrika5588a132016-10-18 05:14:30 -0700155 rec_bytes_per_sample_ = sizeof(int16_t) * channels;
henrika0fd68012016-07-04 13:01:19 +0200156 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000157}
158
henrika0fd68012016-07-04 13:01:19 +0200159int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) {
henrika49810512016-08-22 05:56:12 -0700160 LOG(INFO) << "SetPlayoutChannels(" << channels << ")";
henrika5588a132016-10-18 05:14:30 -0700161 rtc::CritScope lock(&lock_);
henrika49810512016-08-22 05:56:12 -0700162 play_channels_ = channels;
henrika5588a132016-10-18 05:14:30 -0700163 play_bytes_per_sample_ = sizeof(int16_t) * channels;
henrika0fd68012016-07-04 13:01:19 +0200164 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000165}
166
henrika0fd68012016-07-04 13:01:19 +0200167int32_t AudioDeviceBuffer::SetRecordingChannel(
168 const AudioDeviceModule::ChannelType channel) {
henrika5588a132016-10-18 05:14:30 -0700169 LOG(INFO) << "SetRecordingChannel(" << channel << ")";
170 LOG(LS_WARNING) << "Not implemented";
171 // Add DCHECK to ensure that user does not try to use this API with a non-
172 // default parameter.
173 RTC_DCHECK_EQ(channel, AudioDeviceModule::kChannelBoth);
174 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000175}
176
henrika0fd68012016-07-04 13:01:19 +0200177int32_t AudioDeviceBuffer::RecordingChannel(
178 AudioDeviceModule::ChannelType& channel) const {
henrika5588a132016-10-18 05:14:30 -0700179 LOG(LS_WARNING) << "Not implemented";
180 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000181}
182
henrika0fd68012016-07-04 13:01:19 +0200183size_t AudioDeviceBuffer::RecordingChannels() const {
henrika49810512016-08-22 05:56:12 -0700184 return rec_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000185}
186
henrika0fd68012016-07-04 13:01:19 +0200187size_t AudioDeviceBuffer::PlayoutChannels() const {
henrika49810512016-08-22 05:56:12 -0700188 return play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000189}
190
henrika0fd68012016-07-04 13:01:19 +0200191int32_t AudioDeviceBuffer::SetCurrentMicLevel(uint32_t level) {
henrika49810512016-08-22 05:56:12 -0700192 current_mic_level_ = level;
henrika0fd68012016-07-04 13:01:19 +0200193 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000194}
195
henrika49810512016-08-22 05:56:12 -0700196int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) {
197 typing_status_ = typing_status;
henrika0fd68012016-07-04 13:01:19 +0200198 return 0;
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +0000199}
200
henrika0fd68012016-07-04 13:01:19 +0200201uint32_t AudioDeviceBuffer::NewMicLevel() const {
henrika49810512016-08-22 05:56:12 -0700202 return new_mic_level_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000203}
204
henrika49810512016-08-22 05:56:12 -0700205void AudioDeviceBuffer::SetVQEData(int play_delay_ms,
206 int rec_delay_ms,
207 int clock_drift) {
208 play_delay_ms_ = play_delay_ms;
209 rec_delay_ms_ = rec_delay_ms;
210 clock_drift_ = clock_drift;
niklase@google.com470e71d2011-07-07 08:21:25 +0000211}
212
pbos@webrtc.org25509882013-04-09 10:30:35 +0000213int32_t AudioDeviceBuffer::StartInputFileRecording(
henrika0fd68012016-07-04 13:01:19 +0200214 const char fileName[kAdmMaxFileNameSize]) {
henrika49810512016-08-22 05:56:12 -0700215 LOG(LS_WARNING) << "Not implemented";
216 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000217}
218
henrika0fd68012016-07-04 13:01:19 +0200219int32_t AudioDeviceBuffer::StopInputFileRecording() {
henrika49810512016-08-22 05:56:12 -0700220 LOG(LS_WARNING) << "Not implemented";
henrika0fd68012016-07-04 13:01:19 +0200221 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000222}
223
pbos@webrtc.org25509882013-04-09 10:30:35 +0000224int32_t AudioDeviceBuffer::StartOutputFileRecording(
henrika0fd68012016-07-04 13:01:19 +0200225 const char fileName[kAdmMaxFileNameSize]) {
henrika49810512016-08-22 05:56:12 -0700226 LOG(LS_WARNING) << "Not implemented";
henrikacf327b42016-08-19 16:37:53 +0200227 return 0;
228}
229
henrika49810512016-08-22 05:56:12 -0700230int32_t AudioDeviceBuffer::StopOutputFileRecording() {
231 LOG(LS_WARNING) << "Not implemented";
232 return 0;
233}
234
235int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer,
236 size_t num_samples) {
henrika5588a132016-10-18 05:14:30 -0700237 const size_t rec_bytes_per_sample = [&] {
238 rtc::CritScope lock(&lock_);
239 return rec_bytes_per_sample_;
240 }();
241 // Copy the complete input buffer to the local buffer.
242 const size_t size_in_bytes = num_samples * rec_bytes_per_sample;
243 const size_t old_size = rec_buffer_.size();
244 rec_buffer_.SetData(static_cast<const uint8_t*>(audio_buffer), size_in_bytes);
245 // Keep track of the size of the recording buffer. Only updated when the
246 // size changes, which is a rare event.
247 if (old_size != rec_buffer_.size()) {
248 LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size();
henrika0fd68012016-07-04 13:01:19 +0200249 }
henrika6c4d0f02016-07-14 05:54:19 -0700250 // Update some stats but do it on the task queue to ensure that the members
251 // are modified and read on the same thread.
henrikaf06f35a2016-09-09 14:23:11 +0200252 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::UpdateRecStats, this,
253 audio_buffer, num_samples));
henrika0fd68012016-07-04 13:01:19 +0200254 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000255}
256
henrika0fd68012016-07-04 13:01:19 +0200257int32_t AudioDeviceBuffer::DeliverRecordedData() {
henrika5588a132016-10-18 05:14:30 -0700258 rtc::CritScope lock(&lock_cb_);
henrika49810512016-08-22 05:56:12 -0700259 if (!audio_transport_cb_) {
henrika3f33e2a2016-07-06 00:33:57 -0700260 LOG(LS_WARNING) << "Invalid audio transport";
niklase@google.com470e71d2011-07-07 08:21:25 +0000261 return 0;
henrika0fd68012016-07-04 13:01:19 +0200262 }
henrika5588a132016-10-18 05:14:30 -0700263 const size_t rec_bytes_per_sample = [&] {
264 rtc::CritScope lock(&lock_);
265 return rec_bytes_per_sample_;
266 }();
267 uint32_t new_mic_level(0);
268 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_;
269 size_t num_samples = rec_buffer_.size() / rec_bytes_per_sample;
270 int32_t res = audio_transport_cb_->RecordedDataIsAvailable(
271 rec_buffer_.data(), num_samples, rec_bytes_per_sample_, rec_channels_,
272 rec_sample_rate_, total_delay_ms, clock_drift_, current_mic_level_,
273 typing_status_, new_mic_level);
henrika0fd68012016-07-04 13:01:19 +0200274 if (res != -1) {
henrika5588a132016-10-18 05:14:30 -0700275 new_mic_level_ = new_mic_level;
henrika49810512016-08-22 05:56:12 -0700276 } else {
277 LOG(LS_ERROR) << "RecordedDataIsAvailable() failed";
henrika0fd68012016-07-04 13:01:19 +0200278 }
henrika0fd68012016-07-04 13:01:19 +0200279 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000280}
281
henrika49810512016-08-22 05:56:12 -0700282int32_t AudioDeviceBuffer::RequestPlayoutData(size_t num_samples) {
henrika3d7346f2016-07-29 16:20:47 +0200283 // Measure time since last function call and update an array where the
284 // position/index corresponds to time differences (in milliseconds) between
285 // two successive playout callbacks, and the stored value is the number of
286 // times a given time difference was found.
287 int64_t now_time = rtc::TimeMillis();
288 size_t diff_time = rtc::TimeDiff(now_time, last_playout_time_);
289 // Truncate at 500ms to limit the size of the array.
290 diff_time = std::min(kMaxDeltaTimeInMs, diff_time);
291 last_playout_time_ = now_time;
292 playout_diff_times_[diff_time]++;
293
henrika5588a132016-10-18 05:14:30 -0700294 const size_t play_bytes_per_sample = [&] {
295 rtc::CritScope lock(&lock_);
296 return play_bytes_per_sample_;
297 }();
henrika0fd68012016-07-04 13:01:19 +0200298
henrika5588a132016-10-18 05:14:30 -0700299 // The consumer can change the request size on the fly and we therefore
300 // resize the buffer accordingly. Also takes place at the first call to this
301 // method.
302 const size_t size_in_bytes = num_samples * play_bytes_per_sample;
303 if (play_buffer_.size() != size_in_bytes) {
304 play_buffer_.SetSize(size_in_bytes);
305 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size();
306 }
307
308 rtc::CritScope lock(&lock_cb_);
henrika0fd68012016-07-04 13:01:19 +0200309
henrika3f33e2a2016-07-06 00:33:57 -0700310 // It is currently supported to start playout without a valid audio
311 // transport object. Leads to warning and silence.
henrika49810512016-08-22 05:56:12 -0700312 if (!audio_transport_cb_) {
henrika3f33e2a2016-07-06 00:33:57 -0700313 LOG(LS_WARNING) << "Invalid audio transport";
henrika0fd68012016-07-04 13:01:19 +0200314 return 0;
315 }
316
henrika3f33e2a2016-07-06 00:33:57 -0700317 int64_t elapsed_time_ms = -1;
318 int64_t ntp_time_ms = -1;
henrika49810512016-08-22 05:56:12 -0700319 size_t num_samples_out(0);
henrika5588a132016-10-18 05:14:30 -0700320 uint32_t res = audio_transport_cb_->NeedMorePlayData(
321 num_samples, play_bytes_per_sample_, play_channels_, play_sample_rate_,
322 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms);
henrika3f33e2a2016-07-06 00:33:57 -0700323 if (res != 0) {
324 LOG(LS_ERROR) << "NeedMorePlayData() failed";
henrika0fd68012016-07-04 13:01:19 +0200325 }
326
henrika6c4d0f02016-07-14 05:54:19 -0700327 // Update some stats but do it on the task queue to ensure that access of
328 // members is serialized hence avoiding usage of locks.
henrikaf06f35a2016-09-09 14:23:11 +0200329 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::UpdatePlayStats, this,
henrika5588a132016-10-18 05:14:30 -0700330 play_buffer_.data(), num_samples_out));
henrika49810512016-08-22 05:56:12 -0700331 return static_cast<int32_t>(num_samples_out);
niklase@google.com470e71d2011-07-07 08:21:25 +0000332}
333
henrika49810512016-08-22 05:56:12 -0700334int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) {
henrika5588a132016-10-18 05:14:30 -0700335 RTC_DCHECK_GT(play_buffer_.size(), 0u);
336 const size_t play_bytes_per_sample = [&] {
337 rtc::CritScope lock(&lock_);
338 return play_bytes_per_sample_;
339 }();
340 memcpy(audio_buffer, play_buffer_.data(), play_buffer_.size());
341 return static_cast<int32_t>(play_buffer_.size() / play_bytes_per_sample);
niklase@google.com470e71d2011-07-07 08:21:25 +0000342}
343
henrika6c4d0f02016-07-14 05:54:19 -0700344void AudioDeviceBuffer::StartTimer() {
henrikaf06f35a2016-09-09 14:23:11 +0200345 num_stat_reports_ = 0;
henrika6c4d0f02016-07-14 05:54:19 -0700346 last_log_stat_time_ = rtc::TimeMillis();
347 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this),
348 kTimerIntervalInMilliseconds);
349}
350
351void AudioDeviceBuffer::LogStats() {
352 RTC_DCHECK(task_queue_.IsCurrent());
353
354 int64_t now_time = rtc::TimeMillis();
355 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds;
356 int64_t time_since_last = rtc::TimeDiff(now_time, last_log_stat_time_);
357 last_log_stat_time_ = now_time;
358
359 // Log the latest statistics but skip the first 10 seconds since we are not
360 // sure of the exact starting point. I.e., the first log printout will be
361 // after ~20 seconds.
henrikaa6d26ec2016-09-20 04:44:04 -0700362 if (++num_stat_reports_ > 1 && time_since_last > 0) {
henrika6c4d0f02016-07-14 05:54:19 -0700363 uint32_t diff_samples = rec_samples_ - last_rec_samples_;
henrikaa6d26ec2016-09-20 04:44:04 -0700364 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
henrika6c4d0f02016-07-14 05:54:19 -0700365 LOG(INFO) << "[REC : " << time_since_last << "msec, "
henrika49810512016-08-22 05:56:12 -0700366 << rec_sample_rate_ / 1000
henrika6c4d0f02016-07-14 05:54:19 -0700367 << "kHz] callbacks: " << rec_callbacks_ - last_rec_callbacks_
368 << ", "
369 << "samples: " << diff_samples << ", "
henrikaa6d26ec2016-09-20 04:44:04 -0700370 << "rate: " << static_cast<int>(rate + 0.5) << ", "
henrikaf06f35a2016-09-09 14:23:11 +0200371 << "level: " << max_rec_level_;
henrika6c4d0f02016-07-14 05:54:19 -0700372
373 diff_samples = play_samples_ - last_play_samples_;
henrikaa6d26ec2016-09-20 04:44:04 -0700374 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
henrika6c4d0f02016-07-14 05:54:19 -0700375 LOG(INFO) << "[PLAY: " << time_since_last << "msec, "
henrika49810512016-08-22 05:56:12 -0700376 << play_sample_rate_ / 1000
henrika6c4d0f02016-07-14 05:54:19 -0700377 << "kHz] callbacks: " << play_callbacks_ - last_play_callbacks_
378 << ", "
379 << "samples: " << diff_samples << ", "
henrikaa6d26ec2016-09-20 04:44:04 -0700380 << "rate: " << static_cast<int>(rate + 0.5) << ", "
henrikaf06f35a2016-09-09 14:23:11 +0200381 << "level: " << max_play_level_;
382 }
383
384 // Count number of times we detect "no audio" corresponding to a case where
385 // all level measurements have been zero.
386 if (max_rec_level_ == 0) {
387 ++num_rec_level_is_zero_;
henrika6c4d0f02016-07-14 05:54:19 -0700388 }
389
390 last_rec_callbacks_ = rec_callbacks_;
391 last_play_callbacks_ = play_callbacks_;
392 last_rec_samples_ = rec_samples_;
393 last_play_samples_ = play_samples_;
henrikaf06f35a2016-09-09 14:23:11 +0200394 max_rec_level_ = 0;
395 max_play_level_ = 0;
henrika6c4d0f02016-07-14 05:54:19 -0700396
397 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis();
398 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval";
399
400 // Update some stats but do it on the task queue to ensure that access of
401 // members is serialized hence avoiding usage of locks.
402 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this),
403 time_to_wait_ms);
404}
405
henrikaf06f35a2016-09-09 14:23:11 +0200406void AudioDeviceBuffer::ResetRecStats() {
407 rec_callbacks_ = 0;
408 last_rec_callbacks_ = 0;
409 rec_samples_ = 0;
410 last_rec_samples_ = 0;
411 max_rec_level_ = 0;
412 num_rec_level_is_zero_ = 0;
413}
414
415void AudioDeviceBuffer::ResetPlayStats() {
416 last_playout_time_ = rtc::TimeMillis();
417 play_callbacks_ = 0;
418 last_play_callbacks_ = 0;
419 play_samples_ = 0;
420 last_play_samples_ = 0;
421 max_play_level_ = 0;
422}
423
424void AudioDeviceBuffer::UpdateRecStats(const void* audio_buffer,
425 size_t num_samples) {
henrika6c4d0f02016-07-14 05:54:19 -0700426 RTC_DCHECK(task_queue_.IsCurrent());
427 ++rec_callbacks_;
428 rec_samples_ += num_samples;
henrikaf06f35a2016-09-09 14:23:11 +0200429
430 // Find the max absolute value in an audio packet twice per second and update
431 // |max_rec_level_| to track the largest value.
432 if (rec_callbacks_ % 50 == 0) {
433 int16_t max_abs = WebRtcSpl_MaxAbsValueW16(
434 static_cast<int16_t*>(const_cast<void*>(audio_buffer)),
435 num_samples * rec_channels_);
436 if (max_abs > max_rec_level_) {
437 max_rec_level_ = max_abs;
438 }
439 }
henrika6c4d0f02016-07-14 05:54:19 -0700440}
441
henrikaf06f35a2016-09-09 14:23:11 +0200442void AudioDeviceBuffer::UpdatePlayStats(const void* audio_buffer,
443 size_t num_samples) {
henrika6c4d0f02016-07-14 05:54:19 -0700444 RTC_DCHECK(task_queue_.IsCurrent());
445 ++play_callbacks_;
446 play_samples_ += num_samples;
henrikaf06f35a2016-09-09 14:23:11 +0200447
448 // Find the max absolute value in an audio packet twice per second and update
449 // |max_play_level_| to track the largest value.
450 if (play_callbacks_ % 50 == 0) {
451 int16_t max_abs = WebRtcSpl_MaxAbsValueW16(
452 static_cast<int16_t*>(const_cast<void*>(audio_buffer)),
453 num_samples * play_channels_);
454 if (max_abs > max_play_level_) {
455 max_play_level_ = max_abs;
456 }
457 }
henrika6c4d0f02016-07-14 05:54:19 -0700458}
459
niklase@google.com470e71d2011-07-07 08:21:25 +0000460} // namespace webrtc