forked from netless-io/flat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartPlayer.ts
More file actions
325 lines (289 loc) · 10 KB
/
SmartPlayer.ts
File metadata and controls
325 lines (289 loc) · 10 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
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
import "video.js/dist/video-js.css";
import "@netless/window-manager/dist/style.css";
import CombinePlayerFactory, { CombinePlayer, PublicCombinedStatus } from "@netless/combine-player";
import {
PluginId as VideoJsPluginId,
videoJsPlugin,
PluginContext as VideoJsPluginContext,
} from "@netless/video-js-plugin";
import { EventEmitter } from "eventemitter3";
import polly from "polly-js";
import {
createPlugins,
PlayableCheckingParams,
Player,
PlayerPhase,
ReplayRoomParams,
WhiteWebSdk,
} from "white-web-sdk";
import { Region } from "flat-components";
import { NETLESS, NODE_ENV } from "../constants/process";
import { WindowManager } from "@netless/window-manager";
export enum SmartPlayerEventType {
Ready = "Ready",
Play = "Play",
Pause = "Pause",
Ended = "Ended",
Error = "Error",
}
export type SmartPlayerEvents = {
[SmartPlayerEventType.Ready]: void;
[SmartPlayerEventType.Play]: void;
[SmartPlayerEventType.Pause]: void;
[SmartPlayerEventType.Ended]: void;
[SmartPlayerEventType.Error]: Error;
};
export declare interface SmartPlayer {
on<U extends keyof SmartPlayerEvents>(
event: U,
listener: (value: SmartPlayerEvents[U]) => void,
): this;
once<U extends keyof SmartPlayerEvents>(
event: U,
listener: (value: SmartPlayerEvents[U]) => void,
): this;
}
// eslint-disable-next-line no-redeclare
export class SmartPlayer extends EventEmitter<SmartPlayerEventType> {
public whiteboardPlayer: Player | undefined;
public combinePlayer: CombinePlayer | undefined;
public whiteWebSdk: WhiteWebSdk | undefined;
public get isPlaying(): boolean {
return this._isPlaying;
}
public get isReady(): boolean {
return this._isReady;
}
public get isEnded(): boolean {
return this._isEnded;
}
public async load({
whiteboardUUID,
whiteboardRoomToken,
region,
whiteboardEl,
videoEl,
recording,
}: {
roomUUID: string;
isCreator: boolean;
whiteboardUUID: string;
whiteboardRoomToken: string;
region?: Region;
whiteboardEl: HTMLDivElement;
videoEl: HTMLVideoElement;
recording?: {
beginTime: number;
endTime: number;
videoURL?: string;
};
}): Promise<void> {
this._isPlaying = false;
this._isReady = false;
this._isEnded = false;
this.destroy();
const plugins = createPlugins({ [VideoJsPluginId]: videoJsPlugin() });
const videoJsPluginContext: Partial<VideoJsPluginContext> = { verbose: true };
plugins.setPluginContext(VideoJsPluginId, videoJsPluginContext);
const whiteWebSdk = new WhiteWebSdk({
appIdentifier: NETLESS.APP_IDENTIFIER,
plugins: plugins,
region,
useMobXState: true,
});
this.whiteWebSdk = whiteWebSdk;
const rangeQuery: PlayableCheckingParams = {
room: whiteboardUUID,
roomToken: whiteboardRoomToken,
region,
};
if (recording) {
rangeQuery.beginTimestamp = recording.beginTime;
rangeQuery.duration = recording.endTime - rangeQuery.beginTimestamp;
}
await polly()
.waitAndRetry(30)
.executeForPromise(async (): Promise<void> => {
const replayState = await whiteWebSdk.isPlayable(rangeQuery);
if (!replayState) {
return Promise.reject("Whiteboard is not playable");
}
});
const replayRoomParams: ReplayRoomParams = {
room: whiteboardUUID,
roomToken: whiteboardRoomToken,
region,
invisiblePlugins: [WindowManager],
useMultiViews: true,
};
if (recording) {
replayRoomParams.beginTimestamp = recording.beginTime;
replayRoomParams.duration = recording.endTime - replayRoomParams.beginTimestamp;
}
const player = await whiteWebSdk.replayRoom(replayRoomParams, {
onPhaseChanged: phase => {
if (this.combinePlayer) {
return;
}
switch (phase) {
case PlayerPhase.Playing: {
this._isPlaying = true;
this._isEnded = false;
this.emit(SmartPlayerEventType.Play);
if (NODE_ENV === "development") {
console.log("SmartPlayer: start playing");
}
break;
}
case PlayerPhase.Pause: {
this._isPlaying = false;
this.emit(SmartPlayerEventType.Pause);
if (NODE_ENV === "development") {
console.log("SmartPlayer: paused");
}
break;
}
case PlayerPhase.Ended:
case PlayerPhase.Stopped: {
this._isPlaying = false;
this._isEnded = true;
this.emit(SmartPlayerEventType.Ended);
if (NODE_ENV === "development") {
console.log("SmartPlayer: ended");
}
break;
}
default: {
break;
}
}
},
onStoppedWithError: (error: Error) => {
this.emit(SmartPlayerEventType.Error, error);
},
});
void WindowManager.mount({
room: player as any,
container: whiteboardEl,
cursor: true,
chessboard: false,
});
this.whiteboardPlayer = player;
if (recording?.videoURL) {
const combinePlayerFactory = new CombinePlayerFactory(player, {
url: recording.videoURL,
videoDOM: videoEl,
});
const combinePlayer = combinePlayerFactory.create();
combinePlayer.setOnStatusChange(status => {
switch (status) {
case PublicCombinedStatus.Playing: {
this._isPlaying = true;
this._isEnded = false;
this.emit(SmartPlayerEventType.Play);
if (NODE_ENV === "development") {
console.log("SmartPlayer: start playing");
}
break;
}
case PublicCombinedStatus.Pause: {
this._isPlaying = false;
this.emit(SmartPlayerEventType.Pause);
if (NODE_ENV === "development") {
console.log("SmartPlayer: paused");
}
break;
}
case PublicCombinedStatus.Ended: {
this._isPlaying = false;
this._isEnded = true;
this.emit(SmartPlayerEventType.Ended);
if (NODE_ENV === "development") {
console.log("SmartPlayer: ended");
}
break;
}
default: {
break;
}
}
});
this.combinePlayer = combinePlayer;
}
this._isReady = true;
this.emit(SmartPlayerEventType.Ready);
if (NODE_ENV === "development") {
console.log("SmartPlayer: ready");
}
if (NODE_ENV === "development") {
(window as any).player = player;
(window as any).combinePlayer = this.combinePlayer;
}
}
public play(): void {
if (this._isPlaying) {
return;
}
const whiteboardPlayer = this.checkWhiteboardLoaded();
if (this.combinePlayer) {
this.combinePlayer.play();
} else {
whiteboardPlayer.play();
}
}
public playBackRate(rate: number): void {
const whiteboardPlayer = this.checkWhiteboardLoaded();
if (this.combinePlayer) {
this.combinePlayer.playbackRate = rate;
} else {
whiteboardPlayer.playbackSpeed = rate;
}
}
public pause(): void {
if (!this._isPlaying) {
return;
}
const whiteboardPlayer = this.checkWhiteboardLoaded();
if (this.combinePlayer) {
this.combinePlayer.pause();
} else {
whiteboardPlayer.pause();
}
}
public seek(ms: number): void {
this._isEnded = false;
const whiteboardPlayer = this.checkWhiteboardLoaded();
if (this.combinePlayer) {
this.combinePlayer.seek(ms);
} else {
void whiteboardPlayer.seekToProgressTime(ms);
}
}
public destroy(): void {
this.whiteWebSdk = undefined;
if (this._isPlaying) {
this.whiteboardPlayer?.stop();
this.combinePlayer?.pause();
}
if (this.whiteboardPlayer) {
this.whiteboardPlayer.callbacks.off();
}
this.combinePlayer?.removeAllStatusChange();
this.whiteboardPlayer = undefined;
this.combinePlayer = undefined;
if (NODE_ENV === "development") {
(window as any).player = null;
(window as any).combinePlayer = null;
}
}
private _isPlaying = false;
private _isReady = false;
private _isEnded = false;
private checkWhiteboardLoaded(): Player {
if (!this.whiteboardPlayer) {
throw new Error("Whiteboard Player not loaded. Call `smartPlayer.load()` first.");
}
return this.whiteboardPlayer;
}
}
export default SmartPlayer;