Skip to content

Commit

Permalink
Merge pull request #116 from vitalygashkov/next
Browse files Browse the repository at this point in the history
Improved content type detection, added segment template sanitization, added audio protection parsing, added more tests
  • Loading branch information
vitalygashkov authored Nov 27, 2024
2 parents a5d3595 + 8ae7982 commit c55b507
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 28 deletions.
9 changes: 4 additions & 5 deletions lib/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ const parseAudioCodec = (codecs) => {
);
};

const isAudioCodec = (codecs) => {
const tryParseAudioCodec = (codecs) => {
try {
parseAudioCodec(codecs);
return true;
return parseAudioCodec(codecs);
} catch (e) {
return false;
return null;
}
};

Expand Down Expand Up @@ -142,7 +141,7 @@ const createAudioTrack = ({
module.exports = {
AUDIO_CODECS,
parseAudioCodec,
isAudioCodec,
tryParseAudioCodec,
createAudioTrack,
getDolbyDigitalPlusComplexityIndex,
checkIsDescriptive,
Expand Down
31 changes: 22 additions & 9 deletions lib/dash.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ const xml = require('./xml');
const { parseDuration, isLanguageTagValid } = require('./util');
const {
parseVideoCodec,
isVideoCodec,
tryParseVideoCodec,
parseDynamicRange,
createVideoTrack,
} = require('./video');
const {
parseAudioCodec,
isAudioCodec,
tryParseAudioCodec,
createAudioTrack,
getDolbyDigitalPlusComplexityIndex,
checkIsDescriptive,
} = require('./audio');
const {
parseSubtitleCodec,
isSubtitleCodec,
tryParseSubtitleCodec,
checkIsClosedCaption,
checkIsSdh,
checkIsForced,
Expand Down Expand Up @@ -79,17 +79,24 @@ const parseBaseUrl = (manifestUrl, mpd, period, representation) => {
return baseUrl;
};

const getTrackTypeByCodecs = (codecs) => {
if (tryParseVideoCodec(codecs)) return 'video';
else if (tryParseAudioCodec(codecs)) return 'audio';
else if (tryParseSubtitleCodec(codecs)) return 'text';
return null;
};

const parseContentTypes = (representation) => {
const codecs = representation.get('codecs');
const codecsType = isVideoCodec(codecs) ? 'video' : isAudioCodec(codecs) ? 'audio' : isSubtitleCodec(codecs) ? 'text' : null;
const mimeType = representation.get('mimeType');
const contentTypeByCodecs = getTrackTypeByCodecs(codecs);
const contentType =
codecsType || codecsTyperepresentation.get('contentType') || mimeType?.split('/')[0];
representation.get('contentType') || mimeType?.split('/')[0];
if (!contentType && !mimeType)
throw new Error(
'Unable to determine the format of a Representation, cannot continue...',
);
return { contentType, mimeType };
return { contentType: contentTypeByCodecs || contentType, mimeType };
};

const parseCodecs = (representation, contentType, mimeType) => {
Expand Down Expand Up @@ -208,6 +215,10 @@ const parseSegmentsFromTemplate = (
: DEFAULT_SEGMENTS_COUNT;
const bandwidth = representation.get('bandwidth');
const id = representation.get('id');

const sanitizeTemplate = (template) =>
template?.replace(/\$.*?(RepresentationID|Number|Time).*?\$/g, '$$$1$$');

const segments = [];
if (segmentTimeline) {
segments.push(
Expand All @@ -219,8 +230,9 @@ const parseSegmentsFromTemplate = (
),
);
} else {
const template = sanitizeTemplate(segmentTemplate.get('media'));
for (let i = startNumber; i < startNumber + segmentsCount; i++) {
const url = buildSegmentUrl(segmentTemplate.get('media'), {
const url = buildSegmentUrl(template, {
Bandwidth: bandwidth,
RepresentationID: id,
Number: i,
Expand All @@ -229,7 +241,9 @@ const parseSegmentsFromTemplate = (
segments.push({ url });
}
}
const initialization = segmentTemplate.get('initialization');
const initialization = sanitizeTemplate(
segmentTemplate.get('initialization'),
);
if (initialization) {
const url = buildSegmentUrl(initialization, {
Bandwidth: bandwidth,
Expand Down Expand Up @@ -305,7 +319,6 @@ const parseContentProtection = (contentProtections) => {
};

const parseManifest = async (text, url, fallbackLanguage) => {
text = text.replace(/\$.*?(RepresentationID|Number|Time).*?\$/g, "$$$1$$");
const mpd = appendUtils(xml.parse(text)).get('MPD');
const period = mpd.get('Period');
const durationString =
Expand Down
9 changes: 4 additions & 5 deletions lib/subtitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ const parseSubtitleCodec = (codecs) => {
);
};

const isSubtitleCodec = (codecs) => {
const tryParseSubtitleCodec = (codecs) => {
try {
parseSubtitleCodec(codecs);
return true;
return parseSubtitleCodec(codecs);
} catch (e) {
return false;
return null;
}
};

Expand Down Expand Up @@ -129,7 +128,7 @@ const createSubtitleTrack = ({

module.exports = {
parseSubtitleCodec,
isSubtitleCodec,
tryParseSubtitleCodec,
checkIsClosedCaption,
checkIsSdh,
checkIsForced,
Expand Down
9 changes: 4 additions & 5 deletions lib/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,11 @@ const parseVideoCodec = (codecs) => {
);
};

const isVideoCodec = (codecs) => {
const tryParseVideoCodec = (codecs) => {
try {
parseVideoCodec(codecs);
return true;
return parseVideoCodec(codecs);
} catch (e) {
return false;
return null;
}
};

Expand Down Expand Up @@ -194,7 +193,7 @@ const parseDynamicRange = (

module.exports = {
parseVideoCodec,
isVideoCodec,
tryParseVideoCodec,
parseDynamicRange,
createVideoTrack,
VIDEO_CODECS,
Expand Down
Loading

0 comments on commit c55b507

Please sign in to comment.