From 18d10633ab9297e60320b7212d5534235700dd54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Monnom?= Date: Sun, 5 Nov 2023 18:27:53 -0800 Subject: [PATCH] fix: add resolution arguments to VideoSource (#88) --- examples/e2ee.py | 11 ++++++----- examples/publish_hue.py | 5 +++-- livekit-rtc/livekit/rtc/video_source.py | 4 +++- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/examples/e2ee.py b/examples/e2ee.py index f3816e8a..d9384798 100644 --- a/examples/e2ee.py +++ b/examples/e2ee.py @@ -11,9 +11,10 @@ # ("livekitrocks") this is our shared key, it must match the one used by your clients SHARED_KEY = b"livekitrocks" +WIDTH, HEIGHT = 1280, 720 async def draw_cube(source: rtc.VideoSource): - W, H, MID_W, MID_H = 1280, 720, 640, 360 + MID_W, MID_H = 640, 360 cube_size = 60 vertices = ( np.array( @@ -45,7 +46,7 @@ async def draw_cube(source: rtc.VideoSource): [3, 7], ] - frame = rtc.ArgbFrame.create(rtc.VideoFormatType.FORMAT_ARGB, W, H) + frame = rtc.ArgbFrame.create(rtc.VideoFormatType.FORMAT_ARGB, WIDTH, HEIGHT) arr = np.frombuffer(frame.data, dtype=np.uint8) angle = 0 @@ -82,8 +83,8 @@ async def draw_cube(source: rtc.VideoSource): ) for dx in [-1, 0, 1]: for dy in [-1, 0, 1]: - if 0 <= x + dx < W and 0 <= y + dy < H: - idx = (y + dy) * W * 4 + (x + dx) * 4 + if 0 <= x + dx < WIDTH and 0 <= y + dy < HEIGHT: + idx = (y + dy) * WIDTH * 4 + (x + dx) * 4 arr[idx : idx + 4] = [255, 255, 255, 255] f = rtc.VideoFrame(0, rtc.VideoRotation.VIDEO_ROTATION_0, frame.to_i420()) @@ -116,7 +117,7 @@ def on_e2ee_state_changed( return False # publish a track - source = rtc.VideoSource() + source = rtc.VideoSource(WIDTH, HEIGHT) track = rtc.LocalVideoTrack.create_video_track("cube", source) options = rtc.TrackPublishOptions() options.source = rtc.TrackSource.SOURCE_CAMERA diff --git a/examples/publish_hue.py b/examples/publish_hue.py index 01d43565..f1b7d545 100644 --- a/examples/publish_hue.py +++ b/examples/publish_hue.py @@ -9,9 +9,10 @@ URL = "ws://localhost:7880" TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5MDY2MTMyODgsImlzcyI6IkFQSVRzRWZpZFpqclFvWSIsIm5hbWUiOiJuYXRpdmUiLCJuYmYiOjE2NzI2MTMyODgsInN1YiI6Im5hdGl2ZSIsInZpZGVvIjp7InJvb20iOiJ0ZXN0Iiwicm9vbUFkbWluIjp0cnVlLCJyb29tQ3JlYXRlIjp0cnVlLCJyb29tSm9pbiI6dHJ1ZSwicm9vbUxpc3QiOnRydWV9fQ.uSNIangMRu8jZD5mnRYoCHjcsQWCrJXgHCs0aNIgBFY" # noqa +WIDTH, HEIGHT = 1280, 720 async def draw_color_cycle(source: rtc.VideoSource): - argb_frame = rtc.ArgbFrame.create(rtc.VideoFormatType.FORMAT_ARGB, 1280, 720) + argb_frame = rtc.ArgbFrame.create(rtc.VideoFormatType.FORMAT_ARGB, WIDTH, HEIGHT) arr = np.frombuffer(argb_frame.data, dtype=np.uint8) framerate = 1 / 30 @@ -50,7 +51,7 @@ async def main(room: rtc.Room): return # publish a track - source = rtc.VideoSource() + source = rtc.VideoSource(WIDTH, HEIGHT) track = rtc.LocalVideoTrack.create_video_track("hue", source) options = rtc.TrackPublishOptions() options.source = rtc.TrackSource.SOURCE_CAMERA diff --git a/livekit-rtc/livekit/rtc/video_source.py b/livekit-rtc/livekit/rtc/video_source.py index 81d6b7fa..4ccb4b41 100644 --- a/livekit-rtc/livekit/rtc/video_source.py +++ b/livekit-rtc/livekit/rtc/video_source.py @@ -19,11 +19,13 @@ class VideoSource: - def __init__(self) -> None: + def __init__(self, width: int, height: int) -> None: req = proto_ffi.FfiRequest() req.new_video_source.type = ( proto_video_frame.VideoSourceType.VIDEO_SOURCE_NATIVE ) + req.new_video_source.resolution.width = width + req.new_video_source.resolution.height = height resp = ffi_client.request(req) self._info = resp.new_video_source.source