A video becomes an asset like a sound: uploaded once, converted to one format by a worker of its own, cut into clips in its tab, and worn by a sprite through its Texture row. Only Chrome is a target, without alpha. - Server: POST ?r=videos queues the upload under the hash of its bytes, so the MP4's name is known at once. The worker (VideoWorker over VideoFormat) writes H.264/AAC, yuv420p, constant frame rate, a keyframe every 15 frames, at most 1920x1080, with a poster and the details beside it. A failed job says why and is tried again on the next upload. - An upload may be up to 2 GB. It is streamed from the request to disk and hashed there, so php-fpm holds none of it in memory. The other routes keep their 256 MB. - The worker runs in the container game-maker-video on stein2, a compose project of its own (tools/video/), and in CI beside the PHP server. The CI image has ffmpeg for it. - Model: asset type video with size, length, frame rate and sound, and clips like a sound's. The clip helpers and commands serve both. - A sprite's Texture row offers videos and clips after the pictures. resolvePicture names a playing video by a key, and the texture library hands it to a VideoLibrary with one player per key. - The editor shows a placed video as a quiet preview. Play starts it over at its clip's start and lets it be heard through the master bus, a pause holds it, Esc makes it quiet again. - Video tab: the picture, a ruler, the clip strip and a track on the Sound tab's time axis. A drag spans a clip, a handle moves an edge, Play plays the picked clip. - The picker shows a video on its way with its progress and adopts an upload from it once it is ready. Dropping a video file does the same. Decided on my own: - Two sprites wearing the same clip share one player and show the same picture. Control per instance belongs to the follow-up with nodes. - A video plays in real time, the game's time scale does not slow it. - The container may use 3 of the 4 cores of stein2. Findings: - The first real upload (279 MB) got 413: the API's own ceiling of 256 MB for every request, not nginx or the Brandmauer. - Chrome has no picture of a video that never played, and a Pixi VideoSource drawn before the first frame stays empty for good. The texture is made in the first frame callback (openVideo). - A player nobody asks for in 2 s is let go. The inspector's PREVIEW asked only on an edit, so with the sprite not on screen its texture was destroyed under it and the batcher threw on every frame. The preview now redraws every frame while it shows a video, like a Time shader. A let-go texture keeps an empty source instead of none. - VideoSource.load() reloads the element and cuts off an earlier play(). Playing starts after loadeddata. Tests: - PHP videos.test.php: 68 checks, with the real ffmpeg - 300 MB upload through Brandmauer and nginx: 201 in 21 s, php-fpm at 13.7 MB RSS at most - Unit videos.test.ts: 20 tests - e2e 66 (stein2, atx was off): ALL PASSED, 18 checks; e2e 04 with the new asset count green - Gate (atx): 294 files, 4520 tests. serialize.test.ts used video as its example of an unknown type and now uses a made-up one. - Counter-checks: a keyframe gap of 30 fails the PHP check, a key that keeps the video's end fails 2 unit tests, a clip without its end fails "plays only its stretch" in e2e 66 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
174 lines
6.0 KiB
PHP
174 lines
6.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* The videos waiting to be converted (#402): `data/videos/queue/{folder}-{hash}.src` holds the
|
|
* uploaded bytes, `{folder}-{hash}.json` what became of them. The API adds to it, the worker in the
|
|
* container `game-maker-video` takes from it, one at a time, oldest first.
|
|
*
|
|
* A job is `queued`, `converting` (with `progress` from 0 to 1) or `failed` (with `error`). A done
|
|
* one is no job any more: its MP4 lies in the account's folder and the job is gone. A failed one
|
|
* stays, so the picker can say why, until the file is deleted or uploaded again.
|
|
*
|
|
* Both files are hidden from the web server: the upload is not what anybody should be served.
|
|
*/
|
|
final class VideoQueue
|
|
{
|
|
public const QUEUED = 'queued';
|
|
public const CONVERTING = 'converting';
|
|
public const FAILED = 'failed';
|
|
|
|
public function __construct(private readonly string $dir)
|
|
{
|
|
if (!is_dir($this->dir) && !mkdir($this->dir, 0775, true) && !is_dir($this->dir)) {
|
|
throw new RuntimeException("Cannot create directory {$this->dir}");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Copies an upload into a temp file beside the jobs, never more than `$maxBytes` of it: a video
|
|
* is too big to be held in memory. The caller hands the file to add() or deletes it.
|
|
*
|
|
* @param resource $in
|
|
* @throws VideoTooLargeException when the stream has more than `$maxBytes`
|
|
*/
|
|
public function receive($in, int $maxBytes): string
|
|
{
|
|
$tmp = "{$this->dir}/upload-" . bin2hex(random_bytes(8)) . '.tmp';
|
|
$out = fopen($tmp, 'xb');
|
|
if ($out === false) {
|
|
throw new RuntimeException('Cannot write uploaded video');
|
|
}
|
|
$copied = stream_copy_to_stream($in, $out, $maxBytes + 1);
|
|
fclose($out);
|
|
if ($copied === false || $copied > $maxBytes) {
|
|
unlink($tmp);
|
|
throw $copied === false ? new RuntimeException('Cannot write uploaded video') : new VideoTooLargeException();
|
|
}
|
|
return $tmp;
|
|
}
|
|
|
|
/** Queues the upload received into `$upload`, which is moved and gone afterwards. */
|
|
public function add(string $folder, string $hash, string $upload, string $name): void
|
|
{
|
|
if (!chmod($upload, AtomicFile::HIDDEN) || !rename($upload, $this->source($folder, $hash))) {
|
|
throw new RuntimeException('Cannot write uploaded video');
|
|
}
|
|
$this->write($folder, $hash, [
|
|
'folder' => $folder,
|
|
'hash' => $hash,
|
|
'name' => $name,
|
|
'state' => self::QUEUED,
|
|
'progress' => 0,
|
|
'error' => '',
|
|
'createdAt' => gmdate('c'),
|
|
]);
|
|
}
|
|
|
|
/** @return array<string,mixed>|null */
|
|
public function job(string $folder, string $hash): ?array
|
|
{
|
|
$path = $this->jobPath($folder, $hash);
|
|
if (!is_file($path)) {
|
|
return null;
|
|
}
|
|
$raw = @file_get_contents($path);
|
|
$job = $raw === false ? null : json_decode($raw, true);
|
|
return is_array($job) ? $job : null;
|
|
}
|
|
|
|
/** @return list<array<string,mixed>> every job, oldest first */
|
|
public function jobs(): array
|
|
{
|
|
$jobs = [];
|
|
foreach (glob($this->dir . '/*.json') ?: [] as $path) {
|
|
if (!preg_match('/^(' . UserStore::FOLDER_PATTERN . ')-([a-f0-9]{64})\.json$/', basename($path), $m)) {
|
|
continue;
|
|
}
|
|
$job = $this->job($m[1], $m[2]);
|
|
if ($job !== null) {
|
|
$jobs[] = $job;
|
|
}
|
|
}
|
|
usort($jobs, static fn(array $a, array $b): int => strcmp((string) $a['createdAt'], (string) $b['createdAt']));
|
|
return $jobs;
|
|
}
|
|
|
|
/** The oldest job nobody has started on, or null when there is none. */
|
|
public function next(): ?array
|
|
{
|
|
foreach ($this->jobs() as $job) {
|
|
if ($job['state'] === self::QUEUED && is_file($this->source($job['folder'], $job['hash']))) {
|
|
return $job;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Changes a job, unless it was deleted in the meantime: then false, and the one converting it
|
|
* knows to stop.
|
|
*
|
|
* @param array<string,mixed> $changes
|
|
*/
|
|
public function update(string $folder, string $hash, array $changes): bool
|
|
{
|
|
$job = $this->job($folder, $hash);
|
|
if ($job === null) {
|
|
return false;
|
|
}
|
|
$this->write($folder, $hash, $changes + $job);
|
|
return true;
|
|
}
|
|
|
|
/** A job that was being converted when the worker stopped is started again. */
|
|
public function requeueInterrupted(): int
|
|
{
|
|
$count = 0;
|
|
foreach ($this->jobs() as $job) {
|
|
if ($job['state'] === self::CONVERTING) {
|
|
$this->update($job['folder'], $job['hash'], ['state' => self::QUEUED, 'progress' => 0]);
|
|
$count++;
|
|
}
|
|
}
|
|
return $count;
|
|
}
|
|
|
|
public function remove(string $folder, string $hash): bool
|
|
{
|
|
$removed = false;
|
|
foreach ([$this->source($folder, $hash), $this->jobPath($folder, $hash)] as $path) {
|
|
if (is_file($path)) {
|
|
$removed = unlink($path) || $removed;
|
|
}
|
|
}
|
|
return $removed;
|
|
}
|
|
|
|
/** Drops the upload once it is converted or cannot be; the job itself says what happened. */
|
|
public function dropSource(string $folder, string $hash): void
|
|
{
|
|
$path = $this->source($folder, $hash);
|
|
if (is_file($path)) {
|
|
unlink($path);
|
|
}
|
|
}
|
|
|
|
public function source(string $folder, string $hash): string
|
|
{
|
|
return "{$this->dir}/{$folder}-{$hash}.src";
|
|
}
|
|
|
|
private function jobPath(string $folder, string $hash): string
|
|
{
|
|
return "{$this->dir}/{$folder}-{$hash}.json";
|
|
}
|
|
|
|
/** @param array<string,mixed> $job */
|
|
private function write(string $folder, string $hash, array $job): void
|
|
{
|
|
$json = json_encode($job, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
AtomicFile::write($this->jobPath($folder, $hash), (string) $json, AtomicFile::HIDDEN, 'video job');
|
|
}
|
|
}
|