Skip to content

Commit

Permalink
feat: expose custom M3U8 mapper API
Browse files Browse the repository at this point in the history
  • Loading branch information
jforbes committed Jan 9, 2019
1 parent 8f45194 commit c84c007
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/playlist-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export default class PlaylistLoader extends EventTarget {
const options = hls.options_;

this.customTagParsers = (options && options.customTagParsers) || [];
this.customTagMappers = (options && options.customTagMappers) || [];

if (!this.srcUrl) {
throw new Error('A non-empty playlist URL is required');
Expand Down Expand Up @@ -273,6 +274,9 @@ export default class PlaylistLoader extends EventTarget {
// adding custom tag parsers
this.customTagParsers.forEach(customParser => parser.addParser(customParser));

// adding custom tag mappers
this.customTagMappers.forEach(mapper => parser.addTagMapper(mapper));

parser.push(xhr.responseText);
parser.end();
parser.manifest.uri = url;
Expand Down Expand Up @@ -515,6 +519,9 @@ export default class PlaylistLoader extends EventTarget {
// adding custom tag parsers
this.customTagParsers.forEach(customParser => parser.addParser(customParser));

// adding custom tag mappers
this.customTagMappers.forEach(mapper => parser.addTagMapper(mapper));

parser.push(req.responseText);
parser.end();

Expand Down
16 changes: 14 additions & 2 deletions test/playlist-loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,28 +862,40 @@ QUnit.test('logs warning for master playlist with invalid STREAM-INF', function(
'logged a warning');
});

QUnit.test('executes custom tag parsers', function(assert) {
QUnit.test('executes custom parsers and mappers', function(assert) {
const customTagParsers = [{
expression: /#PARSER/,
customType: 'test',
segment: true
}];
const customTagMappers = [{
expression: /#MAPPER/,
map(line) {
const regex = /#MAPPER:(\d+)/g;
const match = regex.exec(line);
const ISOdate = new Date(Number(match[1])).toISOString();

return `#EXT-X-PROGRAM-DATE-TIME:${ISOdate}`;
}
}];

this.fakeHls.options_ = { customTagParsers };
this.fakeHls.options_ = { customTagParsers, customTagMappers };

let loader = new PlaylistLoader('master.m3u8', this.fakeHls);

loader.load();
this.requests.pop().respond(200, null,
'#EXTM3U\n' +
'#PARSER:parsed\n' +
'#MAPPER:1511816599485\n' +
'#EXTINF:10,\n' +
'0.ts\n' +
'#EXT-X-ENDLIST\n');

const segment = loader.master.playlists[0].segments[0];

assert.strictEqual(segment.custom.test, '#PARSER:parsed', 'parsed custom tag');
assert.ok(segment.dateTimeObject, 'converted and parsed custom time');

delete this.fakeHls.options_;
});
Expand Down

0 comments on commit c84c007

Please sign in to comment.