blob: f74d3d58e1852ff846bf121ceaeb027dde2e27e3 [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),
henrika3355f6d2016-10-21 12:45:25 +020062 num_rec_level_is_zero_(0),
63 rec_stat_count_(0),
64 play_stat_count_(0) {
henrika3f33e2a2016-07-06 00:33:57 -070065 LOG(INFO) << "AudioDeviceBuffer::ctor";
niklase@google.com470e71d2011-07-07 08:21:25 +000066}
67
henrika0fd68012016-07-04 13:01:19 +020068AudioDeviceBuffer::~AudioDeviceBuffer() {
henrika6c4d0f02016-07-14 05:54:19 -070069 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrika3f33e2a2016-07-06 00:33:57 -070070 LOG(INFO) << "AudioDeviceBuffer::~dtor";
henrika3d7346f2016-07-29 16:20:47 +020071
72 size_t total_diff_time = 0;
73 int num_measurements = 0;
74 LOG(INFO) << "[playout diff time => #measurements]";
75 for (size_t diff = 0; diff < arraysize(playout_diff_times_); ++diff) {
76 uint32_t num_elements = playout_diff_times_[diff];
77 if (num_elements > 0) {
78 total_diff_time += num_elements * diff;
79 num_measurements += num_elements;
80 LOG(INFO) << "[" << diff << " => " << num_elements << "]";
81 }
82 }
83 if (num_measurements > 0) {
84 LOG(INFO) << "total_diff_time: " << total_diff_time;
85 LOG(INFO) << "num_measurements: " << num_measurements;
86 LOG(INFO) << "average: "
87 << static_cast<float>(total_diff_time) / num_measurements;
88 }
henrikaf06f35a2016-09-09 14:23:11 +020089
90 // Add UMA histogram to keep track of the case when only zeros have been
91 // recorded. Ensure that recording callbacks have started and that at least
92 // one timer event has been able to update |num_rec_level_is_zero_|.
93 // I am avoiding use of the task queue here since we are under destruction
94 // and reading these members on the creating thread feels safe.
95 if (rec_callbacks_ > 0 && num_stat_reports_ > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -070096 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros",
henrikaf06f35a2016-09-09 14:23:11 +020097 static_cast<int>(num_stat_reports_ == num_rec_level_is_zero_));
98 }
niklase@google.com470e71d2011-07-07 08:21:25 +000099}
100
henrika0fd68012016-07-04 13:01:19 +0200101int32_t AudioDeviceBuffer::RegisterAudioCallback(
henrika49810512016-08-22 05:56:12 -0700102 AudioTransport* audio_callback) {
henrika3f33e2a2016-07-06 00:33:57 -0700103 LOG(INFO) << __FUNCTION__;
henrika5588a132016-10-18 05:14:30 -0700104 rtc::CritScope lock(&lock_cb_);
henrika49810512016-08-22 05:56:12 -0700105 audio_transport_cb_ = audio_callback;
henrika0fd68012016-07-04 13:01:19 +0200106 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000107}
108
henrika0fd68012016-07-04 13:01:19 +0200109int32_t AudioDeviceBuffer::InitPlayout() {
henrikad7a89db2016-08-19 08:09:25 -0700110 LOG(INFO) << __FUNCTION__;
henrika49810512016-08-22 05:56:12 -0700111 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrikaf06f35a2016-09-09 14:23:11 +0200112 ResetPlayStats();
henrika6c4d0f02016-07-14 05:54:19 -0700113 if (!timer_has_started_) {
114 StartTimer();
115 timer_has_started_ = true;
116 }
henrika0fd68012016-07-04 13:01:19 +0200117 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000118}
119
henrika0fd68012016-07-04 13:01:19 +0200120int32_t AudioDeviceBuffer::InitRecording() {
henrikad7a89db2016-08-19 08:09:25 -0700121 LOG(INFO) << __FUNCTION__;
henrika49810512016-08-22 05:56:12 -0700122 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrikaf06f35a2016-09-09 14:23:11 +0200123 ResetRecStats();
henrika6c4d0f02016-07-14 05:54:19 -0700124 if (!timer_has_started_) {
125 StartTimer();
126 timer_has_started_ = true;
127 }
henrika0fd68012016-07-04 13:01:19 +0200128 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000129}
130
henrika0fd68012016-07-04 13:01:19 +0200131int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) {
henrika3f33e2a2016-07-06 00:33:57 -0700132 LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")";
henrika5588a132016-10-18 05:14:30 -0700133 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700134 rec_sample_rate_ = fsHz;
henrika0fd68012016-07-04 13:01:19 +0200135 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000136}
137
henrika0fd68012016-07-04 13:01:19 +0200138int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) {
henrika3f33e2a2016-07-06 00:33:57 -0700139 LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")";
henrika5588a132016-10-18 05:14:30 -0700140 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700141 play_sample_rate_ = fsHz;
henrika0fd68012016-07-04 13:01:19 +0200142 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000143}
144
henrika0fd68012016-07-04 13:01:19 +0200145int32_t AudioDeviceBuffer::RecordingSampleRate() const {
henrika49810512016-08-22 05:56:12 -0700146 return rec_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000147}
148
henrika0fd68012016-07-04 13:01:19 +0200149int32_t AudioDeviceBuffer::PlayoutSampleRate() const {
henrika49810512016-08-22 05:56:12 -0700150 return play_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000151}
152
henrika0fd68012016-07-04 13:01:19 +0200153int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) {
henrika49810512016-08-22 05:56:12 -0700154 LOG(INFO) << "SetRecordingChannels(" << channels << ")";
henrika5588a132016-10-18 05:14:30 -0700155 rtc::CritScope lock(&lock_);
henrika49810512016-08-22 05:56:12 -0700156 rec_channels_ = channels;
henrika5588a132016-10-18 05:14:30 -0700157 rec_bytes_per_sample_ = sizeof(int16_t) * channels;
henrika0fd68012016-07-04 13:01:19 +0200158 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000159}
160
henrika0fd68012016-07-04 13:01:19 +0200161int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) {
henrika49810512016-08-22 05:56:12 -0700162 LOG(INFO) << "SetPlayoutChannels(" << channels << ")";
henrika5588a132016-10-18 05:14:30 -0700163 rtc::CritScope lock(&lock_);
henrika49810512016-08-22 05:56:12 -0700164 play_channels_ = channels;
henrika5588a132016-10-18 05:14:30 -0700165 play_bytes_per_sample_ = sizeof(int16_t) * channels;
henrika0fd68012016-07-04 13:01:19 +0200166 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000167}
168
henrika0fd68012016-07-04 13:01:19 +0200169int32_t AudioDeviceBuffer::SetRecordingChannel(
170 const AudioDeviceModule::ChannelType channel) {
henrika5588a132016-10-18 05:14:30 -0700171 LOG(INFO) << "SetRecordingChannel(" << channel << ")";
172 LOG(LS_WARNING) << "Not implemented";
173 // Add DCHECK to ensure that user does not try to use this API with a non-
174 // default parameter.
175 RTC_DCHECK_EQ(channel, AudioDeviceModule::kChannelBoth);
176 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000177}
178
henrika0fd68012016-07-04 13:01:19 +0200179int32_t AudioDeviceBuffer::RecordingChannel(
180 AudioDeviceModule::ChannelType& channel) const {
henrika5588a132016-10-18 05:14:30 -0700181 LOG(LS_WARNING) << "Not implemented";
182 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000183}
184
henrika0fd68012016-07-04 13:01:19 +0200185size_t AudioDeviceBuffer::RecordingChannels() const {
henrika49810512016-08-22 05:56:12 -0700186 return rec_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000187}
188
henrika0fd68012016-07-04 13:01:19 +0200189size_t AudioDeviceBuffer::PlayoutChannels() const {
henrika49810512016-08-22 05:56:12 -0700190 return play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000191}
192
henrika0fd68012016-07-04 13:01:19 +0200193int32_t AudioDeviceBuffer::SetCurrentMicLevel(uint32_t level) {
henrika49810512016-08-22 05:56:12 -0700194 current_mic_level_ = level;
henrika0fd68012016-07-04 13:01:19 +0200195 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000196}
197
henrika49810512016-08-22 05:56:12 -0700198int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) {
199 typing_status_ = typing_status;
henrika0fd68012016-07-04 13:01:19 +0200200 return 0;
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +0000201}
202
henrika0fd68012016-07-04 13:01:19 +0200203uint32_t AudioDeviceBuffer::NewMicLevel() const {
henrika49810512016-08-22 05:56:12 -0700204 return new_mic_level_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205}
206
henrika49810512016-08-22 05:56:12 -0700207void AudioDeviceBuffer::SetVQEData(int play_delay_ms,
208 int rec_delay_ms,
209 int clock_drift) {
210 play_delay_ms_ = play_delay_ms;
211 rec_delay_ms_ = rec_delay_ms;
212 clock_drift_ = clock_drift;
niklase@google.com470e71d2011-07-07 08:21:25 +0000213}
214
pbos@webrtc.org25509882013-04-09 10:30:35 +0000215int32_t AudioDeviceBuffer::StartInputFileRecording(
henrika0fd68012016-07-04 13:01:19 +0200216 const char fileName[kAdmMaxFileNameSize]) {
henrika49810512016-08-22 05:56:12 -0700217 LOG(LS_WARNING) << "Not implemented";
218 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000219}
220
henrika0fd68012016-07-04 13:01:19 +0200221int32_t AudioDeviceBuffer::StopInputFileRecording() {
henrika49810512016-08-22 05:56:12 -0700222 LOG(LS_WARNING) << "Not implemented";
henrika0fd68012016-07-04 13:01:19 +0200223 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000224}
225
pbos@webrtc.org25509882013-04-09 10:30:35 +0000226int32_t AudioDeviceBuffer::StartOutputFileRecording(
henrika0fd68012016-07-04 13:01:19 +0200227 const char fileName[kAdmMaxFileNameSize]) {
henrika49810512016-08-22 05:56:12 -0700228 LOG(LS_WARNING) << "Not implemented";
henrikacf327b42016-08-19 16:37:53 +0200229 return 0;
230}
231
henrika49810512016-08-22 05:56:12 -0700232int32_t AudioDeviceBuffer::StopOutputFileRecording() {
233 LOG(LS_WARNING) << "Not implemented";
234 return 0;
235}
236
237int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer,
238 size_t num_samples) {
henrika3355f6d2016-10-21 12:45:25 +0200239 const size_t rec_channels = [&] {
henrika5588a132016-10-18 05:14:30 -0700240 rtc::CritScope lock(&lock_);
henrika3355f6d2016-10-21 12:45:25 +0200241 return rec_channels_;
henrika5588a132016-10-18 05:14:30 -0700242 }();
243 // Copy the complete input buffer to the local buffer.
henrika3355f6d2016-10-21 12:45:25 +0200244 const size_t size_in_bytes = num_samples * rec_channels * sizeof(int16_t);
henrika5588a132016-10-18 05:14:30 -0700245 const size_t old_size = rec_buffer_.size();
246 rec_buffer_.SetData(static_cast<const uint8_t*>(audio_buffer), size_in_bytes);
247 // Keep track of the size of the recording buffer. Only updated when the
248 // size changes, which is a rare event.
249 if (old_size != rec_buffer_.size()) {
250 LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size();
henrika0fd68012016-07-04 13:01:19 +0200251 }
henrika3355f6d2016-10-21 12:45:25 +0200252 // Derive a new level value twice per second.
253 int16_t max_abs = 0;
254 RTC_DCHECK_LT(rec_stat_count_, 50);
255 if (++rec_stat_count_ >= 50) {
256 const size_t size = num_samples * rec_channels;
257 // Returns the largest absolute value in a signed 16-bit vector.
258 max_abs = WebRtcSpl_MaxAbsValueW16(
259 reinterpret_cast<const int16_t*>(rec_buffer_.data()), size);
260 rec_stat_count_ = 0;
261 }
henrika6c4d0f02016-07-14 05:54:19 -0700262 // Update some stats but do it on the task queue to ensure that the members
henrika3355f6d2016-10-21 12:45:25 +0200263 // are modified and read on the same thread. Note that |max_abs| will be
264 // zero in most calls and then have no effect of the stats. It is only updated
265 // approximately two times per second and can then change the stats.
henrikaf06f35a2016-09-09 14:23:11 +0200266 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::UpdateRecStats, this,
henrika3355f6d2016-10-21 12:45:25 +0200267 max_abs, num_samples));
henrika0fd68012016-07-04 13:01:19 +0200268 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000269}
270
henrika0fd68012016-07-04 13:01:19 +0200271int32_t AudioDeviceBuffer::DeliverRecordedData() {
henrika5588a132016-10-18 05:14:30 -0700272 rtc::CritScope lock(&lock_cb_);
henrika49810512016-08-22 05:56:12 -0700273 if (!audio_transport_cb_) {
henrika3f33e2a2016-07-06 00:33:57 -0700274 LOG(LS_WARNING) << "Invalid audio transport";
niklase@google.com470e71d2011-07-07 08:21:25 +0000275 return 0;
henrika0fd68012016-07-04 13:01:19 +0200276 }
henrika5588a132016-10-18 05:14:30 -0700277 const size_t rec_bytes_per_sample = [&] {
278 rtc::CritScope lock(&lock_);
279 return rec_bytes_per_sample_;
280 }();
281 uint32_t new_mic_level(0);
282 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_;
283 size_t num_samples = rec_buffer_.size() / rec_bytes_per_sample;
284 int32_t res = audio_transport_cb_->RecordedDataIsAvailable(
285 rec_buffer_.data(), num_samples, rec_bytes_per_sample_, rec_channels_,
286 rec_sample_rate_, total_delay_ms, clock_drift_, current_mic_level_,
287 typing_status_, new_mic_level);
henrika0fd68012016-07-04 13:01:19 +0200288 if (res != -1) {
henrika5588a132016-10-18 05:14:30 -0700289 new_mic_level_ = new_mic_level;
henrika49810512016-08-22 05:56:12 -0700290 } else {
291 LOG(LS_ERROR) << "RecordedDataIsAvailable() failed";
henrika0fd68012016-07-04 13:01:19 +0200292 }
henrika0fd68012016-07-04 13:01:19 +0200293 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000294}
295
henrika49810512016-08-22 05:56:12 -0700296int32_t AudioDeviceBuffer::RequestPlayoutData(size_t num_samples) {
henrika3d7346f2016-07-29 16:20:47 +0200297 // Measure time since last function call and update an array where the
298 // position/index corresponds to time differences (in milliseconds) between
299 // two successive playout callbacks, and the stored value is the number of
300 // times a given time difference was found.
301 int64_t now_time = rtc::TimeMillis();
302 size_t diff_time = rtc::TimeDiff(now_time, last_playout_time_);
303 // Truncate at 500ms to limit the size of the array.
304 diff_time = std::min(kMaxDeltaTimeInMs, diff_time);
305 last_playout_time_ = now_time;
306 playout_diff_times_[diff_time]++;
307
henrika3355f6d2016-10-21 12:45:25 +0200308 const size_t play_channels = [&] {
henrika5588a132016-10-18 05:14:30 -0700309 rtc::CritScope lock(&lock_);
henrika3355f6d2016-10-21 12:45:25 +0200310 return play_channels_;
henrika5588a132016-10-18 05:14:30 -0700311 }();
henrika0fd68012016-07-04 13:01:19 +0200312
henrika5588a132016-10-18 05:14:30 -0700313 // The consumer can change the request size on the fly and we therefore
314 // resize the buffer accordingly. Also takes place at the first call to this
315 // method.
henrika3355f6d2016-10-21 12:45:25 +0200316 const size_t play_bytes_per_sample = play_channels * sizeof(int16_t);
henrika5588a132016-10-18 05:14:30 -0700317 const size_t size_in_bytes = num_samples * play_bytes_per_sample;
318 if (play_buffer_.size() != size_in_bytes) {
319 play_buffer_.SetSize(size_in_bytes);
320 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size();
321 }
322
323 rtc::CritScope lock(&lock_cb_);
henrika0fd68012016-07-04 13:01:19 +0200324
henrika3f33e2a2016-07-06 00:33:57 -0700325 // It is currently supported to start playout without a valid audio
326 // transport object. Leads to warning and silence.
henrika49810512016-08-22 05:56:12 -0700327 if (!audio_transport_cb_) {
henrika3f33e2a2016-07-06 00:33:57 -0700328 LOG(LS_WARNING) << "Invalid audio transport";
henrika0fd68012016-07-04 13:01:19 +0200329 return 0;
330 }
331
henrika3355f6d2016-10-21 12:45:25 +0200332 // Retrieve new 16-bit PCM audio data using the audio transport instance.
henrika3f33e2a2016-07-06 00:33:57 -0700333 int64_t elapsed_time_ms = -1;
334 int64_t ntp_time_ms = -1;
henrika49810512016-08-22 05:56:12 -0700335 size_t num_samples_out(0);
henrika5588a132016-10-18 05:14:30 -0700336 uint32_t res = audio_transport_cb_->NeedMorePlayData(
henrika3355f6d2016-10-21 12:45:25 +0200337 num_samples, play_bytes_per_sample_, play_channels, play_sample_rate_,
henrika5588a132016-10-18 05:14:30 -0700338 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms);
henrika3f33e2a2016-07-06 00:33:57 -0700339 if (res != 0) {
340 LOG(LS_ERROR) << "NeedMorePlayData() failed";
henrika0fd68012016-07-04 13:01:19 +0200341 }
342
henrika3355f6d2016-10-21 12:45:25 +0200343 // Derive a new level value twice per second.
344 int16_t max_abs = 0;
345 RTC_DCHECK_LT(play_stat_count_, 50);
346 if (++play_stat_count_ >= 50) {
347 const size_t size = num_samples * play_channels;
348 // Returns the largest absolute value in a signed 16-bit vector.
349 max_abs = WebRtcSpl_MaxAbsValueW16(
350 reinterpret_cast<const int16_t*>(play_buffer_.data()), size);
351 play_stat_count_ = 0;
352 }
353 // Update some stats but do it on the task queue to ensure that the members
354 // are modified and read on the same thread. Note that |max_abs| will be
355 // zero in most calls and then have no effect of the stats. It is only updated
356 // approximately two times per second and can then change the stats.
henrikaf06f35a2016-09-09 14:23:11 +0200357 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::UpdatePlayStats, this,
henrika3355f6d2016-10-21 12:45:25 +0200358 max_abs, num_samples_out));
henrika49810512016-08-22 05:56:12 -0700359 return static_cast<int32_t>(num_samples_out);
niklase@google.com470e71d2011-07-07 08:21:25 +0000360}
361
henrika49810512016-08-22 05:56:12 -0700362int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) {
henrika5588a132016-10-18 05:14:30 -0700363 RTC_DCHECK_GT(play_buffer_.size(), 0u);
364 const size_t play_bytes_per_sample = [&] {
365 rtc::CritScope lock(&lock_);
366 return play_bytes_per_sample_;
367 }();
368 memcpy(audio_buffer, play_buffer_.data(), play_buffer_.size());
369 return static_cast<int32_t>(play_buffer_.size() / play_bytes_per_sample);
niklase@google.com470e71d2011-07-07 08:21:25 +0000370}
371
henrika6c4d0f02016-07-14 05:54:19 -0700372void AudioDeviceBuffer::StartTimer() {
henrikaf06f35a2016-09-09 14:23:11 +0200373 num_stat_reports_ = 0;
henrika6c4d0f02016-07-14 05:54:19 -0700374 last_log_stat_time_ = rtc::TimeMillis();
375 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this),
376 kTimerIntervalInMilliseconds);
377}
378
379void AudioDeviceBuffer::LogStats() {
380 RTC_DCHECK(task_queue_.IsCurrent());
381
382 int64_t now_time = rtc::TimeMillis();
383 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds;
384 int64_t time_since_last = rtc::TimeDiff(now_time, last_log_stat_time_);
385 last_log_stat_time_ = now_time;
386
387 // Log the latest statistics but skip the first 10 seconds since we are not
388 // sure of the exact starting point. I.e., the first log printout will be
389 // after ~20 seconds.
henrikaa6d26ec2016-09-20 04:44:04 -0700390 if (++num_stat_reports_ > 1 && time_since_last > 0) {
henrika6c4d0f02016-07-14 05:54:19 -0700391 uint32_t diff_samples = rec_samples_ - last_rec_samples_;
henrikaa6d26ec2016-09-20 04:44:04 -0700392 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
henrika6c4d0f02016-07-14 05:54:19 -0700393 LOG(INFO) << "[REC : " << time_since_last << "msec, "
henrika49810512016-08-22 05:56:12 -0700394 << rec_sample_rate_ / 1000
henrika6c4d0f02016-07-14 05:54:19 -0700395 << "kHz] callbacks: " << rec_callbacks_ - last_rec_callbacks_
396 << ", "
397 << "samples: " << diff_samples << ", "
henrikaa6d26ec2016-09-20 04:44:04 -0700398 << "rate: " << static_cast<int>(rate + 0.5) << ", "
henrikaf06f35a2016-09-09 14:23:11 +0200399 << "level: " << max_rec_level_;
henrika6c4d0f02016-07-14 05:54:19 -0700400
401 diff_samples = play_samples_ - last_play_samples_;
henrikaa6d26ec2016-09-20 04:44:04 -0700402 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
henrika6c4d0f02016-07-14 05:54:19 -0700403 LOG(INFO) << "[PLAY: " << time_since_last << "msec, "
henrika49810512016-08-22 05:56:12 -0700404 << play_sample_rate_ / 1000
henrika6c4d0f02016-07-14 05:54:19 -0700405 << "kHz] callbacks: " << play_callbacks_ - last_play_callbacks_
406 << ", "
407 << "samples: " << diff_samples << ", "
henrikaa6d26ec2016-09-20 04:44:04 -0700408 << "rate: " << static_cast<int>(rate + 0.5) << ", "
henrikaf06f35a2016-09-09 14:23:11 +0200409 << "level: " << max_play_level_;
410 }
411
412 // Count number of times we detect "no audio" corresponding to a case where
413 // all level measurements have been zero.
414 if (max_rec_level_ == 0) {
415 ++num_rec_level_is_zero_;
henrika6c4d0f02016-07-14 05:54:19 -0700416 }
417
418 last_rec_callbacks_ = rec_callbacks_;
419 last_play_callbacks_ = play_callbacks_;
420 last_rec_samples_ = rec_samples_;
421 last_play_samples_ = play_samples_;
henrikaf06f35a2016-09-09 14:23:11 +0200422 max_rec_level_ = 0;
423 max_play_level_ = 0;
henrika6c4d0f02016-07-14 05:54:19 -0700424
425 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis();
426 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval";
427
428 // Update some stats but do it on the task queue to ensure that access of
429 // members is serialized hence avoiding usage of locks.
430 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this),
431 time_to_wait_ms);
432}
433
henrikaf06f35a2016-09-09 14:23:11 +0200434void AudioDeviceBuffer::ResetRecStats() {
435 rec_callbacks_ = 0;
436 last_rec_callbacks_ = 0;
437 rec_samples_ = 0;
438 last_rec_samples_ = 0;
439 max_rec_level_ = 0;
440 num_rec_level_is_zero_ = 0;
441}
442
443void AudioDeviceBuffer::ResetPlayStats() {
444 last_playout_time_ = rtc::TimeMillis();
445 play_callbacks_ = 0;
446 last_play_callbacks_ = 0;
447 play_samples_ = 0;
448 last_play_samples_ = 0;
449 max_play_level_ = 0;
450}
451
henrika3355f6d2016-10-21 12:45:25 +0200452void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs, size_t num_samples) {
henrika6c4d0f02016-07-14 05:54:19 -0700453 RTC_DCHECK(task_queue_.IsCurrent());
454 ++rec_callbacks_;
455 rec_samples_ += num_samples;
henrika3355f6d2016-10-21 12:45:25 +0200456 if (max_abs > max_rec_level_) {
457 max_rec_level_ = max_abs;
henrikaf06f35a2016-09-09 14:23:11 +0200458 }
henrika6c4d0f02016-07-14 05:54:19 -0700459}
460
henrika3355f6d2016-10-21 12:45:25 +0200461void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs, size_t num_samples) {
henrika6c4d0f02016-07-14 05:54:19 -0700462 RTC_DCHECK(task_queue_.IsCurrent());
463 ++play_callbacks_;
464 play_samples_ += num_samples;
henrika3355f6d2016-10-21 12:45:25 +0200465 if (max_abs > max_play_level_) {
466 max_play_level_ = max_abs;
henrikaf06f35a2016-09-09 14:23:11 +0200467 }
henrika6c4d0f02016-07-14 05:54:19 -0700468}
469
niklase@google.com470e71d2011-07-07 08:21:25 +0000470} // namespace webrtc