Skip to content

fix(rtc): leave the bytes past an AudioFrame's samples out of its data - #868

Open
ANDROIDHASSAN wants to merge 4 commits into
livekit:mainfrom
ANDROIDHASSAN:fix/audio-frame-data-length
Open

ANDROIDHASSAN wants to merge 4 commits into
livekit:mainfrom
ANDROIDHASSAN:fix/audio-frame-data-length

Conversation

@ANDROIDHASSAN

@ANDROIDHASSAN ANDROIDHASSAN commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

AudioFrame accepts a buffer that is larger than num_channels * samples_per_channel samples (only a shorter one raises), but three places used the whole buffer instead of the frame:

  • data returned every sample in the buffer, so combine_audio_frames copied the extra samples in and the combined frame, sized by samples_per_channel, lost the end of the later frames:
    f1 = rtc.AudioFrame(array.array("h", [1, 2, 99, 99]).tobytes(), 16000, 1, 2)
    f2 = rtc.AudioFrame(array.array("h", [3, 4]).tobytes(), 16000, 1, 2)
    list(rtc.combine_audio_frames([f1, f2]).data)
    # main: [1, 2, 99, 99]
  • to_wav_bytes wrote the whole buffer: 4 frames in the WAV for that 2-sample frame.
  • AudioProcessingModule.process_stream / process_reverse_stream sent len(data._data) as the size, 400 bytes for a 10 ms 16 kHz mono frame over a 200-sample buffer instead of 320.

data now returns only the frame's samples, and to_wav_bytes and the APM requests use it. The buffer itself is kept as passed, so the frame still shares the caller's memory (the APM processes in place) and stays picklable.

The parity check on the whole buffer is gone: an odd byte past the frame is not part of it (test_a_trailing_odd_byte_is_not_part_of_the_frame, raises ValueError without this).

Tests in tests/rtc/test_audio_frame.py, each with a frame whose buffer has extra samples:

  • data, combine_audio_frames, to_wav_bytes and the pydantic serializer return or write only the frame's samples.
  • test_apm_sends_only_the_frame_bytes[process_stream|process_reverse_stream]: the request size is 320 (FFI client stubbed).
  • test_frame_over_a_larger_bytearray_shares_its_memory: writing through frame.data still changes the caller's bytearray.

6 of the 7 fail on main (the memory-sharing one guards the current behaviour). tests/rtc and livekit-rtc/tests here, without the FFI library: the same 17 failures and 17 errors before and after, from the missing native library; 131 passed before, 138 now. ruff check, ruff format --check (0.15.4) pass; mypy (1.19.1) on livekit-rtc reports the same 4 errors in jupyter.py before and after.

Not changed: the example in combine_audio_frames' docstring builds AudioFrame(b"\x01\x02", 48000, 2, 1), which raises ValueError because 2 channels need 4 bytes.

AudioFrame accepts a buffer larger than num_channels * samples_per_channel
samples, but data, to_wav_bytes and AudioProcessingModule used the whole
buffer. combine_audio_frames then shifted every later frame by the extra
bytes and dropped its tail, and the APM was told the frame was longer than
10 ms.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Devin Review: 1 flag

Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

@cloudwebrtc cloudwebrtc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Slicing data to the frame's samples fixes combine_audio_frames, to_wav_bytes and the APM request sizes at the source, and the other frame.data readers (AudioResampler.push, AudioMixer, the pydantic serializer) pick it up for free. The buffer is still shared with the caller, so in-place APM processing keeps working. This composes fine with #904 in either order: once both land, frame.data.nbytes in combine_audio_frames is exact.

data (Union[bytes, bytearray, memoryview]): The raw audio data, which must be at least
`num_channels * samples_per_channel * sizeof(int16)` bytes long.
`num_channels * samples_per_channel * sizeof(int16)` bytes long. Bytes past that
size are not part of the frame.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, optional: with this contract, __init__ still checks the parity of the whole buffer (data_len % sizeof(int16) != 0), so a single trailing byte past the frame is rejected even though it's not part of the frame:

rtc.AudioFrame(bytearray(321), 16000, 1, 160)  # needs 320 bytes, raises ValueError

Since min_size is always even, the parity check could be dropped, or the docstring could say the buffer length must still be a multiple of 2 bytes. Either is fine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped the parity check in 4cf7dcd, since min_size already guarantees enough whole samples. test_a_trailing_odd_byte_is_not_part_of_the_frame builds a frame over a buffer with one extra byte; it raised ValueError before.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants