Keep ownership of observers on the Rust side
This commit is contained in:
@@ -13,8 +13,8 @@ namespace livekit {
|
||||
|
||||
}
|
||||
|
||||
void DataChannel::register_observer(std::unique_ptr<NativeDataChannelObserver> observer) {
|
||||
data_channel_->RegisterObserver(observer.get());
|
||||
void DataChannel::register_observer(NativeDataChannelObserver &observer) {
|
||||
data_channel_->RegisterObserver(&observer);
|
||||
}
|
||||
|
||||
void DataChannel::unregister_observer() {
|
||||
@@ -65,7 +65,7 @@ namespace livekit {
|
||||
observer_->on_buffered_amount_change(sent_data_size);
|
||||
}
|
||||
|
||||
std::unique_ptr<NativeDataChannelObserver> create_native_peer_connection_observer(rust::Box<DataChannelObserverWrapper> observer){
|
||||
std::unique_ptr<NativeDataChannelObserver> create_native_data_channel_observer(rust::Box<DataChannelObserverWrapper> observer){
|
||||
return std::make_unique<NativeDataChannelObserver>(std::move(observer));
|
||||
}
|
||||
} // livekit
|
||||
@@ -60,7 +60,6 @@ pub mod ffi {
|
||||
type NativeDataChannelInit;
|
||||
type NativeDataChannelObserver;
|
||||
|
||||
|
||||
fn close(self: Pin<&mut DataChannel>);
|
||||
|
||||
fn create_data_channel_init(init: DataChannelInit) -> UniquePtr<NativeDataChannelInit>;
|
||||
|
||||
@@ -21,26 +21,24 @@ namespace livekit {
|
||||
return rtc_options;
|
||||
}
|
||||
|
||||
PeerConnection::PeerConnection(rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection,
|
||||
std::unique_ptr<NativePeerConnectionObserver> observer) : peer_connection_(
|
||||
std::move(peer_connection)), observer_(std::move(observer)) {
|
||||
PeerConnection::PeerConnection(rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection) : peer_connection_(std::move(peer_connection)) {
|
||||
|
||||
}
|
||||
|
||||
void PeerConnection::create_offer(std::unique_ptr<NativeCreateSdpObserverHandle> observer_handle, RTCOfferAnswerOptions options) {
|
||||
peer_connection_->CreateOffer(observer_handle->observer.get(), toNativeOfferAnswerOptions(options));
|
||||
void PeerConnection::create_offer(NativeCreateSdpObserverHandle &observer_handle, RTCOfferAnswerOptions options) {
|
||||
peer_connection_->CreateOffer(observer_handle.observer.get(), toNativeOfferAnswerOptions(options));
|
||||
}
|
||||
|
||||
void PeerConnection::create_answer(std::unique_ptr<NativeCreateSdpObserverHandle> observer_handle, RTCOfferAnswerOptions options) {
|
||||
peer_connection_->CreateAnswer(observer_handle->observer.get(), toNativeOfferAnswerOptions(options));
|
||||
void PeerConnection::create_answer(NativeCreateSdpObserverHandle &observer_handle, RTCOfferAnswerOptions options) {
|
||||
peer_connection_->CreateAnswer(observer_handle.observer.get(), toNativeOfferAnswerOptions(options));
|
||||
}
|
||||
|
||||
void PeerConnection::set_local_description(std::unique_ptr<SessionDescription> desc, std::unique_ptr<NativeSetLocalSdpObserverHandle> observer) {
|
||||
peer_connection_->SetLocalDescription(desc->clone()->release(), observer->observer);
|
||||
void PeerConnection::set_local_description(std::unique_ptr<SessionDescription> desc, NativeSetLocalSdpObserverHandle &observer) {
|
||||
peer_connection_->SetLocalDescription(desc->clone()->release(), observer.observer);
|
||||
}
|
||||
|
||||
void PeerConnection::set_remote_description(std::unique_ptr<SessionDescription> desc, std::unique_ptr<NativeSetRemoteSdpObserverHandle> observer) {
|
||||
peer_connection_->SetRemoteDescription(desc->clone()->release(), observer->observer);
|
||||
void PeerConnection::set_remote_description(std::unique_ptr<SessionDescription> desc, NativeSetRemoteSdpObserverHandle &observer) {
|
||||
peer_connection_->SetRemoteDescription(desc->clone()->release(), observer.observer);
|
||||
}
|
||||
|
||||
std::unique_ptr<DataChannel> PeerConnection::create_data_channel(rust::String label, std::unique_ptr<NativeDataChannelInit> init) {
|
||||
|
||||
@@ -109,26 +109,38 @@ pub mod ffi {
|
||||
type NativePeerConnectionObserver;
|
||||
type PeerConnection;
|
||||
|
||||
fn create_offer(
|
||||
/// SAFETY
|
||||
/// The observer must live as long as the operation ends
|
||||
unsafe fn create_offer(
|
||||
self: Pin<&mut PeerConnection>,
|
||||
observer: UniquePtr<NativeCreateSdpObserverHandle>,
|
||||
observer: Pin<&mut NativeCreateSdpObserverHandle>,
|
||||
options: RTCOfferAnswerOptions,
|
||||
);
|
||||
fn create_answer(
|
||||
|
||||
/// SAFETY
|
||||
/// The observer must live as long as the operation ends
|
||||
unsafe fn create_answer(
|
||||
self: Pin<&mut PeerConnection>,
|
||||
observer: UniquePtr<NativeCreateSdpObserverHandle>,
|
||||
observer: Pin<&mut NativeCreateSdpObserverHandle>,
|
||||
options: RTCOfferAnswerOptions,
|
||||
);
|
||||
fn set_local_description(
|
||||
|
||||
/// SAFETY
|
||||
/// The observer must live as long as the operation ends
|
||||
unsafe fn set_local_description(
|
||||
self: Pin<&mut PeerConnection>,
|
||||
desc: UniquePtr<SessionDescription>,
|
||||
observer: UniquePtr<NativeSetLocalSdpObserverHandle>,
|
||||
observer: Pin<&mut NativeSetLocalSdpObserverHandle>,
|
||||
);
|
||||
fn set_remote_description(
|
||||
|
||||
/// SAFETY
|
||||
/// The observer must live as long as the operation ends
|
||||
unsafe fn set_remote_description(
|
||||
self: Pin<&mut PeerConnection>,
|
||||
desc: UniquePtr<SessionDescription>,
|
||||
observer: UniquePtr<NativeSetRemoteSdpObserverHandle>,
|
||||
observer: Pin<&mut NativeSetRemoteSdpObserverHandle>,
|
||||
);
|
||||
|
||||
fn close(self: Pin<&mut PeerConnection>);
|
||||
|
||||
fn create_native_peer_connection_observer(
|
||||
@@ -212,10 +224,7 @@ pub mod ffi {
|
||||
self: &PeerConnectionObserverWrapper,
|
||||
receiver: UniquePtr<RtpReceiver>,
|
||||
);
|
||||
unsafe fn on_interesting_usage(
|
||||
self: &PeerConnectionObserverWrapper,
|
||||
usage_pattern: i32,
|
||||
);
|
||||
unsafe fn on_interesting_usage(self: &PeerConnectionObserverWrapper, usage_pattern: i32);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -358,10 +367,7 @@ impl PeerConnectionObserverWrapper {
|
||||
(*self.observer).on_ice_connection_receiving_change(receiving);
|
||||
}
|
||||
|
||||
unsafe fn on_ice_selected_candidate_pair_changed(
|
||||
&self,
|
||||
event: ffi::CandidatePairChangeEvent,
|
||||
) {
|
||||
unsafe fn on_ice_selected_candidate_pair_changed(&self, event: ffi::CandidatePairChangeEvent) {
|
||||
(*self.observer).on_ice_selected_candidate_pair_changed(event);
|
||||
}
|
||||
|
||||
|
||||
@@ -53,15 +53,15 @@ namespace livekit{
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<PeerConnection> PeerConnectionFactory::create_peer_connection(std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration> config, std::unique_ptr<NativePeerConnectionObserver> observer) const {
|
||||
webrtc::PeerConnectionDependencies deps{observer.get()};
|
||||
std::unique_ptr<PeerConnection> PeerConnectionFactory::create_peer_connection(std::unique_ptr<webrtc::PeerConnectionInterface::RTCConfiguration> config, NativePeerConnectionObserver &observer) const {
|
||||
webrtc::PeerConnectionDependencies deps{&observer};
|
||||
auto result = peer_factory_->CreatePeerConnectionOrError(*config, std::move(deps));
|
||||
|
||||
if(!result.ok()) {
|
||||
throw std::runtime_error(serialize_error(to_error(result.error())));
|
||||
}
|
||||
|
||||
return std::make_unique<PeerConnection>(result.value(), std::move(observer));
|
||||
return std::make_unique<PeerConnection>(result.value());
|
||||
}
|
||||
|
||||
std::unique_ptr<PeerConnectionFactory> create_peer_connection_factory() {
|
||||
|
||||
@@ -44,10 +44,12 @@ pub mod ffi {
|
||||
fn create_peer_connection_factory() -> UniquePtr<PeerConnectionFactory>;
|
||||
fn create_rtc_configuration(conf: RTCConfiguration) -> UniquePtr<NativeRTCConfiguration>;
|
||||
|
||||
fn create_peer_connection(
|
||||
/// SAFETY
|
||||
/// The observer must live as long as the PeerConnection
|
||||
unsafe fn create_peer_connection(
|
||||
self: &PeerConnectionFactory,
|
||||
config: UniquePtr<NativeRTCConfiguration>,
|
||||
observer: UniquePtr<NativePeerConnectionObserver>,
|
||||
observer: Pin<&mut NativePeerConnectionObserver>,
|
||||
) -> Result<UniquePtr<PeerConnection>>;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user