From c84c00772fe42ecc42892639fad9844b9c610034 Mon Sep 17 00:00:00 2001 From: jforbes Date: Wed, 2 Jan 2019 15:45:55 -0500 Subject: [PATCH] feat: expose custom M3U8 mapper API --- src/playlist-loader.js | 7 +++++++ test/playlist-loader.test.js | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/playlist-loader.js b/src/playlist-loader.js index bf60034f7..f8e994219 100644 --- a/src/playlist-loader.js +++ b/src/playlist-loader.js @@ -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'); @@ -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; @@ -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(); diff --git a/test/playlist-loader.test.js b/test/playlist-loader.test.js index e11d16ac8..b4a0cf320 100644 --- a/test/playlist-loader.test.js +++ b/test/playlist-loader.test.js @@ -862,14 +862,24 @@ 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); @@ -877,6 +887,7 @@ QUnit.test('executes custom tag parsers', function(assert) { this.requests.pop().respond(200, null, '#EXTM3U\n' + '#PARSER:parsed\n' + + '#MAPPER:1511816599485\n' + '#EXTINF:10,\n' + '0.ts\n' + '#EXT-X-ENDLIST\n'); @@ -884,6 +895,7 @@ QUnit.test('executes custom tag parsers', function(assert) { 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_; });