@clockworkdog/cogs-client 3.0.0-alpha.6 → 3.0.0-alpha.8

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/index.d.ts CHANGED
@@ -5,6 +5,8 @@ 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
+ export * from './state-based/MediaPreloader';
8
10
  export { default as CogsAudioPlayer } from './AudioPlayer';
9
11
  export { default as CogsVideoPlayer } from './VideoPlayer';
10
12
  export { SurfaceManager } from './state-based/SurfaceManager';
package/dist/index.js CHANGED
@@ -1,6 +1,8 @@
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
+ export * from './state-based/MediaPreloader';
4
6
  export { default as CogsAudioPlayer } from './AudioPlayer';
5
7
  export { default as CogsVideoPlayer } from './VideoPlayer';
6
8
  export { SurfaceManager } from './state-based/SurfaceManager';
@@ -1,9 +1,11 @@
1
1
  import { AudioState } from '../types/MediaSchema';
2
2
  import { ClipManager } from './ClipManager';
3
+ import { MediaPreloader } from './MediaPreloader';
3
4
  export declare class AudioManager extends ClipManager<AudioState> {
5
+ private mediaPreloader;
4
6
  private audioElement?;
5
7
  private isSeeking;
6
- constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: AudioState);
8
+ constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: AudioState, constructAssetURL: (file: string) => string, mediaPreloader: MediaPreloader);
7
9
  private updateAudioElement;
