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