-
Notifications
You must be signed in to change notification settings - Fork 1
/
master.js
260 lines (225 loc) · 10 KB
/
master.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
* This file demonstrates the process of starting WebRTC streaming using a KVS Signaling Channel.
*/
const master = {
signalingClient: null,
peerConnectionByClientId: {},
dataChannelByClientId: {},
localStream: null,
remoteStreams: [],
peerConnectionStatsInterval: null,
};
async function startMaster(localView, remoteView, formValues, onStatsReport, onRemoteDataMessage) {
master.localView = localView;
master.remoteView = remoteView;
// Create KVS client
const kinesisVideoClient = new AWS.KinesisVideo({
region: formValues.region,
accessKeyId: formValues.accessKeyId,
secretAccessKey: formValues.secretAccessKey,
sessionToken: formValues.sessionToken,
endpoint: formValues.endpoint,
correctClockSkew: true,
});
// Get signaling channel ARN
const describeSignalingChannelResponse = await kinesisVideoClient
.describeSignalingChannel({
ChannelName: formValues.channelName,
})
.promise();
const channelARN = describeSignalingChannelResponse.ChannelInfo.ChannelARN;
console.log('[MASTER] Channel ARN: ', channelARN);
// Get signaling channel endpoints
const getSignalingChannelEndpointResponse = await kinesisVideoClient
.getSignalingChannelEndpoint({
ChannelARN: channelARN,
SingleMasterChannelEndpointConfiguration: {
Protocols: ['WSS', 'HTTPS'],
Role: KVSWebRTC.Role.MASTER,
},
})
.promise();
const endpointsByProtocol = getSignalingChannelEndpointResponse.ResourceEndpointList.reduce((endpoints, endpoint) => {
endpoints[endpoint.Protocol] = endpoint.ResourceEndpoint;
return endpoints;
}, {});
console.log('[MASTER] Endpoints: ', endpointsByProtocol);
// Create Signaling Client
master.signalingClient = new KVSWebRTC.SignalingClient({
channelARN,
channelEndpoint: endpointsByProtocol.WSS,
role: KVSWebRTC.Role.MASTER,
region: formValues.region,
credentials: {
accessKeyId: formValues.accessKeyId,
secretAccessKey: formValues.secretAccessKey,
sessionToken: formValues.sessionToken,
},
systemClockOffset: kinesisVideoClient.config.systemClockOffset,
});
// Get ICE server configuration
const kinesisVideoSignalingChannelsClient = new AWS.KinesisVideoSignalingChannels({
region: formValues.region,
accessKeyId: formValues.accessKeyId,
secretAccessKey: formValues.secretAccessKey,
sessionToken: formValues.sessionToken,
endpoint: endpointsByProtocol.HTTPS,
correctClockSkew: true,
});
const getIceServerConfigResponse = await kinesisVideoSignalingChannelsClient
.getIceServerConfig({
ChannelARN: channelARN,
})
.promise();
const iceServers = [];
if (!formValues.natTraversalDisabled && !formValues.forceTURN) {
iceServers.push({ urls: `stun:stun.kinesisvideo.${formValues.region}.amazonaws.com:443` });
}
if (!formValues.natTraversalDisabled) {
getIceServerConfigResponse.IceServerList.forEach(iceServer =>
iceServers.push({
urls: iceServer.Uris,
username: iceServer.Username,
credential: iceServer.Password,
}),
);
}
console.log('[MASTER] ICE servers: ', iceServers);
const configuration = {
iceServers,
iceTransportPolicy: formValues.forceTURN ? 'relay' : 'all',
};
const resolution = formValues.widescreen ? { width: { ideal: 1280 }, height: { ideal: 720 } } : { width: { ideal: 640 }, height: { ideal: 480 } };
const constraints = {
video: formValues.sendVideo ? resolution : false,
audio: formValues.sendAudio,
};
// Get a stream from the webcam and display it in the local view.
// If no video/audio needed, no need to request for the sources.
// Otherwise, the browser will throw an error saying that either video or audio has to be enabled.
if (formValues.sendVideo || formValues.sendAudio) {
try {
master.localStream = await navigator.mediaDevices.getUserMedia(constraints);
localView.srcObject = master.localStream;
} catch (e) {
console.error('[MASTER] Could not find webcam');
}
}
master.signalingClient.on('open', async () => {
console.log('[MASTER] Connected to signaling service');
});
master.signalingClient.on('sdpOffer', async (offer, remoteClientId) => {
console.log('[MASTER] Received SDP offer from client: ' + remoteClientId);
// Create a new peer connection using the offer from the given client
const peerConnection = new RTCPeerConnection(configuration);
master.peerConnectionByClientId[remoteClientId] = peerConnection;
if (formValues.openDataChannel) {
master.dataChannelByClientId[remoteClientId] = peerConnection.createDataChannel('kvsDataChannel');
peerConnection.ondatachannel = event => {
event.channel.onmessage = onRemoteDataMessage;
};
}
// Poll for connection stats
if (!master.peerConnectionStatsInterval) {
master.peerConnectionStatsInterval = setInterval(() => peerConnection.getStats().then(onStatsReport), 1000);
}
// Send any ICE candidates to the other peer
peerConnection.addEventListener('icecandidate', ({ candidate }) => {
if (candidate) {
console.log('[MASTER] Generated ICE candidate for client: ' + remoteClientId);
// When trickle ICE is enabled, send the ICE candidates as they are generated.
if (formValues.useTrickleICE) {
console.log('[MASTER] Sending ICE candidate to client: ' + remoteClientId);
master.signalingClient.sendIceCandidate(candidate, remoteClientId);
}
} else {
console.log('[MASTER] All ICE candidates have been generated for client: ' + remoteClientId);
// When trickle ICE is disabled, send the answer now that all the ICE candidates have ben generated.
if (!formValues.useTrickleICE) {
console.log('[MASTER] Sending SDP answer to client: ' + remoteClientId);
master.signalingClient.sendSdpAnswer(peerConnection.localDescription, remoteClientId);
}
}
});
// As remote tracks are received, add them to the remote view
peerConnection.addEventListener('track', event => {
console.log('[MASTER] Received remote track from client: ' + remoteClientId);
if (remoteView.srcObject) {
return;
}
remoteView.srcObject = event.streams[0];
});
// If there's no video/audio, master.localStream will be null. So, we should skip adding the tracks from it.
if (master.localStream) {
master.localStream.getTracks().forEach(track => peerConnection.addTrack(track, master.localStream));
}
await peerConnection.setRemoteDescription(offer);
// Create an SDP answer to send back to the client
console.log('[MASTER] Creating SDP answer for client: ' + remoteClientId);
await peerConnection.setLocalDescription(
await peerConnection.createAnswer({
offerToReceiveAudio: true,
offerToReceiveVideo: true,
}),
);
// When trickle ICE is enabled, send the answer now and then send ICE candidates as they are generated. Otherwise wait on the ICE candidates.
if (formValues.useTrickleICE) {
console.log('[MASTER] Sending SDP answer to client: ' + remoteClientId);
master.signalingClient.sendSdpAnswer(peerConnection.localDescription, remoteClientId);
}
console.log('[MASTER] Generating ICE candidates for client: ' + remoteClientId);
});
master.signalingClient.on('iceCandidate', async (candidate, remoteClientId) => {
console.log('[MASTER] Received ICE candidate from client: ' + remoteClientId);
// Add the ICE candidate received from the client to the peer connection
const peerConnection = master.peerConnectionByClientId[remoteClientId];
peerConnection.addIceCandidate(candidate);
});
master.signalingClient.on('close', () => {
console.log('[MASTER] Disconnected from signaling channel');
});
master.signalingClient.on('error', () => {
console.error('[MASTER] Signaling client error');
});
console.log('[MASTER] Starting master connection');
master.signalingClient.open();
}
function stopMaster() {
console.log('[MASTER] Stopping master connection');
if (master.signalingClient) {
master.signalingClient.close();
master.signalingClient = null;
}
Object.keys(master.peerConnectionByClientId).forEach(clientId => {
master.peerConnectionByClientId[clientId].close();
});
master.peerConnectionByClientId = [];
if (master.localStream) {
master.localStream.getTracks().forEach(track => track.stop());
master.localStream = null;
}
master.remoteStreams.forEach(remoteStream => remoteStream.getTracks().forEach(track => track.stop()));
master.remoteStreams = [];
if (master.peerConnectionStatsInterval) {
clearInterval(master.peerConnectionStatsInterval);
master.peerConnectionStatsInterval = null;
}
if (master.localView) {
master.localView.srcObject = null;
}
if (master.remoteView) {
master.remoteView.srcObject = null;
}
if (master.dataChannelByClientId) {
master.dataChannelByClientId = {};
}
}
function sendMasterMessage(message) {
Object.keys(master.dataChannelByClientId).forEach(clientId => {
try {
master.dataChannelByClientId[clientId].send(message);
} catch (e) {
console.error('[MASTER] Send DataChannel: ', e.toString());
}
});
}