Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure rtc.AudioFrame mem align #370

Merged
merged 8 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions livekit-rtc/livekit/rtc/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ def task_done_logger(task: asyncio.Task) -> None:
return


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


T = TypeVar("T")
Expand Down
8 changes: 6 additions & 2 deletions livekit-rtc/livekit/rtc/audio_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def __init__(
Raises:
ValueError: If the length of `data` is smaller than the required size.
"""
data = memoryview(data).cast("B")

if len(data) < num_channels * samples_per_channel * ctypes.sizeof(
ctypes.c_int16
):
Expand All @@ -59,7 +61,9 @@ def __init__(
# can happen if data is bigger than needed
raise ValueError("data length must be a multiple of sizeof(int16)")

self._data = bytearray(data)
n = len(data) // ctypes.sizeof(ctypes.c_int16)
self._data = (ctypes.c_int16 * n).from_buffer_copy(data)

self._sample_rate = sample_rate
self._num_channels = num_channels
self._samples_per_channel = samples_per_channel
Expand Down Expand Up @@ -133,7 +137,7 @@ def data(self) -> memoryview:
Returns:
memoryview: A memory view of the audio data.
"""
return memoryview(self._data).cast("h")
return memoryview(self._data).cast("B").cast("h")

@property
def sample_rate(self) -> int:
Expand Down
Loading