| 1 | import Foundation |
| 2 | |
| 3 | public struct RosterPeer: Decodable, Identifiable { |
| 4 | public let peer: String |
| 5 | public let name: String |
| 6 | public let kind: String |
| 7 | public let gen: Int64? |
| 8 | /// The peer's own last report of its microphone and camera (spec |
| 9 | /// §15.19). Nil is unknown — a client that predates the report, or one |
| 10 | /// that has not made it yet — and is shown as neither muted nor off. |
| 11 | public let media: PeerMedia? |
| 12 | /// What their participation was opened from (spec §15.28): "mac 0.4.12", |
| 13 | /// "native" for an app from before that, nil for a browser or a server |
| 14 | /// from before it. |
| 15 | public let client: String? |
| 16 | public var id: String { peer } |
| 17 | } |
| 18 | /// What a peer says about its own outgoing media (spec §15.19), and about |
| 19 | /// the sound reaching it (§15.20). |
| 20 | public struct PeerMedia: Decodable, Equatable, Sendable { |
| 21 | public let muted: Bool |
| 22 | public let cameraOff: Bool |
| 23 | /// Its browser refused to play sound and is waiting for a click. Nil |
| 24 | /// (absent) is none; the key only travels with bad news. |
| 25 | public let audioBlocked: Bool? |
| 26 | /// The peers whose audio reaches it as silence. Nil (absent) is none. |
| 27 | public let noSoundFrom: [String]? |
| 28 | /// The peers whose shared screen has stopped moving for it (§15.21). |
| 29 | /// A sharer that finds itself here recovers its screen. Nil is none. |
| 30 | public let screenStalled: [String]? |
| 31 | /// The peers whose audio it is actually receiving and playing (§15.24). |
| 32 | /// Nil (absent) is a client that does not say; empty is one that hears |
| 33 | /// nobody yet. |
| 34 | public let hearing: [String]? |
| 35 | /// What it asks each sender for (spec §15.30): peer id to "reduced", |
| 36 | /// "minimal" or "audio", only those below normal. Nil (absent) is normal |
| 37 | /// from everyone, which is also what a client from before it means. Read |
| 38 | /// leniently: a value of any other shape is taken as absent rather than |
| 39 | /// costing the roster its decode. |
| 40 | public let video: [String: String]? |
| 41 | |
| 42 | public init( |
| 43 | muted: Bool, cameraOff: Bool, audioBlocked: Bool? = nil, noSoundFrom: [String]? = nil, |
| 44 | screenStalled: [String]? = nil, hearing: [String]? = nil, video: [String: String]? = nil |
| 45 | ) { |
| 46 | self.muted = muted |
| 47 | self.cameraOff = cameraOff |
| 48 | self.audioBlocked = audioBlocked |
| 49 | self.noSoundFrom = noSoundFrom |
| 50 | self.screenStalled = screenStalled |
| 51 | self.hearing = hearing |
| 52 | self.video = video |
| 53 | } |
| 54 | |
| 55 | private enum CodingKeys: String, CodingKey { |
| 56 | case muted, cameraOff, audioBlocked, noSoundFrom, screenStalled, hearing, video |
| 57 | } |
| 58 | |
| 59 | public init(from decoder: Decoder) throws { |
| 60 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 61 | muted = try c.decode(Bool.self, forKey: .muted) |
| 62 | cameraOff = try c.decode(Bool.self, forKey: .cameraOff) |
| 63 | audioBlocked = try c.decodeIfPresent(Bool.self, forKey: .audioBlocked) |
| 64 | noSoundFrom = try c.decodeIfPresent([String].self, forKey: .noSoundFrom) |
| 65 | screenStalled = try c.decodeIfPresent([String].self, forKey: .screenStalled) |
| 66 | hearing = try c.decodeIfPresent([String].self, forKey: .hearing) |
| 67 | video = (try? c.decodeIfPresent([String: String].self, forKey: .video)) ?? nil |
| 68 | } |
| 69 | } |
| 70 | public struct RoomState: Decodable { |
| 71 | public let phase: String |
| 72 | public let peer: String |
| 73 | public let boot: String |
| 74 | public let reason: String? |
| 75 | public let peers: [RosterPeer] |
| 76 | public let waiting: [RosterPeer] |
| 77 | } |
| 78 | struct WireEvent: Decodable { |
| 79 | let type: String |
| 80 | let state: RoomState? |
| 81 | let message: Message? |
| 82 | let ice: ICEConfig? |
| 83 | let info: MeetingInfo? |
| 84 | let signal: Message? |
| 85 | } |
| 86 | struct Message: Decodable { |
| 87 | let type: String |
| 88 | let from: String? |
| 89 | let peer: String? |
| 90 | let name: String? |
| 91 | let gen: Int64? |
| 92 | let seq: UInt64? |
| 93 | let payload: Payload? |
| 94 | } |
| 95 | struct Payload: Decodable { |
| 96 | let description: Description? |
| 97 | let candidate: Candidate? |
| 98 | let text: String? |
| 99 | /// `share`'s one field (§5.4): whether the sender is showing a screen. |
| 100 | let on: Bool? |
| 101 | } |
| 102 | struct Description: Codable { |
| 103 | let type: String |
| 104 | let sdp: String |
| 105 | } |
| 106 | struct Candidate: Codable { |
| 107 | let candidate: String |
| 108 | let sdpMid: String? |
| 109 | let sdpMLineIndex: Int32? |
| 110 | } |
| 111 | struct ICEConfig: Decodable { |
| 112 | struct Server: Decodable { |
| 113 | let urls: [String] |
| 114 | let username: String? |
| 115 | let credential: String? |
| 116 | } |
| 117 | let iceServers: [Server] |
| 118 | let renewAt: Int64 |
| 119 | let expiresAt: Int64 |
| 120 | } |
| 121 | /// §5.3 for the apps: when a fresh `/ice` answer should restart ICE on a |
| 122 | /// connection, rather than be adopted for whatever it gathers next. |
| 123 | /// |
| 124 | /// The core fetches the list on every roster (every stream resume, five |
| 125 | /// minutes apart at most) as well as at `renewAt`, so a connection hears a |
| 126 | /// fresh list at the latest five minutes after its own renewal falls due — |
| 127 | /// well inside the half hour between `renewAt` and expiry. |
| 128 | enum ICERenewal { |
| 129 | /// - Parameters: |
| 130 | /// - gathered: the list the connection was built with, or last restarted onto. |
| 131 | /// - next: the list just fetched. |
| 132 | /// - now: unix seconds. |
| 133 | static func restart(gathered: ICEConfig?, next: ICEConfig, now: Int64) -> Bool { |
| 134 | // The floor has nothing to renew onto. |
| 135 | guard !next.iceServers.isEmpty else { return false } |
| 136 | guard let gathered else { return true } |
| 137 | // Built on the floor while the first fetch was out, and a relay since: |
| 138 | // adopt it now, as the browser's adoptServers does. |
| 139 | if gathered.iceServers.isEmpty { return true } |
| 140 | return gathered.renewAt > 0 && now >= gathered.renewAt |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | func json(_ value: Any) -> String? { |
| 145 | guard JSONSerialization.isValidJSONObject(value), |
| 146 | let data = try? JSONSerialization.data(withJSONObject: value) |
| 147 | else { return nil } |
| 148 | return String(data: data, encoding: .utf8) |
| 149 | } |
| 150 | |
| 151 | struct MeetingInfo: Decodable { |
| 152 | let title: String |
| 153 | /// A title someone chose rather than one Calls wrote. Absent from a |
| 154 | /// server before 0.4.7, where it reads as false and no title is shown. |
| 155 | let named: Bool? |
| 156 | let name: String |
| 157 | let standing: String |
| 158 | let floor: Bool |
| 159 | } |
| 160 | |