Repository navigation
fix(rtc): allocate total frame bytes in combine_audio_frames - #904
NabiBukhsh-AI wants to merge 2 commits into
Conversation
|
Hi @cloudwebrtc @lukasIO, Gentle ping on this fix. It allocates the correct total buffer size ( All CI tests and Devin review are passing green. Looking forward to your review! |
cloudwebrtc
left a comment
There was a problem hiding this comment.
LGTM. frame.data is an int16 view, so len() counted samples and the buffer was allocated at half size. The output was still correct because slice assignment grows the bytearray, so this mainly avoids the extra reallocations, but nbytes is clearly the right measure. The docstring and tests look good.
One optional, pre-existing nit outside the diff: the early return if not isinstance(buffer, list): return buffer treats any non-list sequence as a single frame, so combine_audio_frames((f1, f2)) returns the tuple unchanged and the caller then fails on .data. The annotation is AudioFrame | list[AudioFrame], so this is only robustness, but returning early only when isinstance(buffer, AudioFrame) and converting everything else with list(...) would cover it. Fine to leave for a follow-up.
|
Thanks for the review, @cloudwebrtc! I've added a changeset ( Thanks also for the pointer to #868, and agreed that the two compose fine in either order. I've left the non-list early return as is to keep this PR focused; it can go in a follow-up. |
Summary
combine_audio_framesaccumulatestotal_data_lengthto preallocate the destinationbytearraybefore copying frame chunks in place:However,
frame.datais anint16memoryview (format='h').len(frame.data)returns the number of samples, not bytes. Because each 16-bit sample is 2 bytes,data = bytearray(total_data_length)preallocated exactly half the memory required for the combined frame.During the copy loop:
Python's
bytearrayslice assignment was forced to repeatedly grow and reallocate the underlying memory buffer on every frame, defeating the function's documented design:Additionally, the docstring's example constructed
AudioFrame(b"\x01\x02", 48000, 2, 1), which raises aValueErrorbecause 2 channels with 1 sample per channel requires 4 bytes.Changes
total_data_length += frame.data.nbytesso the preallocatedbytearrayaccurately matches the total byte size of all frame buffers upfront.combined_frame.data.tobytes().Testing
Added unit test suite in
tests/rtc/test_combine_audio_frames.py:test_combine_audio_frames_allocates_exact_byte_size: verifies that the allocatedbytearraysize equalscombined.data.nbytes(fails onmainwhere it allocated half the required size).test_combine_audio_frames_concatenates_samples_correctly: verifies sample concatenation.test_combine_audio_frames_stereo: verifies multi-channel frames.test_combine_audio_frames_single_frame: returns single frame as-is.test_combine_audio_frames_empty_raises: raisesValueErroron empty list.test_combine_audio_frames_mismatched_sample_rate: raisesValueErroron sample rate mismatch.test_combine_audio_frames_mismatched_channels: raisesValueErroron channel count mismatch.test_combine_audio_frames_docstring_example: verifies the doctest example runs cleanly.uv run pytest tests/rtc/test_combine_audio_frames.py -v -W error(8/8 passed)uv run ruff check livekit-rtc/livekit/rtc/utils.py tests/rtc/test_combine_audio_frames.py(passed)uv run ruff format --check livekit-rtc/livekit/rtc/utils.py tests/rtc/test_combine_audio_frames.py(passed)uv run mypy livekit-rtc/livekit/rtc/utils.py tests/rtc/test_combine_audio_frames.py(0 issues)