blob: 19d45b28569feaa9688449a73fb788e22915f9c2 [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
henrikaf5022222016-11-07 15:56:59 +010025#include "webrtc/base/platform_thread.h"
26
niklase@google.com470e71d2011-07-07 08:21:25 +000027namespace webrtc {
28
henrika6c4d0f02016-07-14 05:54:19 -070029static const char kTimerQueueName[] = "AudioDeviceBufferTimer";
30
31// Time between two sucessive calls to LogStats().
32static const size_t kTimerIntervalInSeconds = 10;
33static const size_t kTimerIntervalInMilliseconds =
34 kTimerIntervalInSeconds * rtc::kNumMillisecsPerSec;
henrikaba156cf2016-10-31 08:18:50 -070035// Min time required to qualify an audio session as a "call". If playout or
36// recording has been active for less than this time we will not store any
37// logs or UMA stats but instead consider the call as too short.
38static const size_t kMinValidCallTimeTimeInSeconds = 10;
39static const size_t kMinValidCallTimeTimeInMilliseconds =
40 kMinValidCallTimeTimeInSeconds * rtc::kNumMillisecsPerSec;
henrika6c4d0f02016-07-14 05:54:19 -070041
henrika0fd68012016-07-04 13:01:19 +020042AudioDeviceBuffer::AudioDeviceBuffer()
henrikaf5022222016-11-07 15:56:59 +010043 : task_queue_(kTimerQueueName),
44 audio_transport_cb_(nullptr),
henrika49810512016-08-22 05:56:12 -070045 rec_sample_rate_(0),
46 play_sample_rate_(0),
47 rec_channels_(0),
48 play_channels_(0),
henrikaf5022222016-11-07 15:56:59 +010049 playing_(false),
50 recording_(false),
henrika49810512016-08-22 05:56:12 -070051 current_mic_level_(0),
52 new_mic_level_(0),
53 typing_status_(false),
54 play_delay_ms_(0),
55 rec_delay_ms_(0),
56 clock_drift_(0),
henrika6c4d0f02016-07-14 05:54:19 -070057 num_stat_reports_(0),
58 rec_callbacks_(0),
59 last_rec_callbacks_(0),
60 play_callbacks_(0),
61 last_play_callbacks_(0),
62 rec_samples_(0),
63 last_rec_samples_(0),
64 play_samples_(0),
65 last_play_samples_(0),
henrikaf06f35a2016-09-09 14:23:11 +020066 max_rec_level_(0),
67 max_play_level_(0),
henrikaf5022222016-11-07 15:56:59 +010068 last_timer_task_time_(0),
henrika3355f6d2016-10-21 12:45:25 +020069 rec_stat_count_(0),
henrikaba156cf2016-10-31 08:18:50 -070070 play_stat_count_(0),
71 play_start_time_(0),
72 rec_start_time_(0),
73 only_silence_recorded_(true) {
henrika3f33e2a2016-07-06 00:33:57 -070074 LOG(INFO) << "AudioDeviceBuffer::ctor";
henrikaf5022222016-11-07 15:56:59 +010075 playout_thread_checker_.DetachFromThread();
76 recording_thread_checker_.DetachFromThread();
niklase@google.com470e71d2011-07-07 08:21:25 +000077}
78
henrika0fd68012016-07-04 13:01:19 +020079AudioDeviceBuffer::~AudioDeviceBuffer() {
henrikaf5022222016-11-07 15:56:59 +010080 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -070081 RTC_DCHECK(!playing_);
82 RTC_DCHECK(!recording_);
henrika3f33e2a2016-07-06 00:33:57 -070083 LOG(INFO) << "AudioDeviceBuffer::~dtor";
niklase@google.com470e71d2011-07-07 08:21:25 +000084}
85
henrika0fd68012016-07-04 13:01:19 +020086int32_t AudioDeviceBuffer::RegisterAudioCallback(
henrika49810512016-08-22 05:56:12 -070087 AudioTransport* audio_callback) {
henrikaf5022222016-11-07 15:56:59 +010088 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrika3f33e2a2016-07-06 00:33:57 -070089 LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +010090 if (playing_ || recording_) {
91 LOG(LS_ERROR) << "Failed to set audio transport since media was active";
92 return -1;
93 }
henrika49810512016-08-22 05:56:12 -070094 audio_transport_cb_ = audio_callback;
henrika0fd68012016-07-04 13:01:19 +020095 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000096}
97
henrikaba156cf2016-10-31 08:18:50 -070098void AudioDeviceBuffer::StartPlayout() {
henrikaf5022222016-11-07 15:56:59 +010099 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700100 // TODO(henrika): allow for usage of DCHECK(!playing_) here instead. Today the
101 // ADM allows calling Start(), Start() by ignoring the second call but it
102 // makes more sense to only allow one call.
103 if (playing_) {
104 return;
henrika6c4d0f02016-07-14 05:54:19 -0700105 }
henrikaba156cf2016-10-31 08:18:50 -0700106 LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +0100107 playout_thread_checker_.DetachFromThread();
henrikaba156cf2016-10-31 08:18:50 -0700108 // Clear members tracking playout stats and do it on the task queue.
109 task_queue_.PostTask([this] { ResetPlayStats(); });
110 // Start a periodic timer based on task queue if not already done by the
111 // recording side.
112 if (!recording_) {
113 StartPeriodicLogging();
114 }
115 const uint64_t now_time = rtc::TimeMillis();
116 // Clear members that are only touched on the main (creating) thread.
117 play_start_time_ = now_time;
henrikaba156cf2016-10-31 08:18:50 -0700118 playing_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000119}
120
henrikaba156cf2016-10-31 08:18:50 -0700121void AudioDeviceBuffer::StartRecording() {
henrikaf5022222016-11-07 15:56:59 +0100122 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700123 if (recording_) {
124 return;
henrika6c4d0f02016-07-14 05:54:19 -0700125 }
henrikaba156cf2016-10-31 08:18:50 -0700126 LOG(INFO) << __FUNCTION__;
henrikaf5022222016-11-07 15:56:59 +0100127 recording_thread_checker_.DetachFromThread();
henrikaba156cf2016-10-31 08:18:50 -0700128 // Clear members tracking recording stats and do it on the task queue.
129 task_queue_.PostTask([this] { ResetRecStats(); });
130 // Start a periodic timer based on task queue if not already done by the
131 // playout side.
132 if (!playing_) {
133 StartPeriodicLogging();
134 }
135 // Clear members that will be touched on the main (creating) thread.
136 rec_start_time_ = rtc::TimeMillis();
137 recording_ = true;
138 // And finally a member which can be modified on the native audio thread.
139 // It is safe to do so since we know by design that the owning ADM has not
140 // yet started the native audio recording.
141 only_silence_recorded_ = true;
142}
143
144void AudioDeviceBuffer::StopPlayout() {
henrikaf5022222016-11-07 15:56:59 +0100145 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700146 if (!playing_) {
147 return;
148 }
149 LOG(INFO) << __FUNCTION__;
150 playing_ = false;
151 // Stop periodic logging if no more media is active.
152 if (!recording_) {
153 StopPeriodicLogging();
154 }
henrikaf5022222016-11-07 15:56:59 +0100155 LOG(INFO) << "total playout time: " << rtc::TimeSince(play_start_time_);
henrikaba156cf2016-10-31 08:18:50 -0700156}
157
158void AudioDeviceBuffer::StopRecording() {
henrikaf5022222016-11-07 15:56:59 +0100159 RTC_DCHECK_RUN_ON(&main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700160 if (!recording_) {
161 return;
162 }
163 LOG(INFO) << __FUNCTION__;
164 recording_ = false;
165 // Stop periodic logging if no more media is active.
166 if (!playing_) {
167 StopPeriodicLogging();
168 }
169 // Add UMA histogram to keep track of the case when only zeros have been
170 // recorded. Measurements (max of absolute level) are taken twice per second,
171 // which means that if e.g 10 seconds of audio has been recorded, a total of
172 // 20 level estimates must all be identical to zero to trigger the histogram.
173 // |only_silence_recorded_| can only be cleared on the native audio thread
174 // that drives audio capture but we know by design that the audio has stopped
175 // when this method is called, hence there should not be aby conflicts. Also,
176 // the fact that |only_silence_recorded_| can be affected during the complete
177 // call makes chances of conflicts with potentially one last callback very
178 // small.
179 const size_t time_since_start = rtc::TimeSince(rec_start_time_);
180 if (time_since_start > kMinValidCallTimeTimeInMilliseconds) {
181 const int only_zeros = static_cast<int>(only_silence_recorded_);
182 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", only_zeros);
183 LOG(INFO) << "HISTOGRAM(WebRTC.Audio.RecordedOnlyZeros): " << only_zeros;
184 }
185 LOG(INFO) << "total recording time: " << time_since_start;
niklase@google.com470e71d2011-07-07 08:21:25 +0000186}
187
henrika0fd68012016-07-04 13:01:19 +0200188int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) {
henrikaf5022222016-11-07 15:56:59 +0100189 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika3f33e2a2016-07-06 00:33:57 -0700190 LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")";
henrika49810512016-08-22 05:56:12 -0700191 rec_sample_rate_ = fsHz;
henrika0fd68012016-07-04 13:01:19 +0200192 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000193}
194
henrika0fd68012016-07-04 13:01:19 +0200195int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) {
henrikaf5022222016-11-07 15:56:59 +0100196 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika3f33e2a2016-07-06 00:33:57 -0700197 LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")";
henrika49810512016-08-22 05:56:12 -0700198 play_sample_rate_ = fsHz;
henrika0fd68012016-07-04 13:01:19 +0200199 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000200}
201
henrika0fd68012016-07-04 13:01:19 +0200202int32_t AudioDeviceBuffer::RecordingSampleRate() const {
henrikaf5022222016-11-07 15:56:59 +0100203 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700204 return rec_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205}
206
henrika0fd68012016-07-04 13:01:19 +0200207int32_t AudioDeviceBuffer::PlayoutSampleRate() const {
henrikaf5022222016-11-07 15:56:59 +0100208 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700209 return play_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000210}
211
henrika0fd68012016-07-04 13:01:19 +0200212int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) {
henrikaf5022222016-11-07 15:56:59 +0100213 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700214 LOG(INFO) << "SetRecordingChannels(" << channels << ")";
henrika49810512016-08-22 05:56:12 -0700215 rec_channels_ = channels;
henrika0fd68012016-07-04 13:01:19 +0200216 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000217}
218
henrika0fd68012016-07-04 13:01:19 +0200219int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) {
henrikaf5022222016-11-07 15:56:59 +0100220 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700221 LOG(INFO) << "SetPlayoutChannels(" << channels << ")";
henrika49810512016-08-22 05:56:12 -0700222 play_channels_ = channels;
henrika0fd68012016-07-04 13:01:19 +0200223 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000224}
225
henrika0fd68012016-07-04 13:01:19 +0200226int32_t AudioDeviceBuffer::SetRecordingChannel(
227 const AudioDeviceModule::ChannelType channel) {
henrika5588a132016-10-18 05:14:30 -0700228 LOG(INFO) << "SetRecordingChannel(" << channel << ")";
229 LOG(LS_WARNING) << "Not implemented";
230 // Add DCHECK to ensure that user does not try to use this API with a non-
231 // default parameter.
232 RTC_DCHECK_EQ(channel, AudioDeviceModule::kChannelBoth);
233 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000234}
235
henrika0fd68012016-07-04 13:01:19 +0200236int32_t AudioDeviceBuffer::RecordingChannel(
237 AudioDeviceModule::ChannelType& channel) const {
henrika5588a132016-10-18 05:14:30 -0700238 LOG(LS_WARNING) << "Not implemented";
239 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000240}
241
henrika0fd68012016-07-04 13:01:19 +0200242size_t AudioDeviceBuffer::RecordingChannels() const {
henrikaf5022222016-11-07 15:56:59 +0100243 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700244 return rec_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000245}
246
henrika0fd68012016-07-04 13:01:19 +0200247size_t AudioDeviceBuffer::PlayoutChannels() const {
henrikaf5022222016-11-07 15:56:59 +0100248 RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
henrika49810512016-08-22 05:56:12 -0700249 return play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000250}
251
henrika0fd68012016-07-04 13:01:19 +0200252int32_t AudioDeviceBuffer::SetCurrentMicLevel(uint32_t level) {
henrikaf5022222016-11-07 15:56:59 +0100253#if !defined(WEBRTC_WIN)
254 // Windows uses a dedicated thread for volume APIs.
255 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
256#endif
henrika49810512016-08-22 05:56:12 -0700257 current_mic_level_ = level;
henrika0fd68012016-07-04 13:01:19 +0200258 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000259}
260
henrika49810512016-08-22 05:56:12 -0700261int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) {
henrikaf5022222016-11-07 15:56:59 +0100262 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700263 typing_status_ = typing_status;
henrika0fd68012016-07-04 13:01:19 +0200264 return 0;
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +0000265}
266
henrika0fd68012016-07-04 13:01:19 +0200267uint32_t AudioDeviceBuffer::NewMicLevel() const {
henrikaf5022222016-11-07 15:56:59 +0100268 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700269 return new_mic_level_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000270}
271
henrika49810512016-08-22 05:56:12 -0700272void AudioDeviceBuffer::SetVQEData(int play_delay_ms,
273 int rec_delay_ms,
274 int clock_drift) {
henrikaf5022222016-11-07 15:56:59 +0100275 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700276 play_delay_ms_ = play_delay_ms;
277 rec_delay_ms_ = rec_delay_ms;
278 clock_drift_ = clock_drift;
niklase@google.com470e71d2011-07-07 08:21:25 +0000279}
280
pbos@webrtc.org25509882013-04-09 10:30:35 +0000281int32_t AudioDeviceBuffer::StartInputFileRecording(
henrika0fd68012016-07-04 13:01:19 +0200282 const char fileName[kAdmMaxFileNameSize]) {
henrika49810512016-08-22 05:56:12 -0700283 LOG(LS_WARNING) << "Not implemented";
284 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000285}
286
henrika0fd68012016-07-04 13:01:19 +0200287int32_t AudioDeviceBuffer::StopInputFileRecording() {
henrika49810512016-08-22 05:56:12 -0700288 LOG(LS_WARNING) << "Not implemented";
henrika0fd68012016-07-04 13:01:19 +0200289 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000290}
291
pbos@webrtc.org25509882013-04-09 10:30:35 +0000292int32_t AudioDeviceBuffer::StartOutputFileRecording(
henrika0fd68012016-07-04 13:01:19 +0200293 const char fileName[kAdmMaxFileNameSize]) {
henrika49810512016-08-22 05:56:12 -0700294 LOG(LS_WARNING) << "Not implemented";
henrikacf327b42016-08-19 16:37:53 +0200295 return 0;
296}
297
henrika49810512016-08-22 05:56:12 -0700298int32_t AudioDeviceBuffer::StopOutputFileRecording() {
299 LOG(LS_WARNING) << "Not implemented";
300 return 0;
301}
302
303int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer,
304 size_t num_samples) {
henrikaf5022222016-11-07 15:56:59 +0100305 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika5588a132016-10-18 05:14:30 -0700306 // Copy the complete input buffer to the local buffer.
henrikaf5022222016-11-07 15:56:59 +0100307 const size_t size_in_bytes = num_samples * rec_channels_ * sizeof(int16_t);
henrika5588a132016-10-18 05:14:30 -0700308 const size_t old_size = rec_buffer_.size();
309 rec_buffer_.SetData(static_cast<const uint8_t*>(audio_buffer), size_in_bytes);
310 // Keep track of the size of the recording buffer. Only updated when the
311 // size changes, which is a rare event.
312 if (old_size != rec_buffer_.size()) {
313 LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size();
henrika0fd68012016-07-04 13:01:19 +0200314 }
henrikaba156cf2016-10-31 08:18:50 -0700315 // Derive a new level value twice per second and check if it is non-zero.
henrika3355f6d2016-10-21 12:45:25 +0200316 int16_t max_abs = 0;
317 RTC_DCHECK_LT(rec_stat_count_, 50);
318 if (++rec_stat_count_ >= 50) {
henrikaf5022222016-11-07 15:56:59 +0100319 const size_t size = num_samples * rec_channels_;
henrika3355f6d2016-10-21 12:45:25 +0200320 // Returns the largest absolute value in a signed 16-bit vector.
321 max_abs = WebRtcSpl_MaxAbsValueW16(
322 reinterpret_cast<const int16_t*>(rec_buffer_.data()), size);
323 rec_stat_count_ = 0;
henrikaba156cf2016-10-31 08:18:50 -0700324 // Set |only_silence_recorded_| to false as soon as at least one detection
325 // of a non-zero audio packet is found. It can only be restored to true
326 // again by restarting the call.
327 if (max_abs > 0) {
328 only_silence_recorded_ = false;
329 }
henrika3355f6d2016-10-21 12:45:25 +0200330 }
henrika6c4d0f02016-07-14 05:54:19 -0700331 // Update some stats but do it on the task queue to ensure that the members
henrika3355f6d2016-10-21 12:45:25 +0200332 // are modified and read on the same thread. Note that |max_abs| will be
333 // zero in most calls and then have no effect of the stats. It is only updated
334 // approximately two times per second and can then change the stats.
henrikaba156cf2016-10-31 08:18:50 -0700335 task_queue_.PostTask(
336 [this, max_abs, num_samples] { UpdateRecStats(max_abs, num_samples); });
henrika0fd68012016-07-04 13:01:19 +0200337 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000338}
339
henrika0fd68012016-07-04 13:01:19 +0200340int32_t AudioDeviceBuffer::DeliverRecordedData() {
henrikaf5022222016-11-07 15:56:59 +0100341 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
henrika49810512016-08-22 05:56:12 -0700342 if (!audio_transport_cb_) {
henrika3f33e2a2016-07-06 00:33:57 -0700343 LOG(LS_WARNING) << "Invalid audio transport";
niklase@google.com470e71d2011-07-07 08:21:25 +0000344 return 0;
henrika0fd68012016-07-04 13:01:19 +0200345 }
henrikaf5022222016-11-07 15:56:59 +0100346 const size_t rec_bytes_per_sample = rec_channels_ * sizeof(int16_t);
henrika5588a132016-10-18 05:14:30 -0700347 uint32_t new_mic_level(0);
348 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_;
349 size_t num_samples = rec_buffer_.size() / rec_bytes_per_sample;
350 int32_t res = audio_transport_cb_->RecordedDataIsAvailable(
henrikaf5022222016-11-07 15:56:59 +0100351 rec_buffer_.data(), num_samples, rec_bytes_per_sample, rec_channels_,
henrika5588a132016-10-18 05:14:30 -0700352 rec_sample_rate_, total_delay_ms, clock_drift_, current_mic_level_,
353 typing_status_, new_mic_level);
henrika0fd68012016-07-04 13:01:19 +0200354 if (res != -1) {
henrika5588a132016-10-18 05:14:30 -0700355 new_mic_level_ = new_mic_level;
henrika49810512016-08-22 05:56:12 -0700356 } else {
357 LOG(LS_ERROR) << "RecordedDataIsAvailable() failed";
henrika0fd68012016-07-04 13:01:19 +0200358 }
henrika0fd68012016-07-04 13:01:19 +0200359 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000360}
361
henrika49810512016-08-22 05:56:12 -0700362int32_t AudioDeviceBuffer::RequestPlayoutData(size_t num_samples) {
henrikaf5022222016-11-07 15:56:59 +0100363 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
henrika5588a132016-10-18 05:14:30 -0700364 // The consumer can change the request size on the fly and we therefore
365 // resize the buffer accordingly. Also takes place at the first call to this
366 // method.
henrikaf5022222016-11-07 15:56:59 +0100367 const size_t play_bytes_per_sample = play_channels_ * sizeof(int16_t);
henrika5588a132016-10-18 05:14:30 -0700368 const size_t size_in_bytes = num_samples * play_bytes_per_sample;
369 if (play_buffer_.size() != size_in_bytes) {
370 play_buffer_.SetSize(size_in_bytes);
371 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size();
372 }
373
henrika49810512016-08-22 05:56:12 -0700374 size_t num_samples_out(0);
henrikaf5022222016-11-07 15:56:59 +0100375 // It is currently supported to start playout without a valid audio
376 // transport object. Leads to warning and silence.
377 if (!audio_transport_cb_) {
378 LOG(LS_WARNING) << "Invalid audio transport";
379 return 0;
380 }
henrikaba156cf2016-10-31 08:18:50 -0700381
henrikaf5022222016-11-07 15:56:59 +0100382 // Retrieve new 16-bit PCM audio data using the audio transport instance.
383 int64_t elapsed_time_ms = -1;
384 int64_t ntp_time_ms = -1;
385 uint32_t res = audio_transport_cb_->NeedMorePlayData(
386 num_samples, play_bytes_per_sample, play_channels_, play_sample_rate_,
387 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms);
388 if (res != 0) {
389 LOG(LS_ERROR) << "NeedMorePlayData() failed";
henrika0fd68012016-07-04 13:01:19 +0200390 }
391
henrika3355f6d2016-10-21 12:45:25 +0200392 // Derive a new level value twice per second.
393 int16_t max_abs = 0;
394 RTC_DCHECK_LT(play_stat_count_, 50);
395 if (++play_stat_count_ >= 50) {
henrikaf5022222016-11-07 15:56:59 +0100396 const size_t size = num_samples * play_channels_;
henrika3355f6d2016-10-21 12:45:25 +0200397 // Returns the largest absolute value in a signed 16-bit vector.
398 max_abs = WebRtcSpl_MaxAbsValueW16(
399 reinterpret_cast<const int16_t*>(play_buffer_.data()), size);
400 play_stat_count_ = 0;
401 }
402 // Update some stats but do it on the task queue to ensure that the members
403 // are modified and read on the same thread. Note that |max_abs| will be
404 // zero in most calls and then have no effect of the stats. It is only updated
405 // approximately two times per second and can then change the stats.
henrikaba156cf2016-10-31 08:18:50 -0700406 task_queue_.PostTask([this, max_abs, num_samples_out] {
407 UpdatePlayStats(max_abs, num_samples_out);
408 });
henrika49810512016-08-22 05:56:12 -0700409 return static_cast<int32_t>(num_samples_out);
niklase@google.com470e71d2011-07-07 08:21:25 +0000410}
411
henrika49810512016-08-22 05:56:12 -0700412int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) {
henrikaf5022222016-11-07 15:56:59 +0100413 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
henrika5588a132016-10-18 05:14:30 -0700414 RTC_DCHECK_GT(play_buffer_.size(), 0u);
henrikaf5022222016-11-07 15:56:59 +0100415 const size_t play_bytes_per_sample = play_channels_ * sizeof(int16_t);
henrika5588a132016-10-18 05:14:30 -0700416 memcpy(audio_buffer, play_buffer_.data(), play_buffer_.size());
417 return static_cast<int32_t>(play_buffer_.size() / play_bytes_per_sample);
niklase@google.com470e71d2011-07-07 08:21:25 +0000418}
419
henrikaba156cf2016-10-31 08:18:50 -0700420void AudioDeviceBuffer::StartPeriodicLogging() {
421 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
422 AudioDeviceBuffer::LOG_START));
henrika6c4d0f02016-07-14 05:54:19 -0700423}
424
henrikaba156cf2016-10-31 08:18:50 -0700425void AudioDeviceBuffer::StopPeriodicLogging() {
426 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
427 AudioDeviceBuffer::LOG_STOP));
428}
429
430void AudioDeviceBuffer::LogStats(LogState state) {
henrikaf5022222016-11-07 15:56:59 +0100431 RTC_DCHECK_RUN_ON(&task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700432 int64_t now_time = rtc::TimeMillis();
henrikaba156cf2016-10-31 08:18:50 -0700433 if (state == AudioDeviceBuffer::LOG_START) {
434 // Reset counters at start. We will not add any logging in this state but
435 // the timer will started by posting a new (delayed) task.
436 num_stat_reports_ = 0;
437 last_timer_task_time_ = now_time;
438 } else if (state == AudioDeviceBuffer::LOG_STOP) {
439 // Stop logging and posting new tasks.
440 return;
441 } else if (state == AudioDeviceBuffer::LOG_ACTIVE) {
442 // Default state. Just keep on logging.
443 }
henrika6c4d0f02016-07-14 05:54:19 -0700444
henrikaba156cf2016-10-31 08:18:50 -0700445 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds;
446 int64_t time_since_last = rtc::TimeDiff(now_time, last_timer_task_time_);
447 last_timer_task_time_ = now_time;
448
449 // Log the latest statistics but skip the first round just after state was
450 // set to LOG_START. Hence, first printed log will be after ~10 seconds.
henrikaa6d26ec2016-09-20 04:44:04 -0700451 if (++num_stat_reports_ > 1 && time_since_last > 0) {
henrika6c4d0f02016-07-14 05:54:19 -0700452 uint32_t diff_samples = rec_samples_ - last_rec_samples_;
henrikaa6d26ec2016-09-20 04:44:04 -0700453 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
henrika6c4d0f02016-07-14 05:54:19 -0700454 LOG(INFO) << "[REC : " << time_since_last << "msec, "
henrika49810512016-08-22 05:56:12 -0700455 << rec_sample_rate_ / 1000
henrika6c4d0f02016-07-14 05:54:19 -0700456 << "kHz] callbacks: " << rec_callbacks_ - last_rec_callbacks_
457 << ", "
458 << "samples: " << diff_samples << ", "
henrikaa6d26ec2016-09-20 04:44:04 -0700459 << "rate: " << static_cast<int>(rate + 0.5) << ", "
henrikaf06f35a2016-09-09 14:23:11 +0200460 << "level: " << max_rec_level_;
henrika6c4d0f02016-07-14 05:54:19 -0700461
462 diff_samples = play_samples_ - last_play_samples_;
henrikaa6d26ec2016-09-20 04:44:04 -0700463 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
henrika6c4d0f02016-07-14 05:54:19 -0700464 LOG(INFO) << "[PLAY: " << time_since_last << "msec, "
henrika49810512016-08-22 05:56:12 -0700465 << play_sample_rate_ / 1000
henrika6c4d0f02016-07-14 05:54:19 -0700466 << "kHz] callbacks: " << play_callbacks_ - last_play_callbacks_
467 << ", "
468 << "samples: " << diff_samples << ", "
henrikaa6d26ec2016-09-20 04:44:04 -0700469 << "rate: " << static_cast<int>(rate + 0.5) << ", "
henrikaf06f35a2016-09-09 14:23:11 +0200470 << "level: " << max_play_level_;
471 }
472
henrika6c4d0f02016-07-14 05:54:19 -0700473 last_rec_callbacks_ = rec_callbacks_;
474 last_play_callbacks_ = play_callbacks_;
475 last_rec_samples_ = rec_samples_;
476 last_play_samples_ = play_samples_;
henrikaf06f35a2016-09-09 14:23:11 +0200477 max_rec_level_ = 0;
478 max_play_level_ = 0;
henrika6c4d0f02016-07-14 05:54:19 -0700479
480 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis();
481 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval";
482
henrikaba156cf2016-10-31 08:18:50 -0700483 // Keep posting new (delayed) tasks until state is changed to kLogStop.
484 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
485 AudioDeviceBuffer::LOG_ACTIVE),
henrika6c4d0f02016-07-14 05:54:19 -0700486 time_to_wait_ms);
487}
488
henrikaf06f35a2016-09-09 14:23:11 +0200489void AudioDeviceBuffer::ResetRecStats() {
henrikaf5022222016-11-07 15:56:59 +0100490 RTC_DCHECK_RUN_ON(&task_queue_);
henrikaf06f35a2016-09-09 14:23:11 +0200491 rec_callbacks_ = 0;
492 last_rec_callbacks_ = 0;
493 rec_samples_ = 0;
494 last_rec_samples_ = 0;
495 max_rec_level_ = 0;
henrikaf06f35a2016-09-09 14:23:11 +0200496}
497
498void AudioDeviceBuffer::ResetPlayStats() {
henrikaf5022222016-11-07 15:56:59 +0100499 RTC_DCHECK_RUN_ON(&task_queue_);
henrikaf06f35a2016-09-09 14:23:11 +0200500 play_callbacks_ = 0;
501 last_play_callbacks_ = 0;
502 play_samples_ = 0;
503 last_play_samples_ = 0;
504 max_play_level_ = 0;
505}
506
henrika3355f6d2016-10-21 12:45:25 +0200507void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs, size_t num_samples) {
henrikaf5022222016-11-07 15:56:59 +0100508 RTC_DCHECK_RUN_ON(&task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700509 ++rec_callbacks_;
510 rec_samples_ += num_samples;
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
henrika3355f6d2016-10-21 12:45:25 +0200516void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs, size_t num_samples) {
henrikaf5022222016-11-07 15:56:59 +0100517 RTC_DCHECK_RUN_ON(&task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700518 ++play_callbacks_;
519 play_samples_ += num_samples;
henrika3355f6d2016-10-21 12:45:25 +0200520 if (max_abs > max_play_level_) {
521 max_play_level_ = max_abs;
henrikaf06f35a2016-09-09 14:23:11 +0200522 }
henrika6c4d0f02016-07-14 05:54:19 -0700523}
524
niklase@google.com470e71d2011-07-07 08:21:25 +0000525} // namespace webrtc