Repository navigation
fix(rtc): leave the bytes past an AudioFrame's samples out of its data - #868
ANDROIDHASSAN wants to merge 4 commits into
Conversation
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.
There was a problem hiding this comment.
🔍 Devin Review: 1 flag
Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
cloudwebrtc
left a comment
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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 ValueErrorSince 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.
There was a problem hiding this comment.
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.
AudioFrameaccepts a buffer that is larger thannum_channels * samples_per_channelsamples (only a shorter one raises), but three places used the whole buffer instead of the frame:datareturned every sample in the buffer, socombine_audio_framescopied the extra samples in and the combined frame, sized bysamples_per_channel, lost the end of the later frames:to_wav_byteswrote the whole buffer: 4 frames in the WAV for that 2-sample frame.AudioProcessingModule.process_stream/process_reverse_streamsentlen(data._data)as the size, 400 bytes for a 10 ms 16 kHz mono frame over a 200-sample buffer instead of 320.datanow returns only the frame's samples, andto_wav_bytesand 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, raisesValueErrorwithout this).Tests in
tests/rtc/test_audio_frame.py, each with a frame whose buffer has extra samples:data,combine_audio_frames,to_wav_bytesand 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 throughframe.datastill changes the caller's bytearray.6 of the 7 fail on
main(the memory-sharing one guards the current behaviour).tests/rtcandlivekit-rtc/testshere, 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) onlivekit-rtcreports the same 4 errors injupyter.pybefore and after.Not changed: the example in
combine_audio_frames' docstring buildsAudioFrame(b"\x01\x02", 48000, 2, 1), which raisesValueErrorbecause 2 channels need 4 bytes.