Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update encoded explainer to include sample code for relay P2Ps #198

Open
wants to merge 6 commits into
base: create-encoded-explainer
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update create-encoded-explainer.md
  • Loading branch information
palak8669 authored Sep 27, 2023
commit f559ec4b5e13b61e50300ed3d00cb5f90c25a09b
30 changes: 26 additions & 4 deletions create-encoded-explainer.md
Original file line number Diff line number Diff line change
@@ -55,23 +55,45 @@ It is harmful to the model if there are couplings between the source of encoded
Let's take a scenario where P2P relays are used to forward frames. Depending solely on a local peer for the stream is not very reliable in this setup. For consistent, glitch-free, low latency streaming, a redundant PeerConnection delivering the same stream is also setup(`recvPc1` and `recvPc2`). A peer can then choose to relay this stream to the next peer in the network (`relayPc`). We pass the readable stream of the incoming PeerConnections to `transferFrames` function which reads the frames and modifies the frame's metadata such that two frames with the same payload have the same metadata (`getUnifiedMetadata`). We then write one of these frames(`isDuplicate`) to the `relayPcWriter`. `getUnifiedMetadata` and `isDuplicate` are application-specific functions.

```
// code in main.js file
const worker = new Worker('worker.js');

// Let recvPc1, recvPc2 be the receiving PCs.
recvPc{1|2}.ontrack = evt => {
transferFrames(evt.receiver.createEncodedStreams().readable.getReader());
evt.receiver.transform = new RTCRtpScriptTransform(worker, { name: "receiverTransform" });
};

// Let relayPc be the PC used to relay frames to the next peer.
relayPcWriter = relayPc.sender.createEncodedStreams().writable.getWriter();
relayPc.sender.transform = new RTCRtpScriptTransform(worker, { name: "senderTransform" });

```

async function transferFrames(reader) {
```
// code in worker.js file
async function transferFrames(reader, writer) {
if(!reader || !writer){
return;
}
while (true) {
const {frame, done} = await reader.read();
if (done) return;

frame.setMetadata(getUnifiedMetadata(frame));
if(!isDuplicate(frame)) {
relayPcWriter.write(frame);
writer.write(frame);
}
}
}

// Code to instantiate reader and writer from the RTPReceiver and RTPSender.
onrtctransform = (event) => {
if (event.transformer.options.name == "senderTransform")
writer = event.transformer.writable;
else if (event.transformer.options.name == "receiverTransform")
reader = event.transformer.readable;
else
return;

transferFrames(reader,writer);
};
```