@zappar/zappar-cv 0.4.0-beta.4 → 2.0.0-beta.0

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.
Files changed (100) hide show
  1. package/CHANGELOG.md +91 -34
  2. package/README.md +2 -2
  3. package/lib/additional.d.ts +2 -1
  4. package/lib/additional.js +1 -2
  5. package/lib/camera-source-map.d.ts +10 -0
  6. package/lib/camera-source-map.js +32 -0
  7. package/lib/camera-source.d.ts +2 -5
  8. package/lib/camera-source.js +18 -32
  9. package/lib/cameramodel.js +18 -23
  10. package/lib/compatibility.js +2 -4
  11. package/lib/deserializer.js +1 -5
  12. package/lib/direct.js +12 -16
  13. package/lib/drawcamera.js +27 -31
  14. package/lib/drawface.js +5 -9
  15. package/lib/drawfaceproject.js +5 -9
  16. package/lib/drawplane.js +6 -11
  17. package/lib/event.js +5 -13
  18. package/lib/facelandmark.d.ts +1 -2
  19. package/lib/facelandmark.js +11 -18
  20. package/lib/facelandmarkdata.js +1 -4
  21. package/lib/facelandmarkinterface.js +1 -2
  22. package/lib/facemesh.js +10 -17
  23. package/lib/gen/zappar-client.js +11 -25
  24. package/lib/gen/zappar-cwrap.js +38 -33
  25. package/lib/gen/zappar-native.d.ts +11 -1
  26. package/lib/gen/zappar-native.js +20 -11
  27. package/lib/gen/zappar-server.js +6 -14
  28. package/lib/gen/zappar.d.ts +12 -0
  29. package/lib/gen/zappar.js +5 -11
  30. package/lib/gfx.d.ts +1 -0
  31. package/lib/gfx.js +1 -0
  32. package/lib/gl-state-manager.d.ts +9 -0
  33. package/lib/gl-state-manager.js +31 -0
  34. package/lib/html-element-source.d.ts +3 -20
  35. package/lib/html-element-source.js +39 -287
  36. package/lib/image-process-gl.d.ts +37 -0
  37. package/lib/image-process-gl.js +306 -0
  38. package/lib/imagebitmap-camera-source.d.ts +36 -0
  39. package/lib/imagebitmap-camera-source.js +273 -0
  40. package/lib/imagetracker.d.ts +28 -0
  41. package/lib/imagetracker.js +115 -0
  42. package/lib/index-standalone.d.ts +3 -0
  43. package/lib/index-standalone.js +5 -0
  44. package/lib/index.d.ts +2 -1
  45. package/lib/index.js +6 -27
  46. package/lib/landmarkdatagenerate.js +25 -30
  47. package/lib/loglevel.js +9 -16
  48. package/lib/messages.js +4 -8
  49. package/lib/mstp-camera-source.d.ts +29 -0
  50. package/lib/mstp-camera-source.js +226 -0
  51. package/lib/native.d.ts +2 -1
  52. package/lib/native.js +229 -130
  53. package/lib/options.d.ts +3 -0
  54. package/lib/options.js +1 -0
  55. package/lib/permission.js +13 -27
  56. package/lib/pipeline.d.ts +14 -5
  57. package/lib/pipeline.js +125 -61
  58. package/lib/profile.d.ts +2 -0
  59. package/lib/profile.js +59 -53
  60. package/lib/riff-reader.d.ts +18 -0
  61. package/lib/riff-reader.js +52 -0
  62. package/lib/sequencerecorder.d.ts +1 -0
  63. package/lib/sequencerecorder.js +17 -11
  64. package/lib/sequencesource.d.ts +7 -1
  65. package/lib/sequencesource.js +102 -64
  66. package/lib/serializer.js +1 -5
  67. package/lib/shader.js +2 -7
  68. package/lib/source.d.ts +6 -4
  69. package/lib/source.js +1 -5
  70. package/lib/version.d.ts +1 -1
  71. package/lib/version.js +1 -4
  72. package/lib/worker-client.d.ts +1 -1
  73. package/lib/worker-client.js +9 -26
  74. package/lib/worker-imagebitmap.d.ts +5 -0
  75. package/lib/worker-imagebitmap.js +59 -0
  76. package/lib/worker-server.d.ts +0 -5
  77. package/lib/worker-server.js +133 -41
  78. package/lib/worker.js +6 -10
  79. package/lib/workerinterface.d.ts +47 -1
  80. package/lib/workerinterface.js +1 -2
  81. package/lib/zcv.js +117 -135
  82. package/lib/zcv.wasm +0 -0
  83. package/package.json +33 -32
  84. package/umd/169.zappar-cv.js +1 -0
  85. package/umd/{482727c0e7dd40d73f5aae47f238ad61.zbin → 482727c0e7dd40d73f5a.zbin} +0 -0
  86. package/umd/6a8309f88f836a752c68.wasm +0 -0
  87. package/umd/752.zappar-cv.js +1 -0
  88. package/umd/{b4f46e148a3b3b5aef90717d8d60e3fc.zbin → b4f46e148a3b3b5aef90.zbin} +0 -0
  89. package/umd/{f7b62a3a53b5c9b3222ba8dc53cb8b11.zbin → f7b62a3a53b5c9b3222b.zbin} +0 -0
  90. package/umd/zappar-cv.js +1 -9
  91. package/umd/zappar-cv.worker.js +1 -1
  92. package/lib/bundleworker-mainthread.d.ts +0 -2
  93. package/lib/bundleworker-mainthread.js +0 -36
  94. package/lib/bundleworker-worker.d.ts +0 -6
  95. package/lib/bundleworker-worker.js +0 -34
  96. package/lib/zcv-slam.js +0 -16
  97. package/lib/zcv-slam.wasm +0 -0
  98. package/umd/1ec038c18c27f4adf35917a8175401f0.wasm +0 -0
  99. package/umd/9c0b0dcf514b48c05541.bundleworker.js +0 -1
  100. package/umd/d115e485cffe500afc4f7c1272ea8f62.wasm +0 -0