8
10
  /**
9
11
  * Helper function to seek to a specified time.
@@ -12,15 +12,17 @@ function playbackSmoothing(deltaTime) {
12
12
  return Math.sign(deltaTime) * Math.pow(Math.abs(deltaTime) / MAX_SYNC_THRESHOLD_MS, PLAYBACK_ADJUSTMENT_SMOOTHING) * MAX_PLAYBACK_RATE_ADJUSTMENT;
13
13
  }
14
14
  export class AudioManager extends ClipManager {
15
+ mediaPreloader;
15
16
  audioElement;
16
17
  isSeeking = false;
17
- constructor(surfaceElement, clipElement, state) {
18
- super(surfaceElement, clipElement, state);
18
+ constructor(surfaceElement, clipElement, state, constructAssetURL, mediaPreloader) {
19
+ super(surfaceElement, clipElement, state, constructAssetURL);
20
+ this.mediaPreloader = mediaPreloader;
19
21
  this.clipElement = clipElement;
20
22
  }
21
23
  updateAudioElement() {
22
- this.destroy();
23
- this.audioElement = document.createElement('audio');
24
+ const element = this.mediaPreloader.getElement(this._state.file, 'audio');
25
+ this.audioElement = element;
24
26
  this.clipElement.replaceChildren(this.audioElement);
25
27
  }
26
28
  /**
@@ -53,9 +55,10 @@ export class AudioManager extends ClipManager {
53
55
  if (!currentState || !this.audioElement)
54
56
  return;
55
57
  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;
58
+ // this.videoElement.src will be a fully qualified URL
59
+ const assetURL = this.constructAssetURL(this._state.file);
60
+ if (!this.audioElement.src.includes(assetURL)) {
61
+ this.updateAudioElement();
59
62
  }
60
63
  if (this.audioElement.volume !== volume) {
61
64
  this.audioElement.volume = volume;
@@ -105,7 +108,6 @@ export class AudioManager extends ClipManager {
105
108
  }
106
109
  destroy() {
107
110
  if (this.audioElement) {
108
- this.audioElement.src = '';
109
111
  this.audioElement.remove();
110
112
  }
111
113
  }
@@ -6,7 +6,8 @@ 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
- constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: T);
9
+ protected constructAssetURL: (file: string) => string;
10
+ constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: T, constructAssetURL: (file: string) => string);
10
11
  /**
11
12
  * This is the delay to be used in the update loop.
12
13
  * It is intended to be dynamic for each loop.
@@ -6,9 +6,11 @@ const DEFAULT_DELAY = 1_000;
6
6
  export class ClipManager {
7
7
  surfaceElement;
8
8
  clipElement;
9
- constructor(surfaceElement, clipElement, state) {
9
+ constructAssetURL;
10
+ constructor(surfaceElement, clipElement, state, constructAssetURL) {
10
11
  this.surfaceElement = surfaceElement;
11
12
  this.clipElement = clipElement;
13
+ this.constructAssetURL = constructAssetURL;
12
14
  this._state = state;
13
15
  // Allow the class to be constructed, then call the loop
14
16
  setTimeout(this.loop);
@@ -36,6 +38,9 @@ export class ClipManager {
36
38
  }
37
39
  _state;
38
40
  setState(newState) {
41
+ if (this._state.file !== newState.file) {
42
+ throw new Error(`Cannot change from ${this._state.file} to ${newState.file}. Create a new clip instead.`);
43
+ }
39
44
  this._state = newState;
40
45
  clearTimeout(this.timeout);
41
46
  this.loop();
@@ -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, constructAssetURL: (file: string) => string);
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, constructAssetURL) {
7
+ super(surfaceElement, clipElement, state, constructAssetURL);
8
8
  this.clipElement = clipElement;
9
9
  }
10
10
  updateImageElement() {
@@ -28,9 +28,10 @@ export class ImageManager extends ClipManager {
28
28
  }
29
29
  if (!this.imageElement || !currentState)
30
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;
31
+ // this.videoElement.src will be a fully qualified URL
32
+ const assetURL = this.constructAssetURL(this._state.file);
33
+ if (!this.imageElement.src.includes(assetURL)) {
34
+ this.imageElement.src = assetURL;
34
35
  }
35
36
  if (this.imageElement.style.objectFit !== this._state.fit) {
36
37
  this.imageElement.style.objectFit = this._state.fit;
@@ -0,0 +1,14 @@
1
+ import { MediaClientConfig } from '../types/CogsClientMessage';
2
+ export declare class MediaPreloader {
3
+ private _state;
4
+ private _elements;
5
+ private _constructAssetURL;
6
+ constructor(constructAssetURL: (file: string) => string, testState?: MediaClientConfig['files']);
7
+ get state(): {
8
+ [x: string]: import("../types/CogsClientMessage").Media;
9
+ };
10
+ setState(newState: MediaClientConfig['files']): void;
11
+ private update;
12
+ getElement(file: string, type: 'audio' | 'video'): HTMLAudioElement;
13
+ releaseElement(resource: string | HTMLMediaElement): void;
14
+ }
@@ -0,0 +1,86 @@
1
+ export class MediaPreloader {
2
+ _state;
3
+ _elements = {};
4
+ _constructAssetURL;
5
+ constructor(constructAssetURL, testState = {}) {
6
+ this._constructAssetURL = constructAssetURL;
7
+ this._state = testState;
8
+ }
9
+ get state() {
10
+ return { ...this._state };
11
+ }
12
+ setState(newState) {
13
+ this._state = newState;
14
+ this.update();
15
+ }
16
+ update() {
17
+ // Clean up previous elements
18
+ for (const [filename, media] of Object.entries(this._elements)) {
19
+ if (!(filename in this._state)) {
20
+ media.element.src = '';
21
+ delete this._elements[filename];
22
+ }
23
+ media.inUse = media.element.isConnected;
24
+ }
25
+ for (const [filename, fileConfig] of Object.entries(this._state)) {
26
+ if (filename in this._elements) {
27
+ continue;
28
+ }
29
+ // Create new elements
30
+ let preloadAttr;
31
+ if (fileConfig.preload === true) {
32
+ preloadAttr = 'auto';
33
+ }
34
+ else if (fileConfig.preload === false) {
35
+ preloadAttr = 'none';
36
+ }
37
+ else {
38
+ preloadAttr = fileConfig.preload;
39
+ }
40
+ switch (fileConfig.type) {
41
+ case 'audio': {
42
+ const element = document.createElement('audio');
43
+ element.src = this._constructAssetURL(filename);
44
+ element.preload = preloadAttr;
45
+ this._elements[filename] = { element, inUse: false, type: 'audio' };
46
+ break;
47
+ }
48
+ case 'video': {
49
+ const element = document.createElement('video');
50
+ element.src = this._constructAssetURL(filename);
51
+ element.preload = preloadAttr;
52
+ this._elements[filename] = { element, inUse: false, type: 'video' };
53
+ break;
54
+ }
55
+ }
56
+ }
57
+ }
58
+ getElement(file, type) {
59
+ const media = this._elements[file];
60
+ if (media && media.inUse === false) {
61
+ media.inUse = true;
62
+ return media.element;
63
+ }
64
+ else {
65
+ const element = document.createElement(type);
66
+ element.src = this._constructAssetURL(file);
67
+ this._elements[file] = { element, type, inUse: true };
68
+ return element;
69
+ }
70
+ }
71
+ releaseElement(resource) {
72
+ if (typeof resource === 'string') {
73
+ const media = this._elements[resource];
74
+ if (media) {
75
+ media.inUse = false;
76
+ }
77
+ }
78
+ else {
79
+ Object.entries(this._elements).forEach(([file, media]) => {
80
+ if (media.element === resource) {
81
+ delete this._elements[file];
82
+ }
83
+ });
84
+ }
85
+ }
86
+ }
@@ -1,4 +1,5 @@
1
1
  import { MediaSurfaceState } from '../types/MediaSchema';
2
+ import { MediaPreloader } from './MediaPreloader';
2
3
  export declare const DATA_CLIP_ID = "data-clip-id";
3
4
  /**
4
5
  * The SurfaceManager will receive state updates and:
@@ -6,11 +7,13 @@ export declare const DATA_CLIP_ID = "data-clip-id";
6
7
  * - Instantiate a ClipManager attached to each respective element
7
8
  */
