forked from EpicGames/PixelStreamingInfrastructure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcirrus.js
1299 lines (1120 loc) · 39.9 KB
/
cirrus.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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright Epic Games, Inc. All Rights Reserved.
//-- Server side logic. Serves pixel streaming WebRTC-based page, proxies data back to Streamer --//
var express = require('express');
var app = express();
const fs = require('fs');
const path = require('path');
const querystring = require('querystring');
const bodyParser = require('body-parser');
const logging = require('./modules/logging.js');
logging.RegisterConsoleLogger();
// Command line argument --configFile needs to be checked before loading the config, all other command line arguments are dealt with through the config object
const defaultConfig = {
UseFrontend: false,
UseMatchmaker: false,
UseHTTPS: false,
HTTPSCertFile: './certificates/client-cert.pem',
HTTPSKeyFile: './certificates/client-key.pem',
UseAuthentication: false,
LogToFile: true,
LogVerbose: true,
HomepageFile: 'player.html',
AdditionalRoutes: new Map(),
EnableWebserver: true,
MatchmakerAddress: "",
MatchmakerPort: 9999,
PublicIp: "localhost",
HttpPort: 80,
HttpsPort: 443,
StreamerPort: 8888,
SFUPort: 8889,
MaxPlayerCount: -1,
DisableSSLCert: true
};
const argv = require('yargs').argv;
var configFile = (typeof argv.configFile != 'undefined') ? argv.configFile.toString() : path.join(__dirname, 'config.json');
console.log(`configFile ${configFile}`);
const config = require('./modules/config.js').init(configFile, defaultConfig);
if (config.LogToFile) {
logging.RegisterFileLogger('./logs/');
}
console.log("Config: " + JSON.stringify(config, null, '\t'));
var http = require('http').Server(app);
if (config.UseHTTPS) {
//HTTPS certificate details
const options = {
key: fs.readFileSync(path.join(__dirname, config.HTTPSKeyFile)),
cert: fs.readFileSync(path.join(__dirname, config.HTTPSCertFile))
};
var https = require('https').Server(options, app);
}
//If not using authetication then just move on to the next function/middleware
var isAuthenticated = redirectUrl => function (req, res, next) { return next(); }
if (config.UseAuthentication && config.UseHTTPS) {
var passport = require('passport');
require('./modules/authentication').init(app);
// Replace the isAuthenticated with the one setup on passport module
isAuthenticated = passport.authenticationMiddleware ? passport.authenticationMiddleware : isAuthenticated
} else if (config.UseAuthentication && !config.UseHTTPS) {
console.error('Trying to use authentication without using HTTPS, this is not allowed and so authentication will NOT be turned on, please turn on HTTPS to turn on authentication');
}
const helmet = require('helmet');
var hsts = require('hsts');
var net = require('net');
var FRONTEND_WEBSERVER = 'https://localhost';
if (config.UseFrontend) {
var httpPort = 3000;
var httpsPort = 8000;
if (config.UseHTTPS && config.DisableSSLCert) {
//Required for self signed certs otherwise just get an error back when sending request to frontend see https://stackoverflow.com/a/35633993
console.logColor(logging.Orange, 'WARNING: config.DisableSSLCert is true. Unauthorized SSL certificates will be allowed! This is convenient for local testing but please DO NOT SHIP THIS IN PRODUCTION. To remove this warning please set DisableSSLCert to false in your config.json.');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"
}
const httpsClient = require('./modules/httpsClient.js');
var webRequest = new httpsClient();
} else {
var httpPort = config.HttpPort;
var httpsPort = config.HttpsPort;
}
var streamerPort = config.StreamerPort; // port to listen to Streamer connections
var sfuPort = config.SFUPort;
var matchmakerAddress = '127.0.0.1';
var matchmakerPort = 9999;
var matchmakerRetryInterval = 5;
var matchmakerKeepAliveInterval = 30;
var maxPlayerCount = -1;
var gameSessionId;
var userSessionId;
var serverPublicIp;
// `clientConfig` is send to Streamer and Players
// Example of STUN server setting
// let clientConfig = {peerConnectionOptions: { 'iceServers': [{'urls': ['stun:34.250.222.95:19302']}] }};
var clientConfig = { type: 'config', peerConnectionOptions: {} };
// Parse public server address from command line
// --publicIp <public address>
try {
if (typeof config.PublicIp != 'undefined') {
serverPublicIp = config.PublicIp.toString();
}
if (typeof config.HttpPort != 'undefined') {
httpPort = config.HttpPort;
}
if (typeof config.HttpsPort != 'undefined') {
httpsPort = config.HttpsPort;
}
if (typeof config.StreamerPort != 'undefined') {
streamerPort = config.StreamerPort;
}
if (typeof config.SFUPort != 'undefined') {
sfuPort = config.SFUPort;
}
if (typeof config.FrontendUrl != 'undefined') {
FRONTEND_WEBSERVER = config.FrontendUrl;
}
if (typeof config.peerConnectionOptions != 'undefined') {
clientConfig.peerConnectionOptions = JSON.parse(config.peerConnectionOptions);
console.log(`peerConnectionOptions = ${JSON.stringify(clientConfig.peerConnectionOptions)}`);
} else {
console.log("No peerConnectionConfig")
}
if (typeof config.MatchmakerAddress != 'undefined') {
matchmakerAddress = config.MatchmakerAddress;
}
if (typeof config.MatchmakerPort != 'undefined') {
matchmakerPort = config.MatchmakerPort;
}
if (typeof config.MatchmakerRetryInterval != 'undefined') {
matchmakerRetryInterval = config.MatchmakerRetryInterval;
}
if (typeof config.MaxPlayerCount != 'undefined') {
maxPlayerCount = config.MaxPlayerCount;
}
} catch (e) {
console.error(e);
process.exit(2);
}
if (config.UseHTTPS) {
app.use(helmet());
app.use(hsts({
maxAge: 15552000 // 180 days in seconds
}));
//Setup http -> https redirect
console.log('Redirecting http->https');
app.use(function (req, res, next) {
if (!req.secure) {
if (req.get('Host')) {
var hostAddressParts = req.get('Host').split(':');
var hostAddress = hostAddressParts[0];
if (httpsPort != 443) {
hostAddress = `${hostAddress}:${httpsPort}`;
}
return res.redirect(['https://', hostAddress, req.originalUrl].join(''));
} else {
console.error(`unable to get host name from header. Requestor ${req.ip}, url path: '${req.originalUrl}', available headers ${JSON.stringify(req.headers)}`);
return res.status(400).send('Bad Request');
}
}
next();
});
}
sendGameSessionData();
// set up rate limiter: maximum of five requests per minute
var RateLimit = require('express-rate-limit');
var limiter = RateLimit({
windowMs: 1*60*1000, // 1 minute
max: 60
});
// apply rate limiter to all requests
app.use(limiter);
//Setup the login page if we are using authentication
if(config.UseAuthentication){
if(config.EnableWebserver) {
app.get('/login', function(req, res){
res.sendFile(path.join(__dirname, '/Public', '/login.html'));
});
}
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
//login page form data is posted here
app.post('/login',
urlencodedParser,
passport.authenticate('local', { failureRedirect: '/login' }),
function(req, res){
//On success try to redirect to the page that they originally tired to get to, default to '/' if no redirect was found
var redirectTo = req.session.redirectTo ? req.session.redirectTo : '/';
delete req.session.redirectTo;
console.log(`Redirecting to: '${redirectTo}'`);
res.redirect(redirectTo);
}
);
}
if(config.EnableWebserver) {
//Setup folders
app.use(express.static(path.join(__dirname, '/Public')))
app.use('/images', express.static(path.join(__dirname, './images')))
app.use('/scripts', [isAuthenticated('/login'),express.static(path.join(__dirname, '/scripts'))]);
app.use('/', [isAuthenticated('/login'), express.static(path.join(__dirname, '/custom_html'))])
}
try {
for (var property in config.AdditionalRoutes) {
if (config.AdditionalRoutes.hasOwnProperty(property)) {
console.log(`Adding additional routes "${property}" -> "${config.AdditionalRoutes[property]}"`)
app.use(property, [isAuthenticated('/login'), express.static(path.join(__dirname, config.AdditionalRoutes[property]))]);
}
}
} catch (err) {
console.error(`reading config.AdditionalRoutes: ${err}`)
}
if(config.EnableWebserver) {
// Request has been sent to site root, send the homepage file
app.get('/', isAuthenticated('/login'), function (req, res) {
homepageFile = (typeof config.HomepageFile != 'undefined' && config.HomepageFile != '') ? config.HomepageFile.toString() : defaultConfig.HomepageFile;
let pathsToTry = [ path.join(__dirname, homepageFile), path.join(__dirname, '/Public', homepageFile), path.join(__dirname, '/custom_html', homepageFile), homepageFile ];
// Try a few paths, see if any resolve to a homepage file the user has set
for(let pathToTry of pathsToTry){
if(fs.existsSync(pathToTry)){
// Send the file for browser to display it
res.sendFile(pathToTry);
return;
}
}
// Catch file doesn't exist, and send back 404 if not
console.error('Unable to locate file ' + homepageFile)
res.status(404).send('Unable to locate file ' + homepageFile);
return;
});
}
//Setup http and https servers
http.listen(httpPort, function () {
console.logColor(logging.Green, 'Http listening on *: ' + httpPort);
});
if (config.UseHTTPS) {
https.listen(httpsPort, function () {
console.logColor(logging.Green, 'Https listening on *: ' + httpsPort);
});
}
console.logColor(logging.Cyan, `Running Cirrus - The Pixel Streaming reference implementation signalling server for Unreal Engine 5.3.`);
let nextPlayerId = 1;
const StreamerType = { Regular: 0, SFU: 1 };
class Streamer {
constructor(initialId, ws, type) {
this.id = initialId;
this.ws = ws;
this.type = type;
this.idCommitted = false;
}
// registers this streamers id
commitId(id) {
this.id = id;
this.idCommitted = true;
}
// returns true if we have a valid id
isIdCommitted() {
return this.idCommitted;
}
// links this streamer to a subscribed SFU player (player component of an SFU)
addSFUPlayer(sfuPlayerId) {
if (!!this.SFUPlayerId && this.SFUPlayerId != sfuPlayerId) {
console.error(`Streamer ${this.id} already has an SFU ${this.SFUPlayerId}. Trying to add ${sfuPlayerId} as SFU.`);
return;
}
this.SFUPlayerId = sfuPlayerId;
}
// removes the previously subscribed SFU player
removeSFUPlayer() {
delete this.SFUPlayerId;
}
// gets the player id of the subscribed SFU if any
getSFUPlayerId() {
return this.SFUPlayerId;
}
// returns true if this streamer is forwarding another streamer
isSFU() {
return this.type == StreamerType.SFU;
}
// links this streamer to a player, used for SFU connections since they have both components
setSFUPlayerComponent(playerComponent) {
if (!this.isSFU()) {
console.error(`Trying to add an SFU player component ${playerComponent.id} to streamer ${this.id} but it is not an SFU type.`);
return;
}
this.sfuPlayerComponent = playerComponent;
}
// gets the player component for this sfu
getSFUPlayerComponent() {
if (!this.isSFU()) {
console.error(`Trying to get an SFU player component from streamer ${this.id} but it is not an SFU type.`);
return null;
}
return this.sfuPlayerComponent;
}
}
const PlayerType = { Regular: 0, SFU: 1 };
const WhoSendsOffer = { Streamer: 0, Browser: 1 };
class Player {
constructor(id, ws, type, whoSendsOffer) {
this.id = id;
this.ws = ws;
this.type = type;
this.whoSendsOffer = whoSendsOffer;
}
isSFU() {
return this.type == PlayerType.SFU;
}
subscribe(streamerId) {
if (!streamers.has(streamerId)) {
console.error(`subscribe: Player ${this.id} tried to subscribe to a non-existent streamer ${streamerId}`);
return;
}
this.streamerId = streamerId;
if (this.type == PlayerType.SFU) {
let streamer = streamers.get(this.streamerId);
streamer.addSFUPlayer(this.id);
}
const msg = { type: 'playerConnected', playerId: this.id, dataChannel: true, sfu: this.type == PlayerType.SFU, sendOffer: this.whoSendsOffer == WhoSendsOffer.Streamer };
logOutgoing(this.streamerId, msg);
this.sendFrom(msg);
}
unsubscribe() {
if (this.streamerId && streamers.has(this.streamerId)) {
if (this.type == PlayerType.SFU) {
let streamer = streamers.get(this.streamerId);
if (streamer.getSFUPlayerId() != this.id) {
console.error(`Trying to unsibscribe SFU player ${this.id} from streamer ${streamer.id} but the current SFUId does not match (${streamer.getSFUPlayerId()}).`)
} else {
streamer.removeSFUPlayer();
}
}
const msg = { type: 'playerDisconnected', playerId: this.id };
logOutgoing(this.streamerId, msg);
this.sendFrom(msg);
}
this.streamerId = null;
}
sendFrom(message) {
if (!this.streamerId) {
if(streamers.size > 0) {
this.streamerId = streamers.entries().next().value[0];
console.logColor(logging.Orange, `Player ${this.id} attempted to send an outgoing message without having subscribed first. Defaulting to ${this.streamerId}`);
} else {
console.logColor(logging.Orange, `Player ${this.id} attempted to send an outgoing message without having subscribed first. No streamer connected so this message isn't going anywhere!`)
return;
}
}
// normally we want to indicate what player this message came from
// but in some instances we might already have set this (streamerDataChannels) due to poor choices
if (!message.playerId) {
message.playerId = this.id;
}
const msgString = JSON.stringify(message);
let streamer = streamers.get(this.streamerId);
if (!streamer) {
console.error(`sendFrom: Player ${this.id} subscribed to non-existent streamer: ${this.streamerId}`);
} else {
streamer.ws.send(msgString);
}
}
sendTo(message) {
const msgString = JSON.stringify(message);
this.ws.send(msgString);
}
setSFUStreamerComponent(streamerComponent) {
if (!this.isSFU()) {
console.error(`Trying to add an SFU streamer component ${streamerComponent.id} to player ${this.id} but it is not an SFU type.`);
return;
}
this.sfuStreamerComponent = streamerComponent;
}
getSFUStreamerComponent() {
if (!this.isSFU()) {
console.error(`Trying to get an SFU streamer component from player ${this.id} but it is not an SFU type.`);
return null;
}
return this.sfuStreamerComponent;
}
};
let streamers = new Map(); // streamerId <-> streamer
let players = new Map(); // playerId <-> player/peer/viewer
const LegacyStreamerPrefix = "__LEGACY_STREAMER__"; // old streamers that dont know how to ID will be assigned this id prefix.
const streamerIdTimeoutSecs = 5;
// gets the SFU subscribed to this streamer if any.
function getSFUForStreamer(streamerId) {
if (!streamers.has(streamerId)) {
return null;
}
const streamer = streamers.get(streamerId);
const sfuPlayerId = streamer.getSFUPlayerId();
if (!!sfuPlayerId) {
return null;
}
return players.get(sfuPlayerId);
}
function logIncoming(sourceName, msg) {
if (config.LogVerbose)
console.logColor(logging.Blue, "\x1b[37m%s ->\x1b[34m %s", sourceName, JSON.stringify(msg));
else
console.logColor(logging.Blue, "\x1b[37m%s ->\x1b[34m %s", sourceName, msg.type);
}
function logOutgoing(destName, msg) {
if (config.LogVerbose)
console.logColor(logging.Green, "\x1b[37m%s <-\x1b[32m %s", destName, JSON.stringify(msg));
else
console.logColor(logging.Green, "\x1b[37m%s <-\x1b[32m %s", destName, msg.type);
}
function logForward(srcName, destName, msg) {
if (config.LogVerbose)
console.logColor(logging.Cyan, "\x1b[37m%s -> %s\x1b[36m %s", srcName, destName, JSON.stringify(msg));
else
console.logColor(logging.Cyan, "\x1b[37m%s -> %s\x1b[36m %s", srcName, destName, msg.type);
}
let WebSocket = require('ws');
let sfuMessageHandlers = new Map();
let playerMessageHandlers = new Map();
function sanitizePlayerId(playerId) {
if (playerId && typeof playerId === 'number') {
playerId = playerId.toString();
}
return playerId;
}
function getPlayerIdFromMessage(msg) {
return sanitizePlayerId(msg.playerId);
}
function getUniqueLegacyId() {
for (let i = 0; i < 99; ++i) {
const testId = LegacyStreamerPrefix + i;
if (!streamers.has(testId)) {
return testId;
}
}
return ""; // no available id
}
function getUniqueSFUId() {
for (let i = 0; i < 99; ++i) {
const testId = SFUStreamerPrefix + i;
let available = true;
for (let player of players) {
if (player.type == PlayerType.SFU) {
if (player.streamer.id == testId) {
available = false;
break;
}
}
}
if (available) {
return testId;
}
}
return ""; // no available id
}
function requestStreamerId(streamer) {
// first we ask the streamer to id itself.
// if it doesnt reply within a time limit we assume it's an older streamer
// and assign it an id.
// request id
const msg = { type: "identify" };
logOutgoing(streamer.id, msg);
streamer.ws.send(JSON.stringify(msg));
streamer.idTimer = setTimeout(function() {
// streamer did not respond in time. give it a legacy id.
const newLegacyId = getUniqueLegacyId();
if (newLegacyId.length == 0) {
const error = `Ran out of legacy ids.`;
console.error(error);
streamer.ws.close(1008, error);
} else {
registerStreamer(newLegacyId, streamer);
}
}, streamerIdTimeoutSecs * 1000);
}
function sanitizeStreamerId(id) {
let maxPostfix = -1;
for (let [streamerId, streamer] of streamers) {
const idMatchRegex = /^(.*?)(\d*)$/;
const [, baseId, postfix] = streamerId.match(idMatchRegex);
if (baseId != id) {
continue;
}
const numPostfix = Number(postfix);
if (numPostfix > maxPostfix) {
maxPostfix = numPostfix
}
}
if (maxPostfix >= 0) {
return id + (maxPostfix + 1);
}
return id;
}
function registerStreamer(id, streamer) {
// make sure the id is unique
const uniqueId = sanitizeStreamerId(id);
streamer.commitId(uniqueId);
if (!!streamer.idTimer) {
clearTimeout(streamer.idTimer);
delete streamer.idTimer;
}
streamers.set(uniqueId, streamer);
console.logColor(logging.Green, `Registered new streamer: ${streamer.id}`);
}
function onStreamerDisconnected(streamer) {
if (!streamer.id || !streamers.has(streamer.id)) {
return;
}
sendStreamerDisconnectedToMatchmaker();
let sfuPlayer = getSFUForStreamer(streamer.id);
if (sfuPlayer) {
const msg = { type: "streamerDisconnected" };
logOutgoing(sfuPlayer.id, msg);
sfuPlayer.sendTo(msg);
disconnectAllPlayers(sfuPlayer.id);
}
disconnectAllPlayers(streamer.id);
streamers.delete(streamer.id);
}
function onStreamerMessageId(streamer, msg) {
logIncoming(streamer.id, msg);
let streamerId = msg.id;
registerStreamer(streamerId, streamer);
}
function onStreamerMessagePing(streamer, msg) {
logIncoming(streamer.id, msg);
const pongMsg = JSON.stringify({ type: "pong", time: msg.time});
streamer.ws.send(pongMsg);
}
function onStreamerMessageDisconnectPlayer(streamer, msg) {
logIncoming(streamer.id, msg);
const playerId = getPlayerIdFromMessage(msg);
const player = players.get(playerId);
if (player) {
player.ws.close(1011 /* internal error */, msg.reason);
}
}
function onStreamerMessageLayerPreference(streamer, msg) {
let sfuPlayer = getSFUForStreamer(streamer.id);
if (sfuPlayer) {
logOutgoing(sfuPlayer.id, msg);
sfuPlayer.sendTo(msg);
}
}
function forwardStreamerMessageToPlayer(streamer, msg) {
const playerId = getPlayerIdFromMessage(msg);
const player = players.get(playerId);
if (player) {
delete msg.playerId;
logForward(streamer.id, playerId, msg);
player.sendTo(msg);
} else {
console.warn("No playerId specified, cannot forward message: %s", msg);
}
}
let streamerMessageHandlers = new Map();
streamerMessageHandlers.set('endpointId', onStreamerMessageId);
streamerMessageHandlers.set('ping', onStreamerMessagePing);
streamerMessageHandlers.set('offer', forwardStreamerMessageToPlayer);
streamerMessageHandlers.set('answer', forwardStreamerMessageToPlayer);
streamerMessageHandlers.set('iceCandidate', forwardStreamerMessageToPlayer);
streamerMessageHandlers.set('disconnectPlayer', onStreamerMessageDisconnectPlayer);
streamerMessageHandlers.set('layerPreference', onStreamerMessageLayerPreference);
console.logColor(logging.Green, `WebSocket listening for Streamer connections on :${streamerPort}`)
let streamerServer = new WebSocket.Server({ port: streamerPort, backlog: 1 });
streamerServer.on('connection', function (ws, req) {
console.logColor(logging.Green, `Streamer connected: ${req.connection.remoteAddress}`);
sendStreamerConnectedToMatchmaker();
const temporaryId = req.connection.remoteAddress;
let streamer = new Streamer(temporaryId, ws, StreamerType.Regular);
ws.on('message', (msgRaw) => {
var msg;
try {
msg = JSON.parse(msgRaw);
} catch(err) {
console.error(`Cannot parse Streamer message: ${msgRaw}\nError: ${err}`);
ws.close(1008, 'Cannot parse');
return;
}
console.log(msgRaw);
let handler = streamerMessageHandlers.get(msg.type);
if (!handler || (typeof handler != 'function')) {
if (config.LogVerbose) {
console.logColor(logging.White, "\x1b[37m-> %s\x1b[34m: %s", streamer.id, msgRaw);
}
console.error(`unsupported Streamer message type: ${msg.type}`);
ws.close(1008, 'Unsupported message type');
return;
}
handler(streamer, msg);
});
ws.on('close', function(code, reason) {
console.error(`streamer ${streamer.id} disconnected: ${code} - ${reason}`);
onStreamerDisconnected(streamer);
});
ws.on('error', function(error) {
console.error(`streamer ${streamer.id} connection error: ${error}`);
onStreamerDisconnected(streamer);
try {
ws.close(1006 /* abnormal closure */, error);
} catch(err) {
console.error(`ERROR: ws.on error: ${err.message}`);
}
});
ws.send(JSON.stringify(clientConfig));
requestStreamerId(streamer);
});
function forwardSFUMessageToPlayer(sfuPlayer, msg) {
const playerId = getPlayerIdFromMessage(msg);
const player = players.get(playerId);
if (player) {
logForward(sfuPlayer.getSFUStreamerComponent().id, playerId, msg);
player.sendTo(msg);
}
}
function forwardSFUMessageToStreamer(sfuPlayer, msg) {
logForward(sfuPlayer.getSFUStreamerComponent().id, sfuPlayer.streamerId, msg);
msg.sfuId = sfuPlayer.id;
sfuPlayer.sendFrom(msg);
}
function onPeerDataChannelsSFUMessage(sfuPlayer, msg) {
// sfu is telling a peer what stream id to use for a data channel
const playerId = getPlayerIdFromMessage(msg);
const player = players.get(playerId);
if (player) {
logForward(sfuPlayer.getSFUStreamerComponent().id, playerId, msg);
player.sendTo(msg);
player.datachannel = true;
}
}
// basically a duplicate of the streamer id request but this one does not register the streamer
function requestSFUStreamerId(sfuPlayer) {
// request id
const msg = { type: "identify" };
const sfuStreamerComponent = sfuPlayer.getSFUStreamerComponent();
logOutgoing(sfuStreamerComponent.id, msg);
sfuStreamerComponent.ws.send(JSON.stringify(msg));
sfuStreamerComponent.idTimer = setTimeout(function() {
// streamer did not respond in time. give it a legacy id.
const newLegacyId = getUniqueSFUId();
if (newLegacyId.length == 0) {
const error = `Ran out of legacy ids.`;
console.error(error);
sfuPlayer.ws.close(1008, error);
} else {
sfuStreamerComponent.id = newLegacyId;
}
}, streamerIdTimeoutSecs * 1000);
}
function onSFUMessageId(sfuPlayer, msg) {
const sfuStreamerComponent = sfuPlayer.getSFUStreamerComponent();
logIncoming(sfuStreamerComponent.id, msg);
sfuStreamerComponent.id = msg.id;
if (!!sfuStreamerComponent.idTimer) {
clearTimeout(sfuStreamerComponent.idTimer);
delete sfuStreamerComponent.idTimer;
}
}
function onSFUMessageStartStreaming(sfuPlayer, msg) {
const sfuStreamerComponent = sfuPlayer.getSFUStreamerComponent();
logIncoming(sfuStreamerComponent.id, msg);
if (streamers.has(sfuStreamerComponent.id)) {
console.error(`SFU ${sfuStreamerComponent.id} is already registered as a streamer and streaming.`)
return;
}
registerStreamer(sfuStreamerComponent.id, sfuStreamerComponent);
}
function onSFUMessageStopStreaming(sfuPlayer, msg) {
const sfuStreamerComponent = sfuPlayer.getSFUStreamerComponent();
logIncoming(sfuStreamerComponent.id, msg);
if (!streamers.has(sfuStreamerComponent.id)) {
console.error(`SFU ${sfuStreamerComponent.id} is not registered as a streamer or streaming.`)
return;
}
onStreamerDisconnected(sfuStreamerComponent);
}
function onSFUDisconnected(sfuPlayer) {
console.log("disconnecting SFU from streamer");
disconnectAllPlayers(sfuPlayer.id);
onStreamerDisconnected(sfuPlayer.getSFUStreamerComponent());
sfuPlayer.unsubscribe();
sfuPlayer.ws.close(4000, "SFU Disconnected");
players.delete(sfuPlayer.id);
streamers.delete(sfuPlayer.id);
}
sfuMessageHandlers.set('listStreamers', onPlayerMessageListStreamers);
sfuMessageHandlers.set('subscribe', onPlayerMessageSubscribe);
sfuMessageHandlers.set('unsubscribe', onPlayerMessageUnsubscribe);
sfuMessageHandlers.set('offer', forwardSFUMessageToPlayer);
sfuMessageHandlers.set('answer', forwardSFUMessageToStreamer);
sfuMessageHandlers.set('streamerDataChannels', forwardSFUMessageToStreamer);
sfuMessageHandlers.set('peerDataChannels', onPeerDataChannelsSFUMessage);
sfuMessageHandlers.set('endpointId', onSFUMessageId);
sfuMessageHandlers.set('startStreaming', onSFUMessageStartStreaming);
sfuMessageHandlers.set('stopStreaming', onSFUMessageStopStreaming);
console.logColor(logging.Green, `WebSocket listening for SFU connections on :${sfuPort}`);
let sfuServer = new WebSocket.Server({ port: sfuPort });
sfuServer.on('connection', function (ws, req) {
let playerId = sanitizePlayerId(nextPlayerId++);
console.logColor(logging.Green, `SFU (${req.connection.remoteAddress}) connected `);
let streamerComponent = new Streamer(req.connection.remoteAddress, ws, StreamerType.SFU);
let playerComponent = new Player(playerId, ws, PlayerType.SFU, WhoSendsOffer.Streamer);
streamerComponent.setSFUPlayerComponent(playerComponent);
playerComponent.setSFUStreamerComponent(streamerComponent);
players.set(playerId, playerComponent);
ws.on('message', (msgRaw) => {
var msg;
try {
msg = JSON.parse(msgRaw);
} catch (err) {
console.error(`Cannot parse SFU message: ${msgRaw}\nError: ${err}`);
ws.close(1008, 'Cannot parse');
return;
}
let sfuPlayer = players.get(playerId);
if (!sfuPlayer) {
console.error(`Received a message from an SFU not in the player list ${playerId}`);
ws.close(1001, 'Broken');
return;
}
let handler = sfuMessageHandlers.get(msg.type);
if (!handler || (typeof handler != 'function')) {
if (config.LogVerbose) {
console.logColor(logging.White, "\x1b[37m-> %s\x1b[34m: %s", sfuPlayer.id, msgRaw);
}
console.error(`unsupported SFU message type: ${msg.type}`);
ws.close(1008, 'Unsupported message type');
return;
}
handler(sfuPlayer, msg);
});
ws.on('close', function(code, reason) {
console.error(`SFU disconnected: ${code} - ${reason}`);
onSFUDisconnected(playerComponent);
});
ws.on('error', function(error) {
console.error(`SFU connection error: ${error}`);
onSFUDisconnected(playerComponent);
try {
ws.close(1006 /* abnormal closure */, error);
} catch(err) {
console.error(`ERROR: ws.on error: ${err.message}`);
}
});
requestStreamerId(playerComponent.getSFUStreamerComponent());
});
let playerCount = 0;
function sendPlayersCount() {
const msg = { type: 'playerCount', count: players.size };
logOutgoing("[players]", msg);
for (let player of players.values()) {
player.sendTo(msg);
}
}
function onPlayerMessageSubscribe(player, msg) {
logIncoming(player.id, msg);
player.subscribe(msg.streamerId);
}
function onPlayerMessageUnsubscribe(player, msg) {
logIncoming(player.id, msg);
player.unsubscribe();
}
function onPlayerMessageStats(player, msg) {
console.log(`player ${playerId}: stats\n${msg.data}`);
}
function onPlayerMessageListStreamers(player, msg) {
logIncoming(player.id, msg);
let reply = { type: 'streamerList', ids: [] };
for (let [streamerId, streamer] of streamers) {
reply.ids.push(streamerId);
}
logOutgoing(player.id, reply);
player.sendTo(reply);
}
function forwardPlayerMessage(player, msg) {
logForward(player.id, player.streamerId, msg);
player.sendFrom(msg);
}
function onPlayerDisconnected(playerId) {
const player = players.get(playerId);
player.unsubscribe();
players.delete(playerId);
--playerCount;
sendPlayersCount();
sendPlayerDisconnectedToFrontend();
sendPlayerDisconnectedToMatchmaker();
}
playerMessageHandlers.set('subscribe', onPlayerMessageSubscribe);
playerMessageHandlers.set('unsubscribe', onPlayerMessageUnsubscribe);
playerMessageHandlers.set('stats', onPlayerMessageStats);
playerMessageHandlers.set('offer', forwardPlayerMessage);
playerMessageHandlers.set('answer', forwardPlayerMessage);
playerMessageHandlers.set('iceCandidate', forwardPlayerMessage);
playerMessageHandlers.set('listStreamers', onPlayerMessageListStreamers);
// sfu related messages
playerMessageHandlers.set('dataChannelRequest', forwardPlayerMessage);
playerMessageHandlers.set('peerDataChannelsReady', forwardPlayerMessage);
console.logColor(logging.Green, `WebSocket listening for Players connections on :${httpPort}`);
let playerServer = new WebSocket.Server({ server: config.UseHTTPS ? https : http});
playerServer.on('connection', function (ws, req) {
var url = require('url');
const parsedUrl = url.parse(req.url);
const urlParams = new URLSearchParams(parsedUrl.search);
const whoSendsOffer = urlParams.has('OfferToReceive') && urlParams.get('OfferToReceive') !== 'false' ? WhoSendsOffer.Browser : WhoSendsOffer.Streamer;
if (playerCount + 1 > maxPlayerCount && maxPlayerCount !== -1)
{
console.logColor(logging.Red, `new connection would exceed number of allowed concurrent connections. Max: ${maxPlayerCount}, Current ${playerCount}`);
ws.close(1013, `too many connections. max: ${maxPlayerCount}, current: ${playerCount}`);
return;
}
++playerCount;
let playerId = sanitizePlayerId(nextPlayerId++);
console.logColor(logging.Green, `player ${playerId} (${req.connection.remoteAddress}) connected`);
let player = new Player(playerId, ws, PlayerType.Regular, whoSendsOffer);
players.set(playerId, player);
ws.on('message', (msgRaw) =>{
var msg;
try {
msg = JSON.parse(msgRaw);
} catch (err) {
console.error(`Cannot parse player ${playerId} message: ${msgRaw}\nError: ${err}`);
ws.close(1008, 'Cannot parse');
return;
}
let player = players.get(playerId);
if (!player) {
console.error(`Received a message from a player not in the player list ${playerId}`);
ws.close(1001, 'Broken');
return;
}
let handler = playerMessageHandlers.get(msg.type);
if (!handler || (typeof handler != 'function')) {
if (config.LogVerbose) {
console.logColor(logging.White, "\x1b[37m-> %s\x1b[34m: %s", playerId, msgRaw);
}
console.error(`unsupported player message type: ${msg.type}`);
ws.close(1008, 'Unsupported message type');
return;
}
handler(player, msg);
});
ws.on('close', function(code, reason) {
console.logColor(logging.Yellow, `player ${playerId} connection closed: ${code} - ${reason}`);
onPlayerDisconnected(playerId);
});
ws.on('error', function(error) {
console.error(`player ${playerId} connection error: ${error}`);
ws.close(1006 /* abnormal closure */, error);
onPlayerDisconnected(playerId);
console.logColor(logging.Red, `Trying to reconnect...`);
reconnect();
});
sendPlayerConnectedToFrontend();
sendPlayerConnectedToMatchmaker();