Skip to content

Commit

Permalink
fix: merge dateRange tags with same IDs and no conflicting attributes (
Browse files Browse the repository at this point in the history
  • Loading branch information
harisha-swaminathan authored Aug 7, 2023
1 parent 6944bb1 commit 73d934c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,21 +696,28 @@ export default class Parser extends Stream {
}
if (dateRange.duration && dateRange.endDate) {
const startDate = dateRange.startDate;
const newDateInSeconds = startDate.setSeconds(startDate.getSeconds() + dateRange.duration);
const newDateInSeconds = startDate.getTime() + (dateRange.duration * 1000);

this.manifest.dateRanges[index].endDate = new Date(newDateInSeconds);
}
if (!dateRangeTags[dateRange.id]) {
dateRangeTags[dateRange.id] = dateRange;
} else {
for (const attribute in dateRangeTags[dateRange.id]) {
if (dateRangeTags[dateRange.id][attribute] !== dateRange[attribute]) {
if (!!dateRange[attribute] && JSON.stringify(dateRangeTags[dateRange.id][attribute]) !== JSON.stringify(dateRange[attribute])) {
this.trigger('warn', {
message: 'EXT-X-DATERANGE tags with the same ID in a playlist must have the same attributes and same attribute values'
message: 'EXT-X-DATERANGE tags with the same ID in a playlist must have the same attributes values'
});
break;
}
}
// if tags with the same ID do not have conflicting attributes, merge them
const dateRangeWithSameId = this.manifest.dateRanges.findIndex((dateRangeToFind) => dateRangeToFind.id === dateRange.id);

this.manifest.dateRanges[dateRangeWithSameId] = Object.assign(this.manifest.dateRanges[dateRangeWithSameId], dateRange);
dateRangeTags[dateRange.id] = Object.assign(dateRangeTags[dateRange.id], dateRange);
// after merging, delete the duplicate dateRange that was added last
this.manifest.dateRanges.pop();
}
},
'independent-segments'() {
Expand Down
34 changes: 30 additions & 4 deletions test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ QUnit.module('m3u8s', function(hooks) {
);
});

QUnit.test('warns when playlist has multiple #EXT-X-DATERANGE tag same ID but different attribute names and values', function(assert) {
QUnit.test('warns when playlist has multiple #EXT-X-DATERANGE tag same ID but different attribute values', function(assert) {
this.parser.push([
'#EXT-X-VERSION:3',
'#EXT-X-MEDIA-SEQUENCE:0',
Expand All @@ -1041,12 +1041,12 @@ QUnit.module('m3u8s', function(hooks) {
'#EXT-X-ENDLIST',
'#EXT-X-PROGRAM-DATE-TIME:2017-07-31T20:35:35.053+00:00',
'#EXT-X-DATERANGE:ID="12345",START-DATE="2023-04-13T18:16:15.840000Z",END-ON-NEXT=YES,CLASS="CLASSATTRIBUTE"',
'#EXT-X-DATERANGE:ID="12345",START-DATE="2023-04-13T18:16:20.840000Z"'
'#EXT-X-DATERANGE:ID="12345",START-DATE="2023-04-13T18:16:15.840000Z",CLASS="CLASSATTRIBUTE1"'
].join('\n'));
this.parser.end();

const warnings = [
'EXT-X-DATERANGE tags with the same ID in a playlist must have the same attributes and same attribute values'
'EXT-X-DATERANGE tags with the same ID in a playlist must have the same attributes values'
];

assert.deepEqual(
Expand Down Expand Up @@ -1095,7 +1095,33 @@ QUnit.module('m3u8s', function(hooks) {
);
});

QUnit.test(' playlist with multiple ext-x-daterange ', function(assert) {
QUnit.test('playlist with multiple ext-x-daterange with same ID but no conflicting attributes', function(assert) {
const expectedDateRange = {
id: '12345',
scte35In: '0xFC30200FFF2',
scte35Out: '0xFC30200FFF2',
startDate: new Date('2023-04-13T18:16:15.840000Z'),
class: 'CLASSATTRIBUTE'
};

this.parser.push([
'#EXT-X-VERSION:3',
'#EXT-X-MEDIA-SEQUENCE:0',
'#EXT-X-DISCONTINUITY-SEQUENCE:0',
'#EXTINF:10,',
'media-00001.ts',
'#EXT-X-ENDLIST',
'#EXT-X-PROGRAM-DATE-TIME:2017-07-31T20:35:35.053+00:00',
'#EXT-X-DATERANGE:ID="12345",SCTE35-IN=0xFC30200FFF2,START-DATE="2023-04-13T18:16:15.840000Z",CLASS="CLASSATTRIBUTE"',
'#EXT-X-DATERANGE:ID="12345",SCTE35-OUT=0xFC30200FFF2,START-DATE="2023-04-13T18:16:15.840000Z"'
].join('\n'));
this.parser.end();
assert.equal(this.parser.manifest.dateRanges.length, 1, 'two dateranges with same ID are merged');
assert.deepEqual(this.parser.manifest.dateRanges[0], expectedDateRange);

});

QUnit.test('playlist with multiple ext-x-daterange ', function(assert) {
this.parser.push([
' #EXTM3U',
'#EXT-X-VERSION:6',
Expand Down

0 comments on commit 73d934c

Please sign in to comment.