Skip to content

Commit 2a98263

Browse files
authored
Add e2ee capability to face_landmark (#55)
1 parent a174b82 commit 2a98263

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

README.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<!--BEGIN_BANNER_IMAGE-->
2-
<picture>
3-
<source media="(prefers-color-scheme: dark)" srcset="/.github/banner_dark.png">
4-
<source media="(prefers-color-scheme: light)" srcset="/.github/banner_light.png">
5-
<img style="width:100%;" alt="The LiveKit icon, the name of the repository and some sample code in the background." src="https://raw.githubusercontent.com/livekit/client-sdk-python/main/.github/banner_light.png">
6-
</picture>
1+
<!--BEGIN_BANNER_IMAGE-->
2+
<picture>
3+
<source media="(prefers-color-scheme: dark)" srcset="/.github/banner_dark.png">
4+
<source media="(prefers-color-scheme: light)" srcset="/.github/banner_light.png">
5+
<img style="width:100%;" alt="The LiveKit icon, the name of the repository and some sample code in the background." src="https://raw.githubusercontent.com/livekit/client-sdk-python/main/.github/banner_light.png">
6+
</picture>
77
<!--END_BANNER_IMAGE-->
88

99
[![pypi-v](https://img.shields.io/pypi/v/livekit.svg)](https://pypi.org/project/livekit/)
@@ -15,6 +15,7 @@ The Livekit Python Client provides a convenient interface for integrating Liveki
1515
Official LiveKit documentation: https://docs.livekit.io/
1616

1717
## Installation
18+
1819
```shell
1920
$ pip install livekit
2021
```
@@ -24,25 +25,34 @@ $ pip install livekit
2425
```python
2526
async def main():
2627
room = livekit.Room()
28+
# By default, autosubscribe is enabled. The participant will be subscribed to
29+
# all published tracks in the room
2730
await room.connect(URL, TOKEN)
2831
logging.info("connected to room %s", room.name)
2932

33+
# participants and tracks that are already available in the room
34+
# participant_connected and track_published events will *not* be emitted for them
35+
for participant in room.participants.items():
36+
for publication in participant.tracks.items():
37+
print("track publication: %s", publication.sid)
38+
3039
@room.on("participant_connected")
3140
def on_participant_connected(participant: livekit.RemoteParticipant):
3241
logging.info(
3342
"participant connected: %s %s", participant.sid, participant.identity)
3443

3544
video_stream = None
45+
46+
# track_subscribed is emitted whenever the local participant is subscribed to a new track
3647
@room.on("track_subscribed")
3748
def on_track_subscribed(track: livekit.Track, publication: livekit.RemoteTrackPublication, participant: livekit.RemoteParticipant):
3849
logging.info("track subscribed: %s", publication.sid)
3950
if track.kind == livekit.TrackKind.KIND_VIDEO:
4051
nonlocal video_stream
4152
video_stream = livekit.VideoStream(track)
4253

43-
@video_stream.on("frame_received")
44-
def on_video_frame(frame: livekit.VideoFrame):
45-
# received a video frame from the track
54+
async for frame in video_stream:
55+
# received a video frame from the track, process it here
4656
pass
4757

4858
await room.run()

examples/e2ee.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def on_e2ee_state_changed(participant: livekit.Participant,
5656
logging.info("connecting to %s", URL)
5757
try:
5858
e2ee_options = livekit.E2EEOptions()
59-
e2ee_options.key_provider_options.shared_key = b"abcdef" # this is our e2ee key
59+
e2ee_options.key_provider_options.shared_key = b"livekitrocks" # this is our e2ee key
6060

6161
await room.connect(URL, TOKEN, options=livekit.RoomOptions(
6262
auto_subscribe=True,

examples/face_landmark/face_landmark.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import asyncio
22
import os
3-
from queue import Queue
3+
import signal
44

55
import cv2
66
import mediapipe as mp
@@ -104,7 +104,19 @@ async def frame_loop(video_stream: livekit.VideoStream) -> None:
104104

105105
async def main() -> None:
106106
room = livekit.Room()
107-
await room.connect(URL, TOKEN)
107+
108+
loop = asyncio.get_event_loop()
109+
loop.add_signal_handler(
110+
signal.SIGINT, lambda: exit(0))
111+
112+
await room.connect(URL, TOKEN, livekit.RoomOptions(
113+
# Unncomment below to enable E2EE
114+
# e2ee=livekit.E2EEOptions(
115+
# key_provider_options=livekit.KeyProviderOptions(
116+
# shared_key=b"livekitrocks"
117+
# )
118+
# ),
119+
))
108120
print("connected to room: " + room.name)
109121

110122
video_stream = None
@@ -113,6 +125,10 @@ async def main() -> None:
113125
def on_track_subscribed(track: livekit.Track, *_):
114126
if track.kind == livekit.TrackKind.KIND_VIDEO:
115127
nonlocal video_stream
128+
# only process the first stream received
129+
if video_stream is not None:
130+
return
131+
print("subscribed to track: " + track.name)
116132
video_stream = livekit.VideoStream(track)
117133
task = asyncio.create_task(frame_loop(video_stream))
118134
tasks.add(task)

0 commit comments

Comments
 (0)