@clockworkdog/cogs-client 3.0.0-alpha.3 → 3.0.0-alpha.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.
- package/dist/AudioPlayer.js +5 -4
- package/dist/CogsConnection.js +37 -20
- package/dist/DataStore.js +11 -15
- package/dist/VideoPlayer.js +8 -4
- package/dist/browser/index.mjs +1504 -1257
- package/dist/browser/index.umd.js +13 -13
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/state-based/AudioManager.d.ts +15 -0
- package/dist/state-based/AudioManager.js +112 -0
- package/dist/state-based/ClipManager.d.ts +22 -0
- package/dist/state-based/ClipManager.js +53 -0
- package/dist/state-based/ImageManager.d.ts +9 -0
- package/dist/state-based/ImageManager.js +53 -0
- package/dist/state-based/SurfaceManager.d.ts +16 -0
- package/dist/state-based/SurfaceManager.js +80 -0
- package/dist/state-based/VideoManager.d.ts +15 -0
- package/dist/state-based/VideoManager.js +126 -0
- package/dist/types/MediaSchema.d.ts +13 -0
- package/dist/types/MediaSchema.js +3 -0
- package/dist/utils/getStateAtTime.js +3 -3
- package/package.json +12 -4
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export type { default as MediaObjectFit } from './types/MediaObjectFit';
|
|
|
7
7
|
export * as MediaSchema from './types/MediaSchema';
|
|
8
8
|
export { default as CogsAudioPlayer } from './AudioPlayer';
|
|
9
9
|
export { default as CogsVideoPlayer } from './VideoPlayer';
|
|
10
|
+
export { SurfaceManager } from './state-based/SurfaceManager';
|
|
10
11
|
export * from './types/AudioState';
|
|
11
12
|
export { assetUrl, preloadUrl } from './utils/urls';
|
|
12
13
|
export { getStateAtTime } from './utils/getStateAtTime';
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,7 @@ export * from './CogsConnection';
|
|
|
3
3
|
export * as MediaSchema from './types/MediaSchema';
|
|
4
4
|
export { default as CogsAudioPlayer } from './AudioPlayer';
|
|
5
5
|
export { default as CogsVideoPlayer } from './VideoPlayer';
|
|
6
|
+
export { SurfaceManager } from './state-based/SurfaceManager';
|
|
6
7
|
export * from './types/AudioState';
|
|
7
8
|
export { assetUrl, preloadUrl } from './utils/urls';
|
|
8
9
|
export { getStateAtTime } from './utils/getStateAtTime';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AudioState } from '../types/MediaSchema';
|
|
2
|
+
import { ClipManager } from './ClipManager';
|
|
3
|
+
export declare class AudioManager extends ClipManager<AudioState> {
|
|
4
|
+
private audioElement?;
|
|
5
|
+
private isSeeking;
|
|
6
|
+
constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: AudioState);
|
|
7
|
+
private updateAudioElement;
|
|
8
|
+
/**
|
|
9
|
+
* Helper function to seek to a specified time.
|
|
10
|
+
* Works with the update loop to poll until seeked event has fired.
|
|
11
|
+
*/
|
|
12
|
+
private seekTo;
|
|
13
|
+
protected update(): void;
|
|
14
|
+
destroy(): void;
|
|
15
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { defaultAudioOptions } from '../types/MediaSchema';
|
|
2
|
+
import { getStateAtTime } from '../utils/getStateAtTime';
|
|
3
|
+
import { ClipManager } from './ClipManager';
|
|
4
|
+
const DEFAULT_AUDIO_POLLING = 1_000;
|
|
5
|
+
const TARGET_SYNC_THRESHOLD_MS = 10; // If we're closer than this we're good enough
|
|
6
|
+
const MAX_SYNC_THRESHOLD_MS = 1_000; // If we're further away than this, we'll seek instead
|
|
7
|
+
const SEEK_LOOKAHEAD_MS = 200; // We won't seek ahead instantly, so lets seek ahead
|
|
8
|
+
const MAX_PLAYBACK_RATE_ADJUSTMENT = 0.2;
|
|
9
|
+
// We smoothly ramp playbackRate up and down
|
|
10
|
+
const PLAYBACK_ADJUSTMENT_SMOOTHING = 0.5;
|
|
11
|
+
function playbackSmoothing(deltaTime) {
|
|
12
|
+
return Math.sign(deltaTime) * Math.pow(Math.abs(deltaTime) / MAX_SYNC_THRESHOLD_MS, PLAYBACK_ADJUSTMENT_SMOOTHING) * MAX_PLAYBACK_RATE_ADJUSTMENT;
|
|
13
|
+
}
|
|
14
|
+
export class AudioManager extends ClipManager {
|
|
15
|
+
audioElement;
|
|
16
|
+
isSeeking = false;
|
|
17
|
+
constructor(surfaceElement, clipElement, state) {
|
|
18
|
+
super(surfaceElement, clipElement, state);
|
|
19
|
+
this.clipElement = clipElement;
|
|
20
|
+
}
|
|
21
|
+
updateAudioElement() {
|
|
22
|
+
this.destroy();
|
|
23
|
+
this.audioElement = document.createElement('audio');
|
|
24
|
+
this.clipElement.replaceChildren(this.audioElement);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Helper function to seek to a specified time.
|
|
28
|
+
* Works with the update loop to poll until seeked event has fired.
|
|
29
|
+
*/
|
|
30
|
+
seekTo(time) {
|
|
31
|
+
if (!this.audioElement)
|
|
32
|
+
return;
|
|
33
|
+
this.audioElement.addEventListener('seeked', () => {
|
|
34
|
+
this.isSeeking = false;
|
|
35
|
+
}, { once: true, passive: true });
|
|
36
|
+
this.audioElement.currentTime = time / 1_000;
|
|
37
|
+
}
|
|
38
|
+
update() {
|
|
39
|
+
// Update loop used to poll until seek finished
|
|
40
|
+
if (this.isSeeking)
|
|
41
|
+
return;
|
|
42
|
+
this.delay = DEFAULT_AUDIO_POLLING;
|
|
43
|
+
// Does the <audio /> element need adding/removing?
|
|
44
|
+
const currentState = getStateAtTime(this._state, Date.now());
|
|
45
|
+
if (currentState) {
|
|
46
|
+
if (!this.audioElement || !this.isConnected(this.audioElement)) {
|
|
47
|
+
this.updateAudioElement();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
this.destroy();
|
|
52
|
+
}
|
|
53
|
+
if (!currentState || !this.audioElement)
|
|
54
|
+
return;
|
|
55
|
+
const { t, rate, volume } = { ...defaultAudioOptions, ...currentState };
|
|
56
|
+
// this.audioElement.src will be a fully qualified URL
|
|
57
|
+
if (!this.audioElement.src.endsWith(this._state.file)) {
|
|
58
|
+
this.audioElement.src = this._state.file;
|
|
59
|
+
}
|
|
60
|
+
if (this.audioElement.volume !== volume) {
|
|
61
|
+
this.audioElement.volume = volume;
|
|
62
|
+
}
|
|
63
|
+
// Should the element be playing?
|
|
64
|
+
if (this.audioElement.paused && rate > 0) {
|
|
65
|
+
this.audioElement.play().catch(() => {
|
|
66
|
+
// Do nothing - this will be retried in the next loop
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
const currentTime = this.audioElement.currentTime * 1000;
|
|
70
|
+
const deltaTime = currentTime - t;
|
|
71
|
+
const deltaTimeAbs = Math.abs(deltaTime);
|
|
72
|
+
this.delay = 100;
|
|
73
|
+
switch (true) {
|
|
74
|
+
case deltaTimeAbs <= TARGET_SYNC_THRESHOLD_MS:
|
|
75
|
+
// We are on course:
|
|
76
|
+
// - The audio is within accepted latency of the server time
|
|
77
|
+
// - The playback rate is aligned with the server rate
|
|
78
|
+
if (this.audioElement.playbackRate !== rate) {
|
|
79
|
+
this.audioElement.playbackRate = rate;
|
|
80
|
+
}
|
|
81
|
+
break;
|
|
82
|
+
case rate > 0 && deltaTimeAbs > TARGET_SYNC_THRESHOLD_MS && deltaTimeAbs <= MAX_SYNC_THRESHOLD_MS: {
|
|
83
|
+
// We are close, we can smoothly adjust with playbackRate:
|
|
84
|
+
// - The audio must be playing
|
|
85
|
+
// - We must be close in time to the server time
|
|
86
|
+
const playbackRateAdjustment = playbackSmoothing(deltaTime);
|
|
87
|
+
const adjustedPlaybackRate = Math.max(0, rate - playbackRateAdjustment);
|
|
88
|
+
if (this.audioElement.playbackRate !== adjustedPlaybackRate) {
|
|
89
|
+
this.audioElement.playbackRate = adjustedPlaybackRate;
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
default: {
|
|
94
|
+
// We cannot smoothly recover:
|
|
95
|
+
// - We seek just ahead of server time
|
|
96
|
+
if (this.audioElement.playbackRate !== rate) {
|
|
97
|
+
this.audioElement.playbackRate = rate;
|
|
98
|
+
}
|
|
99
|
+
// delay to poll until seeked
|
|
100
|
+
this.delay = 10;
|
|
101
|
+
this.seekTo(t + rate * (SEEK_LOOKAHEAD_MS / 1000));
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
destroy() {
|
|
107
|
+
if (this.audioElement) {
|
|
108
|
+
this.audioElement.src = '';
|
|
109
|
+
this.audioElement.remove();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { MediaClipState } from '../types/MediaSchema';
|
|
2
|
+
/**
|
|
3
|
+
* Each instance of a ClipManager is responsible for displaying
|
|
4
|
+
* an image/audio/video clip in the correct state.
|
|
5
|
+
*/
|
|
6
|
+
export declare abstract class ClipManager<T extends MediaClipState> {
|
|
7
|
+
private surfaceElement;
|
|
8
|
+
protected clipElement: HTMLElement;
|
|
9
|
+
constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: T);
|
|
10
|
+
/**
|
|
11
|
+
* This is the delay to be used in the update loop.
|
|
12
|
+
* It is intended to be dynamic for each loop.
|
|
13
|
+
*/
|
|
14
|
+
protected delay: number;
|
|
15
|
+
protected abstract update(): void;
|
|
16
|
+
abstract destroy(): void;
|
|
17
|
+
isConnected(element?: HTMLElement): boolean;
|
|
18
|
+
protected _state: T;
|
|
19
|
+
setState(newState: T): void;
|
|
20
|
+
private timeout;
|
|
21
|
+
private loop;
|
|
22
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const DEFAULT_DELAY = 1_000;
|
|
2
|
+
/**
|
|
3
|
+
* Each instance of a ClipManager is responsible for displaying
|
|
4
|
+
* an image/audio/video clip in the correct state.
|
|
5
|
+
*/
|
|
6
|
+
export class ClipManager {
|
|
7
|
+
surfaceElement;
|
|
8
|
+
clipElement;
|
|
9
|
+
constructor(surfaceElement, clipElement, state) {
|
|
10
|
+
this.surfaceElement = surfaceElement;
|
|
11
|
+
this.clipElement = clipElement;
|
|
12
|
+
this._state = state;
|
|
13
|
+
// Allow the class to be constructed, then call the loop
|
|
14
|
+
setTimeout(this.loop);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* This is the delay to be used in the update loop.
|
|
18
|
+
* It is intended to be dynamic for each loop.
|
|
19
|
+
*/
|
|
20
|
+
delay = DEFAULT_DELAY;
|
|
21
|
+
isConnected(element) {
|
|
22
|
+
if (!this.surfaceElement) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
if (!this.clipElement) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
if (!this.surfaceElement.contains(this.clipElement)) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
if (element) {
|
|
32
|
+
if (!this.clipElement.contains(element))
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
_state;
|
|
38
|
+
setState(newState) {
|
|
39
|
+
this._state = newState;
|
|
40
|
+
clearTimeout(this.timeout);
|
|
41
|
+
this.loop();
|
|
42
|
+
}
|
|
43
|
+
timeout;
|
|
44
|
+
loop = async () => {
|
|
45
|
+
if (this.isConnected()) {
|
|
46
|
+
this.update();
|
|
47
|
+
this.timeout = setTimeout(this.loop, this.delay);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
this.destroy();
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ImageState } from '../types/MediaSchema';
|
|
2
|
+
import { ClipManager } from './ClipManager';
|
|
3
|
+
export declare class ImageManager extends ClipManager<ImageState> {
|
|
4
|
+
private imageElement?;
|
|
5
|
+
constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: ImageState);
|
|
6
|
+
private updateImageElement;
|
|
7
|
+
protected update(): void;
|
|
8
|
+
destroy(): void;
|
|
9
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { defaultImageOptions } from '../types/MediaSchema';
|
|
2
|
+
import { getStateAtTime } from '../utils/getStateAtTime';
|
|
3
|
+
import { ClipManager } from './ClipManager';
|
|
4
|
+
export class ImageManager extends ClipManager {
|
|
5
|
+
imageElement;
|
|
6
|
+
constructor(surfaceElement, clipElement, state) {
|
|
7
|
+
super(surfaceElement, clipElement, state);
|
|
8
|
+
this.clipElement = clipElement;
|
|
9
|
+
}
|
|
10
|
+
updateImageElement() {
|
|
11
|
+
this.imageElement = document.createElement('img');
|
|
12
|
+
this.clipElement.replaceChildren(this.imageElement);
|
|
13
|
+
this.imageElement.style.position = 'absolute';
|
|
14
|
+
this.imageElement.style.height = '100%';
|
|
15
|
+
this.imageElement.style.widows = '100%';
|
|
16
|
+
}
|
|
17
|
+
update() {
|
|
18
|
+
const currentState = getStateAtTime(this._state, Date.now());
|
|
19
|
+
// Does the <img /> element need adding/removing?
|
|
20
|
+
if (currentState) {
|
|
21
|
+
if (!this.imageElement || !this.isConnected(this.imageElement)) {
|
|
22
|
+
this.updateImageElement();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
this.imageElement?.remove();
|
|
27
|
+
this.imageElement = undefined;
|
|
28
|
+
}
|
|
29
|
+
if (!this.imageElement || !currentState)
|
|
30
|
+
return;
|
|
31
|
+
// this.imageElement.src will be a fully qualified URL
|
|
32
|
+
if (!this.imageElement.src.startsWith(this._state.file)) {
|
|
33
|
+
this.imageElement.src = this._state.file;
|
|
34
|
+
}
|
|
35
|
+
if (this.imageElement.style.objectFit !== this._state.fit) {
|
|
36
|
+
this.imageElement.style.objectFit = this._state.fit;
|
|
37
|
+
}
|
|
38
|
+
if (parseFloat(this.imageElement.style.opacity) !== currentState.opacity) {
|
|
39
|
+
this.imageElement.style.opacity = String(currentState.opacity ?? defaultImageOptions.opacity);
|
|
40
|
+
}
|
|
41
|
+
const z = Math.round(currentState.zIndex ?? defaultImageOptions.zIndex);
|
|
42
|
+
if (parseInt(this.imageElement.style.zIndex) !== z) {
|
|
43
|
+
this.imageElement.style.zIndex = String(z);
|
|
44
|
+
}
|
|
45
|
+
const { opacity } = currentState;
|
|
46
|
+
if (typeof opacity === 'string' && opacity !== this.imageElement.style.opacity) {
|
|
47
|
+
this.imageElement.style.opacity = opacity;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
destroy() {
|
|
51
|
+
this.imageElement?.remove();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { MediaSurfaceState } from '../types/MediaSchema';
|
|
2
|
+
export declare const DATA_CLIP_ID = "data-clip-id";
|
|
3
|
+
/**
|
|
4
|
+
* The SurfaceManager will receive state updates and:
|
|
5
|
+
* - Ensure that each clip has a parent element
|
|
6
|
+
* - Instantiate a ClipManager attached to each respective element
|
|
7
|
+
*/
|
|
8
|
+
export declare class SurfaceManager {
|
|
9
|
+
private _state;
|
|
10
|
+
setState(newState: MediaSurfaceState): void;
|
|
11
|
+
private _element;
|
|
12
|
+
get element(): HTMLDivElement;
|
|
13
|
+
private resources;
|
|
14
|
+
constructor(testState?: MediaSurfaceState);
|
|
15
|
+
update(): void;
|
|
16
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { ImageManager } from './ImageManager';
|
|
2
|
+
import { VideoManager } from './VideoManager';
|
|
3
|
+
import { AudioManager } from './AudioManager';
|
|
4
|
+
export const DATA_CLIP_ID = 'data-clip-id';
|
|
5
|
+
/**
|
|
6
|
+
* The SurfaceManager will receive state updates and:
|
|
7
|
+
* - Ensure that each clip has a parent element
|
|
8
|
+
* - Instantiate a ClipManager attached to each respective element
|
|
9
|
+
*/
|
|
10
|
+
export class SurfaceManager {
|
|
11
|
+
_state = {};
|
|
12
|
+
setState(newState) {
|
|
13
|
+
this._state = newState;
|
|
14
|
+
this.update();
|
|
15
|
+
}
|
|
16
|
+
_element;
|
|
17
|
+
get element() {
|
|
18
|
+
return this._element;
|
|
19
|
+
}
|
|
20
|
+
resources = {};
|
|
21
|
+
constructor(testState) {
|
|
22
|
+
this._element = document.createElement('div');
|
|
23
|
+
this._element.style.width = '100%';
|
|
24
|
+
this._element.style.height = '100%';
|
|
25
|
+
this._state = testState || {};
|
|
26
|
+
this.update();
|
|
27
|
+
}
|
|
28
|
+
update() {
|
|
29
|
+
// Destroy stale managers
|
|
30
|
+
Object.entries(this.resources).forEach(([clipId, { element, manager }]) => {
|
|
31
|
+
if (!(clipId in this._state)) {
|
|
32
|
+
delete this.resources[clipId];
|
|
33
|
+
element.remove();
|
|
34
|
+
manager?.destroy();
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
// Create and attach new wrapper elements
|
|
38
|
+
const elements = Object.keys(this._state)
|
|
39
|
+
.toSorted()
|
|
40
|
+
.map((clipId) => {
|
|
41
|
+
const resource = this.resources[clipId];
|
|
42
|
+
if (resource) {
|
|
43
|
+
return resource.element;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
const element = document.createElement('div');
|
|
47
|
+
element.setAttribute(DATA_CLIP_ID, clipId);
|
|
48
|
+
this.resources[clipId] = { element };
|
|
49
|
+
return element;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
this._element.replaceChildren(...elements);
|
|
53
|
+
// Create new managers
|
|
54
|
+
Object.keys(this._state)
|
|
55
|
+
.toSorted()
|
|
56
|
+
.forEach((clipId) => {
|
|
57
|
+
const clip = this._state[clipId];
|
|
58
|
+
const resource = this.resources[clipId];
|
|
59
|
+
if (!resource) {
|
|
60
|
+
throw new Error('Failed to create resource');
|
|
61
|
+
}
|
|
62
|
+
if (!resource.manager) {
|
|
63
|
+
switch (clip.type) {
|
|
64
|
+
case 'image':
|
|
65
|
+
resource.manager = new ImageManager(this._element, resource.element, clip);
|
|
66
|
+
break;
|
|
67
|
+
case 'audio':
|
|
68
|
+
resource.manager = new AudioManager(this._element, resource.element, clip);
|
|
69
|
+
break;
|
|
70
|
+
case 'video':
|
|
71
|
+
resource.manager = new VideoManager(this._element, resource.element, clip);
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
resource.manager.setState(clip);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { VideoState } from '../types/MediaSchema';
|
|
2
|
+
import { ClipManager } from './ClipManager';
|
|
3
|
+
export declare class VideoManager extends ClipManager<VideoState> {
|
|
4
|
+
private videoElement?;
|
|
5
|
+
private isSeeking;
|
|
6
|
+
constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: VideoState);
|
|
7
|
+
private updateVideoElement;
|
|
8
|
+
/**
|
|
9
|
+
* Helper function to seek to a specified time.
|
|
10
|
+
* Works with the update loop to poll until seeked event has fired.
|
|
11
|
+
*/
|
|
12
|
+
private seekTo;
|
|
13
|
+
protected update(): void;
|
|
14
|
+
destroy(): void;
|
|
15
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { defaultVideoOptions } from '../types/MediaSchema';
|
|
2
|
+
import { getStateAtTime } from '../utils/getStateAtTime';
|
|
3
|
+
import { ClipManager } from './ClipManager';
|
|
4
|
+
const DEFAULT_VIDEO_POLLING = 1_000;
|
|
5
|
+
const TARGET_SYNC_THRESHOLD_MS = 10; // If we're closer than this we're good enough
|
|
6
|
+
const MAX_SYNC_THRESHOLD_MS = 1_000; // If we're further away than this, we'll seek instead
|
|
7
|
+
const SEEK_LOOKAHEAD_MS = 200; // We won't seek ahead instantly, so lets seek ahead
|
|
8
|
+
const MAX_PLAYBACK_RATE_ADJUSTMENT = 0.5;
|
|
9
|
+
// We smoothly ramp playbackRate up and down
|
|
10
|
+
const PLAYBACK_ADJUSTMENT_SMOOTHING = 0.3;
|
|
11
|
+
function playbackSmoothing(deltaTime) {
|
|
12
|
+
return Math.sign(deltaTime) * Math.pow(Math.abs(deltaTime) / MAX_SYNC_THRESHOLD_MS, PLAYBACK_ADJUSTMENT_SMOOTHING) * MAX_PLAYBACK_RATE_ADJUSTMENT;
|
|
13
|
+
}
|
|
14
|
+
export class VideoManager extends ClipManager {
|
|
15
|
+
videoElement;
|
|
16
|
+
isSeeking = false;
|
|
17
|
+
constructor(surfaceElement, clipElement, state) {
|
|
18
|
+
super(surfaceElement, clipElement, state);
|
|
19
|
+
this.clipElement = clipElement;
|
|
20
|
+
}
|
|
21
|
+
updateVideoElement() {
|
|
22
|
+
this.destroy();
|
|
23
|
+
this.videoElement = document.createElement('video');
|
|
24
|
+
this.clipElement.replaceChildren(this.videoElement);
|
|
25
|
+
this.videoElement.style.position = 'absolute';
|
|
26
|
+
this.videoElement.style.width = '100%';
|
|
27
|
+
this.videoElement.style.height = '100%';
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Helper function to seek to a specified time.
|
|
31
|
+
* Works with the update loop to poll until seeked event has fired.
|
|
32
|
+
*/
|
|
33
|
+
seekTo(time) {
|
|
34
|
+
if (!this.videoElement)
|
|
35
|
+
return;
|
|
36
|
+
this.videoElement.addEventListener('seeked', () => {
|
|
37
|
+
this.isSeeking = false;
|
|
38
|
+
}, { once: true, passive: true });
|
|
39
|
+
this.videoElement.currentTime = time / 1_000;
|
|
40
|
+
}
|
|
41
|
+
update() {
|
|
42
|
+
// Update loop used to poll until seek finished
|
|
43
|
+
if (this.isSeeking)
|
|
44
|
+
return;
|
|
45
|
+
this.delay = DEFAULT_VIDEO_POLLING;
|
|
46
|
+
// Does the <video /> element need adding/removing?
|
|
47
|
+
const currentState = getStateAtTime(this._state, Date.now());
|
|
48
|
+
if (currentState) {
|
|
49
|
+
if (!this.videoElement || !this.isConnected(this.videoElement)) {
|
|
50
|
+
this.updateVideoElement();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
this.videoElement?.remove();
|
|
55
|
+
this.videoElement = undefined;
|
|
56
|
+
}
|
|
57
|
+
if (!currentState || !this.videoElement)
|
|
58
|
+
return;
|
|
59
|
+
const { t, rate, volume } = { ...defaultVideoOptions, ...currentState };
|
|
60
|
+
// this.videoElement.src will be a fully qualified URL
|
|
61
|
+
if (!this.videoElement.src.endsWith(this._state.file)) {
|
|
62
|
+
this.videoElement.src = this._state.file;
|
|
63
|
+
}
|
|
64
|
+
if (this.videoElement.style.objectFit !== this._state.fit) {
|
|
65
|
+
this.videoElement.style.objectFit = this._state.fit;
|
|
66
|
+
}
|
|
67
|
+
if (parseFloat(this.videoElement.style.opacity) !== currentState.opacity) {
|
|
68
|
+
this.videoElement.style.opacity = String(currentState.opacity ?? defaultVideoOptions.opacity);
|
|
69
|
+
}
|
|
70
|
+
const z = Math.round(currentState.zIndex ?? defaultVideoOptions.zIndex);
|
|
71
|
+
if (parseInt(this.videoElement.style.zIndex) !== z) {
|
|
72
|
+
this.videoElement.style.zIndex = String(z);
|
|
73
|
+
}
|
|
74
|
+
if (this.videoElement.volume !== volume) {
|
|
75
|
+
this.videoElement.volume = volume;
|
|
76
|
+
}
|
|
77
|
+
// Should the element be playing?
|
|
78
|
+
if (this.videoElement.paused && rate > 0) {
|
|
79
|
+
this.videoElement.play().catch(() => {
|
|
80
|
+
// Do nothing - this will be retried in the next loop
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
const currentTime = this.videoElement.currentTime * 1000;
|
|
84
|
+
const deltaTime = currentTime - t;
|
|
85
|
+
const deltaTimeAbs = Math.abs(deltaTime);
|
|
86
|
+
this.delay = 100;
|
|
87
|
+
switch (true) {
|
|
88
|
+
case deltaTimeAbs <= TARGET_SYNC_THRESHOLD_MS:
|
|
89
|
+
// We are on course:
|
|
90
|
+
// - The video is within accepted latency of the server time
|
|
91
|
+
// - The playback rate is aligned with the server rate
|
|
92
|
+
if (this.videoElement.playbackRate !== rate) {
|
|
93
|
+
this.videoElement.playbackRate = rate;
|
|
94
|
+
}
|
|
95
|
+
break;
|
|
96
|
+
case rate > 0 && deltaTimeAbs > TARGET_SYNC_THRESHOLD_MS && deltaTimeAbs <= MAX_SYNC_THRESHOLD_MS: {
|
|
97
|
+
// We are close, we can smoothly adjust with playbackRate:
|
|
98
|
+
// - The video must be playing
|
|
99
|
+
// - We must be close in time to the server time
|
|
100
|
+
const playbackRateAdjustment = playbackSmoothing(deltaTime);
|
|
101
|
+
const adjustedPlaybackRate = Math.max(0, rate - playbackRateAdjustment);
|
|
102
|
+
if (this.videoElement.playbackRate !== adjustedPlaybackRate) {
|
|
103
|
+
this.videoElement.playbackRate = adjustedPlaybackRate;
|
|
104
|
+
}
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
default: {
|
|
108
|
+
// We cannot smoothly recover:
|
|
109
|
+
// - We seek just ahead of server time
|
|
110
|
+
if (this.videoElement.playbackRate !== rate) {
|
|
111
|
+
this.videoElement.playbackRate = rate;
|
|
112
|
+
}
|
|
113
|
+
// delay to poll until seeked
|
|
114
|
+
this.delay = 10;
|
|
115
|
+
this.seekTo(t + rate * (SEEK_LOOKAHEAD_MS / 1000));
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
destroy() {
|
|
121
|
+
if (this.videoElement) {
|
|
122
|
+
this.videoElement.src = '';
|
|
123
|
+
this.videoElement.remove();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -7,6 +7,7 @@ declare const TemporalProperties: z.ZodObject<{
|
|
|
7
7
|
export type VisualProperties = z.infer<typeof VisualProperties>;
|
|
8
8
|
declare const VisualProperties: z.ZodObject<{
|
|
9
9
|
opacity: z.ZodNumber;
|
|
10
|
+
zIndex: z.ZodNumber;
|
|
10
11
|
}, z.core.$strip>;
|
|
11
12
|
export type AudialProperties = z.infer<typeof AudialProperties>;
|
|
12
13
|
declare const AudialProperties: z.ZodObject<{
|
|
@@ -40,6 +41,7 @@ export type InitialImageKeyframe = z.infer<typeof InitialImageKeyframe>;
|
|
|
40
41
|
declare const InitialImageKeyframe: z.ZodTuple<[z.ZodNumber, z.ZodObject<{
|
|
41
42
|
set: z.ZodOptional<z.ZodObject<{
|
|
42
43
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
44
|
+
zIndex: z.ZodOptional<z.ZodNumber>;
|
|
43
45
|
}, z.core.$strip>>;
|
|
44
46
|
}, z.core.$strip>], null>;
|
|
45
47
|
/**
|
|
@@ -49,9 +51,11 @@ export type ImageKeyframe = z.infer<typeof ImageKeyframe>;
|
|
|
49
51
|
declare const ImageKeyframe: z.ZodTuple<[z.ZodNumber, z.ZodObject<{
|
|
50
52
|
set: z.ZodOptional<z.ZodObject<{
|
|
51
53
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
54
|
+
zIndex: z.ZodOptional<z.ZodNumber>;
|
|
52
55
|
}, z.core.$strip>>;
|
|
53
56
|
lerp: z.ZodOptional<z.ZodObject<{
|
|
54
57
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
58
|
+
zIndex: z.ZodOptional<z.ZodNumber>;
|
|
55
59
|
}, z.core.$strip>>;
|
|
56
60
|
}, z.core.$strip>], null>;
|
|
57
61
|
/**
|
|
@@ -86,6 +90,7 @@ export type InitialVideoKeyframe = z.infer<typeof InitialVideoKeyframe>;
|
|
|
86
90
|
declare const InitialVideoKeyframe: z.ZodTuple<[z.ZodNumber, z.ZodObject<{
|
|
87
91
|
set: z.ZodOptional<z.ZodObject<{
|
|
88
92
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
93
|
+
zIndex: z.ZodOptional<z.ZodNumber>;
|
|
89
94
|
volume: z.ZodOptional<z.ZodNumber>;
|
|
90
95
|
t: z.ZodOptional<z.ZodNumber>;
|
|
91
96
|
rate: z.ZodOptional<z.ZodNumber>;
|
|
@@ -98,12 +103,14 @@ export type VideoKeyframe = z.infer<typeof VideoKeyframe>;
|
|
|
98
103
|
declare const VideoKeyframe: z.ZodTuple<[z.ZodNumber, z.ZodObject<{
|
|
99
104
|
set: z.ZodOptional<z.ZodObject<{
|
|
100
105
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
106
|
+
zIndex: z.ZodOptional<z.ZodNumber>;
|
|
101
107
|
volume: z.ZodOptional<z.ZodNumber>;
|
|
102
108
|
t: z.ZodOptional<z.ZodNumber>;
|
|
103
109
|
rate: z.ZodOptional<z.ZodNumber>;
|
|
104
110
|
}, z.core.$strip>>;
|
|
105
111
|
lerp: z.ZodOptional<z.ZodObject<{
|
|
106
112
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
113
|
+
zIndex: z.ZodOptional<z.ZodNumber>;
|
|
107
114
|
volume: z.ZodOptional<z.ZodNumber>;
|
|
108
115
|
}, z.core.$strip>>;
|
|
109
116
|
}, z.core.$strip>], null>;
|
|
@@ -111,13 +118,16 @@ export declare const MediaSurfaceStateSchema: z.ZodRecord<z.ZodString, z.ZodUnio
|
|
|
111
118
|
keyframes: z.ZodTuple<[z.ZodTuple<[z.ZodNumber, z.ZodObject<{
|
|
112
119
|
set: z.ZodOptional<z.ZodObject<{
|
|
113
120
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
121
|
+
zIndex: z.ZodOptional<z.ZodNumber>;
|
|
114
122
|
}, z.core.$strip>>;
|
|
115
123
|
lerp: z.ZodOptional<z.ZodObject<{
|
|
116
124
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
125
|
+
zIndex: z.ZodOptional<z.ZodNumber>;
|
|
117
126
|
}, z.core.$strip>>;
|
|
118
127
|
}, z.core.$strip>], null>], z.ZodUnion<readonly [z.ZodTuple<[z.ZodNumber, z.ZodObject<{
|
|
119
128
|
set: z.ZodOptional<z.ZodObject<{
|
|
120
129
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
130
|
+
zIndex: z.ZodOptional<z.ZodNumber>;
|
|
121
131
|
}, z.core.$strip>>;
|
|
122
132
|
}, z.core.$strip>], null>, z.ZodTuple<[z.ZodNumber, z.ZodNull], null>]>>;
|
|
123
133
|
type: z.ZodLiteral<"image">;
|
|
@@ -147,17 +157,20 @@ export declare const MediaSurfaceStateSchema: z.ZodRecord<z.ZodString, z.ZodUnio
|
|
|
147
157
|
keyframes: z.ZodTuple<[z.ZodTuple<[z.ZodNumber, z.ZodObject<{
|
|
148
158
|
set: z.ZodOptional<z.ZodObject<{
|
|
149
159
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
160
|
+
zIndex: z.ZodOptional<z.ZodNumber>;
|
|
150
161
|
volume: z.ZodOptional<z.ZodNumber>;
|
|
151
162
|
t: z.ZodOptional<z.ZodNumber>;
|
|
152
163
|
rate: z.ZodOptional<z.ZodNumber>;
|
|
153
164
|
}, z.core.$strip>>;
|
|
154
165
|
lerp: z.ZodOptional<z.ZodObject<{
|
|
155
166
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
167
|
+
zIndex: z.ZodOptional<z.ZodNumber>;
|
|
156
168
|
volume: z.ZodOptional<z.ZodNumber>;
|
|
157
169
|
}, z.core.$strip>>;
|
|
158
170
|
}, z.core.$strip>], null>], z.ZodUnion<readonly [z.ZodTuple<[z.ZodNumber, z.ZodObject<{
|
|
159
171
|
set: z.ZodOptional<z.ZodObject<{
|
|
160
172
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
173
|
+
zIndex: z.ZodOptional<z.ZodNumber>;
|
|
161
174
|
volume: z.ZodOptional<z.ZodNumber>;
|
|
162
175
|
t: z.ZodOptional<z.ZodNumber>;
|
|
163
176
|
rate: z.ZodOptional<z.ZodNumber>;
|
|
@@ -5,6 +5,7 @@ const TemporalProperties = z.object({
|
|
|
5
5
|
});
|
|
6
6
|
const VisualProperties = z.object({
|
|
7
7
|
opacity: z.number().gte(0).lte(1),
|
|
8
|
+
zIndex: z.number(),
|
|
8
9
|
});
|
|
9
10
|
const AudialProperties = z.object({
|
|
10
11
|
volume: z.number().gte(0).lte(1),
|
|
@@ -139,6 +140,7 @@ true;
|
|
|
139
140
|
true;
|
|
140
141
|
export const defaultImageOptions = {
|
|
141
142
|
opacity: 1,
|
|
143
|
+
zIndex: 0,
|
|
142
144
|
};
|
|
143
145
|
export const defaultAudioOptions = {
|
|
144
146
|
t: 0,
|
|
@@ -150,4 +152,5 @@ export const defaultVideoOptions = {
|
|
|
150
152
|
rate: 1,
|
|
151
153
|
volume: 1,
|
|
152
154
|
opacity: 1,
|
|
155
|
+
zIndex: 0,
|
|
153
156
|
};
|
|
@@ -47,11 +47,11 @@ export function getPropertiesAtTime(keyframes, time) {
|
|
|
47
47
|
// If lerp and set are both present, we assume we lerp up until the timestamp,
|
|
48
48
|
// then set to a new value
|
|
49
49
|
Object.entries(properties.lerp ?? {}).forEach(([property, value]) => {
|
|
50
|
-
propertyKeyframes[property]
|
|
50
|
+
propertyKeyframes[property] ??= {};
|
|
51
51
|
propertyKeyframes[property].before = [timestamp, value];
|
|
52
52
|
});
|
|
53
53
|
Object.entries(properties.set ?? {}).forEach(([property, value]) => {
|
|
54
|
-
propertyKeyframes[property]
|
|
54
|
+
propertyKeyframes[property] ??= {};
|
|
55
55
|
propertyKeyframes[property].before = [timestamp, value];
|
|
56
56
|
});
|
|
57
57
|
}
|
|
@@ -59,7 +59,7 @@ export function getPropertiesAtTime(keyframes, time) {
|
|
|
59
59
|
// We're trying to find the closest timestamp afterwards for lerping
|
|
60
60
|
// So only set if not yet set
|
|
61
61
|
Object.entries(properties.lerp ?? {}).forEach(([property, value]) => {
|
|
62
|
-
propertyKeyframes[property]
|
|
62
|
+
propertyKeyframes[property] ??= {};
|
|
63
63
|
if (propertyKeyframes[property].after === undefined) {
|
|
64
64
|
propertyKeyframes[property].after = [timestamp, value];
|
|
65
65
|
}
|