-
Notifications
You must be signed in to change notification settings - Fork 114
add preconnect audio buffer API to LocalAudioTrack #648
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
theomonnom
wants to merge
1
commit into
main
Choose a base branch
from
theo/preconnect-buffer-api
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,81 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import threading | ||
|
|
||
| from .audio_frame import AudioFrame | ||
|
|
||
|
|
||
| class AudioRingBuffer: | ||
| """Pre-allocated circular buffer for raw PCM audio data. | ||
|
|
||
| Stores int16 PCM samples in a fixed-size bytearray. Push is zero-allocation. | ||
| """ | ||
|
|
||
| def __init__(self, max_duration: float, sample_rate: int, num_channels: int) -> None: | ||
| self._sample_rate = sample_rate | ||
| self._num_channels = num_channels | ||
| self._bytes_per_second = sample_rate * num_channels * 2 # int16 | ||
| self._max_bytes = int(max_duration * self._bytes_per_second) | ||
| if self._max_bytes <= 0: | ||
| raise ValueError("max_duration must be positive") | ||
|
|
||
| self._buf = bytearray(self._max_bytes) | ||
| self._write_pos = 0 | ||
| self._size = 0 | ||
| self._lock = threading.Lock() | ||
|
|
||
| @property | ||
| def duration(self) -> float: | ||
| with self._lock: | ||
| return self._size / self._bytes_per_second | ||
|
|
||
| @property | ||
| def max_duration(self) -> float: | ||
| return self._max_bytes / self._bytes_per_second | ||
|
|
||
| def push(self, frame: AudioFrame) -> None: | ||
| data = frame.data.cast("b") | ||
| n = len(data) | ||
| if n == 0: | ||
| return | ||
|
|
||
| with self._lock: | ||
| if n >= self._max_bytes: | ||
| # frame larger than buffer — keep only the tail | ||
| self._buf[:] = data[n - self._max_bytes :] | ||
| self._write_pos = 0 | ||
| self._size = self._max_bytes | ||
| return | ||
|
|
||
| end = self._write_pos + n | ||
| if end <= self._max_bytes: | ||
| self._buf[self._write_pos : end] = data | ||
| else: | ||
| first = self._max_bytes - self._write_pos | ||
| self._buf[self._write_pos : self._max_bytes] = data[:first] | ||
| self._buf[: n - first] = data[first:] | ||
|
|
||
| self._write_pos = end % self._max_bytes | ||
| self._size = min(self._size + n, self._max_bytes) | ||
|
|
||
| def capture(self) -> bytes: | ||
| """Snapshot the buffer contents and reset. Returns raw PCM bytes.""" | ||
| with self._lock: | ||
| if self._size == 0: | ||
| return b"" | ||
|
|
||
| read_pos = (self._write_pos - self._size) % self._max_bytes | ||
| if read_pos + self._size <= self._max_bytes: | ||
| data = bytes(self._buf[read_pos : read_pos + self._size]) | ||
| else: | ||
| first = self._max_bytes - read_pos | ||
| data = bytes(self._buf[read_pos:]) + bytes(self._buf[: self._size - first]) | ||
|
|
||
| self._write_pos = 0 | ||
| self._size = 0 | ||
| return data | ||
|
|
||
| def clear(self) -> None: | ||
| with self._lock: | ||
| self._write_pos = 0 | ||
| self._size = 0 |
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
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
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.
🔴 Registering async callback with EventEmitter.on() always raises ValueError
The
_setup_preconnect_auto_sendmethod defines_on_participant_activeas anasync defand registers it withroom.on("participant_active", _on_participant_active)at line 816. However,EventEmitter.on()(event_emitter.py:160-163) explicitly rejects async callbacks by raisingValueError("Cannot register an async callback with .on(). Use asyncio.create_task within your synchronous callback instead."). This means the auto-send feature will always crash with aValueErrorwhen_setup_preconnect_auto_sendis called, which propagates up throughpublish_track()whenpreconnect_buffer_auto_send_tois set and the track has a preconnect buffer.Prompt for agents
Was this helpful? React with 👍 or 👎 to provide feedback.