33import logging
44import os
55from signal import SIGINT , SIGTERM
6+ from time import perf_counter
67
78import numpy as np
89from livekit import api , rtc
910
1011WIDTH , HEIGHT = 1280 , 720
11-
12+ FPS = 30
1213
1314# ensure LIVEKIT_URL, LIVEKIT_API_KEY, and LIVEKIT_API_SECRET are set
1415
@@ -38,8 +39,14 @@ async def main(room: rtc.Room):
3839 # publish a track
3940 source = rtc .VideoSource (WIDTH , HEIGHT )
4041 track = rtc .LocalVideoTrack .create_video_track ("hue" , source )
41- options = rtc .TrackPublishOptions ()
42- options .source = rtc .TrackSource .SOURCE_CAMERA
42+ options = rtc .TrackPublishOptions (
43+ source = rtc .TrackSource .SOURCE_CAMERA ,
44+ simulcast = True ,
45+ video_encoding = rtc .VideoEncoding (
46+ max_framerate = FPS ,
47+ max_bitrate = 3_000_000 ,
48+ ),
49+ )
4350 publication = await room .local_participant .publish_track (track , options )
4451 logging .info ("published track %s" , publication .sid )
4552
@@ -50,12 +57,11 @@ async def draw_color_cycle(source: rtc.VideoSource):
5057 argb_frame = bytearray (WIDTH * HEIGHT * 4 )
5158 arr = np .frombuffer (argb_frame , dtype = np .uint8 )
5259
53- framerate = 1 / 30
60+ framerate = 1 / FPS
5461 hue = 0.0
62+ next_frame_time = perf_counter ()
5563
5664 while True :
57- start_time = asyncio .get_event_loop ().time ()
58-
5965 rgb = colorsys .hsv_to_rgb (hue , 1.0 , 1.0 )
6066 rgb = [(x * 255 ) for x in rgb ] # type: ignore
6167
@@ -69,8 +75,10 @@ async def draw_color_cycle(source: rtc.VideoSource):
6975 source .capture_frame (frame )
7076 hue = (hue + framerate / 3 ) % 1.0
7177
72- code_duration = asyncio .get_event_loop ().time () - start_time
73- await asyncio .sleep (1 / 30 - code_duration )
78+ # code_duration = perf_counter() - start_time
79+ next_frame_time += 1 / FPS
80+ await asyncio .sleep (next_frame_time - perf_counter ())
81+ # await asyncio.sleep(1 / FPS - code_duration)
7482
7583
7684if __name__ == "__main__" :
0 commit comments