oficina / meet Public

Clone
git clone https://amadan.net/oficina/meet

Plain git — no account needed to clone.

Download

Download this file

1import CoreGraphics
2import Foundation
3
4/// Which way up a picture is, as it is displayed: rotation applied
5/// (2026-10-21, Paul, after Farshad joined from an iPhone held upright:
6/// "Can we detect vertical video and display it appropriately?"). Since
7/// 0.4.10 a camera on the stage fills the window, which cut a 9:16 face
8/// to a thin slice of a 16:10 Mac window.
9public enum VideoShape: String, Sendable, Equatable {
10 case portrait, landscape, square
11
12 /// How much longer one side must be than the other for a picture to
13 /// count as portrait or landscape: h > w × 1.1 is portrait.
14 public static let enter: CGFloat = 1.1
15 /// How far it must come back before it stops counting as that: a
16 /// portrait picture stays portrait until it is no taller than it is
17 /// wide. The band between this and `enter` is what keeps a picture near
18 /// square, or a resolution step on the way, from flapping.
19 public static let hold: CGFloat = 1.0
20
21 /// A size's shape with no history: what a window or a tile is.
22 public static func of(_ size: CGSize) -> VideoShape {
23 guard size.width > 0, size.height > 0 else { return .landscape }
24 if size.height > size.width * enter { return .portrait }
25 if size.width > size.height * enter { return .landscape }
26 return .square
27 }
28
29 /// A frame's size as it is shown: WebRTC carries a phone's camera
30 /// sideways with a rotation to apply (90 or 270 for one held upright),
31 /// so its buffer's width is the picture's height.
32 public static func displayed(width: CGFloat, height: CGFloat, rotation: Int) -> CGSize {
33 let quarter = ((rotation % 360) + 360) % 180 == 90
34 return quarter ? CGSize(width: height, height: width) : CGSize(width: width, height: height)
35 }
36}
37
38/// A picture's shape over time, with hysteresis: a phone turned mid-call
39/// switches once, at the turn, and a picture that sits near square or
40/// passes through it on a resolution change does not flap.
41public struct VideoShapeReading: Sendable, Equatable {
42 public private(set) var shape: VideoShape?
43
44 public init() {}
45
46 /// Takes the picture's newest displayed size and says its shape.
47 @discardableResult public mutating func read(_ size: CGSize) -> VideoShape {
48 guard size.width > 0, size.height > 0 else { return shape ?? .landscape }
49 let fresh = VideoShape.of(size)
50 switch shape {
51 case .portrait? where size.height > size.width * VideoShape.hold:
52 break // Still taller than wide: stays portrait.
53 case .landscape? where size.width > size.height * VideoShape.hold:
54 break // Still wider than tall: stays landscape.
55 default:
56 shape = fresh
57 }
58 return shape ?? fresh
59 }
60}
61
62/// Fill or fit: whether a picture is cropped to fill its tile or window
63/// (0.4.10's way for every camera), or shown whole, centred, with the same
64/// picture soft and dimmed around it (`ScreenBackdrop`).
65///
66/// | picture | its place | crop to fill | shown |
67/// |--------------------|----------------------------|--------------|--------|
68/// | a shared screen | anything | any | fit |
69/// | a camera | the same shape | any | fill |
70/// | a camera | another shape | ≤ 15% | fill |
71/// | a camera | another shape | > 15% | fit |
72///
73/// "The same shape" is `VideoShape`: a portrait phone on a landscape Mac
74/// window, a landscape Mac on an upright phone, and a square-ish picture
75/// in a 16:9 tile are each another shape. A small crop is never worth the
76/// bars, so a 4:3 camera in a 16:9 tile, or a 16:9 one in a 16:10 window,
77/// fills as it always did. The web's room.js decides the same way
78/// (`videoFits`).
79public enum VideoPlacement {
80 /// The most of a dimension filling may cut before a mismatched picture
81 /// is fitted instead.
82 public static let tolerance: CGFloat = 0.15
83
84 /// How much of its overflowing dimension a picture loses to fill
85 /// `bounds`: 0 for the same aspect, 0.4375 for 16:9 in a 9:16 tile.
86 public static func crop(video: CGSize, in bounds: CGSize) -> CGFloat {
87 guard video.width > 0, video.height > 0, bounds.width > 0, bounds.height > 0 else { return 0 }
88 let across = bounds.width / video.width
89 let down = bounds.height / video.height
90 return 1 - min(across, down) / max(across, down)
91 }
92
93 /// Whether the picture is fitted whole. `shape` is the picture's shape
94 /// as a `VideoShapeReading` holds it, so a rotation decides once.
95 public static func fits(video: CGSize, shape: VideoShape, in bounds: CGSize, screen: Bool) -> Bool {
96 if screen { return true }
97 guard video.width > 0, video.height > 0, bounds.width > 0, bounds.height > 0 else {
98 return false
99 }
100 if crop(video: video, in: bounds) <= tolerance { return false }
101 return shape != VideoShape.of(bounds)
102 }
103}
104
105/// One renderer's picture and where it goes: what the Mac's and the
106/// iPhone's video views keep, told each new size the renderer reports
107/// and asked where to put it at every layout.
108public struct PlacedPicture: Sendable, Equatable {
109 /// A shared screen: always fitted.
110 public var screen = false
111 /// The picture's size as displayed, rotation applied; 16:9 until the
112 /// first frame says otherwise, which fills as 0.4.10 did.
113 public private(set) var video = CGSize(width: 16, height: 9)
114 public private(set) var reading = VideoShapeReading()
115
116 public init(screen: Bool = false) { self.screen = screen }
117
118 public var shape: VideoShape { reading.shape ?? VideoShape.of(video) }
119
120 public mutating func update(video size: CGSize) {
121 guard size.width > 0, size.height > 0 else { return }
122 video = size
123 reading.read(size)
124 }
125
126 /// Whether the picture is fitted in `bounds`, and its frame there.
127 public func place(in bounds: CGSize) -> (frame: CGRect, fitted: Bool) {
128 let fitted = VideoPlacement.fits(video: video, shape: shape, in: bounds, screen: screen)
129 return (VideoFrame.place(video: video, in: bounds, fit: fitted), fitted)
130 }
131}
132