| 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 | #pragma once |
| 18 | |
| 19 | #include <memory> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "api/audio/audio_frame.h" |
| 23 | #include "api/audio_options.h" |
| 24 | #include "api/task_queue/task_queue_factory.h" |
| 25 | #include "common_audio/resampler/include/push_resampler.h" |
| 26 | #include "livekit/helper.h" |
| 27 | #include "livekit/media_stream_track.h" |
| 28 | #include "livekit/webrtc.h" |
| 29 | #include "pc/local_audio_source.h" |
| 30 | #include "rtc_base/synchronization/mutex.h" |
| 31 | #include "api/task_queue/task_queue_base.h" |
| 32 | #include "rtc_base/task_utils/repeating_task.h" |
| 33 | #include "rtc_base/thread_annotations.h" |
| 34 | #include "rust/cxx.h" |
| 35 | |
| 36 | namespace livekit_ffi { |
| 37 | class AudioTrack; |
| 38 | class NativeAudioSink; |
| 39 | class AudioTrackSource; |
| 40 | class SourceContext; |
| 41 | |
| 42 | using CompleteCallback = void (*)(const livekit_ffi::SourceContext*); |
| 43 | } // namespace livekit_ffi |
| 44 | #include "webrtc-sys/src/audio_track.rs.h" |
| 45 | |
| 46 | namespace livekit_ffi { |
| 47 | |
| 48 | class AudioTrack : public MediaStreamTrack { |
| 49 | private: |
| 50 | friend RtcRuntime; |
| 51 | AudioTrack(std::shared_ptr<RtcRuntime> rtc_runtime, |
| 52 | webrtc::scoped_refptr<webrtc::AudioTrackInterface> track); |
| 53 | |
| 54 | public: |
| 55 | ~AudioTrack(); |
| 56 | |
| 57 | void add_sink(const std::shared_ptr<NativeAudioSink>& sink) const; |
| 58 | void remove_sink(const std::shared_ptr<NativeAudioSink>& sink) const; |
| 59 | |
| 60 | private: |
| 61 | webrtc::AudioTrackInterface* track() const { |
| 62 | return static_cast<webrtc::AudioTrackInterface*>(track_.get()); |
| 63 | } |
| 64 | |
| 65 | mutable webrtc::Mutex mutex_; |
| 66 | |
| 67 | // Same for VideoTrack: |
| 68 | // Keep a strong reference to the added sinks, so we don't need to |
| 69 | // manage the lifetime safety on the Rust side |
| 70 | mutable std::vector<std::shared_ptr<NativeAudioSink>> sinks_; |
| 71 | }; |
| 72 | |
| 73 | class NativeAudioSink : public webrtc::AudioTrackSinkInterface { |
| 74 | public: |
| 75 | explicit NativeAudioSink(rust::Box<AudioSinkWrapper> observer, |
| 76 | int sample_rate, |
| 77 | int num_channels); |
| 78 | void OnData(const void* audio_data, |
| 79 | int bits_per_sample, |
| 80 | int sample_rate, |
| 81 | size_t number_of_channels, |
| 82 | size_t number_of_frames) override; |
| 83 | |
| 84 | private: |
| 85 | rust::Box<AudioSinkWrapper> observer_; |
| 86 | |
| 87 | int sample_rate_; |
| 88 | int num_channels_; |
| 89 | |
| 90 | webrtc::AudioFrame frame_; |
| 91 | webrtc::PushResampler<int16_t> resampler_; |
| 92 | }; |
| 93 | |
| 94 | std::shared_ptr<NativeAudioSink> new_native_audio_sink( |
| 95 | rust::Box<AudioSinkWrapper> observer, |
| 96 | int sample_rate, |
| 97 | int num_channels); |
| 98 | |
| 99 | class AudioTrackSource { |
| 100 | class InternalSource : public webrtc::LocalAudioSource { |
| 101 | public: |
| 102 | InternalSource(const webrtc::AudioOptions& options, |
| 103 | int sample_rate, |
| 104 | int num_channels, |
| 105 | int buffer_size_ms, |
| 106 | webrtc::TaskQueueFactory* task_queue_factory); |
| 107 | |
| 108 | ~InternalSource() override; |
| 109 | |
| 110 | SourceState state() const override; |
| 111 | bool remote() const override; |
| 112 | |
| 113 | const webrtc::AudioOptions options() const override; |
| 114 | |
| 115 | void AddSink(webrtc::AudioTrackSinkInterface* sink) override; |
| 116 | void RemoveSink(webrtc::AudioTrackSinkInterface* sink) override; |
| 117 | |
| 118 | void set_options(const webrtc::AudioOptions& options); |
| 119 | |
| 120 | bool capture_frame(rust::Slice<const int16_t> audio_data, |
| 121 | uint32_t sample_rate, |
| 122 | uint32_t number_of_channels, |
| 123 | size_t number_of_frames, |
| 124 | const SourceContext* ctx, |
| 125 | void (*on_complete)(const SourceContext*)); |
| 126 | |
| 127 | void clear_buffer(); |
| 128 | |
| 129 | // Indicate this is an external audio source (when external_audio_source.patch is applied). |
| 130 | // This prevents AudioState from sending device audio to streams using this source. |
| 131 | // Note: Omit 'override' to allow builds without the patch applied. |
| 132 | // When the patch is applied, this will correctly override the base class virtual method. |
| 133 | bool is_external_source() const { return true; } |
| 134 | |
| 135 | private: |
| 136 | mutable webrtc::Mutex mutex_; |
| 137 | std::unique_ptr<webrtc::TaskQueueBase, webrtc::TaskQueueDeleter> audio_queue_; |
| 138 | webrtc::RepeatingTaskHandle audio_task_; |
| 139 | |
| 140 | std::vector<webrtc::AudioTrackSinkInterface*> sinks_ RTC_GUARDED_BY(mutex_); |
| 141 | std::vector<int16_t> buffer_ RTC_GUARDED_BY(mutex_); |
| 142 | |
| 143 | const SourceContext* capture_userdata_ RTC_GUARDED_BY(mutex_); |
| 144 | void (*on_complete_)(const SourceContext*) RTC_GUARDED_BY(mutex_); |
| 145 | |
| 146 | std::vector<int16_t> silence_buffer_; |
| 147 | |
| 148 | int sample_rate_ = 0; |
| 149 | int num_channels_ = 0; |
| 150 | int queue_size_samples_ = 0; |
| 151 | int notify_threshold_samples_ = 0; |
| 152 | |
| 153 | webrtc::AudioOptions options_{}; |
| 154 | }; |
| 155 | |
| 156 | public: |
| 157 | AudioTrackSource(AudioSourceOptions options, |
| 158 | int sample_rate, |
| 159 | int num_channels, |
| 160 | int queue_size_ms, |
| 161 | webrtc::TaskQueueFactory* task_queue_factory); |
| 162 | |
| 163 | AudioSourceOptions audio_options() const; |
| 164 | |
| 165 | void set_audio_options(const AudioSourceOptions& options) const; |
| 166 | |
| 167 | bool capture_frame(rust::Slice<const int16_t> audio_data, |
| 168 | uint32_t sample_rate, |
| 169 | uint32_t number_of_channels, |
| 170 | size_t number_of_frames, |
| 171 | const SourceContext* ctx, |
| 172 | CompleteCallback on_complete) const; |
| 173 | |
| 174 | void clear_buffer() const; |
| 175 | |
| 176 | webrtc::scoped_refptr<InternalSource> get() const; |
| 177 | |
| 178 | private: |
| 179 | webrtc::scoped_refptr<InternalSource> source_; |
| 180 | }; |
| 181 | |
| 182 | std::shared_ptr<AudioTrackSource> new_audio_track_source( |
| 183 | AudioSourceOptions options, |
| 184 | int sample_rate, |
| 185 | int num_channels, |
| 186 | int queue_size_ms); |
| 187 | |
| 188 | static std::shared_ptr<MediaStreamTrack> audio_to_media( |
| 189 | std::shared_ptr<AudioTrack> track) { |
| 190 | return track; |
| 191 | } |
| 192 | |
| 193 | static std::shared_ptr<AudioTrack> media_to_audio( |
| 194 | std::shared_ptr<MediaStreamTrack> track) { |
| 195 | return std::static_pointer_cast<AudioTrack>(track); |
| 196 | } |
| 197 | |
| 198 | static std::shared_ptr<AudioTrack> _shared_audio_track() { |
| 199 | return nullptr; // Ignore |
| 200 | } |
| 201 | |
| 202 | static std::shared_ptr<AudioTrackSource> _shared_audio_track_source() { |
| 203 | return nullptr; // Ignore |
| 204 | } |
| 205 | |
| 206 | } // namespace livekit_ffi |