@zaisma/game-server 1.49.0 → 1.51.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bench/brand/_headers +3 -1
- package/bench/brand/design/videos/share-card-moving.mp4 +0 -0
- package/bench/brand/share-card-letters.png +0 -0
- package/bench/brand/zaisma-wordmark-glass-black-448.png +0 -0
- package/bench/brand/zaisma-wordmark-glass-white-448.png +0 -0
- package/bench/glass.js +11305 -10431
- package/bin/local-play-bench.js +62 -1
- package/bin/local-play.d.ts +1 -0
- package/bin/local-play.js +132 -8
- package/package.json +1 -1
- package/sdk/sound.js +84 -0
- package/sdk/zaisma.d.ts +19 -3
- package/sdk/zaisma.js +23 -1
- package/src/clips.d.ts +50 -0
- package/src/clips.js +187 -0
- package/src/internal.d.ts +1 -0
- package/src/internal.js +1 -0
package/src/clips.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A game's clip, as files: the rules a clip keeps (up to 6 seconds, silent, H.264, fitting the 1200 by 630 share card) and the reader that
|
|
3
|
+
* checks them from an MP4's own boxes, with no decoder. Zaisma's developer API checks every clip with it, and play-locally's bench checks
|
|
4
|
+
* what it recorded before writing it, so both say the same.
|
|
5
|
+
*/
|
|
6
|
+
/** A clip's limits: what iMessage, Discord and Telegram all play, fitted inside the 1200 by 630 share card. */
|
|
7
|
+
export const CLIP_LIMITS = {
|
|
8
|
+
maximumDurationMs: 6000,
|
|
9
|
+
maximumWidthPx: 1200,
|
|
10
|
+
maximumHeightPx: 630,
|
|
11
|
+
/** The least a clip takes on its long and short sides, as a share picture. */
|
|
12
|
+
minimumLongSidePx: 600,
|
|
13
|
+
minimumShortSidePx: 315,
|
|
14
|
+
/** About 3 MB is usual: 6 seconds at 1200 by 630 and 4 megabits a second. */
|
|
15
|
+
maximumSizeBytes: 6 * 1024 * 1024,
|
|
16
|
+
maximumStillSizeBytes: 2 * 1024 * 1024,
|
|
17
|
+
/** The developer API's JSON body: the clip and its still in base64 (a third larger), with room for the field names. */
|
|
18
|
+
maximumBodySizeBytes: 11 * 1024 * 1024,
|
|
19
|
+
};
|
|
20
|
+
/** The sample formats that are H.264. */
|
|
21
|
+
const H264_FORMATS = ["avc1", "avc3"];
|
|
22
|
+
/** How deep boxes may nest inside a track: a clip's go about four deep. */
|
|
23
|
+
const MAXIMUM_BOX_DEPTH_COUNT = 12;
|
|
24
|
+
const CONTAINER_BOXES = new Set(["moov", "trak", "mdia", "minf", "stbl", "edts", "mvex"]);
|
|
25
|
+
/** The boxes between two places in the file, in order; null when one runs past its parent or is too short to be a box. */
|
|
26
|
+
function boxesIn(bytes, startIndex, endIndex) {
|
|
27
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
28
|
+
const boxes = [];
|
|
29
|
+
let atIndex = startIndex;
|
|
30
|
+
while (atIndex + 8 <= endIndex) {
|
|
31
|
+
let sizeBytes = view.getUint32(atIndex);
|
|
32
|
+
const type = String.fromCharCode(bytes[atIndex + 4], bytes[atIndex + 5], bytes[atIndex + 6], bytes[atIndex + 7]);
|
|
33
|
+
let headerBytes = 8;
|
|
34
|
+
if (sizeBytes === 1) {
|
|
35
|
+
if (atIndex + 16 > endIndex)
|
|
36
|
+
return null;
|
|
37
|
+
const highNumber = view.getUint32(atIndex + 8), lowNumber = view.getUint32(atIndex + 12);
|
|
38
|
+
sizeBytes = highNumber * 2 ** 32 + lowNumber;
|
|
39
|
+
headerBytes = 16;
|
|
40
|
+
}
|
|
41
|
+
else if (sizeBytes === 0) {
|
|
42
|
+
sizeBytes = endIndex - atIndex;
|
|
43
|
+
}
|
|
44
|
+
if (sizeBytes < headerBytes || atIndex + sizeBytes > endIndex)
|
|
45
|
+
return null;
|
|
46
|
+
boxes.push({ type, bodyStartIndex: atIndex + headerBytes, endIndex: atIndex + sizeBytes });
|
|
47
|
+
atIndex += sizeBytes;
|
|
48
|
+
}
|
|
49
|
+
return boxes;
|
|
50
|
+
}
|
|
51
|
+
/** A track's facts from its boxes, or null when its boxes are broken. */
|
|
52
|
+
function trackFacts(bytes, trak) {
|
|
53
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
54
|
+
let handler = "", format = "", widthPx = 0, heightPx = 0, durationMs = null;
|
|
55
|
+
// A real clip nests a handful of boxes deep; a file nesting deeper is broken (and can't run the reader out of stack).
|
|
56
|
+
const visit = (box, depthNumber) => {
|
|
57
|
+
if (CONTAINER_BOXES.has(box.type)) {
|
|
58
|
+
if (depthNumber >= MAXIMUM_BOX_DEPTH_COUNT)
|
|
59
|
+
return false;
|
|
60
|
+
const children = boxesIn(bytes, box.bodyStartIndex, box.endIndex);
|
|
61
|
+
return children !== null && children.every((child) => visit(child, depthNumber + 1));
|
|
62
|
+
}
|
|
63
|
+
const bodyIndex = box.bodyStartIndex, bodyBytes = box.endIndex - box.bodyStartIndex;
|
|
64
|
+
if (box.type === "tkhd") {
|
|
65
|
+
// Width and height are the last 8 bytes, 16.16 fixed point, whatever the version.
|
|
66
|
+
if (bodyBytes < 84)
|
|
67
|
+
return false;
|
|
68
|
+
widthPx = Math.round(view.getUint32(box.endIndex - 8) / 65536);
|
|
69
|
+
heightPx = Math.round(view.getUint32(box.endIndex - 4) / 65536);
|
|
70
|
+
}
|
|
71
|
+
else if (box.type === "mdhd") {
|
|
72
|
+
const boxVersionNumber = bytes[bodyIndex];
|
|
73
|
+
if (bodyBytes < (boxVersionNumber === 1 ? 32 : 20))
|
|
74
|
+
return false;
|
|
75
|
+
const timescaleNumber = view.getUint32(bodyIndex + (boxVersionNumber === 1 ? 20 : 12));
|
|
76
|
+
const durationNumber = boxVersionNumber === 1 ? view.getUint32(bodyIndex + 24) * 2 ** 32 + view.getUint32(bodyIndex + 28) : view.getUint32(bodyIndex + 16);
|
|
77
|
+
if (timescaleNumber > 0)
|
|
78
|
+
durationMs = Math.round((durationNumber / timescaleNumber) * 1000);
|
|
79
|
+
}
|
|
80
|
+
else if (box.type === "hdlr") {
|
|
81
|
+
if (bodyBytes < 12)
|
|
82
|
+
return false;
|
|
83
|
+
handler = String.fromCharCode(...bytes.subarray(bodyIndex + 8, bodyIndex + 12));
|
|
84
|
+
}
|
|
85
|
+
else if (box.type === "stsd") {
|
|
86
|
+
// Version and flags, the entry count, then the first entry: its size and its format.
|
|
87
|
+
if (bodyBytes < 16)
|
|
88
|
+
return false;
|
|
89
|
+
format = String.fromCharCode(...bytes.subarray(bodyIndex + 12, bodyIndex + 16));
|
|
90
|
+
}
|
|
91
|
+
return true;
|
|
92
|
+
};
|
|
93
|
+
const children = boxesIn(bytes, trak.bodyStartIndex, trak.endIndex);
|
|
94
|
+
if (!children || !children.every((child) => visit(child, 1)))
|
|
95
|
+
return null;
|
|
96
|
+
return { handler, format, widthPx, heightPx, durationMs };
|
|
97
|
+
}
|
|
98
|
+
/** An MP4's facts from its boxes, or null when it isn't an MP4 this can read. */
|
|
99
|
+
export function readMp4(bytes) {
|
|
100
|
+
const top = boxesIn(bytes, 0, bytes.byteLength);
|
|
101
|
+
if (!top || top[0]?.type !== "ftyp")
|
|
102
|
+
return null;
|
|
103
|
+
const brand = String.fromCharCode(...bytes.subarray(top[0].bodyStartIndex, top[0].bodyStartIndex + 4));
|
|
104
|
+
if (brand === "qt " || /^M4[ABP] /.test(brand))
|
|
105
|
+
return null;
|
|
106
|
+
const moov = top.find((box) => box.type === "moov");
|
|
107
|
+
if (!moov)
|
|
108
|
+
return null;
|
|
109
|
+
const moovChildren = boxesIn(bytes, moov.bodyStartIndex, moov.endIndex);
|
|
110
|
+
if (!moovChildren)
|
|
111
|
+
return null;
|
|
112
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
113
|
+
let movieDurationMs = null;
|
|
114
|
+
const mvhd = moovChildren.find((box) => box.type === "mvhd");
|
|
115
|
+
if (mvhd) {
|
|
116
|
+
const boxVersionNumber = bytes[mvhd.bodyStartIndex];
|
|
117
|
+
if (mvhd.endIndex - mvhd.bodyStartIndex >= (boxVersionNumber === 1 ? 32 : 20)) {
|
|
118
|
+
const timescaleNumber = view.getUint32(mvhd.bodyStartIndex + (boxVersionNumber === 1 ? 20 : 12));
|
|
119
|
+
const durationNumber = boxVersionNumber === 1 ? view.getUint32(mvhd.bodyStartIndex + 24) * 2 ** 32 + view.getUint32(mvhd.bodyStartIndex + 28) : view.getUint32(mvhd.bodyStartIndex + 16);
|
|
120
|
+
if (timescaleNumber > 0)
|
|
121
|
+
movieDurationMs = Math.round((durationNumber / timescaleNumber) * 1000);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const tracks = [];
|
|
125
|
+
for (const trak of moovChildren.filter((box) => box.type === "trak")) {
|
|
126
|
+
const facts = trackFacts(bytes, trak);
|
|
127
|
+
if (!facts)
|
|
128
|
+
return null;
|
|
129
|
+
tracks.push(facts);
|
|
130
|
+
}
|
|
131
|
+
const videoTracks = tracks.filter((track) => track.handler === "vide");
|
|
132
|
+
const video = videoTracks[0];
|
|
133
|
+
if (!video)
|
|
134
|
+
return null;
|
|
135
|
+
const isFragmented = moovChildren.some((box) => box.type === "mvex") || top.some((box) => box.type === "moof");
|
|
136
|
+
return {
|
|
137
|
+
widthPx: video.widthPx,
|
|
138
|
+
heightPx: video.heightPx,
|
|
139
|
+
durationMs: Math.max(video.durationMs ?? 0, movieDurationMs ?? 0),
|
|
140
|
+
videoFormat: video.format,
|
|
141
|
+
videoTrackCount: videoTracks.length,
|
|
142
|
+
soundTrackCount: tracks.filter((track) => track.handler === "soun").length,
|
|
143
|
+
isFragmented,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/** Whether a clip's size fits the share card: inside 1200 by 630, and at least 600 by 315 on its long and short sides. */
|
|
147
|
+
export function isClipSizeFitting(widthPx, heightPx) {
|
|
148
|
+
return (widthPx <= CLIP_LIMITS.maximumWidthPx &&
|
|
149
|
+
heightPx <= CLIP_LIMITS.maximumHeightPx &&
|
|
150
|
+
Math.max(widthPx, heightPx) >= CLIP_LIMITS.minimumLongSidePx &&
|
|
151
|
+
Math.min(widthPx, heightPx) >= CLIP_LIMITS.minimumShortSidePx);
|
|
152
|
+
}
|
|
153
|
+
/** The one ffmpeg line that makes any video into a clip, for fixes: its first 6 seconds, fitted inside the card, silent, H.264. */
|
|
154
|
+
export const CLIP_FFMPEG_LINE = `ffmpeg -i in.mp4 -t 6 -an -vf "scale=1200:630:force_original_aspect_ratio=decrease:force_divisible_by=2" -c:v libx264 -pix_fmt yuv420p -movflags +faststart clip.mp4`;
|
|
155
|
+
/** A clip file's facts, or the first rule it breaks. */
|
|
156
|
+
export function checkClip(bytes) {
|
|
157
|
+
if (bytes.byteLength > CLIP_LIMITS.maximumSizeBytes) {
|
|
158
|
+
return { problem: { code: "clip_too_big", message: `The clip is ${(bytes.byteLength / 1048576).toFixed(1)} MB, over ${CLIP_LIMITS.maximumSizeBytes / 1048576} MB.`, fix: `Save it at a lower bitrate (about 4 megabits a second is plenty), like: ${CLIP_FFMPEG_LINE.replace("-pix_fmt", "-b:v 4M -pix_fmt")}` } };
|
|
159
|
+
}
|
|
160
|
+
const facts = readMp4(bytes);
|
|
161
|
+
if (!facts)
|
|
162
|
+
return { problem: { code: "not_a_clip", message: "That isn't an MP4 video with a picture track.", fix: `Send an MP4 (H.264), like the bench's Record a clip makes, or make one: ${CLIP_FFMPEG_LINE}` } };
|
|
163
|
+
if (facts.isFragmented)
|
|
164
|
+
return { problem: { code: "not_a_clip", message: "The MP4 is fragmented (as a browser's recorder saves it), so its length can't be read.", fix: `Save it as a plain MP4: ${CLIP_FFMPEG_LINE}` } };
|
|
165
|
+
if (facts.videoTrackCount > 1)
|
|
166
|
+
return { problem: { code: "not_a_clip", message: `The MP4 has ${facts.videoTrackCount} picture tracks.`, fix: `Send one picture track: ${CLIP_FFMPEG_LINE}` } };
|
|
167
|
+
if (!H264_FORMATS.includes(facts.videoFormat)) {
|
|
168
|
+
return { problem: { code: "not_a_clip", message: `The clip's video is ${JSON.stringify(facts.videoFormat)}, not H.264, which every app that plays a link's video plays.`, fix: `Save it as H.264: ${CLIP_FFMPEG_LINE}` } };
|
|
169
|
+
}
|
|
170
|
+
if (facts.soundTrackCount)
|
|
171
|
+
return { problem: { code: "clip_has_sound", message: "The clip has a sound track; a clip is silent.", fix: `Take the sound out (ffmpeg's -an): ${CLIP_FFMPEG_LINE}` } };
|
|
172
|
+
if (facts.durationMs > CLIP_LIMITS.maximumDurationMs) {
|
|
173
|
+
return { problem: { code: "clip_too_long", message: `The clip runs ${(facts.durationMs / 1000).toFixed(1)} seconds, over ${CLIP_LIMITS.maximumDurationMs / 1000}.`, fix: `Cut it to at most 6 seconds that loop well (ffmpeg's -t 6): ${CLIP_FFMPEG_LINE}` } };
|
|
174
|
+
}
|
|
175
|
+
if (facts.durationMs <= 0)
|
|
176
|
+
return { problem: { code: "not_a_clip", message: "The clip's length can't be read: it says 0 seconds.", fix: `Save it again: ${CLIP_FFMPEG_LINE}` } };
|
|
177
|
+
if (!isClipSizeFitting(facts.widthPx, facts.heightPx)) {
|
|
178
|
+
return {
|
|
179
|
+
problem: {
|
|
180
|
+
code: "bad_clip_dimensions",
|
|
181
|
+
message: `The clip is ${facts.widthPx} by ${facts.heightPx} px.`,
|
|
182
|
+
fix: `Fit it inside ${CLIP_LIMITS.maximumWidthPx} by ${CLIP_LIMITS.maximumHeightPx} px, at least ${CLIP_LIMITS.minimumLongSidePx} by ${CLIP_LIMITS.minimumShortSidePx}: ${CLIP_FFMPEG_LINE}`,
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
return { facts };
|
|
187
|
+
}
|
package/src/internal.d.ts
CHANGED
|
@@ -15,3 +15,4 @@ export * from "./chat.ts";
|
|
|
15
15
|
export * from "./voice-ticket.ts";
|
|
16
16
|
export * from "./relay.ts";
|
|
17
17
|
export * from "./doors.ts";
|
|
18
|
+
export { CLIP_FFMPEG_LINE, CLIP_LIMITS, checkClip, isClipSizeFitting, readMp4, type ClipProblem, type ClipProblemCode, type Mp4Facts } from "./clips.ts";
|
package/src/internal.js
CHANGED