Skip to content

Commit

Permalink
Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie committed Sep 29, 2024
1 parent 8e3dbe9 commit 053aa0c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Sources/LiveKit/Convenience/AudioProcessing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public class AudioVisualizeProcessor {
maxDB: Float = 32.0,
bandsCount: Int = 100,
isCentered: Bool = false,
smoothingFactor: Float = 0.2) // Smoothing factor for smoother transitions
smoothingFactor: Float = 0.3) // Smoothing factor for smoother transitions
{
self.minFrequency = minFrequency
self.maxFrequency = maxFrequency
Expand Down
44 changes: 7 additions & 37 deletions Sources/LiveKit/Protocols/AudioRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,17 @@ public protocol AudioRenderer {
func render(pcmBuffer: AVAudioPCMBuffer)
}

class AudioRendererAdapter: NSObject, LKRTCAudioRenderer {
private weak var target: AudioRenderer?
private let targetHashValue: Int
class AudioRendererAdapter: MulticastDelegate<AudioRenderer>, LKRTCAudioRenderer {
//
typealias Delegate = AudioRenderer

struct GlobalState {
var instanceCount: Int = 0
init() {
super.init(label: "AudioRendererAdapter")
}

private static var _state = StateSync(GlobalState())

init(target: AudioRenderer) {
self.target = target
targetHashValue = ObjectIdentifier(target).hashValue

let count = Self._state.mutate {
$0.instanceCount += 1
return $0.instanceCount
}

print("AudioRendererAdapter instance count: \(count)")
}

deinit {
let count = Self._state.mutate {
$0.instanceCount -= 1
return $0.instanceCount
}
print("AudioRendererAdapter instance count: \(count)")
}
// MARK: - LKRTCAudioRenderer

func render(pcmBuffer: AVAudioPCMBuffer) {
target?.render(pcmBuffer: pcmBuffer)
}

// Proxy the equality operators
override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? AudioRendererAdapter else { return false }
return targetHashValue == other.targetHashValue
}

override var hash: Int {
targetHashValue
notify { $0.render(pcmBuffer: pcmBuffer) }
}
}
26 changes: 22 additions & 4 deletions Sources/LiveKit/Track/Remote/RemoteAudioTrack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import AVFoundation
import CoreMedia

#if swift(>=5.9)
Expand All @@ -36,6 +37,8 @@ public class RemoteAudioTrack: Track, RemoteTrack, AudioTrack {
}
}

private lazy var _adapter = AudioRendererAdapter()

init(name: String,
source: Track.Source,
track: LKRTCMediaStreamTrack,
Expand All @@ -48,14 +51,29 @@ public class RemoteAudioTrack: Track, RemoteTrack, AudioTrack {
reportStatistics: reportStatistics)
}

public func add(audioRenderer: AudioRenderer) {
deinit {
// Directly remove the adapter without unnecessary checks
guard let audioTrack = mediaTrack as? LKRTCAudioTrack else { return }
audioTrack.add(AudioRendererAdapter(target: audioRenderer))
audioTrack.remove(_adapter)
}

public func add(audioRenderer: AudioRenderer) {
let wasEmpty = _adapter.countDelegates == 0
_adapter.add(delegate: audioRenderer)
// Attach adapter only if it wasn't attached before
if wasEmpty {
guard let audioTrack = mediaTrack as? LKRTCAudioTrack else { return }
audioTrack.add(_adapter)
}
}

public func remove(audioRenderer: AudioRenderer) {
guard let audioTrack = mediaTrack as? LKRTCAudioTrack else { return }
audioTrack.remove(AudioRendererAdapter(target: audioRenderer))
_adapter.remove(delegate: audioRenderer)
// Remove adapter only if there are no more delegates
if _adapter.countDelegates == 0 {
guard let audioTrack = mediaTrack as? LKRTCAudioTrack else { return }
audioTrack.remove(_adapter)
}
}

// MARK: - Internal
Expand Down

0 comments on commit 053aa0c

Please sign in to comment.