-
Notifications
You must be signed in to change notification settings - Fork 164
Fix audio processing delegate lifecycle #784
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e79588b
Progress capturePostProcessingDelegate
hiroshihorie d2f318c
Merge branch 'main' into hiroshi/audio-processing-lifecycle
hiroshihorie 2620ed9
un-weak etc
hiroshihorie 5ef1fcb
Move test
hiroshihorie b6fef49
changes
hiroshihorie e6019f4
Minor clean up
hiroshihorie b6679bf
update test import
hiroshihorie 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,87 @@ | ||
| /* | ||
| * Copyright 2025 LiveKit | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import Accelerate | ||
| import AVFoundation | ||
| import Foundation | ||
| import LiveKitWebRTC | ||
|
|
||
| @testable import LiveKit | ||
| import XCTest | ||
|
|
||
| private enum CallType { | ||
| case initialize | ||
| case process | ||
| case release | ||
| } | ||
|
|
||
| private class ProcessingDelegateTester: AudioCustomProcessingDelegate, @unchecked Sendable { | ||
| let label: String | ||
| struct State { | ||
| var entries: [CallType] = [] | ||
| } | ||
|
|
||
| let _state = StateSync(State()) | ||
|
|
||
| init(label: String) { | ||
| self.label = label | ||
| } | ||
|
|
||
| func audioProcessingInitialize(sampleRate sampleRateHz: Int, channels: Int) { | ||
| _state.mutate { $0.entries.append(.initialize) } | ||
| print("ProcessingDelegate(\(label)).Initialize(sampleRate: \(sampleRateHz), channels: \(channels))") | ||
| } | ||
|
|
||
| func audioProcessingProcess(audioBuffer: LiveKit.LKAudioBuffer) { | ||
| _state.mutate { $0.entries.append(.initialize) } | ||
| print("ProcessingDelegate(\(label)).Process(audioBuffer: \(audioBuffer.frames))") | ||
| } | ||
|
|
||
| func audioProcessingRelease() { | ||
| _state.mutate { $0.entries.append(.release) } | ||
| print("ProcessingDelegate(\(label)).Release") | ||
| } | ||
| } | ||
|
|
||
| class AudioProcessingLifecycle: LKTestCase { | ||
| func testAudioProcessing() async throws { | ||
| let processorA = ProcessingDelegateTester(label: "A") | ||
| let processorB = ProcessingDelegateTester(label: "B") | ||
| // Set processing delegate | ||
| AudioManager.shared.capturePostProcessingDelegate = processorA | ||
|
|
||
| try await withRooms([RoomTestingOptions(canPublish: true)]) { rooms in | ||
| // Alias to Room1 | ||
| let room1 = rooms[0] | ||
| // Publish mic | ||
| try await room1.localParticipant.setMicrophone(enabled: true) | ||
| do { | ||
| // 1 secs... | ||
| let ns = UInt64(1 * 1_000_000_000) | ||
| try await Task.sleep(nanoseconds: ns) | ||
| } | ||
| AudioManager.shared.capturePostProcessingDelegate = processorB | ||
| do { | ||
| // 1 secs... | ||
| let ns = UInt64(1 * 1_000_000_000) | ||
| try await Task.sleep(nanoseconds: ns) | ||
| } | ||
| } | ||
|
|
||
| // Remove processing delegate | ||
| AudioManager.shared.capturePostProcessingDelegate = nil | ||
| } | ||
| } |
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.
do we really need to set it weak ? or we make sure it will be set to nil when AudioRenderer get removed from the track ?
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.
I generally agree with this comment as posted elsewhere, for delegates it's the "default" approach, but for "top-down" ownership model (no cycles) we should use
weaksparingly.Uh oh!
There was an error while loading. Please reload this page.
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.
Either option works for me, as long as we’re okay with having inconsistencies related to the delegate weakness in the SDK, since anything using
MulticastDelegateis weak now, e.g.room.add(delegate:),AudioManager.shared.add(localAudioRenderer:), etc.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.
Unfortunately I don't see any option to "automate" it e.g. via swiftlint as every use case is vastly different and must be evaluated separately 😿