Skip to content

Commit

Permalink
feat: attempt to help Safari as well
Browse files Browse the repository at this point in the history
  • Loading branch information
robtpaton committed Aug 17, 2024
1 parent 607f0bb commit 1eb4ef0
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions custom_components/webrtc/www/video-rtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ export class VideoRTC extends HTMLElement {
* @type {Object.<string,Function>}
*/
this.onmessage = null;

/**
* [internal] If this browser appears to be Safari
* @type {boolean}
*/
this.isSafari = window.navigator.userAgent.match(/Version\/(\d+).+Safari/);
}

/**
Expand All @@ -165,19 +171,21 @@ export class VideoRTC extends HTMLElement {
*/
play() {
this.video.play().catch((firstError) => {
console.warn(firstError);
const isAutoplayError = firstError.message.includes("NotAllowedError");
console.warn(firstError)

const isChromeOrFirefoxAutoplayError = firstError.name == "NotAllowedError";
const isSafariAutoplayError = this.isSafari && firstError.name == "AbortError";
const isAutoplayError = isChromeOrFirefoxAutoplayError || isSafariAutoplayError;

if (!this.video.muted && isAutoplayError) {
this.video.muted = true;
setTimeout(() =>
this.video.play().catch(secondError => {
console.warn(secondError);
}
), PLAY_REATTEMPT_TIMEOUT);
), this.PLAY_REATTEMPT_TIMEOUT);
}
});
}

/**
* Send message to server via WebSocket
Expand Down Expand Up @@ -254,8 +262,7 @@ export class VideoRTC extends HTMLElement {
this.appendChild(this.video);

// all Safari lies about supported audio codecs
const m = window.navigator.userAgent.match(/Version\/(\d+).+Safari/);
if (m) {
if (this.isSafari) {
// AAC from v13, FLAC from v14, OPUS - unsupported
const skip = m[1] < '13' ? 'mp4a.40.2' : m[1] < '14' ? 'flac' : 'opus';
this.CODECS.splice(this.CODECS.indexOf(skip));
Expand Down

0 comments on commit 1eb4ef0

Please sign in to comment.