@epicgames-ps/lib-pixelstreamingfrontend-ue5.5 0.0.5

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.
Files changed (138) hide show
  1. package/.cspell.json +48 -0
  2. package/.eslintignore +8 -0
  3. package/.eslintrc.js +8 -0
  4. package/.prettierignore +0 -0
  5. package/.prettierrc.json +6 -0
  6. package/dist/lib-pixelstreamingfrontend.esm.js +1 -0
  7. package/dist/lib-pixelstreamingfrontend.js +1 -0
  8. package/jest.config.js +18 -0
  9. package/package.json +48 -0
  10. package/readme.md +15 -0
  11. package/src/AFK/AFKController.test.ts +162 -0
  12. package/src/AFK/AFKController.ts +158 -0
  13. package/src/Config/Config.test.ts +222 -0
  14. package/src/Config/Config.ts +970 -0
  15. package/src/Config/SettingBase.ts +65 -0
  16. package/src/Config/SettingFlag.ts +99 -0
  17. package/src/Config/SettingNumber.ts +111 -0
  18. package/src/Config/SettingOption.ts +124 -0
  19. package/src/Config/SettingText.ts +82 -0
  20. package/src/DataChannel/DataChannelController.ts +138 -0
  21. package/src/DataChannel/DataChannelLatencyTestController.ts +129 -0
  22. package/src/DataChannel/DataChannelLatencyTestResults.ts +67 -0
  23. package/src/DataChannel/DataChannelSender.ts +59 -0
  24. package/src/DataChannel/InitialSettings.ts +61 -0
  25. package/src/DataChannel/LatencyTestResults.ts +76 -0
  26. package/src/FreezeFrame/FreezeFrame.ts +114 -0
  27. package/src/FreezeFrame/FreezeFrameController.ts +114 -0
  28. package/src/Inputs/FakeTouchController.ts +199 -0
  29. package/src/Inputs/GamepadController.ts +314 -0
  30. package/src/Inputs/GamepadTypes.ts +10 -0
  31. package/src/Inputs/HoveringMouseEvents.ts +192 -0
  32. package/src/Inputs/IMouseEvents.ts +64 -0
  33. package/src/Inputs/ITouchController.ts +29 -0
  34. package/src/Inputs/InputClassesFactory.ts +140 -0
  35. package/src/Inputs/KeyboardController.ts +354 -0
  36. package/src/Inputs/LockedMouseEvents.ts +287 -0
  37. package/src/Inputs/MouseButtons.ts +25 -0
  38. package/src/Inputs/MouseController.ts +362 -0
  39. package/src/Inputs/SpecialKeyCodes.ts +16 -0
  40. package/src/Inputs/TouchController.ts +208 -0
  41. package/src/Inputs/XRGamepadController.ts +126 -0
  42. package/src/PeerConnectionController/AggregatedStats.ts +311 -0
  43. package/src/PeerConnectionController/CandidatePairStats.ts +17 -0
  44. package/src/PeerConnectionController/CandidateStat.ts +13 -0
  45. package/src/PeerConnectionController/CodecStats.ts +19 -0
  46. package/src/PeerConnectionController/DataChannelStats.ts +17 -0
  47. package/src/PeerConnectionController/InboundRTPStats.ts +154 -0
  48. package/src/PeerConnectionController/InboundTrackStats.ts +34 -0
  49. package/src/PeerConnectionController/OutBoundRTPStats.ts +26 -0
  50. package/src/PeerConnectionController/PeerConnectionController.ts +563 -0
  51. package/src/PeerConnectionController/SessionStats.ts +10 -0
  52. package/src/PeerConnectionController/StreamStats.ts +11 -0
  53. package/src/PixelStreaming/PixelStreaming.test.ts +626 -0
  54. package/src/PixelStreaming/PixelStreaming.ts +851 -0
  55. package/src/UI/OnScreenKeyboard.ts +97 -0
  56. package/src/UeInstanceMessage/ResponseController.ts +47 -0
  57. package/src/UeInstanceMessage/SendMessageController.ts +154 -0
  58. package/src/UeInstanceMessage/StreamMessageController.ts +233 -0
  59. package/src/UeInstanceMessage/ToStreamerMessagesController.ts +62 -0
  60. package/src/Util/CoordinateConverter.ts +289 -0
  61. package/src/Util/EventEmitter.ts +611 -0
  62. package/src/Util/EventListenerTracker.ts +29 -0
  63. package/src/Util/FileUtil.ts +140 -0
  64. package/src/Util/RTCUtils.ts +41 -0
  65. package/src/Util/WebGLUtils.ts +49 -0
  66. package/src/Util/WebXRUtils.ts +25 -0
  67. package/src/VideoPlayer/StreamController.ts +89 -0
  68. package/src/VideoPlayer/VideoPlayer.ts +246 -0
  69. package/src/WebRtcPlayer/WebRtcPlayerController.ts +2158 -0
  70. package/src/WebXR/WebXRController.ts +319 -0
  71. package/src/__test__/mockMediaStream.ts +124 -0
  72. package/src/__test__/mockRTCPeerConnection.ts +347 -0
  73. package/src/__test__/mockRTCRtpReceiver.ts +22 -0
  74. package/src/__test__/mockWebSocket.ts +136 -0
  75. package/src/pixelstreamingfrontend.ts +46 -0
  76. package/tsconfig.jest.json +8 -0
  77. package/tsconfig.json +24 -0
  78. package/types/AFK/AFKController.d.ts +39 -0
  79. package/types/Config/Config.d.ts +218 -0
  80. package/types/Config/SettingBase.d.ts +30 -0
  81. package/types/Config/SettingFlag.d.ts +33 -0
  82. package/types/Config/SettingNumber.d.ts +45 -0
  83. package/types/Config/SettingOption.d.ts +43 -0
  84. package/types/Config/SettingText.d.ts +29 -0
  85. package/types/DataChannel/DataChannelController.d.ts +59 -0
  86. package/types/DataChannel/DataChannelLatencyTestController.d.ts +26 -0
  87. package/types/DataChannel/DataChannelLatencyTestResults.d.ts +46 -0
  88. package/types/DataChannel/DataChannelSender.d.ts +21 -0
  89. package/types/DataChannel/InitialSettings.d.ts +44 -0
  90. package/types/DataChannel/LatencyTestResults.d.ts +31 -0
  91. package/types/FreezeFrame/FreezeFrame.d.ts +36 -0
  92. package/types/FreezeFrame/FreezeFrameController.d.ts +37 -0
  93. package/types/Inputs/FakeTouchController.d.ts +61 -0
  94. package/types/Inputs/GamepadController.d.ts +85 -0
  95. package/types/Inputs/GamepadTypes.d.ts +8 -0
  96. package/types/Inputs/HoveringMouseEvents.d.ts +56 -0
  97. package/types/Inputs/IMouseEvents.d.ts +53 -0
  98. package/types/Inputs/ITouchController.d.ts +24 -0
  99. package/types/Inputs/InputClassesFactory.d.ts +54 -0
  100. package/types/Inputs/KeyboardController.d.ts +62 -0
  101. package/types/Inputs/LockedMouseEvents.d.ts +80 -0
  102. package/types/Inputs/MouseButtons.d.ts +22 -0
  103. package/types/Inputs/MouseController.d.ts +75 -0
  104. package/types/Inputs/SpecialKeyCodes.d.ts +14 -0
  105. package/types/Inputs/TouchController.d.ts +53 -0
  106. package/types/Inputs/XRGamepadController.d.ts +15 -0
  107. package/types/PeerConnectionController/AggregatedStats.d.ts +77 -0
  108. package/types/PeerConnectionController/CandidatePairStats.d.ts +15 -0
  109. package/types/PeerConnectionController/CandidateStat.d.ts +11 -0
  110. package/types/PeerConnectionController/CodecStats.d.ts +14 -0
  111. package/types/PeerConnectionController/DataChannelStats.d.ts +15 -0
  112. package/types/PeerConnectionController/InboundRTPStats.d.ts +141 -0
  113. package/types/PeerConnectionController/InboundTrackStats.d.ts +32 -0
  114. package/types/PeerConnectionController/OutBoundRTPStats.d.ts +23 -0
  115. package/types/PeerConnectionController/PeerConnectionController.d.ts +132 -0
  116. package/types/PeerConnectionController/SessionStats.d.ts +8 -0
  117. package/types/PeerConnectionController/StreamStats.d.ts +9 -0
  118. package/types/PixelStreaming/PixelStreaming.d.ts +259 -0
  119. package/types/UI/OnScreenKeyboard.d.ts +31 -0
  120. package/types/UeInstanceMessage/ResponseController.d.ts +19 -0
  121. package/types/UeInstanceMessage/SendMessageController.d.ts +18 -0
  122. package/types/UeInstanceMessage/StreamMessageController.d.ts +29 -0
  123. package/types/UeInstanceMessage/ToStreamerMessagesController.d.ts +32 -0
  124. package/types/Util/CoordinateConverter.d.ts +100 -0
  125. package/types/Util/EventEmitter.d.ts +422 -0
  126. package/types/Util/EventListenerTracker.d.ts +14 -0
  127. package/types/Util/FileUtil.d.ts +32 -0
  128. package/types/Util/RTCUtils.d.ts +8 -0
  129. package/types/Util/WebGLUtils.d.ts +4 -0
  130. package/types/Util/WebXRUtils.d.ts +9 -0
  131. package/types/VideoPlayer/StreamController.d.ts +24 -0
  132. package/types/VideoPlayer/VideoPlayer.d.ts +78 -0
  133. package/types/WebRtcPlayer/WebRtcPlayerController.d.ts +377 -0
  134. package/types/WebXR/WebXRController.d.ts +26 -0
  135. package/types/pixelstreamingfrontend.d.ts +22 -0
  136. package/webpack.common.js +35 -0
  137. package/webpack.dev.js +35 -0
  138. package/webpack.prod.js +36 -0
