Skip to content

Commit 7783fc5

Browse files
authored
feat: update ffi (#20)
1 parent ac47489 commit 7783fc5

33 files changed

+865
-911
lines changed

client-sdk-rust

examples/basic_room.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ def on_participant_disconnected(participant: livekit.RemoteParticipant):
2222
participant.sid, participant.identity)
2323

2424
@room.listens_to("local_track_published")
25-
def on_local_track_published(publication: livekit.LocalTrackPublication, track: livekit.LocalAudioTrack | livekit.LocalVideoTrack):
25+
def on_local_track_published(publication: livekit.LocalTrackPublication,
26+
track: livekit.LocalAudioTrack
27+
| livekit.LocalVideoTrack):
2628
logging.info("local track published: %s", publication.sid)
2729

2830
@room.listens_to("active_speakers_changed")
@@ -34,20 +36,24 @@ def on_local_track_unpublished(publication: livekit.LocalTrackPublication):
3436
logging.info("local track unpublished: %s", publication.sid)
3537

3638
@room.listens_to("track_published")
37-
def on_track_published(publication: livekit.RemoteTrackPublication, participant: livekit.RemoteParticipant):
39+
def on_track_published(publication: livekit.RemoteTrackPublication,
40+
participant: livekit.RemoteParticipant):
3841
logging.info("track published: %s from participant %s (%s)",
3942
publication.sid, participant.sid, participant.identity)
4043

4144
@room.listens_to("track_unpublished")
42-
def on_track_unpublished(publication: livekit.RemoteTrackPublication, participant: livekit.RemoteParticipant):
45+
def on_track_unpublished(publication: livekit.RemoteTrackPublication,
46+
participant: livekit.RemoteParticipant):
4347
logging.info("track unpublished: %s", publication.sid)
4448

4549
# Keep a reference to the streams, otherwise they will be disposed
4650
audio_stream = None
4751
video_stream = None
4852

4953
@room.listens_to("track_subscribed")
50-
def on_track_subscribed(track: livekit.Track, publication: livekit.RemoteTrackPublication, participant: livekit.RemoteParticipant):
54+
def on_track_subscribed(track: livekit.Track,
55+
publication: livekit.RemoteTrackPublication,
56+
participant: livekit.RemoteParticipant):
5157
logging.info("track subscribed: %s", publication.sid)
5258
if track.kind == livekit.TrackKind.KIND_VIDEO:
5359
nonlocal video_stream
@@ -68,19 +74,26 @@ def on_audio_frame(frame: livekit.AudioFrame):
6874
pass
6975

7076
@room.listens_to("track_unsubscribed")
71-
def on_track_unsubscribed(track: livekit.Track, publication: livekit.RemoteTrackPublication, participant: livekit.RemoteParticipant):
77+
def on_track_unsubscribed(track: livekit.Track,
78+
publication: livekit.RemoteTrackPublication,
79+
participant: livekit.RemoteParticipant):
7280
logging.info("track unsubscribed: %s", publication.sid)
7381

7482
@room.listens_to("data_received")
75-
def on_data_received(data: bytes, kind: livekit.DataPacketKind, participant: livekit.Participant):
83+
def on_data_received(data: bytes,
84+
kind: livekit.DataPacketKind,
85+
participant: livekit.Participant):
7686
logging.info("received data from %s: %s", participant.identity, data)
7787

7888
@room.listens_to("connection_quality_changed")
79-
def on_connection_quality_changed(participant: livekit.Participant, quality: livekit.ConnectionQuality):
89+
def on_connection_quality_changed(participant: livekit.Participant,
90+
quality: livekit.ConnectionQuality):
8091
logging.info("connection quality changed for %s", participant.identity)
8192

8293
@room.listens_to("track_subscription_failed")
83-
def on_track_subscription_failed(participant: livekit.RemoteParticipant, track_sid: str, error: str):
94+
def on_track_subscription_failed(participant: livekit.RemoteParticipant,
95+
track_sid: str,
96+
error: str):
8497
logging.info("track subscription failed: %s %s",
8598
participant.identity, error)
8699

@@ -111,6 +124,8 @@ def on_reconnected() -> None:
111124

112125
await room.local_participant.publish_data("hello world")
113126

127+
logging.info("participants: %s", room.participants)
128+
114129
await room.run()
115130
except livekit.ConnectError as e:
116131
logging.error("failed to connect to the room: %s", e)

livekit/_ffi_client.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,22 @@
3939

4040
# C function types
4141
ffi_lib.livekit_ffi_request.argtypes = [
42-
ctypes.POINTER(ctypes.c_ubyte), ctypes.c_size_t, ctypes.POINTER(ctypes.POINTER(ctypes.c_ubyte)), ctypes.POINTER(ctypes.c_size_t)]
42+
ctypes.POINTER(ctypes.c_ubyte),
43+
ctypes.c_size_t,
44+
ctypes.POINTER(ctypes.POINTER(ctypes.c_ubyte)),
45+
ctypes.POINTER(ctypes.c_size_t)
46+
]
4347
ffi_lib.livekit_ffi_request.restype = ctypes.c_size_t
4448

4549
ffi_lib.livekit_ffi_drop_handle.argtypes = [ctypes.c_size_t]
4650
ffi_lib.livekit_ffi_drop_handle.restype = ctypes.c_bool
4751

4852
INVALID_HANDLE = 0
4953

54+
5055
@ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.POINTER(ctypes.c_uint8), ctypes.c_size_t)
51-
def ffi_event_callback(data_ptr: ctypes.POINTER(ctypes.c_uint8), data_len: ctypes.c_size_t) -> None: # type: ignore
56+
def ffi_event_callback(data_ptr: ctypes.POINTER(ctypes.c_uint8), # type: ignore
57+
data_len: ctypes.c_size_t) -> None:
5258
event_data = bytes(data_ptr[:int(data_len)])
5359
event = proto_ffi.FfiEvent()
5460
event.ParseFromString(event_data)
@@ -71,7 +77,7 @@ def __init__(self) -> None:
7177

7278
req = proto_ffi.FfiRequest()
7379
cb_callback = int(ctypes.cast(
74-
ffi_event_callback, ctypes.c_void_p).value) # type: ignore
80+
ffi_event_callback, ctypes.c_void_p).value) # type: ignore
7581
req.initialize.event_callback_ptr = cb_callback
7682
self.request(req)
7783

@@ -96,9 +102,8 @@ def request(self, req: proto_ffi.FfiRequest) -> proto_ffi.FfiResponse:
96102
FfiHandle(handle)
97103
return resp
98104

99-
class FfiHandle:
100-
handle = INVALID_HANDLE
101105

106+
class FfiHandle:
102107
def __init__(self, handle: int) -> None:
103108
self.handle = handle
104109

@@ -108,4 +113,5 @@ def __del__(self):
108113
ctypes.c_size_t(self.handle))
109114

110115

111-
ffi_client = FfiClient()
116+
ffi_client = FfiClient()
117+
ffi_client.set_event_loop(asyncio.get_event_loop())

livekit/_proto/audio_frame_pb2.py

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

0 commit comments

Comments
 (0)