Calls: portrait video gates — web scenario and unit tests, iPhone snapshots, eased turns only in place
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
9 files changed,
+347
−7
Makefile+1 −0internal/calls/static/calls.css+7 −5internal/callstest/browser_test.go+1 −0native/apple/Sources/CallsKit/MacVideoTile.swift+5 −1native/ios/Sources/CallsPhone.swift+5 −1native/ios/Sources/PhoneCallFixture.swift+6 −0native/ios/UITests/CallsPhoneUITests.swift+42 −0test/browser/call.mjs+165 −0test/browser/room-portrait.test.mjs+115 −0
diff --git a/Makefile b/Makefile| index 662fb97..bf998f0 100644 |
| --- a/Makefile |
| +++ b/Makefile |
| @@ -159,6 +159,7 @@ browser: |
| node --test test/browser/room-lane.test.mjs |
| node --test test/browser/room-quality.test.mjs |
| node --test test/browser/room-wake.test.mjs |
| + node --test test/browser/room-portrait.test.mjs |
| node --test test/browser/media.test.mjs |
| node --test test/browser/audio.test.mjs |
| node --test test/browser/devices.test.mjs |
diff --git a/internal/calls/static/calls.css b/internal/calls/static/calls.css| index d102ff9..a22fa4f 100644 |
| --- a/internal/calls/static/calls.css |
| +++ b/internal/calls/static/calls.css |
| @@ -790,11 +790,13 @@ body:has(> .calls-window) { background: var(--rst-bg); } |
| .calls-window:has(.calls-chat-overlay:not([hidden])) #tiles:not([data-corner='top-left'], [data-corner='bottom-left']) > .calls-tile-pip { |
| inset-inline-end: calc(12px + min(320px, 100cqw - 24px) + 12px); |
| } |
| -/* An upright camera (a phone held upright, yours) floats upright: 9:16 |
| - at the height a landscape self view would have times 1.6, rather than |
| - cropped to a landscape sliver (2026-10-21; PiPLayout in the apps). */ |
| -.calls-window #tiles > .calls-tile-pip[data-shape='portrait'] { |
| - inline-size: clamp(90px, 10.125cqw, 132px); |
| +/* An upright camera (a phone held upright, yours) floats upright: 9:16, |
| + 1.6 times as tall as a landscape self view (the filmstrip's height), |
| + rather than cropped to a landscape sliver (2026-10-21; PiPLayout in |
| + the apps). */ |
| +.calls-window #tiles[data-layout] > .calls-tile-pip[data-shape='portrait'] { |
| + block-size: calc(var(--calls-strip-h, 120px) * 1.6); |
| + inline-size: auto; |
| aspect-ratio: 9 / 16; |
| } |
| .calls-window #tiles > .calls-tile-pip.calls-tile-dragging { transition: none; z-index: 4; cursor: grabbing; } |
diff --git a/internal/callstest/browser_test.go b/internal/callstest/browser_test.go| index 8851e48..55d089c 100644 |
| --- a/internal/callstest/browser_test.go |
| +++ b/internal/callstest/browser_test.go |
| @@ -86,6 +86,7 @@ var scenarios = []string{ |
| "layout", |
| "glyphs", |
| "stage", |
| + "portrait", |
| "window", |
| "title", |
| "chrome", |
diff --git a/native/apple/Sources/CallsKit/MacVideoTile.swift b/native/apple/Sources/CallsKit/MacVideoTile.swift| index 0e0efbf..5880f6d 100644 |
| --- a/native/apple/Sources/CallsKit/MacVideoTile.swift |
| +++ b/native/apple/Sources/CallsKit/MacVideoTile.swift |
| @@ -148,6 +148,7 @@ |
| private var picture = PlacedPicture() |
| private(set) var fitted = false |
| private var placedOnce = false |
| + private var placedIn = CGSize.zero |
| private var lastShape: VideoShape? |
| /// How many times the picture has turned between filling and fitted, |
| /// and changed shape, for the gate: a phone turned once is once each. |
| @@ -222,7 +223,10 @@ |
| let frame = NSRect( |
| x: placed.frame.minX, y: bounds.height - placed.frame.maxY, width: placed.frame.width, |
| height: placed.frame.height) |
| - let turned = placedOnce && placed.fitted != fitted |
| + // Eased only when the picture turned in a place that kept its size: |
| + // a window being resized follows the pointer instead. |
| + let turned = placedOnce && placed.fitted != fitted && bounds.size == placedIn |
| + placedIn = bounds.size |
| placedOnce = true |
| if turned, !NSWorkspace.shared.accessibilityDisplayShouldReduceMotion { |
| // A phone turned mid-call: the picture eases to its new place |
diff --git a/native/ios/Sources/CallsPhone.swift b/native/ios/Sources/CallsPhone.swift| index b14cb8f..8765504 100644 |
| --- a/native/ios/Sources/CallsPhone.swift |
| +++ b/native/ios/Sources/CallsPhone.swift |
| @@ -551,6 +551,7 @@ final class PlacedVideoView: UIView, RTCVideoViewDelegate { |
| private var picture = PlacedPicture() |
| private var fitted = false |
| private var placedOnce = false |
| + private var placedIn = CGSize.zero |
| private var lastShape: VideoShape? |
| var fittedChanged: (Bool) -> Void = { _ in } |
| var shapeChanged: ((VideoShape) -> Void)? |
| @@ -583,7 +584,10 @@ final class PlacedVideoView: UIView, RTCVideoViewDelegate { |
| override func layoutSubviews() { |
| super.layoutSubviews() |
| let placed = picture.place(in: bounds.size) |
| - let turned = placedOnce && placed.fitted != fitted |
| + // Eased only when the picture turned in a place that kept its size: |
| + // a rotation of the phone itself lays out at its own pace. |
| + let turned = placedOnce && placed.fitted != fitted && bounds.size == placedIn |
| + placedIn = bounds.size |
| placedOnce = true |
| if turned, !UIAccessibility.isReduceMotionEnabled { |
| // A phone turned mid-call: the picture eases to its new place. |
diff --git a/native/ios/Sources/PhoneCallFixture.swift b/native/ios/Sources/PhoneCallFixture.swift| index f3e4284..6937fce 100644 |
| --- a/native/ios/Sources/PhoneCallFixture.swift |
| +++ b/native/ios/Sources/PhoneCallFixture.swift |
| @@ -21,6 +21,10 @@ |
| /// li,tom,me,ana`. `-CallsFixtureRimGallery silent|low|mid|high` shows, |
| /// in place of any call, the warm rim on a name at that level over the |
| /// pictures it has to read on (`WarmRimGallery`, 0.4.3). |
| + /// `-CallsFixtureFeeds ana=720x1280,li=1280x720` draws each peer's |
| + /// picture at that size on every tile it has (portrait video, |
| + /// 2026-10-21), and `-CallsFixtureFillEveryCamera YES` places them as |
| + /// 0.4.10 did, every camera filling, for the before pictures. |
| struct PhoneCallFixture: View { |
| static var requested: CallFixture? { |
| UserDefaults.standard.string(forKey: "CallsCallFixture").flatMap(CallFixture.init(rawValue:)) |
| @@ -50,11 +54,13 @@ |
| .environment( |
| \.fixtureStagePicture, |
| defaults.string(forKey: "CallsFixtureStagePicture").flatMap(FixtureBackdrop.init(rawValue:))) |
| + .environment(\.fixtureFeeds, FixtureFeeds.parse(defaults.string(forKey: "CallsFixtureFeeds") ?? "")) |
| .environment(\.callOverlayGlass, !defaults.bool(forKey: "CallsFixtureNoGlass")) |
| .preferredColorScheme(defaults.bool(forKey: "CallsFixtureDark") ? .dark : .light) |
| .onAppear { |
| guard !applied else { return } |
| applied = true |
| + FixtureVideo.fillsEveryCamera = defaults.bool(forKey: "CallsFixtureFillEveryCamera") |
| fixture.apply(to: call) |
| UserDefaults.standard.string(forKey: "CallsFixtureSelf") |
| .flatMap(FixtureSelf.init(rawValue:))?.apply(to: call) |
diff --git a/native/ios/UITests/CallsPhoneUITests.swift b/native/ios/UITests/CallsPhoneUITests.swift| index 7ef1c47..9efd708 100644 |
| --- a/native/ios/UITests/CallsPhoneUITests.swift |
| +++ b/native/ios/UITests/CallsPhoneUITests.swift |
| @@ -490,6 +490,48 @@ final class CallsPhoneUITests: XCTestCase { |
| } |
| } |
| + /// Portrait video (2026-10-21): an upright phone viewing a laptop's |
| + /// landscape camera (Li) and another phone's upright one (Ana), on the |
| + /// stage, in the filmstrip and in Grid; before (0.4.10's placement, |
| + /// every camera filling) and after (a camera of another shape than its |
| + /// tile shown whole, its picture soft behind it). |
| + @MainActor func testPortraitVideoSnapshots() throws { |
| + XCUIDevice.shared.orientation = .portrait |
| + let feeds = ["-CallsFixtureFeeds", "ana=720x1280,li=1280x720,me=720x1280"] |
| + let cases: [(String, [String])] = [ |
| + ("stage-landscape", ["-CallsFixtureLayout", "speaker", "-CallsFixtureSpeaker", "li"]), |
| + ("stage-portrait", ["-CallsFixtureLayout", "speaker", "-CallsFixtureSpeaker", "ana"]), |
| + ("grid", ["-CallsFixtureLayout", "grid"]), |
| + ] |
| + for before in [true, false] { |
| + for (name, extra) in cases { |
| + let app = launchCall( |
| + "grid", |
| + extra + feeds + ["-CallsFixtureControls", "hidden", "-CallsFixtureStagePicture", "busy", |
| + "-CallsFixtureFillEveryCamera", before ? "YES" : "NO"], |
| + dark: true) |
| + XCTAssertTrue(app.descendants(matching: .any)["callStage"].firstMatch.waitForExistence(timeout: 15)) |
| + Thread.sleep(forTimeInterval: 1) |
| + try snapshot(app, "portrait-video-\(name)-\(before ? "before" : "after")") |
| + } |
| + } |
| + // A call of two, Ana on a laptop filling the upright phone's stage before |
| + // and fitted after, and your own upright camera floating upright. |
| + for before in [true, false] { |
| + let app = launchCall( |
| + "focus", |
| + ["-CallsFixtureFeeds", "ana=1280x720,me=720x1280", "-CallsFixtureControls", "hidden", |
| + "-CallsFixtureFillEveryCamera", before ? "YES" : "NO"], |
| + dark: true) |
| + XCTAssertTrue(app.descendants(matching: .any)["callStage"].firstMatch.waitForExistence(timeout: 15)) |
| + Thread.sleep(forTimeInterval: 1) |
| + let selfView = app.descendants(matching: .any)["selfView"] |
| + XCTAssertTrue(selfView.exists) |
| + XCTAssertGreaterThan(selfView.frame.height, selfView.frame.width, "an upright self view floats upright") |
| + try snapshot(app, "portrait-video-two-landscape-\(before ? "before" : "after")") |
| + } |
| + } |
| + |
| /// Landscape keeps both bar buttons on screen, clear of the Dynamic |
| /// Island's side. |
| @MainActor func testLandscapeKeepsTheBarOnScreen() throws { |
diff --git a/test/browser/call.mjs b/test/browser/call.mjs| index 05a98ac..ad1d940 100644 |
| --- a/test/browser/call.mjs |
| +++ b/test/browser/call.mjs |
| @@ -32,6 +32,7 @@ |
| // to nobody; and --auto-accept-this-tab-capture answers getDisplayMedia |
| // with no picker, which the fake-ui flag does not cover (§5.4). |
| import { syntheticMedia } from './synthetic-media.mjs'; |
| +import { portraitCamera } from './portrait-camera.mjs'; |
| import { writeSync, mkdirSync, writeFileSync } from 'node:fs'; |
| import { inflateSync } from 'node:zlib'; |
| import { createInterface } from 'node:readline'; |
| @@ -5330,6 +5331,170 @@ const run = { |
| await ctxB.close(); |
| }, |
| + // Portrait video (2026-10-21, Paul, after Farshad joined from an |
| + // iPhone held upright: "Can we detect vertical video and display it |
| + // appropriately?"). B's camera is upright (portrait-camera.mjs, 720 × |
| + // 1280, a red band across its top and a green one across its bottom). |
| + // On A's landscape stage it is fitted whole, both bands showing, with |
| + // its backdrop painted; in Grid and in the filmstrip, tiles keep their |
| + // 16:9 shape and B's is fitted in them; on an upright phone it fills; |
| + // B's own self view floats upright; a landscape camera on an upright |
| + // phone is fitted; and B turning the phone on its side mid-call makes |
| + // A's stage fill again, its shape and placement each changing once. |
| + // CALLS_PORTRAIT_SNAPSHOTS=<dir> keeps pictures, before (0.4.10's |
| + // fill, forced) and after. |
| + async portrait() { |
| + const out = process.env.CALLS_PORTRAIT_SNAPSHOTS; |
| + if (out) mkdirSync(out, { recursive: true }); |
| + const ctxA = await newContext('portrait A', { cookie: cookieA }); |
| + const ctxB = await newContext('portrait B', { cookie: cookieB }); |
| + await ctxB.addInitScript(portraitCamera); |
| + const pageA = await openRoom(ctxA); |
| + const pageB = await openRoom(ctxB); |
| + const pair = await assertPair(pageA, pageB, 'portrait A', 'portrait B'); |
| + if (!pair) throw new Error('portrait call did not connect'); |
| + const a = pair.a.peer; |
| + const b = pair.b.peer; |
| + for (const page of [pageA, pageB]) { |
| + await page.setViewportSize({ width: 1280, height: 800 }); |
| + await chooseLayout(page, 'speaker'); |
| + } |
| + const look = (page, key) => page.evaluate((k) => { |
| + const fig = document.querySelector('#tiles > figure[data-tile="' + k + '"]'); |
| + if (!fig) return null; |
| + const v = fig.querySelector('video'); |
| + const c = fig.querySelector('.calls-tile-backdrop'); |
| + const r = fig.getBoundingClientRect(); |
| + return { |
| + stage: fig.hasAttribute('data-stage'), pip: fig.classList.contains('calls-tile-pip'), |
| + fit: fig.dataset.fit || '', shape: fig.dataset.shape || '', plain: fig.dataset.backdrop === 'plain', |
| + video: [v.videoWidth, v.videoHeight], objectFit: getComputedStyle(v).objectFit, |
| + box: { x: r.x, y: r.y, width: r.width, height: r.height }, |
| + backdrop: c ? { shown: getComputedStyle(c).display !== 'none', painted: +(c.dataset.painted || 0), costMs: +(c.dataset.costMs || 0), size: [c.width, c.height] } : null, |
| + }; |
| + }, key); |
| + // Where the picture is drawn in its tile, from the element's box and |
| + // object-fit: what `contain` or `cover` makes of 720 × 1280. |
| + const content = (t) => { |
| + const [vw, vh] = t.video; |
| + const s = (t.objectFit === 'contain' ? Math.min : Math.max)(t.box.width / vw, t.box.height / vh); |
| + const w = vw * s; |
| + const h = vh * s; |
| + const y = t.objectFit === 'contain' ? (t.box.height - h) / 2 : (t.box.height - h) * 0.35; |
| + return { x: t.box.x + (t.box.width - w) / 2, y: t.box.y + y, width: w, height: h }; |
| + }; |
| + const red = (c) => c[0] > 180 && c[1] < 90 && c[2] < 90; |
| + const green = (c) => c[1] > 180 && c[0] < 110 && c[2] < 110; |
| + const bands = async (page, t) => { |
| + const midX = t.box.x + t.box.width / 2; |
| + const top = await sampleColour(page, midX, t.box.y + Math.max(4, t.box.height * 0.02)); |
| + const bottom = await sampleColour(page, midX, t.box.y + t.box.height - Math.max(4, t.box.height * 0.02)); |
| + return { top, bottom, whole: red(top) && green(bottom) }; |
| + }; |
| + const shoot = async (page, name) => { if (out) await page.screenshot({ path: out + '/web-' + name + '.png' }); }; |
| + const settle = async (page, key, want, ms = 15000) => until(async () => { |
| + const t = await look(page, key); |
| + return t && want(t) ? t : false; |
| + }, ms); |
| + await pageA.evaluate(() => window.callsChrome.pin(false)); |
| + await pageB.evaluate(() => window.callsChrome.pin(false)); |
| + |
| + // A's landscape stage, B upright on it. |
| + // Upright: its sender may scale it (720 × 1280 arrives as 540 × 960 |
| + // on a cold start) but never turn it. |
| + let t = await settle(pageA, b, (x) => x.stage && x.video[1] > x.video[0] * 1.5 && x.fit === '1' && x.backdrop.painted > 1); |
| + check(t, 'B\'s upright camera is on A\'s stage, fitted, its backdrop painted — ' + JSON.stringify(t || (await look(pageA, b)))); |
| + if (t) { |
| + check(t.shape === 'portrait' && t.objectFit === 'contain', 'it reads as portrait and is shown whole (' + t.shape + ', ' + t.objectFit + ')'); |
| + check(t.backdrop.shown && t.backdrop.size[0] === 36 && t.backdrop.size[1] === 64, 'its backdrop shows, sampled upright (' + t.backdrop.size.join(' × ') + ')'); |
| + const c = content(t); |
| + check(Math.abs(c.height - t.box.height) < 1 && c.width < t.box.width / 2, |
| + 'the whole frame is in the window: ' + c.width.toFixed(0) + ' × ' + c.height.toFixed(0) + ' in ' + t.box.width + ' × ' + t.box.height); |
| + await sleep(600); |
| + const seen = await bands(pageA, t); |
| + check(seen.whole, 'its top and bottom both show on screen: red ' + seen.top + ', green ' + seen.bottom); |
| + await shoot(pageA, 'desktop-stage-portrait-after'); |
| + // 0.4.10, for the record: forced back to fill, the bands are gone. |
| + await pageA.evaluate((k) => { document.querySelector('#tiles > figure[data-tile="' + k + '"]').dataset.fit = '0'; }, b); |
| + await sleep(400); |
| + const before = await bands(pageA, { ...t, objectFit: 'cover' }); |
| + check(!before.whole, 'as 0.4.10 filled it, neither band showed (red ' + before.top + ', green ' + before.bottom + ')'); |
| + await shoot(pageA, 'desktop-stage-portrait-before'); |
| + await pageA.evaluate((k) => { document.querySelector('#tiles > figure[data-tile="' + k + '"]').dataset.fit = '1'; }, b); |
| + } |
| + |
| + // B's own self view, floating: upright, as its camera is. |
| + const self = await settle(pageB, 'self', (x) => x.pip && x.shape === 'portrait'); |
| + check(self && self.box.height > self.box.width * 1.5, 'B\'s upright self view floats upright — ' + JSON.stringify(self && self.box)); |
| + // And A's landscape camera on B's landscape stage fills, as before. |
| + t = await settle(pageB, a, (x) => x.stage && x.video[0] > 0 && x.fit === '0'); |
| + check(t && t.objectFit === 'cover', 'A\'s landscape camera fills B\'s landscape stage (' + (t && t.objectFit) + ')'); |
| + await shoot(pageB, 'desktop-self-portrait-after'); |
| + |
| + // The filmstrip: A pins herself; B's tile keeps its 16:9 and is fitted. |
| + await openTileMenu(pageA, 'self'); |
| + await pageA.locator('#tile-menu [data-act="pin"]').click(); |
| + t = await settle(pageA, b, (x) => !x.stage && x.fit === '1' && x.backdrop.shown); |
| + check(t && Math.abs(t.box.width / t.box.height - 16 / 9) < 0.05, 'in the filmstrip B\'s tile stays 16:9 and is fitted with its backdrop — ' + JSON.stringify(t && t.box)); |
| + await shoot(pageA, 'desktop-filmstrip-portrait-after'); |
| + // Grid: the same, tiles uniform. |
| + await chooseLayout(pageA, 'grid'); |
| + t = await settle(pageA, b, (x) => x.fit === '1' && x.backdrop.shown); |
| + const self2 = await look(pageA, 'self'); |
| + check(t && self2 && Math.abs(t.box.width - self2.box.width) < 1 && Math.abs(t.box.height - self2.box.height) < 1, |
| + 'in Grid every tile is one shape, B\'s fitted in its own — ' + JSON.stringify([t && t.box, self2 && self2.box])); |
| + await shoot(pageA, 'desktop-grid-portrait-after'); |
| + await chooseLayout(pageA, 'speaker'); |
| + await openTileMenu(pageA, 'self'); |
| + await pageA.locator('#tile-menu [data-act="pin"]').click(); |
| + |
| + // Phones. A upright phone viewing B's upright camera: fills. |
| + await pageA.setViewportSize({ width: 390, height: 844 }); |
| + t = await settle(pageA, b, (x) => x.stage && x.fit === '0'); |
| + check(t && t.objectFit === 'cover', 'on an upright phone B\'s upright camera fills (' + (t && t.objectFit) + ')'); |
| + await shoot(pageA, 'phone-viewing-portrait-after'); |
| + // B on an upright phone viewing A's landscape camera: fitted. |
| + await pageB.setViewportSize({ width: 390, height: 844 }); |
| + t = await settle(pageB, a, (x) => x.stage && x.fit === '1' && x.backdrop.painted > 0); |
| + check(t && t.objectFit === 'contain', 'on an upright phone A\'s landscape camera is shown whole (' + (t && t.objectFit) + ')'); |
| + await sleep(600); |
| + await shoot(pageB, 'phone-viewing-landscape-after'); |
| + await pageB.evaluate((k) => { document.querySelector('#tiles > figure[data-tile="' + k + '"]').dataset.fit = '0'; }, a); |
| + await sleep(300); |
| + await shoot(pageB, 'phone-viewing-landscape-before'); |
| + await pageB.evaluate((k) => { document.querySelector('#tiles > figure[data-tile="' + k + '"]').dataset.fit = '1'; }, a); |
| + await pageB.setViewportSize({ width: 1280, height: 800 }); |
| + await pageA.setViewportSize({ width: 1280, height: 800 }); |
| + |
| + // B turns the phone on its side mid-call. |
| + t = await settle(pageA, b, (x) => x.stage && x.fit === '1'); |
| + check(t, 'B is back on A\'s landscape stage, fitted'); |
| + await pageA.evaluate((k) => { |
| + const fig = document.querySelector('#tiles > figure[data-tile="' + k + '"]'); |
| + window.__portraitChanges = { fit: 0, shape: 0 }; |
| + new MutationObserver((list) => { |
| + for (const m of list) { |
| + if (m.attributeName === 'data-fit') window.__portraitChanges.fit += 1; |
| + if (m.attributeName === 'data-shape') window.__portraitChanges.shape += 1; |
| + } |
| + }).observe(fig, { attributes: true, attributeFilter: ['data-fit', 'data-shape'] }); |
| + }, b); |
| + await pageB.evaluate(() => window.__turnCamera(1280, 720)); |
| + t = await settle(pageA, b, (x) => x.video[0] === 1280 && x.video[1] === 720); |
| + await sleep(2000); |
| + t = await look(pageA, b); |
| + const changes = await pageA.evaluate(() => window.__portraitChanges); |
| + check(t && t.fit === '0' && t.shape === 'landscape' && t.objectFit === 'cover', 'turned on its side, B fills A\'s stage again (' + JSON.stringify(t && [t.fit, t.shape, t.objectFit]) + ')'); |
| + check(changes.fit === 1 && changes.shape === 1, 'the turn switched once: placement ' + changes.fit + ', shape ' + changes.shape); |
| + const cost = t && t.backdrop ? t.backdrop.costMs : NaN; |
| + line('portrait: the backdrop\'s slowest paint was ' + cost + ' ms, twice a second'); |
| + check(cost < 8, 'a backdrop paint is cheap (' + cost + ' ms)'); |
| + await leaveRoom(pageA); |
| + await leaveRoom(pageB); |
| + await ctxA.close(); |
| + await ctxB.close(); |
| + }, |
| + |
| // The call window (2026-09-28). Paul asked for the web call to be |
| // more like the Mac's: the whole call in a rounded window inset from |
| // the viewport. So: the window is inset with rounded corners on a |
diff --git a/test/browser/room-portrait.test.mjs b/test/browser/room-portrait.test.mjs| new file mode 100644 |
| index 0000000..a8bb88e |
| --- /dev/null |
| +++ b/test/browser/room-portrait.test.mjs |
| @@ -0,0 +1,115 @@ |
| +// room-portrait.test.mjs — portrait video (2026-10-21), gated without a |
| +// browser: room.js's videoShape (the classifier, with hysteresis) and |
| +// videoFits (fill or fit), lifted out of the shipped file by name with |
| +// the constants they read. The native apps' VideoShapeTests hold |
| +// VideoShape, VideoShapeReading and VideoPlacement to the same table. |
| +// |
| +// node --test test/browser/room-portrait.test.mjs |
| +import { test } from 'node:test'; |
| +import assert from 'node:assert/strict'; |
| +import { readFileSync } from 'node:fs'; |
| +import { fileURLToPath } from 'node:url'; |
| + |
| +const roomJS = fileURLToPath(new URL('../../internal/calls/static/room.js', import.meta.url)); |
| +const src = readFileSync(roomJS, 'utf8'); |
| + |
| +function lift(name) { |
| + const start = src.indexOf('function ' + name + '('); |
| + assert.notEqual(start, -1, name + ' is not in room.js any more — this test is stale, not passing'); |
| + let depth = 0; |
| + for (let i = src.indexOf('{', start); i < src.length; i++) { |
| + if (src[i] === '{') depth += 1; |
| + else if (src[i] === '}') { |
| + depth -= 1; |
| + if (depth === 0) return src.slice(start, i + 1); |
| + } |
| + } |
| + assert.fail('unbalanced braces reading ' + name + ' out of room.js'); |
| +} |
| +function liftConst(name) { |
| + const m = src.match(new RegExp('const ' + name + ' = ([0-9.]+);')); |
| + assert.ok(m, name + ' is not a numeric const in room.js any more'); |
| + return 'const ' + name + ' = ' + m[1] + ';'; |
| +} |
| +const CONSTS = ['SHAPE_ENTER', 'SHAPE_HOLD', 'FIT_TOLERANCE'].map(liftConst).join('\n'); |
| +// eslint-disable-next-line no-new-func |
| +const { videoShape, videoFits } = new Function( |
| + CONSTS + '\n' + lift('videoShape') + '\n' + lift('videoFits') + '; return { videoShape, videoFits };')(); |
| + |
| +test('the thresholds: h > w × 1.1 is portrait, w > h × 1.1 landscape, square-ish between', () => { |
| + assert.equal(videoShape(720, 1280, ''), 'portrait'); |
| + assert.equal(videoShape(1280, 720, ''), 'landscape'); |
| + assert.equal(videoShape(640, 480, ''), 'landscape'); |
| + assert.equal(videoShape(500, 500, ''), 'square'); |
| + assert.equal(videoShape(1000, 1100, ''), 'square', 'exactly 1.1 is not yet portrait'); |
| + assert.equal(videoShape(1000, 1101, ''), 'portrait'); |
| + assert.equal(videoShape(1101, 1000, ''), 'landscape'); |
| + assert.equal(videoShape(0, 0, ''), 'landscape', 'nothing known is landscape, as 0.4.10'); |
| + assert.equal(videoShape(0, 0, 'portrait'), 'portrait', 'and nothing new keeps what was'); |
| +}); |
| + |
| +test('hysteresis holds a shape through the band, and lets go past it', () => { |
| + let s = videoShape(720, 1280, ''); |
| + assert.equal(s, 'portrait'); |
| + s = videoShape(1000, 1050, s); |
| + assert.equal(s, 'portrait', 'still taller than wide'); |
| + s = videoShape(1000, 1000, s); |
| + assert.equal(s, 'square', 'no longer taller than wide'); |
| + s = videoShape(1000, 1080, s); |
| + assert.equal(s, 'square', 'back into the band is not portrait again'); |
| + s = videoShape(1000, 1200, s); |
| + assert.equal(s, 'portrait'); |
| +}); |
| + |
| +test('a picture jittering about the threshold changes once, not every frame', () => { |
| + let s = videoShape(1000, 1000, ''); |
| + let changes = 0; |
| + for (let i = 0; i < 40; i++) { |
| + const next = videoShape(1000, i % 2 ? 1105 : 1095, s); |
| + if (next !== s) changes += 1; |
| + s = next; |
| + } |
| + assert.equal(changes, 1); |
| +}); |
| + |
| +test('a phone turned mid-call switches once, through resolution steps', () => { |
| + // videoWidth and videoHeight have the sender's rotation applied: an |
| + // upright iPhone arrives as 720 × 1280 whatever its buffer is. |
| + const sizes = [[1280, 720], [960, 540], [1280, 720], [720, 1280], [540, 960], [720, 1280]]; |
| + let s = ''; |
| + const seen = []; |
| + for (const [w, h] of sizes) { |
| + s = videoShape(w, h, s); |
| + if (seen[seen.length - 1] !== s) seen.push(s); |
| + } |
| + assert.deepEqual(seen, ['landscape', 'portrait']); |
| +}); |
| + |
| +test('fill or fit: the decision table', () => { |
| + const fits = (vw, vh, bw, bh, screen = false) => videoFits(vw, vh, videoShape(vw, vh, ''), bw, bh, screen); |
| + // A screen is always fitted. |
| + assert.equal(fits(1920, 1200, 1440, 900, true), true); |
| + // The same shape fills, however much it crops. |
| + assert.equal(fits(1280, 720, 1440, 900), false); |
| + assert.equal(fits(720, 1280, 393, 852), false); |
| + assert.equal(fits(2560, 1080, 320, 180), false, '21:9 in 16:9 crops 25% and fills'); |
| + assert.equal(fits(640, 480, 320, 180), false, '4:3 in a 16:9 tile fills'); |
| + // Another shape with a crop of 15% or less still fills. |
| + assert.equal(fits(1000, 1000, 1100, 1000), false); |
| + // Another shape with more is fitted. |
| + assert.equal(fits(720, 1280, 1440, 900), true, 'an upright phone on a landscape window'); |
| + assert.equal(fits(720, 1280, 320, 180), true, 'and in a grid or filmstrip tile'); |
| + assert.equal(fits(1280, 720, 393, 852), true, 'a laptop on an upright phone'); |
| + assert.equal(fits(720, 720, 1440, 900), true, 'a square picture in a 16:10 window'); |
| + assert.equal(fits(480, 640, 1080, 1000), true, 'portrait in a square-ish place, 31% cut'); |
| + // Nothing known yet: fill. |
| + assert.equal(videoFits(0, 0, 'portrait', 1440, 900, false), false); |
| + assert.equal(videoFits(720, 1280, 'portrait', 0, 0, false), false); |
| +}); |
| + |
| +test('the shape held by hysteresis is the one that decides', () => { |
| + // A picture that was portrait and is now 1000 × 1050 is still portrait, |
| + // so in a 16:9 tile it stays fitted rather than flipping to fill. |
| + assert.equal(videoFits(1000, 1050, 'portrait', 320, 180, false), true); |
| + assert.equal(videoFits(1000, 1050, 'square', 320, 180, false), true, 'square is another shape too'); |
| +}); |