@@ -0,0 +1,115 @@
1
+ import { RiffReader } from "./riff-reader";
2
+ let byId = new Map();
3
+ const decoder = new TextDecoder();
4
+ export class ImageTracker {
5
+ constructor(_client, _impl) {
6
+ this._client = _client;
7
+ this._impl = _impl;
8
+ this._targets = [];
9
+ }
10
+ static create(pipeline, client) {
11
+ let ret = client.image_tracker_create(pipeline);
12
+ byId.set(ret, new ImageTracker(client, ret));
13
+ return ret;
14
+ }
15
+ static get(p) {
16
+ return byId.get(p);
17
+ }
18
+ destroy() {
19
+ this._client.image_tracker_destroy(this._impl);
20
+ byId.delete(this._impl);
21
+ }
22
+ loadFromMemory(data) {
23
+ this._targets.push({ data });
24
+ this._client.image_tracker_target_load_from_memory(this._impl, data);
25
+ }
26
+ targetCount() {
27
+ return this._targets.length;
28
+ }
29
+ getTargetInfo(i) {
30
+ let current = this._targets[i];
31
+ if (current && current.info)
32
+ return current.info;
33
+ current.info = {
34
+ topRadius: -1,
35
+ bottomRadius: -1,
36
+ sideLength: -1,
37
+ physicalScaleFactor: -1
38
+ };
39
+ try {
40
+ const reader = new RiffReader(current.data, false);
41
+ const imgChunk = reader.find("IMG ");
42
+ if (imgChunk) {
43
+ let mimeType = "image/png";
44
+ const mimeTypeChunk = reader.find("IMGM");
45
+ if (mimeTypeChunk)
46
+ mimeType = decoder.decode(mimeTypeChunk.data);
47
+ current.info.preview = { mimeType, compressed: imgChunk.data };
48
+ }
49
+ const odleChunk = reader.find("ODLE");
50
+ if (odleChunk) {
51
+ const odle = decoder.decode(odleChunk.data);
52
+ this._parseOdle(odle, current.info);
53
+ }
54
+ }
55
+ catch (err) { }
56
+ return current.info;
57
+ }
58
+ _parseOdle(data, info) {
59
+ let currentOffset = 0;
60
+ let version = "0";
61
+ [version, currentOffset] = readLine(currentOffset, data);
62
+ if (version === "3") {
63
+ let treeOrFlat = "0";
64
+ [treeOrFlat, currentOffset] = readLine(currentOffset, data);
65
+ if (treeOrFlat !== "0" && treeOrFlat !== "1")
66
+ return;
67
+ let numberTargets = "0";
68
+ [numberTargets, currentOffset] = readLine(currentOffset, data);
69
+ const parsedTargets = parseInt(numberTargets);
70
+ if (isNaN(parsedTargets) || parsedTargets < 1)
71
+ return;
72
+ let emptyLine = "";
73
+ [emptyLine, currentOffset] = readLine(currentOffset, data);
74
+ if (emptyLine.length !== 0)
75
+ return;
76
+ let infoLine = "";
77
+ [infoLine, currentOffset] = readLine(currentOffset, data);
78
+ const infoLineParts = infoLine.split(" ");
79
+ if (infoLineParts.length < 7)
80
+ return;
81
+ info.physicalScaleFactor = parseFloat(infoLineParts[6]);
82
+ if (isNaN(info.physicalScaleFactor))
83
+ info.physicalScaleFactor = -1;
84
+ if (infoLineParts.length >= 8) {
85
+ info.topRadius = parseFloat(infoLineParts[7]);
86
+ if (isNaN(info.topRadius))
87
+ info.topRadius = -1;
88
+ info.bottomRadius = info.topRadius;
89
+ }
90
+ if (infoLineParts.length >= 9) {
91
+ info.bottomRadius = parseFloat(infoLineParts[8]);
92
+ if (isNaN(info.bottomRadius))
93
+ info.bottomRadius = -1;
94
+ }
95
+ if (infoLineParts.length >= 10) {
96
+ info.sideLength = parseFloat(infoLineParts[9]);
97
+ if (isNaN(info.sideLength))
98
+ info.sideLength = -1;
99
+ }
100
+ }
101
+ }
102
+ getDecodedPreview(i) {
103
+ const info = this.getTargetInfo(i);
104
+ if (!info.preview)
105
+ return undefined;
106
+ const blob = new Blob([info.preview.compressed], { type: info.preview.mimeType });
107
+ const image = new Image();
108
+ image.src = URL.createObjectURL(blob);
109
+ return image;
110
+ }
111
+ }
112
+ function readLine(offset, str) {
113
+ let indx = str.indexOf("\n", offset);
114
+ return [str.substring(offset, indx >= 0 ? indx : undefined).replace("\r", ""), indx + 1];
115
+ }
@@ -0,0 +1,3 @@
1
+ import { Options } from "./options";
2
+ export * from "./index";
3
+ export declare function initialize(opts?: Options): import(".").Zappar;
@@ -0,0 +1,5 @@
1
+ import { initialize as init } from "./index";
2
+ export * from "./index";
3
+ export function initialize(opts) {
4
+ return init(Object.assign(Object.assign({}, opts), { worker: (opts === null || opts === void 0 ? void 0 : opts.worker) || new (require("worker-loader?inline=fallback!./worker").default)() }));
5
+ }
package/lib/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { zappar } from "./gen/zappar";
2
2
  import { Additional } from "./additional";
