Skip to content

Commit d478901

Browse files
committed
from_buffer_copy
1 parent 38d08c9 commit d478901

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

livekit-rtc/livekit/rtc/_utils.py

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

4242

43-
def get_address(data: memoryview) -> int:
44-
"""Get the address of a buffer using ctypes"""
45-
nbytes = data.nbytes
46-
buffer = (ctypes.c_int8 * nbytes).from_buffer(data)
47-
return ctypes.addressof(buffer)
43+
def get_address(mv: memoryview) -> int:
44+
return ctypes.addressof(ctypes.c_char.from_buffer(mv))
4845

4946

5047
T = TypeVar("T")

livekit-rtc/livekit/rtc/audio_frame.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def __init__(
4848
Raises:
4949
ValueError: If the length of `data` is smaller than the required size.
5050
"""
51+
data = memoryview(data).cast("B")
52+
5153
if len(data) < num_channels * samples_per_channel * ctypes.sizeof(
5254
ctypes.c_int16
5355
):
@@ -59,9 +61,8 @@ def __init__(
5961
# can happen if data is bigger than needed
6062
raise ValueError("data length must be a multiple of sizeof(int16)")
6163

62-
data = memoryview(data).cast("B")
63-
self._data = (ctypes.c_int16 * (len(data) // ctypes.sizeof(ctypes.c_int16)))()
64-
ctypes.memmove(self._data, data, len(data))
64+
n = len(data) // ctypes.sizeof(ctypes.c_int16)
65+
self._data = (ctypes.c_int16 * n).from_buffer_copy(data)
6566

6667
self._sample_rate = sample_rate
6768
self._num_channels = num_channels

0 commit comments

Comments
 (0)