Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions Sources/LiveKit/Audio/Manager/AudioManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@
// Keep this var within State so it's protected by UnfairLock
public var localTracksCount: Int = 0
public var remoteTracksCount: Int = 0
public var customConfigureFunc: ConfigureAudioSessionFunc?

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-14, 15.4, macOS,variant=Mac Catalyst)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-14, 15.4, macOS,variant=Mac Catalyst)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-26, latest, tvOS Simulator,name=Apple TV,OS=26.0)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-14, 15.4, tvOS Simulator,name=Apple TV,OS=17.5)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-26, latest, macOS,variant=Mac Catalyst)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-26, latest, macOS,variant=Mac Catalyst)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-15, 16.4, tvOS Simulator,name=Apple TV,OS=18.5)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-15, 16.4, tvOS Simulator,name=Apple TV,OS=18.5)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-15, 16.4, visionOS Simulator,name=Apple Vision Pro,OS=2.5)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-15, 16.4, visionOS Simulator,name=Apple Vision Pro,OS=2.5)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-26, latest, iOS Simulator,name=iPhone 17 Pro,OS=26.0, true)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-26, latest, iOS Simulator,name=iPhone 17 Pro,OS=26.0, true)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-26, latest, iOS Simulator,name=iPhone 17 Pro,OS=26.0, true)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-15, 16.4, macOS,variant=Mac Catalyst)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-14, 15.4, iOS Simulator,name=iPhone 15 Pro,OS=17.5)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-15, 16.4, iOS Simulator,name=iPhone 16 Pro,OS=18.5)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-15, 16.4, iOS Simulator,name=iPhone 16 Pro,OS=18.5)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-26, latest, visionOS Simulator,name=Apple Vision Pro,OS=26.0)

'ConfigureAudioSessionFunc' is deprecated

Check warning on line 104 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-26, latest, visionOS Simulator,name=Apple Vision Pro,OS=26.0)

'ConfigureAudioSessionFunc' is deprecated
public var sessionConfiguration: AudioSessionConfiguration?

public var trackState: TrackState {

Check warning on line 107 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-14, 15.4, macOS,variant=Mac Catalyst)

'TrackState' is deprecated

Check warning on line 107 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-14, 15.4, macOS,variant=Mac Catalyst)

'TrackState' is deprecated

Check warning on line 107 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-26, latest, tvOS Simulator,name=Apple TV,OS=26.0)

'TrackState' is deprecated

Check warning on line 107 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-14, 15.4, tvOS Simulator,name=Apple TV,OS=17.5)

'TrackState' is deprecated

Check warning on line 107 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-26, latest, macOS,variant=Mac Catalyst)

'TrackState' is deprecated

Check warning on line 107 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-15, 16.4, tvOS Simulator,name=Apple TV,OS=18.5)

'TrackState' is deprecated

Check warning on line 107 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-15, 16.4, tvOS Simulator,name=Apple TV,OS=18.5)

'TrackState' is deprecated

Check warning on line 107 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-15, 16.4, visionOS Simulator,name=Apple Vision Pro,OS=2.5)

'TrackState' is deprecated

Check warning on line 107 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-15, 16.4, visionOS Simulator,name=Apple Vision Pro,OS=2.5)

'TrackState' is deprecated

Check warning on line 107 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-26, latest, iOS Simulator,name=iPhone 17 Pro,OS=26.0, true)

'TrackState' is deprecated

Check warning on line 107 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-26, latest, iOS Simulator,name=iPhone 17 Pro,OS=26.0, true)

'TrackState' is deprecated

Check warning on line 107 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-15, 16.4, macOS,variant=Mac Catalyst)

'TrackState' is deprecated

Check warning on line 107 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-14, 15.4, iOS Simulator,name=iPhone 15 Pro,OS=17.5)

'TrackState' is deprecated

Check warning on line 107 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-15, 16.4, iOS Simulator,name=iPhone 16 Pro,OS=18.5)

'TrackState' is deprecated

Check warning on line 107 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-15, 16.4, iOS Simulator,name=iPhone 16 Pro,OS=18.5)

'TrackState' is deprecated

Check warning on line 107 in Sources/LiveKit/Audio/Manager/AudioManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-26, latest, visionOS Simulator,name=Apple Vision Pro,OS=26.0)

'TrackState' is deprecated
switch (localTracksCount > 0, remoteTracksCount > 0) {
case (true, false): .localOnly
case (false, true): .remoteOnly
Expand All @@ -117,28 +117,28 @@

// MARK: - AudioProcessingModule

private lazy var capturePostProcessingDelegateAdapter: AudioCustomProcessingDelegateAdapter = {
let adapter = AudioCustomProcessingDelegateAdapter(label: "capturePost")
RTC.audioProcessingModule.capturePostProcessingDelegate = adapter
return adapter
}()

private lazy var renderPreProcessingDelegateAdapter: AudioCustomProcessingDelegateAdapter = {
let adapter = AudioCustomProcessingDelegateAdapter(label: "renderPre")
RTC.audioProcessingModule.renderPreProcessingDelegate = adapter
return adapter
}()
private lazy var capturePostProcessingDelegateAdapter = AudioCustomProcessingDelegateAdapter(label: "capturePost")

private lazy var renderPreProcessingDelegateAdapter = AudioCustomProcessingDelegateAdapter(label: "renderPre")

let capturePostProcessingDelegateSubject = CurrentValueSubject<AudioCustomProcessingDelegate?, Never>(nil)

/// Add a delegate to modify the local audio buffer before it is sent to the network
/// - Note: Only one delegate can be set at a time, but you can create one to wrap others if needed
/// - Note: If you only need to observe the buffer (rather than modify it), use ``add(localAudioRenderer:)`` instead
public var capturePostProcessingDelegate: AudioCustomProcessingDelegate? {
get { capturePostProcessingDelegateAdapter.target }
set {
capturePostProcessingDelegateAdapter.set(target: newValue)
capturePostProcessingDelegateSubject.send(newValue)
public weak var capturePostProcessingDelegate: AudioCustomProcessingDelegate? {
Copy link
Contributor

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 ?

Copy link
Contributor

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 weak sparingly.

Copy link
Member Author

@hiroshihorie hiroshihorie Oct 7, 2025

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 MulticastDelegate is weak now, e.g. room.add(delegate:), AudioManager.shared.add(localAudioRenderer:), etc.

Copy link
Contributor

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 😿

didSet {
if let capturePostProcessingDelegate {
//
RTC.audioProcessingModule.capturePostProcessingDelegate = nil
capturePostProcessingDelegateAdapter.set(target: nil)
//
capturePostProcessingDelegateAdapter.set(target: capturePostProcessingDelegate)
RTC.audioProcessingModule.capturePostProcessingDelegate = capturePostProcessingDelegateAdapter
} else {
RTC.audioProcessingModule.capturePostProcessingDelegate = nil
capturePostProcessingDelegateAdapter.set(target: nil)
}
}
}

Expand Down
87 changes: 87 additions & 0 deletions Tests/LiveKitTests/Audio/AudioProcessingLifecycle.swift
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
}
}
Loading