Skip to content

Need to provide hints to increase buffering for power consumption reasons #348

Closed

Description

@cwilso

It's been requested that we enable a batching mechanism to let users of the API tell us to process a batch of frames at a time (rather than the lowest latency we can support on a given system), trading off higher latency for applications that would rather minimize power (e.g. a simple background-music-playback app).

Activity

  1. added this to the milestone on Aug 25, 2014
  2. self-assigned this
    on Aug 25, 2014
  3. padenot commented on Aug 25, 2014

    @padenot
    Member

    I've also had people requesting something like this to me as
    well.

    I was thinking that it could be part of some some of Audio
    Channel API [0], but that's still not specced properly
    (although I remember talking to Google people about it, and
    Microsoft has something similar).

    In general, I'd prefer to spec this in terms of use case or
    developer intent than explicitly latency.

    [0]http://dxr.mozilla.org/mozilla-central/source/dom/interfaces
    /html/nsIDOMHTMLMediaElement.idl#98

  4. jernoble commented on Aug 25, 2014

    @jernoble
    Member

    Why is this not up to the UA? For example, the WebAudio API on iOS will already switch to a larger buffer size when the system goes into a low-power mode.

    padenot:

    In general, I'd prefer to spec this in terms of use case or developer intent than explicitly latency.

    I agree, and this could tie into other API feature requests as well. An author could mark an AudioSession as "media playback", which would both trigger a lower buffer size, and would interrupt other media playback on the device.

  5. cwilso commented on Aug 25, 2014

    @cwilso
    ContributorAuthor

    Why is this not up to the UA?

    It should be, ultimately, and I wouldn't want to take away the ability of a
    system to up the buffer size because it decides to go into a low-power mode.

    However, apps should be specifically able to say "I'm cool with having a
    larger buffer size". I don't think this is just media playback, per se -
    it's any less-interactive audio scenario. It's finer grained (or perhaps
    just orthogonal) to the audio channel specification types.

    On Mon, Aug 25, 2014 at 10:10 AM, Jer Noble [email protected]
    wrote:

    Why is this not up to the UA? For example, the WebAudio API on iOS will
    already switch to a larger buffer size when the system goes into a
    low-power mode.

    padenot:

    In general, I'd prefer to spec this in terms of use case or developer
    intent than explicitly latency.

    I agree, and this could tie into other API feature requests as well. An
    author could mark an AudioSession as "media playback", which would both
    trigger a lower buffer size, and would interrupt other media playback on
    the device.

    —
    Reply to this email directly or view it on GitHub
    #348 (comment)
    .

  6. jernoble commented on Aug 28, 2014

    @jernoble
    Member

    I didn't see a specific API proposal here, so here's a strawman:

    enum AudioContextPlaybackCategory { "interactive", "non-interactive", "media"};
    
    partial interface AudioContext {
        // defaults to "interactive"
        attribute AudioContextPlaybackCategory playbackCategory;
    }
    

    A UA could decide to set the hardware buffer size to 128 for "interactive" playback, to 4096 for "non-interactive", and to 44,100 for "media" playback.

  7. padenot commented on Aug 28, 2014

    @padenot
    Member

    In Gecko, we put that in the AudioContext ctor to make it clear
    that it can't be changed.

    On Thu, Aug 28, 2014, at 07:09 PM, Jer Noble wrote:

    I didn't see a specific API proposal here, so here's a
    strawman:
    enum AudioContextPlaybackCategory { "interactive", "non-interactive", "m
    edia"};

    partial interface AudioContext {
    // defaults to "interactive"
    attribute AudioContextPlaybackCategory playbackCategory;
    }

    A UA could decide to set the hardware buffer size to 128 for
    "interactive" playback, to 4096 for "non-interactive", and
    to 44,100 for "media" playback.

    —
    Reply to this email directly or [1]view it on GitHub.

    References

    1. Need to provide hints to increase buffering for power consumption reasons #348 (comment)
  8. jernoble commented on Aug 28, 2014

    @jernoble
    Member

    padenot:

    In Gecko, we put that in the AudioContext ctor to make it clear that it can't be changed.

    Is there some reason why it can't be changed? Maybe that's reasonable, but for iOS and OS X it's certainly possible to change the hardware buffer size dynamically.

  9. padenot commented on Aug 28, 2014

    @padenot
    Member

    Sorry, I was unclear.

    We implemented a whatwg proposal where you give an audio channel (roughly corresponding to use cases, see the link to the interface in the second comment) to a context, or media element, etc, and this gets decided on construction. It is possible to change the latency while everything is running, but that's not exposed yet to the web (and in fact, AudioChannel stuff is only available to system apps, iirc, it's not really exposed to the web).

  10. cwilso commented on Aug 29, 2014

    @cwilso
    ContributorAuthor

    ROC: Sorry, the specific proposal I'd made was here: http://cwilso.github.io/web-audio-api/proposals.html#widl-AudioContext-AudioContext-AudioContext-float-buffering.

    In short, the constructor for AudioContext becomes:
    AudioContext AudioContext (optional float buffering = 0);

    A user may choose to ask the system to process blocks of audio in batches, in order to reduce power consumption (while increasing latency). This parameter is specified as the number of seconds of desired buffering latency. This is a hint to the system about the minimum requested buffering; underlying constraints may forcer larger buffers (higher latency) than requested for system reasons. This buffer request will also be increased to the nearest larger duration that equals an integral number of 128-sample processing blocks.

  11. jernoble commented on Aug 29, 2014

    @jernoble
    Member

    cwilso:

    AudioContext AudioContext (optional float buffering = 0);

    There are a couple of problems with this proposal:

    1. It is extreme overkill for the use case in question. Instead of indicating to the UA that the author would like the UA to prioritize power consumption over latency, this API requires the author to pick a specific buffer size in order to do so. The control (preferred buffer size) is two levels removed from its intended goal (power consumption, by way of increasing CPU idle periods, presumably). And setting the buffer size to a high value could conceivably be counterproductive (to power consumption) on certain UAs.

    2. It takes a very specific parameter, which is almost always ignored. The buffering parameter will be rounded to the nearest 128 / sampleRate. It will be ignored if the hardware minimum buffer size exceeds it, or basically whenever the UA decides to. The parameter feels like an inherently broken promise.

    If the UA is going to generally ignore this parameter, and if the UA knows how to best tune the buffer size (among other things) to achieve low power consumption at the expense of latency, it would be better to have the hint be something both less precise and more descriptive: e.g., "low", "medium", and "high". (To be clear, I prefer the "interactive", "non-interactive", "media" proposal, but for reasons unrelated to power use.)

  12. cwilso commented on Sep 4, 2014

    @cwilso
    ContributorAuthor
    1. I'd argue it's not overkill. It's not a binary "prioritize power over latency", it's a "here's the level of latency I'm happy with."

    2. Happy to change the semantic there - number of blocks? And of course, the hardware can up it - the same has always been true of buffersize in ScriptProcessor.

    The UA doesn't know best how to tune the buffer size without being told what level of latency a particular app is comfortable with. I think this is beyond low/medium/high. "Interactive" is different for a live audio mixer vs a DJ scratching, e.g.

  13. jernoble commented on Sep 4, 2014

    @jernoble
    Member

    @cwilso

    1. I'd argue it's not overkill. It's not a binary "prioritize power over latency", it's a "here's the level of latency I'm happy with."

    But that’s not the use case you’re trying to solve. You specifically brought up “power consumption” as the driving use case for this change, which is not what this API actually fixes. If you want a generic latency control mechanism, this use case doesn't justify it.

  14. cwilso commented on Sep 4, 2014

    @cwilso
    ContributorAuthor

    Sorry, I should have expanded. "I want to minimize power. Here's a level
    of latency I'm willing to trade for it."

    On Thu, Sep 4, 2014 at 11:11 AM, Jer Noble [email protected] wrote:

    @cwilso https://github.com/cwilso

    1. I'd argue it's not overkill. It's not a binary "prioritize power over
      latency", it's a "here's the level of latency I'm happy with."

    But that’s not the use case you’re trying to solve. You specifically
    brought up “power consumption” as the driving use case for this change,
    which is not what this API actually fixes. If you want a generic latency
    control mechanism, this use case doesn't justify it.

    —
    Reply to this email directly or view it on GitHub
    #348 (comment)
    .

  15. 65 remaining items

  16. mdjp commented on Apr 7, 2016

    @mdjp
    Member

    Change processingLatency to baseLatency to avoid confusion. Otherwise accept revision to add numeric value.

  17. added
    Needs Edits
    and removed
    Needs Discussion
    on Apr 7, 2016
  18. hoch commented on Apr 7, 2016

    @hoch
    Member

    Note: this constructor option and the latency property are only valid for real-time audio context.

  19. alvestrand commented on Apr 11, 2016

    @alvestrand

    /sub
    The latency constraint (a request to getUserMedia) is already in that spec.

  20. rtoy commented on Apr 14, 2016

    @rtoy
    Member

    Question. If the user specified "interactive" (or some other enum), what should baseLatency return? Should it be the actual latency or "interactive"? As currently spec'ed in #775, we need to return a double, but should it also support enums? A double is nice, though.

    Finally, it's the time in sec. A value in msec would be more natural, but does make it different from all other time values in WebAudio.

  21. foolip commented on Apr 14, 2016

    @foolip
    Contributor

    Having baseLatency always be a double would make sense to me. Otherwise there's presumably no way of knowing what "interactive" mapped to, and if knowing baseLatency is ever useful it ought to be potentially useful in that case as well.

  22. tomasgunn commented on Apr 14, 2016

    @tomasgunn

    Agree that double would be useful even if it's best effort.

    On Thu, Apr 14, 2016, 11:21 Philip Jägenstedt [email protected]
    wrote:

    Having baseLatency always be a double would make sense to me. Otherwise
    there's presumably no way of knowing what "interactive" mapped to, and if
    knowing baseLatency is ever useful it ought to be potentially useful in
    that case as well.

    —
    You are receiving this because you were mentioned.
    Reply to this email directly or view it on GitHub
    #348 (comment)

  23. rtoy commented on Apr 14, 2016

    @rtoy
    Member

    I actually like a numeric value for all cases too. I'm going to leave it as double, then.

  24. added a commit that references this issue on May 12, 2016
    7f128c4
  25. added a commit that references this issue on Jun 29, 2016
    20a542d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Needs Edits

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions