Skip to content

Commit 18309d2

Browse files
authored
Add step-at-a-time API example
1 parent ab01356 commit 18309d2

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

explainer.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,36 @@ The encoder is an object that takes a Stream(raw frames) and emits a Stream(enco
3636

3737
## Code examples
3838

39-
We can pass a factory function to the PeerConnection that does the building whenever an encoder is needed (and similar for the decoder):
39+
In order to insert your own processing in the media pipeline, do the following:
4040

41+
1. Declare a function that does what you want to a single frame.
4142
<pre>
42-
pc = new PeerConnection( {
43+
function mungeFunction(frame) { … }
44+
</pre>
45+
2. Set up a transform stream that will apply this function to all frames passed to it.
46+
<pre>
47+
var munger = new TransformStream({transformer: mungeFunction});
48+
</pre>
49+
3. Create a function that will take the original encoder, connect it to the transformStream in an appropriate way, and return an object that can be treated by the rest of the system as if it is an encoder:
50+
<pre>
51+
function installMunger(encoder, context) {
52+
encoder.readable.pipeTo(munger.writable);
53+
var wrappedEncoder = { readable: munger.readable,
54+
writable: encoder.writable };
55+
return wrappedEncoder;
56+
}
57+
</pre>
58+
4. Tell the PeerConnection to call this function whenever an encoder is created:
59+
<pre>
60+
pc = new RTCPeerConnection ({
61+
encoderFactory: installMunger;
62+
});
63+
</pre>
64+
65+
Or do it all using a deeply nested set of parentheses:
66+
67+
<pre>
68+
pc = new RTCPeerConnection( {
4369
encoderFactory: (encoder) => {
4470
var munger = new TransformStream({
4571
transformer: munge
@@ -56,7 +82,7 @@ The PC will then connect the returned object’s “writable” to the media inp
5682

5783
When the processing is to be done in a worker, we let the factory method pass the pipes to the worker:
5884
<pre>
59-
pc = new PeerConnection({
85+
pc = new RTCPeerConnection({
6086
encoderFactory: (encoder) => {
6187
var munger = new TransformStream({ transformer: munge });
6288
output = encoder.readable.pipeThrough(munger.writable);

0 commit comments

Comments
 (0)