Started Room, publisher negotiation, add RTCRuntime dependencies across webrtc instances

This commit is contained in:
Théo Monnom
2022-09-29 21:34:30 +02:00
parent 32ac92b171
commit 0d34e5a48e
29 changed files with 707 additions and 185 deletions
@@ -11,8 +11,9 @@
namespace livekit {
DataChannel::DataChannel(
std::shared_ptr<RTCRuntime> rtc_runtime,
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel)
: data_channel_(std::move(data_channel)) {}
: rtc_runtime_(std::move(rtc_runtime)), data_channel_(std::move(data_channel)) {}
void DataChannel::register_observer(NativeDataChannelObserver& observer) {
data_channel_->RegisterObserver(&observer);
@@ -31,6 +32,10 @@ rust::String DataChannel::label() const {
return data_channel_->label();
}
DataState DataChannel::state() const {
return static_cast<DataState>(data_channel_->state());
}
void DataChannel::close() {
return data_channel_->Close();
}
@@ -69,6 +69,7 @@ pub mod ffi {
fn unregister_observer(self: Pin<&mut DataChannel>);
fn send(self: Pin<&mut DataChannel>, data: &DataBuffer) -> bool;
fn label(self: &DataChannel) -> String;
fn state(self: &DataChannel) -> DataState;
fn close(self: Pin<&mut DataChannel>);
fn create_data_channel_init(init: DataChannelInit) -> UniquePtr<NativeDataChannelInit>;
@@ -12,7 +12,7 @@
namespace livekit {
const std::string& serialize_sdp_error(webrtc::SdpParseError error) {
std::string serialize_sdp_error(webrtc::SdpParseError error) {
std::stringstream ss;
ss << std::hex << std::setfill('0');
ss << std::setw(8) << (uint32_t)error.line.length();
@@ -25,6 +25,18 @@ IceCandidate::IceCandidate(
std::unique_ptr<webrtc::IceCandidateInterface> ice_candidate)
: ice_candidate_(std::move(ice_candidate)) {}
rust::String IceCandidate::sdp_mid() const {
return ice_candidate_->sdp_mid();
}
int IceCandidate::sdp_mline_index() const {
return ice_candidate_->sdp_mline_index();
}
rust::String IceCandidate::candidate() const {
return stringify();
}
rust::String IceCandidate::stringify() const {
std::string str;
ice_candidate_->ToString(&str);
@@ -49,6 +49,9 @@ pub mod ffi {
type NativeSetLocalSdpObserverHandle;
type NativeSetRemoteSdpObserverHandle;
fn sdp_mid(self: &IceCandidate) -> String;
fn sdp_mline_index(self: &IceCandidate) -> i32;
fn candidate(self: &IceCandidate) -> String;
fn stringify(self: &IceCandidate) -> String;
fn stringify(self: &SessionDescription) -> String;
@@ -24,8 +24,10 @@ toNativeOfferAnswerOptions(const RTCOfferAnswerOptions& options) {
}
PeerConnection::PeerConnection(
std::shared_ptr<RTCRuntime> rtc_runtime,
rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection)
: peer_connection_(std::move(peer_connection)) {}
: rtc_runtime_(std::move(rtc_runtime)),
peer_connection_(std::move(peer_connection)) {}
void PeerConnection::create_offer(
NativeCreateSdpObserverHandle& observer_handle,
@@ -65,7 +67,7 @@ std::unique_ptr<DataChannel> PeerConnection::create_data_channel(
throw std::runtime_error(serialize_error(to_error(result.error())));
}
return std::make_unique<DataChannel>(result.value());
return std::make_unique<DataChannel>(rtc_runtime_, result.value());
}
void PeerConnection::add_ice_candidate(
@@ -97,14 +99,19 @@ SignalingState PeerConnection::signaling_state() const {
}
IceGatheringState PeerConnection::ice_gathering_state() const {
return static_cast<IceGatheringState>(peer_connection_->ice_gathering_state());
return static_cast<IceGatheringState>(
peer_connection_->ice_gathering_state());
}
IceConnectionState PeerConnection::ice_connection_state() const {
return static_cast<IceConnectionState>(
peer_connection_->ice_connection_state());
}
void PeerConnection::close() {
peer_connection_->Close();
}
// AddIceCandidateObserver
NativeAddIceCandidateObserver::NativeAddIceCandidateObserver(
@@ -124,8 +131,9 @@ create_native_add_ice_candidate_observer(
// PeerConnectionObserver
NativePeerConnectionObserver::NativePeerConnectionObserver(
std::shared_ptr<RTCRuntime> rtc_runtime,
rust::Box<PeerConnectionObserverWrapper> observer)
: observer_(std::move(observer)) {}
: rtc_runtime_(std::move(rtc_runtime)), observer_(std::move(observer)) {}
void NativePeerConnectionObserver::OnSignalingChange(
webrtc::PeerConnectionInterface::SignalingState new_state) {
@@ -144,7 +152,7 @@ void NativePeerConnectionObserver::OnRemoveStream(
void NativePeerConnectionObserver::OnDataChannel(
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
observer_->on_data_channel(std::make_unique<DataChannel>(data_channel));
observer_->on_data_channel(std::make_unique<DataChannel>(rtc_runtime_, data_channel));
}
void NativePeerConnectionObserver::OnRenegotiationNeeded() {
@@ -255,7 +263,9 @@ void NativePeerConnectionObserver::OnInterestingUsage(int usage_pattern) {
std::unique_ptr<NativePeerConnectionObserver>
create_native_peer_connection_observer(
std::shared_ptr<RTCRuntime> rtc_runtime,
rust::Box<PeerConnectionObserverWrapper> observer) {
return std::make_unique<NativePeerConnectionObserver>(std::move(observer));
return std::make_unique<NativePeerConnectionObserver>(rtc_runtime,
std::move(observer));
}
} // namespace livekit
@@ -109,6 +109,7 @@ pub mod ffi {
type NativeSetRemoteSdpObserverHandle = crate::jsep::ffi::NativeSetRemoteSdpObserverHandle;
type NativeDataChannelInit = crate::data_channel::ffi::NativeDataChannelInit;
type SessionDescription = crate::jsep::ffi::SessionDescription;
type RTCRuntime = crate::webrtc::ffi::RTCRuntime;
type NativeAddIceCandidateObserver;
type NativePeerConnectionObserver;
@@ -166,9 +167,12 @@ pub mod ffi {
fn ice_gathering_state(self: &PeerConnection) -> IceGatheringState;
fn ice_connection_state(self: &PeerConnection) -> IceConnectionState;
fn close(self: Pin<&mut PeerConnection>);
fn create_native_peer_connection_observer(
rtc_runtime: SharedPtr<RTCRuntime>,
observer: Box<PeerConnectionObserverWrapper>,
) -> UniquePtr<NativePeerConnectionObserver>;
@@ -4,6 +4,8 @@
#include "livekit/peer_connection_factory.h"
#include <utility>
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
#include "api/audio_codecs/builtin_audio_encoder_factory.h"
#include "api/rtc_event_log/rtc_event_log_factory.h"
@@ -16,24 +18,15 @@
namespace livekit {
PeerConnectionFactory::PeerConnectionFactory() {
rtc::LogMessage::LogToDebug(rtc::LS_INFO);
PeerConnectionFactory::PeerConnectionFactory(
std::shared_ptr<RTCRuntime> rtc_runtime)
: rtc_runtime_(std::move(rtc_runtime)) {
RTC_LOG(LS_INFO) << "PeerConnectionFactory::PeerConnectionFactory()";
network_thread_ = rtc::Thread::CreateWithSocketServer();
network_thread_->SetName("network_thread", &network_thread_);
network_thread_->Start();
worker_thread_ = rtc::Thread::Create();
worker_thread_->SetName("worker_thread", &worker_thread_);
worker_thread_->Start();
signaling_thread_ = rtc::Thread::Create();
signaling_thread_->SetName("signaling_thread", &signaling_thread_);
signaling_thread_->Start();
webrtc::PeerConnectionFactoryDependencies dependencies;
dependencies.network_thread = network_thread_.get();
dependencies.worker_thread = worker_thread_.get();
dependencies.signaling_thread = signaling_thread_.get();
dependencies.network_thread = rtc_runtime_->network_thread();
dependencies.worker_thread = rtc_runtime_->worker_thread();
dependencies.signaling_thread = rtc_runtime_->signaling_thread();
dependencies.task_queue_factory = webrtc::CreateDefaultTaskQueueFactory();
dependencies.event_log_factory = std::make_unique<webrtc::RtcEventLogFactory>(
dependencies.task_queue_factory.get());
@@ -56,6 +49,10 @@ PeerConnectionFactory::PeerConnectionFactory() {
}
}
PeerConnectionFactory::~PeerConnectionFactory() {
RTC_LOG(LS_INFO) << "PeerConnectionFactory::~PeerConnectionFactory()";
}
std::unique_ptr<PeerConnection> PeerConnectionFactory::create_peer_connection(
std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration> config,
NativePeerConnectionObserver& observer) const {
@@ -67,11 +64,11 @@ std::unique_ptr<PeerConnection> PeerConnectionFactory::create_peer_connection(
throw std::runtime_error(serialize_error(to_error(result.error())));
}
return std::make_unique<PeerConnection>(result.value());
return std::make_unique<PeerConnection>(rtc_runtime_, result.value());
}
std::unique_ptr<PeerConnectionFactory> create_peer_connection_factory() {
return std::make_unique<PeerConnectionFactory>();
std::unique_ptr<PeerConnectionFactory> create_peer_connection_factory(std::shared_ptr<RTCRuntime> rtc_runtime) {
return std::make_unique<PeerConnectionFactory>(std::move(rtc_runtime));
}
std::unique_ptr<NativeRTCConfiguration> create_rtc_configuration(
@@ -43,8 +43,9 @@ pub mod ffi {
crate::peer_connection::ffi::NativePeerConnectionObserver;
type PeerConnectionFactory;
type NativeRTCConfiguration;
type RTCRuntime = crate::webrtc::ffi::RTCRuntime;
fn create_peer_connection_factory() -> UniquePtr<PeerConnectionFactory>;
fn create_peer_connection_factory(runtime: SharedPtr<RTCRuntime>) -> UniquePtr<PeerConnectionFactory>;
fn create_rtc_configuration(conf: RTCConfiguration) -> UniquePtr<NativeRTCConfiguration>;
/// SAFETY
@@ -8,8 +8,19 @@
namespace livekit {
RTCRuntime::RTCRuntime() {
rtc::LogMessage::LogToDebug(rtc::LS_INFO);
RTC_LOG(LS_INFO) << "RTCRuntime()";
RTC_CHECK(rtc::InitializeSSL()) << "Failed to InitializeSSL()";
network_thread_ = rtc::Thread::CreateWithSocketServer();
network_thread_->SetName("network_thread", &network_thread_);
network_thread_->Start();
worker_thread_ = rtc::Thread::Create();
worker_thread_->SetName("worker_thread", &worker_thread_);
worker_thread_->Start();
signaling_thread_ = rtc::Thread::Create();
signaling_thread_->SetName("signaling_thread", &signaling_thread_);
signaling_thread_->Start();
}
RTCRuntime::~RTCRuntime() {
@@ -17,7 +28,19 @@ RTCRuntime::~RTCRuntime() {
RTC_CHECK(rtc::CleanupSSL()) << "Failed to CleanupSSL()";
}
std::unique_ptr<RTCRuntime> create_rtc_runtime() {
return std::make_unique<RTCRuntime>();
rtc::Thread* RTCRuntime::network_thread() const {
return network_thread_.get();
}
rtc::Thread* RTCRuntime::worker_thread() const {
return worker_thread_.get();
}
rtc::Thread* RTCRuntime::signaling_thread() const {
return signaling_thread_.get();
}
std::shared_ptr<RTCRuntime> create_rtc_runtime() {
return std::make_shared<RTCRuntime>();
}
} // namespace livekit
@@ -7,7 +7,7 @@ pub mod ffi {
type RTCRuntime;
fn create_rtc_runtime() -> UniquePtr<RTCRuntime>;
fn create_rtc_runtime() -> SharedPtr<RTCRuntime>;
}
}