Repository navigation
Conversation
Changeset ✓This PR includes a changeset covering all affected packages:
|
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at |
There was a problem hiding this comment.
Note to reviewers: I took all livekit-ffi/src/core_modules/data_track/xxx.rs files and split each out into two versions:
livekit-ffi/src/core_modules/data_track_manager/xxx.rscontains thecore-modules-only parts of the change - so the data track managers and associated infrastructurelivekit-ffi/src/data_track/xxx.rscontains all the infrastructure that will be used both inroom-apisandcore-modules, which includes "record" type structs likeDataTrackInfo, higher level "object" type structs likeLocalDataTrack, and some utilities.
|
|
||
| /// Protobuf-driven tests for data tracks, the next types to be migrated. | ||
| /// | ||
| /// Unlike the resampler, a data track only exists inside a connected room, so | ||
| /// these run against a LiveKit server (`livekit-server --dev` with | ||
| /// `enable_data_tracks: true`), the same as the `livekit` crate's e2e tests. | ||
| #[cfg(feature = "__lk-e2e-test")] | ||
| pub(crate) mod data_track { | ||
| use super::request; | ||
| use crate::{ | ||
| proto, | ||
| server::{requests::handle_request, FfiConfig}, | ||
| FfiHandleId, FFI_SERVER, | ||
| }; |
There was a problem hiding this comment.
Note to reviewers: I've added some livekit-ffi legacy ffi data track e2e tests here.
Note that these are the majority of the diff. Without these, the diff would be about ~400 lines or so.
I have some e2e tests which exercise the full room apis uniffi interface locally as well, but I'm unsure how thorough those should really be. We already have pretty good test coverage of data tracks as a feature, and the sox resampler tests exercise the shared legacy fii <-> uniffi conversion paths. The uniffi wrapper layer is pretty thin and so I'm not currently convinced that it warrants additional test coverage. If you disagree though, let me know and I can push them up.
| } | ||
|
|
||
| impl FfiHandle for FfiLocalDataTrack {} | ||
| impl FfiHandle for FfiRemoteDataTrack {} |
There was a problem hiding this comment.
Note to reviewers: I think all the changes in this file are the part I am least confident on and could use the most eyes from others.
There was a problem hiding this comment.
Note
Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.
Devin Review found 2 potential issues.
1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
| /// | ||
| /// Not named `data_track`: the crate root republishes this module with a glob, and the root's own | ||
| /// `data_track` would silently shadow it. | ||
| pub mod data_track_manager; |
There was a problem hiding this comment.
🟡 Core-module delegate API paths disappear
Existing Rust consumers of data_track::local::LocalDataTrackManagerDelegate lose that path when the manager moves. The remote delegate and signal-response error paths also disappear, breaking source compatibility.
Learn more
The old public data_track tree included the manager delegate traits and HandleSignalResponseError, in addition to track objects. Renaming that tree removes those paths from the exported Rust API even though the corresponding UniFFI names remain available. The new root-level data_track tree only includes shared track objects, so a previous Rust import of a delegate trait no longer resolves.
Example: A downstream implementation importing livekit_ffi::data_track::remote::RemoteDataTrackManagerDelegate must change its import after this release, despite not adopting any new API.
Recommended fix: Preserve the old public paths with re-exports in data_track::local, data_track::remote, and data_track::common under core-modules, or explicitly document the breaking API change if retaining them is impossible.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
IMO, this is fine, but I should probably make it a minor release - done in 049dfc0
|
In your code bottom example in the PR description, the higher level python classes |
@MaxHeimbrock To clarify - yes, these high level classes would still exist in each downstream client sdk and written in that client sdk's higher level language. These would then very thinly wrap objects like |
Ok, so if I read this correctly, most of the higher logic will be generated by uniffi, but we still need to hand-write a thin wrapper ourselves? Or will that all be working out of the box with the uniffi generated code? I think it would be best for me to try this myself, using Unity as the example. I would check out your branch, build the ffi library, but can now generate uniffi C# code by pointing the bindgen at this library now. Then I can import that C# code into the LiveKit SDK and write code similar to yours, acquiring the handle via FFI but using it in the uniffi classes after calling Am I missing something? |
|
Where is |
Already available in the FFI language sdks. For example, in the unity sdk, this would be the ffi handle id that FFIHandle wraps. |
Got it, then if I find time I will try the flow from the description in C# |
| let _ = send_event(proto::DataTrackPublished { track }.into()); | ||
| let info = track.info().clone(); | ||
| let publisher_identity = track.publisher_identity().to_string(); | ||
| let track = Arc::new(data_track::RemoteDataTrack::new(track)); |
There was a problem hiding this comment.
suggestion: Need to look into this more closely to see if this can be avoided in a UniFFI context, but data track objects generally shouldn't be wrapped in Arc and rather should just be cloned directly (Arc is already used internally).
There was a problem hiding this comment.
I think this is another case where the naming ended up being unintentionally confusing - data_track::RemoteDataTrack is the ffi data track type, not the not the livekit version. So this is needed, yes.
To make this less confusing, I have renamed it to data_track_ffi in 891e976. We keep running into this issue though so I wonder if there's a more structural way you can think to address this. Maybe aim to fully qualify each ffi struct usage with a livekit_ffi::xxx::yyy::MyStruct type of pattern?
Protobuf-driven e2e tests for data tracks, in the style of the resampler's migration tests: publish, subscribe and push, unpublish ending the stream, dropping the local handle unpublishing, duplicate-name errors and handle type checks. They need a server, so they sit behind a new `__lk-e2e-test` feature that the workspace-wide e2e job already passes.
… both surfaces Both now carry their `uniffi` feature unconditionally rather than only under `core-modules`, so the data track objects can be exported by either surface. In room-apis these are the crate instances `livekit` already pulls in.
LocalDataTrack, RemoteDataTrack, DataTrackStream and the records they use (DataTrackFrame, DataTrackInfo, DataTrackSchemaId, DataTrackOptions, DataTrackSubscribeOptions, RemoteDataTrackPipelineOptions) move verbatim to a top-level `data_track` module that both surfaces compile. The `Bytes` remote-type registration they depend on moves to the crate root with them. The managers, their delegates and the signal-response helpers stay in core-modules: they exist only for SDKs that run their own room.
…manager The crate root republishes core-modules with a glob, and the new top-level `data_track` silently shadowed the core-modules module of the same name, leaving the managers with no public path. What remains there is the managers, so name it for them.
With the data track objects exported by room-apis, livekit_ffi.py now imports the livekit_common and livekit_datatrack modules generated beside it. UniFFI defaults to relative imports, which only work inside a package, so the documented `import livekit_ffi` from a flat directory broke. Configure absolute imports, and have CI import the bindings that way. Generated bindings for core-modules are unchanged by the data track move apart from item order, checksums and one doc comment.
LocalDataTrack and RemoteDataTrack become named structs with a `handle_id: OnceLock<FfiHandleId>` field that only exists in room-apis builds, the slot `migrate_from_ffi!` expects, as on SoxResampler. A `new` constructor keeps the cfg out of callers; core-modules' managers use it.
…and uniffi In room-apis, LocalDataTrack and RemoteDataTrack gain the `migrate_from_ffi!` methods (`ffi_handle_id`, `from_ffi_handle_id`, `take_ffi_handle_id`), so a track can cross between the two FFI surfaces like SoxResampler. Core-modules builds have no handle map and are unaffected.
…ffi objects The room-apis request handlers now keep each track as an Arc of the shared LocalDataTrack/RemoteDataTrack, published through `ffi_handle_id()` like SoxResampler, instead of a separate FfiLocalDataTrack/FfiRemoteDataTrack wrapper. A handle the room hands out can therefore be picked up over uniffi with `from_ffi_handle_id`, and the two sides co-own the track: it is unpublished on drop only once neither holds it. The protobuf request surface is unchanged; the handlers become `_ffi` methods on the shared types. Data track streams stay independent per surface.
…rfaces A local data track unpublishes when its last owner drops it. Once a C ABI track handle has been taken up over UniFFI, the handle map and the UniFFI side co-own the track, so dropping the handle alone no longer unpublishes it. Say so on the proto handles and where the handle map stores the tracks, and add the livekit-ffi changeset for the data track work.
The Python SDK consumes the bindings from inside a package, where UniFFI's default relative imports work. Drop the external_packages overrides and the flat-import smoke test; CI keeps asserting the data track objects are present.
…quest handlers The handlers in requests.rs, and the call sites that hand out track handles, now call the shared tracks directly instead of going through `_ffi` methods on them. Setting up a legacy subscription moves to FfiDataTrackStream::subscribe, beside the stream it creates.
Call `migrate_from_ffi!` next to each type, as SoxResampler does, so `handle_id` no longer needs crate visibility and the nested migration module in room-apis goes away. The request handlers that need the wrapped track read it through an `inner()` accessor compiled only into room-apis.
The data stream readers spawn onto `crate::runtime::runtime()` when closed, and are about to be shared by both surfaces. Room-apis returns the FFI server's runtime; core-modules keeps its own.
A pending `next()` holds the stream, so releasing it from the foreign side could not end the subscription while a read was in flight. Swift and Dart cannot cancel that read, which left the subscription running until the next frame arrived. `close()` wakes the pending read with `None` and drops the core stream, matching `close()` on the data stream readers. `tokio-util` is now a dependency of both surfaces, since the shared stream uses its `CancellationToken`.
wait_for_unpublish awaits a tokio watch channel, which needs no runtime to poll.
…datatrack #[uniffi(default)] accepts only a literal, so the FFI record repeated the core default and could drift from it. The field is now optional, and an unset value falls back to the core RemoteDataTrackPipelineOptions default.
Its exported close() clashes with the AutoCloseable close() UniFFI generates for every Kotlin object, so it becomes close_stream there, like the other stream objects.
…ack_ffi Makes it clear at the call sites that Arc<data_track_ffi::RemoteDataTrack> wraps the FFI wrapper object, not the core DataTrack.
049dfc0 to
33246bc
Compare
alan-george-lk
left a comment
There was a problem hiding this comment.
I integrated this into the C++ SDK as part of my bindgen async work -- all existing data tracks tests passed without test modification. Nice work!
Overview
Today, there are two ways client sdks that implement data tracks (using the rust implementation) consume the rust code:
core-modulesstyle: swift, android, and flutter all use the existing data tracks uniffi interface which has existed for many months. This is what used to be inlivekit-uniffiprior to the livekit-ffi / livekit-uniffi merge.room-apisstyle: node, python, c++, and unity all use the legacy C ABI style FFI interface, squashing all their call semantics into request / response protobufs.The focus of this pull request is to bring an interface very similar to the current
core-modulesstyle to consumers usinglivekit-ffivia theroom-apisstyle.Summary of change
The primary implementation goal of this is to reuse as much code between the two
livekit-ffi"modes" as possible. Any "record" type structs likeDataTrackInfoshould be used by both styles, as well asLocalDataTrackandRemoteDataTrack. This means that all downstream sdks which use rust can now use most of the same primitives!Also, just like the sox resampler change,
LocalDataTrackandRemoteDataTracknow can be created from a ffi handle id, and similarly a uniffi object can have a ffi handle created from it. This allows for a more flexible migration strategy, so it can be done piecemeal if desired.Usage Example
Here's how previously you would subscribe to a remote data track in python:
And, here's how it now works with the new uniffi based api - less verbose, and matches almost 1:1 with how it works in swift / kotlin / flutter today: