@wemap/camera 3.2.3

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.
@@ -0,0 +1,79 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>Camera</title>
8
+
9
+ <script src="/js/camera-lib.js"></script>
10
+
11
+ <style type="text/css">
12
+ html {
13
+ height: 100%;
14
+ }
15
+
16
+ body {
17
+ height: 100%;
18
+ margin: 0px;
19
+ }
20
+
21
+ #camera-container {
22
+ width: 100%;
23
+ height: 100%;
24
+ }
25
+
26
+ #logger {
27
+ background: #ffffffaa;
28
+ position: absolute;
29
+ bottom: 0;
30
+ left: 0;
31
+ z-index: 2;
32
+ margin: 5px;
33
+ padding-left: 10px;
34
+ padding-right: 10px;
35
+ min-width: 100px;
36
+ }
37
+ </style>
38
+ </head>
39
+
40
+ <body>
41
+
42
+ <div id="camera-container"></div>
43
+ <div id="logger">
44
+ <div class="title">Logger</div>
45
+ <button id="start">Start</button>
46
+ <button id="stop" disabled>Stop</button>
47
+ </div>
48
+
49
+ <script>
50
+ /* global Camera */
51
+
52
+ const cameraContainer = document.getElementById('camera-container');
53
+ const camera = new Camera(cameraContainer);
54
+
55
+ const SERVER_URL = 'https://localhost:9002';
56
+
57
+ const startButton = document.getElementById('start');
58
+ const stopButton = document.getElementById('stop');
59
+
60
+ startButton.addEventListener('click', () => {
61
+ document.dispatchEvent(new CustomEvent('providers.logger.start', {detail: {
62
+ serverUrl: SERVER_URL,
63
+ logsSize: 300
64
+ }}));
65
+ camera.start();
66
+ startButton.disabled = true;
67
+ stopButton.disabled = false;
68
+ });
69
+
70
+ stopButton.addEventListener('click', () => {
71
+ document.dispatchEvent(new Event('providers.logger.stop'));
72
+ camera.stop();
73
+ startButton.disabled = false;
74
+ stopButton.disabled = true;
75
+ });
76
+ </script>
77
+ </body>
78
+
79
+ </html>
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+
4
+ <head>
5
+ <meta charset="utf-8">
6
+ <meta name="viewport" content="width=device-width, user-scalable=no">
7
+ <title>Camera debug</title>
8
+ </head>
9
+
10
+ <body>
11
+ <a href="./simple.html">Simple</a><br />
12
+ <a href="./camera-logger.html">Camera-logger</a>
13
+ </body>
14
+
15
+ </html>
@@ -0,0 +1,109 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>Camera</title>
8
+
9
+ <script src="/js/camera-lib.js"></script>
10
+
11
+ <style type="text/css">
12
+ html {
13
+ height: 100%;
14
+ }
15
+
16
+ body {
17
+ height: 100%;
18
+ margin: 0px;
19
+ }
20
+
21
+ #camera-container {
22
+ background-color: red;
23
+ }
24
+
25
+ #css-switcher {
26
+ position: absolute;
27
+ top: 0px;
28
+ }
29
+
30
+ #camera-toggle {
31
+ position: absolute;
32
+ top: 30px;
33
+ }
34
+
35
+ #fov-container {
36
+ position: absolute;
37
+ bottom: 0px;
38
+ margin: 5px;
39
+ background-color: white;
40
+ }
41
+
42
+ #camera-container.classic {
43
+ width: 800px;
44
+ height: 600px;
45
+ }
46
+
47
+ #camera-container.full {
48
+ width: 100%;
49
+ height: 100%;
50
+ }
51
+
52
+ #camera-container.wide {
53
+ width: 800px;
54
+ height: 200px;
55
+ }
56
+
57
+ #camera-container.slim {
58
+ width: 200px;
59
+ height: 600px;
60
+ }
61
+ </style>
62
+ </head>
63
+
64
+ <body>
65
+
66
+ <div id="camera-container" class="classic"></div>
67
+ <button id="css-switcher">Switch Camera size</button>
68
+ <button id="camera-toggle">Start</button>
69
+ <div id="fov-container"></div>
70
+
71
+ <script>
72
+ /* global Camera */
73
+
74
+ const cameraContainer = document.getElementById('camera-container');
75
+ const cameraToggle = document.getElementById('camera-toggle');
76
+ const fovContainer = document.getElementById('fov-container');
77
+
78
+ const camera = new Camera(cameraContainer);
79
+
80
+ camera.on('fov.changed', ({
81
+ vertical, horizontal
82
+ }) => (fovContainer.innerHTML = `FOV: ${vertical.toFixed(2)}, ${horizontal.toFixed(2)}`));
83
+
84
+ const format = ['classic', 'full', 'wide', 'slim'];
85
+ document.getElementById('css-switcher').addEventListener('click', () => {
86
+ cameraContainer.className = format[(format.indexOf(cameraContainer.className) + 1) % format.length];
87
+ camera.notifyContainerSizeChanged();
88
+ });
89
+
90
+ cameraToggle.addEventListener('click', () => {
91
+ cameraToggle.disabled = true;
92
+ if (cameraToggle.innerHTML === 'Start') {
93
+ camera.start().then(() => {
94
+ cameraToggle.disabled = false;
95
+ });
96
+ cameraToggle.innerHTML = 'Stop';
97
+ } else {
98
+ camera.stop().then(() => {
99
+ cameraToggle.disabled = false;
100
+ });
101
+ fovContainer.innerHTML = '';
102
+ cameraToggle.innerHTML = 'Start';
103
+ }
104
+ });
105
+
106
+ </script>
107
+ </body>
108
+
109
+ </html>
package/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import Camera from './src/Camera.js';
2
+
3
+ export { Camera };
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "author": "Wemap",
3
+ "contributors": [
4
+ "Thibaud Michel <thibaud@getwemap.com>"
5
+ ],
6
+ "description": "Wemap Camera utils package",
7
+ "main": "index.js",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/wemap/wemap-modules-js.git",
11
+ "directory": "packages/camera"
12
+ },
13
+ "name": "@wemap/camera",
14
+ "version": "3.2.3",
15
+ "bugs": {
16
+ "url": "https://github.com/wemap/wemap-modules-js/issues"
17
+ },
18
+ "homepage": "https://github.com/wemap/wemap-modules-js#readme",
19
+ "keywords": [
20
+ "utils",
21
+ "camera",
22
+ "wemap"
23
+ ],
24
+ "license": "ISC",
25
+ "dependencies": {
26
+ "@wemap/navigation-logger": "^3.2.3",
27
+ "events": "^3.1.0"
28
+ },
29
+ "type": "module",
30
+ "gitHead": "f7075f59fc3227c7f4cb7f1fa8cc69b98705364e"
31
+ }
package/src/Camera.js ADDED
@@ -0,0 +1,220 @@
1
+ import EventEmitter from 'events';
2
+
3
+ import CameraLogger from './CameraLogger.js';
4
+
5
+ const GENERIC_HARDWARE_VERTICAL_FOV = 60;
6
+
7
+ const parameters = {
8
+ sourceWidth: 640,
9
+ sourceHeight: 480
10
+ };
11
+
12
+ const userMediaConstraints = {
13
+ audio: false,
14
+ video: {
15
+ facingMode: 'environment',
16
+ width: { ideal: parameters.sourceWidth },
17
+ height: { ideal: parameters.sourceHeight }
18
+ }
19
+ };
20
+
21
+
22
+ // Helped from https://github.com/jeromeetienne/AR.js/blob/master/three.js/src/threex/threex-artoolkitsource.js
23
+
24
+ // Camera preview will fill component bounds without transformations.
25
+ // If the component aspect ratio is different from camera aspect ratio,
26
+ // camera preview is extended then cropped.
27
+ class Camera extends EventEmitter {
28
+
29
+ videoContainer;
30
+ videoElement;
31
+
32
+ fov = null;
33
+
34
+ _videoPlaying = false;
35
+
36
+ _hardwareVerticalFov = GENERIC_HARDWARE_VERTICAL_FOV;
37
+ _resizeOnWindowChange;
38
+
39
+ _cameraLogger;
40
+
41
+ constructor(container, resizeOnWindowChange = false) {
42
+ super();
43
+
44
+ this.videoContainer = container;
45
+ this._resizeOnWindowChange = resizeOnWindowChange;
46
+
47
+ this.videoContainer.style.overflow = 'hidden';
48
+
49
+ this.videoElement = document.createElement('video');
50
+ this.videoElement.setAttribute('id', 'wemap-camera');
51
+ this.videoElement.setAttribute('preload', 'none');
52
+ this.videoElement.setAttribute('muted', '');
53
+ this.videoElement.setAttribute('playsinline', '');
54
+ this.videoContainer.appendChild(this.videoElement);
55
+
56
+ this._cameraLogger = new CameraLogger();
57
+ }
58
+
59
+ async start() {
60
+
61
+ if (this.videoPlaying) {
62
+ throw new Error('Camera is already started');
63
+ }
64
+
65
+ this.videoPlaying = true;
66
+
67
+ await Camera.checkAvailability();
68
+
69
+ const stream = await navigator.mediaDevices.getUserMedia(userMediaConstraints);
70
+ this.videoElement.srcObject = stream;
71
+ this.videoElement.oncanplaythrough = () => this.videoElement.play();
72
+ this.videoStream = stream;
73
+
74
+ await new Promise(resolve => {
75
+ this.videoElement.onloadedmetadata = resolve;
76
+ });
77
+
78
+ this._resizeElement();
79
+ this._computeFov();
80
+
81
+ if (this._resizeOnWindowChange) {
82
+ window.addEventListener('resize', this.notifyContainerSizeChanged);
83
+ }
84
+
85
+ this._cameraLogger.attachStream(this.videoStream);
86
+ }
87
+
88
+ async stop() {
89
+
90
+ if (!this.videoPlaying) {
91
+ throw new Error('Camera is not started yet');
92
+ }
93
+
94
+ this._cameraLogger.detachStream();
95
+
96
+ if (this.videoStream && this.videoStream.stop) {
97
+ // compatibility with old JS API
98
+ this.videoStream.stop();
99
+ } else if (this.videoStream) {
100
+ // new JS API
101
+ this.videoStream.getVideoTracks().forEach(track => track.stop());
102
+ }
103
+ this.videoStream = null;
104
+ this.videoElement.srcObject = null;
105
+
106
+ if (this._resizeOnWindowChange) {
107
+ window.removeEventListener('resize', this.notifyContainerSizeChanged);
108
+ }
109
+ this.videoPlaying = false;
110
+ }
111
+
112
+ static async checkAvailability(testUserMedia = false) {
113
+
114
+ if (!navigator.mediaDevices) {
115
+ throw new Error('navigator.mediaDevices not present in your browser');
116
+ }
117
+
118
+ if (!navigator.mediaDevices.enumerateDevices) {
119
+ throw new Error('navigator.mediaDevices.enumerateDevices not present in your browser');
120
+ }
121
+
122
+ if (!navigator.mediaDevices.getUserMedia) {
123
+ throw new Error('navigator.mediaDevices.getUserMedia not present in your browser');
124
+ }
125
+
126
+ const devices = await navigator.mediaDevices.enumerateDevices();
127
+ if (!devices.find(device => device.kind === 'videoinput')) {
128
+ throw new Error('No camera found');
129
+ }
130
+
131
+ if (testUserMedia) {
132
+ const stream = await navigator.mediaDevices.getUserMedia(userMediaConstraints);
133
+ if (stream.stop) {
134
+ stream.stop();
135
+ } else {
136
+ stream.getVideoTracks().forEach(track => track.stop());
137
+ }
138
+ }
139
+ }
140
+
141
+ set hardwareVerticalFov(hardwareVerticalFov) {
142
+ this._hardwareVerticalFov = hardwareVerticalFov;
143
+ this._computeFov();
144
+ }
145
+
146
+ notifyContainerSizeChanged = () => {
147
+ this._resizeElement();
148
+ this._computeFov();
149
+ }
150
+
151
+ _resizeElement = () => {
152
+
153
+ if (!this.videoElement) {
154
+ throw new Error('No video element found');
155
+ }
156
+
157
+ const sourceWidth = this.videoElement.videoWidth;
158
+ const sourceHeight = this.videoElement.videoHeight;
159
+
160
+ const containerWidth = this.videoContainer.offsetWidth;
161
+ const containerHeight = this.videoContainer.offsetHeight;
162
+
163
+ const sourceAspect = sourceWidth / sourceHeight;
164
+ const screenAspect = containerWidth / containerHeight;
165
+
166
+ if (screenAspect < sourceAspect) {
167
+
168
+ const newWidth = sourceAspect * containerHeight;
169
+ this.videoElement.style.width = newWidth + 'px';
170
+ this.videoElement.style.marginLeft = -(newWidth - containerWidth) / 2 + 'px';
171
+
172
+ this.videoElement.style.height = containerHeight + 'px';
173
+ this.videoElement.style.marginTop = '0px';
174
+
175
+ } else {
176
+
177
+ const newHeight = 1 / (sourceAspect / containerWidth);
178
+ this.videoElement.style.height = newHeight + 'px';
179
+ this.videoElement.style.marginTop = -(newHeight - containerHeight) / 2 + 'px';
180
+
181
+ this.videoElement.style.width = containerWidth + 'px';
182
+ this.videoElement.style.marginLeft = '0px';
183
+ }
184
+
185
+ }
186
+
187
+ _computeFov = () => {
188
+
189
+ const sourceWidth = this.videoElement.videoWidth;
190
+ const sourceHeight = this.videoElement.videoHeight;
191
+
192
+ const containerWidth = this.videoContainer.offsetWidth;
193
+ const containerHeight = this.videoContainer.offsetHeight;
194
+
195
+ const sourceAspect = sourceWidth / sourceHeight;
196
+ const screenAspect = containerWidth / containerHeight;
197
+
198
+ let fovV = this._hardwareVerticalFov;
199
+ let fovH = Camera.convertFov(fovV, sourceAspect);
200
+
201
+ if (screenAspect < sourceAspect) {
202
+ fovH = Camera.convertFov(fovV, screenAspect);
203
+ } else {
204
+ fovV = Camera.convertFov(fovH, 1 / screenAspect);
205
+ }
206
+
207
+ this.fov = {
208
+ vertical: fovV,
209
+ horizontal: fovH
210
+ };
211
+
212
+ this.emit('fov.changed', this.fov);
213
+ }
214
+
215
+ static convertFov(angle, aspectRatio) {
216
+ return 2 * Math.atan(Math.tan((angle / 180 * Math.PI) / 2) * aspectRatio) * 180 / Math.PI;
217
+ }
218
+ }
219
+
220
+ export default Camera;
@@ -0,0 +1,78 @@
1
+ import { NavigationLogger } from '@wemap/navigation-logger';
2
+
3
+ class CameraLogger {
4
+
5
+ _videoStream = null;
6
+ _loggerParams = null;
7
+
8
+ constructor() {
9
+
10
+ this._loggerParams = NavigationLogger.params;
11
+
12
+ NavigationLogger.on('started', ({ params }) => {
13
+ this._loggerParams = params;
14
+ if (this._videoStream !== null) {
15
+ this._start();
16
+ }
17
+ });
18
+
19
+ NavigationLogger.on('stopped', () => {
20
+ this._loggerParams = null;
21
+ this._stop();
22
+ });
23
+
24
+ }
25
+
26
+ attachStream(videoStream) {
27
+ this._videoStream = videoStream;
28
+ if (NavigationLogger.isRecording) {
29
+ this._start();
30
+ }
31
+ }
32
+
33
+ detachStream() {
34
+ this._videoStream = null;
35
+ this._stop();
36
+ }
37
+
38
+ _start() {
39
+
40
+ if (!('MediaRecorder' in window)) {
41
+ return;
42
+ }
43
+
44
+ if (this.mediaRecorder) {
45
+ throw new Error('CameraLogger is already recording');
46
+ }
47
+
48
+ const options = {
49
+ mimeType: 'video/webm;codecs=vp8',
50
+ bitsPerSecond: this._loggerParams.videoBitrate
51
+ };
52
+
53
+ this.videoBlobs = [];
54
+
55
+ // https://stackoverflow.com/questions/56826079/how-to-concat-chunks-of-incoming-binary-into-video-webm-file-node-js
56
+ this.mediaRecorder = new MediaRecorder(this._videoStream, options);
57
+ this.mediaRecorder.ondataavailable = event => {
58
+ if (event.data && event.data.size > 0) {
59
+ NavigationLogger.sendVideoChunck(event.data);
60
+ }
61
+ };
62
+
63
+ this.mediaRecorder.start(this._loggerParams.flushInterval);
64
+ }
65
+
66
+
67
+ _stop() {
68
+
69
+ if (!this.mediaRecorder) {
70
+ return;
71
+ }
72
+
73
+ this.mediaRecorder.stop();
74
+ this.mediaRecorder = null;
75
+ }
76
+ }
77
+
78
+ export default CameraLogger;