Skip to content

Commit 4657ae1

Browse files
Add support for datachannel encryption (#512)
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 5f32b59 commit 4657ae1

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

livekit-rtc/livekit/rtc/room.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import logging
2020
from dataclasses import dataclass, field
2121
from typing import Callable, Dict, Literal, Optional, cast, Mapping
22+
import warnings
2223

2324
from .event_emitter import EventEmitter
2425
from ._ffi_client import FfiClient, FfiHandle
@@ -62,6 +63,7 @@
6263
"participant_name_changed",
6364
"participant_attributes_changed",
6465
"connection_quality_changed",
66+
"participant_encryption_status_changed",
6567
"data_received",
6668
"sip_dtmf_received",
6769
"transcription_received",
@@ -97,6 +99,8 @@ class RoomOptions:
9799
"""Automatically subscribe to tracks when participants join."""
98100
dynacast: bool = False
99101
e2ee: E2EEOptions | None = None
102+
"""Deprecated, use `encryption` field instead"""
103+
encryption: E2EEOptions | None = None
100104
"""Options for end-to-end encryption."""
101105
rtc_config: RtcConfiguration | None = None
102106
"""WebRTC-related configuration."""
@@ -352,6 +356,8 @@ def on(self, event: EventTypes, callback: Optional[Callable] = None) -> Callable
352356
- Arguments: `participant` (Participant), `old_name` (str), `new_name` (str)
353357
- **"participant_attributes_changed"**: Called when a participant's attributes change.
354358
- Arguments: `changed_attributes` (dict), `participant` (Participant)
359+
- **"participant_encryption_status_changed"**: Called when a participant's encryption status changes.
360+
- Arguments `is_encrypted` (bool), `participant` (Participant)
355361
- **"connection_quality_changed"**: Called when a participant's connection quality changes.
356362
- Arguments: `participant` (Participant), `quality` (ConnectionQuality)
357363
- **"transcription_received"**: Called when a transcription is received.
@@ -419,6 +425,12 @@ def on_participant_connected(participant):
419425
req.connect.options.dynacast = options.dynacast
420426

421427
if options.e2ee:
428+
warnings.warn(
429+
"options.e2ee is deprecated, use options.encryption instead",
430+
DeprecationWarning,
431+
stacklevel=2,
432+
)
433+
422434
req.connect.options.e2ee.encryption_type = options.e2ee.encryption_type
423435
req.connect.options.e2ee.key_provider_options.shared_key = (
424436
options.e2ee.key_provider_options.shared_key # type: ignore
@@ -433,6 +445,21 @@ def on_participant_connected(participant):
433445
options.e2ee.key_provider_options.ratchet_window_size
434446
)
435447

448+
if options.encryption:
449+
req.connect.options.encryption.encryption_type = options.encryption.encryption_type
450+
req.connect.options.encryption.key_provider_options.shared_key = (
451+
options.encryption.key_provider_options.shared_key # type: ignore
452+
)
453+
req.connect.options.encryption.key_provider_options.ratchet_salt = (
454+
options.encryption.key_provider_options.ratchet_salt
455+
)
456+
req.connect.options.encryption.key_provider_options.failure_tolerance = (
457+
options.encryption.key_provider_options.failure_tolerance
458+
)
459+
req.connect.options.encryption.key_provider_options.ratchet_window_size = (
460+
options.encryption.key_provider_options.ratchet_window_size
461+
)
462+
436463
if options.rtc_config:
437464
req.connect.options.rtc_config.ice_transport_type = (
438465
options.rtc_config.ice_transport_type
@@ -460,7 +487,9 @@ def on_participant_connected(participant):
460487

461488
self._ffi_handle = FfiHandle(cb.connect.result.room.handle.id)
462489

463-
self._e2ee_manager = E2EEManager(self._ffi_handle.handle, options.e2ee)
490+
self._e2ee_manager = E2EEManager(
491+
self._ffi_handle.handle, options.encryption or options.e2ee
492+
)
464493

465494
self._info = cb.connect.result.room.info
466495
self._connection_state = ConnectionState.CONN_CONNECTED
@@ -735,6 +764,14 @@ def _on_room_event(self, event: proto_room.RoomEvent):
735764
changed_attributes,
736765
participant,
737766
)
767+
elif which == "participant_encryption_status_changed":
768+
identity = event.participant_encryption_status_changed.participant_identity
769+
participant = self._retrieve_participant(identity)
770+
self.emit(
771+
"participant_encryption_status_changed",
772+
participant,
773+
event.participant_encryption_status_changed.is_encrypted,
774+
)
738775
elif which == "connection_quality_changed":
739776
identity = event.connection_quality_changed.participant_identity
740777
# TODO: pass participant identity

0 commit comments

Comments
 (0)