From 32a582c09f86a32d52928a4d438f3c5eb43ded88 Mon Sep 17 00:00:00 2001 From: Daisuke Murase Date: Mon, 6 Jan 2025 15:39:29 -0800 Subject: [PATCH] Close track FFI handles automatically when a FfiRoom is closed. (#539) * trying to dispose associated stream handles when a room is closed * update Cargo.lock * remove debug log * store empty handle for existing ffi clients * impl FfiHandle for () * add nanpa changeset --- .nanpa/free-stream-handles-when-room-is-closed.kdl | 1 + Cargo.lock | 14 +++++++------- livekit-ffi/src/server/mod.rs | 5 +++-- livekit-ffi/src/server/requests.rs | 2 +- livekit-ffi/src/server/room.rs | 10 +++++++++- 5 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 .nanpa/free-stream-handles-when-room-is-closed.kdl diff --git a/.nanpa/free-stream-handles-when-room-is-closed.kdl b/.nanpa/free-stream-handles-when-room-is-closed.kdl new file mode 100644 index 00000000..52784843 --- /dev/null +++ b/.nanpa/free-stream-handles-when-room-is-closed.kdl @@ -0,0 +1 @@ +patch type="fixed" package="livekit-ffi" "Automatically close audio/video stream handles when the associated room is closed" \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index e915bba8..7fc649ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1321,7 +1321,7 @@ dependencies = [ [[package]] name = "imgproc" -version = "0.3.11" +version = "0.3.12" dependencies = [ "yuv-sys", ] @@ -1537,7 +1537,7 @@ dependencies = [ [[package]] name = "libwebrtc" -version = "0.3.7" +version = "0.3.8" dependencies = [ "cxx", "env_logger", @@ -1593,7 +1593,7 @@ checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" [[package]] name = "livekit" -version = "0.7.0" +version = "0.7.1" dependencies = [ "chrono", "futures-util", @@ -1641,7 +1641,7 @@ dependencies = [ [[package]] name = "livekit-ffi" -version = "0.12.3" +version = "0.12.4" dependencies = [ "console-subscriber", "dashmap", @@ -3293,7 +3293,7 @@ checksum = "1778a42e8b3b90bff8d0f5032bf22250792889a5cdc752aa0020c84abe3aaf10" [[package]] name = "webrtc-sys" -version = "0.3.5" +version = "0.3.6" dependencies = [ "cc", "cxx", @@ -3306,7 +3306,7 @@ dependencies = [ [[package]] name = "webrtc-sys-build" -version = "0.3.5" +version = "0.3.6" dependencies = [ "fs2", "regex", @@ -3578,7 +3578,7 @@ dependencies = [ [[package]] name = "yuv-sys" -version = "0.3.6" +version = "0.3.7" dependencies = [ "bindgen", "cc", diff --git a/livekit-ffi/src/server/mod.rs b/livekit-ffi/src/server/mod.rs index cd015b5f..9c0f7dc8 100644 --- a/livekit-ffi/src/server/mod.rs +++ b/livekit-ffi/src/server/mod.rs @@ -70,6 +70,7 @@ impl FfiHandle for Arc> {} impl FfiHandle for AudioFrame<'static> {} impl FfiHandle for BoxVideoBuffer {} impl FfiHandle for Box<[u8]> {} +impl FfiHandle for () {} pub struct FfiServer { /// Store all Ffi handles inside an HashMap, if this isn't efficient enough @@ -133,7 +134,7 @@ impl FfiServer { log::info!("initializing ffi server v{}", env!("CARGO_PKG_VERSION")); // TODO: Move this log } - pub async fn dispose(&self) { + pub async fn dispose(&'static self) { self.logger.set_capture_logs(false); log::info!("disposing ffi server"); @@ -146,7 +147,7 @@ impl FfiServer { } for room in rooms { - room.close().await; + room.close(self).await; } // Drop all handles diff --git a/livekit-ffi/src/server/requests.rs b/livekit-ffi/src/server/requests.rs index f14ab0d7..32740289 100644 --- a/livekit-ffi/src/server/requests.rs +++ b/livekit-ffi/src/server/requests.rs @@ -67,7 +67,7 @@ fn on_disconnect( let ffi_room = server.retrieve_handle::(disconnect.room_handle).unwrap().clone(); - ffi_room.close().await; + ffi_room.close(server).await; let _ = server.send_event(proto::ffi_event::Message::Disconnect(proto::DisconnectCallback { diff --git a/livekit-ffi/src/server/room.rs b/livekit-ffi/src/server/room.rs index ca0d26ac..fe6be411 100644 --- a/livekit-ffi/src/server/room.rs +++ b/livekit-ffi/src/server/room.rs @@ -266,7 +266,15 @@ impl FfiRoom { } /// Close the room and stop the tasks - pub async fn close(&self) { + pub async fn close(&self, server: &'static FfiServer) { + // drop associated track handles + for (_, &handle) in self.inner.track_handle_lookup.lock().iter() { + if server.drop_handle(handle) { + // Store an empty handle for the FFI client that assumes a handle exists for this id. + server.store_handle(handle, ()); + } + } + let _ = self.inner.room.close().await; let handle = self.handle.lock().await.take();