Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions src/inheritAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,46 @@ export const toRepresentations =
return flatten(representations.map(inheritBaseUrls(attrs, adaptationSetBaseUrls, adaptationSetSegmentInfo)));
};

/**
* Gets the duration of a Period, following DASH spec section 5.3.2.1
*
* @param {Object} options
* Options object
* @param {Object} options.attributes
* Period attributes, with the resolved start
* @param {Object} [options.nextPeriodAttributes]
* Next period attributes (if a next period is available)
* @param {Object} options.mpdAttributes
* MPD attributes
* @return {number|undefined}
* The period duration, or undefined if it can't be known, or if the period
* runs from the start to the end of the presentation
*/
export const getPeriodDuration = ({ attributes, nextPeriodAttributes, mpdAttributes }) => {
if (typeof attributes.duration === 'number') {
return attributes.duration;
}

if (typeof attributes.start !== 'number') {
return;
}

let end;

if (nextPeriodAttributes) {
// A Period without @duration ends where the next Period starts.
end = nextPeriodAttributes.start;
} else if (mpdAttributes.type === 'static' && attributes.start > 0) {
// The last Period ends at the end of the presentation. A last Period that starts at 0
// already gets the whole presentation through sourceDuration.
end = mpdAttributes.mediaPresentationDuration;
}

if (typeof end === 'number' && end > attributes.start) {
return end - attributes.start;
}
};

/**
* Contains all period information for mapping nodes onto adaptation sets.
*
Expand Down Expand Up @@ -445,14 +485,19 @@ export const toRepresentations =
* @return {toAdaptationSetsCallback}
* Callback map function
*/
export const toAdaptationSets = (mpdAttributes, mpdBaseUrls) => (period, index) => {
export const toAdaptationSets = (mpdAttributes, mpdBaseUrls) => (period, index, periods = []) => {
const periodBaseUrls = buildBaseUrls(mpdBaseUrls, findChildren(period.node, 'BaseURL'));
const periodAttributes = merge(mpdAttributes, {
periodStart: period.attributes.start
});
const periodDuration = getPeriodDuration({
attributes: period.attributes,
nextPeriodAttributes: periods[index + 1] ? periods[index + 1].attributes : null,
mpdAttributes
});

if (typeof period.attributes.duration === 'number') {
periodAttributes.periodDuration = period.attributes.duration;
if (typeof periodDuration === 'number') {
periodAttributes.periodDuration = periodDuration;
}
const adaptationSets = findChildren(period.node, 'AdaptationSet');
const periodSegmentInfo = getSegmentInformation(period.node);
Expand Down
26 changes: 26 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,29 @@ QUnit.test('multiperiod_startnumber_removed_periods test manifest', function(ass

assert.deepEqual(actual, multiperiodStartnumberRemovedPeriodsManifest);
});

QUnit.test('Periods without @duration end at the next Period@start', function(assert) {
const manifest = `<?xml version="1.0"?>
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" type="static" mediaPresentationDuration="PT20S"
profiles="urn:mpeg:dash:profile:isoff-live:2011">
<Period id="1" start="PT0S">
<AdaptationSet mimeType="video/mp4">
<SegmentTemplate timescale="1" duration="5" media="a-$Number$.m4s"/>
<Representation id="a" bandwidth="1"/>
</AdaptationSet>
</Period>
<Period id="2" start="PT10S">
<AdaptationSet mimeType="video/mp4">
<SegmentTemplate timescale="1" duration="5" media="b-$Number$.m4s"/>
<Representation id="b" bandwidth="1"/>
</AdaptationSet>
</Period>
</MPD>`;
const segments = parse(manifest, { manifestUri: 'https://example.com/m.mpd' }).playlists
.map((playlist) => playlist.segments.map((segment) => [segment.uri, segment.presentationTime, segment.duration]));

assert.deepEqual(segments, [
[['a-1.m4s', 0, 5], ['a-2.m4s', 5, 5]],
[['b-1.m4s', 10, 5], ['b-2.m4s', 15, 5]]
], 'each Period only has its own segments');
});
72 changes: 72 additions & 0 deletions test/inheritAttributes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
parseCaptionServiceMetadata,
getSegmentInformation,
getPeriodStart,
getPeriodDuration,
toEventStream
} from '../src/inheritAttributes';
import { stringToMpdXml } from '../src/stringToMpdXml';
Expand Down Expand Up @@ -187,6 +188,75 @@ QUnit.test('null when dynamic, no prior period, and no start attribute', functio
);
});

