@clockworkdog/cogs-client-react 3.0.0-alpha.11 → 3.0.0-alpha.14
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.
|
@@ -1,14 +1,20 @@
|
|
|
1
|
-
import { SurfaceManager } from '@clockworkdog/cogs-client';
|
|
1
|
+
import { MediaPreloader, SurfaceManager } from '@clockworkdog/cogs-client';
|
|
2
2
|
import React, { useEffect, useRef, useState } from 'react';
|
|
3
3
|
export function MediaSurface({ cogsConnection }) {
|
|
4
4
|
const surfaceManagerRef = useRef(undefined);
|
|
5
5
|
const surfaceStateRef = useRef(undefined);
|
|
6
|
+
const volumeRef = useRef(cogsConnection.mediaConfig?.globalVolume);
|
|
7
|
+
const mediaPreloaderRef = useRef(null);
|
|
6
8
|
const [surfaceElem, setSurfaceElem] = useState(null);
|
|
7
9
|
// Keep updated list of audio outputs
|
|
8
10
|
const audioOutputs = useRef({});
|
|
9
11
|
useEffect(() => {
|
|
10
12
|
async function updateAudioOutputs() {
|
|
11
13
|
const audioOutputs = {};
|
|
14
|
+
if (!navigator.mediaDevices) {
|
|
15
|
+
// `navigator.mediaDevices` is undefined on COGS AV <= 4.5 because of secure origin permissions
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
12
18
|
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
13
19
|
const outputs = devices.filter((device) => device.kind === 'audiooutput');
|
|
14
20
|
outputs.forEach((output) => {
|
|
@@ -21,7 +27,17 @@ export function MediaSurface({ cogsConnection }) {
|
|
|
21
27
|
}, []);
|
|
22
28
|
// Create and attach new surface manager
|
|
23
29
|
useEffect(() => {
|
|
24
|
-
const
|
|
30
|
+
const constructURL = (url) => cogsConnection.getAssetUrl(url);
|
|
31
|
+
const preloader = new MediaPreloader(constructURL);
|
|
32
|
+
mediaPreloaderRef.current = preloader;
|
|
33
|
+
const files = cogsConnection.mediaConfig?.files;
|
|
34
|
+
if (files) {
|
|
35
|
+
preloader.setState(files);
|
|
36
|
+
}
|
|
37
|
+
const sm = new SurfaceManager(constructURL, (outputLabel) => audioOutputs.current[outputLabel] ?? '', {}, preloader);
|
|
38
|
+
if (volumeRef.current !== undefined) {
|
|
39
|
+
sm.volume = volumeRef.current;
|
|
40
|
+
}
|
|
25
41
|
surfaceManagerRef.current = sm;
|
|
26
42
|
surfaceElem?.replaceChildren(sm.element);
|
|
27
43
|
return () => {
|
|
@@ -32,14 +48,25 @@ export function MediaSurface({ cogsConnection }) {
|
|
|
32
48
|
}, [surfaceElem, cogsConnection.getAssetUrl]);
|
|
33
49
|
// Listen to messages
|
|
34
50
|
useEffect(() => {
|
|
35
|
-
function
|
|
36
|
-
|
|
51
|
+
function handleMessages({ message }) {
|
|
52
|
+
const preloader = mediaPreloaderRef.current;
|
|
53
|
+
const manager = surfaceManagerRef.current;
|
|
54
|
+
if (message.type === 'media_state' && message.media_strategy === 'state' && manager) {
|
|
37
55
|
surfaceStateRef.current = message.state;
|
|
38
|
-
|
|
56
|
+
manager.setState(message.state);
|
|
57
|
+
}
|
|
58
|
+
if (message.type === 'media_config_update') {
|
|
59
|
+
volumeRef.current = message.globalVolume;
|
|
60
|
+
if (preloader) {
|
|
61
|
+
preloader.setState(message.files);
|
|
62
|
+
}
|
|
63
|
+
if (manager) {
|
|
64
|
+
manager.volume = message.globalVolume;
|
|
65
|
+
}
|
|
39
66
|
}
|
|
40
67
|
}
|
|
41
|
-
cogsConnection.addEventListener('message',
|
|
42
|
-
return () => cogsConnection.removeEventListener('message',
|
|
68
|
+
cogsConnection.addEventListener('message', handleMessages);
|
|
69
|
+
return () => cogsConnection.removeEventListener('message', handleMessages);
|
|
43
70
|
}, [cogsConnection]);
|
|
44
71
|
return React.createElement("div", { className: "media-surface", ref: setSurfaceElem });
|
|
45
72
|
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "React components and hooks to 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/react",
|
|
6
|
-
"version": "3.0.0-alpha.
|
|
6
|
+
"version": "3.0.0-alpha.14",
|
|
7
7
|
"keywords": [],
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"repository": {
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"prerelease": "yarn npm publish --access public --tag=next"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@clockworkdog/cogs-client": "^3.0.0-alpha.
|
|
31
|
+
"@clockworkdog/cogs-client": "^3.0.0-alpha.14"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
34
|
"react": "^18.0.0 || ^19.0.0",
|