From 9cf39b1da6fe8024f07298ee19a5e2db3163faea Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 18 Oct 2022 14:50:03 +0200 Subject: [PATCH] Disconnect from live-kit `Room` on drop --- crates/call/src/room.rs | 9 ++++++++- .../Sources/LiveKitBridge/LiveKitBridge.swift | 6 ++++++ crates/live_kit_client/src/live_kit_client.rs | 6 +++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/call/src/room.rs b/crates/call/src/room.rs index 909fe4d977..1bd86944bf 100644 --- a/crates/call/src/room.rs +++ b/crates/call/src/room.rs @@ -50,7 +50,9 @@ impl Entity for Room { type Event = Event; fn release(&mut self, _: &mut MutableAppContext) { - self.client.send(proto::LeaveRoom { id: self.id }).log_err(); + if self.status.is_online() { + self.client.send(proto::LeaveRoom { id: self.id }).log_err(); + } } } @@ -210,6 +212,7 @@ impl Room { self.pending_participants.clear(); self.participant_user_ids.clear(); self.subscriptions.clear(); + self.live_kit_room.take(); self.client.send(proto::LeaveRoom { id: self.id })?; Ok(()) } @@ -630,4 +633,8 @@ impl RoomStatus { pub fn is_offline(&self) -> bool { matches!(self, RoomStatus::Offline) } + + pub fn is_online(&self) -> bool { + matches!(self, RoomStatus::Online) + } } diff --git a/crates/live_kit_client/LiveKitBridge/Sources/LiveKitBridge/LiveKitBridge.swift b/crates/live_kit_client/LiveKitBridge/Sources/LiveKitBridge/LiveKitBridge.swift index 1c49109fc5..dadc2fdb10 100644 --- a/crates/live_kit_client/LiveKitBridge/Sources/LiveKitBridge/LiveKitBridge.swift +++ b/crates/live_kit_client/LiveKitBridge/Sources/LiveKitBridge/LiveKitBridge.swift @@ -83,6 +83,12 @@ public func LKRoomConnect(room: UnsafeRawPointer, url: CFString, token: CFString } } +@_cdecl("LKRoomDisconnect") +public func LKRoomDisconnect(room: UnsafeRawPointer) { + let room = Unmanaged.fromOpaque(room).takeUnretainedValue() + room.disconnect() +} + @_cdecl("LKRoomPublishVideoTrack") public func LKRoomPublishVideoTrack(room: UnsafeRawPointer, track: UnsafeRawPointer, callback: @escaping @convention(c) (UnsafeRawPointer, CFString?) -> Void, callback_data: UnsafeRawPointer) { let room = Unmanaged.fromOpaque(room).takeUnretainedValue() diff --git a/crates/live_kit_client/src/live_kit_client.rs b/crates/live_kit_client/src/live_kit_client.rs index af46fbb2e0..07075dcee8 100644 --- a/crates/live_kit_client/src/live_kit_client.rs +++ b/crates/live_kit_client/src/live_kit_client.rs @@ -43,6 +43,7 @@ extern "C" { callback: extern "C" fn(*mut c_void, CFStringRef), callback_data: *mut c_void, ); + fn LKRoomDisconnect(room: *const c_void); fn LKRoomPublishVideoTrack( room: *const c_void, track: *const c_void, @@ -195,7 +196,10 @@ impl Room { impl Drop for Room { fn drop(&mut self) { - unsafe { LKRelease(self.native_room) } + unsafe { + LKRoomDisconnect(self.native_room); + LKRelease(self.native_room); + } } }