-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathClient.js
622 lines (532 loc) · 18.3 KB
/
Client.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
const EnhancedEventEmitter = require('./EnhancedEventEmitter');
const StreamManager = require('./StreamManager');
const SdpTransformer = require('sdp-transform');
const wsClient = require('./WebSocketClient');
const UserInfo = require('./UserInfo');
const MediaStatsInfo = require('./MediaStatsInfo');
const PCManager = require('./PCManager')
class Client extends EnhancedEventEmitter
{
constructor()
{
super();
this._remoteUsers = new Map();//uid, UserInfo
this._cameraStream = null;
this._screenStream = null;
this._closed = true;
this._connected = false;
this._sendPCMap = new Map();//peerConnectionId, PCManager object
this._recvPCMap = new Map();//peerConnectionId, PCManager object
this._sendVideoStats = new MediaStatsInfo();
this._sendAudioStats = new MediaStatsInfo();
this._cameraIndex = 0;
setInterval(async () => {
if (!this._connected) {
return;
}
await this.OnPublisherStats();
}, 2000);
setInterval(async () => {
if (!this._connected) {
return;
}
await this.OnSubscribeStats();
}, 2000);
setInterval(async () => {
if (!this._connected) {
return;
}
console.log("uid:", this._uid, " heartbeat");
await this.SendHeartBeat();
}, 2000);
}
async OpenCamera()
{
if (this._cameraStream)
{
throw Error("the camera is opened");
}
try
{
this._cameraStream = new StreamManager();
return await this._cameraStream.Open('camera');
} catch (error) {
console.log("create stream error:", error);
throw new Error("create stream error:" + error);
}
}
/*
return: {"users":[{"uid":"11111"}, {"uid":"22222"}]}
*/
async Join({serverHost, roomId, userId})
{
this._server = serverHost;
this._roomId = roomId;
this._uid = userId;
console.log("join api server:", serverHost, "roomId:", roomId, "userId:", userId);
this.ws = new wsClient();
this.url = 'ws://' + serverHost;
this.ws.Connect(this.url);
await new Promise((resolve, reject) => {
this._wsRegistEvent(resolve, reject);
});
var data = {
'roomId': roomId,
'uid': userId
};
console.log("ws is connected, starting joining server:", this.url, "data:", data);
var respData = null;
try {
respData = await this.ws.request('join', data);
} catch (error) {
console.log("join exception error:", error);
throw error;
}
var users = respData['users'];
for (const user of users)
{
var uid = user['uid'];
var userinfo = new UserInfo({roomId: this._roomId, uid: uid});
this._remoteUsers.set(uid, userinfo);
}
console.log("join response:", JSON.stringify(respData));
return respData;
}
async PublishCamera({videoEnable, audioEnable})
{
if (!this._connected)
{
throw new Error('websocket is not ready');
}
if (!this._cameraStream)
{
throw new Error('camera does not init');
}
if (!videoEnable && !audioEnable)
{
throw new Error('video and audio are not enable');
}
if (this._publishCamera)
{
throw new Error('the camera has been published');
}
var mediaTracks = [];
if (videoEnable)
{
mediaTracks.push(this._cameraStream.GetVideoTrack());
}
if (audioEnable)
{
mediaTracks.push(this._cameraStream.GetAudioTrack());
}
var sendCameraPc = null;
var offerInfo;
try {
sendCameraPc = new PCManager();
sendCameraPc.CreatePeerConnection('send');
sendCameraPc.SetType('camera');
offerInfo = await sendCameraPc.AddSendMediaTrack(mediaTracks);
} catch (error) {
throw error;
}
var data = {
'roomId': this._roomId,
'uid': this._uid,
'sdp' : offerInfo.offSdp
}
var respData;
try {
console.log("send publish request:", data);
respData = await this.ws.request('publish', data);
} catch (error) {
console.log("send publish message exception:", error)
throw error
}
console.log("Publish response message:", respData);
var answerSdp = respData['sdp'];
var peerConnectionId = respData['pcid'];
sendCameraPc.SetId(peerConnectionId);
await sendCameraPc.SetSendAnswerSdp(answerSdp);
var answerSdpObj = SdpTransformer.parse(answerSdp);
for (const item of answerSdpObj.media)
{
if (item.type == 'video')
{
this._cameraVideoMid = item.mid;
}
else if (item.type == 'audio')
{
this._cameraAudioMid = item.mid;
}
else
{
throw new Error("the sdp type is unkown:", item.type);
}
}
this._sendPCMap.set(peerConnectionId, sendCameraPc);
return;
}
async UnPublishCamera({videoDisable, audioDisable})
{
if (!this._connected)
{
throw new Error('websocket is not ready');
}
if (!this._cameraStream)
{
throw new Error('camera does not init');
}
if (!videoDisable && !audioDisable)
{
throw new Error('video and audio are not disable');
}
var removeMids = [];
if (videoDisable)
{
removeMids.push(this._cameraVideoMid);
}
if (audioDisable)
{
removeMids.push(this._cameraAudioMid);
}
var sendPC = null;
for (var pc of this._sendPCMap.values())
{
if (pc.GetType() == 'camera')
{
sendPC = pc;
break;
}
}
if (sendPC == null)
{
throw new Error("fail to find camera");
}
sendPC.removeSendTrack(removeMids);
sendPC.ClosePC();
this._sendPCMap.delete(sendPC.GetId());
//send unpublish request
var data = {
'roomId': this._roomId,
'uid': this._uid,
'pcid' : sendPC.GetId()
}
var respData;
try {
console.log("unpublish request: ", data);
respData = await this.ws.request('unpublish', data);
} catch (error) {
console.log("send unpublish message exception:", error)
throw error
}
console.log("UnPublish response message:", respData);
return respData;
}
async OnPublisherStats() {
let statsList = await this.GetPublisherRtcStats();
if (!statsList) {
return;
}
statsList.forEach((report) => {
//console.log("report type:", report.type, ", report:", JSON.stringify(report));
if (report.type == 'outbound-rtp') {
if (report.mediaType == 'video') {
this._sendVideoStats.SetWidth(report.frameWidth);
this._sendVideoStats.SetHeight(report.frameHeight);
this._sendVideoStats.SetFps(report.framesPerSecond);
this._sendVideoStats.SetBytesSent(report.bytesSent);
} else if (report.mediaType == 'audio') {
this._sendAudioStats.SetFps(report.framesPerSecond);
this._sendAudioStats.SetBytesSent(report.bytesSent);
this._sendAudioStats.SetFrameSent(report.packetsSent);
}
} else if (report.type == 'candidate-pair') {
if (report.nominated) {
this._sendVideoStats.SetRtt(report.currentRoundTripTime * 1000);
}
}
});
this.safeEmit('stats', {
'video': {
'width': this._sendVideoStats.GetWidth(),
'height': this._sendVideoStats.GetHeight(),
'fps': this._sendVideoStats.GetFps(),
'bps': this._sendVideoStats.GetSentBitsPerSec()
},
'audio': {
'fps': this._sendAudioStats.GetFps(),
'bps': this._sendAudioStats.GetSentBitsPerSec()
},
'rtt': this._sendVideoStats.GetRtt()
});
}
async GetPublisherRtcStats() {
if (this._sendPCMap.size == 0) {
return null;
}
for (const sendPc of this._sendPCMap.values()) {
if (sendPc == null) {
console.log('peer connection is null, the pc map length:', this._sendPCMap.size);
continue;
}
let stats = await sendPc.GetStats();
return stats;
}
return null;
}
async OnSubscribeStats() {
if (this._remoteUsers.size == 0) {
return;
}
for (const user of this._remoteUsers.values()) {
if (user == null) {
continue;
}
let pcId = user.GetPcid();
let recvPc = this._recvPCMap.get(pcId);
if (recvPc == null) {
continue;
}
let stats = await recvPc.GetStats();
if (stats == null) {
continue;
}
stats.forEach((report) => {
if (report.type == 'inbound-rtp') {
if (report.mediaType == 'video') {
user.RecvVideoStats().SetWidth(report.frameWidth);
user.RecvVideoStats().SetHeight(report.frameHeight);
user.RecvVideoStats().SetFps(report.framesPerSecond);
user.RecvVideoStats().SetBytesSent(report.bytesReceived);
} else if (report.mediaType == 'audio') {
user.RecvAudioStats().SetFps(report.framesPerSecond);
user.RecvAudioStats().SetBytesSent(report.bytesReceived);
user.RecvAudioStats().SetFrameSent(report.packetsReceived);
}
} else if (report.type == 'candidate-pair') {
if (report.nominated) {
user.RecvVideoStats().SetRtt(report.currentRoundTripTime * 1000);
}
}
});
this.safeEmit('remoteStats', {
'uid': user.GetUserId(),
'video': {
'width': user.RecvVideoStats().GetWidth(),
'height': user.RecvVideoStats().GetHeight(),
'fps': parseInt(user.RecvVideoStats().GetFps()),
'bps': parseInt(user.RecvVideoStats().GetSentBitsPerSec())
},
'audio': {
'fps': parseInt(user.RecvAudioStats().GetFps()),
'bps': parseInt(user.RecvAudioStats().GetSentBitsPerSec())
},
'rtt': user.RecvVideoStats().GetRtt()
});
}
return;
}
_wsRegistEvent(resolve, reject)
{
this.ws.on('open', () =>
{
this._connected = true;
this._closed = false;
resolve(0);
});
this.ws.on('close', () =>
{
if (this._connected) {
this.safeEmit('disconected', '');
}
this._connected = false;
this._closed = true;
reject(new Error('protoo close'));
});
this.ws.on('error', (err) =>
{
this._connected = false;
this._closed = true;
reject(err);
});
this.ws.on('notification', (info) =>
{
try {
console.log("notification method:", info.method);
console.log("notification info:", info);
if (info.method == 'userin')
{
var remoteUid = info.data['uid'];
var userType = info.data['user_type'];
var remoteUser = new UserInfo({roomId: this._roomId, uid:remoteUid, userType: userType});
this._remoteUsers.set(remoteUid, remoteUser);
}
this.safeEmit(info.method, info.data);
} catch (error) {
console.log("notify error:", error);
}
});
}
async Subscribe(remoteUid, userType, remotePcId, publishers)
{
if (!this._connected)
{
throw new Error('websocket is not ready');
}
var hasUid = this._remoteUsers.has(remoteUid);
if (!hasUid)
{
throw new Error('remote uid has not exist:' + remoteUid);
}
var remoteUser = this._remoteUsers.get(remoteUid);
console.log("start subscribe remote user:", remoteUser, "publishers:",
publishers, "userType:", userType);
var recvPC = new PCManager();
recvPC.CreatePeerConnection('recv');
recvPC.SetRemoteUid(remoteUid);
for (const info of publishers) {
recvPC.AddSubscriberMedia(info);
}
var offerSdp = await recvPC.GetSubscribeOfferSdp();
console.log("update publishers:", publishers);
var respData = null;
var data = {
'roomId': this._roomId,
'uid': this._uid,
'user_type': userType,
'remoteUid': remoteUid,
'remotePcId': remotePcId,
'publishers': publishers,
'sdp' : offerSdp
}
try {
console.log("subscribe request: ", data);
respData = await this.ws.request('subscribe', data);
} catch (error) {
console.log("send subscribe message exception:", error)
throw error
}
console.log("subscribe response message:", respData);
var respSdp = respData.sdp;
var pcid = respData.pcid;
var respSdpJson = SdpTransformer.parse(respSdp);
console.log("subscribe response json sdp:", JSON.stringify(respSdpJson));
recvPC.SetRemoteSubscriberSdp(respSdp);
var trackList = [];
for (const mediaInfo of publishers)
{
console.log("subscribe is waiting track ready...");
var newTrack = await new Promise(async (resolve, reject) =>
{
recvPC.on('newTrack', (track) => {
console.log("rtc receive new track:", track);
if (track != null) {
resolve(track);
} else {
reject(track);
}
});
});
if (newTrack != null)
{
trackList.push(newTrack);
}
}
console.log("receive new track list:", trackList);
var mediaStream = remoteUser.CreateMediaStream();
for (const track of trackList)
{
console.log("remote user:", remoteUser, "add new track:", track);
mediaStream.addTrack(track);
}
console.log("set subscribe pcid:", pcid, "publishers:", publishers);
recvPC.SetSubscribeInfo(pcid, publishers)
recvPC.SetId(pcid);
this._recvPCMap.set(pcid, recvPC);
remoteUser.SetPcId(pcid);
remoteUser.SetPublishers(publishers);
return mediaStream;
}
GetRemoteUserPcId(remoteUid) {
var remoteUser = this._remoteUsers.get(remoteUid);
if (!remoteUser) {
return '';
}
return remoteUser.GetPcId();
}
GetRemoteUserPublishers(remoteUid) {
var publisers;
var remoteUser = this._remoteUsers.get(remoteUid);
if (!remoteUser) {
return publisers;
}
return remoteUser.GetPublishers();
}
async SendHeartBeat() {
if (!this._connected) {
throw new Error("websocket is not ready");
}
var data = {
'uid': this._uid
}
var respData;
console.log("request heartbeat data:", data);
try {
respData = await this.ws.request('heartbeat', data);
} catch (error) {
console.log('heartbeat error:', error);
throw error;
}
console.log('heartbeat return data:', respData);
}
async UnSubscribe(remoteUid, publisers)
{
if (!this._connected)
{
throw new Error('websocket is not ready');
}
var hasUid = this._remoteUsers.has(remoteUid);
if (!hasUid)
{
throw new Error('remote uid has not exist:' + remoteUid);
}
var remoteUser = this._remoteUsers.get(remoteUid);
var pcid = '';
console.log("start unsubscribe remote user:", remoteUser, "publishers:", publisers);
for (var [keyPCid, recvPC] of this._recvPCMap)
{
pcid = recvPC.GetSubscribePcId(publisers);
if (pcid != undefined && pcid.length > 0)
{
break;
}
}
if (pcid == undefined || pcid.length == '')
{
console.log("fail to get peer connection id:", pcid);
throw new Error("fail to get peer connection id");
}
for (const info of publisers) {
recvPC.RemoveSubscriberMedia(info);
}
recvPC.SetRemoteUnSubscriberSdp();
recvPC.ClosePC();
remoteUser.CloseMediaStream();
var data = {
'uid': this._uid,
'remoteUid': remoteUid,
'pcid': pcid,
'publishers': publisers
}
var respData;
console.log("request unsubscribe data:", data);
try {
respData = await this.ws.request('unsubscribe', data);
} catch (error) {
console.log('unsubscribe error:', error);
throw error;
}
console.log('unsubscribe return data:', respData);
}
};
module.exports = Client;