Skip to content

Commit

Permalink
feat: Adding support for segments in Period and Representation. (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
ishita12 authored and jforbes committed Feb 26, 2018
1 parent 4c7e2b4 commit 8e59b38
Show file tree
Hide file tree
Showing 10 changed files with 1,248 additions and 132 deletions.
200 changes: 192 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"src/"
],
"devDependencies": {
"babel-eslint": "^8.2.1",
"babel-plugin-external-helpers": "^6.22.0",
"babel-plugin-transform-object-assign": "^6.8.0",
"babel-preset-es2015": "^6.14.0",
Expand Down
41 changes: 26 additions & 15 deletions src/inheritAttributes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { flatten } from './utils/list';
import { shallowMerge, getAttributes } from './utils/object';
import { getAttributes, merge } from './utils/object';
import { parseDuration } from './utils/time';
import { findChildren, getContent } from './utils/xml';
import resolveUrl from './utils/resolveUrl';
Expand Down Expand Up @@ -52,7 +52,7 @@ export const getSegmentInformation = (adaptationSet) => {
const segmentTemplate = findChildren(adaptationSet, 'SegmentTemplate')[0];
const segmentList = findChildren(adaptationSet, 'SegmentList')[0];
const segmentUrls = segmentList && findChildren(segmentList, 'SegmentURL')
.map(s => shallowMerge({ tag: 'SegmentURL' }, getAttributes(s)));
.map(s => merge({ tag: 'SegmentURL' }, getAttributes(s)));
const segmentBase = findChildren(adaptationSet, 'SegmentBase')[0];
const segmentTimelineParentNode = segmentList || segmentTemplate;
const segmentTimeline = segmentTimelineParentNode &&
Expand All @@ -78,21 +78,29 @@ export const getSegmentInformation = (adaptationSet) => {
template.initialization = { sourceURL: template.initialization };
}

return {
const segmentInfo = {
template,
timeline: segmentTimeline &&
findChildren(segmentTimeline, 'S').map(s => getAttributes(s)),
list: segmentList && shallowMerge(
list: segmentList && merge(
getAttributes(segmentList),
{
segmentUrls,
initialization: getAttributes(segmentInitialization)
}),
base: segmentBase && shallowMerge(
base: segmentBase && merge(
getAttributes(segmentBase), {
initialization: getAttributes(segmentInitialization)
})
};

Object.keys(segmentInfo).forEach(key => {
if (!segmentInfo[key]) {
delete segmentInfo[key];
}
});

return segmentInfo;
};

/**
Expand Down Expand Up @@ -131,15 +139,16 @@ export const getSegmentInformation = (adaptationSet) => {
* Callback map function
*/
export const inheritBaseUrls =
(adaptationSetAttributes, adaptationSetBaseUrls, segmentInfo) => (representation) => {
(adaptationSetAttributes, adaptationSetBaseUrls, adaptationSetSegmentInfo) => (representation) => {
const repBaseUrlElements = findChildren(representation, 'BaseURL');
const repBaseUrls = buildBaseUrls(adaptationSetBaseUrls, repBaseUrlElements);
const attributes = shallowMerge(adaptationSetAttributes, getAttributes(representation));
const attributes = merge(adaptationSetAttributes, getAttributes(representation));
const representationSegmentInfo = getSegmentInformation(representation);

return repBaseUrls.map(baseUrl => {
return {
segmentInfo,
attributes: shallowMerge(attributes, { baseUrl })
segmentInfo: merge(adaptationSetSegmentInfo, representationSegmentInfo),
attributes: merge(attributes, { baseUrl })
};
});
};
Expand Down Expand Up @@ -167,20 +176,21 @@ export const inheritBaseUrls =
* Callback map function
*/
export const toRepresentations =
(periodAttributes, periodBaseUrls) => (adaptationSet) => {
(periodAttributes, periodBaseUrls, periodSegmentInfo) => (adaptationSet) => {
const adaptationSetAttributes = getAttributes(adaptationSet);
const adaptationSetBaseUrls = buildBaseUrls(periodBaseUrls,
findChildren(adaptationSet, 'BaseURL'));
const role = findChildren(adaptationSet, 'Role')[0];
const roleAttributes = { role: getAttributes(role) };
const attrs = shallowMerge(periodAttributes,
const attrs = merge(periodAttributes,
adaptationSetAttributes,
roleAttributes);
const segmentInfo = getSegmentInformation(adaptationSet);
const representations = findChildren(adaptationSet, 'Representation');
const adaptationSetSegmentInfo = merge(periodSegmentInfo, segmentInfo);

return flatten(
representations.map(inheritBaseUrls(attrs, adaptationSetBaseUrls, segmentInfo)));
representations.map(inheritBaseUrls(attrs, adaptationSetBaseUrls, adaptationSetSegmentInfo)));
};

/**
Expand Down Expand Up @@ -210,10 +220,12 @@ export const toRepresentations =
*/
export const toAdaptationSets = (mpdAttributes, mpdBaseUrls) => (period, periodIndex) => {
const periodBaseUrls = buildBaseUrls(mpdBaseUrls, findChildren(period, 'BaseURL'));
const periodAttributes = shallowMerge({ periodIndex }, mpdAttributes);
const periodAtt = getAttributes(period);
const periodAttributes = merge(mpdAttributes, periodAtt, { periodIndex });
const adaptationSets = findChildren(period, 'AdaptationSet');
const periodSegmentInfo = getSegmentInformation(period);

return flatten(adaptationSets.map(toRepresentations(periodAttributes, periodBaseUrls)));
return flatten(adaptationSets.map(toRepresentations(periodAttributes, periodBaseUrls, periodSegmentInfo)));
};

/**
Expand Down Expand Up @@ -243,4 +255,3 @@ export const inheritAttributes = (mpd, manifestUri = '') => {

return flatten(periods.map(toAdaptationSets(mpdAttributes, mpdBaseUrls)));
};

12 changes: 5 additions & 7 deletions src/toPlaylists.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import { shallowMerge } from './utils/object';
import { merge } from './utils/object';
import { segmentsFromTemplate } from './segment/segmentTemplate';
import { segmentsFromList } from './segment/segmentList';
import { segmentsFromBase } from './segment/segmentBase';

export const generateSegments = (segmentInfo, attributes) => {
if (segmentInfo.template) {
return segmentsFromTemplate(
shallowMerge(segmentInfo.template, attributes),
merge(attributes, segmentInfo.template),
segmentInfo.timeline
);
}

if (segmentInfo.base) {
return segmentsFromBase(shallowMerge(segmentInfo.base, attributes));
return segmentsFromBase(merge(attributes, segmentInfo.base));
}

if (segmentInfo.list) {
return segmentsFromList(
shallowMerge(segmentInfo.list, attributes), segmentInfo.timeline
merge(attributes, segmentInfo.list), segmentInfo.timeline
);
}
};

export const toPlaylists = representations => {
export const toPlaylists = (representations) => {
return representations.map(({ attributes, segmentInfo }) => {
const segments = generateSegments(segmentInfo, attributes);

Expand Down
Loading

0 comments on commit 8e59b38

Please sign in to comment.