3
+ import { Options } from "./options";
3
4
  export declare type Zappar = zappar & Additional;
4
- export declare function initialize(): Zappar;
5
+ export declare function initialize(opts?: Options): Zappar;
5
6
  export { zappar_image_tracker_t, zappar_instant_world_tracker_t, zappar_barcode_finder_t, zappar_face_tracker_t, zappar_face_landmark_t, barcode_format_t, face_landmark_name_t, instant_world_tracker_transform_orientation_t, log_level_t, zappar_face_mesh_t, zappar_pipeline_t, zappar_camera_source_t, zappar_sequence_source_t } from "./gen/zappar";
package/lib/index.js CHANGED
@@ -1,28 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.log_level_t = exports.instant_world_tracker_transform_orientation_t = exports.face_landmark_name_t = exports.barcode_format_t = exports.initialize = void 0;
4
- if (typeof Z_STANDALONE !== 'undefined' && Z_STANDALONE) {
5
- if (document.currentScript) {
6
- const url = new URL(document.currentScript.src);
7
- let href = url.toString();
8
- if (url.pathname) {
9
- let pathParts = href.split("/");
10
- pathParts.pop();
11
- href = pathParts.join("/") + "/";
12
- }
13
- __webpack_public_path__ = href;
14
- }
1
+ import { initialize as nativeInitialize } from "./native";
2
+ import { VERSION } from "./version";
3
+ export function initialize(opts) {
4
+ console.log(`Zappar CV v${VERSION}`);
5
+ return nativeInitialize(opts);
15
6
  }
16
- ////
17
- const native_1 = require("./native");
18
- const version_1 = require("./version");
19
- function initialize() {
20
- console.log(`Zappar CV v${version_1.VERSION}`);
21
- return native_1.initialize();
22
- }
23
- exports.initialize = initialize;
24
- var zappar_1 = require("./gen/zappar");
25
- Object.defineProperty(exports, "barcode_format_t", { enumerable: true, get: function () { return zappar_1.barcode_format_t; } });
26
- Object.defineProperty(exports, "face_landmark_name_t", { enumerable: true, get: function () { return zappar_1.face_landmark_name_t; } });
27
- Object.defineProperty(exports, "instant_world_tracker_transform_orientation_t", { enumerable: true, get: function () { return zappar_1.instant_world_tracker_transform_orientation_t; } });
28
- Object.defineProperty(exports, "log_level_t", { enumerable: true, get: function () { return zappar_1.log_level_t; } });
7
+ export { barcode_format_t, face_landmark_name_t, instant_world_tracker_transform_orientation_t, log_level_t } from "./gen/zappar";
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
2
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
3
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,32 +7,30 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
8
  });
