-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
64 lines (62 loc) · 1.88 KB
/
index.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
<style>
textarea {
width: 100%;
height: 20%;
}
video {
width: 160px;
height: 120px;
}
</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>OFFER <button id="copy-offer" disabled>copy</button></p>
<textarea id="offer" disabled></textarea>
</div>
<div>
<p><button id="paste-answer" disabled>paste answer and set remote description</button></p>
<textarea id="answer" disabled></textarea>
</div>
<div id="ice"></div>
<div id="err"></div>
<p>Paste it to <a href="answer.html">answer.html</a>, copy the answer back.</p>
<script>
let localStream;
const pc = new RTCPeerConnection();
pc.onicegatheringstatechange = (event) => {
if (pc.iceGatheringState !== 'complete') return;
document.getElementById('offer').value = pc.localDescription.sdp;
}
pc.oniceconnectionstatechange = () => {
ice.innerText = pc.iceConnectionState;
};
pc.ontrack = (event) => {
if (!remoteVideo.srcObject) {
remoteVideo.srcObject = event.streams[0];
}
};
navigator.mediaDevices.getUserMedia({audio: true, video: true})
.then(async (stream) => {
localStream = stream;
localVideo.srcObject = stream;
stream.getTracks().forEach(t => pc.addTrack(t, stream));
await pc.setLocalDescription();
document.getElementById('copy-offer').disabled = false;
})
.catch((e) => {
console.log(e);
err.innerText = e.toString();
})
document.getElementById('paste-answer').onclick = async () => {
const sdp = await navigator.clipboard.readText();
document.getElementById('answer').value = sdp;
await pc.setRemoteDescription({type: 'answer', sdp});
document.getElementById('paste-answer').disabled = true;
};
document.getElementById('copy-offer').onclick = () => {
navigator.clipboard.writeText(pc.localDescription.sdp);
document.getElementById('paste-answer').disabled = false;
};
</script>