blob: 3bbc7147ff04358beb130978445f07ab884b0d86 [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>
henrika7be78832017-06-13 17:34:16 +020012#include <cmath>
henrika3d7346f2016-07-29 16:20:47 +020013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/audio_device/audio_device_buffer.h"
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"
17#include "modules/audio_device/audio_device_config.h"
18#include "rtc_base/arraysize.h"
19#include "rtc_base/bind.h"
20#include "rtc_base/checks.h"
21#include "rtc_base/format_macros.h"
22#include "rtc_base/logging.h"
23#include "rtc_base/timeutils.h"
24#include "system_wrappers/include/metrics.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000025
niklase@google.com470e71d2011-07-07 08:21:25 +000026namespace webrtc {
27
henrika6c4d0f02016-07-14 05:54:19 -070028static const char kTimerQueueName[] = "AudioDeviceBufferTimer";
29
30// Time between two sucessive calls to LogStats().
31static const size_t kTimerIntervalInSeconds = 10;
32static const size_t kTimerIntervalInMilliseconds =
33 kTimerIntervalInSeconds * rtc::kNumMillisecsPerSec;
henrikaba156cf2016-10-31 08:18:50 -070034// Min time required to qualify an audio session as a "call". If playout or
35// recording has been active for less than this time we will not store any
36// logs or UMA stats but instead consider the call as too short.
37static const size_t kMinValidCallTimeTimeInSeconds = 10;
38static const size_t kMinValidCallTimeTimeInMilliseconds =
39 kMinValidCallTimeTimeInSeconds * rtc::kNumMillisecsPerSec;
henrika7be78832017-06-13 17:34:16 +020040#ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE
41static const double k2Pi = 6.28318530717959;
42#endif
henrika6c4d0f02016-07-14 05:54:19 -070043
henrika0fd68012016-07-04 13:01:19 +020044AudioDeviceBuffer::AudioDeviceBuffer()
henrikaf5022222016-11-07 15:56:59 +010045 : task_queue_(kTimerQueueName),
46 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
henrika4af73662017-10-11 13:16:17 +020068 WebRtcSpl_Init();
henrikaf5022222016-11-07 15:56:59 +010069 playout_thread_checker_.DetachFromThread();
70 recording_thread_checker_.DetachFromThread();
niklase@google.com470e71d2011-07-07 08:21:25 +000071}
72
henrika0fd68012016-07-04 13:01:19 +020073AudioDeviceBuffer::~AudioDeviceBuffer() {
henrikaf5022222016-11-07 15:56:59 +010074 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -070075 RTC_DCHECK(!playing_);
76 RTC_DCHECK(!recording_);
Mirko Bonadei675513b2017-11-09 11:09:25 +010077 RTC_LOG(INFO) << "AudioDeviceBuffer::~dtor";
niklase@google.com470e71d2011-07-07 08:21:25 +000078}
79
henrika0fd68012016-07-04 13:01:19 +020080int32_t AudioDeviceBuffer::RegisterAudioCallback(
henrika49810512016-08-22 05:56:12 -070081 AudioTransport* audio_callback) {
henrikaf5022222016-11-07 15:56:59 +010082 RTC_DCHECK_RUN_ON(&main_thread_checker_);
Mirko Bonadei675513b2017-11-09 11:09:25 +010083 RTC_LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +010084 if (playing_ || recording_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010085 RTC_LOG(LS_ERROR) << "Failed to set audio transport since media was active";
henrikaf5022222016-11-07 15:56:59 +010086 return -1;
87 }
henrika49810512016-08-22 05:56:12 -070088 audio_transport_cb_ = audio_callback;
henrika0fd68012016-07-04 13:01:19 +020089 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000090}
91
henrikaba156cf2016-10-31 08:18:50 -070092void AudioDeviceBuffer::StartPlayout() {
henrikaf5022222016-11-07 15:56:59 +010093 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -070094 // TODO(henrika): allow for usage of DCHECK(!playing_) here instead. Today the
95 // ADM allows calling Start(), Start() by ignoring the second call but it
96 // makes more sense to only allow one call.
97 if (playing_) {
98 return;
henrika6c4d0f02016-07-14 05:54:19 -070099 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100100 RTC_LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +0100101 playout_thread_checker_.DetachFromThread();
henrikaba156cf2016-10-31 08:18:50 -0700102 // Clear members tracking playout stats and do it on the task queue.
103 task_queue_.PostTask([this] { ResetPlayStats(); });
104 // Start a periodic timer based on task queue if not already done by the
105 // recording side.
106 if (!recording_) {
107 StartPeriodicLogging();
108 }
nissedeb95f32016-11-28 01:54:54 -0800109 const int64_t now_time = rtc::TimeMillis();
henrikaba156cf2016-10-31 08:18:50 -0700110 // Clear members that are only touched on the main (creating) thread.
111 play_start_time_ = now_time;
henrikaba156cf2016-10-31 08:18:50 -0700112 playing_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000113}
114
henrikaba156cf2016-10-31 08:18:50 -0700115void AudioDeviceBuffer::StartRecording() {
henrikaf5022222016-11-07 15:56:59 +0100116 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700117 if (recording_) {
118 return;
henrika6c4d0f02016-07-14 05:54:19 -0700119 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100120 RTC_LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +0100121 recording_thread_checker_.DetachFromThread();
henrikaba156cf2016-10-31 08:18:50 -0700122 // Clear members tracking recording stats and do it on the task queue.
123 task_queue_.PostTask([this] { ResetRecStats(); });
124 // Start a periodic timer based on task queue if not already done by the
125 // playout side.
126 if (!playing_) {
127 StartPeriodicLogging();
128 }
129 // Clear members that will be touched on the main (creating) thread.
130 rec_start_time_ = rtc::TimeMillis();
131 recording_ = true;
132 // And finally a member which can be modified on the native audio thread.
133 // It is safe to do so since we know by design that the owning ADM has not
134 // yet started the native audio recording.
135 only_silence_recorded_ = true;
136}
137
138void AudioDeviceBuffer::StopPlayout() {
henrikaf5022222016-11-07 15:56:59 +0100139 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700140 if (!playing_) {
141 return;
142 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100143 RTC_LOG(INFO) << __FUNCTION__;
henrikaba156cf2016-10-31 08:18:50 -0700144 playing_ = false;
145 // Stop periodic logging if no more media is active.
146 if (!recording_) {
147 StopPeriodicLogging();
148 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100149 RTC_LOG(INFO) << "total playout time: " << rtc::TimeSince(play_start_time_);
henrikaba156cf2016-10-31 08:18:50 -0700150}
151
152void AudioDeviceBuffer::StopRecording() {
henrikaf5022222016-11-07 15:56:59 +0100153 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700154 if (!recording_) {
155 return;
156 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100157 RTC_LOG(INFO) << __FUNCTION__;
henrikaba156cf2016-10-31 08:18:50 -0700158 recording_ = false;
159 // Stop periodic logging if no more media is active.
160 if (!playing_) {
161 StopPeriodicLogging();
162 }
163 // Add UMA histogram to keep track of the case when only zeros have been
164 // recorded. Measurements (max of absolute level) are taken twice per second,
165 // which means that if e.g 10 seconds of audio has been recorded, a total of
166 // 20 level estimates must all be identical to zero to trigger the histogram.
167 // |only_silence_recorded_| can only be cleared on the native audio thread
168 // that drives audio capture but we know by design that the audio has stopped
169 // when this method is called, hence there should not be aby conflicts. Also,
170 // the fact that |only_silence_recorded_| can be affected during the complete
171 // call makes chances of conflicts with potentially one last callback very
172 // small.
173 const size_t time_since_start = rtc::TimeSince(rec_start_time_);
174 if (time_since_start > kMinValidCallTimeTimeInMilliseconds) {
175 const int only_zeros = static_cast<int>(only_silence_recorded_);
176 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", only_zeros);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100177 RTC_LOG(INFO) << "HISTOGRAM(WebRTC.Audio.RecordedOnlyZeros): "
178 << only_zeros;
henrikaba156cf2016-10-31 08:18:50 -0700179 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100180 RTC_LOG(INFO) << "total recording time: " << time_since_start;
niklase@google.com470e71d2011-07-07 08:21:25 +0000181}
182
henrika0fd68012016-07-04 13:01:19 +0200183int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) {
henrikaf5022222016-11-07 15:56:59 +0100184 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100185 RTC_LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")";
henrika49810512016-08-22 05:56:12 -0700186 rec_sample_rate_ = fsHz;
henrika0fd68012016-07-04 13:01:19 +0200187 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000188}
189
henrika0fd68012016-07-04 13:01:19 +0200190int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) {
henrikaf5022222016-11-07 15:56:59 +0100191 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100192 RTC_LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")";
henrika49810512016-08-22 05:56:12 -0700193 play_sample_rate_ = fsHz;
henrika0fd68012016-07-04 13:01:19 +0200194 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000195}
196
henrika0fd68012016-07-04 13:01:19 +0200197int32_t AudioDeviceBuffer::RecordingSampleRate() const {
henrikaf5022222016-11-07 15:56:59 +0100198 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700199 return rec_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000200}
201
henrika0fd68012016-07-04 13:01:19 +0200202int32_t AudioDeviceBuffer::PlayoutSampleRate() const {
henrikaf5022222016-11-07 15:56:59 +0100203 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700204 return play_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205}
206
henrika0fd68012016-07-04 13:01:19 +0200207int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) {
henrikaf5022222016-11-07 15:56:59 +0100208 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100209 RTC_LOG(INFO) << "SetRecordingChannels(" << channels << ")";
henrika49810512016-08-22 05:56:12 -0700210 rec_channels_ = channels;
henrika0fd68012016-07-04 13:01:19 +0200211 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000212}
213
henrika0fd68012016-07-04 13:01:19 +0200214int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) {
henrikaf5022222016-11-07 15:56:59 +0100215 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100216 RTC_LOG(INFO) << "SetPlayoutChannels(" << channels << ")";
henrika49810512016-08-22 05:56:12 -0700217 play_channels_ = channels;
henrika0fd68012016-07-04 13:01:19 +0200218 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000219}
220
henrika0fd68012016-07-04 13:01:19 +0200221size_t AudioDeviceBuffer::RecordingChannels() const {
henrikaf5022222016-11-07 15:56:59 +0100222 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700223 return rec_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000224}
225
henrika0fd68012016-07-04 13:01:19 +0200226size_t AudioDeviceBuffer::PlayoutChannels() const {
henrikaf5022222016-11-07 15:56:59 +0100227 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700228 return play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000229}
230
henrika49810512016-08-22 05:56:12 -0700231int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) {
henrikaf5022222016-11-07 15:56:59 +0100232 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700233 typing_status_ = typing_status;
henrika0fd68012016-07-04 13:01:19 +0200234 return 0;
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +0000235}
236
henrika883d00f2018-03-16 10:09:49 +0100237void AudioDeviceBuffer::NativeAudioPlayoutInterrupted() {
henrika09a76192017-08-23 15:04:40 +0200238 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
239 playout_thread_checker_.DetachFromThread();
henrika883d00f2018-03-16 10:09:49 +0100240}
241
242void AudioDeviceBuffer::NativeAudioRecordingInterrupted() {
243 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika09a76192017-08-23 15:04:40 +0200244 recording_thread_checker_.DetachFromThread();
245}
246
henrika49810512016-08-22 05:56:12 -0700247void AudioDeviceBuffer::SetVQEData(int play_delay_ms,
Fredrik Solenberg1a50cd52018-01-16 09:19:38 +0100248 int rec_delay_ms) {
henrikaf5022222016-11-07 15:56:59 +0100249 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700250 play_delay_ms_ = play_delay_ms;
251 rec_delay_ms_ = rec_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000252}
253
henrika49810512016-08-22 05:56:12 -0700254int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer,
henrika51e96082016-11-10 00:40:37 -0800255 size_t samples_per_channel) {
henrikaf5022222016-11-07 15:56:59 +0100256 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika5588a132016-10-18 05:14:30 -0700257 // Copy the complete input buffer to the local buffer.
henrika5588a132016-10-18 05:14:30 -0700258 const size_t old_size = rec_buffer_.size();
henrika51e96082016-11-10 00:40:37 -0800259 rec_buffer_.SetData(static_cast<const int16_t*>(audio_buffer),
260 rec_channels_ * samples_per_channel);
henrika5588a132016-10-18 05:14:30 -0700261 // Keep track of the size of the recording buffer. Only updated when the
262 // size changes, which is a rare event.
263 if (old_size != rec_buffer_.size()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100264 RTC_LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size();
henrika0fd68012016-07-04 13:01:19 +0200265 }
henrika51e96082016-11-10 00:40:37 -0800266
henrikaba156cf2016-10-31 08:18:50 -0700267 // Derive a new level value twice per second and check if it is non-zero.
henrika3355f6d2016-10-21 12:45:25 +0200268 int16_t max_abs = 0;
269 RTC_DCHECK_LT(rec_stat_count_, 50);
270 if (++rec_stat_count_ >= 50) {
henrika3355f6d2016-10-21 12:45:25 +0200271 // Returns the largest absolute value in a signed 16-bit vector.
henrika51e96082016-11-10 00:40:37 -0800272 max_abs = WebRtcSpl_MaxAbsValueW16(rec_buffer_.data(), rec_buffer_.size());
henrika3355f6d2016-10-21 12:45:25 +0200273 rec_stat_count_ = 0;
henrikaba156cf2016-10-31 08:18:50 -0700274 // Set |only_silence_recorded_| to false as soon as at least one detection
275 // of a non-zero audio packet is found. It can only be restored to true
276 // again by restarting the call.
277 if (max_abs > 0) {
278 only_silence_recorded_ = false;
279 }
henrika3355f6d2016-10-21 12:45:25 +0200280 }
henrika87d11cd2017-02-08 07:16:56 -0800281 // Update recording stats which is used as base for periodic logging of the
282 // audio input state.
283 UpdateRecStats(max_abs, samples_per_channel);
henrika0fd68012016-07-04 13:01:19 +0200284 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000285}
286
henrika0fd68012016-07-04 13:01:19 +0200287int32_t AudioDeviceBuffer::DeliverRecordedData() {
henrikaf5022222016-11-07 15:56:59 +0100288 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700289 if (!audio_transport_cb_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100290 RTC_LOG(LS_WARNING) << "Invalid audio transport";
niklase@google.com470e71d2011-07-07 08:21:25 +0000291 return 0;
henrika0fd68012016-07-04 13:01:19 +0200292 }
henrika51e96082016-11-10 00:40:37 -0800293 const size_t frames = rec_buffer_.size() / rec_channels_;
294 const size_t bytes_per_frame = rec_channels_ * sizeof(int16_t);
Fredrik Solenberg1a50cd52018-01-16 09:19:38 +0100295 uint32_t new_mic_level_dummy = 0;
henrika5588a132016-10-18 05:14:30 -0700296 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_;
henrika5588a132016-10-18 05:14:30 -0700297 int32_t res = audio_transport_cb_->RecordedDataIsAvailable(
henrika51e96082016-11-10 00:40:37 -0800298 rec_buffer_.data(), frames, bytes_per_frame, rec_channels_,
Fredrik Solenberg1a50cd52018-01-16 09:19:38 +0100299 rec_sample_rate_, total_delay_ms, 0, 0, typing_status_,
300 new_mic_level_dummy);
301 if (res == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100302 RTC_LOG(LS_ERROR) << "RecordedDataIsAvailable() failed";
henrika0fd68012016-07-04 13:01:19 +0200303 }
henrika0fd68012016-07-04 13:01:19 +0200304 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000305}
306
henrika51e96082016-11-10 00:40:37 -0800307int32_t AudioDeviceBuffer::RequestPlayoutData(size_t samples_per_channel) {
henrikaf5022222016-11-07 15:56:59 +0100308 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
henrika51e96082016-11-10 00:40:37 -0800309 // The consumer can change the requested size on the fly and we therefore
henrika5588a132016-10-18 05:14:30 -0700310 // resize the buffer accordingly. Also takes place at the first call to this
311 // method.
henrika51e96082016-11-10 00:40:37 -0800312 const size_t total_samples = play_channels_ * samples_per_channel;
313 if (play_buffer_.size() != total_samples) {
314 play_buffer_.SetSize(total_samples);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100315 RTC_LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size();
henrika5588a132016-10-18 05:14:30 -0700316 }
317
henrika49810512016-08-22 05:56:12 -0700318 size_t num_samples_out(0);
henrikaf5022222016-11-07 15:56:59 +0100319 // It is currently supported to start playout without a valid audio
320 // transport object. Leads to warning and silence.
321 if (!audio_transport_cb_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100322 RTC_LOG(LS_WARNING) << "Invalid audio transport";
henrikaf5022222016-11-07 15:56:59 +0100323 return 0;
324 }
henrikaba156cf2016-10-31 08:18:50 -0700325
henrikaf5022222016-11-07 15:56:59 +0100326 // Retrieve new 16-bit PCM audio data using the audio transport instance.
327 int64_t elapsed_time_ms = -1;
328 int64_t ntp_time_ms = -1;
henrika51e96082016-11-10 00:40:37 -0800329 const size_t bytes_per_frame = play_channels_ * sizeof(int16_t);
henrikaf5022222016-11-07 15:56:59 +0100330 uint32_t res = audio_transport_cb_->NeedMorePlayData(
henrika51e96082016-11-10 00:40:37 -0800331 samples_per_channel, bytes_per_frame, play_channels_, play_sample_rate_,
henrikaf5022222016-11-07 15:56:59 +0100332 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms);
333 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100334 RTC_LOG(LS_ERROR) << "NeedMorePlayData() failed";
henrika0fd68012016-07-04 13:01:19 +0200335 }
336
henrika3355f6d2016-10-21 12:45:25 +0200337 // Derive a new level value twice per second.
338 int16_t max_abs = 0;
339 RTC_DCHECK_LT(play_stat_count_, 50);
340 if (++play_stat_count_ >= 50) {
henrika3355f6d2016-10-21 12:45:25 +0200341 // Returns the largest absolute value in a signed 16-bit vector.
henrika51e96082016-11-10 00:40:37 -0800342 max_abs =
343 WebRtcSpl_MaxAbsValueW16(play_buffer_.data(), play_buffer_.size());
henrika3355f6d2016-10-21 12:45:25 +0200344 play_stat_count_ = 0;
345 }
henrika87d11cd2017-02-08 07:16:56 -0800346 // Update playout stats which is used as base for periodic logging of the
347 // audio output state.
henrika76535de2017-09-11 01:25:55 -0700348 UpdatePlayStats(max_abs, num_samples_out / play_channels_);
349 return static_cast<int32_t>(num_samples_out / play_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000350}
351
henrika49810512016-08-22 05:56:12 -0700352int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) {
henrikaf5022222016-11-07 15:56:59 +0100353 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
kwibergaf476c72016-11-28 15:21:39 -0800354 RTC_DCHECK_GT(play_buffer_.size(), 0);
henrika7be78832017-06-13 17:34:16 +0200355#ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE
356 const double phase_increment =
357 k2Pi * 440.0 / static_cast<double>(play_sample_rate_);
358 int16_t* destination_r = reinterpret_cast<int16_t*>(audio_buffer);
henrika29e865a2018-04-24 13:22:31 +0200359 if (play_channels_ == 1) {
360 for (size_t i = 0; i < play_buffer_.size(); ++i) {
361 destination_r[i] = static_cast<int16_t>((sin(phase_) * (1 << 14)));
362 phase_ += phase_increment;
363 }
364 } else if (play_channels_ == 2) {
365 for (size_t i = 0; i < play_buffer_.size() / 2; ++i) {
366 destination_r[2 * i] = destination_r[2 * i + 1] =
367 static_cast<int16_t>((sin(phase_) * (1 << 14)));
368 phase_ += phase_increment;
369 }
henrika7be78832017-06-13 17:34:16 +0200370 }
371#else
henrika51e96082016-11-10 00:40:37 -0800372 memcpy(audio_buffer, play_buffer_.data(),
henrika7be78832017-06-13 17:34:16 +0200373 play_buffer_.size() * sizeof(int16_t));
374#endif
henrika51e96082016-11-10 00:40:37 -0800375 // Return samples per channel or number of frames.
376 return static_cast<int32_t>(play_buffer_.size() / play_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000377}
378
henrikaba156cf2016-10-31 08:18:50 -0700379void AudioDeviceBuffer::StartPeriodicLogging() {
380 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
381 AudioDeviceBuffer::LOG_START));
henrika6c4d0f02016-07-14 05:54:19 -0700382}
383
henrikaba156cf2016-10-31 08:18:50 -0700384void AudioDeviceBuffer::StopPeriodicLogging() {
385 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
386 AudioDeviceBuffer::LOG_STOP));
387}
388
389void AudioDeviceBuffer::LogStats(LogState state) {
henrikaf5022222016-11-07 15:56:59 +0100390 RTC_DCHECK_RUN_ON(&task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700391 int64_t now_time = rtc::TimeMillis();
henrika0b3a6382016-11-11 02:28:50 -0800392
henrikaba156cf2016-10-31 08:18:50 -0700393 if (state == AudioDeviceBuffer::LOG_START) {
394 // Reset counters at start. We will not add any logging in this state but
395 // the timer will started by posting a new (delayed) task.
396 num_stat_reports_ = 0;
397 last_timer_task_time_ = now_time;
henrika0b3a6382016-11-11 02:28:50 -0800398 log_stats_ = true;
henrikaba156cf2016-10-31 08:18:50 -0700399 } else if (state == AudioDeviceBuffer::LOG_STOP) {
400 // Stop logging and posting new tasks.
henrika0b3a6382016-11-11 02:28:50 -0800401 log_stats_ = false;
henrikaba156cf2016-10-31 08:18:50 -0700402 } else if (state == AudioDeviceBuffer::LOG_ACTIVE) {
henrika0b3a6382016-11-11 02:28:50 -0800403 // Keep logging unless logging was disabled while task was posted.
404 }
405
406 // Avoid adding more logs since we are in STOP mode.
407 if (!log_stats_) {
408 return;
henrikaba156cf2016-10-31 08:18:50 -0700409 }
henrika6c4d0f02016-07-14 05:54:19 -0700410
henrikaba156cf2016-10-31 08:18:50 -0700411 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds;
412 int64_t time_since_last = rtc::TimeDiff(now_time, last_timer_task_time_);
413 last_timer_task_time_ = now_time;
414
henrika87d11cd2017-02-08 07:16:56 -0800415 Stats stats;
416 {
417 rtc::CritScope cs(&lock_);
418 stats = stats_;
419 stats_.max_rec_level = 0;
420 stats_.max_play_level = 0;
421 }
422
henrikaba156cf2016-10-31 08:18:50 -0700423 // Log the latest statistics but skip the first round just after state was
424 // set to LOG_START. Hence, first printed log will be after ~10 seconds.
henrikaa6d26ec2016-09-20 04:44:04 -0700425 if (++num_stat_reports_ > 1 && time_since_last > 0) {
henrika87d11cd2017-02-08 07:16:56 -0800426 uint32_t diff_samples = stats.rec_samples - last_stats_.rec_samples;
henrikaa6d26ec2016-09-20 04:44:04 -0700427 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100428 RTC_LOG(INFO) << "[REC : " << time_since_last << "msec, "
429 << rec_sample_rate_ / 1000 << "kHz] callbacks: "
430 << stats.rec_callbacks - last_stats_.rec_callbacks << ", "
431 << "samples: " << diff_samples << ", "
432 << "rate: " << static_cast<int>(rate + 0.5) << ", "
433 << "level: " << stats.max_rec_level;
henrika6c4d0f02016-07-14 05:54:19 -0700434
henrika87d11cd2017-02-08 07:16:56 -0800435 diff_samples = stats.play_samples - last_stats_.play_samples;
henrikaa6d26ec2016-09-20 04:44:04 -0700436 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100437 RTC_LOG(INFO) << "[PLAY: " << time_since_last << "msec, "
438 << play_sample_rate_ / 1000 << "kHz] callbacks: "
439 << stats.play_callbacks - last_stats_.play_callbacks << ", "
440 << "samples: " << diff_samples << ", "
441 << "rate: " << static_cast<int>(rate + 0.5) << ", "
442 << "level: " << stats.max_play_level;
henrika87d11cd2017-02-08 07:16:56 -0800443 last_stats_ = stats;
henrikaf06f35a2016-09-09 14:23:11 +0200444 }
445
henrika6c4d0f02016-07-14 05:54:19 -0700446 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis();
447 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval";
448
henrikaba156cf2016-10-31 08:18:50 -0700449 // Keep posting new (delayed) tasks until state is changed to kLogStop.
450 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
451 AudioDeviceBuffer::LOG_ACTIVE),
henrika6c4d0f02016-07-14 05:54:19 -0700452 time_to_wait_ms);
453}
454
henrikaf06f35a2016-09-09 14:23:11 +0200455void AudioDeviceBuffer::ResetRecStats() {
henrikaf5022222016-11-07 15:56:59 +0100456 RTC_DCHECK_RUN_ON(&task_queue_);
henrika87d11cd2017-02-08 07:16:56 -0800457 last_stats_.ResetRecStats();
458 rtc::CritScope cs(&lock_);
459 stats_.ResetRecStats();
henrikaf06f35a2016-09-09 14:23:11 +0200460}
461
462void AudioDeviceBuffer::ResetPlayStats() {
henrikaf5022222016-11-07 15:56:59 +0100463 RTC_DCHECK_RUN_ON(&task_queue_);
henrika87d11cd2017-02-08 07:16:56 -0800464 last_stats_.ResetPlayStats();
465 rtc::CritScope cs(&lock_);
466 stats_.ResetPlayStats();
henrikaf06f35a2016-09-09 14:23:11 +0200467}
468
henrika51e96082016-11-10 00:40:37 -0800469void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs,
470 size_t samples_per_channel) {
henrika87d11cd2017-02-08 07:16:56 -0800471 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
472 rtc::CritScope cs(&lock_);
473 ++stats_.rec_callbacks;
474 stats_.rec_samples += samples_per_channel;
475 if (max_abs > stats_.max_rec_level) {
476 stats_.max_rec_level = max_abs;
henrikaf06f35a2016-09-09 14:23:11 +0200477 }
henrika6c4d0f02016-07-14 05:54:19 -0700478}
479
henrika51e96082016-11-10 00:40:37 -0800480void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs,
481 size_t samples_per_channel) {
henrika87d11cd2017-02-08 07:16:56 -0800482 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
483 rtc::CritScope cs(&lock_);
484 ++stats_.play_callbacks;
485 stats_.play_samples += samples_per_channel;
486 if (max_abs > stats_.max_play_level) {
487 stats_.max_play_level = max_abs;
henrikaf06f35a2016-09-09 14:23:11 +0200488 }
henrika6c4d0f02016-07-14 05:54:19 -0700489}
490
niklase@google.com470e71d2011-07-07 08:21:25 +0000491} // namespace webrtc