// A FILM ON A TV (cinemas-airplay, 18 Nov 2026; owner, 17 Nov: a film from // Cinemas would not AirPlay to his projector). A sealed film plays from the // core's door at 127.0.0.1, which an AirPlay receiver cannot reach. What a // film does on a route is the core's (`shell.player` `route`): the TV plays // it from the phone's Wi-Fi address, or the phone plays it and its picture // goes to the TV. This is the player's side of it for one play: what it asks // the core as the route changes, and what it does with each answer. // // - Apple's player begins playing to a TV (`isExternalPlaybackActive`): ask // `external`. A `receiver` answer with a `stream` is the film's Wi-Fi // address: play it from where it stands, and ask `check` after // `check_after_s`, the receiver's time to reach it. // - A `phone` answer (no Wi-Fi, the receiver never asked, the film failed on // the TV): external playback off, so the phone decodes and the picture goes // out by mirroring, from what the phone plays it from again (an item the TV // could not play has failed); the core's words said once a play. Never an // error. // - The film fails while the TV plays it: ask `failed`. // - FETCHED IS NOT PLAYING (airplay-refused, 18 Nov 2026: the owner's Mac took // the index and refused the film, and the phone showed it connected for // minutes). Every film on a TV, kept, signed or sealed, is checked: each // `receiver` answer carries the next `check_after_s`, and every check says // where the phone's player stands and whether it is paused; the core // answers `phone` when the film was never seen playing there or stands still. // - Already on AirPlay when a film or the next episode opens: the core is asked // before the first item loads, so the TV is handed the Wi-Fi address first, // never the phone's 127.0.0.1 one it cannot reach. // - The route leaves AirPlay: ask `phone` (the Wi-Fi door shuts), back on the // phone's own address, external playback allowed again. import Foundation struct FilmRouting: Equatable { enum State: Equatable { /// Playing on the phone. case phone /// The core asked what to do for a TV; its answer not yet in. case asking /// The TV plays it, from the Wi-Fi door when `onWiFi`. case receiver(onWiFi: Bool) /// The phone plays it and its picture goes to the TV. case throughPhone } enum Act: Equatable { /// Play from this address, from where it stands. case play(URL) /// Ask `check` after these seconds. case check(after: Double) /// Play from what the phone plays it from (its own address, or what it /// keeps) again, from where it stands. case playHere /// Apple's player may (true) or may not (false) hand the film to the TV. case allowTV(Bool) /// A toast. case say(String) } private(set) var state: State = .phone /// The film's "through this phone" said this play. private var said = false /// Apple's player began playing to a TV: the route to ask, if any. mutating func began() -> String? { guard state == .phone else { return nil } state = .asking return "external" } /// The route left AirPlay: the route to ask, and what to do now. mutating func cameBack() -> (ask: String?, acts: [Act]) { defer { state = .phone } switch state { case .phone: return (nil, []) case .asking: return ("phone", []) case .receiver(let onWiFi): return ("phone", onWiFi ? [.playHere] : []) case .throughPhone: return (nil, [.allowTV(true)]) } } /// The film failed: the route to ask while the TV played it; nil otherwise. func failed() -> String? { if case .receiver = state { return "failed" } return state == .asking ? "failed" : nil } /// The time to see the film playing on the TV is up: the route to ask, if any. func checking() -> String? { if case .receiver = state { return "check" } return nil } /// A film (or the next episode) opens in the player, before its first /// item loads: the route to ask for it when the sound already goes to a /// TV. Playing through the phone, it stays so. mutating func opening(onTV: Bool) -> String? { guard state != .throughPhone else { return nil } state = .phone return onTV ? began() : nil } /// The core answered the route asked at `opening`: the address the first /// item loads from (nil, what the phone plays it from), and what else the /// player does. Nothing has loaded yet, so nothing plays again. mutating func first(_ route: PlayerRoute) -> (from: URL?, acts: [Act]) { var from: URL? var acts: [Act] = [] for act in took(route) { switch act { case .play(let url): from = url case .playHere: continue default: acts.append(act) } } return (from, acts) } /// The core answered a route: what the player does. mutating func took(_ route: PlayerRoute) -> [Act] { // Back on the phone before the answer came: nothing of it holds. if state == .phone { return [] } switch route.to { case .receiver: // Already through the phone (a failure's answer came first): it stays so. if state == .throughPhone { return [] } let check = route.checkAfterS.map { [Act.check(after: $0)] } ?? [] guard let stream = route.stream.flatMap(URL.init(string:)) else { state = .receiver(onWiFi: false) return check } let fresh = state != .receiver(onWiFi: true) state = .receiver(onWiFi: true) return (fresh ? [.play(stream)] : []) + check case .phone: state = .throughPhone var acts: [Act] = [.allowTV(false), .playHere] if let words = route.said, !said { said = true acts.append(.say(words)) } return acts } } }