Skip to content

Commit

Permalink
fix: Add Opus and AV1 detection in TS (#6385)
Browse files Browse the repository at this point in the history
This detection is necessary to be able to correctly detect whether a
media playlist of HLS that has TS segments is compatible or not.

Example:
https://raw.githubusercontent.com/obecny/hls-opus-testing/main/stream.m3u8
(media playlist with Opus)
  • Loading branch information
avelad committed Apr 2, 2024
1 parent fdc7c6c commit bc23fff
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
8 changes: 8 additions & 0 deletions lib/media/segment_utils.js
Expand Up @@ -72,6 +72,10 @@ shaka.media.SegmentUtils = class {
codecs.push('ec-3');
hasAudio = true;
break;
case 'opus':
codecs.push('opus');
hasAudio = true;
break;
}
switch (tsCodecs.video) {
case 'avc':
Expand All @@ -90,6 +94,10 @@ shaka.media.SegmentUtils = class {
}
hasVideo = true;
break;
case 'av1':
codecs.push('av01.0.01M.08');
hasVideo = true;
break;
}
if (!codecs.length) {
return null;
Expand Down
44 changes: 36 additions & 8 deletions lib/util/ts_parser.js
Expand Up @@ -10,6 +10,7 @@ goog.require('shaka.Deprecate');
goog.require('shaka.log');
goog.require('shaka.util.ExpGolomb');
goog.require('shaka.util.Id3Utils');
goog.require('shaka.util.StringUtils');
goog.require('shaka.util.Uint8ArrayUtils');


Expand Down Expand Up @@ -233,6 +234,7 @@ shaka.util.TsParser = class {
* @private
*/
parsePMT_(data, offset) {
const StringUtils = shaka.util.StringUtils;
const result = {
audio: -1,
video: -1,
Expand All @@ -255,26 +257,52 @@ shaka.util.TsParser = class {
case 0x06:
// stream_type 6 can mean a lot of different things in case of DVB.
// We need to look at the descriptors. Right now, we're only
// interested in AC-3 and EC-3 audio, so we do the descriptor parsing
// only when we don't have an audio PID yet.
if (result.audio == -1 && esInfoLength > 0) {
// interested in a few audio and video formats,.
if (esInfoLength > 0) {
let parsePos = offset + 5;
let remaining = esInfoLength;
// Descriptor info: https://www.streamguru.de/mpeg-analyzer/supported-descriptor-list/
while (remaining > 2) {
const descriptorId = data[parsePos];
const descriptorLen = data[parsePos + 1] + 2;
switch (descriptorId) {
// Registration descriptor
case 0x05: {
const registrationData =
data.subarray(parsePos + 2, parsePos + descriptorLen);
const registration =
StringUtils.fromCharCode(registrationData);
if (result.audio == -1 && registration === 'Opus') {
result.audio = pid;
result.audioCodec = 'opus';
} else if (result.video == -1 && registration === 'AV01') {
result.video = pid;
result.videoCodec = 'av1';
}
break;
}
// DVB Descriptor for AC-3
case 0x6a:
result.audio = pid;
result.audioCodec = 'ac3';
if (result.audio == -1) {
result.audio = pid;
result.audioCodec = 'ac3';
}
break;
// DVB Descriptor for EC-3
case 0x7a:
result.audio = pid;
result.audioCodec = 'ec3';
if (result.audio == -1) {
result.audio = pid;
result.audioCodec = 'ec3';
}
break;
// DVB Descriptor for AAC
case 0x7c:
if (result.audio == -1) {
result.audio = pid;
result.audioCodec = 'aac';
}
break;
}
const descriptorLen = data[parsePos + 1] + 2;
parsePos += descriptorLen;
remaining -= descriptorLen;
}
Expand Down

0 comments on commit bc23fff

Please sign in to comment.