-
Notifications
You must be signed in to change notification settings - Fork 2
/
answer.html
55 lines (53 loc) · 1.72 KB
/
answer.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
<style>
textarea {
width: 100%;
height: 20%;
}
video {
width: 320px;
height: 240px;
}
</style>
<script src="https://webrtchacks.github.io/adapter/adapter-latest.js"></script>
<video id="localVideo" autoplay muted></video>
<video id="remoteVideo" autoplay></video>
<div>
<p><button id="paste-offer">Paste offer and set remote description</button><input type="checkbox" id="gum">add stream</input></p>
<textarea id="offer" disabled></textarea>
</div>
<div>
<p><button id="copy-answer" disabled>copy answer</button></p>
<textarea id="answer" disabled></textarea>
</div>
<div id="ice"></div>
<div id="err"></div>
<script>
var pc = new RTCPeerConnection();
var localStream;
pc.onicegatheringstatechange = (event) => {
if (pc.iceGatheringState !== 'complete') return;
document.getElementById('answer').value = pc.localDescription.sdp;
}
pc.oniceconnectionstatechange = () => {
ice.innerText = pc.iceConnectionState;
};
pc.onaddstream = (event) => {
remoteVideo.srcObject = event.stream;
};
document.getElementById('paste-offer').onclick = async () => {
const sdp = await navigator.clipboard.readText();
document.getElementById('offer').value = sdp;
await pc.setRemoteDescription({type: 'offer', sdp})
if (document.getElementById('gum').checked) {
localStream = await navigator.mediaDevices.getUserMedia({audio: true, video: true})
localVideo.srcObject = localStream;
localStream.getTracks().forEach(t => pc.addTrack(t, localStream));
}
await pc.setLocalDescription();
document.getElementById('paste-offer').disabled = true;
document.getElementById('copy-answer').disabled = false;
}
document.getElementById('copy-answer').onclick = () => {
navigator.clipboard.writeText(pc.localDescription.sdp);
};
</script>