rtc_config

This commit is contained in:
Théo Monnom
2022-09-25 13:54:56 +02:00
parent e74aac70bb
commit 4067a9add6
26 changed files with 865 additions and 140 deletions
-2
View File
@@ -1,2 +0,0 @@
[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]
@@ -1,2 +0,0 @@
[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]
@@ -19,6 +19,7 @@ class IceCandidate {
explicit IceCandidate(
std::unique_ptr<webrtc::IceCandidateInterface> ice_candidate);
rust::String stringify() const;
std::unique_ptr<webrtc::IceCandidateInterface> release();
private:
@@ -34,6 +34,10 @@ class PeerConnection {
std::unique_ptr<NativeDataChannelInit> init);
void add_ice_candidate(std::unique_ptr<IceCandidate> candidate,
NativeAddIceCandidateObserver& observer);
std::unique_ptr<SessionDescription> local_description() const;
std::unique_ptr<SessionDescription> remote_description() const;
SignalingState signaling_state() const;
IceGatheringState ice_gathering_state() const;
void close();
private:
@@ -17,6 +17,10 @@ struct DataChannelObserverWrapper;
struct AddIceCandidateObserverWrapper;
// Shared types
enum class PeerConnectionState;
enum class SignalingState;
enum class IceConnectionState;
enum class IceGatheringState;
struct RTCOfferAnswerOptions;
struct RTCError;
struct DataChannelInit;
@@ -3,7 +3,7 @@ use std::slice;
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
#[derive(Debug)]
#[repr(u32)]
#[repr(i32)]
pub enum Priority {
VeryLow,
Low,
@@ -36,7 +36,7 @@ pub mod ffi {
}
#[derive(Debug)]
#[repr(u32)]
#[repr(i32)]
pub enum DataState {
Connecting,
Open,
@@ -16,6 +16,12 @@ IceCandidate::IceCandidate(
std::unique_ptr<webrtc::IceCandidateInterface> ice_candidate)
: ice_candidate_(std::move(ice_candidate)) {}
rust::String IceCandidate::stringify() const {
std::string str;
ice_candidate_->ToString(&str);
return rust::String{str};
}
std::unique_ptr<webrtc::IceCandidateInterface> IceCandidate::release() {
return std::move(ice_candidate_);
}
@@ -32,6 +32,8 @@ pub mod ffi {
type NativeSetLocalSdpObserverHandle;
type NativeSetRemoteSdpObserverHandle;
fn stringify(self: &IceCandidate) -> String;
fn stringify(self: &SessionDescription) -> String;
fn clone(self: &SessionDescription) -> UniquePtr<SessionDescription>;
@@ -46,7 +48,7 @@ pub mod ffi {
) -> UniquePtr<NativeSetRemoteSdpObserverHandle>;
fn _unique_ice_candidate() -> UniquePtr<IceCandidate>; // Ignore
fn _unique_session_description() -> UniquePtr<SessionDescription>; // Ignore
fn _unique_session_description() -> UniquePtr<SessionDescription>; // Ignore
}
}
@@ -60,12 +62,14 @@ unsafe impl Send for ffi::SessionDescription {}
impl Debug for ffi::IceCandidate {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "TODO") // TODO(theomonnom)
write!(f, "{}", self.stringify())
}
}
unsafe impl Send for ffi::IceCandidate {}
unsafe impl Sync for ffi::IceCandidate {}
// CreateSdpObserver
pub trait CreateSdpObserver: Send {
@@ -76,10 +76,35 @@ void PeerConnection::add_ice_candidate(
[&](const webrtc::RTCError& err) { observer.OnComplete(to_error(err)); });
}
std::unique_ptr<SessionDescription> PeerConnection::local_description() const {
auto local_description = peer_connection_->local_description();
if (local_description)
return std::make_unique<SessionDescription>(local_description->Clone());
return std::unique_ptr<SessionDescription>();
}
std::unique_ptr<SessionDescription> PeerConnection::remote_description() const {
auto remote_description = peer_connection_->remote_description();
if (remote_description)
return std::make_unique<SessionDescription>(remote_description->Clone());
return std::unique_ptr<SessionDescription>();
}
SignalingState PeerConnection::signaling_state() const {
return static_cast<SignalingState>(peer_connection_->signaling_state());
}
IceGatheringState PeerConnection::ice_gathering_state() const {
return static_cast<IceGatheringState>(peer_connection_->ice_gathering_state());
}
void PeerConnection::close() {
peer_connection_->Close();
}
// AddIceCandidateObserver
NativeAddIceCandidateObserver::NativeAddIceCandidateObserver(
@@ -23,7 +23,7 @@ pub mod ffi {
}
#[derive(Debug)]
#[repr(u32)]
#[repr(i32)]
pub enum PeerConnectionState {
New,
Connecting,
@@ -34,7 +34,7 @@ pub mod ffi {
}
#[derive(Debug)]
#[repr(u32)]
#[repr(i32)]
pub enum SignalingState {
Stable,
HaveLocalOffer,
@@ -45,7 +45,7 @@ pub mod ffi {
}
#[derive(Debug)]
#[repr(u32)]
#[repr(i32)]
pub enum IceConnectionState {
IceConnectionNew,
IceConnectionChecking,
@@ -58,7 +58,7 @@ pub mod ffi {
}
#[derive(Debug)]
#[repr(u32)]
#[repr(i32)]
pub enum IceGatheringState {
IceGatheringNew,
IceGatheringGathering,
@@ -158,6 +158,14 @@ pub mod ffi {
observer: Pin<&mut NativeAddIceCandidateObserver>,
);
fn local_description(self: &PeerConnection) -> UniquePtr<SessionDescription>;
fn remote_description(self: &PeerConnection) -> UniquePtr<SessionDescription>;
fn signaling_state(self: &PeerConnection) -> SignalingState;
fn ice_gathering_state(self: &PeerConnection) -> IceGatheringState;
fn close(self: Pin<&mut PeerConnection>);
fn create_native_peer_connection_observer(
@@ -86,8 +86,13 @@ std::unique_ptr<NativeRTCConfiguration> create_rtc_configuration(
for (auto& url : item.urls) {
ice_server.urls.emplace_back(url.c_str());
}
rtc->servers.push_back(ice_server);
rtc->continual_gathering_policy =
static_cast<webrtc::PeerConnectionInterface::ContinualGatheringPolicy>(
conf.continual_gathering_policy);
rtc->type = static_cast<webrtc::PeerConnectionInterface::IceTransportsType>(
conf.ice_transport_type);
}
return rtc;
@@ -12,9 +12,27 @@ pub mod ffi {
pub password: String,
}
#[derive(Debug)]
#[repr(i32)]
pub enum ContinualGatheringPolicy {
GatherOnce,
GatherContinually,
}
#[derive(Debug)]
#[repr(i32)]
pub enum IceTransportsType {
None,
Relay,
NoHost,
All,
}
#[derive(Debug, Clone)]
pub struct RTCConfiguration {
pub ice_servers: Vec<ICEServer>,
pub continual_gathering_policy: ContinualGatheringPolicy,
pub ice_transport_type: IceTransportsType,
}
unsafe extern "C++" {
@@ -22,7 +40,7 @@ pub mod ffi {
type PeerConnection = crate::peer_connection::ffi::PeerConnection;
type NativePeerConnectionObserver =
crate::peer_connection::ffi::NativePeerConnectionObserver;
crate::peer_connection::ffi::NativePeerConnectionObserver;
type PeerConnectionFactory;
type NativeRTCConfiguration;
@@ -38,3 +56,6 @@ pub mod ffi {
) -> Result<UniquePtr<PeerConnection>>;
}
}
unsafe impl Send for ffi::PeerConnectionFactory {}
unsafe impl Sync for ffi::PeerConnectionFactory {}
@@ -7,7 +7,7 @@ use std::fmt::{Display, Formatter};
#[cxx::bridge(namespace = "livekit")]
pub mod ffi {
#[derive(Debug)]
#[repr(u32)]
#[repr(i32)]
pub enum RTCErrorType {
None,
UnsupportedOperation,
@@ -24,7 +24,7 @@ pub mod ffi {
}
#[derive(Debug)]
#[repr(u32)]
#[repr(i32)]
pub enum RTCErrorDetailType {
None,
DataChannelFailure,
@@ -10,3 +10,6 @@ pub mod ffi {
fn create_rtc_runtime() -> UniquePtr<RTCRuntime>;
}
}
unsafe impl Send for ffi::RTCRuntime {}
unsafe impl Sync for ffi::RTCRuntime {}
+8 -8
View File
@@ -128,14 +128,14 @@ impl Default for InternalDataChannelObserver {
#[derive(Debug)]
pub struct DataChannelInit {
#[deprecated]
reliable: bool,
ordered: bool,
max_retransmit_time: Option<i32>,
max_retransmits: Option<i32>,
protocol: String,
negotiated: bool,
id: i32,
priority: Option<Priority>,
pub reliable: bool,
pub ordered: bool,
pub max_retransmit_time: Option<i32>,
pub max_retransmits: Option<i32>,
pub protocol: String,
pub negotiated: bool,
pub id: i32,
pub priority: Option<Priority>,
}
impl Default for DataChannelInit {
+13
View File
@@ -2,6 +2,7 @@ use cxx::UniquePtr;
use libwebrtc_sys::jsep as sys_jsep;
// TODO Maybe we can replace that by a serialized IceCandidateInit
#[derive(Debug)]
pub struct IceCandidate {
cxx_handle: UniquePtr<sys_jsep::ffi::IceCandidate>,
@@ -17,6 +18,12 @@ impl IceCandidate {
}
}
impl ToString for IceCandidate {
fn to_string(&self) -> String {
self.cxx_handle.stringify()
}
}
#[derive(Debug)]
pub struct SessionDescription {
cxx_handle: UniquePtr<sys_jsep::ffi::SessionDescription>,
@@ -32,6 +39,12 @@ impl SessionDescription {
}
}
impl ToString for SessionDescription {
fn to_string(&self) -> String {
self.cxx_handle.stringify()
}
}
impl Clone for SessionDescription {
fn clone(&self) -> Self {
SessionDescription::new(self.cxx_handle.clone())
+37 -10
View File
@@ -51,7 +51,7 @@ impl PeerConnection {
}
}
pub async fn create_offer(&mut self) -> Result<SessionDescription, SdpError> {
pub async fn create_offer(&mut self, options: RTCOfferAnswerOptions) -> Result<SessionDescription, SdpError> {
let (tx, mut rx) = mpsc::channel(1);
let wrapper =
@@ -62,7 +62,7 @@ impl PeerConnection {
unsafe {
self.cxx_handle
.pin_mut()
.create_offer(native_wrapper.pin_mut(), RTCOfferAnswerOptions::default());
.create_offer(native_wrapper.pin_mut(), options);
}
match rx.recv().await {
@@ -71,7 +71,7 @@ impl PeerConnection {
}
}
pub async fn create_answer(&mut self) -> Result<SessionDescription, SdpError> {
pub async fn create_answer(&mut self, options: RTCOfferAnswerOptions) -> Result<SessionDescription, SdpError> {
let (tx, mut rx) = mpsc::channel(1);
let wrapper =
@@ -82,7 +82,7 @@ impl PeerConnection {
unsafe {
self.cxx_handle
.pin_mut()
.create_answer(native_wrapper.pin_mut(), RTCOfferAnswerOptions::default());
.create_answer(native_wrapper.pin_mut(), options);
}
match rx.recv().await {
@@ -154,6 +154,7 @@ impl PeerConnection {
}
}
// TODO(theomonnom) Use IceCandidateInit instead of IceCandidate
pub async fn add_ice_candidate(&mut self, candidate: IceCandidate) -> Result<(), SdpError> {
let (tx, mut rx) = mpsc::channel(1);
let observer = sys_pc::AddIceCandidateObserverWrapper::new(Box::new(move |error| {
@@ -172,6 +173,32 @@ impl PeerConnection {
}
}
pub fn local_description(&self) -> Option<SessionDescription> {
let local_description = self.cxx_handle.local_description();
if local_description.is_null() {
None
} else {
Some(SessionDescription::new(local_description))
}
}
pub fn remote_description(&self) -> Option<SessionDescription> {
let remote_description = self.cxx_handle.remote_description();
if remote_description.is_null() {
None
} else {
Some(SessionDescription::new(remote_description))
}
}
pub fn signaling_state(&self) -> SignalingState {
self.cxx_handle.signaling_state()
}
pub fn ice_gathering_state(&self) -> IceGatheringState {
self.cxx_handle.ice_gathering_state()
}
pub fn close(&mut self) {
self.cxx_handle.pin_mut().close();
}
@@ -344,16 +371,16 @@ 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>;
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>;
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>;
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>;
@@ -368,16 +395,16 @@ pub(crate) struct InternalObserver {
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>>>,
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>>>,
Arc<Mutex<Option<OnIceConnectionReceivingChangeHandler>>>,
on_ice_selected_candidate_pair_changed_handler:
Arc<Mutex<Option<OnIceSelectedCandidatePairChangedHandler>>>,
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>>>,
@@ -2,7 +2,9 @@ use cxx::UniquePtr;
use libwebrtc_sys::peer_connection as sys_pc;
use libwebrtc_sys::peer_connection_factory as sys_factory;
pub use sys_factory::ffi::{ICEServer, RTCConfiguration};
pub use sys_factory::ffi::{
ContinualGatheringPolicy, ICEServer, IceTransportsType, RTCConfiguration,
};
use crate::peer_connection::{InternalObserver, PeerConnection};
use crate::rtc_error::RTCError;
-1
View File
@@ -1,3 +1,2 @@
// TODO(theomonnom) Wrap the RTCError ffi so we can use Option(u16)
pub use libwebrtc_sys::rtc_error::ffi::RTCError;