1- import ctypes , sys , os
1+ from ctypes import *
2+ import sys , os
23import livekit ._proto .ffi_pb2 as proto
34
45basedir = os .path .abspath (os .path .dirname (__file__ ))
1011 libfile = 'liblivekit_ffi.so'
1112libpath = os .path .join (basedir , libfile )
1213
13- ffi_lib = ctypes .CDLL (libpath )
14+ ffi_lib = CDLL (libpath )
15+
16+ # C function types
17+ ffi_lib .livekit_ffi_request .argtypes = [POINTER (c_ubyte ), c_size_t , POINTER (POINTER (c_ubyte )), c_size_t ]
18+ ffi_lib .livekit_ffi_request .restype = c_size_t
19+
20+ ffi_lib .livekit_ffi_drop_handle .argtypes = [c_size_t ]
21+ ffi_lib .livekit_ffi_drop_handle .restype = c_bool
1422
1523INVALID_HANDLE = 0
1624
@@ -19,7 +27,18 @@ def __init__(self):
1927 pass
2028
2129 def request (self , req : proto .FFIRequest ) -> proto .FFIResponse :
22- pass
30+ data = bytearray (req .SerializeToString ())
31+ data_len = len (data )
32+ resp_ptr = POINTER (c_ubyte )()
33+ resp_len = c_size_t ()
34+ handle = ffi_lib .livekit_ffi_request (data , data_len , byref (resp_ptr ), byref (resp_len ))
35+
36+ resp_data = bytearray (resp_ptr [:resp_len .value ])
37+ resp = proto .FFIResponse ()
38+ resp .ParseFromString (resp_data )
39+
40+ FfiHandle (handle )
41+ return resp
2342
2443class FfiHandle :
2544 handle = INVALID_HANDLE
@@ -29,4 +48,4 @@ def __init__(self, handle: int):
2948
3049 def __del__ (self ):
3150 if self .handle != INVALID_HANDLE :
32- assert (ffi_lib .livekit_ffi_drop_handle (self .handle ))
51+ assert (ffi_lib .livekit_ffi_drop_handle (c_size_t ( self .handle ) ))
0 commit comments