@zappar/zappar-cv 3.0.0-beta.6 → 3.0.1-alpha.1

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/README.md CHANGED
@@ -18,8 +18,8 @@ npm i @zappar/zappar-cv
18
18
 
19
19
  You can use our CDN from within your HTML:
20
20
  ```
21
- <script src="https://libs.zappar.com/zappar-cv/3.0.0-beta.6/zappar-cv.js"></script>
21
+ <script src="https://libs.zappar.com/zappar-cv/3.0.1-alpha.1/zappar-cv.js"></script>
22
22
  ```
23
23
 
24
24
  Or you can download and host our standalone JavaScript bundle:
25
- [https://libs.zappar.com/zappar-cv/3.0.0-beta.6/zappar-cv.zip](https://libs.zappar.com/zappar-cv/3.0.0-beta.6/zappar-cv.zip)
25
+ [https://libs.zappar.com/zappar-cv/3.0.1-alpha.1/zappar-cv.zip](https://libs.zappar.com/zappar-cv/3.0.1-alpha.1/zappar-cv.zip)
@@ -35,11 +35,15 @@ export class CameraSource extends HTMLElementSource {
35
35
  if (!pipeline)
36
36
  return;
37
37
  let timeStamp = (ev.timeStamp !== undefined && ev.timeStamp !== null) ? ev.timeStamp : performance.now();
38
- let interval = ev.interval * profile.intervalMultiplier;
39
- if (this._lastTimestamp > -1 && !profile.trustSensorIntervals) {
40
- interval = Math.max(timeStamp - this._lastTimestamp, interval);
38
+ const interval = ev.interval * profile.intervalMultiplier;
39
+ if (profile.trustSensorIntervals) {
40
+ if (this._lastTimestamp === -1)
41
+ this._lastTimestamp = timeStamp;
42
+ else {
43
+ this._lastTimestamp += interval;
44
+ timeStamp = this._lastTimestamp;
45
+ }
41
46
  }
42
- this._lastTimestamp = timeStamp;
43
47
  if (ev.acceleration !== null &&
44
48
  ev.acceleration.x !== null &&
45
49
  ev.acceleration.y !== null &&
@@ -0,0 +1 @@
1
+ export declare function launchCeresWorker(wasmUrl: string, module: WebAssembly.Module, port: MessagePort): Promise<void>;
@@ -0,0 +1,67 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { getWorkerMessageAPI } from "./worker-messages";
11
+ import * as ZNM from "./zappar-cv";
12
+ const ctx = self;
13
+ let launchHandler = (evt) => {
14
+ if (evt && evt.data && evt.data.t === "wasm") {
15
+ let url = location.href.startsWith("blob") ? evt.data.url : new URL("./zappar-cv.wasm", import.meta.url).toString();
16
+ launchCeresWorker(url, evt.data.module, evt.data.port);
17
+ ctx.removeEventListener("message", launchHandler);
18
+ }
19
+ };
20
+ ctx.addEventListener("message", launchHandler);
21
+ export function launchCeresWorker(wasmUrl, module, port) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ let mod = ZNM.default({
24
+ locateFile: (path, prefix) => {
25
+ if (path.endsWith("zappar-cv.wasm")) {
26
+ return wasmUrl;
27
+ }
28
+ return prefix + path;
29
+ },
30
+ instantiateWasm: (imports, successCallback) => {
31
+ const instance = new WebAssembly.Instance(module, imports);
32
+ successCallback(instance);
33
+ return instance.exports;
34
+ },
35
+ onRuntimeInitialized: () => {
36
+ const ceres_worker = mod.cwrap('ceres_worker', null, []);
37
+ const messageAPI = getWorkerMessageAPI(mod);
38
+ port.addEventListener('message', evt => {
39
+ if (evt.data.t !== 'msgsend')
40
+ return;
41
+ const data = evt.data.data;
42
+ const reference = evt.data.reference;
43
+ const ptr = mod._malloc(data.byteLength);
44
+ mod.HEAPU8.set(data, ptr);
45
+ messageAPI.worker_message_receive(reference, data.byteLength, ptr);
46
+ mod._free(ptr);
47
+ ceres_worker();
48
+ const toSend = messageAPI.worker_message_send_count();
49
+ for (let i = 0; i < toSend; i++) {
50
+ const reference = messageAPI.worker_message_send_reference(i);
51
+ const dataSize = messageAPI.worker_message_send_data_size(i);
52
+ const dataPtr = messageAPI.worker_message_send_data(i);
53
+ const data = mod.HEAPU8.slice(dataPtr, dataPtr + dataSize);
54
+ port.postMessage({
55
+ t: 'msgrec',
56
+ data,
57
+ reference
58
+ }, [data.buffer]);
59
+ }
60
+ messageAPI.worker_message_send_clear();
61
+ });
62
+ port.start();
63
+ }
64
+ });
65
+ });
66
+ }
67
+ ;
package/lib/direct.js CHANGED
@@ -13,6 +13,8 @@ import { messageManager as workerMessageManager } from "./worker-server";
13
13
  export let messageManager = new MsgManager();
14
14
  export function launchWorker(wasm) {
15
15
  return __awaiter(this, void 0, void 0, function* () {
16
+ const response = yield fetch(new URL("./zappar-cv.wasm", import.meta.url).toString());
17
+ const module = yield WebAssembly.compileStreaming(response);
16
18
  messageManager.onOutgoingMessage.bind(() => {
17
19
  let msgs = messageManager.getOutgoingMessages();
18
20
  for (let msg of msgs)
@@ -23,6 +25,6 @@ export function launchWorker(wasm) {
23
25
  for (let msg of msgs)
24
26
  messageManager.postIncomingMessage(msg.msg);
25
27
  });
26
- launchWorkerServer(wasm);
28
+ launchWorkerServer(wasm, module);
27
29
  });
28
30
  }
@@ -103,7 +103,7 @@ export class zappar_client {
103
103
  m.bool(user_facing);
104
104
  });
105
105
  },
106
- pipeline_camera_frame_submit_raw_pointer: (o, data, dataLength, format, width, height, user_data, camera_to_device_transform, rotation, camera_model, user_facing) => {
106
+ pipeline_camera_frame_submit_raw_pointer: (o, data, dataLength, format, width, height, user_data, camera_to_device_transform, rotation, camera_model, user_facing, timestamp) => {
107
107
  let s = this._pipeline_state_by_instance.get(o);
108
108
  if (!s)
109
109
  throw new Error("This object has been destroyed");
@@ -26,7 +26,7 @@ export function getRuntimeObject(mod) {
26
26
  ]);
27
27
  let pipeline_camera_frame_submit_raw_pointer_wrapped = mod.cwrap("zappar_pipeline_camera_frame_submit_raw_pointer", null, [
28
28
  "number",
29
- "number", "number", "number", "number", "number", "number", "number", "number", "number", "number"
29
+ "number", "number", "number", "number", "number", "number", "number", "number", "number", "number", "number"
30
30
  ]);
31
31
  let pipeline_camera_frame_camera_attitude_wrapped = mod.cwrap("zappar_pipeline_camera_frame_camera_attitude", "number", [
32
32
  "number"
@@ -296,7 +296,7 @@ export function getRuntimeObject(mod) {
296
296
  let ret = pipeline_camera_frame_submit_wrapped(o, arg_data, arg_len_data, arg_width, arg_height, arg_user_data, arg_camera_to_device_transform, arg_camera_model, arg_user_facing);
297
297
  return ret;
298
298
  },
299
- pipeline_camera_frame_submit_raw_pointer: (o, data, dataLength, format, width, height, user_data, camera_to_device_transform, rotation, camera_model, user_facing) => {
299
+ pipeline_camera_frame_submit_raw_pointer: (o, data, dataLength, format, width, height, user_data, camera_to_device_transform, rotation, camera_model, user_facing, timestamp) => {
300
300
  let arg_data = data;
301
301
  let arg_dataLength = dataLength;
302
302
  let arg_format = format;
@@ -309,7 +309,8 @@ export function getRuntimeObject(mod) {
309
309
  let arg_camera_model = getFloatDataArrayForArgIndex(8, camera_model.byteLength);
310
310
  mod.HEAPF32.set(camera_model, arg_camera_model / 4);
311
311
  let arg_user_facing = user_facing ? 1 : 0;
312
- let ret = pipeline_camera_frame_submit_raw_pointer_wrapped(o, arg_data, arg_dataLength, arg_format, arg_width, arg_height, arg_user_data, arg_camera_to_device_transform, arg_rotation, arg_camera_model, arg_user_facing);
312
+ let arg_timestamp = timestamp;
313
+ let ret = pipeline_camera_frame_submit_raw_pointer_wrapped(o, arg_data, arg_dataLength, arg_format, arg_width, arg_height, arg_user_data, arg_camera_to_device_transform, arg_rotation, arg_camera_model, arg_user_facing, arg_timestamp);
313
314
  return ret;
314
315
  },
315
316
  pipeline_camera_frame_camera_attitude: (o) => {
@@ -107,7 +107,7 @@ export interface zappar_cwrap {
107
107
  pipeline_camera_model(o: zappar_pipeline_t): Float32Array;
108
108
  pipeline_camera_frame_user_data(o: zappar_pipeline_t): number;
109
109
  pipeline_camera_frame_submit(o: zappar_pipeline_t, data: ArrayBuffer, width: number, height: number, user_data: number, camera_to_device_transform: Float32Array, camera_model: Float32Array, user_facing: boolean): void;
110
- pipeline_camera_frame_submit_raw_pointer(o: zappar_pipeline_t, data: number, dataLength: number, format: frame_pixel_format_t, width: number, height: number, user_data: number, camera_to_device_transform: Float32Array, rotation: number, camera_model: Float32Array, user_facing: boolean): void;
110
+ pipeline_camera_frame_submit_raw_pointer(o: zappar_pipeline_t, data: number, dataLength: number, format: frame_pixel_format_t, width: number, height: number, user_data: number, camera_to_device_transform: Float32Array, rotation: number, camera_model: Float32Array, user_facing: boolean, timestamp: number): void;
111
111
  pipeline_camera_frame_camera_attitude(o: zappar_pipeline_t): Float32Array;
112
112
  pipeline_camera_frame_device_attitude(o: zappar_pipeline_t): Float32Array;
113
113
  pipeline_motion_accelerometer_submit(o: zappar_pipeline_t, time: number, x: number, y: number, z: number): void;
@@ -11,6 +11,9 @@ export declare class MSTPCameraSource extends Source {
11
11
  private _isPaused;
12
12
  private _isUserFacing;
13
13
  private _cameraToScreenRotation;
14
+ private _accelerometer?;
15
+ private _linearAcceleration?;
16
+ private _gyroscope?;
14
17
  constructor(_impl: zappar_camera_source_t, _pipeline: zappar_pipeline_t, _deviceId: string);
15
18
  destroy(): void;
16
19
  private _stop;
@@ -26,6 +29,12 @@ export declare class MSTPCameraSource extends Source {
26
29
  private _deviceMotionListener;
27
30
  private _startDeviceOrientation;
28
31
  private _startDeviceMotion;
32
+ private _lastAccelerometerTimestamp;
33
+ private _accelerometerReading;
34
+ private _lastLinearAccelerationTimestamp;
35
+ private _linearAccelerationReading;
36
+ private _lastGyroscopeReading;
37
+ private _gyroscopeReading;
29
38
  private _stopDeviceMotion;
30
39
  uploadGL(info: CameraFrameInfo): void;
31
40
  }
@@ -31,12 +31,15 @@ export class MSTPCameraSource extends Source {
31
31
  if (!pipeline)
32
32
  return;
33
33
  let timeStamp = (ev.timeStamp !== undefined && ev.timeStamp !== null) ? ev.timeStamp : performance.now();
34
- let interval = ev.interval;
35
- if (this._lastTimestamp > -1 && !profile.trustSensorIntervals) {
36
- interval = Math.max(timeStamp - this._lastTimestamp, interval);
34
+ const interval = ev.interval * profile.intervalMultiplier;
35
+ if (profile.trustSensorIntervals) {
36
+ if (this._lastTimestamp === -1)
37
+ this._lastTimestamp = timeStamp;
38
+ else {
39
+ this._lastTimestamp += interval;
40
+ timeStamp = this._lastTimestamp;
41
+ }
37
42
  }
38
- this._lastTimestamp = timeStamp;
39
- interval *= profile.intervalMultiplier;
40
43
  if (ev.acceleration !== null &&
41
44
  ev.acceleration.x !== null &&
42
45
  ev.acceleration.y !== null &&
@@ -61,6 +64,47 @@ export class MSTPCameraSource extends Source {
61
64
  this._startDeviceOrientation();
62
65
  }
63
66
  };
67
+ this._lastAccelerometerTimestamp = -1;
68
+ this._accelerometerReading = () => {
69
+ var _a;
70
+ if (!this._accelerometer || this._accelerometer.x === undefined || this._accelerometer.y === undefined || this._accelerometer.z === undefined)
71
+ return;
72
+ let pipeline = Pipeline.get(this._pipeline);
73
+ if (!pipeline)
74
+ return;
75
+ const timestamp = (_a = this._accelerometer.timestamp) !== null && _a !== void 0 ? _a : performance.now();
76
+ const interval = this._lastAccelerometerTimestamp > -1 ? timestamp - this._lastAccelerometerTimestamp : 30;
77
+ this._lastAccelerometerTimestamp = timestamp;
78
+ pipeline.motionAccelerometerSubmit(timestamp, this._accelerometer.x * profile.deviceMotionMutliplier, this._accelerometer.y * profile.deviceMotionMutliplier, this._accelerometer.z * profile.deviceMotionMutliplier);
79
+ pipeline.motionAccelerometerWithGravitySubmitInt(timestamp, interval, this._accelerometer.x * profile.deviceMotionMutliplier, this._accelerometer.y * profile.deviceMotionMutliplier, this._accelerometer.z * profile.deviceMotionMutliplier);
80
+ };
81
+ this._lastLinearAccelerationTimestamp = -1;
82
+ this._linearAccelerationReading = () => {
83
+ var _a;
84
+ if (!this._linearAcceleration || this._linearAcceleration.x === undefined || this._linearAcceleration.y === undefined || this._linearAcceleration.z === undefined)
85
+ return;
86
+ let pipeline = Pipeline.get(this._pipeline);
87
+ if (!pipeline)
88
+ return;
89
+ const timestamp = (_a = this._linearAcceleration.timestamp) !== null && _a !== void 0 ? _a : performance.now();
90
+ const interval = this._lastLinearAccelerationTimestamp > -1 ? timestamp - this._lastLinearAccelerationTimestamp : 30;
91
+ this._lastLinearAccelerationTimestamp = timestamp;
92
+ pipeline.motionAccelerometerWithoutGravitySubmitInt(timestamp, interval, this._linearAcceleration.x * profile.deviceMotionMutliplier, this._linearAcceleration.y * profile.deviceMotionMutliplier, this._linearAcceleration.z * profile.deviceMotionMutliplier);
93
+ };
94
+ this._lastGyroscopeReading = -1;
95
+ this._gyroscopeReading = () => {
96
+ var _a;
97
+ if (!this._gyroscope || this._gyroscope.x === undefined || this._gyroscope.y === undefined || this._gyroscope.z === undefined)
98
+ return;
99
+ let pipeline = Pipeline.get(this._pipeline);
100
+ if (!pipeline)
101
+ return;
102
+ const timestamp = (_a = this._gyroscope.timestamp) !== null && _a !== void 0 ? _a : performance.now();
103
+ const interval = this._lastGyroscopeReading > -1 ? timestamp - this._lastGyroscopeReading : 30;
104
+ this._lastGyroscopeReading = timestamp;
105
+ pipeline.motionRotationRateSubmit(timestamp, -this._gyroscope.x, -this._gyroscope.y, -this._gyroscope.z);
106
+ pipeline.motionRotationRateSubmitInt(timestamp, interval, this._gyroscope.x * 180 / Math.PI, this._gyroscope.y * 180 / Math.PI, this._gyroscope.z * 180 / Math.PI);
107
+ };
64
108
  zcout("Using MSTP camera source");
65
109
  }
66
110
  destroy() {
@@ -221,9 +265,38 @@ export class MSTPCameraSource extends Source {
221
265
  });
222
266
  }
223
267
  _startDeviceMotion() {
224
- window.addEventListener("devicemotion", this._deviceMotionListener, false);
268
+ try {
269
+ this._accelerometer = new Accelerometer({ frequency: 60 });
270
+ this._linearAcceleration = new LinearAccelerationSensor({ frequency: 60 });
271
+ this._gyroscope = new Gyroscope({ frequency: 60 });
272
+ // throw new Error();
273
+ this._accelerometer.addEventListener('reading', this._accelerometerReading);
274
+ this._accelerometer.start();
275
+ this._linearAcceleration.addEventListener('reading', this._linearAccelerationReading);
276
+ this._linearAcceleration.start();
277
+ this._gyroscope.addEventListener('reading', this._gyroscopeReading);
278
+ this._gyroscope.start();
279
+ }
280
+ catch (err) {
281
+ window.addEventListener("devicemotion", this._deviceMotionListener, false);
282
+ }
225
283
  }
226
284
  _stopDeviceMotion() {
285
+ if (this._accelerometer) {
286
+ this._accelerometer.stop();
287
+ this._accelerometer.removeEventListener('reading', this._accelerometerReading);
288
+ delete this._accelerometer;
289
+ }
290
+ if (this._linearAcceleration) {
291
+ this._linearAcceleration.stop();
292
+ this._linearAcceleration.removeEventListener('reading', this._linearAccelerationReading);
293
+ delete this._linearAcceleration;
294
+ }
295
+ if (this._gyroscope) {
296
+ this._gyroscope.stop();
297
+ this._gyroscope.removeEventListener('reading', this._gyroscopeReading);
298
+ delete this._gyroscope;
299
+ }
227
300
  window.removeEventListener("devicemotion", this._deviceMotionListener);
228
301
  }
229
302
  uploadGL(info) {
package/lib/native.js CHANGED
@@ -12,7 +12,7 @@ import { zappar_client } from "./gen/zappar-client";
12
12
  import { drawPlane } from "./drawplane";
13
13
  import { cameraRotationForScreenOrientation, projectionMatrix } from "./cameramodel";
14
14
  import { mat4, vec3 } from "gl-matrix";
15
- import { launchWorker, messageManager } from "./worker-client";
15
+ import { launchCeresWorker, launchWorker, messageManager } from "./worker-client";
16
16
  import { permissionRequestUI, permissionGrantedAll, permissionGrantedCamera, permissionGrantedMotion, permissionDeniedAny, permissionDeniedCamera, permissionDeniedMotion, permissionRequestMotion, permissionRequestCamera, permissionRequestAll, permissionDeniedUI } from "./permission";
17
17
  import { createFaceMesh, destroyFaceMesh, getFaceMesh } from "./facemesh";
18
18
  import { Pipeline, applyScreenCounterRotation } from "./pipeline";
@@ -79,6 +79,10 @@ export function initialize(opts) {
79
79
  src.streamEnded();
80
80
  break;
81
81
  }
82
+ case "setupCeresWorker": {
83
+ launchCeresWorker(msg.port);
84
+ break;
85
+ }
82
86
  case "licerr": {
83
87
  let div = document.createElement("div");
84
88
  div.innerHTML = "Visit <a href='https://docs.zap.works/universal-ar/licensing/' style='color: white;'>our licensing page</a> to find out about hosting on your own domain.";
package/lib/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "3.0.0-beta.6";
1
+ export declare const VERSION = "3.0.1-alpha.1";
package/lib/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "3.0.0-beta.6";
1
+ export const VERSION = "3.0.1-alpha.1";
@@ -1,3 +1,4 @@
1
1
  import { MsgManager } from "./messages";
2
2
  export declare let messageManager: MsgManager;
3
3
  export declare function launchWorker(worker?: Worker): Promise<void>;
4
+ export declare function launchCeresWorker(port: MessagePort, worker?: Worker): Promise<void>;
@@ -9,13 +9,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import { MsgManager } from "./messages";
11
11
  export let messageManager = new MsgManager();
12
+ const wasmModule = (() => __awaiter(void 0, void 0, void 0, function* () {
13
+ const response = yield fetch(new URL("./zappar-cv.wasm", import.meta.url).toString());
14
+ const module = yield WebAssembly.compileStreaming(response);
15
+ return module;
16
+ }))();
12
17
  export function launchWorker(worker) {
13
18
  return __awaiter(this, void 0, void 0, function* () {
14
19
  if (!worker)
15
20
  worker = new Worker(new URL("./worker", import.meta.url), { type: 'module' });
16
21
  worker.postMessage({
17
22
  t: "wasm",
18
- url: new URL("./zappar-cv.wasm", import.meta.url).toString()
23
+ url: new URL("./zappar-cv.wasm", import.meta.url).toString(),
24
+ module: yield wasmModule
19
25
  });
20
26
  yield waitForLoad(worker);
21
27
  function sendOutgoing() {
@@ -31,6 +37,18 @@ export function launchWorker(worker) {
31
37
  });
32
38
  });
33
39
  }
40
+ export function launchCeresWorker(port, worker) {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ if (!worker)
43
+ worker = new Worker(new URL("./ceres-worker", import.meta.url), { type: 'module' });
44
+ worker.postMessage({
45
+ t: "wasm",
46
+ url: new URL("./zappar-cv.wasm", import.meta.url).toString(),
47
+ module: yield wasmModule,
48
+ port
49
+ }, [port]);
50
+ });
51
+ }
34
52
  function waitForLoad(w) {
35
53
  return new Promise(resolve => {
36
54
  let listener = (msg) => {
@@ -0,0 +1,10 @@
1
+ import type * as ZNM from "./zappar-cv";
2
+ export interface WorkerMessageAPI {
3
+ worker_message_send_count: () => number;
4
+ worker_message_send_clear: () => void;
5
+ worker_message_send_data_size: (index: number) => number;
6
+ worker_message_send_data: (index: number) => number;
7
+ worker_message_send_reference: (index: number) => number;
8
+ worker_message_receive: (ref: number, size: number, data: number) => void;
9
+ }
10
+ export declare function getWorkerMessageAPI(mod: ZNM.Module): WorkerMessageAPI;
@@ -0,0 +1,10 @@
1
+ export function getWorkerMessageAPI(mod) {
2
+ return {
3
+ worker_message_send_count: mod.cwrap("worker_message_send_count", "number", []),
4
+ worker_message_send_clear: mod.cwrap("worker_message_send_clear", null, []),
5
+ worker_message_send_data_size: mod.cwrap("worker_message_send_data_size", "number", ["number"]),
6
+ worker_message_send_data: mod.cwrap("worker_message_send_data", "number", ["number"]),
7
+ worker_message_send_reference: mod.cwrap("worker_message_send_reference", "number", ["number"]),
8
+ worker_message_receive: mod.cwrap("worker_message_receive", null, ["number", "number", "number"]),
9
+ };
10
+ }
@@ -1,3 +1,3 @@
1
1
  import { MsgManager } from "./messages";
2
2
  export declare let messageManager: MsgManager;
3
- export declare function launchWorkerServer(wasmUrl: string): Promise<void>;
3
+ export declare function launchWorkerServer(wasmUrl: string, module: WebAssembly.Module): Promise<void>;
@@ -15,9 +15,11 @@ import { mat4 } from "gl-matrix";
15
15
  import { frame_pixel_format_t } from "./gen/zappar-native";
16
16
  import { handleImageBitmap } from "./worker-imagebitmap";
17
17
  import { profile } from "./profile";
18
+ import { getWorkerMessageAPI } from "./worker-messages";
18
19
  export let messageManager = new MsgManager();
20
+ let workerMessageChannel;
19
21
  let latestCameraToScreenRotation = 0;
20
- export function launchWorkerServer(wasmUrl) {
22
+ export function launchWorkerServer(wasmUrl, module) {
21
23
  return __awaiter(this, void 0, void 0, function* () {
22
24
  let mod = ZNM.default({
23
25
  locateFile: (path, prefix) => {
@@ -26,8 +28,14 @@ export function launchWorkerServer(wasmUrl) {
26
28
  }
27
29
  return prefix + path;
28
30
  },
31
+ instantiateWasm: (imports, successCallback) => {
32
+ const instance = new WebAssembly.Instance(module, imports);
33
+ successCallback(instance);
34
+ return instance.exports;
35
+ },
29
36
  onRuntimeInitialized: () => {
30
37
  let r = getRuntimeObject(mod);
38
+ const workerMessageAPI = getWorkerMessageAPI(mod);
31
39
  let server = new zappar_server(r, (pipelineId, ab) => {
32
40
  messageManager.postOutgoingMessage({
33
41
  p: pipelineId,
@@ -55,6 +63,7 @@ export function launchWorkerServer(wasmUrl) {
55
63
  r.pipeline_frame_update(pipeline);
56
64
  att = r.pipeline_camera_frame_device_attitude(pipeline);
57
65
  server.exploreState();
66
+ handleMessages(mod, workerMessageAPI);
58
67
  }
59
68
  let ret = {
60
69
  token: msgt.token,
@@ -68,7 +77,7 @@ export function launchWorkerServer(wasmUrl) {
68
77
  }
69
78
  case "streamC2S": {
70
79
  let msgt = msg;
71
- consumeStream(mod, r, msgt.s, msgt.p, msgt.userFacing, server, msgt.source).then(() => {
80
+ consumeStream(mod, r, msgt.s, msgt.p, msgt.userFacing, server, msgt.source, workerMessageAPI).then(() => {
72
81
  let m = { t: "streamEndedS2C", p: msgt.p, source: msgt.source };
73
82
  // console.log('Stream ended, requesting new stream');
74
83
  messageManager.postOutgoingMessage(m, []);
@@ -132,7 +141,7 @@ export function launchWorkerServer(wasmUrl) {
132
141
  });
133
142
  }
134
143
  ;
135
- function consumeStream(mod, r, stream, p, userFacing, server, source) {
144
+ function consumeStream(mod, r, stream, p, userFacing, server, source, workerMessageAPI) {
136
145
  return __awaiter(this, void 0, void 0, function* () {
137
146
  while (true) {
138
147
  let reader;
@@ -145,7 +154,7 @@ function consumeStream(mod, r, stream, p, userFacing, server, source) {
145
154
  continue;
146
155
  }
147
156
  try {
148
- yield consumeReader(mod, r, reader, p, userFacing, server, source);
157
+ yield consumeReader(mod, r, reader, p, userFacing, server, source, workerMessageAPI);
149
158
  return;
150
159
  }
151
160
  catch (err) {
@@ -173,8 +182,8 @@ function getFrameOrTimeout(reader) {
173
182
  }
174
183
  const cameraToDeviceTransform = mat4.create();
175
184
  const cameraModel = new Float32Array([300, 300, 160, 120, 0, 0]);
176
- function consumeReader(mod, r, reader, p, userFacing, server, source) {
177
- var _a;
185
+ function consumeReader(mod, r, reader, p, userFacing, server, source, workerMessageAPI) {
186
+ var _a, _b;
178
187
  return __awaiter(this, void 0, void 0, function* () {
179
188
  while (true) {
180
189
  let result = yield getFrameOrTimeout(reader);
@@ -244,10 +253,11 @@ function consumeReader(mod, r, reader, p, userFacing, server, source) {
244
253
  const pipeline = server._pipeline_by_instance.get(p);
245
254
  if (pipeline) {
246
255
  try {
247
- r.pipeline_camera_frame_submit_raw_pointer(pipeline, streamDataBufferPointer, size, framePixelFormatFromFormat(frame.format), width, height, token, cameraToDeviceTransform, latestCameraToScreenRotation, cameraModel, userFacing);
256
+ r.pipeline_camera_frame_submit_raw_pointer(pipeline, streamDataBufferPointer, size, framePixelFormatFromFormat(frame.format), width, height, token, cameraToDeviceTransform, latestCameraToScreenRotation, cameraModel, userFacing, (_b = frame.timestamp) !== null && _b !== void 0 ? _b : -1);
257
+ handleMessages(mod, workerMessageAPI);
248
258
  }
249
259
  catch (err) {
250
- // console.log('Exception during camera processing', err);
260
+ console.log('Exception during camera processing', err);
251
261
  // const func = mod.cwrap('what_to_stderr', 'number', ['number']);
252
262
  // func(err);
253
263
  // (mod as any).ccall('what_to_stderr', 'number', ['number'], [err]);
@@ -281,3 +291,34 @@ function framePixelFormatFromFormat(f) {
281
291
  }
282
292
  return frame_pixel_format_t.FRAME_PIXEL_FORMAT_Y;
283
293
  }
294
+ function handleMessages(mod, m) {
295
+ const toSend = m.worker_message_send_count();
296
+ if (toSend === 0)
297
+ return;
298
+ if (!workerMessageChannel) {
299
+ workerMessageChannel = new MessageChannel();
300
+ workerMessageChannel.port1.start();
301
+ workerMessageChannel.port1.addEventListener('message', evt => {
302
+ if (evt.data.t !== 'msgrec')
303
+ return;
304
+ const data = evt.data.data;
305
+ const ptr = mod._malloc(data.byteLength);
306
+ mod.HEAPU8.set(data, ptr);
307
+ m.worker_message_receive(evt.data.reference, data.byteLength, ptr);
308
+ mod._free(ptr);
309
+ });
310
+ messageManager.postOutgoingMessage({ t: 'setupCeresWorker', port: workerMessageChannel.port2 }, [workerMessageChannel.port2]);
311
+ }
312
+ for (let i = 0; i < toSend; i++) {
313
+ const reference = m.worker_message_send_reference(i);
314
+ const dataSize = m.worker_message_send_data_size(i);
315
+ const dataPtr = m.worker_message_send_data(i);
316
+ const data = mod.HEAPU8.slice(dataPtr, dataPtr + dataSize);
317
+ workerMessageChannel.port1.postMessage({
318
+ t: 'msgsend',
319
+ data,
320
+ reference
321
+ }, [data.buffer]);
322
+ }
323
+ m.worker_message_send_clear();
324
+ }
package/lib/worker.js CHANGED
@@ -9,7 +9,7 @@ messageManager.onOutgoingMessage.bind(() => {
9
9
  let launchHandler = (evt) => {
10
10
  if (evt && evt.data && evt.data.t === "wasm") {
11
11
  let url = location.href.startsWith("blob") ? evt.data.url : new URL("./zappar-cv.wasm", import.meta.url).toString();
12
- launchWorkerServer(url);
12
+ launchWorkerServer(url, evt.data.module);
13
13
  ctx.removeEventListener("message", launchHandler);
14
14
  }
15
15
  };