Skip to content

Commit

Permalink
fix(Xbox): Simplify the use of WebView2 (#7743)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad authored and joeyparrish committed Dec 11, 2024
1 parent 69e1bbe commit 0cbe208
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 51 deletions.
15 changes: 4 additions & 11 deletions docs/tutorials/screen-resolution-detection.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,10 @@ the `WinRT access` field to `All`.

### WebView2

When using a WebView2 control in a UWP app, additional steps are required in
order to enable the screen resolution detection. First, the WebView2's user-agent
is the same as Edge Chromium and does not contain the term "Xbox One", so it has
to be manually added like this when initializing your WebView2:

```CSharp
webView.CoreWebView2.Settings.UserAgent += " Xbox One";
```

Also, you will need to add a special project called WinRTAdapter in your project's
solution. This project allows WinRT APIs to be exposed in the WebView2 control.
When using a WebView2 control in a UWP app, an additional step is required in
order to enable the screen resolution detection. You will need to add a special
project called WinRTAdapter in your project's solution. This project allows
WinRT APIs to be exposed in the WebView2 control.
You will find more information on this [here](https://learn.microsoft.com/en-us/microsoft-edge/webview2/how-to/winrt-from-js).
Make sure you put `Windows.Media.Protection.ProtectionCapabilities`
and `Windows.Media.Protection.ProtectionCapabilityResult` in the WinRTAdapter
Expand Down
88 changes: 48 additions & 40 deletions lib/util/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,46 +765,6 @@ shaka.util.Platform = class {
shaka.log.alwaysWarn('Tizen: Error detecting screen size, default ' +
'screen size 1920x1080.');
}
} else if (Platform.isXboxOne()) {
maxResolution.width = 1920;
maxResolution.height = 1080;
try {
let winRT = undefined;

// Try to access to WinRT for WebView, if it's not defined,
// try to access to WinRT for WebView2, if it's not defined either,
// let it throw.
if (typeof Windows !== 'undefined') {
winRT = Windows;
} else {
winRT = chrome.webview.hostObjects.sync.Windows;
}

const protectionCapabilities =
new winRT.Media.Protection.ProtectionCapabilities();
const protectionResult =
winRT.Media.Protection.ProtectionCapabilityResult;
// isTypeSupported may return "maybe", which means the operation is not
// completed. This means we need to retry
// https://learn.microsoft.com/en-us/uwp/api/windows.media.protection.protectioncapabilityresult?view=winrt-22621
let result = null;
const type =
'video/mp4;codecs="hvc1,mp4a";features="decode-res-x=3840,' +
'decode-res-y=2160,decode-bitrate=20000,decode-fps=30,' +
'decode-bpc=10,display-res-x=3840,display-res-y=2160,' +
'display-bpc=8"';
const keySystem = 'com.microsoft.playready.recommendation';
do {
result = protectionCapabilities.isTypeSupported(type, keySystem);
} while (result === protectionResult.maybe);
if (result === protectionResult.probably) {
maxResolution.width = 3840;
maxResolution.height = 2160;
}
} catch (e) {
shaka.log.alwaysWarn('Xbox: Error detecting screen size, default ' +
'screen size 1920x1080.');
}
} else if (Platform.isWebOS()) {
try {
const deviceInfo =
Expand Down Expand Up @@ -851,6 +811,54 @@ shaka.util.Platform = class {
maxResolution.width = 1920;
maxResolution.height = 1080;
}
} else {
// For Xbox and UWP apps.
let winRT = undefined;
try {
// Try to access to WinRT for WebView, if it's not defined,
// try to access to WinRT for WebView2, if it's not defined either,
// let it throw.
if (typeof Windows !== 'undefined') {
winRT = Windows;
} else {
winRT = chrome.webview.hostObjects.sync.Windows;
}
} catch (e) {}
if (winRT) {
maxResolution.width = 1920;
maxResolution.height = 1080;
try {
const protectionCapabilities =
new winRT.Media.Protection.ProtectionCapabilities();
const protectionResult =
winRT.Media.Protection.ProtectionCapabilityResult;
// isTypeSupported may return "maybe", which means the operation
// is not completed. This means we need to retry
// https://learn.microsoft.com/en-us/uwp/api/windows.media.protection.protectioncapabilityresult?view=winrt-22621
let result = null;
const type =
'video/mp4;codecs="hvc1,mp4a";features="decode-res-x=3840,' +
'decode-res-y=2160,decode-bitrate=20000,decode-fps=30,' +
'decode-bpc=10,display-res-x=3840,display-res-y=2160,' +
'display-bpc=8"';
const keySystem = 'com.microsoft.playready.recommendation';
do {
result = protectionCapabilities.isTypeSupported(type, keySystem);
} while (result === protectionResult.maybe);
if (result === protectionResult.probably) {
maxResolution.width = 3840;
maxResolution.height = 2160;
}
} catch (e) {
shaka.log.alwaysWarn('Xbox: Error detecting screen size, default ' +
'screen size 1920x1080.');
}
} else if (Platform.isXboxOne()) {
maxResolution.width = 1920;
maxResolution.height = 1080;
shaka.log.alwaysWarn('Xbox: Error detecting screen size, default ' +
'screen size 1920x1080.');
}
}
return maxResolution;
}
Expand Down

0 comments on commit 0cbe208

Please sign in to comment.