Skip to content

Commit d28f54c

Browse files
authored
ensure rtc.AudioFrame mem align (#370)
1 parent 361f1ea commit d28f54c

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

livekit-rtc/livekit/rtc/_utils.py

+2-5
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

+6-2
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,7 +61,9 @@ 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-
self._data = bytearray(data)
64+
n = len(data) // ctypes.sizeof(ctypes.c_int16)
65+
self._data = (ctypes.c_int16 * n).from_buffer_copy(data)
66+
6367
self._sample_rate = sample_rate
6468
self._num_channels = num_channels
6569
self._samples_per_channel = samples_per_channel
@@ -133,7 +137,7 @@ def data(self) -> memoryview:
133137
Returns:
134138
memoryview: A memory view of the audio data.
135139
"""
136-
return memoryview(self._data).cast("h")
140+
return memoryview(self._data).cast("B").cast("h")
137141

138142
@property
139143
def sample_rate(self) -> int:

0 commit comments

Comments
 (0)