Skip to content
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

Lazy initialize singleton mixer node #58

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 7 additions & 4 deletions AudioStreaming/Streaming/AudioPlayer/AudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ open class AudioPlayer {
private var stateBeforePaused: InternalState = .initial

/// The underlying `AVAudioEngine` object
private let audioEngine = AVAudioEngine()
private let audioEngine: AVAudioEngine
/// An `AVAudioUnit` object that represents the audio player
private(set) var player = AVAudioUnit()
/// An `AVAudioUnitTimePitch` that controls the playback rate of the audio engine
Expand All @@ -134,7 +134,8 @@ open class AudioPlayer {

public init(configuration: AudioPlayerConfiguration = .default) {
self.configuration = configuration.normalizeValues()

let engine = AVAudioEngine()
self.audioEngine = engine
rendererContext = AudioRendererContext(configuration: configuration, outputAudioFormat: outputAudioFormat)
playerContext = AudioPlayerContext()
entriesQueue = PlayerQueueEntries()
Expand All @@ -150,12 +151,14 @@ open class AudioPlayer {
rendererContext: rendererContext,
outputAudioFormat: outputAudioFormat.basicStreamDescription)

frameFilterProcessor = FrameFilterProcessor(mixerNode: audioEngine.mainMixerNode)

playerRenderProcessor = AudioPlayerRenderProcessor(playerContext: playerContext,
rendererContext: rendererContext,
outputAudioFormat: outputAudioFormat.basicStreamDescription)


frameFilterProcessor = FrameFilterProcessor(mixerNodeProvider: {
engine.mainMixerNode
})
Comment on lines +159 to +161
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could also be possible that the frameFilterProcessor be initialised when a relevant method is first used. But we could improve on another PR.

configPlayerContext()
configPlayerNode()
setupEngine()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,17 @@ final class FrameFilterProcessor: NSObject, FrameFiltering {
}

private let lock = UnfairLock()
private let mixerNode: AVAudioMixerNode
private let mixerNodeProvider: (() -> AVAudioMixerNode)
private lazy var mixerNode: AVAudioMixerNode = {
return mixerNodeProvider()
}()

private(set) var entries: [FilterEntry] = []

private var hasInstalledTap: Bool = false

init(mixerNode: AVAudioMixerNode) {
self.mixerNode = mixerNode
init(mixerNodeProvider: @escaping (() -> AVAudioMixerNode)) {
self.mixerNodeProvider = mixerNodeProvider
}

public func add(entry: FilterEntry) {
Expand Down