Commit aa3d5337 authored by Yuwei Huang's avatar Yuwei Huang Committed by Chromium LUCI CQ

[remoting host][mac] Add silence detection for AudioCapturerMac

This CL adds silence detection logic for AudioCapturerMac so that it
doesn't send empty audio packets and waste the bandwidth.

Bug: 1161363
Change-Id: I891355b0ca31fb12eb40feb4f9b189645d36193c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2618473Reviewed-by: default avatarJoe Downing <joedow@chromium.org>
Commit-Queue: Yuwei Huang <yuweih@chromium.org>
Cr-Commit-Position: refs/heads/master@{#841737}
parent ad4e0077
......@@ -33,6 +33,7 @@ constexpr int kBytesPerFrame = kBytesPerChannel * kChannelsPerFrame;
constexpr float kBufferTimeDurationSec = 0.01f; // 10ms
constexpr size_t kBufferByteSize =
kSampleRate * kBytesPerFrame * kBufferTimeDurationSec;
constexpr int kAudioSilenceThreshold = 0;
// Total delay: kBufferTimeDurationSec * kNumberBuffers
constexpr int kNumberBuffers = 2;
......@@ -94,7 +95,8 @@ AudioCapturerInstanceSet* AudioCapturerInstanceSet::Get() {
} // namespace
AudioCapturerMac::AudioCapturerMac(const std::string& audio_device_uid)
: audio_device_uid_(audio_device_uid) {
: audio_device_uid_(audio_device_uid),
silence_detector_(kAudioSilenceThreshold) {
DETACH_FROM_SEQUENCE(sequence_checker_);
DCHECK(!audio_device_uid.empty());
......@@ -170,8 +172,9 @@ void AudioCapturerMac::HandleInputBuffer(AudioQueueRef aq,
DCHECK_EQ(input_queue_, aq);
DCHECK(callback_);
// TODO(yuweih): Add silence detection and drop empty packets.
if (!silence_detector_.IsSilence(
reinterpret_cast<const int16_t*>(buffer->mAudioData),
buffer->mAudioDataByteSize / sizeof(int16_t) / kChannelsPerFrame)) {
auto packet = std::make_unique<AudioPacket>();
packet->add_data(buffer->mAudioData, buffer->mAudioDataByteSize);
packet->set_encoding(AudioPacket::ENCODING_RAW);
......@@ -179,6 +182,7 @@ void AudioCapturerMac::HandleInputBuffer(AudioQueueRef aq,
packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2);
packet->set_channels(AudioPacket::CHANNELS_STEREO);
callback_.Run(std::move(packet));
}
// Recycle the buffer.
// Only the first 2 params are needed for recording.
......@@ -242,6 +246,8 @@ bool AudioCapturerMac::StartInputQueue() {
}
is_started_ = true;
silence_detector_.Reset(kSampleRate, kChannelsPerFrame);
return true;
}
......
......@@ -15,6 +15,7 @@
#include "base/sequence_checker.h"
#include "base/sequenced_task_runner.h"
#include "remoting/host/audio_capturer.h"
#include "remoting/host/audio_silence_detector.h"
namespace remoting {
......@@ -57,6 +58,7 @@ class AudioCapturerMac : public AudioCapturer {
std::string audio_device_uid_;
AudioStreamBasicDescription stream_description_;
AudioSilenceDetector silence_detector_;
PacketCapturedCallback callback_;
AudioQueueRef input_queue_ = nullptr;
bool is_started_ = false;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment