Add a video player, launched by selecting a video file from the Files app

This commit is contained in:
James Babcock committed 2026-09-28 14:11:04 -07:00
1 parent deaa93fead
commit cb6dddd120
13 files changed
+1784 -3

No files matched your search

+3
View File
@@ -56,6 +56,7 @@ test('Files on iOS exposes Run app for EHPKs and passes the selected local path'
const load = loader({ global: { isIOS: true } }, {
'../../native/file-access': {}, '../../native/font-files': { isFontFile: () => false },
'../../graphics/installed-fonts': {}, '../../native/image-files': { isDecodableImageFile: () => false },
'../../native/video-player': { isPlayableVideoFile: () => true },
'../evenhub/installed-apps': { readEvenHubPackageManifest: () => null }, '../evenhub/permission-dialog': {},
'./file-browser': { FileBrowserLayer: class { constructor(options) { browserOptions = options; } } },
'./file-info-dialog': { FileInfoDialogLayer: class { constructor(entry, value) { actions = value; } } },
@@ -64,6 +65,8 @@ test('Files on iOS exposes Run app for EHPKs and passes the selected local path'
});
load('app/apps/files/files-app.ts').createFilesAppWindow({ openEhpkApp: path => { opened = path; }, installEhpkApp: path => { installed = path; } });
assert.equal(browserOptions.isSupportedFile('test.EHPK'), true);
// Video playback is Android-only for now.
assert.equal(browserOptions.isSupportedFile('clip.mp4'), false);
const ctx = { stack: { push() {}, pop() {} } };
browserOptions.onFilePicked({ name: 'test.ehpk', path: '/Documents/test.ehpk' }, ctx);
assert.deepEqual(Array.from(actions, a => a.label), ['Run app', 'Install']);
@@ -0,0 +1,75 @@
package com.faceclaw.app
import kotlin.test.*
class VideoFrameScalerTest {
private val identity = ByteArray(256) { it.toByte() }
private fun scale(scaler: VideoFrameScaler, luma: ByteArray, lut: ByteArray = identity): IntArray {
val out = ByteArray(scaler.outputWidth * scaler.outputHeight)
scaler.scale(luma, 0, scaler.sourceWidth, 1, lut, out)
return IntArray(out.size) { out[it].toInt() and 0xff }
}
@Test fun twoToOneDownscaleAveragesEachBox() {
// 4x2 source -> 2x1: each output is the mean of a 2x2 box.
val luma = byteArrayOf(0, 100, 200.toByte(), 200.toByte(), 20, 80, 100, 100)
val scaler = VideoFrameScaler(4, 2, 0, 2, 1)
assertContentEquals(intArrayOf(50, 150), scale(scaler, luma))
}
@Test fun largeDownscaleStaysWithinTheTapBudget() {
// 100:1 horizontally still averages only MAX_TAPS samples, spread across the box.
val luma = ByteArray(200) { (if (it % 100 < 50) 0 else 200).toByte() }
val scaler = VideoFrameScaler(200, 1, 0, 2, 1)
assertContentEquals(intArrayOf(100, 100), scale(scaler, luma))
}
@Test fun upscaleIsNearestNeighbor() {
val scaler = VideoFrameScaler(2, 1, 0, 4, 2)
assertContentEquals(intArrayOf(10, 10, 90, 90, 10, 10, 90, 90), scale(scaler, byteArrayOf(10, 90)))
}
@Test fun offsetAndStridesSelectTheVisiblePlane() {
// A 2x2 visible crop at (1, 1) of a 4-wide plane, with interleaved samples (pixelStride 2).
val plane = ByteArray(40)
fun put(x: Int, y: Int, v: Int) { plane[(1 + y) * 10 + 1 + x * 2] = v.toByte() }
put(0, 0, 11); put(1, 0, 22); put(0, 1, 33); put(1, 1, 44)
val scaler = VideoFrameScaler(2, 2, 0, 2, 2)
val out = ByteArray(4)
scaler.scale(plane, 11, 10, 2, identity, out)
assertContentEquals(byteArrayOf(11, 22, 33, 44), out)
}
@Test fun rotationsMatchTheContainerClockwiseConvention() {
// Coded 3x2: 1 2 3
// 4 5 6
val luma = byteArrayOf(1, 2, 3, 4, 5, 6)
assertContentEquals(intArrayOf(4, 1, 5, 2, 6, 3), scale(VideoFrameScaler(3, 2, 90, 2, 3), luma))
assertContentEquals(intArrayOf(6, 5, 4, 3, 2, 1), scale(VideoFrameScaler(3, 2, 180, 3, 2), luma))
assertContentEquals(intArrayOf(3, 6, 2, 5, 1, 4), scale(VideoFrameScaler(3, 2, 270, 2, 3), luma))
}
@Test fun normalizeRotationSnapsToQuarterTurns() {
assertEquals(0, VideoFrameScaler.normalizeRotation(0))
assertEquals(90, VideoFrameScaler.normalizeRotation(90))
assertEquals(270, VideoFrameScaler.normalizeRotation(-90))
assertEquals(180, VideoFrameScaler.normalizeRotation(540))
assertEquals(0, VideoFrameScaler.normalizeRotation(359))
}
@Test fun limitedRangeStretchesAndMonochromeThresholds() {
val gray = VideoFrameScaler.toneLut(limitedRange = true, gamma = 1f, monochrome = false)
assertEquals(0, gray[16].toInt() and 0xff)
assertEquals(0, gray[0].toInt() and 0xff)
assertEquals(255, gray[235].toInt() and 0xff)
assertEquals(255, gray[255].toInt() and 0xff)
val mono = VideoFrameScaler.toneLut(limitedRange = false, gamma = 2.2f, monochrome = true)
assertEquals(0, mono[127].toInt() and 0xff)
assertEquals(255, mono[128].toInt() and 0xff)
// Gamma darkens midtones but leaves the ends alone.
val photo = VideoFrameScaler.toneLut(limitedRange = false, gamma = 2.2f, monochrome = false)
assertTrue((photo[128].toInt() and 0xff) < 64)
assertEquals(255, photo[255].toInt() and 0xff)
}
}
+1
View File
@@ -36,6 +36,7 @@
"../app/g2/glasses-display-state.ts",
"../app/g2/glance-state.ts",
"../app/apps/glanceboard/layout.ts",
"../app/apps/files/video-layout.ts",
"../app/g2/pairing-candidates.ts",
"../app/apps/calculator/math/*.ts",
"../app/apps/microphones/mic-protocol.ts",
+57
View File
@@ -0,0 +1,57 @@
const test = require("node:test");
const assert = require("node:assert/strict");
const {
fitVideo,
formatMediaTime,
videoHeightMode,
videoRect,
} = require("../.test-build/app/apps/files/video-layout.js");
const WINDOW = { width: 576, height: 260 };
const TALL_WINDOW = { width: 576, height: 452 };
// ../g2flash/demos/badapple.mp4
const BAD_APPLE = { width: 1444, height: 1080 };
test("fixed sizes fit their box and keep the aspect ratio", () => {
assert.deepEqual(videoRect("small", BAD_APPLE, WINDOW), { x: 208, y: 70, width: 160, height: 120 });
assert.deepEqual(videoRect("medium", BAD_APPLE, WINDOW), { x: 168, y: 40, width: 240, height: 180 });
const wide = videoRect("small", { width: 1920, height: 1080 }, WINDOW);
assert.equal(wide.width, 160);
assert.equal(wide.height, 90);
});
test("large fills the window height; full screen fills the tall window's width", () => {
assert.deepEqual(videoRect("large", BAD_APPLE, WINDOW), { x: 112, y: 0, width: 348, height: 260 });
assert.deepEqual(videoRect("full", BAD_APPLE, TALL_WINDOW), { x: 0, y: 10, width: 576, height: 430 });
assert.equal(videoHeightMode("full"), "max");
assert.equal(videoHeightMode("large"), "min");
});
test("rects are aligned for the delta encoder and stay inside the viewport", () => {
for (const size of ["small", "medium", "large", "full"]) {
for (const video of [BAD_APPLE, { width: 1080, height: 1920 }, { width: 100, height: 20 }, { width: 7, height: 5 }]) {
for (const viewport of [WINDOW, TALL_WINDOW, { width: 150, height: 100 }]) {
const rect = videoRect(size, video, viewport);
const where = `${size} ${video.width}x${video.height} in ${viewport.width}x${viewport.height}`;
assert.equal(rect.x % 4, 0, where);
assert.equal(rect.y % 2, 0, where);
assert.equal(rect.width % 2, 0, where);
assert.equal(rect.height % 2, 0, where);
assert.ok(rect.width >= 2 && rect.height >= 2, where);
assert.ok(rect.x + rect.width <= viewport.width, where);
assert.ok(rect.y + rect.height <= viewport.height, where);
}
}
}
});
test("a tiny video is scaled up to the chosen size", () => {
assert.deepEqual(fitVideo({ width: 80, height: 60 }, { width: 240, height: 180 }), { width: 240, height: 180 });
});
test("media times", () => {
assert.equal(formatMediaTime(0), "0:00");
assert.equal(formatMediaTime(232_130), "3:52");
assert.equal(formatMediaTime(3_723_000), "1:02:03");
});