Skip to content

Commit 3a7fe96

Browse files
committed
wip
1 parent 7ce73af commit 3a7fe96

28 files changed

+1492
-956
lines changed

.gitignore

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# pdm
105+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106+
#pdm.lock
107+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108+
# in version control.
109+
# https://pdm.fming.dev/#use-with-ide
110+
.pdm.toml
111+
112+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113+
__pypackages__/
114+
115+
# Celery stuff
116+
celerybeat-schedule
117+
celerybeat.pid
118+
119+
# SageMath parsed files
120+
*.sage.py
121+
122+
# Environments
123+
.env
124+
.venv
125+
env/
126+
venv/
127+
ENV/
128+
env.bak/
129+
venv.bak/
130+
131+
# Spyder project settings
132+
.spyderproject
133+
.spyproject
134+
135+
# Rope project settings
136+
.ropeproject
137+
138+
# mkdocs documentation
139+
/site
140+
141+
# mypy
142+
.mypy_cache/
143+
.dmypy.json
144+
dmypy.json
145+
146+
# Pyre type checker
147+
.pyre/
148+
149+
# pytype static type analyzer
150+
.pytype/
151+
152+
# Cython debug symbols
153+
cython_debug/
154+
155+
# PyCharm
156+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158+
# and can be added to the global gitignore or merged into this file. For a more nuclear
159+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160+
#.idea/

README.md

Whitespace-only changes.

client-sdk-rust

examples/basic_room/room.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import livekit
2+
import asyncio
3+
4+
URL = 'ws://localhost:7880'
5+
TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5MDY2MTMyODgsImlzcyI6IkFQSVRzRWZpZFpqclFvWSIsIm5hbWUiOiJuYXRpdmUiLCJuYmYiOjE2NzI2MTMyODgsInN1YiI6Im5hdGl2ZSIsInZpZGVvIjp7InJvb20iOiJ0ZXN0Iiwicm9vbUFkbWluIjp0cnVlLCJyb29tQ3JlYXRlIjp0cnVlLCJyb29tSm9pbiI6dHJ1ZSwicm9vbUxpc3QiOnRydWV9fQ.uSNIangMRu8jZD5mnRYoCHjcsQWCrJXgHCs0aNIgBFY'
6+
7+
8+
async def main():
9+
room = livekit.Room()
10+
await room.connect(URL, TOKEN)
11+
12+
@room.on("participant_connected")
13+
def on_participant_connected(participant: livekit.RemoteParticipant):
14+
print("Participant connected: " + participant.identity)
15+
16+
print("Connected to room with sid: " + room.sid)
17+
await room.run()
18+
19+
if __name__ == "__main__":
20+
asyncio.run(main())

livekit/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@
33

44
__version__ = "0.0.1"
55

6+
from _proto.track_pb2 import (TrackKind, TrackSource, StreamState)
7+
8+
from .room import Room
9+
from .participant import (Participant, LocalParticipant, RemoteParticipant)
10+
from .track import (Track, LocalAudioTrack, LocalVideoTrack,
11+
RemoteAudioTrack, RemoteVideoTrack)
12+
from .track_publication import (
13+
TrackPublication, LocalTrackPublication, RemoteTrackPublication)

livekit/_ffi_client.py

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,88 @@
11
from ctypes import *
2-
import sys, os
3-
import livekit._proto.ffi_pb2 as proto
2+
import sys
3+
from ._proto import ffi_pb2 as proto_ffi
4+
from ._proto import room_pb2 as proto_room
5+
from pyee.asyncio import EventEmitter
6+
import pkg_resources
7+
import asyncio
8+
import threading
9+
import logging
410

5-
basedir = os.path.abspath(os.path.dirname(__file__))
611
if sys.platform == "win32":
712
libfile = 'livekit_ffi.dll'
813
elif sys.platform == "darwin":
914
libfile = 'liblivekit_ffi.dylib'
1015
else:
1116
libfile = 'liblivekit_ffi.so'
12-
libpath = os.path.join(basedir, libfile)
17+
18+
libpath = pkg_resources.resource_filename('livekit', libfile)
1319

1420
ffi_lib = CDLL(libpath)
1521

1622
# C function types
17-
ffi_lib.livekit_ffi_request.argtypes = [POINTER(c_ubyte), c_size_t, POINTER(POINTER(c_ubyte)), c_size_t]
23+
ffi_lib.livekit_ffi_request.argtypes = [POINTER(c_ubyte), c_size_t, POINTER(POINTER(c_ubyte)), POINTER(c_size_t)]
1824
ffi_lib.livekit_ffi_request.restype = c_size_t
1925

2026
ffi_lib.livekit_ffi_drop_handle.argtypes = [c_size_t]
2127
ffi_lib.livekit_ffi_drop_handle.restype = c_bool
2228

2329
INVALID_HANDLE = 0
2430

25-
class FfiClient:
31+
@CFUNCTYPE(c_void_p, POINTER(c_uint8), c_size_t)
32+
def ffi_event_callback(data_ptr: POINTER(c_uint8), data_len: c_size_t):
33+
event_data = bytes(data_ptr[:data_len])
34+
event = proto_ffi.FfiEvent()
35+
event.ParseFromString(event_data)
36+
37+
ffi_client = FfiClient()
38+
with ffi_client._lock:
39+
loop = ffi_client._event_loop
40+
41+
loop.call_soon_threadsafe(dispatch_event, event)
42+
43+
def dispatch_event(event: proto_ffi.FfiEvent):
44+
ffi_client = FfiClient()
45+
which = event.WhichOneof('message')
46+
if which == 'connect':
47+
ffi_client.emit('connect', event.connect)
48+
elif which == 'room_event':
49+
ffi_client.emit('room', event.room_event)
50+
51+
class Singleton(type):
52+
_instances = {}
53+
def __call__(cls, *args, **kwargs):
54+
if cls not in cls._instances:
55+
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
56+
return cls._instances[cls]
57+
58+
class FfiClient(EventEmitter, metaclass=Singleton):
2659
def __init__(self):
27-
pass
60+
super().__init__()
61+
self._lock = threading.Lock()
62+
self._event_loop = None
63+
64+
req = proto_ffi.FfiRequest()
65+
req.initialize.event_callback_ptr = cast(ffi_event_callback, c_void_p).value
66+
self.request(req)
67+
68+
def set_event_loop(self, loop: asyncio.AbstractEventLoop):
69+
with self._lock:
70+
if self._event_loop is not None and self._event_loop != loop:
71+
logging.warning("FfiClient is now using a different asyncio event_loop")
2872

29-
def request(self, req: proto.FFIRequest) -> proto.FFIResponse:
30-
data = bytearray(req.SerializeToString())
73+
self._event_loop = loop
74+
75+
def request(self, req: proto_ffi.FfiRequest) -> proto_ffi.FfiResponse:
76+
data = req.SerializeToString()
3177
data_len = len(data)
78+
data = (c_ubyte * data_len)(*data)
79+
3280
resp_ptr = POINTER(c_ubyte)()
3381
resp_len = c_size_t()
3482
handle = ffi_lib.livekit_ffi_request(data, data_len, byref(resp_ptr), byref(resp_len))
3583

36-
resp_data = bytearray(resp_ptr[:resp_len.value])
37-
resp = proto.FFIResponse()
84+
resp_data = bytes(resp_ptr[:resp_len.value])
85+
resp = proto_ffi.FfiResponse()
3886
resp.ParseFromString(resp_data)
3987

4088
FfiHandle(handle)

livekit/_proto/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import os
2+
import sys
3+
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))

0 commit comments

Comments
 (0)