Skip to content

Fully configure frame processors when they are used directly on an audio stream#679

Open
1egoman wants to merge 16 commits into
mainfrom
frame-processor-on-audio-stream
Open

Fully configure frame processors when they are used directly on an audio stream#679
1egoman wants to merge 16 commits into
mainfrom
frame-processor-on-audio-stream

Conversation

@1egoman
Copy link
Copy Markdown
Contributor

@1egoman 1egoman commented May 20, 2026

Updates the python sdk so that FrameProcessor-based noise cancellation providers can be used directly on AudioStream, without having to go through the agent's RoomIO to be able to initialize itself with credentials.

For example, with this change, something like the below becomes possible:

stream = rtc.AudioStream.from_track(                                                                                                                   
    track=track,
    sample_rate=SAMPLE_RATE,                                             
    num_channels=CHANNELS,
    noise_cancellation=ai_coustics.audio_enhancement(model=ai_coustics.EnhancerModel.QUAIL_VF_L)  ,
) 

The way this works - Tracks now keep track of which room they are part of (holding a weakref value). When the room a track is in changes, it computes new frame processor options and sends these to any AudioStreams which are associated with the track.

The noise_cancellation_leave_open parameter allows the agents sdk to call this from_track method with a frame processor which remains open across the whole session, and won't be auto-closed when the track is closed.

This goes along with https://github.com/livekit/agents/pull/new/move-frame-processor-metadata-to-client-sdk, which removes the relevant event handling logic in the agents sdk. I will make this as a follow up once I can determine this pull request is generally moving in a good direction. Also I will follow up with a node version of this once the python one is in a good state.

Todo

  • Add some tests for this newly added behavior

@1egoman 1egoman force-pushed the frame-processor-on-audio-stream branch from 3e5a9ab to f62c247 Compare May 26, 2026 15:15
Comment on lines +63 to +72
# track left a room — clear processor's room context
# FIXME: This isn't really good, and I can't figure out what should happen here
# Closing the processor doesn't work (the track could get added to another room later)
# Empty values like this don't work, because it causes a drm::Error in the plugin
# Talk to lukas about this in a 1:1 and see if he can think of anything better
stream._on_processor_stream_info_updated(
room_name="", participant_identity="", publication_sid=""
)
# stream._on_processor_credentials_updated(token="", url="")
return
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.

This is the biggest thing still to be determined: I'm not exactly sure what to do when a track is removed from a room while an AudioStream that is within that track has a FrameProcessor registered.

  • Initial thought: set empty metadata with something like stream._on_processor_credentials_updated(token="", url=""). However, this causes the ai-coustics plugin to throw an error because the "" string cannot be parsed as a URL.
  • Next thought: disable the FrameProcessor by setting enabled to False in this situation (probably also log a warning too that this is being done?). The big problem with this is it would overzelously disable FrameProcessors which don't need credentials to work (like the already-existing Krisp VIVA FrameProcessor).
  • Other idea: Maybe modify the FrameProcessor interface to make token / url Optional[str]? But that would be a breaking api change, so that's probably out...

Curious what others think here and if there's an approach which I have missed.

@1egoman 1egoman marked this pull request as ready for review May 26, 2026 21:25
Copy link
Copy Markdown
Contributor

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

Choose a reason for hiding this comment

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

Devin Review found 1 potential issue.

View 5 additional findings in Devin Review.

Open in Devin Review

Comment on lines +342 to 347
if self._processor is not None and not self._processor_leave_open:
self._processor._close()
if self._track is not None:
self._track._unregister_audio_stream(self)
self._ffi_handle.dispose()
await self._task
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.

🔴 Processor closed before _run task finishes, allowing _process() calls on closed processor

In aclose(), self._processor._close() is called at line 343 before await self._task at line 347. Although these are synchronous calls that happen atomically before the event loop yields, once await self._task yields control, the _run loop (audio_stream.py:312-334) resumes and may process audio frames that were already buffered in _ffi_queue before the EOS event arrives. Those frames reach self._processor._process(frame) at line 322, which is now called on a closed processor. While the try/except at line 321-327 catches Python exceptions, if the processor's _close() releases native resources that _process() depends on, this could cause undefined behavior or a crash that isn't caught by the exception handler. The fix is to move the processor close to after await self._task so the _run loop has fully terminated before the processor is torn down.

Suggested change
if self._processor is not None and not self._processor_leave_open:
self._processor._close()
if self._track is not None:
self._track._unregister_audio_stream(self)
self._ffi_handle.dispose()
await self._task
if self._track is not None:
self._track._unregister_audio_stream(self)
self._ffi_handle.dispose()
await self._task
if self._processor is not None and not self._processor_leave_open:
self._processor._close()
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

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.

1 participant