Repository navigation
consistently clean up audio contexts and graphs #2029
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
lukasIO
wants to merge
6
commits into
main
Choose a base branch
from
lukas/webaudio-fix
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
26546d5
consistently clean up audio contexts and graphs
lukasIO 7470e4c
Merge remote-tracking branch 'origin/main' into lukas/webaudio-fix
lukasIO 39d7041
Release audio contexts and web audio graphs on disconnect
lukasIO 1310fe6
fix: give the audio context test stub a real resume() body to satisfy…
lukasIO d5dea4f
Hand back the shared audio nodes instead of tearing them out
lukasIO 74ed7c1
Merge remote-tracking branch 'origin/main' into lukas/webaudio-fix
lukasIO 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| --- | ||
| 'livekit-client': minor | ||
| --- | ||
|
|
||
| Release the audio contexts and web audio graphs the SDK creates. Previously they accumulated over mute/unmute cycles. They are now collected after `room.disconnect()`. | ||
|
|
||
| - `detectSilence` tears its context down on the throw path as well as the happy one. | ||
| - The cleanup from `createAudioAnalyser` disconnects its nodes before it closes the context, and it is now idempotent. | ||
| - The shared empty audio stream track is refcounted. `releaseEmptyAudioStreamTrack` is a new export, and its context closes once the last clone is handed back. | ||
| - If `webAudioMix` is an object without an `audioContext`, a room now closes the context it created for itself. | ||
| - When the underlying track is replaced, audio processors are passed the audio context. This lets them rebuild their filter nodes on unmute. Previously they left the old nodes behind. | ||
| - The iOS dummy audio element and its `visibilitychange` listener are torn down on disconnect. Previously the listener outlived every room on the page. | ||
|
|
||
| This also fixes a false `AudioSilenceDetected`. Silence detection read a zero-filled buffer from a context that never reached `running`. An autoplay-blocked page therefore reported silence on every track it checked. | ||
|
|
||
| Three further behavior changes to be aware of: | ||
|
|
||
| - A track retained across rooms loses its audio processor. `room.disconnect({ stopTracks: false })` detaches the track from the audio context, which stops the processor. Call `setProcessor` again after you republish the track. | ||
| - Under `webAudioMix`, every attached element is muted, not only the first one. A second attached element no longer plays the track twice. When the audio context goes away, those elements are unmuted again and their volume is restored. | ||
| - `Participant.setAudioContext` and `LocalAudioTrack.setAudioContext` now return a promise. Both are marked `@internal`, but both appear in the published type declarations. | ||
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 |
|---|---|---|
|
|
@@ -78,6 +78,8 @@ let startTime: number; | |
|
|
||
| let streamReaderAbortController: AbortController | undefined; | ||
|
|
||
| let stopLocalVolumeMeter: (() => void) | undefined; | ||
|
|
||
| let localDataTracks: Array<LocalDataTrack> = []; | ||
| let remoteDataTracks: Array<RemoteDataTrack> = []; | ||
|
|
||
|
|
@@ -307,17 +309,26 @@ const appActions = { | |
| const track = pub.track; | ||
|
|
||
| if (isLocalTrack(track) && isAudioTrack(track)) { | ||
| const { calculateVolume } = createAudioAnalyser(track); | ||
| const { calculateVolume, cleanup } = createAudioAnalyser(track); | ||
|
|
||
| setInterval(() => { | ||
| const interval = setInterval(() => { | ||
| $('local-volume')?.setAttribute('value', calculateVolume().toFixed(4)); | ||
| }, 200); | ||
| stopLocalVolumeMeter?.(); | ||
| stopLocalVolumeMeter = () => { | ||
| clearInterval(interval); | ||
| cleanup().catch(() => {}); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Should this log the caught error? |
||
| }; | ||
| } | ||
| renderParticipant(room.localParticipant); | ||
| updateButtonsForPublishState(); | ||
| renderScreenShare(room); | ||
| }) | ||
| .on(RoomEvent.LocalTrackUnpublished, () => { | ||
| .on(RoomEvent.LocalTrackUnpublished, (pub) => { | ||
| if (pub.kind === Track.Kind.Audio) { | ||
| stopLocalVolumeMeter?.(); | ||
| stopLocalVolumeMeter = undefined; | ||
| } | ||
| renderParticipant(room.localParticipant); | ||
| updateButtonsForPublishState(); | ||
| renderScreenShare(room); | ||
|
|
@@ -1013,6 +1024,9 @@ function handleRoomDisconnect(reason?: DisconnectReason) { | |
| }); | ||
| renderScreenShare(currentRoom); | ||
|
|
||
| stopLocalVolumeMeter?.(); | ||
| stopLocalVolumeMeter = undefined; | ||
|
|
||
| localDataTracks = []; | ||
| renderLocalDataTracks(); | ||
|
|
||
|
|
||
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.
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.
suggestion: This might be worth calling out in whatever docs surfaces mention
TrackProcessorand associated behavior.