3434atexit .register (_resource_files .close )
3535
3636
37+ def _lib_name ():
38+ if platform .system () == "Linux" :
39+ return "liblivekit_ffi.so"
40+ elif platform .system () == "Darwin" :
41+ return "liblivekit_ffi.dylib"
42+ elif platform .system () == "Windows" :
43+ return "livekit_ffi.dll"
44+ return None
45+
46+
3747def get_ffi_lib ():
3848 # allow to override the lib path using an env var
3949 libpath = os .environ .get ("LIVEKIT_LIB_PATH" , "" ).strip ()
4050 if libpath :
4151 return ctypes .CDLL (libpath )
4252
43- if platform .system () == "Linux" :
44- libname = "liblivekit_ffi.so"
45- elif platform .system () == "Darwin" :
46- libname = "liblivekit_ffi.dylib"
47- elif platform .system () == "Windows" :
48- libname = "livekit_ffi.dll"
49- else :
53+ libname = _lib_name ()
54+ if libname is None :
5055 raise Exception (
51- f"no ffi library found for platform { platform .system ()} . \
52- Set LIVEKIT_LIB_PATH to specify a the lib path"
56+ f"no ffi library found for platform { platform .system ()} . "
57+ " Set LIVEKIT_LIB_PATH to specify the lib path"
5358 )
5459
5560 res = importlib .resources .files ("livekit.rtc.resources" ) / libname
@@ -58,32 +63,8 @@ def get_ffi_lib():
5863 return ctypes .CDLL (str (path ))
5964
6065
61- ffi_lib = get_ffi_lib ()
6266ffi_cb_fnc = ctypes .CFUNCTYPE (None , ctypes .POINTER (ctypes .c_uint8 ), ctypes .c_size_t )
6367
64- # C function types
65- ffi_lib .livekit_ffi_initialize .argtypes = [
66- ffi_cb_fnc ,
67- ctypes .c_bool ,
68- ctypes .c_char_p ,
69- ctypes .c_char_p ,
70- ]
71-
72- ffi_lib .livekit_ffi_request .argtypes = [
73- ctypes .POINTER (ctypes .c_ubyte ),
74- ctypes .c_size_t ,
75- ctypes .POINTER (ctypes .POINTER (ctypes .c_ubyte )),
76- ctypes .POINTER (ctypes .c_size_t ),
77- ]
78- ffi_lib .livekit_ffi_request .restype = ctypes .c_uint64
79-
80- ffi_lib .livekit_ffi_drop_handle .argtypes = [ctypes .c_uint64 ]
81- ffi_lib .livekit_ffi_drop_handle .restype = ctypes .c_bool
82-
83-
84- ffi_lib .livekit_ffi_dispose .argtypes = []
85- ffi_lib .livekit_ffi_dispose .restype = None
86-
8768INVALID_HANDLE = 0
8869
8970
@@ -102,7 +83,9 @@ def disposed(self) -> bool:
10283 def dispose (self ) -> None :
10384 if self .handle != INVALID_HANDLE and not self ._disposed :
10485 self ._disposed = True
105- assert ffi_lib .livekit_ffi_drop_handle (ctypes .c_uint64 (self .handle ))
86+ assert FfiClient .instance ._ffi_lib .livekit_ffi_drop_handle (
87+ ctypes .c_uint64 (self .handle )
88+ )
10689
10790 def __repr__ (self ) -> str :
10891 return f"FfiHandle({ self .handle } )"
@@ -214,10 +197,40 @@ def __init__(self) -> None:
214197 self ._lock = threading .RLock ()
215198 self ._queue = FfiQueue [proto_ffi .FfiEvent ]()
216199
217- ffi_lib .livekit_ffi_initialize (
200+ try :
201+ self ._ffi_lib = get_ffi_lib ()
202+ except Exception as e :
203+ libname = _lib_name () or "livekit_ffi"
204+ raise ImportError (
205+ "failed to load %s: %s\n "
206+ "Install the livekit package with: pip install livekit\n "
207+ "Or set LIVEKIT_LIB_PATH to the path of the native library."
208+ % (libname , e )
209+ ) from None
210+ self ._ffi_lib .livekit_ffi_initialize .argtypes = [
211+ ffi_cb_fnc ,
212+ ctypes .c_bool ,
213+ ctypes .c_char_p ,
214+ ctypes .c_char_p ,
215+ ]
216+ self ._ffi_lib .livekit_ffi_request .argtypes = [
217+ ctypes .POINTER (ctypes .c_ubyte ),
218+ ctypes .c_size_t ,
219+ ctypes .POINTER (ctypes .POINTER (ctypes .c_ubyte )),
220+ ctypes .POINTER (ctypes .c_size_t ),
221+ ]
222+ self ._ffi_lib .livekit_ffi_request .restype = ctypes .c_uint64
223+ self ._ffi_lib .livekit_ffi_drop_handle .argtypes = [ctypes .c_uint64 ]
224+ self ._ffi_lib .livekit_ffi_drop_handle .restype = ctypes .c_bool
225+ self ._ffi_lib .livekit_ffi_dispose .argtypes = []
226+ self ._ffi_lib .livekit_ffi_dispose .restype = None
227+
228+ self ._ffi_lib .livekit_ffi_initialize (
218229 ffi_event_callback , True , b"python" , __version__ .encode ("ascii" )
219230 )
220231
232+ ffi_lib = self ._ffi_lib
233+
221234 @atexit .register
222235 def _dispose_lk_ffi ():
223236 ffi_lib .livekit_ffi_dispose ()
@@ -233,7 +246,7 @@ def request(self, req: proto_ffi.FfiRequest) -> proto_ffi.FfiResponse:
233246
234247 resp_ptr = ctypes .POINTER (ctypes .c_ubyte )()
235248 resp_len = ctypes .c_size_t ()
236- handle = ffi_lib .livekit_ffi_request (
249+ handle = self . _ffi_lib .livekit_ffi_request (
237250 data , proto_len , ctypes .byref (resp_ptr ), ctypes .byref (resp_len )
238251 )
239252 assert handle != INVALID_HANDLE
0 commit comments