2011-07-07 08:21:25 +00:00
|
|
|
/*
|
2012-02-20 09:17:41 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-07-07 08:21:25 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-07-29 16:20:47 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
2013-07-11 13:24:38 +00:00
|
|
|
#include "webrtc/modules/audio_device/audio_device_buffer.h"
|
2013-09-13 00:02:13 +00:00
|
|
|
|
2016-07-29 16:20:47 +02:00
|
|
|
#include "webrtc/base/arraysize.h"
|
2016-07-14 05:54:19 -07:00
|
|
|
#include "webrtc/base/bind.h"
|
2016-07-06 00:33:57 -07:00
|
|
|
#include "webrtc/base/checks.h"
|
|
|
|
|
#include "webrtc/base/logging.h"
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
#include "webrtc/base/format_macros.h"
|
2016-07-14 05:54:19 -07:00
|
|
|
#include "webrtc/base/timeutils.h"
|
2013-07-11 13:24:38 +00:00
|
|
|
#include "webrtc/modules/audio_device/audio_device_config.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2016-07-14 05:54:19 -07:00
|
|
|
static const char kTimerQueueName[] = "AudioDeviceBufferTimer";
|
|
|
|
|
|
|
|
|
|
// Time between two sucessive calls to LogStats().
|
|
|
|
|
static const size_t kTimerIntervalInSeconds = 10;
|
|
|
|
|
static const size_t kTimerIntervalInMilliseconds =
|
|
|
|
|
kTimerIntervalInSeconds * rtc::kNumMillisecsPerSec;
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
AudioDeviceBuffer::AudioDeviceBuffer()
|
2016-08-22 05:56:12 -07:00
|
|
|
: audio_transport_cb_(nullptr),
|
2016-07-14 05:54:19 -07:00
|
|
|
task_queue_(kTimerQueueName),
|
|
|
|
|
timer_has_started_(false),
|
2016-08-22 05:56:12 -07:00
|
|
|
rec_sample_rate_(0),
|
|
|
|
|
play_sample_rate_(0),
|
|
|
|
|
rec_channels_(0),
|
|
|
|
|
play_channels_(0),
|
|
|
|
|
rec_channel_(AudioDeviceModule::kChannelBoth),
|
|
|
|
|
rec_bytes_per_sample_(0),
|
|
|
|
|
play_bytes_per_sample_(0),
|
|
|
|
|
rec_samples_per_10ms_(0),
|
|
|
|
|
rec_bytes_per_10ms_(0),
|
|
|
|
|
play_samples_per_10ms_(0),
|
|
|
|
|
play_bytes_per_10ms_(0),
|
|
|
|
|
current_mic_level_(0),
|
|
|
|
|
new_mic_level_(0),
|
|
|
|
|
typing_status_(false),
|
|
|
|
|
play_delay_ms_(0),
|
|
|
|
|
rec_delay_ms_(0),
|
|
|
|
|
clock_drift_(0),
|
2016-07-14 05:54:19 -07:00
|
|
|
num_stat_reports_(0),
|
|
|
|
|
rec_callbacks_(0),
|
|
|
|
|
last_rec_callbacks_(0),
|
|
|
|
|
play_callbacks_(0),
|
|
|
|
|
last_play_callbacks_(0),
|
|
|
|
|
rec_samples_(0),
|
|
|
|
|
last_rec_samples_(0),
|
|
|
|
|
play_samples_(0),
|
|
|
|
|
last_play_samples_(0),
|
|
|
|
|
last_log_stat_time_(0) {
|
2016-07-06 00:33:57 -07:00
|
|
|
LOG(INFO) << "AudioDeviceBuffer::ctor";
|
2016-09-09 13:15:37 +02:00
|
|
|
// TODO(henrika): improve buffer handling and ensure that we don't allocate
|
|
|
|
|
// more than what is required.
|
|
|
|
|
play_buffer_.reset(new int8_t[kMaxBufferSizeBytes]);
|
|
|
|
|
rec_buffer_.reset(new int8_t[kMaxBufferSizeBytes]);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
AudioDeviceBuffer::~AudioDeviceBuffer() {
|
2016-07-14 05:54:19 -07:00
|
|
|
RTC_DCHECK(thread_checker_.CalledOnValidThread());
|
2016-07-06 00:33:57 -07:00
|
|
|
LOG(INFO) << "AudioDeviceBuffer::~dtor";
|
2016-07-29 16:20:47 +02:00
|
|
|
|
|
|
|
|
size_t total_diff_time = 0;
|
|
|
|
|
int num_measurements = 0;
|
|
|
|
|
LOG(INFO) << "[playout diff time => #measurements]";
|
|
|
|
|
for (size_t diff = 0; diff < arraysize(playout_diff_times_); ++diff) {
|
|
|
|
|
uint32_t num_elements = playout_diff_times_[diff];
|
|
|
|
|
if (num_elements > 0) {
|
|
|
|
|
total_diff_time += num_elements * diff;
|
|
|
|
|
num_measurements += num_elements;
|
|
|
|
|
LOG(INFO) << "[" << diff << " => " << num_elements << "]";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (num_measurements > 0) {
|
|
|
|
|
LOG(INFO) << "total_diff_time: " << total_diff_time;
|
|
|
|
|
LOG(INFO) << "num_measurements: " << num_measurements;
|
|
|
|
|
LOG(INFO) << "average: "
|
|
|
|
|
<< static_cast<float>(total_diff_time) / num_measurements;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
int32_t AudioDeviceBuffer::RegisterAudioCallback(
|
2016-08-22 05:56:12 -07:00
|
|
|
AudioTransport* audio_callback) {
|
2016-07-06 00:33:57 -07:00
|
|
|
LOG(INFO) << __FUNCTION__;
|
2016-07-14 05:54:19 -07:00
|
|
|
rtc::CritScope lock(&_critSectCb);
|
2016-08-22 05:56:12 -07:00
|
|
|
audio_transport_cb_ = audio_callback;
|
2016-07-04 13:01:19 +02:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
int32_t AudioDeviceBuffer::InitPlayout() {
|
2016-08-19 08:09:25 -07:00
|
|
|
LOG(INFO) << __FUNCTION__;
|
2016-08-22 05:56:12 -07:00
|
|
|
RTC_DCHECK(thread_checker_.CalledOnValidThread());
|
2016-07-29 16:20:47 +02:00
|
|
|
last_playout_time_ = rtc::TimeMillis();
|
2016-07-14 05:54:19 -07:00
|
|
|
if (!timer_has_started_) {
|
|
|
|
|
StartTimer();
|
|
|
|
|
timer_has_started_ = true;
|
|
|
|
|
}
|
2016-07-04 13:01:19 +02:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
int32_t AudioDeviceBuffer::InitRecording() {
|
2016-08-19 08:09:25 -07:00
|
|
|
LOG(INFO) << __FUNCTION__;
|
2016-08-22 05:56:12 -07:00
|
|
|
RTC_DCHECK(thread_checker_.CalledOnValidThread());
|
2016-07-14 05:54:19 -07:00
|
|
|
if (!timer_has_started_) {
|
|
|
|
|
StartTimer();
|
|
|
|
|
timer_has_started_ = true;
|
|
|
|
|
}
|
2016-07-04 13:01:19 +02:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) {
|
2016-07-06 00:33:57 -07:00
|
|
|
LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")";
|
2016-07-14 05:54:19 -07:00
|
|
|
rtc::CritScope lock(&_critSect);
|
2016-08-22 05:56:12 -07:00
|
|
|
rec_sample_rate_ = fsHz;
|
2016-07-04 13:01:19 +02:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) {
|
2016-07-06 00:33:57 -07:00
|
|
|
LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")";
|
2016-07-14 05:54:19 -07:00
|
|
|
rtc::CritScope lock(&_critSect);
|
2016-08-22 05:56:12 -07:00
|
|
|
play_sample_rate_ = fsHz;
|
2016-07-04 13:01:19 +02:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
int32_t AudioDeviceBuffer::RecordingSampleRate() const {
|
2016-08-22 05:56:12 -07:00
|
|
|
return rec_sample_rate_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
int32_t AudioDeviceBuffer::PlayoutSampleRate() const {
|
2016-08-22 05:56:12 -07:00
|
|
|
return play_sample_rate_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) {
|
2016-08-22 05:56:12 -07:00
|
|
|
LOG(INFO) << "SetRecordingChannels(" << channels << ")";
|
2016-07-14 05:54:19 -07:00
|
|
|
rtc::CritScope lock(&_critSect);
|
2016-08-22 05:56:12 -07:00
|
|
|
rec_channels_ = channels;
|
|
|
|
|
rec_bytes_per_sample_ =
|
2016-07-04 13:01:19 +02:00
|
|
|
2 * channels; // 16 bits per sample in mono, 32 bits in stereo
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) {
|
2016-08-22 05:56:12 -07:00
|
|
|
LOG(INFO) << "SetPlayoutChannels(" << channels << ")";
|
2016-07-14 05:54:19 -07:00
|
|
|
rtc::CritScope lock(&_critSect);
|
2016-08-22 05:56:12 -07:00
|
|
|
play_channels_ = channels;
|
2016-07-04 13:01:19 +02:00
|
|
|
// 16 bits per sample in mono, 32 bits in stereo
|
2016-08-22 05:56:12 -07:00
|
|
|
play_bytes_per_sample_ = 2 * channels;
|
2016-07-04 13:01:19 +02:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
int32_t AudioDeviceBuffer::SetRecordingChannel(
|
|
|
|
|
const AudioDeviceModule::ChannelType channel) {
|
2016-07-14 05:54:19 -07:00
|
|
|
rtc::CritScope lock(&_critSect);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2016-08-22 05:56:12 -07:00
|
|
|
if (rec_channels_ == 1) {
|
2016-07-04 13:01:19 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
if (channel == AudioDeviceModule::kChannelBoth) {
|
|
|
|
|
// two bytes per channel
|
2016-08-22 05:56:12 -07:00
|
|
|
rec_bytes_per_sample_ = 4;
|
2016-07-04 13:01:19 +02:00
|
|
|
} else {
|
|
|
|
|
// only utilize one out of two possible channels (left or right)
|
2016-08-22 05:56:12 -07:00
|
|
|
rec_bytes_per_sample_ = 2;
|
2016-07-04 13:01:19 +02:00
|
|
|
}
|
2016-08-22 05:56:12 -07:00
|
|
|
rec_channel_ = channel;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
int32_t AudioDeviceBuffer::RecordingChannel(
|
|
|
|
|
AudioDeviceModule::ChannelType& channel) const {
|
2016-08-22 05:56:12 -07:00
|
|
|
channel = rec_channel_;
|
2016-07-04 13:01:19 +02:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
size_t AudioDeviceBuffer::RecordingChannels() const {
|
2016-08-22 05:56:12 -07:00
|
|
|
return rec_channels_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
size_t AudioDeviceBuffer::PlayoutChannels() const {
|
2016-08-22 05:56:12 -07:00
|
|
|
return play_channels_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
int32_t AudioDeviceBuffer::SetCurrentMicLevel(uint32_t level) {
|
2016-08-22 05:56:12 -07:00
|
|
|
current_mic_level_ = level;
|
2016-07-04 13:01:19 +02:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-22 05:56:12 -07:00
|
|
|
int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) {
|
|
|
|
|
typing_status_ = typing_status;
|
2016-07-04 13:01:19 +02:00
|
|
|
return 0;
|
2013-05-07 21:04:24 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
uint32_t AudioDeviceBuffer::NewMicLevel() const {
|
2016-08-22 05:56:12 -07:00
|
|
|
return new_mic_level_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-22 05:56:12 -07:00
|
|
|
void AudioDeviceBuffer::SetVQEData(int play_delay_ms,
|
|
|
|
|
int rec_delay_ms,
|
|
|
|
|
int clock_drift) {
|
|
|
|
|
play_delay_ms_ = play_delay_ms;
|
|
|
|
|
rec_delay_ms_ = rec_delay_ms;
|
|
|
|
|
clock_drift_ = clock_drift;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-09 10:30:35 +00:00
|
|
|
int32_t AudioDeviceBuffer::StartInputFileRecording(
|
2016-07-04 13:01:19 +02:00
|
|
|
const char fileName[kAdmMaxFileNameSize]) {
|
2016-08-22 05:56:12 -07:00
|
|
|
LOG(LS_WARNING) << "Not implemented";
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
int32_t AudioDeviceBuffer::StopInputFileRecording() {
|
2016-08-22 05:56:12 -07:00
|
|
|
LOG(LS_WARNING) << "Not implemented";
|
2016-07-04 13:01:19 +02:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-09 10:30:35 +00:00
|
|
|
int32_t AudioDeviceBuffer::StartOutputFileRecording(
|
2016-07-04 13:01:19 +02:00
|
|
|
const char fileName[kAdmMaxFileNameSize]) {
|
2016-08-22 05:56:12 -07:00
|
|
|
LOG(LS_WARNING) << "Not implemented";
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
int32_t AudioDeviceBuffer::StopOutputFileRecording() {
|
2016-08-22 05:56:12 -07:00
|
|
|
LOG(LS_WARNING) << "Not implemented";
|
2016-07-04 13:01:19 +02:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-22 05:56:12 -07:00
|
|
|
int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer,
|
|
|
|
|
size_t num_samples) {
|
2016-09-09 13:15:37 +02:00
|
|
|
UpdateRecordingParameters();
|
2016-08-22 05:56:12 -07:00
|
|
|
// WebRTC can only receive audio in 10ms chunks, hence we fail if the native
|
|
|
|
|
// audio layer tries to deliver something else.
|
|
|
|
|
RTC_CHECK_EQ(num_samples, rec_samples_per_10ms_);
|
2016-08-19 08:09:25 -07:00
|
|
|
|
2016-08-22 05:56:12 -07:00
|
|
|
rtc::CritScope lock(&_critSect);
|
2016-08-19 08:09:25 -07:00
|
|
|
|
2016-08-22 05:56:12 -07:00
|
|
|
if (rec_channel_ == AudioDeviceModule::kChannelBoth) {
|
|
|
|
|
// Copy the complete input buffer to the local buffer.
|
|
|
|
|
memcpy(&rec_buffer_[0], audio_buffer, rec_bytes_per_10ms_);
|
2016-07-04 13:01:19 +02:00
|
|
|
} else {
|
2016-08-22 05:56:12 -07:00
|
|
|
int16_t* ptr16In = (int16_t*)audio_buffer;
|
|
|
|
|
int16_t* ptr16Out = (int16_t*)&rec_buffer_[0];
|
|
|
|
|
if (AudioDeviceModule::kChannelRight == rec_channel_) {
|
2016-07-04 13:01:19 +02:00
|
|
|
ptr16In++;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2016-08-22 05:56:12 -07:00
|
|
|
// Exctract left or right channel from input buffer to the local buffer.
|
|
|
|
|
for (size_t i = 0; i < rec_samples_per_10ms_; i++) {
|
2016-07-04 13:01:19 +02:00
|
|
|
*ptr16Out = *ptr16In;
|
|
|
|
|
ptr16Out++;
|
|
|
|
|
ptr16In++;
|
|
|
|
|
ptr16In++;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2016-07-04 13:01:19 +02:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2016-07-14 05:54:19 -07:00
|
|
|
// Update some stats but do it on the task queue to ensure that the members
|
|
|
|
|
// are modified and read on the same thread.
|
|
|
|
|
task_queue_.PostTask(
|
2016-08-22 05:56:12 -07:00
|
|
|
rtc::Bind(&AudioDeviceBuffer::UpdateRecStats, this, num_samples));
|
2016-07-04 13:01:19 +02:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
int32_t AudioDeviceBuffer::DeliverRecordedData() {
|
2016-08-22 05:56:12 -07:00
|
|
|
RTC_DCHECK(audio_transport_cb_);
|
2016-07-14 05:54:19 -07:00
|
|
|
rtc::CritScope lock(&_critSectCb);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2016-08-22 05:56:12 -07:00
|
|
|
if (!audio_transport_cb_) {
|
2016-07-06 00:33:57 -07:00
|
|
|
LOG(LS_WARNING) << "Invalid audio transport";
|
2016-07-04 13:01:19 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
int32_t res(0);
|
|
|
|
|
uint32_t newMicLevel(0);
|
2016-08-22 05:56:12 -07:00
|
|
|
uint32_t totalDelayMS = play_delay_ms_ + rec_delay_ms_;
|
|
|
|
|
res = audio_transport_cb_->RecordedDataIsAvailable(
|
|
|
|
|
&rec_buffer_[0], rec_samples_per_10ms_, rec_bytes_per_sample_,
|
|
|
|
|
rec_channels_, rec_sample_rate_, totalDelayMS, clock_drift_,
|
|
|
|
|
current_mic_level_, typing_status_, newMicLevel);
|
2016-07-04 13:01:19 +02:00
|
|
|
if (res != -1) {
|
2016-08-22 05:56:12 -07:00
|
|
|
new_mic_level_ = newMicLevel;
|
|
|
|
|
} else {
|
|
|
|
|
LOG(LS_ERROR) << "RecordedDataIsAvailable() failed";
|
2016-07-04 13:01:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-22 05:56:12 -07:00
|
|
|
int32_t AudioDeviceBuffer::RequestPlayoutData(size_t num_samples) {
|
2016-07-29 16:20:47 +02:00
|
|
|
// Measure time since last function call and update an array where the
|
|
|
|
|
// position/index corresponds to time differences (in milliseconds) between
|
|
|
|
|
// two successive playout callbacks, and the stored value is the number of
|
|
|
|
|
// times a given time difference was found.
|
|
|
|
|
int64_t now_time = rtc::TimeMillis();
|
|
|
|
|
size_t diff_time = rtc::TimeDiff(now_time, last_playout_time_);
|
|
|
|
|
// Truncate at 500ms to limit the size of the array.
|
|
|
|
|
diff_time = std::min(kMaxDeltaTimeInMs, diff_time);
|
|
|
|
|
last_playout_time_ = now_time;
|
|
|
|
|
playout_diff_times_[diff_time]++;
|
|
|
|
|
|
2016-09-09 13:15:37 +02:00
|
|
|
UpdatePlayoutParameters();
|
2016-08-22 05:56:12 -07:00
|
|
|
// WebRTC can only provide audio in 10ms chunks, hence we fail if the native
|
|
|
|
|
// audio layer asks for something else.
|
|
|
|
|
RTC_CHECK_EQ(num_samples, play_samples_per_10ms_);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2016-07-14 05:54:19 -07:00
|
|
|
rtc::CritScope lock(&_critSectCb);
|
2016-07-04 13:01:19 +02:00
|
|
|
|
2016-07-06 00:33:57 -07:00
|
|
|
// It is currently supported to start playout without a valid audio
|
|
|
|
|
// transport object. Leads to warning and silence.
|
2016-08-22 05:56:12 -07:00
|
|
|
if (!audio_transport_cb_) {
|
2016-07-06 00:33:57 -07:00
|
|
|
LOG(LS_WARNING) << "Invalid audio transport";
|
2016-07-04 13:01:19 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-06 00:33:57 -07:00
|
|
|
uint32_t res(0);
|
|
|
|
|
int64_t elapsed_time_ms = -1;
|
|
|
|
|
int64_t ntp_time_ms = -1;
|
2016-08-22 05:56:12 -07:00
|
|
|
size_t num_samples_out(0);
|
|
|
|
|
res = audio_transport_cb_->NeedMorePlayData(
|
|
|
|
|
play_samples_per_10ms_, play_bytes_per_sample_, play_channels_,
|
|
|
|
|
play_sample_rate_, &play_buffer_[0], num_samples_out, &elapsed_time_ms,
|
|
|
|
|
&ntp_time_ms);
|
2016-07-06 00:33:57 -07:00
|
|
|
if (res != 0) {
|
|
|
|
|
LOG(LS_ERROR) << "NeedMorePlayData() failed";
|
2016-07-04 13:01:19 +02:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2016-07-14 05:54:19 -07:00
|
|
|
// Update some stats but do it on the task queue to ensure that access of
|
|
|
|
|
// members is serialized hence avoiding usage of locks.
|
|
|
|
|
task_queue_.PostTask(
|
2016-08-22 05:56:12 -07:00
|
|
|
rtc::Bind(&AudioDeviceBuffer::UpdatePlayStats, this, num_samples_out));
|
|
|
|
|
return static_cast<int32_t>(num_samples_out);
|
2016-08-19 16:37:53 +02:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2016-08-22 05:56:12 -07:00
|
|
|
int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) {
|
2016-08-19 16:37:53 +02:00
|
|
|
rtc::CritScope lock(&_critSect);
|
2016-08-22 05:56:12 -07:00
|
|
|
memcpy(audio_buffer, &play_buffer_[0], play_bytes_per_10ms_);
|
|
|
|
|
return static_cast<int32_t>(play_samples_per_10ms_);
|
|
|
|
|
}
|
2016-08-19 08:09:25 -07:00
|
|
|
|
2016-09-09 13:15:37 +02:00
|
|
|
void AudioDeviceBuffer::UpdatePlayoutParameters() {
|
2016-08-22 05:56:12 -07:00
|
|
|
RTC_CHECK(play_bytes_per_sample_);
|
|
|
|
|
rtc::CritScope lock(&_critSect);
|
2016-09-09 13:15:37 +02:00
|
|
|
// Update the required buffer size given sample rate and number of channels.
|
2016-08-22 05:56:12 -07:00
|
|
|
play_samples_per_10ms_ = static_cast<size_t>(play_sample_rate_ * 10 / 1000);
|
|
|
|
|
play_bytes_per_10ms_ = play_bytes_per_sample_ * play_samples_per_10ms_;
|
2016-09-09 13:15:37 +02:00
|
|
|
RTC_DCHECK_LE(play_bytes_per_10ms_, kMaxBufferSizeBytes);
|
2016-08-22 05:56:12 -07:00
|
|
|
}
|
2016-08-19 08:09:25 -07:00
|
|
|
|
2016-09-09 13:15:37 +02:00
|
|
|
void AudioDeviceBuffer::UpdateRecordingParameters() {
|
2016-08-22 05:56:12 -07:00
|
|
|
RTC_CHECK(rec_bytes_per_sample_);
|
|
|
|
|
rtc::CritScope lock(&_critSect);
|
2016-09-09 13:15:37 +02:00
|
|
|
// Update the required buffer size given sample rate and number of channels.
|
2016-08-22 05:56:12 -07:00
|
|
|
rec_samples_per_10ms_ = static_cast<size_t>(rec_sample_rate_ * 10 / 1000);
|
|
|
|
|
rec_bytes_per_10ms_ = rec_bytes_per_sample_ * rec_samples_per_10ms_;
|
2016-09-09 13:15:37 +02:00
|
|
|
RTC_DCHECK_LE(rec_bytes_per_10ms_, kMaxBufferSizeBytes);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-14 05:54:19 -07:00
|
|
|
void AudioDeviceBuffer::StartTimer() {
|
|
|
|
|
last_log_stat_time_ = rtc::TimeMillis();
|
|
|
|
|
task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this),
|
|
|
|
|
kTimerIntervalInMilliseconds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AudioDeviceBuffer::LogStats() {
|
|
|
|
|
RTC_DCHECK(task_queue_.IsCurrent());
|
|
|
|
|
|
|
|
|
|
int64_t now_time = rtc::TimeMillis();
|
|
|
|
|
int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds;
|
|
|
|
|
int64_t time_since_last = rtc::TimeDiff(now_time, last_log_stat_time_);
|
|
|
|
|
last_log_stat_time_ = now_time;
|
|
|
|
|
|
|
|
|
|
// Log the latest statistics but skip the first 10 seconds since we are not
|
|
|
|
|
// sure of the exact starting point. I.e., the first log printout will be
|
|
|
|
|
// after ~20 seconds.
|
|
|
|
|
if (++num_stat_reports_ > 1) {
|
|
|
|
|
uint32_t diff_samples = rec_samples_ - last_rec_samples_;
|
|
|
|
|
uint32_t rate = diff_samples / kTimerIntervalInSeconds;
|
|
|
|
|
LOG(INFO) << "[REC : " << time_since_last << "msec, "
|
2016-08-22 05:56:12 -07:00
|
|
|
<< rec_sample_rate_ / 1000
|
2016-07-14 05:54:19 -07:00
|
|
|
<< "kHz] callbacks: " << rec_callbacks_ - last_rec_callbacks_
|
|
|
|
|
<< ", "
|
|
|
|
|
<< "samples: " << diff_samples << ", "
|
|
|
|
|
<< "rate: " << rate;
|
|
|
|
|
|
|
|
|
|
diff_samples = play_samples_ - last_play_samples_;
|
|
|
|
|
rate = diff_samples / kTimerIntervalInSeconds;
|
|
|
|
|
LOG(INFO) << "[PLAY: " << time_since_last << "msec, "
|
2016-08-22 05:56:12 -07:00
|
|
|
<< play_sample_rate_ / 1000
|
2016-07-14 05:54:19 -07:00
|
|
|
<< "kHz] callbacks: " << play_callbacks_ - last_play_callbacks_
|
|
|
|
|
<< ", "
|
|
|
|
|
<< "samples: " << diff_samples << ", "
|
|
|
|
|
<< "rate: " << rate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
last_rec_callbacks_ = rec_callbacks_;
|
|
|
|
|
last_play_callbacks_ = play_callbacks_;
|
|
|
|
|
last_rec_samples_ = rec_samples_;
|
|
|
|
|
last_play_samples_ = play_samples_;
|
|
|
|
|
|
|
|
|
|
int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis();
|
|
|
|
|
RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval";
|
|
|
|
|
|
|
|
|
|
// Update some stats but do it on the task queue to ensure that access of
|
|
|
|
|
// members is serialized hence avoiding usage of locks.
|
|
|
|
|
task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this),
|
|
|
|
|
time_to_wait_ms);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AudioDeviceBuffer::UpdateRecStats(size_t num_samples) {
|
|
|
|
|
RTC_DCHECK(task_queue_.IsCurrent());
|
|
|
|
|
++rec_callbacks_;
|
|
|
|
|
rec_samples_ += num_samples;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AudioDeviceBuffer::UpdatePlayStats(size_t num_samples) {
|
|
|
|
|
RTC_DCHECK(task_queue_.IsCurrent());
|
|
|
|
|
++play_callbacks_;
|
|
|
|
|
play_samples_ += num_samples;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-07 08:21:25 +00:00
|
|
|
} // namespace webrtc
|