| 1 | /* |
| 2 | * Copyright 2025 LiveKit, Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "livekit/audio_track.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <iostream> |
| 21 | #include <iterator> |
| 22 | #include <memory> |
| 23 | |
| 24 | #include "api/audio_options.h" |
| 25 | #include "api/audio/audio_frame.h" |
| 26 | #include "api/media_stream_interface.h" |
| 27 | #include "api/task_queue/task_queue_base.h" |
| 28 | #include "audio/remix_resample.h" |
| 29 | #include "common_audio/include/audio_util.h" |
| 30 | #include "livekit/global_task_queue.h" |
| 31 | #include "rtc_base/checks.h" |
| 32 | #include "rtc_base/logging.h" |
| 33 | #include "rtc_base/ref_counted_object.h" |
| 34 | #include "rtc_base/synchronization/mutex.h" |
| 35 | #include "rust/cxx.h" |
| 36 | #include "webrtc-sys/src/audio_track.rs.h" |
| 37 | |
| 38 | namespace livekit_ffi { |
| 39 | |
| 40 | inline webrtc::AudioOptions to_native_audio_options( |
| 41 | const AudioSourceOptions& options) { |
| 42 | webrtc::AudioOptions rtc_options{}; |
| 43 | rtc_options.echo_cancellation = options.echo_cancellation; |
| 44 | rtc_options.noise_suppression = options.noise_suppression; |
| 45 | rtc_options.auto_gain_control = options.auto_gain_control; |
| 46 | return rtc_options; |
| 47 | } |
| 48 | |
| 49 | inline AudioSourceOptions to_rust_audio_options( |
| 50 | const webrtc::AudioOptions& rtc_options) { |
| 51 | AudioSourceOptions options{}; |
| 52 | options.echo_cancellation = rtc_options.echo_cancellation.value_or(false); |
| 53 | options.noise_suppression = rtc_options.noise_suppression.value_or(false); |
| 54 | options.auto_gain_control = rtc_options.auto_gain_control.value_or(false); |
| 55 | return options; |
| 56 | } |
| 57 | |
| 58 | AudioTrack::AudioTrack(std::shared_ptr<RtcRuntime> rtc_runtime, |
| 59 | webrtc::scoped_refptr<webrtc::AudioTrackInterface> track) |
| 60 | : MediaStreamTrack(rtc_runtime, std::move(track)) {} |
| 61 | |
| 62 | AudioTrack::~AudioTrack() { |
| 63 | webrtc::MutexLock lock(&mutex_); |
| 64 | for (auto& sink : sinks_) { |
| 65 | track()->RemoveSink(sink.get()); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void AudioTrack::add_sink(const std::shared_ptr<NativeAudioSink>& sink) const { |
| 70 | webrtc::MutexLock lock(&mutex_); |
| 71 | track()->AddSink(sink.get()); |
| 72 | sinks_.push_back(sink); |
| 73 | } |
| 74 | |
| 75 | void AudioTrack::remove_sink( |
| 76 | const std::shared_ptr<NativeAudioSink>& sink) const { |
| 77 | webrtc::MutexLock lock(&mutex_); |
| 78 | track()->RemoveSink(sink.get()); |
| 79 | sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end()); |
| 80 | } |
| 81 | |
| 82 | NativeAudioSink::NativeAudioSink(rust::Box<AudioSinkWrapper> observer, |
| 83 | int sample_rate, |
| 84 | int num_channels) |
| 85 | : observer_(std::move(observer)), |
| 86 | sample_rate_(sample_rate), |
| 87 | num_channels_(num_channels) { |
| 88 | frame_.sample_rate_hz_ = sample_rate; |
| 89 | frame_.num_channels_ = num_channels; |
| 90 | frame_.samples_per_channel_ = webrtc::SampleRateToDefaultChannelSize(sample_rate); |
| 91 | } |
| 92 | |
| 93 | void NativeAudioSink::OnData(const void* audio_data, |
| 94 | int bits_per_sample, |
| 95 | int sample_rate, |
| 96 | size_t number_of_channels, |
| 97 | size_t number_of_frames) { |
| 98 | RTC_CHECK_EQ(16, bits_per_sample); |
| 99 | |
| 100 | const int16_t* data = static_cast<const int16_t*>(audio_data); |
| 101 | |
| 102 | if (sample_rate_ != sample_rate || num_channels_ != number_of_channels) { |
| 103 | webrtc::InterleavedView<const int16_t> source(data, |
| 104 | number_of_frames, |
| 105 | number_of_channels); |
| 106 | // resample/remix before capturing |
| 107 | webrtc::voe::RemixAndResample(source, sample_rate, &resampler_, &frame_); |
| 108 | |
| 109 | rust::Slice<const int16_t> rust_slice( |
| 110 | frame_.data(), frame_.num_channels() * frame_.samples_per_channel()); |
| 111 | |
| 112 | observer_->on_data(rust_slice, frame_.sample_rate_hz(), |
| 113 | frame_.num_channels(), frame_.samples_per_channel()); |
| 114 | |
| 115 | } else { |
| 116 | rust::Slice<const int16_t> rust_slice( |
| 117 | data, number_of_channels * number_of_frames); |
| 118 | |
| 119 | observer_->on_data(rust_slice, sample_rate, number_of_channels, |
| 120 | number_of_frames); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | std::shared_ptr<NativeAudioSink> new_native_audio_sink( |
| 125 | rust::Box<AudioSinkWrapper> observer, |
| 126 | int sample_rate, |
| 127 | int num_channels) { |
| 128 | return std::make_shared<NativeAudioSink>(std::move(observer), sample_rate, |
| 129 | num_channels); |
| 130 | } |
| 131 | |
| 132 | AudioTrackSource::InternalSource::InternalSource( |
| 133 | const webrtc::AudioOptions& options, |
| 134 | int sample_rate, |
| 135 | int num_channels, |
| 136 | int queue_size_ms, // must be a multiple of 10ms |
| 137 | webrtc::TaskQueueFactory* task_queue_factory) |
| 138 | : options_(options), |
| 139 | sample_rate_(sample_rate), |
| 140 | num_channels_(num_channels), |
| 141 | capture_userdata_(nullptr), |
| 142 | on_complete_(nullptr) { |
| 143 | if (!queue_size_ms) { |
| 144 | // Set queue_size_samples_ to 0 so that capture_frame() will get to the fast path. |
| 145 | queue_size_samples_ = 0; |
| 146 | return; // no audio queue |
| 147 | } |
| 148 | |
| 149 | int samples10ms = sample_rate / 100 * num_channels; |
| 150 | |
| 151 | silence_buffer_.assign(samples10ms, 0); |
| 152 | queue_size_samples_ = queue_size_ms / 10 * samples10ms; |
| 153 | notify_threshold_samples_ = queue_size_samples_; // TODO: this is currently |
| 154 | // using x2 the queue size |
| 155 | buffer_.reserve(queue_size_samples_ + notify_threshold_samples_); |
| 156 | |
| 157 | audio_queue_ = |
| 158 | task_queue_factory->CreateTaskQueue( |
| 159 | "AudioSourceCapture", webrtc::TaskQueueFactory::Priority::NORMAL); |
| 160 | |
| 161 | audio_task_ = webrtc::RepeatingTaskHandle::Start( |
| 162 | audio_queue_.get(), |
| 163 | [this, samples10ms]() { |
| 164 | webrtc::MutexLock lock(&mutex_); |
| 165 | constexpr int kBitsPerSample = sizeof(int16_t) * 8; |
| 166 | |
| 167 | if (buffer_.size() >= samples10ms) { |
| 168 | for (auto sink : sinks_) |
| 169 | sink->OnData(buffer_.data(), kBitsPerSample, sample_rate_, |
| 170 | num_channels_, samples10ms / num_channels_); |
| 171 | |
| 172 | buffer_.erase(buffer_.begin(), buffer_.begin() + samples10ms); |
| 173 | } else { |
| 174 | // Always provide a 10ms frame to avoid playout underruns. |
| 175 | for (auto sink : sinks_) |
| 176 | sink->OnData(silence_buffer_.data(), kBitsPerSample, sample_rate_, |
| 177 | num_channels_, samples10ms / num_channels_); |
| 178 | } |
| 179 | |
| 180 | if (on_complete_ && buffer_.size() <= notify_threshold_samples_) { |
| 181 | on_complete_(capture_userdata_); |
| 182 | on_complete_ = nullptr; |
| 183 | capture_userdata_ = nullptr; |
| 184 | } |
| 185 | |
| 186 | return webrtc::TimeDelta::Millis(10); |
| 187 | }, |
| 188 | webrtc::TaskQueueBase::DelayPrecision::kHigh); |
| 189 | } |
| 190 | |
| 191 | AudioTrackSource::InternalSource::~InternalSource() { |
| 192 | } |
| 193 | |
| 194 | bool AudioTrackSource::InternalSource::capture_frame( |
| 195 | rust::Slice<const int16_t> data, |
| 196 | uint32_t sample_rate, |
| 197 | uint32_t number_of_channels, |
| 198 | size_t number_of_frames, |
| 199 | const SourceContext* ctx, |
| 200 | void (*on_complete)(const SourceContext*)) { |
| 201 | webrtc::MutexLock lock(&mutex_); |
| 202 | |
| 203 | if (queue_size_samples_) { |
| 204 | int available = |
| 205 | (queue_size_samples_ + notify_threshold_samples_) - buffer_.size(); |
| 206 | if (available < data.size()) |
| 207 | return false; |
| 208 | |
| 209 | if (on_complete_ || capture_userdata_) |
| 210 | return false; |
| 211 | |
| 212 | buffer_.insert(buffer_.end(), data.begin(), data.end()); |
| 213 | |
| 214 | if (buffer_.size() <= notify_threshold_samples_) { |
| 215 | on_complete(ctx); // complete directly |
| 216 | } else { |
| 217 | on_complete_ = on_complete; |
| 218 | capture_userdata_ = ctx; |
| 219 | } |
| 220 | |
| 221 | } else { |
| 222 | // Fast path: capture directly when the queue buffer is 0 (frame size must be 10ms) |
| 223 | for (auto sink : sinks_) |
| 224 | sink->OnData(data.data(), sizeof(int16_t) * 8, sample_rate, |
| 225 | number_of_channels, number_of_frames); |
| 226 | } |
| 227 | |
| 228 | return true; |
| 229 | } |
| 230 | |
| 231 | void AudioTrackSource::InternalSource::clear_buffer() { |
| 232 | webrtc::MutexLock lock(&mutex_); |
| 233 | buffer_.clear(); |
| 234 | } |
| 235 | |
| 236 | webrtc::MediaSourceInterface::SourceState |
| 237 | AudioTrackSource::InternalSource::state() const { |
| 238 | return webrtc::MediaSourceInterface::SourceState::kLive; |
| 239 | } |
| 240 | |
| 241 | bool AudioTrackSource::InternalSource::remote() const { |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | const webrtc::AudioOptions AudioTrackSource::InternalSource::options() const { |
| 246 | webrtc::MutexLock lock(&mutex_); |
| 247 | return options_; |
| 248 | } |
| 249 | |
| 250 | void AudioTrackSource::InternalSource::set_options( |
| 251 | const webrtc::AudioOptions& options) { |
| 252 | webrtc::MutexLock lock(&mutex_); |
| 253 | options_ = options; |
| 254 | } |
| 255 | |
| 256 | void AudioTrackSource::InternalSource::AddSink( |
| 257 | webrtc::AudioTrackSinkInterface* sink) { |
| 258 | webrtc::MutexLock lock(&mutex_); |
| 259 | sinks_.push_back(sink); |
| 260 | } |
| 261 | |
| 262 | void AudioTrackSource::InternalSource::RemoveSink( |
| 263 | webrtc::AudioTrackSinkInterface* sink) { |
| 264 | webrtc::MutexLock lock(&mutex_); |
| 265 | sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end()); |
| 266 | } |
| 267 | |
| 268 | AudioTrackSource::AudioTrackSource(AudioSourceOptions options, |
| 269 | int sample_rate, |
| 270 | int num_channels, |
| 271 | int queue_size_ms, |
| 272 | webrtc::TaskQueueFactory* task_queue_factory) |
| 273 | : source_(webrtc::make_ref_counted<InternalSource>( |
| 274 | to_native_audio_options(options), |
| 275 | sample_rate, |
| 276 | num_channels, |
| 277 | queue_size_ms, |
| 278 | task_queue_factory)) {} |
| 279 | |
| 280 | AudioSourceOptions AudioTrackSource::audio_options() const { |
| 281 | return to_rust_audio_options(source_->options()); |
| 282 | } |
| 283 | |
| 284 | void AudioTrackSource::set_audio_options( |
| 285 | const AudioSourceOptions& options) const { |
| 286 | source_->set_options(to_native_audio_options(options)); |
| 287 | } |
| 288 | |
| 289 | bool AudioTrackSource::capture_frame( |
| 290 | rust::Slice<const int16_t> audio_data, |
| 291 | uint32_t sample_rate, |
| 292 | uint32_t number_of_channels, |
| 293 | size_t number_of_frames, |
| 294 | const SourceContext* ctx, |
| 295 | void (*on_complete)(const SourceContext*)) const { |
| 296 | return source_->capture_frame(audio_data, sample_rate, number_of_channels, |
| 297 | number_of_frames, ctx, on_complete); |
| 298 | } |
| 299 | |
| 300 | void AudioTrackSource::clear_buffer() const { |
| 301 | source_->clear_buffer(); |
| 302 | } |
| 303 | |
| 304 | std::shared_ptr<AudioTrackSource> new_audio_track_source( |
| 305 | AudioSourceOptions options, |
| 306 | int sample_rate, |
| 307 | int num_channels, |
| 308 | int queue_size_ms) { |
| 309 | return std::make_shared<AudioTrackSource>(options, sample_rate, num_channels, |
| 310 | queue_size_ms, |
| 311 | GetGlobalTaskQueueFactory()); |
| 312 | } |
| 313 | |
| 314 | webrtc::scoped_refptr<AudioTrackSource::InternalSource> AudioTrackSource::get() |
| 315 | const { |
| 316 | return source_; |
| 317 | } |
| 318 | |
| 319 | } // namespace livekit_ffi |