diff --git a/examples/publish_hue.py b/examples/publish_hue.py index 8c26d083..e2af0c3b 100644 --- a/examples/publish_hue.py +++ b/examples/publish_hue.py @@ -3,12 +3,13 @@ import logging import os from signal import SIGINT, SIGTERM +from time import perf_counter import numpy as np from livekit import api, rtc WIDTH, HEIGHT = 1280, 720 - +FPS = 30 # ensure LIVEKIT_URL, LIVEKIT_API_KEY, and LIVEKIT_API_SECRET are set @@ -38,8 +39,14 @@ async def main(room: rtc.Room): # publish a track source = rtc.VideoSource(WIDTH, HEIGHT) track = rtc.LocalVideoTrack.create_video_track("hue", source) - options = rtc.TrackPublishOptions() - options.source = rtc.TrackSource.SOURCE_CAMERA + options = rtc.TrackPublishOptions( + source=rtc.TrackSource.SOURCE_CAMERA, + simulcast=True, + video_encoding=rtc.VideoEncoding( + max_framerate=FPS, + max_bitrate=3_000_000, + ), + ) publication = await room.local_participant.publish_track(track, options) logging.info("published track %s", publication.sid) @@ -50,12 +57,11 @@ async def draw_color_cycle(source: rtc.VideoSource): argb_frame = bytearray(WIDTH * HEIGHT * 4) arr = np.frombuffer(argb_frame, dtype=np.uint8) - framerate = 1 / 30 + framerate = 1 / FPS hue = 0.0 + next_frame_time = perf_counter() while True: - start_time = asyncio.get_event_loop().time() - rgb = colorsys.hsv_to_rgb(hue, 1.0, 1.0) rgb = [(x * 255) for x in rgb] # type: ignore @@ -69,8 +75,10 @@ async def draw_color_cycle(source: rtc.VideoSource): source.capture_frame(frame) hue = (hue + framerate / 3) % 1.0 - code_duration = asyncio.get_event_loop().time() - start_time - await asyncio.sleep(1 / 30 - code_duration) + # code_duration = perf_counter() - start_time + next_frame_time += 1 / FPS + await asyncio.sleep(next_frame_time - perf_counter()) + # await asyncio.sleep(1 / FPS - code_duration) if __name__ == "__main__":