forked from PhotoboothProject/photobooth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview.js
More file actions
246 lines (223 loc) · 8.44 KB
/
preview.js
File metadata and controls
246 lines (223 loc) · 8.44 KB
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
237
238
239
240
241
242
243
244
245
246
/* eslint n/no-unsupported-features/node-builtins: "off" */
/* globals photoBooth photoboothTools */
function addCacheBustingParam(url) {
const timestamp = new Date().getTime();
if (url.includes('?')) {
return `${url}&t=${timestamp}`;
}
return `${url}?t=${timestamp}`;
}
function getRootProperty(property) {
const root = document.documentElement;
const style = getComputedStyle(root);
return style.getPropertyValue(property).trim();
}
const photoboothPreview = (function () {
// vars
const CameraDisplayMode = {
INIT: 1,
BACKGROUND: 2,
COUNTDOWN: 3,
TEST: 4
},
PreviewMode = {
NONE: 'none',
DEVICE: 'device_cam',
URL: 'url'
},
webcamConstraints = {
audio: false,
video: {
width: config.preview.videoWidth,
height: config.preview.videoHeight,
facingMode: config.preview.camera_mode
}
},
api = {};
let pid,
video,
loader,
url,
pictureFrame,
collageFrame,
retryGetMedia = 3;
api.changeVideoMode = function (mode) {
photoboothTools.console.logDev('Preview: Changing video mode: ' + mode);
if (mode !== CameraDisplayMode.BACKGROUND) {
loader.css('--stage-background', 'transparent');
}
video.show();
};
api.initializeMedia = function (
cb = () => {
return;
},
retry = 0
) {
photoboothTools.console.logDev('Preview: Trying to initialize media...');
if (
!navigator.mediaDevices ||
config.preview.mode === PreviewMode.NONE.valueOf() ||
config.preview.mode === PreviewMode.URL.valueOf()
) {
photoboothTools.console.logDev('Preview: No preview from device cam or no webcam available!');
return;
}
const getMedia =
navigator.mediaDevices.getUserMedia ||
navigator.mediaDevices.webkitGetUserMedia ||
navigator.mediaDevices.mozGetUserMedia ||
false;
if (!getMedia) {
photoboothTools.console.logDev('Preview: Could not get media!');
return;
}
getMedia
.call(navigator.mediaDevices, webcamConstraints)
.then(function (stream) {
photoboothTools.console.logDev('Preview: getMedia done!');
api.stream = stream;
video.get(0).srcObject = stream;
cb();
})
.catch(function (error) {
photoboothTools.console.log('ERROR: Preview: Could not get user media: ', error);
if (retry < retryGetMedia) {
photoboothTools.console.logDev(
'Preview: Retrying to get user media. Retry ' + retry + ' / ' + retryGetMedia
);
retry += 1;
setTimeout(function () {
api.initializeMedia(cb, retry);
}, 1000);
} else {
photoboothTools.console.logDev(
'ERROR: Preview: Unable to get user media. Failed retries: ' + retry
);
}
});
};
api.getAndDisplayMedia = function (mode) {
if (api.stream && api.stream.active) {
api.changeVideoMode(mode);
} else {
api.initializeMedia(() => {
api.changeVideoMode(mode);
});
}
};
api.runCmd = function (mode) {
const dataVideo = {
play: mode,
pid: pid
};
jQuery
.post('api/previewCamera.php', dataVideo)
.done(function (result) {
photoboothTools.console.log('Preview: ' + dataVideo.play + ' webcam successfully.');
pid = result.pid;
})
// eslint-disable-next-line no-unused-vars
.fail(function (xhr, status, result) {
photoboothTools.console.log('ERROR: Preview: Failed to ' + dataVideo.play + ' webcam!');
});
};
api.startVideo = function (mode, retry = 0, maxGetMediaRetry = 3) {
retryGetMedia = maxGetMediaRetry;
photoboothTools.console.log('Preview: startVideo mode: ' + mode);
if (config.preview.mode !== PreviewMode.URL.valueOf()) {
if (!navigator.mediaDevices || config.preview.mode === PreviewMode.NONE.valueOf()) {
return;
}
}
switch (mode) {
case CameraDisplayMode.INIT:
photoboothTools.console.logDev('Preview: Running preview cmd (INIT).');
api.runCmd('start');
break;
case CameraDisplayMode.BACKGROUND:
if (
config.preview.mode === PreviewMode.DEVICE.valueOf() &&
config.commands.preview &&
!config.preview.bsm
) {
photoboothTools.console.logDev('Preview: Running preview cmd (BACKGROUND).');
api.runCmd('start');
}
api.getAndDisplayMedia(CameraDisplayMode.BACKGROUND);
break;
case CameraDisplayMode.COUNTDOWN:
if (config.commands.preview) {
if (
config.preview.bsm ||
(!config.preview.bsm && retry > 0) ||
(typeof photoBooth !== 'undefined' && photoBooth.nextCollageNumber > 0)
) {
photoboothTools.console.logDev('Preview: Running preview cmd (COUNTDOWN).');
api.runCmd('start');
}
}
if (config.preview.mode === PreviewMode.DEVICE.valueOf()) {
photoboothTools.console.logDev('Preview: Preview at countdown from device cam.');
api.getAndDisplayMedia(CameraDisplayMode.COUNTDOWN);
} else if (config.preview.mode === PreviewMode.URL.valueOf()) {
photoboothTools.console.logDev('Preview: Preview at countdown from URL.');
setTimeout(function () {
url.attr('src', addCacheBustingParam(getRootProperty('--background-preview')));
url.show();
}, config.preview.url_delay);
}
break;
case CameraDisplayMode.TEST:
if (config.preview.mode === PreviewMode.DEVICE.valueOf()) {
photoboothTools.console.logDev('Preview: Preview from device cam.');
api.getAndDisplayMedia(CameraDisplayMode.TEST);
} else if (config.preview.mode === PreviewMode.URL.valueOf()) {
photoboothTools.console.logDev('Preview: Preview from URL.');
setTimeout(function () {
url.attr('src', addCacheBustingParam(getRootProperty('--background-preview')));
url.show();
}, config.preview.url_delay);
}
break;
default:
photoboothTools.console.log('ERROR: Preview: Call for unexpected video mode: ' + mode);
break;
}
};
api.stopPreview = function () {
if (config.commands.preview_kill) {
api.runCmd('stop');
}
if (config.preview.mode === PreviewMode.DEVICE.valueOf() || config.preview.mode === PreviewMode.URL.valueOf()) {
api.stopVideo();
}
};
api.stopVideo = function () {
loader.css('--stage-background', 'var(--background-countdown-color)');
if (api.stream) {
api.stream.getTracks()[0].stop();
api.stream = null;
}
url.hide();
url.attr('src', '');
video.hide();
pictureFrame.hide();
collageFrame.hide();
};
api.setElements = () => {
video = $('#preview--video');
loader = $('.stage[data-stage="loader"]');
url = $('#preview--ipcam');
pictureFrame = $('#previewframe--picture');
collageFrame = $('#previewframe--collage');
};
api.init = function () {
api.setElements();
};
return api;
})();
$(function () {
photoboothPreview.init();
photoboothTools.console.log('Preview: Preview functions available.');
});