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