10
9
  };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.generateDataCPP = exports.generateDataJSON = void 0;
13
- const facemesh_1 = require("./facemesh");
14
- const zappar_1 = require("./gen/zappar");
10
+ import { FaceMesh } from "./facemesh";
11
+ import { face_landmark_name_t } from "./gen/zappar";
15
12
  let vertexIndexByName = new Map([
16
- [zappar_1.face_landmark_name_t.EAR_LEFT, 888],
17
- [zappar_1.face_landmark_name_t.EAR_RIGHT, 467],
18
- [zappar_1.face_landmark_name_t.EYE_LEFT, [1076, 1062]],
19
- [zappar_1.face_landmark_name_t.EYE_RIGHT, [1094, 1108]],
20
- [zappar_1.face_landmark_name_t.NOSE_BRIDGE, 36],
21
- [zappar_1.face_landmark_name_t.NOSE_BASE, 3],
22
- [zappar_1.face_landmark_name_t.NOSE_TIP, 8],
23
- [zappar_1.face_landmark_name_t.LIP_TOP, 24],
24
- [zappar_1.face_landmark_name_t.LIP_BOTTOM, 25],
25
- [zappar_1.face_landmark_name_t.MOUTH_CENTER, [24, 25]],
26
- [zappar_1.face_landmark_name_t.CHIN, 1049],
27
- [zappar_1.face_landmark_name_t.EYEBROW_LEFT, 657],
28
- [zappar_1.face_landmark_name_t.EYEBROW_RIGHT, 210],
13
+ [face_landmark_name_t.EAR_LEFT, 888],
14
+ [face_landmark_name_t.EAR_RIGHT, 467],
15
+ [face_landmark_name_t.EYE_LEFT, [1076, 1062]],
16
+ [face_landmark_name_t.EYE_RIGHT, [1094, 1108]],
17
+ [face_landmark_name_t.NOSE_BRIDGE, 36],
18
+ [face_landmark_name_t.NOSE_BASE, 3],
19
+ [face_landmark_name_t.NOSE_TIP, 8],
20
+ [face_landmark_name_t.LIP_TOP, 24],
21
+ [face_landmark_name_t.LIP_BOTTOM, 25],
22
+ [face_landmark_name_t.MOUTH_CENTER, [24, 25]],
23
+ [face_landmark_name_t.CHIN, 1049],
24
+ [face_landmark_name_t.EYEBROW_LEFT, 657],
25
+ [face_landmark_name_t.EYEBROW_RIGHT, 210],
29
26
  ]);
