blob: 4102a610d10c18a1074f44aa76872b2d4d2de2de [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>
12
pbos@webrtc.org811269d2013-07-11 13:24:38 +000013#include "webrtc/modules/audio_device/audio_device_buffer.h"
andrew@webrtc.org25534502013-09-13 00:02:13 +000014
henrika3d7346f2016-07-29 16:20:47 +020015#include "webrtc/base/arraysize.h"
henrika6c4d0f02016-07-14 05:54:19 -070016#include "webrtc/base/bind.h"
henrika3f33e2a2016-07-06 00:33:57 -070017#include "webrtc/base/checks.h"
18#include "webrtc/base/logging.h"
Peter Kastingdce40cf2015-08-24 14:52:23 -070019#include "webrtc/base/format_macros.h"
henrika6c4d0f02016-07-14 05:54:19 -070020#include "webrtc/base/timeutils.h"
henrikaf06f35a2016-09-09 14:23:11 +020021#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
pbos@webrtc.org811269d2013-07-11 13:24:38 +000022#include "webrtc/modules/audio_device/audio_device_config.h"
henrikaf06f35a2016-09-09 14:23:11 +020023#include "webrtc/system_wrappers/include/metrics.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000024
niklase@google.com470e71d2011-07-07 08:21:25 +000025namespace webrtc {
26
henrika6c4d0f02016-07-14 05:54:19 -070027static const char kTimerQueueName[] = "AudioDeviceBufferTimer";
28
29// Time between two sucessive calls to LogStats().
30static const size_t kTimerIntervalInSeconds = 10;
31static const size_t kTimerIntervalInMilliseconds =
32 kTimerIntervalInSeconds * rtc::kNumMillisecsPerSec;
henrikaba156cf2016-10-31 08:18:50 -070033// Min time required to qualify an audio session as a "call". If playout or
34// recording has been active for less than this time we will not store any
35// logs or UMA stats but instead consider the call as too short.
36static const size_t kMinValidCallTimeTimeInSeconds = 10;
37static const size_t kMinValidCallTimeTimeInMilliseconds =
38 kMinValidCallTimeTimeInSeconds * rtc::kNumMillisecsPerSec;
henrika6c4d0f02016-07-14 05:54:19 -070039
henrika0fd68012016-07-04 13:01:19 +020040AudioDeviceBuffer::AudioDeviceBuffer()
henrikaf5022222016-11-07 15:56:59 +010041 : task_queue_(kTimerQueueName),
42 audio_transport_cb_(nullptr),
henrika49810512016-08-22 05:56:12 -070043 rec_sample_rate_(0),
44 play_sample_rate_(0),
45 rec_channels_(0),
46 play_channels_(0),
henrikaf5022222016-11-07 15:56:59 +010047 playing_(false),
48 recording_(false),
henrika49810512016-08-22 05:56:12 -070049 current_mic_level_(0),
50 new_mic_level_(0),
51 typing_status_(false),
52 play_delay_ms_(0),
53 rec_delay_ms_(0),
54 clock_drift_(0),
henrika6c4d0f02016-07-14 05:54:19 -070055 num_stat_reports_(0),
56 rec_callbacks_(0),
57 last_rec_callbacks_(0),
58 play_callbacks_(0),
59 last_play_callbacks_(0),
60 rec_samples_(0),
61 last_rec_samples_(0),
62 play_samples_(0),
63 last_play_samples_(0),
henrikaf06f35a2016-09-09 14:23:11 +020064 max_rec_level_(0),
65 max_play_level_(0),
henrikaf5022222016-11-07 15:56:59 +010066 last_timer_task_time_(0),
henrika3355f6d2016-10-21 12:45:25 +020067 rec_stat_count_(0),
henrikaba156cf2016-10-31 08:18:50 -070068 play_stat_count_(0),
69 play_start_time_(0),
70 rec_start_time_(0),
71 only_silence_recorded_(true) {
henrika3f33e2a2016-07-06 00:33:57 -070072 LOG(INFO) << "AudioDeviceBuffer::ctor";
henrikaf5022222016-11-07 15:56:59 +010073 playout_thread_checker_.DetachFromThread();
74 recording_thread_checker_.DetachFromThread();
niklase@google.com470e71d2011-07-07 08:21:25 +000075}
76
henrika0fd68012016-07-04 13:01:19 +020077AudioDeviceBuffer::~AudioDeviceBuffer() {
henrikaf5022222016-11-07 15:56:59 +010078 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -070079 RTC_DCHECK(!playing_);
80 RTC_DCHECK(!recording_);
henrika3f33e2a2016-07-06 00:33:57 -070081 LOG(INFO) << "AudioDeviceBuffer::~dtor";
niklase@google.com470e71d2011-07-07 08:21:25 +000082}
83
henrika0fd68012016-07-04 13:01:19 +020084int32_t AudioDeviceBuffer::RegisterAudioCallback(
henrika49810512016-08-22 05:56:12 -070085 AudioTransport* audio_callback) {
henrikaf5022222016-11-07 15:56:59 +010086 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrika3f33e2a2016-07-06 00:33:57 -070087 LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +010088 if (playing_ || recording_) {
89 LOG(LS_ERROR) << "Failed to set audio transport since media was active";
90 return -1;
91 }
henrika49810512016-08-22 05:56:12 -070092 audio_transport_cb_ = audio_callback;
henrika0fd68012016-07-04 13:01:19 +020093 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000094}
95
henrikaba156cf2016-10-31 08:18:50 -070096void AudioDeviceBuffer::StartPlayout() {
henrikaf5022222016-11-07 15:56:59 +010097 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -070098 // TODO(henrika): allow for usage of DCHECK(!playing_) here instead. Today the
99 // ADM allows calling Start(), Start() by ignoring the second call but it
100 // makes more sense to only allow one call.
101 if (playing_) {
102 return;
henrika6c4d0f02016-07-14 05:54:19 -0700103 }
henrikaba156cf2016-10-31 08:18:50 -0700104 LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +0100105 playout_thread_checker_.DetachFromThread();
henrikaba156cf2016-10-31 08:18:50 -0700106 // Clear members tracking playout stats and do it on the task queue.
107 task_queue_.PostTask([this] { ResetPlayStats(); });
108 // Start a periodic timer based on task queue if not already done by the
109 // recording side.
110 if (!recording_) {
111 StartPeriodicLogging();
112 }
113 const uint64_t now_time = rtc::TimeMillis();
114 // Clear members that are only touched on the main (creating) thread.
115 play_start_time_ = now_time;
henrikaba156cf2016-10-31 08:18:50 -0700116 playing_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000117}
118
henrikaba156cf2016-10-31 08:18:50 -0700119void AudioDeviceBuffer::StartRecording() {
henrikaf5022222016-11-07 15:56:59 +0100120 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700121 if (recording_) {
122 return;
henrika6c4d0f02016-07-14 05:54:19 -0700123 }
henrikaba156cf2016-10-31 08:18:50 -0700124 LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +0100125 recording_thread_checker_.DetachFromThread();
henrikaba156cf2016-10-31 08:18:50 -0700126 // Clear members tracking recording stats and do it on the task queue.
127 task_queue_.PostTask([this] { ResetRecStats(); });
128 // Start a periodic timer based on task queue if not already done by the
129 // playout side.
130 if (!playing_) {
131 StartPeriodicLogging();
132 }
133 // Clear members that will be touched on the main (creating) thread.
134 rec_start_time_ = rtc::TimeMillis();
135 recording_ = true;
136 // And finally a member which can be modified on the native audio thread.
137 // It is safe to do so since we know by design that the owning ADM has not
138 // yet started the native audio recording.
139 only_silence_recorded_ = true;
140}
141
142void AudioDeviceBuffer::StopPlayout() {
henrikaf5022222016-11-07 15:56:59 +0100143 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700144 if (!playing_) {
145 return;
146 }
147 LOG(INFO) << __FUNCTION__;
148 playing_ = false;
149 // Stop periodic logging if no more media is active.
150 if (!recording_) {
151 StopPeriodicLogging();
152 }
henrikaf5022222016-11-07 15:56:59 +0100153 LOG(INFO) << "total playout time: " << rtc::TimeSince(play_start_time_);
henrikaba156cf2016-10-31 08:18:50 -0700154}
155
156void AudioDeviceBuffer::StopRecording() {
henrikaf5022222016-11-07 15:56:59 +0100157 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700158 if (!recording_) {
159 return;
160 }
161 LOG(INFO) << __FUNCTION__;
162 recording_ = false;
163 // Stop periodic logging if no more media is active.
164 if (!playing_) {
165 StopPeriodicLogging();
166 }
167 // Add UMA histogram to keep track of the case when only zeros have been
168 // recorded. Measurements (max of absolute level) are taken twice per second,
169 // which means that if e.g 10 seconds of audio has been recorded, a total of
170 // 20 level estimates must all be identical to zero to trigger the histogram.
171 // |only_silence_recorded_| can only be cleared on the native audio thread
172 // that drives audio capture but we know by design that the audio has stopped
173 // when this method is called, hence there should not be aby conflicts. Also,
174 // the fact that |only_silence_recorded_| can be affected during the complete
175 // call makes chances of conflicts with potentially one last callback very
176 // small.
177 const size_t time_since_start = rtc::TimeSince(rec_start_time_);
178 if (time_since_start > kMinValidCallTimeTimeInMilliseconds) {
179 const int only_zeros = static_cast<int>(only_silence_recorded_);
180 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", only_zeros);
181 LOG(INFO) << "HISTOGRAM(WebRTC.Audio.RecordedOnlyZeros): " << only_zeros;
182 }
183 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());
henrika3f33e2a2016-07-06 00:33:57 -0700188 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());
henrika3f33e2a2016-07-06 00:33:57 -0700195 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());
henrika49810512016-08-22 05:56:12 -0700212 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());
henrika49810512016-08-22 05:56:12 -0700219 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 +0200224int32_t AudioDeviceBuffer::SetRecordingChannel(
225 const AudioDeviceModule::ChannelType channel) {
henrika5588a132016-10-18 05:14:30 -0700226 LOG(INFO) << "SetRecordingChannel(" << channel << ")";
227 LOG(LS_WARNING) << "Not implemented";
228 // Add DCHECK to ensure that user does not try to use this API with a non-
229 // default parameter.
230 RTC_DCHECK_EQ(channel, AudioDeviceModule::kChannelBoth);
231 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000232}
233
henrika0fd68012016-07-04 13:01:19 +0200234int32_t AudioDeviceBuffer::RecordingChannel(
235 AudioDeviceModule::ChannelType& channel) const {
henrika5588a132016-10-18 05:14:30 -0700236 LOG(LS_WARNING) << "Not implemented";
237 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000238}
239
henrika0fd68012016-07-04 13:01:19 +0200240size_t AudioDeviceBuffer::RecordingChannels() const {
henrikaf5022222016-11-07 15:56:59 +0100241 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700242 return rec_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000243}
244
henrika0fd68012016-07-04 13:01:19 +0200245size_t AudioDeviceBuffer::PlayoutChannels() const {
henrikaf5022222016-11-07 15:56:59 +0100246 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700247 return play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000248}
249
henrika0fd68012016-07-04 13:01:19 +0200250int32_t AudioDeviceBuffer::SetCurrentMicLevel(uint32_t level) {
henrikaf5022222016-11-07 15:56:59 +0100251#if !defined(WEBRTC_WIN)
252 // Windows uses a dedicated thread for volume APIs.
253 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
254#endif
henrika49810512016-08-22 05:56:12 -0700255 current_mic_level_ = level;
henrika0fd68012016-07-04 13:01:19 +0200256 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000257}
258
henrika49810512016-08-22 05:56:12 -0700259int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) {
henrikaf5022222016-11-07 15:56:59 +0100260 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700261 typing_status_ = typing_status;
henrika0fd68012016-07-04 13:01:19 +0200262 return 0;
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +0000263}
264
henrika0fd68012016-07-04 13:01:19 +0200265uint32_t AudioDeviceBuffer::NewMicLevel() const {
henrikaf5022222016-11-07 15:56:59 +0100266 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700267 return new_mic_level_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000268}
269
henrika49810512016-08-22 05:56:12 -0700270void AudioDeviceBuffer::SetVQEData(int play_delay_ms,
271 int rec_delay_ms,
272 int clock_drift) {
henrikaf5022222016-11-07 15:56:59 +0100273 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700274 play_delay_ms_ = play_delay_ms;
275 rec_delay_ms_ = rec_delay_ms;
276 clock_drift_ = clock_drift;
niklase@google.com470e71d2011-07-07 08:21:25 +0000277}
278
pbos@webrtc.org25509882013-04-09 10:30:35 +0000279int32_t AudioDeviceBuffer::StartInputFileRecording(
henrika0fd68012016-07-04 13:01:19 +0200280 const char fileName[kAdmMaxFileNameSize]) {
henrika49810512016-08-22 05:56:12 -0700281 LOG(LS_WARNING) << "Not implemented";
282 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000283}
284
henrika0fd68012016-07-04 13:01:19 +0200285int32_t AudioDeviceBuffer::StopInputFileRecording() {
henrika49810512016-08-22 05:56:12 -0700286 LOG(LS_WARNING) << "Not implemented";
henrika0fd68012016-07-04 13:01:19 +0200287 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000288}
289
pbos@webrtc.org25509882013-04-09 10:30:35 +0000290int32_t AudioDeviceBuffer::StartOutputFileRecording(
henrika0fd68012016-07-04 13:01:19 +0200291 const char fileName[kAdmMaxFileNameSize]) {
henrika49810512016-08-22 05:56:12 -0700292 LOG(LS_WARNING) << "Not implemented";
henrikacf327b42016-08-19 16:37:53 +0200293 return 0;
294}
295
henrika49810512016-08-22 05:56:12 -0700296int32_t AudioDeviceBuffer::StopOutputFileRecording() {
297 LOG(LS_WARNING) << "Not implemented";
298 return 0;
299}
300
301int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer,
henrika51e96082016-11-10 00:40:37 -0800302 size_t samples_per_channel) {
henrikaf5022222016-11-07 15:56:59 +0100303 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika5588a132016-10-18 05:14:30 -0700304 // Copy the complete input buffer to the local buffer.
henrika5588a132016-10-18 05:14:30 -0700305 const size_t old_size = rec_buffer_.size();
henrika51e96082016-11-10 00:40:37 -0800306 rec_buffer_.SetData(static_cast<const int16_t*>(audio_buffer),
307 rec_channels_ * samples_per_channel);
henrika5588a132016-10-18 05:14:30 -0700308 // Keep track of the size of the recording buffer. Only updated when the
309 // size changes, which is a rare event.
310 if (old_size != rec_buffer_.size()) {
311 LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size();
henrika0fd68012016-07-04 13:01:19 +0200312 }
henrika51e96082016-11-10 00:40:37 -0800313
henrikaba156cf2016-10-31 08:18:50 -0700314 // Derive a new level value twice per second and check if it is non-zero.
henrika3355f6d2016-10-21 12:45:25 +0200315 int16_t max_abs = 0;
316 RTC_DCHECK_LT(rec_stat_count_, 50);
317 if (++rec_stat_count_ >= 50) {
henrika3355f6d2016-10-21 12:45:25 +0200318 // Returns the largest absolute value in a signed 16-bit vector.
henrika51e96082016-11-10 00:40:37 -0800319 max_abs = WebRtcSpl_MaxAbsValueW16(rec_buffer_.data(), rec_buffer_.size());
henrika3355f6d2016-10-21 12:45:25 +0200320 rec_stat_count_ = 0;
henrikaba156cf2016-10-31 08:18:50 -0700321 // Set |only_silence_recorded_| to false as soon as at least one detection
322 // of a non-zero audio packet is found. It can only be restored to true
323 // again by restarting the call.
324 if (max_abs > 0) {
325 only_silence_recorded_ = false;
326 }
henrika3355f6d2016-10-21 12:45:25 +0200327 }
henrika6c4d0f02016-07-14 05:54:19 -0700328 // Update some stats but do it on the task queue to ensure that the members
henrika3355f6d2016-10-21 12:45:25 +0200329 // are modified and read on the same thread. Note that |max_abs| will be
330 // zero in most calls and then have no effect of the stats. It is only updated
331 // approximately two times per second and can then change the stats.
henrika51e96082016-11-10 00:40:37 -0800332 task_queue_.PostTask([this, max_abs, samples_per_channel] {
333 UpdateRecStats(max_abs, samples_per_channel);
334 });
henrika0fd68012016-07-04 13:01:19 +0200335 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000336}
337
henrika0fd68012016-07-04 13:01:19 +0200338int32_t AudioDeviceBuffer::DeliverRecordedData() {
henrikaf5022222016-11-07 15:56:59 +0100339 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700340 if (!audio_transport_cb_) {
henrika3f33e2a2016-07-06 00:33:57 -0700341 LOG(LS_WARNING) << "Invalid audio transport";
niklase@google.com470e71d2011-07-07 08:21:25 +0000342 return 0;
henrika0fd68012016-07-04 13:01:19 +0200343 }
henrika51e96082016-11-10 00:40:37 -0800344 const size_t frames = rec_buffer_.size() / rec_channels_;
345 const size_t bytes_per_frame = rec_channels_ * sizeof(int16_t);
henrika5588a132016-10-18 05:14:30 -0700346 uint32_t new_mic_level(0);
347 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_;
henrika5588a132016-10-18 05:14:30 -0700348 int32_t res = audio_transport_cb_->RecordedDataIsAvailable(
henrika51e96082016-11-10 00:40:37 -0800349 rec_buffer_.data(), frames, bytes_per_frame, rec_channels_,
henrika5588a132016-10-18 05:14:30 -0700350 rec_sample_rate_, total_delay_ms, clock_drift_, current_mic_level_,
351 typing_status_, new_mic_level);
henrika0fd68012016-07-04 13:01:19 +0200352 if (res != -1) {
henrika5588a132016-10-18 05:14:30 -0700353 new_mic_level_ = new_mic_level;
henrika49810512016-08-22 05:56:12 -0700354 } else {
355 LOG(LS_ERROR) << "RecordedDataIsAvailable() failed";
henrika0fd68012016-07-04 13:01:19 +0200356 }
henrika0fd68012016-07-04 13:01:19 +0200357 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000358}
359
henrika51e96082016-11-10 00:40:37 -0800360int32_t AudioDeviceBuffer::RequestPlayoutData(size_t samples_per_channel) {
henrikaf5022222016-11-07 15:56:59 +0100361 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
henrika51e96082016-11-10 00:40:37 -0800362 // The consumer can change the requested size on the fly and we therefore
henrika5588a132016-10-18 05:14:30 -0700363 // resize the buffer accordingly. Also takes place at the first call to this
364 // method.
henrika51e96082016-11-10 00:40:37 -0800365 const size_t total_samples = play_channels_ * samples_per_channel;
366 if (play_buffer_.size() != total_samples) {
367 play_buffer_.SetSize(total_samples);
henrika5588a132016-10-18 05:14:30 -0700368 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size();
369 }
370
henrika49810512016-08-22 05:56:12 -0700371 size_t num_samples_out(0);
henrikaf5022222016-11-07 15:56:59 +0100372 // It is currently supported to start playout without a valid audio
373 // transport object. Leads to warning and silence.
374 if (!audio_transport_cb_) {
375 LOG(LS_WARNING) << "Invalid audio transport";
376 return 0;
377 }
henrikaba156cf2016-10-31 08:18:50 -0700378
henrikaf5022222016-11-07 15:56:59 +0100379 // Retrieve new 16-bit PCM audio data using the audio transport instance.
380 int64_t elapsed_time_ms = -1;
381 int64_t ntp_time_ms = -1;
henrika51e96082016-11-10 00:40:37 -0800382 const size_t bytes_per_frame = play_channels_ * sizeof(int16_t);
henrikaf5022222016-11-07 15:56:59 +0100383 uint32_t res = audio_transport_cb_->NeedMorePlayData(
henrika51e96082016-11-10 00:40:37 -0800384 samples_per_channel, bytes_per_frame, play_channels_, play_sample_rate_,
henrikaf5022222016-11-07 15:56:59 +0100385 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms);
386 if (res != 0) {
387 LOG(LS_ERROR) << "NeedMorePlayData() failed";
henrika0fd68012016-07-04 13:01:19 +0200388 }
389
henrika3355f6d2016-10-21 12:45:25 +0200390 // Derive a new level value twice per second.
391 int16_t max_abs = 0;
392 RTC_DCHECK_LT(play_stat_count_, 50);
393 if (++play_stat_count_ >= 50) {
henrika3355f6d2016-10-21 12:45:25 +0200394 // Returns the largest absolute value in a signed 16-bit vector.
henrika51e96082016-11-10 00:40:37 -0800395 max_abs =
396 WebRtcSpl_MaxAbsValueW16(play_buffer_.data(), play_buffer_.size());
henrika3355f6d2016-10-21 12:45:25 +0200397 play_stat_count_ = 0;
398 }
399 // Update some stats but do it on the task queue to ensure that the members
400 // are modified and read on the same thread. Note that |max_abs| will be
401 // zero in most calls and then have no effect of the stats. It is only updated
402 // approximately two times per second and can then change the stats.
henrikaba156cf2016-10-31 08:18:50 -0700403 task_queue_.PostTask([this, max_abs, num_samples_out] {
404 UpdatePlayStats(max_abs, num_samples_out);
405 });
henrika49810512016-08-22 05:56:12 -0700406 return static_cast<int32_t>(num_samples_out);
niklase@google.com470e71d2011-07-07 08:21:25 +0000407}
408
henrika49810512016-08-22 05:56:12 -0700409int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) {
henrikaf5022222016-11-07 15:56:59 +0100410 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
henrika5588a132016-10-18 05:14:30 -0700411 RTC_DCHECK_GT(play_buffer_.size(), 0u);
henrika51e96082016-11-10 00:40:37 -0800412 const size_t bytes_per_sample = sizeof(int16_t);
413 memcpy(audio_buffer, play_buffer_.data(),
414 play_buffer_.size() * bytes_per_sample);
415 // Return samples per channel or number of frames.
416 return static_cast<int32_t>(play_buffer_.size() / play_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000417}
418
henrikaba156cf2016-10-31 08:18:50 -0700419void AudioDeviceBuffer::StartPeriodicLogging() {
420 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
421 AudioDeviceBuffer::LOG_START));
henrika6c4d0f02016-07-14 05:54:19 -0700422}
423
henrikaba156cf2016-10-31 08:18:50 -0700424void AudioDeviceBuffer::StopPeriodicLogging() {
425 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
426 AudioDeviceBuffer::LOG_STOP));
427}
428
429void AudioDeviceBuffer::LogStats(LogState state) {
henrikaf5022222016-11-07 15:56:59 +0100430 RTC_DCHECK_RUN_ON(&task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700431 int64_t now_time = rtc::TimeMillis();
henrikaba156cf2016-10-31 08:18:50 -0700432 if (state == AudioDeviceBuffer::LOG_START) {
433 // Reset counters at start. We will not add any logging in this state but
434 // the timer will started by posting a new (delayed) task.
435 num_stat_reports_ = 0;
436 last_timer_task_time_ = now_time;
437 } else if (state == AudioDeviceBuffer::LOG_STOP) {
438 // Stop logging and posting new tasks.
439 return;
440 } else if (state == AudioDeviceBuffer::LOG_ACTIVE) {
441 // Default state. Just keep on logging.
442 }
henrika6c4d0f02016-07-14 05:54:19 -0700443
henrikaba156cf2016-10-31 08:18:50 -0700444 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds;
445 int64_t time_since_last = rtc::TimeDiff(now_time, last_timer_task_time_);
446 last_timer_task_time_ = now_time;
447
448 // Log the latest statistics but skip the first round just after state was
449 // set to LOG_START. Hence, first printed log will be after ~10 seconds.
henrikaa6d26ec2016-09-20 04:44:04 -0700450 if (++num_stat_reports_ > 1 && time_since_last > 0) {
henrika6c4d0f02016-07-14 05:54:19 -0700451 uint32_t diff_samples = rec_samples_ - last_rec_samples_;
henrikaa6d26ec2016-09-20 04:44:04 -0700452 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
henrika6c4d0f02016-07-14 05:54:19 -0700453 LOG(INFO) << "[REC : " << time_since_last << "msec, "
henrika49810512016-08-22 05:56:12 -0700454 << rec_sample_rate_ / 1000
henrika6c4d0f02016-07-14 05:54:19 -0700455 << "kHz] callbacks: " << rec_callbacks_ - last_rec_callbacks_
456 << ", "
457 << "samples: " << diff_samples << ", "
henrikaa6d26ec2016-09-20 04:44:04 -0700458 << "rate: " << static_cast<int>(rate + 0.5) << ", "
henrikaf06f35a2016-09-09 14:23:11 +0200459 << "level: " << max_rec_level_;
henrika6c4d0f02016-07-14 05:54:19 -0700460
461 diff_samples = play_samples_ - last_play_samples_;
henrikaa6d26ec2016-09-20 04:44:04 -0700462 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
henrika6c4d0f02016-07-14 05:54:19 -0700463 LOG(INFO) << "[PLAY: " << time_since_last << "msec, "
henrika49810512016-08-22 05:56:12 -0700464 << play_sample_rate_ / 1000
henrika6c4d0f02016-07-14 05:54:19 -0700465 << "kHz] callbacks: " << play_callbacks_ - last_play_callbacks_
466 << ", "
467 << "samples: " << diff_samples << ", "
henrikaa6d26ec2016-09-20 04:44:04 -0700468 << "rate: " << static_cast<int>(rate + 0.5) << ", "
henrikaf06f35a2016-09-09 14:23:11 +0200469 << "level: " << max_play_level_;
470 }
471
henrika6c4d0f02016-07-14 05:54:19 -0700472 last_rec_callbacks_ = rec_callbacks_;
473 last_play_callbacks_ = play_callbacks_;
474 last_rec_samples_ = rec_samples_;
475 last_play_samples_ = play_samples_;
henrikaf06f35a2016-09-09 14:23:11 +0200476 max_rec_level_ = 0;
477 max_play_level_ = 0;
henrika6c4d0f02016-07-14 05:54:19 -0700478
479 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis();
480 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval";
481
henrikaba156cf2016-10-31 08:18:50 -0700482 // Keep posting new (delayed) tasks until state is changed to kLogStop.
483 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
484 AudioDeviceBuffer::LOG_ACTIVE),
henrika6c4d0f02016-07-14 05:54:19 -0700485 time_to_wait_ms);
486}
487
henrikaf06f35a2016-09-09 14:23:11 +0200488void AudioDeviceBuffer::ResetRecStats() {
henrikaf5022222016-11-07 15:56:59 +0100489 RTC_DCHECK_RUN_ON(&task_queue_);
henrikaf06f35a2016-09-09 14:23:11 +0200490 rec_callbacks_ = 0;
491 last_rec_callbacks_ = 0;
492 rec_samples_ = 0;
493 last_rec_samples_ = 0;
494 max_rec_level_ = 0;
henrikaf06f35a2016-09-09 14:23:11 +0200495}
496
497void AudioDeviceBuffer::ResetPlayStats() {
henrikaf5022222016-11-07 15:56:59 +0100498 RTC_DCHECK_RUN_ON(&task_queue_);
henrikaf06f35a2016-09-09 14:23:11 +0200499 play_callbacks_ = 0;
500 last_play_callbacks_ = 0;
501 play_samples_ = 0;
502 last_play_samples_ = 0;
503 max_play_level_ = 0;
504}
505
henrika51e96082016-11-10 00:40:37 -0800506void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs,
507 size_t samples_per_channel) {
henrikaf5022222016-11-07 15:56:59 +0100508 RTC_DCHECK_RUN_ON(&task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700509 ++rec_callbacks_;
henrika51e96082016-11-10 00:40:37 -0800510 rec_samples_ += samples_per_channel;
henrika3355f6d2016-10-21 12:45:25 +0200511 if (max_abs > max_rec_level_) {
512 max_rec_level_ = max_abs;
henrikaf06f35a2016-09-09 14:23:11 +0200513 }
henrika6c4d0f02016-07-14 05:54:19 -0700514}
515
henrika51e96082016-11-10 00:40:37 -0800516void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs,
517 size_t samples_per_channel) {
henrikaf5022222016-11-07 15:56:59 +0100518 RTC_DCHECK_RUN_ON(&task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700519 ++play_callbacks_;
henrika51e96082016-11-10 00:40:37 -0800520 play_samples_ += samples_per_channel;
henrika3355f6d2016-10-21 12:45:25 +0200521 if (max_abs > max_play_level_) {
522 max_play_level_ = max_abs;
henrikaf06f35a2016-09-09 14:23:11 +0200523 }
henrika6c4d0f02016-07-14 05:54:19 -0700524}
525
niklase@google.com470e71d2011-07-07 08:21:25 +0000526} // namespace webrtc