-
Notifications
You must be signed in to change notification settings - Fork 195
PlatformAudio stability/shutdown #1193
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
Draft
alan-george-lk
wants to merge
5
commits into
main
Choose a base branch
from
feature/platform-audio-stability
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.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
01fc4c1
Fix FFI dispose cleanup and platform ADM teardown race
alan-george-lk 44e17fe
Update changeset for platform audio stability work
alan-george-lk d3bb145
Address PR review feedback on AdmProxy audio shutdown
alan-george-lk 2a1003d
Serialize LkRuntime lifecycles and fix audio I/O stop/detach ordering
alan-george-lk fd48e5c
Avoid blocking FFI dispose on runtime teardown
alan-george-lk 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,14 @@ | ||
| --- | ||
| livekit: minor | ||
| libwebrtc: minor | ||
| livekit-ffi: patch | ||
| webrtc-sys: patch | ||
| --- | ||
|
|
||
| Fix platform ADM teardown races on macOS and release FFI handles on dispose. | ||
|
|
||
| - Clear remaining FFI handles during `FfiServer::dispose` so native resources are released across repeated initialize/shutdown cycles. | ||
| - Stop and detach platform/synthetic audio I/O before peer connection factory teardown, preventing `CaptureWorkerThread` from delivering into destroyed transports. Audio I/O is stopped (joining the worker threads) before the audio callback is detached, since `AudioDeviceBuffer` refuses callback changes while media is active. | ||
| - Close rooms before dropping FFI track handles and stop platform capture when releasing the platform ADM reference. | ||
| - Add `LkRuntime::shutdown_audio_io()` and `PeerConnectionFactoryExt::shutdown_audio_io()` for explicit audio I/O shutdown during runtime teardown. | ||
| - Serialize runtime lifecycles: `LkRuntime::instance()` now waits (bounded, 10s) for a previous runtime's teardown to fully complete before constructing a new peer connection factory/ADM. This closes a race where factory/ADM/transport destruction from one initialize/shutdown cycle could overlap the next cycle's startup. |
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
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
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 |
|---|---|---|
|
|
@@ -71,6 +71,9 @@ AdmProxy::AdmProxy(const webrtc::Environment& env, webrtc::Thread* worker_thread | |
| AdmProxy::~AdmProxy() { | ||
| RTC_LOG(LS_VERBOSE) << "AdmProxy::~AdmProxy()"; | ||
|
|
||
| StopAudioIO(); | ||
|
|
||
| webrtc::MutexLock lock(&mutex_); | ||
| if (synthetic_adm_) { | ||
| synthetic_adm_->Terminate(); | ||
| synthetic_adm_ = nullptr; | ||
|
|
@@ -180,6 +183,7 @@ void AdmProxy::ReleasePlatformAdm() { | |
| // Note: We do NOT terminate the Platform ADM - it stays alive until destructor. | ||
| // This avoids iOS KVO race conditions from re-creating the ADM. | ||
| if (platform_adm_ref_count_ == 0) { | ||
| StopPlatformAudioIO(); | ||
| SwitchPlayoutModeIfNeeded(); | ||
| SwitchRecordingAdmIfNeeded(); | ||
| } | ||
|
|
@@ -273,6 +277,50 @@ void AdmProxy::SwitchRecordingAdmIfNeeded() { | |
| } | ||
| } | ||
|
|
||
| void AdmProxy::StopPlatformAudioIO() { | ||
| recording_ = false; | ||
|
|
||
| if (platform_adm_) { | ||
| // Stop capture/playout first: StopRecording()/StopPlayout() join the | ||
| // platform worker threads. Only then detach the callback - | ||
| // AudioDeviceBuffer refuses RegisterAudioCallback() while media is active, | ||
| // so detaching before stopping would silently fail and leave the transport | ||
| // attached. | ||
| platform_adm_->StopRecording(); | ||
| platform_adm_->StopPlayout(); | ||
| platform_adm_->RegisterAudioCallback(nullptr); | ||
| // platform_adm_ is kept alive for re-acquire and iOS compatibility; see | ||
| // ReleasePlatformAdm(). | ||
| } | ||
| } | ||
|
|
||
| void AdmProxy::StopAudioIO() { | ||
| webrtc::MutexLock lock(&mutex_); | ||
|
|
||
| recording_ = false; | ||
| playing_ = false; | ||
| recording_initialized_ = false; | ||
| playout_initialized_ = false; | ||
|
|
||
| // Stop before detaching: AudioDeviceBuffer refuses RegisterAudioCallback() | ||
| // while media is active, so the detach only takes effect once | ||
| // capture/playout worker threads have been stopped (and joined). | ||
| if (platform_adm_) { | ||
| platform_adm_->StopRecording(); | ||
| platform_adm_->StopPlayout(); | ||
| platform_adm_->RegisterAudioCallback(nullptr); | ||
| } | ||
|
|
||
| if (synthetic_adm_) { | ||
| synthetic_adm_->StopRecording(); | ||
| synthetic_adm_->StopPlayout(); | ||
|
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. same here |
||
| synthetic_adm_->RegisterAudioCallback(nullptr); | ||
| // synthetic_adm_ is kept alive until ~AdmProxy() / Terminate(). | ||
| } | ||
|
|
||
| audio_transport_ = nullptr; | ||
| } | ||
|
|
||
| // ============================================================================= | ||
| // AudioDeviceModule Interface Implementation | ||
| // ============================================================================= | ||
|
|
@@ -306,8 +354,9 @@ int32_t AdmProxy::Init() { | |
| } | ||
|
|
||
| int32_t AdmProxy::Terminate() { | ||
| webrtc::MutexLock lock(&mutex_); | ||
| StopAudioIO(); | ||
|
|
||
| webrtc::MutexLock lock(&mutex_); | ||
| int32_t result = 0; | ||
| if (synthetic_adm_) { | ||
| result = synthetic_adm_->Terminate(); | ||
|
|
@@ -554,11 +603,20 @@ int32_t AdmProxy::StopRecording() { | |
| webrtc::MutexLock lock(&mutex_); | ||
| recording_ = false; | ||
|
|
||
| auto* adm = recording_adm(); | ||
| if (adm) { | ||
| return adm->StopRecording(); | ||
| int32_t result = 0; | ||
| if (platform_adm_) { | ||
| const int32_t platform_result = platform_adm_->StopRecording(); | ||
| if (result == 0) { | ||
| result = platform_result; | ||
| } | ||
| } | ||
| return 0; | ||
| if (synthetic_adm_) { | ||
| const int32_t synthetic_result = synthetic_adm_->StopRecording(); | ||
| if (result == 0) { | ||
| result = synthetic_result; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| bool AdmProxy::Recording() const { | ||
|
|
||
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.
nit, curiously, why couldn't we set platform_adm_ = nullptr after calling StopXXX() function ?