forked from hcrlab/stretch_web_interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webrtcconnection.ts
351 lines (308 loc) · 13.9 KB
/
webrtcconnection.ts
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//
// initial code retrieved from the following link on 9/13/2017
// https://github.com/googlecodelabs/webrtc-web/blob/master/step-05/js/main.js
//
// initial code licensed with Apache License 2.0
//
import { io, Socket } from "socket.io-client";
import { Robot } from "../pages/robot/ts/robot";
import { generateUUID, safelyParseJSON } from "./util";
import type { WebRTCMessage, CameraInfo } from "./util";
import type { Request, Response, Responder } from "./requestresponse";
import { AvailableRobots } from "./socketio";
// Free STUN server offered by Google
const pcConfig = {
'iceServers': [{
'urls': 'stun:stun.l.google.com:19302'
}]
};
export class WebRTCConnection {
private socket: Socket
private pc?: RTCPeerConnection
private onConnectionStart: () => void
private requestResponders: Map<string, Responder> = new Map()
private pendingRequests: Map<string, (response: any) => void> = new Map()
cameraInfo: CameraInfo = {}
private makingOffer = false
private ignoreOffer = false
private isSettingRemoteAnswerPending = false
private onConnectionEnd: () => void
private onMessage: (obj: WebRTCMessage | WebRTCMessage[]) => void
private onMessageChannelOpen: () => void
private onTrackAdded: (ev: RTCTrackEvent) => void
private onRequestChannelOpen: () => void
private messageChannel?: RTCDataChannel
private requestChannel?: RTCDataChannel
constructor(peerName: string, polite: boolean, {
// TODO: make these placeholder functions match the definitions above
onConnectionStart = () => { },
onConnectionEnd = () => { },
onMessage = (message: WebRTCMessage | WebRTCMessage[]) => { },
onMessageChannelOpen = () => { },
onTrackAdded = (ev: RTCTrackEvent) => { },
onAvailableRobotsChanged = (available_robots: AvailableRobots) => { },
onRequestChannelOpen = () => { }
} = {}) {
this.onConnectionStart = onConnectionStart;
// Fired when the disconnection is NOT manually triggered, but happens for an external reason
this.onConnectionEnd = onConnectionEnd;
this.onMessage = onMessage;
this.onMessageChannelOpen = onMessageChannelOpen;
this.onTrackAdded = onTrackAdded;
this.onRequestChannelOpen = onRequestChannelOpen;
this.createPeerConnection();
this.socket = io();
this.socket.on('created', room => {
console.log('Created room ' + room);
});
this.socket.on('full', room => {
console.log('Room ' + room + ' is full');
});
this.socket.on('available robots', onAvailableRobotsChanged)
this.socket.on('join', room => {
console.log('Another peer made a request to join room ' + room);
console.log('I am ' + peerName + '!');
if (this.onConnectionStart) this.onConnectionStart()
});
this.socket.on('joined', room => {
console.log('joined: ' + room);
});
this.socket.on('bye', () => {
console.log('Session terminated.');
this.stop();
})
this.socket.on('signalling', (message: SignallingMessage) => {
let {candidate, sessionDescription, cameraInfo} = message;
console.log('Received message:', message);
if (cameraInfo) {
if (Object.keys(cameraInfo).length === 0) {
console.warn("Received a camera info mapping with no entries")
}
this.cameraInfo = cameraInfo
}
if (sessionDescription?.type === 'offer' || sessionDescription?.type === 'answer') {
if (!this.pc) throw 'pc is undefined';
const readyForOffer =
!this.makingOffer &&
(this.pc.signalingState === "stable" || this.isSettingRemoteAnswerPending);
const offerCollision = sessionDescription.type === "offer" && !readyForOffer;
this.ignoreOffer = !polite && offerCollision;
if (this.ignoreOffer) {
console.error("Ignoring offer")
return;
}
this.isSettingRemoteAnswerPending = sessionDescription.type === "answer";
this.pc.setRemoteDescription(sessionDescription).then(async () => {
this.isSettingRemoteAnswerPending = false;
if (sessionDescription?.type === "offer") {
if (!this.pc) throw 'pc is undefined';
return this.pc.setLocalDescription();
} else {
return false;
}
}).then(() => {
if (!this.pc?.localDescription) throw 'pc is undefined';
this.sendSignallingMessage({sessionDescription: this.pc.localDescription});
});
} else if (candidate !== undefined && this.pc) {
// Note that the last candidate will be null, so we check whether the candidate variable is
// defined at all versus being truthy.
this.pc.addIceCandidate(candidate).catch(e => {
if (!this.ignoreOffer) {
console.log("Failure during addIceCandidate(): " + e.name);
throw e;
}
});
} else {
console.error("Unable to handle message")
}
});
}
/**
* Called to create bidirectional message channels with the connected peer.
* Note that the peer is expected to be using this class to manage their end of the connection as well,
* in which case the necessary event handlers will be installed in `createPeerConnection` during `ondatachannel`.
*/
openDataChannels() {
this.messageChannel = this.pc!.createDataChannel('messages');
this.requestChannel = this.pc!.createDataChannel('requestresponse');
this.messageChannel.onmessage = this.onReceiveMessageCallback.bind(this);
this.requestChannel.onmessage = this.processRequestResponse.bind(this);
}
createPeerConnection() {
console.log('Creating peer connection');
try {
this.pc = new RTCPeerConnection(pcConfig);
this.pc.onicecandidate = (event) => {
this.sendSignallingMessage({
candidate: event.candidate!
});
};
this.pc.ondatachannel = event => {
// The remote has opened a data channel. We'll set up different handlers for the different channels.
if (event.channel.label === "messages") {
this.messageChannel = event.channel;
this.messageChannel.onmessage = this.onReceiveMessageCallback.bind(this);
let onDataChannelStateChange = () => {
if (!this.messageChannel) throw 'messageChannel is undefined';
const readyState = this.messageChannel.readyState;
console.log('Data channel state is: ' + readyState);
if (readyState === 'open') {
if (this.onMessageChannelOpen) this.onMessageChannelOpen();
}
}
this.messageChannel.onopen = onDataChannelStateChange;
this.messageChannel.onclose = onDataChannelStateChange;
} else if (event.channel.label === "requestresponse") {
this.requestChannel = event.channel
this.requestChannel.onmessage = this.processRequestResponse.bind(this);
if (this.onRequestChannelOpen) this.onRequestChannelOpen();
} else {
console.error("Unknown channel opened:", event.channel.label)
}
};
// TODO (kavidey): Figure out why typescript doesn't think that these callbacks exist
this.pc.onopen = () => {
console.log('RTC channel opened.');
};
this.pc.ontrack = this.onTrackAdded
this.pc.onremovestream = (event: any) => {
console.log('Remote stream removed. Event: ', event);
};
this.pc.onnegotiationneeded = async () => {
console.log("Negotiation needed")
try {
this.makingOffer = true;
if (!this.pc) throw 'pc is undefined';
await this.pc.setLocalDescription();
if (this.pc.localDescription) {
this.sendSignallingMessage({
sessionDescription: this.pc.localDescription,
cameraInfo: this.cameraInfo
});
}
} catch (err) {
console.error(err);
} finally {
this.makingOffer = false;
}
};
this.pc.oniceconnectionstatechange = () => {
if (!this.pc) throw 'pc is undefined';
if (this.pc.iceConnectionState === "failed") {
this.pc.restartIce();
}
};
this.pc.onconnectionstatechange = () => {
if (!this.pc) throw 'pc is undefined';
if (this.pc.connectionState === "failed" || this.pc.connectionState === "disconnected") {
console.error(this.pc.connectionState, "Resetting the PeerConnection")
this.createPeerConnection()
if (this.onConnectionEnd) this.onConnectionEnd();
}
}
console.log('Created RTCPeerConnection');
} catch (e: any) {
console.error('Failed to create PeerConnection, exception: ' + e.message);
return;
}
}
sendSignallingMessage(message: SignallingMessage) {
this.socket.emit('signalling', message);
}
availableRobots() {
console.log('asking server what robots are available');
this.socket.emit('what robots are available');
}
connectToRobot(robot: Robot) {
console.log('attempting to connect to robot =');
console.log(robot);
this.socket.emit('join', robot);
}
hangup() {
// Tell the other end that we're ending the call so they can stop, and get us kicked out of the robot room
console.warn("Hanging up")
this.socket.emit('bye');
if (!this.pc) throw 'pc is undefined';
if (this.pc.connectionState === "new") {
// Don't reset PCs that don't have any state to reset
return;
}
console.log('Hanging up.');
this.stop();
}
addTrack(track: MediaStreamTrack, stream: MediaStream, streamName: string) {
// Keep track of the mapping from the random ID and the nice stream name so we can send this to the other end later
this.cameraInfo[stream.id] = streamName
if (!this.pc) throw 'pc is undefined';
this.pc.addTrack(track, stream)
}
stop() {
if (!this.pc) throw 'pc is undefined';
const senders = this.pc.getSenders();
senders.forEach((sender) => this.pc?.removeTrack(sender));
this.pc.close();
this.createPeerConnection()
}
////////////////////////////////////////////////////////////
// RTCDataChannel
// on Sept. 15, 2017 copied initial code from
// https://github.com/googlecodelabs/webrtc-web/blob/master/step-03/js/main.js
// initial code licensed with Apache License 2.0
////////////////////////////////////////////////////////////
sendData(obj: WebRTCMessage | WebRTCMessage[]) {
if (!this.messageChannel || (this.messageChannel.readyState !== 'open')) {
//console.warn("Trying to send data, but data channel isn't ready")
return;
}
const data = JSON.stringify(obj);
this.messageChannel.send(data)
}
onReceiveMessageCallback(event: { data: string }) {
const obj: WebRTCMessage | WebRTCMessage[] = safelyParseJSON(event.data);
if (this.onMessage) this.onMessage(obj)
}
makeRequest<response>(type: string): Promise<response> {
return new Promise((resolve, reject) => {
let id = generateUUID();
this.requestChannel!.send(JSON.stringify({
type: "request",
id: id,
requestType: type
}));
this.pendingRequests.set(id, (responseData: response) => {
resolve(responseData);
this.pendingRequests.delete(id);
});
});
}
registerRequestResponder(requestType: string, responder: Responder) {
this.requestResponders.set(requestType, responder);
}
processRequestResponse(ev: MessageEvent<string>): void {
const message = safelyParseJSON<Request | Response>(ev.data);
if (message.type === "request") {
let response: Response = {
type: "response",
id: message.id,
requestType: message.requestType
}
if (this.requestResponders.has(message.requestType)) {
this.requestResponders.get(message.requestType)!().then((data) => {
response.data = data;
this.requestChannel!.send(JSON.stringify(response));
});
} else {
console.error("Heard request with no responder")
// Send a response so the other side can error out
this.requestChannel!.send(JSON.stringify(response))
}
} else if (message.type == "response") {
if (this.pendingRequests.has(message.id)) {
this.pendingRequests.get(message.id)!(message.data);
} else {
console.error("Heard response for request we didn't send")
}
}
}
}