← Back to Pull requests

feat(ffmpeg): prefetch priority, cancelation and listing #108

Merged
niklaspandersson merged 12 files (972 lines) on Nov 20, 2026 14:08 UTC
View on GitHub
niklaspandersson niklaspandersson commented on Nov 03, 2026 18:48 UTC
Author

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]

  • PRIORITY is a whole number, default 0. Higher runs first; equal priorities run in the order they were queued.

  • Sending PREFETCH again for a queued prefetch with another priority moves it.

  • When every worker is busy, a higher priority preempts the running prefetch with the lowest, strictly lower, priority. That one is requeued at the front of its priority and resumes from what it already cached.
  • PREFETCH CANCEL [channel[-layer]] url [SEEK/IN/LENGTH/OUT]

  • Cancels the prefetch its parameters describe: without a range, the whole-file prefetch only; with a range, that range in any spelling (frames or times, SEEK/LENGTH or IN/OUT).

  • 202 if a queued or running prefetch was cancelled, 404 if none matched. What's already cached stays cached.
  • PREFETCH STATUS [CACHED] [url]

  • One line per prefetch: STATE priority bytes size request.

  • Without CACHED: RUNNING and QUEUED prefetches, in the order they run. size is -1 until known.

  • With CACHED: also what earlier prefetches left in the cache, also after a restart, with priority -: DONE, FAILED, CANCELLED, PAUSED, ABORTED or INTERRUPTED (Caspar stopped while it ran).

  • DONE means available: the prefetch finished, or, for a range, the whole file's did. A DONE is never downgraded by a later failed attempt.

  • With a url: only that url's prefetches (whole file and ranges).

  • request is 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.
  • PREFETCH "http://host/clip.mp4" IN :2.4 OUT :5
    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 :2.4 OUT :5
    

    Cache files

  • Cache files are named by the lowercase hex SHA-256 of the url (shared's cache_key): <sha256>.cache, .spacemap, and a new .prefetch with one line per prefetched whole file or range, which STATUS CACHED reads.

  • Existing caches are not migrated. Files written before this are no longer found, so each clip is fetched again on first use. The old files can be deleted.

  • Needs an FFmpeg with the shared protocol's cache_key option (nxt FFmpeg d2684191a7).
  • Gotchas

  • PLAYed clips aren't tracked. Only PREFETCH writes .prefetch records, so a clip cached by playing it through shared: doesn't show up in STATUS CACHED, even if it's fully cached.

  • Existing caches are not used. Files written before this have the old names and aren't found, so each clip is fetched again on first use. The old files can be deleted.

  • The url is the cache identity, query string included. clip.mp4?token=a and clip.mp4?token=b are separate caches, so a PLAY with a different signed token or query parameter won't find what was prefetched.

  • CANCEL matches the range, not the spelling. If two clients queued the same range with different spellings, one CANCEL stops it for both.
  • 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.

    Diff: 972 lines (+903 / -69) across 12 files Reviewer: ronag Review duration: 24199m
    All Vouch review confidence checks passed
    Residual risk 0.06 = Change Risk (0.59) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.59 · 972 lines diff across 12 files
    0.59
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.88 · 5 substantive comment(s)
    0.88
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    89%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.400
    Real PR Conversation Timeline
    36 items
    ronag ronag commented on Nov 03, 2026 18:53 UTC
    Contributor

    How do I know that a prefect has completed?

    Copilot Copilot commented on code on Nov 03, 2026 18:55 UTC
    Code Review
    src/modules/ffmpeg/producer/prefetch.cpp:1
    @@ -46,14 +47,81 @@ namespace {
     
     class prefetcher
     {
    -    std::mutex                                     mutex_;
    -    std::condition_variable                        cond_;
    -    std::deque<spl::shared_ptr<prefetch_strategy>> queue_;
    -    std::set<std::string>                          pending_; // queued or running
    -    std::vector<std::thread>                       workers_;
    -    std::atomic<bool>                              abort_{false};
    +    enum class stop_reason
    +    {
    +        none,
    +        cancel,
    +        preempt,
    +    };
    +
    +    struct job
    +    {
    +        job(spl::shared_ptr<prefetch_strategy> strategy, int priority, uint64_t seq, prefetcher* owner)
    +            : strategy(std::move(strategy))
    +            , key(this->strategy->describe())
    +            , priority(priority)
    +            , seq(seq)
    +            , owner(owner)
    +        {
    +        }

    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.

    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Nov 03, 2026 18:55 UTC
    Reviewer

    [!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>

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.png" alt="Low severity" width="62" height="18" align="texttop"></picture> Duplicate strategy key computation causes inconsistent job tracking · New

  • </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:

  • Add PRIORITY handling to PREFETCH, including reprioritisation of queued requests and preemption of lower-priority running work.

  • Add PREFETCH CANCEL to stop queued/running prefetch jobs (optionally scoped by range/key).

  • Add 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>

    niklaspandersson niklaspandersson commented on Nov 03, 2026 19:02 UTC
    Author

    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

    Copilot Copilot commented on code on Nov 13, 2026 07:51 UTC
    Code Review
    src/modules/ffmpeg/producer/prefetch.cpp:1
    @@ -66,64 +137,179 @@ class prefetcher
             }
         }
     
    -    // Returns false if the same prefetch is already queued or running.
    -    bool enqueue(spl::shared_ptr<prefetch_strategy> strategy)
    +    enqueue_result enqueue(spl::shared_ptr<prefetch_strategy> strategy, int priority)
         {
             std::lock_guard<std::mutex> lock(mutex_);
     
    -        if (!pending_.insert(strategy->describe()).second) {
    -            return false;
    +        auto key = strategy->describe();
    +        auto it  = pending_.find(key);
    +        if (it != pending_.end()) {
    +            auto& existing = it->second;
    +            if (existing->priority == priority || std::find(queue_.begin(), queue_.end(), existing) == queue_.end()) {
    +                return enqueue_result::already_pending;
    +            }
    +            existing->priority = priority;
    +            preempt();
    +            return enqueue_result::reprioritised;

    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.

    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Nov 13, 2026 07:51 UTC
    Reviewer

    [!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>

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> Requeued PREFETCH keeps old seq and breaks priority ordering · New

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.png" alt="Low severity" width="62" height="18" align="texttop"></picture> Duplicate strategy key computation causes inconsistent job tracking

  • </details>

    <sub>Review effort: Lite</sub>

    ronag ronag commented on Nov 13, 2026 07:59 UTC
    Contributor

    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

    ronag ronag commented on Nov 13, 2026 07:59 UTC
    Contributor

    Do we have a (configurable) concurrency limit on prefetching?

    niklaspandersson niklaspandersson commented on Nov 13, 2026 08:08 UTC
    Author

    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

  • 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
  • niklaspandersson niklaspandersson commented on Nov 13, 2026 08:11 UTC
    Author

    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

    ronag ronag commented on Nov 13, 2026 08:17 UTC
    Contributor

    > 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

    ronag ronag approved these changes on Nov 13, 2026 08:18 UTC
    Approved Reviewer

    Approved these changes.

    ronag ronag commented on Nov 13, 2026 08:19 UTC
    Contributor

    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?

    niklaspandersson niklaspandersson commented on Nov 13, 2026 08:57 UTC
    Author

    Could we add a

    >
    > PREFETCH STATUS {url}
    > 

    > That outputs the same line as PREFETCH LIST but for on specific entry?

    > Maybe PREFETCH STATUS is the same as PREFETCH LIST and PREFETCH STATUS {url} is one entry?

  • Looks good to me. Bear in mind that you still might get multiple rows in the result if you've done something like this

  •     PEFETCH {url} IN 120. OUT 250.
        PREFETCH {url}
        

  • Should we skip LIST altogether and just go with the broader STATUS?
  • ronag ronag commented on Nov 13, 2026 08:58 UTC
    Contributor

    Should we skip LIST altogether and just go with the broader STATUS?

    yes

    ronag ronag commented on Nov 13, 2026 08:59 UTC
    Contributor

    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?

    niklaspandersson niklaspandersson commented on Nov 13, 2026 09:14 UTC
    Author

    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.
  • ronag ronag commented on Nov 13, 2026 09:20 UTC
    Contributor

    > 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

    niklaspandersson niklaspandersson commented on Nov 13, 2026 09:24 UTC
    Author

    > 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

    ronag ronag commented on Nov 13, 2026 09:33 UTC
    Contributor

    > > 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?

    niklaspandersson niklaspandersson commented on Nov 13, 2026 09:33 UTC
    Author

    > > 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

    niklaspandersson niklaspandersson commented on Nov 13, 2026 09:54 UTC
    Author

    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?

    ronag ronag commented on Nov 13, 2026 11:50 UTC
    Contributor

    > 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?

    Copilot Copilot commented on code on Nov 13, 2026 12:08 UTC
    Code Review
    src/modules/ffmpeg/ffmpeg.cpp:1
    @@ -119,6 +119,9 @@ void init(const core::module_dependencies& dependencies)
         // Not a channel command, see prefetch_command. A channel command named PREFETCH would take over
         // "PREFETCH 1 url".
         dependencies.command_repository->register_command(L"Media Commands", L"PREFETCH", prefetch_command, 1);
    +    dependencies.command_repository->register_command(
    +        L"Media Commands", L"PREFETCH CANCEL", prefetch_cancel_command, 1);
    +    dependencies.command_repository->register_command(L"Media Commands", L"PREFETCH STATUS", prefetch_status_command, 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.

    Copilot Copilot commented on code on Nov 13, 2026 12:08 UTC
    Code Review
    src/modules/ffmpeg/producer/prefetch.cpp:1
    @@ -178,38 +428,178 @@ std::wstring prefetch_command(protocol::amcp::command_context& ctx)
         // Local files have nothing to cache, and only real protocols go through the shared protocol.
         if (!resolved || !boost::contains(resolved->path, L"://") ||
             avio_find_protocol_name(u8(resolved->path).c_str()) == nullptr) {
    -        CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] Cannot prefetch, not a supported url";
    -        return L"403 PREFETCH FAILED\r\n";
    +        CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] " << command << L": not a supported url";
    +        return {};
         }
     
         auto has_range = contains_param(L"SEEK", params) || contains_param(L"IN", params) ||
                          contains_param(L"LENGTH", params) || contains_param(L"OUT", params);
         std::optional<play_range> range;
    +    int                       priority = 0;
         try {
             if (has_range) {
                 range = parse_play_range(params, format_desc ? &*format_desc : nullptr);
             }
    +        priority = get_param(L"PRIORITY", params, 0);
         } catch (user_error& e) {
             auto msg = boost::get_error_info<msg_info_t>(e);
    -        CASPAR_LOG(error) << L"ffmpeg[" << resolved->path << L"] Cannot prefetch, "
    -                          << (msg ? u16(*msg) : L"invalid SEEK, IN, LENGTH or OUT");
    -        return L"403 PREFETCH FAILED\r\n";
    +        CASPAR_LOG(error) << L"ffmpeg[" << resolved->path << L"] " << command << L": "
    +                          << (msg ? u16(*msg) : L"invalid SEEK, IN, LENGTH, OUT or PRIORITY");
    +        return {};
         }
     
    -    auto strategy =
    -        range ? create_range_prefetch(u8(resolved->path), *range) : create_whole_file_prefetch(u8(resolved->path));
    +    auto url = u8(resolved->path);
    +    return prefetch_request{range ? create_range_prefetch(url, *range) : create_whole_file_prefetch(url),
    +                            priority,
    +                            request_text(ctx.parameters)};
    +}
    +
    +} // namespace
    +
    +std::wstring prefetch_command(protocol::amcp::command_context& ctx)
    +{
    +    auto request = parse_request(L"PREFETCH", ctx);
    +    if (!request) {
    +        return L"403 PREFETCH FAILED\r\n";
    +    }
     
         std::lock_guard<std::mutex> lock(instance_mutex);
         if (!instance) {
             instance = std::make_unique<prefetcher>();
         }
    -    if (!instance->enqueue(strategy)) {
    -        CASPAR_LOG(info) << L"ffmpeg[" << u16(strategy->describe()) << L"] Prefetch already in progress";
    +    const auto name = u16(request->strategy->describe());
    +    switch (instance->enqueue(request->strategy, request->request, request->priority)) {
    +        case prefetcher::enqueue_result::queued:
    +            break;
    +        case prefetcher::enqueue_result::reprioritised:
    +            CASPAR_LOG(info) << L"ffmpeg[" << name << L"] Prefetch moved to priority " << request->priority;
    +            break;
    +        case prefetcher::enqueue_result::already_pending:
    +            CASPAR_LOG(info) << L"ffmpeg[" << name << L"] Prefetch already in progress";
    +            break;
         }
     
         return L"202 PREFETCH OK\r\n";
     }
     
    +std::wstring prefetch_status_command(protocol::amcp::command_context& ctx)
    +{
    +    auto params = ctx.parameters;
    +    auto cached = !params.empty() && boost::iequals(params.at(0), L"CACHED");
    +    if (cached) {
    +        params.erase(params.begin());
    +    }
    +
    +    std::optional<std::string> url;
    +    if (!params.empty()) {
    +        // As PREFETCH takes it, so that "shared:" doesn't matter.
    +        auto resolved = resolve_path(params.at(0));
    +        if (!resolved || !boost::contains(resolved->path, L"://")) {
    +            CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] PREFETCH STATUS: not a supported url";
    +            return L"403 PREFETCH STATUS FAILED\r\n";
    +        }
    +        url = u8(resolved->path);
    +    }
    +
    +    std::vector<prefetcher::list_entry> live;
    +    {
    +        std::lock_guard<std::mutex> lock(instance_mutex);
    +        if (instance) {
    +            live = instance->list(url);
    +        }
    +    }
    +
    +    std::wstringstream reply;
    +    reply << L"200 PREFETCH STATUS OK\r\n";
    +    // A line per spelling of the request, so that each client finds the one it sent.
    +    auto line = [&](const wchar_t*                  state,
    +                    const std::wstring&             priority,
    +                    int64_t                         bytes,
    +                    int64_t                         size,
    +                    const std::vector<std::string>& requests) {
    +        for (const auto& request : requests) {
    +            reply << state << L" " << priority << L" " << bytes << L" " << size << L" " << u16(request) << L"\r\n";
    +        }

    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.

    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Nov 13, 2026 12:08 UTC
    Reviewer

    [!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>

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> PREFETCH LIST is documented but not registered as a command · New

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> PREFETCH STATUS returns raw request text instead of canonical key · New

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> Requeued PREFETCH keeps old seq and breaks priority ordering

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.png" alt="Low severity" width="62" height="18" align="texttop"></picture> Duplicate strategy key computation causes inconsistent job tracking

  • </details>

    <sub>Review effort: Lite</sub>

    niklaspandersson niklaspandersson commented on Nov 13, 2026 13:26 UTC
    Author

    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.

  • PRIORITY isn't part of a request's identity, so it isn't echoed. If the

  • same request is sent again with a new PRIORITY, the job is moved in the
    queue and no line is added.
  • Different spellings of the same range (1 url IN 120 OUT 250,

  • url SEEK :2.4 LENGTH :2.6) are still one job. Each spelling gets its
    own line, live and CACHED, showing that job's state.
  • DONE means the range is available. A range is also DONE when the whole

  • file has been prefetched. Its bytes and size are then the whole file's.
  • On disk, each <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)

  • 202 when a queued or running prefetch was cancelled. 404 when none

  • matches, including when only ranges of the url are queued.
  • What's already cached stays cached.

  • Cancelling matches the range, not the spelling. If two clients queued

  • the same range with different spellings, one CANCEL stops it for both.
  • There is no "cancel everything for a url". A client can list

  • its prefetches with PREFETCH STATUS url and cancel each one separately.

    Copilot Copilot commented on code on Nov 13, 2026 13:27 UTC
    Code Review
    src/modules/ffmpeg/producer/prefetch.cpp:1
    @@ -178,38 +428,177 @@ std::wstring prefetch_command(protocol::amcp::command_context& ctx)
         // Local files have nothing to cache, and only real protocols go through the shared protocol.
         if (!resolved || !boost::contains(resolved->path, L"://") ||
             avio_find_protocol_name(u8(resolved->path).c_str()) == nullptr) {
    -        CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] Cannot prefetch, not a supported url";
    -        return L"403 PREFETCH FAILED\r\n";
    +        CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] " << command << L": not a supported url";
    +        return {};
         }
     
         auto has_range = contains_param(L"SEEK", params) || contains_param(L"IN", params) ||
                          contains_param(L"LENGTH", params) || contains_param(L"OUT", params);
         std::optional<play_range> range;
    +    int                       priority = 0;
         try {
             if (has_range) {
                 range = parse_play_range(params, format_desc ? &*format_desc : nullptr);
             }
    +        priority = get_param(L"PRIORITY", params, 0);
         } catch (user_error& e) {
             auto msg = boost::get_error_info<msg_info_t>(e);
    -        CASPAR_LOG(error) << L"ffmpeg[" << resolved->path << L"] Cannot prefetch, "
    -                          << (msg ? u16(*msg) : L"invalid SEEK, IN, LENGTH or OUT");
    -        return L"403 PREFETCH FAILED\r\n";
    +        CASPAR_LOG(error) << L"ffmpeg[" << resolved->path << L"] " << command << L": "
    +                          << (msg ? u16(*msg) : L"invalid SEEK, IN, LENGTH, OUT or PRIORITY");
    +        return {};
         }
     
    -    auto strategy =
    -        range ? create_range_prefetch(u8(resolved->path), *range) : create_whole_file_prefetch(u8(resolved->path));
    +    auto url = u8(resolved->path);
    +    return prefetch_request{
    +        range ? create_range_prefetch(url, *range) : create_whole_file_prefetch(url), priority, request_text(params)};
    +}
    +
    +} // namespace
    +
    +std::wstring prefetch_command(protocol::amcp::command_context& ctx)
    +{
    +    auto request = parse_request(L"PREFETCH", ctx);
    +    if (!request) {
    +        return L"403 PREFETCH FAILED\r\n";
    +    }
     
         std::lock_guard<std::mutex> lock(instance_mutex);
         if (!instance) {
             instance = std::make_unique<prefetcher>();
         }
    -    if (!instance->enqueue(strategy)) {
    -        CASPAR_LOG(info) << L"ffmpeg[" << u16(strategy->describe()) << L"] Prefetch already in progress";
    +    const auto name = u16(request->strategy->describe());
    +    switch (instance->enqueue(request->strategy, request->request, request->priority)) {
    +        case prefetcher::enqueue_result::queued:
    +            break;
    +        case prefetcher::enqueue_result::reprioritised:
    +            CASPAR_LOG(info) << L"ffmpeg[" << name << L"] Prefetch moved to priority " << request->priority;
    +            break;
    +        case prefetcher::enqueue_result::already_pending:
    +            CASPAR_LOG(info) << L"ffmpeg[" << name << L"] Prefetch already in progress";
    +            break;
         }
     
         return L"202 PREFETCH OK\r\n";
     }
     
    +std::wstring prefetch_status_command(protocol::amcp::command_context& ctx)
    +{
    +    auto params = ctx.parameters;
    +    auto cached = !params.empty() && boost::iequals(params.at(0), L"CACHED");
    +    if (cached) {
    +        params.erase(params.begin());
    +    }
    +
    +    std::optional<std::string> url;
    +    if (!params.empty()) {
    +        // As PREFETCH takes it, so that "shared:" doesn't matter.
    +        auto resolved = resolve_path(params.at(0));
    +        if (!resolved || !boost::contains(resolved->path, L"://")) {
    +            CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] PREFETCH STATUS: not a supported url";
    +            return L"403 PREFETCH STATUS FAILED\r\n";
    +        }

    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().

    Copilot Copilot commented on code on Nov 13, 2026 13:27 UTC
    Code Review
    src/modules/ffmpeg/producer/prefetch.cpp:1
    @@ -178,38 +428,177 @@ std::wstring prefetch_command(protocol::amcp::command_context& ctx)
         // Local files have nothing to cache, and only real protocols go through the shared protocol.
         if (!resolved || !boost::contains(resolved->path, L"://") ||
             avio_find_protocol_name(u8(resolved->path).c_str()) == nullptr) {
    -        CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] Cannot prefetch, not a supported url";
    -        return L"403 PREFETCH FAILED\r\n";
    +        CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] " << command << L": not a supported url";
    +        return {};
         }
     
         auto has_range = contains_param(L"SEEK", params) || contains_param(L"IN", params) ||
                          contains_param(L"LENGTH", params) || contains_param(L"OUT", params);
         std::optional<play_range> range;
    +    int                       priority = 0;
         try {
             if (has_range) {
                 range = parse_play_range(params, format_desc ? &*format_desc : nullptr);
             }
    +        priority = get_param(L"PRIORITY", params, 0);
         } catch (user_error& e) {
             auto msg = boost::get_error_info<msg_info_t>(e);
    -        CASPAR_LOG(error) << L"ffmpeg[" << resolved->path << L"] Cannot prefetch, "
    -                          << (msg ? u16(*msg) : L"invalid SEEK, IN, LENGTH or OUT");
    -        return L"403 PREFETCH FAILED\r\n";
    +        CASPAR_LOG(error) << L"ffmpeg[" << resolved->path << L"] " << command << L": "
    +                          << (msg ? u16(*msg) : L"invalid SEEK, IN, LENGTH, OUT or PRIORITY");
    +        return {};
         }
     
    -    auto strategy =
    -        range ? create_range_prefetch(u8(resolved->path), *range) : create_whole_file_prefetch(u8(resolved->path));
    +    auto url = u8(resolved->path);
    +    return prefetch_request{
    +        range ? create_range_prefetch(url, *range) : create_whole_file_prefetch(url), priority, request_text(params)};
    +}
    +
    +} // namespace
    +
    +std::wstring prefetch_command(protocol::amcp::command_context& ctx)
    +{
    +    auto request = parse_request(L"PREFETCH", ctx);
    +    if (!request) {
    +        return L"403 PREFETCH FAILED\r\n";
    +    }
     
         std::lock_guard<std::mutex> lock(instance_mutex);
         if (!instance) {
             instance = std::make_unique<prefetcher>();
         }
    -    if (!instance->enqueue(strategy)) {
    -        CASPAR_LOG(info) << L"ffmpeg[" << u16(strategy->describe()) << L"] Prefetch already in progress";
    +    const auto name = u16(request->strategy->describe());
    +    switch (instance->enqueue(request->strategy, request->request, request->priority)) {
    +        case prefetcher::enqueue_result::queued:
    +            break;
    +        case prefetcher::enqueue_result::reprioritised:
    +            CASPAR_LOG(info) << L"ffmpeg[" << name << L"] Prefetch moved to priority " << request->priority;
    +            break;
    +        case prefetcher::enqueue_result::already_pending:
    +            CASPAR_LOG(info) << L"ffmpeg[" << name << L"] Prefetch already in progress";
    +            break;
         }
     
         return L"202 PREFETCH OK\r\n";
     }
     
    +std::wstring prefetch_status_command(protocol::amcp::command_context& ctx)
    +{
    +    auto params = ctx.parameters;
    +    auto cached = !params.empty() && boost::iequals(params.at(0), L"CACHED");
    +    if (cached) {
    +        params.erase(params.begin());
    +    }
    +
    +    std::optional<std::string> url;
    +    if (!params.empty()) {
    +        // As PREFETCH takes it, so that "shared:" doesn't matter.
    +        auto resolved = resolve_path(params.at(0));
    +        if (!resolved || !boost::contains(resolved->path, L"://")) {
    +            CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] PREFETCH STATUS: not a supported url";
    +            return L"403 PREFETCH STATUS FAILED\r\n";
    +        }
    +        url = u8(resolved->path);
    +    }
    +
    +    std::vector<prefetcher::list_entry> live;
    +    {
    +        std::lock_guard<std::mutex> lock(instance_mutex);
    +        if (instance) {
    +            live = instance->list(url);
    +        }
    +    }
    +
    +    std::wstringstream reply;
    +    reply << L"200 PREFETCH STATUS OK\r\n";
    +    // A line per spelling of the request, so that each client finds the one it sent.
    +    auto line = [&](const wchar_t*                  state,
    +                    const std::wstring&             priority,
    +                    int64_t                         bytes,
    +                    int64_t                         size,
    +                    const std::vector<std::string>& requests) {
    +        for (const auto& request : requests) {
    +            reply << state << L" " << priority << L" " << bytes << L" " << size << L" " << u16(request) << L"\r\n";
    +        }
    +    };
    +    for (const auto& e : live) {
    +        line(e.state, std::to_wstring(e.priority), e.bytes, e.size, e.requests);
    +    }
    +
    +    if (cached) {
    +        auto records = read_cached_prefetches(url);
    +        std::sort(records.begin(), records.end(), [](const auto& a, const auto& b) {
    +            return a.url != b.url ? a.url < b.url
    +                                  : describe_prefetch(a.url, a.range) < describe_prefetch(b.url, b.range);
    +        });
    +
    +        std::map<std::string, const prefetch_record*> whole_done;
    +        for (const auto& r : records) {
    +            if (!r.range && r.state == prefetch_state::done) {
    +                whole_done[r.url] = &r;
    +            }
    +        }
    +
    +        for (const auto& r : records) {
    +            const auto key = describe_prefetch(r.url, r.range);
    +            // DONE means available: any range of a url whose whole file is cached is too, whatever its own
    +            // prefetch did. It then shows the whole file's bytes.
    +            auto whole = r.range && r.state != prefetch_state::done ? whole_done.find(r.url) : whole_done.end();
    +            if (r.state == prefetch_state::done || whole != whole_done.end()) {
    +                const auto& done = whole != whole_done.end() ? *whole->second : r;
    +                line(L"DONE", L"-", done.bytes, done.size, r.requests);
    +                continue;
    +            }
    +            // Otherwise a live job is the current state of its request.
    +            if (std::any_of(live.begin(), live.end(), [&](const prefetcher::list_entry& e) { return e.key == key; })) {
    +                continue;
    +            }
    +
    +            const wchar_t* state = L"";
    +            switch (r.state) {
    +                case prefetch_state::done:
    +                    break;
    +                case prefetch_state::running:
    +                    state = L"INTERRUPTED"; // no live job: Caspar stopped without recording its end
    +                    break;
    +                case prefetch_state::failed:
    +                    state = L"FAILED";
    +                    break;
    +                case prefetch_state::cancelled:
    +                    state = L"CANCELLED";
    +                    break;
    +                case prefetch_state::paused:
    +                    state = L"PAUSED";
    +                    break;
    +                case prefetch_state::aborted:
    +                    state = L"ABORTED";
    +                    break;
    +            }
    +            line(state, L"-", r.bytes, r.size, r.requests);
    +        }
    +    }
    +
    +    reply << L"\r\n";
    +    return reply.str();
    +}
    +
    +std::wstring prefetch_cancel_command(protocol::amcp::command_context& ctx)
    +{
    +    auto request = parse_request(L"PREFETCH CANCEL", ctx);
    +    if (!request) {
    +        return L"403 PREFETCH CANCEL FAILED\r\n";
    +    }
    +
    +    const auto key = request->strategy->describe();
    +
    +    std::lock_guard<std::mutex> lock(instance_mutex);
    +    if (!instance || !instance->cancel(key)) {
    +        CASPAR_LOG(info) << L"ffmpeg[" << u16(key) << L"] No prefetch to cancel";
    +        return L"404 PREFETCH CANCEL FAILED\r\n";
    +    }
    +
    +    return L"202 PREFETCH CANCEL OK\r\n";
    +}

    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.

    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Nov 13, 2026 13:27 UTC
    Reviewer

    [!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>

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> STATUS accepts unsupported URLs instead of matching PREFETCH validation · New

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> CANCEL only stops one range instead of all jobs for the URL · New

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> PREFETCH STATUS returns raw request text instead of canonical key

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> PREFETCH LIST is documented but not registered as a command

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> Requeued PREFETCH keeps old seq and breaks priority ordering

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.png" alt="Low severity" width="62" height="18" align="texttop"></picture> Duplicate strategy key computation causes inconsistent job tracking

  • </details>

    <sub>Review effort: Lite</sub>

    niklaspandersson niklaspandersson commented on code on Nov 13, 2026 13:29 UTC
    Code Review
    src/modules/ffmpeg/producer/prefetch.cpp:1
    @@ -178,38 +428,177 @@ std::wstring prefetch_command(protocol::amcp::command_context& ctx)
         // Local files have nothing to cache, and only real protocols go through the shared protocol.
         if (!resolved || !boost::contains(resolved->path, L"://") ||
             avio_find_protocol_name(u8(resolved->path).c_str()) == nullptr) {
    -        CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] Cannot prefetch, not a supported url";
    -        return L"403 PREFETCH FAILED\r\n";
    +        CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] " << command << L": not a supported url";
    +        return {};
         }
     
         auto has_range = contains_param(L"SEEK", params) || contains_param(L"IN", params) ||
                          contains_param(L"LENGTH", params) || contains_param(L"OUT", params);
         std::optional<play_range> range;
    +    int                       priority = 0;
         try {
             if (has_range) {
                 range = parse_play_range(params, format_desc ? &*format_desc : nullptr);
             }
    +        priority = get_param(L"PRIORITY", params, 0);
         } catch (user_error& e) {
             auto msg = boost::get_error_info<msg_info_t>(e);
    -        CASPAR_LOG(error) << L"ffmpeg[" << resolved->path << L"] Cannot prefetch, "
    -                          << (msg ? u16(*msg) : L"invalid SEEK, IN, LENGTH or OUT");
    -        return L"403 PREFETCH FAILED\r\n";
    +        CASPAR_LOG(error) << L"ffmpeg[" << resolved->path << L"] " << command << L": "
    +                          << (msg ? u16(*msg) : L"invalid SEEK, IN, LENGTH, OUT or PRIORITY");
    +        return {};
         }
     
    -    auto strategy =
    -        range ? create_range_prefetch(u8(resolved->path), *range) : create_whole_file_prefetch(u8(resolved->path));
    +    auto url = u8(resolved->path);
    +    return prefetch_request{
    +        range ? create_range_prefetch(url, *range) : create_whole_file_prefetch(url), priority, request_text(params)};
    +}
    +
    +} // namespace
    +
    +std::wstring prefetch_command(protocol::amcp::command_context& ctx)
    +{
    +    auto request = parse_request(L"PREFETCH", ctx);
    +    if (!request) {
    +        return L"403 PREFETCH FAILED\r\n";
    +    }
     
         std::lock_guard<std::mutex> lock(instance_mutex);
         if (!instance) {
             instance = std::make_unique<prefetcher>();
         }
    -    if (!instance->enqueue(strategy)) {
    -        CASPAR_LOG(info) << L"ffmpeg[" << u16(strategy->describe()) << L"] Prefetch already in progress";
    +    const auto name = u16(request->strategy->describe());
    +    switch (instance->enqueue(request->strategy, request->request, request->priority)) {
    +        case prefetcher::enqueue_result::queued:
    +            break;
    +        case prefetcher::enqueue_result::reprioritised:
    +            CASPAR_LOG(info) << L"ffmpeg[" << name << L"] Prefetch moved to priority " << request->priority;
    +            break;
    +        case prefetcher::enqueue_result::already_pending:
    +            CASPAR_LOG(info) << L"ffmpeg[" << name << L"] Prefetch already in progress";
    +            break;
         }
     
         return L"202 PREFETCH OK\r\n";
     }
     
    +std::wstring prefetch_status_command(protocol::amcp::command_context& ctx)
    +{
    +    auto params = ctx.parameters;
    +    auto cached = !params.empty() && boost::iequals(params.at(0), L"CACHED");
    +    if (cached) {
    +        params.erase(params.begin());
    +    }
    +
    +    std::optional<std::string> url;
    +    if (!params.empty()) {
    +        // As PREFETCH takes it, so that "shared:" doesn't matter.
    +        auto resolved = resolve_path(params.at(0));
    +        if (!resolved || !boost::contains(resolved->path, L"://")) {
    +            CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] PREFETCH STATUS: not a supported url";
    +            return L"403 PREFETCH STATUS FAILED\r\n";
    +        }
    +        url = u8(resolved->path);
    +    }
    +
    +    std::vector<prefetcher::list_entry> live;
    +    {
    +        std::lock_guard<std::mutex> lock(instance_mutex);
    +        if (instance) {
    +            live = instance->list(url);
    +        }
    +    }
    +
    +    std::wstringstream reply;
    +    reply << L"200 PREFETCH STATUS OK\r\n";
    +    // A line per spelling of the request, so that each client finds the one it sent.
    +    auto line = [&](const wchar_t*                  state,
    +                    const std::wstring&             priority,
    +                    int64_t                         bytes,
    +                    int64_t                         size,
    +                    const std::vector<std::string>& requests) {
    +        for (const auto& request : requests) {
    +            reply << state << L" " << priority << L" " << bytes << L" " << size << L" " << u16(request) << L"\r\n";
    +        }
    +    };
    +    for (const auto& e : live) {
    +        line(e.state, std::to_wstring(e.priority), e.bytes, e.size, e.requests);
    +    }
    +
    +    if (cached) {
    +        auto records = read_cached_prefetches(url);
    +        std::sort(records.begin(), records.end(), [](const auto& a, const auto& b) {
    +            return a.url != b.url ? a.url < b.url
    +                                  : describe_prefetch(a.url, a.range) < describe_prefetch(b.url, b.range);
    +        });
    +
    +        std::map<std::string, const prefetch_record*> whole_done;
    +        for (const auto& r : records) {
    +            if (!r.range && r.state == prefetch_state::done) {
    +                whole_done[r.url] = &r;
    +            }
    +        }
    +
    +        for (const auto& r : records) {
    +            const auto key = describe_prefetch(r.url, r.range);
    +            // DONE means available: any range of a url whose whole file is cached is too, whatever its own
    +            // prefetch did. It then shows the whole file's bytes.
    +            auto whole = r.range && r.state != prefetch_state::done ? whole_done.find(r.url) : whole_done.end();
    +            if (r.state == prefetch_state::done || whole != whole_done.end()) {
    +                const auto& done = whole != whole_done.end() ? *whole->second : r;
    +                line(L"DONE", L"-", done.bytes, done.size, r.requests);
    +                continue;
    +            }
    +            // Otherwise a live job is the current state of its request.
    +            if (std::any_of(live.begin(), live.end(), [&](const prefetcher::list_entry& e) { return e.key == key; })) {
    +                continue;
    +            }
    +
    +            const wchar_t* state = L"";
    +            switch (r.state) {
    +                case prefetch_state::done:
    +                    break;
    +                case prefetch_state::running:
    +                    state = L"INTERRUPTED"; // no live job: Caspar stopped without recording its end
    +                    break;
    +                case prefetch_state::failed:
    +                    state = L"FAILED";
    +                    break;
    +                case prefetch_state::cancelled:
    +                    state = L"CANCELLED";
    +                    break;
    +                case prefetch_state::paused:
    +                    state = L"PAUSED";
    +                    break;
    +                case prefetch_state::aborted:
    +                    state = L"ABORTED";
    +                    break;
    +            }
    +            line(state, L"-", r.bytes, r.size, r.requests);
    +        }
    +    }
    +
    +    reply << L"\r\n";
    +    return reply.str();
    +}
    +
    +std::wstring prefetch_cancel_command(protocol::amcp::command_context& ctx)
    +{
    +    auto request = parse_request(L"PREFETCH CANCEL", ctx);
    +    if (!request) {
    +        return L"403 PREFETCH CANCEL FAILED\r\n";
    +    }
    +
    +    const auto key = request->strategy->describe();
    +
    +    std::lock_guard<std::mutex> lock(instance_mutex);
    +    if (!instance || !instance->cancel(key)) {
    +        CASPAR_LOG(info) << L"ffmpeg[" << u16(key) << L"] No prefetch to cancel";
    +        return L"404 PREFETCH CANCEL FAILED\r\n";
    +    }
    +
    +    return L"202 PREFETCH CANCEL OK\r\n";
    +}

    The original PR description is obsolete. Se the updated posted above

    niklaspandersson niklaspandersson commented on code on Nov 13, 2026 13:32 UTC
    Code Review
    src/modules/ffmpeg/producer/prefetch.cpp:1
    @@ -178,38 +428,178 @@ std::wstring prefetch_command(protocol::amcp::command_context& ctx)
         // Local files have nothing to cache, and only real protocols go through the shared protocol.
         if (!resolved || !boost::contains(resolved->path, L"://") ||
             avio_find_protocol_name(u8(resolved->path).c_str()) == nullptr) {
    -        CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] Cannot prefetch, not a supported url";
    -        return L"403 PREFETCH FAILED\r\n";
    +        CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] " << command << L": not a supported url";
    +        return {};
         }
     
         auto has_range = contains_param(L"SEEK", params) || contains_param(L"IN", params) ||
                          contains_param(L"LENGTH", params) || contains_param(L"OUT", params);
         std::optional<play_range> range;
    +    int                       priority = 0;
         try {
             if (has_range) {
                 range = parse_play_range(params, format_desc ? &*format_desc : nullptr);
             }
    +        priority = get_param(L"PRIORITY", params, 0);
         } catch (user_error& e) {
             auto msg = boost::get_error_info<msg_info_t>(e);
    -        CASPAR_LOG(error) << L"ffmpeg[" << resolved->path << L"] Cannot prefetch, "
    -                          << (msg ? u16(*msg) : L"invalid SEEK, IN, LENGTH or OUT");
    -        return L"403 PREFETCH FAILED\r\n";
    +        CASPAR_LOG(error) << L"ffmpeg[" << resolved->path << L"] " << command << L": "
    +                          << (msg ? u16(*msg) : L"invalid SEEK, IN, LENGTH, OUT or PRIORITY");
    +        return {};
         }
     
    -    auto strategy =
    -        range ? create_range_prefetch(u8(resolved->path), *range) : create_whole_file_prefetch(u8(resolved->path));
    +    auto url = u8(resolved->path);
    +    return prefetch_request{range ? create_range_prefetch(url, *range) : create_whole_file_prefetch(url),
    +                            priority,
    +                            request_text(ctx.parameters)};
    +}
    +
    +} // namespace
    +
    +std::wstring prefetch_command(protocol::amcp::command_context& ctx)
    +{
    +    auto request = parse_request(L"PREFETCH", ctx);
    +    if (!request) {
    +        return L"403 PREFETCH FAILED\r\n";
    +    }
     
         std::lock_guard<std::mutex> lock(instance_mutex);
         if (!instance) {
             instance = std::make_unique<prefetcher>();
         }
    -    if (!instance->enqueue(strategy)) {
    -        CASPAR_LOG(info) << L"ffmpeg[" << u16(strategy->describe()) << L"] Prefetch already in progress";
    +    const auto name = u16(request->strategy->describe());
    +    switch (instance->enqueue(request->strategy, request->request, request->priority)) {
    +        case prefetcher::enqueue_result::queued:
    +            break;
    +        case prefetcher::enqueue_result::reprioritised:
    +            CASPAR_LOG(info) << L"ffmpeg[" << name << L"] Prefetch moved to priority " << request->priority;
    +            break;
    +        case prefetcher::enqueue_result::already_pending:
    +            CASPAR_LOG(info) << L"ffmpeg[" << name << L"] Prefetch already in progress";
    +            break;
         }
     
         return L"202 PREFETCH OK\r\n";
     }
     
    +std::wstring prefetch_status_command(protocol::amcp::command_context& ctx)
    +{
    +    auto params = ctx.parameters;
    +    auto cached = !params.empty() && boost::iequals(params.at(0), L"CACHED");
    +    if (cached) {
    +        params.erase(params.begin());
    +    }
    +
    +    std::optional<std::string> url;
    +    if (!params.empty()) {
    +        // As PREFETCH takes it, so that "shared:" doesn't matter.
    +        auto resolved = resolve_path(params.at(0));
    +        if (!resolved || !boost::contains(resolved->path, L"://")) {
    +            CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] PREFETCH STATUS: not a supported url";
    +            return L"403 PREFETCH STATUS FAILED\r\n";
    +        }
    +        url = u8(resolved->path);
    +    }
    +
    +    std::vector<prefetcher::list_entry> live;
    +    {
    +        std::lock_guard<std::mutex> lock(instance_mutex);
    +        if (instance) {
    +            live = instance->list(url);
    +        }
    +    }
    +
    +    std::wstringstream reply;
    +    reply << L"200 PREFETCH STATUS OK\r\n";
    +    // A line per spelling of the request, so that each client finds the one it sent.
    +    auto line = [&](const wchar_t*                  state,
    +                    const std::wstring&             priority,
    +                    int64_t                         bytes,
    +                    int64_t                         size,
    +                    const std::vector<std::string>& requests) {
    +        for (const auto& request : requests) {
    +            reply << state << L" " << priority << L" " << bytes << L" " << size << L" " << u16(request) << L"\r\n";
    +        }

    The original PR description is obsolete. Se the updated posted below

    ronag ronag approved these changes on Nov 13, 2026 13:34 UTC
    Approved Reviewer

    Approved these changes.

    niklaspandersson niklaspandersson commented on code on Nov 13, 2026 13:36 UTC
    Code Review
    src/modules/ffmpeg/producer/prefetch.cpp:1
    @@ -178,38 +428,177 @@ std::wstring prefetch_command(protocol::amcp::command_context& ctx)
         // Local files have nothing to cache, and only real protocols go through the shared protocol.
         if (!resolved || !boost::contains(resolved->path, L"://") ||
             avio_find_protocol_name(u8(resolved->path).c_str()) == nullptr) {
    -        CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] Cannot prefetch, not a supported url";
    -        return L"403 PREFETCH FAILED\r\n";
    +        CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] " << command << L": not a supported url";
    +        return {};
         }
     
         auto has_range = contains_param(L"SEEK", params) || contains_param(L"IN", params) ||
                          contains_param(L"LENGTH", params) || contains_param(L"OUT", params);
         std::optional<play_range> range;
    +    int                       priority = 0;
         try {
             if (has_range) {
                 range = parse_play_range(params, format_desc ? &*format_desc : nullptr);
             }
    +        priority = get_param(L"PRIORITY", params, 0);
         } catch (user_error& e) {
             auto msg = boost::get_error_info<msg_info_t>(e);
    -        CASPAR_LOG(error) << L"ffmpeg[" << resolved->path << L"] Cannot prefetch, "
    -                          << (msg ? u16(*msg) : L"invalid SEEK, IN, LENGTH or OUT");
    -        return L"403 PREFETCH FAILED\r\n";
    +        CASPAR_LOG(error) << L"ffmpeg[" << resolved->path << L"] " << command << L": "
    +                          << (msg ? u16(*msg) : L"invalid SEEK, IN, LENGTH, OUT or PRIORITY");
    +        return {};
         }
     
    -    auto strategy =
    -        range ? create_range_prefetch(u8(resolved->path), *range) : create_whole_file_prefetch(u8(resolved->path));
    +    auto url = u8(resolved->path);
    +    return prefetch_request{
    +        range ? create_range_prefetch(url, *range) : create_whole_file_prefetch(url), priority, request_text(params)};
    +}
    +
    +} // namespace
    +
    +std::wstring prefetch_command(protocol::amcp::command_context& ctx)
    +{
    +    auto request = parse_request(L"PREFETCH", ctx);
    +    if (!request) {
    +        return L"403 PREFETCH FAILED\r\n";
    +    }
     
         std::lock_guard<std::mutex> lock(instance_mutex);
         if (!instance) {
             instance = std::make_unique<prefetcher>();
         }
    -    if (!instance->enqueue(strategy)) {
    -        CASPAR_LOG(info) << L"ffmpeg[" << u16(strategy->describe()) << L"] Prefetch already in progress";
    +    const auto name = u16(request->strategy->describe());
    +    switch (instance->enqueue(request->strategy, request->request, request->priority)) {
    +        case prefetcher::enqueue_result::queued:
    +            break;
    +        case prefetcher::enqueue_result::reprioritised:
    +            CASPAR_LOG(info) << L"ffmpeg[" << name << L"] Prefetch moved to priority " << request->priority;
    +            break;
    +        case prefetcher::enqueue_result::already_pending:
    +            CASPAR_LOG(info) << L"ffmpeg[" << name << L"] Prefetch already in progress";
    +            break;
         }
     
         return L"202 PREFETCH OK\r\n";
     }
     
    +std::wstring prefetch_status_command(protocol::amcp::command_context& ctx)
    +{
    +    auto params = ctx.parameters;
    +    auto cached = !params.empty() && boost::iequals(params.at(0), L"CACHED");
    +    if (cached) {
    +        params.erase(params.begin());
    +    }
    +
    +    std::optional<std::string> url;
    +    if (!params.empty()) {
    +        // As PREFETCH takes it, so that "shared:" doesn't matter.
    +        auto resolved = resolve_path(params.at(0));
    +        if (!resolved || !boost::contains(resolved->path, L"://")) {
    +            CASPAR_LOG(error) << L"ffmpeg[" << params.at(0) << L"] PREFETCH STATUS: not a supported url";
    +            return L"403 PREFETCH STATUS FAILED\r\n";
    +        }

    Fixed in the ab83c6d

    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Nov 13, 2026 13:44 UTC
    Reviewer

    [!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>

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> PREFETCH LIST is documented but not registered as a command

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> Requeued PREFETCH keeps old seq and breaks priority ordering

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.png" alt="Low severity" width="62" height="18" align="texttop"></picture> Duplicate strategy key computation causes inconsistent job tracking

  • </details>

    <details>
    <summary><strong>3 resolved since last review</strong></summary>

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> CANCEL only stops one range instead of all jobs for the URL

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> STATUS accepts unsupported URLs instead of matching PREFETCH validation

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> PREFETCH STATUS returns raw request text instead of canonical key

  • </details>

    <sub>Review effort: Lite</sub>

    Reviewers
    Assignees
    niklaspandersson niklaspandersson
    Labels
    risk: low confidence: 89.4% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 59.1% (MEDIUM) · Higher = larger blast radius
    59.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 89.4% · Lower = rushed or shallow review
    89.4%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 6.3% · Safe to merge
    6.3%