From 607f0bb4deaaff7eb01b45f2452826e092049c4c Mon Sep 17 00:00:00 2001 From: Rob Paton Date: Sat, 17 Aug 2024 13:11:47 +0000 Subject: [PATCH 1/3] fix: prevent errors other than autoplay errors from muting video --- custom_components/webrtc/www/video-rtc.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/custom_components/webrtc/www/video-rtc.js b/custom_components/webrtc/www/video-rtc.js index 273bc09..7d1bdbd 100644 --- a/custom_components/webrtc/www/video-rtc.js +++ b/custom_components/webrtc/www/video-rtc.js @@ -20,6 +20,7 @@ export class VideoRTC extends HTMLElement { this.DISCONNECT_TIMEOUT = 5000; this.RECONNECT_TIMEOUT = 30000; + this.PLAY_REATTEMPT_TIMEOUT = 500; this.CODECS = [ 'avc1.640029', // H.264 high 4.1 (Chromecast 1st and 2nd Gen) @@ -163,12 +164,17 @@ export class VideoRTC extends HTMLElement { * https://developer.chrome.com/blog/autoplay/ */ play() { - this.video.play().catch(() => { - if (!this.video.muted) { + this.video.play().catch((firstError) => { + console.warn(firstError); + const isAutoplayError = firstError.message.includes("NotAllowedError"); + + if (!this.video.muted && isAutoplayError) { this.video.muted = true; - this.video.play().catch(er => { - console.warn(er); - }); + setTimeout(() => + this.video.play().catch(secondError => { + console.warn(secondError); + } + ), PLAY_REATTEMPT_TIMEOUT); } }); } From e5695cc9eaa7e921e912336bb3834dd2822f7b69 Mon Sep 17 00:00:00 2001 From: Rob Paton Date: Sat, 17 Aug 2024 13:53:01 +0000 Subject: [PATCH 2/3] refactor: use error name --- custom_components/webrtc/www/video-rtc.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/webrtc/www/video-rtc.js b/custom_components/webrtc/www/video-rtc.js index 7d1bdbd..46af29c 100644 --- a/custom_components/webrtc/www/video-rtc.js +++ b/custom_components/webrtc/www/video-rtc.js @@ -166,7 +166,7 @@ export class VideoRTC extends HTMLElement { play() { this.video.play().catch((firstError) => { console.warn(firstError); - const isAutoplayError = firstError.message.includes("NotAllowedError"); + const isAutoplayError = firstError.name == "NotAllowedError"; if (!this.video.muted && isAutoplayError) { this.video.muted = true; @@ -174,7 +174,7 @@ export class VideoRTC extends HTMLElement { this.video.play().catch(secondError => { console.warn(secondError); } - ), PLAY_REATTEMPT_TIMEOUT); + ), this.PLAY_REATTEMPT_TIMEOUT); } }); } From d0cd114764461213b02d8d9825adca9a6fb9bcd7 Mon Sep 17 00:00:00 2001 From: Rob Paton Date: Sun, 18 Aug 2024 09:16:50 +0000 Subject: [PATCH 3/3] feat: remove timeout --- custom_components/webrtc/www/video-rtc.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/custom_components/webrtc/www/video-rtc.js b/custom_components/webrtc/www/video-rtc.js index 46af29c..2e1a652 100644 --- a/custom_components/webrtc/www/video-rtc.js +++ b/custom_components/webrtc/www/video-rtc.js @@ -20,7 +20,6 @@ export class VideoRTC extends HTMLElement { this.DISCONNECT_TIMEOUT = 5000; this.RECONNECT_TIMEOUT = 30000; - this.PLAY_REATTEMPT_TIMEOUT = 500; this.CODECS = [ 'avc1.640029', // H.264 high 4.1 (Chromecast 1st and 2nd Gen) @@ -170,11 +169,9 @@ export class VideoRTC extends HTMLElement { if (!this.video.muted && isAutoplayError) { this.video.muted = true; - setTimeout(() => - this.video.play().catch(secondError => { - console.warn(secondError); - } - ), this.PLAY_REATTEMPT_TIMEOUT); + this.video.play().catch(secondError => { + console.warn(secondError); + }); } }); }