From 2e6215e31918e087a7c5cb0bc46657ff7789f1ae Mon Sep 17 00:00:00 2001 From: Alex X Date: Wed, 17 Apr 2024 16:50:03 +0300 Subject: [PATCH] Add support save screenshot for MJPEG source --- custom_components/webrtc/www/webrtc-camera.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/custom_components/webrtc/www/webrtc-camera.js b/custom_components/webrtc/www/webrtc-camera.js index a960ff4..fd9f9e1 100644 --- a/custom_components/webrtc/www/webrtc-camera.js +++ b/custom_components/webrtc/www/webrtc-camera.js @@ -422,14 +422,21 @@ class WebRTCCamera extends VideoRTC { } saveScreenshot() { - const canvas = document.createElement('canvas'); - canvas.width = this.video.videoWidth; - canvas.height = this.video.videoHeight; - canvas.getContext('2d').drawImage(this.video, 0, 0, canvas.width, canvas.height); + const a = document.createElement('a'); + + if (this.video.videoWidth && this.video.videoHeight) { + const canvas = document.createElement('canvas'); + canvas.width = this.video.videoWidth; + canvas.height = this.video.videoHeight; + canvas.getContext('2d').drawImage(this.video, 0, 0, canvas.width, canvas.height); + a.href = canvas.toDataURL('image/jpeg'); + } else if (this.video.poster && this.video.poster.startsWith('data:image/jpeg')) { + a.href = this.video.poster; + } else { + return; + } const ts = new Date().toISOString().substring(0, 19).replaceAll('-', '').replaceAll(':', ''); - const a = document.createElement('a'); - a.href = canvas.toDataURL('image/jpeg'); a.download = `snapshot_${ts}.jpeg`; a.click(); }