Skip to content

Fire QuotaExceeded if codec resources are exhausted/reclaimed #121

Closed

Description

@chcunningham

The UA (and underlying platform) will often have a limit for how many codecs can be simultaneously instantiated. Particularly for hardware codecs. We could throw a QuotaExceeded exception via the error callback when this occurs.

Additionally, the UA should have the ability to reclaim codec resources if it can identify they're not being used. Maybe this could be done silently (no use -> no sideffects -> why notify). If the app picks up usage later, we could silently reallocate the resources and chug along. If the resources are no longer available, throw the same QuotaExceeded described above.

Similar consideration: UAs (at least Chromium) may suspended video decoding for <video> when tabs are put into the background (and resumed when the tab re-enters foreground). Similar techniques could be used with WebCodecs, which would again throw the QuotaExceeded error (or some other error). This is tricky though: we can't reliably know that VideoDecoder (and kin) isn't doing useful work in the background. We might consider a reserving the right to suspend backgrounded Window & DedicatedWorker VideoDecoders, but offer a different lifetime contract for SharedWorker and ServiceWoker. We haven't thought through the SharedWorker and ServiceWorker usage yet (I'll soon reduce the exposed= to just DedicateWorker in the IDL).

Activity

  1. chcunningham commented on Jan 8, 2021

    @chcunningham
    CollaboratorAuthor
  2. sandersdan commented on Jan 8, 2021

    @sandersdan
    Contributor

    I like QuotaExceeded as it's clear that you may be able to obtain resources in the future. Usually with quotas an app will want to be able to query capacity, which we don't always know, but at least in the case of background/foreground transitions there are already events apps could listen to.

  3. added
    breakingInterface changes that would break current usage (producing errors or undesired behavior).
    on May 12, 2021
  4. chcunningham commented on May 12, 2021

    @chcunningham
    CollaboratorAuthor

    Triage note: marked breaking as this would be a new error. Not seriously breaking, as its an error to the callback (not thrown), but breaking nonetheless.

  5. tguilbert-google commented on Jun 8, 2021

    @tguilbert-google
    Member

    We outlined two cases where we might want to fire a QuotaExceeded error:

    • Failing to configure a codec with a valid accelerationPreference = "require" config, due to all hardware codecs being allocated.
    • Reclaiming a codec, after somehow determining that it wasn't being used anymore.

    Offline, we acknowledged that the solution for recovering from both of these errors is different, and might warrant the use of different errors:

    • For the failed configure, retrying with with accelerationPreference = "allow" will fix the issue.
    • For the reclaimed codec, creating a new codec with the same config will work.

    What about using a TimeoutError for the reclaimed codec case? Or is it a common enough failure case for "regular" codec operations, that we'd want to leave it free for UAs to report timeouts in an active codec pipeline?

  6. tguilbert-google commented on Jun 15, 2021

    @tguilbert-google
    Member

    I am working on a PR. For now this is the general direction it will be going.

    There are roughly 4 scenarios where we might want to throw an exception due to insufficient/reclaime resources.

    1. Too many pending codec operations/outstanding buffers (e.g. queueing hundreds of encode requests).

    Our current implementation limits the number of parallel requests internally sent to Encoders/Decoders. Outstanding requests accumulate in a higher level queue, which should be managed by different backpressure mechanisms.

    Proposal: do not add any new errors/exceptions here.

    1. Configuring a codec with hardwareAcceleration = "require", and not having enough hardware codecs available

    If a call to configure() with hardwareAcceleration = "require" fails, the app can re-issue the same configure call with hardwareAcceleration = "allow" to solve the issue. However, raising a QuotaExceeded error specifically for this case would make it easier for websites to fingerprint the number of available hardware codecs.

    Note: isConfigSupported() is not a hard guarantee that a configure call with the returned config succeeds.

    Proposal: Do not surface a distinct QuotaExceeded error in this case, primarily because of fingerprinting concerns. Instead, surface the same exception as regular initialization errors. Add a note in the privacy section documenting this choice.

    1. Reclaiming a backgrounded codec

    A backgrounded website might be actively using codecs. The User Agent might want to reclaim the instantiated codecs for use by foreground websites instead.

    Proposal: Add a “Resource Reclamation” portion to the spec, which outlines how user agents may be allowed to reclaim codecs. Ideally, the UA should prioritize how long a codec has been backgrounded for, how active that codec is, and what type of resources it is holding on to. Errors raised through the error() callback can be QuotaExceeded, or a newly minted CodecReclaimed exception.

    1. Reclaiming an inactive codec

    WebCodec object instances might be holding on to hardware codecs, and not be closed, but not be actively used. The UA should have the ability to reclaim those resources.

    Proposal: In the “Resource Reclamation” section, document a UA’s ability to reclaim a webpage’s inactive codecs. Raise the same error type as in scenario #3, via the error() callback, albeit with a different message.

  7. tguilbert-google commented on Jun 15, 2021

    @tguilbert-google
    Member

    Question raised by @sandersdan:
    Should a User Agent be allowed to reclaim an active codec on a foreground page?

  8. chcunningham commented on Jun 25, 2021

    @chcunningham
    CollaboratorAuthor

    Question raised by @sandersdan:
    Should a User Agent be allowed to reclaim an active codec on a foreground page?

    My leaning is 'no'. To my knowledge we don't do this in Chrome <video> today, so I don't immediately see a use case. Open to hearing more on why / when this could be used.

    Proposal: Add a “Resource Reclamation” portion to the spec, ...

    Some design discussion has come up in #282. I want to move that here for easier reading / visibility.

    @padenot wrote:

    It's going to be extremely common for a user to run, say, an encoding operation to export something in a creation software, and then do something else that might use Web Codecs for something more trivial. Therefore, I don't know that we can do anything to background active codecs. It's not possible for an implementation to determine which is the usage that is the most important.

    We hear this. In addition to the creation software, we think these other use cases could be broken by the proposed background reclamation.

    • (RTC) Camera or screen share encoding in a background tab
    • (Media playback) Audio decoding for playout in a background tab

    Still, we want to avoid the inverse problem of only being able to offer software codecs to these use cases because some trivial uses of WebCodecs (say advertising) have hold of all the hardware codecs in background tabs.

    Here's strawman:

    • we update "background" definition to explicitly exclude a number of sensitive conditions like:
      • tabs with running camera/microphone/screen capture
      • tabs that are actively playing audio (AudioDecoder not "background", but VideoDecoders are still fair game)

    This would remedy the RTC and media playback use cases I mentioned.

    It doesn't fix the creation software case. That one is harder to distinguish from the trivial uses. I think solving that is a pretty big design space (e.g. something akin to the wakelock API or the background fetch API, but for WebCodecs). I favor moving forward with the proposal above, and scoping the remedy for background creation software to a follow up extension.

    @aboba @padenot WDYT?

  9. padenot commented on Jun 25, 2021

    @padenot
    Collaborator

    It doesn't fix the creation software case. That one is harder to distinguish from the trivial uses. I think solving that is a pretty big design space (e.g. something akin to the wakelock API or the background fetch API, but for WebCodecs). I favor moving forward with the proposal above, and scoping the remedy for background creation software to a follow up extension.

    You're going to have creation software play music when rendering then.

  10. chcunningham commented on Jun 25, 2021

    @chcunningham
    CollaboratorAuthor

    You're going to have creation software play music when rendering then.

    Ha, yes that may occur. Note that this would only save them from us reclaiming AudioDecoders. The idea with the playing audio case is that this enables the same reclamation Firefox and Chrome do today where you suspend video decoding for <video> in the background while the audio track continues to play (user mostly doesn't notice).

    I favor moving forward with the proposal above, and scoping the remedy for background creation software to a follow up extension.

    Does that sound reasonable?

  11. padenot commented on Jul 5, 2021

    @padenot
    Collaborator

    I'm mostly concerned about long-running background jobs here. I don't know that encoders can be safely reclaimed. I think an exception for WebRTC APIs make sense, but generally it should be possible to encode a long movie without seeing the encoding job paused halfway why the user isn't looking. The problem is that the source of this encode job might be a decoding job, in the common scenario of transcoding.

    Decoders are easier: audio is rarely limited in number of instances. Video sometimes is, but it's common to release codec instances when backgrounded and this empirically works well (as implemented natively by multiple UAs).

  12. chcunningham commented on Jul 7, 2021

    @chcunningham
    CollaboratorAuthor

    Editors call:

    @padenot

    • uncomfortable with reclaiming background active encoders because of breaks mentioned above. Would accept reclaiming inactive encoders with significant inactivity timeout (e.g. 15s).
    • reclaiming decoders may also break transcoding use cases, so don't reclaim bg decoders on pages that also have active encoders. Otherwise, decoder reclamation sounds fine.
  13. chcunningham commented on Jul 21, 2021

    @chcunningham
    CollaboratorAuthor

    Editors call:

    @aboba also prefers longer inactivity timeout as mentioned above. 15s could be a bit long when it comes to contended hardware resources.

    @tguilbert-google let us know your thoughts on the above.

  14. tguilbert-google commented on Jul 23, 2021

    @tguilbert-google
    Member

    15s of inactivity sounds good to me. The point of not reclaiming decoders if encoders are active sounds good too.

    I unfortunately didn't get a chance to update this PR yet, and I will be OOO next week, but I will update it before the Aug 4th editor's call

  15. dominic-p commented on Apr 17, 2025

    @dominic-p

    I apologize for resurrecting an ancient issue. I have just started using WebCodecs for a security camera application, and my VideoDecoders are getting reclaimed due to inactivity if the tab stays in the background for too long.

    I understand from these docs that the only way to keep an active VideoDecoder in the background from being reclaimed is to have an active VideoEncoder or an active AudioEncoder on the same global object. The problem for my use case is that these are security camera feeds that don't have audio (even if they did, I would only be decoding it).

    I could make a dummy AudioEncoder and run it in my worker to try to workaround this, but I thought I would bring up the issue in case we should consider additional reclamation rules to cover this use case.

  16. padenot commented on Apr 17, 2025

    @padenot
    Collaborator

    You should probably open a bug on Chromium's tracker (because this is browser engine specific), and open a new issue if you have a use-case that comes to mind to not have the decoder reclaimed.

    Running some sort of image analysis algorithm in the background, and alert/log in case of event immediately comes to mind, but it's better if you state your use case in a new issue, so we can discuss it.

    Thanks for the message, in any case.

  17. dominic-p commented on Apr 17, 2025

    @dominic-p

    Thanks for the response. I really appreciate it. I opened #889 to discuss this use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    breakingInterface changes that would break current usage (producing errors or undesired behavior).

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions