Skip to content

Commit 5fa80ed

Browse files
committed
Ensure live preview is not cached
On some devices, the video preview freezes when the video is hidden and then shown again (e.g, show preview during countdown; hide preview to show taken picture; show preview during countdown of the next picture). This is because the last frame can be cached by the browser when the preview is hidden, and this cached frame is displayed when the preview is shown again. To disable caching, a unique parameter is added to the URL of the stream of the stream. Signed-off-by: Nils Bosbach <n.bosbach@gmx.de>
1 parent 8a0fb78 commit 5fa80ed

File tree

6 files changed

+25
-14
lines changed

6 files changed

+25
-14
lines changed

assets/js/preview.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
/* eslint n/no-unsupported-features/node-builtins: "off" */
22
/* globals photoBooth photoboothTools */
3+
4+
function addCacheBustingParam(url) {
5+
const timestamp = new Date().getTime();
6+
7+
if (url.includes('?')) {
8+
return `${url}&t=${timestamp}`;
9+
}
10+
11+
return `${url}?t=${timestamp}`;
12+
}
13+
14+
function getRootProperty(property) {
15+
const root = document.documentElement;
16+
const style = getComputedStyle(root);
17+
return style.getPropertyValue(property).trim();
18+
}
19+
320
const photoboothPreview = (function () {
421
// vars
522
const CameraDisplayMode = {
@@ -163,8 +180,8 @@ const photoboothPreview = (function () {
163180
} else if (config.preview.mode === PreviewMode.URL.valueOf()) {
164181
photoboothTools.console.logDev('Preview: Preview at countdown from URL.');
165182
setTimeout(function () {
183+
url.attr('src', addCacheBustingParam(getRootProperty('--background-preview')));
166184
url.show();
167-
url.addClass('streaming');
168185
}, config.preview.url_delay);
169186
}
170187
break;
@@ -175,8 +192,8 @@ const photoboothPreview = (function () {
175192
} else if (config.preview.mode === PreviewMode.URL.valueOf()) {
176193
photoboothTools.console.logDev('Preview: Preview from URL.');
177194
setTimeout(function () {
195+
url.attr('src', addCacheBustingParam(getRootProperty('--background-preview')));
178196
url.show();
179-
url.addClass('streaming');
180197
}, config.preview.url_delay);
181198
}
182199
break;
@@ -201,8 +218,8 @@ const photoboothPreview = (function () {
201218
api.stream.getTracks()[0].stop();
202219
api.stream = null;
203220
}
204-
url.removeClass('streaming');
205221
url.hide();
222+
url.attr('src', '');
206223
video.hide();
207224
pictureFrame.hide();
208225
collageFrame.hide();

assets/js/test-preview.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const photoboothPreviewTest = (function () {
9696
if (config.preview.mode === PreviewMode.DEVICE.valueOf()) {
9797
photoboothPreview.stopVideo();
9898
} else if (config.preview.mode === PreviewMode.URL.valueOf()) {
99-
previewIpcam.removeClass('streaming');
99+
previewIpcam.attr('src', '');
100100
previewIpcam.hide();
101101
}
102102

assets/sass/components/_preview.scss

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,3 @@
9292
scale: 1 -1;
9393
}
9494
}
95-
96-
#preview--ipcam {
97-
&.streaming {
98-
background-image: var(--background-preview);
99-
}
100-
}

docs/faq/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ If you like to have the same preview independent of the device you access Photob
468468
Make sure to have a stream available you can use (e.g. from your Webcam, Smartphone Camera or Raspberry Pi Camera)
469469

470470
- Admin panel config _"Preview mode"_: `from URL`
471-
- Admin panel config _"Preview-URL"_ example (add needed IP address instead): `url(http://192.168.0.2:8081)`
471+
- Admin panel config _"Preview-URL"_ example (add needed IP address instead): `http://192.168.0.2:8081`
472472

473473
**Note**
474474

@@ -480,7 +480,7 @@ Make sure to have a stream available you can use (e.g. from your Webcam, Smartph
480480
481481
If you want to use a stream from your DSLR or Pi Camera, install go2rtc and setup needed service to use.
482482
483-
go2rtc can be accessed at `http://localhost:1984`. Use `url("http://localhost:1984/api/stream.mjpeg?src=photobooth")` as _"Preview-URL"_ (replace `localhost` with Photobooths IP for remote access).
483+
go2rtc can be accessed at `http://localhost:1984`. Use `http://localhost:1984/api/stream.mjpeg?src=photobooth` as _"Preview-URL"_ (replace `localhost` with Photobooths IP for remote access).
484484
To be able to also capture images you need to adjust the capture command.
485485
_"Commands"_: _"Take picture command"_: `capture %s`
486486

lib/configsetup.inc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@
13651365
'preview_url' => [
13661366
'type' => 'input',
13671367
'name' => 'preview[url]',
1368-
'placeholder' => 'url(http://localhost:8081)',
1368+
'placeholder' => 'http://localhost:8081',
13691369
'value' => htmlentities($config['preview']['url'] ?? ''),
13701370
],
13711371
'preview_url_delay' => [

template/components/preview.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
echo '<div id="preview-container" style="aspect-ratio: ' . $comp . '">';
4545
echo '<div id="preview-wrapper" style="aspect-ratio:' . $comp . '">';
4646
echo '<video id="preview--video" style="' . $composed_style . '" class="' . $previewFlipClass . ' ' . $previewStyleClass . '" autoplay playsinline></video>';
47-
echo '<div id="preview--ipcam" style="' . $composed_style . '" class="' . $previewFlipClass . ' ' . $previewStyleClass . '"></div>';
47+
echo '<img id="preview--ipcam" style="' . $composed_style . '" class="' . $previewFlipClass . ' ' . $previewStyleClass . '"></img>';
4848
echo '<div id="preview--none">' . $languageService->translate('no_preview') . '</div>';
4949
echo '</div></div>';
5050

0 commit comments

Comments
 (0)