Skip to content

Commit a4b89ac

Browse files
committed
wip
1 parent 57ae0ec commit a4b89ac

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

livekit-rtc/livekit/rtc/_utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,16 @@ def task_done_logger(task: asyncio.Task) -> None:
4040
return
4141

4242

43-
def get_address(mv: memoryview) -> int:
44-
return ctypes.addressof(ctypes.c_char.from_buffer(mv))
43+
def get_address(data) -> int:
44+
if isinstance(data, memoryview):
45+
if not data.readonly:
46+
return ctypes.addressof(ctypes.c_char.from_buffer(data))
47+
data = data.obj
48+
if isinstance(data, bytearray):
49+
return ctypes.addressof(ctypes.c_char.from_buffer(data))
50+
if isinstance(data, bytes):
51+
return ctypes.cast(ctypes.c_char_p(data), ctypes.c_void_p).value
52+
raise TypeError(f"expected bytes, bytearray, or memoryview, got {type(data)}")
4553

4654

4755
T = TypeVar("T")

livekit-rtc/livekit/rtc/audio_frame.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,22 @@ def __init__(
4949
Raises:
5050
ValueError: If the length of `data` is smaller than the required size.
5151
"""
52-
data = memoryview(data).cast("B")
52+
if isinstance(data, memoryview):
53+
data = data.obj
5354

54-
if len(data) < num_channels * samples_per_channel * ctypes.sizeof(ctypes.c_int16):
55+
min_size = num_channels * samples_per_channel * ctypes.sizeof(ctypes.c_int16)
56+
data_len = len(data)
57+
58+
if data_len < min_size:
5559
raise ValueError(
5660
"data length must be >= num_channels * samples_per_channel * sizeof(int16)"
5761
)
5862

59-
if len(data) % ctypes.sizeof(ctypes.c_int16) != 0:
63+
if data_len % ctypes.sizeof(ctypes.c_int16) != 0:
6064
# can happen if data is bigger than needed
6165
raise ValueError("data length must be a multiple of sizeof(int16)")
6266

63-
n = len(data) // ctypes.sizeof(ctypes.c_int16)
64-
self._data = (ctypes.c_int16 * n).from_buffer_copy(data)
67+
self._data = data
6568

6669
self._sample_rate = sample_rate
6770
self._num_channels = num_channels

livekit-rtc/livekit/rtc/video_frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __init__(
5151
self._width = width
5252
self._height = height
5353
self._type = type
54-
self._data = bytearray(data)
54+
self._data = data
5555

5656
@property
5757
def width(self) -> int:

0 commit comments

Comments
 (0)