@@ -0,0 +1,851 @@
1
+ // Copyright Epic Games, Inc. All Rights Reserved.
2
+
3
+ import { Config, OptionParameters } from '../Config/Config';
4
+ import { LatencyTestResults } from '../DataChannel/LatencyTestResults';
5
+ import { AggregatedStats } from '../PeerConnectionController/AggregatedStats';
6
+ import { WebRtcPlayerController } from '../WebRtcPlayer/WebRtcPlayerController';
7
+ import { Flags, NumericParameters } from '../Config/Config';
8
+ import { Logger } from '@epicgames-ps/lib-pixelstreamingcommon-ue5.5';
9
+ import { InitialSettings } from '../DataChannel/InitialSettings';
10
+ import { OnScreenKeyboard } from '../UI/OnScreenKeyboard';
11
+ import {
12
+ EventEmitter,
13
+ InitialSettingsEvent,
14
+ LatencyTestResultEvent,
15
+ PixelStreamingEvent,
16
+ StatsReceivedEvent,
17
+ StreamLoadingEvent,
18
+ StreamPreConnectEvent,
19
+ StreamReconnectEvent,
20
+ StreamPreDisconnectEvent,
21
+ VideoEncoderAvgQPEvent,
22
+ VideoInitializedEvent,
23
+ WebRtcAutoConnectEvent,
24
+ WebRtcConnectedEvent,
25
+ WebRtcConnectingEvent,
26
+ WebRtcDisconnectedEvent,
27
+ WebRtcFailedEvent,
28
+ WebRtcSdpEvent,
29
+ DataChannelLatencyTestResponseEvent,
30
+ DataChannelLatencyTestResultEvent,
31
+ PlayerCountEvent
32
+ } from '../Util/EventEmitter';
33
+ import { MessageReceive } from '@epicgames-ps/lib-pixelstreamingcommon-ue5.5';
34
+ import { WebXRController } from '../WebXR/WebXRController';
35
+ import { MessageDirection } from '../UeInstanceMessage/StreamMessageController';
36
+ import {
37
+ DataChannelLatencyTestConfig,
38
+ DataChannelLatencyTestController
39
+ } from "../DataChannel/DataChannelLatencyTestController";
40
+ import {
41
+ DataChannelLatencyTestResponse,
42
+ DataChannelLatencyTestResult
43
+ } from "../DataChannel/DataChannelLatencyTestResults";
44
+ import { RTCUtils } from '../Util/RTCUtils';
45
+
46
+
47
+ export interface PixelStreamingOverrides {
48
+ /** The DOM elment where Pixel Streaming video and user input event handlers are attached to.
49
+ * You can give an existing DOM element here. If not given, the library will create a new div element
50
+ * that is not attached anywhere. In this case you can later get access to this new element and
51
+ * attach it to your web page. */
52
+ videoElementParent?: HTMLElement;
53
+ }
54
+
55
+ /**
56
+ * The key class for the browser side of a Pixel Streaming application, it includes:
57
+ * WebRTC handling, XR support, input handling, and emitters for lifetime and state change events.
58
+ * Users are encouraged to use this class as is, through composition, or extend it. In any case,
59
+ * this will likely be the core of your Pixel Streaming experience in terms of functionality.
60
+ */
61
+ export class PixelStreaming {
62
+ protected _webRtcController: WebRtcPlayerController;
63
+ protected _webXrController: WebXRController;
64
+ protected _dataChannelLatencyTestController: DataChannelLatencyTestController;
65
+ /**
66
+ * Configuration object. You can read or modify config through this object. Whenever
67
+ * the configuration is changed, the library will emit a `settingsChanged` event.
68
+ */
69
+ public config: Config;
70
+
71
+ private _videoElementParent: HTMLElement;
72
+
73
+ private allowConsoleCommands = false;
74
+
75
+ private onScreenKeyboardHelper: OnScreenKeyboard;
76
+
77
+ private _videoStartTime: number;
78
+ private _inputController: boolean;
79
+
80
+ private _eventEmitter: EventEmitter;
81
+
82
+ /**
83
+ * @param config - A newly instantiated config object
84
+ * @param overrides - Parameters to override default behaviour
85
+ * returns the base Pixel streaming object
86
+ */
87
+ constructor(config: Config, overrides?: PixelStreamingOverrides) {
88
+ this.config = config;
89
+
90
+ if (overrides?.videoElementParent) {
91
+ this._videoElementParent = overrides.videoElementParent;
92
+ }
93
+
94
+ this._eventEmitter = new EventEmitter();
95
+
96
+ this.configureSettings();
97
+
98
+ // setup WebRTC
99
+ this.setWebRtcPlayerController(
100
+ new WebRtcPlayerController(this.config, this)
101
+ );
102
+
103
+ // Onscreen keyboard
104
+ this.onScreenKeyboardHelper = new OnScreenKeyboard(
105
+ this.videoElementParent
106
+ );
107
+ this.onScreenKeyboardHelper.unquantizeAndDenormalizeUnsigned = (
108
+ x: number,
109
+ y: number
110
+ ) =>
111
+ this._webRtcController.requestUnquantizedAndDenormalizeUnsigned(
112
+ x,
113
+ y
114
+ );
115
+ this._activateOnScreenKeyboard = (command: MessageReceive.MessageOnScreenKeyboard) =>
116
+ this.onScreenKeyboardHelper.showOnScreenKeyboard(command);
117
+
118
+ this._webXrController = new WebXRController(this._webRtcController);
119
+ }
120
+
121
+ /**
122
+ * Gets the element that contains the video stream element.
123
+ */
124
+ public get videoElementParent(): HTMLElement {
125
+ if (!this._videoElementParent) {
126
+ this._videoElementParent = document.createElement('div');
127
+ this._videoElementParent.id = 'videoElementParent';
128
+ }
129
+ return this._videoElementParent;
130
+ }
131
+
132
+ /**
133
+ * Configure the settings with on change listeners and any additional per experience settings.
134
+ */
135
+ private configureSettings(): void {
136
+ this.config._addOnSettingChangedListener(
137
+ Flags.IsQualityController,
138
+ (wantsQualityController: boolean) => {
139
+ // If the setting has been set to true (either programatically or the user has flicked the toggle)
140
+ // and we aren't currently quality controller, send the request
141
+ if (
142
+ wantsQualityController === true &&
143
+ !this._webRtcController.isQualityController
144
+ ) {
145
+ this._webRtcController.sendRequestQualityControlOwnership();
146
+ }
147
+ }
148
+ );
149
+
150
+ this.config._addOnSettingChangedListener(
151
+ Flags.AFKDetection,
152
+ (isAFKEnabled: boolean) => {
153
+ this._webRtcController.setAfkEnabled(isAFKEnabled);
154
+ }
155
+ );
156
+
157
+ this.config._addOnSettingChangedListener(
158
+ Flags.MatchViewportResolution,
159
+ () => {
160
+ this._webRtcController.videoPlayer.updateVideoStreamSize();
161
+ }
162
+ );
163
+
164
+ this.config._addOnSettingChangedListener(
165
+ Flags.HoveringMouseMode,
166
+ (isHoveringMouse: boolean) => {
167
+ this.config.setFlagLabel(
168
+ Flags.HoveringMouseMode,
169
+ `Control Scheme: ${
170
+ isHoveringMouse ? 'Hovering' : 'Locked'
171
+ } Mouse`
172
+ );
173
+ this._webRtcController.setMouseInputEnabled(this.config.isFlagEnabled(Flags.MouseInput));
174
+ }
175
+ );
176
+
177
+ // user input
178
+ this.config._addOnSettingChangedListener(
179
+ Flags.KeyboardInput,
180
+ (isEnabled: boolean) => {
181
+ this._webRtcController.setKeyboardInputEnabled(isEnabled);
182
+ }
183
+ );
184
+
185
+ this.config._addOnSettingChangedListener(
186
+ Flags.MouseInput,
187
+ (isEnabled: boolean) => {
188
+ this._webRtcController.setMouseInputEnabled(isEnabled);
189
+ }
190
+ );
191
+
192
+ this.config._addOnSettingChangedListener(
193
+ Flags.TouchInput,
194
+ (isEnabled: boolean) => {
195
+ this._webRtcController.setTouchInputEnabled(isEnabled);
196
+ }
197
+ );
198
+
199
+ this.config._addOnSettingChangedListener(
200
+ Flags.GamepadInput,
201
+ (isEnabled: boolean) => {
202
+ this._webRtcController.setGamePadInputEnabled(isEnabled);
203
+ }
204
+ );
205
+
206
+ // encoder settings
207
+ this.config._addOnNumericSettingChangedListener(
208
+ NumericParameters.MinQP,
209
+ (newValue: number) => {
210
+ Logger.Log(
211
+ Logger.GetStackTrace(),
212
+ '-------- Sending MinQP --------',
213
+ 7
214
+ );
215
+ this._webRtcController.sendEncoderMinQP(newValue);
216
+ Logger.Log(
217
+ Logger.GetStackTrace(),
218
+ '-------------------------------------------',
219
+ 7
220
+ );
221
+ }
222
+ );
223
+
224
+ this.config._addOnNumericSettingChangedListener(
225
+ NumericParameters.MaxQP,
226
+ (newValue: number) => {
227
+ Logger.Log(
228
+ Logger.GetStackTrace(),
229
+ '-------- Sending encoder settings --------',
230
+ 7
231
+ );
232
+ this._webRtcController.sendEncoderMaxQP(newValue);
233
+ Logger.Log(
234
+ Logger.GetStackTrace(),
235
+ '-------------------------------------------',
236
+ 7
237
+ );
238
+ }
239
+ );
240
+
241
+ // WebRTC settings
242
+ this.config._addOnNumericSettingChangedListener(
243
+ NumericParameters.WebRTCMinBitrate,
244
+ (newValue: number) => {
245
+ Logger.Log(
246
+ Logger.GetStackTrace(),
247
+ '-------- Sending web rtc settings --------',
248
+ 7
249
+ );
250
+ this._webRtcController.sendWebRTCMinBitrate(newValue * 1000 /* kbps to bps */);
251
+ Logger.Log(
252
+ Logger.GetStackTrace(),
253
+ '-------------------------------------------',
254
+ 7
255
+ );
256
+ }
257
+ );
258
+
259
+ this.config._addOnNumericSettingChangedListener(
260
+ NumericParameters.WebRTCMaxBitrate,
261
+ (newValue: number) => {
262
+ Logger.Log(
263
+ Logger.GetStackTrace(),
264
+ '-------- Sending web rtc settings --------',
265
+ 7
266
+ );
267
+ this._webRtcController.sendWebRTCMaxBitrate(newValue * 1000 /* kbps to bps */);
268
+ Logger.Log(
269
+ Logger.GetStackTrace(),
270
+ '-------------------------------------------',
271
+ 7
272
+ );
273
+ }
274
+ );
275
+
276
+ this.config._addOnNumericSettingChangedListener(
277
+ NumericParameters.WebRTCFPS,
278
+ (newValue: number) => {
279
+ Logger.Log(
280
+ Logger.GetStackTrace(),
281
+ '-------- Sending web rtc settings --------',
282
+ 7
283
+ );
284
+ this._webRtcController.sendWebRTCFps(newValue);
285
+ Logger.Log(
286
+ Logger.GetStackTrace(),
287
+ '-------------------------------------------',
288
+ 7
289
+ );
290
+ }
291
+ );
292
+
293
+ this.config._addOnOptionSettingChangedListener(
294
+ OptionParameters.PreferredCodec,
295
+ (newValue: string) => {
296
+ if (this._webRtcController) {
297
+ this._webRtcController.setPreferredCodec(newValue);
298
+ }
299
+ }
300
+ );
301
+
302
+ this.config._registerOnChangeEvents(this._eventEmitter);
303
+ }
304
+
305
+ /**
306
+ * Activate the on screen keyboard when receiving the command from the streamer
307
+ * @param command - the keyboard command
308
+ */
309
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
310
+ _activateOnScreenKeyboard(command: MessageReceive.MessageOnScreenKeyboard): void {
311
+ throw new Error('Method not implemented.');
312
+ }
313
+
314
+ /**
315
+ * Set the input control ownership
316
+ * @param inputControlOwnership - does the user have input control ownership
317
+ */
318
+ _onInputControlOwnership(inputControlOwnership: boolean): void {
319
+ this._inputController = inputControlOwnership;
320
+ }
321
+
322
+ /**
323
+ * Instantiate the WebRTCPlayerController interface to provide WebRTCPlayerController functionality within this class and set up anything that requires it
324
+ * @param webRtcPlayerController - a WebRtcPlayerController controller instance
325
+ */
326
+ private setWebRtcPlayerController(
327
+ webRtcPlayerController: WebRtcPlayerController
328
+ ) {
329
+ this._webRtcController = webRtcPlayerController;
330
+
331
+ this._webRtcController.setPreferredCodec(
332
+ this.config.getSettingOption(OptionParameters.PreferredCodec)
333
+ .selected
334
+ );
335
+ this._webRtcController.resizePlayerStyle();
336
+
337
+ // connect if auto connect flag is enabled
338
+ this.checkForAutoConnect();
339
+ }
340
+
341
+ /**
342
+ * Connect to signaling server.
343
+ */
344
+ public connect() {
345
+ this._eventEmitter.dispatchEvent(new StreamPreConnectEvent());
346
+ this._webRtcController.connectToSignallingServer();
347
+ }
348
+
349
+ /**
350
+ * Reconnects to the signaling server. If connection is up, disconnects first
351
+ * before establishing a new connection
352
+ */
353
+ public reconnect() {
354
+ this._eventEmitter.dispatchEvent(new StreamReconnectEvent());
355
+ this._webRtcController.tryReconnect("Reconnecting...");
356
+ }
357
+
358
+ /**
359
+ * Disconnect from the signaling server and close open peer connections.
360
+ */
361
+ public disconnect() {
362
+ this._eventEmitter.dispatchEvent(new StreamPreDisconnectEvent());
363
+ this._webRtcController.close();
364
+ }
365
+
366
+ /**
367
+ * Play the stream. Can be called only after a peer connection has been established.
368
+ */
369
+ public play() {
370
+ this._onStreamLoading();
371
+ this._webRtcController.playStream();
372
+ }
373
+
374
+ /**
375
+ * Auto connect if AutoConnect flag is enabled
376
+ */
377
+ private checkForAutoConnect() {
378
+ // set up if the auto play will be used or regular click to start
379
+ if (this.config.isFlagEnabled(Flags.AutoConnect)) {
380
+ // if autoplaying show an info overlay while while waiting for the connection to begin
381
+ this._onWebRtcAutoConnect();
382
+ this._webRtcController.connectToSignallingServer();
383
+ }
384
+ }
385
+
386
+ /**
387
+ * Will unmute the microphone track which is sent to Unreal Engine.
388
+ * By default, will only unmute an existing mic track.
389
+ *
390
+ * @param forceEnable Can be used for cases when this object wasn't initialized with a mic track.
391
+ * If this parameter is true, the connection will be restarted with a microphone.
392
+ * Warning: this takes some time, as a full renegotiation and reconnection will happen.
393
+ */
394
+ public unmuteMicrophone(forceEnable = false) : void {
395
+ // If there's an existing mic track, we just set muted state
396
+ if (this.config.isFlagEnabled('UseMic')) {
397
+ this.setMicrophoneMuted(false);
398
+ return;
399
+ }
400
+
401
+ // If there's no pre-existing mic track, and caller is ok with full reset, we enable and reset
402
+ if (forceEnable) {
403
+ this.config.setFlagEnabled("UseMic", true);
404
+ this.reconnect();
405
+ return;
406
+ }
407
+
408
+ // If we prefer not to force a reconnection, just warn the user that this operation didn't happen
409
+ Logger.Warning(
410
+ Logger.GetStackTrace(),
411
+ 'Trying to unmute mic, but PixelStreaming was initialized with no microphone track. Call with forceEnable == true to re-connect with a mic track.'
412
+ );
413
+ }
414
+
415
+ public muteMicrophone() : void {
416
+ if (this.config.isFlagEnabled('UseMic')) {
417
+ this.setMicrophoneMuted(true);
418
+ return;
419
+ }
420
+
421
+ // If there wasn't a mic track, just let user know there's nothing to mute
422
+ Logger.Info(
423
+ Logger.GetStackTrace(),
424
+ 'Trying to mute mic, but PixelStreaming has no microphone track, so sending sound is already disabled.'
425
+ );
426
+ }
427
+
428
+ private setMicrophoneMuted(mute: boolean) : void
429
+ {
430
+ for (const transceiver of this._webRtcController?.peerConnectionController?.peerConnection?.getTransceivers() ?? []) {
431
+ if (RTCUtils.canTransceiverSendAudio(transceiver)) {
432
+ transceiver.sender.track.enabled = !mute;
433
+ }
434
+ }
435
+ }
436
+
437
+ /**
438
+ * Emit an event on auto connecting
439
+ */
440
+ _onWebRtcAutoConnect() {
441
+ this._eventEmitter.dispatchEvent(new WebRtcAutoConnectEvent());
442
+ }
443
+
444
+ /**
445
+ * Set up functionality to happen when receiving a webRTC answer
446
+ */
447
+ _onWebRtcSdp() {
448
+ this._eventEmitter.dispatchEvent(new WebRtcSdpEvent());
449
+ }
450
+
451
+ /**
452
+ * Emits a StreamLoading event
453
+ */
454
+ _onStreamLoading() {
455
+ this._eventEmitter.dispatchEvent(new StreamLoadingEvent());
456
+ }
457
+
458
+ /**
459
+ * Event fired when the video is disconnected - emits given eventString or an override
460
+ * message from webRtcController if one has been set
461
+ * @param eventString - a string describing why the connection closed
462
+ * @param allowClickToReconnect - true if we want to allow the user to retry the connection with a click
463
+ */
464
+ _onDisconnect(eventString: string, allowClickToReconnect: boolean) {
465
+ this._eventEmitter.dispatchEvent(
466
+ new WebRtcDisconnectedEvent({
467
+ eventString: eventString,
468
+ allowClickToReconnect: allowClickToReconnect
469
+ })
470
+ );
471
+ }
472
+
473
+ /**
474
+ * Handles when Web Rtc is connecting
475
+ */
476
+ _onWebRtcConnecting() {
477
+ this._eventEmitter.dispatchEvent(new WebRtcConnectingEvent());
478
+ }
479
+
480
+ /**
481
+ * Handles when Web Rtc has connected
482
+ */
483
+ _onWebRtcConnected() {
484
+ this._eventEmitter.dispatchEvent(new WebRtcConnectedEvent());
485
+ }
486
+
487
+ /**
488
+ * Handles when Web Rtc fails to connect
489
+ */
490
+ _onWebRtcFailed() {
491
+ this._eventEmitter.dispatchEvent(new WebRtcFailedEvent());
492
+ }
493
+
494
+ /**
495
+ * Handle when the Video has been Initialized
496
+ */
497
+ _onVideoInitialized() {
498
+ this._eventEmitter.dispatchEvent(new VideoInitializedEvent());
499
+ this._videoStartTime = Date.now();
500
+ }
501
+
502
+ /**
503
+ * Set up functionality to happen when receiving latency test results
504
+ * @param latency - latency test results object
505
+ */
506
+ _onLatencyTestResult(latencyTimings: LatencyTestResults) {
507
+ this._eventEmitter.dispatchEvent(
508
+ new LatencyTestResultEvent({ latencyTimings })
509
+ );
510
+ }
511
+
512
+ _onDataChannelLatencyTestResponse(response: DataChannelLatencyTestResponse) {
513
+ this._eventEmitter.dispatchEvent(
514
+ new DataChannelLatencyTestResponseEvent({ response })
515
+ );
516
+ }
517
+
518
+ /**
519
+ * Set up functionality to happen when receiving video statistics
520
+ * @param videoStats - video statistics as a aggregate stats object
521
+ */
522
+ _onVideoStats(videoStats: AggregatedStats) {
523
+ // Duration
524
+ if (!this._videoStartTime || this._videoStartTime === undefined) {
525
+ this._videoStartTime = Date.now();
526
+ }
527
+ videoStats.handleSessionStatistics(
528
+ this._videoStartTime,
529
+ this._inputController,
530
+ this._webRtcController.videoAvgQp
531
+ );
532
+
533
+ this._eventEmitter.dispatchEvent(
534
+ new StatsReceivedEvent({ aggregatedStats: videoStats })
535
+ );
536
+ }
537
+
538
+ /**
539
+ * Set up functionality to happen when calculating the average video encoder qp
540
+ * @param QP - the quality number of the stream
541
+ */
542
+ _onVideoEncoderAvgQP(QP: number) {
543
+ this._eventEmitter.dispatchEvent(
544
+ new VideoEncoderAvgQPEvent({ avgQP: QP })
545
+ );
546
+ }
547
+
548
+ /**
549
+ * Set up functionality to happen when receiving and handling initial settings for the UE app
550
+ * @param settings - initial UE app settings
551
+ */
552
+ _onInitialSettings(settings: InitialSettings) {
553
+ this._eventEmitter.dispatchEvent(
554
+ new InitialSettingsEvent({ settings })
555
+ );
556
+ if (settings.PixelStreamingSettings) {
557
+ this.allowConsoleCommands =
558
+ settings.PixelStreamingSettings.AllowPixelStreamingCommands ?? false;
559
+ if (this.allowConsoleCommands === false) {
560
+ Logger.Info(
561
+ Logger.GetStackTrace(),
562
+ '-AllowPixelStreamingCommands=false, sending arbitrary console commands from browser to UE is disabled.'
563
+ );
564
+ }
565
+ }
566
+
567
+ const useUrlParams = this.config.useUrlParams;
568
+ const urlParams = new URLSearchParams(window.location.search);
569
+ Logger.Info(
570
+ Logger.GetStackTrace(),
571
+ `using URL parameters ${useUrlParams}`
572
+ );
573
+ if (settings.EncoderSettings) {
574
+ this.config.setNumericSetting(
575
+ NumericParameters.MinQP,
576
+ // If a setting is set in the URL, make sure we respect that value as opposed to what the application sends us
577
+ (useUrlParams && urlParams.has(NumericParameters.MinQP))
578
+ ? Number.parseFloat(urlParams.get(NumericParameters.MinQP))
579
+ : settings.EncoderSettings.MinQP
580
+ );
581
+
582
+
583
+ this.config.setNumericSetting(
584
+ NumericParameters.MaxQP,
585
+ (useUrlParams && urlParams.has(NumericParameters.MaxQP))
586
+ ? Number.parseFloat(urlParams.get(NumericParameters.MaxQP))
587
+ : settings.EncoderSettings.MaxQP
588
+ );
589
+ }
590
+ if (settings.WebRTCSettings) {
591
+ this.config.setNumericSetting(
592
+ NumericParameters.WebRTCMinBitrate,
593
+ (useUrlParams && urlParams.has(NumericParameters.WebRTCMinBitrate))
594
+ ? Number.parseFloat(urlParams.get(NumericParameters.WebRTCMinBitrate))
595
+ : (settings.WebRTCSettings.MinBitrate / 1000) /* bps to kbps */
596
+ );
597
+ this.config.setNumericSetting(
598
+ NumericParameters.WebRTCMaxBitrate,
599
+ (useUrlParams && urlParams.has(NumericParameters.WebRTCMaxBitrate))
600
+ ? Number.parseFloat(urlParams.get(NumericParameters.WebRTCMaxBitrate))
601
+ : (settings.WebRTCSettings.MaxBitrate / 1000) /* bps to kbps */
602
+
603
+ );
604
+ this.config.setNumericSetting(
605
+ NumericParameters.WebRTCFPS,
606
+ (useUrlParams && urlParams.has(NumericParameters.WebRTCFPS))
607
+ ? Number.parseFloat(urlParams.get(NumericParameters.WebRTCFPS))
608
+ : settings.WebRTCSettings.FPS
609
+ );
610
+ }
611
+ }
612
+
613
+ /**
614
+ * Set up functionality to happen when setting quality control ownership of a stream
615
+ * @param hasQualityOwnership - does this user have quality ownership of the stream true / false
616
+ */
617
+ _onQualityControlOwnership(hasQualityOwnership: boolean) {
618
+ this.config.setFlagEnabled(
619
+ Flags.IsQualityController,
620
+ hasQualityOwnership
621
+ );
622
+ }
623
+
624
+ _onPlayerCount(playerCount: number) {
625
+ this._eventEmitter.dispatchEvent(
626
+ new PlayerCountEvent({ count: playerCount })
627
+ );
628
+ }
629
+
630
+ /**
631
+ * Request a connection latency test.
632
+ * NOTE: There are plans to refactor all request* functions. Expect changes if you use this!
633
+ * @returns
634
+ */
635
+ public requestLatencyTest() {
636
+ if (!this._webRtcController.videoPlayer.isVideoReady()) {
637
+ return false;
638
+ }
639
+ this._webRtcController.sendLatencyTest();
640
+ return true;
641
+ }
642
+
643
+ /**
644
+ * Request a data channel latency test.
645
+ * NOTE: There are plans to refactor all request* functions. Expect changes if you use this!
646
+ */
647
+ public requestDataChannelLatencyTest(config: DataChannelLatencyTestConfig) {
648
+ if (!this._webRtcController.videoPlayer.isVideoReady()) {
649
+ return false;
650
+ }
651
+ if (!this._dataChannelLatencyTestController) {
652
+ this._dataChannelLatencyTestController = new DataChannelLatencyTestController(
653
+ this._webRtcController.sendDataChannelLatencyTest.bind(this._webRtcController),
654
+ (result: DataChannelLatencyTestResult) => {
655
+ this._eventEmitter.dispatchEvent(new DataChannelLatencyTestResultEvent( { result }))
656
+ });
657
+ this.addEventListener(
658
+ "dataChannelLatencyTestResponse",
659
+ ({data: {response} }) => {
660
+ this._dataChannelLatencyTestController.receive(response);
661
+ }
662
+ )
663
+ }
664
+ return this._dataChannelLatencyTestController.start(config);
665
+ }
666
+
667
+ /**
668
+ * Request for the UE application to show FPS counter.
669
+ * NOTE: There are plans to refactor all request* functions. Expect changes if you use this!
670
+ * @returns
671
+ */
672
+ public requestShowFps() {
673
+ if (!this._webRtcController.videoPlayer.isVideoReady()) {
674
+ return false;
675
+ }
676
+ this._webRtcController.sendShowFps();
677
+ return true;
678
+ }
679
+
680
+ /**
681
+ * Request for a new IFrame from the UE application.
682
+ * NOTE: There are plans to refactor all request* functions. Expect changes if you use this!
683
+ * @returns
684
+ */
685
+ public requestIframe() {
686
+ if (!this._webRtcController.videoPlayer.isVideoReady()) {
687
+ return false;
688
+ }
689
+ this._webRtcController.sendIframeRequest();
690
+ return true;
691
+ }
692
+
693
+ /**
694
+ * Send data to UE application. The data will be run through JSON.stringify() so e.g. strings
695
+ * and any serializable plain JSON objects with no recurrence can be sent.
696
+ * @returns true if succeeded, false if rejected
697
+ */
698
+ public emitUIInteraction(descriptor: object | string) {
699
+ if (!this._webRtcController.videoPlayer.isVideoReady()) {
700
+ return false;
701
+ }
702
+ this._webRtcController.emitUIInteraction(descriptor);
703
+ return true;
704
+ }
705
+
706
+ /**
707
+ * Send a command to UE application. Blocks ConsoleCommand descriptors unless UE
708
+ * has signaled that it allows console commands.
709
+ * @returns true if succeeded, false if rejected
710
+ */
711
+ public emitCommand(descriptor: object) {
712
+ if (!this._webRtcController.videoPlayer.isVideoReady()) {
713
+ return false;
714
+ }
715
+ if (!this.allowConsoleCommands && 'ConsoleCommand' in descriptor) {
716
+ return false;
717
+ }
718
+ this._webRtcController.emitCommand(descriptor);
719
+ return true;
720
+ }
721
+
722
+ /**
723
+ * Send a console command to UE application. Only allowed if UE has signaled that it allows
724
+ * console commands.
725
+ * @returns true if succeeded, false if rejected
726
+ */
727
+ public emitConsoleCommand(command: string) {
728
+ if (!this.allowConsoleCommands || !this._webRtcController.videoPlayer.isVideoReady()) {
729
+ return false;
730
+ }
731
+ this._webRtcController.emitConsoleCommand(command);
732
+ return true;
733
+ }
734
+
735
+ /**
736
+ * Add a UE -> browser response event listener
737
+ * @param name - The name of the response handler
738
+ * @param listener - The method to be activated when a message is received
739
+ */
740
+ public addResponseEventListener(
741
+ name: string,
742
+ listener: (response: string) => void
743
+ ) {
744
+ this._webRtcController.responseController.addResponseEventListener(name, listener);
745
+ }
746
+
747
+ /**
748
+ * Remove a UE -> browser response event listener
749
+ * @param name - The name of the response handler
750
+ */
751
+ public removeResponseEventListener(name: string) {
752
+ this._webRtcController.responseController.removeResponseEventListener(name);
753
+ }
754
+
755
+ /**
756
+ * Dispatch a new event.
757
+ * @param e event
758
+ * @returns
759
+ */
760
+ public dispatchEvent(e: PixelStreamingEvent): boolean {
761
+ return this._eventEmitter.dispatchEvent(e);
762
+ }
763
+
764
+ /**
765
+ * Register an event handler.
766
+ * @param type event name
767
+ * @param listener event handler function
768
+ */
769
+ public addEventListener<
770
+ T extends PixelStreamingEvent['type'],
771
+ E extends PixelStreamingEvent & { type: T }
772
+ >(type: T, listener: (e: Event & E) => void) {
773
+ this._eventEmitter.addEventListener(type, listener);
774
+ }
775
+
776
+ /**
777
+ * Remove an event handler.
778
+ * @param type event name
779
+ * @param listener event handler function
780
+ */
781
+ public removeEventListener<
782
+ T extends PixelStreamingEvent['type'],
783
+ E extends PixelStreamingEvent & { type: T }
784
+ >(type: T, listener: (e: Event & E) => void) {
785
+ this._eventEmitter.removeEventListener(type, listener);
786
+ }
787
+
788
+ /**
789
+ * Enable/disable XR mode.
790
+ */
791
+ public toggleXR() {
792
+ this.webXrController.xrClicked();
793
+ }
794
+
795
+ /**
796
+ * Pass in a function to generate a signalling server URL.
797
+ * This function is useful if you need to programmatically construct your signalling server URL.
798
+ * @param signallingUrlBuilderFunc A function that generates a signalling server url.
799
+ */
800
+ public setSignallingUrlBuilder(signallingUrlBuilderFunc: ()=>string) {
801
+ this._webRtcController.signallingUrlBuilder = signallingUrlBuilderFunc;
802
+ }
803
+
804
+ /**
805
+ * Public getter for the websocket controller. Access to this property allows you to send
806
+ * custom websocket messages.
807
+ */
808
+ public get signallingProtocol() {
809
+ return this._webRtcController.protocol;
810
+ }
811
+
812
+ /**
813
+ * Public getter for the webXrController controller. Used for all XR features.
814
+ */
815
+ public get webXrController() {
816
+ return this._webXrController;
817
+ }
818
+
819
+ public registerMessageHandler(name: string, direction: MessageDirection, handler?: (data: ArrayBuffer | Array<number | string>) => void) {
820
+ if(direction === MessageDirection.FromStreamer && typeof handler === 'undefined') {
821
+ Logger.Warning(Logger.GetStackTrace(), `Unable to register an undefined handler for ${name}`)
822
+ return;
823
+ }
824
+
825
+ if(direction === MessageDirection.ToStreamer && typeof handler === 'undefined') {
826
+ this._webRtcController.streamMessageController.registerMessageHandler(
827
+ direction,
828
+ name,
829
+ (data: Array<number | string>) =>
830
+ this._webRtcController.sendMessageController.sendMessageToStreamer(
831
+ name,
832
+ data
833
+ )
834
+ );
835
+ } else {
836
+ this._webRtcController.streamMessageController.registerMessageHandler(
837
+ direction,
838
+ name,
839
+ (data: ArrayBuffer) => handler(data)
840
+ );
841
+ }
842
+ }
843
+
844
+ public get toStreamerHandlers() {
845
+ return this._webRtcController.streamMessageController.toStreamerHandlers;
846
+ }
847
+
848
+ public isReconnecting() {
849
+ return this._webRtcController.isReconnecting;
850
+ }
851
+ }