Skip to content

Commit f168a28

Browse files
fix(rtc): copy read-only frame buffers that get_address cannot take (#885)
Copy read-only memoryviews whose backing object cannot be addressed by get_address. Add AudioFrame and VideoFrame regression tests for read-only NumPy buffers. Fixes #884.
1 parent 407502d commit f168a28

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎livekit-rtc/livekit/rtc/_utils.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,17 @@ def _ensure_compatible_buffer(
4646
"""Validate and normalize a buffer for FFI use.
4747
4848
Sliced memoryviews are materialized because get_address cannot
49-
reliably resolve their offset for all buffer types.
49+
reliably resolve their offset for all buffer types. So are read-only
50+
memoryviews of anything but bytes or bytearray (e.g. a read-only numpy
51+
array), whose address get_address cannot take.
5052
"""
5153
if isinstance(data, memoryview):
5254
if not data.contiguous:
5355
raise ValueError("memoryview must be contiguous")
5456
if data.nbytes != len(data.obj): # type: ignore[arg-type]
5557
return bytearray(data) if not data.readonly else bytes(data)
58+
if data.readonly and not isinstance(data.obj, (bytes, bytearray)):
59+
return bytes(data)
5660
elif not isinstance(data, (bytes, bytearray)):
5761
raise TypeError(f"expected bytes, bytearray, or memoryview, got {type(data)}")
5862
return data

‎tests/rtc/test_audio_frame.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from __future__ import annotations
2+
3+
import ctypes
4+
5+
import pytest
6+
7+
from livekit import rtc
8+
9+
10+
def test_frame_from_a_read_only_numpy_buffer_can_be_captured() -> None:
11+
# np.frombuffer returns a read-only array, so its memoryview is read-only too.
12+
np = pytest.importorskip("numpy")
13+
samples = bytes(range(8))
14+
frame = rtc.AudioFrame(memoryview(np.frombuffer(samples, dtype=np.uint8)), 48000, 1, 4)
15+
16+
info = frame._proto_info()
17+
18+
assert ctypes.string_at(info.data_ptr, len(samples)) == samples

‎tests/rtc/test_video_frame.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ def test_get_plane_returns_each_i422_plane() -> None:
6565
assert [bytes(frame.get_plane(i)) for i in range(3)] == [y, u, v]
6666

6767

68+
def test_frame_from_a_read_only_numpy_buffer_can_be_captured() -> None:
69+
# np.frombuffer returns a read-only array, so its memoryview is read-only too.
70+
np = pytest.importorskip("numpy")
71+
w, h = 4, 2
72+
pixels = bytes(range(w * h * 4))
73+
frame = rtc.VideoFrame(w, h, T.RGBA, memoryview(np.frombuffer(pixels, dtype=np.uint8)))
74+
75+
info = frame._proto_info()
76+
77+
assert ctypes.string_at(info.data_ptr, len(pixels)) == pixels
78+
79+
6880
@pytest.mark.parametrize(
6981
"name",
7082
["RGBA", "ABGR", "ARGB", "BGRA", "RGB24", "I420", "I420A", "I422", "I444", "I010", "NV12"],

0 commit comments

Comments
 (0)