Skip to content
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

Parse <SegmentList> and <SegmentBase> #18

Merged
merged 15 commits into from
Feb 5, 2018
5 changes: 4 additions & 1 deletion src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ export default {
INVALID_NUMBER_OF_PERIOD: 'INVALID_NUMBER_OF_PERIOD',
DASH_EMPTY_MANIFEST: 'DASH_EMPTY_MANIFEST',
DASH_INVALID_XML: 'DASH_INVALID_XML',
UNSUPPORTED_SEGMENTATION_TYPE: 'UNSUPPORTED_SEGMENTATION_TYPE'
UNSUPPORTED_SEGMENTATION_TYPE: 'UNSUPPORTED_SEGMENTATION_TYPE',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this error

NO_BASE_URL: 'NO_BASE_URL',
MISSING_SEGMENT_INFORMATION: 'MISSING_SEGMENT_INFORMATION',
SEGMENT_TIME_UNSPECIFIED: 'SEGMENT_TIME_UNSPECIFIED'
};
16 changes: 12 additions & 4 deletions src/inheritAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { flatten } from './utils/list';
import { shallowMerge, getAttributes } from './utils/object';
import { parseDuration } from './utils/time';
import { findChildren, getContent } from './utils/xml';
import resolveUrl from './resolveUrl';
import resolveUrl from './utils/resolveUrl';
import errors from './errors';

