Repository navigation
fix(rtc): allocate total frame bytes in combine_audio_frames #904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NabiBukhsh-AI
wants to merge
1
commit into
livekit:main
Choose a base branch
from
NabiBukhsh-AI:fix/rtc-combine-audio-frames-prealloc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import array | ||
| import pytest | ||
|
|
||
| from livekit import rtc | ||
| import livekit.rtc.utils as utils | ||
|
|
||
|
|
||
| def test_combine_audio_frames_allocates_exact_byte_size(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| allocated: list[int] = [] | ||
| orig_bytearray = bytearray | ||
|
|
||
| def spy_bytearray(size: int) -> bytearray: | ||
| allocated.append(size) | ||
| return orig_bytearray(size) | ||
|
|
||
| monkeypatch.setattr(utils, "bytearray", spy_bytearray, raising=False) | ||
|
|
||
| f1 = rtc.AudioFrame(array.array("h", [1, 2, 3, 4]).tobytes(), 16000, 1, 4) | ||
| f2 = rtc.AudioFrame(array.array("h", [5, 6, 7, 8]).tobytes(), 16000, 1, 4) | ||
| combined = rtc.combine_audio_frames([f1, f2]) | ||
|
|
||
| assert len(allocated) == 1 | ||
| assert allocated[0] == combined.data.nbytes | ||
|
|
||
|
|
||
| def test_combine_audio_frames_concatenates_samples_correctly() -> None: | ||
| f1 = rtc.AudioFrame(array.array("h", [10, 20, 30]).tobytes(), 16000, 1, 3) | ||
| f2 = rtc.AudioFrame(array.array("h", [40, 50]).tobytes(), 16000, 1, 2) | ||
| combined = rtc.combine_audio_frames([f1, f2]) | ||
|
|
||
| assert combined.sample_rate == 16000 | ||
| assert combined.num_channels == 1 | ||
| assert combined.samples_per_channel == 5 | ||
| assert list(combined.data) == [10, 20, 30, 40, 50] | ||
|
|
||
|
|
||
| def test_combine_audio_frames_stereo() -> None: | ||
| # 2 channels, 2 samples per channel = 4 samples = 8 bytes | ||
| f1 = rtc.AudioFrame(array.array("h", [1, 2, 3, 4]).tobytes(), 48000, 2, 2) | ||
| f2 = rtc.AudioFrame(array.array("h", [5, 6, 7, 8]).tobytes(), 48000, 2, 2) | ||
| combined = rtc.combine_audio_frames([f1, f2]) | ||
|
|
||
| assert combined.sample_rate == 48000 | ||
| assert combined.num_channels == 2 | ||
| assert combined.samples_per_channel == 4 | ||
| assert list(combined.data) == [1, 2, 3, 4, 5, 6, 7, 8] | ||
|
|
||
|
|
||
| def test_combine_audio_frames_single_frame() -> None: | ||
| frame = rtc.AudioFrame(array.array("h", [1, 2, 3]).tobytes(), 16000, 1, 3) | ||
| assert rtc.combine_audio_frames(frame) is frame | ||
|
|
||
|
|
||
| def test_combine_audio_frames_empty_raises() -> None: | ||
| with pytest.raises(ValueError, match="buffer is empty"): | ||
| rtc.combine_audio_frames([]) | ||
|
|
||
|
|
||
| def test_combine_audio_frames_mismatched_sample_rate() -> None: | ||
| f1 = rtc.AudioFrame(array.array("h", [1, 2]).tobytes(), 16000, 1, 2) | ||
| f2 = rtc.AudioFrame(array.array("h", [3, 4]).tobytes(), 48000, 1, 2) | ||
| with pytest.raises(ValueError, match="Sample rate mismatch"): | ||
| rtc.combine_audio_frames([f1, f2]) | ||
|
|
||
|
|
||
| def test_combine_audio_frames_mismatched_channels() -> None: | ||
| f1 = rtc.AudioFrame(array.array("h", [1, 2]).tobytes(), 16000, 1, 2) | ||
| f2 = rtc.AudioFrame(array.array("h", [3, 4, 5, 6]).tobytes(), 16000, 2, 2) | ||
| with pytest.raises(ValueError, match="Channel count mismatch"): | ||
| rtc.combine_audio_frames([f1, f2]) | ||
|
|
||
|
|
||
| def test_combine_audio_frames_docstring_example() -> None: | ||
| frame1 = rtc.AudioFrame( | ||
| data=b"\x01\x00", sample_rate=48000, num_channels=1, samples_per_channel=1 | ||
| ) | ||
| frame2 = rtc.AudioFrame( | ||
| data=b"\x02\x00", sample_rate=48000, num_channels=1, samples_per_channel=1 | ||
| ) | ||
| combined_frame = rtc.combine_audio_frames([frame1, frame2]) | ||
| assert combined_frame.data.tobytes() == b"\x01\x00\x02\x00" | ||
| assert combined_frame.sample_rate == 48000 | ||
| assert combined_frame.num_channels == 1 | ||
| assert combined_frame.samples_per_channel == 2 |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, no change needed here:
AudioFrameaccepts a buffer larger thannum_channels * samples_per_channel * 2bytes, and onmainframe.datareturns the whole buffer. So for such a framenbytes(and the copy below) includes the trailing bytes, whiletotal_samples_per_channeldoesn't, which shifts later frames in the combined output. That's being fixed separately in #868, which makesdatareturn only the frame's samples. Once it lands,frame.data.nbyteshere is exact, so the two PRs compose fine in either order.