8
9
  export declare class SurfaceManager {
10
+ private constructAssetUrl;
11
+ private mediaPreloader;
9
12
  private _state;
10
13
  setState(newState: MediaSurfaceState): void;
11
14
  private _element;
12
15
  get element(): HTMLDivElement;
13
16
  private resources;
14
- constructor(testState?: MediaSurfaceState);
17
+ constructor(constructAssetUrl: (file: string) => string, initialState?: MediaSurfaceState, mediaPreloader?: MediaPreloader);
15
18
  update(): void;
16
19
  }
@@ -1,6 +1,7 @@
1
1
  import { ImageManager } from './ImageManager';
2
2
  import { VideoManager } from './VideoManager';
3
3
  import { AudioManager } from './AudioManager';
4
+ import { MediaPreloader } from './MediaPreloader';
4
5
  export const DATA_CLIP_ID = 'data-clip-id';
5
6
  /**
6
7
  * The SurfaceManager will receive state updates and:
@@ -8,6 +9,8 @@ export const DATA_CLIP_ID = 'data-clip-id';
8
9
  * - Instantiate a ClipManager attached to each respective element
9
10
  */
10
11
  export class SurfaceManager {
12
+ constructAssetUrl;
13
+ mediaPreloader;
11
14
  _state = {};
12
15
  setState(newState) {
13
16
  this._state = newState;
@@ -18,11 +21,14 @@ export class SurfaceManager {
18
21
  return this._element;
19
22
  }
20
23
  resources = {};
21
- constructor(testState) {
24
+ constructor(constructAssetUrl, initialState = {}, mediaPreloader = new MediaPreloader(constructAssetUrl)) {
25
+ this.constructAssetUrl = constructAssetUrl;
26
+ this.mediaPreloader = mediaPreloader;
22
27
  this._element = document.createElement('div');
28
+ this._element.className = 'surface-manager';
23
29
  this._element.style.width = '100%';
24
30
  this._element.style.height = '100%';
25
- this._state = testState || {};
31
+ this._state = initialState || {};
26
32
  this.update();
27
33
  }
28
34
  update() {
@@ -62,13 +68,13 @@ export class SurfaceManager {
62
68
  if (!resource.manager) {
63
69
  switch (clip.type) {
64
70
  case 'image':
65
- resource.manager = new ImageManager(this._element, resource.element, clip);
71
+ resource.manager = new ImageManager(this._element, resource.element, clip, this.constructAssetUrl);
66
72
  break;
67
73
  case 'audio':
68
- resource.manager = new AudioManager(this._element, resource.element, clip);
74
+ resource.manager = new AudioManager(this._element, resource.element, clip, this.constructAssetUrl, this.mediaPreloader);
69
75
  break;
70
76
  case 'video':
71
- resource.manager = new VideoManager(this._element, resource.element, clip);
77
+ resource.manager = new VideoManager(this._element, resource.element, clip, this.constructAssetUrl, this.mediaPreloader);
72
78
  break;
73
79
  }
74
80
  }
@@ -1,10 +1,12 @@
1
1
  import { VideoState } from '../types/MediaSchema';
2
2
  import { ClipManager } from './ClipManager';
3
+ import { MediaPreloader } from './MediaPreloader';
3
4
  export declare class VideoManager extends ClipManager<VideoState> {
5
+ private mediaPreloader;
4
6
  private videoElement?;
5
7
  private isSeeking;
6
8
  private timeToIntercept;
7
- constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: VideoState);
9
+ constructor(surfaceElement: HTMLElement, clipElement: HTMLElement, state: VideoState, constructAssetURL: (file: string) => string, mediaPreloader: MediaPreloader);
8
10
  private updateVideoElement;
9
11
  private get videoDuration();
10
12
  /**
@@ -29,18 +29,20 @@ function isLooping(state, time, duration) {
29
29
  return Math.abs(timeRemaining - timeUntilKeyframe) <= LOOPING_EPSILON_MS;
30
30
  }
31
31
  export class VideoManager extends ClipManager {
32
+ mediaPreloader;
32
33
  videoElement;
33
34
  // We seek to another part of the video and do nothing until we get there
34
35
  isSeeking = false;
35
36
  // We change playbackRate to intercept the server time of the video and don't change course until we intercept
36
37
  timeToIntercept = undefined;
37
- constructor(surfaceElement, clipElement, state) {
38
- super(surfaceElement, clipElement, state);
38
+ constructor(surfaceElement, clipElement, state, constructAssetURL, mediaPreloader) {
39
+ super(surfaceElement, clipElement, state, constructAssetURL);
40
+ this.mediaPreloader = mediaPreloader;
39
41
  this.clipElement = clipElement;
40
42
  }
41
43
  updateVideoElement() {
42
- this.destroy();
43
- this.videoElement = document.createElement('video');
44
+ const element = this.mediaPreloader.getElement(this._state.file, 'video');
45
+ this.videoElement = element;
44
46
  this.clipElement.replaceChildren(this.videoElement);
45
47
  this.videoElement.style.position = 'absolute';
46
48
  this.videoElement.style.width = '100%';
@@ -86,8 +88,9 @@ export class VideoManager extends ClipManager {
86
88
  return;
87
89
  const { t, rate, volume } = { ...defaultVideoOptions, ...currentState };
88
90
  // this.videoElement.src will be a fully qualified URL
89
- if (!this.videoElement.src.endsWith(this._state.file)) {
90
- this.videoElement.src = this._state.file;
91
+ const assetURL = this.constructAssetURL(this._state.file);
92
+ if (!this.videoElement.src.includes(assetURL)) {
93
+ this.updateVideoElement();
91
94
  }
92
95
  if (this.videoElement.style.objectFit !== this._state.fit) {
93
96
  this.videoElement.style.objectFit = this._state.fit;
@@ -205,7 +208,7 @@ export class VideoManager extends ClipManager {
205
208
  }
206
209
  destroy() {
207
210
  if (this.videoElement) {
208
- this.videoElement.src = '';
211
+ this.mediaPreloader.releaseElement(this.videoElement);
209
212
  this.videoElement.remove();
210
213
  }
211
214
  }
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",
6
+ "version": "3.0.0-alpha.8",
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.6",
40
+ "@clockworkdog/timesync": "^3.0.0-alpha.8",
41
41
  "howler": "clockwork-dog/howler.js#fix-looping-clips",
42
42
  "reconnecting-websocket": "^4.4.0",
43
43
  "zod": "^4.1.13"