Skip to content

Commit

Permalink
send silence frames on the AudioSource when the queue is empty (#433)
Browse files Browse the repository at this point in the history
* send silence frames

* Update audio_track.cpp

* Update audio_track.cpp

* directly send silence
  • Loading branch information
theomonnom authored Sep 13, 2024
1 parent 40a1f11 commit c84f3c2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions webrtc-sys/include/livekit/audio_track.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class AudioTrackSource {
int buffer_size_ms,
webrtc::TaskQueueFactory* task_queue_factory);

~InternalSource() override;

SourceState state() const override;
bool remote() const override;

Expand Down Expand Up @@ -134,6 +136,9 @@ class AudioTrackSource {
const SourceContext* capture_userdata_ RTC_GUARDED_BY(mutex_);
void (*on_complete_)(const SourceContext*) RTC_GUARDED_BY(mutex_);

int missed_frames_ RTC_GUARDED_BY(mutex_) = 0;
int16_t* silence_buffer_ = nullptr;

int sample_rate_;
int num_channels_;
int queue_size_samples_;
Expand Down
20 changes: 19 additions & 1 deletion webrtc-sys/src/audio_track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,18 @@ AudioTrackSource::InternalSource::InternalSource(
if (!queue_size_ms)
return; // no audio queue

// start sending silence when there is nothing on the queue for 10 frames
// (100ms)
const int silence_frames_threshold = 10;
missed_frames_ = silence_frames_threshold;

int samples10ms = sample_rate / 100 * num_channels;

silence_buffer_ = new int16_t[samples10ms]();
queue_size_samples_ = queue_size_ms / 10 * samples10ms;
notify_threshold_samples_ = queue_size_samples_; // TODO: this is currently
// using x2 the queue size
buffer_.resize(queue_size_samples_ + notify_threshold_samples_);
buffer_.reserve(queue_size_samples_ + notify_threshold_samples_);

audio_queue_ =
std::make_unique<rtc::TaskQueue>(task_queue_factory->CreateTaskQueue(
Expand All @@ -160,6 +167,13 @@ AudioTrackSource::InternalSource::InternalSource(
num_channels_, samples10ms / num_channels_);

buffer_.erase(buffer_.begin(), buffer_.begin() + samples10ms);
} else {
missed_frames_++;
if (missed_frames_ >= silence_frames_threshold) {
for (auto sink : sinks_)
sink->OnData(silence_buffer_, sizeof(int16_t), sample_rate_,
num_channels_, samples10ms / num_channels_);
}
}

if (on_complete_ && buffer_.size() <= notify_threshold_samples_) {
Expand All @@ -173,6 +187,10 @@ AudioTrackSource::InternalSource::InternalSource(
webrtc::TaskQueueBase::DelayPrecision::kHigh);
}

AudioTrackSource::InternalSource::~InternalSource() {
delete[] silence_buffer_;
}

bool AudioTrackSource::InternalSource::capture_frame(
rust::Slice<const int16_t> data,
uint32_t sample_rate,
Expand Down

0 comments on commit c84f3c2

Please sign in to comment.