import CoreGraphics import Foundation /// Which way up a picture is, as it is displayed: rotation applied /// (2026-10-21, Paul, after Farshad joined from an iPhone held upright: /// "Can we detect vertical video and display it appropriately?"). Since /// 0.4.10 a camera on the stage fills the window, which cut a 9:16 face /// to a thin slice of a 16:10 Mac window. public enum VideoShape: String, Sendable, Equatable { case portrait, landscape, square /// How much longer one side must be than the other for a picture to /// count as portrait or landscape: h > w × 1.1 is portrait. public static let enter: CGFloat = 1.1 /// How far it must come back before it stops counting as that: a /// portrait picture stays portrait until it is no taller than it is /// wide. The band between this and `enter` is what keeps a picture near /// square, or a resolution step on the way, from flapping. public static let hold: CGFloat = 1.0 /// A size's shape with no history: what a window or a tile is. public static func of(_ size: CGSize) -> VideoShape { guard size.width > 0, size.height > 0 else { return .landscape } if size.height > size.width * enter { return .portrait } if size.width > size.height * enter { return .landscape } return .square } /// A frame's size as it is shown: WebRTC carries a phone's camera /// sideways with a rotation to apply (90 or 270 for one held upright), /// so its buffer's width is the picture's height. public static func displayed(width: CGFloat, height: CGFloat, rotation: Int) -> CGSize { let quarter = ((rotation % 360) + 360) % 180 == 90 return quarter ? CGSize(width: height, height: width) : CGSize(width: width, height: height) } } /// A picture's shape over time, with hysteresis: a phone turned mid-call /// switches once, at the turn, and a picture that sits near square or /// passes through it on a resolution change does not flap. public struct VideoShapeReading: Sendable, Equatable { public private(set) var shape: VideoShape? public init() {} /// Takes the picture's newest displayed size and says its shape. @discardableResult public mutating func read(_ size: CGSize) -> VideoShape { guard size.width > 0, size.height > 0 else { return shape ?? .landscape } let fresh = VideoShape.of(size) switch shape { case .portrait? where size.height > size.width * VideoShape.hold: break // Still taller than wide: stays portrait. case .landscape? where size.width > size.height * VideoShape.hold: break // Still wider than tall: stays landscape. default: shape = fresh } return shape ?? fresh } } /// Fill or fit: whether a picture is cropped to fill its tile or window /// (0.4.10's way for every camera), or shown whole, centred, with the same /// picture soft and dimmed around it (`ScreenBackdrop`). /// /// | picture | its place | crop to fill | shown | /// |--------------------|----------------------------|--------------|--------| /// | a shared screen | anything | any | fit | /// | a camera | the same shape | any | fill | /// | a camera | another shape | ≤ 15% | fill | /// | a camera | another shape | > 15% | fit | /// /// "The same shape" is `VideoShape`: a portrait phone on a landscape Mac /// window, a landscape Mac on an upright phone, and a square-ish picture /// in a 16:9 tile are each another shape. A small crop is never worth the /// bars, so a 4:3 camera in a 16:9 tile, or a 16:9 one in a 16:10 window, /// fills as it always did. The web's room.js decides the same way /// (`videoFits`). public enum VideoPlacement { /// The most of a dimension filling may cut before a mismatched picture /// is fitted instead. public static let tolerance: CGFloat = 0.15 /// How much of its overflowing dimension a picture loses to fill /// `bounds`: 0 for the same aspect, 0.4375 for 16:9 in a 9:16 tile. public static func crop(video: CGSize, in bounds: CGSize) -> CGFloat { guard video.width > 0, video.height > 0, bounds.width > 0, bounds.height > 0 else { return 0 } let across = bounds.width / video.width let down = bounds.height / video.height return 1 - min(across, down) / max(across, down) } /// Whether the picture is fitted whole. `shape` is the picture's shape /// as a `VideoShapeReading` holds it, so a rotation decides once. public static func fits(video: CGSize, shape: VideoShape, in bounds: CGSize, screen: Bool) -> Bool { if screen { return true } guard video.width > 0, video.height > 0, bounds.width > 0, bounds.height > 0 else { return false } if crop(video: video, in: bounds) <= tolerance { return false } return shape != VideoShape.of(bounds) } } /// One renderer's picture and where it goes: what the Mac's and the /// iPhone's video views keep, told each new size the renderer reports /// and asked where to put it at every layout. public struct PlacedPicture: Sendable, Equatable { /// A shared screen: always fitted. public var screen = false /// The picture's size as displayed, rotation applied; 16:9 until the /// first frame says otherwise, which fills as 0.4.10 did. public private(set) var video = CGSize(width: 16, height: 9) public private(set) var reading = VideoShapeReading() public init(screen: Bool = false) { self.screen = screen } public var shape: VideoShape { reading.shape ?? VideoShape.of(video) } public mutating func update(video size: CGSize) { guard size.width > 0, size.height > 0 else { return } video = size reading.read(size) } /// Whether the picture is fitted in `bounds`, and its frame there. public func place(in bounds: CGSize) -> (frame: CGRect, fitted: Bool) { let fitted = VideoPlacement.fits(video: video, shape: shape, in: bounds, screen: screen) return (VideoFrame.place(video: video, in: bounds, fit: fitted), fitted) } }