Skip to content
Open
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: 4 additions & 0 deletions OpenParsec.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
A1B2C3D42F8E000200000002 /* StreamRecovery.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D42F8E000200000001 /* StreamRecovery.swift */; };
170E0A772BEF5AFF00B0EA32 /* ParsecViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170E0A762BEF5AFF00B0EA32 /* ParsecViewController.swift */; };
17374D412CB8E35D00EDCEE7 /* KeyCodeTranslators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17374D402CB8E35500EDCEE7 /* KeyCodeTranslators.swift */; };
17840D1D2CB639530097374B /* ParsecUserData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17840D1C2CB6394D0097374B /* ParsecUserData.swift */; };
Expand Down Expand Up @@ -59,6 +60,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
A1B2C3D42F8E000200000001 /* StreamRecovery.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StreamRecovery.swift; sourceTree = "<group>"; };
170E0A762BEF5AFF00B0EA32 /* ParsecViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParsecViewController.swift; sourceTree = "<group>"; };
17374D402CB8E35500EDCEE7 /* KeyCodeTranslators.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyCodeTranslators.swift; sourceTree = "<group>"; };
17840D1C2CB6394D0097374B /* ParsecUserData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParsecUserData.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -141,6 +143,7 @@
isa = PBXGroup;
children = (
E76224A02F8C017700A2F86F /* ParsecBackgroundManager.swift */,
A1B2C3D42F8E000200000001 /* StreamRecovery.swift */,
E762250A2F8D0A0000A2F86F /* PictureInPictureManager.swift */,
17374D402CB8E35500EDCEE7 /* KeyCodeTranslators.swift */,
17840D1C2CB6394D0097374B /* ParsecUserData.swift */,
Expand Down Expand Up @@ -290,6 +293,7 @@
27AC751829EA339B00E8CAF7 /* URLImage.swift in Sources */,
27B23A242B1B98EF00B52F14 /* ParsecMetalViewController.swift in Sources */,
E76224A12F8C017F00A2F86F /* ParsecBackgroundManager.swift in Sources */,
A1B2C3D42F8E000200000002 /* StreamRecovery.swift in Sources */,
E762250B2F8D0A0100A2F86F /* PictureInPictureManager.swift in Sources */,
27A923E029E8E53000F54BDA /* TouchHandlingView.swift in Sources */,
27E61A92292965FC00FF6563 /* AppDelegate.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion OpenParsec/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ struct MainView: View {
pollTimer = nil
CParsec.initialize()
DataManager.model.reset()
ParsecBackgroundManager.shared.reconnectAttempts = 0
ParsecBackgroundManager.shared.recoveryBudget.reset()
connectingToName = who.hostname
retryingConnect = retried
withAnimation { isConnecting = true }
Expand Down
4 changes: 2 additions & 2 deletions OpenParsec/ParsecBackgroundManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ParsecBackgroundManager {
}
private var didDisconnectDueToBackground = false
private(set) var isReconnecting = false
var reconnectAttempts = 0
var recoveryBudget = StreamRecoveryBudget()
var reconnectStartTime = Date()
var resumedAt: Date?
var isPaused = false
Expand Down Expand Up @@ -115,7 +115,7 @@ class ParsecBackgroundManager {
didDisconnectDueToBackground = false
endedByUser = true
isReconnecting = false
reconnectAttempts = 0
recoveryBudget.reset()
resumeOnLaunch = false
}

Expand Down
52 changes: 34 additions & 18 deletions OpenParsec/ParsecView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ struct ParsecStatusBar: View {
@Binding var DCAlertText: String
@Binding var isReconnecting: Bool
@Binding var connectionLost: Bool
@Binding var restoreGuard: StreamRestoreGuard
@State var networkFailureSince: Date?
@State var parsecViewController: ParsecViewController?
@State var wasDisconnected: Bool = true
let timer = Timer.publish(every: 0.5, on: .main, in: .common).autoconnect()
let onSilentDisconnect: () -> Void

init(showMenu: Binding<Bool>, showDCAlert: Binding<Bool>, DCAlertText: Binding<String>, parsecViewController: ParsecViewController, isReconnecting: Binding<Bool>, connectionLost: Binding<Bool>, onSilentDisconnect: @escaping () -> Void) {
init(showMenu: Binding<Bool>, showDCAlert: Binding<Bool>, DCAlertText: Binding<String>, parsecViewController: ParsecViewController, isReconnecting: Binding<Bool>, connectionLost: Binding<Bool>, restoreGuard: Binding<StreamRestoreGuard>, onSilentDisconnect: @escaping () -> Void) {
_showMenu = showMenu
_showDCAlert = showDCAlert
_DCAlertText = DCAlertText
_isReconnecting = isReconnecting
_connectionLost = connectionLost
_restoreGuard = restoreGuard
self.parsecViewController = parsecViewController
self.onSilentDisconnect = onSilentDisconnect
}
Expand Down Expand Up @@ -55,6 +57,9 @@ struct ParsecStatusBar: View {
return
}

let mgr = ParsecBackgroundManager.shared
let now = ProcessInfo.processInfo.systemUptime
restoreGuard.expire(at: now)
var pcs = ParsecClientStatus()
var status = CParsec.getStatusEx(&pcs)

Expand All @@ -63,7 +68,7 @@ struct ParsecStatusBar: View {
pipActive = PictureInPictureManager.shared.isPiPActive
}

if status == PARSEC_OK && pcs.networkFailure && !isReconnecting && !pipActive {
if status == PARSEC_OK && pcs.networkFailure && !pipActive {
if networkFailureSince == nil { networkFailureSince = Date() }
let lost = Date().timeIntervalSince(networkFailureSince!)
let justResumed = Date().timeIntervalSince(ParsecBackgroundManager.shared.resumedAt ?? .distantPast) < 10
Expand All @@ -81,6 +86,7 @@ struct ParsecStatusBar: View {
}

if status != PARSEC_OK {
mgr.recoveryBudget.recordFailure()
if ParsecBackgroundManager.shared.isMarkedForReconnect || ParsecBackgroundManager.shared.endedByUser {
return
}
Expand Down Expand Up @@ -110,21 +116,16 @@ struct ParsecStatusBar: View {
}

if SettingsHandler.autoReconnect, !status.isPermanentFailure, let peerId = ParsecBackgroundManager.shared.lastPeerId {
let mgr = ParsecBackgroundManager.shared
if mgr.reconnectAttempts < 3 {
let gap: TimeInterval = mgr.reconnectAttempts == 0 ? 0 : mgr.reconnectAttempts == 1 ? 2 : 5
if Date().timeIntervalSince(mgr.reconnectStartTime) < gap {
return
}
mgr.reconnectAttempts += 1
appNote("[reconnect] attempt \(mgr.reconnectAttempts) after status \(status.rawValue)")
if !mgr.recoveryBudget.isExhausted {
guard mgr.recoveryBudget.beginAttempt(at: now) else { return }
appNote("[reconnect] attempt \(mgr.recoveryBudget.attempts) after status \(status.rawValue)")
isReconnecting = true
mgr.reconnectStartTime = Date()
mgr.glkViewController?.isPaused = true
parsecViewController?.resetKeyState()
CParsec.reconnect(peerId)
return
}
mgr.reconnectAttempts = 0
}

isReconnecting = false
Expand All @@ -141,8 +142,14 @@ struct ParsecStatusBar: View {
return
}

if pcs.networkFailure {
mgr.recoveryBudget.recordFailure()
return
}
let stable = mgr.recoveryBudget.recordHealthy(at: now)
if stable { restoreGuard.end() }
if isReconnecting {
ParsecBackgroundManager.shared.reconnectAttempts = 0
guard stable else { return }
isReconnecting = false
appNote("[reconnect] live again")
ParsecView.getHostUserData()
Expand Down Expand Up @@ -209,6 +216,7 @@ struct ParsecView: View {
@State var preferH265: Bool = true
@State var isReconnecting: Bool = false
@State var connectionLost: Bool = false
@State var restoreGuard = StreamRestoreGuard()
@State var showSettings: Bool = false
@State var decoderAtOpen: DecoderPref = .h264
@State var fpsAtOpen: Int = 0
Expand Down Expand Up @@ -255,12 +263,12 @@ struct ParsecView: View {
.zIndex(1)
.prefersPersistentSystemOverlaysHidden()

ParsecStatusBar(showMenu: $showMenu, showDCAlert: $showDCAlert, DCAlertText: $DCAlertText, parsecViewController: parsecViewController, isReconnecting: $isReconnecting, connectionLost: $connectionLost, onSilentDisconnect: { disconnect() })
ParsecStatusBar(showMenu: $showMenu, showDCAlert: $showDCAlert, DCAlertText: $DCAlertText, parsecViewController: parsecViewController, isReconnecting: $isReconnecting, connectionLost: $connectionLost, restoreGuard: $restoreGuard, onSilentDisconnect: { disconnect() })

if isReconnecting || connectionLost {
if isReconnecting || connectionLost || restoreGuard.isActive {
VStack(spacing: 12) {
ActivityIndicator(isAnimating: .constant(true), style: .large, tint: .white)
Text(isReconnecting ? ParsecBackgroundManager.shared.lastHostname.map { "Reconnecting to \($0)..." } ?? "Reconnecting..." : "Connection lost...")
Text(isReconnecting || restoreGuard.isActive ? ParsecBackgroundManager.shared.lastHostname.map { "Reconnecting to \($0)..." } ?? "Reconnecting..." : "Connection lost...")
.font(.system(size: 16, weight: .medium))
.foregroundColor(.white)
Button(action: { disconnect() }) {
Expand All @@ -278,7 +286,7 @@ struct ParsecView: View {
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.black.opacity(0.6))
.background(Color.black)
.zIndex(10)
}

Expand Down Expand Up @@ -560,6 +568,11 @@ struct ParsecView: View {
}
}
}
PictureInPictureManager.shared.onRestoreUserInterface = { [self] in
// a stale stream still reports OK for seconds, keep it covered until it proves healthy
ParsecBackgroundManager.shared.recoveryBudget.recordFailure()
restoreGuard.begin(at: ProcessInfo.processInfo.systemUptime)
}
PictureInPictureManager.shared.onPiPStartFailed = {
if UIApplication.shared.applicationState != .active {
ParsecBackgroundManager.shared.glkViewController?.isPaused = true
Expand Down Expand Up @@ -666,8 +679,11 @@ struct ParsecView: View {

func reconnectTo(_ peerId: String) {
isReconnecting = true
ParsecBackgroundManager.shared.reconnectAttempts = 0
ParsecBackgroundManager.shared.reconnectStartTime = Date()
let mgr = ParsecBackgroundManager.shared
mgr.recoveryBudget.reset()
_ = mgr.recoveryBudget.beginAttempt(at: ProcessInfo.processInfo.systemUptime)
mgr.reconnectStartTime = Date()
mgr.glkViewController?.isPaused = true
parsecViewController.resetKeyState()
CParsec.reconnect(peerId)
}
Expand Down
16 changes: 10 additions & 6 deletions OpenParsec/PictureInPictureManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class PictureInPictureManager: NSObject {
}

private var cachedFormatDescription: CMVideoFormatDescription?
private var displayLayerFlushes = 0
private var displayLayerRecovery = StreamRecoveryBudget()
private var displayLayerGaveUp = false

private(set) var isPiPActive = false
Expand Down Expand Up @@ -283,18 +283,22 @@ class PictureInPictureManager: NSObject {
}

private func displayLayerTakesFrames(_ displayLayer: AVSampleBufferDisplayLayer) -> Bool {
guard !displayLayerGaveUp else { return false }
let now = ProcessInfo.processInfo.systemUptime
if displayLayer.status == .failed {
guard !displayLayerGaveUp else { return false }
guard displayLayerFlushes < 3 else {
displayLayerRecovery.recordFailure()
guard !displayLayerRecovery.isExhausted else {
displayLayerGaveUp = true
handleTerminalDisplayLayerFailure(displayLayer)
return false
}
displayLayerFlushes += 1
guard displayLayerRecovery.beginAttempt(at: now) else { return false }
displayLayer.flush()
cachedFormatDescription = nil
} else if displayLayer.status == .rendering {
displayLayerFlushes = 0
displayLayerRecovery.recordHealthy(at: now)
} else {
displayLayerRecovery.recordFailure()
}
return displayLayer.isReadyForMoreMediaData
}
Expand Down Expand Up @@ -413,7 +417,7 @@ class PictureInPictureManager: NSObject {
}

private func resetDisplayLayerFailureState() {
displayLayerFlushes = 0
displayLayerRecovery.reset()
displayLayerGaveUp = false
}

Expand Down
5 changes: 5 additions & 0 deletions OpenParsec/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
private var pendingUnpause: DispatchWorkItem?

func sceneDidBecomeActive(_ scene: UIScene) {
ParsecBackgroundManager.shared.recoveryBudget.recordFailure()
if #available(iOS 15.0, *) {
PictureInPictureManager.shared.stopPiP()
if PictureInPictureManager.shared.stopFramePump() {
Expand Down Expand Up @@ -68,6 +69,10 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
}

func sceneWillEnterForeground(_ scene: UIScene) {
// returning through the app icon skips the PiP restore callback
if #available(iOS 15.0, *), PictureInPictureManager.shared.isPiPActive {
PictureInPictureManager.shared.onRestoreUserInterface?()
}
}

func sceneDidEnterBackground(_ scene: UIScene) {
Expand Down
56 changes: 56 additions & 0 deletions OpenParsec/StreamRecovery.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Foundation

struct StreamRestoreGuard: Equatable {
private var deadline: TimeInterval?

var isActive: Bool { deadline != nil }

mutating func begin(at time: TimeInterval) {
deadline = time + 20
}

mutating func expire(at time: TimeInterval) {
if let deadline, time >= deadline { end() }
}

mutating func end() {
deadline = nil
}
}

struct StreamRecoveryBudget {
private(set) var attempts = 0
private(set) var lastAttemptAt: TimeInterval?
private var healthySince: TimeInterval?

var isExhausted: Bool { attempts >= 3 }

mutating func beginAttempt(at time: TimeInterval) -> Bool {
healthySince = nil
guard !isExhausted else { return false }
if let lastAttemptAt {
let delay: TimeInterval = attempts == 1 ? 2 : 5
guard time - lastAttemptAt >= delay else { return false }
}
attempts += 1
lastAttemptAt = time
return true
}

mutating func recordFailure() {
healthySince = nil
}

@discardableResult
mutating func recordHealthy(at time: TimeInterval) -> Bool {
if healthySince == nil { healthySince = time }
guard time - healthySince! >= 5 else { return false }
attempts = 0
lastAttemptAt = nil
return true
}

mutating func reset() {
self = StreamRecoveryBudget()
}
}
Loading