Skip to content

Commit

Permalink
Parse EXT-X-CUE-OUT and EXT-X-CUE-IN (#1)
Browse files Browse the repository at this point in the history
* Updated to latest mocha that works with node 12

* Added support for EXT-X-CUE-OUT and EXT-X-CUE-IN
  • Loading branch information
birme authored Mar 9, 2020
1 parent 5ab15a0 commit 716d51a
Show file tree
Hide file tree
Showing 9 changed files with 978 additions and 55 deletions.
4 changes: 3 additions & 1 deletion m3u/AttributeList.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ var dataTypes = AttributeList.dataTypes = {
'title' : 'enumerated-string',
'type' : 'enumerated-string',
'uri' : 'quoted-string',
'video' : 'quoted-string'
'video' : 'quoted-string',
'cueout' : 'decimal-integer',
'cuein' : 'boolean'
};

AttributeList.prototype.mergeAttributes = function mergeAttributes(attributes) {
Expand Down
2 changes: 1 addition & 1 deletion m3u/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var Item = module.exports = function Item(attributes) {
discontinuity : null,
duration : null,
title : null,
uri : null
uri : null,
};
};

Expand Down
7 changes: 7 additions & 0 deletions m3u/PlaylistItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ PlaylistItem.prototype.toString = function toString() {
if (this.get('discontinuity')) {
output.push('#EXT-X-DISCONTINUITY');
}
if (this.get('cueout')) {
var duration = this.get('cueout');
output.push('#EXT-X-CUE-OUT:DURATION=' + duration);
}
if (this.get('cuein')) {
output.push('#EXT-X-CUE-IN');
}
if (this.get('date')) {
var date = this.get('date');
if (date.getMonth) {
Expand Down
927 changes: 875 additions & 52 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"chunked-stream": "~0.0.1"
},
"devDependencies": {
"mocha": "~1.6.0",
"mocha": "~7.1.0",
"sinon": "~1.5.0",
"should": "7.1.1"
},
Expand Down
14 changes: 14 additions & 0 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,26 @@ m3uParser.prototype['EXTINF'] = function parseInf(data) {
this.currentItem.set('discontinuity', true);
this.playlistDiscontinuity = false;
}
if (typeof this.cueOut !== 'undefined') {
this.currentItem.set('cueout', this.cueOut);
}
this.currentItem.set('cuein', this.cueIn ? true : false);
};

m3uParser.prototype['EXT-X-DISCONTINUITY'] = function parseInf() {
this.playlistDiscontinuity = true;
}

m3uParser.prototype['EXT-X-CUE-OUT'] = function parseInf(data) {
var attr = this.parseAttributes(data);
var durationAttr = attr.find(elem => elem.key.toLowerCase() === 'duration');
this.cueOut = durationAttr ? durationAttr.value : 0;
}

m3uParser.prototype['EXT-X-CUE-IN'] = function parseInf() {
this.cueIn = true;
}

m3uParser.prototype['EXT-X-BYTERANGE'] = function parseByteRange(data) {
this.currentItem.set('byteRange', data);
};
Expand Down
36 changes: 36 additions & 0 deletions test/acceptance/parse-playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,42 @@ describe('parsing playlist m3u8', function() {
});
});
});

describe('11th PlaylistItem', function() {
it('has a cue out', function(done) {
var parser = getParser();

parser.on('m3u', function(m3u) {
var item = m3u.items.PlaylistItem[10];
item.get('cueout').should.equal(30);
done();
});
});
});

describe('13th PlaylistItem', function() {
it('has not a cue in', function(done) {
var parser = getParser();

parser.on('m3u', function(m3u) {
var item = m3u.items.PlaylistItem[12];
item.get('cuein').should.equal(false);
done();
});
});
});

describe('14th PlaylistItem', function() {
it('has a cue in', function(done) {
var parser = getParser();

parser.on('m3u', function(m3u) {
var item = m3u.items.PlaylistItem[13];
item.get('cuein').should.equal(true);
done();
});
});
});
});

function getParser() {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/playlist.m3u8
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ hls_450k_video.ts
#EXTINF:10,
#EXT-X-BYTERANGE:485040@4727636
hls_450k_video.ts
#EXT-X-CUE-OUT:DURATION=30
#EXTINF:10,
#EXT-X-BYTERANGE:709136@5212676
hls_450k_video.ts
Expand All @@ -42,6 +43,7 @@ hls_450k_video.ts
#EXTINF:10,
#EXT-X-BYTERANGE:456276@6651816
hls_450k_video.ts
#EXT-X-CUE-IN
#EXTINF:10,
#EXT-X-BYTERANGE:468684@7108092
hls_450k_video.ts
Expand Down
39 changes: 39 additions & 0 deletions test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,45 @@ describe('parser', function() {
});
});

describe('#EXT-X-CUE-OUT', function() {
it('should indicate cue out with a duration', function() {
var parser = getParser();

parser['EXT-X-CUE-OUT']('DURATION=30');
parser.EXTINF('4.5,some title');
parser.currentItem.constructor.name.should.eql('PlaylistItem');
parser.currentItem.get('cueout').should.eql(30);
});

it('should indicate cue out without a duration', function() {
var parser = getParser();

parser['EXT-X-CUE-OUT']('');
parser.EXTINF('4.5,some title');
parser.currentItem.constructor.name.should.eql('PlaylistItem');
parser.currentItem.get('cueout').should.eql(0);
});
});

describe('#EXT-X-CUE-IN', function() {
it('should indicate cue in is true if present', function() {
var parser = getParser();

parser['EXT-X-CUE-IN']();
parser.EXTINF('4.5,some title');
parser.currentItem.constructor.name.should.eql('PlaylistItem');
parser.currentItem.get('cuein').should.eql(true);
});

it('should indicate cue in is false if not present', function() {
var parser = getParser();

parser.EXTINF('4.5,some title');
parser.currentItem.constructor.name.should.eql('PlaylistItem');
parser.currentItem.get('cuein').should.eql(false);
});
});

describe('#EXT-X-STREAM-INF', function() {
it('should create a new Stream item', function() {
var parser = getParser();
Expand Down

0 comments on commit 716d51a

Please sign in to comment.