askrobots / network-computer-ios
Public
iPhone client for network-computer: see and drive a remote Linux/Mac desktop over WebRTC, phone as trackpad and keyboard, desktop on a TV via AirPlay.
ExternalSceneDelegate.swift · 1.4 KB · 37 lines
| 1 | import UIKit |
| 2 | import Combine |
| 3 | import WebRTC |
| 4 | |
| 5 | /// Drives a TV over USB-C/HDMI or AirPlay: the remote desktop fills the external |
| 6 | /// screen at its native resolution while the phone stays a trackpad and keyboard. |
| 7 | final class ExternalSceneDelegate: UIResponder, UIWindowSceneDelegate { |
| 8 | var window: UIWindow? |
| 9 | private var sub: AnyCancellable? |
| 10 | |
| 11 | func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options: UIScene.ConnectionOptions) { |
| 12 | guard let ws = scene as? UIWindowScene else { return } |
| 13 | let w = UIWindow(windowScene: ws) |
| 14 | let vc = UIViewController() |
| 15 | vc.view.backgroundColor = .black |
| 16 | let video = RTCMTLVideoView(frame: vc.view.bounds) |
| 17 | video.videoContentMode = .scaleAspectFit |
| 18 | video.autoresizingMask = [.flexibleWidth, .flexibleHeight] |
| 19 | vc.view.addSubview(video) |
| 20 | w.rootViewController = vc |
| 21 | window = w |
| 22 | w.isHidden = false |
| 23 | |
| 24 | sub = SessionStore.shared.$videoTrack.sink { [weak video] track in |
| 25 | guard let video else { return } |
| 26 | SessionStore.shared.externalRenderer = video |
| 27 | track?.add(video) |
| 28 | } |
| 29 | SessionStore.shared.externalDisplayConnected = true |
| 30 | } |
| 31 | |
| 32 | func sceneDidDisconnect(_ scene: UIScene) { |
| 33 | SessionStore.shared.externalRenderer = nil |
| 34 | SessionStore.shared.externalDisplayConnected = false |
| 35 | sub = nil |
| 36 | } |
| 37 | } |