-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
140 lines (122 loc) · 3.36 KB
/
main.js
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
/*
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree.
*/
'use strict';
const startButton = document.getElementById('startButton');
const hangupButton = document.getElementById('hangupButton');
hangupButton.disabled = true;
const localVideo = document.getElementById('localVideo');
const remoteVideo = document.getElementById('remoteVideo');
let pc;
let localStream;
const signaling = new BroadcastChannel('webrtc');
signaling.onmessage = e => {
if (!localStream) {
console.log('not ready yet');
return;
}
switch (e.data.type) {
case 'offer':
handleOffer(e.data);
break;
case 'answer':
handleAnswer(e.data);
break;
case 'candidate':
handleCandidate(e.data);
break;
case 'ready':
// A second tab joined. This tab will initiate a call unless in a call already.
if (pc) {
console.log('already in call, ignoring');
return;
}
makeCall();
break;
case 'bye':
if (pc) {
hangup();
}
break;
default:
console.log('unhandled', e);
break;
}
};
startButton.onclick = async () => {
localStream = await navigator.mediaDevices.getUserMedia({audio: true, video: true});
localVideo.srcObject = localStream;
startButton.disabled = true;
hangupButton.disabled = false;
signaling.postMessage({type: 'ready'});
};
hangupButton.onclick = async () => {
hangup();
signaling.postMessage({type: 'bye'});
};
async function hangup() {
if (pc) {
pc.close();
pc = null;
}
localStream.getTracks().forEach(track => track.stop());
localStream = null;
startButton.disabled = false;
hangupButton.disabled = true;
};
function createPeerConnection() {
pc = new RTCPeerConnection();
pc.onicecandidate = e => {
const message = {
type: 'candidate',
candidate: null,
};
if (e.candidate) {
message.candidate = e.candidate.candidate;
message.sdpMid = e.candidate.sdpMid;
message.sdpMLineIndex = e.candidate.sdpMLineIndex;
}
signaling.postMessage(message);
};
pc.ontrack = e => remoteVideo.srcObject = e.streams[0];
localStream.getTracks().forEach(track => pc.addTrack(track, localStream));
}
async function makeCall() {
await createPeerConnection();
const offer = await pc.createOffer();
signaling.postMessage({type: 'offer', sdp: offer.sdp});
await pc.setLocalDescription(offer);
}
async function handleOffer(offer) {
if (pc) {
console.error('existing peerconnection');
return;
}
await createPeerConnection();
await pc.setRemoteDescription(offer);
const answer = await pc.createAnswer();
signaling.postMessage({type: 'answer', sdp: answer.sdp});
await pc.setLocalDescription(answer);
}
async function handleAnswer(answer) {
if (!pc) {
console.error('no peerconnection');
return;
}
await pc.setRemoteDescription(answer);
}
async function handleCandidate(candidate) {
if (!pc) {
console.error('no peerconnection');
return;
}
if (!candidate.candidate) {
await pc.addIceCandidate(null);
} else {
await pc.addIceCandidate(candidate);
}
}