Repository navigation
fix(rtc): allocate total frame bytes in combine_audio_frames - #904
Open
NabiBukhsh-AI wants to merge 1 commit into
Open
NabiBukhsh-AI wants to merge 1 commit into
NabiBukhsh-AI wants to merge 1 commit into
Conversation
NabiBukhsh-AI
requested review from
changt,
cloudwebrtc,
lukasIO and
xianshijing-lk
as code owners
October 26, 2026 08:21
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)