Skip to content

Commit 50bdb28

Browse files
authored
feat: new video buffer api (#155)
1 parent b5ac94c commit 50bdb28

File tree

16 files changed

+441
-1370
lines changed

16 files changed

+441
-1370
lines changed

examples/face_landmark/face_landmark.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def on_track_subscribed(track: rtc.Track, *_):
4242
return
4343

4444
print("subscribed to track: " + track.name)
45-
video_stream = rtc.VideoStream(track)
45+
video_stream = rtc.VideoStream(track, format=rtc.VideoBufferType.RGB24)
4646
task = asyncio.create_task(frame_loop(video_stream))
4747
tasks.add(task)
4848
task.add_done_callback(tasks.remove)
@@ -104,35 +104,22 @@ def draw_landmarks_on_image(rgb_image, detection_result):
104104

105105
async def frame_loop(video_stream: rtc.VideoStream) -> None:
106106
landmarker = FaceLandmarker.create_from_options(options)
107-
argb_frame = None
108107
cv2.namedWindow("livekit_video", cv2.WINDOW_AUTOSIZE)
109108
cv2.startWindowThread()
110-
async for frame in video_stream:
111-
buffer = frame.buffer
112-
113-
if (
114-
argb_frame is None
115-
or argb_frame.width != buffer.width
116-
or argb_frame.height != buffer.height
117-
):
118-
argb_frame = rtc.ArgbFrame.create(
119-
rtc.VideoFormatType.FORMAT_ABGR, buffer.width, buffer.height
120-
)
121-
122-
buffer.to_argb(argb_frame)
109+
async for frame_event in video_stream:
110+
buffer = frame_event.frame
123111

124-
arr = np.frombuffer(argb_frame.data, dtype=np.uint8)
125-
arr = arr.reshape((argb_frame.height, argb_frame.width, 4))
126-
arr = cv2.cvtColor(arr, cv2.COLOR_RGBA2RGB)
112+
arr = np.frombuffer(buffer.data, dtype=np.uint8)
113+
arr = arr.reshape((buffer.height, buffer.width, 3))
127114

128115
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=arr)
129-
130-
detection_result = landmarker.detect_for_video(mp_image, frame.timestamp_us)
116+
detection_result = landmarker.detect_for_video(
117+
mp_image, frame_event.timestamp_us
118+
)
131119

132120
draw_landmarks_on_image(arr, detection_result)
133121

134122
arr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
135-
136123
cv2.imshow("livekit_video", arr)
137124
if cv2.waitKey(1) & 0xFF == ord("q"):
138125
break

examples/publish_hue.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ async def main(room: rtc.Room):
4747

4848

4949
async def draw_color_cycle(source: rtc.VideoSource):
50-
argb_frame = rtc.ArgbFrame.create(rtc.VideoFormatType.FORMAT_ARGB, WIDTH, HEIGHT)
51-
arr = np.frombuffer(argb_frame.data, dtype=np.uint8)
50+
argb_frame = bytearray(WIDTH * HEIGHT * 4)
51+
arr = np.frombuffer(argb_frame, dtype=np.uint8)
5252

5353
framerate = 1 / 30
5454
hue = 0.0
@@ -65,8 +65,7 @@ async def draw_color_cycle(source: rtc.VideoSource):
6565
arr.flat[2::4] = argb_color[2]
6666
arr.flat[3::4] = argb_color[3]
6767

68-
frame = rtc.VideoFrame(argb_frame.to_i420())
69-
68+
frame = rtc.VideoFrame(WIDTH, HEIGHT, rtc.VideoBufferType.RGBA, argb_frame)
7069
source.capture_frame(frame)
7170
hue = (hue + framerate / 3) % 1.0
7271

livekit-rtc/livekit/rtc/__init__.py

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
)
2727
from ._proto.e2ee_pb2 import EncryptionType, EncryptionState
2828
from ._proto.track_pb2 import StreamState, TrackKind, TrackSource
29-
from ._proto.video_frame_pb2 import VideoFormatType, VideoFrameBufferType, VideoRotation
29+
from ._proto.video_frame_pb2 import VideoBufferType, VideoRotation
3030
from ._proto import stats_pb2 as stats
3131
from .audio_frame import AudioFrame
3232
from .audio_source import AudioSource
33-
from .audio_stream import AudioStream
33+
from .audio_stream import AudioStream, AudioFrameEvent
3434
from .participant import LocalParticipant, Participant, RemoteParticipant
3535
from .room import ConnectError, Room, RoomOptions, RtcConfiguration, DataPacket
3636
from .track import (
@@ -57,21 +57,10 @@
5757
TrackPublication,
5858
)
5959
from .video_frame import (
60-
ArgbFrame,
61-
I010Buffer,
62-
I420ABuffer,
63-
I420Buffer,
64-
I422Buffer,
65-
NativeVideoBuffer,
66-
NV12Buffer,
67-
PlanarYuv8Buffer,
68-
PlanarYuv16Buffer,
69-
PlanarYuvBuffer,
7060
VideoFrame,
71-
VideoFrameBuffer,
7261
)
7362
from .video_source import VideoSource
74-
from .video_stream import VideoStream
63+
from .video_stream import VideoStream, VideoFrameEvent
7564
from .chat import ChatManager, ChatMessage
7665

7766
from .version import __version__
@@ -89,13 +78,13 @@
8978
"StreamState",
9079
"TrackKind",
9180
"TrackSource",
92-
"VideoFormatType",
93-
"VideoFrameBufferType",
81+
"VideoBufferType",
9482
"VideoRotation",
9583
"stats",
9684
"AudioFrame",
9785
"AudioSource",
9886
"AudioStream",
87+
"AudioFrameEvent",
9988
"LocalParticipant",
10089
"Participant",
10190
"RemoteParticipant",
@@ -121,20 +110,10 @@
121110
"LocalTrackPublication",
122111
"RemoteTrackPublication",
123112
"TrackPublication",
124-
"ArgbFrame",
125-
"I010Buffer",
126-
"I420ABuffer",
127-
"I420Buffer",
128-
"I422Buffer",
129-
"NativeVideoBuffer",
130-
"NV12Buffer",
131-
"PlanarYuv8Buffer",
132-
"PlanarYuv16Buffer",
133-
"PlanarYuvBuffer",
134113
"VideoFrame",
135-
"VideoFrameBuffer",
136114
"VideoSource",
137115
"VideoStream",
116+
"VideoFrameEvent",
138117
"ChatManager",
139118
"ChatMessage",
140119
"__version__",

livekit-rtc/livekit/rtc/_proto/audio_frame_pb2.py

Lines changed: 51 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)