-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: export generateSidxKey, multiple audio/vtt playlists #123
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,7 +67,7 @@ export const segmentsFromBase = (attributes) => { | |
* @param {Object} sidx the parsed sidx box | ||
* @return {Object} the playlist object with the updated sidx information | ||
*/ | ||
export const addSegmentsToPlaylist = (playlist, sidx, baseUrl) => { | ||
export const addSidxSegmentsToPlaylist = (playlist, sidx, baseUrl) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed this function, as it was named terribly. It only adds sidx segments to a playlist, and we have other functions that add segments from our dash manifest to a playlist object. |
||
// Retain init segment information | ||
const initSegment = playlist.sidx.map ? playlist.sidx.map : null; | ||
// Retain source duration from initial master manifest parsing | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import { values } from './utils/object'; | ||
import { findIndexes } from './utils/list'; | ||
import { addSegmentsToPlaylist } from './segment/segmentBase'; | ||
import { addSidxSegmentsToPlaylist as addSidxSegmentsToPlaylist_ } from './segment/segmentBase'; | ||
import { byteRangeToString } from './segment/urlType'; | ||
|
||
export const generateSidxKey = (sidx) => sidx && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need this function in VHS, we re-implemented it there which was fairly pointless imo. |
||
sidx.uri + '-' + byteRangeToString(sidx.byterange); | ||
|
||
const mergeDiscontiguousPlaylists = playlists => { | ||
const mergedPlaylists = values(playlists.reduce((acc, playlist) => { | ||
// assuming playlist IDs are the same across periods | ||
|
@@ -40,31 +43,30 @@ const mergeDiscontiguousPlaylists = playlists => { | |
}); | ||
}; | ||
|
||
const addSegmentInfoFromSidx = (playlists, sidxMapping = {}) => { | ||
export const addSidxSegmentsToPlaylist = (playlist, sidxMapping) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add Sidx segments to a single playlist. We used to do this only on an array of playlists, but it was weird because for audio/vtt playlists we needed to pass in a single playlist. The naming of these functions was also weird. |
||
const sidxKey = generateSidxKey(playlist.sidx); | ||
const sidxMatch = sidxKey && sidxMapping[sidxKey] && sidxMapping[sidxKey].sidx; | ||
|
||
if (sidxMatch) { | ||
addSidxSegmentsToPlaylist_(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; | ||
}; | ||
|
||
export const formatAudioPlaylist = ({ attributes, segments, sidx }) => { | ||
export const formatAudioPlaylist = ({ attributes, segments, sidx }, isAudioOnly) => { | ||
const playlist = { | ||
attributes: { | ||
NAME: attributes.id, | ||
|
@@ -89,6 +91,11 @@ export const formatAudioPlaylist = ({ attributes, segments, sidx }) => { | |
playlist.sidx = sidx; | ||
} | ||
|
||
if (isAudioOnly) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we are an audio only playlist, we still want subtitles and audio groups to work correctly. |
||
playlist.attributes.AUDIO = 'audio'; | ||
playlist.attributes.SUBTITLES = 'subs'; | ||
} | ||
|
||
return playlist; | ||
}; | ||
|
||
|
@@ -127,7 +134,7 @@ export const formatVttPlaylist = ({ attributes, segments }) => { | |
}; | ||
}; | ||
|
||
export const organizeAudioPlaylists = (playlists, sidxMapping = {}) => { | ||
export const organizeAudioPlaylists = (playlists, sidxMapping = {}, isAudioOnly = false) => { | ||
let mainPlaylist; | ||
|
||
const formattedPlaylists = playlists.reduce((a, playlist) => { | ||
|
@@ -143,23 +150,19 @@ export const organizeAudioPlaylists = (playlists, sidxMapping = {}) => { | |
label = `${playlist.attributes.lang}${roleLabel}`; | ||
} | ||
|
||
// 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]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the fix here, we should create the group, and add playlists to it. Not only create a group with one playlist ever. |
||
a[label] = { | ||
language, | ||
autoselect: true, | ||
default: role === 'main', | ||
playlists: [], | ||
uri: '' | ||
}; | ||
} | ||
|
||
a[label] = { | ||
language, | ||
autoselect: true, | ||
default: role === 'main', | ||
playlists: addSegmentInfoFromSidx( | ||
[formatAudioPlaylist(playlist)], | ||
sidxMapping | ||
), | ||
uri: '' | ||
}; | ||
const formatted = addSidxSegmentsToPlaylist(formatAudioPlaylist(playlist, isAudioOnly), sidxMapping); | ||
|
||
a[label].playlists.push(formatted); | ||
|
||
if (typeof mainPlaylist === 'undefined' && role === 'main') { | ||
mainPlaylist = playlist; | ||
|
@@ -183,21 +186,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]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same fix as audio playlists. |
||
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; | ||
}, {}); | ||
|
@@ -237,6 +235,13 @@ export const formatVideoPlaylist = ({ attributes, segments, sidx }) => { | |
return playlist; | ||
}; | ||
|
||
const videoOnly = ({ attributes }) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We defined these in a function before, needlessly creating these functions on every mpd parse isn't something we should do. |
||
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 {}; | ||
|
@@ -250,13 +255,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); | ||
|
@@ -274,7 +272,7 @@ export const toM3u8 = (dashPlaylists, locations, sidxMapping = {}) => { | |
}, | ||
uri: '', | ||
duration, | ||
playlists: addSegmentInfoFromSidx(videoPlaylists, sidxMapping) | ||
playlists: addSidxSegmentsToPlaylists(videoPlaylists, sidxMapping) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new name, same function. |
||
}; | ||
|
||
if (minimumUpdatePeriod >= 0) { | ||
|
@@ -289,8 +287,10 @@ export const toM3u8 = (dashPlaylists, locations, sidxMapping = {}) => { | |
master.suggestedPresentationDelay = suggestedPresentationDelay; | ||
} | ||
|
||
const isAudioOnly = master.playlists.length === 0; | ||
|
||
if (audioPlaylists.length) { | ||
master.mediaGroups.AUDIO.audio = organizeAudioPlaylists(audioPlaylists, sidxMapping); | ||
master.mediaGroups.AUDIO.audio = organizeAudioPlaylists(audioPlaylists, sidxMapping, isAudioOnly); | ||
} | ||
|
||
if (vttPlaylists.length) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
export const parsedManifest = { | ||
allowCache: true, | ||
discontinuityStarts: [], | ||
segments: [], | ||
endList: true, | ||
mediaGroups: { | ||
'AUDIO': { | ||
audio: { | ||
en: { | ||
language: 'en', | ||
autoselect: true, | ||
default: true, | ||
playlists: [ | ||
{ | ||
attributes: { | ||
'NAME': '0', | ||
'BANDWIDTH': 130803, | ||
'CODECS': 'mp4a.40.2', | ||
'PROGRAM-ID': 1, | ||
'AUDIO': 'audio', | ||
'SUBTITLES': 'subs' | ||
}, | ||
uri: '', | ||
endList: true, | ||
timeline: 0, | ||
resolvedUri: '', | ||
targetDuration: 60, | ||
segments: [], | ||
mediaSequence: 1, | ||
sidx: { | ||
uri: 'http://example.com/audio_en_2c_128k_aac.mp4', | ||
resolvedUri: 'http://example.com/audio_en_2c_128k_aac.mp4', | ||
byterange: { | ||
length: 224, | ||
offset: 786 | ||
}, | ||
map: { | ||
uri: '', | ||
resolvedUri: 'http://example.com/audio_en_2c_128k_aac.mp4', | ||
byterange: { | ||
length: 786, | ||
offset: 0 | ||
} | ||
}, | ||
duration: 60, | ||
timeline: 0, | ||
number: 0 | ||
} | ||
} | ||
], | ||
uri: '' | ||
}, | ||
es: { | ||
language: 'es', | ||
autoselect: true, | ||
default: false, | ||
playlists: [ | ||
{ | ||
attributes: { | ||
'NAME': '1', | ||
'BANDWIDTH': 130405, | ||
'CODECS': 'mp4a.40.2', | ||
'PROGRAM-ID': 1, | ||
'AUDIO': 'audio', | ||
'SUBTITLES': 'subs' | ||
}, | ||
uri: '', | ||
endList: true, | ||
timeline: 0, | ||
resolvedUri: '', | ||
targetDuration: 60, | ||
segments: [], | ||
mediaSequence: 1, | ||
sidx: { | ||
uri: 'http://example.com/audio_es_2c_128k_aac.mp4', | ||
resolvedUri: 'http://example.com/audio_es_2c_128k_aac.mp4', | ||
byterange: { | ||
length: 224, | ||
offset: 786 | ||
}, | ||
map: { | ||
uri: '', | ||
resolvedUri: 'http://example.com/audio_es_2c_128k_aac.mp4', | ||
byterange: { | ||
length: 786, | ||
offset: 0 | ||
} | ||
}, | ||
duration: 60, | ||
timeline: 0, | ||
number: 0 | ||
} | ||
} | ||
], | ||
uri: '' | ||
} | ||
} | ||
}, | ||
'VIDEO': {}, | ||
'CLOSED-CAPTIONS': {}, | ||
'SUBTITLES': {} | ||
}, | ||
uri: '', | ||
duration: 60, | ||
playlists: [] | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!--Generated with https://github.com/google/shaka-packager version v2.4.1-c731217-release--> | ||
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:mpeg:dash:schema:mpd:2011 DASH-MPD.xsd" profiles="urn:mpeg:dash:profile:isoff-on-demand:2011" minBufferTime="PT2S" type="static" mediaPresentationDuration="PT60S"> | ||
<Period id="0"> | ||
<AdaptationSet id="0" contentType="audio" lang="en" subsegmentAlignment="true"> | ||
<Representation id="0" bandwidth="130803" codecs="mp4a.40.2" mimeType="audio/mp4" audioSamplingRate="48000"> | ||
<AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="2"/> | ||
<BaseURL>http://example.com/audio_en_2c_128k_aac.mp4</BaseURL> | ||
<SegmentBase indexRange="786-1009" timescale="48000"> | ||
<Initialization range="0-785"/> | ||
</SegmentBase> | ||
</Representation> | ||
</AdaptationSet> | ||
<AdaptationSet id="1" contentType="audio" lang="es" subsegmentAlignment="true"> | ||
<Representation id="1" bandwidth="130405" codecs="mp4a.40.2" mimeType="audio/mp4" audioSamplingRate="48000"> | ||
<AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="2"/> | ||
<BaseURL>http://example.com/audio_es_2c_128k_aac.mp4</BaseURL> | ||
<SegmentBase indexRange="786-1009" timescale="48000"> | ||
<Initialization range="0-785"/> | ||
</SegmentBase> | ||
</Representation> | ||
</AdaptationSet> | ||
|
||
</Period> | ||
</MPD> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we duplicate this exact function in vhs right now. Seems better to have one source of truth.