fork/rust-sdks
git clone https://git.julia.blue/fork/rust-sdks
25f4c9a
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/media_stream_interface.h" 22#include "api/video/video_frame.h" 23#include "livekit/helper.h" 24#include "livekit/media_stream_track.h" 25#include "livekit/video_frame.h" 26#include "livekit/webrtc.h" 27#include "media/base/adapted_video_track_source.h" 28#include "rtc_base/synchronization/mutex.h" 29#include "rtc_base/timestamp_aligner.h" 30#include "rust/cxx.h" 31 32namespace livekit { 33class VideoTrack ; 34class NativeVideoSink ; 35class VideoTrackSource ; 36}// namespace livekit 37#include "webrtc-sys/src/video_track.rs.h" 38 39namespace livekit { 40 41class VideoTrack :public MediaStreamTrack { 42private : 43friend RtcRuntime ; 44VideoTrack (std ::shared_ptr < RtcRuntime > rtc_runtime , 45rtc ::scoped_refptr < webrtc ::VideoTrackInterface > track ); 46 47public : 48 ~VideoTrack (); 49 50void add_sink (const std ::shared_ptr < NativeVideoSink >& sink )const ; 51void remove_sink (const std ::shared_ptr < NativeVideoSink >& sink )const ; 52 53void set_should_receive (bool should_receive )const ; 54bool should_receive ()const ; 55ContentHint content_hint ()const ; 56void set_content_hint (ContentHint hint )const ; 57 58private : 59webrtc ::VideoTrackInterface * track ()const { 60return static_cast < webrtc ::VideoTrackInterface *> (track_ .get ()); 61 } 62 63mutable webrtc ::Mutex mutex_ ; 64 65// Same for AudioTrack: 66// Keep a strong reference to the added sinks, so we don't need to 67// manage the lifetime safety on the Rust side 68mutable std ::vector < std ::shared_ptr < NativeVideoSink >>sinks_ ; 69}; 70 71class NativeVideoSink :public rtc ::VideoSinkInterface < webrtc ::VideoFrame > { 72public : 73explicit NativeVideoSink (rust ::Box < VideoSinkWrapper > observer ); 74 75void OnFrame (const webrtc ::VideoFrame & frame )override ; 76void OnDiscardedFrame ()override ; 77void OnConstraintsChanged ( 78const webrtc ::VideoTrackSourceConstraints & constraints) override; 79 80private: 81rust::Box < VideoSinkWrapper > observer_; 82}; 83 84std::shared_ptr < NativeVideoSink > new_native_video_sink ( 85rust ::Box < VideoSinkWrapper > observer); 86 87class VideoTrackSource { 88class InternalSource : public rtc::AdaptedVideoTrackSource { 89public : 90InternalSource( const VideoResolution & 91resolution); // (0, 0) means no resolution/optional, the 92// source will guess the resolution at the 93// first captured frame 94~ InternalSource () override; 95 96bool is_screencast () const override ; 97absl ::optional < bool > needs_denoising () const override; 98SourceState state () const override ; 99bool remote () const override; 100VideoResolution video_resolution () const ; 101bool on_captured_frame ( const webrtc ::VideoFrame & frame); 102 103private : 104mutable webrtc::Mutex mutex_; 105rtc ::TimestampAligner timestamp_aligner_; 106VideoResolution resolution_; 107}; 108 109public : 110VideoTrackSource ( const VideoResolution & resolution); 111 112VideoResolution video_resolution () const; 113 114bool on_captured_frame ( const std ::unique_ptr < VideoFrame >& frame) 115const; // frames pushed from Rust (+interior mutability) 116 117rtc::scoped_refptr < InternalSource > get () const; 118 119private: 120rtc::scoped_refptr < InternalSource > source_; 121}; 122 123std ::shared_ptr < VideoTrackSource > new_video_track_source ( 124const VideoResolution & resolution); 125 126static std ::shared_ptr < MediaStreamTrack > video_to_media ( 127std ::shared_ptr < VideoTrack > track) { 128return track; 129} 130 131static std ::shared_ptr < VideoTrack > media_to_video ( 132std ::shared_ptr < MediaStreamTrack > track) { 133return std::static_pointer_cast < VideoTrack > (track); 134} 135 136static std ::shared_ptr < VideoTrack > _shared_video_track () { 137return nullptr ; // Ignore 138} 139 140} // namespace livekit