diff --git a/browser/components/preferences/in-content/content.xul b/browser/components/preferences/in-content/content.xul
index 0cfdc424bf..07f9de0409 100644
--- a/browser/components/preferences/in-content/content.xul
+++ b/browser/components/preferences/in-content/content.xul
@@ -68,8 +68,28 @@
+
#endif
+
+
+
+
+
+
+
+
+
+
diff --git a/browser/internaluserscripts/bundled-scripts/youtube-codec-check.user.js b/browser/internaluserscripts/bundled-scripts/youtube-codec-check.user.js
new file mode 100644
index 0000000000..c6d6165c88
--- /dev/null
+++ b/browser/internaluserscripts/bundled-scripts/youtube-codec-check.user.js
@@ -0,0 +1,77 @@
+/*
+ * Adapted from enhanced-h264ify's inject_codec_check.js.
+ *
+ * The MIT License (MIT)
+ * Copyright (c) 2019 alextrv
+ * Copyright (c) 2015 erkserkserks
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ */
+
+/* YouTube-only codec selection controls. */
+(function () {
+ "use strict";
+
+ var settings = window.__dactyloidaeYouTubeVideoSettings || {};
+ var originalCanPlayType = HTMLMediaElement.prototype.canPlayType;
+ var originalIsTypeSupported = window.MediaSource &&
+ window.MediaSource.isTypeSupported;
+
+ function isVideoType(type) {
+ return /(^|\s|;)video\//i.test(type) ||
+ /(^|[; ])codecs\s*=\s*["']?(?:avc|vp0|av0)/i.test(type);
+ }
+
+ function isBlocked(type) {
+ if (!type || !isVideoType(type)) {
+ return false;
+ }
+
+ // AV1 is always blocked, independently of the preferences.
+ if (/av01|av99/i.test(type)) {
+ return true;
+ }
+
+ if (settings.forceH264 && !/avc1|avc3/i.test(type)) {
+ return true;
+ }
+
+ if (settings.hideUnaccelerated &&
+ !settings.vp9HardwareAccelerated && /vp9|vp09/i.test(type)) {
+ return true;
+ }
+
+ if (settings.disable60fps) {
+ var match = /(?:framerate|fps)\s*=\s*([0-9]+(?:\.[0-9]+)?)/i.exec(type);
+ if (match && Number(match[1]) > 30) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ HTMLMediaElement.prototype.canPlayType = function (type) {
+ if (isBlocked(type)) {
+ return "";
+ }
+ return originalCanPlayType.call(this, type);
+ };
+
+ if (window.MediaSource && originalIsTypeSupported) {
+ window.MediaSource.isTypeSupported = function (type) {
+ if (isBlocked(type)) {
+ return false;
+ }
+ return originalIsTypeSupported.call(this, type);
+ };
+ }
+}());
diff --git a/browser/internaluserscripts/components/internaluserscripts.js b/browser/internaluserscripts/components/internaluserscripts.js
index d60ce90ea9..8876acaac7 100644
--- a/browser/internaluserscripts/components/internaluserscripts.js
+++ b/browser/internaluserscripts/components/internaluserscripts.js
@@ -99,6 +99,54 @@ InternalUserscriptsService.prototype = {
}
} catch (e) {}
let contentWin = win.wrappedJSObject || win;
+ let documentURI = null;
+ try {
+ documentURI = win.document.documentURIObject;
+ } catch (e) {}
+
+ // The codec shim is deliberately limited to YouTube. Use the browser's
+ // decoder probe rather than guessing from the user's graphics settings.
+ if (documentURI &&
+ documentURI.host &&
+ /(^|\.)youtube(?:-nocookie)?\.com$/i.test(documentURI.host)) {
+ let settings = {
+ hideUnaccelerated: Services.prefs.getBoolPref(
+ "browser.video.youtube.hide-unaccelerated", true),
+ forceH264: Services.prefs.getBoolPref(
+ "browser.video.youtube.force-h264", false),
+ disable60fps: Services.prefs.getBoolPref(
+ "browser.video.youtube.disable-60fps", false),
+ vp9HardwareAccelerated: false,
+ };
+
+ let loadCodecShim = function (vp9HardwareAccelerated) {
+ settings.vp9HardwareAccelerated = vp9HardwareAccelerated;
+ contentWin.__dactyloidaeYouTubeVideoSettings = settings;
+ try {
+ Services.scriptloader.loadSubScript(
+ "chrome://internaluserscripts/content/bundled-scripts/youtube-codec-check.user.js",
+ contentWin);
+ } catch (e) {}
+ };
+
+ try {
+ let utils = win.QueryInterface(Ci.nsIInterfaceRequestor)
+ .getInterface(Ci.nsIDOMWindowUtils);
+ let hardwareSupport = utils.supportsHardwareVP9Decoding;
+ if (hardwareSupport && typeof hardwareSupport.then === "function") {
+ hardwareSupport.then(function (result) {
+ loadCodecShim(/^Yes;/.test(result));
+ }, function () {
+ loadCodecShim(false);
+ });
+ } else {
+ loadCodecShim(/^Yes;/.test(hardwareSupport));
+ }
+ } catch (e) {
+ loadCodecShim(false);
+ }
+ }
+
let logPolyfill = function (name, source) {
try {
if (
diff --git a/browser/internaluserscripts/defaults/preferences/internaluserscripts.js b/browser/internaluserscripts/defaults/preferences/internaluserscripts.js
index f87d1d0121..3dbc97c3bf 100644
--- a/browser/internaluserscripts/defaults/preferences/internaluserscripts.js
+++ b/browser/internaluserscripts/defaults/preferences/internaluserscripts.js
@@ -3,3 +3,6 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
pref("browser.internal-userscripts.enabled", true);
+pref("browser.video.youtube.hide-unaccelerated", true);
+pref("browser.video.youtube.force-h264", false);
+pref("browser.video.youtube.disable-60fps", false);
diff --git a/browser/internaluserscripts/moz.build b/browser/internaluserscripts/moz.build
index 40f2b86f9d..208a980047 100644
--- a/browser/internaluserscripts/moz.build
+++ b/browser/internaluserscripts/moz.build
@@ -30,4 +30,5 @@ FINAL_TARGET_FILES['internal-userscripts'] += [
'bundled-scripts/textencoderstream-polyfill.user.js',
'bundled-scripts/transformstream-polyfill.user.js',
'bundled-scripts/webauthn-microsoft-shim.user.js',
+ 'bundled-scripts/youtube-codec-check.user.js',
]
diff --git a/browser/locales/en-US/chrome/browser/preferences/content.dtd b/browser/locales/en-US/chrome/browser/preferences/content.dtd
index bca4824f5a..85fd1fcf25 100644
--- a/browser/locales/en-US/chrome/browser/preferences/content.dtd
+++ b/browser/locales/en-US/chrome/browser/preferences/content.dtd
@@ -52,3 +52,11 @@
#endif
+
+
+
+
+
+
+
+