niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
xians@webrtc.org | 20aabbb | 2012-02-20 09:17:41 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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 | |
henrika | 3d7346f | 2016-07-29 16:20:47 +0200 | [diff] [blame] | 11 | #include <algorithm> |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 12 | #include <cmath> |
henrika | 3d7346f | 2016-07-29 16:20:47 +0200 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "modules/audio_device/audio_device_buffer.h" |
andrew@webrtc.org | 2553450 | 2013-09-13 00:02:13 +0000 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
| 17 | #include "modules/audio_device/audio_device_config.h" |
| 18 | #include "rtc_base/arraysize.h" |
| 19 | #include "rtc_base/bind.h" |
| 20 | #include "rtc_base/checks.h" |
| 21 | #include "rtc_base/format_macros.h" |
| 22 | #include "rtc_base/logging.h" |
henrika | 5b6afc0 | 2018-09-05 14:34:40 +0200 | [diff] [blame] | 23 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/timeutils.h" |
| 25 | #include "system_wrappers/include/metrics.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | namespace webrtc { |
| 28 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 29 | static const char kTimerQueueName[] = "AudioDeviceBufferTimer"; |
| 30 | |
| 31 | // Time between two sucessive calls to LogStats(). |
| 32 | static const size_t kTimerIntervalInSeconds = 10; |
| 33 | static const size_t kTimerIntervalInMilliseconds = |
| 34 | kTimerIntervalInSeconds * rtc::kNumMillisecsPerSec; |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 35 | // 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. |
| 38 | static const size_t kMinValidCallTimeTimeInSeconds = 10; |
| 39 | static const size_t kMinValidCallTimeTimeInMilliseconds = |
| 40 | kMinValidCallTimeTimeInSeconds * rtc::kNumMillisecsPerSec; |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 41 | #ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE |
| 42 | static const double k2Pi = 6.28318530717959; |
| 43 | #endif |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 44 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 45 | AudioDeviceBuffer::AudioDeviceBuffer() |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 46 | : task_queue_(kTimerQueueName), |
| 47 | audio_transport_cb_(nullptr), |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 48 | rec_sample_rate_(0), |
| 49 | play_sample_rate_(0), |
| 50 | rec_channels_(0), |
| 51 | play_channels_(0), |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 52 | playing_(false), |
| 53 | recording_(false), |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 54 | typing_status_(false), |
| 55 | play_delay_ms_(0), |
| 56 | rec_delay_ms_(0), |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 57 | num_stat_reports_(0), |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 58 | last_timer_task_time_(0), |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame] | 59 | rec_stat_count_(0), |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 60 | play_stat_count_(0), |
| 61 | play_start_time_(0), |
henrika | 0b3a638 | 2016-11-11 02:28:50 -0800 | [diff] [blame] | 62 | only_silence_recorded_(true), |
| 63 | log_stats_(false) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 64 | RTC_LOG(INFO) << "AudioDeviceBuffer::ctor"; |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 65 | #ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE |
| 66 | phase_ = 0.0; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 67 | RTC_LOG(WARNING) << "AUDIO_DEVICE_PLAYS_SINUS_TONE is defined!"; |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 68 | #endif |
henrika | 4af7366 | 2017-10-11 13:16:17 +0200 | [diff] [blame] | 69 | WebRtcSpl_Init(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 70 | } |
| 71 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 72 | AudioDeviceBuffer::~AudioDeviceBuffer() { |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 73 | RTC_DCHECK_RUN_ON(&main_thread_checker_); |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 74 | RTC_DCHECK(!playing_); |
| 75 | RTC_DCHECK(!recording_); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 76 | RTC_LOG(INFO) << "AudioDeviceBuffer::~dtor"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 77 | } |
| 78 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 79 | int32_t AudioDeviceBuffer::RegisterAudioCallback( |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 80 | AudioTransport* audio_callback) { |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 81 | RTC_DCHECK_RUN_ON(&main_thread_checker_); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 82 | RTC_LOG(INFO) << __FUNCTION__; |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 83 | if (playing_ || recording_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 84 | RTC_LOG(LS_ERROR) << "Failed to set audio transport since media was active"; |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 85 | return -1; |
| 86 | } |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 87 | audio_transport_cb_ = audio_callback; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 88 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | } |
| 90 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 91 | void AudioDeviceBuffer::StartPlayout() { |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 92 | RTC_DCHECK_RUN_ON(&main_thread_checker_); |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 93 | // TODO(henrika): allow for usage of DCHECK(!playing_) here instead. Today the |
| 94 | // ADM allows calling Start(), Start() by ignoring the second call but it |
| 95 | // makes more sense to only allow one call. |
| 96 | if (playing_) { |
| 97 | return; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 98 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 99 | RTC_LOG(INFO) << __FUNCTION__; |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 100 | // Clear members tracking playout stats and do it on the task queue. |
| 101 | task_queue_.PostTask([this] { ResetPlayStats(); }); |
| 102 | // Start a periodic timer based on task queue if not already done by the |
| 103 | // recording side. |
| 104 | if (!recording_) { |
| 105 | StartPeriodicLogging(); |
| 106 | } |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 107 | const int64_t now_time = rtc::TimeMillis(); |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 108 | // Clear members that are only touched on the main (creating) thread. |
| 109 | play_start_time_ = now_time; |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 110 | playing_ = true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 111 | } |
| 112 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 113 | void AudioDeviceBuffer::StartRecording() { |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 114 | RTC_DCHECK_RUN_ON(&main_thread_checker_); |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 115 | if (recording_) { |
| 116 | return; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 117 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 118 | RTC_LOG(INFO) << __FUNCTION__; |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 119 | // Clear members tracking recording stats and do it on the task queue. |
| 120 | task_queue_.PostTask([this] { ResetRecStats(); }); |
| 121 | // Start a periodic timer based on task queue if not already done by the |
| 122 | // playout side. |
| 123 | if (!playing_) { |
| 124 | StartPeriodicLogging(); |
| 125 | } |
| 126 | // Clear members that will be touched on the main (creating) thread. |
| 127 | rec_start_time_ = rtc::TimeMillis(); |
| 128 | recording_ = true; |
| 129 | // And finally a member which can be modified on the native audio thread. |
| 130 | // It is safe to do so since we know by design that the owning ADM has not |
| 131 | // yet started the native audio recording. |
| 132 | only_silence_recorded_ = true; |
| 133 | } |
| 134 | |
| 135 | void AudioDeviceBuffer::StopPlayout() { |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 136 | RTC_DCHECK_RUN_ON(&main_thread_checker_); |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 137 | if (!playing_) { |
| 138 | return; |
| 139 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 140 | RTC_LOG(INFO) << __FUNCTION__; |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 141 | playing_ = false; |
| 142 | // Stop periodic logging if no more media is active. |
| 143 | if (!recording_) { |
| 144 | StopPeriodicLogging(); |
| 145 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 146 | RTC_LOG(INFO) << "total playout time: " << rtc::TimeSince(play_start_time_); |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void AudioDeviceBuffer::StopRecording() { |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 150 | RTC_DCHECK_RUN_ON(&main_thread_checker_); |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 151 | if (!recording_) { |
| 152 | return; |
| 153 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 154 | RTC_LOG(INFO) << __FUNCTION__; |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 155 | recording_ = false; |
| 156 | // Stop periodic logging if no more media is active. |
| 157 | if (!playing_) { |
| 158 | StopPeriodicLogging(); |
| 159 | } |
| 160 | // Add UMA histogram to keep track of the case when only zeros have been |
| 161 | // recorded. Measurements (max of absolute level) are taken twice per second, |
| 162 | // which means that if e.g 10 seconds of audio has been recorded, a total of |
| 163 | // 20 level estimates must all be identical to zero to trigger the histogram. |
| 164 | // |only_silence_recorded_| can only be cleared on the native audio thread |
| 165 | // that drives audio capture but we know by design that the audio has stopped |
| 166 | // when this method is called, hence there should not be aby conflicts. Also, |
| 167 | // the fact that |only_silence_recorded_| can be affected during the complete |
| 168 | // call makes chances of conflicts with potentially one last callback very |
| 169 | // small. |
| 170 | const size_t time_since_start = rtc::TimeSince(rec_start_time_); |
| 171 | if (time_since_start > kMinValidCallTimeTimeInMilliseconds) { |
| 172 | const int only_zeros = static_cast<int>(only_silence_recorded_); |
| 173 | RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", only_zeros); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 174 | RTC_LOG(INFO) << "HISTOGRAM(WebRTC.Audio.RecordedOnlyZeros): " |
| 175 | << only_zeros; |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 176 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 177 | RTC_LOG(INFO) << "total recording time: " << time_since_start; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 178 | } |
| 179 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 180 | int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 181 | RTC_LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")"; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 182 | rec_sample_rate_ = fsHz; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 183 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | } |
| 185 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 186 | int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 187 | RTC_LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")"; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 188 | play_sample_rate_ = fsHz; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 189 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 190 | } |
| 191 | |
henrika | cfbd26d | 2018-09-05 11:36:22 +0200 | [diff] [blame] | 192 | uint32_t AudioDeviceBuffer::RecordingSampleRate() const { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 193 | return rec_sample_rate_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 194 | } |
| 195 | |
henrika | cfbd26d | 2018-09-05 11:36:22 +0200 | [diff] [blame] | 196 | uint32_t AudioDeviceBuffer::PlayoutSampleRate() const { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 197 | return play_sample_rate_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 198 | } |
| 199 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 200 | int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 201 | RTC_LOG(INFO) << "SetRecordingChannels(" << channels << ")"; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 202 | rec_channels_ = channels; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 203 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 204 | } |
| 205 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 206 | int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 207 | RTC_LOG(INFO) << "SetPlayoutChannels(" << channels << ")"; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 208 | play_channels_ = channels; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 209 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 210 | } |
| 211 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 212 | size_t AudioDeviceBuffer::RecordingChannels() const { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 213 | return rec_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 214 | } |
| 215 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 216 | size_t AudioDeviceBuffer::PlayoutChannels() const { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 217 | return play_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 218 | } |
| 219 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 220 | int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) { |
| 221 | typing_status_ = typing_status; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 222 | return 0; |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 225 | void AudioDeviceBuffer::SetVQEData(int play_delay_ms, int rec_delay_ms) { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 226 | play_delay_ms_ = play_delay_ms; |
| 227 | rec_delay_ms_ = rec_delay_ms; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 228 | } |
| 229 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 230 | int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer, |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 231 | size_t samples_per_channel) { |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 232 | // Copy the complete input buffer to the local buffer. |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 233 | const size_t old_size = rec_buffer_.size(); |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 234 | rec_buffer_.SetData(static_cast<const int16_t*>(audio_buffer), |
| 235 | rec_channels_ * samples_per_channel); |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 236 | // Keep track of the size of the recording buffer. Only updated when the |
| 237 | // size changes, which is a rare event. |
| 238 | if (old_size != rec_buffer_.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 239 | RTC_LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size(); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 240 | } |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 241 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 242 | // Derive a new level value twice per second and check if it is non-zero. |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame] | 243 | int16_t max_abs = 0; |
| 244 | RTC_DCHECK_LT(rec_stat_count_, 50); |
| 245 | if (++rec_stat_count_ >= 50) { |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame] | 246 | // Returns the largest absolute value in a signed 16-bit vector. |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 247 | max_abs = WebRtcSpl_MaxAbsValueW16(rec_buffer_.data(), rec_buffer_.size()); |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame] | 248 | rec_stat_count_ = 0; |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 249 | // Set |only_silence_recorded_| to false as soon as at least one detection |
| 250 | // of a non-zero audio packet is found. It can only be restored to true |
| 251 | // again by restarting the call. |
| 252 | if (max_abs > 0) { |
| 253 | only_silence_recorded_ = false; |
| 254 | } |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame] | 255 | } |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 256 | // Update recording stats which is used as base for periodic logging of the |
| 257 | // audio input state. |
| 258 | UpdateRecStats(max_abs, samples_per_channel); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 259 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 260 | } |
| 261 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 262 | int32_t AudioDeviceBuffer::DeliverRecordedData() { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 263 | if (!audio_transport_cb_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 264 | RTC_LOG(LS_WARNING) << "Invalid audio transport"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 265 | return 0; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 266 | } |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 267 | const size_t frames = rec_buffer_.size() / rec_channels_; |
| 268 | const size_t bytes_per_frame = rec_channels_ * sizeof(int16_t); |
Fredrik Solenberg | 1a50cd5 | 2018-01-16 09:19:38 +0100 | [diff] [blame] | 269 | uint32_t new_mic_level_dummy = 0; |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 270 | uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_; |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 271 | int32_t res = audio_transport_cb_->RecordedDataIsAvailable( |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 272 | rec_buffer_.data(), frames, bytes_per_frame, rec_channels_, |
Fredrik Solenberg | 1a50cd5 | 2018-01-16 09:19:38 +0100 | [diff] [blame] | 273 | rec_sample_rate_, total_delay_ms, 0, 0, typing_status_, |
| 274 | new_mic_level_dummy); |
| 275 | if (res == -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 276 | RTC_LOG(LS_ERROR) << "RecordedDataIsAvailable() failed"; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 277 | } |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 278 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 279 | } |
| 280 | |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 281 | int32_t AudioDeviceBuffer::RequestPlayoutData(size_t samples_per_channel) { |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 282 | // The consumer can change the requested size on the fly and we therefore |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 283 | // resize the buffer accordingly. Also takes place at the first call to this |
| 284 | // method. |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 285 | const size_t total_samples = play_channels_ * samples_per_channel; |
| 286 | if (play_buffer_.size() != total_samples) { |
| 287 | play_buffer_.SetSize(total_samples); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 288 | RTC_LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size(); |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 289 | } |
| 290 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 291 | size_t num_samples_out(0); |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 292 | // It is currently supported to start playout without a valid audio |
| 293 | // transport object. Leads to warning and silence. |
| 294 | if (!audio_transport_cb_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 295 | RTC_LOG(LS_WARNING) << "Invalid audio transport"; |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 296 | return 0; |
| 297 | } |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 298 | |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 299 | // Retrieve new 16-bit PCM audio data using the audio transport instance. |
| 300 | int64_t elapsed_time_ms = -1; |
| 301 | int64_t ntp_time_ms = -1; |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 302 | const size_t bytes_per_frame = play_channels_ * sizeof(int16_t); |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 303 | uint32_t res = audio_transport_cb_->NeedMorePlayData( |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 304 | samples_per_channel, bytes_per_frame, play_channels_, play_sample_rate_, |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 305 | play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms); |
| 306 | if (res != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 307 | RTC_LOG(LS_ERROR) << "NeedMorePlayData() failed"; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 308 | } |
| 309 | |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame] | 310 | // Derive a new level value twice per second. |
| 311 | int16_t max_abs = 0; |
| 312 | RTC_DCHECK_LT(play_stat_count_, 50); |
| 313 | if (++play_stat_count_ >= 50) { |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame] | 314 | // Returns the largest absolute value in a signed 16-bit vector. |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 315 | max_abs = |
| 316 | WebRtcSpl_MaxAbsValueW16(play_buffer_.data(), play_buffer_.size()); |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame] | 317 | play_stat_count_ = 0; |
| 318 | } |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 319 | // Update playout stats which is used as base for periodic logging of the |
| 320 | // audio output state. |
henrika | 76535de | 2017-09-11 01:25:55 -0700 | [diff] [blame] | 321 | UpdatePlayStats(max_abs, num_samples_out / play_channels_); |
| 322 | return static_cast<int32_t>(num_samples_out / play_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 323 | } |
| 324 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 325 | int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 326 | RTC_DCHECK_GT(play_buffer_.size(), 0); |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 327 | #ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE |
| 328 | const double phase_increment = |
| 329 | k2Pi * 440.0 / static_cast<double>(play_sample_rate_); |
| 330 | int16_t* destination_r = reinterpret_cast<int16_t*>(audio_buffer); |
henrika | 29e865a | 2018-04-24 13:22:31 +0200 | [diff] [blame] | 331 | if (play_channels_ == 1) { |
| 332 | for (size_t i = 0; i < play_buffer_.size(); ++i) { |
| 333 | destination_r[i] = static_cast<int16_t>((sin(phase_) * (1 << 14))); |
| 334 | phase_ += phase_increment; |
| 335 | } |
| 336 | } else if (play_channels_ == 2) { |
| 337 | for (size_t i = 0; i < play_buffer_.size() / 2; ++i) { |
| 338 | destination_r[2 * i] = destination_r[2 * i + 1] = |
| 339 | static_cast<int16_t>((sin(phase_) * (1 << 14))); |
| 340 | phase_ += phase_increment; |
| 341 | } |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 342 | } |
| 343 | #else |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 344 | memcpy(audio_buffer, play_buffer_.data(), |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 345 | play_buffer_.size() * sizeof(int16_t)); |
| 346 | #endif |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 347 | // Return samples per channel or number of frames. |
| 348 | return static_cast<int32_t>(play_buffer_.size() / play_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 349 | } |
| 350 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 351 | void AudioDeviceBuffer::StartPeriodicLogging() { |
| 352 | task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, |
| 353 | AudioDeviceBuffer::LOG_START)); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 354 | } |
| 355 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 356 | void AudioDeviceBuffer::StopPeriodicLogging() { |
| 357 | task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, |
| 358 | AudioDeviceBuffer::LOG_STOP)); |
| 359 | } |
| 360 | |
| 361 | void AudioDeviceBuffer::LogStats(LogState state) { |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 362 | RTC_DCHECK_RUN_ON(&task_queue_); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 363 | int64_t now_time = rtc::TimeMillis(); |
henrika | 0b3a638 | 2016-11-11 02:28:50 -0800 | [diff] [blame] | 364 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 365 | if (state == AudioDeviceBuffer::LOG_START) { |
| 366 | // Reset counters at start. We will not add any logging in this state but |
| 367 | // the timer will started by posting a new (delayed) task. |
| 368 | num_stat_reports_ = 0; |
| 369 | last_timer_task_time_ = now_time; |
henrika | 0b3a638 | 2016-11-11 02:28:50 -0800 | [diff] [blame] | 370 | log_stats_ = true; |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 371 | } else if (state == AudioDeviceBuffer::LOG_STOP) { |
| 372 | // Stop logging and posting new tasks. |
henrika | 0b3a638 | 2016-11-11 02:28:50 -0800 | [diff] [blame] | 373 | log_stats_ = false; |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 374 | } else if (state == AudioDeviceBuffer::LOG_ACTIVE) { |
henrika | 0b3a638 | 2016-11-11 02:28:50 -0800 | [diff] [blame] | 375 | // Keep logging unless logging was disabled while task was posted. |
| 376 | } |
| 377 | |
| 378 | // Avoid adding more logs since we are in STOP mode. |
| 379 | if (!log_stats_) { |
| 380 | return; |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 381 | } |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 382 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 383 | int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds; |
| 384 | int64_t time_since_last = rtc::TimeDiff(now_time, last_timer_task_time_); |
| 385 | last_timer_task_time_ = now_time; |
| 386 | |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 387 | Stats stats; |
| 388 | { |
| 389 | rtc::CritScope cs(&lock_); |
| 390 | stats = stats_; |
| 391 | stats_.max_rec_level = 0; |
| 392 | stats_.max_play_level = 0; |
| 393 | } |
| 394 | |
henrika | cfbd26d | 2018-09-05 11:36:22 +0200 | [diff] [blame] | 395 | // Cache current sample rate from atomic members. |
| 396 | const uint32_t rec_sample_rate = rec_sample_rate_; |
| 397 | const uint32_t play_sample_rate = play_sample_rate_; |
| 398 | |
| 399 | // Log the latest statistics but skip the first two rounds just after state |
| 400 | // was set to LOG_START to ensure that we have at least one full stable |
| 401 | // 10-second interval for sample-rate estimation. Hence, first printed log |
| 402 | // will be after ~20 seconds. |
| 403 | if (++num_stat_reports_ > 2 && time_since_last > 0) { |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 404 | uint32_t diff_samples = stats.rec_samples - last_stats_.rec_samples; |
henrika | a6d26ec | 2016-09-20 04:44:04 -0700 | [diff] [blame] | 405 | float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); |
henrika | cfbd26d | 2018-09-05 11:36:22 +0200 | [diff] [blame] | 406 | uint32_t abs_diff_rate_in_percent = 0; |
| 407 | if (rec_sample_rate > 0) { |
| 408 | abs_diff_rate_in_percent = static_cast<uint32_t>( |
| 409 | 0.5f + |
| 410 | ((100.0f * std::abs(rate - rec_sample_rate)) / rec_sample_rate)); |
| 411 | RTC_HISTOGRAM_PERCENTAGE("WebRTC.Audio.RecordSampleRateOffsetInPercent", |
| 412 | abs_diff_rate_in_percent); |
| 413 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 414 | RTC_LOG(INFO) << "[REC : " << time_since_last << "msec, " |
henrika | cfbd26d | 2018-09-05 11:36:22 +0200 | [diff] [blame] | 415 | << rec_sample_rate / 1000 << "kHz] callbacks: " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 416 | << stats.rec_callbacks - last_stats_.rec_callbacks << ", " |
| 417 | << "samples: " << diff_samples << ", " |
| 418 | << "rate: " << static_cast<int>(rate + 0.5) << ", " |
henrika | cfbd26d | 2018-09-05 11:36:22 +0200 | [diff] [blame] | 419 | << "rate diff: " << abs_diff_rate_in_percent << "%, " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 420 | << "level: " << stats.max_rec_level; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 421 | |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 422 | diff_samples = stats.play_samples - last_stats_.play_samples; |
henrika | a6d26ec | 2016-09-20 04:44:04 -0700 | [diff] [blame] | 423 | rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); |
henrika | cfbd26d | 2018-09-05 11:36:22 +0200 | [diff] [blame] | 424 | abs_diff_rate_in_percent = 0; |
| 425 | if (play_sample_rate > 0) { |
| 426 | abs_diff_rate_in_percent = static_cast<uint32_t>( |
| 427 | 0.5f + |
| 428 | ((100.0f * std::abs(rate - play_sample_rate)) / play_sample_rate)); |
| 429 | RTC_HISTOGRAM_PERCENTAGE("WebRTC.Audio.PlayoutSampleRateOffsetInPercent", |
| 430 | abs_diff_rate_in_percent); |
| 431 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 432 | RTC_LOG(INFO) << "[PLAY: " << time_since_last << "msec, " |
henrika | cfbd26d | 2018-09-05 11:36:22 +0200 | [diff] [blame] | 433 | << play_sample_rate / 1000 << "kHz] callbacks: " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 434 | << stats.play_callbacks - last_stats_.play_callbacks << ", " |
| 435 | << "samples: " << diff_samples << ", " |
| 436 | << "rate: " << static_cast<int>(rate + 0.5) << ", " |
henrika | cfbd26d | 2018-09-05 11:36:22 +0200 | [diff] [blame] | 437 | << "rate diff: " << abs_diff_rate_in_percent << "%, " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 438 | << "level: " << stats.max_play_level; |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 439 | } |
henrika | cfbd26d | 2018-09-05 11:36:22 +0200 | [diff] [blame] | 440 | last_stats_ = stats; |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 441 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 442 | int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis(); |
| 443 | RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval"; |
| 444 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 445 | // Keep posting new (delayed) tasks until state is changed to kLogStop. |
| 446 | task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, |
| 447 | AudioDeviceBuffer::LOG_ACTIVE), |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 448 | time_to_wait_ms); |
| 449 | } |
| 450 | |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 451 | void AudioDeviceBuffer::ResetRecStats() { |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 452 | RTC_DCHECK_RUN_ON(&task_queue_); |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 453 | last_stats_.ResetRecStats(); |
| 454 | rtc::CritScope cs(&lock_); |
| 455 | stats_.ResetRecStats(); |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | void AudioDeviceBuffer::ResetPlayStats() { |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 459 | RTC_DCHECK_RUN_ON(&task_queue_); |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 460 | last_stats_.ResetPlayStats(); |
| 461 | rtc::CritScope cs(&lock_); |
| 462 | stats_.ResetPlayStats(); |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 463 | } |
| 464 | |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 465 | void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs, |
| 466 | size_t samples_per_channel) { |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 467 | rtc::CritScope cs(&lock_); |
| 468 | ++stats_.rec_callbacks; |
| 469 | stats_.rec_samples += samples_per_channel; |
| 470 | if (max_abs > stats_.max_rec_level) { |
| 471 | stats_.max_rec_level = max_abs; |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 472 | } |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 473 | } |
| 474 | |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 475 | void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs, |
| 476 | size_t samples_per_channel) { |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 477 | rtc::CritScope cs(&lock_); |
| 478 | ++stats_.play_callbacks; |
| 479 | stats_.play_samples += samples_per_channel; |
| 480 | if (max_abs > stats_.max_play_level) { |
| 481 | stats_.max_play_level = max_abs; |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 482 | } |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 483 | } |
| 484 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 485 | } // namespace webrtc |