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