livekit 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
livekit/__init__.py ADDED
@@ -0,0 +1,19 @@
1
+ """LiveKit Client SDK
2
+ """
3
+
4
+ __version__ = "0.0.1"
5
+
6
+ from ._proto.video_frame_pb2 import (
7
+ VideoRotation, VideoFormatType, VideoFrameBufferType)
8
+ from ._proto.track_pb2 import (TrackKind, TrackSource, StreamState)
9
+
10
+ from .room import Room
11
+ from .participant import (Participant, LocalParticipant, RemoteParticipant)
12
+ from .track import (Track, LocalAudioTrack, LocalVideoTrack,
13
+ RemoteAudioTrack, RemoteVideoTrack)
14
+ from .track_publication import (
15
+ TrackPublication, LocalTrackPublication, RemoteTrackPublication)
16
+
17
+ from .video_frame import (ArgbFrame, VideoFrame, VideoFrameBuffer, NativeVideoFrameBuffer, PlanarYuvBuffer,
18
+ PlanarYuv8Buffer, PlanarYuv16Buffer, I420Buffer, I420ABuffer, I422Buffer, I010Buffer, NV12Buffer)
19
+ from .video_stream import VideoStream
livekit/_ffi_client.py ADDED
@@ -0,0 +1,111 @@
1
+ from ctypes import *
2
+ import platform
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
10
+
11
+ os = platform.system().lower()
12
+ arch = platform.machine().lower()
13
+ lib_path = 'lib/{}/{}'.format(os, arch)
14
+
15
+ if os == "windows":
16
+ lib_file = 'livekit_ffi.dll'
17
+ elif os == "darwin":
18
+ lib_file = 'liblivekit_ffi.dylib'
19
+ else:
20
+ lib_file = 'liblivekit_ffi.so'
21
+
22
+ libpath = pkg_resources.resource_filename('livekit', lib_path + '/' + lib_file)
23
+
24
+ ffi_lib = CDLL(libpath)
25
+
26
+ # C function types
27
+ ffi_lib.livekit_ffi_request.argtypes = [
28
+ POINTER(c_ubyte), c_size_t, POINTER(POINTER(c_ubyte)), POINTER(c_size_t)]
29
+ ffi_lib.livekit_ffi_request.restype = c_size_t
30
+
31
+ ffi_lib.livekit_ffi_drop_handle.argtypes = [c_size_t]
32
+ ffi_lib.livekit_ffi_drop_handle.restype = c_bool
33
+
34
+ INVALID_HANDLE = 0
35
+
36
+
37
+ @CFUNCTYPE(c_void_p, POINTER(c_uint8), c_size_t)
38
+ def ffi_event_callback(data_ptr: POINTER(c_uint8), data_len: c_size_t):
39
+ event_data = bytes(data_ptr[:data_len])
40
+ event = proto_ffi.FfiEvent()
41
+ event.ParseFromString(event_data)
42
+
43
+ ffi_client = FfiClient()
44
+ with ffi_client._lock:
45
+ loop = ffi_client._event_loop
46
+
47
+ loop.call_soon_threadsafe(dispatch_event, event)
48
+
49
+
50
+ def dispatch_event(event: proto_ffi.FfiEvent):
51
+ ffi_client = FfiClient()
52
+ which = event.WhichOneof('message')
53
+ ffi_client.emit(which, getattr(event, which))
54
+
55
+
56
+ class Singleton(type):
57
+ _instances = {}
58
+
59
+ def __call__(cls, *args, **kwargs):
60
+ if cls not in cls._instances:
61
+ cls._instances[cls] = super(
62
+ Singleton, cls).__call__(*args, **kwargs)
63
+ return cls._instances[cls]
64
+
65
+
66
+ class FfiClient(EventEmitter, metaclass=Singleton):
67
+ def __init__(self):
68
+ super().__init__()
69
+ self._lock = threading.Lock()
70
+ self._event_loop = None
71
+
72
+ req = proto_ffi.FfiRequest()
73
+ req.initialize.event_callback_ptr = cast(
74
+ ffi_event_callback, c_void_p).value
75
+ self.request(req)
76
+
77
+ def set_event_loop(self, loop: asyncio.AbstractEventLoop):
78
+ with self._lock:
79
+ if self._event_loop is not None and self._event_loop != loop:
80
+ logging.warning(
81
+ "FfiClient is now using a different asyncio event_loop")
82
+
83
+ self._event_loop = loop
84
+
85
+ def request(self, req: proto_ffi.FfiRequest) -> proto_ffi.FfiResponse:
86
+ data = req.SerializeToString()
87
+ data_len = len(data)
88
+ data = (c_ubyte * data_len)(*data)
89
+
90
+ resp_ptr = POINTER(c_ubyte)()
91
+ resp_len = c_size_t()
92
+ handle = ffi_lib.livekit_ffi_request(
93
+ data, data_len, byref(resp_ptr), byref(resp_len))
94
+
95
+ resp_data = bytes(resp_ptr[:resp_len.value])
96
+ resp = proto_ffi.FfiResponse()
97
+ resp.ParseFromString(resp_data)
98
+
99
+ FfiHandle(handle)
100
+ return resp
101
+
102
+
103
+ class FfiHandle:
104
+ handle = INVALID_HANDLE
105
+
106
+ def __init__(self, handle: int):
107
+ self.handle = handle
108
+
109
+ def __del__(self):
110
+ if self.handle != INVALID_HANDLE:
111
+ assert (ffi_lib.livekit_ffi_drop_handle(c_size_t(self.handle)))
Binary file
livekit/participant.py ADDED
@@ -0,0 +1,34 @@
1
+ from ._proto import participant_pb2 as proto_participant
2
+ from .track_publication import TrackPublication
3
+
4
+
5
+ class Participant():
6
+ def __init__(self, info: proto_participant.ParticipantInfo):
7
+ self._info = info
8
+ self._tracks: dict[str, TrackPublication] = {}
9
+
10
+ @property
11
+ def sid(self) -> str:
12
+ return self._info.sid
13
+
14
+ @property
15
+ def name(self) -> str:
16
+ return self._info.name
17
+
18
+ @property
19
+ def identity(self) -> str:
20
+ return self._info.identity
21
+
22
+ @property
23
+ def metadata(self) -> str:
24
+ return self._info.metadata
25
+
26
+
27
+ class LocalParticipant(Participant):
28
+ def __init__(self, info: proto_participant.ParticipantInfo):
29
+ super().__init__(info)
30
+
31
+
32
+ class RemoteParticipant(Participant):
33
+ def __init__(self, info: proto_participant.ParticipantInfo):
34
+ super().__init__(info)
livekit/room.py ADDED
@@ -0,0 +1,141 @@
1
+ import asyncio
2
+ from pyee.asyncio import AsyncIOEventEmitter
3
+ from ._ffi_client import (FfiClient, FfiHandle)
4
+ from ._proto import ffi_pb2 as proto_ffi
5
+ from ._proto import room_pb2 as proto_room
6
+ from ._proto import participant_pb2 as proto_participant
7
+ from .participant import (Participant, LocalParticipant, RemoteParticipant)
8
+ from .track_publication import (RemoteTrackPublication, LocalTrackPublication)
9
+ from .track import (RemoteAudioTrack, RemoteVideoTrack)
10
+ from livekit import TrackKind
11
+ import weakref
12
+
13
+
14
+ class ConnectError(Exception):
15
+ def __init__(self, message: str):
16
+ self.message = message
17
+
18
+
19
+ class Room(AsyncIOEventEmitter):
20
+ def __init__(self):
21
+ super().__init__()
22
+ self._ffi_handle: FfiHandle = None
23
+ self._room_info: proto_room.RoomInfo = None
24
+ self._participants: dict[str, RemoteParticipant] = {}
25
+
26
+ def __del__(self):
27
+ ffi_client = FfiClient()
28
+ ffi_client.remove_listener('room_event', self._on_room_event)
29
+
30
+ async def connect(self, url: str, token: str):
31
+ # TODO(theomonnom): We should be more flexible about the event loop
32
+ ffi_client = FfiClient()
33
+ ffi_client.set_event_loop(asyncio.get_running_loop())
34
+
35
+ req = proto_ffi.FfiRequest()
36
+ req.connect.url = url
37
+ req.connect.token = token
38
+
39
+ resp = ffi_client.request(req)
40
+ async_id = resp.connect.async_id
41
+
42
+ future = asyncio.Future()
43
+
44
+ def on_connect_callback(cb: proto_room.ConnectCallback):
45
+ if cb.async_id == async_id:
46
+ # add existing participants
47
+ for participant_info in cb.room.participants:
48
+ self._create_remote_participant(participant_info)
49
+
50
+ future.set_result(cb)
51
+ ffi_client.remove_listener('connect', on_connect_callback)
52
+
53
+ ffi_client.add_listener('connect', on_connect_callback)
54
+ resp = await future
55
+
56
+ if resp.error:
57
+ raise ConnectError(resp.error)
58
+ else:
59
+ self._ffi_handle = FfiHandle(resp.room.handle.id)
60
+ self._room_info = resp.room
61
+ self._close_future = asyncio.Future()
62
+ ffi_client.add_listener('room_event', self._on_room_event)
63
+
64
+ async def close(self):
65
+ self._ffi_handle = None
66
+ # TODO(theomonnom): wait for ffi resp
67
+ self._close_future.set_result(None)
68
+
69
+ async def run(self):
70
+ await self._close_future
71
+
72
+ def _on_room_event(self, event: proto_room.RoomEvent):
73
+ which = event.WhichOneof('message')
74
+ if which == 'participant_connected':
75
+ remote_participant = self._create_remote_participant(
76
+ event.participant_connected.info)
77
+ self.emit('participant_connected', remote_participant)
78
+ elif which == 'participant_disconnected':
79
+ sid = event.participant_disconnected.info.sid
80
+ participant = self._participants.pop(sid)
81
+ self.emit('participant_disconnected', participant)
82
+ elif which == 'track_published':
83
+ participant = self._participants[event.track_published.participant_sid]
84
+ publication = RemoteTrackPublication(
85
+ event.track_published.publication)
86
+ participant._tracks[publication.sid] = publication
87
+ self.emit('track_published', publication, participant)
88
+ elif which == 'track_unpublished':
89
+ participant = self._participants[event.track_unpublished.participant_sid]
90
+ publication = participant._tracks.pop(
91
+ event.track_unpublished.publication_sid)
92
+ self.emit('track_unpublished', publication, participant)
93
+ elif which == 'track_subscribed':
94
+ track_info = event.track_subscribed.track
95
+ participant = self._participants[event.track_subscribed.participant_sid]
96
+ publication = participant._tracks[track_info.sid]
97
+
98
+ if track_info.kind == TrackKind.KIND_VIDEO:
99
+ video_track = RemoteVideoTrack(
100
+ track_info, weakref.ref(self), weakref.ref(participant))
101
+ publication._track = video_track
102
+ self.emit('track_subscribed', video_track,
103
+ publication, participant)
104
+ elif track_info.kind == TrackKind.KIND_AUDIO:
105
+ audio_track = RemoteAudioTrack(
106
+ track_info, weakref.ref(self), weakref.ref(participant))
107
+ publication._track = audio_track
108
+ self.emit('track_subscribed', audio_track,
109
+ publication, participant)
110
+ elif which == 'track_unsubscribed':
111
+ participant = self._participants[event.track_unsubscribed.participant_sid]
112
+ publication = participant._tracks[event.track_unsubscribed.track_sid]
113
+ track = publication._track
114
+ publication._track = None
115
+ self.emit('track_unsubscribed', track, publication, participant)
116
+
117
+ def _create_remote_participant(self, info: proto_participant.ParticipantInfo) -> RemoteParticipant:
118
+ if info.sid in self._participants:
119
+ raise Exception('participant already exists')
120
+
121
+ participant = RemoteParticipant(info)
122
+ self._participants[participant.sid] = participant
123
+
124
+ # add existing track publications
125
+ for publication_info in info.publications:
126
+ publication = RemoteTrackPublication(publication_info)
127
+ participant._tracks[publication.sid] = publication
128
+
129
+ return participant
130
+
131
+ @property
132
+ def sid(self) -> str:
133
+ return self._room_info.sid
134
+
135
+ @property
136
+ def name(self) -> str:
137
+ return self._room_info.name
138
+
139
+ @property
140
+ def metadata(self) -> str:
141
+ return self._room_info.metadata
livekit/track.py ADDED
@@ -0,0 +1,63 @@
1
+ from ._proto import track_pb2 as proto_track
2
+ from ._ffi_client import FfiHandle
3
+ from typing import (Optional, TYPE_CHECKING)
4
+ from weakref import ref
5
+
6
+ if TYPE_CHECKING:
7
+ from livekit import (Room, Participant)
8
+
9
+
10
+ class Track():
11
+ def __init__(self, handle: Optional[FfiHandle], info: proto_track.TrackInfo, room: ref['Room'], participant: ref['Participant']):
12
+ self._info = info
13
+ self._ffi_handle = handle
14
+
15
+ # TODO(theomonnom): Simplify that and use a FfiHandleId?
16
+ # the weak references are needed because when we need to communicate with the
17
+ # ffi_server which track we are referring to, we also need to provide the room
18
+ # and the participant.
19
+ self._room = room
20
+ self._participant = participant
21
+
22
+ @property
23
+ def sid(self) -> str:
24
+ return self._info.sid
25
+
26
+ @property
27
+ def name(self) -> str:
28
+ return self._info.name
29
+
30
+ @property
31
+ def kind(self) -> proto_track.TrackKind:
32
+ return self._info.kind
33
+
34
+ @property
35
+ def stream_state(self) -> proto_track.StreamState:
36
+ return self._info.stream_state
37
+
38
+ @property
39
+ def muted(self) -> bool:
40
+ return self._info.muted
41
+
42
+ def update_info(self, info: proto_track.TrackInfo):
43
+ self._info = info
44
+
45
+
46
+ class LocalAudioTrack(Track):
47
+ def __init__(self, ffi_handle: FfiHandle, info: proto_track.TrackInfo, room: ref['Room'], participant: ref['Participant']):
48
+ super().__init__(ffi_handle, info, room, participant)
49
+
50
+
51
+ class LocalVideoTrack(Track):
52
+ def __init__(self, ffi_handle: FfiHandle, info: proto_track.TrackInfo, room: ref['Room'], participant: ref['Participant']):
53
+ super().__init__(ffi_handle, info, room, participant)
54
+
55
+
56
+ class RemoteAudioTrack(Track):
57
+ def __init__(self, info: proto_track.TrackInfo, room: ref['Room'], participant: ref['Participant']):
58
+ super().__init__(None, info, room, participant)
59
+
60
+
61
+ class RemoteVideoTrack(Track):
62
+ def __init__(self, info: proto_track.TrackInfo, room: ref['Room'], participant: ref['Participant']):
63
+ super().__init__(None, info, room, participant)
@@ -0,0 +1,55 @@
1
+ from livekit._proto import track_pb2 as proto_track
2
+ from ._proto import track_pb2 as proto_track
3
+ from .track import Track
4
+
5
+
6
+ class TrackPublication():
7
+ def __init__(self, info: proto_track.TrackPublicationInfo):
8
+ self._info = info
9
+ self._track: Track = None
10
+
11
+ @property
12
+ def sid(self) -> str:
13
+ return self._info.sid
14
+
15
+ @property
16
+ def name(self) -> str:
17
+ return self._info.name
18
+
19
+ @property
20
+ def kind(self) -> proto_track.TrackKind:
21
+ return self._info.kind
22
+
23
+ @property
24
+ def source(self) -> proto_track.TrackSource:
25
+ return self._info.source
26
+
27
+ @property
28
+ def simulcasted(self) -> bool:
29
+ return self._info.simulcasted
30
+
31
+ @property
32
+ def width(self) -> int:
33
+ return self._info.width
34
+
35
+ @property
36
+ def height(self) -> int:
37
+ return self._info.height
38
+
39
+ @property
40
+ def mime_type(self) -> str:
41
+ return self._info.mime_type
42
+
43
+ @property
44
+ def muted(self) -> bool:
45
+ return self._info.muted
46
+
47
+
48
+ class LocalTrackPublication(TrackPublication):
49
+ def __init__(self, info: proto_track.TrackPublicationInfo):
50
+ super().__init__(info)
51
+
52
+
53
+ class RemoteTrackPublication(TrackPublication):
54
+ def __init__(self, info: proto_track.TrackPublicationInfo):
55
+ super().__init__(info)
livekit/video_frame.py ADDED
@@ -0,0 +1,229 @@
1
+ import ctypes
2
+ from ._ffi_client import FfiHandle
3
+ from ._proto import video_frame_pb2 as proto_video_frame
4
+ from ._proto import ffi_pb2 as proto_ffi
5
+ from ._ffi_client import FfiClient
6
+ from livekit import (VideoRotation, VideoFormatType, VideoFrameBufferType)
7
+
8
+
9
+ class VideoFrame():
10
+ def __init__(self, info: proto_video_frame.VideoFrameInfo) -> None:
11
+ self._info = info
12
+ pass
13
+
14
+ @property
15
+ def timestamp(self) -> int:
16
+ return self._info.timestamp
17
+
18
+ @property
19
+ def rotation(self) -> VideoRotation:
20
+ return self._info.rotation
21
+
22
+
23
+ class VideoFrameBuffer():
24
+ def __init__(self, ffi_handle: FfiHandle, info: proto_video_frame.VideoFrameBufferInfo):
25
+ self._info = info
26
+ self._ffi_handle = ffi_handle
27
+
28
+ @property
29
+ def width(self) -> int:
30
+ return self._info.width
31
+
32
+ @property
33
+ def height(self) -> int:
34
+ return self._info.height
35
+
36
+ @property
37
+ def type(self) -> VideoFrameBufferType:
38
+ return self._info.buffer_type
39
+
40
+ def to_i420(self) -> 'I420Buffer':
41
+ req = proto_ffi.FfiRequest()
42
+ req.to_i420.buffer.id = self._ffi_handle.handle
43
+
44
+ ffi_client = FfiClient()
45
+ resp = ffi_client.request(req)
46
+
47
+ new_info = resp.to_i420.buffer
48
+ ffi_handle = FfiHandle(new_info.handle.id)
49
+ return I420Buffer(ffi_handle, new_info)
50
+
51
+ def to_argb(self, dst: 'ArgbFrame'):
52
+ req = proto_ffi.FfiRequest()
53
+ req.to_argb.buffer.id = self._ffi_handle.handle
54
+ req.to_argb.dst_ptr = ctypes.addressof(dst.data)
55
+ req.to_argb.dst_format = dst.format
56
+ req.to_argb.dst_stride = dst.width
57
+ req.to_argb.dst_width = dst.width
58
+ req.to_argb.dst_height = dst.height
59
+
60
+ ffi_client = FfiClient()
61
+ ffi_client.request(req)
62
+
63
+ @staticmethod
64
+ def create(ffi_handle: FfiHandle, info: proto_video_frame.VideoFrameBufferInfo) -> 'VideoFrameBuffer':
65
+ """
66
+ Create the right class instance from the VideoFrameBufferInfo
67
+ """
68
+
69
+ if info.buffer_type == VideoFrameBufferType.NATIVE:
70
+ return NativeVideoFrameBuffer(ffi_handle, info)
71
+ elif info.buffer_type == VideoFrameBufferType.I420:
72
+ return I420Buffer(ffi_handle, info)
73
+ elif info.buffer_type == VideoFrameBufferType.I420A:
74
+ return I420ABuffer(ffi_handle, info)
75
+ elif info.buffer_type == VideoFrameBufferType.I422:
76
+ return I422Buffer(ffi_handle, info)
77
+ elif info.buffer_type == VideoFrameBufferType.I444:
78
+ return I444Buffer(ffi_handle, info)
79
+ elif info.buffer_type == VideoFrameBufferType.I010:
80
+ return I010Buffer(ffi_handle, info)
81
+ elif info.buffer_type == VideoFrameBufferType.NV12:
82
+ return NV12Buffer(ffi_handle, info)
83
+
84
+
85
+ class NativeVideoFrameBuffer(VideoFrameBuffer):
86
+ def __init__(self, ffi_handle: FfiHandle, info: proto_video_frame.VideoFrameBufferInfo):
87
+ super().__init__(ffi_handle, info)
88
+
89
+
90
+ class PlanarYuvBuffer(VideoFrameBuffer):
91
+ def __init__(self, ffi_handle: FfiHandle, info: proto_video_frame.VideoFrameBufferInfo):
92
+ super().__init__(ffi_handle, info)
93
+
94
+ @property
95
+ def chroma_width(self) -> int:
96
+ return self._info.yuv.chroma_width
97
+
98
+ @property
99
+ def chroma_height(self) -> int:
100
+ return self._info.yuv.chroma_height
101
+
102
+ @property
103
+ def stride_y(self) -> int:
104
+ return self._info.yuv.stride_y
105
+
106
+ @property
107
+ def stride_u(self) -> int:
108
+ return self._info.yuv.stride_u
109
+
110
+ @property
111
+ def stride_v(self) -> int:
112
+ return self._info.yuv.stride_v
113
+
114
+
115
+ class PlanarYuv8Buffer(PlanarYuvBuffer):
116
+ def __init__(self, ffi_handle: FfiHandle, info: proto_video_frame.VideoFrameBufferInfo):
117
+ super().__init__(ffi_handle, info)
118
+
119
+ @property
120
+ def data_y(self):
121
+ arr = ctypes.cast(self._info.yuv.data_y_ptr, ctypes.POINTER(
122
+ ctypes.c_uint8 * self._info.yuv.stride_y * self._info.height)).contents
123
+ return arr
124
+
125
+ @property
126
+ def data_u(self):
127
+ arr = ctypes.cast(self._info.yuv.data_u_ptr, ctypes.POINTER(
128
+ ctypes.c_uint8 * self._info.yuv.stride_u * self._info.yuv.chroma_height)).contents
129
+ return arr
130
+
131
+ @property
132
+ def data_v(self):
133
+ arr = ctypes.cast(self._info.yuv.data_v_ptr, ctypes.POINTER(
134
+ ctypes.c_uint8 * self._info.yuv.stride_v * self._info.yuv.chroma_height)).contents
135
+ return arr
136
+
137
+
138
+ class PlanarYuv16Buffer(PlanarYuvBuffer):
139
+ def __init__(self, ffi_handle: FfiHandle, info: proto_video_frame.VideoFrameBufferInfo):
140
+ super().__init__(ffi_handle, info)
141
+
142
+ @property
143
+ def data_y(self):
144
+ arr = ctypes.cast(self._info.yuv.data_y_ptr, ctypes.POINTER(
145
+ ctypes.c_uint16 * self._info.yuv.stride_y / 2 * self._info.height)).contents
146
+ return arr
147
+
148
+ @property
149
+ def data_u(self):
150
+ arr = ctypes.cast(self._info.yuv.data_u_ptr, ctypes.POINTER(
151
+ ctypes.c_uint16 * self._info.yuv.stride_u / 2 * self._info.yuv.chroma_height)).contents
152
+ return arr
153
+
154
+ @property
155
+ def data_v(self):
156
+ arr = ctypes.cast(self._info.yuv.data_v_ptr, ctypes.POINTER(
157
+ ctypes.c_uint16 * self._info.yuv.stride_v / 2 * self._info.yuv.chroma_height)).contents
158
+ return arr
159
+
160
+
161
+ class BiplanaraYuv8Buffer(VideoFrameBuffer):
162
+ def __init__(self, ffi_handle: FfiHandle, info: proto_video_frame.VideoFrameBufferInfo):
163
+ super().__init__(ffi_handle, info)
164
+
165
+ @property
166
+ def data_y(self):
167
+ arr = ctypes.cast(self._info.bi_yuv.data_y_ptr, ctypes.POINTER(
168
+ ctypes.c_uint8 * self._info.bi_yuv.stride_y * self._info.height)).contents
169
+ return arr
170
+
171
+ @property
172
+ def data_uv(self):
173
+ arr = ctypes.cast(self._info.bi_yuv.data_uv_ptr, ctypes.POINTER(
174
+ ctypes.c_uint8 * self._info.bi_yuv.stride_uv * self._info.bi_yuv.chroma_height)).contents
175
+ return arr
176
+
177
+
178
+ class I420Buffer(PlanarYuv8Buffer):
179
+ def __init__(self, ffi_handle: FfiHandle, info: proto_video_frame.VideoFrameBufferInfo):
180
+ super().__init__(ffi_handle, info)
181
+
182
+
183
+ class I420ABuffer(PlanarYuv8Buffer):
184
+ def __init__(self, ffi_handle: FfiHandle, info: proto_video_frame.VideoFrameBufferInfo):
185
+ super().__init__(ffi_handle, info)
186
+
187
+ @property
188
+ def data_a(self):
189
+ arr = ctypes.cast(self._info.yuv.data_a_ptr, ctypes.POINTER(
190
+ ctypes.c_uint8 * self._info.yuv.stride_a * self._info.height)).contents
191
+ return arr
192
+
193
+
194
+ class I422Buffer(PlanarYuv8Buffer):
195
+ def __init__(self, ffi_handle: FfiHandle, info: proto_video_frame.VideoFrameBufferInfo):
196
+ super().__init__(ffi_handle, info)
197
+
198
+
199
+ class I444Buffer(PlanarYuv8Buffer):
200
+ def __init__(self, ffi_handle: FfiHandle, info: proto_video_frame.VideoFrameBufferInfo):
201
+ super().__init__(ffi_handle, info)
202
+
203
+
204
+ class I010Buffer(PlanarYuv16Buffer):
205
+ def __init__(self, ffi_handle: FfiHandle, info: proto_video_frame.VideoFrameBufferInfo):
206
+ super().__init__(ffi_handle, info)
207
+
208
+
209
+ class NV12Buffer(BiplanaraYuv8Buffer):
210
+ def __init__(self, ffi_handle: FfiHandle, info: proto_video_frame.VideoFrameBufferInfo):
211
+ super().__init__(ffi_handle, info)
212
+
213
+
214
+ class ArgbFrame:
215
+ """
216
+ Mainly used to simplify the usage of to_argb method
217
+ So the users don't need to deal with ctypes
218
+ """
219
+
220
+ def __init__(self, format: VideoFormatType, width: int, height: int) -> None:
221
+ self._format = format
222
+ self.width = width
223
+ self.height = height
224
+ self.data = (ctypes.c_uint8 * (width * height *
225
+ ctypes.sizeof(ctypes.c_uint32)))() # alloc frame
226
+
227
+ @property
228
+ def format(self) -> VideoFormatType:
229
+ return self._format
@@ -0,0 +1,72 @@
1
+ from pyee.asyncio import AsyncIOEventEmitter
2
+ from ._ffi_client import (FfiClient, FfiHandle)
3
+ from livekit import (Track, Room, Participant)
4
+ from ._proto import track_pb2 as proto_track
5
+ from ._proto import ffi_pb2 as proto_ffi
6
+ from ._proto import video_frame_pb2 as proto_video_frame
7
+ from .video_frame import (VideoFrame, VideoFrameBuffer)
8
+
9
+
10
+ class VideoStream(AsyncIOEventEmitter):
11
+ _streams: dict[int, 'VideoStream'] = {}
12
+ _initialized = False
13
+
14
+ @classmethod
15
+ def initalize(cls):
16
+ if cls._initialized:
17
+ return
18
+
19
+ cls._initialized = True
20
+ ffi_client = FfiClient()
21
+ ffi_client.add_listener('video_stream_event',
22
+ cls._on_video_stream_event)
23
+
24
+ @classmethod
25
+ def _on_video_stream_event(cls, event: proto_video_frame.VideoStreamEvent):
26
+ stream = cls._streams.get(event.handle.id)
27
+ if stream is None:
28
+ return
29
+
30
+ which = event.WhichOneof('message')
31
+ if which == 'frame_received':
32
+ frame_info = event.frame_received.frame
33
+ buffer_info = event.frame_received.buffer
34
+ ffi_handle = FfiHandle(buffer_info.handle.id)
35
+
36
+ frame = VideoFrame(frame_info)
37
+ buffer = VideoFrameBuffer.create(ffi_handle, buffer_info)
38
+ stream._on_frame_received(frame, buffer)
39
+
40
+ def __init__(self, track: Track):
41
+ super().__init__()
42
+ self.initalize()
43
+
44
+ room = track._room()
45
+ if room is None:
46
+ raise ValueError("track's room is not valid anymore")
47
+
48
+ participant = track._participant()
49
+ if participant is None:
50
+ raise ValueError("track's participant is not valid anymore")
51
+
52
+ req = proto_ffi.FfiRequest()
53
+ new_video_stream = req.new_video_stream
54
+ new_video_stream.room_handle.id = room._ffi_handle.handle
55
+ new_video_stream.track_sid = track.sid
56
+ new_video_stream.participant_sid = participant.sid
57
+ new_video_stream.type = proto_video_frame.VideoStreamType.VIDEO_STREAM_NATIVE
58
+
59
+ ffi_client = FfiClient()
60
+ resp = ffi_client.request(req)
61
+ stream_info = resp.new_video_stream.stream
62
+
63
+ self._streams[stream_info.handle.id] = self
64
+ self._ffi_handle = FfiHandle(stream_info.handle.id)
65
+ self._info = stream_info
66
+ self._track = track
67
+
68
+ def _on_frame_received(self, frame: VideoFrame, buffer: VideoFrameBuffer):
69
+ self.emit('frame_received', frame, buffer)
70
+
71
+ def __del__(self):
72
+ self._streams.pop(self._ffi_handle.handle, None)
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,22 @@
1
+ Metadata-Version: 2.1
2
+ Name: livekit
3
+ Version: 0.0.1
4
+ Summary: A sample Python project
5
+ Home-page: https://github.com/livekit/client-sdk-python
6
+ Project-URL: Website, https://livekit.io/
7
+ Project-URL: Source, https://github.com/livekit/client-sdk-python/
8
+ Keywords: webrtc,livekit
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: Apache Software License
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.7
14
+ Classifier: Programming Language :: Python :: 3.8
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3 :: Only
18
+ Requires-Python: >=3.7, <4
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ Requires-Dist: pyee (>=9.0.0)
22
+
@@ -0,0 +1,19 @@
1
+ livekit/__init__.py,sha256=HyfFzpIyRxO3OWdEwJ0eamVLOUxjp4dARcCPh1FdnPk,804
2
+ livekit/_ffi_client.py,sha256=5h0dsFI3DfV8OsH-u-JoFLuz5zp1VUoc4IT0XOwFKRw,3163
3
+ livekit/liblivekit_ffi.dylib,sha256=o0RrOCtu6f_Rf2VLaDY12nNwurYEldeu8YlkNZD2SN4,27826513
4
+ livekit/participant.py,sha256=1T1I-HdJBU85l8BBuWF--r5t8wA3X-rTAyVI0tflmsc,851
5
+ livekit/room.py,sha256=fwO-cOpgjprfHMstCCprPmnLpv6r7cQwAdmVcUNwuxw,5738
6
+ livekit/track.py,sha256=vn8pzEMmRIkus1AZ9GPViLmq3mWCFPIANwAa0fAz8t4,2094
7
+ livekit/track_publication.py,sha256=8ucbQ5hip95xOtQJazAJYv9FjORB9KNqt7Jts3VMoY4,1294
8
+ livekit/video_frame.py,sha256=u5XxlJh6WrBO8e-iS-GkxeQXguFG6f3kfiLc_Sdx3Xo,7652
9
+ livekit/video_stream.py,sha256=PrInW4O035Re29VKuULYGGT_KyrldIymI_EZSIcmx1g,2544
10
+ livekit/lib/darwin/arm64/liblivekit_ffi.dylib,sha256=Z-2XX_F3TVZrxfbh_OEsfXZk-sbcNOTecJ4vcbWhXzg,27826513
11
+ livekit/lib/darwin/x86_64/liblivekit_ffi.dylib,sha256=tpGLzJd3xgyqFivM49Saved1NoJPqZmFJjypA56U9Lk,32760856
12
+ livekit/lib/linux/x86_64/liblivekit_ffi.so,sha256=kTB2ENi6H3wEVsIzB7Gm9c03SIV5KZqBedPLs1rDJHs,34487208
13
+ livekit/lib/windows/arm64/livekit_ffi.dll,sha256=ysOLOSt-W-jiOlIz0TfYk4an5CXWIerBdsNqR5zq0Z4,21245952
14
+ livekit/lib/windows/x86_64/livekit_ffi.dll,sha256=0Y3OZqIGuoxh-6O8sXjI_SPTtxS1hoK2yTKE_fPKwJQ,21245952
15
+ livekit-0.0.1.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
16
+ livekit-0.0.1.dist-info/METADATA,sha256=kDOc99GAY7XXunEgxYVlxh56MY090Dc0EwPgX7u0ozU,850
17
+ livekit-0.0.1.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
18
+ livekit-0.0.1.dist-info/top_level.txt,sha256=OoDok3xUmXbZRvOrfvvXB-Juu4DX79dlq188E19YHoo,8
19
+ livekit-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.40.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ livekit