Skip to content

[audioworklet] Revise AWN/AWP instantiation algorithm: structured serialization and abstract operation #1193

Closed

Description

@annevk

In whatwg/html@97d644c @domenic changed the way cloning works to be serializer-based instead. The Audio API needs to be updated to account for that.

Activity

  1. self-assigned this
    on Apr 5, 2017
  2. hoch commented on Apr 5, 2017

    @hoch
    Member

    @annevk We don't support Transfer in AudioWorklet's comm channel anyway, so changing the term from 'structured cloning' to 'serializing'. (and perhaps with a link to the reference) Does it makes sense?

  3. annevk commented on Apr 6, 2017

    @annevk
    Author

    Yeah, I think you want to invoke StructuredSerialize and StructuredDeserialize, referenced and with their proper names, but you also need to define the processing model of some of this in more detail.

    E.g., step 4 of "The instantiation of AudioWorkletNode and AudioWorkletProcessor" currently says structured cloning is involved, but I can't find where that actually happens. (That algorithm also weirdly seems to cross threads without any kind of message passing or acknowledment that it's doing so. Is there an issue on that?)

  4. domenic commented on Apr 6, 2017

    @domenic
    Contributor

    I think ideally this would be an opportunity to make the actual usage more rigorous in "The instantiation of AudioWorkletNode and AudioWorkletProcessor". In particular, you'd have steps like:

    • Let serializedOptions be StructuredSerialize(options).
    • Let optionsInWorklet be StructuredDeserialize(serializedOptions, the AudioWorkletGlobalScope's Realm).
    • Now you can use optionsInWorklet in the Construct() step.

    I'm not sure exactly how to phrase the prose references that currently say "structured clone". Maybe something like replacing

    This may be any value or JavaScript object handled by the structured clone algorithm.

    by

    This value may be any serializable value

  5. domenic commented on Apr 6, 2017

    @domenic
    Contributor

    Oh, I see I left my tab open for too long and @annevk said what I was going to say, basically.

  6. hoch commented on Apr 6, 2017

    @hoch
    Member

    Thanks @annevk, @domenic - I will try to capture this change in our text.

  7. hoch commented on Apr 6, 2017

    @hoch
    Member

    One more question:

    That algorithm also weirdly seems to cross threads without any kind of message passing or acknowledment that it's doing so. Is there an issue on that?

    @annevk How do you describe cross-thread operation in terms of EcmaScript? The algorithm is largely based on the PaintWorklet, except that it has several cross-thread tasks. Any example for this pattern?

  8. annevk commented on Apr 7, 2017

    @annevk
    Author

    Well, each thread has an event loop. So what you end up doing is queuing tasks back and forth (message passing through tasks).

  9. padenot commented on Apr 7, 2017

    @padenot
    Member

    We have several examples of cross-thread messaging in the Web Audio API. When talking to the rendering thread, use a control message. We don't have a traditional event loop, but we have our own mechanism that is compatible with our processing based on rendering quantum. When talking to the main thread, queue a task on the main thread as usual, quoting the current spec:

    Communication from the control thread to the rendering thread is done using control message
    passing. Communication in the other direction is done using regular event loop tasks.

  10. annevk commented on Apr 7, 2017

    @annevk
    Author

    The way worklets are defined they have an event loop, so you'll have to reconcile that somehow I suppose.

  11. padenot commented on Apr 7, 2017

    @padenot
    Member

    Yes, this is not hard to do.

  12. hoch commented on Apr 7, 2017

    @hoch
    Member

    I was hoping to find some examples that describe cross-thread operation in terms of ES, but I guess a well-written prose is good enough for this case.

  13. annevk commented on Apr 7, 2017

    @annevk
    Author

    @hoch in ECMAScript you'd have to use a worker and postMessage(). In a specification that translates to serializing objects and queuing tasks to deserialize them.

  14. hoch commented on Apr 7, 2017

    @hoch
    Member

    @annevk Thanks for the clarification. We don't use either worker of postMessage, so I will try to capture the cross-thread task in a prose, as @padenot suggested above.

  15. annevk commented on Apr 7, 2017

    @annevk
    Author

    @hoch for the record, you can't describe anything in terms of ECMAScript in the specification. You'll always need to use some abstract concepts. There will be way too many gotchas otherwise.

  16. 17 remaining items

  17. hoch commented on Jun 8, 2017

    @hoch
    Member

    We can make AWN output silence when it's in a weird state:

    1. The construction of AWP fails.
    2. The AWP process() method throws the exception.
    3. The construction of AWP is not finished, but the AWN is connected to the graph.

    Then asynchronously notify the user with:

    1. AudioWorkletNode.onstatechange (AudioNode is already EventTarget, so this makes sense.)
    2. AudioWorkletNode.port.onmessage

    @padenot WDYT?

  18. padenot commented on Jun 9, 2017

    @padenot
    Member
    1. This we can synchronously throw an exception, abort the construction (i.e. return null so it can't be connected to anything)
    2. This we should make the Worklet invalid and make it output silence as you note. The other option would be to make it pass-through, but I don't like that, it makes it harder to debug, in a way.
    3. is normal, it should not be an error. You create it, you connect it, etc., but it's not ready so it outputs silence like the other nodes.

    For the notification, onstatechange or onerror would work. Semantically speaking, I don't think we should overload onmessage on the port, which is used for normal (author-initiated) messages.

  19. hoch commented on Jun 9, 2017

    @hoch
    Member
    1. This we can synchronously throw an exception, abort the construction (i.e. return null so it can't be connected to anything)

    Hmm. I thought you wanted to make the instantiation process non-atomic, asynchronous. Then how can we throw an exception synchronously from the async process?

    1. This we should make the Worklet invalid and make it output silence as you note. The other option would be to make it pass-through, but I don't like that, it makes it harder to debug, in a way.

    Agreed. I like it to be silent for the same reason.

    1. isn't really controversial. :)

    For the notification, onstatechange or onerror would work. Semantically speaking, I don't think we should overload onmessage on the port, which is used for normal (author-initiated) messages.

    Exactly. Having onstatechange also means we need state read-only attribute in the node. Plus, onerror can be used for two purposes: 1) when the AWP construction fails and 2) when the execution of process() throws and it becomes silent.

  20. padenot commented on Jun 12, 2017

    @padenot
    Member

    Hmm. I thought you wanted to make the instantiation process non-atomic, asynchronous. Then how can we throw an exception synchronously from the async process?

    Oops sorry I misread AWP for AWN, you're right that it would put the node in error.

  21. added 3 commits that reference this issue on Jun 14, 2017
    f09864d
    fe2e76a
    fe176b7
  22. hoch commented on Jun 15, 2017

    @hoch
    Member

    Here's the work-in-progress PR, so we can discuss in F2F: Preview

  23. added
    Needs Discussion
    on Jun 16, 2017
  24. joeberkovitz commented on Jun 20, 2017

    @joeberkovitz

    From F2F: We'll adopt @hoch's state enumeration proposal. We need language explaining that the running and stopped states are distinguished only by the value of the AWN's active source flag.

    We also need to revise the description of how the active source flag is propagated from the rendering thread to the main thread to explicitly state that a message is queued. There are two copies of this flag: one on the AWP (to gate whether process() gets called again) and another on the AWN (to drive the internal reference that affects GC).

  25. added a commit that references this issue on Jun 21, 2017
    a370962
  26. hoch commented on Jun 23, 2017

    @hoch
    Member

    The instantiation of AWN/AWP requires some thread-specific information for each object. For this purpose, we need some synchronization steps when addModule() call resolves. So I filed an issue for the worklet spec: w3c/css-houdini-drafts#418.

  27. added a commit that references this issue on Jun 30, 2017
    b928a27
  28. joeberkovitz commented on Sep 7, 2017

    @joeberkovitz

    Fixed by #1265

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

Metadata

Metadata

Assignees

Labels

Needs Discussion

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions