This commit is contained in:
Théo Monnom
2022-09-14 21:33:34 +02:00
parent 12a6d232b5
commit f643f458f8
23 changed files with 519 additions and 252 deletions
-1
View File
@@ -24,7 +24,6 @@ impl RTCEngine {
Ok(())
}
pub fn update(&self) {}
async fn handle_rtc(mut signal_receiver: broadcast::Receiver<signal_response::Message>) {
+3 -9
View File
@@ -1,9 +1,9 @@
use regex::Regex;
use std::env;
use std::fs;
use std::io::Write;
use std::path;
use std::process::Command;
use regex::Regex;
const MAC_SDKS: &str =
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs";
@@ -241,16 +241,10 @@ fn main() {
builder.warnings(false).compile("lkwebrtc");
for entry in glob::glob("./src/**/*.cpp").unwrap() {
println!(
"cargo:rerun-if-changed={}",
entry.unwrap().display()
);
println!("cargo:rerun-if-changed={}", entry.unwrap().display());
}
for entry in glob::glob("./include/**/*.h").unwrap() {
println!(
"cargo:rerun-if-changed={}",
entry.unwrap().display()
);
println!("cargo:rerun-if-changed={}", entry.unwrap().display());
}
}
@@ -16,7 +16,6 @@ namespace livekit {
class IceCandidate {
public:
explicit IceCandidate(std::unique_ptr<webrtc::IceCandidateInterface> ice_candidate);
private:
std::unique_ptr<webrtc::IceCandidateInterface> ice_candidate_;
};
@@ -29,6 +28,7 @@ namespace livekit {
public:
explicit SessionDescription(std::unique_ptr<webrtc::SessionDescriptionInterface> session_description);
rust::String stringify() const;
std::unique_ptr<SessionDescription> clone() const;
std::unique_ptr<webrtc::SessionDescriptionInterface> release();
@@ -40,6 +40,10 @@ namespace livekit {
return nullptr; // Ignore
}
static std::shared_ptr<SessionDescription> _shared_session_description(){
return nullptr; // Ignore
}
// SetCreateSdpObserver
class NativeCreateSdpObserver : public webrtc::CreateSessionDescriptionObserver {
@@ -6,7 +6,7 @@ pub mod ffi {
include!("livekit/candidate.h");
type Candidate; // cricket::Candidate
fn _unique_candidate() -> UniquePtr<Candidate>; // Ignore
}
}
@@ -19,6 +19,12 @@ namespace livekit {
}
rust::String SessionDescription::stringify() const {
std::string str;
session_description_->ToString(&str);
return rust::String{str};
}
std::unique_ptr<SessionDescription> SessionDescription::clone() const {
return std::make_unique<SessionDescription>(session_description_->Clone());
}
+31 -17
View File
@@ -1,5 +1,5 @@
use std::fmt::{Debug, Formatter};
use cxx::UniquePtr;
use cxx::{type_id, ExternType};
use crate::rtc_error::ffi::RTCError;
@@ -8,7 +8,10 @@ pub mod ffi {
extern "Rust" {
type CreateSdpObserverWrapper;
fn on_success(self: &CreateSdpObserverWrapper, session_description: UniquePtr<SessionDescription>);
fn on_success(
self: &CreateSdpObserverWrapper,
session_description: UniquePtr<SessionDescription>,
);
fn on_failure(self: &CreateSdpObserverWrapper, error: RTCError);
type SetLocalSdpObserverWrapper;
@@ -29,18 +32,35 @@ pub mod ffi {
type NativeSetLocalSdpObserverHandle;
type NativeSetRemoteSdpObserverHandle;
fn create_native_create_sdp_observer(observer: Box<CreateSdpObserverWrapper>) -> UniquePtr<NativeCreateSdpObserverHandle>;
fn create_native_set_local_sdp_observer(observer: Box<SetLocalSdpObserverWrapper>) -> UniquePtr<NativeSetLocalSdpObserverHandle>;
fn create_native_set_remote_sdp_observer(observer: Box<SetRemoteSdpObserverWrapper>) -> UniquePtr<NativeSetRemoteSdpObserverHandle>;
fn stringify(self: &SessionDescription) -> String;
fn create_native_create_sdp_observer(
observer: Box<CreateSdpObserverWrapper>,
) -> UniquePtr<NativeCreateSdpObserverHandle>;
fn create_native_set_local_sdp_observer(
observer: Box<SetLocalSdpObserverWrapper>,
) -> UniquePtr<NativeSetLocalSdpObserverHandle>;
fn create_native_set_remote_sdp_observer(
observer: Box<SetRemoteSdpObserverWrapper>,
) -> UniquePtr<NativeSetRemoteSdpObserverHandle>;
fn _unique_ice_candidate() -> UniquePtr<IceCandidate>; // Ignore
fn _shared_session_description() -> SharedPtr<SessionDescription>; // Ignore
fn _unique_session_description() -> UniquePtr<SessionDescription>; // Ignore
}
}
impl Debug for ffi::SessionDescription {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "{}", self.stringify())
}
}
unsafe impl Send for ffi::SessionDescription {}
// CreateSdpObserver
pub trait CreateSdpObserver: Send + Sync {
pub trait CreateSdpObserver: Send {
fn on_success(&self, session_description: UniquePtr<ffi::SessionDescription>);
fn on_failure(&self, error: RTCError);
}
@@ -51,9 +71,7 @@ pub struct CreateSdpObserverWrapper {
impl CreateSdpObserverWrapper {
pub fn new(observer: Box<dyn CreateSdpObserver>) -> Self {
Self {
observer
}
Self { observer }
}
fn on_success(&self, session_description: UniquePtr<ffi::SessionDescription>) {
@@ -67,7 +85,7 @@ impl CreateSdpObserverWrapper {
// SetLocalSdpObserver
pub trait SetLocalSdpObserver: Send + Sync {
pub trait SetLocalSdpObserver: Send {
fn on_set_local_description_complete(&self, error: RTCError);
}
@@ -77,9 +95,7 @@ pub struct SetLocalSdpObserverWrapper {
impl SetLocalSdpObserverWrapper {
pub fn new(observer: Box<dyn SetLocalSdpObserver>) -> Self {
Self {
observer
}
Self { observer }
}
fn on_set_local_description_complete(&self, error: RTCError) {
@@ -89,7 +105,7 @@ impl SetLocalSdpObserverWrapper {
// SetRemoteSdpObserver
pub trait SetRemoteSdpObserver: Send + Sync {
pub trait SetRemoteSdpObserver: Send {
fn on_set_remote_description_complete(&self, error: RTCError);
}
@@ -99,9 +115,7 @@ pub struct SetRemoteSdpObserverWrapper {
impl SetRemoteSdpObserverWrapper {
pub fn new(observer: Box<dyn SetRemoteSdpObserver>) -> Self {
Self {
observer
}
Self { observer }
}
fn on_set_remote_description_complete(&self, error: RTCError) {
@@ -1,10 +1,9 @@
pub mod candidate;
pub mod data_channel;
pub mod jsep;
pub mod media_stream_interface;
pub mod peer_connection;
pub mod peer_connection_factory;
pub mod data_channel;
pub mod jsep;
pub mod candidate;
pub mod rtc_error;
pub mod rtp_receiver;
pub mod rtp_transceiver;
pub mod rtc_error;
@@ -9,5 +9,4 @@ pub mod ffi {
fn _unique_media_stream() -> UniquePtr<MediaStreamInterface>; // Ignore
}
}
@@ -1,10 +1,12 @@
use cxx::UniquePtr;
use std::cell::RefCell;
use std::rc::Rc;
use crate::candidate::ffi::Candidate;
use crate::data_channel::ffi::DataChannel;
use crate::jsep::ffi::IceCandidate;
use crate::media_stream_interface::ffi::MediaStreamInterface;
use crate::rtp_receiver::ffi::RtpReceiver;
use crate::rtp_transceiver::ffi::RtpTransceiver;
use cxx::UniquePtr;
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
@@ -61,7 +63,7 @@ pub mod ffi {
pub enum IceGatheringState {
IceGatheringNew,
IceGatheringGathering,
IceGatheringComplete
IceGatheringComplete,
}
#[derive(Debug)]
@@ -79,11 +81,11 @@ pub mod ffi {
// Wrapper to opaque C++ objects
// https://github.com/dtolnay/cxx/issues/741
struct MediaStreamPtr {
pub ptr: UniquePtr<MediaStreamInterface>
pub ptr: UniquePtr<MediaStreamInterface>,
}
struct CandidatePtr {
pub ptr: UniquePtr<Candidate>
struct CandidatePtr {
pub ptr: UniquePtr<Candidate>,
}
unsafe extern "C++" {
@@ -109,13 +111,31 @@ pub mod ffi {
type NativePeerConnectionObserver;
type PeerConnection;
fn create_offer(self: Pin<&mut PeerConnection>, observer: UniquePtr<NativeCreateSdpObserverHandle>, options: RTCOfferAnswerOptions);
fn create_answer(self: Pin<&mut PeerConnection>, observer: UniquePtr<NativeCreateSdpObserverHandle>, options: RTCOfferAnswerOptions);
fn set_local_description(self: Pin<&mut PeerConnection>, desc: UniquePtr<SessionDescription>, observer: UniquePtr<NativeSetLocalSdpObserverHandle>);
fn set_remote_description(self: Pin<&mut PeerConnection>, desc: UniquePtr<SessionDescription>, observer: UniquePtr<NativeSetRemoteSdpObserverHandle>);
fn create_offer(
self: Pin<&mut PeerConnection>,
observer: UniquePtr<NativeCreateSdpObserverHandle>,
options: RTCOfferAnswerOptions,
);
fn create_answer(
self: Pin<&mut PeerConnection>,
observer: UniquePtr<NativeCreateSdpObserverHandle>,
options: RTCOfferAnswerOptions,
);
fn set_local_description(
self: Pin<&mut PeerConnection>,
desc: UniquePtr<SessionDescription>,
observer: UniquePtr<NativeSetLocalSdpObserverHandle>,
);
fn set_remote_description(
self: Pin<&mut PeerConnection>,
desc: UniquePtr<SessionDescription>,
observer: UniquePtr<NativeSetRemoteSdpObserverHandle>,
);
fn close(self: Pin<&mut PeerConnection>);
fn create_native_peer_connection_observer(observer: Box<PeerConnectionObserverWrapper>) -> UniquePtr<NativePeerConnectionObserver>;
fn create_native_peer_connection_observer(
observer: Box<PeerConnectionObserverWrapper>,
) -> UniquePtr<NativePeerConnectionObserver>;
fn _unique_peer_connection() -> UniquePtr<PeerConnection>; // Ignore
}
@@ -124,23 +144,73 @@ pub mod ffi {
type PeerConnectionObserverWrapper;
fn on_signaling_change(self: &mut PeerConnectionObserverWrapper, new_state: SignalingState);
fn on_add_stream(self: &mut PeerConnectionObserverWrapper, stream: UniquePtr<MediaStreamInterface>);
fn on_remove_stream(self: &mut PeerConnectionObserverWrapper, stream: UniquePtr<MediaStreamInterface>);
fn on_data_channel(self: &mut PeerConnectionObserverWrapper, data_channel: UniquePtr<DataChannel>);
fn on_add_stream(
self: &mut PeerConnectionObserverWrapper,
stream: UniquePtr<MediaStreamInterface>,
);
fn on_remove_stream(
self: &mut PeerConnectionObserverWrapper,
stream: UniquePtr<MediaStreamInterface>,
);
fn on_data_channel(
self: &mut PeerConnectionObserverWrapper,
data_channel: UniquePtr<DataChannel>,
);
fn on_renegotiation_needed(self: &mut PeerConnectionObserverWrapper);
fn on_negotiation_needed_event(self: &mut PeerConnectionObserverWrapper, event: u32);
fn on_ice_connection_change(self: &mut PeerConnectionObserverWrapper, new_state: IceConnectionState);
fn on_standardized_ice_connection_change(self: &mut PeerConnectionObserverWrapper, new_state: IceConnectionState);
fn on_connection_change(self: &mut PeerConnectionObserverWrapper, new_state: PeerConnectionState);
fn on_ice_gathering_change(self: &mut PeerConnectionObserverWrapper, new_state: IceGatheringState);
fn on_ice_candidate(self: &mut PeerConnectionObserverWrapper, candidate: UniquePtr<IceCandidate>);
fn on_ice_candidate_error(self: &mut PeerConnectionObserverWrapper, address: String, port: i32, url: String, error_code: i32, error_text: String);
fn on_ice_candidates_removed(self: &mut PeerConnectionObserverWrapper, removed: Vec<CandidatePtr>);
fn on_ice_connection_receiving_change(self: &mut PeerConnectionObserverWrapper, receiving: bool);
fn on_ice_selected_candidate_pair_changed(self: &mut PeerConnectionObserverWrapper, event: CandidatePairChangeEvent);
fn on_add_track(self: &mut PeerConnectionObserverWrapper, receiver: UniquePtr<RtpReceiver>, streams: Vec<MediaStreamPtr>);
fn on_track(self: &mut PeerConnectionObserverWrapper, transceiver: UniquePtr<RtpTransceiver>);
fn on_remove_track(self: &mut PeerConnectionObserverWrapper, receiver: UniquePtr<RtpReceiver>);
fn on_ice_connection_change(
self: &mut PeerConnectionObserverWrapper,
new_state: IceConnectionState,
);
fn on_standardized_ice_connection_change(
self: &mut PeerConnectionObserverWrapper,
new_state: IceConnectionState,
);
fn on_connection_change(
self: &mut PeerConnectionObserverWrapper,
new_state: PeerConnectionState,
);
fn on_ice_gathering_change(
self: &mut PeerConnectionObserverWrapper,
new_state: IceGatheringState,
);
fn on_ice_candidate(
self: &mut PeerConnectionObserverWrapper,
candidate: UniquePtr<IceCandidate>,
);
fn on_ice_candidate_error(
self: &mut PeerConnectionObserverWrapper,
address: String,
port: i32,
url: String,
error_code: i32,
error_text: String,
);
fn on_ice_candidates_removed(
self: &mut PeerConnectionObserverWrapper,
removed: Vec<CandidatePtr>,
);
fn on_ice_connection_receiving_change(
self: &mut PeerConnectionObserverWrapper,
receiving: bool,
);
fn on_ice_selected_candidate_pair_changed(
self: &mut PeerConnectionObserverWrapper,
event: CandidatePairChangeEvent,
);
fn on_add_track(
self: &mut PeerConnectionObserverWrapper,
receiver: UniquePtr<RtpReceiver>,
streams: Vec<MediaStreamPtr>,
);
fn on_track(
self: &mut PeerConnectionObserverWrapper,
transceiver: UniquePtr<RtpTransceiver>,
);
fn on_remove_track(
self: &mut PeerConnectionObserverWrapper,
receiver: UniquePtr<RtpReceiver>,
);
fn on_interesting_usage(self: &mut PeerConnectionObserverWrapper, usage_pattern: i32);
}
}
@@ -151,10 +221,10 @@ unsafe impl Send for ffi::PeerConnection {}
impl Default for ffi::RTCOfferAnswerOptions {
/*
static const int kUndefined = -1;
static const int kMaxOfferToReceiveMedia = 1;
static const int kOfferToReceiveMediaTrue = 1;
*/
static const int kUndefined = -1;
static const int kMaxOfferToReceiveMedia = 1;
static const int kOfferToReceiveMediaTrue = 1;
*/
fn default() -> Self {
Self {
@@ -165,7 +235,7 @@ impl Default for ffi::RTCOfferAnswerOptions {
use_rtp_mux: true,
raw_packetization_for_video: false,
num_simulcast_layers: 1,
use_obsolete_sctp_sdp: false
use_obsolete_sctp_sdp: false,
}
}
}
@@ -182,73 +252,90 @@ pub trait PeerConnectionObserver: Send + Sync {
fn on_connection_change(&mut self, new_state: ffi::PeerConnectionState);
fn on_ice_gathering_change(&mut self, new_state: ffi::IceGatheringState);
fn on_ice_candidate(&mut self, candidate: UniquePtr<IceCandidate>);
fn on_ice_candidate_error(&mut self, address: String, port: i32, url: String, error_code: i32, error_text: String);
fn on_ice_candidate_error(
&mut self,
address: String,
port: i32,
url: String,
error_code: i32,
error_text: String,
);
fn on_ice_candidates_removed(&mut self, removed: Vec<UniquePtr<Candidate>>);
fn on_ice_connection_receiving_change(&mut self, receiving: bool);
fn on_ice_selected_candidate_pair_changed(&mut self, event: ffi::CandidatePairChangeEvent);
fn on_add_track(&mut self, receiver: UniquePtr<RtpReceiver>, streams: Vec<UniquePtr<MediaStreamInterface>>);
fn on_add_track(
&mut self,
receiver: UniquePtr<RtpReceiver>,
streams: Vec<UniquePtr<MediaStreamInterface>>,
);
fn on_track(&mut self, transceiver: UniquePtr<RtpTransceiver>);
fn on_remove_track(&mut self, receiver: UniquePtr<RtpReceiver>);
fn on_interesting_usage(&mut self, usage_pattern: i32);
}
pub struct PeerConnectionObserverWrapper {
observer: Box<dyn PeerConnectionObserver>,
observer: Rc<RefCell<dyn PeerConnectionObserver>>,
}
impl PeerConnectionObserverWrapper {
pub fn new(observer: Box<dyn PeerConnectionObserver>) -> Self {
Self {
observer
}
pub fn new(observer: Rc<RefCell<dyn PeerConnectionObserver>>) -> Self {
Self { observer }
}
fn on_signaling_change(&mut self, new_state: ffi::SignalingState) {
self.observer.on_signaling_change(new_state);
self.observer.borrow_mut().on_signaling_change(new_state);
}
fn on_add_stream(&mut self, stream: UniquePtr<MediaStreamInterface>) {
self.observer.on_add_stream(stream);
self.observer.borrow_mut().on_add_stream(stream);
}
fn on_remove_stream(&mut self, stream: UniquePtr<MediaStreamInterface>) {
self.observer.on_remove_stream(stream);
self.observer.borrow_mut().on_remove_stream(stream);
}
fn on_data_channel(&mut self, data_channel: UniquePtr<DataChannel>) {
self.observer.on_data_channel(data_channel);
self.observer.borrow_mut().on_data_channel(data_channel);
}
fn on_renegotiation_needed(&mut self) {
self.observer.on_renegotiation_needed();
self.observer.borrow_mut().on_renegotiation_needed();
}
fn on_negotiation_needed_event(&mut self, event: u32) {
self.observer.on_negotiation_needed_event(event);
self.observer.borrow_mut().on_negotiation_needed_event(event);
}
fn on_ice_connection_change(&mut self, new_state: ffi::IceConnectionState) {
self.observer.on_ice_connection_change(new_state);
self.observer.borrow_mut().on_ice_connection_change(new_state);
}
fn on_standardized_ice_connection_change(&mut self, new_state: ffi::IceConnectionState) {
self.observer.on_standardized_ice_connection_change(new_state);
self.observer.borrow_mut().on_standardized_ice_connection_change(new_state);
}
fn on_connection_change(&mut self, new_state: ffi::PeerConnectionState) {
self.observer.on_connection_change(new_state);
self.observer.borrow_mut().on_connection_change(new_state);
}
fn on_ice_gathering_change(&mut self, new_state: ffi::IceGatheringState) {
self.observer.on_ice_gathering_change(new_state);
self.observer.borrow_mut().on_ice_gathering_change(new_state);
}
fn on_ice_candidate(&mut self, candidate: UniquePtr<IceCandidate>) {
self.observer.on_ice_candidate(candidate);
self.observer.borrow_mut().on_ice_candidate(candidate);
}
fn on_ice_candidate_error(&mut self, address: String, port: i32, url: String, error_code: i32, error_text: String) {
self.observer.on_ice_candidate_error(address, port, url, error_code, error_text);
fn on_ice_candidate_error(
&mut self,
address: String,
port: i32,
url: String,
error_code: i32,
error_text: String,
) {
self.observer
.borrow_mut().on_ice_candidate_error(address, port, url, error_code, error_text);
}
fn on_ice_candidates_removed(&mut self, removed: Vec<ffi::CandidatePtr>) {
@@ -258,36 +345,40 @@ impl PeerConnectionObserverWrapper {
vec.push(v.ptr);
}
self.observer.on_ice_candidates_removed(vec);
self.observer.borrow_mut().on_ice_candidates_removed(vec);
}
fn on_ice_connection_receiving_change(&mut self, receiving: bool) {
self.observer.on_ice_connection_receiving_change(receiving);
self.observer.borrow_mut().on_ice_connection_receiving_change(receiving);
}
fn on_ice_selected_candidate_pair_changed(&mut self, event: ffi::CandidatePairChangeEvent) {
self.observer.on_ice_selected_candidate_pair_changed(event);
self.observer.borrow_mut().on_ice_selected_candidate_pair_changed(event);
}
fn on_add_track(&mut self, receiver: UniquePtr<RtpReceiver>, streams: Vec<ffi::MediaStreamPtr>) {
fn on_add_track(
&mut self,
receiver: UniquePtr<RtpReceiver>,
streams: Vec<ffi::MediaStreamPtr>,
) {
let mut vec = Vec::new();
for v in streams {
vec.push(v.ptr);
}
self.observer.on_add_track(receiver, vec);
self.observer.borrow_mut().on_add_track(receiver, vec);
}
fn on_track(&mut self, transceiver: UniquePtr<RtpTransceiver>) {
self.observer.on_track(transceiver);
self.observer.borrow_mut().on_track(transceiver);
}
fn on_remove_track(&mut self, receiver: UniquePtr<RtpReceiver>) {
self.observer.on_remove_track(receiver);
self.observer.borrow_mut().on_remove_track(receiver);
}
fn on_interesting_usage(&mut self, usage_pattern: i32) {
self.observer.on_interesting_usage(usage_pattern);
self.observer.borrow_mut().on_interesting_usage(usage_pattern);
}
}
@@ -77,7 +77,7 @@ namespace livekit{
ice_server.password = item.password.c_str();
for (auto &url: item.urls){
ice_server.urls.push_back(url.c_str());
ice_server.urls.emplace_back(url.c_str());
}
rtc->servers.push_back(ice_server);
@@ -1,18 +1,21 @@
use std::any::Any;
use std::thread::sleep;
use std::time::Duration;
use cxx::UniquePtr;
use log::info;
use crate::candidate::ffi::Candidate;
use crate::data_channel::ffi::DataChannel;
use crate::jsep::ffi::IceCandidate;
use crate::media_stream_interface::ffi::MediaStreamInterface;
use crate::{jsep, peer_connection};
use crate::jsep::CreateSdpObserver;
use crate::peer_connection::ffi::{CandidatePairChangeEvent, IceConnectionState, IceGatheringState, PeerConnectionState, SignalingState};
use crate::media_stream_interface::ffi::MediaStreamInterface;
use crate::peer_connection::ffi::{
CandidatePairChangeEvent, IceConnectionState, IceGatheringState, PeerConnectionState,
SignalingState,
};
use crate::peer_connection::PeerConnectionObserver;
use crate::rtp_receiver::ffi::RtpReceiver;
use crate::rtp_transceiver::ffi::RtpTransceiver;
use crate::{jsep, peer_connection};
use cxx::UniquePtr;
use log::info;
use std::any::Any;
use std::thread::sleep;
use std::time::Duration;
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
@@ -33,22 +36,21 @@ pub mod ffi {
include!("livekit/peer_connection_factory.h");
type PeerConnection = crate::peer_connection::ffi::PeerConnection;
type NativePeerConnectionObserver = crate::peer_connection::ffi::NativePeerConnectionObserver;
type NativePeerConnectionObserver =
crate::peer_connection::ffi::NativePeerConnectionObserver;
type PeerConnectionFactory;
type NativeRTCConfiguration;
fn create_peer_connection_factory() -> UniquePtr<PeerConnectionFactory>;
fn create_rtc_configuration(conf: RTCConfiguration) -> UniquePtr<NativeRTCConfiguration>;
unsafe fn create_peer_connection(self: &PeerConnectionFactory, config: UniquePtr<NativeRTCConfiguration>, observer: UniquePtr<NativePeerConnectionObserver>) -> Result<UniquePtr<PeerConnection>>;
fn create_peer_connection(
self: &PeerConnectionFactory,
config: UniquePtr<NativeRTCConfiguration>,
observer: UniquePtr<NativePeerConnectionObserver>,
) -> Result<UniquePtr<PeerConnection>>;
}
}
}
/*
@@ -183,4 +185,3 @@ mod test {
}
*/
@@ -1,5 +1,8 @@
use std::fmt::{Display, Formatter};
use crate::rtc_error::ffi::RTCErrorType;
use std::error::Error;
use std::fmt::{Display, Formatter};
// cxx doesn't support custom Exception type, so we serialize RTCError inside the cxx::Exception "what" string
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
@@ -40,7 +43,7 @@ pub mod ffi {
pub message: String,
pub error_detail: RTCErrorDetailType,
pub has_sctp_cause_code: bool, // cxx doesn't support the Option trait
pub sctp_cause_code: u16
pub sctp_cause_code: u16,
}
}
@@ -63,15 +66,21 @@ impl ffi::RTCError {
message,
}
}
pub fn ok(&self) -> bool {
return self.error_type == RTCErrorType::None;
}
}
impl Error for ffi::RTCError {
}
impl Error for ffi::RTCError {}
impl Display for ffi::RTCError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "RtcError occurred {:?}: {}", self.error_type, self.message)
write!(
f,
"RtcError occurred {:?}: {}",
self.error_type, self.message
)
}
}
@@ -90,7 +99,7 @@ mod tests {
}
#[test]
fn serialize_deserialize(){
fn serialize_deserialize() {
let str = ffi::serialize_deserialize();
let error = unsafe { RTCError::from(&str) };
@@ -98,11 +107,14 @@ mod tests {
assert_eq!(error.error_detail, RTCErrorDetailType::DataChannelFailure);
assert_eq!(error.has_sctp_cause_code, true);
assert_eq!(error.sctp_cause_code, 24);
assert_eq!(error.message, "this is not a test, I repeat, this is not a test");
assert_eq!(
error.message,
"this is not a test, I repeat, this is not a test"
);
}
#[test]
fn throw_error(){
fn throw_error() {
let exc: cxx::Exception = ffi::throw_error().err().unwrap();
let error = unsafe { RTCError::from(exc.what()) };
@@ -112,4 +124,4 @@ mod tests {
assert_eq!(error.sctp_cause_code, 0);
assert_eq!(error.message, "exception is thrown!");
}
}
}
+2 -4
View File
@@ -1,6 +1,4 @@
#[derive(Debug)]
pub struct DataChannel {
}
}
@@ -1,4 +0,0 @@
pub struct IceCandidate {
}
+24
View File
@@ -0,0 +1,24 @@
use cxx::{SharedPtr, UniquePtr};
use libwebrtc_sys::jsep as sys_jsep;
#[derive(Debug)]
pub struct IceCandidate {
}
#[derive(Debug)]
pub struct SessionDescription {
cxx_handle: UniquePtr<sys_jsep::ffi::SessionDescription>
}
impl SessionDescription {
pub(crate) fn new(cxx_handle: UniquePtr<sys_jsep::ffi::SessionDescription>) -> Self {
Self {
cxx_handle
}
}
pub(crate) fn release(self) -> UniquePtr<sys_jsep::ffi::SessionDescription>{
self.cxx_handle
}
}
+4 -5
View File
@@ -1,9 +1,8 @@
pub mod peer_connection_factory;
pub mod peer_connection;
pub mod rtc_error;
pub mod data_channel;
pub mod media_stream;
pub mod peer_connection;
pub mod peer_connection_factory;
pub mod rtc_error;
pub mod rtp_receiver;
pub mod rtp_transceiver;
pub mod ice_candidate;
pub mod session_description;
pub mod jsep;
+2 -1
View File
@@ -1,4 +1,5 @@
#[derive(Debug)]
pub struct MediaStream {
}
}
+224 -79
View File
@@ -1,23 +1,22 @@
use std::sync::{Arc, Mutex};
use cxx::UniquePtr;
use tokio::sync::{mpsc, oneshot};
use libwebrtc_sys::peer_connection as sys_pc;
use libwebrtc_sys::jsep as sys_jsep;
use libwebrtc_sys::peer_connection as sys_pc;
use std::sync::{Arc, Mutex};
use thiserror::Error;
use tokio::sync::{mpsc, oneshot};
use crate::data_channel::DataChannel;
use crate::media_stream::MediaStream;
use crate::ice_candidate::IceCandidate;
use crate::rtc_error::RTCError;
use crate::rtp_receiver::RtpReceiver;
use crate::rtp_transceiver::RtpTransceiver;
use crate::session_description::SessionDescription;
use crate::rtc_error::RTCError;
use crate::jsep::{SessionDescription, IceCandidate};
pub use libwebrtc_sys::peer_connection::ffi::PeerConnectionState;
pub use libwebrtc_sys::peer_connection::ffi::SignalingState;
pub use libwebrtc_sys::peer_connection::ffi::IceConnectionState;
pub use libwebrtc_sys::peer_connection::ffi::IceGatheringState;
pub use libwebrtc_sys::peer_connection::ffi::PeerConnectionState;
pub use libwebrtc_sys::peer_connection::ffi::RTCOfferAnswerOptions;
pub use libwebrtc_sys::peer_connection::ffi::SignalingState;
#[derive(Error, Debug)]
pub enum SdpError {
@@ -29,44 +28,27 @@ pub enum SdpError {
pub struct PeerConnection {
cxx_handle: UniquePtr<sys_pc::ffi::PeerConnection>,
observer: InternalObserver
observer: InternalObserver,
}
impl PeerConnection {
pub fn new(cxx_handle: UniquePtr<sys_pc::ffi::PeerConnection>) -> Self {
pub(crate) fn new(cxx_handle: UniquePtr<sys_pc::ffi::PeerConnection>) -> Self {
Self {
cxx_handle,
observer: InternalObserver {
on_signaling_change_handler: Arc::new(Default::default()),
on_add_stream_handler: Arc::new(Default::default()),
on_remove_stream_handler: Arc::new(Default::default()),
on_data_channel_handler: Arc::new(Default::default()),
on_renegotiation_needed_handler: Arc::new(Default::default()),
on_negotiation_needed_event_handler: Arc::new(Default::default()),
on_ice_connection_change_handler: Arc::new(Default::default()),
on_standardized_ice_connection_change_handler: Arc::new(Default::default()),
on_connection_change_handler: Arc::new(Default::default()),
on_ice_gathering_change_handler: Arc::new(Default::default()),
on_ice_candidate_handler: Arc::new(Default::default()),
on_ice_candidate_error_handler: Arc::new(Default::default()),
on_ice_candidates_removed_handler: Arc::new(Default::default()),
on_ice_connection_receiving_change_handler: Arc::new(Default::default()),
on_ice_selected_candidate_pair_changed_handler: Arc::new(Default::default()),
on_add_track_handler: Arc::new(Default::default()),
on_track_handler: Arc::new(Default::default()),
on_remove_track_handler: Arc::new(Default::default()),
on_interesting_usage_handler: Arc::new(Default::default())
}
observer: InternalObserver::default()
}
}
pub async fn create_offer(&mut self) -> Result<SessionDescription, SdpError> {
let (tx, mut rx) = mpsc::channel(1);
let wrapper = sys_jsep::CreateSdpObserverWrapper::new(Box::new(InternalCreateSdpObserver { tx }));
let wrapper =
sys_jsep::CreateSdpObserverWrapper::new(Box::new(InternalCreateSdpObserver { tx }));
let native_wrapper = sys_jsep::ffi::create_native_create_sdp_observer(Box::new(wrapper));
self.cxx_handle.pin_mut().create_offer(native_wrapper, RTCOfferAnswerOptions::default());
self.cxx_handle
.pin_mut()
.create_offer(native_wrapper, RTCOfferAnswerOptions::default());
match rx.recv().await {
Some(value) => value.map_err(Into::into),
@@ -77,10 +59,13 @@ impl PeerConnection {
pub async fn create_answer(&mut self) -> Result<SessionDescription, SdpError> {
let (tx, mut rx) = mpsc::channel(1);
let wrapper = sys_jsep::CreateSdpObserverWrapper::new(Box::new(InternalCreateSdpObserver { tx }));
let wrapper =
sys_jsep::CreateSdpObserverWrapper::new(Box::new(InternalCreateSdpObserver { tx }));
let native_wrapper = sys_jsep::ffi::create_native_create_sdp_observer(Box::new(wrapper));
self.cxx_handle.pin_mut().create_answer(native_wrapper, RTCOfferAnswerOptions::default());
self.cxx_handle
.pin_mut()
.create_answer(native_wrapper, RTCOfferAnswerOptions::default());
match rx.recv().await {
Some(value) => value.map_err(Into::into),
@@ -88,15 +73,42 @@ impl PeerConnection {
}
}
pub async fn set_local_description(&mut self, desc: SessionDescription) -> Result<(), SdpError> {
pub async fn set_local_description(
&mut self,
desc: SessionDescription,
) -> Result<(), SdpError> {
let (tx, mut rx) = mpsc::channel(1);
let wrapper =
sys_jsep::SetLocalSdpObserverWrapper::new(Box::new(InternalSetLocalSdpObserver { tx }));
let native_wrapper = sys_jsep::ffi::create_native_set_local_sdp_observer(Box::new(wrapper));
self.cxx_handle
.pin_mut()
.set_local_description(desc.release(), native_wrapper);
Ok(())
match rx.recv().await {
Some(value) => value.map_err(Into::into),
None => Err(SdpError::RecvError("channel closed".to_string())),
}
}
pub async fn set_remote_description(&mut self, desc: SessionDescription) -> Result<(), SdpError> {
pub async fn set_remote_description(
&mut self,
desc: SessionDescription,
) -> Result<(), SdpError> {
let (tx, mut rx) = mpsc::channel(1);
let wrapper =
sys_jsep::SetRemoteSdpObserverWrapper::new(Box::new(InternalSetRemoteSdpObserver { tx }));
let native_wrapper = sys_jsep::ffi::create_native_set_remote_sdp_observer(Box::new(wrapper));
Ok(())
self.cxx_handle
.pin_mut()
.set_remote_description(desc.release(), native_wrapper);
match rx.recv().await {
Some(value) => value.map_err(Into::into),
None => Err(SdpError::RecvError("channel closed".to_string())),
}
}
pub fn on_signaling_change(&mut self, handler: OnSignalingChangeHandler) {
@@ -116,15 +128,30 @@ impl PeerConnection {
}
pub fn on_renegotiation_needed(&mut self, handler: OnRenegotiationNeededHandler) {
*self.observer.on_renegotiation_needed_handler.lock().unwrap() = Some(handler);
*self
.observer
.on_renegotiation_needed_handler
.lock()
.unwrap() = Some(handler);
}
pub fn on_ice_connection_change(&mut self, handler: OnIceConnectionChangeHandler) {
*self.observer.on_ice_connection_change_handler.lock().unwrap() = Some(handler);
*self
.observer
.on_ice_connection_change_handler
.lock()
.unwrap() = Some(handler);
}
pub fn on_standardized_ice_connection_change(&mut self, handler: OnStandardizedIceConnectionChangeHandler) {
*self.observer.on_standardized_ice_connection_change_handler.lock().unwrap() = Some(handler);
pub fn on_standardized_ice_connection_change(
&mut self,
handler: OnStandardizedIceConnectionChangeHandler,
) {
*self
.observer
.on_standardized_ice_connection_change_handler
.lock()
.unwrap() = Some(handler);
}
pub fn on_connection_change(&mut self, handler: OnConnectionChangeHandler) {
@@ -132,7 +159,11 @@ impl PeerConnection {
}
pub fn on_ice_gathering_change(&mut self, handler: OnIceGatheringChangeHandler) {
*self.observer.on_ice_gathering_change_handler.lock().unwrap() = Some(handler);
*self
.observer
.on_ice_gathering_change_handler
.lock()
.unwrap() = Some(handler);
}
pub fn on_ice_candidate(&mut self, handler: OnIceCandidateHandler) {
@@ -144,15 +175,33 @@ impl PeerConnection {
}
pub fn on_ice_candidates_removed(&mut self, handler: OnIceCandidatesRemovedHandler) {
*self.observer.on_ice_candidates_removed_handler.lock().unwrap() = Some(handler);
*self
.observer
.on_ice_candidates_removed_handler
.lock()
.unwrap() = Some(handler);
}
pub fn on_ice_connection_receiving_change(&mut self, handler: OnIceConnectionReceivingChangeHandler) {
*self.observer.on_ice_connection_receiving_change_handler.lock().unwrap() = Some(handler);
pub fn on_ice_connection_receiving_change(
&mut self,
handler: OnIceConnectionReceivingChangeHandler,
) {
*self
.observer
.on_ice_connection_receiving_change_handler
.lock()
.unwrap() = Some(handler);
}
pub fn on_ice_selected_candidate_pair_changed(&mut self, handler: OnIceSelectedCandidatePairChangedHandler) {
*self.observer.on_ice_selected_candidate_pair_changed_handler.lock().unwrap() = Some(handler);
pub fn on_ice_selected_candidate_pair_changed(
&mut self,
handler: OnIceSelectedCandidatePairChangedHandler,
) {
*self
.observer
.on_ice_selected_candidate_pair_changed_handler
.lock()
.unwrap() = Some(handler);
}
pub fn on_add_track(&mut self, handler: OnAddTrackHandler) {
@@ -175,44 +224,48 @@ impl PeerConnection {
// CreateSdpObserver
struct InternalCreateSdpObserver {
tx: mpsc::Sender<Result<SessionDescription, RTCError>>
tx: mpsc::Sender<Result<SessionDescription, RTCError>>,
}
impl sys_jsep::CreateSdpObserver for InternalCreateSdpObserver {
fn on_success(&self, session_description: UniquePtr<libwebrtc_sys::jsep::ffi::SessionDescription>) {
self.tx.blocking_send(Ok(SessionDescription{})).unwrap(); // TODO
fn on_success(
&self,
session_description: UniquePtr<libwebrtc_sys::jsep::ffi::SessionDescription>,
) {
self.tx.blocking_send(Ok(SessionDescription::new(session_description))).unwrap();
}
fn on_failure(&self, error: RTCError) {
self.tx.blocking_send(Err(error)).unwrap(); // TODO
self.tx.blocking_send(Err(error)).unwrap();
}
}
// SetLocalSdpObserver
struct InternalSetLocalSdpObserver {
tx: mpsc::Sender<Result<(), RTCError>>
tx: mpsc::Sender<Result<(), RTCError>>,
}
impl sys_jsep::SetLocalSdpObserver for InternalSetLocalSdpObserver {
fn on_set_local_description_complete(&self, error: RTCError) {
self.tx.blocking_send(Ok(())).unwrap();
let res = if error.ok() { Ok(()) } else { Err(error) };
self.tx.blocking_send(res).unwrap();
}
}
// SetRemoteSdpObserver
struct InternalSetRemoteSdpObserver {
tx: mpsc::Sender<Result<(), RTCError>>,
}
impl sys_jsep::SetRemoteSdpObserver for InternalSetRemoteSdpObserver {
fn on_set_remote_description_complete(&self, error: RTCError) {
todo!()
let res = if error.ok() { Ok(()) } else { Err(error) };
self.tx.blocking_send(res).unwrap();
}
}
// PeerConnectionObserver
// TODO(theomonnom) Should we return futures?
@@ -223,20 +276,23 @@ pub type OnDataChannelHandler = Box<dyn FnMut(DataChannel) + Send + Sync>;
pub type OnRenegotiationNeededHandler = Box<dyn FnMut() + Send + Sync>;
pub type OnNegotiationNeededEventHandler = Box<dyn FnMut(u32) + Send + Sync>;
pub type OnIceConnectionChangeHandler = Box<dyn FnMut(IceConnectionState) + Send + Sync>;
pub type OnStandardizedIceConnectionChangeHandler = Box<dyn FnMut(IceConnectionState) + Send + Sync>;
pub type OnStandardizedIceConnectionChangeHandler =
Box<dyn FnMut(IceConnectionState) + Send + Sync>;
pub type OnConnectionChangeHandler = Box<dyn FnMut(PeerConnectionState) + Send + Sync>;
pub type OnIceGatheringChangeHandler = Box<dyn FnMut(IceGatheringState) + Send + Sync>;
pub type OnIceCandidateHandler = Box<dyn FnMut(IceCandidate) + Send + Sync>;
pub type OnIceCandidateErrorHandler = Box<dyn FnMut(String, i32, String, i32, String) + Send + Sync>;
pub type OnIceCandidateErrorHandler =
Box<dyn FnMut(String, i32, String, i32, String) + Send + Sync>;
pub type OnIceCandidatesRemovedHandler = Box<dyn FnMut(Vec<IceCandidate>) + Send + Sync>;
pub type OnIceConnectionReceivingChangeHandler = Box<dyn FnMut(bool) + Send + Sync>;
pub type OnIceSelectedCandidatePairChangedHandler = Box<dyn FnMut(libwebrtc_sys::peer_connection::ffi::CandidatePairChangeEvent) + Send + Sync>;
pub type OnIceSelectedCandidatePairChangedHandler =
Box<dyn FnMut(libwebrtc_sys::peer_connection::ffi::CandidatePairChangeEvent) + Send + Sync>;
pub type OnAddTrackHandler = Box<dyn FnMut(RtpReceiver, Vec<MediaStream>) + Send + Sync>;
pub type OnTrackHandler = Box<dyn FnMut(RtpTransceiver) + Send + Sync>;
pub type OnRemoveTrackHandler = Box<dyn FnMut(RtpReceiver) + Send + Sync>;
pub type OnInterestingUsageHandler = Box<dyn FnMut(i32) + Send + Sync>;
struct InternalObserver {
pub(crate) struct InternalObserver {
on_signaling_change_handler: Arc<Mutex<Option<OnSignalingChangeHandler>>>,
on_add_stream_handler: Arc<Mutex<Option<OnAddStreamHandler>>>,
on_remove_stream_handler: Arc<Mutex<Option<OnRemoveStreamHandler>>>,
@@ -244,18 +300,47 @@ struct InternalObserver {
on_renegotiation_needed_handler: Arc<Mutex<Option<OnRenegotiationNeededHandler>>>,
on_negotiation_needed_event_handler: Arc<Mutex<Option<OnNegotiationNeededEventHandler>>>,
on_ice_connection_change_handler: Arc<Mutex<Option<OnIceConnectionChangeHandler>>>,
on_standardized_ice_connection_change_handler: Arc<Mutex<Option<OnStandardizedIceConnectionChangeHandler>>>,
on_standardized_ice_connection_change_handler:
Arc<Mutex<Option<OnStandardizedIceConnectionChangeHandler>>>,
on_connection_change_handler: Arc<Mutex<Option<OnConnectionChangeHandler>>>,
on_ice_gathering_change_handler: Arc<Mutex<Option<OnIceGatheringChangeHandler>>>,
on_ice_candidate_handler: Arc<Mutex<Option<OnIceCandidateHandler>>>,
on_ice_candidate_error_handler: Arc<Mutex<Option<OnIceCandidateErrorHandler>>>,
on_ice_candidates_removed_handler: Arc<Mutex<Option<OnIceCandidatesRemovedHandler>>>,
on_ice_connection_receiving_change_handler: Arc<Mutex<Option<OnIceConnectionReceivingChangeHandler>>>,
on_ice_selected_candidate_pair_changed_handler: Arc<Mutex<Option<OnIceSelectedCandidatePairChangedHandler>>>,
on_ice_connection_receiving_change_handler:
Arc<Mutex<Option<OnIceConnectionReceivingChangeHandler>>>,
on_ice_selected_candidate_pair_changed_handler:
Arc<Mutex<Option<OnIceSelectedCandidatePairChangedHandler>>>,
on_add_track_handler: Arc<Mutex<Option<OnAddTrackHandler>>>,
on_track_handler: Arc<Mutex<Option<OnTrackHandler>>>,
on_remove_track_handler: Arc<Mutex<Option<OnRemoveTrackHandler>>>,
on_interesting_usage_handler: Arc<Mutex<Option<OnInterestingUsageHandler>>>
on_interesting_usage_handler: Arc<Mutex<Option<OnInterestingUsageHandler>>>,
}
impl Default for InternalObserver {
fn default() -> Self {
Self {
on_signaling_change_handler: Arc::new(Default::default()),
on_add_stream_handler: Arc::new(Default::default()),
on_remove_stream_handler: Arc::new(Default::default()),
on_data_channel_handler: Arc::new(Default::default()),
on_renegotiation_needed_handler: Arc::new(Default::default()),
on_negotiation_needed_event_handler: Arc::new(Default::default()),
on_ice_connection_change_handler: Arc::new(Default::default()),
on_standardized_ice_connection_change_handler: Arc::new(Default::default()),
on_connection_change_handler: Arc::new(Default::default()),
on_ice_gathering_change_handler: Arc::new(Default::default()),
on_ice_candidate_handler: Arc::new(Default::default()),
on_ice_candidate_error_handler: Arc::new(Default::default()),
on_ice_candidates_removed_handler: Arc::new(Default::default()),
on_ice_connection_receiving_change_handler: Arc::new(Default::default()),
on_ice_selected_candidate_pair_changed_handler: Arc::new(Default::default()),
on_add_track_handler: Arc::new(Default::default()),
on_track_handler: Arc::new(Default::default()),
on_remove_track_handler: Arc::new(Default::default()),
on_interesting_usage_handler: Arc::new(Default::default()),
}
}
}
// Observers are being called on the Signaling Thread
@@ -267,21 +352,30 @@ impl sys_pc::PeerConnectionObserver for InternalObserver {
}
}
fn on_add_stream(&mut self, stream: UniquePtr<libwebrtc_sys::media_stream_interface::ffi::MediaStreamInterface>) {
fn on_add_stream(
&mut self,
stream: UniquePtr<libwebrtc_sys::media_stream_interface::ffi::MediaStreamInterface>,
) {
let mut handler = self.on_add_stream_handler.lock().unwrap();
if let Some(f) = handler.as_mut() {
// TODO(theomonnom)
}
}
fn on_remove_stream(&mut self, stream: UniquePtr<libwebrtc_sys::media_stream_interface::ffi::MediaStreamInterface>) {
fn on_remove_stream(
&mut self,
stream: UniquePtr<libwebrtc_sys::media_stream_interface::ffi::MediaStreamInterface>,
) {
let mut handler = self.on_remove_stream_handler.lock().unwrap();
if let Some(f) = handler.as_mut() {
// TODO(theomonnom)
}
}
fn on_data_channel(&mut self, data_channel: UniquePtr<libwebrtc_sys::data_channel::ffi::DataChannel>) {
fn on_data_channel(
&mut self,
data_channel: UniquePtr<libwebrtc_sys::data_channel::ffi::DataChannel>,
) {
let mut handler = self.on_data_channel_handler.lock().unwrap();
if let Some(f) = handler.as_mut() {
// TODO(theomonnom)
@@ -310,7 +404,10 @@ impl sys_pc::PeerConnectionObserver for InternalObserver {
}
fn on_standardized_ice_connection_change(&mut self, new_state: IceConnectionState) {
let mut handler = self.on_standardized_ice_connection_change_handler.lock().unwrap();
let mut handler = self
.on_standardized_ice_connection_change_handler
.lock()
.unwrap();
if let Some(f) = handler.as_mut() {
f(new_state);
}
@@ -337,14 +434,24 @@ impl sys_pc::PeerConnectionObserver for InternalObserver {
}
}
fn on_ice_candidate_error(&mut self, address: String, port: i32, url: String, error_code: i32, error_text: String) {
fn on_ice_candidate_error(
&mut self,
address: String,
port: i32,
url: String,
error_code: i32,
error_text: String,
) {
let mut handler = self.on_ice_candidate_error_handler.lock().unwrap();
if let Some(f) = handler.as_mut() {
f(address, port, url, error_code, error_text);
}
}
fn on_ice_candidates_removed(&mut self, removed: Vec<UniquePtr<libwebrtc_sys::candidate::ffi::Candidate>>) {
fn on_ice_candidates_removed(
&mut self,
removed: Vec<UniquePtr<libwebrtc_sys::candidate::ffi::Candidate>>,
) {
let mut handler = self.on_ice_candidates_removed_handler.lock().unwrap();
if let Some(f) = handler.as_mut() {
// TODO(theomonnom)
@@ -352,34 +459,53 @@ impl sys_pc::PeerConnectionObserver for InternalObserver {
}
fn on_ice_connection_receiving_change(&mut self, receiving: bool) {
let mut handler = self.on_ice_connection_receiving_change_handler.lock().unwrap();
let mut handler = self
.on_ice_connection_receiving_change_handler
.lock()
.unwrap();
if let Some(f) = handler.as_mut() {
f(receiving);
}
}
fn on_ice_selected_candidate_pair_changed(&mut self, event: libwebrtc_sys::peer_connection::ffi::CandidatePairChangeEvent) {
let mut handler = self.on_ice_selected_candidate_pair_changed_handler.lock().unwrap();
fn on_ice_selected_candidate_pair_changed(
&mut self,
event: libwebrtc_sys::peer_connection::ffi::CandidatePairChangeEvent,
) {
let mut handler = self
.on_ice_selected_candidate_pair_changed_handler
.lock()
.unwrap();
if let Some(f) = handler.as_mut() {
f(event);
}
}
fn on_add_track(&mut self, receiver: UniquePtr<libwebrtc_sys::rtp_receiver::ffi::RtpReceiver>, streams: Vec<UniquePtr<libwebrtc_sys::media_stream_interface::ffi::MediaStreamInterface>>) {
fn on_add_track(
&mut self,
receiver: UniquePtr<libwebrtc_sys::rtp_receiver::ffi::RtpReceiver>,
streams: Vec<UniquePtr<libwebrtc_sys::media_stream_interface::ffi::MediaStreamInterface>>,
) {
let mut handler = self.on_add_track_handler.lock().unwrap();
if let Some(f) = handler.as_mut() {
// TODO(theomonnom)
}
}
fn on_track(&mut self, transceiver: UniquePtr<libwebrtc_sys::rtp_transceiver::ffi::RtpTransceiver>) {
fn on_track(
&mut self,
transceiver: UniquePtr<libwebrtc_sys::rtp_transceiver::ffi::RtpTransceiver>,
) {
let mut handler = self.on_track_handler.lock().unwrap();
if let Some(f) = handler.as_mut() {
// TODO(theomonnom)
}
}
fn on_remove_track(&mut self, receiver: UniquePtr<libwebrtc_sys::rtp_receiver::ffi::RtpReceiver>) {
fn on_remove_track(
&mut self,
receiver: UniquePtr<libwebrtc_sys::rtp_receiver::ffi::RtpReceiver>,
) {
let mut handler = self.on_remove_track_handler.lock().unwrap();
if let Some(f) = handler.as_mut() {
// TODO(theomonnom)
@@ -392,4 +518,23 @@ impl sys_pc::PeerConnectionObserver for InternalObserver {
f(usage_pattern);
}
}
}
}
#[cfg(test)]
mod tests {
use libwebrtc_sys::peer_connection_factory::ffi::RTCConfiguration;
use crate::peer_connection_factory::PeerConnectionFactory;
#[tokio::test]
async fn create_pc() {
let factory = PeerConnectionFactory::new();
let mut pc = factory.create_peer_connection(
RTCConfiguration {
ice_servers: vec![],
},
Box::new(()),
).unwrap();
let offer = pc.create_offer().await.unwrap();
}
}
@@ -1,53 +1,43 @@
use cxx::UniquePtr;
use libwebrtc_sys::peer_connection_factory as sys_factory;
use libwebrtc_sys::peer_connection as sys_pc;
use libwebrtc_sys::peer_connection_factory as sys_factory;
use crate::peer_connection::PeerConnection;
use crate::rtc_error::RTCError;
pub use sys_factory::ffi::{RTCConfiguration, ICEServer};
pub use sys_factory::ffi::{ICEServer, RTCConfiguration};
pub struct PeerConnectionFactory {
cxx_handle: UniquePtr<sys_factory::ffi::PeerConnectionFactory>
cxx_handle: UniquePtr<sys_factory::ffi::PeerConnectionFactory>,
}
impl PeerConnectionFactory {
pub fn new() -> Self {
Self {
cxx_handle: sys_factory::ffi::create_peer_connection_factory()
cxx_handle: sys_factory::ffi::create_peer_connection_factory(),
}
}
pub fn create_peer_connection(&self, config: RTCConfiguration, observer: Box<dyn sys_pc::PeerConnectionObserver>) -> Result<PeerConnection, RTCError> {
pub fn create_peer_connection(
&self,
config: RTCConfiguration,
observer: Box<dyn sys_pc::PeerConnectionObserver>,
) -> Result<PeerConnection, RTCError> {
let native_config = sys_factory::ffi::create_rtc_configuration(config);
let native_observer = sys_pc::ffi::create_native_peer_connection_observer(Box::new(sys_pc::PeerConnectionObserverWrapper::new(observer)));
let native_observer = sys_pc::ffi::create_native_peer_connection_observer(Box::new(
sys_pc::PeerConnectionObserverWrapper::new(observer),
));
let pc_result : Result<UniquePtr<sys_pc::ffi::PeerConnection>, cxx::Exception> = unsafe {
self.cxx_handle.create_peer_connection(native_config, native_observer)
let pc_result: Result<UniquePtr<sys_pc::ffi::PeerConnection>, cxx::Exception> = unsafe {
self.cxx_handle
.create_peer_connection(native_config, native_observer)
};
match pc_result {
Ok(cxx_handle) => {
Ok(PeerConnection::new(cxx_handle))
}
Ok(cxx_handle) => Ok(PeerConnection::new(cxx_handle)),
Err(e) => {
Err(unsafe {RTCError::from(e.what()) }) // TODO
Err(unsafe { RTCError::from(e.what()) }) // TODO
}
}
}
}
#[cfg(test)]
mod tests {
use crate::peer_connection_factory::PeerConnectionFactory;
use crate::peer_connection_factory::{RTCConfiguration, ICEServer};
#[test]
fn create_pc(){
let factory = PeerConnectionFactory::new();
let pc = factory.create_peer_connection(RTCConfiguration{
ice_servers: vec!()
}, Box::new(()));
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
use std::fmt;
// TODO(theomonnom) Wrap the RTCError ffi so we can use Option(u16)
pub use libwebrtc_sys::rtc_error::ffi::RTCError;
+2 -2
View File
@@ -1,5 +1,5 @@
#[derive(Debug)]
pub struct RtpReceiver {
}
}
+2 -2
View File
@@ -1,5 +1,5 @@
#[derive(Debug)]
pub struct RtpTransceiver {
}
}
@@ -1,5 +0,0 @@
#[derive(Debug)]
pub struct SessionDescription {
}