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

Disconnect room if macOS will sleep #421

Merged
merged 2 commits into from
Jul 4, 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
8 changes: 8 additions & 0 deletions Sources/LiveKit/Core/Room.swift
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,14 @@ extension Room: AppStateDelegate {
await self.disconnect()
}
}

func appWillSleep() {
Task.detached {
await self.disconnect()
}
}

func appDidWake() {}
}

// MARK: - Devices
Expand Down
52 changes: 39 additions & 13 deletions Sources/LiveKit/Support/AppStateListener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,73 @@ import Foundation
import UIKit
#endif

#if canImport(AppKit)
import AppKit
#endif

@objc
protocol AppStateDelegate: AnyObject {
func appDidEnterBackground()
func appWillEnterForeground()
func appWillTerminate()
/// Only for macOS.
func appWillSleep()
/// Only for macOS.
func appDidWake()
}

class AppStateListener: MulticastDelegate<AppStateDelegate> {
static let shared = AppStateListener()

private let queue = OperationQueue()

private init() {
super.init(label: "AppStateDelegate")

let center = NotificationCenter.default
let defaultCenter = NotificationCenter.default

#if os(iOS)
center.addObserver(forName: UIApplication.didEnterBackgroundNotification,
object: nil,
queue: OperationQueue.main)
defaultCenter.addObserver(forName: UIApplication.didEnterBackgroundNotification,
object: nil,
queue: queue)
{ _ in

self.log("UIApplication.didEnterBackground")
self.notify { $0.appDidEnterBackground() }
}

center.addObserver(forName: UIApplication.willEnterForegroundNotification,
object: nil,
queue: OperationQueue.main)
defaultCenter.addObserver(forName: UIApplication.willEnterForegroundNotification,
object: nil,
queue: queue)
{ _ in

self.log("UIApplication.willEnterForeground")
self.notify { $0.appWillEnterForeground() }
}

center.addObserver(forName: UIApplication.willTerminateNotification,
object: nil,
queue: OperationQueue.main)
defaultCenter.addObserver(forName: UIApplication.willTerminateNotification,
object: nil,
queue: queue)
{ _ in

self.log("UIApplication.willTerminate")
self.notify { $0.appWillTerminate() }
}
#elseif os(macOS)
let workspaceCenter = NSWorkspace.shared.notificationCenter

workspaceCenter.addObserver(forName: NSWorkspace.willSleepNotification,
object: nil,
queue: queue)
{ _ in
self.log("NSWorkspace.willSleepNotification")
self.notify { $0.appWillSleep() }
}

workspaceCenter.addObserver(forName: NSWorkspace.didWakeNotification,
object: nil,
queue: queue)
{ _ in
self.log("NSWorkspace.didWakeNotification")
self.notify { $0.appDidWake() }
}
#endif
}
}