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