Skip to content

Commit

Permalink
fix(HLS): Fix support legacy AVC1 codec used in HLS (shaka-project#4716)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad committed Nov 19, 2022
1 parent 884c4ca commit c3ff8e5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
40 changes: 30 additions & 10 deletions lib/util/stream_utils.js
Expand Up @@ -438,15 +438,20 @@ shaka.util.StreamUtils = class {
const video = variant.video;
const ContentType = shaka.util.ManifestParserUtils.ContentType;
if (video) {
let videoCodecs = shaka.util.StreamUtils.patchVp9(video.codecs);
let videoCodecs =
shaka.util.StreamUtils.getCorrectVideoCodecs_(video.codecs);
// For multiplexed streams. Here we must check the audio of the
// stream to see if it is compatible.
if (video.codecs.includes(',')) {
const allCodecs = video.codecs.split(',');
videoCodecs = shaka.util.ManifestParserUtils.guessCodecs(
ContentType.VIDEO, allCodecs);
const audioCodecs = shaka.util.ManifestParserUtils.guessCodecs(
videoCodecs =
shaka.util.StreamUtils.getCorrectVideoCodecs_(videoCodecs);
let audioCodecs = shaka.util.ManifestParserUtils.guessCodecs(
ContentType.AUDIO, allCodecs);
audioCodecs =
shaka.util.StreamUtils.getCorrectAudioCodecs_(audioCodecs);
const audioFullType = shaka.util.MimeUtils.getFullOrConvertedType(
video.mimeType, audioCodecs, ContentType.AUDIO);
if (!MediaSource.isTypeSupported(audioFullType)) {
Expand Down Expand Up @@ -568,7 +573,8 @@ shaka.util.StreamUtils = class {
const allCodecs = video.codecs.split(',');
videoCodecs = shaka.util.ManifestParserUtils.guessCodecs(
ContentType.VIDEO, allCodecs);
videoCodecs = shaka.util.StreamUtils.patchVp9(videoCodecs);
videoCodecs =
shaka.util.StreamUtils.getCorrectVideoCodecs_(videoCodecs);
const audioCodecs = shaka.util.ManifestParserUtils.guessCodecs(
ContentType.AUDIO, allCodecs);

Expand All @@ -582,7 +588,7 @@ shaka.util.StreamUtils = class {
spatialRendering: false,
};
}
videoCodecs = shaka.util.StreamUtils.patchVp9(videoCodecs);
videoCodecs = shaka.util.StreamUtils.getCorrectVideoCodecs_(videoCodecs);
const fullType = shaka.util.MimeUtils.getFullOrConvertedType(
video.mimeType, videoCodecs, ContentType.VIDEO);
// VideoConfiguration
Expand Down Expand Up @@ -751,15 +757,29 @@ shaka.util.StreamUtils = class {


/**
* MediaCapabilities supports 'vp09...' codecs, but not 'vp9'. Translate vp9
* codec strings into 'vp09...', to allow such content to play with
* mediaCapabilities enabled.
*
* Generates the correct video codec for MediaDecodingConfiguration and
* for MediaSource.isTypeSupported.
* @param {string} codec
* @return {string}
* @private
*/
static patchVp9(codec) {
if (codec == 'vp9') {
static getCorrectVideoCodecs_(codec) {
if (codec.includes('avc1')) {
// Convert avc1 codec string from RFC-4281 to RFC-6381 for
// MediaSource.isTypeSupported
// Example, convert avc1.66.30 to avc1.42001e (0x42 == 66 and 0x1e == 30)
const avcdata = codec.split('.');
if (avcdata.length == 3) {
let result = avcdata.shift() + '.';
result += parseInt(avcdata.shift(), 10).toString(16);
result +=
('000' + parseInt(avcdata.shift(), 10).toString(16)).slice(-4);
return result;
}
} else if (codec == 'vp9') {
// MediaCapabilities supports 'vp09...' codecs, but not 'vp9'. Translate
// vp9 codec strings into 'vp09...', to allow such content to play with
// mediaCapabilities enabled.
// This means profile 0, level 4.1, 8-bit color. This supports 1080p @
// 60Hz. See https://en.wikipedia.org/wiki/VP9#Levels
//
Expand Down
18 changes: 18 additions & 0 deletions test/util/stream_utils_unit.js
Expand Up @@ -717,6 +717,24 @@ describe('StreamUtils', () => {

expect(manifest.variants.length).toBe(1);
});

it('supports legacy AVC1 codec', async () => {
if (!MediaSource.isTypeSupported('video/mp4; codecs="avc1.42001e"')) {
pending('Codec avc1.42001e is not supported by the platform.');
}
manifest = shaka.test.ManifestGenerator.generate((manifest) => {
manifest.addVariant(0, (variant) => {
variant.addVideo(1, (stream) => {
stream.mime('video/mp4', 'avc1.66.30');
});
});
});

await shaka.util.StreamUtils.filterManifest(
fakeDrmEngine, /* currentVariant= */ null, manifest);

expect(manifest.variants.length).toBe(1);
});
});

describe('chooseCodecsAndFilterManifest', () => {
Expand Down

0 comments on commit c3ff8e5

Please sign in to comment.