blob: 158a86ad6ccc0db1e63cd2b841a378274169b346 [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 current_mic_level_(0),
54 new_mic_level_(0),
55 typing_status_(false),
56 play_delay_ms_(0),
57 rec_delay_ms_(0),
58 clock_drift_(0),
henrika6c4d0f02016-07-14 05:54:19 -070059 num_stat_reports_(0),
henrikaf5022222016-11-07 15:56:59 +010060 last_timer_task_time_(0),
henrika3355f6d2016-10-21 12:45:25 +020061 rec_stat_count_(0),
henrikaba156cf2016-10-31 08:18:50 -070062 play_stat_count_(0),
63 play_start_time_(0),
henrika0b3a6382016-11-11 02:28:50 -080064 only_silence_recorded_(true),
65 log_stats_(false) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010066 RTC_LOG(INFO) << "AudioDeviceBuffer::ctor";
henrika7be78832017-06-13 17:34:16 +020067#ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE
68 phase_ = 0.0;
Mirko Bonadei675513b2017-11-09 11:09:25 +010069 RTC_LOG(WARNING) << "AUDIO_DEVICE_PLAYS_SINUS_TONE is defined!";
henrika7be78832017-06-13 17:34:16 +020070#endif
henrika4af73662017-10-11 13:16:17 +020071 WebRtcSpl_Init();
henrikaf5022222016-11-07 15:56:59 +010072 playout_thread_checker_.DetachFromThread();
73 recording_thread_checker_.DetachFromThread();
niklase@google.com470e71d2011-07-07 08:21:25 +000074}
75
henrika0fd68012016-07-04 13:01:19 +020076AudioDeviceBuffer::~AudioDeviceBuffer() {
henrikaf5022222016-11-07 15:56:59 +010077 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -070078 RTC_DCHECK(!playing_);
79 RTC_DCHECK(!recording_);
Mirko Bonadei675513b2017-11-09 11:09:25 +010080 RTC_LOG(INFO) << "AudioDeviceBuffer::~dtor";
niklase@google.com470e71d2011-07-07 08:21:25 +000081}
82
henrika0fd68012016-07-04 13:01:19 +020083int32_t AudioDeviceBuffer::RegisterAudioCallback(
henrika49810512016-08-22 05:56:12 -070084 AudioTransport* audio_callback) {
henrikaf5022222016-11-07 15:56:59 +010085 RTC_DCHECK_RUN_ON(&main_thread_checker_);
Mirko Bonadei675513b2017-11-09 11:09:25 +010086 RTC_LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +010087 if (playing_ || recording_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010088 RTC_LOG(LS_ERROR) << "Failed to set audio transport since media was active";
henrikaf5022222016-11-07 15:56:59 +010089 return -1;
90 }
henrika49810512016-08-22 05:56:12 -070091 audio_transport_cb_ = audio_callback;
henrika0fd68012016-07-04 13:01:19 +020092 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000093}
94
henrikaba156cf2016-10-31 08:18:50 -070095void AudioDeviceBuffer::StartPlayout() {
henrikaf5022222016-11-07 15:56:59 +010096 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -070097 // TODO(henrika): allow for usage of DCHECK(!playing_) here instead. Today the
98 // ADM allows calling Start(), Start() by ignoring the second call but it
99 // makes more sense to only allow one call.
100 if (playing_) {
101 return;
henrika6c4d0f02016-07-14 05:54:19 -0700102 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100103 RTC_LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +0100104 playout_thread_checker_.DetachFromThread();
henrikaba156cf2016-10-31 08:18:50 -0700105 // Clear members tracking playout stats and do it on the task queue.
106 task_queue_.PostTask([this] { ResetPlayStats(); });
107 // Start a periodic timer based on task queue if not already done by the
108 // recording side.
109 if (!recording_) {
110 StartPeriodicLogging();
111 }
nissedeb95f32016-11-28 01:54:54 -0800112 const int64_t now_time = rtc::TimeMillis();
henrikaba156cf2016-10-31 08:18:50 -0700113 // Clear members that are only touched on the main (creating) thread.
114 play_start_time_ = now_time;
henrikaba156cf2016-10-31 08:18:50 -0700115 playing_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000116}
117
henrikaba156cf2016-10-31 08:18:50 -0700118void AudioDeviceBuffer::StartRecording() {
henrikaf5022222016-11-07 15:56:59 +0100119 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700120 if (recording_) {
121 return;
henrika6c4d0f02016-07-14 05:54:19 -0700122 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100123 RTC_LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +0100124 recording_thread_checker_.DetachFromThread();
henrikaba156cf2016-10-31 08:18:50 -0700125 // Clear members tracking recording stats and do it on the task queue.
126 task_queue_.PostTask([this] { ResetRecStats(); });
127 // Start a periodic timer based on task queue if not already done by the
128 // playout side.
129 if (!playing_) {
130 StartPeriodicLogging();
131 }
132 // Clear members that will be touched on the main (creating) thread.
133 rec_start_time_ = rtc::TimeMillis();
134 recording_ = true;
135 // And finally a member which can be modified on the native audio thread.
136 // It is safe to do so since we know by design that the owning ADM has not
137 // yet started the native audio recording.
138 only_silence_recorded_ = true;
139}
140
141void AudioDeviceBuffer::StopPlayout() {
henrikaf5022222016-11-07 15:56:59 +0100142 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700143 if (!playing_) {
144 return;
145 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100146 RTC_LOG(INFO) << __FUNCTION__;
henrikaba156cf2016-10-31 08:18:50 -0700147 playing_ = false;
148 // Stop periodic logging if no more media is active.
149 if (!recording_) {
150 StopPeriodicLogging();
151 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100152 RTC_LOG(INFO) << "total playout time: " << rtc::TimeSince(play_start_time_);
henrikaba156cf2016-10-31 08:18:50 -0700153}
154
155void AudioDeviceBuffer::StopRecording() {
henrikaf5022222016-11-07 15:56:59 +0100156 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700157 if (!recording_) {
158 return;
159 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100160 RTC_LOG(INFO) << __FUNCTION__;
henrikaba156cf2016-10-31 08:18:50 -0700161 recording_ = false;
162 // Stop periodic logging if no more media is active.
163 if (!playing_) {
164 StopPeriodicLogging();
165 }
166 // Add UMA histogram to keep track of the case when only zeros have been
167 // recorded. Measurements (max of absolute level) are taken twice per second,
168 // which means that if e.g 10 seconds of audio has been recorded, a total of
169 // 20 level estimates must all be identical to zero to trigger the histogram.
170 // |only_silence_recorded_| can only be cleared on the native audio thread
171 // that drives audio capture but we know by design that the audio has stopped
172 // when this method is called, hence there should not be aby conflicts. Also,
173 // the fact that |only_silence_recorded_| can be affected during the complete
174 // call makes chances of conflicts with potentially one last callback very
175 // small.
176 const size_t time_since_start = rtc::TimeSince(rec_start_time_);
177 if (time_since_start > kMinValidCallTimeTimeInMilliseconds) {
178 const int only_zeros = static_cast<int>(only_silence_recorded_);
179 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", only_zeros);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100180 RTC_LOG(INFO) << "HISTOGRAM(WebRTC.Audio.RecordedOnlyZeros): "
181 << only_zeros;
henrikaba156cf2016-10-31 08:18:50 -0700182 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100183 RTC_LOG(INFO) << "total recording time: " << time_since_start;
niklase@google.com470e71d2011-07-07 08:21:25 +0000184}
185
henrika0fd68012016-07-04 13:01:19 +0200186int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) {
henrikaf5022222016-11-07 15:56:59 +0100187 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100188 RTC_LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")";
henrika49810512016-08-22 05:56:12 -0700189 rec_sample_rate_ = fsHz;
henrika0fd68012016-07-04 13:01:19 +0200190 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000191}
192
henrika0fd68012016-07-04 13:01:19 +0200193int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) {
henrikaf5022222016-11-07 15:56:59 +0100194 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100195 RTC_LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")";
henrika49810512016-08-22 05:56:12 -0700196 play_sample_rate_ = fsHz;
henrika0fd68012016-07-04 13:01:19 +0200197 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000198}
199
henrika0fd68012016-07-04 13:01:19 +0200200int32_t AudioDeviceBuffer::RecordingSampleRate() const {
henrikaf5022222016-11-07 15:56:59 +0100201 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700202 return rec_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000203}
204
henrika0fd68012016-07-04 13:01:19 +0200205int32_t AudioDeviceBuffer::PlayoutSampleRate() const {
henrikaf5022222016-11-07 15:56:59 +0100206 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700207 return play_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000208}
209
henrika0fd68012016-07-04 13:01:19 +0200210int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) {
henrikaf5022222016-11-07 15:56:59 +0100211 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100212 RTC_LOG(INFO) << "SetRecordingChannels(" << channels << ")";
henrika49810512016-08-22 05:56:12 -0700213 rec_channels_ = channels;
henrika0fd68012016-07-04 13:01:19 +0200214 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000215}
216
henrika0fd68012016-07-04 13:01:19 +0200217int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) {
henrikaf5022222016-11-07 15:56:59 +0100218 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100219 RTC_LOG(INFO) << "SetPlayoutChannels(" << channels << ")";
henrika49810512016-08-22 05:56:12 -0700220 play_channels_ = channels;
henrika0fd68012016-07-04 13:01:19 +0200221 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000222}
223
henrika0fd68012016-07-04 13:01:19 +0200224size_t AudioDeviceBuffer::RecordingChannels() const {
henrikaf5022222016-11-07 15:56:59 +0100225 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700226 return rec_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000227}
228
henrika0fd68012016-07-04 13:01:19 +0200229size_t AudioDeviceBuffer::PlayoutChannels() const {
henrikaf5022222016-11-07 15:56:59 +0100230 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700231 return play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000232}
233
henrika0fd68012016-07-04 13:01:19 +0200234int32_t AudioDeviceBuffer::SetCurrentMicLevel(uint32_t level) {
henrikaf5022222016-11-07 15:56:59 +0100235#if !defined(WEBRTC_WIN)
236 // Windows uses a dedicated thread for volume APIs.
237 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
238#endif
henrika49810512016-08-22 05:56:12 -0700239 current_mic_level_ = level;
henrika0fd68012016-07-04 13:01:19 +0200240 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000241}
242
henrika49810512016-08-22 05:56:12 -0700243int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) {
henrikaf5022222016-11-07 15:56:59 +0100244 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700245 typing_status_ = typing_status;
henrika0fd68012016-07-04 13:01:19 +0200246 return 0;
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +0000247}
248
henrika09a76192017-08-23 15:04:40 +0200249void AudioDeviceBuffer::NativeAudioInterrupted() {
250 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
251 playout_thread_checker_.DetachFromThread();
252 recording_thread_checker_.DetachFromThread();
253}
254
henrika0fd68012016-07-04 13:01:19 +0200255uint32_t AudioDeviceBuffer::NewMicLevel() const {
henrikaf5022222016-11-07 15:56:59 +0100256 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700257 return new_mic_level_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000258}
259
henrika49810512016-08-22 05:56:12 -0700260void AudioDeviceBuffer::SetVQEData(int play_delay_ms,
261 int rec_delay_ms,
262 int clock_drift) {
henrikaf5022222016-11-07 15:56:59 +0100263 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700264 play_delay_ms_ = play_delay_ms;
265 rec_delay_ms_ = rec_delay_ms;
266 clock_drift_ = clock_drift;
niklase@google.com470e71d2011-07-07 08:21:25 +0000267}
268
henrika49810512016-08-22 05:56:12 -0700269int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer,
henrika51e96082016-11-10 00:40:37 -0800270 size_t samples_per_channel) {
henrikaf5022222016-11-07 15:56:59 +0100271 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika5588a132016-10-18 05:14:30 -0700272 // Copy the complete input buffer to the local buffer.
henrika5588a132016-10-18 05:14:30 -0700273 const size_t old_size = rec_buffer_.size();
henrika51e96082016-11-10 00:40:37 -0800274 rec_buffer_.SetData(static_cast<const int16_t*>(audio_buffer),
275 rec_channels_ * samples_per_channel);
henrika5588a132016-10-18 05:14:30 -0700276 // Keep track of the size of the recording buffer. Only updated when the
277 // size changes, which is a rare event.
278 if (old_size != rec_buffer_.size()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100279 RTC_LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size();
henrika0fd68012016-07-04 13:01:19 +0200280 }
henrika51e96082016-11-10 00:40:37 -0800281
henrikaba156cf2016-10-31 08:18:50 -0700282 // Derive a new level value twice per second and check if it is non-zero.
henrika3355f6d2016-10-21 12:45:25 +0200283 int16_t max_abs = 0;
284 RTC_DCHECK_LT(rec_stat_count_, 50);
285 if (++rec_stat_count_ >= 50) {
henrika3355f6d2016-10-21 12:45:25 +0200286 // Returns the largest absolute value in a signed 16-bit vector.
henrika51e96082016-11-10 00:40:37 -0800287 max_abs = WebRtcSpl_MaxAbsValueW16(rec_buffer_.data(), rec_buffer_.size());
henrika3355f6d2016-10-21 12:45:25 +0200288 rec_stat_count_ = 0;
henrikaba156cf2016-10-31 08:18:50 -0700289 // Set |only_silence_recorded_| to false as soon as at least one detection
290 // of a non-zero audio packet is found. It can only be restored to true
291 // again by restarting the call.
292 if (max_abs > 0) {
293 only_silence_recorded_ = false;
294 }
henrika3355f6d2016-10-21 12:45:25 +0200295 }
henrika87d11cd2017-02-08 07:16:56 -0800296 // Update recording stats which is used as base for periodic logging of the
297 // audio input state.
298 UpdateRecStats(max_abs, samples_per_channel);
henrika0fd68012016-07-04 13:01:19 +0200299 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000300}
301
henrika0fd68012016-07-04 13:01:19 +0200302int32_t AudioDeviceBuffer::DeliverRecordedData() {
henrikaf5022222016-11-07 15:56:59 +0100303 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700304 if (!audio_transport_cb_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100305 RTC_LOG(LS_WARNING) << "Invalid audio transport";
niklase@google.com470e71d2011-07-07 08:21:25 +0000306 return 0;
henrika0fd68012016-07-04 13:01:19 +0200307 }
henrika51e96082016-11-10 00:40:37 -0800308 const size_t frames = rec_buffer_.size() / rec_channels_;
309 const size_t bytes_per_frame = rec_channels_ * sizeof(int16_t);
henrika5588a132016-10-18 05:14:30 -0700310 uint32_t new_mic_level(0);
311 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_;
henrika5588a132016-10-18 05:14:30 -0700312 int32_t res = audio_transport_cb_->RecordedDataIsAvailable(
henrika51e96082016-11-10 00:40:37 -0800313 rec_buffer_.data(), frames, bytes_per_frame, rec_channels_,
henrika5588a132016-10-18 05:14:30 -0700314 rec_sample_rate_, total_delay_ms, clock_drift_, current_mic_level_,
315 typing_status_, new_mic_level);
henrika0fd68012016-07-04 13:01:19 +0200316 if (res != -1) {
henrika5588a132016-10-18 05:14:30 -0700317 new_mic_level_ = new_mic_level;
henrika49810512016-08-22 05:56:12 -0700318 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100319 RTC_LOG(LS_ERROR) << "RecordedDataIsAvailable() failed";
henrika0fd68012016-07-04 13:01:19 +0200320 }
henrika0fd68012016-07-04 13:01:19 +0200321 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000322}
323
henrika51e96082016-11-10 00:40:37 -0800324int32_t AudioDeviceBuffer::RequestPlayoutData(size_t samples_per_channel) {
henrikaf5022222016-11-07 15:56:59 +0100325 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
henrika51e96082016-11-10 00:40:37 -0800326 // The consumer can change the requested size on the fly and we therefore
henrika5588a132016-10-18 05:14:30 -0700327 // resize the buffer accordingly. Also takes place at the first call to this
328 // method.
henrika51e96082016-11-10 00:40:37 -0800329 const size_t total_samples = play_channels_ * samples_per_channel;
330 if (play_buffer_.size() != total_samples) {
331 play_buffer_.SetSize(total_samples);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100332 RTC_LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size();
henrika5588a132016-10-18 05:14:30 -0700333 }
334
henrika49810512016-08-22 05:56:12 -0700335 size_t num_samples_out(0);
henrikaf5022222016-11-07 15:56:59 +0100336 // It is currently supported to start playout without a valid audio
337 // transport object. Leads to warning and silence.
338 if (!audio_transport_cb_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100339 RTC_LOG(LS_WARNING) << "Invalid audio transport";
henrikaf5022222016-11-07 15:56:59 +0100340 return 0;
341 }
henrikaba156cf2016-10-31 08:18:50 -0700342
henrikaf5022222016-11-07 15:56:59 +0100343 // Retrieve new 16-bit PCM audio data using the audio transport instance.
344 int64_t elapsed_time_ms = -1;
345 int64_t ntp_time_ms = -1;
henrika51e96082016-11-10 00:40:37 -0800346 const size_t bytes_per_frame = play_channels_ * sizeof(int16_t);
henrikaf5022222016-11-07 15:56:59 +0100347 uint32_t res = audio_transport_cb_->NeedMorePlayData(
henrika51e96082016-11-10 00:40:37 -0800348 samples_per_channel, bytes_per_frame, play_channels_, play_sample_rate_,
henrikaf5022222016-11-07 15:56:59 +0100349 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms);
350 if (res != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100351 RTC_LOG(LS_ERROR) << "NeedMorePlayData() failed";
henrika0fd68012016-07-04 13:01:19 +0200352 }
353
henrika3355f6d2016-10-21 12:45:25 +0200354 // Derive a new level value twice per second.
355 int16_t max_abs = 0;
356 RTC_DCHECK_LT(play_stat_count_, 50);
357 if (++play_stat_count_ >= 50) {
henrika3355f6d2016-10-21 12:45:25 +0200358 // Returns the largest absolute value in a signed 16-bit vector.
henrika51e96082016-11-10 00:40:37 -0800359 max_abs =
360 WebRtcSpl_MaxAbsValueW16(play_buffer_.data(), play_buffer_.size());
henrika3355f6d2016-10-21 12:45:25 +0200361 play_stat_count_ = 0;
362 }
henrika87d11cd2017-02-08 07:16:56 -0800363 // Update playout stats which is used as base for periodic logging of the
364 // audio output state.
henrika76535de2017-09-11 01:25:55 -0700365 UpdatePlayStats(max_abs, num_samples_out / play_channels_);
366 return static_cast<int32_t>(num_samples_out / play_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000367}
368
henrika49810512016-08-22 05:56:12 -0700369int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) {
henrikaf5022222016-11-07 15:56:59 +0100370 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
kwibergaf476c72016-11-28 15:21:39 -0800371 RTC_DCHECK_GT(play_buffer_.size(), 0);
henrika7be78832017-06-13 17:34:16 +0200372#ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE
373 const double phase_increment =
374 k2Pi * 440.0 / static_cast<double>(play_sample_rate_);
375 int16_t* destination_r = reinterpret_cast<int16_t*>(audio_buffer);
376 for (size_t i = 0; i < play_buffer_.size(); ++i) {
377 destination_r[i] = static_cast<int16_t>((sin(phase_) * (1 << 14)));
378 phase_ += phase_increment;
379 }
380#else
henrika51e96082016-11-10 00:40:37 -0800381 memcpy(audio_buffer, play_buffer_.data(),
henrika7be78832017-06-13 17:34:16 +0200382 play_buffer_.size() * sizeof(int16_t));
383#endif
henrika51e96082016-11-10 00:40:37 -0800384 // Return samples per channel or number of frames.
385 return static_cast<int32_t>(play_buffer_.size() / play_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000386}
387
henrikaba156cf2016-10-31 08:18:50 -0700388void AudioDeviceBuffer::StartPeriodicLogging() {
389 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
390 AudioDeviceBuffer::LOG_START));
henrika6c4d0f02016-07-14 05:54:19 -0700391}
392
henrikaba156cf2016-10-31 08:18:50 -0700393void AudioDeviceBuffer::StopPeriodicLogging() {
394 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
395 AudioDeviceBuffer::LOG_STOP));
396}
397
398void AudioDeviceBuffer::LogStats(LogState state) {
henrikaf5022222016-11-07 15:56:59 +0100399 RTC_DCHECK_RUN_ON(&task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700400 int64_t now_time = rtc::TimeMillis();
henrika0b3a6382016-11-11 02:28:50 -0800401
henrikaba156cf2016-10-31 08:18:50 -0700402 if (state == AudioDeviceBuffer::LOG_START) {
403 // Reset counters at start. We will not add any logging in this state but
404 // the timer will started by posting a new (delayed) task.
405 num_stat_reports_ = 0;
406 last_timer_task_time_ = now_time;
henrika0b3a6382016-11-11 02:28:50 -0800407 log_stats_ = true;
henrikaba156cf2016-10-31 08:18:50 -0700408 } else if (state == AudioDeviceBuffer::LOG_STOP) {
409 // Stop logging and posting new tasks.
henrika0b3a6382016-11-11 02:28:50 -0800410 log_stats_ = false;
henrikaba156cf2016-10-31 08:18:50 -0700411 } else if (state == AudioDeviceBuffer::LOG_ACTIVE) {
henrika0b3a6382016-11-11 02:28:50 -0800412 // Keep logging unless logging was disabled while task was posted.
413 }
414
415 // Avoid adding more logs since we are in STOP mode.
416 if (!log_stats_) {
417 return;
henrikaba156cf2016-10-31 08:18:50 -0700418 }
henrika6c4d0f02016-07-14 05:54:19 -0700419
henrikaba156cf2016-10-31 08:18:50 -0700420 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds;
421 int64_t time_since_last = rtc::TimeDiff(now_time, last_timer_task_time_);
422 last_timer_task_time_ = now_time;
423
henrika87d11cd2017-02-08 07:16:56 -0800424 Stats stats;
425 {
426 rtc::CritScope cs(&lock_);
427 stats = stats_;
428 stats_.max_rec_level = 0;
429 stats_.max_play_level = 0;
430 }
431
henrikaba156cf2016-10-31 08:18:50 -0700432 // Log the latest statistics but skip the first round just after state was
433 // set to LOG_START. Hence, first printed log will be after ~10 seconds.
henrikaa6d26ec2016-09-20 04:44:04 -0700434 if (++num_stat_reports_ > 1 && time_since_last > 0) {
henrika87d11cd2017-02-08 07:16:56 -0800435 uint32_t diff_samples = stats.rec_samples - last_stats_.rec_samples;
henrikaa6d26ec2016-09-20 04:44:04 -0700436 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100437 RTC_LOG(INFO) << "[REC : " << time_since_last << "msec, "
438 << rec_sample_rate_ / 1000 << "kHz] callbacks: "
439 << stats.rec_callbacks - last_stats_.rec_callbacks << ", "
440 << "samples: " << diff_samples << ", "
441 << "rate: " << static_cast<int>(rate + 0.5) << ", "
442 << "level: " << stats.max_rec_level;
henrika6c4d0f02016-07-14 05:54:19 -0700443
henrika87d11cd2017-02-08 07:16:56 -0800444 diff_samples = stats.play_samples - last_stats_.play_samples;
henrikaa6d26ec2016-09-20 04:44:04 -0700445 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100446 RTC_LOG(INFO) << "[PLAY: " << time_since_last << "msec, "
447 << play_sample_rate_ / 1000 << "kHz] callbacks: "
448 << stats.play_callbacks - last_stats_.play_callbacks << ", "
449 << "samples: " << diff_samples << ", "
450 << "rate: " << static_cast<int>(rate + 0.5) << ", "
451 << "level: " << stats.max_play_level;
henrika87d11cd2017-02-08 07:16:56 -0800452 last_stats_ = stats;
henrikaf06f35a2016-09-09 14:23:11 +0200453 }
454
henrika6c4d0f02016-07-14 05:54:19 -0700455 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis();
456 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval";
457
henrikaba156cf2016-10-31 08:18:50 -0700458 // Keep posting new (delayed) tasks until state is changed to kLogStop.
459 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
460 AudioDeviceBuffer::LOG_ACTIVE),
henrika6c4d0f02016-07-14 05:54:19 -0700461 time_to_wait_ms);
462}
463
henrikaf06f35a2016-09-09 14:23:11 +0200464void AudioDeviceBuffer::ResetRecStats() {
henrikaf5022222016-11-07 15:56:59 +0100465 RTC_DCHECK_RUN_ON(&task_queue_);
henrika87d11cd2017-02-08 07:16:56 -0800466 last_stats_.ResetRecStats();
467 rtc::CritScope cs(&lock_);
468 stats_.ResetRecStats();
henrikaf06f35a2016-09-09 14:23:11 +0200469}
470
471void AudioDeviceBuffer::ResetPlayStats() {
henrikaf5022222016-11-07 15:56:59 +0100472 RTC_DCHECK_RUN_ON(&task_queue_);
henrika87d11cd2017-02-08 07:16:56 -0800473 last_stats_.ResetPlayStats();
474 rtc::CritScope cs(&lock_);
475 stats_.ResetPlayStats();
henrikaf06f35a2016-09-09 14:23:11 +0200476}
477
henrika51e96082016-11-10 00:40:37 -0800478void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs,
479 size_t samples_per_channel) {
henrika87d11cd2017-02-08 07:16:56 -0800480 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
481 rtc::CritScope cs(&lock_);
482 ++stats_.rec_callbacks;
483 stats_.rec_samples += samples_per_channel;
484 if (max_abs > stats_.max_rec_level) {
485 stats_.max_rec_level = max_abs;
henrikaf06f35a2016-09-09 14:23:11 +0200486 }
henrika6c4d0f02016-07-14 05:54:19 -0700487}
488
henrika51e96082016-11-10 00:40:37 -0800489void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs,
490 size_t samples_per_channel) {
henrika87d11cd2017-02-08 07:16:56 -0800491 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
492 rtc::CritScope cs(&lock_);
493 ++stats_.play_callbacks;
494 stats_.play_samples += samples_per_channel;
495 if (max_abs > stats_.max_play_level) {
496 stats_.max_play_level = max_abs;
henrikaf06f35a2016-09-09 14:23:11 +0200497 }
henrika6c4d0f02016-07-14 05:54:19 -0700498}
499
niklase@google.com470e71d2011-07-07 08:21:25 +0000500} // namespace webrtc