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), |
| 42 | rec_channel_(AudioDeviceModule::kChannelBoth), |
| 43 | rec_bytes_per_sample_(0), |
| 44 | play_bytes_per_sample_(0), |
| 45 | rec_samples_per_10ms_(0), |
| 46 | rec_bytes_per_10ms_(0), |
| 47 | play_samples_per_10ms_(0), |
| 48 | play_bytes_per_10ms_(0), |
| 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), |
| 56 | rec_callbacks_(0), |
| 57 | last_rec_callbacks_(0), |
| 58 | play_callbacks_(0), |
| 59 | last_play_callbacks_(0), |
| 60 | rec_samples_(0), |
| 61 | last_rec_samples_(0), |
| 62 | play_samples_(0), |
| 63 | last_play_samples_(0), |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 64 | last_log_stat_time_(0), |
| 65 | max_rec_level_(0), |
| 66 | max_play_level_(0), |
| 67 | num_rec_level_is_zero_(0) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 68 | LOG(INFO) << "AudioDeviceBuffer::ctor"; |
henrika | 073378e | 2016-09-09 13:15:37 +0200 | [diff] [blame] | 69 | // TODO(henrika): improve buffer handling and ensure that we don't allocate |
| 70 | // more than what is required. |
| 71 | play_buffer_.reset(new int8_t[kMaxBufferSizeBytes]); |
| 72 | rec_buffer_.reset(new int8_t[kMaxBufferSizeBytes]); |
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 | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 76 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 77 | LOG(INFO) << "AudioDeviceBuffer::~dtor"; |
henrika | 3d7346f | 2016-07-29 16:20:47 +0200 | [diff] [blame] | 78 | |
| 79 | size_t total_diff_time = 0; |
| 80 | int num_measurements = 0; |
| 81 | LOG(INFO) << "[playout diff time => #measurements]"; |
| 82 | for (size_t diff = 0; diff < arraysize(playout_diff_times_); ++diff) { |
| 83 | uint32_t num_elements = playout_diff_times_[diff]; |
| 84 | if (num_elements > 0) { |
| 85 | total_diff_time += num_elements * diff; |
| 86 | num_measurements += num_elements; |
| 87 | LOG(INFO) << "[" << diff << " => " << num_elements << "]"; |
| 88 | } |
| 89 | } |
| 90 | if (num_measurements > 0) { |
| 91 | LOG(INFO) << "total_diff_time: " << total_diff_time; |
| 92 | LOG(INFO) << "num_measurements: " << num_measurements; |
| 93 | LOG(INFO) << "average: " |
| 94 | << static_cast<float>(total_diff_time) / num_measurements; |
| 95 | } |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 96 | |
| 97 | // Add UMA histogram to keep track of the case when only zeros have been |
| 98 | // recorded. Ensure that recording callbacks have started and that at least |
| 99 | // one timer event has been able to update |num_rec_level_is_zero_|. |
| 100 | // I am avoiding use of the task queue here since we are under destruction |
| 101 | // and reading these members on the creating thread feels safe. |
| 102 | if (rec_callbacks_ > 0 && num_stat_reports_ > 0) { |
asapersson | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 103 | RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 104 | static_cast<int>(num_stat_reports_ == num_rec_level_is_zero_)); |
| 105 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 106 | } |
| 107 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 108 | int32_t AudioDeviceBuffer::RegisterAudioCallback( |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 109 | AudioTransport* audio_callback) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 110 | LOG(INFO) << __FUNCTION__; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 111 | rtc::CritScope lock(&_critSectCb); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 112 | audio_transport_cb_ = audio_callback; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 113 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | } |
| 115 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 116 | int32_t AudioDeviceBuffer::InitPlayout() { |
henrika | d7a89db | 2016-08-19 08:09:25 -0700 | [diff] [blame] | 117 | LOG(INFO) << __FUNCTION__; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 118 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 119 | ResetPlayStats(); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 120 | if (!timer_has_started_) { |
| 121 | StartTimer(); |
| 122 | timer_has_started_ = true; |
| 123 | } |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 124 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 125 | } |
| 126 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 127 | int32_t AudioDeviceBuffer::InitRecording() { |
henrika | d7a89db | 2016-08-19 08:09:25 -0700 | [diff] [blame] | 128 | LOG(INFO) << __FUNCTION__; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 129 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 130 | ResetRecStats(); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 131 | if (!timer_has_started_) { |
| 132 | StartTimer(); |
| 133 | timer_has_started_ = true; |
| 134 | } |
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::SetRecordingSampleRate(uint32_t fsHz) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 139 | LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")"; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 140 | rtc::CritScope lock(&_critSect); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 141 | rec_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::SetPlayoutSampleRate(uint32_t fsHz) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 146 | LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")"; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 147 | rtc::CritScope lock(&_critSect); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 148 | play_sample_rate_ = fsHz; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 149 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 150 | } |
| 151 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 152 | int32_t AudioDeviceBuffer::RecordingSampleRate() const { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 153 | return rec_sample_rate_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | } |
| 155 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 156 | int32_t AudioDeviceBuffer::PlayoutSampleRate() const { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 157 | return play_sample_rate_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 158 | } |
| 159 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 160 | int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 161 | LOG(INFO) << "SetRecordingChannels(" << channels << ")"; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 162 | rtc::CritScope lock(&_critSect); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 163 | rec_channels_ = channels; |
| 164 | rec_bytes_per_sample_ = |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 165 | 2 * channels; // 16 bits per sample in mono, 32 bits in stereo |
| 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::SetPlayoutChannels(size_t channels) { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 170 | LOG(INFO) << "SetPlayoutChannels(" << channels << ")"; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 171 | rtc::CritScope lock(&_critSect); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 172 | play_channels_ = channels; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 173 | // 16 bits per sample in mono, 32 bits in stereo |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 174 | play_bytes_per_sample_ = 2 * channels; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 175 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | } |
| 177 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 178 | int32_t AudioDeviceBuffer::SetRecordingChannel( |
| 179 | const AudioDeviceModule::ChannelType channel) { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 180 | rtc::CritScope lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 181 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 182 | if (rec_channels_ == 1) { |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 183 | return -1; |
| 184 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 185 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 186 | if (channel == AudioDeviceModule::kChannelBoth) { |
| 187 | // two bytes per channel |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 188 | rec_bytes_per_sample_ = 4; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 189 | } else { |
| 190 | // only utilize one out of two possible channels (left or right) |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 191 | rec_bytes_per_sample_ = 2; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 192 | } |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 193 | rec_channel_ = channel; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 194 | |
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::RecordingChannel( |
| 199 | AudioDeviceModule::ChannelType& channel) const { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 200 | channel = rec_channel_; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 201 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 202 | } |
| 203 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 204 | size_t AudioDeviceBuffer::RecordingChannels() const { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 205 | return rec_channels_; |
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 | size_t AudioDeviceBuffer::PlayoutChannels() const { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 209 | return play_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 210 | } |
| 211 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 212 | int32_t AudioDeviceBuffer::SetCurrentMicLevel(uint32_t level) { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 213 | current_mic_level_ = level; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 214 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 215 | } |
| 216 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 217 | int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) { |
| 218 | typing_status_ = typing_status; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 219 | return 0; |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 220 | } |
| 221 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 222 | uint32_t AudioDeviceBuffer::NewMicLevel() const { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 223 | return new_mic_level_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 224 | } |
| 225 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 226 | void AudioDeviceBuffer::SetVQEData(int play_delay_ms, |
| 227 | int rec_delay_ms, |
| 228 | int clock_drift) { |
| 229 | play_delay_ms_ = play_delay_ms; |
| 230 | rec_delay_ms_ = rec_delay_ms; |
| 231 | clock_drift_ = clock_drift; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 232 | } |
| 233 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 234 | int32_t AudioDeviceBuffer::StartInputFileRecording( |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 235 | const char fileName[kAdmMaxFileNameSize]) { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 236 | LOG(LS_WARNING) << "Not implemented"; |
| 237 | return 0; |
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::StopInputFileRecording() { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 241 | LOG(LS_WARNING) << "Not implemented"; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 242 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 243 | } |
| 244 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 245 | int32_t AudioDeviceBuffer::StartOutputFileRecording( |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 246 | const char fileName[kAdmMaxFileNameSize]) { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 247 | LOG(LS_WARNING) << "Not implemented"; |
henrika | cf327b4 | 2016-08-19 16:37:53 +0200 | [diff] [blame] | 248 | return 0; |
| 249 | } |
| 250 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 251 | int32_t AudioDeviceBuffer::StopOutputFileRecording() { |
| 252 | LOG(LS_WARNING) << "Not implemented"; |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer, |
| 257 | size_t num_samples) { |
henrika | 073378e | 2016-09-09 13:15:37 +0200 | [diff] [blame] | 258 | UpdateRecordingParameters(); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 259 | // WebRTC can only receive audio in 10ms chunks, hence we fail if the native |
| 260 | // audio layer tries to deliver something else. |
| 261 | RTC_CHECK_EQ(num_samples, rec_samples_per_10ms_); |
| 262 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 263 | rtc::CritScope lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 264 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 265 | if (rec_channel_ == AudioDeviceModule::kChannelBoth) { |
| 266 | // Copy the complete input buffer to the local buffer. |
| 267 | memcpy(&rec_buffer_[0], audio_buffer, rec_bytes_per_10ms_); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 268 | } else { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 269 | int16_t* ptr16In = (int16_t*)audio_buffer; |
| 270 | int16_t* ptr16Out = (int16_t*)&rec_buffer_[0]; |
| 271 | if (AudioDeviceModule::kChannelRight == rec_channel_) { |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 272 | ptr16In++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 273 | } |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 274 | // Exctract left or right channel from input buffer to the local buffer. |
| 275 | for (size_t i = 0; i < rec_samples_per_10ms_; i++) { |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 276 | *ptr16Out = *ptr16In; |
| 277 | ptr16Out++; |
| 278 | ptr16In++; |
| 279 | ptr16In++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 280 | } |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 281 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 282 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 283 | // Update some stats but do it on the task queue to ensure that the members |
| 284 | // are modified and read on the same thread. |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 285 | task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::UpdateRecStats, this, |
| 286 | audio_buffer, num_samples)); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 287 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 288 | } |
| 289 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 290 | int32_t AudioDeviceBuffer::DeliverRecordedData() { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 291 | rtc::CritScope lock(&_critSectCb); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 292 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 293 | if (!audio_transport_cb_) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 294 | LOG(LS_WARNING) << "Invalid audio transport"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 295 | return 0; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | int32_t res(0); |
| 299 | uint32_t newMicLevel(0); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 300 | uint32_t totalDelayMS = play_delay_ms_ + rec_delay_ms_; |
| 301 | res = audio_transport_cb_->RecordedDataIsAvailable( |
| 302 | &rec_buffer_[0], rec_samples_per_10ms_, rec_bytes_per_sample_, |
| 303 | rec_channels_, rec_sample_rate_, totalDelayMS, clock_drift_, |
| 304 | current_mic_level_, typing_status_, newMicLevel); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 305 | if (res != -1) { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 306 | new_mic_level_ = newMicLevel; |
| 307 | } else { |
| 308 | LOG(LS_ERROR) << "RecordedDataIsAvailable() failed"; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 312 | } |
| 313 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 314 | int32_t AudioDeviceBuffer::RequestPlayoutData(size_t num_samples) { |
henrika | 3d7346f | 2016-07-29 16:20:47 +0200 | [diff] [blame] | 315 | // Measure time since last function call and update an array where the |
| 316 | // position/index corresponds to time differences (in milliseconds) between |
| 317 | // two successive playout callbacks, and the stored value is the number of |
| 318 | // times a given time difference was found. |
| 319 | int64_t now_time = rtc::TimeMillis(); |
| 320 | size_t diff_time = rtc::TimeDiff(now_time, last_playout_time_); |
| 321 | // Truncate at 500ms to limit the size of the array. |
| 322 | diff_time = std::min(kMaxDeltaTimeInMs, diff_time); |
| 323 | last_playout_time_ = now_time; |
| 324 | playout_diff_times_[diff_time]++; |
| 325 | |
henrika | 073378e | 2016-09-09 13:15:37 +0200 | [diff] [blame] | 326 | UpdatePlayoutParameters(); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 327 | // WebRTC can only provide audio in 10ms chunks, hence we fail if the native |
| 328 | // audio layer asks for something else. |
| 329 | RTC_CHECK_EQ(num_samples, play_samples_per_10ms_); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 330 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 331 | rtc::CritScope lock(&_critSectCb); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 332 | |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 333 | // It is currently supported to start playout without a valid audio |
| 334 | // transport object. Leads to warning and silence. |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 335 | if (!audio_transport_cb_) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 336 | LOG(LS_WARNING) << "Invalid audio transport"; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 340 | uint32_t res(0); |
| 341 | int64_t elapsed_time_ms = -1; |
| 342 | int64_t ntp_time_ms = -1; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 343 | size_t num_samples_out(0); |
| 344 | res = audio_transport_cb_->NeedMorePlayData( |
| 345 | play_samples_per_10ms_, play_bytes_per_sample_, play_channels_, |
| 346 | play_sample_rate_, &play_buffer_[0], num_samples_out, &elapsed_time_ms, |
| 347 | &ntp_time_ms); |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 348 | if (res != 0) { |
| 349 | LOG(LS_ERROR) << "NeedMorePlayData() failed"; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 350 | } |
| 351 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 352 | // Update some stats but do it on the task queue to ensure that access of |
| 353 | // members is serialized hence avoiding usage of locks. |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 354 | task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::UpdatePlayStats, this, |
| 355 | &play_buffer_[0], num_samples_out)); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 356 | return static_cast<int32_t>(num_samples_out); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 357 | } |
| 358 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 359 | int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 360 | rtc::CritScope lock(&_critSect); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 361 | memcpy(audio_buffer, &play_buffer_[0], play_bytes_per_10ms_); |
| 362 | return static_cast<int32_t>(play_samples_per_10ms_); |
| 363 | } |
punyabrata@webrtc.org | c980146 | 2011-11-29 18:49:54 +0000 | [diff] [blame] | 364 | |
henrika | 073378e | 2016-09-09 13:15:37 +0200 | [diff] [blame] | 365 | void AudioDeviceBuffer::UpdatePlayoutParameters() { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 366 | RTC_CHECK(play_bytes_per_sample_); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 367 | rtc::CritScope lock(&_critSect); |
henrika | 073378e | 2016-09-09 13:15:37 +0200 | [diff] [blame] | 368 | // Update the required buffer size given sample rate and number of channels. |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 369 | play_samples_per_10ms_ = static_cast<size_t>(play_sample_rate_ * 10 / 1000); |
| 370 | play_bytes_per_10ms_ = play_bytes_per_sample_ * play_samples_per_10ms_; |
henrika | 073378e | 2016-09-09 13:15:37 +0200 | [diff] [blame] | 371 | RTC_DCHECK_LE(play_bytes_per_10ms_, kMaxBufferSizeBytes); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 372 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 373 | |
henrika | 073378e | 2016-09-09 13:15:37 +0200 | [diff] [blame] | 374 | void AudioDeviceBuffer::UpdateRecordingParameters() { |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 375 | RTC_CHECK(rec_bytes_per_sample_); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 376 | rtc::CritScope lock(&_critSect); |
henrika | 073378e | 2016-09-09 13:15:37 +0200 | [diff] [blame] | 377 | // Update the required buffer size given sample rate and number of channels. |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 378 | rec_samples_per_10ms_ = static_cast<size_t>(rec_sample_rate_ * 10 / 1000); |
| 379 | rec_bytes_per_10ms_ = rec_bytes_per_sample_ * rec_samples_per_10ms_; |
henrika | 073378e | 2016-09-09 13:15:37 +0200 | [diff] [blame] | 380 | RTC_DCHECK_LE(rec_bytes_per_10ms_, kMaxBufferSizeBytes); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 381 | } |
| 382 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 383 | void AudioDeviceBuffer::StartTimer() { |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 384 | num_stat_reports_ = 0; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 385 | last_log_stat_time_ = rtc::TimeMillis(); |
| 386 | task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this), |
| 387 | kTimerIntervalInMilliseconds); |
| 388 | } |
| 389 | |
| 390 | void AudioDeviceBuffer::LogStats() { |
| 391 | RTC_DCHECK(task_queue_.IsCurrent()); |
| 392 | |
| 393 | int64_t now_time = rtc::TimeMillis(); |
| 394 | int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds; |
| 395 | int64_t time_since_last = rtc::TimeDiff(now_time, last_log_stat_time_); |
| 396 | last_log_stat_time_ = now_time; |
| 397 | |
| 398 | // Log the latest statistics but skip the first 10 seconds since we are not |
| 399 | // sure of the exact starting point. I.e., the first log printout will be |
| 400 | // after ~20 seconds. |
| 401 | if (++num_stat_reports_ > 1) { |
| 402 | uint32_t diff_samples = rec_samples_ - last_rec_samples_; |
| 403 | uint32_t rate = diff_samples / kTimerIntervalInSeconds; |
| 404 | LOG(INFO) << "[REC : " << time_since_last << "msec, " |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 405 | << rec_sample_rate_ / 1000 |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 406 | << "kHz] callbacks: " << rec_callbacks_ - last_rec_callbacks_ |
| 407 | << ", " |
| 408 | << "samples: " << diff_samples << ", " |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 409 | << "rate: " << rate << ", " |
| 410 | << "level: " << max_rec_level_; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 411 | |
| 412 | diff_samples = play_samples_ - last_play_samples_; |
| 413 | rate = diff_samples / kTimerIntervalInSeconds; |
| 414 | LOG(INFO) << "[PLAY: " << time_since_last << "msec, " |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 415 | << play_sample_rate_ / 1000 |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 416 | << "kHz] callbacks: " << play_callbacks_ - last_play_callbacks_ |
| 417 | << ", " |
| 418 | << "samples: " << diff_samples << ", " |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 419 | << "rate: " << rate << ", " |
| 420 | << "level: " << max_play_level_; |
| 421 | } |
| 422 | |
| 423 | // Count number of times we detect "no audio" corresponding to a case where |
| 424 | // all level measurements have been zero. |
| 425 | if (max_rec_level_ == 0) { |
| 426 | ++num_rec_level_is_zero_; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | last_rec_callbacks_ = rec_callbacks_; |
| 430 | last_play_callbacks_ = play_callbacks_; |
| 431 | last_rec_samples_ = rec_samples_; |
| 432 | last_play_samples_ = play_samples_; |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 433 | max_rec_level_ = 0; |
| 434 | max_play_level_ = 0; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 435 | |
| 436 | int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis(); |
| 437 | RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval"; |
| 438 | |
| 439 | // Update some stats but do it on the task queue to ensure that access of |
| 440 | // members is serialized hence avoiding usage of locks. |
| 441 | task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this), |
| 442 | time_to_wait_ms); |
| 443 | } |
| 444 | |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 445 | void AudioDeviceBuffer::ResetRecStats() { |
| 446 | rec_callbacks_ = 0; |
| 447 | last_rec_callbacks_ = 0; |
| 448 | rec_samples_ = 0; |
| 449 | last_rec_samples_ = 0; |
| 450 | max_rec_level_ = 0; |
| 451 | num_rec_level_is_zero_ = 0; |
| 452 | } |
| 453 | |
| 454 | void AudioDeviceBuffer::ResetPlayStats() { |
| 455 | last_playout_time_ = rtc::TimeMillis(); |
| 456 | play_callbacks_ = 0; |
| 457 | last_play_callbacks_ = 0; |
| 458 | play_samples_ = 0; |
| 459 | last_play_samples_ = 0; |
| 460 | max_play_level_ = 0; |
| 461 | } |
| 462 | |
| 463 | void AudioDeviceBuffer::UpdateRecStats(const void* audio_buffer, |
| 464 | size_t num_samples) { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 465 | RTC_DCHECK(task_queue_.IsCurrent()); |
| 466 | ++rec_callbacks_; |
| 467 | rec_samples_ += num_samples; |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 468 | |
| 469 | // Find the max absolute value in an audio packet twice per second and update |
| 470 | // |max_rec_level_| to track the largest value. |
| 471 | if (rec_callbacks_ % 50 == 0) { |
| 472 | int16_t max_abs = WebRtcSpl_MaxAbsValueW16( |
| 473 | static_cast<int16_t*>(const_cast<void*>(audio_buffer)), |
| 474 | num_samples * rec_channels_); |
| 475 | if (max_abs > max_rec_level_) { |
| 476 | max_rec_level_ = max_abs; |
| 477 | } |
| 478 | } |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 479 | } |
| 480 | |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 481 | void AudioDeviceBuffer::UpdatePlayStats(const void* audio_buffer, |
| 482 | size_t num_samples) { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 483 | RTC_DCHECK(task_queue_.IsCurrent()); |
| 484 | ++play_callbacks_; |
| 485 | play_samples_ += num_samples; |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 486 | |
| 487 | // Find the max absolute value in an audio packet twice per second and update |
| 488 | // |max_play_level_| to track the largest value. |
| 489 | if (play_callbacks_ % 50 == 0) { |
| 490 | int16_t max_abs = WebRtcSpl_MaxAbsValueW16( |
| 491 | static_cast<int16_t*>(const_cast<void*>(audio_buffer)), |
| 492 | num_samples * play_channels_); |
| 493 | if (max_abs > max_play_level_) { |
| 494 | max_play_level_ = max_abs; |
| 495 | } |
| 496 | } |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 497 | } |
| 498 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 499 | } // namespace webrtc |