blob: 37191fe9b0f2e59e5562d565befe69e514aa78bf [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
Yves Gerey988cc082018-10-23 12:03:01 +020011#include <string.h>
henrika7be78832017-06-13 17:34:16 +020012#include <cmath>
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <cstddef>
14#include <cstdint>
andrew@webrtc.org25534502013-09-13 00:02:13 +000015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "common_audio/signal_processing/include/signal_processing_library.h"
Yves Gerey988cc082018-10-23 12:03:01 +020017#include "modules/audio_device/audio_device_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/bind.h"
19#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "system_wrappers/include/metrics.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
niklase@google.com470e71d2011-07-07 08:21:25 +000024namespace webrtc {
25
henrika6c4d0f02016-07-14 05:54:19 -070026static const char kTimerQueueName[] = "AudioDeviceBufferTimer";
27
28// Time between two sucessive calls to LogStats().
29static const size_t kTimerIntervalInSeconds = 10;
30static const size_t kTimerIntervalInMilliseconds =
31 kTimerIntervalInSeconds * rtc::kNumMillisecsPerSec;
henrikaba156cf2016-10-31 08:18:50 -070032// Min time required to qualify an audio session as a "call". If playout or
33// recording has been active for less than this time we will not store any
34// logs or UMA stats but instead consider the call as too short.
35static const size_t kMinValidCallTimeTimeInSeconds = 10;
36static const size_t kMinValidCallTimeTimeInMilliseconds =
37 kMinValidCallTimeTimeInSeconds * rtc::kNumMillisecsPerSec;
henrika7be78832017-06-13 17:34:16 +020038#ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE
39static const double k2Pi = 6.28318530717959;
40#endif
henrika6c4d0f02016-07-14 05:54:19 -070041
Danil Chapovalov1c41be62019-04-01 09:16:12 +020042AudioDeviceBuffer::AudioDeviceBuffer(TaskQueueFactory* task_queue_factory)
43 : task_queue_(task_queue_factory->CreateTaskQueue(
44 kTimerQueueName,
45 TaskQueueFactory::Priority::NORMAL)),
henrikaf5022222016-11-07 15:56:59 +010046 audio_transport_cb_(nullptr),
henrika49810512016-08-22 05:56:12 -070047 rec_sample_rate_(0),
48 play_sample_rate_(0),
49 rec_channels_(0),
50 play_channels_(0),
henrikaf5022222016-11-07 15:56:59 +010051 playing_(false),
52 recording_(false),
henrika49810512016-08-22 05:56:12 -070053 typing_status_(false),
54 play_delay_ms_(0),
55 rec_delay_ms_(0),
henrika6c4d0f02016-07-14 05:54:19 -070056 num_stat_reports_(0),
henrikaf5022222016-11-07 15:56:59 +010057 last_timer_task_time_(0),
henrika3355f6d2016-10-21 12:45:25 +020058 rec_stat_count_(0),
henrikaba156cf2016-10-31 08:18:50 -070059 play_stat_count_(0),
60 play_start_time_(0),
henrika0b3a6382016-11-11 02:28:50 -080061 only_silence_recorded_(true),
62 log_stats_(false) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010063 RTC_LOG(INFO) << "AudioDeviceBuffer::ctor";
henrika7be78832017-06-13 17:34:16 +020064#ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE
65 phase_ = 0.0;
Mirko Bonadei675513b2017-11-09 11:09:25 +010066 RTC_LOG(WARNING) << "AUDIO_DEVICE_PLAYS_SINUS_TONE is defined!";
henrika7be78832017-06-13 17:34:16 +020067#endif
niklase@google.com470e71d2011-07-07 08:21:25 +000068}
69
henrika0fd68012016-07-04 13:01:19 +020070AudioDeviceBuffer::~AudioDeviceBuffer() {
henrikaf5022222016-11-07 15:56:59 +010071 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -070072 RTC_DCHECK(!playing_);
73 RTC_DCHECK(!recording_);
Mirko Bonadei675513b2017-11-09 11:09:25 +010074 RTC_LOG(INFO) << "AudioDeviceBuffer::~dtor";
niklase@google.com470e71d2011-07-07 08:21:25 +000075}
76
henrika0fd68012016-07-04 13:01:19 +020077int32_t AudioDeviceBuffer::RegisterAudioCallback(
henrika49810512016-08-22 05:56:12 -070078 AudioTransport* audio_callback) {
henrikaf5022222016-11-07 15:56:59 +010079 RTC_DCHECK_RUN_ON(&main_thread_checker_);
Mirko Bonadei675513b2017-11-09 11:09:25 +010080 RTC_LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +010081 if (playing_ || recording_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010082 RTC_LOG(LS_ERROR) << "Failed to set audio transport since media was active";
henrikaf5022222016-11-07 15:56:59 +010083 return -1;
84 }
henrika49810512016-08-22 05:56:12 -070085 audio_transport_cb_ = audio_callback;
henrika0fd68012016-07-04 13:01:19 +020086 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000087}
88
henrikaba156cf2016-10-31 08:18:50 -070089void AudioDeviceBuffer::StartPlayout() {
henrikaf5022222016-11-07 15:56:59 +010090 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -070091 // TODO(henrika): allow for usage of DCHECK(!playing_) here instead. Today the
92 // ADM allows calling Start(), Start() by ignoring the second call but it
93 // makes more sense to only allow one call.
94 if (playing_) {
95 return;
henrika6c4d0f02016-07-14 05:54:19 -070096 }
Mirko Bonadei675513b2017-11-09 11:09:25 +010097 RTC_LOG(INFO) << __FUNCTION__;
henrikaba156cf2016-10-31 08:18:50 -070098 // Clear members tracking playout stats and do it on the task queue.
99 task_queue_.PostTask([this] { ResetPlayStats(); });
100 // Start a periodic timer based on task queue if not already done by the
101 // recording side.
102 if (!recording_) {
103 StartPeriodicLogging();
104 }
nissedeb95f32016-11-28 01:54:54 -0800105 const int64_t now_time = rtc::TimeMillis();
henrikaba156cf2016-10-31 08:18:50 -0700106 // Clear members that are only touched on the main (creating) thread.
107 play_start_time_ = now_time;
henrikaba156cf2016-10-31 08:18:50 -0700108 playing_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000109}
110
henrikaba156cf2016-10-31 08:18:50 -0700111void AudioDeviceBuffer::StartRecording() {
henrikaf5022222016-11-07 15:56:59 +0100112 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700113 if (recording_) {
114 return;
henrika6c4d0f02016-07-14 05:54:19 -0700115 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100116 RTC_LOG(INFO) << __FUNCTION__;
henrikaba156cf2016-10-31 08:18:50 -0700117 // Clear members tracking recording stats and do it on the task queue.
118 task_queue_.PostTask([this] { ResetRecStats(); });
119 // Start a periodic timer based on task queue if not already done by the
120 // playout side.
121 if (!playing_) {
122 StartPeriodicLogging();
123 }
124 // Clear members that will be touched on the main (creating) thread.
125 rec_start_time_ = rtc::TimeMillis();
126 recording_ = true;
127 // And finally a member which can be modified on the native audio thread.
128 // It is safe to do so since we know by design that the owning ADM has not
129 // yet started the native audio recording.
130 only_silence_recorded_ = true;
131}
132
133void AudioDeviceBuffer::StopPlayout() {
henrikaf5022222016-11-07 15:56:59 +0100134 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700135 if (!playing_) {
136 return;
137 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100138 RTC_LOG(INFO) << __FUNCTION__;
henrikaba156cf2016-10-31 08:18:50 -0700139 playing_ = false;
140 // Stop periodic logging if no more media is active.
141 if (!recording_) {
142 StopPeriodicLogging();
143 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100144 RTC_LOG(INFO) << "total playout time: " << rtc::TimeSince(play_start_time_);
henrikaba156cf2016-10-31 08:18:50 -0700145}
146
147void AudioDeviceBuffer::StopRecording() {
henrikaf5022222016-11-07 15:56:59 +0100148 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700149 if (!recording_) {
150 return;
151 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100152 RTC_LOG(INFO) << __FUNCTION__;
henrikaba156cf2016-10-31 08:18:50 -0700153 recording_ = false;
154 // Stop periodic logging if no more media is active.
155 if (!playing_) {
156 StopPeriodicLogging();
157 }
158 // Add UMA histogram to keep track of the case when only zeros have been
159 // recorded. Measurements (max of absolute level) are taken twice per second,
160 // which means that if e.g 10 seconds of audio has been recorded, a total of
161 // 20 level estimates must all be identical to zero to trigger the histogram.
162 // |only_silence_recorded_| can only be cleared on the native audio thread
163 // that drives audio capture but we know by design that the audio has stopped
164 // when this method is called, hence there should not be aby conflicts. Also,
165 // the fact that |only_silence_recorded_| can be affected during the complete
166 // call makes chances of conflicts with potentially one last callback very
167 // small.
168 const size_t time_since_start = rtc::TimeSince(rec_start_time_);
169 if (time_since_start > kMinValidCallTimeTimeInMilliseconds) {
170 const int only_zeros = static_cast<int>(only_silence_recorded_);
171 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", only_zeros);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100172 RTC_LOG(INFO) << "HISTOGRAM(WebRTC.Audio.RecordedOnlyZeros): "
173 << only_zeros;
henrikaba156cf2016-10-31 08:18:50 -0700174 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100175 RTC_LOG(INFO) << "total recording time: " << time_since_start;
niklase@google.com470e71d2011-07-07 08:21:25 +0000176}
177
henrika0fd68012016-07-04 13:01:19 +0200178int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100179 RTC_LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")";
henrika49810512016-08-22 05:56:12 -0700180 rec_sample_rate_ = fsHz;
henrika0fd68012016-07-04 13:01:19 +0200181 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000182}
183
henrika0fd68012016-07-04 13:01:19 +0200184int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100185 RTC_LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")";
henrika49810512016-08-22 05:56:12 -0700186 play_sample_rate_ = fsHz;
henrika0fd68012016-07-04 13:01:19 +0200187 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000188}
189
henrikacfbd26d2018-09-05 11:36:22 +0200190uint32_t AudioDeviceBuffer::RecordingSampleRate() const {
henrika49810512016-08-22 05:56:12 -0700191 return rec_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000192}
193
henrikacfbd26d2018-09-05 11:36:22 +0200194uint32_t AudioDeviceBuffer::PlayoutSampleRate() const {
henrika49810512016-08-22 05:56:12 -0700195 return play_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000196}
197
henrika0fd68012016-07-04 13:01:19 +0200198int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100199 RTC_LOG(INFO) << "SetRecordingChannels(" << channels << ")";
henrika49810512016-08-22 05:56:12 -0700200 rec_channels_ = channels;
henrika0fd68012016-07-04 13:01:19 +0200201 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000202}
203
henrika0fd68012016-07-04 13:01:19 +0200204int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100205 RTC_LOG(INFO) << "SetPlayoutChannels(" << channels << ")";
henrika49810512016-08-22 05:56:12 -0700206 play_channels_ = channels;
henrika0fd68012016-07-04 13:01:19 +0200207 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000208}
209
henrika0fd68012016-07-04 13:01:19 +0200210size_t AudioDeviceBuffer::RecordingChannels() const {
henrika49810512016-08-22 05:56:12 -0700211 return rec_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000212}
213
henrika0fd68012016-07-04 13:01:19 +0200214size_t AudioDeviceBuffer::PlayoutChannels() const {
henrika49810512016-08-22 05:56:12 -0700215 return play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000216}
217
henrika49810512016-08-22 05:56:12 -0700218int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) {
219 typing_status_ = typing_status;
henrika0fd68012016-07-04 13:01:19 +0200220 return 0;
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +0000221}
222
Yves Gerey665174f2018-06-19 15:03:05 +0200223void AudioDeviceBuffer::SetVQEData(int play_delay_ms, int rec_delay_ms) {
henrika49810512016-08-22 05:56:12 -0700224 play_delay_ms_ = play_delay_ms;
225 rec_delay_ms_ = rec_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000226}
227
henrika49810512016-08-22 05:56:12 -0700228int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer,
henrika51e96082016-11-10 00:40:37 -0800229 size_t samples_per_channel) {
henrika5588a132016-10-18 05:14:30 -0700230 // Copy the complete input buffer to the local buffer.
henrika5588a132016-10-18 05:14:30 -0700231 const size_t old_size = rec_buffer_.size();
henrika51e96082016-11-10 00:40:37 -0800232 rec_buffer_.SetData(static_cast<const int16_t*>(audio_buffer),
233 rec_channels_ * samples_per_channel);
henrika5588a132016-10-18 05:14:30 -0700234 // Keep track of the size of the recording buffer. Only updated when the
235 // size changes, which is a rare event.
236 if (old_size != rec_buffer_.size()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100237 RTC_LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size();
henrika0fd68012016-07-04 13:01:19 +0200238 }
henrika51e96082016-11-10 00:40:37 -0800239
henrikaba156cf2016-10-31 08:18:50 -0700240 // Derive a new level value twice per second and check if it is non-zero.
henrika3355f6d2016-10-21 12:45:25 +0200241 int16_t max_abs = 0;
242 RTC_DCHECK_LT(rec_stat_count_, 50);
243 if (++rec_stat_count_ >= 50) {
henrika3355f6d2016-10-21 12:45:25 +0200244 // Returns the largest absolute value in a signed 16-bit vector.
henrika51e96082016-11-10 00:40:37 -0800245 max_abs = WebRtcSpl_MaxAbsValueW16(rec_buffer_.data(), rec_buffer_.size());
henrika3355f6d2016-10-21 12:45:25 +0200246 rec_stat_count_ = 0;
henrikaba156cf2016-10-31 08:18:50 -0700247 // Set |only_silence_recorded_| to false as soon as at least one detection
248 // of a non-zero audio packet is found. It can only be restored to true
249 // again by restarting the call.
250 if (max_abs > 0) {
251 only_silence_recorded_ = false;
252 }
henrika3355f6d2016-10-21 12:45:25 +0200253 }
henrika87d11cd2017-02-08 07:16:56 -0800254 // Update recording stats which is used as base for periodic logging of the
255 // audio input state.
256 UpdateRecStats(max_abs, samples_per_channel);
henrika0fd68012016-07-04 13:01:19 +0200257 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000258}
259
henrika0fd68012016-07-04 13:01:19 +0200260int32_t AudioDeviceBuffer::DeliverRecordedData() {
henrika49810512016-08-22 05:56:12 -0700261 if (!audio_transport_cb_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100262 RTC_LOG(LS_WARNING) << "Invalid audio transport";
niklase@google.com470e71d2011-07-07 08:21:25 +0000263 return 0;
henrika0fd68012016-07-04 13:01:19 +0200264 }
henrika51e96082016-11-10 00:40:37 -0800265 const size_t frames = rec_buffer_.size() / rec_channels_;
266 const size_t bytes_per_frame = rec_channels_ * sizeof(int16_t);
Fredrik Solenberg1a50cd52018-01-16 09:19:38 +0100267 uint32_t new_mic_level_dummy = 0;
henrika5588a132016-10-18 05:14:30 -0700268 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_;
henrika5588a132016-10-18 05:14:30 -0700269 int32_t res = audio_transport_cb_->RecordedDataIsAvailable(
henrika51e96082016-11-10 00:40:37 -0800270 rec_buffer_.data(), frames, bytes_per_frame, rec_channels_,
Fredrik Solenberg1a50cd52018-01-16 09:19:38 +0100271 rec_sample_rate_, total_delay_ms, 0, 0, typing_status_,
272 new_mic_level_dummy);
273 if (res == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100274 RTC_LOG(LS_ERROR) << "RecordedDataIsAvailable() failed";
henrika0fd68012016-07-04 13:01:19 +0200275 }
henrika0fd68012016-07-04 13:01:19 +0200276 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000277}
278
henrika51e96082016-11-10 00:40:37 -0800279int32_t AudioDeviceBuffer::RequestPlayoutData(size_t samples_per_channel) {
henrika51e96082016-11-10 00:40:37 -0800280 // The consumer can change the requested size on the fly and we therefore
henrika5588a132016-10-18 05:14:30 -0700281 // resize the buffer accordingly. Also takes place at the first call to this
282 // method.
henrika51e96082016-11-10 00:40:37 -0800283 const size_t total_samples = play_channels_ * samples_per_channel;
284 if (play_buffer_.size() != total_samples) {
285 play_buffer_.SetSize(total_samples);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100286 RTC_LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size();
henrika5588a132016-10-18 05:14:30 -0700287 }
288
henrika49810512016-08-22 05:56:12 -0700289 size_t num_samples_out(0);
henrikaf5022222016-11-07 15:56:59 +0100290 // It is currently supported to start playout without a valid audio
291 // transport object. Leads to warning and silence.
292 if (!audio_transport_cb_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100293 RTC_LOG(LS_WARNING) << "Invalid audio transport";
henrikaf5022222016-11-07 15:56:59 +0100294 return 0;
295 }
henrikaba156cf2016-10-31 08:18:50 -0700296
henrikaf5022222016-11-07 15:56:59 +0100297 // Retrieve new 16-bit PCM audio data using the audio transport instance.
298 int64_t elapsed_time_ms = -1;
299 int64_t ntp_time_ms = -1;
henrika51e96082016-11-10 00:40:37 -0800300 const size_t bytes_per_frame = play_channels_ * sizeof(int16_t);
henrikaf5022222016-11-07 15:56:59 +0100301 uint32_t res = audio_transport_cb_->NeedMorePlayData(
henrika51e96082016-11-10 00:40:37 -0800302 samples_per_channel, bytes_per_frame, play_channels_, play_sample_rate_,
henrikaf5022222016-11-07 15:56:59 +0100303 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms);
304 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100305 RTC_LOG(LS_ERROR) << "NeedMorePlayData() failed";
henrika0fd68012016-07-04 13:01:19 +0200306 }
307
henrika3355f6d2016-10-21 12:45:25 +0200308 // Derive a new level value twice per second.
309 int16_t max_abs = 0;
310 RTC_DCHECK_LT(play_stat_count_, 50);
311 if (++play_stat_count_ >= 50) {
henrika3355f6d2016-10-21 12:45:25 +0200312 // Returns the largest absolute value in a signed 16-bit vector.
henrika51e96082016-11-10 00:40:37 -0800313 max_abs =
314 WebRtcSpl_MaxAbsValueW16(play_buffer_.data(), play_buffer_.size());
henrika3355f6d2016-10-21 12:45:25 +0200315 play_stat_count_ = 0;
316 }
henrika87d11cd2017-02-08 07:16:56 -0800317 // Update playout stats which is used as base for periodic logging of the
318 // audio output state.
henrika76535de2017-09-11 01:25:55 -0700319 UpdatePlayStats(max_abs, num_samples_out / play_channels_);
320 return static_cast<int32_t>(num_samples_out / play_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000321}
322
henrika49810512016-08-22 05:56:12 -0700323int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) {
kwibergaf476c72016-11-28 15:21:39 -0800324 RTC_DCHECK_GT(play_buffer_.size(), 0);
henrika7be78832017-06-13 17:34:16 +0200325#ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE
326 const double phase_increment =
327 k2Pi * 440.0 / static_cast<double>(play_sample_rate_);
328 int16_t* destination_r = reinterpret_cast<int16_t*>(audio_buffer);
henrika29e865a2018-04-24 13:22:31 +0200329 if (play_channels_ == 1) {
330 for (size_t i = 0; i < play_buffer_.size(); ++i) {
331 destination_r[i] = static_cast<int16_t>((sin(phase_) * (1 << 14)));
332 phase_ += phase_increment;
333 }
334 } else if (play_channels_ == 2) {
335 for (size_t i = 0; i < play_buffer_.size() / 2; ++i) {
336 destination_r[2 * i] = destination_r[2 * i + 1] =
337 static_cast<int16_t>((sin(phase_) * (1 << 14)));
338 phase_ += phase_increment;
339 }
henrika7be78832017-06-13 17:34:16 +0200340 }
341#else
henrika51e96082016-11-10 00:40:37 -0800342 memcpy(audio_buffer, play_buffer_.data(),
henrika7be78832017-06-13 17:34:16 +0200343 play_buffer_.size() * sizeof(int16_t));
344#endif
henrika51e96082016-11-10 00:40:37 -0800345 // Return samples per channel or number of frames.
346 return static_cast<int32_t>(play_buffer_.size() / play_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000347}
348
henrikaba156cf2016-10-31 08:18:50 -0700349void AudioDeviceBuffer::StartPeriodicLogging() {
350 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
351 AudioDeviceBuffer::LOG_START));
henrika6c4d0f02016-07-14 05:54:19 -0700352}
353
henrikaba156cf2016-10-31 08:18:50 -0700354void AudioDeviceBuffer::StopPeriodicLogging() {
355 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
356 AudioDeviceBuffer::LOG_STOP));
357}
358
359void AudioDeviceBuffer::LogStats(LogState state) {
henrikaf5022222016-11-07 15:56:59 +0100360 RTC_DCHECK_RUN_ON(&task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700361 int64_t now_time = rtc::TimeMillis();
henrika0b3a6382016-11-11 02:28:50 -0800362
henrikaba156cf2016-10-31 08:18:50 -0700363 if (state == AudioDeviceBuffer::LOG_START) {
364 // Reset counters at start. We will not add any logging in this state but
365 // the timer will started by posting a new (delayed) task.
366 num_stat_reports_ = 0;
367 last_timer_task_time_ = now_time;
henrika0b3a6382016-11-11 02:28:50 -0800368 log_stats_ = true;
henrikaba156cf2016-10-31 08:18:50 -0700369 } else if (state == AudioDeviceBuffer::LOG_STOP) {
370 // Stop logging and posting new tasks.
henrika0b3a6382016-11-11 02:28:50 -0800371 log_stats_ = false;
henrikaba156cf2016-10-31 08:18:50 -0700372 } else if (state == AudioDeviceBuffer::LOG_ACTIVE) {
henrika0b3a6382016-11-11 02:28:50 -0800373 // Keep logging unless logging was disabled while task was posted.
374 }
375
376 // Avoid adding more logs since we are in STOP mode.
377 if (!log_stats_) {
378 return;
henrikaba156cf2016-10-31 08:18:50 -0700379 }
henrika6c4d0f02016-07-14 05:54:19 -0700380
henrikaba156cf2016-10-31 08:18:50 -0700381 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds;
382 int64_t time_since_last = rtc::TimeDiff(now_time, last_timer_task_time_);
383 last_timer_task_time_ = now_time;
384
henrika87d11cd2017-02-08 07:16:56 -0800385 Stats stats;
386 {
387 rtc::CritScope cs(&lock_);
388 stats = stats_;
389 stats_.max_rec_level = 0;
390 stats_.max_play_level = 0;
391 }
392
henrikacfbd26d2018-09-05 11:36:22 +0200393 // Cache current sample rate from atomic members.
394 const uint32_t rec_sample_rate = rec_sample_rate_;
395 const uint32_t play_sample_rate = play_sample_rate_;
396
397 // Log the latest statistics but skip the first two rounds just after state
398 // was set to LOG_START to ensure that we have at least one full stable
399 // 10-second interval for sample-rate estimation. Hence, first printed log
400 // will be after ~20 seconds.
henrikac5fe1662018-09-13 16:57:01 +0200401 if (++num_stat_reports_ > 2 &&
402 static_cast<size_t>(time_since_last) > kTimerIntervalInMilliseconds / 2) {
henrika87d11cd2017-02-08 07:16:56 -0800403 uint32_t diff_samples = stats.rec_samples - last_stats_.rec_samples;
henrikaa6d26ec2016-09-20 04:44:04 -0700404 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
henrikacfbd26d2018-09-05 11:36:22 +0200405 uint32_t abs_diff_rate_in_percent = 0;
henrikaf1239b52018-09-25 15:39:22 +0200406 if (rec_sample_rate > 0 && rate > 0) {
henrikacfbd26d2018-09-05 11:36:22 +0200407 abs_diff_rate_in_percent = static_cast<uint32_t>(
408 0.5f +
409 ((100.0f * std::abs(rate - rec_sample_rate)) / rec_sample_rate));
410 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Audio.RecordSampleRateOffsetInPercent",
411 abs_diff_rate_in_percent);
henrikac5fe1662018-09-13 16:57:01 +0200412 RTC_LOG(INFO) << "[REC : " << time_since_last << "msec, "
413 << rec_sample_rate / 1000 << "kHz] callbacks: "
414 << stats.rec_callbacks - last_stats_.rec_callbacks << ", "
415 << "samples: " << diff_samples << ", "
416 << "rate: " << static_cast<int>(rate + 0.5) << ", "
417 << "rate diff: " << abs_diff_rate_in_percent << "%, "
418 << "level: " << stats.max_rec_level;
henrikacfbd26d2018-09-05 11:36:22 +0200419 }
henrika6c4d0f02016-07-14 05:54:19 -0700420
henrika87d11cd2017-02-08 07:16:56 -0800421 diff_samples = stats.play_samples - last_stats_.play_samples;
henrikaa6d26ec2016-09-20 04:44:04 -0700422 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
henrikacfbd26d2018-09-05 11:36:22 +0200423 abs_diff_rate_in_percent = 0;
henrikaf1239b52018-09-25 15:39:22 +0200424 if (play_sample_rate > 0 && rate > 0) {
henrikacfbd26d2018-09-05 11:36:22 +0200425 abs_diff_rate_in_percent = static_cast<uint32_t>(
426 0.5f +
427 ((100.0f * std::abs(rate - play_sample_rate)) / play_sample_rate));
428 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Audio.PlayoutSampleRateOffsetInPercent",
429 abs_diff_rate_in_percent);
henrikac5fe1662018-09-13 16:57:01 +0200430 RTC_LOG(INFO) << "[PLAY: " << time_since_last << "msec, "
431 << play_sample_rate / 1000 << "kHz] callbacks: "
432 << stats.play_callbacks - last_stats_.play_callbacks << ", "
433 << "samples: " << diff_samples << ", "
434 << "rate: " << static_cast<int>(rate + 0.5) << ", "
435 << "rate diff: " << abs_diff_rate_in_percent << "%, "
436 << "level: " << stats.max_play_level;
henrikacfbd26d2018-09-05 11:36:22 +0200437 }
henrikaf06f35a2016-09-09 14:23:11 +0200438 }
henrikacfbd26d2018-09-05 11:36:22 +0200439 last_stats_ = stats;
henrikaf06f35a2016-09-09 14:23:11 +0200440
henrika6c4d0f02016-07-14 05:54:19 -0700441 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis();
442 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval";
443
henrikaba156cf2016-10-31 08:18:50 -0700444 // Keep posting new (delayed) tasks until state is changed to kLogStop.
445 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
446 AudioDeviceBuffer::LOG_ACTIVE),
henrika6c4d0f02016-07-14 05:54:19 -0700447 time_to_wait_ms);
448}
449
henrikaf06f35a2016-09-09 14:23:11 +0200450void AudioDeviceBuffer::ResetRecStats() {
henrikaf5022222016-11-07 15:56:59 +0100451 RTC_DCHECK_RUN_ON(&task_queue_);
henrika87d11cd2017-02-08 07:16:56 -0800452 last_stats_.ResetRecStats();
453 rtc::CritScope cs(&lock_);
454 stats_.ResetRecStats();
henrikaf06f35a2016-09-09 14:23:11 +0200455}
456
457void AudioDeviceBuffer::ResetPlayStats() {
henrikaf5022222016-11-07 15:56:59 +0100458 RTC_DCHECK_RUN_ON(&task_queue_);
henrika87d11cd2017-02-08 07:16:56 -0800459 last_stats_.ResetPlayStats();
460 rtc::CritScope cs(&lock_);
461 stats_.ResetPlayStats();
henrikaf06f35a2016-09-09 14:23:11 +0200462}
463
henrika51e96082016-11-10 00:40:37 -0800464void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs,
465 size_t samples_per_channel) {
henrika87d11cd2017-02-08 07:16:56 -0800466 rtc::CritScope cs(&lock_);
467 ++stats_.rec_callbacks;
468 stats_.rec_samples += samples_per_channel;
469 if (max_abs > stats_.max_rec_level) {
470 stats_.max_rec_level = max_abs;
henrikaf06f35a2016-09-09 14:23:11 +0200471 }
henrika6c4d0f02016-07-14 05:54:19 -0700472}
473
henrika51e96082016-11-10 00:40:37 -0800474void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs,
475 size_t samples_per_channel) {
henrika87d11cd2017-02-08 07:16:56 -0800476 rtc::CritScope cs(&lock_);
477 ++stats_.play_callbacks;
478 stats_.play_samples += samples_per_channel;
479 if (max_abs > stats_.max_play_level) {
480 stats_.max_play_level = max_abs;
henrikaf06f35a2016-09-09 14:23:11 +0200481 }
henrika6c4d0f02016-07-14 05:54:19 -0700482}
483
niklase@google.com470e71d2011-07-07 08:21:25 +0000484} // namespace webrtc