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"
|
2016-09-09 14:23:11 +02:00
|
|
|
#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
|
2013-07-11 13:24:38 +00:00
|
|
|
#include "webrtc/modules/audio_device/audio_device_config.h"
|
2016-09-09 14:23:11 +02:00
|
|
|
#include "webrtc/system_wrappers/include/metrics.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-10-31 08:18:50 -07:00
|
|
|
// Min time required to qualify an audio session as a "call". If playout or
|
|
|
|
|
// recording has been active for less than this time we will not store any
|
|
|
|
|
// logs or UMA stats but instead consider the call as too short.
|
|
|
|
|
static const size_t kMinValidCallTimeTimeInSeconds = 10;
|
|
|
|
|
static const size_t kMinValidCallTimeTimeInMilliseconds =
|
|
|
|
|
kMinValidCallTimeTimeInSeconds * rtc::kNumMillisecsPerSec;
|
2016-07-14 05:54:19 -07:00
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
AudioDeviceBuffer::AudioDeviceBuffer()
|
2016-11-07 15:56:59 +01:00
|
|
|
: task_queue_(kTimerQueueName),
|
|
|
|
|
audio_transport_cb_(nullptr),
|
2016-08-22 05:56:12 -07:00
|
|
|
rec_sample_rate_(0),
|
|
|
|
|
play_sample_rate_(0),
|
|
|
|
|
rec_channels_(0),
|
|
|
|
|
play_channels_(0),
|
2016-11-07 15:56:59 +01:00
|
|
|
playing_(false),
|
|
|
|
|
recording_(false),
|
2016-08-22 05:56:12 -07:00
|
|
|
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),
|
2016-09-09 14:23:11 +02:00
|
|
|
max_rec_level_(0),
|
|
|
|
|
max_play_level_(0),
|
2016-11-07 15:56:59 +01:00
|
|
|
last_timer_task_time_(0),
|
2016-10-21 12:45:25 +02:00
|
|
|
rec_stat_count_(0),
|
2016-10-31 08:18:50 -07:00
|
|
|
play_stat_count_(0),
|
|
|
|
|
play_start_time_(0),
|
|
|
|
|
rec_start_time_(0),
|
|
|
|
|
only_silence_recorded_(true) {
|
2016-07-06 00:33:57 -07:00
|
|
|
LOG(INFO) << "AudioDeviceBuffer::ctor";
|
2016-11-07 15:56:59 +01:00
|
|
|
playout_thread_checker_.DetachFromThread();
|
|
|
|
|
recording_thread_checker_.DetachFromThread();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
AudioDeviceBuffer::~AudioDeviceBuffer() {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&main_thread_checker_);
|
2016-10-31 08:18:50 -07:00
|
|
|
RTC_DCHECK(!playing_);
|
|
|
|
|
RTC_DCHECK(!recording_);
|
2016-07-06 00:33:57 -07:00
|
|
|
LOG(INFO) << "AudioDeviceBuffer::~dtor";
|
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-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&main_thread_checker_);
|
2016-07-06 00:33:57 -07:00
|
|
|
LOG(INFO) << __FUNCTION__;
|
2016-11-07 15:56:59 +01:00
|
|
|
if (playing_ || recording_) {
|
|
|
|
|
LOG(LS_ERROR) << "Failed to set audio transport since media was active";
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
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-10-31 08:18:50 -07:00
|
|
|
void AudioDeviceBuffer::StartPlayout() {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&main_thread_checker_);
|
2016-10-31 08:18:50 -07:00
|
|
|
// TODO(henrika): allow for usage of DCHECK(!playing_) here instead. Today the
|
|
|
|
|
// ADM allows calling Start(), Start() by ignoring the second call but it
|
|
|
|
|
// makes more sense to only allow one call.
|
|
|
|
|
if (playing_) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-08-19 08:09:25 -07:00
|
|
|
LOG(INFO) << __FUNCTION__;
|
2016-11-07 15:56:59 +01:00
|
|
|
playout_thread_checker_.DetachFromThread();
|
2016-10-31 08:18:50 -07:00
|
|
|
// Clear members tracking playout stats and do it on the task queue.
|
|
|
|
|
task_queue_.PostTask([this] { ResetPlayStats(); });
|
|
|
|
|
// Start a periodic timer based on task queue if not already done by the
|
|
|
|
|
// recording side.
|
|
|
|
|
if (!recording_) {
|
|
|
|
|
StartPeriodicLogging();
|
|
|
|
|
}
|
|
|
|
|
const uint64_t now_time = rtc::TimeMillis();
|
|
|
|
|
// Clear members that are only touched on the main (creating) thread.
|
|
|
|
|
play_start_time_ = now_time;
|
|
|
|
|
playing_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AudioDeviceBuffer::StartRecording() {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&main_thread_checker_);
|
2016-10-31 08:18:50 -07:00
|
|
|
if (recording_) {
|
|
|
|
|
return;
|
2016-07-14 05:54:19 -07:00
|
|
|
}
|
2016-10-31 08:18:50 -07:00
|
|
|
LOG(INFO) << __FUNCTION__;
|
2016-11-07 15:56:59 +01:00
|
|
|
recording_thread_checker_.DetachFromThread();
|
2016-10-31 08:18:50 -07:00
|
|
|
// Clear members tracking recording stats and do it on the task queue.
|
|
|
|
|
task_queue_.PostTask([this] { ResetRecStats(); });
|
|
|
|
|
// Start a periodic timer based on task queue if not already done by the
|
|
|
|
|
// playout side.
|
|
|
|
|
if (!playing_) {
|
|
|
|
|
StartPeriodicLogging();
|
|
|
|
|
}
|
|
|
|
|
// Clear members that will be touched on the main (creating) thread.
|
|
|
|
|
rec_start_time_ = rtc::TimeMillis();
|
|
|
|
|
recording_ = true;
|
|
|
|
|
// And finally a member which can be modified on the native audio thread.
|
|
|
|
|
// It is safe to do so since we know by design that the owning ADM has not
|
|
|
|
|
// yet started the native audio recording.
|
|
|
|
|
only_silence_recorded_ = true;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-31 08:18:50 -07:00
|
|
|
void AudioDeviceBuffer::StopPlayout() {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&main_thread_checker_);
|
2016-10-31 08:18:50 -07:00
|
|
|
if (!playing_) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-08-19 08:09:25 -07:00
|
|
|
LOG(INFO) << __FUNCTION__;
|
2016-10-31 08:18:50 -07:00
|
|
|
playing_ = false;
|
|
|
|
|
// Stop periodic logging if no more media is active.
|
|
|
|
|
if (!recording_) {
|
|
|
|
|
StopPeriodicLogging();
|
|
|
|
|
}
|
2016-11-07 15:56:59 +01:00
|
|
|
LOG(INFO) << "total playout time: " << rtc::TimeSince(play_start_time_);
|
2016-10-31 08:18:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AudioDeviceBuffer::StopRecording() {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&main_thread_checker_);
|
2016-10-31 08:18:50 -07:00
|
|
|
if (!recording_) {
|
|
|
|
|
return;
|
2016-07-14 05:54:19 -07:00
|
|
|
}
|
2016-10-31 08:18:50 -07:00
|
|
|
LOG(INFO) << __FUNCTION__;
|
|
|
|
|
recording_ = false;
|
|
|
|
|
// Stop periodic logging if no more media is active.
|
|
|
|
|
if (!playing_) {
|
|
|
|
|
StopPeriodicLogging();
|
|
|
|
|
}
|
|
|
|
|
// Add UMA histogram to keep track of the case when only zeros have been
|
|
|
|
|
// recorded. Measurements (max of absolute level) are taken twice per second,
|
|
|
|
|
// which means that if e.g 10 seconds of audio has been recorded, a total of
|
|
|
|
|
// 20 level estimates must all be identical to zero to trigger the histogram.
|
|
|
|
|
// |only_silence_recorded_| can only be cleared on the native audio thread
|
|
|
|
|
// that drives audio capture but we know by design that the audio has stopped
|
|
|
|
|
// when this method is called, hence there should not be aby conflicts. Also,
|
|
|
|
|
// the fact that |only_silence_recorded_| can be affected during the complete
|
|
|
|
|
// call makes chances of conflicts with potentially one last callback very
|
|
|
|
|
// small.
|
|
|
|
|
const size_t time_since_start = rtc::TimeSince(rec_start_time_);
|
|
|
|
|
if (time_since_start > kMinValidCallTimeTimeInMilliseconds) {
|
|
|
|
|
const int only_zeros = static_cast<int>(only_silence_recorded_);
|
|
|
|
|
RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", only_zeros);
|
|
|
|
|
LOG(INFO) << "HISTOGRAM(WebRTC.Audio.RecordedOnlyZeros): " << only_zeros;
|
|
|
|
|
}
|
|
|
|
|
LOG(INFO) << "total recording time: " << time_since_start;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
|
2016-07-06 00:33:57 -07:00
|
|
|
LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")";
|
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-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
|
2016-07-06 00:33:57 -07:00
|
|
|
LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")";
|
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-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
|
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-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
|
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-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
|
2016-08-22 05:56:12 -07:00
|
|
|
LOG(INFO) << "SetRecordingChannels(" << channels << ")";
|
|
|
|
|
rec_channels_ = 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::SetPlayoutChannels(size_t channels) {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
|
2016-08-22 05:56:12 -07:00
|
|
|
LOG(INFO) << "SetPlayoutChannels(" << channels << ")";
|
|
|
|
|
play_channels_ = 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-10-18 05:14:30 -07:00
|
|
|
LOG(INFO) << "SetRecordingChannel(" << channel << ")";
|
|
|
|
|
LOG(LS_WARNING) << "Not implemented";
|
|
|
|
|
// Add DCHECK to ensure that user does not try to use this API with a non-
|
|
|
|
|
// default parameter.
|
|
|
|
|
RTC_DCHECK_EQ(channel, AudioDeviceModule::kChannelBoth);
|
|
|
|
|
return -1;
|
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-10-18 05:14:30 -07:00
|
|
|
LOG(LS_WARNING) << "Not implemented";
|
|
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-04 13:01:19 +02:00
|
|
|
size_t AudioDeviceBuffer::RecordingChannels() const {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
|
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-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK(main_thread_checker_.CalledOnValidThread());
|
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-11-07 15:56:59 +01:00
|
|
|
#if !defined(WEBRTC_WIN)
|
|
|
|
|
// Windows uses a dedicated thread for volume APIs.
|
|
|
|
|
RTC_DCHECK_RUN_ON(&recording_thread_checker_);
|
|
|
|
|
#endif
|
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) {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&recording_thread_checker_);
|
2016-08-22 05:56:12 -07:00
|
|
|
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-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&recording_thread_checker_);
|
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) {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&recording_thread_checker_);
|
2016-08-22 05:56:12 -07:00
|
|
|
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,
|
2016-11-10 00:40:37 -08:00
|
|
|
size_t samples_per_channel) {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&recording_thread_checker_);
|
2016-10-18 05:14:30 -07:00
|
|
|
// Copy the complete input buffer to the local buffer.
|
|
|
|
|
const size_t old_size = rec_buffer_.size();
|
2016-11-10 00:40:37 -08:00
|
|
|
rec_buffer_.SetData(static_cast<const int16_t*>(audio_buffer),
|
|
|
|
|
rec_channels_ * samples_per_channel);
|
2016-10-18 05:14:30 -07:00
|
|
|
// Keep track of the size of the recording buffer. Only updated when the
|
|
|
|
|
// size changes, which is a rare event.
|
|
|
|
|
if (old_size != rec_buffer_.size()) {
|
|
|
|
|
LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size();
|
2016-07-04 13:01:19 +02:00
|
|
|
}
|
2016-11-10 00:40:37 -08:00
|
|
|
|
2016-10-31 08:18:50 -07:00
|
|
|
// Derive a new level value twice per second and check if it is non-zero.
|
2016-10-21 12:45:25 +02:00
|
|
|
int16_t max_abs = 0;
|
|
|
|
|
RTC_DCHECK_LT(rec_stat_count_, 50);
|
|
|
|
|
if (++rec_stat_count_ >= 50) {
|
|
|
|
|
// Returns the largest absolute value in a signed 16-bit vector.
|
2016-11-10 00:40:37 -08:00
|
|
|
max_abs = WebRtcSpl_MaxAbsValueW16(rec_buffer_.data(), rec_buffer_.size());
|
2016-10-21 12:45:25 +02:00
|
|
|
rec_stat_count_ = 0;
|
2016-10-31 08:18:50 -07:00
|
|
|
// Set |only_silence_recorded_| to false as soon as at least one detection
|
|
|
|
|
// of a non-zero audio packet is found. It can only be restored to true
|
|
|
|
|
// again by restarting the call.
|
|
|
|
|
if (max_abs > 0) {
|
|
|
|
|
only_silence_recorded_ = false;
|
|
|
|
|
}
|
2016-10-21 12:45:25 +02:00
|
|
|
}
|
2016-07-14 05:54:19 -07:00
|
|
|
// Update some stats but do it on the task queue to ensure that the members
|
2016-10-21 12:45:25 +02:00
|
|
|
// are modified and read on the same thread. Note that |max_abs| will be
|
|
|
|
|
// zero in most calls and then have no effect of the stats. It is only updated
|
|
|
|
|
// approximately two times per second and can then change the stats.
|
2016-11-10 00:40:37 -08:00
|
|
|
task_queue_.PostTask([this, max_abs, samples_per_channel] {
|
|
|
|
|
UpdateRecStats(max_abs, samples_per_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
|
|
|
int32_t AudioDeviceBuffer::DeliverRecordedData() {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&recording_thread_checker_);
|
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-11-10 00:40:37 -08:00
|
|
|
const size_t frames = rec_buffer_.size() / rec_channels_;
|
|
|
|
|
const size_t bytes_per_frame = rec_channels_ * sizeof(int16_t);
|
2016-10-18 05:14:30 -07:00
|
|
|
uint32_t new_mic_level(0);
|
|
|
|
|
uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_;
|
|
|
|
|
int32_t res = audio_transport_cb_->RecordedDataIsAvailable(
|
2016-11-10 00:40:37 -08:00
|
|
|
rec_buffer_.data(), frames, bytes_per_frame, rec_channels_,
|
2016-10-18 05:14:30 -07:00
|
|
|
rec_sample_rate_, total_delay_ms, clock_drift_, current_mic_level_,
|
|
|
|
|
typing_status_, new_mic_level);
|
2016-07-04 13:01:19 +02:00
|
|
|
if (res != -1) {
|
2016-10-18 05:14:30 -07:00
|
|
|
new_mic_level_ = new_mic_level;
|
2016-08-22 05:56:12 -07:00
|
|
|
} 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-11-10 00:40:37 -08:00
|
|
|
int32_t AudioDeviceBuffer::RequestPlayoutData(size_t samples_per_channel) {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&playout_thread_checker_);
|
2016-11-10 00:40:37 -08:00
|
|
|
// The consumer can change the requested size on the fly and we therefore
|
2016-10-18 05:14:30 -07:00
|
|
|
// resize the buffer accordingly. Also takes place at the first call to this
|
|
|
|
|
// method.
|
2016-11-10 00:40:37 -08:00
|
|
|
const size_t total_samples = play_channels_ * samples_per_channel;
|
|
|
|
|
if (play_buffer_.size() != total_samples) {
|
|
|
|
|
play_buffer_.SetSize(total_samples);
|
2016-10-18 05:14:30 -07:00
|
|
|
LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size();
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2016-08-22 05:56:12 -07:00
|
|
|
size_t num_samples_out(0);
|
2016-11-07 15:56:59 +01:00
|
|
|
// It is currently supported to start playout without a valid audio
|
|
|
|
|
// transport object. Leads to warning and silence.
|
|
|
|
|
if (!audio_transport_cb_) {
|
|
|
|
|
LOG(LS_WARNING) << "Invalid audio transport";
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-10-31 08:18:50 -07:00
|
|
|
|
2016-11-07 15:56:59 +01:00
|
|
|
// Retrieve new 16-bit PCM audio data using the audio transport instance.
|
|
|
|
|
int64_t elapsed_time_ms = -1;
|
|
|
|
|
int64_t ntp_time_ms = -1;
|
2016-11-10 00:40:37 -08:00
|
|
|
const size_t bytes_per_frame = play_channels_ * sizeof(int16_t);
|
2016-11-07 15:56:59 +01:00
|
|
|
uint32_t res = audio_transport_cb_->NeedMorePlayData(
|
2016-11-10 00:40:37 -08:00
|
|
|
samples_per_channel, bytes_per_frame, play_channels_, play_sample_rate_,
|
2016-11-07 15:56:59 +01:00
|
|
|
play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms);
|
|
|
|
|
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-10-21 12:45:25 +02:00
|
|
|
// Derive a new level value twice per second.
|
|
|
|
|
int16_t max_abs = 0;
|
|
|
|
|
RTC_DCHECK_LT(play_stat_count_, 50);
|
|
|
|
|
if (++play_stat_count_ >= 50) {
|
|
|
|
|
// Returns the largest absolute value in a signed 16-bit vector.
|
2016-11-10 00:40:37 -08:00
|
|
|
max_abs =
|
|
|
|
|
WebRtcSpl_MaxAbsValueW16(play_buffer_.data(), play_buffer_.size());
|
2016-10-21 12:45:25 +02:00
|
|
|
play_stat_count_ = 0;
|
|
|
|
|
}
|
|
|
|
|
// Update some stats but do it on the task queue to ensure that the members
|
|
|
|
|
// are modified and read on the same thread. Note that |max_abs| will be
|
|
|
|
|
// zero in most calls and then have no effect of the stats. It is only updated
|
|
|
|
|
// approximately two times per second and can then change the stats.
|
2016-10-31 08:18:50 -07:00
|
|
|
task_queue_.PostTask([this, max_abs, num_samples_out] {
|
|
|
|
|
UpdatePlayStats(max_abs, num_samples_out);
|
|
|
|
|
});
|
2016-08-22 05:56:12 -07:00
|
|
|
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-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&playout_thread_checker_);
|
2016-10-18 05:14:30 -07:00
|
|
|
RTC_DCHECK_GT(play_buffer_.size(), 0u);
|
2016-11-10 00:40:37 -08:00
|
|
|
const size_t bytes_per_sample = sizeof(int16_t);
|
|
|
|
|
memcpy(audio_buffer, play_buffer_.data(),
|
|
|
|
|
play_buffer_.size() * bytes_per_sample);
|
|
|
|
|
// Return samples per channel or number of frames.
|
|
|
|
|
return static_cast<int32_t>(play_buffer_.size() / play_channels_);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-31 08:18:50 -07:00
|
|
|
void AudioDeviceBuffer::StartPeriodicLogging() {
|
|
|
|
|
task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
|
|
|
|
|
AudioDeviceBuffer::LOG_START));
|
2016-07-14 05:54:19 -07:00
|
|
|
}
|
|
|
|
|
|
2016-10-31 08:18:50 -07:00
|
|
|
void AudioDeviceBuffer::StopPeriodicLogging() {
|
|
|
|
|
task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
|
|
|
|
|
AudioDeviceBuffer::LOG_STOP));
|
|
|
|
|
}
|
2016-07-14 05:54:19 -07:00
|
|
|
|
2016-10-31 08:18:50 -07:00
|
|
|
void AudioDeviceBuffer::LogStats(LogState state) {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&task_queue_);
|
2016-07-14 05:54:19 -07:00
|
|
|
int64_t now_time = rtc::TimeMillis();
|
2016-10-31 08:18:50 -07:00
|
|
|
if (state == AudioDeviceBuffer::LOG_START) {
|
|
|
|
|
// Reset counters at start. We will not add any logging in this state but
|
|
|
|
|
// the timer will started by posting a new (delayed) task.
|
|
|
|
|
num_stat_reports_ = 0;
|
|
|
|
|
last_timer_task_time_ = now_time;
|
|
|
|
|
} else if (state == AudioDeviceBuffer::LOG_STOP) {
|
|
|
|
|
// Stop logging and posting new tasks.
|
|
|
|
|
return;
|
|
|
|
|
} else if (state == AudioDeviceBuffer::LOG_ACTIVE) {
|
|
|
|
|
// Default state. Just keep on logging.
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-14 05:54:19 -07:00
|
|
|
int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds;
|
2016-10-31 08:18:50 -07:00
|
|
|
int64_t time_since_last = rtc::TimeDiff(now_time, last_timer_task_time_);
|
|
|
|
|
last_timer_task_time_ = now_time;
|
2016-07-14 05:54:19 -07:00
|
|
|
|
2016-10-31 08:18:50 -07:00
|
|
|
// Log the latest statistics but skip the first round just after state was
|
|
|
|
|
// set to LOG_START. Hence, first printed log will be after ~10 seconds.
|
2016-09-20 04:44:04 -07:00
|
|
|
if (++num_stat_reports_ > 1 && time_since_last > 0) {
|
2016-07-14 05:54:19 -07:00
|
|
|
uint32_t diff_samples = rec_samples_ - last_rec_samples_;
|
2016-09-20 04:44:04 -07:00
|
|
|
float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
|
2016-07-14 05:54:19 -07:00
|
|
|
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 << ", "
|
2016-09-20 04:44:04 -07:00
|
|
|
<< "rate: " << static_cast<int>(rate + 0.5) << ", "
|
2016-09-09 14:23:11 +02:00
|
|
|
<< "level: " << max_rec_level_;
|
2016-07-14 05:54:19 -07:00
|
|
|
|
|
|
|
|
diff_samples = play_samples_ - last_play_samples_;
|
2016-09-20 04:44:04 -07:00
|
|
|
rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
|
2016-07-14 05:54:19 -07:00
|
|
|
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 << ", "
|
2016-09-20 04:44:04 -07:00
|
|
|
<< "rate: " << static_cast<int>(rate + 0.5) << ", "
|
2016-09-09 14:23:11 +02:00
|
|
|
<< "level: " << max_play_level_;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-14 05:54:19 -07:00
|
|
|
last_rec_callbacks_ = rec_callbacks_;
|
|
|
|
|
last_play_callbacks_ = play_callbacks_;
|
|
|
|
|
last_rec_samples_ = rec_samples_;
|
|
|
|
|
last_play_samples_ = play_samples_;
|
2016-09-09 14:23:11 +02:00
|
|
|
max_rec_level_ = 0;
|
|
|
|
|
max_play_level_ = 0;
|
2016-07-14 05:54:19 -07:00
|
|
|
|
|
|
|
|
int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis();
|
|
|
|
|
RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval";
|
|
|
|
|
|
2016-10-31 08:18:50 -07:00
|
|
|
// Keep posting new (delayed) tasks until state is changed to kLogStop.
|
|
|
|
|
task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
|
|
|
|
|
AudioDeviceBuffer::LOG_ACTIVE),
|
2016-07-14 05:54:19 -07:00
|
|
|
time_to_wait_ms);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-09 14:23:11 +02:00
|
|
|
void AudioDeviceBuffer::ResetRecStats() {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&task_queue_);
|
2016-09-09 14:23:11 +02:00
|
|
|
rec_callbacks_ = 0;
|
|
|
|
|
last_rec_callbacks_ = 0;
|
|
|
|
|
rec_samples_ = 0;
|
|
|
|
|
last_rec_samples_ = 0;
|
|
|
|
|
max_rec_level_ = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AudioDeviceBuffer::ResetPlayStats() {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&task_queue_);
|
2016-09-09 14:23:11 +02:00
|
|
|
play_callbacks_ = 0;
|
|
|
|
|
last_play_callbacks_ = 0;
|
|
|
|
|
play_samples_ = 0;
|
|
|
|
|
last_play_samples_ = 0;
|
|
|
|
|
max_play_level_ = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 00:40:37 -08:00
|
|
|
void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs,
|
|
|
|
|
size_t samples_per_channel) {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&task_queue_);
|
2016-07-14 05:54:19 -07:00
|
|
|
++rec_callbacks_;
|
2016-11-10 00:40:37 -08:00
|
|
|
rec_samples_ += samples_per_channel;
|
2016-10-21 12:45:25 +02:00
|
|
|
if (max_abs > max_rec_level_) {
|
|
|
|
|
max_rec_level_ = max_abs;
|
2016-09-09 14:23:11 +02:00
|
|
|
}
|
2016-07-14 05:54:19 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-10 00:40:37 -08:00
|
|
|
void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs,
|
|
|
|
|
size_t samples_per_channel) {
|
2016-11-07 15:56:59 +01:00
|
|
|
RTC_DCHECK_RUN_ON(&task_queue_);
|
2016-07-14 05:54:19 -07:00
|
|
|
++play_callbacks_;
|
2016-11-10 00:40:37 -08:00
|
|
|
play_samples_ += samples_per_channel;
|
2016-10-21 12:45:25 +02:00
|
|
|
if (max_abs > max_play_level_) {
|
|
|
|
|
max_play_level_ = max_abs;
|
2016-09-09 14:23:11 +02:00
|
|
|
}
|
2016-07-14 05:54:19 -07:00
|
|
|
}
|
|
|
|
|
|
2011-07-07 08:21:25 +00:00
|
|
|
} // namespace webrtc
|