w3c / webrtc-rtptransport

Repository for the RTPTransport specification of the WebRTC Working Group
Other
15 stars 5 forks source link

Should RtpSendStream/RtpReceiveStream be transferable? #36

Open youennf opened 1 month ago

youennf commented 1 month ago

explainer-use-case-1.md sets the two objects as transferable. The main benefit is to allow to transfer these objects to workers so that packet processing is done there. Another approach is to consider how it is being done in webrtc encoded transform or for encoded source.

It would be good to compare the pros and cons of both approaches.

youennf commented 1 month ago

Here is an example of how would look like this API without transferable.

// Setup API
partial dictionary RtpSendStreamInit {
    required Worker worker;
}
partial dictionary RtpReceiveStreamInit {
    required Worker worker;
}
partial interface RtpSender {
  Promise<undefined> replaceSendStreams(Worker worker);
}
partial interface RtpReceiver {
  Promise<undefined> replaceReceiveStreams(Worker worker);
}
partial interface RtpTransport {
  Promise<undefined> addRtpSendStream(RtpSendStreamInit);
  Promise<undefined> addRtpReceiveStream(RtpReceiveStreamInit);
}

// Packet API
[Exposed=Worker]
interface RtpSendStream {
    ...
}
[Exposed=Worker]
interface RtpReceiveStream {
    ...
}

// Worker context events
[Exposed=Worker]
interface RTCRtpReceiveStreamEvent: Event {
    readonly attribute RtpReceiveStream stream;
}
[Exposed=Worker]
interface RTCRtpSendStreamEvent: Event {
    readonly attribute RtpSendStream stream;
}
partial interface DedicatedWorkerGlobalScope {
    attribute onrtcrtpsendstream;
    attribute onrtcrtpreceivestream;
}
youennf commented 1 month ago

Tentative evaluation of pros and cons:

Pros for transferable approach

Cons for transferable approach

Pros for non transferable approach

Cons for non transferable approach

tonyherre commented 1 month ago

I would add a Pro for transferability / Con for non-transferable that this uses a standard JS pattern with which developers are already familiar, rather than needing to learn the semantics of another api-specific "one off transfer". Applying the Priority of Constituencies, an easier to use web developer API carries more weight than an easier to spec/implement one which puts a greater burden on the web dev.

youennf commented 1 month ago

this uses a standard JS pattern with which developers are already familiar, rather than needing to learn the semantics of another api-specific "one off transfer"

Yes and no. First, both models are in use. From past experience, web developers had no issue understanding the one off transfer model.

Second, the semantics of the "one off transfer" are simpler to understand. With the transferability approach, web developers may need to understand:

In that same vein, if we want to extend these objects in the future, we would need to design these API extensions with transferability as a constraint.

alvestrand commented 1 month ago

Pros for the transferable approach:

Cons for the one-off approach:

We have already tackled many of the transferable-approach issues when dealing with transfer of tracks. There's no such thing as a "self-explanatory" API. There is just more or less explanation needed.

youennf commented 1 month ago
  • Doesn't permit applications that make their own decisions on where to allocate tasks to threads

How so? in both approaches, the web application is able to decide whether it wants to send/receive packets in a worker/thread A or in a worker/thread B.

Philipel-WebRTC commented 3 weeks ago

One thing that came up when thinking about how to implement a JS level pacer.

The app should have control over the order in which packets are put onto the network, and having RtpSendStreams on separate workers would make this difficult.

Lets say for example if you want to send a packet from stream A and stream B within a short interval. Now the pacer can post to woker A and worker B so that RtpSendStream.sendRtp can be called from the respective worker, but now there is no guarantee that worker A will even call sendRtp before packet B has been sent.

AFAICT the solution to this would be to have all RtpSendStreams on the same worker, so the usefulness of making them transferable might be limited.