// Cotton Calling's engine, made once per process with its live ports: the // call center, CallKit, PushKit and WebRTC. // // The PushKit registry is made at launch, before any view (`attach`), so a // VoIP push that launched the app in the background finds its handler. The // core is opened by whoever asks first (`CottonApp.open()`), and the center // asks for it through the opener it was attached with. #if os(iOS) import CottonKit import Foundation import UIKit @MainActor public final class CallingEngine { public static let shared = CallingEngine() public let center: CallCenter public let peer: WebRTCPeer private let system: CallKitSystem private let opener = CoreOpener() private var registry: VoIPRegistry? private var ringFrames: NSObjectProtocol? private init() { let log: @Sendable (String) -> Void = { Log.write($0) } let system = CallKitSystem(icon: UIImage(named: "CallKitIcon")?.pngData(), log: log) let peer = WebRTCPeer(log: log) let opener = self.opener #if targetEnvironment(simulator) // CallKit cannot hold an outgoing call on a simulator (SimulatorCallSystem). let shown = SimulatorCallSystem(log: log) #else let shown = system #endif center = CallCenter( ports: .init(system: shown, socket: WebSocketCallSocket(log: log), peer: peer, audio: CallAudioSession(), tone: RingbackTone(), clock: MainClock()), core: { await opener.core() }, log: log ) system.center = center #if targetEnvironment(simulator) shown.center = center #endif self.system = system self.peer = peer } /// Start listening for VoIP pushes, the core opened through `open` when /// one is first needed. Once, at launch. public func attach(_ open: @escaping @Sendable () async -> (any CottonCalling)?) { guard registry == nil else { return } Task { await opener.set(open) } registry = VoIPRegistry(handler: VoIPPushHandler(reporter: system, center: center), center: center) // A ring over the open chat socket (the Worker's way to ring a phone that has Comms open, in place of a push): // the core's ring reports it to the system, as for any ring not pushed (`CallRingFrame`). let center = center ringFrames = NotificationCenter.default.addObserver(forName: CallRingFrame.arrived, object: nil, queue: .main) { note in guard let ring = note.object as? CallRingFrame else { return } MainActor.assumeIsolated { switch ring { case .ring(let room, let video, let box, let at): center.incoming(room: room, video: video, ring: box, privateKey: box == nil ? nil : ChatKeychain().key(), at: at) case .stop(let room, let reason, let box, let times), .missed(let room, let reason, let box, let times): // Over the socket: nothing was reported to CallKit, so a stop for a ring never heard rings // nothing; on a missed call it writes the row, at the ring's own times (CALLS FIX 2). center.stop(room: room, reason: reason, ring: box, privateKey: box == nil ? nil : ChatKeychain().key(), times: times) } } } } } /// The core, opened on first ask and kept. private actor CoreOpener { private var open: (@Sendable () async -> (any CottonCalling)?)? private var waiting: [CheckedContinuation] = [] private var opened: CoreCalls? func set(_ open: @escaping @Sendable () async -> (any CottonCalling)?) { self.open = open waiting.forEach { $0.resume() } waiting.removeAll() } func core() async -> (any CallsCore)? { if let opened { return opened } if open == nil { await withCheckedContinuation { waiting.append($0) } } guard let core = await open?() else { return nil } let calls = CoreCalls(core: core) opened = calls return calls } } #endif