Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
46 changes: 29 additions & 17 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-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, 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-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-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-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-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-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, 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, 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, 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, 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, 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, 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-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, 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-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-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-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-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, 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, 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, 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, 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, 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, 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,29 @@

// 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)
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)
}
capturePostProcessingDelegateSubject.send(capturePostProcessingDelegate)
}
}

Expand All @@ -147,8 +148,19 @@
/// - Note: If you only need to observe the buffer (rather than modify it), use ``add(remoteAudioRenderer:)`` instead
/// - Note: If you need to observe the buffer for individual tracks, use ``RemoteAudioTrack/add(audioRenderer:)`` instead
public var renderPreProcessingDelegate: AudioCustomProcessingDelegate? {
get { renderPreProcessingDelegateAdapter.target }
set { renderPreProcessingDelegateAdapter.set(target: newValue) }
didSet {
if let renderPreProcessingDelegate {
//
RTC.audioProcessingModule.renderPreProcessingDelegate = nil
renderPreProcessingDelegateAdapter.set(target: nil)
//
renderPreProcessingDelegateAdapter.set(target: renderPreProcessingDelegate)
RTC.audioProcessingModule.renderPreProcessingDelegate = renderPreProcessingDelegateAdapter
} else {
RTC.audioProcessingModule.renderPreProcessingDelegate = nil
renderPreProcessingDelegateAdapter.set(target: nil)
}
}
}

// MARK: - AudioDeviceModule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class AudioCustomProcessingDelegateAdapter: MulticastDelegate<AudioRenderer>, @u
// MARK: - Private

private struct State {
weak var target: AudioCustomProcessingDelegate?
var target: AudioCustomProcessingDelegate?
}

private var _state = StateSync(State())
Expand Down
108 changes: 108 additions & 0 deletions Tests/LiveKitAudioTests/AudioProcessingLifecycle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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 LiveKitTestSupport
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(.process) }
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)
}

// Verify processorA was initialized and received audio
let stateA = processorA._state.copy()
XCTAssertTrue(stateA.entries.contains(.initialize), "Processor A should have been initialized")
XCTAssertTrue(stateA.entries.contains(.process), "Processor A should have processed audio")

// Switch to processorB
AudioManager.shared.capturePostProcessingDelegate = processorB
do {
// 1 secs...
let ns = UInt64(1 * 1_000_000_000)
try await Task.sleep(nanoseconds: ns)
}

// Verify processorA was released
let stateA2 = processorA._state.copy()
XCTAssertTrue(stateA2.entries.contains(.release), "Processor A should have been released")

// Verify processorB was initialized and received audio
let stateB = processorB._state.copy()
XCTAssertTrue(stateB.entries.contains(.initialize), "Processor B should have been initialized")
XCTAssertTrue(stateB.entries.contains(.process), "Processor B should have processed audio")
}

// Remove processing delegate
AudioManager.shared.capturePostProcessingDelegate = nil

// Verify processorB was released
let stateB2 = processorB._state.copy()
XCTAssertTrue(stateB2.entries.contains(.release), "Processor B should have been released")
}
}
Loading