refactor: remove playback-profiles and browser detection

Signed-off-by: Fernando Fernández <ferferga@hotmail.com>
This commit is contained in:
Fernando Fernández
2024-05-19 14:24:46 +02:00
parent cbc36cb466
commit bef453c96b
18 changed files with 0 additions and 1828 deletions
-314
View File
@@ -1,314 +0,0 @@
/**
* Utilities to detect the browser and get information on the current environment
* Based on https://github.com/google/shaka-player/blob/master/lib/util/platform.js
*
* @deprecated - Parsing User Agent is a maintenance burden and
* should rely on external libraries only. It's also going to be replaced with Client-Hints.
* Migration paths:
* * Check for platform-specific features *where needed*
* directly (i.e Chromecast/AirPlay/MSE) instead of a per-browser basis.
* This will always be 100% fault free.
*
* * Use something like https://www.npmjs.com/package/unique-names-generator to
* distinguish between instances. Instance names could be shown and be modified by the user
* at settings. This would make user instances distinguishable in a 100% fault-tolerant way
* and solve incongruencies like how a device is named. For instance,
* an instance running in an Android Auto headset will be recognised as Android only, which is less
* than ideal.
*/
export function supportsMediaSource(): boolean {
/*
* Browsers that lack a media source implementation will have no reference
* to |window.MediaSource|.
*/
return !!window.MediaSource;
}
/**
* Check if the user agent of the navigator contains a key.
*
* @private
* @static
* @param key - Key for which to perform a check.
* @returns Determines if user agent of navigator contains a key
*/
function userAgentContains(key: string): boolean {
const userAgent = navigator.userAgent || '';
return userAgent.includes(key);
}
/* Desktop Browsers */
/**
* Check if the current platform is Mozilla Firefox.
*
* @returns Determines if browser is Mozilla Firefox
*/
export function isFirefox(): boolean {
return userAgentContains('Firefox/');
}
/**
* Check if the current platform is Microsoft Edge.
*
* @static
* @returns Determines if browser is Microsoft Edge
*/
export function isEdge(): boolean {
return userAgentContains('Edg/') || userAgentContains('Edge/');
}
/**
* Check if the current platform is Chromium based.
*
* @returns Determines if browser is Chromium based
*/
export function isChromiumBased(): boolean {
return userAgentContains('Chrome');
}
/**
* Check if the current platform is Google Chrome.
*
* @returns Determines if browser is Google Chrome
*/
export function isChrome(): boolean {
/*
* The Edge user agent will also contain the "Chrome" keyword, so we need
* to make sure this is not Edge.
*/
return userAgentContains('Chrome') && !isEdge() && !isWebOS();
}
/**
* Check if the current platform is from Apple.
*
* Returns true on all iOS browsers and on desktop Safari.
*
* Returns false for non-Safari browsers on macOS, which are independent of
* Apple.
*
* @returns Determines if current platform is from Apple
*/
export function isApple(): boolean {
return navigator.vendor.includes('Apple') && !isTizen();
}
/**
* Returns a major version number for Safari, or Safari-based iOS browsers.
*
* @returns The major version number for Safari
*/
export function safariVersion(): number | undefined {
// All iOS browsers and desktop Safari will return true for isApple().
if (!isApple()) {
return;
}
let userAgent = '';
if (navigator.userAgent) {
userAgent = navigator.userAgent;
}
/*
* This works for iOS Safari and desktop Safari, which contain something
* like "Version/13.0" indicating the major Safari or iOS version.
*/
let match = /Version\/(\d+)/.exec(userAgent);
if (match) {
return Number.parseInt(match[1], /* Base= */ 10);
}
/*
* This works for all other browsers on iOS, which contain something like
* "OS 13_3" indicating the major & minor iOS version.
*/
match = /OS (\d+)(?:_\d+)?/.exec(userAgent);
if (match) {
return Number.parseInt(match[1], /* Base= */ 10);
}
}
/* TV Platforms */
/**
* Check if the current platform is Tizen.
*
* @returns Determines if current platform is Tizen
*/
export function isTizen(): boolean {
return userAgentContains('Tizen');
}
/**
* Check if the current platform is Tizen 2
*
* @returns Determines if current platform is Tizen 2
*/
export function isTizen2(): boolean {
return userAgentContains('Tizen 2');
}
/**
* Check if the current platform is Tizen 3
*
* @returns Determines if current platform is Tizen 3
* @memberof BrowserDetector
*/
export function isTizen3(): boolean {
return userAgentContains('Tizen 3');
}
/**
* Check if the current platform is Tizen 4.
*
* @returns Determines if current platform is Tizen 4
* @memberof BrowserDetector
*/
export function isTizen4(): boolean {
return userAgentContains('Tizen 4');
}
/**
* Check if the current platform is Tizen 5.
*
* @returns Determines if current platform is Tizen 5
* @memberof BrowserDetector
*/
export function isTizen5(): boolean {
return userAgentContains('Tizen 5');
}
/**
* Check if the current platform is Tizen 5.5.
*
* @returns Determines if current platform is Tizen 5.5
* @memberof BrowserDetector
*/
export function isTizen55(): boolean {
return userAgentContains('Tizen 5.5');
}
/**
* Check if the current platform is WebOS.
*
* @returns Determines if current platform is WebOS
* @memberof BrowserDetector
*/
export function isWebOS(): boolean {
return userAgentContains('Web0S');
}
/**
* Determines if current platform is WebOS1
*/
export function isWebOS1(): boolean {
return (
isWebOS()
&& userAgentContains('AppleWebKit/537')
&& !userAgentContains('Chrome/')
);
}
/**
* Determines if current platform is WebOS2
*/
export function isWebOS2(): boolean {
return (
isWebOS()
&& userAgentContains('AppleWebKit/538')
&& !userAgentContains('Chrome/')
);
}
/**
* Determines if current platform is WebOS3
*/
export function isWebOS3(): boolean {
return isWebOS() && userAgentContains('Chrome/38');
}
/**
* Determines if current platform is WebOS4
*/
export function isWebOS4(): boolean {
return isWebOS() && userAgentContains('Chrome/53');
}
/**
* Determines if current platform is WebOS5
*/
export function isWebOS5(): boolean {
return isWebOS() && userAgentContains('Chrome/68');
}
/* Platform Utilities */
/**
* Determines if current platform is Android
*/
export function isAndroid(): boolean {
return userAgentContains('Android');
}
/**
* Guesses if the platform is a mobile one (iOS or Android).
*
* @returns Determines if current platform is mobile (Guess)
*/
export function isMobile(): boolean {
let userAgent = '';
if (navigator.userAgent) {
userAgent = navigator.userAgent;
}
if (/iPhone|iPad|iPod|Android/.test(userAgent)) {
// This is Android, iOS, or iPad < 13.
return true;
}
/*
* Starting with iOS 13 on iPad, the user agent string no longer has the
* word "iPad" in it. It looks very similar to desktop Safari. This seems
* to be intentional on Apple's part.
* See: https://forums.developer.apple.com/thread/119186
*
* So if it's an Apple device with multi-touch support, assume it's a mobile
* device. If some future iOS version starts masking their user agent on
* both iPhone & iPad, this clause should still work. If a future
* multi-touch desktop Mac is released, this will need some adjustment.
*/
return isApple() && navigator.maxTouchPoints > 1;
}
/**
* Guesses if the platform is a Smart TV (Tizen or WebOS).
*
* @returns Determines if platform is a Smart TV
*/
export function isTv(): boolean {
return isTizen() || isWebOS();
}
/**
* Guesses if the platform is a PS4
*
* @returns Determines if the device is a PS4
*/
export function isPs4(): boolean {
return userAgentContains('playstation 4');
}
/**
* Guesses if the platform is a Xbox
*
* @returns Determines if the device is a Xbox
*/
export function isXbox(): boolean {
return userAgentContains('xbox');
}
@@ -1,108 +0,0 @@
/**
* @deprecated - Check @/utils/playback-profiles/index
*/
import {
type DirectPlayProfile,
DlnaProfileType
} from '@jellyfin/sdk/lib/generated-client';
import { getSupportedMP4VideoCodecs } from './helpers/mp4-video-formats';
import { getSupportedMP4AudioCodecs } from './helpers/mp4-audio-formats';
import { hasMkvSupport } from './helpers/transcoding-formats';
import { getSupportedWebMAudioCodecs } from './helpers/webm-audio-formats';
import { getSupportedWebMVideoCodecs } from './helpers/webm-video-formats';
import { getSupportedAudioCodecs } from './helpers/audio-formats';
/**
* Returns a valid DirectPlayProfile for the current platform.
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns An array of direct play profiles for the current platform.
*/
export function getDirectPlayProfiles(
videoTestElement: HTMLVideoElement
): DirectPlayProfile[] {
const DirectPlayProfiles: DirectPlayProfile[] = [];
const webmVideoCodecs = getSupportedWebMVideoCodecs(videoTestElement);
const webmAudioCodecs = getSupportedWebMAudioCodecs(videoTestElement);
const mp4VideoCodecs = getSupportedMP4VideoCodecs(videoTestElement);
const mp4AudioCodecs = getSupportedMP4AudioCodecs(videoTestElement);
if (webmVideoCodecs.length > 0) {
DirectPlayProfiles.push({
Container: 'webm',
Type: DlnaProfileType.Video,
VideoCodec: webmVideoCodecs.join(','),
AudioCodec: webmAudioCodecs.join(',')
});
}
if (mp4VideoCodecs.length > 0) {
DirectPlayProfiles.push({
Container: 'mp4,m4v',
Type: DlnaProfileType.Video,
VideoCodec: mp4VideoCodecs.join(','),
AudioCodec: mp4AudioCodecs.join(',')
});
}
if (hasMkvSupport(videoTestElement) && mp4VideoCodecs.length > 0) {
DirectPlayProfiles.push({
Container: 'mkv',
Type: DlnaProfileType.Video,
VideoCodec: mp4VideoCodecs.join(','),
AudioCodec: mp4AudioCodecs.join(',')
});
}
const supportedAudio = [
'opus',
'mp3',
'mp2',
'aac',
'flac',
'alac',
'webma',
'wma',
'wav',
'ogg',
'oga'
];
for (const audioFormat of supportedAudio.filter(format =>
getSupportedAudioCodecs(format)
)) {
DirectPlayProfiles.push({
Container: audioFormat,
Type: DlnaProfileType.Audio
});
if (audioFormat === 'opus' || audioFormat === 'webma') {
DirectPlayProfiles.push({
Container: 'webm',
Type: DlnaProfileType.Audio,
AudioCodec: audioFormat
});
}
// Aac also appears in the m4a and m4b container
if (audioFormat === 'aac' || audioFormat === 'alac') {
DirectPlayProfiles.push(
{
Container: 'm4a',
AudioCodec: audioFormat,
Type: DlnaProfileType.Audio
},
{
Container: 'm4b',
AudioCodec: audioFormat,
Type: DlnaProfileType.Audio
}
);
}
}
return DirectPlayProfiles;
}
@@ -1,44 +0,0 @@
/**
* @deprecated - Check @/utils/playback-profiles/index
*/
import { isApple, isTizen, isTv, isWebOS } from '@/utils/browser-detection';
/**
* Determines if audio codec is supported
*/
export function getSupportedAudioCodecs(format: string): boolean {
let typeString;
if (format === 'flac' && isTv()) {
return true;
} else if (format === 'wma' && isTizen()) {
return true;
} else if (format === 'asf' && isTv()) {
return true;
} else if (format === 'opus') {
if (!isWebOS()) {
typeString = 'audio/ogg; codecs="opus"';
return !!document
.createElement('audio')
.canPlayType(typeString)
.replace(/no/, '');
}
return false;
} else if (format === 'alac' && isApple()) {
return true;
} else if (format === 'webma') {
typeString = 'audio/webm';
} else if (format === 'mp2') {
typeString = 'audio/mpeg';
} else {
typeString = 'audio/' + format;
}
return !!document
.createElement('audio')
.canPlayType(typeString)
.replace(/no/, '');
}
@@ -1,364 +0,0 @@
/**
* @deprecated - Check @/utils/playback-profiles/index
*/
import {
CodecType,
ProfileConditionType,
ProfileConditionValue,
type CodecProfile,
type ProfileCondition
} from '@jellyfin/sdk/lib/generated-client';
import {
isApple,
isChromiumBased,
isEdge,
isMobile,
isPs4,
isTizen,
isTv,
isWebOS,
isXbox,
safariVersion
} from '@/utils/browser-detection';
import { isFunc, isObj } from '@/utils/validation';
/**
* Gets the max video bitrate
*
* @returns Returns the MaxVideoBitrate
*/
function getGlobalMaxVideoBitrate(): number | undefined {
let isTizenFhd = false;
if (
isTizen()
&& 'webapis' in window
&& isObj(window.webapis)
&& window.webapis
&& 'productinfo' in window.webapis
&& isObj(window.webapis.productinfo)
&& window.webapis.productinfo
&& 'isUdPanelSupported' in window.webapis.productinfo
&& isFunc(window.webapis.productinfo.isUdPanelSupported)
) {
isTizenFhd = !window.webapis.productinfo.isUdPanelSupported();
}
/*
* TODO: These values are taken directly from Jellyfin-web.
* The source of them needs to be investigated.
*/
if (isPs4()) {
return 8_000_000;
}
if (isXbox()) {
return 12_000_000;
}
if (isTizen() && isTizenFhd) {
return 20_000_000;
}
}
/**
* Creates a profile condition object for use in device playback profiles.
*
* @param Property - Value for the property
* @param Condition - Condition that the property must comply with
* @param Value - Value to check in the condition
* @param IsRequired - Whether this property is required
* @returns - Constructed ProfileCondition object
*/
function createProfileCondition(
Property: ProfileConditionValue,
Condition: ProfileConditionType,
Value: string,
IsRequired = false
): ProfileCondition {
return {
Condition,
Property,
Value,
IsRequired
};
}
/**
* Gets the AAC audio codec profile conditions
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns - Array of ACC Profile conditions
*/
export function getAacCodecProfileConditions(
videoTestElement: HTMLVideoElement
): ProfileCondition[] {
const supportsSecondaryAudio = isTizen();
const conditions: ProfileCondition[] = [];
// Handle he-aac not supported
if (
!videoTestElement
.canPlayType('video/mp4; codecs="avc1.640029, mp4a.40.5"')
.replace(/no/, '')
) {
// TODO: This needs to become part of the stream url in order to prevent stream copy
conditions.push(
createProfileCondition(
ProfileConditionValue.AudioProfile,
ProfileConditionType.NotEquals,
'HE-AAC'
)
);
}
if (!supportsSecondaryAudio) {
conditions.push(
createProfileCondition(
ProfileConditionValue.IsSecondaryAudio,
ProfileConditionType.Equals,
'false'
)
);
}
return conditions;
}
/**
* Gets an array with all the codec profiles that this client supports
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns - Array containing the different profiles for the client
*/
export function getCodecProfiles(
videoTestElement: HTMLVideoElement
): CodecProfile[] {
const CodecProfiles: CodecProfile[] = [];
const aacProfileConditions = getAacCodecProfileConditions(videoTestElement);
const supportsSecondaryAudio = isTizen();
if (aacProfileConditions.length > 0) {
CodecProfiles.push({
Type: CodecType.VideoAudio,
Codec: 'aac',
Conditions: aacProfileConditions
});
}
if (!supportsSecondaryAudio) {
CodecProfiles.push({
Type: CodecType.VideoAudio,
Conditions: [
createProfileCondition(
ProfileConditionValue.IsSecondaryAudio,
ProfileConditionType.Equals,
'false'
)
]
});
}
let maxH264Level = 42;
let h264Profiles = 'high|main|baseline|constrained baseline';
if (
isTv()
|| videoTestElement
.canPlayType('video/mp4; codecs="avc1.640833"')
.replace(/no/, '')
) {
maxH264Level = 51;
}
if (
videoTestElement
.canPlayType('video/mp4; codecs="avc1.640834"')
.replace(/no/, '')
) {
maxH264Level = 52;
}
if (
(isTizen()
|| videoTestElement
.canPlayType('video/mp4; codecs="avc1.6e0033"')
.replace(/no/, '')) // TODO: These tests are passing in Safari, but playback is failing
&& (!isApple() || !isWebOS() || !(isEdge() && !isChromiumBased()))
) {
h264Profiles += '|high 10';
}
let maxHevcLevel = 120;
let hevcProfiles = 'main';
const hevcProfilesMain10 = 'main|main 10';
// HEVC Main profile, Level 4.1
if (
videoTestElement
.canPlayType('video/mp4; codecs="hvc1.1.4.L123"')
.replace(/no/, '')
|| videoTestElement
.canPlayType('video/mp4; codecs="hev1.1.4.L123"')
.replace(/no/, '')
) {
maxHevcLevel = 123;
}
// HEVC Main10 profile, Level 4.1
if (
videoTestElement
.canPlayType('video/mp4; codecs="hvc1.2.4.L123"')
.replace(/no/, '')
|| videoTestElement
.canPlayType('video/mp4; codecs="hev1.2.4.L123"')
.replace(/no/, '')
) {
maxHevcLevel = 123;
hevcProfiles = hevcProfilesMain10;
}
// HEVC Main10 profile, Level 5.1
if (
videoTestElement
.canPlayType('video/mp4; codecs="hvc1.2.4.L153"')
.replace(/no/, '')
|| videoTestElement
.canPlayType('video/mp4; codecs="hev1.2.4.L153"')
.replace(/no/, '')
) {
maxHevcLevel = 153;
hevcProfiles = hevcProfilesMain10;
}
// HEVC Main10 profile, Level 6.1
if (
videoTestElement
.canPlayType('video/mp4; codecs="hvc1.2.4.L183"')
.replace(/no/, '')
|| videoTestElement
.canPlayType('video/mp4; codecs="hev1.2.4.L183"')
.replace(/no/, '')
) {
maxHevcLevel = 183;
hevcProfiles = hevcProfilesMain10;
}
const hevcCodecProfileConditions: ProfileCondition[] = [
createProfileCondition(
ProfileConditionValue.IsAnamorphic,
ProfileConditionType.NotEquals,
'true'
),
createProfileCondition(
ProfileConditionValue.VideoProfile,
ProfileConditionType.EqualsAny,
hevcProfiles
),
createProfileCondition(
ProfileConditionValue.VideoLevel,
ProfileConditionType.LessThanEqual,
maxHevcLevel.toString()
)
];
const h264CodecProfileConditions: ProfileCondition[] = [
createProfileCondition(
ProfileConditionValue.IsAnamorphic,
ProfileConditionType.NotEquals,
'true'
),
createProfileCondition(
ProfileConditionValue.VideoProfile,
ProfileConditionType.EqualsAny,
h264Profiles
),
createProfileCondition(
ProfileConditionValue.VideoLevel,
ProfileConditionType.LessThanEqual,
maxH264Level.toString()
)
];
if (!isTv()) {
h264CodecProfileConditions.push(
createProfileCondition(
ProfileConditionValue.IsInterlaced,
ProfileConditionType.NotEquals,
'true'
)
);
hevcCodecProfileConditions.push(
createProfileCondition(
ProfileConditionValue.IsInterlaced,
ProfileConditionType.NotEquals,
'true'
)
);
}
const globalMaxVideoBitrate = (getGlobalMaxVideoBitrate() ?? '').toString();
if (globalMaxVideoBitrate) {
h264CodecProfileConditions.push(
createProfileCondition(
ProfileConditionValue.VideoBitrate,
ProfileConditionType.LessThanEqual,
globalMaxVideoBitrate,
true
)
);
}
if (globalMaxVideoBitrate) {
hevcCodecProfileConditions.push(
createProfileCondition(
ProfileConditionValue.VideoBitrate,
ProfileConditionType.LessThanEqual,
globalMaxVideoBitrate,
true
)
);
}
// On iOS 12.x, for TS container max h264 level is 4.2
if (isApple() && isMobile() && Number(safariVersion()) < 13) {
const codecProfile = {
Type: CodecType.Video,
Codec: 'h264',
Container: 'ts',
Conditions: h264CodecProfileConditions.filter((condition) => {
return condition.Property !== 'VideoLevel';
})
};
codecProfile.Conditions.push(
createProfileCondition(
ProfileConditionValue.VideoLevel,
ProfileConditionType.LessThanEqual,
'42'
)
);
CodecProfiles.push(codecProfile);
}
CodecProfiles.push(
{
Type: CodecType.Video,
Codec: 'h264',
Conditions: h264CodecProfileConditions
},
{
Type: CodecType.Video,
Codec: 'hevc',
Conditions: hevcCodecProfileConditions
}
);
return CodecProfiles;
}
@@ -1,51 +0,0 @@
/**
* @deprecated - Check @/utils/playback-profiles/index
*/
import { getSupportedAudioCodecs } from './audio-formats';
import {
hasAacSupport,
hasAc3InHlsSupport,
hasAc3Support,
hasEac3Support,
hasMp3AudioSupport
} from './mp4-audio-formats';
import { isEdge } from '@/utils/browser-detection';
/**
* Gets an array with the supported fmp4 codecs
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns List of supported FMP4 audio codecs
*/
export function getSupportedFmp4AudioCodecs(
videoTestElement: HTMLVideoElement
): string[] {
const codecs = [];
if (hasAacSupport(videoTestElement)) {
codecs.push('aac');
}
if (hasMp3AudioSupport(videoTestElement)) {
codecs.push('mp3');
}
if (hasAc3Support(videoTestElement) && hasAc3InHlsSupport(videoTestElement)) {
codecs.push('ac3');
if (hasEac3Support(videoTestElement)) {
codecs.push('eac3');
}
}
if (getSupportedAudioCodecs('flac') && !isEdge()) {
codecs.push('flac');
}
if (getSupportedAudioCodecs('alac')) {
codecs.push('alac');
}
return codecs;
}
@@ -1,46 +0,0 @@
/**
* @deprecated - Check @/utils/playback-profiles/index
*/
import { hasH264Support, hasHevcSupport } from './mp4-video-formats';
import {
isApple,
isChrome,
isEdge,
isFirefox,
isTizen,
isWebOS
} from '@/utils/browser-detection';
/**
* Gets an array of supported fmp4 video codecs
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns List of supported fmp4 video codecs
*/
export function getSupportedFmp4VideoCodecs(
videoTestElement: HTMLVideoElement
): string[] {
const codecs = [];
if (
(isApple() || isEdge() || isTizen() || isWebOS())
&& hasHevcSupport(videoTestElement)
) {
codecs.push('hevc');
}
if (
hasH264Support(videoTestElement)
&& (isChrome()
|| isFirefox()
|| isApple()
|| isEdge()
|| isTizen()
|| isWebOS())
) {
codecs.push('h264');
}
return codecs;
}
@@ -1,89 +0,0 @@
/**
* @deprecated - Check @/utils/playback-profiles/index
*/
import { hasH264Support, hasH265Support } from './mp4-video-formats';
import { hasEac3Support, hasAacSupport } from './mp4-audio-formats';
import { getSupportedAudioCodecs } from './audio-formats';
import { isTv } from '@/utils/browser-detection';
/**
* Check if client supports AC3 in HLS stream
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Determines if the browser has AC3 in HLS support
*/
function supportsAc3InHls(
videoTestElement: HTMLVideoElement
): boolean | string {
if (isTv()) {
return true;
}
if (videoTestElement.canPlayType) {
return (
videoTestElement
.canPlayType('application/x-mpegurl; codecs="avc1.42E01E, ac-3"')
.replace(/no/, '')
|| videoTestElement
.canPlayType(
'application/vnd.apple.mpegURL; codecs="avc1.42E01E, ac-3"'
)
.replace(/no/, '')
);
}
return false;
}
/**
* Gets the supported HLS video codecs
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Array of video codecs supported in HLS
*/
export function getHlsVideoCodecs(
videoTestElement: HTMLVideoElement
): string[] {
const hlsVideoCodecs = [];
if (hasH264Support(videoTestElement)) {
hlsVideoCodecs.push('h264');
}
if (hasH265Support(videoTestElement) || isTv()) {
hlsVideoCodecs.push('h265', 'hevc');
}
return hlsVideoCodecs;
}
/**
* Gets the supported HLS audio codecs
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Array of audio codecs supported in HLS
*/
export function getHlsAudioCodecs(
videoTestElement: HTMLVideoElement
): string[] {
const hlsVideoAudioCodecs = [];
if (supportsAc3InHls(videoTestElement)) {
hlsVideoAudioCodecs.push('ac3');
if (hasEac3Support(videoTestElement)) {
hlsVideoAudioCodecs.push('eac3');
}
}
if (hasAacSupport(videoTestElement)) {
hlsVideoAudioCodecs.push('aac');
}
if (getSupportedAudioCodecs('opus')) {
hlsVideoAudioCodecs.push('opus');
}
return hlsVideoAudioCodecs;
}
@@ -1,202 +0,0 @@
/**
* @deprecated - Check @/utils/playback-profiles/index
*/
import { hasVp8Support } from './mp4-video-formats';
import { getSupportedAudioCodecs } from './audio-formats';
import {
isTizen,
isTizen4,
isTizen5,
isTizen55,
isTv,
isWebOS
} from '@/utils/browser-detection';
/**
* Checks if the client can play the AC3 codec
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Determines if the browser has AC3 support
*/
export function hasAc3Support(videoTestElement: HTMLVideoElement): boolean {
if (isTv()) {
return true;
}
return !!videoTestElement
.canPlayType('audio/mp4; codecs="ac-3"')
.replace(/no/, '');
}
/**
* Checks if the client can play AC3 in a HLS stream
* @param videoTestElement - A HTML video element for testing codecs
* @returns Determines if the browser has AC3 support
*/
export function hasAc3InHlsSupport(
videoTestElement: HTMLVideoElement
): boolean {
if (isTizen() || isWebOS()) {
return true;
}
if (videoTestElement.canPlayType) {
return !!(
videoTestElement
.canPlayType('application/x-mpegurl; codecs="avc1.42E01E, ac-3"')
.replace(/no/, '')
|| videoTestElement
.canPlayType(
'application/vnd.apple.mpegURL; codecs="avc1.42E01E, ac-3"'
)
.replace(/no/, '')
);
}
return false;
}
/**
* Checks if the cliemt has E-AC3 codec support
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Determines if browser has EAC3 support
*/
export function hasEac3Support(videoTestElement: HTMLVideoElement): boolean {
if (isTv()) {
return true;
}
return !!videoTestElement
.canPlayType('audio/mp4; codecs="ec-3"')
.replace(/no/, '');
}
/**
* Checks if the client has AAC codec support
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Determines if browser has AAC support
*/
export function hasAacSupport(videoTestElement: HTMLVideoElement): boolean {
return !!videoTestElement
.canPlayType('video/mp4; codecs="avc1.640029, mp4a.40.2"')
.replace(/no/, '');
}
/**
* Checks if the client has MP2 codec support
*
* @returns Determines if browser has MP2 support
*/
export function hasMp2AudioSupport(): boolean {
return isTv();
}
/**
* Checks if the client has MP3 audio codec support
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Determines if browser has Mp3 support
*/
export function hasMp3AudioSupport(
videoTestElement: HTMLVideoElement
): boolean {
return !!(
videoTestElement
.canPlayType('video/mp4; codecs="avc1.640029, mp4a.69"')
.replace(/no/, '')
|| videoTestElement
.canPlayType('video/mp4; codecs="avc1.640029, mp4a.6B"')
.replace(/no/, '')
|| videoTestElement
.canPlayType('video/mp4; codecs="avc1.640029, mp3"')
.replace(/no/, '')
);
}
/**
* Determines DTS audio support
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Determines if browserr has DTS audio support
*/
export function hasDtsSupport(
videoTestElement: HTMLVideoElement
): boolean | string {
// DTS audio not supported in 2018 models (Tizen 4.0)
if (isTizen4() || isTizen5() || isTizen55()) {
return false;
}
return (
isTv()
|| videoTestElement
.canPlayType('video/mp4; codecs="dts-"')
.replace(/no/, '')
|| videoTestElement.canPlayType('video/mp4; codecs="dts+"').replace(/no/, '')
);
}
/**
* Gets an array of supported MP4 codecs
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Array of supported MP4 audio codecs
*/
export function getSupportedMP4AudioCodecs(
videoTestElement: HTMLVideoElement
): string[] {
const codecs = [];
if (hasAacSupport(videoTestElement)) {
codecs.push('aac');
}
if (hasMp3AudioSupport(videoTestElement)) {
codecs.push('mp3');
}
if (hasAc3Support(videoTestElement)) {
codecs.push('ac3');
if (hasEac3Support(videoTestElement)) {
codecs.push('eac3');
}
}
if (hasMp2AudioSupport()) {
codecs.push('mp2');
}
if (hasDtsSupport(videoTestElement)) {
codecs.push('dca', 'dts');
}
if (isTizen() || isWebOS()) {
codecs.push('pcm_s16le', 'pcm_s24le');
}
if (isTizen()) {
codecs.push('aac_latm');
}
if (getSupportedAudioCodecs('opus')) {
codecs.push('opus');
}
if (getSupportedAudioCodecs('flac')) {
codecs.push('flac');
}
if (getSupportedAudioCodecs('alac')) {
codecs.push('alac');
}
if (hasVp8Support(videoTestElement) || isTizen()) {
codecs.push('vorbis');
}
return codecs;
}
@@ -1,188 +0,0 @@
/**
* @deprecated - Check @/utils/playback-profiles/index
*/
import {
isApple,
isTizen,
isTizen55,
isTv,
isWebOS5
} from '@/utils/browser-detection';
/**
* Checks if the client has support for the H264 codec
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Determines if browser has H264 support
*/
export function hasH264Support(videoTestElement: HTMLVideoElement): boolean {
return !!(
videoTestElement
.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"')
.replace(/no/, '')
);
}
/**
* Checks if the client has support for the H265 codec
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Determines if browser has H265 support
*/
export function hasH265Support(videoTestElement: HTMLVideoElement): boolean {
if (isTv()) {
return true;
}
return !!(
videoTestElement.canPlayType
&& (videoTestElement
.canPlayType('video/mp4; codecs="hvc1.1.L120"')
.replace(/no/, '')
|| videoTestElement
.canPlayType('video/mp4; codecs="hev1.1.L120"')
.replace(/no/, '')
|| videoTestElement
.canPlayType('video/mp4; codecs="hvc1.1.0.L120"')
.replace(/no/, '')
|| videoTestElement
.canPlayType('video/mp4; codecs="hev1.1.0.L120"')
.replace(/no/, ''))
);
}
/**
* Checks if the client has support for the HEVC codec
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Determines if browser has HEVC Support
*/
export function hasHevcSupport(videoTestElement: HTMLVideoElement): boolean {
if (isTv()) {
return true;
}
return !!(
!!videoTestElement.canPlayType
&& (videoTestElement
.canPlayType('video/mp4; codecs="hvc1.1.L120"')
.replace(/no/, '')
|| videoTestElement
.canPlayType('video/mp4; codecs="hev1.1.L120"')
.replace(/no/, '')
|| videoTestElement
.canPlayType('video/mp4; codecs="hvc1.1.0.L120"')
.replace(/no/, '')
|| videoTestElement
.canPlayType('video/mp4; codecs="hev1.1.0.L120"')
.replace(/no/, ''))
);
}
/**
* Checks if the client has support for the AV1 codec
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Determines if browser has AV1 support
*/
export function hasAv1Support(videoTestElement: HTMLVideoElement): boolean {
if (
(isTizen() && isTizen55())
|| (isWebOS5() && window.outerHeight >= 2160)
) {
return true;
}
return !!(
videoTestElement
.canPlayType('video/webm; codecs="av01.0.15M.10"')
.replace(/no/, '')
);
}
/**
* Check if the client has support for the VC1 codec
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Determines if browser has VC1 support
*/
function hasVc1Support(videoTestElement: HTMLVideoElement): boolean {
return !!(
isTv()
|| videoTestElement.canPlayType('video/mp4; codecs="vc-1"').replace(/no/, '')
);
}
/**
* Checks if the client has support for the VP8 codec
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Determines if browser has VP8 support
*/
export function hasVp8Support(videoTestElement: HTMLVideoElement): boolean {
return !!(
videoTestElement.canPlayType('video/webm; codecs="vp8"').replace(/no/, '')
);
}
/**
* Checks if the client has support for the VP9 codec
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Determines if browser has VP9 support
*/
export function hasVp9Support(videoTestElement: HTMLVideoElement): boolean {
return !!(
videoTestElement.canPlayType('video/webm; codecs="vp9"').replace(/no/, '')
);
}
/**
* Queries the platform for the codecs suppers in an MP4 container.
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Array of codec identifiers.
*/
export function getSupportedMP4VideoCodecs(
videoTestElement: HTMLVideoElement
): string[] {
const codecs = [];
if (hasH264Support(videoTestElement)) {
codecs.push('h264');
}
if (
hasHevcSupport(videoTestElement) // Safari is lying on HDR and 60fps videos, use fMP4 instead
&& !isApple()
) {
codecs.push('hevc');
}
if (isTv()) {
codecs.push('mpeg2video');
}
if (hasVc1Support(videoTestElement)) {
codecs.push('vc1');
}
if (isTizen()) {
codecs.push('msmpeg4v2');
}
if (hasVp8Support(videoTestElement)) {
codecs.push('vp8');
}
if (hasVp9Support(videoTestElement)) {
codecs.push('vp9');
}
if (hasAv1Support(videoTestElement)) {
codecs.push('av1');
}
return codecs;
}
@@ -1,54 +0,0 @@
/**
* @deprecated - Check @/utils/playback-profiles/index
*/
import {
isEdge,
isTizen,
isTv,
supportsMediaSource
} from '@/utils/browser-detection';
/**
* Checks if the client can play native HLS
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns Determines if the browser can play native Hls
*/
export function canPlayNativeHls(videoTestElement: HTMLVideoElement): boolean {
if (isTizen()) {
return true;
}
return !!(
videoTestElement.canPlayType('application/x-mpegURL').replace(/no/, '')
|| videoTestElement
.canPlayType('application/vnd.apple.mpegURL')
.replace(/no/, '')
);
}
/**
* Determines if the browser can play Hls with Media Source Extensions
*/
export function canPlayHlsWithMSE(): boolean {
return supportsMediaSource();
}
/**
* Determines if the browser can play Mkvs
*/
export function hasMkvSupport(videoTestElement: HTMLVideoElement): boolean {
if (isTv()) {
return true;
}
if (
videoTestElement.canPlayType('video/x-matroska').replace(/no/, '')
|| videoTestElement.canPlayType('video/mkv').replace(/no/, '')
) {
return true;
}
return !!isEdge();
}
@@ -1,38 +0,0 @@
/**
* @deprecated - Check @/utils/playback-profiles/index
*/
import {
hasAacSupport,
hasAc3InHlsSupport,
hasAc3Support,
hasEac3Support,
hasMp3AudioSupport
} from './mp4-audio-formats';
/**
* List of supported Ts audio codecs
*/
export function getSupportedTsAudioCodecs(
videoTestElement: HTMLVideoElement
): string[] {
const codecs = [];
if (hasAacSupport(videoTestElement)) {
codecs.push('aac');
}
if (hasMp3AudioSupport(videoTestElement)) {
codecs.push('mp3');
}
if (hasAc3Support(videoTestElement) && hasAc3InHlsSupport(videoTestElement)) {
codecs.push('ac3');
if (hasEac3Support(videoTestElement)) {
codecs.push('eac3');
}
}
return codecs;
}
@@ -1,20 +0,0 @@
/**
* @deprecated - Check @/utils/playback-profiles/index
*/
import { hasH264Support } from './mp4-video-formats';
/**
* List of supported ts video codecs
*/
export function getSupportedTsVideoCodecs(
videoTestElement: HTMLVideoElement
): string[] {
const codecs = [];
if (hasH264Support(videoTestElement)) {
codecs.push('h264');
}
return codecs;
}
@@ -1,25 +0,0 @@
/**
* @deprecated - Check @/utils/playback-profiles/index
*/
import { isWebOS } from '@/utils/browser-detection';
/**
* Get an array of supported codecs
*/
export function getSupportedWebMAudioCodecs(
videoTestElement: HTMLVideoElement
): string[] {
const codecs = [];
codecs.push('vorbis');
if (
!isWebOS()
&& videoTestElement.canPlayType('audio/ogg; codecs="opus"').replace(/no/, '')
) {
codecs.push('opus');
}
return codecs;
}
@@ -1,32 +0,0 @@
/**
* @deprecated - Check @/utils/playback-profiles/index
*/
import {
hasAv1Support,
hasVp8Support,
hasVp9Support
} from './mp4-video-formats';
/**
* Get an array of supported codecs WebM video codecs
*/
export function getSupportedWebMVideoCodecs(
videoTestElement: HTMLVideoElement
): string[] {
const codecs = [];
if (hasVp8Support(videoTestElement)) {
codecs.push('vp8');
}
if (hasVp9Support(videoTestElement)) {
codecs.push('vp9');
}
if (hasAv1Support(videoTestElement)) {
codecs.push('av1');
}
return codecs;
}
@@ -1,58 +0,0 @@
/**
* @deprecated
* Since we're targeting modern environments/devices only, it makes sense to switch
* to the native MediaCapabilities API, widely supported on modern devices, but not in older.
*
* Given a media file, we should test with MC the compatibility of video, audio and subtitle streams
* independently:
* If success: Don't request transcoding and direct play that specific stream.
* If failure: Request transcoding of the failing streams to a previously hardcoded
* bitrate/codec combination
*
* For the hardcoded bitrate/codecs combination we can use what we know that are universally
* compatible, even without testing for explicit compatibility (we can do simple checks,
* but the more we do, the complex/less portable our solution can get).
* Examples: H264, AAC and VTT/SASS (thanks to JASSUB).
*
* Other codec combinations can be hardcoded, even if they're not direct-playable in
* most browsers (like H265 or AV1), so the few browsers that support them benefits from less bandwidth
* usage (although this will rarely happen: The most expected situations when transcoding
* is when the media's codecs are more "powerful" than what the client is capable of, and H265 is
* pretty modern, so it would've been catched-up by MediaCapabilities. However,
* we must take into account the playback of really old codecs like MPEG or H263,
* whose support are probably likely going to be removed from browsers,
* so MediaCapabilities reports as unsupported, so we would be going from an "inferior" codec to a
* "superior" codec in this situation)
*/
import type { DeviceProfile } from '@jellyfin/sdk/lib/generated-client';
import { getCodecProfiles } from './helpers/codec-profiles';
import { getDirectPlayProfiles } from './directplay-profile';
import { getTranscodingProfiles } from './transcoding-profile';
import { getSubtitleProfiles } from './subtitle-profile';
import { getResponseProfiles } from './response-profile';
/**
* Creates a device profile containing supported codecs for the active Cast device.
*
* @param videoTestElement - Dummy video element for compatibility tests
*/
function getDeviceProfile(videoTestElement: HTMLVideoElement): DeviceProfile {
// MaxStaticBitrate seems to be for offline sync only
return {
MaxStreamingBitrate: 120_000_000,
MaxStaticBitrate: 0,
MusicStreamingTranscodingBitrate: Math.min(120_000_000, 192_000),
DirectPlayProfiles: getDirectPlayProfiles(videoTestElement),
TranscodingProfiles: getTranscodingProfiles(videoTestElement),
ContainerProfiles: [],
CodecProfiles: getCodecProfiles(videoTestElement),
SubtitleProfiles: getSubtitleProfiles(),
ResponseProfiles: getResponseProfiles()
};
}
const videoTestElement = document.createElement('video');
const playbackProfile = getDeviceProfile(videoTestElement);
export default playbackProfile;
@@ -1,25 +0,0 @@
/**
* @deprecated - Check @/utils/playback-profiles/index
*/
import {
DlnaProfileType,
type ResponseProfile
} from '@jellyfin/sdk/lib/generated-client';
/**
* Returns a valid ResponseProfile for the current platform.
*
* @returns An array of subtitle profiles for the current platform.
*/
export function getResponseProfiles(): ResponseProfile[] {
const ResponseProfiles = [];
ResponseProfiles.push({
Type: DlnaProfileType.Video,
Container: 'm4v',
MimeType: 'video/mp4'
});
return ResponseProfiles;
}
@@ -1,34 +0,0 @@
/**
* @deprecated - Check @/utils/playback-profiles/index
*/
import {
SubtitleDeliveryMethod,
type SubtitleProfile
} from '@jellyfin/sdk/lib/generated-client';
/**
* Returns a valid SubtitleProfile for the current platform.
*
* @returns An array of subtitle profiles for the current platform.
*/
export function getSubtitleProfiles(): SubtitleProfile[] {
const SubtitleProfiles: SubtitleProfile[] = [];
SubtitleProfiles.push(
{
Format: 'vtt',
Method: SubtitleDeliveryMethod.External
},
{
Format: 'ass',
Method: SubtitleDeliveryMethod.External
},
{
Format: 'ssa',
Method: SubtitleDeliveryMethod.External
}
);
return SubtitleProfiles;
}
@@ -1,136 +0,0 @@
/**
* @deprecated - Check @/utils/playback-profiles/index
*/
import {
DlnaProfileType,
EncodingContext,
type TranscodingProfile
} from '@jellyfin/sdk/lib/generated-client';
import { getSupportedAudioCodecs } from './helpers/audio-formats';
import { getSupportedMP4AudioCodecs } from './helpers/mp4-audio-formats';
import {
getSupportedMP4VideoCodecs,
hasVp8Support
} from './helpers/mp4-video-formats';
import {
canPlayNativeHls,
canPlayHlsWithMSE,
hasMkvSupport
} from './helpers/transcoding-formats';
import { getSupportedTsAudioCodecs } from './helpers/ts-audio-formats';
import { getSupportedTsVideoCodecs } from './helpers/ts-video-formats';
import {
isTv,
isApple,
isEdge,
isChromiumBased,
isAndroid,
isTizen
} from '@/utils/browser-detection';
/**
* Returns a valid TranscodingProfile for the current platform.
*
* @param videoTestElement - A HTML video element for testing codecs
* @returns An array of transcoding profiles for the current platform.
*/
export function getTranscodingProfiles(
videoTestElement: HTMLVideoElement
): TranscodingProfile[] {
const TranscodingProfiles: TranscodingProfile[] = [];
const physicalAudioChannels = isTv() ? 6 : 2;
const hlsBreakOnNonKeyFrames = !!(
isApple()
|| (isEdge() && !isChromiumBased())
|| !canPlayNativeHls(videoTestElement)
);
const mp4AudioCodecs = getSupportedMP4AudioCodecs(videoTestElement);
const mp4VideoCodecs = getSupportedMP4VideoCodecs(videoTestElement);
const canPlayHls = canPlayNativeHls(videoTestElement) || canPlayHlsWithMSE();
if (canPlayHls) {
TranscodingProfiles.push({
// Hlsjs, edge, and android all seem to require ts container
Container:
!canPlayNativeHls(videoTestElement)
|| (isEdge() && !isChromiumBased())
|| isAndroid()
? 'ts'
: 'aac',
Type: DlnaProfileType.Audio,
AudioCodec: 'aac',
Context: EncodingContext.Streaming,
Protocol: 'hls',
MaxAudioChannels: physicalAudioChannels.toString(),
MinSegments: isApple() ? 2 : 1,
BreakOnNonKeyFrames: hlsBreakOnNonKeyFrames
});
}
for (const audioFormat of ['aac', 'mp3', 'opus', 'wav'].filter(format =>
getSupportedAudioCodecs(format)
)) {
TranscodingProfiles.push({
Container: audioFormat,
Type: DlnaProfileType.Audio,
AudioCodec: audioFormat,
Context: EncodingContext.Streaming,
Protocol: 'http',
MaxAudioChannels: physicalAudioChannels.toString()
});
}
const hlsInTsVideoCodecs = getSupportedTsVideoCodecs(videoTestElement);
const hlsInTsAudioCodecs = getSupportedTsAudioCodecs(videoTestElement);
if (
canPlayHls
&& hlsInTsVideoCodecs.length > 0
&& hlsInTsAudioCodecs.length > 0
) {
TranscodingProfiles.push({
Container: 'ts',
Type: DlnaProfileType.Video,
AudioCodec: hlsInTsAudioCodecs.join(','),
VideoCodec: hlsInTsVideoCodecs.join(','),
Context: EncodingContext.Streaming,
Protocol: 'hls',
MaxAudioChannels: physicalAudioChannels.toString(),
MinSegments: isApple() ? 2 : 1,
BreakOnNonKeyFrames: hlsBreakOnNonKeyFrames
});
}
if (hasMkvSupport(videoTestElement) && !isTizen()) {
TranscodingProfiles.push({
Container: 'mkv',
Type: DlnaProfileType.Video,
AudioCodec: mp4AudioCodecs.join(','),
VideoCodec: mp4VideoCodecs.join(','),
Context: EncodingContext.Streaming,
MaxAudioChannels: physicalAudioChannels.toString(),
CopyTimestamps: true
});
}
if (hasVp8Support(videoTestElement)) {
TranscodingProfiles.push({
Container: 'webm',
Type: DlnaProfileType.Video,
AudioCodec: 'vorbis',
VideoCodec: 'vpx',
Context: EncodingContext.Streaming,
Protocol: 'http',
/*
* If audio transcoding is needed, limit channels to number of physical audio channels
* Trying to transcode to 5 channels when there are only 2 speakers generally does not sound good
*/
MaxAudioChannels: physicalAudioChannels.toString()
});
}
return TranscodingProfiles;
}