Skip to content

Commit

Permalink
Update publish_hue to publish at 30fps (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidzhao authored Dec 11, 2024
1 parent 522b34f commit a07e5eb
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions examples/publish_hue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand All @@ -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

Expand All @@ -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__":
Expand Down

0 comments on commit a07e5eb

Please sign in to comment.