QUnit.module('getPeriodDuration');

QUnit.test('uses Period@duration when available', function(assert) {
assert.equal(getPeriodDuration({
attributes: { start: 10, duration: 5 },
nextPeriodAttributes: { start: 30 },
mpdAttributes: { type: 'static', mediaPresentationDuration: 60 }
}), 5, 'Period@duration wins');
});

QUnit.test('ends at the next Period@start', function(assert) {
assert.equal(getPeriodDuration({
attributes: { start: 10 },
nextPeriodAttributes: { start: 30 },
mpdAttributes: { type: 'static', mediaPresentationDuration: 60 }
}), 20, 'static');
assert.equal(getPeriodDuration({
attributes: { start: 10 },
nextPeriodAttributes: { start: 30 },
mpdAttributes: { type: 'dynamic' }
}), 20, 'dynamic');
});

QUnit.test('last static Period ends at mediaPresentationDuration', function(assert) {
assert.equal(getPeriodDuration({
attributes: { start: 10 },
nextPeriodAttributes: null,
mpdAttributes: { type: 'static', mediaPresentationDuration: 60 }
}), 50, 'start > 0');
assert.equal(getPeriodDuration({
attributes: { start: 0 },
nextPeriodAttributes: null,
mpdAttributes: { type: 'static', mediaPresentationDuration: 60 }
}), undefined, 'start 0 is left to sourceDuration');
});

QUnit.test('undefined when the end is unknown or not after the start', function(assert) {
assert.equal(getPeriodDuration({
attributes: { start: 10 },
nextPeriodAttributes: null,
mpdAttributes: { type: 'dynamic', mediaPresentationDuration: 60 }
}), undefined, 'last dynamic Period');
assert.equal(getPeriodDuration({
attributes: { start: 10 },
nextPeriodAttributes: null,
mpdAttributes: { type: 'static' }
}), undefined, 'no mediaPresentationDuration');
assert.equal(getPeriodDuration({
attributes: { start: 10 },
nextPeriodAttributes: { start: null },
mpdAttributes: { type: 'static', mediaPresentationDuration: 60 }
}), undefined, 'next Period has no start');
assert.equal(getPeriodDuration({
attributes: { start: null },
nextPeriodAttributes: { start: 30 },
mpdAttributes: { type: 'static', mediaPresentationDuration: 60 }
}), undefined, 'Period has no start');
assert.equal(getPeriodDuration({
attributes: { start: 30 },
nextPeriodAttributes: { start: 30 },
mpdAttributes: { type: 'static', mediaPresentationDuration: 60 }
}), undefined, 'next Period starts at the same time');
assert.equal(getPeriodDuration({
attributes: { start: 70 },
nextPeriodAttributes: null,
mpdAttributes: { type: 'static', mediaPresentationDuration: 60 }
}), undefined, 'starts after mediaPresentationDuration');
});

QUnit.module('getSegmentInformation');

QUnit.test('undefined Segment information when no Segment nodes', function(assert) {
Expand Down Expand Up @@ -1151,6 +1221,8 @@ QUnit.test('end to end - basic multiperiod', function(assert) {
id: 'test',
mediaPresentationDuration: 60,
mimeType: 'video/mp4',
// inferred from the end of the presentation
periodDuration: 30,
// inferred start
periodStart: 30,
role: {
Expand Down