-
Notifications
You must be signed in to change notification settings - Fork 19
/
poorman.html
218 lines (207 loc) · 7.81 KB
/
poorman.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<html>
<head>
<meta charset="utf-8">
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<script src="https://webrtc.github.io/samples/src/js/third_party/graph.js"></script>
<script src="test-assert.js"></script>
<script src="draw-graphs.js"></script>
<style>
video {
width: 320px;
}
#superVideo {
display: none;
}
canvas {
display: none;
}
</style>
</head>
<body>
<div id="local">
<h2>Local Video</h2>
</div>
<h2>Remote Videos</h2>
<input type="range" id="layer" name="max spatial layer" min="0" max="2" value="0" disabled/>
<label for="temporal">Select spatial layer</label>
<br>
<input type="checkbox" id="flicker" disabled/>
<label for="flicker">Demonstrate flickering due to srcObject reattach</label>
<br>
<video id="superVideo" autoplay muted></video>
<div id="remotes">
</div>
<script>
const SDPUtils = adapter.sdp;
const lastTimestamp = {};
async function decodeFunction(encodedFrame, controller, stream) {
const ssrc = encodedFrame.getMetadata().synchronizationSource;
if (!lastTimestamp[ssrc]) {
lastTimestamp[ssrc] = encodedFrame.timestamp - 100000;
}
// Demonstrate it for video and make assumptions like "do it for keyframes"
if (encodedFrame.type === 'key' && encodedFrame.timestamp - lastTimestamp[ssrc] > 90000) {
console.log('switching...', stream);
if (document.getElementById('flicker').checked) {
superVideo.srcObject = stream; // this causes a flicker
}
document.querySelectorAll('#remotes video').forEach(v => {
v.style.display = v.srcObject === stream ? 'block' : 'none';
});
}
lastTimestamp[ssrc] = encodedFrame.timestamp;
controller.enqueue(encodedFrame);
}
const pc1 = new RTCPeerConnection();
const pc2 = new RTCPeerConnection({encodedInsertableStreams: true});
pc1.onicecandidate = (e) => pc2.addIceCandidate(e.candidate);
pc2.onicecandidate = (e) => pc1.addIceCandidate(e.candidate);
pc2.ontrack = (e) => {
show(e.streams[0], true);
const receiverStreams = e.receiver.createEncodedStreams();
const transformStream = new TransformStream({
transform: (f, c) => decodeFunction(f, c, e.streams[0]),
});
receiverStreams.readable
.pipeThrough(transformStream)
.pipeTo(receiverStreams.writable);
};
async function turnOnOff(layer, active) {
const sender = pc1.getSenders()[0];
const parameters = sender.getParameters();
parameters.encodings[layer].active = active;
sender.setParameters(parameters);
}
document.getElementById('layer').onclick = () => {
const selectedLayer = document.getElementById('layer').value >>> 0;
// Turn on the selected layer.
turnOnOff(selectedLayer, true);
// Turn off the other layers with a slight delay.
setTimeout(() => {
for (let i = 0; i < 3; i++) {
if (i !== selectedLayer) {
turnOnOff(i, false);
}
}
}, 1000);
}
document.getElementById('flicker').onchange = () => {
const flicker = document.getElementById('flicker').checked
document.getElementById('superVideo').style.display = flicker ? 'block' : 'none';
document.getElementById('remotes').style.display = !flicker ? 'block' : 'none';
}
const extensionsToFilter = [
'urn:ietf:params:rtp-hdrext:sdes:mid',
'urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id',
'urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id',
];
const rids = [0, 1, 2];
navigator.mediaDevices.getUserMedia({video: {width: 1280, height: 720}})
.then((stream) => {
pc1.addTransceiver(stream.getVideoTracks()[0], {
streams: [stream],
sendEncodings: rids.map(rid => ({rid})),
});
show(stream, false);
return pc1.createOffer();
})
.then((offer) => {
const sections = SDPUtils.splitSections(offer.sdp);
const dtls = SDPUtils.getDtlsParameters(sections[1], sections[0]);
const ice = SDPUtils.getIceParameters(sections[1], sections[0]);
const rtpParameters = SDPUtils.parseRtpParameters(sections[1]);
const rtcpParameters = SDPUtils.parseRtcpParameters(sections[1]);
// The gist of this hack is that rid and mid have the same wire format.
// Kudos to orphis for this clever hack!
const rid = rtpParameters.headerExtensions.find(ext => ext.uri === 'urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id');
rtpParameters.headerExtensions = rtpParameters.headerExtensions.filter(ext => {
return !extensionsToFilter.includes(ext.uri);
});
// This tells the other side that the RID packets are actually mids.
rtpParameters.headerExtensions.push({id: rid.id, uri: 'urn:ietf:params:rtp-hdrext:sdes:mid', direction: 'sendrecv'});
// Filter rtx as we have no wait to reinterpret rrid. Not doing this makes probing use RTX, its not understood
// and ramp-up is slower.
rtpParameters.codecs = rtpParameters.codecs.filter(c => c.name.toUpperCase() !== 'RTX');
let sdp = SDPUtils.writeSessionBoilerplate() +
SDPUtils.writeDtlsParameters(dtls, 'actpass') +
SDPUtils.writeIceParameters(ice) +
'a=group:BUNDLE 0 1 2\r\n' +
'a=msid-semantic:WMS *\r\n';
const codecs = SDPUtils.writeRtpDescription('video', rtpParameters) +
SDPUtils.writeRtcpParameters({
mux:rtcpParameters.mux,
reducedSize: rtcpParameters.reducedSize,
});
sdp += codecs +
'a=setup:actpass\r\n' +
'a=mid:' + rids[0] + '\r\n' +
'a=msid:low low\r\n';
sdp += codecs +
'a=setup:actpass\r\n' +
'a=mid:' + rids[1] + '\r\n' +
'a=msid:mid mid\r\n';
sdp += codecs +
'a=setup:actpass\r\n' +
'a=mid:' + rids[2] + '\r\n' +
'a=msid:hi hi\r\n';
return Promise.all([
pc1.setLocalDescription(offer),
pc2.setRemoteDescription({
type: 'offer',
sdp,
}),
]);
})
.then(() => pc2.createAnswer())
.then(answer => {
const sections = SDPUtils.splitSections(answer.sdp);
const dtls = SDPUtils.getDtlsParameters(sections[1], sections[0]);
const ice = SDPUtils.getIceParameters(sections[1], sections[0]);
const rtpParameters = SDPUtils.parseRtpParameters(sections[1]);
const rtcpParameters = SDPUtils.parseRtcpParameters(sections[1]);
// Avoid duplicating the mid extension even though Chrome does not care (boo!)
rtpParameters.headerExtensions = rtpParameters.headerExtensions.filter(ext => {
return !extensionsToFilter.includes(ext.uri);
});
let sdp = SDPUtils.writeSessionBoilerplate() +
SDPUtils.writeDtlsParameters(dtls, 'active') +
SDPUtils.writeIceParameters(ice) +
'a=group:BUNDLE 0\r\n' +
'a=msid-semantic:WMS *\r\n';
const codecs = SDPUtils.writeRtpDescription('video', rtpParameters) +
SDPUtils.writeRtcpParameters({
mux:rtcpParameters.mux,
reducedSize: rtcpParameters.reducedSize,
});
sdp += codecs;
sdp += 'a=setup:active\r\n';
rids.forEach(rid => {
sdp += 'a=rid:' + rid + ' recv\r\n';
});
sdp += 'a=simulcast:recv ' + rids.join(';') + '\r\n';
sdp += 'a=mid:' + SDPUtils.getMid(SDPUtils.splitSections(pc1.localDescription.sdp)[1]) + '\r\n';
// Re-add headerextensions we filtered.
const headerExtensions = SDPUtils.parseRtpParameters(SDPUtils.splitSections(pc1.localDescription.sdp)[1]).headerExtensions;
headerExtensions.forEach(ext => {
if (extensionsToFilter.includes(ext.uri)) {
sdp += 'a=extmap:' + ext.id + ' ' + ext.uri + '\r\n';
}
});
return Promise.all([
pc2.setLocalDescription(answer),
pc1.setRemoteDescription({
type: 'answer',
sdp
}),
]);
})
.then(() => {
document.getElementById('layer').disabled = false;
document.getElementById('flicker').disabled = false;
// deactivate all the layers
[0, 1, 2].forEach(layer => turnOnOff(layer, false))
})
.catch(e => console.error(e));
</script>
</body>
</html>