30
- function generateDataJSON() {
27
+ export function generateDataJSON() {
31
28
  return __awaiter(this, void 0, void 0, function* () {
32
- let mesh = new facemesh_1.FaceMesh();
33
- let url = require("file-loader!./face_mesh_face_model.zbin").default;
34
- let req = yield fetch(url);
29
+ let mesh = new FaceMesh();
30
+ let url = new URL("./face_mesh_face_model.zbin", import.meta.url);
31
+ let req = yield fetch(url.toString());
35
32
  mesh.loadFromMemory(yield req.arrayBuffer(), false, false, false, false);
36
- let names = new Set([zappar_1.face_landmark_name_t.EYE_LEFT, zappar_1.face_landmark_name_t.EYE_RIGHT, zappar_1.face_landmark_name_t.EAR_LEFT, zappar_1.face_landmark_name_t.EAR_RIGHT, zappar_1.face_landmark_name_t.NOSE_BRIDGE, zappar_1.face_landmark_name_t.NOSE_TIP, zappar_1.face_landmark_name_t.NOSE_BASE, zappar_1.face_landmark_name_t.LIP_TOP, zappar_1.face_landmark_name_t.LIP_BOTTOM, zappar_1.face_landmark_name_t.MOUTH_CENTER, zappar_1.face_landmark_name_t.CHIN, zappar_1.face_landmark_name_t.EYEBROW_LEFT, zappar_1.face_landmark_name_t.EYEBROW_RIGHT]);
33
+ let names = new Set([face_landmark_name_t.EYE_LEFT, face_landmark_name_t.EYE_RIGHT, face_landmark_name_t.EAR_LEFT, face_landmark_name_t.EAR_RIGHT, face_landmark_name_t.NOSE_BRIDGE, face_landmark_name_t.NOSE_TIP, face_landmark_name_t.NOSE_BASE, face_landmark_name_t.LIP_TOP, face_landmark_name_t.LIP_BOTTOM, face_landmark_name_t.MOUTH_CENTER, face_landmark_name_t.CHIN, face_landmark_name_t.EYEBROW_LEFT, face_landmark_name_t.EYEBROW_RIGHT]);
37
34
  let data = {};
38
35
  for (let n of names) {
39
36
  let indices = vertexIndexByName.get(n);
@@ -55,17 +52,16 @@ function generateDataJSON() {
55
52
  });
56
53
  });
57
54
  }
58
- exports.generateDataJSON = generateDataJSON;
59
55
  function arrayAsCPPLiteral(a) {
60
56
  return `{ ${a.join(", ")} }`;
61
57
  }
62
- function generateDataCPP() {
58
+ export function generateDataCPP() {
63
59
  return __awaiter(this, void 0, void 0, function* () {
64
- let mesh = new facemesh_1.FaceMesh();
65
- let url = require("file-loader!./face_mesh_face_model.zbin").default;
66
- let req = yield fetch(url);
60
+ let mesh = new FaceMesh();
61
+ let url = new URL("./face_mesh_face_model.zbin", import.meta.url);
62
+ let req = yield fetch(url.toString());
67
63
  mesh.loadFromMemory(yield req.arrayBuffer(), false, false, false, false);
68
- let names = [zappar_1.face_landmark_name_t.EYE_LEFT, zappar_1.face_landmark_name_t.EYE_RIGHT, zappar_1.face_landmark_name_t.EAR_LEFT, zappar_1.face_landmark_name_t.EAR_RIGHT, zappar_1.face_landmark_name_t.NOSE_BRIDGE, zappar_1.face_landmark_name_t.NOSE_TIP, zappar_1.face_landmark_name_t.NOSE_BASE, zappar_1.face_landmark_name_t.LIP_TOP, zappar_1.face_landmark_name_t.LIP_BOTTOM, zappar_1.face_landmark_name_t.MOUTH_CENTER, zappar_1.face_landmark_name_t.CHIN, zappar_1.face_landmark_name_t.EYEBROW_LEFT, zappar_1.face_landmark_name_t.EYEBROW_RIGHT];
64
+ let names = [face_landmark_name_t.EYE_LEFT, face_landmark_name_t.EYE_RIGHT, face_landmark_name_t.EAR_LEFT, face_landmark_name_t.EAR_RIGHT, face_landmark_name_t.NOSE_BRIDGE, face_landmark_name_t.NOSE_TIP, face_landmark_name_t.NOSE_BASE, face_landmark_name_t.LIP_TOP, face_landmark_name_t.LIP_BOTTOM, face_landmark_name_t.MOUTH_CENTER, face_landmark_name_t.CHIN, face_landmark_name_t.EYEBROW_LEFT, face_landmark_name_t.EYEBROW_RIGHT];
69
65
  let output = "std::vector<std::vector<std::vector<float> > > _zappar_landmark_means = {\n";
70
66
  output += names.map(val => {
71
67
  let indices = vertexIndexByName.get(val);
@@ -99,4 +95,3 @@ function generateDataCPP() {
99
95
  return output;
100
96
  });
101
97
  }
102
- exports.generateDataCPP = generateDataCPP;
package/lib/loglevel.js CHANGED
@@ -1,24 +1,17 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.zcwarn = exports.zcerr = exports.zcout = exports.setLogLevel = void 0;
4
- const zappar_1 = require("./gen/zappar");
5
- let logLevel = zappar_1.log_level_t.LOG_LEVEL_ERROR;
6
- function setLogLevel(l) {
1
+ import { log_level_t } from "./gen/zappar";
2
+ let logLevel = log_level_t.LOG_LEVEL_ERROR;
3
+ export function setLogLevel(l) {
7
4
  logLevel = l;
8
5
  }
9
- exports.setLogLevel = setLogLevel;
10
- function zcout(...args) {
11
- if (logLevel >= zappar_1.log_level_t.LOG_LEVEL_VERBOSE)
6
+ export function zcout(...args) {
7
+ if (logLevel >= log_level_t.LOG_LEVEL_VERBOSE)
12
8
  console.log("[Zappar] INFO", ...args);
13
9
  }
14
- exports.zcout = zcout;
15
- function zcerr(...args) {
16
- if (logLevel >= zappar_1.log_level_t.LOG_LEVEL_ERROR)
10
+ export function zcerr(...args) {
11
+ if (logLevel >= log_level_t.LOG_LEVEL_ERROR)
17
12
  console.error("[Zappar] ERROR", ...args);
18
13
  }
19
- exports.zcerr = zcerr;
20
- function zcwarn(...args) {
21
- if (logLevel >= zappar_1.log_level_t.LOG_LEVEL_VERBOSE)
14
+ export function zcwarn(...args) {
15
+ if (logLevel >= log_level_t.LOG_LEVEL_VERBOSE)
22
16
  console.log("[Zappar] WARN", ...args);
23
17
  }
24
- exports.zcwarn = zcwarn;
package/lib/messages.js CHANGED
@@ -1,11 +1,8 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MsgManager = void 0;
4
- const event_1 = require("./event");
5
- class MsgManager {
1
+ import { Event, Event1 } from "./event";
2
+ export class MsgManager {
6
3
  constructor() {
7
- this.onOutgoingMessage = new event_1.Event();
8
- this.onIncomingMessage = new event_1.Event1();
4
+ this.onOutgoingMessage = new Event();
5
+ this.onIncomingMessage = new Event1();
9
6
  this._outgoingMessages = [];
10
7
  }
11
8
  postIncomingMessage(msg) {
@@ -24,4 +21,3 @@ class MsgManager {
24
21
  return ret;
25
22
  }
26
23
  }
27
- exports.MsgManager = MsgManager;
@@ -0,0 +1,29 @@
1
+ import { zappar_camera_source_t, zappar_pipeline_t } from "./gen/zappar";
2
+ import { CameraFrameInfo, Source } from "./source";
3
+ export declare class MSTPCameraSource extends Source {
4
+ private _impl;
5
+ private _pipeline;
6
+ private _deviceId;
7
+ static USER_DEFAULT_DEVICE_ID: string;
8
+ static DEFAULT_DEVICE_ID: string;
9
+ private _currentStream;
10
+ private _activeDeviceId;
11
+ private _isPaused;
12
+ private _isUserFacing;
13
+ private _cameraToScreenRotation;
14
+ constructor(_impl: zappar_camera_source_t, _pipeline: zappar_pipeline_t, _deviceId: string);
15
+ destroy(): void;
16
+ private _stop;
17
+ pause(): void;
18
+ start(): void;
19
+ private _getConstraints;
20
+ getFrame(allowRead: boolean): void;
21
+ private _getUserMedia;
22
+ private _syncCamera;
23
+ private _hasStartedOrientation;
24
+ private _deviceMotionListener;
25
+ private _startDeviceOrientation;
26
+ private _startDeviceMotion;
27
+ private _stopDeviceMotion;
28
+ uploadGL(info: CameraFrameInfo): void;
29
+ }
@@ -0,0 +1,226 @@
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 { profile } from "./profile";
11
+ import { Pipeline } from "./pipeline";
12
+ import { Source } from "./source";
13
+ import { cameraRotationForScreenOrientation } from "./cameramodel";
14
+ import { zcout } from "./loglevel";
15
+ import { deleteCameraSource } from "./camera-source-map";
16
+ export class MSTPCameraSource extends Source {
17
+ constructor(_impl, _pipeline, _deviceId) {
18
+ super();
19
+ this._impl = _impl;
20
+ this._pipeline = _pipeline;
21
+ this._deviceId = _deviceId;
22
+ this._currentStream = null;
23
+ this._activeDeviceId = null;
24
+ this._isPaused = true;
25
+ this._isUserFacing = false;
26
+ this._cameraToScreenRotation = 0;
27
+ this._hasStartedOrientation = false;
28
+ this._deviceMotionListener = (ev) => {
29
+ let pipeline = Pipeline.get(this._pipeline);
30
+ if (!pipeline)
31
+ return;
32
+ let timeStamp = (ev.timeStamp !== undefined && ev.timeStamp !== null) ? ev.timeStamp : performance.now();
33
+ if (ev.accelerationIncludingGravity !== null &&
34
+ ev.accelerationIncludingGravity.x !== null &&
35
+ ev.accelerationIncludingGravity.y !== null &&
36
+ ev.accelerationIncludingGravity.z !== null) {
37
+ pipeline.motionAccelerometerSubmit(timeStamp, ev.accelerationIncludingGravity.x * profile.deviceMotionMutliplier, ev.accelerationIncludingGravity.y * profile.deviceMotionMutliplier, ev.accelerationIncludingGravity.z * profile.deviceMotionMutliplier);
38
+ }
39
+ if (ev.rotationRate !== null &&
40
+ ev.rotationRate.alpha !== null &&
41
+ ev.rotationRate.beta !== null &&
42
+ ev.rotationRate.gamma !== null && !this._hasStartedOrientation) {
43
+ ev.timeStamp;
44
+ pipeline.motionRotationRateSubmit(timeStamp, ev.rotationRate.alpha * Math.PI / -180.0, ev.rotationRate.beta * Math.PI / -180.0, ev.rotationRate.gamma * Math.PI / -180.0);
45
+ }
46
+ else if (!this._hasStartedOrientation) {
47
+ this._startDeviceOrientation();
48
+ }
49
+ };
50
+ zcout("Using MSTP camera source");
51
+ }
52
+ destroy() {
53
+ this.pause();
54
+ deleteCameraSource(this._impl);
55
+ }
56
+ _stop() {
57
+ if (!this._currentStream)
58
+ return;
59
+ let tracks = this._currentStream.getTracks();
60
+ tracks.forEach(t => t.stop());
61
+ this._currentStream = null;
62
+ }
63
+ pause() {
64
+ this._isPaused = true;
65
+ let p = Pipeline.get(this._pipeline);
66
+ if (p && p.currentCameraSource === this)
67
+ p.currentCameraSource = undefined;
68
+ this._stopDeviceMotion();
69
+ this._syncCamera();
70
+ }
71
+ start() {
72
+ var _a;
73
+ let p = Pipeline.get(this._pipeline);
74
+ if (p && p.currentCameraSource !== this) {
75
+ (_a = p.currentCameraSource) === null || _a === void 0 ? void 0 : _a.pause();
76
+ p.currentCameraSource = this;
77
+ }
78
+ this._isPaused = false;
79
+ this._startDeviceMotion();
80
+ this._syncCamera();
81
+ }
82
+ _getConstraints() {
83
+ return __awaiter(this, void 0, void 0, function* () {
84
+ let deviceId;
85
+ let facingMode;
86
+ if (this._deviceId !== MSTPCameraSource.DEFAULT_DEVICE_ID &&
87
+ this._deviceId !== MSTPCameraSource.USER_DEFAULT_DEVICE_ID) {
88
+ // Custom device
89
+ deviceId = this._deviceId;
90
+ }
91
+ else {
92
+ facingMode = (this._deviceId === MSTPCameraSource.DEFAULT_DEVICE_ID) ? "environment" : "user";
93
+ }
94
+ let constraints = {
95
+ audio: false,
96
+ video: {
97
+ facingMode: facingMode,
98
+ width: profile.videoWidth,
99
+ height: profile.videoHeight,
100
+ frameRate: profile.requestHighFrameRate ? 60 : 30,
101
+ deviceId: deviceId
102
+ }
103
+ };
104
+ if (deviceId)
105
+ return constraints;
106
+ if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices)
107
+ return constraints;
108
+ let devices = yield navigator.mediaDevices.enumerateDevices();
109
+ let hasHadCapabilities = false;
110
+ devices = devices.filter(val => {
111
+ // Remove non-video devices
112
+ if (val.kind !== "videoinput")
113
+ return false;
114
+ // If the media info object contains capabilities, use it to filter to the correct facing cameras
115
+ if (val.getCapabilities) {
116
+ hasHadCapabilities = true;
117
+ let capabilities = val.getCapabilities();
118
+ if (capabilities && capabilities.facingMode && capabilities.facingMode.indexOf(facingMode === "user" ? "user" : "environment") < 0)
119
+ return false;
120
+ }
121
+ return true;
122
+ });
123
+ // If none of the devices had capability info, or we have no devices left, fall back to the standard constraints
124
+ if (!hasHadCapabilities || devices.length === 0) {
125
+ return constraints;
126
+ }
127
+ if (typeof constraints.video === "object") {
128
+ zcout("choosing device ID", devices[devices.length - 1].deviceId);
129
+ constraints.video.deviceId = devices[devices.length - 1].deviceId;
130
+ }
131
+ return constraints;
132
+ });
133
+ }
134
+ getFrame(allowRead) {
135
+ var _a, _b;
136
+ let rotation = cameraRotationForScreenOrientation(false);
137
+ if (rotation != this._cameraToScreenRotation) {
138
+ (_b = (_a = Pipeline.get(this._pipeline)) === null || _a === void 0 ? void 0 : _a.sendCameraToScreenRotationToWorker) === null || _b === void 0 ? void 0 : _b.call(_a, rotation);
139
+ this._cameraToScreenRotation = rotation;
140
+ }
141
+ return;
142
+ }
143
+ _getUserMedia() {
144
+ return __awaiter(this, void 0, void 0, function* () {
145
+ let constraints = yield this._getConstraints();
146
+ if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia)
147
+ return yield navigator.mediaDevices.getUserMedia(constraints);
148
+ return yield new Promise((resolve, reject) => {
149
+ navigator.getUserMedia(constraints, resolve, reject);
150
+ });
151
+ });
152
+ }
153
+ _syncCamera() {
154
+ return __awaiter(this, void 0, void 0, function* () {
155
+ if (this._currentStream && this._isPaused) {
156
+ this._stop();
157
+ return;
158
+ }
159
+ if (this._currentStream && this._activeDeviceId !== this._deviceId)
160
+ this._stop();
161
+ if (!this._isPaused) {
162
+ this._activeDeviceId = this._deviceId;
163
+ this._currentStream = yield this._getUserMedia();
164
+ if (this._isPaused) {
165
+ yield this._syncCamera();
166
+ return;
167
+ }
168
+ this._isUserFacing = false;
169
+ if (this._currentStream) {
170
+ let videoTracks = this._currentStream.getVideoTracks();
171
+ if (videoTracks.length > 0) {
172
+ this._isUserFacing = videoTracks[0].getSettings().facingMode === "user";
173
+ let processor = new MediaStreamTrackProcessor({
174
+ track: videoTracks[0]
175
+ });
176
+ let p = Pipeline.get(this._pipeline);
177
+ if (p)
178
+ p.sendCameraStreamToWorker(this._impl, processor.readable, this._isUserFacing);
179
+ }
180
+ }
181
+ }
182
+ });
183
+ }
184
+ _startDeviceOrientation() {
185
+ if (this._hasStartedOrientation)
186
+ return;
187
+ this._hasStartedOrientation = true;
188
+ window.addEventListener("deviceorientation", (ev) => {
189
+ let pipeline = Pipeline.get(this._pipeline);
190
+ if (!pipeline)
191
+ return;
192
+ let timeStamp = (ev.timeStamp !== undefined && ev.timeStamp !== null) ? ev.timeStamp : performance.now();
193
+ if (ev.alpha === null || ev.beta === null || ev.gamma === null)
194
+ return;
195
+ pipeline.motionAttitudeSubmit(timeStamp, ev.alpha, ev.beta, ev.gamma);
196
+ });
197
+ }
198
+ _startDeviceMotion() {
199
+ window.addEventListener("devicemotion", this._deviceMotionListener, false);
200
+ }
201
+ _stopDeviceMotion() {
202
+ window.removeEventListener("devicemotion", this._deviceMotionListener);
203
+ }
204
+ uploadGL(info) {
205
+ const pipeline = Pipeline.get(this._pipeline);
206
+ const gl = pipeline === null || pipeline === void 0 ? void 0 : pipeline.glContext;
207
+ if (!info ||
208
+ info.texture ||
209
+ !info.frame ||
210
+ !pipeline ||
211
+ !gl)
212
+ return;
213
+ let texture = pipeline.getVideoTexture();
214
+ if (!texture)
215
+ return;
216
+ gl.bindTexture(gl.TEXTURE_2D, texture);
217
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
218
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, info.frame);
219
+ gl.bindTexture(gl.TEXTURE_2D, null);
220
+ info.texture = texture;
221
+ info.frame.close();
222
+ delete info.frame;
223
+ }
224
+ }
225
+ MSTPCameraSource.USER_DEFAULT_DEVICE_ID = "Simulated User Default Device ID: a908df7f-5661-4d20-b227-a1c15d2fdb4b";
226
+ MSTPCameraSource.DEFAULT_DEVICE_ID = "Simulated Default Device ID: a908df7f-5661-4d20-b227-a1c15d2fdb4b";
package/lib/native.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  import { Zappar } from ".";
2
- export declare function initialize(): Zappar;
2
+ import { Options } from "./options";
3
+ export declare function initialize(opts?: Options): Zappar;