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; |
| 33 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 34 | AudioDeviceBuffer::AudioDeviceBuffer() |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 35 | : audio_transport_cb_(nullptr), |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 36 | task_queue_(kTimerQueueName), |
| 37 | timer_has_started_(false), |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 38 | rec_sample_rate_(0), |
| 39 | play_sample_rate_(0), |
| 40 | rec_channels_(0), |
| 41 | play_channels_(0), |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 42 | rec_bytes_per_sample_(0), |
| 43 | play_bytes_per_sample_(0), |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 44 | current_mic_level_(0), |
| 45 | new_mic_level_(0), |
| 46 | typing_status_(false), |
| 47 | play_delay_ms_(0), |
| 48 | rec_delay_ms_(0), |
| 49 | clock_drift_(0), |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 50 | num_stat_reports_(0), |
| 51 | rec_callbacks_(0), |
| 52 | last_rec_callbacks_(0), |
| 53 | play_callbacks_(0), |
| 54 | last_play_callbacks_(0), |
| 55 | rec_samples_(0), |
| 56 | last_rec_samples_(0), |
| 57 | play_samples_(0), |
| 58 | last_play_samples_(0), |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 59 | last_log_stat_time_(0), |
| 60 | max_rec_level_(0), |
| 61 | max_play_level_(0), |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 62 | num_rec_level_is_zero_(0), |
| 63 | rec_stat_count_(0), |
| 64 | play_stat_count_(0) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 65 | LOG(INFO) << "AudioDeviceBuffer::ctor"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | } |
| 67 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 68 | AudioDeviceBuffer::~AudioDeviceBuffer() { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 69 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 70 | LOG(INFO) << "AudioDeviceBuffer::~dtor"; |
henrika | 3d7346f | 2016-07-29 16:20:47 +0200 | [diff] [blame] | 71 | |
| 72 | size_t total_diff_time = 0; |
| 73 | int num_measurements = 0; |
| 74 | LOG(INFO) << "[playout diff time => #measurements]"; |
| 75 | for (size_t diff = 0; diff < arraysize(playout_diff_times_); ++diff) { |
| 76 | uint32_t num_elements = playout_diff_times_[diff]; |
| 77 | if (num_elements > 0) { |
| 78 | total_diff_time += num_elements * diff; |
| 79 | num_measurements += num_elements; |
| 80 | LOG(INFO) << "[" << diff << " => " << num_elements << "]"; |
| 81 | } |
| 82 | } |
| 83 | if (num_measurements > 0) { |
| 84 | LOG(INFO) << "total_diff_time: " << total_diff_time; |
| 85 | LOG(INFO) << "num_measurements: " << num_measurements; |
| 86 | LOG(INFO) << "average: " |
| 87 | << static_cast<float>(total_diff_time) / num_measurements; |
| 88 | } |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 89 | |
| 90 | // Add UMA histogram to keep track of the case when only zeros have been |
| 91 | // recorded. Ensure that recording callbacks have started and that at least |
| 92 | // one timer event has been able to update |num_rec_level_is_zero_|. |
| 93 | // I am avoiding use of the task queue here since we are under destruction |
| 94 | // and reading these members on the creating thread feels safe. |
| 95 | if (rec_callbacks_ > 0 && num_stat_reports_ > 0) { |
asapersson | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 96 | RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 97 | static_cast<int>(num_stat_reports_ == num_rec_level_is_zero_)); |
| 98 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | } |
| 100 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 101 | int32_t AudioDeviceBuffer::RegisterAudioCallback( |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 102 | AudioTransport* audio_callback) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 103 | LOG(INFO) << __FUNCTION__; |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 104 | rtc::CritScope lock(&lock_cb_); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 105 | audio_transport_cb_ = audio_callback; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 106 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | } |
| 108 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 109 | int32_t AudioDeviceBuffer::InitPlayout() { |
henrika | d7a89db | 2016-08-19 08:09:25 -0700 | [diff] [blame] | 110 | LOG(INFO) << __FUNCTION__; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 111 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 112 | ResetPlayStats(); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 113 | if (!timer_has_started_) { |
| 114 | StartTimer(); |
| 115 | timer_has_started_ = true; |
| 116 | } |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 117 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | } |
| 119 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 120 | int32_t AudioDeviceBuffer::InitRecording() { |
henrika | d7a89db | 2016-08-19 08:09:25 -0700 | [diff] [blame] | 121 | LOG(INFO) << __FUNCTION__; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 122 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 123 | ResetRecStats(); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 124 | if (!timer_has_started_) { |
| 125 | StartTimer(); |
| 126 | timer_has_started_ = true; |
| 127 | } |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 128 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | } |
| 130 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 131 | int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 132 | LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")"; |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 133 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 134 | rec_sample_rate_ = fsHz; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 135 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 136 | } |
| 137 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 138 | int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 139 | LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")"; |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 140 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 141 | play_sample_rate_ = fsHz; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 142 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 143 | } |
| 144 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 145 | int32_t AudioDeviceBuffer::RecordingSampleRate() const { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 146 | return rec_sample_rate_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 147 | } |
| 148 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 149 | int32_t AudioDeviceBuffer::PlayoutSampleRate() const { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 150 | return play_sample_rate_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 151 | } |
| 152 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 153 | int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 154 | LOG(INFO) << "SetRecordingChannels(" << channels << ")"; |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 155 | rtc::CritScope lock(&lock_); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 156 | rec_channels_ = channels; |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 157 | rec_bytes_per_sample_ = sizeof(int16_t) * channels; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 158 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | } |
| 160 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 161 | int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 162 | LOG(INFO) << "SetPlayoutChannels(" << channels << ")"; |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 163 | rtc::CritScope lock(&lock_); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 164 | play_channels_ = channels; |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 165 | play_bytes_per_sample_ = sizeof(int16_t) * channels; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 166 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 167 | } |
| 168 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 169 | int32_t AudioDeviceBuffer::SetRecordingChannel( |
| 170 | const AudioDeviceModule::ChannelType channel) { |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 171 | LOG(INFO) << "SetRecordingChannel(" << channel << ")"; |
| 172 | LOG(LS_WARNING) << "Not implemented"; |
| 173 | // Add DCHECK to ensure that user does not try to use this API with a non- |
| 174 | // default parameter. |
| 175 | RTC_DCHECK_EQ(channel, AudioDeviceModule::kChannelBoth); |
| 176 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 177 | } |
| 178 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 179 | int32_t AudioDeviceBuffer::RecordingChannel( |
| 180 | AudioDeviceModule::ChannelType& channel) const { |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 181 | LOG(LS_WARNING) << "Not implemented"; |
| 182 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 183 | } |
| 184 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 185 | size_t AudioDeviceBuffer::RecordingChannels() const { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 186 | return rec_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 187 | } |
| 188 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 189 | size_t AudioDeviceBuffer::PlayoutChannels() const { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 190 | return play_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 191 | } |
| 192 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 193 | int32_t AudioDeviceBuffer::SetCurrentMicLevel(uint32_t level) { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 194 | current_mic_level_ = level; |
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 | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 198 | int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) { |
| 199 | typing_status_ = typing_status; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 200 | return 0; |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 201 | } |
| 202 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 203 | uint32_t AudioDeviceBuffer::NewMicLevel() const { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 204 | return new_mic_level_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 205 | } |
| 206 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 207 | void AudioDeviceBuffer::SetVQEData(int play_delay_ms, |
| 208 | int rec_delay_ms, |
| 209 | int clock_drift) { |
| 210 | play_delay_ms_ = play_delay_ms; |
| 211 | rec_delay_ms_ = rec_delay_ms; |
| 212 | clock_drift_ = clock_drift; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 213 | } |
| 214 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 215 | int32_t AudioDeviceBuffer::StartInputFileRecording( |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 216 | const char fileName[kAdmMaxFileNameSize]) { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 217 | LOG(LS_WARNING) << "Not implemented"; |
| 218 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 219 | } |
| 220 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 221 | int32_t AudioDeviceBuffer::StopInputFileRecording() { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 222 | LOG(LS_WARNING) << "Not implemented"; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 223 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 224 | } |
| 225 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 226 | int32_t AudioDeviceBuffer::StartOutputFileRecording( |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 227 | const char fileName[kAdmMaxFileNameSize]) { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 228 | LOG(LS_WARNING) << "Not implemented"; |
henrika | cf327b4 | 2016-08-19 16:37:53 +0200 | [diff] [blame] | 229 | return 0; |
| 230 | } |
| 231 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 232 | int32_t AudioDeviceBuffer::StopOutputFileRecording() { |
| 233 | LOG(LS_WARNING) << "Not implemented"; |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer, |
| 238 | size_t num_samples) { |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 239 | const size_t rec_channels = [&] { |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 240 | rtc::CritScope lock(&lock_); |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 241 | return rec_channels_; |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 242 | }(); |
| 243 | // Copy the complete input buffer to the local buffer. |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 244 | const size_t size_in_bytes = num_samples * rec_channels * sizeof(int16_t); |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 245 | const size_t old_size = rec_buffer_.size(); |
| 246 | rec_buffer_.SetData(static_cast<const uint8_t*>(audio_buffer), size_in_bytes); |
| 247 | // Keep track of the size of the recording buffer. Only updated when the |
| 248 | // size changes, which is a rare event. |
| 249 | if (old_size != rec_buffer_.size()) { |
| 250 | LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size(); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 251 | } |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 252 | // Derive a new level value twice per second. |
| 253 | int16_t max_abs = 0; |
| 254 | RTC_DCHECK_LT(rec_stat_count_, 50); |
| 255 | if (++rec_stat_count_ >= 50) { |
| 256 | const size_t size = num_samples * rec_channels; |
| 257 | // Returns the largest absolute value in a signed 16-bit vector. |
| 258 | max_abs = WebRtcSpl_MaxAbsValueW16( |
| 259 | reinterpret_cast<const int16_t*>(rec_buffer_.data()), size); |
| 260 | rec_stat_count_ = 0; |
| 261 | } |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 262 | // Update some stats but do it on the task queue to ensure that the members |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 263 | // are modified and read on the same thread. Note that |max_abs| will be |
| 264 | // zero in most calls and then have no effect of the stats. It is only updated |
| 265 | // approximately two times per second and can then change the stats. |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 266 | task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::UpdateRecStats, this, |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 267 | max_abs, num_samples)); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 268 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 269 | } |
| 270 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 271 | int32_t AudioDeviceBuffer::DeliverRecordedData() { |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 272 | rtc::CritScope lock(&lock_cb_); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 273 | if (!audio_transport_cb_) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 274 | LOG(LS_WARNING) << "Invalid audio transport"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 275 | return 0; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 276 | } |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 277 | const size_t rec_bytes_per_sample = [&] { |
| 278 | rtc::CritScope lock(&lock_); |
| 279 | return rec_bytes_per_sample_; |
| 280 | }(); |
| 281 | uint32_t new_mic_level(0); |
| 282 | uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_; |
| 283 | size_t num_samples = rec_buffer_.size() / rec_bytes_per_sample; |
| 284 | int32_t res = audio_transport_cb_->RecordedDataIsAvailable( |
| 285 | rec_buffer_.data(), num_samples, rec_bytes_per_sample_, rec_channels_, |
| 286 | rec_sample_rate_, total_delay_ms, clock_drift_, current_mic_level_, |
| 287 | typing_status_, new_mic_level); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 288 | if (res != -1) { |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 289 | new_mic_level_ = new_mic_level; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 290 | } else { |
| 291 | LOG(LS_ERROR) << "RecordedDataIsAvailable() failed"; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 292 | } |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 293 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 294 | } |
| 295 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 296 | int32_t AudioDeviceBuffer::RequestPlayoutData(size_t num_samples) { |
henrika | 3d7346f | 2016-07-29 16:20:47 +0200 | [diff] [blame] | 297 | // Measure time since last function call and update an array where the |
| 298 | // position/index corresponds to time differences (in milliseconds) between |
| 299 | // two successive playout callbacks, and the stored value is the number of |
| 300 | // times a given time difference was found. |
| 301 | int64_t now_time = rtc::TimeMillis(); |
| 302 | size_t diff_time = rtc::TimeDiff(now_time, last_playout_time_); |
| 303 | // Truncate at 500ms to limit the size of the array. |
| 304 | diff_time = std::min(kMaxDeltaTimeInMs, diff_time); |
| 305 | last_playout_time_ = now_time; |
| 306 | playout_diff_times_[diff_time]++; |
| 307 | |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 308 | const size_t play_channels = [&] { |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 309 | rtc::CritScope lock(&lock_); |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 310 | return play_channels_; |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 311 | }(); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 312 | |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 313 | // The consumer can change the request size on the fly and we therefore |
| 314 | // resize the buffer accordingly. Also takes place at the first call to this |
| 315 | // method. |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 316 | const size_t play_bytes_per_sample = play_channels * sizeof(int16_t); |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 317 | const size_t size_in_bytes = num_samples * play_bytes_per_sample; |
| 318 | if (play_buffer_.size() != size_in_bytes) { |
| 319 | play_buffer_.SetSize(size_in_bytes); |
| 320 | LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size(); |
| 321 | } |
| 322 | |
| 323 | rtc::CritScope lock(&lock_cb_); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 324 | |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 325 | // It is currently supported to start playout without a valid audio |
| 326 | // transport object. Leads to warning and silence. |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 327 | if (!audio_transport_cb_) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 328 | LOG(LS_WARNING) << "Invalid audio transport"; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 332 | // Retrieve new 16-bit PCM audio data using the audio transport instance. |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 333 | int64_t elapsed_time_ms = -1; |
| 334 | int64_t ntp_time_ms = -1; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 335 | size_t num_samples_out(0); |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 336 | uint32_t res = audio_transport_cb_->NeedMorePlayData( |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 337 | num_samples, play_bytes_per_sample_, play_channels, play_sample_rate_, |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 338 | play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms); |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 339 | if (res != 0) { |
| 340 | LOG(LS_ERROR) << "NeedMorePlayData() failed"; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 341 | } |
| 342 | |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 343 | // Derive a new level value twice per second. |
| 344 | int16_t max_abs = 0; |
| 345 | RTC_DCHECK_LT(play_stat_count_, 50); |
| 346 | if (++play_stat_count_ >= 50) { |
| 347 | const size_t size = num_samples * play_channels; |
| 348 | // Returns the largest absolute value in a signed 16-bit vector. |
| 349 | max_abs = WebRtcSpl_MaxAbsValueW16( |
| 350 | reinterpret_cast<const int16_t*>(play_buffer_.data()), size); |
| 351 | play_stat_count_ = 0; |
| 352 | } |
| 353 | // Update some stats but do it on the task queue to ensure that the members |
| 354 | // are modified and read on the same thread. Note that |max_abs| will be |
| 355 | // zero in most calls and then have no effect of the stats. It is only updated |
| 356 | // approximately two times per second and can then change the stats. |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 357 | task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::UpdatePlayStats, this, |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 358 | max_abs, num_samples_out)); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 359 | return static_cast<int32_t>(num_samples_out); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 360 | } |
| 361 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 362 | int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) { |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 363 | RTC_DCHECK_GT(play_buffer_.size(), 0u); |
| 364 | const size_t play_bytes_per_sample = [&] { |
| 365 | rtc::CritScope lock(&lock_); |
| 366 | return play_bytes_per_sample_; |
| 367 | }(); |
| 368 | memcpy(audio_buffer, play_buffer_.data(), play_buffer_.size()); |
| 369 | return static_cast<int32_t>(play_buffer_.size() / play_bytes_per_sample); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 370 | } |
| 371 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 372 | void AudioDeviceBuffer::StartTimer() { |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 373 | num_stat_reports_ = 0; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 374 | last_log_stat_time_ = rtc::TimeMillis(); |
| 375 | task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this), |
| 376 | kTimerIntervalInMilliseconds); |
| 377 | } |
| 378 | |
| 379 | void AudioDeviceBuffer::LogStats() { |
| 380 | RTC_DCHECK(task_queue_.IsCurrent()); |
| 381 | |
| 382 | int64_t now_time = rtc::TimeMillis(); |
| 383 | int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds; |
| 384 | int64_t time_since_last = rtc::TimeDiff(now_time, last_log_stat_time_); |
| 385 | last_log_stat_time_ = now_time; |
| 386 | |
| 387 | // Log the latest statistics but skip the first 10 seconds since we are not |
| 388 | // sure of the exact starting point. I.e., the first log printout will be |
| 389 | // after ~20 seconds. |
henrika | a6d26ec | 2016-09-20 04:44:04 -0700 | [diff] [blame] | 390 | if (++num_stat_reports_ > 1 && time_since_last > 0) { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 391 | uint32_t diff_samples = rec_samples_ - last_rec_samples_; |
henrika | a6d26ec | 2016-09-20 04:44:04 -0700 | [diff] [blame] | 392 | float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 393 | LOG(INFO) << "[REC : " << time_since_last << "msec, " |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 394 | << rec_sample_rate_ / 1000 |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 395 | << "kHz] callbacks: " << rec_callbacks_ - last_rec_callbacks_ |
| 396 | << ", " |
| 397 | << "samples: " << diff_samples << ", " |
henrika | a6d26ec | 2016-09-20 04:44:04 -0700 | [diff] [blame] | 398 | << "rate: " << static_cast<int>(rate + 0.5) << ", " |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 399 | << "level: " << max_rec_level_; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 400 | |
| 401 | diff_samples = play_samples_ - last_play_samples_; |
henrika | a6d26ec | 2016-09-20 04:44:04 -0700 | [diff] [blame] | 402 | rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 403 | LOG(INFO) << "[PLAY: " << time_since_last << "msec, " |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 404 | << play_sample_rate_ / 1000 |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 405 | << "kHz] callbacks: " << play_callbacks_ - last_play_callbacks_ |
| 406 | << ", " |
| 407 | << "samples: " << diff_samples << ", " |
henrika | a6d26ec | 2016-09-20 04:44:04 -0700 | [diff] [blame] | 408 | << "rate: " << static_cast<int>(rate + 0.5) << ", " |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 409 | << "level: " << max_play_level_; |
| 410 | } |
| 411 | |
| 412 | // Count number of times we detect "no audio" corresponding to a case where |
| 413 | // all level measurements have been zero. |
| 414 | if (max_rec_level_ == 0) { |
| 415 | ++num_rec_level_is_zero_; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | last_rec_callbacks_ = rec_callbacks_; |
| 419 | last_play_callbacks_ = play_callbacks_; |
| 420 | last_rec_samples_ = rec_samples_; |
| 421 | last_play_samples_ = play_samples_; |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 422 | max_rec_level_ = 0; |
| 423 | max_play_level_ = 0; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 424 | |
| 425 | int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis(); |
| 426 | RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval"; |
| 427 | |
| 428 | // Update some stats but do it on the task queue to ensure that access of |
| 429 | // members is serialized hence avoiding usage of locks. |
| 430 | task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this), |
| 431 | time_to_wait_ms); |
| 432 | } |
| 433 | |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 434 | void AudioDeviceBuffer::ResetRecStats() { |
| 435 | rec_callbacks_ = 0; |
| 436 | last_rec_callbacks_ = 0; |
| 437 | rec_samples_ = 0; |
| 438 | last_rec_samples_ = 0; |
| 439 | max_rec_level_ = 0; |
| 440 | num_rec_level_is_zero_ = 0; |
| 441 | } |
| 442 | |
| 443 | void AudioDeviceBuffer::ResetPlayStats() { |
| 444 | last_playout_time_ = rtc::TimeMillis(); |
| 445 | play_callbacks_ = 0; |
| 446 | last_play_callbacks_ = 0; |
| 447 | play_samples_ = 0; |
| 448 | last_play_samples_ = 0; |
| 449 | max_play_level_ = 0; |
| 450 | } |
| 451 | |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 452 | void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs, size_t num_samples) { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 453 | RTC_DCHECK(task_queue_.IsCurrent()); |
| 454 | ++rec_callbacks_; |
| 455 | rec_samples_ += num_samples; |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 456 | if (max_abs > max_rec_level_) { |
| 457 | max_rec_level_ = max_abs; |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 458 | } |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 459 | } |
| 460 | |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 461 | void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs, size_t num_samples) { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 462 | RTC_DCHECK(task_queue_.IsCurrent()); |
| 463 | ++play_callbacks_; |
| 464 | play_samples_ += num_samples; |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame^] | 465 | if (max_abs > max_play_level_) { |
| 466 | max_play_level_ = max_abs; |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 467 | } |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 468 | } |
| 469 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 470 | } // namespace webrtc |