Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 12 additions & 2 deletions livekit-rtc/livekit/rtc/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,18 @@ def task_done_logger(task: asyncio.Task) -> None:
return


def get_address(mv: memoryview) -> int:
return ctypes.addressof(ctypes.c_char.from_buffer(mv))
def get_address(data) -> int:
if isinstance(data, memoryview):
if not data.readonly:
return ctypes.addressof(ctypes.c_char.from_buffer(data))
data = data.obj
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
if isinstance(data, bytearray):
return ctypes.addressof(ctypes.c_char.from_buffer(data))
if isinstance(data, bytes):
addr = ctypes.cast(ctypes.c_char_p(data), ctypes.c_void_p).value
assert addr is not None
return addr
raise TypeError(f"expected bytes, bytearray, or memoryview, got {type(data)}")
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.


T = TypeVar("T")
Expand Down
15 changes: 9 additions & 6 deletions livekit-rtc/livekit/rtc/audio_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,22 @@ def __init__(
Raises:
ValueError: If the length of `data` is smaller than the required size.
"""
data = memoryview(data).cast("B")
if isinstance(data, memoryview):
data = data.obj # type: ignore[assignment]
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
Outdated

if len(data) < num_channels * samples_per_channel * ctypes.sizeof(ctypes.c_int16):
min_size = num_channels * samples_per_channel * ctypes.sizeof(ctypes.c_int16)
data_len = len(data)

if data_len < min_size:
raise ValueError(
"data length must be >= num_channels * samples_per_channel * sizeof(int16)"
)

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

n = len(data) // ctypes.sizeof(ctypes.c_int16)
self._data = (ctypes.c_int16 * n).from_buffer_copy(data)
self._data = data
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.

self._sample_rate = sample_rate
self._num_channels = num_channels
Expand Down Expand Up @@ -97,7 +100,7 @@ def _from_owned_info(owned_info: proto_audio.OwnedAudioFrameBuffer) -> "AudioFra

def _proto_info(self) -> proto_audio.AudioFrameBufferInfo:
audio_info = proto_audio.AudioFrameBufferInfo()
audio_info.data_ptr = get_address(memoryview(self._data))
audio_info.data_ptr = get_address(self._data)
audio_info.sample_rate = self.sample_rate
audio_info.num_channels = self.num_channels
audio_info.samples_per_channel = self.samples_per_channel
Expand Down
2 changes: 1 addition & 1 deletion livekit-rtc/livekit/rtc/video_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(
self._width = width
self._height = height
self._type = type
self._data = bytearray(data)
self._data = data

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