Skip to content

Commit

Permalink
update media capabilities detection
Browse files Browse the repository at this point in the history
  • Loading branch information
unclekingpin committed Nov 2, 2023
1 parent c216400 commit b4287f1
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 124 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"hat": "0.0.3",
"hls.js": "https://github.com/Stremio/hls.js/releases/download/v1.2.3-patch1/hls.js-1.2.3-patch1.tgz",
"lodash.clonedeep": "4.5.0",
"lodash.mergewith": "4.6.2",
"magnet-uri": "6.2.0",
"url": "0.11.0",
"video-name-parser": "1.4.6",
Expand Down
189 changes: 120 additions & 69 deletions src/mediaCapabilities.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,99 @@
var VIDEO_CODEC_CONFIGS = [
{
codec: 'h264',
mime: 'video/mp4; codecs="avc1.42E01E"',
},
{
codec: 'h265',
mime: 'video/mp4; codecs="hev1.1.6.L150.B0"',
aliases: ['hevc']
},
{
codec: 'vp8',
mime: 'video/mp4; codecs="vp8"'
},
{
codec: 'vp9',
mime: 'video/mp4; codecs="vp9"'
}
];
var MP4_CONFIG = {
VIDEO_CODECS: [
{
codec: "h264",
force: window.chrome || window.cast,
mime: 'video/mp4; codecs="avc1.42E01E"',
},
{
codec: "h265",
force: window.chrome || window.cast,
mime: 'video/mp4; codecs="hev1.1.6.L150.B0"',
aliases: ["hevc"],
},
{
codec: "vp8",
mime: 'video/mp4; codecs="vp8"',
},
{
codec: "vp9",
mime: 'video/mp4; codecs="vp9"',
},
],
AUDIO_CODEC: [
{
codec: "aac",
force: window.chrome || window.cast,
mime: 'audio/mp4; codecs="mp4a.40.2"',
},
{
codec: "mp3",
force: window.chrome || window.cast,
mime: 'audio/mp4; codecs="mp3"',
},
{
codec: "ac3",
mime: 'audio/mp4; codecs="ac-3"',
},
{
codec: "eac3",
mime: 'audio/mp4; codecs="ec-3"',
},
{
codec: "vorbis",
mime: 'audio/mp4; codecs="vorbis"',
},
{
codec: "opus",
mime: 'audio/mp4; codecs="opus"',
},
],
};

var AUDIO_CODEC_CONFIGS = [
{
codec: 'aac',
mime: 'audio/mp4; codecs="mp4a.40.2"'
},
{
codec: 'mp3',
mime: 'audio/mp4; codecs="mp3"'
},
{
codec: 'ac3',
mime: 'audio/mp4; codecs="ac-3"'
},
{
codec: 'eac3',
mime: 'audio/mp4; codecs="ec-3"'
},
{
codec: 'vorbis',
mime: 'audio/mp4; codecs="vorbis"'
},
{
codec: 'opus',
mime: 'audio/mp4; codecs="opus"'
}
];
var MATROSKA_CONFIG = {
VIDEO_CODECS: [
{
codec: "h264",
force: window.chrome || window.cast,
},
{
codec: "h265",
force: window.chrome || window.cast,
aliases: ["hevc"],
},
{
codec: "vp8",
mime: 'video/webm; codecs="vp8"',
},
{
codec: "vp9",
mime: 'video/webm; codecs="vp9"',
},
],
AUDIO_CODEC: [
{
codec: "aac",
force: window.chrome || window.cast,
},
{
codec: "mp3",
force: window.chrome || window.cast,
},
{
codec: "vorbis",
mime: 'audio/webm; codecs="vorbis"',
},
{
codec: "opus",
mime: 'audio/webm; codecs="opus"',
},
],
};

function canPlay(config, options) {
return options.mediaElement.canPlayType(config.mime) ?
[config.codec].concat(config.aliases || [])
:
[];
return config.force || options.mediaElement.canPlayType(config.mime)
? [config.codec].concat(config.aliases || [])
: [];
}

function getMaxAudioChannels() {
Expand All @@ -66,28 +110,35 @@ function getMaxAudioChannels() {
}

function getMediaCapabilities() {
var mediaElement = document.createElement('video');
var formats = ['mp4', 'webm'];
var videoCodecs = VIDEO_CODEC_CONFIGS
.map(function(config) {
return canPlay(config, { mediaElement: mediaElement });
})
.reduce(function(result, value) {
return result.concat(value);
}, []);
var audioCodecs = AUDIO_CODEC_CONFIGS
.map(function(config) {
return canPlay(config, { mediaElement: mediaElement });
})
.reduce(function(result, value) {
return result.concat(value);
}, []);
var mediaElement = document.createElement("video");
var maxAudioChannels = getMaxAudioChannels();
return {
formats: formats,
videoCodecs: videoCodecs,
audioCodecs: audioCodecs,
maxAudioChannels: maxAudioChannels
mp4: {
videoCodecs: MP4_CONFIG.VIDEO_CODECS.map(function (config) {
return canPlay(config, { mediaElement: mediaElement });
}).reduce(function (result, value) {
return result.concat(value);
}, []),
audioCodecs: MP4_CONFIG.AUDIO_CODEC.map(function (config) {
return canPlay(config, { mediaElement: mediaElement });
}).reduce(function (result, value) {
return result.concat(value);
}, []),
maxAudioChannels,
},
'matroska,webm': {
videoCodecs: MATROSKA_CONFIG.VIDEO_CODECS.map(function (config) {
return canPlay(config, { mediaElement: mediaElement });
}).reduce(function (result, value) {
return result.concat(value);
}, []),
audioCodecs: MATROSKA_CONFIG.AUDIO_CODEC.map(function (config) {
return canPlay(config, { mediaElement: mediaElement });
}).reduce(function (result, value) {
return result.concat(value);
}, []),
maxAudioChannels,
},
};
}

Expand Down
Loading

0 comments on commit b4287f1

Please sign in to comment.