-
Notifications
You must be signed in to change notification settings - Fork 1
/
PopWebRtcApi.js
398 lines (336 loc) · 9.87 KB
/
PopWebRtcApi.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
Pop.WebRtc = {};
Pop.WebRtc.IceServers =
[
'stun.l.google.com:19302'
];
function GetWebRtcConfiguration()
{
const Configuration = {};
Configuration.iceServers = Pop.WebRtc.IceServers.map(IceServerToConfiguration);
return Configuration;
}
function IceServerToConfiguration(IceServerAddress)
{
const Entry = {};
Entry.urls = `stun:${IceServerAddress}`;
return Entry;
}
class WebRtcChannel_t
{
constructor(Connection,Name,OnMessage,OnClosed)
{
this.Name = Name;
this.OnMessage = OnMessage;
this.OnClosed = OnClosed;
// buffer up messages that caller tries to send before we're open
this.PendingSends = [];
this.Channel = Connection.createDataChannel(Name);
this.Channel.onopen = this.OnOpen.bind(this);
this.Channel.onclose = this.OnClose.bind(this);
this.Channel.onmessage = OnMessage;
}
OnOpen(Event)
{
Pop.Debug(`Channel ${this.Name} opened`);
}
OnClose(Event)
{
this.OnClosed(this);
}
Send(Data)
{
// flush pending sends, made simple by just adding new message to list
this.PendingSends.push(Data);
if ( this.Channel.readyState != 'open' )
{
Pop.Debug(`Channel readystate is ${this.Channel.readyState}`);
return;
}
const PoppedSends = this.PendingSends.splice(0,this.PendingSends.length);
for ( let SendData of PoppedSends )
{
this.Channel.send(SendData);
}
}
}
Pop.WebRtc.Server = class
{
constructor()
{
this.Connection = new RTCPeerConnection( GetWebRtcConfiguration() );
this.Connection.onicecandidate = this.OnIceCandidate.bind(this);
this.HasAddressPromise = Pop.CreatePromise();
this.HasConnectedPeerPromise = Pop.CreatePromise();
this.Address = {};
this.Address.Sdp = null; // connection info for protocol
this.Address.OfferDescription = null; // sdp is in here
this.Address.IceCandidateStrings = []; // possible routing candidates
this.MessageQueue = new Pop.PromiseQueue(`WebRtc server message queue`);
this.Channels = {};
this.Channels['Data'] = null;
this.CreateChannels();
}
// gr: this is only required in WebRtc, see if there's a nicer way to not need this
// we need to notify of the answer (which includes the SDP)
// but we dont NEED the ice candidiate to connect local-local
async AddClient(Address)
{
for ( let ic=0; ic<Address.IceCandidateStrings.length; ic++ )
{
const IceCandidateString = Address.IceCandidateStrings[ic];
const IceCandidate = JSON.parse(IceCandidateString);
try
{
if ( !this.Connection.remoteDescription )
throw `Cannot add ice candidiate until remote description has been set/processed`;
const Addded = await this.Connection.addIceCandidate(IceCandidate);
Pop.Debug(`Added client ice candidate to server`);
}
catch(e)
{
Pop.Warning(`Error client ice candidate to server ${e}`);
}
}
// gr:we get an exception if we already have one set
if ( this.Connection.remoteDescription )
{
//Pop.Warning(`connection already has a remote descrption, ignoring new one in addclient()`);
return;
}
const ClientDescription = new RTCSessionDescription(Address.AnswerDescription);
try
{
const Result = await this.Connection.setRemoteDescription(ClientDescription);
Pop.Debug(`Server SetRemote`);
// how do we know?
Pop.Debug(`Server set remote result;`,Result);
}
catch(e)
{
Pop.Warning(`Server set remote error;`,e);
this.HasConnectedPeerPromise.Reject(e);
throw e;
}
// now we have a remote description, we should be connected...
}
OnChannelMessage(Message,Channel)
{
const Packet = {};
Packet.Peer = null;
Packet.Data = Message.data;
Packet.Channel = Channel;
Packet.RecieveTime = Pop.GetTimeNowMs();
this.MessageQueue.Push(Packet);
//Pop.Debug(`Server OnChannelMessage`,...arguments);
}
OnChannelClosed(Channel)
{
Pop.Debug(`Server OnChannelClosed`,...arguments);
}
async CreateChannels()
{
// gr: have to create streams and channels before creating offer (description) to get ice candidate callback
const ChannelNames = Object.keys(this.Channels);
for ( let ChannelName of ChannelNames )
{
const OnMessage = this.OnChannelMessage.bind(this);
const OnClosed = this.OnChannelClosed.bind(this);
const Channel = new WebRtcChannel_t(this.Connection,ChannelName,OnMessage,OnClosed);
this.Channels[ChannelName] = Channel;
}
// now we have all channels& streams, we create the description+sdp
const OfferDescription = await this.Connection.createOffer();
this.Address.OfferDescription = OfferDescription;
this.Address.Sdp = this.Address.OfferDescription.sdp;
await this.Connection.setLocalDescription( this.Address.OfferDescription );
Pop.Debug(`Server setLocal`);
this.OnAddressChanged();
}
async WaitForConnect()
{
return this.HasAddressPromise;
}
async WaitForNewPeer()
{
return this.HasConnectedPeerPromise;
}
async WaitForMessage()
{
return this.MessageQueue.WaitForNext();
}
GetAddress()
{
return this.Address;
}
OnIceCandidate(Event)
{
// update "address"
const Candidate = Event.candidate;
if ( !Candidate )
{
Pop.Debug(`Server got ${Candidate} candidate`);
return;
}
const CandidateJson = Candidate.toJSON();
const CandidateString = JSON.stringify(CandidateJson);
this.Address.IceCandidateStrings.push(CandidateString);
this.OnAddressChanged();
}
OnAddressChanged()
{
// do we have everything we need to be considered listening?
// gr: I think as a minimum we just need SDP
if ( !this.Address.Sdp )
{
Pop.Warning(`OnAddressChanged, waiting for SDP`);
return;
}
// docs say we don't need an ice candidate, but...
// it won't connect without...
if ( !this.Address.IceCandidateStrings.length )
{
Pop.Warning(`OnAddressChanged, waiting for ice candidate`);
return;
}
this.HasAddressPromise.Resolve();
}
GetPeers()
{
return [null];
}
Send(Peer,Data,ChannelName='Data')
{
if ( Peer != this.GetPeers()[0] )
throw `Send(Peer=${Peer}) peer expected=${this.GetPeers()[0]}`;
if ( !this.Channels.hasOwnProperty(ChannelName) )
throw `Server has no channel named ${ChannelName}`;
const Channel = this.Channels[ChannelName];
Channel.Send(Data);
}
};
Pop.WebRtc.Client = class
{
// RemoteAddress here is ice candidate & offer-description
constructor(RemoteAddress)
{
this.MessageQueue = new Pop.PromiseQueue(`webrtc client message queue`);
this.Address = {};
this.Address.IceCandidateStrings = [];
this.Address.AnswerDescription = null;
this.HasAddressPromise = Pop.CreatePromise();
this.Channels = {}; // Channel['Name']
this.Connection = new RTCPeerConnection( GetWebRtcConfiguration() );
this.Connection.onicecandidate = this.OnIceCandidate.bind(this);
this.Connection.ondatachannel = this.OnFoundDataChannel.bind(this);
this.ConnectedPromise = this.Connect(RemoteAddress);
}
async Connect(Address)
{
// set the description&sdp of "server"
const OfferDescription = new RTCSessionDescription(Address.OfferDescription);
await this.Connection.setRemoteDescription(OfferDescription);
Pop.Debug(`Client SetRemote`);
// now we've set description(offer), we can make an answer
const AnswerDescription = await this.Connection.createAnswer();
await this.Connection.setLocalDescription( AnswerDescription );
Pop.Debug(`Client setLocal`);
// add all possible candidates (async, hence here)
//forEach ( let IceCandidate of Address.IceCandidateStrings )
for ( let i=0; i<Address.IceCandidateStrings.length; i++ )
{
const IceCandidateString = Address.IceCandidateStrings[i];
const IceCandidate = JSON.parse(IceCandidateString);
try
{
const AddedIce = await this.Connection.addIceCandidate(IceCandidate);
Pop.Debug(`Client added ice candidate`);
}
catch(e)
{
Pop.Warning(`Client failed to add ice candidiate; ${e}`);
}
}
this.Address.AnswerDescription = AnswerDescription;
this.OnAddressChanged();
}
GetAddress()
{
return this.Address;
}
OnIceCandidate(Event)
{
// update "address"
const Candidate = Event.candidate;
if ( !Candidate )
{
Pop.Debug(`Server got ${Candidate} candidate`);
return;
}
const CandidateJson = Candidate.toJSON();
const CandidateString = JSON.stringify(CandidateJson);
this.Address.IceCandidateStrings.push(CandidateString);
this.OnAddressChanged();
}
async WaitForAddress()
{
return this.HasAddressPromise;
}
OnConnectError(Error)
{
this.ConnectedPromise.Reject(Error);
}
OnFoundDataChannel(Event)
{
const Channel = Event.channel;
Pop.Debug(`OnFoundChannel`,Channel);
const ChannelName = 'Data';
this.Channels[ChannelName] = Channel;
function OnMessage(Event)
{
//Pop.Debug(`Client got message`,Event);
const Packet = {};
Packet.Peer = null;
Packet.Data = Event.data;
this.MessageQueue.Push(Packet);
}
Channel.onmessage = OnMessage.bind(this);
Channel.onopen = e => console.log(`Client channel open`,e);
Channel.onclose = e => console.log(`Client channel close`,e);
}
async WaitForConnect()
{
return this.ConnectedPromise;
}
async WaitForMessage()
{
// wait for connection first, if we've lost connection
// it will reject
await this.ConnectedPromise;
return this.MessageQueue.WaitForNext();
}
OnAddressChanged()
{
Pop.Debug(`client address changed`);
// gr: at least in a local-local, it SEEMS we don't need an ice candidate
// this may be different once we get onto the internet
this.HasAddressPromise.Resolve( this.Address );
}
GetPeers()
{
return [null];
}
// peer is not needed, but fits api of other sockets
Send(Peer,Data,ChannelName='Data')
{
if ( Peer != this.GetPeers()[0] )
throw `Send(Peer=${Peer}) peer expected=${this.GetPeers()[0]}`;
if ( !this.Channels.hasOwnProperty(ChannelName) )
{
//throw `No channel named ${ChannelName}; ${Object.keys(this.Channels)}`;
Pop.Debug(`No channel named ${ChannelName}; ${Object.keys(this.Channels)}`);
return;
}
const Channel = this.Channels[ChannelName];
Channel.send(Data);
}
};