/**
Expand Down Expand Up @@ -50,16 +50,24 @@ export const buildBaseUrls = (referenceUrls, baseUrlElements) => {
*/
export const getSegmentInformation = (adaptationSet) => {
const segmentTemplate = findChildren(adaptationSet, 'SegmentTemplate')[0];
const segmentTimeline =
segmentTemplate && findChildren(segmentTemplate, 'SegmentTimeline')[0];
const segmentList = findChildren(adaptationSet, 'SegmentList')[0];
const segmentUrls = segmentList && findChildren(segmentList, 'SegmentURL');
const segmentBase = findChildren(adaptationSet, 'SegmentBase')[0];
const segmentTimelineNode = segmentList ? segmentList : segmentTemplate;
const segmentTimeline =
segmentTimelineNode && findChildren(segmentTimelineNode, 'SegmentTimeline')[0];

return {
template: segmentTemplate && getAttributes(segmentTemplate),
timeline: segmentTimeline &&
findChildren(segmentTimeline, 'S').map(s => getAttributes(s)),
list: segmentList && getAttributes(segmentList),
list: segmentList &&
shallowMerge(getAttributes(segmentList),
{
segmentUrls: segmentUrls &&
segmentUrls.map(s =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could move this map to findChildren(segmentList, 'SegmentURL').map(..., then this just becomes { segmentUrls }

shallowMerge({ tag: 'SegmentURL' }, getAttributes(s)))
}),
base: segmentBase && getAttributes(segmentBase)
};
};
Expand Down
62 changes: 62 additions & 0 deletions src/segment/segmentBase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import resolveUrl from '../utils/resolveUrl';
import errors from '../errors';
import { parseByDuration } from './timeParser';

/**
* Translates SegmentBase into a set of segments.
* (DASH SPEC Section 5.3.9.3.2) contains a set of <SegmentURL> nodes. Each
* node should be translated into segment.
*
* @param {Object} attributes
* Object containing all inherited attributes from parent elements with attribute
* names as keys
* @return {Object.<Array>} list of segments
*/
export const segmentsFromBase = (attributes) => {
const {
baseUrl,
initialization = '',
sourceDuration,
timescale = 1,
startNumber = 1,
periodIndex = 0,
duration
} = attributes;

// base url is required for SegmentBase to work, per spec (Section 5.3.9.2.1)
if (!baseUrl) {
throw new Error(errors.NO_BASE_URL);
}

const segment = {
map: {
uri: initialization,
resolvedUri: resolveUrl(attributes.baseUrl || '', initialization)
},
resolvedUri: resolveUrl(attributes.baseUrl || '', ''),
uri: attributes.baseUrl
};
const parsedTimescale = parseInt(timescale, 10);

// If there is a duration, use it, otherwise use the given duration of the source
// (since SegmentBase is only for one total segment)
if (duration) {
const parsedDuration = parseInt(duration, 10);
const start = parseInt(startNumber, 10);
const segmentTimeInfo = parseByDuration(start,
periodIndex,
parsedTimescale,
parsedDuration,
sourceDuration);

if (segmentTimeInfo.length >= 1) {
segment.duration = segmentTimeInfo[0].duration;
segment.timeline = segmentTimeInfo[0].timeline;
}
} else if (sourceDuration) {
segment.duration = (sourceDuration / parsedTimescale);
segment.timeline = 0;
}

return [segment];
};
111 changes: 111 additions & 0 deletions src/segment/segmentList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import resolveUrl from '../utils/resolveUrl';
import { parseByDuration, parseByTimeline } from './timeParser';
import errors from '../errors';

/**
* Converts a <SegmentUrl> (of type URLType from the DASH spec 5.3.9.2 Table 14)
* to an object that matches the output of a segment in videojs/mpd-parser
*
* @param {Object} attributes
* Object containing all inherited attributes from parent elements with attribute
* names as keys
* @param {Object} segmentUrl
* <SegmentURL> node to translate into a segment object
* @return {Object} translated segment object
*/
const SegmentURLToSegmentObject = (attributes, segmentUrl) => {
const initUri = attributes.initialization || '';

const segment = {
map: {
uri: initUri,
resolvedUri: resolveUrl(attributes.baseUrl || '', initUri)
},
resolvedUri: resolveUrl(attributes.baseUrl || '', segmentUrl.media),
uri: segmentUrl.media
};

// Follows byte-range-spec per RFC2616
// @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.1
if (segmentUrl.mediaRange) {
const ranges = segmentUrl.mediaRange.split('-');
const startRange = parseInt(ranges[0], 10);
const endRange = parseInt(ranges[1], 10);

segment.byterange = {
length: endRange - startRange,
offset: startRange
};
}

return segment;
};

/**
* Generates a list of segments using information provided by the SegmentList element
* SegmentList (DASH SPEC Section 5.3.9.3.2) contains a set of <SegmentURL> nodes. Each
* node should be translated into segment.
*
* @param {Object} attributes
* Object containing all inherited attributes from parent elements with attribute
* names as keys
* @param {Object[]|undefined} segmentTimeline
* List of objects representing the attributes of each S element contained within
* the SegmentTimeline element
* @return {Object.<Array>} list of segments
*/
export const segmentsFromList = (attributes, segmentTimeline) => {
const {
timescale = 1,
duration,
segmentUrls = [],
periodIndex = 0,
startNumber = 1
} = attributes;

// Per spec (5.3.9.2.1) no way to determine segment duration OR
// if both SegmentTimeline and @duration are defined, it is outside of spec.
if ((!duration && !segmentTimeline) ||
(duration && segmentTimeline)) {
throw new Error(errors.SEGMENT_TIME_UNSPECIFIED);
}

const parsedTimescale = parseInt(timescale, 10);
const start = parseInt(startNumber, 10);
const segmentUrlMap = segmentUrls.map(segmentUrlObject =>
SegmentURLToSegmentObject(attributes, segmentUrlObject));
let segmentTimeInfo;

if (duration) {
const parsedDuration = parseInt(duration, 10);

segmentTimeInfo = parseByDuration(start,
periodIndex,
parsedTimescale,
parsedDuration,
attributes.sourceDuration);
}

if (segmentTimeline) {
segmentTimeInfo = parseByTimeline(start,
periodIndex,
parsedTimescale,
segmentTimeline,
attributes.sourceDuration);
}

const segments = segmentTimeInfo.map((segmentTime, index) => {
if (segmentUrlMap[index]) {
const segment = segmentUrlMap[index];

segment.timeline = segmentTime.timeline;
segment.duration = segmentTime.duration;
return segment;
}
// Since we're mapping we should get rid of any blank segments (in case
// the given SegmentTimeline is handling for more elements than we have
// SegmentURLs for).
}).filter(segment => segment);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this common/supposed/allowed to happen? What cases cause this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So... this shouldn't happen if the mpd is well constructed. But, let's say you have a segment timeline that defines r=10, but you only have 5 segments listed. We will have too many duration objects for the number of segments we have. I guess we could throw an error, but I felt like handling it and truncating made more sense in this case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I suppose its either we crash up front, or play what we can parse and just have an incomplete video, which is better than no video


return segments;
};
126 changes: 2 additions & 124 deletions src/segmentTemplate.js → src/segment/segmentTemplate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { range } from './utils/list';
import resolveUrl from './resolveUrl';
import resolveUrl from '../utils/resolveUrl';
import { parseByDuration, parseByTimeline } from './timeParser';

const identifierPattern = /\$([A-z]*)(?:(%0)([0-9]+)d)?\$/g;

Expand Down Expand Up @@ -90,128 +90,6 @@ export const identifierReplacement = (values) => (match, identifier, format, wid
export const constructTemplateUrl = (url, values) =>
url.replace(identifierPattern, identifierReplacement(values));

/**
* Uses information provided by SegmentTemplate@duration attribute to determine segment
* timing and duration
*
* @param {number} start
* The start number for the first segment of this period
* @param {number} timeline
* The timeline (period index) for the first segment of this period
* @param {number} timescale
* The timescale for the timestamps contained within the media content
* @param {number} duration
* Duration of each segment
* @param {number} sourceDuration
* Duration of the entire Media Presentation
* @return {{number: number, duration: number, time: number, timeline: number}[]}
* List of Objects with segment timing and duration info
*/
export const parseByDuration = (start, timeline, timescale, duration, sourceDuration) => {
const count = Math.ceil(sourceDuration / (duration / timescale));

return range(start, start + count).map((number, index) => {
const segment = { number, duration: duration / timescale, timeline };

if (index === count - 1) {
// final segment may be less than duration
segment.duration = sourceDuration - (segment.duration * index);
}

segment.time = (segment.number - start) * duration;

return segment;
});
};

/**
* Uses information provided by SegmentTemplate.SegmentTimeline to determine segment
* timing and duration
*
* @param {number} start
* The start number for the first segment of this period
* @param {number} timeline
* The timeline (period index) for the first segment of this period
* @param {number} timescale
* The timescale for the timestamps contained within the media content
* @param {Object[]} segmentTimeline
* List of objects representing the attributes of each S element contained within
* @param {number} sourceDuration
* Duration of the entire Media Presentation
* @return {{number: number, duration: number, time: number, timeline: number}[]}
* List of Objects with segment timing and duration info
*/
export const parseByTimeline =
(start, timeline, timescale, segmentTimeline, sourceDuration) => {
const segments = [];
let time = -1;

for (let sIndex = 0; sIndex < segmentTimeline.length; sIndex++) {
const S = segmentTimeline[sIndex];
const duration = parseInt(S.d, 10);
const repeat = parseInt(S.r || 0, 10);
const segmentTime = parseInt(S.t || 0, 10);

if (time < 0) {
// first segment
time = segmentTime;
}

if (segmentTime && segmentTime > time) {
// discontinuity

// TODO: How to handle this type of discontinuity
// timeline++ here would treat it like HLS discontuity and content would
// get appended without gap
// E.G.
// <S t="0" d="1" />
// <S d="1" />
// <S d="1" />
// <S t="5" d="1" />
// would have $Time$ values of [0, 1, 2, 5]
// should this be appened at time positions [0, 1, 2, 3],(#EXT-X-DISCONTINUITY)
// or [0, 1, 2, gap, gap, 5]? (#EXT-X-GAP)
// does the value of sourceDuration consider this when calculating arbitrary
// negative @r repeat value?
// E.G. Same elements as above with this added at the end
// <S d="1" r="-1" />
// with a sourceDuration of 10
// Would the 2 gaps be included in the time duration calculations resulting in
// 8 segments with $Time$ values of [0, 1, 2, 5, 6, 7, 8, 9] or 10 segments
// with $Time$ values of [0, 1, 2, 5, 6, 7, 8, 9, 10, 11] ?

time = segmentTime;
}

let count;

if (repeat < 0) {
const nextS = sIndex + 1;

if (nextS === segmentTimeline.length) {
// last segment
// TODO: This may be incorrect depending on conclusion of TODO above
count = ((sourceDuration * timescale) - time) / duration;
} else {
count = (parseInt(segmentTimeline[nextS].t, 10) - time) / duration;
}
} else {
count = repeat + 1;
}

const end = start + segments.length + count;
let number = start + segments.length;

while (number < end) {
segments.push({ number, duration: duration / timescale, time, timeline });
time += duration;
number++;
}
}

return segments;
};

/**
* Generates a list of objects containing timing and duration information about each
* segment needed to generate segment uris and the complete segment object
Expand Down
Loading