-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathVideoPlayer.ts
More file actions
241 lines (208 loc) · 7.78 KB
/
VideoPlayer.ts
File metadata and controls
241 lines (208 loc) · 7.78 KB
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
// Copyright Epic Games, Inc. All Rights Reserved.
import { Config, Flags, NumericParameters } from '../Config/Config';
import { Logger } from '@epicgames-ps/lib-pixelstreamingcommon-ue5.7';
/**
* Extra types for the HTMLElement
*/
declare global {
interface HTMLElement {
mozRequestPointerLock(): Promise<void>;
}
}
/**
* The video player html element
*/
export class VideoPlayer {
private config: Config;
private videoElement: HTMLVideoElement;
private audioElement?: HTMLAudioElement;
private orientationChangeTimeout: number;
private lastTimeResized = new Date().getTime();
onMatchViewportResolutionCallback: (width: number, height: number) => void;
onResizePlayerCallback: () => void;
resizeTimeoutHandle: number;
/**
* @param videoElementParent the html div the the video player will be injected into
* @param config the applications configuration. We're interested in the startVideoMuted flag
*/
constructor(videoElementParent: HTMLElement, config: Config) {
this.videoElement = document.createElement('video');
this.config = config;
this.videoElement.id = 'streamingVideo';
this.videoElement.disablePictureInPicture = true;
this.videoElement.playsInline = true;
this.videoElement.style.width = '100%';
this.videoElement.style.height = '100%';
this.videoElement.style.position = 'absolute';
this.videoElement.style.pointerEvents = 'all';
videoElementParent.appendChild(this.videoElement);
this.onResizePlayerCallback = () => {
console.log('Resolution changed, restyling player, did you forget to override this function?');
};
this.onMatchViewportResolutionCallback = () => {
console.log(
'Resolution changed and match viewport resolution is turned on, did you forget to override this function?'
);
};
// set play for video (and audio)
this.videoElement.onclick = () => {
if (this.audioElement != undefined && this.audioElement.paused) {
this.audioElement.play();
}
if (this.videoElement.paused) {
this.videoElement.play();
}
};
this.videoElement.onloadedmetadata = () => {
this.onVideoInitialized();
};
// set resize events to the windows if it is resized or its orientation is changed
window.addEventListener('resize', () => this.resizePlayerStyle(), true);
window.addEventListener('orientationchange', () => this.onOrientationChange());
}
public destroy() {
this.videoElement.src = '';
this.videoElement.srcObject = null;
this.videoElement.remove();
if (this.audioElement) {
this.audioElement.src = '';
this.audioElement.srcObject = null;
this.audioElement.remove();
}
}
public setAudioElement(audioElement: HTMLAudioElement): void {
this.audioElement = audioElement;
}
/**
* Sets up the video element with any application config and plays the video element.
* @returns A promise for if playing the video was successful or not.
*/
play(): Promise<void> {
this.videoElement.muted = this.config.isFlagEnabled(Flags.StartVideoMuted);
this.videoElement.autoplay = this.config.isFlagEnabled(Flags.AutoPlayVideo);
return this.videoElement.play();
}
/**
* @returns True if the video element is paused.
*/
isPaused(): boolean {
return this.videoElement.paused;
}
/**
* @returns - whether the video element is playing.
*/
isVideoReady(): boolean {
return this.videoElement.readyState !== undefined && this.videoElement.readyState > 0;
}
/**
* @returns True if the video element has a valid video source (srcObject).
*/
hasVideoSource(): boolean {
return this.videoElement.srcObject !== undefined && this.videoElement.srcObject !== null;
}
/**
* Get the current context of the html video element
* @returns - the current context of the video element
*/
getVideoElement(): HTMLVideoElement {
return this.videoElement;
}
/**
* Get the current context of the html video elements parent
* @returns - the current context of the video elements parent
*/
getVideoParentElement(): HTMLElement | undefined {
return this.videoElement.parentElement ?? undefined;
}
/**
* Set the Video Elements src object tracks to enable
* @param enabled - Enable Tracks on the Src Object
*/
setVideoEnabled(enabled: boolean) {
// this is a temporary hack until type scripts video element is updated to reflect the need for tracks on a html video element
const videoElement = this.videoElement;
(<MediaStream>videoElement.srcObject)
.getTracks()
.forEach((track: MediaStreamTrack) => (track.enabled = enabled));
}
/**
* An override for when the video has been initialized with a srcObject
*/
onVideoInitialized() {
// Default Functionality: Do Nothing
}
/**
* On the orientation change of a window clear the timeout
*/
onOrientationChange() {
clearTimeout(this.orientationChangeTimeout);
this.orientationChangeTimeout = window.setTimeout(() => {
this.resizePlayerStyle();
}, 500);
}
/**
* Resizes the player style based on the window height and width
* @returns - nil if requirements are satisfied
*/
resizePlayerStyle() {
const videoElementParent = this.getVideoParentElement();
if (!videoElementParent) {
return;
}
this.updateVideoStreamSize();
if (videoElementParent.classList.contains('fixed-size')) {
this.onResizePlayerCallback();
return;
}
// controls for resizing the player
this.resizePlayerStyleToFillParentElement();
this.onResizePlayerCallback();
}
/**
* Resizes the player element to fill the parent element
*/
resizePlayerStyleToFillParentElement() {
const videoElementParent = this.getVideoParentElement();
//Video is not initialized yet so set videoElementParent to size of parent element
const styleWidth = '100%';
const styleHeight = '100%';
const styleTop = 0;
const styleLeft = 0;
videoElementParent.setAttribute(
'style',
'top: ' +
styleTop +
'px; left: ' +
styleLeft +
'px; width: ' +
styleWidth +
'; height: ' +
styleHeight +
'; cursor: default;'
);
}
updateVideoStreamSize() {
if (!this.config.isFlagEnabled(Flags.MatchViewportResolution)) {
return;
}
const now = new Date().getTime();
if (now - this.lastTimeResized > 300) {
const videoElementParent = this.getVideoParentElement();
if (!videoElementParent) {
return;
}
const viewportResolutionScale = this.config.getNumericSettingValue(
NumericParameters.ViewportResolutionScale
);
this.onMatchViewportResolutionCallback(
videoElementParent.clientWidth * viewportResolutionScale,
videoElementParent.clientHeight * viewportResolutionScale
);
this.lastTimeResized = new Date().getTime();
} else {
Logger.Info('Resizing too often - skipping');
clearTimeout(this.resizeTimeoutHandle);
this.resizeTimeoutHandle = window.setTimeout(() => this.updateVideoStreamSize(), 100);
}
}
}