Skip to content

Commit

Permalink
fix: export generateSidxKey add multiple audio/vtt playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey committed Mar 15, 2021
1 parent 3910096 commit 2109892
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 52 deletions.
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { version } from '../package.json';
import { toM3u8 } from './toM3u8';
import { toM3u8, generateSidxKey } from './toM3u8';
import { toPlaylists } from './toPlaylists';
import { inheritAttributes } from './inheritAttributes';
import { stringToMpdXml } from './stringToMpdXml';
Expand Down Expand Up @@ -36,5 +36,6 @@ export {
inheritAttributes,
toPlaylists,
toM3u8,
addSidxSegmentsToPlaylist
addSidxSegmentsToPlaylist,
generateSidxKey
};
92 changes: 42 additions & 50 deletions src/toM3u8.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { findIndexes } from './utils/list';
import { addSegmentsToPlaylist } from './segment/segmentBase';
import { byteRangeToString } from './segment/urlType';

export const generateSidxKey = (sidx) => sidx &&
sidx.uri + '-' + byteRangeToString(sidx.byterange);

const mergeDiscontiguousPlaylists = playlists => {
const mergedPlaylists = values(playlists.reduce((acc, playlist) => {
// assuming playlist IDs are the same across periods
Expand Down Expand Up @@ -40,25 +43,24 @@ const mergeDiscontiguousPlaylists = playlists => {
});
};

const addSegmentInfoFromSidx = (playlists, sidxMapping = {}) => {
export const addSidxSegmentsToPlaylist = (playlist, sidxMapping) => {
const sidxKey = generateSidxKey(playlist.sidx);
const sidxMatch = sidxKey && sidxMapping[sidxKey] && sidxMapping[sidxKey].sidx;

if (sidxMatch) {
addSegmentsToPlaylist(playlist, sidxMatch, playlist.sidx.resolvedUri);
}

return playlist;
};

export const addSidxSegmentsToPlaylists = (playlists, sidxMapping = {}) => {
if (!Object.keys(sidxMapping).length) {
return playlists;
}

for (const i in playlists) {
const playlist = playlists[i];

if (!playlist.sidx) {
continue;
}

const sidxKey = playlist.sidx.uri + '-' +
byteRangeToString(playlist.sidx.byterange);
const sidxMatch = sidxMapping[sidxKey] && sidxMapping[sidxKey].sidx;

if (playlist.sidx && sidxMatch) {
addSegmentsToPlaylist(playlist, sidxMatch, playlist.sidx.resolvedUri);
}
playlists[i] = addSidxSegmentsToPlaylist(playlists[i], sidxMapping);
}

return playlists;
Expand Down Expand Up @@ -144,22 +146,17 @@ export const organizeAudioPlaylists = (playlists, sidxMapping = {}) => {
}

// skip if we already have the highest quality audio for a language
if (a[label] &&
a[label].playlists[0].attributes.BANDWIDTH >
playlist.attributes.bandwidth) {
return a;
if (!a[label]) {
a[label] = {
language,
autoselect: true,
default: role === 'main',
playlists: [],
uri: ''
};
}

a[label] = {
language,
autoselect: true,
default: role === 'main',
playlists: addSegmentInfoFromSidx(
[formatAudioPlaylist(playlist)],
sidxMapping
),
uri: ''
};
a[label].playlists.push(addSidxSegmentsToPlaylist(formatAudioPlaylist(playlist), sidxMapping));

if (typeof mainPlaylist === 'undefined' && role === 'main') {
mainPlaylist = playlist;
Expand All @@ -183,21 +180,16 @@ export const organizeVttPlaylists = (playlists, sidxMapping = {}) => {
return playlists.reduce((a, playlist) => {
const label = playlist.attributes.lang || 'text';

// skip if we already have subtitles
if (a[label]) {
return a;
if (!a[label]) {
a[label] = {
language: label,
default: false,
autoselect: false,
playlists: [],
uri: ''
};
}

a[label] = {
language: label,
default: false,
autoselect: false,
playlists: addSegmentInfoFromSidx(
[formatVttPlaylist(playlist)],
sidxMapping
),
uri: ''
};
a[label].playlists.push(addSidxSegmentsToPlaylist(formatVttPlaylist(playlist), sidxMapping));

return a;
}, {});
Expand Down Expand Up @@ -237,6 +229,13 @@ export const formatVideoPlaylist = ({ attributes, segments, sidx }) => {
return playlist;
};

const videoOnly = ({ attributes }) =>
attributes.mimeType === 'video/mp4' || attributes.mimeType === 'video/webm' || attributes.contentType === 'video';
const audioOnly = ({ attributes }) =>
attributes.mimeType === 'audio/mp4' || attributes.mimeType === 'audio/webm' || attributes.contentType === 'audio';
const vttOnly = ({ attributes }) =>
attributes.mimeType === 'text/vtt' || attributes.contentType === 'text';

export const toM3u8 = (dashPlaylists, locations, sidxMapping = {}) => {
if (!dashPlaylists.length) {
return {};
Expand All @@ -250,13 +249,6 @@ export const toM3u8 = (dashPlaylists, locations, sidxMapping = {}) => {
minimumUpdatePeriod
} = dashPlaylists[0].attributes;

const videoOnly = ({ attributes }) =>
attributes.mimeType === 'video/mp4' || attributes.mimeType === 'video/webm' || attributes.contentType === 'video';
const audioOnly = ({ attributes }) =>
attributes.mimeType === 'audio/mp4' || attributes.mimeType === 'audio/webm' || attributes.contentType === 'audio';
const vttOnly = ({ attributes }) =>
attributes.mimeType === 'text/vtt' || attributes.contentType === 'text';

const videoPlaylists = mergeDiscontiguousPlaylists(dashPlaylists.filter(videoOnly)).map(formatVideoPlaylist);
const audioPlaylists = mergeDiscontiguousPlaylists(dashPlaylists.filter(audioOnly));
const vttPlaylists = dashPlaylists.filter(vttOnly);
Expand All @@ -274,7 +266,7 @@ export const toM3u8 = (dashPlaylists, locations, sidxMapping = {}) => {
},
uri: '',
duration,
playlists: addSegmentInfoFromSidx(videoPlaylists, sidxMapping)
playlists: addSidxSegmentsToPlaylists(videoPlaylists, sidxMapping)
};

if (minimumUpdatePeriod >= 0) {
Expand Down

0 comments on commit 2109892

Please sign in to comment.