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 XCTest
3
4@testable import CallsKit
5
6/// Portrait video (2026-10-21): which way up a picture is, and whether it
7/// fills its place or is shown whole.
8final class VideoShapeTests: XCTestCase {
9 private func size(_ w: CGFloat, _ h: CGFloat) -> CGSize { CGSize(width: w, height: h) }
10
11 // MARK: The classifier
12
13 func testThresholds() {
14 XCTAssertEqual(VideoShape.of(size(720, 1280)), .portrait)
15 XCTAssertEqual(VideoShape.of(size(1280, 720)), .landscape)
16 XCTAssertEqual(VideoShape.of(size(640, 480)), .landscape, "4:3 is landscape")
17 XCTAssertEqual(VideoShape.of(size(480, 640)), .portrait)
18 XCTAssertEqual(VideoShape.of(size(500, 500)), .square)
19 // h > w × 1.1 is portrait; exactly 1.1 is not yet.
20 XCTAssertEqual(VideoShape.of(size(1000, 1100)), .square)
21 XCTAssertEqual(VideoShape.of(size(1000, 1101)), .portrait)
22 XCTAssertEqual(VideoShape.of(size(1101, 1000)), .landscape)
23 XCTAssertEqual(VideoShape.of(size(0, 0)), .landscape, "nothing known is today's landscape")
24 }
25
26 func testRotationIsApplied() {
27 // An iPhone held upright sends its 1280 × 720 buffer turned 90°.
28 XCTAssertEqual(VideoShape.displayed(width: 1280, height: 720, rotation: 90), size(720, 1280))
29 XCTAssertEqual(VideoShape.displayed(width: 1280, height: 720, rotation: 270), size(720, 1280))
30 XCTAssertEqual(VideoShape.displayed(width: 1280, height: 720, rotation: 180), size(1280, 720))
31 XCTAssertEqual(VideoShape.displayed(width: 1280, height: 720, rotation: 0), size(1280, 720))
32 XCTAssertEqual(VideoShape.of(VideoShape.displayed(width: 1280, height: 720, rotation: 90)), .portrait)
33 }
34
35 func testHysteresisHoldsAShapeThroughTheBand() {
36 var reading = VideoShapeReading()
37 XCTAssertEqual(reading.read(size(720, 1280)), .portrait)
38 // Still taller than wide, inside the band: stays portrait.
39 XCTAssertEqual(reading.read(size(1000, 1050)), .portrait)
40 XCTAssertEqual(reading.read(size(1000, 1001)), .portrait)
41 // No taller than wide: let go, to what it now is.
42 XCTAssertEqual(reading.read(size(1000, 1000)), .square)
43 // Back up into the band is not enough to be portrait again.
44 XCTAssertEqual(reading.read(size(1000, 1080)), .square)
45 XCTAssertEqual(reading.read(size(1000, 1200)), .portrait)
46 }
47
48 func testNearSquareDoesNotFlap() {
49 // A picture jittering about 1.1 from square never changes once.
50 var reading = VideoShapeReading()
51 var changes = 0
52 var last = reading.read(size(1000, 1000))
53 for step in 0..<40 {
54 let h: CGFloat = step.isMultiple(of: 2) ? 1095 : 1105
55 let now = reading.read(size(1000, h))
56 if now != last { changes += 1 }
57 last = now
58 }
59 XCTAssertEqual(changes, 1, "into portrait once, then held there")
60 }
61
62 func testARotationMidCallSwitchesOnce() {
63 // A phone turned: landscape, the steps a resolution ladder may report
64 // on the way, then upright, with no flapping between.
65 var reading = VideoShapeReading()
66 let sizes = [
67 size(1280, 720), size(960, 540), size(1280, 720), // landscape, adapting
68 size(720, 1280), size(540, 960), size(720, 1280), // turned upright
69 ]
70 var shapes: [VideoShape] = []
71 for s in sizes {
72 let shape = reading.read(s)
73 if shapes.last != shape { shapes.append(shape) }
74 }
75 XCTAssertEqual(shapes, [.landscape, .portrait])
76 }
77
78 // MARK: Fill or fit
79
80 func testCrop() {
81 XCTAssertEqual(VideoPlacement.crop(video: size(1280, 720), in: size(1600, 900)), 0, accuracy: 1e-6)
82 // 16:9 in 16:10 cuts a tenth of its width.
83 XCTAssertEqual(VideoPlacement.crop(video: size(16, 9), in: size(16, 10)), 0.1, accuracy: 1e-6)
84 // 9:16 in 16:10: all but 9/16 × 10/16 of its height goes.
85 XCTAssertEqual(
86 VideoPlacement.crop(video: size(9, 16), in: size(16, 10)), 1 - (9.0 / 16) * (10.0 / 16), accuracy: 1e-6)
87 }
88
89 /// The decision table in VideoPlacement's comment, row by row.
90 func testDecisionTable() {
91 let mac = size(1440, 900) // 16:10
92 let phone = size(393, 852) // an upright iPhone
93 let tile = size(320, 180) // a grid or filmstrip tile, 16:9
94 let phoneTile = size(99, 132) // the iPhone's filmstrip, 3:4
95 func fits(_ v: CGSize, _ b: CGSize, screen: Bool = false) -> Bool {
96 var reading = VideoShapeReading()
97 return VideoPlacement.fits(video: v, shape: reading.read(v), in: b, screen: screen)
98 }
99 // A screen is always fitted.
100 XCTAssertTrue(fits(size(1920, 1200), mac, screen: true))
101 XCTAssertTrue(fits(size(1440, 900), mac, screen: true))
102 // The same shape fills, however much it crops.
103 XCTAssertFalse(fits(size(1280, 720), mac))
104 XCTAssertFalse(fits(size(720, 1280), phone))
105 XCTAssertFalse(fits(size(2560, 1080), tile), "21:9 in 16:9 crops 25% and still fills")
106 // Another shape with a small crop fills.
107 XCTAssertFalse(fits(size(1000, 1000), size(1100, 1000)), "square in a near-square place")
108 XCTAssertFalse(fits(size(1050, 1000), size(1000, 1080)))
109 // Another shape with a big crop is fitted.
110 XCTAssertTrue(fits(size(720, 1280), mac), "Farshad's iPhone on a Mac window")
111 XCTAssertTrue(fits(size(720, 1280), tile), "and in a grid or filmstrip tile")
112 XCTAssertTrue(fits(size(1280, 720), phone), "a Mac on an upright phone")
113 XCTAssertTrue(fits(size(1280, 720), phoneTile), "and in the phone's 3:4 filmstrip")
114 XCTAssertTrue(fits(size(720, 720), mac), "a square picture in a 16:10 window")
115 // The 15% line: 4:3 into 16:9 (25%) is the same shape and fills.
116 XCTAssertFalse(fits(size(640, 480), tile))
117 // A portrait 3:4 into a square-ish 1.08:1 place cuts 31%: fitted.
118 XCTAssertTrue(fits(size(480, 640), size(1080, 1000)))
119 // Nothing known yet: fill, as 0.4.10.
120 XCTAssertFalse(VideoPlacement.fits(video: .zero, shape: .portrait, in: mac, screen: false))
121 }
122
123 func testPlacedPictureFitsCentredAndFillsWithHeadroom() {
124 var picture = PlacedPicture()
125 let mac = size(1600, 1000)
126 // Before the first frame: 16:9, filling.
127 XCTAssertFalse(picture.place(in: mac).fitted)
128 picture.update(video: size(720, 1280))
129 let placed = picture.place(in: mac)
130 XCTAssertTrue(placed.fitted)
131 XCTAssertEqual(placed.frame.height, 1000, accuracy: 1e-6, "the whole height shown")
132 XCTAssertEqual(placed.frame.width, 562.5, accuracy: 1e-6)
133 XCTAssertEqual(placed.frame.midX, 800, accuracy: 1e-6, "centred")
134 // Turned back to landscape: fills again, cropped with headroom.
135 picture.update(video: size(1280, 720))
136 let filled = picture.place(in: mac)
137 XCTAssertFalse(filled.fitted)
138 XCTAssertGreaterThanOrEqual(filled.frame.width, 1600)
139 XCTAssertGreaterThanOrEqual(filled.frame.height, 1000 - 1e-6)
140 }
141
142 // MARK: The self view
143
144 func testAnUprightSelfFloatsUpright() {
145 let mac = size(1200, 800)
146 let landscape = PiPLayout.size(in: mac, touch: false)
147 XCTAssertGreaterThan(landscape.width, landscape.height)
148 let upright = PiPLayout.size(in: mac, touch: false, shape: .portrait)
149 XCTAssertGreaterThan(upright.height, upright.width)
150 XCTAssertEqual(upright.width / upright.height, 9.0 / 16, accuracy: 0.02)
151 // A phone: its own picture's shape wins over the screen's.
152 let phone = size(393, 852)
153 XCTAssertGreaterThan(PiPLayout.size(in: phone, touch: true).height, PiPLayout.size(in: phone, touch: true).width)
154 let turned = PiPLayout.size(in: phone, touch: true, shape: .landscape)
155 XCTAssertGreaterThan(turned.width, turned.height)
156 }
157
158 // MARK: The backdrop's picture
159
160 func testBackdropPictureTakesThePicturesShape() {
161 XCTAssertTrue(BackdropSampler.size(for: size(1280, 720)) == (64, 36))
162 XCTAssertTrue(BackdropSampler.size(for: size(720, 1280)) == (36, 64))
163 XCTAssertTrue(BackdropSampler.size(for: size(500, 500)) == (48, 48))
164 }
165
166 func testBackdropPictureIsTurnedUpright() {
167 // 3 × 2, each pixel's red its index: turned 90° clockwise it is 2 × 3
168 // with the bottom-left pixel first.
169 let w = 3
170 let h = 2
171 var rgba = [UInt8](repeating: 255, count: w * h * 4)
172 for i in 0..<(w * h) { rgba[i * 4] = UInt8(i) }
173 let turned = BackdropSampler.rotate(rgba, width: w, height: h, rotation: 90)
174 XCTAssertEqual(turned.width, 2)
175 XCTAssertEqual(turned.height, 3)
176 XCTAssertEqual(stride(from: 0, to: 6, by: 1).map { turned.pixels[$0 * 4] }, [3, 0, 4, 1, 5, 2])
177 let back = BackdropSampler.rotate(turned.pixels, width: 2, height: 3, rotation: 270)
178 XCTAssertEqual(back.pixels, rgba)
179 let half = BackdropSampler.rotate(rgba, width: w, height: h, rotation: 180)
180 XCTAssertEqual(stride(from: 0, to: 6, by: 1).map { half.pixels[$0 * 4] }, [5, 4, 3, 2, 1, 0])
181 }
182
183 /// The cost of several fitted tiles' backdrops: each takes a real
184 /// 1280 × 720 frame through WebRTC's scale, the conversion, the blur and
185 /// the CGImage, twice a second. Printed for the record; held well under
186 /// a frame's time for the whole budget at once.
187 func testSeveralBackdropsAreCheap() {
188 let source = RTCI420Buffer(width: 1280, height: 720)
189 let upright = RTCVideoFrame(buffer: source, rotation: ._90, timeStampNs: 0)
190 let level = RTCVideoFrame(buffer: source, rotation: ._0, timeStampNs: 0)
191 _ = BackdropSampler.picture(of: upright)
192 let runs = 20
193 let start = Date()
194 for _ in 0..<runs {
195 for tile in 0..<BackdropBudget.limit {
196 XCTAssertNotNil(BackdropSampler.picture(of: tile.isMultiple(of: 2) ? upright : level))
197 }
198 }
199 let each = Date().timeIntervalSince(start) / Double(runs) * 1000
200 print(
201 "backdrop: \(String(format: "%.3f", each)) ms for \(BackdropBudget.limit) tiles' pictures, "
202 + "twice a second (\(String(format: "%.3f", each * 2 / 10)) % of one core)")
203 XCTAssertLessThan(each, 16)
204 }
205}
206