Skip to content

webrtc-sys: keep NativeAudioSource send streams out of AudioState - #1491

Merged
hiroshihorie merged 2 commits into
livekit:mainfrom
richard134:fix-external-audio-source-sending
Oct 17, 2026
Merged

hiroshihorie merged 2 commits into
livekit:mainfrom
richard134:fix-external-audio-source-sending

Conversation

@richard134

Copy link
Copy Markdown
Contributor

Fixes #1490. The m150 port of external_audio_source.patch (#1284) dropped sending_ = false; from AudioSendStream::Stop(), and StoreEncoderProperties() adds streams to AudioState without checking config_.external_source. A NativeAudioSource stream added there is never removed, and the ADM capture thread later calls into it from AudioTransportImpl::SendProcessedData after it is destroyed.

This restores sending_ = false; in Stop() and skips AddSendingStream in StoreEncoderProperties() for external sources. The updated patch applies cleanly with git apply to audio/audio_send_stream.cc at blob e9e72b48e7, the file the current patch targets. Tested by recompiling that file into the webrtc-89d790b libwebrtc.a: a program that records with PlatformAudio and repeatedly publishes and removes a stereo NativeAudioSource track crashed 4 of 4 runs before and 0 of 4 after. Shipping it needs a new WebRTC build.

@richard134
richard134 requested a review from ladvoc as a code owner October 3, 2026 07:30
@CLAassistant

CLAassistant commented Oct 3, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@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.

Note

Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.

Devin Review found 1 potential issue.

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

Devin Review

- if (sending_) {
+ // External sources never registered with AudioState (see Start()), and
+ // must not be added here either: Stop() would not remove them again.
+ if (sending_ && !config_.external_source) {

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.

🔴 Existing WebRTC artifact retains audio crash

For default builds, StoreEncoderProperties() still runs from the unchanged prebuilt WebRTC artifact. WEBRTC_TAG selects that artifact, so publishing and removing native audio tracks during platform capture can still crash.

Learn more

The default build links a downloaded WebRTC static library rather than compiling this patch. WEBRTC_TAG still names the existing release, and download_url selects its unchanged archive. The new condition exists only in a WebRTC build made from the updated patch. Until a patched archive is published and selected, default SDK builds retain the dangling send-stream behavior under active platform capture.

Example: An application installs this SDK update without setting LK_CUSTOM_WEBRTC. It downloads the existing webrtc-89d790b archive, then repeatedly publishes and removes a stereo NativeAudioSource track while PlatformAudio records. The archive still registers the external stream in AudioState, so the capture thread can reach it after destruction.

Recommended fix: Build and publish patched WebRTC artifacts for supported targets, update WEBRTC_TAG to the new artifact release, and verify a default SDK build uses them.

Devin Review


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

@hiroshihorie
hiroshihorie self-requested a review October 3, 2026 09:38

@hiroshihorie hiroshihorie left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, please sign the CLA

The m150 port of external_audio_source.patch (livekit#1284) dropped
`sending_ = false;` from AudioSendStream::Stop(), so a stopped stream
stayed sending. StoreEncoderProperties() then adds it to AudioState
whenever a new encoder is set up, and does not check
config_.external_source, while Stop() only unregisters non-external
streams. A NativeAudioSource stream added that way was never removed,
and with PlatformAudio recording, the capture thread called into it
from AudioTransportImpl::SendProcessedData after it was destroyed.

Keep `sending_ = false;` in Stop() and leave external sources out of
AudioState in StoreEncoderProperties(), as Start() already does.

Fixes livekit#1490
@richard134
richard134 force-pushed the fix-external-audio-source-sending branch from 2724c41 to fa5e901 Compare October 3, 2026 12:45
This change only edits the patch, so no released crate picks it up until
a new WebRTC build is published and WEBRTC_TAG points at it. The
changeset moves to that bump PR so the changelog lands with the fix.

@alan-george-lk alan-george-lk 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.

This fixed a crash in a platform audio unit test in the C++ SDK, appreciate it! Note that Devin.ai flagged a red-level warning

@hiroshihorie hiroshihorie added the internal to tag changes that don't require changelog documentation label Oct 17, 2026

@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 found 1 new potential issue.

⚠️ 1 issue in files not directly in the diff

⚠️ Audio source fix has no changeset

Deleting this changeset leaves the audio source patch without a release entry. The remaining changeset covers a different change, so knope cannot document this fix.

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

Devin Review

@hiroshihorie
hiroshihorie merged commit 3c688ad into livekit:main Oct 17, 2026
36 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal to tag changes that don't require changelog documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

external_audio_source.patch: m150 port drops sending_ = false, NativeAudioSource send streams left in AudioState (use after free in SendProcessedData)

4 participants