Skip to content
Merged
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
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

max_framerate might need to be a float here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should convert automatically? because that's non-obvious to a user

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python should do it automatically?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should yeah.. this is working for me

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
Loading