feat(ffmpeg): prefetch priority, cancelation and listing #108
How do I know that a prefect has completed?
job::key is derived by calling strategy->describe() inside the constructor, while pending_ is keyed by a separate strategy->describe() call in enqueue(). This duplicates work and can lead to inconsistencies (e.g., CANCEL/LIST use job::key, while de-dup uses the map key). Make the key a single computed value passed into job so all tracking uses the same string.
This issue also appears on line 141 of the same file.
[!NOTE]
Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
The change still has an unresolved tracking-key inconsistency in src/modules/ffmpeg/producer/prefetch.cpp where strategy->describe() is recomputed for both job::key and pending_ map keys, duplicating work and risking mismatched CANCEL/LIST vs de-dup behavior (also noted again at line 141).
<details open>
<summary><strong>1 open issue</strong></summary>
</details>
<details>
<summary><strong>What changed in this PR</strong></summary>
This PR extends the ffmpeg module’s AMCP PREFETCH functionality with queue control features (priority ordering, cancellation, and listing), adding preemption behavior when workers are saturated to better control background cache warming.
Changes:
PRIORITY handling to PREFETCH, including reprioritisation of queued requests and preemption of lower-priority running work.PREFETCH CANCEL to stop queued/running prefetch jobs (optionally scoped by range/key).PREFETCH LIST to return current running/queued prefetch state including progress and ordering.| File | Description |
| ---- | ----------- |
| src/modules/ffmpeg/producer/prefetch.h | Documents new PREFETCH variants (PRIORITY/CANCEL/LIST) and exposes new command handlers. |
| src/modules/ffmpeg/producer/prefetch.cpp | Reworks the prefetch queue into prioritized jobs with cancellation, listing, and preemption logic; adds shared request parsing. |
| src/modules/ffmpeg/producer/prefetch_strategy.h | Adds url() to strategies and makes progress fields atomic for concurrent LIST reads. |
| src/modules/ffmpeg/producer/prefetch_whole_file.cpp | Implements url() for whole-file prefetch strategy. |
| src/modules/ffmpeg/producer/prefetch_range.cpp | Implements url() for range prefetch strategy. |
| src/modules/ffmpeg/ffmpeg.cpp | Registers PREFETCH CANCEL and PREFETCH LIST AMCP commands. |
</details>
<sub>Review effort: Lite</sub>
How do I know that a prefect has completed?
~~The short answer is: you don't at the moment. I've discussed this with Niklas H, and it requires public apis from the shared protocol that are non-trivial. This is the slack-thread: https://nxtedition.slack.com/archives/C07RFT1UXBM/p1793009036151269~~
PREFETCH STATUS CACHED {url} now answers it: DONE means the range or whole file is in the cache
Reprioritising an already-queued PREFETCH only changes priority but keeps the original seq. That means a job moved into a priority can run ahead of other jobs already queued at that priority (because runs_before breaks ties by seq), which doesn’t match the PR description’s “moves it” semantics / “same priority runs in order they were queued” after the move.
[!NOTE]
Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
A moderate active issue remains in src/modules/ffmpeg/producer/prefetch.cpp:151 where reprioritizing an already-queued PREFETCH updates priority without adjusting seq, allowing it to jump ahead of other jobs at the same priority contrary to the intended “move”/FIFO semantics.
<details open>
<summary><strong>2 open issues</strong></summary>
</details>
<sub>Review effort: Lite</sub>
What happens in the following case?
PREFETCH [channel[-layer]] url [SEEK/IN/LENGTH/OUT] PRIORITY 1
PREFETCH [channel[-layer]] url [SEEK/IN/LENGTH/OUT] PRIORITY 0
PREFETCH [channel[-layer]] url [SEEK/IN/LENGTH/OUT] PRIORITY 2
Does last always win? i.e. it updates the existing one?
Also it's weird that PREFTECH needs a channel and layer? shouldn't it be:
PREFETCH url [PRIORITY/SEEK/in/LENGTH/OUT]?
For simplicity, maybe sending PRIORITY -1 means cancel? alternatively PRIORITY 0 means CANCEL and PRIORITY 1 is the default
Do we have a (configurable) concurrency limit on prefetching?
What happens in the following case?
>> PREFETCH [channel[-layer]] url [SEEK/IN/LENGTH/OUT] PRIORITY 1 > PREFETCH [channel[-layer]] url [SEEK/IN/LENGTH/OUT] PRIORITY 0 > PREFETCH [channel[-layer]] url [SEEK/IN/LENGTH/OUT] PRIORITY 2 >
> Does last always win? i.e. it updates the existing one?
> Also it's weird that PREFTECH needs a channel and layer? shouldn't it be:
> PREFETCH url [PRIORITY/SEEK/in/LENGTH/OUT]?
> For simplicity, maybe sending PRIORITY -1 means cancel? alternatively PRIORITY 0 means CANCEL and PRIORITY 1 is the default
Do we have a (configurable) concurrency limit on prefetching?
Yes: ffmpeg.producer.cache.prefetch-concurrency, it was introduced with the initial PREFETCH work. It is set to 2 by default
> Does last always win? i.e. it updates the existing one?
> Also it's weird that PREFTECH needs a channel and layer? shouldn't it be:
> PREFETCH url [PRIORITY/SEEK/in/LENGTH/OUT]?
> For simplicity, maybe sending PRIORITY -1 means cancel? alternatively PRIORITY 0 means CANCEL and PRIORITY 1 is the default
> It updates the existing one.
You only need channel if you want to use the old school way of representing time by frame numbers, since that depends on the channel settings
* I don't think I agree about canceling by setting priority to -1 or 0 being simpler than a dedicated command with explicit sematics
So we agree the following will work:
PREFTECH {url}
PREFTECH {url} PRIORITY 2
PREFTECH CANCEL {url}
It's a bit weird that the CANCEL verb goes after PREFTECH but not verb is required for the other command. But I guess that's just bike shedding at this point.
LGTM
Approved these changes.
Could we add a
PREFETCH STATUS {url}
That outputs the same line as PREFETCH LIST but for on specific entry?
What is key?
Maybe PREFETCH STATUS is the same as PREFETCH LIST and PREFETCH STATUS {url} is one entry?
Could we add a
>> PREFETCH STATUS {url} >
> That outputs the same line as PREFETCH LIST but for on specific entry?
> MaybePREFETCH STATUSis the same asPREFETCH LISTandPREFETCH STATUS {url}is one entry?
PEFETCH {url} IN 120. OUT 250.
PREFETCH {url}
Should we skip LIST altogether and just go with the broader STATUS?
yes
Bear in mind that you still might get multiple rows in the result if you've done something like this
I guess the status "list" should include the in/out for each entry as well?
What is key?
What key are you refering to?
> What is key?> What key are you refering to?
> The cache_key sent to shared is just a sha256 hash.
In the prefetcher code, key is a string to uniquely identify a prefetch job. it includes the url and start/end times if they were given.
You mentioned a key here:
RUNNING|QUEUED priority bytes size key
> Bear in mind that you still might get multiple rows in the result if you've done something like this
> I guess the status "list" should include the in/out for each entry as well?
Yea, that sounds good. That is not in now. I'm discussing with claude how to do it. I assume that you'd want the same defining params back (url + in/seek/out/length) as you put in. but not priority
> > Bear in mind that you still might get multiple rows in the result if you've done something like this
>
>
> I guess the status "list" should include the in/out for each entry as well?
> Yea, that sounds good. That is not in now. I'm discussing with claude how to do it. I assume that you'd want the same defining params back (url + in/seek/out/length) as you put in. but not priority
why not priority?
> > What is key?
>
>
> What key are you refering to?
>
> The cache_key sent to shared is just a sha256 hash.
> In the prefetcher code, key is a string to uniquely identify a prefetch job. it includes the url and start/end times if they were given.
> You mentioned a key here:
> RUNNING|QUEUED priority bytes size key
Yea, that is the same as above, a combination of the url and start/end times. But they are processed, so most of the tie not recognisable as the same as the params a client has sent
why not priority?
Because that has no impact on what has been cached for that request. And what should it respond with if the priority is changed during prefetching?
> why not priority?
> Because that has no impact on what has been cached for that request. And what should it respond with if the priority is changed during prefetching?
The current priority? and if it's done then I guess it's 0?
PR description specifies a PREFETCH LIST command, but the module only registers PREFETCH STATUS. If clients/scripts are built around LIST, they'll fail to find the command. Consider registering LIST as an alias (or renaming the command) so the implementation matches the documented interface.
PR description says the LIST output format is RUNNING|QUEUED priority bytes size key, but PREFETCH STATUS currently outputs the original request text (potentially including channel/range tokens) instead of the canonical key (describe-prefetch). This is an observable API mismatch and could break consumers that parse the documented format.
This issue also appears on line 585 of the same file.
[!NOTE]
Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
The change exposes a documented command/output contract mismatch—PREFETCH STATUS prints the original request text rather than the canonical key and the described PREFETCH LIST command is not registered—creating observable API discrepancies that could break consumers.
<details open>
<summary><strong>4 open issues</strong></summary>
</details>
<sub>Review effort: Lite</sub>
The last updated made the following changes:
PREFETCH STATUS: each line ends with the request as it was sent
A client can now match its PREFETCH to a STATUS line without converting
SEEK/IN/LENGTH/OUT. The last field of each line is the PREFETCH parameters as the client sent them (url as given, range parameters in the client’s spelling and order), without the channel and PRIORITY:
PREFETCH 1 "http://host/clip.mp4" IN 120 OUT 250
PREFETCH "http://host/clip.mp4"
PREFETCH STATUS CACHED "http://host/clip.mp4"
200 PREFETCH STATUS OK
RUNNING 0 6291456 50572771 http://host/clip.mp4
DONE - 27134997 50572771 http://host/clip.mp4 IN 120 OUT 250
Line format: STATE priority bytes size request. The request comes last
because the url and params can contain spaces.
same request is sent again with a new PRIORITY, the job is moved in the
queue and no line is added.
1 url IN 120 OUT 250,
url SEEK :2.4 LENGTH :2.6) are still one job. Each spelling gets itsown line, live and CACHED, showing that job's state.
file has been prefetched. Its bytes and size are then the whole file's.
<key>.prefetch line has a requests field listing every
spelling seen, base64-encoded and comma-separated. A spelling sent later
is added even if its own attempt failed (for example while offline), so
it reports DONE once the range is cached. The format changed in place
(still
caspar-prefetch 1) because it hasn't been released.PREFETCH CANCEL only cancels the prefetch its parameters describe
Before, PREFETCH CANCEL url cancelled every prefetch of the url, ranges
included. There was no way to stop a whole-file prefetch and keep a range
of the same clip coming.
PREFETCH CANCEL url the whole-file prefetch of url only
PREFETCH CANCEL url SEEK :5 OUT :7 that range, in any spelling of it
PREFETCH CANCEL 1 url SEEK 250 OUT 350 (the same range in frames at 50p)
matches, including when only ranges of the url are queued.
the same range with different spellings, one CANCEL stops it for both.
its prefetches with
PREFETCH STATUS url and cancel each one separately.PREFETCH STATUS claims to validate URLs “as PREFETCH takes it”, but it only checks for ://. PREFETCH also rejects unknown protocols via avio_find_protocol_name(...) == nullptr, so STATUS can currently accept unsupported URLs and return an empty/incorrect result while still saying “OK”. Align the validation with parse_request().
PREFETCH CANCEL currently computes a single key and cancels only that one prefetch. With no range parameters this only cancels the whole-file prefetch (key == url), but the PR description says omitting the range should cancel every queued/running prefetch for that URL (all ranges). Consider canceling all live jobs for the URL when range() is empty.
[!NOTE]
Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
Several moderate, concrete mismatches remain between the documented AMCP commands/behavior and the implemented PREFETCH STATUS/CANCEL validation and output (including protocol checks, missing LIST command, and range-cancel semantics), which could mislead clients and produce incorrect “OK” results.
<details open>
<summary><strong>6 open issues</strong></summary>
</details>
<sub>Review effort: Lite</sub>
The original PR description is obsolete. Se the updated posted above
The original PR description is obsolete. Se the updated posted below
Approved these changes.
Fixed in the ab83c6d
[!NOTE]
Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.
<!-- ccr-overview-v2 -->
🔵 Needs a closer look
The change introduces cross-thread cancellation/preemption plus a new persistent .prefetch state with inter-process file locking, and the key correctness/portability and race-condition risks (Windows vs POSIX locking/rename semantics) require runtime validation that static review alone cannot provide.
<details open>
<summary><strong>3 open issues</strong></summary>
</details>
<details>
<summary><strong>3 resolved since last review</strong></summary>
</details>
<sub>Review effort: Lite</sub>
ronag
niklaspandersson
PREFETCH priority, cancel and status
Adds queue control to PREFETCH and a way to see what's queued, running and cached.
Commands
PREFETCH [channel[-layer]] url [SEEK/IN/LENGTH/OUT] [PRIORITY n]PRIORITYis a whole number, default 0. Higher runs first; equal priorities run in the order they were queued.PREFETCH CANCEL [channel[-layer]] url [SEEK/IN/LENGTH/OUT]202if a queued or running prefetch was cancelled,404if none matched. What's already cached stays cached.PREFETCH STATUS [CACHED] [url]STATE priority bytes size request.CACHED:RUNNINGandQUEUEDprefetches, in the order they run.sizeis-1until known.CACHED: also what earlier prefetches left in the cache, also after a restart, with priority-:DONE,FAILED,CANCELLED,PAUSED,ABORTEDorINTERRUPTED(Caspar stopped while it ran).DONEmeans available: the prefetch finished, or, for a range, the whole file's did. ADONEis never downgraded by a later failed attempt.requestis the PREFETCH parameters as sent, without the channel and PRIORITY, so a client can match a line to its request. Several spellings of the same range share one prefetch and get a line each.Cache files
cache_key):<sha256>.cache,.spacemap, and a new.prefetchwith one line per prefetched whole file or range, whichSTATUS CACHEDreads.cache_keyoption (nxt FFmpeg d2684191a7).Gotchas
.prefetchrecords, so a clip cached by playing it throughshared:doesn't show up inSTATUS CACHED, even if it's fully cached.clip.mp4?token=aandclip.mp4?token=bare separate caches, so a PLAY with a different signed token or query parameter won't find what was prefetched.Testing
Manual kit tests against a throttled local HTTP server cover priority ordering, preemption, cancel, status (live and cached, across restarts, many request spellings) and the existing size-limit and range-prefetch tests.