@epicgames-ps/lib-pixelstreamingsignalling-ue5.8 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.lintstagedrc.mjs +7 -0
- package/.prettierignore +0 -0
- package/CHANGELOG.md +21 -0
- package/README.md +32 -0
- package/dist/cjs/Logger.js +148 -0
- package/dist/cjs/Logger.js.map +1 -0
- package/dist/cjs/LoggingUtils.js +64 -0
- package/dist/cjs/LoggingUtils.js.map +1 -0
- package/dist/cjs/PlayerConnection.js +220 -0
- package/dist/cjs/PlayerConnection.js.map +1 -0
- package/dist/cjs/PlayerRegistry.js +77 -0
- package/dist/cjs/PlayerRegistry.js.map +1 -0
- package/dist/cjs/SFUConnection.js +264 -0
- package/dist/cjs/SFUConnection.js.map +1 -0
- package/dist/cjs/SignallingServer.js +149 -0
- package/dist/cjs/SignallingServer.js.map +1 -0
- package/dist/cjs/StreamerConnection.js +172 -0
- package/dist/cjs/StreamerConnection.js.map +1 -0
- package/dist/cjs/StreamerRegistry.js +149 -0
- package/dist/cjs/StreamerRegistry.js.map +1 -0
- package/dist/cjs/Utils.js +19 -0
- package/dist/cjs/Utils.js.map +1 -0
- package/dist/cjs/WebServer.js +124 -0
- package/dist/cjs/WebServer.js.map +1 -0
- package/dist/cjs/pixelstreamingsignalling.js +28 -0
- package/dist/cjs/pixelstreamingsignalling.js.map +1 -0
- package/dist/esm/Logger.js +141 -0
- package/dist/esm/Logger.js.map +1 -0
- package/dist/esm/LoggingUtils.js +58 -0
- package/dist/esm/LoggingUtils.js.map +1 -0
- package/dist/esm/PlayerConnection.js +183 -0
- package/dist/esm/PlayerConnection.js.map +1 -0
- package/dist/esm/PlayerRegistry.js +73 -0
- package/dist/esm/PlayerRegistry.js.map +1 -0
- package/dist/esm/SFUConnection.js +227 -0
- package/dist/esm/SFUConnection.js.map +1 -0
- package/dist/esm/SignallingServer.js +112 -0
- package/dist/esm/SignallingServer.js.map +1 -0
- package/dist/esm/StreamerConnection.js +135 -0
- package/dist/esm/StreamerConnection.js.map +1 -0
- package/dist/esm/StreamerRegistry.js +145 -0
- package/dist/esm/StreamerRegistry.js.map +1 -0
- package/dist/esm/Utils.js +15 -0
- package/dist/esm/Utils.js.map +1 -0
- package/dist/esm/WebServer.js +117 -0
- package/dist/esm/WebServer.js.map +1 -0
- package/dist/esm/pixelstreamingsignalling.js +12 -0
- package/dist/esm/pixelstreamingsignalling.js.map +1 -0
- package/dist/types/Logger.d.ts +20 -0
- package/dist/types/LoggingUtils.d.ts +44 -0
- package/dist/types/PlayerConnection.d.ts +66 -0
- package/dist/types/PlayerRegistry.d.ts +66 -0
- package/dist/types/SFUConnection.d.ts +85 -0
- package/dist/types/SignallingServer.d.ts +48 -0
- package/dist/types/StreamerConnection.d.ts +61 -0
- package/dist/types/StreamerRegistry.d.ts +96 -0
- package/dist/types/Utils.d.ts +8 -0
- package/dist/types/WebServer.d.ts +42 -0
- package/dist/types/pixelstreamingsignalling.d.ts +9 -0
- package/docs/Logger/README.md +19 -0
- package/docs/Logger/functions/InitLogging.md +26 -0
- package/docs/Logger/interfaces/IConfig.md +41 -0
- package/docs/Logger/variables/Logger.md +15 -0
- package/docs/LoggingUtils/README.md +19 -0
- package/docs/LoggingUtils/functions/createHandlerListener.md +42 -0
- package/docs/LoggingUtils/functions/logForward.md +36 -0
- package/docs/LoggingUtils/functions/logIncoming.md +30 -0
- package/docs/LoggingUtils/functions/logOutgoing.md +30 -0
- package/docs/LoggingUtils/interfaces/IMessageLogger.md +29 -0
- package/docs/LoggingUtils/interfaces/IProtoLogObj.md +57 -0
- package/docs/PlayerConnection/README.md +11 -0
- package/docs/PlayerConnection/classes/PlayerConnection.md +177 -0
- package/docs/PlayerRegistry/README.md +16 -0
- package/docs/PlayerRegistry/classes/PlayerRegistry.md +423 -0
- package/docs/PlayerRegistry/interfaces/IPlayer.md +86 -0
- package/docs/PlayerRegistry/interfaces/IPlayerInfo.md +43 -0
- package/docs/README.md +19 -0
- package/docs/SFUConnection/README.md +11 -0
- package/docs/SFUConnection/classes/SFUConnection.md +554 -0
- package/docs/SignallingServer/README.md +19 -0
- package/docs/SignallingServer/classes/SignallingServer.md +76 -0
- package/docs/SignallingServer/interfaces/IServerConfig.md +92 -0
- package/docs/SignallingServer/type-aliases/ProtocolConfig.md +15 -0
- package/docs/StreamerConnection/README.md +11 -0
- package/docs/StreamerConnection/classes/StreamerConnection.md +507 -0
- package/docs/StreamerRegistry/README.md +16 -0
- package/docs/StreamerRegistry/classes/StreamerRegistry.md +431 -0
- package/docs/StreamerRegistry/interfaces/IStreamer.md +375 -0
- package/docs/StreamerRegistry/interfaces/IStreamerInfo.md +51 -0
- package/docs/Utils/README.md +12 -0
- package/docs/Utils/functions/beautify.md +23 -0
- package/docs/Utils/functions/stringify.md +23 -0
- package/docs/WebServer/README.md +15 -0
- package/docs/WebServer/classes/WebServer.md +50 -0
- package/docs/WebServer/interfaces/IWebServerConfig.md +76 -0
- package/docs/pixelstreamingsignalling/README.md +145 -0
- package/eslint.config.mjs +39 -0
- package/package.json +58 -0
- package/src/Logger.ts +189 -0
- package/src/LoggingUtils.ts +80 -0
- package/src/PlayerConnection.ts +257 -0
- package/src/PlayerRegistry.ts +116 -0
- package/src/SFUConnection.ts +329 -0
- package/src/SignallingServer.ts +214 -0
- package/src/StreamerConnection.ts +194 -0
- package/src/StreamerRegistry.ts +252 -0
- package/src/Utils.ts +16 -0
- package/src/WebServer.ts +213 -0
- package/src/pixelstreamingsignalling.ts +11 -0
- package/tsconfig.cjs.json +8 -0
- package/tsconfig.esm.json +8 -0
- package/tsconfig.json +29 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { WebSocketTransportNJS, SignallingProtocol, MessageHelpers, Messages } from '@epicgames-ps/lib-pixelstreamingcommon-ue5.8';
|
|
2
|
+
import { Logger } from './Logger';
|
|
3
|
+
import * as LogUtils from './LoggingUtils';
|
|
4
|
+
/**
|
|
5
|
+
* A connection between the signalling server and a player connection.
|
|
6
|
+
* This is where messages expected to be handled by the player come in
|
|
7
|
+
* and where messages are sent to the player.
|
|
8
|
+
*
|
|
9
|
+
* Interesting internals:
|
|
10
|
+
* playerId: The unique id string of this player.
|
|
11
|
+
* transport: The ITransport where transport events can be subscribed to
|
|
12
|
+
* protocol: The SignallingProtocol where signalling messages can be
|
|
13
|
+
* subscribed to.
|
|
14
|
+
*/
|
|
15
|
+
export class PlayerConnection {
|
|
16
|
+
/**
|
|
17
|
+
* Initializes a new connection with given and sane values. Adds listeners for the
|
|
18
|
+
* websocket close and error so it can react by unsubscribing and resetting itself.
|
|
19
|
+
* @param server - The signalling server object that spawned this player.
|
|
20
|
+
* @param ws - The websocket coupled to this player connection.
|
|
21
|
+
* @param remoteAddress - The remote address of this connection. Only used as display.
|
|
22
|
+
* @param request - The HTTP upgrade request that opened this connection, if available.
|
|
23
|
+
*/
|
|
24
|
+
constructor(server, ws, remoteAddress, request) {
|
|
25
|
+
this.server = server;
|
|
26
|
+
this.playerId = '';
|
|
27
|
+
this.subscribedStreamer = null;
|
|
28
|
+
this.transport = new WebSocketTransportNJS(ws);
|
|
29
|
+
this.protocol = new SignallingProtocol(this.transport);
|
|
30
|
+
this.remoteAddress = remoteAddress;
|
|
31
|
+
this.request = request;
|
|
32
|
+
this.transport.on('error', this.onTransportError.bind(this));
|
|
33
|
+
this.transport.on('close', this.onTransportClose.bind(this));
|
|
34
|
+
this.streamerIdChangeListener = this.onStreamerIdChanged.bind(this);
|
|
35
|
+
this.streamerDisconnectedListener = this.onStreamerDisconnected.bind(this);
|
|
36
|
+
this.registerMessageHandlers();
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Returns an identifier that is displayed in logs.
|
|
40
|
+
* @returns A string describing this connection.
|
|
41
|
+
*/
|
|
42
|
+
getReadableIdentifier() {
|
|
43
|
+
return this.playerId;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Sends a signalling message to the player.
|
|
47
|
+
* @param message - The message to send.
|
|
48
|
+
*/
|
|
49
|
+
sendMessage(message) {
|
|
50
|
+
LogUtils.logOutgoing(this, message);
|
|
51
|
+
this.protocol.sendMessage(message);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Returns a descriptive object for the REST API inspection operations.
|
|
55
|
+
* @returns An IPlayerInfo object containing viewable information about this connection.
|
|
56
|
+
*/
|
|
57
|
+
getPlayerInfo() {
|
|
58
|
+
var _a;
|
|
59
|
+
return {
|
|
60
|
+
playerId: this.playerId,
|
|
61
|
+
type: 'Player',
|
|
62
|
+
subscribedTo: (_a = this.subscribedStreamer) === null || _a === void 0 ? void 0 : _a.streamerId,
|
|
63
|
+
remoteAddress: this.remoteAddress
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
registerMessageHandlers() {
|
|
67
|
+
/* eslint-disable @typescript-eslint/unbound-method */
|
|
68
|
+
this.protocol.on(Messages.subscribe.typeName, LogUtils.createHandlerListener(this, this.onSubscribeMessage));
|
|
69
|
+
this.protocol.on(Messages.unsubscribe.typeName, LogUtils.createHandlerListener(this, this.onUnsubscribeMessage));
|
|
70
|
+
this.protocol.on(Messages.listStreamers.typeName, LogUtils.createHandlerListener(this, this.onListStreamers));
|
|
71
|
+
this.protocol.on(Messages.ping.typeName, LogUtils.createHandlerListener(this, this.onPingMessage));
|
|
72
|
+
/* eslint-enable @typescript-eslint/unbound-method */
|
|
73
|
+
this.protocol.on(Messages.offer.typeName, this.sendToStreamer.bind(this));
|
|
74
|
+
this.protocol.on(Messages.answer.typeName, this.sendToStreamer.bind(this));
|
|
75
|
+
this.protocol.on(Messages.iceCandidate.typeName, this.sendToStreamer.bind(this));
|
|
76
|
+
this.protocol.on(Messages.dataChannelRequest.typeName, this.sendToStreamer.bind(this));
|
|
77
|
+
this.protocol.on(Messages.peerDataChannelsReady.typeName, this.sendToStreamer.bind(this));
|
|
78
|
+
this.protocol.on(Messages.layerPreference.typeName, this.sendToStreamer.bind(this));
|
|
79
|
+
this.protocol.on('unhandled', (message) => {
|
|
80
|
+
Logger.warn(`Unhandled player protocol message: ${JSON.stringify(message)}`);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
sendToStreamer(message) {
|
|
84
|
+
if (!this.subscribedStreamer) {
|
|
85
|
+
Logger.warn(`Player ${this.playerId} tried to send to a streamer but they're not subscribed to any.`);
|
|
86
|
+
const streamerId = this.server.streamerRegistry.getFirstStreamerId();
|
|
87
|
+
if (!streamerId) {
|
|
88
|
+
Logger.error('There are no streamers to force a subscription. Disconnecting.');
|
|
89
|
+
this.disconnect();
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
Logger.warn(`Subscribing to ${streamerId}`);
|
|
94
|
+
this.subscribe(streamerId);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
message.playerId = this.playerId;
|
|
98
|
+
LogUtils.logForward(this, this.subscribedStreamer, message);
|
|
99
|
+
this.subscribedStreamer.protocol.sendMessage(message);
|
|
100
|
+
}
|
|
101
|
+
subscribe(streamerId) {
|
|
102
|
+
const streamer = this.server.streamerRegistry.find(streamerId);
|
|
103
|
+
if (!streamer) {
|
|
104
|
+
Logger.error(`subscribe: Player ${this.playerId} tried to subscribe to a non-existent streamer ${streamerId}`);
|
|
105
|
+
const failureMessage = MessageHelpers.createMessage(Messages.subscribeFailed, {
|
|
106
|
+
message: `Streamer ${streamerId} does not exist.`
|
|
107
|
+
});
|
|
108
|
+
this.protocol.sendMessage(failureMessage);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (this.subscribedStreamer) {
|
|
112
|
+
Logger.warn(`subscribe: Player ${this.playerId} is resubscribing to a streamer but is already subscribed to ${this.subscribedStreamer.streamerId}`);
|
|
113
|
+
this.unsubscribe();
|
|
114
|
+
}
|
|
115
|
+
if (streamer.maxSubscribers > 0 && streamer.subscribers.size >= streamer.maxSubscribers) {
|
|
116
|
+
Logger.error(`subscribe: Player ${this.playerId} could not subscribe to ${streamerId}. Max players (${streamer.maxSubscribers}) reached.`);
|
|
117
|
+
const failureMessage = MessageHelpers.createMessage(Messages.subscribeFailed, {
|
|
118
|
+
message: `Streamer ${streamerId} is full. Max players = ${streamer.maxSubscribers}.`
|
|
119
|
+
});
|
|
120
|
+
this.protocol.sendMessage(failureMessage);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
this.subscribedStreamer = streamer;
|
|
124
|
+
this.subscribedStreamer.subscribers.add(this.playerId);
|
|
125
|
+
this.subscribedStreamer.on('id_changed', this.streamerIdChangeListener);
|
|
126
|
+
this.subscribedStreamer.on('disconnect', this.streamerDisconnectedListener);
|
|
127
|
+
const connectedMessage = MessageHelpers.createMessage(Messages.playerConnected, {
|
|
128
|
+
playerId: this.playerId,
|
|
129
|
+
dataChannel: true,
|
|
130
|
+
sfu: false
|
|
131
|
+
});
|
|
132
|
+
this.sendToStreamer(connectedMessage);
|
|
133
|
+
}
|
|
134
|
+
unsubscribe() {
|
|
135
|
+
if (!this.subscribedStreamer) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
this.subscribedStreamer.subscribers.delete(this.playerId);
|
|
139
|
+
const disconnectedMessage = MessageHelpers.createMessage(Messages.playerDisconnected, {
|
|
140
|
+
playerId: this.playerId
|
|
141
|
+
});
|
|
142
|
+
this.sendToStreamer(disconnectedMessage);
|
|
143
|
+
this.subscribedStreamer.off('id_changed', this.streamerIdChangeListener);
|
|
144
|
+
this.subscribedStreamer.off('disconnect', this.streamerDisconnectedListener);
|
|
145
|
+
this.subscribedStreamer = null;
|
|
146
|
+
}
|
|
147
|
+
disconnect() {
|
|
148
|
+
this.unsubscribe();
|
|
149
|
+
this.protocol.disconnect();
|
|
150
|
+
}
|
|
151
|
+
onStreamerDisconnected() {
|
|
152
|
+
this.disconnect();
|
|
153
|
+
}
|
|
154
|
+
onTransportError(error) {
|
|
155
|
+
Logger.error(`Player (${this.playerId}) transport error ${error.message}`);
|
|
156
|
+
}
|
|
157
|
+
onTransportClose(_event) {
|
|
158
|
+
Logger.debug('PlayerConnection transport close.');
|
|
159
|
+
this.disconnect();
|
|
160
|
+
}
|
|
161
|
+
onSubscribeMessage(message) {
|
|
162
|
+
this.subscribe(message.streamerId);
|
|
163
|
+
}
|
|
164
|
+
onUnsubscribeMessage(_message) {
|
|
165
|
+
this.unsubscribe();
|
|
166
|
+
}
|
|
167
|
+
onListStreamers(_message) {
|
|
168
|
+
const listMessage = MessageHelpers.createMessage(Messages.streamerList, {
|
|
169
|
+
ids: this.server.streamerRegistry.streamers
|
|
170
|
+
.filter((streamer) => streamer.streaming)
|
|
171
|
+
.map((streamer) => streamer.streamerId)
|
|
172
|
+
});
|
|
173
|
+
this.sendMessage(listMessage);
|
|
174
|
+
}
|
|
175
|
+
onStreamerIdChanged(newId) {
|
|
176
|
+
const renameMessage = MessageHelpers.createMessage(Messages.streamerIdChanged, { newID: newId });
|
|
177
|
+
this.sendMessage(renameMessage);
|
|
178
|
+
}
|
|
179
|
+
onPingMessage(message) {
|
|
180
|
+
this.sendMessage(MessageHelpers.createMessage(Messages.pong, { time: message.time }));
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
//# sourceMappingURL=PlayerConnection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlayerConnection.js","sourceRoot":"","sources":["../../src/PlayerConnection.ts"],"names":[],"mappings":"AAGA,OAAO,EAEH,qBAAqB,EACrB,kBAAkB,EAClB,cAAc,EACd,QAAQ,EAEX,MAAM,8CAA8C,CAAC;AAGtD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,KAAK,QAAQ,MAAM,gBAAgB,CAAC;AAG3C;;;;;;;;;;GAUG;AACH,MAAM,OAAO,gBAAgB;IAkBzB;;;;;;;OAOG;IACH,YAAY,MAAwB,EAAE,EAAa,EAAE,aAAsB,EAAE,OAAyB;QAClG,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,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AAC,IAAI,EAAE,CAAC,CAAC,CAAC;IAC1F,CAAC;CACJ"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { EventEmitter } from '@epicgames-ps/lib-pixelstreamingcommon-ue5.8';
|
|
2
|
+
import { Logger } from './Logger';
|
|
3
|
+
/**
|
|
4
|
+
* Handles all the player connections of a signalling server and
|
|
5
|
+
* can be used to lookup connections by id etc.
|
|
6
|
+
* Fires events when players are added or removed.
|
|
7
|
+
* Events:
|
|
8
|
+
* 'added': (playerId: string) Player was added.
|
|
9
|
+
* 'removed': (playerId: string) Player was removed.
|
|
10
|
+
*/
|
|
11
|
+
export class PlayerRegistry extends EventEmitter {
|
|
12
|
+
constructor() {
|
|
13
|
+
super();
|
|
14
|
+
this.players = new Map();
|
|
15
|
+
this.players = new Map();
|
|
16
|
+
this.nextPlayerId = 0;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Assigns a unique id to the player and adds it to the registry
|
|
20
|
+
*/
|
|
21
|
+
add(player) {
|
|
22
|
+
player.playerId = this.getUniquePlayerId();
|
|
23
|
+
this.players.set(player.playerId, player);
|
|
24
|
+
this.emit('added', player.playerId);
|
|
25
|
+
Logger.info(`Registered new player: ${player.playerId}`);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Removes a player from the registry. Does nothing if the id
|
|
29
|
+
* does not exist.
|
|
30
|
+
*/
|
|
31
|
+
remove(player) {
|
|
32
|
+
if (!this.players.has(player.playerId)) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
this.emit('removed', player.playerId);
|
|
36
|
+
this.players.delete(player.playerId);
|
|
37
|
+
Logger.info(`Unregistered player: ${player.playerId}`);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Tests if a player id exists in the registry.
|
|
41
|
+
*/
|
|
42
|
+
has(playerId) {
|
|
43
|
+
return this.players.has(playerId);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Gets a player from the registry using the player id.
|
|
47
|
+
* Returns undefined if the player doesn't exist.
|
|
48
|
+
*/
|
|
49
|
+
get(playerId) {
|
|
50
|
+
return this.players.get(playerId);
|
|
51
|
+
}
|
|
52
|
+
listPlayers() {
|
|
53
|
+
return Array.from(this.players.values());
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Returns true when the registry is empty.
|
|
57
|
+
*/
|
|
58
|
+
empty() {
|
|
59
|
+
return this.players.size === 0;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Gets the total number of connected players.
|
|
63
|
+
*/
|
|
64
|
+
count() {
|
|
65
|
+
return this.players.size;
|
|
66
|
+
}
|
|
67
|
+
getUniquePlayerId() {
|
|
68
|
+
const newPlayerId = `Player${this.nextPlayerId}`;
|
|
69
|
+
this.nextPlayerId++;
|
|
70
|
+
return newPlayerId;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=PlayerRegistry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlayerRegistry.js","sourceRoot":"","sources":["../../src/PlayerRegistry.ts"],"names":[],"mappings":"AAEA,OAAO,EAAmC,YAAY,EAAE,MAAM,8CAA8C,CAAC;AAC7G,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AA+BlC;;;;;;;GAOG;AACH,MAAM,OAAO,cAAe,SAAQ,YAAY;IAI5C;QACI,KAAK,EAAE,CAAC;QAJJ,YAAO,GAAyB,IAAI,GAAG,EAAE,CAAC;QAK9C,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;QACzB,IAAI,CAAC,YAAY,GAAG,CAAC,CAAC;IAC1B,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,MAAe;QACf,MAAM,CAAC,QAAQ,GAAG,IAAI,CAAC,iBAAiB,EAAE,CAAC;QAC3C,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAA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
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { WebSocketTransportNJS, SignallingProtocol, MessageHelpers, Messages, EventEmitter } from '@epicgames-ps/lib-pixelstreamingcommon-ue5.8';
|
|
2
|
+
import { Logger } from './Logger';
|
|
3
|
+
import * as LogUtils from './LoggingUtils';
|
|
4
|
+
/**
|
|
5
|
+
* A SFU connection to the signalling server.
|
|
6
|
+
* An SFU can act as both a streamer and a player. It can subscribe to
|
|
7
|
+
* streamers like a player, and other players can subscribe to the sfu.
|
|
8
|
+
* Therefore the SFU will have a streamer id and a player id and be
|
|
9
|
+
* registered in both streamer registries and player registries.
|
|
10
|
+
*
|
|
11
|
+
* Interesting internals:
|
|
12
|
+
* playerId: The player id of this connectiom.
|
|
13
|
+
* streamerId: The streamer id of this connection.
|
|
14
|
+
* transport: The ITransport where transport events can be subscribed to
|
|
15
|
+
* protocol: The SignallingProtocol where signalling messages can be
|
|
16
|
+
* subscribed to.
|
|
17
|
+
* streaming: True when the streamer is ready to accept subscriptions.
|
|
18
|
+
*/
|
|
19
|
+
export class SFUConnection extends EventEmitter {
|
|
20
|
+
/**
|
|
21
|
+
* Construct a new SFU connection.
|
|
22
|
+
* @param server - The signalling server object that spawned this sfu.
|
|
23
|
+
* @param ws - The websocket coupled to this sfu connection.
|
|
24
|
+
* @param remoteAddress - The remote address of this connection. Only used as display.
|
|
25
|
+
* @param request - The HTTP upgrade request that opened this connection, if available.
|
|
26
|
+
*/
|
|
27
|
+
constructor(server, ws, remoteAddress, request) {
|
|
28
|
+
super();
|
|
29
|
+
this.server = server;
|
|
30
|
+
this.transport = new WebSocketTransportNJS(ws);
|
|
31
|
+
this.protocol = new SignallingProtocol(this.transport);
|
|
32
|
+
this.playerId = '';
|
|
33
|
+
this.streamerId = '';
|
|
34
|
+
this.streaming = false;
|
|
35
|
+
this.remoteAddress = remoteAddress;
|
|
36
|
+
this.request = request;
|
|
37
|
+
this.subscribedStreamer = null;
|
|
38
|
+
this.maxSubscribers = 0;
|
|
39
|
+
this.subscribers = new Set();
|
|
40
|
+
this.transport.on('error', this.onTransportError.bind(this));
|
|
41
|
+
this.transport.on('close', this.onTransportClose.bind(this));
|
|
42
|
+
this.layerPreferenceListener = this.onLayerPreference.bind(this);
|
|
43
|
+
this.streamerIdChangeListener = this.onStreamerIdChanged.bind(this);
|
|
44
|
+
this.streamerDisconnectedListener = this.onStreamerDisconnected.bind(this);
|
|
45
|
+
this.registerMessageHandlers();
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Returns an identifier that is displayed in logs.
|
|
49
|
+
* @returns A string describing this connection.
|
|
50
|
+
*/
|
|
51
|
+
getReadableIdentifier() {
|
|
52
|
+
return `(${this.streamerId}:${this.playerId})`;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Sends a signalling message to the SFU.
|
|
56
|
+
* @param message - The message to send.
|
|
57
|
+
*/
|
|
58
|
+
sendMessage(message) {
|
|
59
|
+
LogUtils.logOutgoing(this, message);
|
|
60
|
+
this.protocol.sendMessage(message);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Returns a descriptive object for the REST API inspection operations.
|
|
64
|
+
* @returns An IStreamerInfo object containing viewable information about this connection.
|
|
65
|
+
*/
|
|
66
|
+
getStreamerInfo() {
|
|
67
|
+
return {
|
|
68
|
+
streamerId: this.streamerId,
|
|
69
|
+
type: 'SFU',
|
|
70
|
+
streaming: this.streaming,
|
|
71
|
+
remoteAddress: this.remoteAddress,
|
|
72
|
+
subscribers: this.server.playerRegistry
|
|
73
|
+
.listPlayers()
|
|
74
|
+
.filter((player) => player.subscribedStreamer == this)
|
|
75
|
+
.map((player) => player.getPlayerInfo())
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Returns a descriptive object for the REST API inspection operations.
|
|
80
|
+
* @returns An IPlayerInfo object containing viewable information about this connection.
|
|
81
|
+
*/
|
|
82
|
+
getPlayerInfo() {
|
|
83
|
+
var _a;
|
|
84
|
+
return {
|
|
85
|
+
playerId: this.playerId,
|
|
86
|
+
type: 'SFU',
|
|
87
|
+
remoteAddress: this.remoteAddress,
|
|
88
|
+
subscribedTo: (_a = this.subscribedStreamer) === null || _a === void 0 ? void 0 : _a.streamerId
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
registerMessageHandlers() {
|
|
92
|
+
/* eslint-disable @typescript-eslint/unbound-method */
|
|
93
|
+
this.protocol.on(Messages.subscribe.typeName, LogUtils.createHandlerListener(this, this.onSubscribeMessage));
|
|
94
|
+
this.protocol.on(Messages.unsubscribe.typeName, LogUtils.createHandlerListener(this, this.onUnsubscribeMessage));
|
|
95
|
+
this.protocol.on(Messages.listStreamers.typeName, LogUtils.createHandlerListener(this, this.onListStreamers));
|
|
96
|
+
this.protocol.on(Messages.endpointId.typeName, LogUtils.createHandlerListener(this, this.onEndpointId));
|
|
97
|
+
this.protocol.on(Messages.streamerDataChannels.typeName, LogUtils.createHandlerListener(this, this.onStreamerDataChannels));
|
|
98
|
+
this.protocol.on(Messages.startStreaming.typeName, LogUtils.createHandlerListener(this, this.onStartStreaming));
|
|
99
|
+
this.protocol.on(Messages.stopStreaming.typeName, LogUtils.createHandlerListener(this, this.onStopStreaming));
|
|
100
|
+
this.protocol.on(Messages.ping.typeName, LogUtils.createHandlerListener(this, this.onPingMessage));
|
|
101
|
+
/* eslint-enable @typescript-eslint/unbound-method */
|
|
102
|
+
this.protocol.on(Messages.offer.typeName, this.sendToPlayer.bind(this));
|
|
103
|
+
this.protocol.on(Messages.answer.typeName, this.sendToStreamer.bind(this));
|
|
104
|
+
this.protocol.on(Messages.peerDataChannels.typeName, this.sendToPlayer.bind(this));
|
|
105
|
+
}
|
|
106
|
+
subscribe(streamerId) {
|
|
107
|
+
const streamer = this.server.streamerRegistry.find(streamerId);
|
|
108
|
+
if (!streamer) {
|
|
109
|
+
Logger.error(`subscribe: SFU ${this.playerId} tried to subscribe to a non-existent streamer ${streamerId}`);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
this.subscribedStreamer = streamer;
|
|
113
|
+
// Register as a subscriber of the upstream streamer so it will forward signalling messages
|
|
114
|
+
// back to this SFU (the streamer's forwardMessage only sends to subscribed players).
|
|
115
|
+
this.subscribedStreamer.subscribers.add(this.playerId);
|
|
116
|
+
this.subscribedStreamer.on('layer_preference', this.layerPreferenceListener);
|
|
117
|
+
this.subscribedStreamer.on('id_changed', this.streamerIdChangeListener);
|
|
118
|
+
this.subscribedStreamer.on('disconnect', this.streamerDisconnectedListener);
|
|
119
|
+
const connectedMessage = MessageHelpers.createMessage(Messages.playerConnected, {
|
|
120
|
+
playerId: this.playerId,
|
|
121
|
+
dataChannel: true,
|
|
122
|
+
sfu: true
|
|
123
|
+
});
|
|
124
|
+
this.sendToStreamer(connectedMessage);
|
|
125
|
+
}
|
|
126
|
+
unsubscribe() {
|
|
127
|
+
if (!this.subscribedStreamer) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const disconnectedMessage = MessageHelpers.createMessage(Messages.playerDisconnected, {
|
|
131
|
+
playerId: this.playerId
|
|
132
|
+
});
|
|
133
|
+
this.sendToStreamer(disconnectedMessage);
|
|
134
|
+
this.subscribedStreamer.subscribers.delete(this.playerId);
|
|
135
|
+
this.subscribedStreamer.off('layer_preference', this.layerPreferenceListener);
|
|
136
|
+
this.subscribedStreamer.off('id_changed', this.streamerIdChangeListener);
|
|
137
|
+
this.subscribedStreamer.off('disconnect', this.streamerDisconnectedListener);
|
|
138
|
+
this.subscribedStreamer = null;
|
|
139
|
+
}
|
|
140
|
+
sendToStreamer(message) {
|
|
141
|
+
if (!this.subscribedStreamer) {
|
|
142
|
+
Logger.error(`SFU ${this.playerId} tried to send to a streamer but they're not subscribed to any.`);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
LogUtils.logForward(this, this.subscribedStreamer, message);
|
|
146
|
+
// normally we want to indicate what player this message came from
|
|
147
|
+
// but in some instances we might already have set this (streamerDataChannels) due to poor choices
|
|
148
|
+
message.playerId = message.playerId || this.playerId;
|
|
149
|
+
this.subscribedStreamer.protocol.sendMessage(message);
|
|
150
|
+
}
|
|
151
|
+
sendToPlayer(message) {
|
|
152
|
+
if (!message.playerId) {
|
|
153
|
+
Logger.error(`SFU ${this.streamerId} trying to send a message to a player with no playerId. Ignored.`);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (!this.subscribers.has(message.playerId)) {
|
|
157
|
+
// Only send to players subscribed to this SFU, otherwise the SFU could target any player
|
|
158
|
+
// on the server via the global player registry (cross-tenant signalling).
|
|
159
|
+
Logger.error(`SFU ${this.streamerId} tried to send a message to player ${message.playerId} which is not subscribed to it. Ignored.`);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const player = this.server.playerRegistry.get(message.playerId);
|
|
163
|
+
if (player) {
|
|
164
|
+
delete message.playerId;
|
|
165
|
+
LogUtils.logForward(this, player, message);
|
|
166
|
+
player.protocol.sendMessage(message);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
Logger.error(`SFU attempted to forward to player ${message.playerId} which does not exist.`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
disconnect() {
|
|
173
|
+
this.unsubscribe();
|
|
174
|
+
this.protocol.disconnect();
|
|
175
|
+
this.emit('disconnect');
|
|
176
|
+
}
|
|
177
|
+
onLayerPreference(message) {
|
|
178
|
+
this.sendMessage(message);
|
|
179
|
+
}
|
|
180
|
+
onStreamerIdChanged(newId) {
|
|
181
|
+
const renameMessage = MessageHelpers.createMessage(Messages.streamerIdChanged, { newID: newId });
|
|
182
|
+
this.sendMessage(renameMessage);
|
|
183
|
+
}
|
|
184
|
+
onStreamerDisconnected() {
|
|
185
|
+
this.unsubscribe();
|
|
186
|
+
this.sendMessage(MessageHelpers.createMessage(Messages.streamerDisconnected));
|
|
187
|
+
this.streaming = false;
|
|
188
|
+
this.emit('disconnect');
|
|
189
|
+
}
|
|
190
|
+
onTransportError(error) {
|
|
191
|
+
Logger.error(`SFU (${this.playerId}) transport error ${error.message}`);
|
|
192
|
+
}
|
|
193
|
+
onTransportClose(_event) {
|
|
194
|
+
Logger.debug('SFUConnection transport close.');
|
|
195
|
+
this.disconnect();
|
|
196
|
+
}
|
|
197
|
+
onSubscribeMessage(message) {
|
|
198
|
+
this.subscribe(message.streamerId);
|
|
199
|
+
}
|
|
200
|
+
onUnsubscribeMessage(_message) {
|
|
201
|
+
this.unsubscribe();
|
|
202
|
+
}
|
|
203
|
+
onListStreamers(_message) {
|
|
204
|
+
const listMessage = MessageHelpers.createMessage(Messages.streamerList, {
|
|
205
|
+
ids: this.server.streamerRegistry.streamers.map((streamer) => streamer.streamerId)
|
|
206
|
+
});
|
|
207
|
+
this.sendMessage(listMessage);
|
|
208
|
+
}
|
|
209
|
+
onStreamerDataChannels(message) {
|
|
210
|
+
message.sfuId = this.playerId;
|
|
211
|
+
this.sendToStreamer(message);
|
|
212
|
+
}
|
|
213
|
+
onEndpointId(_message) {
|
|
214
|
+
this.streaming = true;
|
|
215
|
+
}
|
|
216
|
+
onStartStreaming(_message) {
|
|
217
|
+
this.streaming = true;
|
|
218
|
+
}
|
|
219
|
+
onStopStreaming(_message) {
|
|
220
|
+
this.streaming = false;
|
|
221
|
+
this.emit('disconnect');
|
|
222
|
+
}
|
|
223
|
+
onPingMessage(message) {
|
|
224
|
+
this.sendMessage(MessageHelpers.createMessage(Messages.pong, { time: message.time }));
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
//# sourceMappingURL=SFUConnection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SFUConnection.js","sourceRoot":"","sources":["../../src/SFUConnection.ts"],"names":[],"mappings":"AAGA,OAAO,EAEH,qBAAqB,EACrB,kBAAkB,EAClB,cAAc,EACd,QAAQ,EAER,YAAY,EACf,MAAM,8CAA8C,CAAC;AAGtD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAA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gB,CAAC,QAAiC;QACtD,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;IAC1B,CAAC;IAEO,eAAe,CAAC,QAAgC;QACpD,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;QACvB,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IAC5B,CAAC;IAEO,aAAa,CAAC,OAAsB;QACxC,IAAI,CAAC,WAAW,CAAC,cAAc,CAAC,aAAa,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;IAC1F,CAAC;CACJ"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import * as wslib from 'ws';
|
|
2
|
+
import { StreamerConnection } from './StreamerConnection';
|
|
3
|
+
import { PlayerConnection } from './PlayerConnection';
|
|
4
|
+
import { SFUConnection } from './SFUConnection';
|
|
5
|
+
import { Logger } from './Logger';
|
|
6
|
+
import { StreamerRegistry } from './StreamerRegistry';
|
|
7
|
+
import { PlayerRegistry } from './PlayerRegistry';
|
|
8
|
+
import { Messages, MessageHelpers, SignallingProtocol, KeepaliveMonitor } from '@epicgames-ps/lib-pixelstreamingcommon-ue5.8';
|
|
9
|
+
import { stringify } from './Utils';
|
|
10
|
+
/**
|
|
11
|
+
* The main signalling server object.
|
|
12
|
+
* Contains a streamer and player registry and handles setting up of websockets
|
|
13
|
+
* to listen for incoming connections.
|
|
14
|
+
*/
|
|
15
|
+
export class SignallingServer {
|
|
16
|
+
/**
|
|
17
|
+
* Initializes the server object and sets up listening sockets for streamers
|
|
18
|
+
* players and optionally SFU connections.
|
|
19
|
+
* @param config - A collection of options for this server.
|
|
20
|
+
*/
|
|
21
|
+
constructor(config) {
|
|
22
|
+
Logger.debug('Started SignallingServer with config: %s', stringify(config));
|
|
23
|
+
this.config = config;
|
|
24
|
+
this.streamerRegistry = new StreamerRegistry(config.authorizeStreamerId);
|
|
25
|
+
this.playerRegistry = new PlayerRegistry();
|
|
26
|
+
this.protocolConfig = {
|
|
27
|
+
protocolVersion: SignallingProtocol.SIGNALLING_VERSION,
|
|
28
|
+
peerConnectionOptions: this.config.peerOptions || {}
|
|
29
|
+
};
|
|
30
|
+
this.startTime = new Date();
|
|
31
|
+
if (!config.playerPort && !config.httpServer && !config.httpsServer) {
|
|
32
|
+
Logger.error('No player port, http server or https server supplied to SignallingServer.');
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
// Streamer connections
|
|
36
|
+
const streamerServer = new wslib.WebSocketServer(Object.assign({ port: config.streamerPort, backlog: 1 }, config.streamerWsOptions));
|
|
37
|
+
streamerServer.on('connection', this.onStreamerConnected.bind(this));
|
|
38
|
+
Logger.info(`Listening for streamer connections on port ${config.streamerPort}`);
|
|
39
|
+
// Player connections
|
|
40
|
+
const server = config.httpsServer || config.httpServer;
|
|
41
|
+
const playerServer = new wslib.WebSocketServer(Object.assign({ server: server, port: server ? undefined : config.playerPort }, config.playerWsOptions));
|
|
42
|
+
playerServer.on('connection', this.onPlayerConnected.bind(this));
|
|
43
|
+
if (!config.httpServer && !config.httpsServer) {
|
|
44
|
+
Logger.info(`Listening for player connections on port ${config.playerPort}`);
|
|
45
|
+
}
|
|
46
|
+
// Optional SFU connections
|
|
47
|
+
if (config.sfuPort) {
|
|
48
|
+
const sfuServer = new wslib.WebSocketServer(Object.assign({ port: config.sfuPort, backlog: 1 }, config.sfuWsOptions));
|
|
49
|
+
sfuServer.on('connection', this.onSFUConnected.bind(this));
|
|
50
|
+
Logger.info(`Listening for SFU connections on port ${config.sfuPort}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
sendConfigMessage(connection) {
|
|
54
|
+
// peer connection options is a general field with all optional fields;
|
|
55
|
+
// it doesnt play nice with mergePartial so we just add it verbatim
|
|
56
|
+
const message = MessageHelpers.createMessage(Messages.config, this.protocolConfig);
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
58
|
+
message.peerConnectionOptions = this.protocolConfig['peerConnectionOptions'];
|
|
59
|
+
connection.sendMessage(message);
|
|
60
|
+
}
|
|
61
|
+
onStreamerConnected(ws, request) {
|
|
62
|
+
Logger.info(`New streamer connection: %s`, request.socket.remoteAddress);
|
|
63
|
+
const newStreamer = new StreamerConnection(this, ws, request.socket.remoteAddress, request);
|
|
64
|
+
newStreamer.maxSubscribers = this.config.maxSubscribers || 0;
|
|
65
|
+
// add it to the registry and when the transport closes, remove it.
|
|
66
|
+
this.streamerRegistry.add(newStreamer);
|
|
67
|
+
newStreamer.transport.on('close', () => {
|
|
68
|
+
this.streamerRegistry.remove(newStreamer);
|
|
69
|
+
Logger.info(`Streamer %s (%s) disconnected.`, newStreamer.streamerId, request.socket.remoteAddress);
|
|
70
|
+
});
|
|
71
|
+
this.sendConfigMessage(newStreamer);
|
|
72
|
+
}
|
|
73
|
+
onPlayerConnected(ws, request) {
|
|
74
|
+
Logger.info(`New player connection: %s (%s)`, request.socket.remoteAddress, request.url);
|
|
75
|
+
const newPlayer = new PlayerConnection(this, ws, request.socket.remoteAddress, request);
|
|
76
|
+
// add it to the registry and when the transport closes, remove it
|
|
77
|
+
this.playerRegistry.add(newPlayer);
|
|
78
|
+
newPlayer.transport.on('close', () => {
|
|
79
|
+
this.playerRegistry.remove(newPlayer);
|
|
80
|
+
Logger.info(`Player %s (%s) disconnected.`, newPlayer.playerId, request.socket.remoteAddress);
|
|
81
|
+
});
|
|
82
|
+
// Optionally monitor the player connection for liveness. A player whose socket dies without
|
|
83
|
+
// a clean close frame (sleeping laptop, dropped Wi-Fi, killed tab) is otherwise only removed
|
|
84
|
+
// once the OS TCP keepalive eventually reaps it, leaving it subscribed in the meantime. When
|
|
85
|
+
// maxSubscribers is set this can hold a slot that no live player is using. We use
|
|
86
|
+
// ws.terminate() rather than a graceful close because a dead peer never completes the close
|
|
87
|
+
// handshake. The monitor stops itself on transport 'close', so no manual teardown is needed.
|
|
88
|
+
const keepaliveTimeout = this.config.playerKeepaliveTimeout || 0;
|
|
89
|
+
if (keepaliveTimeout > 0) {
|
|
90
|
+
const keepalive = new KeepaliveMonitor(newPlayer.protocol, keepaliveTimeout);
|
|
91
|
+
keepalive.onTimeout = () => {
|
|
92
|
+
Logger.info(`Player %s (%s) failed keepalive - terminating dead connection.`, newPlayer.playerId, request.socket.remoteAddress);
|
|
93
|
+
ws.terminate();
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
this.sendConfigMessage(newPlayer);
|
|
97
|
+
}
|
|
98
|
+
onSFUConnected(ws, request) {
|
|
99
|
+
Logger.info(`New SFU connection: %s`, request.socket.remoteAddress);
|
|
100
|
+
const newSFU = new SFUConnection(this, ws, request.socket.remoteAddress, request);
|
|
101
|
+
// SFU acts as both a streamer and player
|
|
102
|
+
this.streamerRegistry.add(newSFU);
|
|
103
|
+
this.playerRegistry.add(newSFU);
|
|
104
|
+
newSFU.transport.on('close', () => {
|
|
105
|
+
this.streamerRegistry.remove(newSFU);
|
|
106
|
+
this.playerRegistry.remove(newSFU);
|
|
107
|
+
Logger.info(`SFU %s (%s) disconnected.`, newSFU.streamerId, request.socket.remoteAddress);
|
|
108
|
+
});
|
|
109
|
+
this.sendConfigMessage(newSFU);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=SignallingServer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SignallingServer.js","sourceRoot":"","sources":["../../src/SignallingServer.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,KAAK,MAAM,IAAI,CAAC;AAC5B,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,gBAAgB,EAAwB,MAAM,oBAAoB,CAAC;AAC5E,OAAO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