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