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