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

v1 Fix DispatchQueueTimer crashes #310

Merged
merged 1 commit into from
Jan 18, 2024
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
4 changes: 2 additions & 2 deletions Sources/LiveKit/Core/SignalClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ private extension SignalClient {

self.pingTimeoutTimer = {
let timer = DispatchQueueTimer(timeInterval: TimeInterval(jr.pingTimeout), queue: self.queue)
timer.handler = { [weak self] in
timer.setOnTimer { [weak self] in
guard let self = self else { return }
self.log("ping/pong timed out", .error)
self.cleanUp(reason: .networkError(SignalClientError.serverPingTimedOut()))
Expand Down Expand Up @@ -729,7 +729,7 @@ private extension SignalClient {

pingIntervalTimer = {
let timer = DispatchQueueTimer(timeInterval: TimeInterval(jr.pingInterval), queue: queue)
timer.handler = { [weak self] in self?.onPingIntervalTimer() }
timer.setOnTimer { [weak self] in self?.onPingIntervalTimer() }
timer.resume()
return timer
}()
Expand Down
2 changes: 1 addition & 1 deletion Sources/LiveKit/Core/Transport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ internal class Transport: MulticastDelegate<TransportDelegate> {
DispatchQueue.liveKitWebRTC.sync { pc.delegate = self }
add(delegate: delegate)

statsTimer.handler = { [weak self] in
statsTimer.setOnTimer { [weak self] in
self?.onStatsTimer()
}

Expand Down
98 changes: 62 additions & 36 deletions Sources/LiveKit/Support/DispatchQueueTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,71 +18,97 @@ import Foundation

internal class DispatchQueueTimer: Loggable {

typealias OnTimer = (() -> Void)

public enum State {
case suspended
case resumed
}

private let queue: DispatchQueue?
private let timeInterval: TimeInterval
private var timer: DispatchSourceTimer!
public var handler: (() -> Void)?
public private(set) var state: State = .suspended
private let _queue: DispatchQueue?
private let _timeInterval: TimeInterval
private var _timer: DispatchSourceTimer!
private var _state: State = .suspended
private var _handler: OnTimer?

private let _lock = UnfairLock()

public init(timeInterval: TimeInterval, queue: DispatchQueue? = nil) {
self.timeInterval = timeInterval
self.queue = queue
self.timer = createTimer()
_timeInterval = timeInterval
_queue = queue
_timer = _createTimer()
}

deinit {
cleanUpTimer()
handler = nil
_cleanUpTimer()
_handler = nil
}

// reset the state
public func reset() {
cleanUpTimer()
timer = createTimer()
state = .suspended
}
// MARK: - Public

public func restart() {
reset()
resume()
public func setOnTimer(_ block: @escaping OnTimer) {
_lock.sync {
_handler = block
}
}

// continue from where it was suspended
public func resume() {
if state == .resumed {
return
_lock.sync {
_resume()
}
state = .resumed
timer.resume()
}

public func suspend() {
if state == .suspended {
return
_lock.sync {
_suspend()
}
}

public func restart() {
_lock.sync {
_restart()
}
state = .suspended
timer.suspend()
}

private func createTimer() -> DispatchSourceTimer {
let timer = DispatchSource.makeTimerSource(queue: queue)
timer.schedule(deadline: .now() + self.timeInterval, repeating: self.timeInterval)
timer.setEventHandler { [weak self] in self?.handler?() }
// MARK: - Private

private func _reset() {
_cleanUpTimer()
_timer = _createTimer()
_state = .suspended
}

private func _restart() {
_reset()
_resume()
}

private func _resume() {
if _state == .resumed { return }
_state = .resumed
_timer.resume()
}

private func _suspend() {
if _state == .suspended { return }
_state = .suspended
_timer.suspend()
}

private func _createTimer() -> DispatchSourceTimer {
let timer = DispatchSource.makeTimerSource(queue: _queue)
timer.schedule(deadline: .now() + self._timeInterval, repeating: self._timeInterval)
timer.setEventHandler { [weak self] in self?._handler?() }
return timer
}

private func cleanUpTimer() {
timer.setEventHandler {}
timer.cancel()
private func _cleanUpTimer() {
_timer.setEventHandler {}
_timer.cancel()
/*
If the timer is suspended, calling cancel without resuming
triggers a crash. This is documented here https://forums.developer.apple.com/thread/15902
*/
resume()
_resume()
}
}
4 changes: 2 additions & 2 deletions Sources/LiveKit/Track/Capturers/MacOSScreenCapturer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public class MacOSScreenCapturer: VideoCapturer {

let timeInterval: TimeInterval = 1 / Double(options.fps)
dispatchSourceTimer = DispatchQueueTimer(timeInterval: timeInterval, queue: captureQueue)
dispatchSourceTimer?.handler = { [weak self] in self?.onDispatchSourceTimer() }
dispatchSourceTimer?.setOnTimer { [weak self] in self?.onDispatchSourceTimer() }
dispatchSourceTimer?.resume()
}

Expand Down Expand Up @@ -447,7 +447,7 @@ extension MacOSScreenCapturer {

frameResendTimer = {
let timer = DispatchQueueTimer(timeInterval: timeInterval, queue: self.captureQueue)
timer.handler = { [weak self] in self?.onFrameResendTimer() }
timer.setOnTimer { [weak self] in self?.onFrameResendTimer() }
timer.resume()
return timer
}()
Expand Down
2 changes: 1 addition & 1 deletion Sources/LiveKit/Track/Track.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public class Track: NSObject, Loggable {
}
}

statsTimer.handler = { [weak self] in
statsTimer.setOnTimer { [weak self] in
self?.onStatsTimer()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class RemoteTrackPublication: TrackPublication {
track: track,
participant: participant)

asTimer.handler = { [weak self] in self?.onAdaptiveStreamTimer() }
asTimer.setOnTimer { [weak self] in self?.onAdaptiveStreamTimer() }
}

deinit {
Expand Down
4 changes: 2 additions & 2 deletions Sources/LiveKit/Views/VideoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public class VideoView: NativeView, Loggable {
}
}

_renderTimer.handler = { [weak self] in
_renderTimer.setOnTimer { [weak self] in

guard let self = self else { return }

Expand All @@ -351,7 +351,7 @@ public class VideoView: NativeView, Loggable {
}
}

_fpsTimer.handler = { [weak self] in
_fpsTimer.setOnTimer { [weak self] in

guard let self = self else { return }

Expand Down