Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,6 @@ cython_debug/
#.idea/

# vscode project settings
.vscode
.vscode

.DS_Store
100 changes: 100 additions & 0 deletions examples/e2ee.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import asyncio
import colorsys
import logging
from signal import SIGINT, SIGTERM

import numpy as np

import livekit

URL = 'ws://localhost:7880'
TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5MDY2MTMyODgsImlzcyI6IkFQSVRzRWZpZFpqclFvWSIsIm5hbWUiOiJuYXRpdmUiLCJuYmYiOjE2NzI2MTMyODgsInN1YiI6Im5hdGl2ZSIsInZpZGVvIjp7InJvb20iOiJ0ZXN0Iiwicm9vbUFkbWluIjp0cnVlLCJyb29tQ3JlYXRlIjp0cnVlLCJyb29tSm9pbiI6dHJ1ZSwicm9vbUxpc3QiOnRydWV9fQ.uSNIangMRu8jZD5mnRYoCHjcsQWCrJXgHCs0aNIgBFY' # noqa


async def publish_frames(source: livekit.VideoSource):
argb_frame = livekit.ArgbFrame(
livekit.VideoFormatType.FORMAT_ARGB, 1280, 720)

arr = np.ctypeslib.as_array(argb_frame.data)

framerate = 1 / 30
hue = 0.0

while True:
frame = livekit.VideoFrame(
0, livekit.VideoRotation.VIDEO_ROTATION_0, argb_frame.to_i420())

rgb = colorsys.hsv_to_rgb(hue, 1.0, 1.0)
rgb = [(x * 255) for x in rgb] # type: ignore

argb_color = np.array(rgb + [255], dtype=np.uint8)
arr.flat[::4] = argb_color[0]
arr.flat[1::4] = argb_color[1]
arr.flat[2::4] = argb_color[2]
arr.flat[3::4] = argb_color[3]

source.capture_frame(frame)

hue += framerate/3 # 3s for a full cycle
if hue >= 1.0:
hue = 0.0

try:
await asyncio.sleep(framerate)
except asyncio.CancelledError:
break


async def main():
room = livekit.Room()

@room.listens_to("e2ee_state_changed")
def on_e2ee_state_changed(participant: livekit.Participant,
state: livekit.E2eeState) -> None:
logging.info("e2ee state changed: %s %s", participant.identity, state)

logging.info("connecting to %s", URL)
try:
e2ee_options = livekit.E2EEOptions()
e2ee_options.key_provider_options.shared_key = b"abcdef" # this is our e2ee key

await room.connect(URL, TOKEN, options=livekit.RoomOptions(
auto_subscribe=True,
e2ee=e2ee_options
))

logging.info("connected to room %s", room.name)
except livekit.ConnectError as e:
logging.error("failed to connect to the room: %s", e)
return False

source = livekit.VideoSource()
source_task = asyncio.create_task(publish_frames(source))

track = livekit.LocalVideoTrack.create_video_track("hue", source)
options = livekit.TrackPublishOptions()
options.source = livekit.TrackSource.SOURCE_CAMERA
publication = await room.local_participant.publish_track(track, options)
logging.info("published track %s", publication.sid)

try:
await room.run()
except asyncio.CancelledError:
logging.info("closing the room")
source_task.cancel()
await source_task
await room.disconnect()


if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, handlers=[
logging.FileHandler("publish_hue.log"), logging.StreamHandler()])

loop = asyncio.get_event_loop()
main_task = asyncio.ensure_future(main())
for signal in [SIGINT, SIGTERM]:
loop.add_signal_handler(signal, main_task.cancel)
try:
loop.run_until_complete(main_task)
finally:
loop.close()
8 changes: 8 additions & 0 deletions livekit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
DataPacketKind,
TrackPublishOptions,
)
from ._proto.e2ee_pb2 import (EncryptionType, E2eeState)
from ._proto.track_pb2 import StreamState, TrackKind, TrackSource
from ._proto.video_frame_pb2 import VideoFormatType, VideoFrameBufferType, VideoRotation
from .audio_frame import AudioFrame
Expand All @@ -36,6 +37,13 @@
RemoteVideoTrack,
Track,
)
from .e2ee import (
E2EEManager,
E2EEOptions,
KeyProviderOptions,
KeyProvider,
FrameCryptor
)
from .track_publication import (
LocalTrackPublication,
RemoteTrackPublication,
Expand Down
58 changes: 29 additions & 29 deletions livekit/_proto/e2ee_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading