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