fork/rust-sdks
git clone https://git.julia.blue/fork/rust-sdks
e33c5a9
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 <stdint.h> 20 21#include <memory> 22#include <string> 23#include <vector> 24 25#include "api/crypto/frame_crypto_transformer.h" 26#include "api/scoped_refptr.h" 27#include "livekit/peer_connection.h" 28#include "livekit/peer_connection_factory.h" 29#include "livekit/rtp_receiver.h" 30#include "livekit/rtp_sender.h" 31#include "livekit/webrtc.h" 32#include "rtc_base/synchronization/mutex.h" 33#include "rust/cxx.h" 34 35namespace livekit { 36 37struct KeyProviderOptions ; 38enum class Algorithm : ::std ::int32_t ; 39class RtcFrameCryptorObserverWrapper ; 40class NativeFrameCryptorObserver ; 41 42/// Shared secret key for frame encryption. 43class KeyProvider { 44public : 45KeyProvider (KeyProviderOptions options ); 46~ KeyProvider () {} 47 48bool set_shared_key ( int32_t index, rust ::Vec < ::std:: uint8_t > key) const { 49std ::vector < uint8_t > key_vec; 50std :: copy (key. begin (), key. end (), std:: back_inserter (key_vec)); 51return impl_ -> SetSharedKey (index, key_vec); 52} 53 54rust ::Vec < ::std:: uint8_t > ratchet_shared_key (int32_t key_index) const { 55rust ::Vec < uint8_t > vec; 56auto data = impl_ -> RatchetSharedKey (key_index); 57if (data. empty ()) { 58throw std::runtime_error( "ratchet_shared_key failed" ); 59} 60 61std :: move (data. begin (), data. end (), std:: back_inserter (vec)); 62return vec; 63} 64 65rust ::Vec < ::std:: uint8_t > get_shared_key (int32_t key_index) const { 66rust ::Vec < uint8_t > vec; 67auto data = impl_ -> ExportSharedKey (key_index); 68if (data. empty ()) { 69throw std::runtime_error( "get_shared_key failed" ); 70} 71 72std :: move (data. begin (), data. end (), std:: back_inserter (vec)); 73return vec; 74} 75 76/// Set the key at the given index. 77bool set_key ( const :: rust ::String participant_id, 78int32_t index, 79rust ::Vec < ::std:: uint8_t > key) const { 80std ::vector < uint8_t > key_vec; 81std :: copy (key. begin (), key. end (), std:: back_inserter (key_vec)); 82return impl_ -> SetKey ( 83std:: string (participant_id. data (), participant_id. size ()), index, 84key_vec); 85} 86 87rust ::Vec < ::std:: uint8_t > ratchet_key (const ::rust::String participant_id, 88int32_t key_index) const { 89rust ::Vec < uint8_t > vec; 90auto data = impl_ -> RatchetKey ( 91std:: string (participant_id. data (), participant_id. size ()), key_index); 92if (data. empty ()) { 93throw std::runtime_error( "ratchet_key failed" ); 94} 95 96std :: move (data. begin (), data. end (), std:: back_inserter (vec)); 97return vec; 98} 99 100rust ::Vec < ::std:: uint8_t > get_key (const ::rust::String participant_id, 101int32_t key_index) const { 102rust ::Vec < uint8_t > vec; 103auto data = impl_ -> ExportKey ( 104std:: string (participant_id. data (), participant_id. size ()), key_index); 105if (data. empty ()) { 106throw std::runtime_error( "get_key failed" ); 107} 108 109std :: move (data. begin (), data. end (), std:: back_inserter (vec)); 110return vec; 111} 112 113void set_sif_trailer ( rust ::Vec < ::std:: uint8_t > trailer) const { 114std ::vector < uint8_t > trailer_vec; 115std :: copy (trailer. begin (), trailer. end (), std:: back_inserter (trailer_vec)); 116impl_ -> SetSifTrailer (trailer_vec); 117} 118 119rtc ::scoped_refptr < webrtc::KeyProvider > rtc_key_provider () { return impl_; } 120 121private : 122rtc ::scoped_refptr < webrtc::DefaultKeyProviderImpl > impl_; 123}; 124 125class FrameCryptor { 126public : 127FrameCryptor ( std ::shared_ptr < RtcRuntime > rtc_runtime, 128const std :: string participant_id, 129webrtc:: FrameCryptorTransformer :: Algorithm algorithm, 130rtc::scoped_refptr < webrtc::KeyProvider > key_provider, 131rtc::scoped_refptr < webrtc::RtpSenderInterface > sender); 132 133FrameCryptor ( std ::shared_ptr < RtcRuntime > rtc_runtime, 134const std :: string participant_id, 135webrtc:: FrameCryptorTransformer :: Algorithm algorithm, 136rtc::scoped_refptr < webrtc::KeyProvider > key_provider, 137rtc::scoped_refptr < webrtc::RtpReceiverInterface > receiver); 138~ FrameCryptor (); 139 140/// Enable/Disable frame crypto for the sender or receiver. 141void set_enabled ( bool enabled) const; 142 143/// Get the enabled state for the sender or receiver. 144bool enabled () const ; 145 146/// Set the key index for the sender or receiver. 147/// If the key index is not set, the key index will be set to 0. 148void set_key_index ( int32_t index) const; 149 150/// Get the key index for the sender or receiver. 151int32_t key_index () const; 152 153rust::String participant_id () const { return participant_id_; } 154 155void register_observer ( 156rust ::Box < RtcFrameCryptorObserverWrapper > observer) const; 157 158void unregister_observer () const; 159 160private: 161std::shared_ptr < RtcRuntime > rtc_runtime_; 162const rust ::String participant_id_; 163mutable webrtc::Mutex mutex_; 164rtc ::scoped_refptr < webrtc::FrameCryptorTransformer > e2ee_transformer_; 165rtc ::scoped_refptr < webrtc::KeyProvider > key_provider_; 166rtc ::scoped_refptr < webrtc::RtpSenderInterface > sender_; 167rtc ::scoped_refptr < webrtc::RtpReceiverInterface > receiver_; 168mutable rtc::scoped_refptr < NativeFrameCryptorObserver > observer_; 169}; 170 171class NativeFrameCryptorObserver 172: public webrtc::FrameCryptorTransformerObserver { 173public : 174NativeFrameCryptorObserver ( rust ::Box < RtcFrameCryptorObserverWrapper > observer, 175const FrameCryptor * fc); 176~ NativeFrameCryptorObserver (); 177 178void OnFrameCryptionStateChanged ( const std ::string participant_id, 179webrtc ::FrameCryptionState error) override; 180 181private : 182rust ::Box < RtcFrameCryptorObserverWrapper > observer_; 183const FrameCryptor * fc_; 184}; 185 186std ::shared_ptr < FrameCryptor > new_frame_cryptor_for_rtp_sender ( 187std::shared_ptr < PeerConnectionFactory > peer_factory, 188const ::rust::String participant_id, 189Algorithm algorithm, 190std::shared_ptr < KeyProvider > key_provider, 191std::shared_ptr < RtpSender > sender); 192 193std ::shared_ptr < FrameCryptor > new_frame_cryptor_for_rtp_receiver ( 194std::shared_ptr < PeerConnectionFactory > peer_factory, 195const ::rust::String participant_id, 196Algorithm algorithm, 197std::shared_ptr < KeyProvider > key_provider, 198std::shared_ptr < RtpReceiver > receiver); 199 200std ::shared_ptr < KeyProvider > new_key_provider (KeyProviderOptions options); 201 202} // namespace livekit