Uploads and the processing pipeline¶
How a file gets from the browser into the media library, and what CoreAPI
does with a video once it has arrived. Upload code lives in
handler_upload.go / handler_upload_progress.go; the pipeline in
handler_process.go, handler_pipeline.go, pipeline_work.go,
handler_normalize.go, handler_transcode.go, ffmpeg_limits.go and
storage_retry.go. What each processing step does to the file is in
Transcoding; serving and storage layout are in
Architecture.
Status
Live on staging (CoreAPI) with @playtelly/capability-apps 0.11.5 in
PlayoutAdmin. TellyboardAdmin is still on capability-apps 0.8.x with its
own media adapter, so it has none of the client features below (5 MB
chunks, no uploads-in-flight list, no Retry transcode).
Upload: tus and the upload token¶
browser ──POST /media/:ws/prepare──▶ CoreAPI (creates an UploadToken)
browser ──tus PATCH (50 MB chunks)──▶ tusd ──▶ S3 bucket
tusd ──POST /hooks/tusd (post-create, post-receive)──▶ CoreAPI (progress)
tusd ──POST /hooks/tusd (post-finish)──────────────▶ CoreAPI (Media row, pipeline starts)
- Prepare —
POST /media/:workspaceId/prepare(permissionworkspace:media:upload) validates the title/size/type, takes the storage prefix from the authorized workspace (never from the body) and creates anUploadTokenrow. It returns{token, expiresAt}. - Upload — the browser sends the file to tusd with the token in the
tus metadata (
uploadToken, plusfilenameandfiletype). tusd does not check the token; it only carries it to the hooks. - Finish — tusd's
post-finishhook turns the token into aMediarow (status=processing), marks the token used and startsprocessMedia.
Token lifetime¶
The token says which workspace, title and profile the finished file gets. Its window is sized to the file, so a large upload on a normal connection does not run out of time:
| Rule | Value (handler_upload_progress.go) |
|---|---|
| Minimum window | 30 minutes (uploadTokenMinTTL) |
| Growth with declared size | + 1 s per 512 KB of fileSize (assumes at least ~4 Mbit/s) |
| Maximum window at Prepare | 24 hours |
| Keep-alive | every progress report pushes expires_at to at least now + 30 min |
| Finish grace | post-finish still accepts an unused token that expired up to 48 h ago |
Example: a 2.3 GB file gets about 1 h 45 min at Prepare, and as long as bytes keep arriving the window keeps moving ahead of it. Only an upload that stops sending can expire, and even then a file that does arrive later is still turned into media within the grace period.
tusd hooks¶
All hooks go to one public, machine-facing route, POST /hooks/tusd:
| Hook | What CoreAPI does |
|---|---|
post-create |
records the first progress (bytes_received, last_progress_at), extends the token |
post-receive |
same, as chunks arrive |
post-finish |
creates the Media row and starts the pipeline |
tusd must be started with the progress events enabled
(-hooks-enabled-events=post-create,post-receive,post-finish; the report
interval is tusd's -progress-hooks-interval). Without the two progress
events uploads still finish normally, but the uploads list shows them as
waiting with no byte count and the keep-alive does not apply. The progress
handler always answers 200, so tusd never blocks or retries on it.
Uploads in flight¶
A Media row only exists after post-finish, so a file that is still
travelling is represented only by its token. Two routes expose them:
| Route | Permission | Purpose |
|---|---|---|
GET /media/:workspaceId/uploads |
workspace:media:view |
unfinished uploads of this workspace, newest first (max 200) |
DELETE /media/:workspaceId/uploads/:id |
workspace:media:upload |
dismiss one (soft-deletes the token; a file that still arrives for it is not turned into media) |
Each item (UploadInFlight) carries id, title, fileName, fileSize,
bytesReceived, percent, state, startedAt, lastProgressAt and
expiresAt. It never carries the token itself.
state |
Meaning |
|---|---|
waiting |
prepared, no bytes reported yet |
uploading |
bytes reported within the last 3 minutes |
stalled |
no bytes for more than 3 minutes, or the window ran out before anything arrived |
percent is null until a byte count is reported and is capped at 99
until post-finish creates the media row. Stalled uploads stay listed for
the 48 h finish grace, so nothing disappears silently.
The video pipeline¶
After post-finish, processMedia moves the file to its final key
(<org>/<ws>/<mediaId>/source/<file> for a video with a profile), sets
status=ready and, for a video, runs runVideoPipeline. Images, documents
and audio keep their own paths described in Transcoding.
One slot, one working copy¶
The whole video pipeline holds one heavy-job slot and works on one local copy of the file:
- Wait for a free slot (stage
queued). - Check free disk in the work directory: at least
MEDIA_PIPELINE_DISK_FACTOR× the file size (the source, the normalized copy and the encoded renditions are on disk together). If not, the transcode fails with a readable reason. - Fetch the file from storage once into a working folder.
- Normalize (stage
normalize): duration fix, remux, two-pass loudness (the measuring pass analyses audio only,-vn). The normalized file replaces the local copy at once; its upload back over the source runs in the background while the next steps work, and the pipeline waits for it before it ends. - Probe: duration and codecs from the local copy.
- Thumbnail: from the local copy, seeking by the probed duration.
- Ladder (stage
transcode, thenpackage, thendone): only when the upload picked a video profile with renditions. - The working folder is removed whichever way the pipeline ends. Leftover folders older than 2 h are removed at boot.
Stage values on Media.transcodingStage: queued, normalize, probe,
transcode, package, thumbnail, done, failed. In the video pipeline
the row moves queued → normalize → transcode → package → done; probe and
thumbnail run while the row still says normalize.
Single-decode ladder¶
encodeLadder encodes the renditions from one decode of the source:
one ffmpeg run splits the decoded frames (split filter) into up to four
renditions (maxLadderOutputs), each with its own scale filter and encoder
options. A profile with more rungs runs in groups of four. If a shared run
fails, that group is re-encoded one rendition at a time, so a ladder that
encoded before still encodes. Output is CMAF/HLS plus the SMIL tree, as
described in Transcoding.
The ladder's segments are uploaded through a bounded pool, and every storage call in the pipeline (source fetch, normalized upload, thumbnail, each segment) is tried up to three times with a short backoff (0.5 s, 2 s).
Configuration¶
All read once at startup (domain/media/config.go). Integers below 1 or
unparsable fall back to the default.
| Env var | Default | Effect |
|---|---|---|
MEDIA_PIPELINE_CONCURRENCY |
1 |
heavy jobs (video pipeline, transcode) running at once per CoreAPI process; the rest wait as queued |
MEDIA_FFMPEG_THREADS |
2 |
thread cap for every media ffmpeg run; in the single-decode ladder it applies per output, so a 3-rung run uses about three times this many encoder threads |
MEDIA_X264_PRESET |
fast |
libx264 preset of the ladder; an unknown value falls back to fast |
MEDIA_PIPELINE_DISK_FACTOR |
4 |
free disk required, as a multiple of the file size |
MEDIA_UPLOAD_CONCURRENCY |
4 |
ladder files uploaded to storage at once |
MEDIA_WORK_DIR |
system temp dir | where working folders go |
LOUDNORM_TARGET_LUFS |
-23 |
loudness target of the normalize step |
Every media ffmpeg runs under nice -n 10 when nice is available, so the
API in the same process stays responsive. ffmpeg would otherwise size its
thread pools from the host's cores rather than the container's limit, which
is why the cap exists.
Failures, heartbeat and retry¶
- Failure reason. A failed transcode sets
transcodingStage=failedand stores the reason inMedia.transcodingError(up to 500 characters). It is cleared when a retry starts. A failed transcode does not changestatus: the source file is stillreadyfor direct playback. - Heartbeat. While a worker runs it stamps
pipeline_heartbeat_atevery 30 s. A row in an in-flight stage whose heartbeat stopped for 3 minutes (or, for rows without one, untouched for 15 minutes) has lost its worker, for example after a restart. - Automatic sweep. 20 s after boot and then every 5 minutes, CoreAPI
restarts such rows, but only rows touched in the last 24 hours. Failed rows
are never restarted automatically. Claiming a row is one conditional
UPDATE, so with several replicas only one restarts it. Platform admins can run the sweep on demand withPOST /media/ops/resume-stuck. - Retry.
POST /media/:workspaceId/:id/retranscode(workspace:media:upload) re-runs the ladder for areadyvideo with a profile whose transcode isfailedor has lost its worker. It answers202withtranscodingStage: "queued";400when the media has no profile,409when the file is not ready or the transcode is still alive. Nothing is uploaded again: it fetches the stored source and does not rerun normalize.
Client side (@playtelly/capability-apps 0.11.x)¶
The shared upload sheet, batch uploader and media library table:
| Feature | Since | Detail |
|---|---|---|
| 50 MB tus chunks | 0.11.1 | TUS_CHUNK_SIZE in media-upload/uploadShared.js; fewer requests, and no practical file-size ceiling from the S3 part limit |
| Longer tus retries | 0.11.2 | retry delays 0 s … 60 s, about two minutes in total |
| Uploads in the Syncing view | 0.11.2 | in-flight uploads appear as rows with a progress bar ("50.0 MB of 286.1 MB"); polled every 2 s while something uploads, every 15 s otherwise; switches itself off against an older CoreAPI |
| Auto-resume | 0.11.3 | when tus gives up on a retryable error the uploader waits for the network and starts again from tusd's offset (5, 10, 20, 40, then 60 s apart, up to 30 times in a row; any progress resets the count) |
| Stalled uploads | 0.11.3 | "Stalled at 43%", last sign of life, and a Dismiss button |
| Retry transcode | 0.11.4 | failed rows show the reason and a Retry transcode button; stage labels such as "Waiting for a free worker" and "Preparing (audio levels)" |
| Stall watchdog | 0.11.5 | an upload that moves no bytes for 90 s is aborted and restarted from tusd's offset (after a 2 s pause) |
A host app opts in by giving its media adapter listUploads,
dismissUpload and retranscodeMedia; without them the library behaves
as before. An upload cannot be resumed after a page reload: the uploader
does not keep the upload URL across reloads.