blob: 052071a7fa2d1f373d6cce70642ef8e7f696be13 [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
henrika0fd68012016-07-04 13:01:19 +020042AudioDeviceBuffer::AudioDeviceBuffer()
henrikaf5022222016-11-07 15:56:59 +010043 : task_queue_(kTimerQueueName),
44 audio_transport_cb_(nullptr),
henrika49810512016-08-22 05:56:12 -070045 rec_sample_rate_(0),
46 play_sample_rate_(0),
47 rec_channels_(0),
48 play_channels_(0),
henrikaf5022222016-11-07 15:56:59 +010049 playing_(false),
50 recording_(false),
henrika49810512016-08-22 05:56:12 -070051 typing_status_(false),
52 play_delay_ms_(0),
53 rec_delay_ms_(0),
henrika6c4d0f02016-07-14 05:54:19 -070054 num_stat_reports_(0),
henrikaf5022222016-11-07 15:56:59 +010055 last_timer_task_time_(0),
henrika3355f6d2016-10-21 12:45:25 +020056 rec_stat_count_(0),
henrikaba156cf2016-10-31 08:18:50 -070057 play_stat_count_(0),
58 play_start_time_(0),
henrika0b3a6382016-11-11 02:28:50 -080059 only_silence_recorded_(true),
60 log_stats_(false) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010061 RTC_LOG(INFO) << "AudioDeviceBuffer::ctor";
henrika7be78832017-06-13 17:34:16 +020062#ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE
63 phase_ = 0.0;
Mirko Bonadei675513b2017-11-09 11:09:25 +010064 RTC_LOG(WARNING) << "AUDIO_DEVICE_PLAYS_SINUS_TONE is defined!";
henrika7be78832017-06-13 17:34:16 +020065#endif
henrika4af73662017-10-11 13:16:17 +020066 WebRtcSpl_Init();
niklase@google.com470e71d2011-07-07 08:21:25 +000067}
68
henrika0fd68012016-07-04 13:01:19 +020069AudioDeviceBuffer::~AudioDeviceBuffer() {
henrikaf5022222016-11-07 15:56:59 +010070 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -070071 RTC_DCHECK(!playing_);
72 RTC_DCHECK(!recording_);
Mirko Bonadei675513b2017-11-09 11:09:25 +010073 RTC_LOG(INFO) << "AudioDeviceBuffer::~dtor";
niklase@google.com470e71d2011-07-07 08:21:25 +000074}
75
henrika0fd68012016-07-04 13:01:19 +020076int32_t AudioDeviceBuffer::RegisterAudioCallback(
henrika49810512016-08-22 05:56:12 -070077 AudioTransport* audio_callback) {
henrikaf5022222016-11-07 15:56:59 +010078 RTC_DCHECK_RUN_ON(&main_thread_checker_);
Mirko Bonadei675513b2017-11-09 11:09:25 +010079 RTC_LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +010080 if (playing_ || recording_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010081 RTC_LOG(LS_ERROR) << "Failed to set audio transport since media was active";
henrikaf5022222016-11-07 15:56:59 +010082 return -1;
83 }
henrika49810512016-08-22 05:56:12 -070084 audio_transport_cb_ = audio_callback;
henrika0fd68012016-07-04 13:01:19 +020085 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000086}
87
henrikaba156cf2016-10-31 08:18:50 -070088void AudioDeviceBuffer::StartPlayout() {
henrikaf5022222016-11-07 15:56:59 +010089 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -070090 // TODO(henrika): allow for usage of DCHECK(!playing_) here instead. Today the
91 // ADM allows calling Start(), Start() by ignoring the second call but it
92 // makes more sense to only allow one call.
93 if (playing_) {
94 return;
henrika6c4d0f02016-07-14 05:54:19 -070095 }
Mirko Bonadei675513b2017-11-09 11:09:25 +010096 RTC_LOG(INFO) << __FUNCTION__;
henrikaba156cf2016-10-31 08:18:50 -070097 // Clear members tracking playout stats and do it on the task queue.
98 task_queue_.PostTask([this] { ResetPlayStats(); });
99 // Start a periodic timer based on task queue if not already done by the
100 // recording side.
101 if (!recording_) {
102 StartPeriodicLogging();
103 }
nissedeb95f32016-11-28 01:54:54 -0800104 const int64_t now_time = rtc::TimeMillis();
henrikaba156cf2016-10-31 08:18:50 -0700105 // Clear members that are only touched on the main (creating) thread.
106 play_start_time_ = now_time;
henrikaba156cf2016-10-31 08:18:50 -0700107 playing_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000108}
109
henrikaba156cf2016-10-31 08:18:50 -0700110void AudioDeviceBuffer::StartRecording() {
henrikaf5022222016-11-07 15:56:59 +0100111 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700112 if (recording_) {
113 return;
henrika6c4d0f02016-07-14 05:54:19 -0700114 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100115 RTC_LOG(INFO) << __FUNCTION__;
henrikaba156cf2016-10-31 08:18:50 -0700116 // Clear members tracking recording stats and do it on the task queue.
117 task_queue_.PostTask([this] { ResetRecStats(); });
118 // Start a periodic timer based on task queue if not already done by the
119 // playout side.
120 if (!playing_) {
121 StartPeriodicLogging();
122 }
123 // Clear members that will be touched on the main (creating) thread.
124 rec_start_time_ = rtc::TimeMillis();
125 recording_ = true;
126 // And finally a member which can be modified on the native audio thread.
127 // It is safe to do so since we know by design that the owning ADM has not
128 // yet started the native audio recording.
129 only_silence_recorded_ = true;
130}
131
132void AudioDeviceBuffer::StopPlayout() {
henrikaf5022222016-11-07 15:56:59 +0100133 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700134 if (!playing_) {
135 return;
136 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100137 RTC_LOG(INFO) << __FUNCTION__;
henrikaba156cf2016-10-31 08:18:50 -0700138 playing_ = false;
139 // Stop periodic logging if no more media is active.
140 if (!recording_) {
141 StopPeriodicLogging();
142 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100143 RTC_LOG(INFO) << "total playout time: " << rtc::TimeSince(play_start_time_);
henrikaba156cf2016-10-31 08:18:50 -0700144}
145
146void AudioDeviceBuffer::StopRecording() {
henrikaf5022222016-11-07 15:56:59 +0100147 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700148 if (!recording_) {
149 return;
150 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100151 RTC_LOG(INFO) << __FUNCTION__;
henrikaba156cf2016-10-31 08:18:50 -0700152 recording_ = false;
153 // Stop periodic logging if no more media is active.
154 if (!playing_) {
155 StopPeriodicLogging();
156 }
157 // Add UMA histogram to keep track of the case when only zeros have been
158 // recorded. Measurements (max of absolute level) are taken twice per second,
159 // which means that if e.g 10 seconds of audio has been recorded, a total of
160 // 20 level estimates must all be identical to zero to trigger the histogram.
161 // |only_silence_recorded_| can only be cleared on the native audio thread
162 // that drives audio capture but we know by design that the audio has stopped
163 // when this method is called, hence there should not be aby conflicts. Also,
164 // the fact that |only_silence_recorded_| can be affected during the complete
165 // call makes chances of conflicts with potentially one last callback very
166 // small.
167 const size_t time_since_start = rtc::TimeSince(rec_start_time_);
168 if (time_since_start > kMinValidCallTimeTimeInMilliseconds) {
169 const int only_zeros = static_cast<int>(only_silence_recorded_);
170 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", only_zeros);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100171 RTC_LOG(INFO) << "HISTOGRAM(WebRTC.Audio.RecordedOnlyZeros): "
172 << only_zeros;
henrikaba156cf2016-10-31 08:18:50 -0700173 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100174 RTC_LOG(INFO) << "total recording time: " << time_since_start;
niklase@google.com470e71d2011-07-07 08:21:25 +0000175}
176
henrika0fd68012016-07-04 13:01:19 +0200177int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100178 RTC_LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")";
henrika49810512016-08-22 05:56:12 -0700179 rec_sample_rate_ = fsHz;
henrika0fd68012016-07-04 13:01:19 +0200180 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000181}
182
henrika0fd68012016-07-04 13:01:19 +0200183int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100184 RTC_LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")";
henrika49810512016-08-22 05:56:12 -0700185 play_sample_rate_ = fsHz;
henrika0fd68012016-07-04 13:01:19 +0200186 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000187}
188
henrikacfbd26d2018-09-05 11:36:22 +0200189uint32_t AudioDeviceBuffer::RecordingSampleRate() const {
henrika49810512016-08-22 05:56:12 -0700190 return rec_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000191}
192
henrikacfbd26d2018-09-05 11:36:22 +0200193uint32_t AudioDeviceBuffer::PlayoutSampleRate() const {
henrika49810512016-08-22 05:56:12 -0700194 return play_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000195}
196
henrika0fd68012016-07-04 13:01:19 +0200197int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100198 RTC_LOG(INFO) << "SetRecordingChannels(" << channels << ")";
henrika49810512016-08-22 05:56:12 -0700199 rec_channels_ = channels;
henrika0fd68012016-07-04 13:01:19 +0200200 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000201}
202
henrika0fd68012016-07-04 13:01:19 +0200203int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100204 RTC_LOG(INFO) << "SetPlayoutChannels(" << channels << ")";
henrika49810512016-08-22 05:56:12 -0700205 play_channels_ = channels;
henrika0fd68012016-07-04 13:01:19 +0200206 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000207}
208
henrika0fd68012016-07-04 13:01:19 +0200209size_t AudioDeviceBuffer::RecordingChannels() const {
henrika49810512016-08-22 05:56:12 -0700210 return rec_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000211}
212
henrika0fd68012016-07-04 13:01:19 +0200213size_t AudioDeviceBuffer::PlayoutChannels() const {
henrika49810512016-08-22 05:56:12 -0700214 return play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000215}
216
henrika49810512016-08-22 05:56:12 -0700217int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) {
218 typing_status_ = typing_status;
henrika0fd68012016-07-04 13:01:19 +0200219 return 0;
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +0000220}
221
Yves Gerey665174f2018-06-19 15:03:05 +0200222void AudioDeviceBuffer::SetVQEData(int play_delay_ms, int rec_delay_ms) {
henrika49810512016-08-22 05:56:12 -0700223 play_delay_ms_ = play_delay_ms;
224 rec_delay_ms_ = rec_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000225}
226
henrika49810512016-08-22 05:56:12 -0700227int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer,
henrika51e96082016-11-10 00:40:37 -0800228 size_t samples_per_channel) {
henrika5588a132016-10-18 05:14:30 -0700229 // Copy the complete input buffer to the local buffer.
henrika5588a132016-10-18 05:14:30 -0700230 const size_t old_size = rec_buffer_.size();
henrika51e96082016-11-10 00:40:37 -0800231 rec_buffer_.SetData(static_cast<const int16_t*>(audio_buffer),
232 rec_channels_ * samples_per_channel);
henrika5588a132016-10-18 05:14:30 -0700233 // Keep track of the size of the recording buffer. Only updated when the
234 // size changes, which is a rare event.
235 if (old_size != rec_buffer_.size()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100236 RTC_LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size();
henrika0fd68012016-07-04 13:01:19 +0200237 }
henrika51e96082016-11-10 00:40:37 -0800238
henrikaba156cf2016-10-31 08:18:50 -0700239 // Derive a new level value twice per second and check if it is non-zero.
henrika3355f6d2016-10-21 12:45:25 +0200240 int16_t max_abs = 0;
241 RTC_DCHECK_LT(rec_stat_count_, 50);
242 if (++rec_stat_count_ >= 50) {
henrika3355f6d2016-10-21 12:45:25 +0200243 // Returns the largest absolute value in a signed 16-bit vector.
henrika51e96082016-11-10 00:40:37 -0800244 max_abs = WebRtcSpl_MaxAbsValueW16(rec_buffer_.data(), rec_buffer_.size());
henrika3355f6d2016-10-21 12:45:25 +0200245 rec_stat_count_ = 0;
henrikaba156cf2016-10-31 08:18:50 -0700246 // Set |only_silence_recorded_| to false as soon as at least one detection
247 // of a non-zero audio packet is found. It can only be restored to true
248 // again by restarting the call.
249 if (max_abs > 0) {
250 only_silence_recorded_ = false;
251 }
henrika3355f6d2016-10-21 12:45:25 +0200252 }
henrika87d11cd2017-02-08 07:16:56 -0800253 // Update recording stats which is used as base for periodic logging of the
254 // audio input state.
255 UpdateRecStats(max_abs, samples_per_channel);
henrika0fd68012016-07-04 13:01:19 +0200256 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000257}
258
henrika0fd68012016-07-04 13:01:19 +0200259int32_t AudioDeviceBuffer::DeliverRecordedData() {
henrika49810512016-08-22 05:56:12 -0700260 if (!audio_transport_cb_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100261 RTC_LOG(LS_WARNING) << "Invalid audio transport";
niklase@google.com470e71d2011-07-07 08:21:25 +0000262 return 0;
henrika0fd68012016-07-04 13:01:19 +0200263 }
henrika51e96082016-11-10 00:40:37 -0800264 const size_t frames = rec_buffer_.size() / rec_channels_;
265 const size_t bytes_per_frame = rec_channels_ * sizeof(int16_t);
Fredrik Solenberg1a50cd52018-01-16 09:19:38 +0100266 uint32_t new_mic_level_dummy = 0;
henrika5588a132016-10-18 05:14:30 -0700267 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_;
henrika5588a132016-10-18 05:14:30 -0700268 int32_t res = audio_transport_cb_->RecordedDataIsAvailable(
henrika51e96082016-11-10 00:40:37 -0800269 rec_buffer_.data(), frames, bytes_per_frame, rec_channels_,
Fredrik Solenberg1a50cd52018-01-16 09:19:38 +0100270 rec_sample_rate_, total_delay_ms, 0, 0, typing_status_,
271 new_mic_level_dummy);
272 if (res == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100273 RTC_LOG(LS_ERROR) << "RecordedDataIsAvailable() failed";
henrika0fd68012016-07-04 13:01:19 +0200274 }
henrika0fd68012016-07-04 13:01:19 +0200275 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000276}
277
henrika51e96082016-11-10 00:40:37 -0800278int32_t AudioDeviceBuffer::RequestPlayoutData(size_t samples_per_channel) {
henrika51e96082016-11-10 00:40:37 -0800279 // The consumer can change the requested size on the fly and we therefore
henrika5588a132016-10-18 05:14:30 -0700280 // resize the buffer accordingly. Also takes place at the first call to this
281 // method.
henrika51e96082016-11-10 00:40:37 -0800282 const size_t total_samples = play_channels_ * samples_per_channel;
283 if (play_buffer_.size() != total_samples) {
284 play_buffer_.SetSize(total_samples);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100285 RTC_LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size();
henrika5588a132016-10-18 05:14:30 -0700286 }
287
henrika49810512016-08-22 05:56:12 -0700288 size_t num_samples_out(0);
henrikaf5022222016-11-07 15:56:59 +0100289 // It is currently supported to start playout without a valid audio
290 // transport object. Leads to warning and silence.
291 if (!audio_transport_cb_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100292 RTC_LOG(LS_WARNING) << "Invalid audio transport";
henrikaf5022222016-11-07 15:56:59 +0100293 return 0;
294 }
henrikaba156cf2016-10-31 08:18:50 -0700295
henrikaf5022222016-11-07 15:56:59 +0100296 // Retrieve new 16-bit PCM audio data using the audio transport instance.
297 int64_t elapsed_time_ms = -1;
298 int64_t ntp_time_ms = -1;
henrika51e96082016-11-10 00:40:37 -0800299 const size_t bytes_per_frame = play_channels_ * sizeof(int16_t);
henrikaf5022222016-11-07 15:56:59 +0100300 uint32_t res = audio_transport_cb_->NeedMorePlayData(
henrika51e96082016-11-10 00:40:37 -0800301 samples_per_channel, bytes_per_frame, play_channels_, play_sample_rate_,
henrikaf5022222016-11-07 15:56:59 +0100302 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms);
303 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100304 RTC_LOG(LS_ERROR) << "NeedMorePlayData() failed";
henrika0fd68012016-07-04 13:01:19 +0200305 }
306
henrika3355f6d2016-10-21 12:45:25 +0200307 // Derive a new level value twice per second.
308 int16_t max_abs = 0;
309 RTC_DCHECK_LT(play_stat_count_, 50);
310 if (++play_stat_count_ >= 50) {
henrika3355f6d2016-10-21 12:45:25 +0200311 // Returns the largest absolute value in a signed 16-bit vector.
henrika51e96082016-11-10 00:40:37 -0800312 max_abs =
313 WebRtcSpl_MaxAbsValueW16(play_buffer_.data(), play_buffer_.size());
henrika3355f6d2016-10-21 12:45:25 +0200314 play_stat_count_ = 0;
315 }
henrika87d11cd2017-02-08 07:16:56 -0800316 // Update playout stats which is used as base for periodic logging of the
317 // audio output state.
henrika76535de2017-09-11 01:25:55 -0700318 UpdatePlayStats(max_abs, num_samples_out / play_channels_);
319 return static_cast<int32_t>(num_samples_out / play_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000320}
321
henrika49810512016-08-22 05:56:12 -0700322int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) {
kwibergaf476c72016-11-28 15:21:39 -0800323 RTC_DCHECK_GT(play_buffer_.size(), 0);
henrika7be78832017-06-13 17:34:16 +0200324#ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE
325 const double phase_increment =
326 k2Pi * 440.0 / static_cast<double>(play_sample_rate_);
327 int16_t* destination_r = reinterpret_cast<int16_t*>(audio_buffer);
henrika29e865a2018-04-24 13:22:31 +0200328 if (play_channels_ == 1) {
329 for (size_t i = 0; i < play_buffer_.size(); ++i) {
330 destination_r[i] = static_cast<int16_t>((sin(phase_) * (1 << 14)));
331 phase_ += phase_increment;
332 }
333 } else if (play_channels_ == 2) {
334 for (size_t i = 0; i < play_buffer_.size() / 2; ++i) {
335 destination_r[2 * i] = destination_r[2 * i + 1] =
336 static_cast<int16_t>((sin(phase_) * (1 << 14)));
337 phase_ += phase_increment;
338 }
henrika7be78832017-06-13 17:34:16 +0200339 }
340#else
henrika51e96082016-11-10 00:40:37 -0800341 memcpy(audio_buffer, play_buffer_.data(),
henrika7be78832017-06-13 17:34:16 +0200342 play_buffer_.size() * sizeof(int16_t));
343#endif
henrika51e96082016-11-10 00:40:37 -0800344 // Return samples per channel or number of frames.
345 return static_cast<int32_t>(play_buffer_.size() / play_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000346}
347
henrikaba156cf2016-10-31 08:18:50 -0700348void AudioDeviceBuffer::StartPeriodicLogging() {
349 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
350 AudioDeviceBuffer::LOG_START));
henrika6c4d0f02016-07-14 05:54:19 -0700351}
352
henrikaba156cf2016-10-31 08:18:50 -0700353void AudioDeviceBuffer::StopPeriodicLogging() {
354 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
355 AudioDeviceBuffer::LOG_STOP));
356}
357
358void AudioDeviceBuffer::LogStats(LogState state) {
henrikaf5022222016-11-07 15:56:59 +0100359 RTC_DCHECK_RUN_ON(&task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700360 int64_t now_time = rtc::TimeMillis();
henrika0b3a6382016-11-11 02:28:50 -0800361
henrikaba156cf2016-10-31 08:18:50 -0700362 if (state == AudioDeviceBuffer::LOG_START) {
363 // Reset counters at start. We will not add any logging in this state but
364 // the timer will started by posting a new (delayed) task.
365 num_stat_reports_ = 0;
366 last_timer_task_time_ = now_time;
henrika0b3a6382016-11-11 02:28:50 -0800367 log_stats_ = true;
henrikaba156cf2016-10-31 08:18:50 -0700368 } else if (state == AudioDeviceBuffer::LOG_STOP) {
369 // Stop logging and posting new tasks.
henrika0b3a6382016-11-11 02:28:50 -0800370 log_stats_ = false;
henrikaba156cf2016-10-31 08:18:50 -0700371 } else if (state == AudioDeviceBuffer::LOG_ACTIVE) {
henrika0b3a6382016-11-11 02:28:50 -0800372 // Keep logging unless logging was disabled while task was posted.
373 }
374
375 // Avoid adding more logs since we are in STOP mode.
376 if (!log_stats_) {
377 return;
henrikaba156cf2016-10-31 08:18:50 -0700378 }
henrika6c4d0f02016-07-14 05:54:19 -0700379
henrikaba156cf2016-10-31 08:18:50 -0700380 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds;
381 int64_t time_since_last = rtc::TimeDiff(now_time, last_timer_task_time_);
382 last_timer_task_time_ = now_time;
383
henrika87d11cd2017-02-08 07:16:56 -0800384 Stats stats;
385 {
386 rtc::CritScope cs(&lock_);
387 stats = stats_;
388 stats_.max_rec_level = 0;
389 stats_.max_play_level = 0;
390 }
391
henrikacfbd26d2018-09-05 11:36:22 +0200392 // Cache current sample rate from atomic members.
393 const uint32_t rec_sample_rate = rec_sample_rate_;
394 const uint32_t play_sample_rate = play_sample_rate_;
395
396 // Log the latest statistics but skip the first two rounds just after state
397 // was set to LOG_START to ensure that we have at least one full stable
398 // 10-second interval for sample-rate estimation. Hence, first printed log
399 // will be after ~20 seconds.
henrikac5fe1662018-09-13 16:57:01 +0200400 if (++num_stat_reports_ > 2 &&
401 static_cast<size_t>(time_since_last) > kTimerIntervalInMilliseconds / 2) {
henrika87d11cd2017-02-08 07:16:56 -0800402 uint32_t diff_samples = stats.rec_samples - last_stats_.rec_samples;
henrikaa6d26ec2016-09-20 04:44:04 -0700403 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
henrikacfbd26d2018-09-05 11:36:22 +0200404 uint32_t abs_diff_rate_in_percent = 0;
henrikaf1239b52018-09-25 15:39:22 +0200405 if (rec_sample_rate > 0 && rate > 0) {
henrikacfbd26d2018-09-05 11:36:22 +0200406 abs_diff_rate_in_percent = static_cast<uint32_t>(
407 0.5f +
408 ((100.0f * std::abs(rate - rec_sample_rate)) / rec_sample_rate));
409 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Audio.RecordSampleRateOffsetInPercent",
410 abs_diff_rate_in_percent);
henrikac5fe1662018-09-13 16:57:01 +0200411 RTC_LOG(INFO) << "[REC : " << time_since_last << "msec, "
412 << rec_sample_rate / 1000 << "kHz] callbacks: "
413 << stats.rec_callbacks - last_stats_.rec_callbacks << ", "
414 << "samples: " << diff_samples << ", "
415 << "rate: " << static_cast<int>(rate + 0.5) << ", "
416 << "rate diff: " << abs_diff_rate_in_percent << "%, "
417 << "level: " << stats.max_rec_level;
henrikacfbd26d2018-09-05 11:36:22 +0200418 }
henrika6c4d0f02016-07-14 05:54:19 -0700419
henrika87d11cd2017-02-08 07:16:56 -0800420 diff_samples = stats.play_samples - last_stats_.play_samples;
henrikaa6d26ec2016-09-20 04:44:04 -0700421 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
henrikacfbd26d2018-09-05 11:36:22 +0200422 abs_diff_rate_in_percent = 0;
henrikaf1239b52018-09-25 15:39:22 +0200423 if (play_sample_rate > 0 && rate > 0) {
henrikacfbd26d2018-09-05 11:36:22 +0200424 abs_diff_rate_in_percent = static_cast<uint32_t>(
425 0.5f +
426 ((100.0f * std::abs(rate - play_sample_rate)) / play_sample_rate));
427 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Audio.PlayoutSampleRateOffsetInPercent",
428 abs_diff_rate_in_percent);
henrikac5fe1662018-09-13 16:57:01 +0200429 RTC_LOG(INFO) << "[PLAY: " << time_since_last << "msec, "
430 << play_sample_rate / 1000 << "kHz] callbacks: "
431 << stats.play_callbacks - last_stats_.play_callbacks << ", "
432 << "samples: " << diff_samples << ", "
433 << "rate: " << static_cast<int>(rate + 0.5) << ", "
434 << "rate diff: " << abs_diff_rate_in_percent << "%, "
435 << "level: " << stats.max_play_level;
henrikacfbd26d2018-09-05 11:36:22 +0200436 }
henrikaf06f35a2016-09-09 14:23:11 +0200437 }
henrikacfbd26d2018-09-05 11:36:22 +0200438 last_stats_ = stats;
henrikaf06f35a2016-09-09 14:23:11 +0200439
henrika6c4d0f02016-07-14 05:54:19 -0700440 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis();
441 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval";
442
henrikaba156cf2016-10-31 08:18:50 -0700443 // Keep posting new (delayed) tasks until state is changed to kLogStop.
444 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
445 AudioDeviceBuffer::LOG_ACTIVE),
henrika6c4d0f02016-07-14 05:54:19 -0700446 time_to_wait_ms);
447}
448
henrikaf06f35a2016-09-09 14:23:11 +0200449void AudioDeviceBuffer::ResetRecStats() {
henrikaf5022222016-11-07 15:56:59 +0100450 RTC_DCHECK_RUN_ON(&task_queue_);
henrika87d11cd2017-02-08 07:16:56 -0800451 last_stats_.ResetRecStats();
452 rtc::CritScope cs(&lock_);
453 stats_.ResetRecStats();
henrikaf06f35a2016-09-09 14:23:11 +0200454}
455
456void AudioDeviceBuffer::ResetPlayStats() {
henrikaf5022222016-11-07 15:56:59 +0100457 RTC_DCHECK_RUN_ON(&task_queue_);
henrika87d11cd2017-02-08 07:16:56 -0800458 last_stats_.ResetPlayStats();
459 rtc::CritScope cs(&lock_);
460 stats_.ResetPlayStats();
henrikaf06f35a2016-09-09 14:23:11 +0200461}
462
henrika51e96082016-11-10 00:40:37 -0800463void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs,
464 size_t samples_per_channel) {
henrika87d11cd2017-02-08 07:16:56 -0800465 rtc::CritScope cs(&lock_);
466 ++stats_.rec_callbacks;
467 stats_.rec_samples += samples_per_channel;
468 if (max_abs > stats_.max_rec_level) {
469 stats_.max_rec_level = max_abs;
henrikaf06f35a2016-09-09 14:23:11 +0200470 }
henrika6c4d0f02016-07-14 05:54:19 -0700471}
472
henrika51e96082016-11-10 00:40:37 -0800473void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs,
474 size_t samples_per_channel) {
henrika87d11cd2017-02-08 07:16:56 -0800475 rtc::CritScope cs(&lock_);
476 ++stats_.play_callbacks;
477 stats_.play_samples += samples_per_channel;
478 if (max_abs > stats_.max_play_level) {
479 stats_.max_play_level = max_abs;
henrikaf06f35a2016-09-09 14:23:11 +0200480 }
henrika6c4d0f02016-07-14 05:54:19 -0700481}
482
niklase@google.com470e71d2011-07-07 08:21:25 +0000483} // namespace webrtc