From c3f0b834c0ad56641bc1ec7fb43e5eea62ea144d Mon Sep 17 00:00:00 2001 From: Luke Bermingham <1215582+lukehb@users.noreply.github.com> Date: Sat, 25 Feb 2023 20:28:10 +1000 Subject: [PATCH] Added XR events: xrSessionStarted, xrSessionEnded, xrFrame (#125) --- .../src/PixelStreaming/PixelStreaming.ts | 6 +-- Frontend/library/src/Util/EventEmitter.ts | 44 ++++++++++++++++++- Frontend/library/src/WebXR/WebXRController.ts | 42 ++++++++++++------ 3 files changed, 73 insertions(+), 19 deletions(-) diff --git a/Frontend/library/src/PixelStreaming/PixelStreaming.ts b/Frontend/library/src/PixelStreaming/PixelStreaming.ts index c3f6020b..e647b6f0 100644 --- a/Frontend/library/src/PixelStreaming/PixelStreaming.ts +++ b/Frontend/library/src/PixelStreaming/PixelStreaming.ts @@ -6,11 +6,7 @@ import { AggregatedStats } from '../PeerConnectionController/AggregatedStats'; import { WebRtcPlayerController } from '../WebRtcPlayer/WebRtcPlayerController'; import { Flags, NumericParameters } from '../Config/Config'; import { Logger } from '../Logger/Logger'; -import { - InitialSettings, - EncoderSettings, - WebRTCSettings -} from '../DataChannel/InitialSettings'; +import { InitialSettings } from '../DataChannel/InitialSettings'; import { OnScreenKeyboard } from '../UI/OnScreenKeyboard'; import { EventEmitter, diff --git a/Frontend/library/src/Util/EventEmitter.ts b/Frontend/library/src/Util/EventEmitter.ts index c172ebdd..89eb3a51 100644 --- a/Frontend/library/src/Util/EventEmitter.ts +++ b/Frontend/library/src/Util/EventEmitter.ts @@ -401,6 +401,45 @@ export class SettingsChangedEvent extends Event { } } +/** + * Event emitted when an XR Session starts + */ +export class XrSessionStartedEvent extends Event { + readonly type: 'xrSessionStarted'; + constructor() { + super('xrSessionStarted'); + } +} + +/** + * Event emitted when an XR Session ends + */ +export class XrSessionEndedEvent extends Event { + readonly type: 'xrSessionEnded'; + constructor() { + super('xrSessionEnded'); + } +} + +export type XrFrameData = { + /** The frame timestamp */ + time: DOMHighResTimeStamp; + /** The XRFrame */ + frame: XRFrame; +}; + +/** + * Event emitted when an XR Frame is complete + */ +export class XrFrameEvent extends Event { + readonly type: 'xrFrame'; + readonly data: XrFrameData + constructor(data: XrFrameEvent['data']) { + super('xrFrame'); + this.data = data; + } +} + export type PixelStreamingEvent = | AfkWarningActivateEvent | AfkWarningUpdateEvent @@ -427,7 +466,10 @@ export type PixelStreamingEvent = | StreamerListMessageEvent | LatencyTestResultEvent | InitialSettingsEvent - | SettingsChangedEvent; + | SettingsChangedEvent + | XrSessionStartedEvent + | XrSessionEndedEvent + | XrFrameEvent; export class EventEmitter extends EventTarget { /** diff --git a/Frontend/library/src/WebXR/WebXRController.ts b/Frontend/library/src/WebXR/WebXRController.ts index 955f7af2..c2d087b6 100644 --- a/Frontend/library/src/WebXR/WebXRController.ts +++ b/Frontend/library/src/WebXR/WebXRController.ts @@ -1,27 +1,32 @@ // Copyright Epic Games, Inc. All Rights Reserved. import { Logger } from '../Logger/Logger'; -import { WebRtcPlayerController } from '../pixelstreamingfrontend'; +import { WebRtcPlayerController } from '../WebRtcPlayer/WebRtcPlayerController'; import { WebGLUtils } from '../Util/WebGLUtils'; import { Controller } from '../Inputs/GamepadTypes'; import { XRGamepadController } from '../Inputs/XRGamepadController'; +import { XrFrameEvent } from '../Util/EventEmitter' export class WebXRController { - xrSession: XRSession; - xrRefSpace: XRReferenceSpace; - gl: WebGL2RenderingContext; + private xrSession: XRSession; + private xrRefSpace: XRReferenceSpace; + private gl: WebGL2RenderingContext; - positionLocation: number; - texcoordLocation: number; - resolutionLocation: WebGLUniformLocation; - offsetLocation: WebGLUniformLocation; + private positionLocation: number; + private texcoordLocation: number; + private resolutionLocation: WebGLUniformLocation; + private offsetLocation: WebGLUniformLocation; - positionBuffer: WebGLBuffer; - texcoordBuffer: WebGLBuffer; + private positionBuffer: WebGLBuffer; + private texcoordBuffer: WebGLBuffer; - webRtcController: WebRtcPlayerController; - xrGamepadController: XRGamepadController; - xrControllers: Array; + private webRtcController: WebRtcPlayerController; + private xrGamepadController: XRGamepadController; + private xrControllers: Array; + + onSessionStarted: EventTarget; + onSessionEnded: EventTarget; + onFrame: EventTarget; constructor(webRtcPlayerController: WebRtcPlayerController) { this.xrSession = null; @@ -30,6 +35,9 @@ export class WebXRController { this.xrGamepadController = new XRGamepadController( this.webRtcController.streamMessageController ); + this.onSessionEnded = new EventTarget(); + this.onSessionStarted = new EventTarget(); + this.onFrame = new EventTarget(); } public xrClicked() { @@ -47,6 +55,7 @@ export class WebXRController { onXrSessionEnded() { Logger.Log(Logger.GetStackTrace(), 'XR Session ended'); this.xrSession = null; + this.onSessionEnded.dispatchEvent(new Event('xrSessionEnded')); } onXrSessionStarted(session: XRSession) { @@ -142,6 +151,8 @@ export class WebXRController { this.onXrFrame(time, frame) ); }); + + this.onSessionStarted.dispatchEvent(new Event('xrSessionStarted')); } onXrFrame(time: DOMHighResTimeStamp, frame: XRFrame) { @@ -194,6 +205,11 @@ export class WebXRController { (time: DOMHighResTimeStamp, frame: XRFrame) => this.onXrFrame(time, frame) ); + + this.onFrame.dispatchEvent(new XrFrameEvent({ + time, + frame + })); } private render(videoElement: HTMLVideoElement) {