-
Notifications
You must be signed in to change notification settings - Fork 18
/
BanubaPlayer.js
236 lines (192 loc) · 5.37 KB
/
BanubaPlayer.js
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
let SDK_VERSION = "1.16.0";
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.has("sdk")) {
SDK_VERSION = urlParams.get("sdk");
}
const {
Dom,
Effect,
Image,
ImageCapture,
Module,
Player,
VideoRecorder,
Webcam,
VERSION,
} = await import(
`https://cdn.jsdelivr.net/npm/@banuba/webar@${SDK_VERSION}/dist/BanubaSDK.browser.esm.min.js`
);
if (VERSION != SDK_VERSION) {
console.warn(
`Version dont match: requested ${SDK_VERSION} - received ${VERSION}`
);
SDK_VERSION = VERSION;
if (SDK_VERSION.includes("-")) {
console.warn("SDK version includes '-'. Removing it...");
SDK_VERSION = SDK_VERSION.slice(0, SDK_VERSION.indexOf("-"));
}
}
const sdkUrl = "https://cdn.jsdelivr.net/npm/@banuba/webar";
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
const modulesList = [
"background",
"body",
"eyes",
"face_tracker",
"hair",
"hands",
"lips",
"skin",
];
export const fps = {
cam: 0,
processing: 0,
render: 0,
};
const fpsCounter = {
cam: 0,
processing: 0,
render: 0,
};
let currentEffect;
console.log("Load player with SDK: ", SDK_VERSION);
const player = await Player.create({
clientToken: window.BANUBA_CLIENT_TOKEN,
proxyVideoRequestsTo: isSafari ? "___range-requests___/" : null,
useFutureInterpolate: false,
locateFile: `${sdkUrl}@${SDK_VERSION}/dist`,
});
await Promise.all(
modulesList.map((moduleId) => {
return new Promise(async (resolve) => {
try {
const module = await Module.preload(
`https://cdn.jsdelivr.net/npm/@banuba/webar@${SDK_VERSION}/dist/modules/${moduleId}.zip`
);
await player.addModule(module);
} catch (error) {
console.warn(`Load module ${moduleId} error: `, error);
}
return resolve();
});
})
);
const startFpsTracking = () => {
player.addEventListener("framereceived", () => fpsCounter.cam++);
player.addEventListener(
"frameprocessed",
({ detail }) => (fpsCounter.processing = 1000 / detail.averagedDuration)
);
player.addEventListener("framerendered", () => fpsCounter.render++);
setInterval(() => {
fps.cam = fpsCounter.cam;
fps.render = fpsCounter.render;
fps.processing = fpsCounter.processing;
fpsCounter.cam = 0;
fpsCounter.render = 0;
}, 1000);
};
let curResult;
let analyseFunc;
const renderAnalysisResultFuncs = {
Detection_gestures: async (paramString, resultBlock) => {
const res = await currentEffect.evalJs(paramString);
if (!(curResult !== res && res !== undefined)) {
return false;
}
curResult = res;
const icon =
res === "No Gesture"
? ""
: `<img src="assets/icons/hand_gestures/${curResult}.svg" alt="${curResult}"/>`;
resultBlock.innerHTML = `${icon}<span>${curResult}</span>`;
},
heart_rate: async (paramString, resultBlock) => {
const res = await currentEffect.evalJs(paramString);
if (!(curResult !== res && res !== undefined)) {
return false;
}
curResult = res;
if (curResult.includes("calculation")) {
resultBlock.classList.add("heart-rate__analyse");
} else {
resultBlock.classList.remove("heart-rate__analyse");
}
resultBlock.innerText = curResult;
return true;
},
test_Ruler: async (paramString, resultBlock) => {
const res = await currentEffect.evalJs(paramString);
if (curResult !== res && res !== undefined) {
curResult = res;
resultBlock.innerText = curResult;
}
},
};
/**
* __analyticsState can be "enabled" or "disabled"
*/
const __analyticsActive = "active";
const __analyticsInActive = "inactive";
let _analyticsState = __analyticsInActive;
export const startAnalysis = async (effectName, paramString, resultBlock) => {
analyseFunc = () =>
renderAnalysisResultFuncs[effectName.split(".")[0]](
paramString,
resultBlock
);
player.addEventListener("framedata", analyseFunc);
_analyticsState = __analyticsActive;
};
export const stopAnalysis = () => {
if (_analyticsState === __analyticsActive)
player.removeEventListener("framedata", analyseFunc);
_analyticsState = __analyticsInActive;
};
export const clearEffect = async () => {
await player.clearEffect();
};
export const muteToggle = (value) => {
player.setVolume(value);
};
export const getSource = (sourceType, file) => {
return sourceType === "webcam" ? new Webcam() : new Image(file);
};
export const getPlayer = () => {
return player;
};
export const startPlayer = (source) => {
player.use(source);
Dom.render(player, "#webar");
startFpsTracking();
};
export const applyEffect = async (effectName) => {
currentEffect = new Effect(effectName);
await player.applyEffect(currentEffect);
};
export const applyEffectParam = async (paramString) => {
await currentEffect.evalJs(paramString);
};
export const startGame = () => {
currentEffect.evalJs("isButtonTouched").then((isButtonTouched) => {
if (isButtonTouched === "false") {
currentEffect.evalJs("onClick()");
}
});
};
export const getScreenshot = async () => {
const capture = new ImageCapture(player);
return await capture.takePhoto();
};
let recorder;
const getRecorder = () => {
if (recorder) return recorder;
recorder = new VideoRecorder(player);
return recorder;
};
export const startRecord = () => {
getRecorder().start();
};
export const stopRecord = async () => {
return await getRecorder().stop();
};