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