Skip to content

Commit

Permalink
Close track FFI handles automatically when a FfiRoom is closed. (#539)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
typester authored Jan 6, 2025
1 parent e0846c2 commit 32a582c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions .nanpa/free-stream-handles-when-room-is-closed.kdl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
patch type="fixed" package="livekit-ffi" "Automatically close audio/video stream handles when the associated room is closed"
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions livekit-ffi/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl FfiHandle for Arc<Mutex<resampler::SoxResampler>> {}
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
Expand Down Expand Up @@ -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");

Expand All @@ -146,7 +147,7 @@ impl FfiServer {
}

for room in rooms {
room.close().await;
room.close(self).await;
}

// Drop all handles
Expand Down
2 changes: 1 addition & 1 deletion livekit-ffi/src/server/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn on_disconnect(
let ffi_room =
server.retrieve_handle::<room::FfiRoom>(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 {
Expand Down
10 changes: 9 additions & 1 deletion livekit-ffi/src/server/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 32a582c

Please sign in to comment.