blob: 8b49a3ffd0b9592586e55a546d9ad2375256205a [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),
henrikaf5022222016-11-07 15:56:59 +010056 last_timer_task_time_(0),
henrika3355f6d2016-10-21 12:45:25 +020057 rec_stat_count_(0),
henrikaba156cf2016-10-31 08:18:50 -070058 play_stat_count_(0),
59 play_start_time_(0),
henrika0b3a6382016-11-11 02:28:50 -080060 only_silence_recorded_(true),
61 log_stats_(false) {
henrika3f33e2a2016-07-06 00:33:57 -070062 LOG(INFO) << "AudioDeviceBuffer::ctor";
henrikaf5022222016-11-07 15:56:59 +010063 playout_thread_checker_.DetachFromThread();
64 recording_thread_checker_.DetachFromThread();
niklase@google.com470e71d2011-07-07 08:21:25 +000065}
66
henrika0fd68012016-07-04 13:01:19 +020067AudioDeviceBuffer::~AudioDeviceBuffer() {
henrikaf5022222016-11-07 15:56:59 +010068 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -070069 RTC_DCHECK(!playing_);
70 RTC_DCHECK(!recording_);
henrika3f33e2a2016-07-06 00:33:57 -070071 LOG(INFO) << "AudioDeviceBuffer::~dtor";
niklase@google.com470e71d2011-07-07 08:21:25 +000072}
73
henrika0fd68012016-07-04 13:01:19 +020074int32_t AudioDeviceBuffer::RegisterAudioCallback(
henrika49810512016-08-22 05:56:12 -070075 AudioTransport* audio_callback) {
henrikaf5022222016-11-07 15:56:59 +010076 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrika3f33e2a2016-07-06 00:33:57 -070077 LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +010078 if (playing_ || recording_) {
79 LOG(LS_ERROR) << "Failed to set audio transport since media was active";
80 return -1;
81 }
henrika49810512016-08-22 05:56:12 -070082 audio_transport_cb_ = audio_callback;
henrika0fd68012016-07-04 13:01:19 +020083 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000084}
85
henrikaba156cf2016-10-31 08:18:50 -070086void AudioDeviceBuffer::StartPlayout() {
henrikaf5022222016-11-07 15:56:59 +010087 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -070088 // TODO(henrika): allow for usage of DCHECK(!playing_) here instead. Today the
89 // ADM allows calling Start(), Start() by ignoring the second call but it
90 // makes more sense to only allow one call.
91 if (playing_) {
92 return;
henrika6c4d0f02016-07-14 05:54:19 -070093 }
henrikaba156cf2016-10-31 08:18:50 -070094 LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +010095 playout_thread_checker_.DetachFromThread();
henrikaba156cf2016-10-31 08:18:50 -070096 // Clear members tracking playout stats and do it on the task queue.
97 task_queue_.PostTask([this] { ResetPlayStats(); });
98 // Start a periodic timer based on task queue if not already done by the
99 // recording side.
100 if (!recording_) {
101 StartPeriodicLogging();
102 }
nissedeb95f32016-11-28 01:54:54 -0800103 const int64_t now_time = rtc::TimeMillis();
henrikaba156cf2016-10-31 08:18:50 -0700104 // Clear members that are only touched on the main (creating) thread.
105 play_start_time_ = now_time;
henrikaba156cf2016-10-31 08:18:50 -0700106 playing_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000107}
108
henrikaba156cf2016-10-31 08:18:50 -0700109void AudioDeviceBuffer::StartRecording() {
henrikaf5022222016-11-07 15:56:59 +0100110 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700111 if (recording_) {
112 return;
henrika6c4d0f02016-07-14 05:54:19 -0700113 }
henrikaba156cf2016-10-31 08:18:50 -0700114 LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +0100115 recording_thread_checker_.DetachFromThread();
henrikaba156cf2016-10-31 08:18:50 -0700116 // Clear members tracking recording stats and do it on the task queue.
117 task_queue_.PostTask([this] { ResetRecStats(); });
118 // Start a periodic timer based on task queue if not already done by the
119 // playout side.
120 if (!playing_) {
121 StartPeriodicLogging();
122 }
123 // Clear members that will be touched on the main (creating) thread.
124 rec_start_time_ = rtc::TimeMillis();
125 recording_ = true;
126 // And finally a member which can be modified on the native audio thread.
127 // It is safe to do so since we know by design that the owning ADM has not
128 // yet started the native audio recording.
129 only_silence_recorded_ = true;
130}
131
132void AudioDeviceBuffer::StopPlayout() {
henrikaf5022222016-11-07 15:56:59 +0100133 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700134 if (!playing_) {
135 return;
136 }
137 LOG(INFO) << __FUNCTION__;
138 playing_ = false;
139 // Stop periodic logging if no more media is active.
140 if (!recording_) {
141 StopPeriodicLogging();
142 }
henrikaf5022222016-11-07 15:56:59 +0100143 LOG(INFO) << "total playout time: " << rtc::TimeSince(play_start_time_);
henrikaba156cf2016-10-31 08:18:50 -0700144}
145
146void AudioDeviceBuffer::StopRecording() {
henrikaf5022222016-11-07 15:56:59 +0100147 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700148 if (!recording_) {
149 return;
150 }
151 LOG(INFO) << __FUNCTION__;
152 recording_ = false;
153 // Stop periodic logging if no more media is active.
154 if (!playing_) {
155 StopPeriodicLogging();
156 }
157 // Add UMA histogram to keep track of the case when only zeros have been
158 // recorded. Measurements (max of absolute level) are taken twice per second,
159 // which means that if e.g 10 seconds of audio has been recorded, a total of
160 // 20 level estimates must all be identical to zero to trigger the histogram.
161 // |only_silence_recorded_| can only be cleared on the native audio thread
162 // that drives audio capture but we know by design that the audio has stopped
163 // when this method is called, hence there should not be aby conflicts. Also,
164 // the fact that |only_silence_recorded_| can be affected during the complete
165 // call makes chances of conflicts with potentially one last callback very
166 // small.
167 const size_t time_since_start = rtc::TimeSince(rec_start_time_);
168 if (time_since_start > kMinValidCallTimeTimeInMilliseconds) {
169 const int only_zeros = static_cast<int>(only_silence_recorded_);
170 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", only_zeros);
171 LOG(INFO) << "HISTOGRAM(WebRTC.Audio.RecordedOnlyZeros): " << only_zeros;
172 }
173 LOG(INFO) << "total recording time: " << time_since_start;
niklase@google.com470e71d2011-07-07 08:21:25 +0000174}
175
henrika0fd68012016-07-04 13:01:19 +0200176int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) {
henrikaf5022222016-11-07 15:56:59 +0100177 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika3f33e2a2016-07-06 00:33:57 -0700178 LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")";
henrika49810512016-08-22 05:56:12 -0700179 rec_sample_rate_ = fsHz;
henrika0fd68012016-07-04 13:01:19 +0200180 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000181}
182
henrika0fd68012016-07-04 13:01:19 +0200183int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) {
henrikaf5022222016-11-07 15:56:59 +0100184 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika3f33e2a2016-07-06 00:33:57 -0700185 LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")";
henrika49810512016-08-22 05:56:12 -0700186 play_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::RecordingSampleRate() const {
henrikaf5022222016-11-07 15:56:59 +0100191 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700192 return rec_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000193}
194
henrika0fd68012016-07-04 13:01:19 +0200195int32_t AudioDeviceBuffer::PlayoutSampleRate() const {
henrikaf5022222016-11-07 15:56:59 +0100196 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700197 return play_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000198}
199
henrika0fd68012016-07-04 13:01:19 +0200200int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) {
henrikaf5022222016-11-07 15:56:59 +0100201 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700202 LOG(INFO) << "SetRecordingChannels(" << channels << ")";
henrika49810512016-08-22 05:56:12 -0700203 rec_channels_ = channels;
henrika0fd68012016-07-04 13:01:19 +0200204 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205}
206
henrika0fd68012016-07-04 13:01:19 +0200207int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) {
henrikaf5022222016-11-07 15:56:59 +0100208 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700209 LOG(INFO) << "SetPlayoutChannels(" << channels << ")";
henrika49810512016-08-22 05:56:12 -0700210 play_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::SetRecordingChannel(
215 const AudioDeviceModule::ChannelType channel) {
henrika5588a132016-10-18 05:14:30 -0700216 LOG(INFO) << "SetRecordingChannel(" << channel << ")";
217 LOG(LS_WARNING) << "Not implemented";
218 // Add DCHECK to ensure that user does not try to use this API with a non-
219 // default parameter.
220 RTC_DCHECK_EQ(channel, AudioDeviceModule::kChannelBoth);
221 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000222}
223
henrika0fd68012016-07-04 13:01:19 +0200224int32_t AudioDeviceBuffer::RecordingChannel(
225 AudioDeviceModule::ChannelType& channel) const {
henrika5588a132016-10-18 05:14:30 -0700226 LOG(LS_WARNING) << "Not implemented";
227 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000228}
229
henrika0fd68012016-07-04 13:01:19 +0200230size_t AudioDeviceBuffer::RecordingChannels() const {
henrikaf5022222016-11-07 15:56:59 +0100231 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700232 return rec_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000233}
234
henrika0fd68012016-07-04 13:01:19 +0200235size_t AudioDeviceBuffer::PlayoutChannels() const {
henrikaf5022222016-11-07 15:56:59 +0100236 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700237 return play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000238}
239
henrika0fd68012016-07-04 13:01:19 +0200240int32_t AudioDeviceBuffer::SetCurrentMicLevel(uint32_t level) {
henrikaf5022222016-11-07 15:56:59 +0100241#if !defined(WEBRTC_WIN)
242 // Windows uses a dedicated thread for volume APIs.
243 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
244#endif
henrika49810512016-08-22 05:56:12 -0700245 current_mic_level_ = level;
henrika0fd68012016-07-04 13:01:19 +0200246 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000247}
248
henrika49810512016-08-22 05:56:12 -0700249int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) {
henrikaf5022222016-11-07 15:56:59 +0100250 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700251 typing_status_ = typing_status;
henrika0fd68012016-07-04 13:01:19 +0200252 return 0;
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +0000253}
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
pbos@webrtc.org25509882013-04-09 10:30:35 +0000269int32_t AudioDeviceBuffer::StartInputFileRecording(
henrika0fd68012016-07-04 13:01:19 +0200270 const char fileName[kAdmMaxFileNameSize]) {
henrika49810512016-08-22 05:56:12 -0700271 LOG(LS_WARNING) << "Not implemented";
272 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000273}
274
henrika0fd68012016-07-04 13:01:19 +0200275int32_t AudioDeviceBuffer::StopInputFileRecording() {
henrika49810512016-08-22 05:56:12 -0700276 LOG(LS_WARNING) << "Not implemented";
henrika0fd68012016-07-04 13:01:19 +0200277 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000278}
279
pbos@webrtc.org25509882013-04-09 10:30:35 +0000280int32_t AudioDeviceBuffer::StartOutputFileRecording(
henrika0fd68012016-07-04 13:01:19 +0200281 const char fileName[kAdmMaxFileNameSize]) {
henrika49810512016-08-22 05:56:12 -0700282 LOG(LS_WARNING) << "Not implemented";
henrikacf327b42016-08-19 16:37:53 +0200283 return 0;
284}
285
henrika49810512016-08-22 05:56:12 -0700286int32_t AudioDeviceBuffer::StopOutputFileRecording() {
287 LOG(LS_WARNING) << "Not implemented";
288 return 0;
289}
290
291int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer,
henrika51e96082016-11-10 00:40:37 -0800292 size_t samples_per_channel) {
henrikaf5022222016-11-07 15:56:59 +0100293 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika5588a132016-10-18 05:14:30 -0700294 // Copy the complete input buffer to the local buffer.
henrika5588a132016-10-18 05:14:30 -0700295 const size_t old_size = rec_buffer_.size();
henrika51e96082016-11-10 00:40:37 -0800296 rec_buffer_.SetData(static_cast<const int16_t*>(audio_buffer),
297 rec_channels_ * samples_per_channel);
henrika5588a132016-10-18 05:14:30 -0700298 // Keep track of the size of the recording buffer. Only updated when the
299 // size changes, which is a rare event.
300 if (old_size != rec_buffer_.size()) {
301 LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size();
henrika0fd68012016-07-04 13:01:19 +0200302 }
henrika51e96082016-11-10 00:40:37 -0800303
henrikaba156cf2016-10-31 08:18:50 -0700304 // Derive a new level value twice per second and check if it is non-zero.
henrika3355f6d2016-10-21 12:45:25 +0200305 int16_t max_abs = 0;
306 RTC_DCHECK_LT(rec_stat_count_, 50);
307 if (++rec_stat_count_ >= 50) {
henrika3355f6d2016-10-21 12:45:25 +0200308 // Returns the largest absolute value in a signed 16-bit vector.
henrika51e96082016-11-10 00:40:37 -0800309 max_abs = WebRtcSpl_MaxAbsValueW16(rec_buffer_.data(), rec_buffer_.size());
henrika3355f6d2016-10-21 12:45:25 +0200310 rec_stat_count_ = 0;
henrikaba156cf2016-10-31 08:18:50 -0700311 // Set |only_silence_recorded_| to false as soon as at least one detection
312 // of a non-zero audio packet is found. It can only be restored to true
313 // again by restarting the call.
314 if (max_abs > 0) {
315 only_silence_recorded_ = false;
316 }
henrika3355f6d2016-10-21 12:45:25 +0200317 }
henrika77ce9a52017-02-03 02:19:17 -0800318 // Update recording stats which is used as base for periodic logging of the
319 // audio input state.
320 UpdateRecStats(max_abs, samples_per_channel);
henrika0fd68012016-07-04 13:01:19 +0200321 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000322}
323
henrika0fd68012016-07-04 13:01:19 +0200324int32_t AudioDeviceBuffer::DeliverRecordedData() {
henrikaf5022222016-11-07 15:56:59 +0100325 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700326 if (!audio_transport_cb_) {
henrika3f33e2a2016-07-06 00:33:57 -0700327 LOG(LS_WARNING) << "Invalid audio transport";
niklase@google.com470e71d2011-07-07 08:21:25 +0000328 return 0;
henrika0fd68012016-07-04 13:01:19 +0200329 }
henrika51e96082016-11-10 00:40:37 -0800330 const size_t frames = rec_buffer_.size() / rec_channels_;
331 const size_t bytes_per_frame = rec_channels_ * sizeof(int16_t);
henrika5588a132016-10-18 05:14:30 -0700332 uint32_t new_mic_level(0);
333 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_;
henrika5588a132016-10-18 05:14:30 -0700334 int32_t res = audio_transport_cb_->RecordedDataIsAvailable(
henrika51e96082016-11-10 00:40:37 -0800335 rec_buffer_.data(), frames, bytes_per_frame, rec_channels_,
henrika5588a132016-10-18 05:14:30 -0700336 rec_sample_rate_, total_delay_ms, clock_drift_, current_mic_level_,
337 typing_status_, new_mic_level);
henrika0fd68012016-07-04 13:01:19 +0200338 if (res != -1) {
henrika5588a132016-10-18 05:14:30 -0700339 new_mic_level_ = new_mic_level;
henrika49810512016-08-22 05:56:12 -0700340 } else {
341 LOG(LS_ERROR) << "RecordedDataIsAvailable() failed";
henrika0fd68012016-07-04 13:01:19 +0200342 }
henrika0fd68012016-07-04 13:01:19 +0200343 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000344}
345
henrika51e96082016-11-10 00:40:37 -0800346int32_t AudioDeviceBuffer::RequestPlayoutData(size_t samples_per_channel) {
henrikaf5022222016-11-07 15:56:59 +0100347 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
henrika51e96082016-11-10 00:40:37 -0800348 // The consumer can change the requested size on the fly and we therefore
henrika5588a132016-10-18 05:14:30 -0700349 // resize the buffer accordingly. Also takes place at the first call to this
350 // method.
henrika51e96082016-11-10 00:40:37 -0800351 const size_t total_samples = play_channels_ * samples_per_channel;
352 if (play_buffer_.size() != total_samples) {
353 play_buffer_.SetSize(total_samples);
henrika5588a132016-10-18 05:14:30 -0700354 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size();
355 }
356
henrika49810512016-08-22 05:56:12 -0700357 size_t num_samples_out(0);
henrikaf5022222016-11-07 15:56:59 +0100358 // It is currently supported to start playout without a valid audio
359 // transport object. Leads to warning and silence.
360 if (!audio_transport_cb_) {
361 LOG(LS_WARNING) << "Invalid audio transport";
362 return 0;
363 }
henrikaba156cf2016-10-31 08:18:50 -0700364
henrikaf5022222016-11-07 15:56:59 +0100365 // Retrieve new 16-bit PCM audio data using the audio transport instance.
366 int64_t elapsed_time_ms = -1;
367 int64_t ntp_time_ms = -1;
henrika51e96082016-11-10 00:40:37 -0800368 const size_t bytes_per_frame = play_channels_ * sizeof(int16_t);
henrikaf5022222016-11-07 15:56:59 +0100369 uint32_t res = audio_transport_cb_->NeedMorePlayData(
henrika51e96082016-11-10 00:40:37 -0800370 samples_per_channel, bytes_per_frame, play_channels_, play_sample_rate_,
henrikaf5022222016-11-07 15:56:59 +0100371 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms);
372 if (res != 0) {
373 LOG(LS_ERROR) << "NeedMorePlayData() failed";
henrika0fd68012016-07-04 13:01:19 +0200374 }
375
henrika3355f6d2016-10-21 12:45:25 +0200376 // Derive a new level value twice per second.
377 int16_t max_abs = 0;
378 RTC_DCHECK_LT(play_stat_count_, 50);
379 if (++play_stat_count_ >= 50) {
henrika3355f6d2016-10-21 12:45:25 +0200380 // Returns the largest absolute value in a signed 16-bit vector.
henrika51e96082016-11-10 00:40:37 -0800381 max_abs =
382 WebRtcSpl_MaxAbsValueW16(play_buffer_.data(), play_buffer_.size());
henrika3355f6d2016-10-21 12:45:25 +0200383 play_stat_count_ = 0;
384 }
henrika77ce9a52017-02-03 02:19:17 -0800385 // Update playout stats which is used as base for periodic logging of the
386 // audio output state.
387 UpdatePlayStats(max_abs, num_samples_out);
henrika49810512016-08-22 05:56:12 -0700388 return static_cast<int32_t>(num_samples_out);
niklase@google.com470e71d2011-07-07 08:21:25 +0000389}
390
henrika49810512016-08-22 05:56:12 -0700391int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) {
henrikaf5022222016-11-07 15:56:59 +0100392 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
kwibergaf476c72016-11-28 15:21:39 -0800393 RTC_DCHECK_GT(play_buffer_.size(), 0);
henrika51e96082016-11-10 00:40:37 -0800394 const size_t bytes_per_sample = sizeof(int16_t);
395 memcpy(audio_buffer, play_buffer_.data(),
396 play_buffer_.size() * bytes_per_sample);
397 // Return samples per channel or number of frames.
398 return static_cast<int32_t>(play_buffer_.size() / play_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000399}
400
henrikaba156cf2016-10-31 08:18:50 -0700401void AudioDeviceBuffer::StartPeriodicLogging() {
402 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
403 AudioDeviceBuffer::LOG_START));
henrika6c4d0f02016-07-14 05:54:19 -0700404}
405
henrikaba156cf2016-10-31 08:18:50 -0700406void AudioDeviceBuffer::StopPeriodicLogging() {
407 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
408 AudioDeviceBuffer::LOG_STOP));
409}
410
411void AudioDeviceBuffer::LogStats(LogState state) {
henrikaf5022222016-11-07 15:56:59 +0100412 RTC_DCHECK_RUN_ON(&task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700413 int64_t now_time = rtc::TimeMillis();
henrika0b3a6382016-11-11 02:28:50 -0800414
henrikaba156cf2016-10-31 08:18:50 -0700415 if (state == AudioDeviceBuffer::LOG_START) {
416 // Reset counters at start. We will not add any logging in this state but
417 // the timer will started by posting a new (delayed) task.
418 num_stat_reports_ = 0;
419 last_timer_task_time_ = now_time;
henrika0b3a6382016-11-11 02:28:50 -0800420 log_stats_ = true;
henrikaba156cf2016-10-31 08:18:50 -0700421 } else if (state == AudioDeviceBuffer::LOG_STOP) {
422 // Stop logging and posting new tasks.
henrika0b3a6382016-11-11 02:28:50 -0800423 log_stats_ = false;
henrikaba156cf2016-10-31 08:18:50 -0700424 } else if (state == AudioDeviceBuffer::LOG_ACTIVE) {
henrika0b3a6382016-11-11 02:28:50 -0800425 // Keep logging unless logging was disabled while task was posted.
426 }
427
428 // Avoid adding more logs since we are in STOP mode.
429 if (!log_stats_) {
430 return;
henrikaba156cf2016-10-31 08:18:50 -0700431 }
henrika6c4d0f02016-07-14 05:54:19 -0700432
henrikaba156cf2016-10-31 08:18:50 -0700433 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds;
434 int64_t time_since_last = rtc::TimeDiff(now_time, last_timer_task_time_);
435 last_timer_task_time_ = now_time;
436
henrika77ce9a52017-02-03 02:19:17 -0800437 Stats stats;
438 {
439 rtc::CritScope cs(&lock_);
440 stats = stats_;
441 stats_.max_rec_level = 0;
442 stats_.max_play_level = 0;
443 }
444
henrikaba156cf2016-10-31 08:18:50 -0700445 // Log the latest statistics but skip the first round just after state was
446 // set to LOG_START. Hence, first printed log will be after ~10 seconds.
henrikaa6d26ec2016-09-20 04:44:04 -0700447 if (++num_stat_reports_ > 1 && time_since_last > 0) {
henrika77ce9a52017-02-03 02:19:17 -0800448 uint32_t diff_samples = stats.rec_samples - last_stats_.rec_samples;
henrikaa6d26ec2016-09-20 04:44:04 -0700449 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
henrika6c4d0f02016-07-14 05:54:19 -0700450 LOG(INFO) << "[REC : " << time_since_last << "msec, "
henrika77ce9a52017-02-03 02:19:17 -0800451 << rec_sample_rate_ / 1000 << "kHz] callbacks: "
452 << stats.rec_callbacks - last_stats_.rec_callbacks << ", "
henrika6c4d0f02016-07-14 05:54:19 -0700453 << "samples: " << diff_samples << ", "
henrikaa6d26ec2016-09-20 04:44:04 -0700454 << "rate: " << static_cast<int>(rate + 0.5) << ", "
henrika77ce9a52017-02-03 02:19:17 -0800455 << "level: " << stats.max_rec_level;
henrika6c4d0f02016-07-14 05:54:19 -0700456
henrika77ce9a52017-02-03 02:19:17 -0800457 diff_samples = stats.play_samples - last_stats_.play_samples;
henrikaa6d26ec2016-09-20 04:44:04 -0700458 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
henrika6c4d0f02016-07-14 05:54:19 -0700459 LOG(INFO) << "[PLAY: " << time_since_last << "msec, "
henrika77ce9a52017-02-03 02:19:17 -0800460 << play_sample_rate_ / 1000 << "kHz] callbacks: "
461 << stats.play_callbacks - last_stats_.play_callbacks << ", "
henrika6c4d0f02016-07-14 05:54:19 -0700462 << "samples: " << diff_samples << ", "
henrikaa6d26ec2016-09-20 04:44:04 -0700463 << "rate: " << static_cast<int>(rate + 0.5) << ", "
henrika77ce9a52017-02-03 02:19:17 -0800464 << "level: " << stats.max_play_level;
465 last_stats_ = stats;
henrikaf06f35a2016-09-09 14:23:11 +0200466 }
467
henrika6c4d0f02016-07-14 05:54:19 -0700468 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis();
469 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval";
470
henrikaba156cf2016-10-31 08:18:50 -0700471 // Keep posting new (delayed) tasks until state is changed to kLogStop.
472 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
473 AudioDeviceBuffer::LOG_ACTIVE),
henrika6c4d0f02016-07-14 05:54:19 -0700474 time_to_wait_ms);
475}
476
henrikaf06f35a2016-09-09 14:23:11 +0200477void AudioDeviceBuffer::ResetRecStats() {
henrikaf5022222016-11-07 15:56:59 +0100478 RTC_DCHECK_RUN_ON(&task_queue_);
henrika77ce9a52017-02-03 02:19:17 -0800479 last_stats_.ResetRecStats();
480 rtc::CritScope cs(&lock_);
481 stats_.ResetRecStats();
henrikaf06f35a2016-09-09 14:23:11 +0200482}
483
484void AudioDeviceBuffer::ResetPlayStats() {
henrikaf5022222016-11-07 15:56:59 +0100485 RTC_DCHECK_RUN_ON(&task_queue_);
henrika77ce9a52017-02-03 02:19:17 -0800486 last_stats_.ResetPlayStats();
487 rtc::CritScope cs(&lock_);
488 stats_.ResetPlayStats();
henrikaf06f35a2016-09-09 14:23:11 +0200489}
490
henrika51e96082016-11-10 00:40:37 -0800491void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs,
492 size_t samples_per_channel) {
henrika77ce9a52017-02-03 02:19:17 -0800493 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
494 rtc::CritScope cs(&lock_);
495 ++stats_.rec_callbacks;
496 stats_.rec_samples += samples_per_channel;
497 if (max_abs > stats_.max_rec_level) {
498 stats_.max_rec_level = max_abs;
henrikaf06f35a2016-09-09 14:23:11 +0200499 }
henrika6c4d0f02016-07-14 05:54:19 -0700500}
501
henrika51e96082016-11-10 00:40:37 -0800502void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs,
503 size_t samples_per_channel) {
henrika77ce9a52017-02-03 02:19:17 -0800504 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
505 rtc::CritScope cs(&lock_);
506 ++stats_.play_callbacks;
507 stats_.play_samples += samples_per_channel;
508 if (max_abs > stats_.max_play_level) {
509 stats_.max_play_level = max_abs;
henrikaf06f35a2016-09-09 14:23:11 +0200510 }
henrika6c4d0f02016-07-14 05:54:19 -0700511}
512
niklase@google.com470e71d2011-07-07 08:21:25 +0000513} // namespace webrtc