blob: 063c65f4fc120febd6da7db6fc800aaf23f4775f [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
pbos@webrtc.org811269d2013-07-11 13:24:38 +000014#include "webrtc/modules/audio_device/audio_device_buffer.h"
andrew@webrtc.org25534502013-09-13 00:02:13 +000015
henrikaf06f35a2016-09-09 14:23:11 +020016#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
pbos@webrtc.org811269d2013-07-11 13:24:38 +000017#include "webrtc/modules/audio_device/audio_device_config.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020018#include "webrtc/rtc_base/arraysize.h"
19#include "webrtc/rtc_base/bind.h"
20#include "webrtc/rtc_base/checks.h"
21#include "webrtc/rtc_base/format_macros.h"
22#include "webrtc/rtc_base/logging.h"
23#include "webrtc/rtc_base/timeutils.h"
henrikaf06f35a2016-09-09 14:23:11 +020024#include "webrtc/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) {
henrika3f33e2a2016-07-06 00:33:57 -070066 LOG(INFO) << "AudioDeviceBuffer::ctor";
henrika7be78832017-06-13 17:34:16 +020067#ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE
68 phase_ = 0.0;
69 LOG(WARNING) << "AUDIO_DEVICE_PLAYS_SINUS_TONE is defined!";
70#endif
henrikaf5022222016-11-07 15:56:59 +010071 playout_thread_checker_.DetachFromThread();
72 recording_thread_checker_.DetachFromThread();
niklase@google.com470e71d2011-07-07 08:21:25 +000073}
74
henrika0fd68012016-07-04 13:01:19 +020075AudioDeviceBuffer::~AudioDeviceBuffer() {
henrikaf5022222016-11-07 15:56:59 +010076 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -070077 RTC_DCHECK(!playing_);
78 RTC_DCHECK(!recording_);
henrika3f33e2a2016-07-06 00:33:57 -070079 LOG(INFO) << "AudioDeviceBuffer::~dtor";
niklase@google.com470e71d2011-07-07 08:21:25 +000080}
81
henrika0fd68012016-07-04 13:01:19 +020082int32_t AudioDeviceBuffer::RegisterAudioCallback(
henrika49810512016-08-22 05:56:12 -070083 AudioTransport* audio_callback) {
henrikaf5022222016-11-07 15:56:59 +010084 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrika3f33e2a2016-07-06 00:33:57 -070085 LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +010086 if (playing_ || recording_) {
87 LOG(LS_ERROR) << "Failed to set audio transport since media was active";
88 return -1;
89 }
henrika49810512016-08-22 05:56:12 -070090 audio_transport_cb_ = audio_callback;
henrika0fd68012016-07-04 13:01:19 +020091 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000092}
93
henrikaba156cf2016-10-31 08:18:50 -070094void AudioDeviceBuffer::StartPlayout() {
henrikaf5022222016-11-07 15:56:59 +010095 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -070096 // TODO(henrika): allow for usage of DCHECK(!playing_) here instead. Today the
97 // ADM allows calling Start(), Start() by ignoring the second call but it
98 // makes more sense to only allow one call.
99 if (playing_) {
100 return;
henrika6c4d0f02016-07-14 05:54:19 -0700101 }
henrikaba156cf2016-10-31 08:18:50 -0700102 LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +0100103 playout_thread_checker_.DetachFromThread();
henrikaba156cf2016-10-31 08:18:50 -0700104 // Clear members tracking playout stats and do it on the task queue.
105 task_queue_.PostTask([this] { ResetPlayStats(); });
106 // Start a periodic timer based on task queue if not already done by the
107 // recording side.
108 if (!recording_) {
109 StartPeriodicLogging();
110 }
nissedeb95f32016-11-28 01:54:54 -0800111 const int64_t now_time = rtc::TimeMillis();
henrikaba156cf2016-10-31 08:18:50 -0700112 // Clear members that are only touched on the main (creating) thread.
113 play_start_time_ = now_time;
henrikaba156cf2016-10-31 08:18:50 -0700114 playing_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000115}
116
henrikaba156cf2016-10-31 08:18:50 -0700117void AudioDeviceBuffer::StartRecording() {
henrikaf5022222016-11-07 15:56:59 +0100118 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700119 if (recording_) {
120 return;
henrika6c4d0f02016-07-14 05:54:19 -0700121 }
henrikaba156cf2016-10-31 08:18:50 -0700122 LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +0100123 recording_thread_checker_.DetachFromThread();
henrikaba156cf2016-10-31 08:18:50 -0700124 // Clear members tracking recording stats and do it on the task queue.
125 task_queue_.PostTask([this] { ResetRecStats(); });
126 // Start a periodic timer based on task queue if not already done by the
127 // playout side.
128 if (!playing_) {
129 StartPeriodicLogging();
130 }
131 // Clear members that will be touched on the main (creating) thread.
132 rec_start_time_ = rtc::TimeMillis();
133 recording_ = true;
134 // And finally a member which can be modified on the native audio thread.
135 // It is safe to do so since we know by design that the owning ADM has not
136 // yet started the native audio recording.
137 only_silence_recorded_ = true;
138}
139
140void AudioDeviceBuffer::StopPlayout() {
henrikaf5022222016-11-07 15:56:59 +0100141 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700142 if (!playing_) {
143 return;
144 }
145 LOG(INFO) << __FUNCTION__;
146 playing_ = false;
147 // Stop periodic logging if no more media is active.
148 if (!recording_) {
149 StopPeriodicLogging();
150 }
henrikaf5022222016-11-07 15:56:59 +0100151 LOG(INFO) << "total playout time: " << rtc::TimeSince(play_start_time_);
henrikaba156cf2016-10-31 08:18:50 -0700152}
153
154void AudioDeviceBuffer::StopRecording() {
henrikaf5022222016-11-07 15:56:59 +0100155 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700156 if (!recording_) {
157 return;
158 }
159 LOG(INFO) << __FUNCTION__;
160 recording_ = false;
161 // Stop periodic logging if no more media is active.
162 if (!playing_) {
163 StopPeriodicLogging();
164 }
165 // Add UMA histogram to keep track of the case when only zeros have been
166 // recorded. Measurements (max of absolute level) are taken twice per second,
167 // which means that if e.g 10 seconds of audio has been recorded, a total of
168 // 20 level estimates must all be identical to zero to trigger the histogram.
169 // |only_silence_recorded_| can only be cleared on the native audio thread
170 // that drives audio capture but we know by design that the audio has stopped
171 // when this method is called, hence there should not be aby conflicts. Also,
172 // the fact that |only_silence_recorded_| can be affected during the complete
173 // call makes chances of conflicts with potentially one last callback very
174 // small.
175 const size_t time_since_start = rtc::TimeSince(rec_start_time_);
176 if (time_since_start > kMinValidCallTimeTimeInMilliseconds) {
177 const int only_zeros = static_cast<int>(only_silence_recorded_);
178 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", only_zeros);
179 LOG(INFO) << "HISTOGRAM(WebRTC.Audio.RecordedOnlyZeros): " << only_zeros;
180 }
181 LOG(INFO) << "total recording time: " << time_since_start;
niklase@google.com470e71d2011-07-07 08:21:25 +0000182}
183
henrika0fd68012016-07-04 13:01:19 +0200184int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) {
henrikaf5022222016-11-07 15:56:59 +0100185 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika3f33e2a2016-07-06 00:33:57 -0700186 LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")";
henrika49810512016-08-22 05:56:12 -0700187 rec_sample_rate_ = fsHz;
henrika0fd68012016-07-04 13:01:19 +0200188 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000189}
190
henrika0fd68012016-07-04 13:01:19 +0200191int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) {
henrikaf5022222016-11-07 15:56:59 +0100192 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika3f33e2a2016-07-06 00:33:57 -0700193 LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")";
henrika49810512016-08-22 05:56:12 -0700194 play_sample_rate_ = fsHz;
henrika0fd68012016-07-04 13:01:19 +0200195 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000196}
197
henrika0fd68012016-07-04 13:01:19 +0200198int32_t AudioDeviceBuffer::RecordingSampleRate() const {
henrikaf5022222016-11-07 15:56:59 +0100199 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700200 return rec_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000201}
202
henrika0fd68012016-07-04 13:01:19 +0200203int32_t AudioDeviceBuffer::PlayoutSampleRate() const {
henrikaf5022222016-11-07 15:56:59 +0100204 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700205 return play_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000206}
207
henrika0fd68012016-07-04 13:01:19 +0200208int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) {
henrikaf5022222016-11-07 15:56:59 +0100209 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700210 LOG(INFO) << "SetRecordingChannels(" << channels << ")";
henrika49810512016-08-22 05:56:12 -0700211 rec_channels_ = channels;
henrika0fd68012016-07-04 13:01:19 +0200212 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000213}
214
henrika0fd68012016-07-04 13:01:19 +0200215int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) {
henrikaf5022222016-11-07 15:56:59 +0100216 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700217 LOG(INFO) << "SetPlayoutChannels(" << channels << ")";
henrika49810512016-08-22 05:56:12 -0700218 play_channels_ = channels;
henrika0fd68012016-07-04 13:01:19 +0200219 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000220}
221
henrika0fd68012016-07-04 13:01:19 +0200222int32_t AudioDeviceBuffer::SetRecordingChannel(
223 const AudioDeviceModule::ChannelType channel) {
henrika5588a132016-10-18 05:14:30 -0700224 LOG(INFO) << "SetRecordingChannel(" << channel << ")";
225 LOG(LS_WARNING) << "Not implemented";
226 // Add DCHECK to ensure that user does not try to use this API with a non-
227 // default parameter.
228 RTC_DCHECK_EQ(channel, AudioDeviceModule::kChannelBoth);
229 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000230}
231
henrika0fd68012016-07-04 13:01:19 +0200232int32_t AudioDeviceBuffer::RecordingChannel(
233 AudioDeviceModule::ChannelType& channel) const {
henrika5588a132016-10-18 05:14:30 -0700234 LOG(LS_WARNING) << "Not implemented";
235 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000236}
237
henrika0fd68012016-07-04 13:01:19 +0200238size_t AudioDeviceBuffer::RecordingChannels() const {
henrikaf5022222016-11-07 15:56:59 +0100239 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700240 return rec_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000241}
242
henrika0fd68012016-07-04 13:01:19 +0200243size_t AudioDeviceBuffer::PlayoutChannels() const {
henrikaf5022222016-11-07 15:56:59 +0100244 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700245 return play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000246}
247
henrika0fd68012016-07-04 13:01:19 +0200248int32_t AudioDeviceBuffer::SetCurrentMicLevel(uint32_t level) {
henrikaf5022222016-11-07 15:56:59 +0100249#if !defined(WEBRTC_WIN)
250 // Windows uses a dedicated thread for volume APIs.
251 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
252#endif
henrika49810512016-08-22 05:56:12 -0700253 current_mic_level_ = level;
henrika0fd68012016-07-04 13:01:19 +0200254 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000255}
256
henrika49810512016-08-22 05:56:12 -0700257int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) {
henrikaf5022222016-11-07 15:56:59 +0100258 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700259 typing_status_ = typing_status;
henrika0fd68012016-07-04 13:01:19 +0200260 return 0;
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +0000261}
262
henrika09a76192017-08-23 15:04:40 +0200263void AudioDeviceBuffer::NativeAudioInterrupted() {
264 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
265 playout_thread_checker_.DetachFromThread();
266 recording_thread_checker_.DetachFromThread();
267}
268
henrika0fd68012016-07-04 13:01:19 +0200269uint32_t AudioDeviceBuffer::NewMicLevel() const {
henrikaf5022222016-11-07 15:56:59 +0100270 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700271 return new_mic_level_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000272}
273
henrika49810512016-08-22 05:56:12 -0700274void AudioDeviceBuffer::SetVQEData(int play_delay_ms,
275 int rec_delay_ms,
276 int clock_drift) {
henrikaf5022222016-11-07 15:56:59 +0100277 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700278 play_delay_ms_ = play_delay_ms;
279 rec_delay_ms_ = rec_delay_ms;
280 clock_drift_ = clock_drift;
niklase@google.com470e71d2011-07-07 08:21:25 +0000281}
282
henrika49810512016-08-22 05:56:12 -0700283int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer,
henrika51e96082016-11-10 00:40:37 -0800284 size_t samples_per_channel) {
henrikaf5022222016-11-07 15:56:59 +0100285 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika5588a132016-10-18 05:14:30 -0700286 // Copy the complete input buffer to the local buffer.
henrika5588a132016-10-18 05:14:30 -0700287 const size_t old_size = rec_buffer_.size();
henrika51e96082016-11-10 00:40:37 -0800288 rec_buffer_.SetData(static_cast<const int16_t*>(audio_buffer),
289 rec_channels_ * samples_per_channel);
henrika5588a132016-10-18 05:14:30 -0700290 // Keep track of the size of the recording buffer. Only updated when the
291 // size changes, which is a rare event.
292 if (old_size != rec_buffer_.size()) {
293 LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size();
henrika0fd68012016-07-04 13:01:19 +0200294 }
henrika51e96082016-11-10 00:40:37 -0800295
henrikaba156cf2016-10-31 08:18:50 -0700296 // Derive a new level value twice per second and check if it is non-zero.
henrika3355f6d2016-10-21 12:45:25 +0200297 int16_t max_abs = 0;
298 RTC_DCHECK_LT(rec_stat_count_, 50);
299 if (++rec_stat_count_ >= 50) {
henrika3355f6d2016-10-21 12:45:25 +0200300 // Returns the largest absolute value in a signed 16-bit vector.
henrika51e96082016-11-10 00:40:37 -0800301 max_abs = WebRtcSpl_MaxAbsValueW16(rec_buffer_.data(), rec_buffer_.size());
henrika3355f6d2016-10-21 12:45:25 +0200302 rec_stat_count_ = 0;
henrikaba156cf2016-10-31 08:18:50 -0700303 // Set |only_silence_recorded_| to false as soon as at least one detection
304 // of a non-zero audio packet is found. It can only be restored to true
305 // again by restarting the call.
306 if (max_abs > 0) {
307 only_silence_recorded_ = false;
308 }
henrika3355f6d2016-10-21 12:45:25 +0200309 }
henrika87d11cd2017-02-08 07:16:56 -0800310 // Update recording stats which is used as base for periodic logging of the
311 // audio input state.
312 UpdateRecStats(max_abs, samples_per_channel);
henrika0fd68012016-07-04 13:01:19 +0200313 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000314}
315
henrika0fd68012016-07-04 13:01:19 +0200316int32_t AudioDeviceBuffer::DeliverRecordedData() {
henrikaf5022222016-11-07 15:56:59 +0100317 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700318 if (!audio_transport_cb_) {
henrika3f33e2a2016-07-06 00:33:57 -0700319 LOG(LS_WARNING) << "Invalid audio transport";
niklase@google.com470e71d2011-07-07 08:21:25 +0000320 return 0;
henrika0fd68012016-07-04 13:01:19 +0200321 }
henrika51e96082016-11-10 00:40:37 -0800322 const size_t frames = rec_buffer_.size() / rec_channels_;
323 const size_t bytes_per_frame = rec_channels_ * sizeof(int16_t);
henrika5588a132016-10-18 05:14:30 -0700324 uint32_t new_mic_level(0);
325 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_;
henrika5588a132016-10-18 05:14:30 -0700326 int32_t res = audio_transport_cb_->RecordedDataIsAvailable(
henrika51e96082016-11-10 00:40:37 -0800327 rec_buffer_.data(), frames, bytes_per_frame, rec_channels_,
henrika5588a132016-10-18 05:14:30 -0700328 rec_sample_rate_, total_delay_ms, clock_drift_, current_mic_level_,
329 typing_status_, new_mic_level);
henrika0fd68012016-07-04 13:01:19 +0200330 if (res != -1) {
henrika5588a132016-10-18 05:14:30 -0700331 new_mic_level_ = new_mic_level;
henrika49810512016-08-22 05:56:12 -0700332 } else {
333 LOG(LS_ERROR) << "RecordedDataIsAvailable() failed";
henrika0fd68012016-07-04 13:01:19 +0200334 }
henrika0fd68012016-07-04 13:01:19 +0200335 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000336}
337
henrika51e96082016-11-10 00:40:37 -0800338int32_t AudioDeviceBuffer::RequestPlayoutData(size_t samples_per_channel) {
henrikaf5022222016-11-07 15:56:59 +0100339 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
henrika51e96082016-11-10 00:40:37 -0800340 // The consumer can change the requested size on the fly and we therefore
henrika5588a132016-10-18 05:14:30 -0700341 // resize the buffer accordingly. Also takes place at the first call to this
342 // method.
henrika51e96082016-11-10 00:40:37 -0800343 const size_t total_samples = play_channels_ * samples_per_channel;
344 if (play_buffer_.size() != total_samples) {
345 play_buffer_.SetSize(total_samples);
henrika5588a132016-10-18 05:14:30 -0700346 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size();
347 }
348
henrika49810512016-08-22 05:56:12 -0700349 size_t num_samples_out(0);
henrikaf5022222016-11-07 15:56:59 +0100350 // It is currently supported to start playout without a valid audio
351 // transport object. Leads to warning and silence.
352 if (!audio_transport_cb_) {
353 LOG(LS_WARNING) << "Invalid audio transport";
354 return 0;
355 }
henrikaba156cf2016-10-31 08:18:50 -0700356
henrikaf5022222016-11-07 15:56:59 +0100357 // Retrieve new 16-bit PCM audio data using the audio transport instance.
358 int64_t elapsed_time_ms = -1;
359 int64_t ntp_time_ms = -1;
henrika51e96082016-11-10 00:40:37 -0800360 const size_t bytes_per_frame = play_channels_ * sizeof(int16_t);
henrikaf5022222016-11-07 15:56:59 +0100361 uint32_t res = audio_transport_cb_->NeedMorePlayData(
henrika51e96082016-11-10 00:40:37 -0800362 samples_per_channel, bytes_per_frame, play_channels_, play_sample_rate_,
henrikaf5022222016-11-07 15:56:59 +0100363 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms);
364 if (res != 0) {
365 LOG(LS_ERROR) << "NeedMorePlayData() failed";
henrika0fd68012016-07-04 13:01:19 +0200366 }
367
henrika3355f6d2016-10-21 12:45:25 +0200368 // Derive a new level value twice per second.
369 int16_t max_abs = 0;
370 RTC_DCHECK_LT(play_stat_count_, 50);
371 if (++play_stat_count_ >= 50) {
henrika3355f6d2016-10-21 12:45:25 +0200372 // Returns the largest absolute value in a signed 16-bit vector.
henrika51e96082016-11-10 00:40:37 -0800373 max_abs =
374 WebRtcSpl_MaxAbsValueW16(play_buffer_.data(), play_buffer_.size());
henrika3355f6d2016-10-21 12:45:25 +0200375 play_stat_count_ = 0;
376 }
henrika87d11cd2017-02-08 07:16:56 -0800377 // Update playout stats which is used as base for periodic logging of the
378 // audio output state.
henrika76535de2017-09-11 01:25:55 -0700379 UpdatePlayStats(max_abs, num_samples_out / play_channels_);
380 return static_cast<int32_t>(num_samples_out / play_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000381}
382
henrika49810512016-08-22 05:56:12 -0700383int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) {
henrikaf5022222016-11-07 15:56:59 +0100384 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
kwibergaf476c72016-11-28 15:21:39 -0800385 RTC_DCHECK_GT(play_buffer_.size(), 0);
henrika7be78832017-06-13 17:34:16 +0200386#ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE
387 const double phase_increment =
388 k2Pi * 440.0 / static_cast<double>(play_sample_rate_);
389 int16_t* destination_r = reinterpret_cast<int16_t*>(audio_buffer);
390 for (size_t i = 0; i < play_buffer_.size(); ++i) {
391 destination_r[i] = static_cast<int16_t>((sin(phase_) * (1 << 14)));
392 phase_ += phase_increment;
393 }
394#else
henrika51e96082016-11-10 00:40:37 -0800395 memcpy(audio_buffer, play_buffer_.data(),
henrika7be78832017-06-13 17:34:16 +0200396 play_buffer_.size() * sizeof(int16_t));
397#endif
henrika51e96082016-11-10 00:40:37 -0800398 // Return samples per channel or number of frames.
399 return static_cast<int32_t>(play_buffer_.size() / play_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000400}
401
henrikaba156cf2016-10-31 08:18:50 -0700402void AudioDeviceBuffer::StartPeriodicLogging() {
403 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
404 AudioDeviceBuffer::LOG_START));
henrika6c4d0f02016-07-14 05:54:19 -0700405}
406
henrikaba156cf2016-10-31 08:18:50 -0700407void AudioDeviceBuffer::StopPeriodicLogging() {
408 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
409 AudioDeviceBuffer::LOG_STOP));
410}
411
412void AudioDeviceBuffer::LogStats(LogState state) {
henrikaf5022222016-11-07 15:56:59 +0100413 RTC_DCHECK_RUN_ON(&task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700414 int64_t now_time = rtc::TimeMillis();
henrika0b3a6382016-11-11 02:28:50 -0800415
henrikaba156cf2016-10-31 08:18:50 -0700416 if (state == AudioDeviceBuffer::LOG_START) {
417 // Reset counters at start. We will not add any logging in this state but
418 // the timer will started by posting a new (delayed) task.
419 num_stat_reports_ = 0;
420 last_timer_task_time_ = now_time;
henrika0b3a6382016-11-11 02:28:50 -0800421 log_stats_ = true;
henrikaba156cf2016-10-31 08:18:50 -0700422 } else if (state == AudioDeviceBuffer::LOG_STOP) {
423 // Stop logging and posting new tasks.
henrika0b3a6382016-11-11 02:28:50 -0800424 log_stats_ = false;
henrikaba156cf2016-10-31 08:18:50 -0700425 } else if (state == AudioDeviceBuffer::LOG_ACTIVE) {
henrika0b3a6382016-11-11 02:28:50 -0800426 // Keep logging unless logging was disabled while task was posted.
427 }
428
429 // Avoid adding more logs since we are in STOP mode.
430 if (!log_stats_) {
431 return;
henrikaba156cf2016-10-31 08:18:50 -0700432 }
henrika6c4d0f02016-07-14 05:54:19 -0700433
henrikaba156cf2016-10-31 08:18:50 -0700434 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds;
435 int64_t time_since_last = rtc::TimeDiff(now_time, last_timer_task_time_);
436 last_timer_task_time_ = now_time;
437
henrika87d11cd2017-02-08 07:16:56 -0800438 Stats stats;
439 {
440 rtc::CritScope cs(&lock_);
441 stats = stats_;
442 stats_.max_rec_level = 0;
443 stats_.max_play_level = 0;
444 }
445
henrikaba156cf2016-10-31 08:18:50 -0700446 // Log the latest statistics but skip the first round just after state was
447 // set to LOG_START. Hence, first printed log will be after ~10 seconds.
henrikaa6d26ec2016-09-20 04:44:04 -0700448 if (++num_stat_reports_ > 1 && time_since_last > 0) {
henrika87d11cd2017-02-08 07:16:56 -0800449 uint32_t diff_samples = stats.rec_samples - last_stats_.rec_samples;
henrikaa6d26ec2016-09-20 04:44:04 -0700450 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
henrika6c4d0f02016-07-14 05:54:19 -0700451 LOG(INFO) << "[REC : " << time_since_last << "msec, "
henrika87d11cd2017-02-08 07:16:56 -0800452 << rec_sample_rate_ / 1000 << "kHz] callbacks: "
453 << stats.rec_callbacks - last_stats_.rec_callbacks << ", "
henrika6c4d0f02016-07-14 05:54:19 -0700454 << "samples: " << diff_samples << ", "
henrikaa6d26ec2016-09-20 04:44:04 -0700455 << "rate: " << static_cast<int>(rate + 0.5) << ", "
henrika87d11cd2017-02-08 07:16:56 -0800456 << "level: " << stats.max_rec_level;
henrika6c4d0f02016-07-14 05:54:19 -0700457
henrika87d11cd2017-02-08 07:16:56 -0800458 diff_samples = stats.play_samples - last_stats_.play_samples;
henrikaa6d26ec2016-09-20 04:44:04 -0700459 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
henrika6c4d0f02016-07-14 05:54:19 -0700460 LOG(INFO) << "[PLAY: " << time_since_last << "msec, "
henrika87d11cd2017-02-08 07:16:56 -0800461 << play_sample_rate_ / 1000 << "kHz] callbacks: "
462 << stats.play_callbacks - last_stats_.play_callbacks << ", "
henrika6c4d0f02016-07-14 05:54:19 -0700463 << "samples: " << diff_samples << ", "
henrikaa6d26ec2016-09-20 04:44:04 -0700464 << "rate: " << static_cast<int>(rate + 0.5) << ", "
henrika87d11cd2017-02-08 07:16:56 -0800465 << "level: " << stats.max_play_level;
466 last_stats_ = stats;
henrikaf06f35a2016-09-09 14:23:11 +0200467 }
468
henrika6c4d0f02016-07-14 05:54:19 -0700469 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis();
470 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval";
471
henrikaba156cf2016-10-31 08:18:50 -0700472 // Keep posting new (delayed) tasks until state is changed to kLogStop.
473 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
474 AudioDeviceBuffer::LOG_ACTIVE),
henrika6c4d0f02016-07-14 05:54:19 -0700475 time_to_wait_ms);
476}
477
henrikaf06f35a2016-09-09 14:23:11 +0200478void AudioDeviceBuffer::ResetRecStats() {
henrikaf5022222016-11-07 15:56:59 +0100479 RTC_DCHECK_RUN_ON(&task_queue_);
henrika87d11cd2017-02-08 07:16:56 -0800480 last_stats_.ResetRecStats();
481 rtc::CritScope cs(&lock_);
482 stats_.ResetRecStats();
henrikaf06f35a2016-09-09 14:23:11 +0200483}
484
485void AudioDeviceBuffer::ResetPlayStats() {
henrikaf5022222016-11-07 15:56:59 +0100486 RTC_DCHECK_RUN_ON(&task_queue_);
henrika87d11cd2017-02-08 07:16:56 -0800487 last_stats_.ResetPlayStats();
488 rtc::CritScope cs(&lock_);
489 stats_.ResetPlayStats();
henrikaf06f35a2016-09-09 14:23:11 +0200490}
491
henrika51e96082016-11-10 00:40:37 -0800492void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs,
493 size_t samples_per_channel) {
henrika87d11cd2017-02-08 07:16:56 -0800494 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
495 rtc::CritScope cs(&lock_);
496 ++stats_.rec_callbacks;
497 stats_.rec_samples += samples_per_channel;
498 if (max_abs > stats_.max_rec_level) {
499 stats_.max_rec_level = max_abs;
henrikaf06f35a2016-09-09 14:23:11 +0200500 }
henrika6c4d0f02016-07-14 05:54:19 -0700501}
502
henrika51e96082016-11-10 00:40:37 -0800503void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs,
504 size_t samples_per_channel) {
henrika87d11cd2017-02-08 07:16:56 -0800505 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
506 rtc::CritScope cs(&lock_);
507 ++stats_.play_callbacks;
508 stats_.play_samples += samples_per_channel;
509 if (max_abs > stats_.max_play_level) {
510 stats_.max_play_level = max_abs;
henrikaf06f35a2016-09-09 14:23:11 +0200511 }
henrika6c4d0f02016-07-14 05:54:19 -0700512}
513
niklase@google.com470e71d2011-07-07 08:21:25 +0000514} // namespace webrtc