livekit 1.2.1__py3-none-win_arm64.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/rtc/__init__.py +264 -0
- livekit/rtc/_ffi_client.py +321 -0
- livekit/rtc/_proto/__init__.py +0 -0
- livekit/rtc/_proto/audio_frame_pb2.py +159 -0
- livekit/rtc/_proto/audio_frame_pb2.pyi +1560 -0
- livekit/rtc/_proto/capture_pb2.py +70 -0
- livekit/rtc/_proto/capture_pb2.pyi +559 -0
- livekit/rtc/_proto/data_stream_pb2.py +171 -0
- livekit/rtc/_proto/data_stream_pb2.pyi +1498 -0
- livekit/rtc/_proto/data_track_pb2.py +120 -0
- livekit/rtc/_proto/data_track_pb2.pyi +1073 -0
- livekit/rtc/_proto/e2ee_pb2.py +81 -0
- livekit/rtc/_proto/e2ee_pb2.pyi +591 -0
- livekit/rtc/_proto/ffi_pb2.py +55 -0
- livekit/rtc/_proto/ffi_pb2.pyi +1246 -0
- livekit/rtc/_proto/handle_pb2.py +27 -0
- livekit/rtc/_proto/handle_pb2.pyi +51 -0
- livekit/rtc/_proto/participant_pb2.py +45 -0
- livekit/rtc/_proto/participant_pb2.pyi +313 -0
- livekit/rtc/_proto/room_pb2.py +311 -0
- livekit/rtc/_proto/room_pb2.pyi +3136 -0
- livekit/rtc/_proto/rpc_pb2.py +47 -0
- livekit/rtc/_proto/rpc_pb2.pyi +261 -0
- livekit/rtc/_proto/stats_pb2.py +123 -0
- livekit/rtc/_proto/stats_pb2.pyi +1514 -0
- livekit/rtc/_proto/track_pb2.py +76 -0
- livekit/rtc/_proto/track_pb2.pyi +544 -0
- livekit/rtc/_proto/track_publication_pb2.py +39 -0
- livekit/rtc/_proto/track_publication_pb2.pyi +145 -0
- livekit/rtc/_proto/video_frame_pb2.py +97 -0
- livekit/rtc/_proto/video_frame_pb2.pyi +838 -0
- livekit/rtc/_utils.py +187 -0
- livekit/rtc/apm.py +119 -0
- livekit/rtc/audio_filter.py +23 -0
- livekit/rtc/audio_frame.py +242 -0
- livekit/rtc/audio_mixer.py +247 -0
- livekit/rtc/audio_resampler.py +174 -0
- livekit/rtc/audio_source.py +191 -0
- livekit/rtc/audio_stream.py +354 -0
- livekit/rtc/data_stream.py +640 -0
- livekit/rtc/data_track.py +296 -0
- livekit/rtc/e2ee.py +320 -0
- livekit/rtc/encoded_video.py +127 -0
- livekit/rtc/event_emitter.py +215 -0
- livekit/rtc/frame_processor.py +37 -0
- livekit/rtc/jupyter.py +68 -0
- livekit/rtc/log.py +3 -0
- livekit/rtc/media_devices.py +596 -0
- livekit/rtc/participant.py +1171 -0
- livekit/rtc/platform_audio.py +426 -0
- livekit/rtc/py.typed +0 -0
- livekit/rtc/resources/LICENSE.md +4055 -0
- livekit/rtc/resources/__init__.py +1 -0
- livekit/rtc/resources/jupyter-html/index.html +101 -0
- livekit/rtc/resources/livekit_ffi.dll +0 -0
- livekit/rtc/resources/livekit_ffi.h +42 -0
- livekit/rtc/room.py +1304 -0
- livekit/rtc/rpc.py +244 -0
- livekit/rtc/synchronizer.py +202 -0
- livekit/rtc/track.py +264 -0
- livekit/rtc/track_publication.py +145 -0
- livekit/rtc/transcription.py +19 -0
- livekit/rtc/utils.py +138 -0
- livekit/rtc/version.py +1 -0
- livekit/rtc/video_frame.py +347 -0
- livekit/rtc/video_source.py +74 -0
- livekit/rtc/video_stream.py +191 -0
- livekit-1.2.1.dist-info/METADATA +80 -0
- livekit-1.2.1.dist-info/RECORD +71 -0
- livekit-1.2.1.dist-info/WHEEL +5 -0
- livekit-1.2.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
# Copyright 2023 LiveKit, Inc.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
from dataclasses import dataclass
|
|
18
|
+
from typing import AsyncIterator, Optional
|
|
19
|
+
|
|
20
|
+
from ._ffi_client import FfiClient, FfiHandle
|
|
21
|
+
from ._proto import ffi_pb2 as proto_ffi
|
|
22
|
+
from ._proto import data_track_pb2 as proto_data_track
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class SubscribeDataTrackError(Exception):
|
|
26
|
+
"""An error that can occur when subscribing to a data track."""
|
|
27
|
+
|
|
28
|
+
def __init__(self, message: str) -> None:
|
|
29
|
+
self.message = message
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class PushFrameError(Exception):
|
|
33
|
+
"""Frame could not be pushed to a data track.
|
|
34
|
+
|
|
35
|
+
Pushing a frame can fail for several reasons:
|
|
36
|
+
|
|
37
|
+
- The track has been unpublished by the local participant or SFU
|
|
38
|
+
- The room is no longer connected
|
|
39
|
+
- Frames are being pushed too fast
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
def __init__(self, message: str) -> None:
|
|
43
|
+
self.message = message
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@dataclass
|
|
47
|
+
class DataTrackInfo:
|
|
48
|
+
"""Information about a published data track."""
|
|
49
|
+
|
|
50
|
+
sid: str
|
|
51
|
+
"""Unique track identifier assigned by the SFU.
|
|
52
|
+
|
|
53
|
+
This identifier may change if a reconnect occurs. Use ``name`` if a
|
|
54
|
+
stable identifier is needed.
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
name: str
|
|
58
|
+
"""Name of the track assigned by the publisher."""
|
|
59
|
+
|
|
60
|
+
uses_e2ee: bool
|
|
61
|
+
"""Whether or not frames sent on the track use end-to-end encryption."""
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass
|
|
65
|
+
class DataTrackFrame:
|
|
66
|
+
"""A frame published on a data track, consisting of a payload and optional metadata."""
|
|
67
|
+
|
|
68
|
+
payload: bytes
|
|
69
|
+
"""The frame's payload."""
|
|
70
|
+
|
|
71
|
+
user_timestamp: Optional[int] = None
|
|
72
|
+
"""The frame's user timestamp, if one is associated."""
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class LocalDataTrack:
|
|
76
|
+
"""Data track published by the local participant."""
|
|
77
|
+
|
|
78
|
+
def __init__(self, owned_info: proto_data_track.OwnedLocalDataTrack) -> None:
|
|
79
|
+
self._info = DataTrackInfo(
|
|
80
|
+
sid=owned_info.info.sid,
|
|
81
|
+
name=owned_info.info.name,
|
|
82
|
+
uses_e2ee=owned_info.info.uses_e2ee,
|
|
83
|
+
)
|
|
84
|
+
self._ffi_handle = FfiHandle(owned_info.handle.id)
|
|
85
|
+
|
|
86
|
+
@property
|
|
87
|
+
def info(self) -> DataTrackInfo:
|
|
88
|
+
"""Information about the data track."""
|
|
89
|
+
return self._info
|
|
90
|
+
|
|
91
|
+
def try_push(self, frame: DataTrackFrame) -> None:
|
|
92
|
+
"""Try pushing a frame to subscribers of the track.
|
|
93
|
+
|
|
94
|
+
See :class:`DataTrackFrame` for how to construct a frame and attach metadata.
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
frame: The data track frame to send.
|
|
98
|
+
|
|
99
|
+
Raises:
|
|
100
|
+
PushFrameError: If the push fails.
|
|
101
|
+
"""
|
|
102
|
+
proto_frame = proto_data_track.DataTrackFrame(payload=bytes(frame.payload))
|
|
103
|
+
if frame.user_timestamp is not None:
|
|
104
|
+
proto_frame.user_timestamp = frame.user_timestamp
|
|
105
|
+
|
|
106
|
+
req = proto_ffi.FfiRequest()
|
|
107
|
+
req.local_data_track_try_push.track_handle = self._ffi_handle.handle
|
|
108
|
+
req.local_data_track_try_push.frame.CopyFrom(proto_frame)
|
|
109
|
+
|
|
110
|
+
resp = FfiClient.instance.request(req)
|
|
111
|
+
if resp.local_data_track_try_push.HasField("error"):
|
|
112
|
+
raise PushFrameError(resp.local_data_track_try_push.error.message)
|
|
113
|
+
|
|
114
|
+
def is_published(self) -> bool:
|
|
115
|
+
"""Whether or not the track is still published."""
|
|
116
|
+
req = proto_ffi.FfiRequest()
|
|
117
|
+
req.local_data_track_is_published.track_handle = self._ffi_handle.handle
|
|
118
|
+
|
|
119
|
+
resp = FfiClient.instance.request(req)
|
|
120
|
+
return bool(resp.local_data_track_is_published.is_published)
|
|
121
|
+
|
|
122
|
+
async def unpublish(self) -> None:
|
|
123
|
+
"""Unpublishes the track."""
|
|
124
|
+
req = proto_ffi.FfiRequest()
|
|
125
|
+
req.local_data_track_unpublish.track_handle = self._ffi_handle.handle
|
|
126
|
+
FfiClient.instance.request(req)
|
|
127
|
+
|
|
128
|
+
def __repr__(self) -> str:
|
|
129
|
+
return f"rtc.LocalDataTrack(sid={self._info.sid}, name={self._info.name})"
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
class RemoteDataTrack:
|
|
133
|
+
"""Data track published by a remote participant."""
|
|
134
|
+
|
|
135
|
+
def __init__(self, owned_info: proto_data_track.OwnedRemoteDataTrack) -> None:
|
|
136
|
+
self._info = DataTrackInfo(
|
|
137
|
+
sid=owned_info.info.sid,
|
|
138
|
+
name=owned_info.info.name,
|
|
139
|
+
uses_e2ee=owned_info.info.uses_e2ee,
|
|
140
|
+
)
|
|
141
|
+
self._ffi_handle = FfiHandle(owned_info.handle.id)
|
|
142
|
+
self._publisher_identity = owned_info.publisher_identity
|
|
143
|
+
|
|
144
|
+
@property
|
|
145
|
+
def info(self) -> DataTrackInfo:
|
|
146
|
+
"""Information about the data track."""
|
|
147
|
+
return self._info
|
|
148
|
+
|
|
149
|
+
@property
|
|
150
|
+
def publisher_identity(self) -> str:
|
|
151
|
+
"""Identity of the participant who published the track."""
|
|
152
|
+
return self._publisher_identity
|
|
153
|
+
|
|
154
|
+
def subscribe(self, *, buffer_size: Optional[int] = None) -> DataTrackStream:
|
|
155
|
+
"""Subscribes to the data track to receive frames.
|
|
156
|
+
|
|
157
|
+
Args:
|
|
158
|
+
buffer_size: Maximum number of received frames to buffer internally.
|
|
159
|
+
When ``None``, the default buffer size is used.
|
|
160
|
+
Zero is not a valid buffer size; if a value of zero is provided, it will be clamped to one.
|
|
161
|
+
|
|
162
|
+
Returns a :class:`DataTrackStream` that yields
|
|
163
|
+
:class:`DataTrackFrame` instances as they arrive. If the
|
|
164
|
+
subscription encounters an error, it is raised as
|
|
165
|
+
:class:`SubscribeDataTrackError` when iteration ends.
|
|
166
|
+
"""
|
|
167
|
+
opts = proto_data_track.DataTrackSubscribeOptions()
|
|
168
|
+
if buffer_size is not None:
|
|
169
|
+
opts.buffer_size = buffer_size
|
|
170
|
+
|
|
171
|
+
req = proto_ffi.FfiRequest()
|
|
172
|
+
req.subscribe_data_track.track_handle = self._ffi_handle.handle
|
|
173
|
+
req.subscribe_data_track.options.CopyFrom(opts)
|
|
174
|
+
|
|
175
|
+
resp = FfiClient.instance.request(req)
|
|
176
|
+
return DataTrackStream(resp.subscribe_data_track.stream)
|
|
177
|
+
|
|
178
|
+
def is_published(self) -> bool:
|
|
179
|
+
"""Whether or not the track is still published."""
|
|
180
|
+
req = proto_ffi.FfiRequest()
|
|
181
|
+
req.remote_data_track_is_published.track_handle = self._ffi_handle.handle
|
|
182
|
+
|
|
183
|
+
resp = FfiClient.instance.request(req)
|
|
184
|
+
return bool(resp.remote_data_track_is_published.is_published)
|
|
185
|
+
|
|
186
|
+
def set_pipeline_options(self, *, max_partial_frames: Optional[int] = None) -> None:
|
|
187
|
+
"""Configures options for the pipeline handling incoming packets for this track.
|
|
188
|
+
|
|
189
|
+
These options apply to all current and future subscriptions of this track, and
|
|
190
|
+
may be set at any time. New options take effect with the next received packet.
|
|
191
|
+
|
|
192
|
+
Args:
|
|
193
|
+
max_partial_frames: Maximum number of partial frames the depacketizer will
|
|
194
|
+
track concurrently for this track. Defaults to 1. Higher values give more
|
|
195
|
+
out-of-order tolerance for high-frequency senders at the cost of additional
|
|
196
|
+
buffering. Zero is not a valid value; if a value of zero is provided, it
|
|
197
|
+
will be clamped to one. When ``None``, the current value is kept.
|
|
198
|
+
"""
|
|
199
|
+
opts = proto_data_track.RemoteDataTrackPipelineOptions()
|
|
200
|
+
if max_partial_frames is not None:
|
|
201
|
+
opts.max_partial_frames = max_partial_frames
|
|
202
|
+
|
|
203
|
+
req = proto_ffi.FfiRequest()
|
|
204
|
+
req.remote_data_track_set_pipeline_options.track_handle = self._ffi_handle.handle
|
|
205
|
+
req.remote_data_track_set_pipeline_options.options.CopyFrom(opts)
|
|
206
|
+
|
|
207
|
+
FfiClient.instance.request(req)
|
|
208
|
+
|
|
209
|
+
def __repr__(self) -> str:
|
|
210
|
+
return (
|
|
211
|
+
f"rtc.RemoteDataTrack(sid={self._info.sid}, name={self._info.name}, "
|
|
212
|
+
f"publisher_identity={self._publisher_identity})"
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
class DataTrackStream:
|
|
217
|
+
"""An active subscription to a remote data track.
|
|
218
|
+
|
|
219
|
+
Use as an async iterator to receive frames::
|
|
220
|
+
|
|
221
|
+
stream = remote_track.subscribe()
|
|
222
|
+
async for frame in stream:
|
|
223
|
+
process(frame.payload)
|
|
224
|
+
|
|
225
|
+
Dropping or closing the stream unsubscribes from the track.
|
|
226
|
+
|
|
227
|
+
If subscribing to the track fails, :class:`SubscribeDataTrackError`
|
|
228
|
+
is raised when iteration ends instead of a normal ``StopAsyncIteration``.
|
|
229
|
+
"""
|
|
230
|
+
|
|
231
|
+
def __init__(self, owned_info: proto_data_track.OwnedDataTrackStream) -> None:
|
|
232
|
+
self._ffi_handle = FfiHandle(owned_info.handle.id)
|
|
233
|
+
handle_id = owned_info.handle.id
|
|
234
|
+
|
|
235
|
+
self._queue = FfiClient.instance.queue.subscribe(
|
|
236
|
+
filter_fn=lambda e: (
|
|
237
|
+
e.WhichOneof("message") == "data_track_stream_event"
|
|
238
|
+
and e.data_track_stream_event.stream_handle == handle_id
|
|
239
|
+
),
|
|
240
|
+
)
|
|
241
|
+
self._closed = False
|
|
242
|
+
|
|
243
|
+
async def read(self) -> Optional[DataTrackFrame]:
|
|
244
|
+
"""Read a single frame, or ``None`` if the stream has ended."""
|
|
245
|
+
try:
|
|
246
|
+
return await self.__anext__()
|
|
247
|
+
except StopAsyncIteration:
|
|
248
|
+
return None
|
|
249
|
+
|
|
250
|
+
def __aiter__(self) -> AsyncIterator[DataTrackFrame]:
|
|
251
|
+
return self
|
|
252
|
+
|
|
253
|
+
async def __anext__(self) -> DataTrackFrame:
|
|
254
|
+
if self._closed:
|
|
255
|
+
raise StopAsyncIteration
|
|
256
|
+
|
|
257
|
+
self._send_read_request()
|
|
258
|
+
event: proto_ffi.FfiEvent = await self._queue.get()
|
|
259
|
+
stream_event = event.data_track_stream_event
|
|
260
|
+
detail = stream_event.WhichOneof("detail")
|
|
261
|
+
|
|
262
|
+
if detail == "frame_received":
|
|
263
|
+
proto_frame = stream_event.frame_received.frame
|
|
264
|
+
user_ts: Optional[int] = None
|
|
265
|
+
if proto_frame.HasField("user_timestamp"):
|
|
266
|
+
user_ts = proto_frame.user_timestamp
|
|
267
|
+
return DataTrackFrame(
|
|
268
|
+
payload=proto_frame.payload,
|
|
269
|
+
user_timestamp=user_ts,
|
|
270
|
+
)
|
|
271
|
+
elif detail == "eos":
|
|
272
|
+
self._close()
|
|
273
|
+
if stream_event.eos.HasField("error"):
|
|
274
|
+
raise SubscribeDataTrackError(stream_event.eos.error.message)
|
|
275
|
+
raise StopAsyncIteration
|
|
276
|
+
else:
|
|
277
|
+
self._close()
|
|
278
|
+
raise StopAsyncIteration
|
|
279
|
+
|
|
280
|
+
def _send_read_request(self) -> None:
|
|
281
|
+
req = proto_ffi.FfiRequest()
|
|
282
|
+
req.data_track_stream_read.stream_handle = self._ffi_handle.handle
|
|
283
|
+
FfiClient.instance.request(req)
|
|
284
|
+
|
|
285
|
+
def _close(self) -> None:
|
|
286
|
+
if not self._closed:
|
|
287
|
+
self._closed = True
|
|
288
|
+
FfiClient.instance.queue.unsubscribe(self._queue)
|
|
289
|
+
|
|
290
|
+
def close(self) -> None:
|
|
291
|
+
"""Explicitly close the subscription and unsubscribe."""
|
|
292
|
+
self._close()
|
|
293
|
+
self._ffi_handle.dispose()
|
|
294
|
+
|
|
295
|
+
async def aclose(self) -> None:
|
|
296
|
+
self.close()
|
livekit/rtc/e2ee.py
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
# Copyright 2023 LiveKit, Inc.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
from dataclasses import dataclass, field
|
|
16
|
+
from typing import List, Optional, cast
|
|
17
|
+
|
|
18
|
+
from ._ffi_client import FfiClient
|
|
19
|
+
from ._proto import e2ee_pb2 as proto_e2ee
|
|
20
|
+
from ._proto import ffi_pb2 as proto_ffi
|
|
21
|
+
|
|
22
|
+
DEFAULT_RATCHET_SALT = b"LKFrameEncryptionKey"
|
|
23
|
+
DEFAULT_RATCHET_WINDOW_SIZE = 16
|
|
24
|
+
DEFAULT_FAILURE_TOLERANCE = -1
|
|
25
|
+
DEFAULT_KEY_RING_SIZE = 16
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass
|
|
29
|
+
class KeyProviderOptions:
|
|
30
|
+
shared_key: Optional[bytes] = None
|
|
31
|
+
ratchet_salt: bytes = DEFAULT_RATCHET_SALT
|
|
32
|
+
ratchet_window_size: int = DEFAULT_RATCHET_WINDOW_SIZE
|
|
33
|
+
failure_tolerance: int = DEFAULT_FAILURE_TOLERANCE
|
|
34
|
+
key_ring_size: int = DEFAULT_KEY_RING_SIZE
|
|
35
|
+
key_derivation_function: proto_e2ee.KeyDerivationFunction.ValueType = (
|
|
36
|
+
proto_e2ee.KeyDerivationFunction.PBKDF2
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass
|
|
41
|
+
class E2EEOptions:
|
|
42
|
+
key_provider_options: KeyProviderOptions = field(default_factory=KeyProviderOptions)
|
|
43
|
+
encryption_type: proto_e2ee.EncryptionType.ValueType = proto_e2ee.EncryptionType.GCM
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class KeyProvider:
|
|
47
|
+
def __init__(self, room_handle: int, options: KeyProviderOptions):
|
|
48
|
+
self._options = options
|
|
49
|
+
self._room_handle = room_handle
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def options(self) -> KeyProviderOptions:
|
|
53
|
+
return self._options
|
|
54
|
+
|
|
55
|
+
def set_shared_key(self, key: bytes, key_index: int) -> None:
|
|
56
|
+
"""Sets the shared encryption key.
|
|
57
|
+
|
|
58
|
+
Parameters:
|
|
59
|
+
key (bytes): The new shared key.
|
|
60
|
+
key_index (int): The index of the key.
|
|
61
|
+
|
|
62
|
+
Example:
|
|
63
|
+
```python
|
|
64
|
+
key_provider.set_shared_key(b"my_shared_key", key_index=1)
|
|
65
|
+
```
|
|
66
|
+
"""
|
|
67
|
+
req = proto_ffi.FfiRequest()
|
|
68
|
+
req.e2ee.room_handle = self._room_handle
|
|
69
|
+
req.e2ee.set_shared_key.key_index = key_index
|
|
70
|
+
req.e2ee.set_shared_key.shared_key = key
|
|
71
|
+
FfiClient.instance.request(req)
|
|
72
|
+
|
|
73
|
+
def export_shared_key(self, key_index: int) -> bytes:
|
|
74
|
+
"""Exports the shared encryption key.
|
|
75
|
+
|
|
76
|
+
Parameters:
|
|
77
|
+
key_index (int): The index of the key to export.
|
|
78
|
+
|
|
79
|
+
Returns:
|
|
80
|
+
bytes: The exported shared key.
|
|
81
|
+
|
|
82
|
+
Example:
|
|
83
|
+
```python
|
|
84
|
+
key = key_provider.export_shared_key(key_index=1)
|
|
85
|
+
```
|
|
86
|
+
"""
|
|
87
|
+
req = proto_ffi.FfiRequest()
|
|
88
|
+
req.e2ee.room_handle = self._room_handle
|
|
89
|
+
req.e2ee.get_shared_key.key_index = key_index
|
|
90
|
+
resp = FfiClient.instance.request(req)
|
|
91
|
+
key = resp.e2ee.get_shared_key.key
|
|
92
|
+
return cast(bytes, key)
|
|
93
|
+
|
|
94
|
+
def ratchet_shared_key(self, key_index: int) -> bytes:
|
|
95
|
+
"""Ratchets the shared encryption key to a new key.
|
|
96
|
+
|
|
97
|
+
Parameters:
|
|
98
|
+
key_index (int): The index of the key to ratchet.
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
bytes: The new ratcheted shared key.
|
|
102
|
+
|
|
103
|
+
Example:
|
|
104
|
+
```python
|
|
105
|
+
new_key = key_provider.ratchet_shared_key(key_index=1)
|
|
106
|
+
```
|
|
107
|
+
"""
|
|
108
|
+
req = proto_ffi.FfiRequest()
|
|
109
|
+
req.e2ee.room_handle = self._room_handle
|
|
110
|
+
req.e2ee.ratchet_shared_key.key_index = key_index
|
|
111
|
+
|
|
112
|
+
resp = FfiClient.instance.request(req)
|
|
113
|
+
|
|
114
|
+
new_key = resp.e2ee.ratchet_shared_key.new_key
|
|
115
|
+
return cast(bytes, new_key)
|
|
116
|
+
|
|
117
|
+
def set_key(self, participant_identity: str, key: bytes, key_index: int) -> None:
|
|
118
|
+
"""Sets the encryption key for a specific participant.
|
|
119
|
+
|
|
120
|
+
Parameters:
|
|
121
|
+
participant_identity (str): The identity of the participant.
|
|
122
|
+
key (bytes): The encryption key to set.
|
|
123
|
+
key_index (int): The index of the key.
|
|
124
|
+
|
|
125
|
+
Example:
|
|
126
|
+
```python
|
|
127
|
+
key_provider.set_key("participant123", b"participant_key", key_index=2)
|
|
128
|
+
```
|
|
129
|
+
"""
|
|
130
|
+
req = proto_ffi.FfiRequest()
|
|
131
|
+
req.e2ee.room_handle = self._room_handle
|
|
132
|
+
req.e2ee.set_key.participant_identity = participant_identity
|
|
133
|
+
req.e2ee.set_key.key_index = key_index
|
|
134
|
+
req.e2ee.set_key.key = key
|
|
135
|
+
|
|
136
|
+
self.key_index = key_index
|
|
137
|
+
FfiClient.instance.request(req)
|
|
138
|
+
|
|
139
|
+
def export_key(self, participant_identity: str, key_index: int) -> bytes:
|
|
140
|
+
"""Exports the encryption key for a specific participant.
|
|
141
|
+
|
|
142
|
+
Parameters:
|
|
143
|
+
participant_identity (str): The identity of the participant.
|
|
144
|
+
key_index (int): The index of the key to export.
|
|
145
|
+
|
|
146
|
+
Returns:
|
|
147
|
+
bytes: The exported key.
|
|
148
|
+
|
|
149
|
+
Example:
|
|
150
|
+
```python
|
|
151
|
+
key = key_provider.export_key("participant123", key_index=2)
|
|
152
|
+
```
|
|
153
|
+
"""
|
|
154
|
+
req = proto_ffi.FfiRequest()
|
|
155
|
+
req.e2ee.room_handle = self._room_handle
|
|
156
|
+
req.e2ee.get_key.participant_identity = participant_identity
|
|
157
|
+
req.e2ee.get_key.key_index = key_index
|
|
158
|
+
resp = FfiClient.instance.request(req)
|
|
159
|
+
key = resp.e2ee.get_key.key
|
|
160
|
+
return cast(bytes, key)
|
|
161
|
+
|
|
162
|
+
def ratchet_key(self, participant_identity: str, key_index: int) -> bytes:
|
|
163
|
+
"""Ratchets the encryption key for a specific participant to a new key.
|
|
164
|
+
|
|
165
|
+
Parameters:
|
|
166
|
+
participant_identity (str): The identity of the participant.
|
|
167
|
+
key_index (int): The index of the key to ratchet.
|
|
168
|
+
|
|
169
|
+
Returns:
|
|
170
|
+
bytes: The new ratcheted key.
|
|
171
|
+
|
|
172
|
+
Example:
|
|
173
|
+
```python
|
|
174
|
+
new_key = key_provider.ratchet_key("participant123", key_index=2)
|
|
175
|
+
```
|
|
176
|
+
"""
|
|
177
|
+
req = proto_ffi.FfiRequest()
|
|
178
|
+
req.e2ee.room_handle = self._room_handle
|
|
179
|
+
req.e2ee.ratchet_key.participant_identity = participant_identity
|
|
180
|
+
req.e2ee.ratchet_key.key_index = key_index
|
|
181
|
+
|
|
182
|
+
resp = FfiClient.instance.request(req)
|
|
183
|
+
new_key = resp.e2ee.ratchet_key.new_key
|
|
184
|
+
return cast(bytes, new_key)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
class FrameCryptor:
|
|
188
|
+
def __init__(
|
|
189
|
+
self,
|
|
190
|
+
room_handle: int,
|
|
191
|
+
participant_identity: str,
|
|
192
|
+
track_sid: str,
|
|
193
|
+
key_index: int,
|
|
194
|
+
enabled: bool,
|
|
195
|
+
):
|
|
196
|
+
self._room_handle = room_handle
|
|
197
|
+
self._enabled = enabled
|
|
198
|
+
self._participant_identity = participant_identity
|
|
199
|
+
self._track_sid = track_sid
|
|
200
|
+
self._key_index = key_index
|
|
201
|
+
|
|
202
|
+
@property
|
|
203
|
+
def participant_identity(self) -> str:
|
|
204
|
+
return self._participant_identity
|
|
205
|
+
|
|
206
|
+
@property
|
|
207
|
+
def track_sid(self) -> str:
|
|
208
|
+
return self._track_sid
|
|
209
|
+
|
|
210
|
+
@property
|
|
211
|
+
def key_index(self) -> int:
|
|
212
|
+
return self._key_index
|
|
213
|
+
|
|
214
|
+
@property
|
|
215
|
+
def enabled(self) -> bool:
|
|
216
|
+
return self._enabled
|
|
217
|
+
|
|
218
|
+
def set_enabled(self, enabled: bool) -> None:
|
|
219
|
+
"""Enables or disables frame encryption.
|
|
220
|
+
|
|
221
|
+
Parameters:
|
|
222
|
+
enabled (bool): True to enable, False to disable.
|
|
223
|
+
|
|
224
|
+
Example:
|
|
225
|
+
```python
|
|
226
|
+
frame_cryptor.set_enabled(True)
|
|
227
|
+
```
|
|
228
|
+
"""
|
|
229
|
+
self._enabled = enabled
|
|
230
|
+
req = proto_ffi.FfiRequest()
|
|
231
|
+
req.e2ee.room_handle = self._room_handle
|
|
232
|
+
req.e2ee.cryptor_set_enabled.participant_identity = self._participant_identity
|
|
233
|
+
req.e2ee.cryptor_set_enabled.track_sid = self._track_sid
|
|
234
|
+
req.e2ee.cryptor_set_enabled.enabled = enabled
|
|
235
|
+
FfiClient.instance.request(req)
|
|
236
|
+
|
|
237
|
+
def set_key_index(self, key_index: int) -> None:
|
|
238
|
+
"""Sets the key index for encryption/decryption.
|
|
239
|
+
|
|
240
|
+
Parameters:
|
|
241
|
+
key_index (int): The new key index.
|
|
242
|
+
|
|
243
|
+
Example:
|
|
244
|
+
```python
|
|
245
|
+
frame_cryptor.set_key_index(3)
|
|
246
|
+
```
|
|
247
|
+
"""
|
|
248
|
+
self._key_index = key_index
|
|
249
|
+
req = proto_ffi.FfiRequest()
|
|
250
|
+
req.e2ee.room_handle = self._room_handle
|
|
251
|
+
req.e2ee.cryptor_set_key_index.participant_identity = self._participant_identity
|
|
252
|
+
req.e2ee.cryptor_set_key_index.track_sid = self._track_sid
|
|
253
|
+
req.e2ee.cryptor_set_key_index.key_index = key_index
|
|
254
|
+
FfiClient.instance.request(req)
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
class E2EEManager:
|
|
258
|
+
def __init__(self, room_handle: int, options: Optional[E2EEOptions]):
|
|
259
|
+
self.options = options
|
|
260
|
+
self._room_handle = room_handle
|
|
261
|
+
self._enabled = options is not None
|
|
262
|
+
|
|
263
|
+
if options is not None:
|
|
264
|
+
self._key_provider = KeyProvider(self._room_handle, options.key_provider_options)
|
|
265
|
+
|
|
266
|
+
@property
|
|
267
|
+
def key_provider(self) -> Optional[KeyProvider]:
|
|
268
|
+
return self._key_provider
|
|
269
|
+
|
|
270
|
+
@property
|
|
271
|
+
def enabled(self) -> bool:
|
|
272
|
+
return self._enabled
|
|
273
|
+
|
|
274
|
+
def set_enabled(self, enabled: bool) -> None:
|
|
275
|
+
"""Enables or disables end-to-end encryption.
|
|
276
|
+
|
|
277
|
+
Parameters:
|
|
278
|
+
enabled (bool): True to enable, False to disable.
|
|
279
|
+
|
|
280
|
+
Example:
|
|
281
|
+
```python
|
|
282
|
+
e2ee_manager.set_enabled(True)
|
|
283
|
+
```
|
|
284
|
+
"""
|
|
285
|
+
self._enabled = enabled
|
|
286
|
+
req = proto_ffi.FfiRequest()
|
|
287
|
+
req.e2ee.room_handle = self._room_handle
|
|
288
|
+
req.e2ee.manager_set_enabled.enabled = enabled
|
|
289
|
+
FfiClient.instance.request(req)
|
|
290
|
+
|
|
291
|
+
def frame_cryptors(self) -> List[FrameCryptor]:
|
|
292
|
+
"""Retrieves the list of frame cryptors for participants.
|
|
293
|
+
|
|
294
|
+
Returns:
|
|
295
|
+
List[FrameCryptor]: A list of FrameCryptor instances.
|
|
296
|
+
|
|
297
|
+
Example:
|
|
298
|
+
```python
|
|
299
|
+
cryptors = e2ee_manager.frame_cryptors()
|
|
300
|
+
for cryptor in cryptors:
|
|
301
|
+
print(cryptor.participant_identity)
|
|
302
|
+
```
|
|
303
|
+
"""
|
|
304
|
+
req = proto_ffi.FfiRequest()
|
|
305
|
+
req.e2ee.room_handle = self._room_handle
|
|
306
|
+
req.e2ee.manager_get_frame_cryptors.SetInParent()
|
|
307
|
+
|
|
308
|
+
resp = FfiClient.instance.request(req)
|
|
309
|
+
frame_cryptors = []
|
|
310
|
+
for frame_cryptor in resp.e2ee.manager_get_frame_cryptors.frame_cryptors:
|
|
311
|
+
frame_cryptors.append(
|
|
312
|
+
FrameCryptor(
|
|
313
|
+
self._room_handle,
|
|
314
|
+
frame_cryptor.participant_identity,
|
|
315
|
+
frame_cryptor.track_sid,
|
|
316
|
+
frame_cryptor.key_index,
|
|
317
|
+
frame_cryptor.enabled,
|
|
318
|
+
)
|
|
319
|
+
)
|
|
320
|
+
return frame_cryptors
|