Skip to content

Commit f65b3fa

Browse files
authored
livekit, livekit-ffi: Update to v2 APIs (#342)
* room: update DataPacket * Remove SID from API and mv {,remote}_participants * Rename tracks to track_publications * livekit-ffi: destination_{sids -> identities} * async room.sid(&self) * bump: livekit 0.4.0, livekit-ffi 0.6.0 * ffi: PublishData.{kind -> reliable} * livekit/room: use inner sid for impl Debug * Add sid FfiRequest, streamline async sid() * livekit-protocol: add Promise<T> * livekit-protocol: allow multi-recv from Promise<T> * livekit-ffi: remove FfiClient GetRoomSid * livekit-ffi: don't expose RoomSidChanged
1 parent 8b276f9 commit f65b3fa

File tree

17 files changed

+201
-110
lines changed

17 files changed

+201
-110
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libwebrtc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description = "Livekit safe bindings to libwebrtc"
88
repository = "https://github.com/livekit/rust-sdks"
99

1010
[dependencies]
11-
livekit-protocol = { path = "../livekit-protocol", version = "0.3.2" }
11+
livekit-protocol = { path = "../livekit-protocol", version = "0.3.3" }
1212
log = "0.4"
1313
serde = { version = "1.0", features = ["derive"] }
1414
serde_json = "1.0"

livekit-api/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ rustls-tls-webpki-roots = [
6565
__rustls-tls = ["tokio-tungstenite?/__rustls-tls", "reqwest?/__rustls"]
6666

6767
[dependencies]
68-
livekit-protocol = { path = "../livekit-protocol", version = "0.3.2" }
68+
livekit-protocol = { path = "../livekit-protocol", version = "0.3.3" }
6969
thiserror = "1.0"
7070
serde = { version = "1.0", features = ["derive"] }
7171
sha2 = "0.10"

livekit-ffi/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "livekit-ffi"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
edition = "2021"
55
license = "Apache-2.0"
66
description = "FFI interface for bindings in other languages"
@@ -18,8 +18,8 @@ __rustls-tls = ["livekit/__rustls-tls"]
1818
tracing = ["tokio/tracing", "console-subscriber"]
1919

2020
[dependencies]
21-
livekit = { path = "../livekit", version = "0.3.2" }
22-
livekit-protocol = { path = "../livekit-protocol", version = "0.3.2" }
21+
livekit = { path = "../livekit", version = "0.4.0" }
22+
livekit-protocol = { path = "../livekit-protocol", version = "0.3.3" }
2323
tokio = { version = "1", features = ["full", "parking_lot"] }
2424
futures-util = { version = "0.3", default-features = false, features = ["sink"] }
2525
parking_lot = { version = "0.12", features = ["deadlock_detection"] }

livekit-ffi/protocol/room.proto

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ message PublishDataRequest {
8888
uint64 local_participant_handle = 1;
8989
uint64 data_ptr = 2;
9090
uint64 data_len = 3;
91-
DataPacketKind kind = 4;
92-
repeated string destination_sids = 5; // destination
91+
bool reliable = 4;
92+
repeated string destination_sids = 5 [deprecated=true];
9393
optional string topic = 6;
94+
repeated string destination_identities = 7;
9495
}
9596
message PublishDataResponse {
9697
uint64 async_id = 1;
@@ -160,7 +161,6 @@ message GetSessionStatsCallback {
160161
repeated RtcStats subscriber_stats = 4;
161162
}
162163

163-
164164
//
165165
// Options
166166
//
@@ -273,9 +273,10 @@ message RoomEvent {
273273
TrackUnmuted track_unmuted = 12;
274274
ActiveSpeakersChanged active_speakers_changed = 13;
275275
RoomMetadataChanged room_metadata_changed = 14;
276-
ParticipantMetadataChanged participant_metadata_changed = 15;
277-
ParticipantNameChanged participant_name_changed = 16;
278-
ConnectionQualityChanged connection_quality_changed = 17;
276+
RoomSidChanged room_sid_changed = 15;
277+
ParticipantMetadataChanged participant_metadata_changed = 16;
278+
ParticipantNameChanged participant_name_changed = 17;
279+
ConnectionQualityChanged connection_quality_changed = 18;
279280
ConnectionStateChanged connection_state_changed = 19;
280281
// Connected connected = 20;
281282
Disconnected disconnected = 21;
@@ -364,6 +365,10 @@ message RoomMetadataChanged {
364365
string metadata = 1;
365366
}
366367

368+
message RoomSidChanged {
369+
string sid = 1;
370+
}
371+
367372
message ParticipantMetadataChanged {
368373
string participant_sid = 1;
369374
string metadata = 2;

livekit-ffi/src/conversion/room.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ impl From<proto::AudioEncoding> for AudioEncoding {
215215
impl From<&FfiRoom> for proto::RoomInfo {
216216
fn from(value: &FfiRoom) -> Self {
217217
let room = &value.inner.room;
218-
Self { sid: room.sid().into(), name: room.name(), metadata: room.metadata() }
218+
Self {
219+
sid: room.maybe_sid().unwrap_or_default().into(),
220+
name: room.name(),
221+
metadata: room.metadata(),
222+
}
219223
}
220224
}

livekit-ffi/src/livekit.proto.rs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// @generated
2-
// This file is @generated by prost-build.
32
#[allow(clippy::derive_partial_eq_without_eq)]
43
#[derive(Clone, PartialEq, ::prost::Message)]
54
pub struct FrameCryptor {
@@ -2019,13 +2018,15 @@ pub struct PublishDataRequest {
20192018
pub data_ptr: u64,
20202019
#[prost(uint64, tag="3")]
20212020
pub data_len: u64,
2022-
#[prost(enumeration="DataPacketKind", tag="4")]
2023-
pub kind: i32,
2024-
/// destination
2021+
#[prost(bool, tag="4")]
2022+
pub reliable: bool,
2023+
#[deprecated]
20252024
#[prost(string, repeated, tag="5")]
20262025
pub destination_sids: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
20272026
#[prost(string, optional, tag="6")]
20282027
pub topic: ::core::option::Option<::prost::alloc::string::String>,
2028+
#[prost(string, repeated, tag="7")]
2029+
pub destination_identities: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
20292030
}
20302031
#[allow(clippy::derive_partial_eq_without_eq)]
20312032
#[derive(Clone, PartialEq, ::prost::Message)]
@@ -2044,20 +2045,6 @@ pub struct PublishDataCallback {
20442045
/// Publish transcription messages to room
20452046
#[allow(clippy::derive_partial_eq_without_eq)]
20462047
#[derive(Clone, PartialEq, ::prost::Message)]
2047-
pub struct TranscriptionSegment {
2048-
#[prost(string, tag="1")]
2049-
pub id: ::prost::alloc::string::String,
2050-
#[prost(string, tag="2")]
2051-
pub text: ::prost::alloc::string::String,
2052-
#[prost(uint64, tag="3")]
2053-
pub start_time: u64,
2054-
#[prost(uint64, tag="4")]
2055-
pub end_time: u64,
2056-
#[prost(bool, tag="5")]
2057-
pub r#final: bool,
2058-
}
2059-
#[allow(clippy::derive_partial_eq_without_eq)]
2060-
#[derive(Clone, PartialEq, ::prost::Message)]
20612048
pub struct PublishTranscriptionRequest {
20622049
#[prost(uint64, tag="1")]
20632050
pub local_participant_handle: u64,
@@ -2240,6 +2227,20 @@ pub struct RoomOptions {
22402227
}
22412228
#[allow(clippy::derive_partial_eq_without_eq)]
22422229
#[derive(Clone, PartialEq, ::prost::Message)]
2230+
pub struct TranscriptionSegment {
2231+
#[prost(string, tag="1")]
2232+
pub id: ::prost::alloc::string::String,
2233+
#[prost(string, tag="2")]
2234+
pub text: ::prost::alloc::string::String,
2235+
#[prost(uint64, tag="3")]
2236+
pub start_time: u64,
2237+
#[prost(uint64, tag="4")]
2238+
pub end_time: u64,
2239+
#[prost(bool, tag="5")]
2240+
pub r#final: bool,
2241+
}
2242+
#[allow(clippy::derive_partial_eq_without_eq)]
2243+
#[derive(Clone, PartialEq, ::prost::Message)]
22432244
pub struct BufferInfo {
22442245
#[prost(uint64, tag="1")]
22452246
pub data_ptr: u64,
@@ -2259,7 +2260,7 @@ pub struct OwnedBuffer {
22592260
pub struct RoomEvent {
22602261
#[prost(uint64, tag="1")]
22612262
pub room_handle: u64,
2262-
#[prost(oneof="room_event::Message", tags="2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 19, 21, 22, 23, 24, 25, 26")]
2263+
#[prost(oneof="room_event::Message", tags="2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26")]
22632264
pub message: ::core::option::Option<room_event::Message>,
22642265
}
22652266
/// Nested message and enum types in `RoomEvent`.
@@ -2294,10 +2295,12 @@ pub mod room_event {
22942295
#[prost(message, tag="14")]
22952296
RoomMetadataChanged(super::RoomMetadataChanged),
22962297
#[prost(message, tag="15")]
2297-
ParticipantMetadataChanged(super::ParticipantMetadataChanged),
2298+
RoomSidChanged(super::RoomSidChanged),
22982299
#[prost(message, tag="16")]
2299-
ParticipantNameChanged(super::ParticipantNameChanged),
2300+
ParticipantMetadataChanged(super::ParticipantMetadataChanged),
23002301
#[prost(message, tag="17")]
2302+
ParticipantNameChanged(super::ParticipantNameChanged),
2303+
#[prost(message, tag="18")]
23012304
ConnectionQualityChanged(super::ConnectionQualityChanged),
23022305
#[prost(message, tag="19")]
23032306
ConnectionStateChanged(super::ConnectionStateChanged),
@@ -2445,6 +2448,12 @@ pub struct RoomMetadataChanged {
24452448
}
24462449
#[allow(clippy::derive_partial_eq_without_eq)]
24472450
#[derive(Clone, PartialEq, ::prost::Message)]
2451+
pub struct RoomSidChanged {
2452+
#[prost(string, tag="1")]
2453+
pub sid: ::prost::alloc::string::String,
2454+
}
2455+
#[allow(clippy::derive_partial_eq_without_eq)]
2456+
#[derive(Clone, PartialEq, ::prost::Message)]
24482457
pub struct ParticipantMetadataChanged {
24492458
#[prost(string, tag="1")]
24502459
pub participant_sid: ::prost::alloc::string::String,

livekit-ffi/src/server/room.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,21 @@ impl FfiRoom {
166166
},
167167
));
168168

169+
// Update Room SID on promise resolve
170+
let room_handle = inner.handle_id.clone();
171+
server.async_runtime.spawn(async move {
172+
let _ = server.send_event(proto::ffi_event::Message::RoomEvent(
173+
proto::RoomEvent {
174+
room_handle,
175+
message: Some(proto::room_event::Message::RoomSidChanged(
176+
proto::RoomSidChanged {
177+
sid: ffi_room.inner.room.sid().await.into(),
178+
},
179+
)),
180+
},
181+
));
182+
});
183+
169184
// Forward events
170185
let event_handle = server.watch_panic({
171186
let close_rx = close_rx.resubscribe();
@@ -237,20 +252,20 @@ impl RoomInner {
237252
}
238253
.to_vec();
239254

240-
let kind = publish.kind();
241-
let destination_sids = publish.destination_sids;
255+
let reliable = publish.reliable;
256+
let topic = publish.topic;
257+
let destination_identities = publish.destination_identities;
242258
let async_id = server.next_id();
243259

244260
if let Err(err) = self.data_tx.send(FfiDataPacket {
245261
payload: DataPacket {
246262
payload: data.to_vec(), // Avoid copy?
247-
kind: kind.into(),
248-
topic: publish.topic,
249-
destination_sids: destination_sids
263+
reliable,
264+
topic,
265+
destination_identities: destination_identities
250266
.into_iter()
251267
.map(|str| str.try_into().unwrap())
252268
.collect(),
253-
destination_identities: Vec::new(), // TODO
254269
},
255270
async_id,
256271
}) {

livekit-protocol/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
[package]
22
name = "livekit-protocol"
3-
version = "0.3.2"
3+
version = "0.3.3"
44
edition = "2021"
55
license = "Apache-2.0"
66
description = "Livekit protocol and utilities for the Rust SDK"
77
repository = "https://github.com/livekit/rust-sdks"
88

99
[dependencies]
1010
livekit-runtime = { path = "../livekit-runtime", version = "0.3.0" }
11-
tokio = { version = "1", default-features = false, features = [ "sync", "macros" ] }
11+
tokio = { version = "1", default-features = false, features = [ "sync", "macros", "rt" ] }
1212
futures-util = { version = "0.3", features = ["sink"] }
1313
parking_lot = "0.12"
1414
prost = "0.12"

livekit-protocol/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
pub mod debouncer;
1919
pub mod enum_dispatch;
2020
pub mod observer;
21+
pub mod promise;
2122

2223
include!("livekit.rs");
2324

livekit-protocol/src/promise.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2024 LiveKit, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use tokio::sync::{oneshot, Mutex};
16+
17+
pub struct Promise<T> {
18+
tx: Mutex<Option<oneshot::Sender<T>>>,
19+
rx: Mutex<Option<oneshot::Receiver<T>>>,
20+
result: Mutex<Option<T>>,
21+
}
22+
23+
impl<T: Clone> Promise<T> {
24+
pub fn new() -> Self {
25+
let (tx, rx) = oneshot::channel();
26+
Self { tx: Mutex::new(Some(tx)), rx: Mutex::new(Some(rx)), result: Default::default() }
27+
}
28+
29+
pub fn resolve(&self, result: T) -> Result<(), &'static str> {
30+
let mut tx = self.tx.try_lock().unwrap();
31+
if tx.is_some() {
32+
let _ = tx.take().unwrap().send(result);
33+
Ok(())
34+
} else {
35+
Err("promise already used")
36+
}
37+
}
38+
39+
pub async fn result(&self) -> T {
40+
let mut rx = self.rx.lock().await;
41+
if rx.is_some() {
42+
self.result.lock().await.replace(rx.take().unwrap().await.unwrap());
43+
}
44+
self.result.lock().await.clone().unwrap()
45+
}
46+
47+
pub fn try_result(&self) -> Option<T> {
48+
self.result.try_lock().unwrap().clone()
49+
}
50+
}

livekit/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "livekit"
3-
version = "0.3.2"
3+
version = "0.4.0"
44
edition = "2021"
55
license = "Apache-2.0"
66
description = "Rust Client SDK for LiveKit"
@@ -30,7 +30,7 @@ __lk-internal = []
3030
livekit-runtime = { path = "../livekit-runtime", version = "0.3.0", default-features = false }
3131
livekit-api = { path = "../livekit-api", version = "0.3.2", default-features = false }
3232
libwebrtc = { path = "../libwebrtc", version = "0.3.2" }
33-
livekit-protocol = { path = "../livekit-protocol", version = "0.3.2" }
33+
livekit-protocol = { path = "../livekit-protocol", version = "0.3.3" }
3434
prost = "0.12"
3535
serde = { version = "1", features = ["derive"] }
3636
serde_json = "1.0"

livekit/src/room/id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct ParticipantIdentity(pub String);
1515
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
1616
pub struct TrackSid(String);
1717

18-
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
18+
#[derive(Clone, Default, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
1919
pub struct RoomSid(String);
2020

2121
impl From<String> for ParticipantIdentity {

0 commit comments

Comments
 (0)