@clockworkdog/cogs-client 3.0.0-alpha.7 → 3.0.0-alpha.9
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/browser/index.mjs +1157 -1195
- package/dist/browser/index.umd.js +5 -5
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/state-based/AudioManager.d.ts +1 -1
- package/dist/state-based/AudioManager.js +12 -9
- package/dist/state-based/ClipManager.d.ts +1 -2
- package/dist/state-based/ClipManager.js +2 -6
- package/dist/state-based/ImageManager.d.ts +1 -1
- package/dist/state-based/ImageManager.js +9 -10
- package/dist/state-based/SurfaceManager.d.ts +1 -2
- package/dist/state-based/SurfaceManager.js +4 -7
- package/dist/state-based/VideoManager.d.ts +1 -3
- package/dist/state-based/VideoManager.js +29 -113
- package/dist/types/MediaSchema.d.ts +13 -13
- package/dist/types/MediaSchema.js +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,6 @@ export type { default as MediaClipStateMessage } from './types/MediaClipStateMes
|
|
|
5
5
|
export type { default as ShowPhase } from './types/ShowPhase';
|
|
6
6
|
export type { default as MediaObjectFit } from './types/MediaObjectFit';
|
|
7
7
|
export * as MediaSchema from './types/MediaSchema';
|
|
8
|
-
export * from './state-based/SurfaceManager';
|
|
9
8
|
export { default as CogsAudioPlayer } from './AudioPlayer';
|
|
10
9
|
export { default as CogsVideoPlayer } from './VideoPlayer';
|
|
11
10
|
export { SurfaceManager } from './state-based/SurfaceManager';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
export { default as CogsConnection } from './CogsConnection';
|
|
2
2
|
export * from './CogsConnection';
|
|
3
3
|
export * as MediaSchema from './types/MediaSchema';
|
|
4
|
-
export * from './state-based/SurfaceManager';
|
|
5
4
|
export { default as CogsAudioPlayer } from './AudioPlayer';
|
|
6
5
|
export { default as CogsVideoPlayer } from './VideoPlayer';
|
|
7
6
|
export { SurfaceManager } from './state-based/SurfaceManager';
|
|
@@ -3,7 +3,7 @@ import { ClipManager } from './ClipManager';
|
|
|
3
3
|
export declare class AudioManager extends ClipManager<AudioState> {
|
|
4
4
|
private audioElement?;
|
|
5
5
|
private isSeeking;
|
|
6
|
-
constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: AudioState
|
|
6
|
+
constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: AudioState);
|
|
7
7
|
private updateAudioElement;
|
|
8
8
|
/**
|
|
9
9
|
* Helper function to seek to a specified time.
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { defaultAudioOptions } from '../types/MediaSchema';
|
|
2
2
|
import { getStateAtTime } from '../utils/getStateAtTime';
|
|
3
3
|
import { ClipManager } from './ClipManager';
|
|
4
|
-
const
|
|
4
|
+
const NO_AUDIO_POLLING = 1_000;
|
|
5
|
+
const AUDIO_PLAYBACK_POLLING = 100;
|
|
6
|
+
const SEEKING_POLLING = 10;
|
|
5
7
|
const TARGET_SYNC_THRESHOLD_MS = 10; // If we're closer than this we're good enough
|
|
6
8
|
const MAX_SYNC_THRESHOLD_MS = 1_000; // If we're further away than this, we'll seek instead
|
|
7
9
|
const SEEK_LOOKAHEAD_MS = 200; // We won't seek ahead instantly, so lets seek ahead
|
|
@@ -14,8 +16,8 @@ function playbackSmoothing(deltaTime) {
|
|
|
14
16
|
export class AudioManager extends ClipManager {
|
|
15
17
|
audioElement;
|
|
16
18
|
isSeeking = false;
|
|
17
|
-
constructor(surfaceElement, clipElement, state
|
|
18
|
-
super(surfaceElement, clipElement, state
|
|
19
|
+
constructor(surfaceElement, clipElement, state) {
|
|
20
|
+
super(surfaceElement, clipElement, state);
|
|
19
21
|
this.clipElement = clipElement;
|
|
20
22
|
}
|
|
21
23
|
updateAudioElement() {
|
|
@@ -30,6 +32,8 @@ export class AudioManager extends ClipManager {
|
|
|
30
32
|
seekTo(time) {
|
|
31
33
|
if (!this.audioElement)
|
|
32
34
|
return;
|
|
35
|
+
this.delay = SEEKING_POLLING;
|
|
36
|
+
this.isSeeking = true;
|
|
33
37
|
this.audioElement.addEventListener('seeked', () => {
|
|
34
38
|
this.isSeeking = false;
|
|
35
39
|
}, { once: true, passive: true });
|
|
@@ -39,7 +43,7 @@ export class AudioManager extends ClipManager {
|
|
|
39
43
|
// Update loop used to poll until seek finished
|
|
40
44
|
if (this.isSeeking)
|
|
41
45
|
return;
|
|
42
|
-
this.delay =
|
|
46
|
+
this.delay = NO_AUDIO_POLLING;
|
|
43
47
|
// Does the <audio /> element need adding/removing?
|
|
44
48
|
const currentState = getStateAtTime(this._state, Date.now());
|
|
45
49
|
if (currentState) {
|
|
@@ -53,10 +57,9 @@ export class AudioManager extends ClipManager {
|
|
|
53
57
|
if (!currentState || !this.audioElement)
|
|
54
58
|
return;
|
|
55
59
|
const { t, rate, volume } = { ...defaultAudioOptions, ...currentState };
|
|
56
|
-
// this.
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
this.audioElement.src = assetURL;
|
|
60
|
+
// this.audioElement.src will be a fully qualified URL
|
|
61
|
+
if (!this.audioElement.src.endsWith(this._state.file)) {
|
|
62
|
+
this.audioElement.src = this._state.file;
|
|
60
63
|
}
|
|
61
64
|
if (this.audioElement.volume !== volume) {
|
|
62
65
|
this.audioElement.volume = volume;
|
|
@@ -70,7 +73,7 @@ export class AudioManager extends ClipManager {
|
|
|
70
73
|
const currentTime = this.audioElement.currentTime * 1000;
|
|
71
74
|
const deltaTime = currentTime - t;
|
|
72
75
|
const deltaTimeAbs = Math.abs(deltaTime);
|
|
73
|
-
this.delay =
|
|
76
|
+
this.delay = AUDIO_PLAYBACK_POLLING;
|
|
74
77
|
switch (true) {
|
|
75
78
|
case deltaTimeAbs <= TARGET_SYNC_THRESHOLD_MS:
|
|
76
79
|
// We are on course:
|
|
@@ -6,8 +6,7 @@ import { MediaClipState } from '../types/MediaSchema';
|
|
|
6
6
|
export declare abstract class ClipManager<T extends MediaClipState> {
|
|
7
7
|
private surfaceElement;
|
|
8
8
|
protected clipElement: HTMLElement;
|
|
9
|
-
|
|
10
|
-
constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: T, constructAssetURL: (file: string) => string);
|
|
9
|
+
constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: T);
|
|
11
10
|
/**
|
|
12
11
|
* This is the delay to be used in the update loop.
|
|
13
12
|
* It is intended to be dynamic for each loop.
|
|
@@ -6,11 +6,9 @@ const DEFAULT_DELAY = 1_000;
|
|
|
6
6
|
export class ClipManager {
|
|
7
7
|
surfaceElement;
|
|
8
8
|
clipElement;
|
|
9
|
-
|
|
10
|
-
constructor(surfaceElement, clipElement, state, constructAssetURL) {
|
|
9
|
+
constructor(surfaceElement, clipElement, state) {
|
|
11
10
|
this.surfaceElement = surfaceElement;
|
|
12
11
|
this.clipElement = clipElement;
|
|
13
|
-
this.constructAssetURL = constructAssetURL;
|
|
14
12
|
this._state = state;
|
|
15
13
|
// Allow the class to be constructed, then call the loop
|
|
16
14
|
setTimeout(this.loop);
|
|
@@ -46,9 +44,7 @@ export class ClipManager {
|
|
|
46
44
|
loop = async () => {
|
|
47
45
|
if (this.isConnected()) {
|
|
48
46
|
this.update();
|
|
49
|
-
|
|
50
|
-
this.timeout = setTimeout(this.loop, this.delay);
|
|
51
|
-
}
|
|
47
|
+
this.timeout = setTimeout(this.loop, this.delay);
|
|
52
48
|
}
|
|
53
49
|
else {
|
|
54
50
|
this.destroy();
|
|
@@ -2,7 +2,7 @@ import { ImageState } from '../types/MediaSchema';
|
|
|
2
2
|
import { ClipManager } from './ClipManager';
|
|
3
3
|
export declare class ImageManager extends ClipManager<ImageState> {
|
|
4
4
|
private imageElement?;
|
|
5
|
-
constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: ImageState
|
|
5
|
+
constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: ImageState);
|
|
6
6
|
private updateImageElement;
|
|
7
7
|
protected update(): void;
|
|
8
8
|
destroy(): void;
|
|
@@ -3,8 +3,8 @@ import { getStateAtTime } from '../utils/getStateAtTime';
|
|
|
3
3
|
import { ClipManager } from './ClipManager';
|
|
4
4
|
export class ImageManager extends ClipManager {
|
|
5
5
|
imageElement;
|
|
6
|
-
constructor(surfaceElement, clipElement, state
|
|
7
|
-
super(surfaceElement, clipElement, state
|
|
6
|
+
constructor(surfaceElement, clipElement, state) {
|
|
7
|
+
super(surfaceElement, clipElement, state);
|
|
8
8
|
this.clipElement = clipElement;
|
|
9
9
|
}
|
|
10
10
|
updateImageElement() {
|
|
@@ -12,7 +12,7 @@ export class ImageManager extends ClipManager {
|
|
|
12
12
|
this.clipElement.replaceChildren(this.imageElement);
|
|
13
13
|
this.imageElement.style.position = 'absolute';
|
|
14
14
|
this.imageElement.style.height = '100%';
|
|
15
|
-
this.imageElement.style.
|
|
15
|
+
this.imageElement.style.width = '100%';
|
|
16
16
|
}
|
|
17
17
|
update() {
|
|
18
18
|
const currentState = getStateAtTime(this._state, Date.now());
|
|
@@ -28,10 +28,9 @@ export class ImageManager extends ClipManager {
|
|
|
28
28
|
}
|
|
29
29
|
if (!this.imageElement || !currentState)
|
|
30
30
|
return;
|
|
31
|
-
// this.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
this.imageElement.src = assetURL;
|
|
31
|
+
// this.imageElement.src will be a fully qualified URL
|
|
32
|
+
if (!this.imageElement.src.endsWith(this._state.file)) {
|
|
33
|
+
this.imageElement.src = this._state.file;
|
|
35
34
|
}
|
|
36
35
|
if (this.imageElement.style.objectFit !== this._state.fit) {
|
|
37
36
|
this.imageElement.style.objectFit = this._state.fit;
|
|
@@ -43,9 +42,9 @@ export class ImageManager extends ClipManager {
|
|
|
43
42
|
if (parseInt(this.imageElement.style.zIndex) !== z) {
|
|
44
43
|
this.imageElement.style.zIndex = String(z);
|
|
45
44
|
}
|
|
46
|
-
const
|
|
47
|
-
if (
|
|
48
|
-
this.imageElement.style.opacity =
|
|
45
|
+
const opacityString = String(currentState.opacity);
|
|
46
|
+
if (this.imageElement.style.opacity !== opacityString) {
|
|
47
|
+
this.imageElement.style.opacity = opacityString;
|
|
49
48
|
}
|
|
50
49
|
}
|
|
51
50
|
destroy() {
|
|
@@ -6,12 +6,11 @@ export declare const DATA_CLIP_ID = "data-clip-id";
|
|
|
6
6
|
* - Instantiate a ClipManager attached to each respective element
|
|
7
7
|
*/
|
|
8
8
|
export declare class SurfaceManager {
|
|
9
|
-
private constructAssetUrl;
|
|
10
9
|
private _state;
|
|
11
10
|
setState(newState: MediaSurfaceState): void;
|
|
12
11
|
private _element;
|
|
13
12
|
get element(): HTMLDivElement;
|
|
14
13
|
private resources;
|
|
15
|
-
constructor(
|
|
14
|
+
constructor(testState?: MediaSurfaceState);
|
|
16
15
|
update(): void;
|
|
17
16
|
}
|
|
@@ -8,7 +8,6 @@ export const DATA_CLIP_ID = 'data-clip-id';
|
|
|
8
8
|
* - Instantiate a ClipManager attached to each respective element
|
|
9
9
|
*/
|
|
10
10
|
export class SurfaceManager {
|
|
11
|
-
constructAssetUrl;
|
|
12
11
|
_state = {};
|
|
13
12
|
setState(newState) {
|
|
14
13
|
this._state = newState;
|
|
@@ -19,10 +18,8 @@ export class SurfaceManager {
|
|
|
19
18
|
return this._element;
|
|
20
19
|
}
|
|
21
20
|
resources = {};
|
|
22
|
-
constructor(
|
|
23
|
-
this.constructAssetUrl = constructAssetUrl;
|
|
21
|
+
constructor(testState) {
|
|
24
22
|
this._element = document.createElement('div');
|
|
25
|
-
this._element.className = 'surface-manager';
|
|
26
23
|
this._element.style.width = '100%';
|
|
27
24
|
this._element.style.height = '100%';
|
|
28
25
|
this._state = testState || {};
|
|
@@ -65,13 +62,13 @@ export class SurfaceManager {
|
|
|
65
62
|
if (!resource.manager) {
|
|
66
63
|
switch (clip.type) {
|
|
67
64
|
case 'image':
|
|
68
|
-
resource.manager = new ImageManager(this._element, resource.element, clip
|
|
65
|
+
resource.manager = new ImageManager(this._element, resource.element, clip);
|
|
69
66
|
break;
|
|
70
67
|
case 'audio':
|
|
71
|
-
resource.manager = new AudioManager(this._element, resource.element, clip
|
|
68
|
+
resource.manager = new AudioManager(this._element, resource.element, clip);
|
|
72
69
|
break;
|
|
73
70
|
case 'video':
|
|
74
|
-
resource.manager = new VideoManager(this._element, resource.element, clip
|
|
71
|
+
resource.manager = new VideoManager(this._element, resource.element, clip);
|
|
75
72
|
break;
|
|
76
73
|
}
|
|
77
74
|
}
|
|
@@ -3,10 +3,8 @@ import { ClipManager } from './ClipManager';
|
|
|
3
3
|
export declare class VideoManager extends ClipManager<VideoState> {
|
|
4
4
|
private videoElement?;
|
|
5
5
|
private isSeeking;
|
|
6
|
-
|
|
7
|
-
constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: VideoState, constructAssetURL: (file: string) => string);
|
|
6
|
+
constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: VideoState);
|
|
8
7
|
private updateVideoElement;
|
|
9
|
-
private get videoDuration();
|
|
10
8
|
/**
|
|
11
9
|
* Helper function to seek to a specified time.
|
|
12
10
|
* Works with the update loop to poll until seeked event has fired.
|
|
@@ -1,41 +1,23 @@
|
|
|
1
1
|
import { defaultVideoOptions } from '../types/MediaSchema';
|
|
2
2
|
import { getStateAtTime } from '../utils/getStateAtTime';
|
|
3
3
|
import { ClipManager } from './ClipManager';
|
|
4
|
-
const
|
|
4
|
+
const NO_VIDEO_POLLING = 1_000;
|
|
5
|
+
const VIDEO_PLAYBACK_POLLING = 100;
|
|
6
|
+
const SEEKING_POLLING = 10;
|
|
5
7
|
const TARGET_SYNC_THRESHOLD_MS = 10; // If we're closer than this we're good enough
|
|
6
8
|
const MAX_SYNC_THRESHOLD_MS = 1_000; // If we're further away than this, we'll seek instead
|
|
7
9
|
const SEEK_LOOKAHEAD_MS = 200; // We won't seek ahead instantly, so lets seek ahead
|
|
8
|
-
const MAX_PLAYBACK_RATE_ADJUSTMENT = 0.
|
|
9
|
-
const INTERCEPTION_EARLY_CHECK_IN = 0.7; // When on course for interception of server time, how early to check in beforehand.
|
|
10
|
+
const MAX_PLAYBACK_RATE_ADJUSTMENT = 0.5;
|
|
10
11
|
// We smoothly ramp playbackRate up and down
|
|
11
12
|
const PLAYBACK_ADJUSTMENT_SMOOTHING = 0.3;
|
|
12
13
|
function playbackSmoothing(deltaTime) {
|
|
13
|
-
return
|
|
14
|
-
}
|
|
15
|
-
// If we notice that at the end of the current playback, we set t=0 we should loop
|
|
16
|
-
const LOOPING_EPSILON_MS = 5;
|
|
17
|
-
function isLooping(state, time, duration) {
|
|
18
|
-
const currentState = getStateAtTime(state, time);
|
|
19
|
-
if (!currentState)
|
|
20
|
-
return false;
|
|
21
|
-
const { t, rate } = currentState;
|
|
22
|
-
if (t === undefined || rate === undefined)
|
|
23
|
-
return false;
|
|
24
|
-
const nextTemporalKeyframe = state.keyframes.filter(([t, kf]) => t > time && (kf?.set?.t !== undefined || kf?.set?.rate !== undefined))[0];
|
|
25
|
-
if (nextTemporalKeyframe?.[1]?.set?.t !== 0)
|
|
26
|
-
return false;
|
|
27
|
-
const timeRemaining = (duration - t) / rate;
|
|
28
|
-
const timeUntilKeyframe = nextTemporalKeyframe[0] - time;
|
|
29
|
-
return Math.abs(timeRemaining - timeUntilKeyframe) <= LOOPING_EPSILON_MS;
|
|
14
|
+
return Math.sign(deltaTime) * Math.pow(Math.abs(deltaTime) / MAX_SYNC_THRESHOLD_MS, PLAYBACK_ADJUSTMENT_SMOOTHING) * MAX_PLAYBACK_RATE_ADJUSTMENT;
|
|
30
15
|
}
|
|
31
16
|
export class VideoManager extends ClipManager {
|
|
32
17
|
videoElement;
|
|
33
|
-
// We seek to another part of the video and do nothing until we get there
|
|
34
18
|
isSeeking = false;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
constructor(surfaceElement, clipElement, state, constructAssetURL) {
|
|
38
|
-
super(surfaceElement, clipElement, state, constructAssetURL);
|
|
19
|
+
constructor(surfaceElement, clipElement, state) {
|
|
20
|
+
super(surfaceElement, clipElement, state);
|
|
39
21
|
this.clipElement = clipElement;
|
|
40
22
|
}
|
|
41
23
|
updateVideoElement() {
|
|
@@ -46,33 +28,27 @@ export class VideoManager extends ClipManager {
|
|
|
46
28
|
this.videoElement.style.width = '100%';
|
|
47
29
|
this.videoElement.style.height = '100%';
|
|
48
30
|
}
|
|
49
|
-
get videoDuration() {
|
|
50
|
-
if (!this.videoElement)
|
|
51
|
-
return undefined;
|
|
52
|
-
if (this.videoElement.readyState < HTMLMediaElement.HAVE_METADATA)
|
|
53
|
-
return undefined;
|
|
54
|
-
return this.videoElement.duration * 1000;
|
|
55
|
-
}
|
|
56
31
|
/**
|
|
57
32
|
* Helper function to seek to a specified time.
|
|
58
33
|
* Works with the update loop to poll until seeked event has fired.
|
|
59
34
|
*/
|
|
60
|
-
seekTo(
|
|
35
|
+
seekTo(ms) {
|
|
61
36
|
if (!this.videoElement)
|
|
62
37
|
return;
|
|
38
|
+
this.delay = SEEKING_POLLING;
|
|
39
|
+
this.isSeeking = true;
|
|
63
40
|
this.videoElement.addEventListener('seeked', () => {
|
|
64
|
-
console.debug('seeked');
|
|
65
41
|
this.isSeeking = false;
|
|
66
42
|
}, { once: true, passive: true });
|
|
67
|
-
this.videoElement.currentTime =
|
|
43
|
+
this.videoElement.currentTime = ms / 1_000;
|
|
68
44
|
}
|
|
69
45
|
update() {
|
|
70
46
|
// Update loop used to poll until seek finished
|
|
71
47
|
if (this.isSeeking)
|
|
72
48
|
return;
|
|
49
|
+
this.delay = NO_VIDEO_POLLING;
|
|
73
50
|
// Does the <video /> element need adding/removing?
|
|
74
|
-
const
|
|
75
|
-
const currentState = getStateAtTime(this._state, now);
|
|
51
|
+
const currentState = getStateAtTime(this._state, Date.now());
|
|
76
52
|
if (currentState) {
|
|
77
53
|
if (!this.videoElement || !this.isConnected(this.videoElement)) {
|
|
78
54
|
this.updateVideoElement();
|
|
@@ -86,108 +62,51 @@ export class VideoManager extends ClipManager {
|
|
|
86
62
|
return;
|
|
87
63
|
const { t, rate, volume } = { ...defaultVideoOptions, ...currentState };
|
|
88
64
|
// this.videoElement.src will be a fully qualified URL
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
this.videoElement.src = assetURL;
|
|
65
|
+
if (!this.videoElement.src.endsWith(this._state.file)) {
|
|
66
|
+
this.videoElement.src = this._state.file;
|
|
92
67
|
}
|
|
93
68
|
if (this.videoElement.style.objectFit !== this._state.fit) {
|
|
94
69
|
this.videoElement.style.objectFit = this._state.fit;
|
|
95
70
|
}
|
|
96
|
-
|
|
97
|
-
|
|
71
|
+
const opacityString = String(currentState.opacity);
|
|
72
|
+
if (this.videoElement.style.opacity !== opacityString) {
|
|
73
|
+
this.videoElement.style.opacity = opacityString;
|
|
98
74
|
}
|
|
99
|
-
const
|
|
100
|
-
if (parseInt(this.videoElement.style.zIndex) !==
|
|
101
|
-
this.videoElement.style.zIndex = String(
|
|
75
|
+
const zIndex = Math.round(currentState.zIndex ?? defaultVideoOptions.zIndex);
|
|
76
|
+
if (parseInt(this.videoElement.style.zIndex) !== zIndex) {
|
|
77
|
+
this.videoElement.style.zIndex = String(zIndex);
|
|
102
78
|
}
|
|
103
79
|
if (this.videoElement.volume !== volume) {
|
|
104
80
|
this.videoElement.volume = volume;
|
|
105
81
|
}
|
|
106
|
-
|
|
107
|
-
if (duration !== undefined) {
|
|
108
|
-
// Is the video looping?
|
|
109
|
-
if (isLooping(this._state, now, duration)) {
|
|
110
|
-
if (!this.videoElement.loop) {
|
|
111
|
-
console.debug('starting loop');
|
|
112
|
-
this.videoElement.loop = true;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
if (this.videoElement.loop) {
|
|
117
|
-
console.debug('stopping loop');
|
|
118
|
-
this.videoElement.loop = false;
|
|
119
|
-
}
|
|
120
|
-
// Has the video finished
|
|
121
|
-
if (t > duration) {
|
|
122
|
-
console.debug('ended');
|
|
123
|
-
this.delay = Infinity;
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
// Should the video be playing
|
|
82
|
+
// Should the element be playing?
|
|
129
83
|
if (this.videoElement.paused && rate > 0) {
|
|
130
|
-
|
|
131
|
-
this
|
|
132
|
-
|
|
133
|
-
});
|
|
134
|
-
}
|
|
84
|
+
this.videoElement.play().catch(() => {
|
|
85
|
+
// Do nothing - this will be retried in the next loop
|
|
86
|
+
});
|
|
135
87
|
}
|
|
136
88
|
const currentTime = this.videoElement.currentTime * 1000;
|
|
137
89
|
const deltaTime = currentTime - t;
|
|
138
90
|
const deltaTimeAbs = Math.abs(deltaTime);
|
|
139
|
-
|
|
140
|
-
if (this.timeToIntercept !== undefined) {
|
|
141
|
-
if (deltaTimeAbs <= TARGET_SYNC_THRESHOLD_MS) {
|
|
142
|
-
// We've successfully got back on track
|
|
143
|
-
console.log('intercepted', `${deltaTime.toFixed(0)}ms`);
|
|
144
|
-
this.timeToIntercept = undefined;
|
|
145
|
-
}
|
|
146
|
-
else {
|
|
147
|
-
const newTimeToIntercept = deltaTime / (rate - this.videoElement.playbackRate);
|
|
148
|
-
if (newTimeToIntercept < this.timeToIntercept && newTimeToIntercept > 0) {
|
|
149
|
-
// We're getting there, let's stay on course
|
|
150
|
-
console.debug(`intercepting ${newTimeToIntercept.toFixed(0)}ms`, `${deltaTime.toFixed(0)}ms`);
|
|
151
|
-
this.timeToIntercept = newTimeToIntercept;
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
// We've gone too far
|
|
155
|
-
console.debug('missed intercept', deltaTime, this.timeToIntercept, newTimeToIntercept);
|
|
156
|
-
this.timeToIntercept = undefined;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
91
|
+
this.delay = VIDEO_PLAYBACK_POLLING;
|
|
160
92
|
switch (true) {
|
|
161
|
-
case deltaTimeAbs <= TARGET_SYNC_THRESHOLD_MS:
|
|
93
|
+
case deltaTimeAbs <= TARGET_SYNC_THRESHOLD_MS:
|
|
162
94
|
// We are on course:
|
|
163
95
|
// - The video is within accepted latency of the server time
|
|
164
96
|
// - The playback rate is aligned with the server rate
|
|
165
|
-
console.debug(`${rate}x`, deltaTime.toFixed(0));
|
|
166
|
-
this.timeToIntercept = undefined;
|
|
167
97
|
if (this.videoElement.playbackRate !== rate) {
|
|
168
98
|
this.videoElement.playbackRate = rate;
|
|
169
99
|
}
|
|
170
|
-
this.delay = DEFAULT_VIDEO_POLLING_MS;
|
|
171
|
-
break;
|
|
172
|
-
}
|
|
173
|
-
case this.timeToIntercept !== undefined:
|
|
174
|
-
// We are currently on course to intercept
|
|
175
|
-
// - We don't want to adjust the playbackRate excessively to pop audio
|
|
176
|
-
// - We are on track to get back on time. So we can wait.
|
|
177
|
-
this.delay = this.timeToIntercept * INTERCEPTION_EARLY_CHECK_IN;
|
|
178
100
|
break;
|
|
179
|
-
case rate > 0 && deltaTimeAbs > TARGET_SYNC_THRESHOLD_MS && deltaTimeAbs <= MAX_SYNC_THRESHOLD_MS
|
|
101
|
+
case rate > 0 && deltaTimeAbs > TARGET_SYNC_THRESHOLD_MS && deltaTimeAbs <= MAX_SYNC_THRESHOLD_MS: {
|
|
180
102
|
// We are close, we can smoothly adjust with playbackRate:
|
|
181
103
|
// - The video must be playing
|
|
182
104
|
// - We must be close in time to the server time
|
|
183
105
|
const playbackRateAdjustment = playbackSmoothing(deltaTime);
|
|
184
|
-
const adjustedPlaybackRate = Math.max(0, rate
|
|
185
|
-
this.timeToIntercept = deltaTime / (rate - adjustedPlaybackRate);
|
|
186
|
-
console.debug(`${adjustedPlaybackRate.toFixed(2)}x`, `${deltaTime.toFixed(0)}ms`);
|
|
106
|
+
const adjustedPlaybackRate = Math.max(0, rate - playbackRateAdjustment);
|
|
187
107
|
if (this.videoElement.playbackRate !== adjustedPlaybackRate) {
|
|
188
108
|
this.videoElement.playbackRate = adjustedPlaybackRate;
|
|
189
109
|
}
|
|
190
|
-
this.delay = this.timeToIntercept * INTERCEPTION_EARLY_CHECK_IN;
|
|
191
110
|
break;
|
|
192
111
|
}
|
|
193
112
|
default: {
|
|
@@ -196,9 +115,6 @@ export class VideoManager extends ClipManager {
|
|
|
196
115
|
if (this.videoElement.playbackRate !== rate) {
|
|
197
116
|
this.videoElement.playbackRate = rate;
|
|
198
117
|
}
|
|
199
|
-
// delay to poll until seeked
|
|
200
|
-
console.debug('seeking');
|
|
201
|
-
this.delay = 10;
|
|
202
118
|
this.seekTo(t + rate * SEEK_LOOKAHEAD_MS);
|
|
203
119
|
break;
|
|
204
120
|
}
|
|
@@ -7,7 +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
|
+
zIndex: z.ZodDefault<z.ZodNumber>;
|
|
11
11
|
}, z.core.$strip>;
|
|
12
12
|
export type AudialProperties = z.infer<typeof AudialProperties>;
|
|
13
13
|
declare const AudialProperties: z.ZodObject<{
|
|
@@ -41,7 +41,7 @@ export type InitialImageKeyframe = z.infer<typeof InitialImageKeyframe>;
|
|
|
41
41
|
declare const InitialImageKeyframe: z.ZodTuple<[z.ZodNumber, z.ZodObject<{
|
|
42
42
|
set: z.ZodOptional<z.ZodObject<{
|
|
43
43
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
44
|
-
zIndex: z.ZodOptional<z.ZodNumber
|
|
44
|
+
zIndex: z.ZodOptional<z.ZodDefault<z.ZodNumber>>;
|
|
45
45
|
}, z.core.$strip>>;
|
|
46
46
|
}, z.core.$strip>], null>;
|
|
47
47
|
/**
|
|
@@ -51,11 +51,11 @@ export type ImageKeyframe = z.infer<typeof ImageKeyframe>;
|
|
|
51
51
|
declare const ImageKeyframe: z.ZodTuple<[z.ZodNumber, z.ZodObject<{
|
|
52
52
|
set: z.ZodOptional<z.ZodObject<{
|
|
53
53
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
54
|
-
zIndex: z.ZodOptional<z.ZodNumber
|
|
54
|
+
zIndex: z.ZodOptional<z.ZodDefault<z.ZodNumber>>;
|
|
55
55
|
}, z.core.$strip>>;
|
|
56
56
|
lerp: z.ZodOptional<z.ZodObject<{
|
|
57
57
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
58
|
-
zIndex: z.ZodOptional<z.ZodNumber
|
|
58
|
+
zIndex: z.ZodOptional<z.ZodDefault<z.ZodNumber>>;
|
|
59
59
|
}, z.core.$strip>>;
|
|
60
60
|
}, z.core.$strip>], null>;
|
|
61
61
|
/**
|
|
@@ -90,7 +90,7 @@ export type InitialVideoKeyframe = z.infer<typeof InitialVideoKeyframe>;
|
|
|
90
90
|
declare const InitialVideoKeyframe: z.ZodTuple<[z.ZodNumber, z.ZodObject<{
|
|
91
91
|
set: z.ZodOptional<z.ZodObject<{
|
|
92
92
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
93
|
-
zIndex: z.ZodOptional<z.ZodNumber
|
|
93
|
+
zIndex: z.ZodOptional<z.ZodDefault<z.ZodNumber>>;
|
|
94
94
|
volume: z.ZodOptional<z.ZodNumber>;
|
|
95
95
|
t: z.ZodOptional<z.ZodNumber>;
|
|
96
96
|
rate: z.ZodOptional<z.ZodNumber>;
|
|
@@ -103,14 +103,14 @@ export type VideoKeyframe = z.infer<typeof VideoKeyframe>;
|
|
|
103
103
|
declare const VideoKeyframe: z.ZodTuple<[z.ZodNumber, z.ZodObject<{
|
|
104
104
|
set: z.ZodOptional<z.ZodObject<{
|
|
105
105
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
106
|
-
zIndex: z.ZodOptional<z.ZodNumber
|
|
106
|
+
zIndex: z.ZodOptional<z.ZodDefault<z.ZodNumber>>;
|
|
107
107
|
volume: z.ZodOptional<z.ZodNumber>;
|
|
108
108
|
t: z.ZodOptional<z.ZodNumber>;
|
|
109
109
|
rate: z.ZodOptional<z.ZodNumber>;
|
|
110
110
|
}, z.core.$strip>>;
|
|
111
111
|
lerp: z.ZodOptional<z.ZodObject<{
|
|
112
112
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
113
|
-
zIndex: z.ZodOptional<z.ZodNumber
|
|
113
|
+
zIndex: z.ZodOptional<z.ZodDefault<z.ZodNumber>>;
|
|
114
114
|
volume: z.ZodOptional<z.ZodNumber>;
|
|
115
115
|
}, z.core.$strip>>;
|
|
116
116
|
}, z.core.$strip>], null>;
|
|
@@ -118,16 +118,16 @@ export declare const MediaSurfaceStateSchema: z.ZodRecord<z.ZodString, z.ZodUnio
|
|
|
118
118
|
keyframes: z.ZodTuple<[z.ZodTuple<[z.ZodNumber, z.ZodObject<{
|
|
119
119
|
set: z.ZodOptional<z.ZodObject<{
|
|
120
120
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
121
|
-
zIndex: z.ZodOptional<z.ZodNumber
|
|
121
|
+
zIndex: z.ZodOptional<z.ZodDefault<z.ZodNumber>>;
|
|
122
122
|
}, z.core.$strip>>;
|
|
123
123
|
lerp: z.ZodOptional<z.ZodObject<{
|
|
124
124
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
125
|
-
zIndex: z.ZodOptional<z.ZodNumber
|
|
125
|
+
zIndex: z.ZodOptional<z.ZodDefault<z.ZodNumber>>;
|
|
126
126
|
}, z.core.$strip>>;
|
|
127
127
|
}, z.core.$strip>], null>], z.ZodUnion<readonly [z.ZodTuple<[z.ZodNumber, z.ZodObject<{
|
|
128
128
|
set: z.ZodOptional<z.ZodObject<{
|
|
129
129
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
130
|
-
zIndex: z.ZodOptional<z.ZodNumber
|
|
130
|
+
zIndex: z.ZodOptional<z.ZodDefault<z.ZodNumber>>;
|
|
131
131
|
}, z.core.$strip>>;
|
|
132
132
|
}, z.core.$strip>], null>, z.ZodTuple<[z.ZodNumber, z.ZodNull], null>]>>;
|
|
133
133
|
type: z.ZodLiteral<"image">;
|
|
@@ -157,20 +157,20 @@ export declare const MediaSurfaceStateSchema: z.ZodRecord<z.ZodString, z.ZodUnio
|
|
|
157
157
|
keyframes: z.ZodTuple<[z.ZodTuple<[z.ZodNumber, z.ZodObject<{
|
|
158
158
|
set: z.ZodOptional<z.ZodObject<{
|
|
159
159
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
160
|
-
zIndex: z.ZodOptional<z.ZodNumber
|
|
160
|
+
zIndex: z.ZodOptional<z.ZodDefault<z.ZodNumber>>;
|
|
161
161
|
volume: z.ZodOptional<z.ZodNumber>;
|
|
162
162
|
t: z.ZodOptional<z.ZodNumber>;
|
|
163
163
|
rate: z.ZodOptional<z.ZodNumber>;
|
|
164
164
|
}, z.core.$strip>>;
|
|
165
165
|
lerp: z.ZodOptional<z.ZodObject<{
|
|
166
166
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
167
|
-
zIndex: z.ZodOptional<z.ZodNumber
|
|
167
|
+
zIndex: z.ZodOptional<z.ZodDefault<z.ZodNumber>>;
|
|
168
168
|
volume: z.ZodOptional<z.ZodNumber>;
|
|
169
169
|
}, z.core.$strip>>;
|
|
170
170
|
}, z.core.$strip>], null>], z.ZodUnion<readonly [z.ZodTuple<[z.ZodNumber, z.ZodObject<{
|
|
171
171
|
set: z.ZodOptional<z.ZodObject<{
|
|
172
172
|
opacity: z.ZodOptional<z.ZodNumber>;
|
|
173
|
-
zIndex: z.ZodOptional<z.ZodNumber
|
|
173
|
+
zIndex: z.ZodOptional<z.ZodDefault<z.ZodNumber>>;
|
|
174
174
|
volume: z.ZodOptional<z.ZodNumber>;
|
|
175
175
|
t: z.ZodOptional<z.ZodNumber>;
|
|
176
176
|
rate: z.ZodOptional<z.ZodNumber>;
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Connect to COGS to build a custom Media Master",
|
|
4
4
|
"author": "Clockwork Dog <info@clockwork.dog>",
|
|
5
5
|
"homepage": "https://github.com/clockwork-dog/cogs-sdk/tree/main/packages/javascript",
|
|
6
|
-
"version": "3.0.0-alpha.
|
|
6
|
+
"version": "3.0.0-alpha.9",
|
|
7
7
|
"keywords": [],
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"repository": {
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"cy:generate": "cypress run --e2e"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@clockworkdog/timesync": "^3.0.0-alpha.
|
|
40
|
+
"@clockworkdog/timesync": "^3.0.0-alpha.9",
|
|
41
41
|
"howler": "clockwork-dog/howler.js#fix-looping-clips",
|
|
42
42
|
"reconnecting-websocket": "^4.4.0",
|
|
43
43
|
"zod": "^4.1.13"
|