Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and use generic playlist attribute list parser #107

Merged
merged 4 commits into from
Dec 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"minify": "uglifyjs dist/hls.js -c sequences=true,dead_code=true,conditionals=true,booleans=true,unused=true,if_return=true,join_vars=true,drop_console=true -m sort --screw-ie8 > dist/hls.min.js",
"watch": "watchify --debug -s Hls src/hls.js -o dist/hls.js",
"pretest": "npm run lint",
"test": "mocha --recursive tests/unit",
"test": "mocha --compilers js:babel/register --recursive tests/unit",
"lint": "jshint src/",
"serve": "http-server -p 8000 .",
"open": "opener http://localhost:8000/demo/",
Expand All @@ -38,6 +38,8 @@
"webworkify": "^1.0.2"
},
"devDependencies": {
"arraybuffer-equal": "^1.0.4",
"babel": "^5.8.34",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @kanongil i am not clear about this new dependency to babel ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mocha does not understand es6 code and needs a compiler, which I register using --compilers js:babel/register. As such it needs to be declared a dev dependency as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine, I will give it a hand soon

"browserify": "^8.1.1",
"exorcist": "^0.4.0",
"http-server": "^0.7.4",
Expand Down
84 changes: 26 additions & 58 deletions src/loader/playlist-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import Event from '../events';
import {ErrorTypes, ErrorDetails} from '../errors';
import URLHelper from '../utils/url';
import AttrList from '../utils/attr-list';
//import {logger} from '../utils/logger';

class PlaylistLoader {
Expand Down Expand Up @@ -49,42 +50,31 @@ class PlaylistLoader {
}

parseMasterPlaylist(string, baseurl) {
var levels = [], level = {}, result, codecs, codec;
let levels = [], result;

// https://regex101.com is your friend
var re = /#EXT-X-STREAM-INF:([^\n\r]*(BAND)WIDTH=(\d+))?([^\n\r]*(CODECS)=\"([^\"\n\r]*)\",?)?([^\n\r]*(RES)OLUTION=(\d+)x(\d+))?([^\n\r]*(NAME)=\"(.*)\")?[^\n\r]*[\r\n]+([^\r\n]+)/g;
const re = /#EXT-X-STREAM-INF:([^\n\r]*)[\r\n]+([^\r\n]+)/g;
while ((result = re.exec(string)) != null){
result.shift();
result = result.filter(function(n) { return (n !== undefined); });
level.url = this.resolve(result.pop(), baseurl);
while (result.length > 0) {
switch (result.shift()) {
case 'RES':
level.width = parseInt(result.shift());
level.height = parseInt(result.shift());
break;
case 'BAND':
level.bitrate = parseInt(result.shift());
break;
case 'NAME':
level.name = result.shift();
break;
case 'CODECS':
codecs = result.shift().split(',');
while (codecs.length > 0) {
codec = codecs.shift();
if (codec.indexOf('avc1') !== -1) {
level.videoCodec = this.avc1toavcoti(codec);
} else {
level.audioCodec = codec;
}
}
break;
default:
break;
const level = {};

level.attrs = new AttrList(result[1]);
level.url = this.resolve(result[2], baseurl);

Object.assign(level, level.attrs.decimalResolution('RESOLUTION'));
level.bitrate = level.attrs.decimalInteger('BANDWIDTH');
level.name = level.attrs.quotedString('NAME');

const codecs = (level.attrs.quotedString('CODECS') || '').split(',');
for (let i = 0; i < codecs.length; i++) {
const codec = codecs[i];
if (codec.indexOf('avc1') !== -1) {
level.videoCodec = this.avc1toavcoti(codec);
} else {
level.audioCodec = codec;
}
}

levels.push(level);
level = {};
}
return levels;
}
Expand All @@ -101,18 +91,6 @@ class PlaylistLoader {
return result;
}

parseKeyParamsByRegex(string, regexp) {
var result = regexp.exec(string);
if (result) {
result.shift();
result = result.filter(function(n) { return (n !== undefined); });
if (result.length === 2) {
return result[1];
}
}
return null;
}

cloneObj(obj) {
return JSON.parse(JSON.stringify(obj));
}
Expand Down Expand Up @@ -175,9 +153,10 @@ class PlaylistLoader {
case 'KEY':
// https://tools.ietf.org/html/draft-pantos-http-live-streaming-08#section-3.4.4
var decryptparams = result[1];
var decryptmethod = this.parseKeyParamsByRegex(decryptparams, /(METHOD)=([^,]*)/),
decrypturi = this.parseKeyParamsByRegex(decryptparams, /(URI)=["]([^,]*)["]/),
decryptiv = this.parseKeyParamsByRegex(decryptparams, /(IV)=([^,]*)/);
var keyAttrs = new AttrList(decryptparams);
var decryptmethod = keyAttrs.enumeratedString('METHOD'),
decrypturi = keyAttrs.quotedString('URI'),
decryptiv = keyAttrs.hexadecimalInteger('IV');
if (decryptmethod) {
levelkey = { method: null, key: null, iv: null, uri: null };
if ((decrypturi) && (decryptmethod === 'AES-128')) {
Expand All @@ -186,18 +165,7 @@ class PlaylistLoader {
levelkey.uri = this.resolve(decrypturi, baseurl);
levelkey.key = null;
// Initialization Vector (IV)
if (decryptiv) {
levelkey.iv = decryptiv;
if (levelkey.iv.substring(0, 2) === '0x') {
levelkey.iv = levelkey.iv.substring(2);
}
levelkey.iv = levelkey.iv.match(/.{8}/g);
levelkey.iv[0] = parseInt(levelkey.iv[0], 16);
levelkey.iv[1] = parseInt(levelkey.iv[1], 16);
levelkey.iv[2] = parseInt(levelkey.iv[2], 16);
levelkey.iv[3] = parseInt(levelkey.iv[3], 16);
levelkey.iv = new Uint32Array(levelkey.iv);
}
levelkey.iv = decryptiv;
}
}
break;
Expand Down
75 changes: 75 additions & 0 deletions src/utils/attr-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

// adapted from https://github.com/kanongil/node-m3u8parse/blob/master/attrlist.js
class AttrList {

constructor(attrs) {
if (typeof attrs === 'string') {
attrs = AttrList.parseAttrList(attrs);
}

Object.assign(this, attrs);
}

decimalInteger(attrName) {
const intValue = parseInt(this[attrName], 10);
if (intValue > Number.MAX_SAFE_INTEGER) {
return Infinity;
}
return intValue;
}

hexadecimalInteger(attrName) {
let stringValue = (this[attrName] || '0x').slice(2);
stringValue = ((stringValue.length & 1) ? '0' : '') + stringValue;

const value = new Uint8Array(stringValue.length / 2);
for (let i = 0; i < stringValue.length / 2; i++) {
value[i] = parseInt(stringValue.slice(i * 2, i * 2 + 2), 16);
}
return value;
}

hexadecimalIntegerAsNumber(attrName) {
const intValue = parseInt(this[attrName], 16);
if (intValue > Number.MAX_SAFE_INTEGER) {
return Infinity;
}
return intValue;
}

decimalFloatingPoint(attrName) {
return parseFloat(this[attrName]);
}

quotedString(attrName) {
const val = this[attrName];
return val ? val.slice(1, -1) : undefined;
}

enumeratedString(attrName) {
return this[attrName];
}

decimalResolution(attrName) {
const res = /^(\d+)x(\d+)$/.exec(this[attrName]);
if (res === null) {
return undefined;
}
return {
width: parseInt(res[1], 10),
height: parseInt(res[2], 10)
};
}

static parseAttrList(input) {
const re = /(.+?)=((?:\".*?\")|.*?)(?:,|$)/g;
var match, attrs = {};
while ((match = re.exec(input)) !== null) {
attrs[match[1]] = match[2];
}
return attrs;
}

}

export default AttrList;
111 changes: 111 additions & 0 deletions tests/unit/utils/attr-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const assert = require('assert');
const bufferIsEqual = require('arraybuffer-equal');

import AttrList from '../../../src/utils/attr-list';

describe('AttrList', () => {
it('constructor() supports empty arguments', () => {
assert.deepEqual(new AttrList(), {});
assert.deepEqual(new AttrList({}), {});
assert.deepEqual(new AttrList(undefined), {});
});
it('constructor() supports object argument', () => {
const obj = { VALUE: "42" };
const list = new AttrList(obj);
assert.strictEqual(list.decimalInteger('VALUE'), 42);
assert.strictEqual(Object.keys(list).length, 1);
});

it('parses valid decimalInteger attribute', () => {
assert.strictEqual(new AttrList('INT=42').decimalInteger('INT'), 42);
assert.strictEqual(new AttrList('INT=0').decimalInteger('INT'), 0);
});
it('parses valid hexadecimalInteger attribute', () => {
assert.strictEqual(new AttrList('HEX=0x42').hexadecimalIntegerAsNumber('HEX'), 0x42);
assert.strictEqual(new AttrList('HEX=0X42').hexadecimalIntegerAsNumber('HEX'), 0x42);
assert.strictEqual(new AttrList('HEX=0x0').hexadecimalIntegerAsNumber('HEX'), 0);
});
it('parses valid decimalFloatingPoint attribute', () => {
assert.strictEqual(new AttrList('FLOAT=0.42').decimalFloatingPoint('FLOAT'), 0.42);
assert.strictEqual(new AttrList('FLOAT=-0.42').decimalFloatingPoint('FLOAT'), -0.42);
assert.strictEqual(new AttrList('FLOAT=0').decimalFloatingPoint('FLOAT'), 0);
});
it('parses valid quotedString attribute', () => {
assert.strictEqual(new AttrList('STRING="hi"').quotedString('STRING'), 'hi');
assert.strictEqual(new AttrList('STRING=""').quotedString('STRING'), '');
});
it('parses exotic quotedString attribute', () => {
const list = new AttrList('STRING="hi,ENUM=OK,RES=4x2"');
assert.strictEqual(list.quotedString('STRING'), 'hi,ENUM=OK,RES=4x2');
assert.strictEqual(Object.keys(list).length, 1);
});
it('parses valid enumeratedString attribute', () => {
assert.strictEqual(new AttrList('ENUM=OK').enumeratedString('ENUM'), 'OK');
});
it('parses exotic enumeratedString attribute', () => {
assert.strictEqual(new AttrList('ENUM=1').enumeratedString('ENUM'), '1');
assert.strictEqual(new AttrList('ENUM=A=B').enumeratedString('ENUM'), 'A=B');
assert.strictEqual(new AttrList('ENUM=A=B=C').enumeratedString('ENUM'), 'A=B=C');
const list = new AttrList('ENUM1=A=B=C,ENUM2=42');
assert.strictEqual(list.enumeratedString('ENUM1'), 'A=B=C');
assert.strictEqual(list.enumeratedString('ENUM2'), '42');
});
it('parses valid decimalResolution attribute', () => {
assert.deepStrictEqual(new AttrList('RES=400x200').decimalResolution('RES'), { width:400, height:200 });
assert.deepStrictEqual(new AttrList('RES=0x0').decimalResolution('RES'), { width:0, height:0 });
});
it('handles invalid decimalResolution attribute', () => {
assert.deepStrictEqual(new AttrList('RES=400x-200').decimalResolution('RES'), undefined);
assert.deepStrictEqual(new AttrList('RES=400.5x200').decimalResolution('RES'), undefined);
assert.deepStrictEqual(new AttrList('RES=400x200.5').decimalResolution('RES'), undefined);
assert.deepStrictEqual(new AttrList('RES=400').decimalResolution('RES'), undefined);
assert.deepStrictEqual(new AttrList('RES=400x').decimalResolution('RES'), undefined);
assert.deepStrictEqual(new AttrList('RES=x200').decimalResolution('RES'), undefined);
assert.deepStrictEqual(new AttrList('RES=x').decimalResolution('RES'), undefined);
});

it('parses multiple attributes', () => {
const list = new AttrList('INT=42,HEX=0x42,FLOAT=0.42,STRING="hi",ENUM=OK,RES=4x2');
assert.strictEqual(list.decimalInteger('INT'), 42);
assert.strictEqual(list.hexadecimalIntegerAsNumber('HEX'), 0x42);
assert.strictEqual(list.decimalFloatingPoint('FLOAT'), 0.42);
assert.strictEqual(list.quotedString('STRING'), 'hi');
assert.strictEqual(list.enumeratedString('ENUM'), 'OK');
assert.deepStrictEqual(list.decimalResolution('RES'), { width:4, height:2 });
assert.strictEqual(Object.keys(list).length, 6);
});

it('handles missing attributes', () => {
const list = new AttrList();
assert(isNaN(list.decimalInteger('INT')));
assert(isNaN(list.hexadecimalIntegerAsNumber('HEX')));
assert(isNaN(list.decimalFloatingPoint('FLOAT')));
assert.strictEqual(list.quotedString('STRING'), undefined);
assert.strictEqual(list.enumeratedString('ENUM'), undefined);
assert.strictEqual(list.decimalResolution('RES'), undefined);
assert.strictEqual(Object.keys(list).length, 0);
});

it('parses dashed attribute names', () => {
const list = new AttrList('INT-VALUE=42,H-E-X=0x42,-FLOAT=0.42,STRING-="hi",ENUM=OK');
assert.strictEqual(list.decimalInteger('INT-VALUE'), 42);
assert.strictEqual(list.hexadecimalIntegerAsNumber('H-E-X'), 0x42);
assert.strictEqual(list.decimalFloatingPoint('-FLOAT'), 0.42);
assert.strictEqual(list.quotedString('STRING-'), 'hi');
assert.strictEqual(list.enumeratedString('ENUM'), 'OK');
assert.strictEqual(Object.keys(list).length, 5);
});

it('handles hexadecimalInteger conversions', () => {
const list = new AttrList('HEX1=0x0123456789abcdef0123456789abcdef,HEX2=0x123,HEX3=0x0');
assert(bufferIsEqual(list.hexadecimalInteger('HEX1').buffer, new Uint8Array([0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef]).buffer));
assert(bufferIsEqual(list.hexadecimalInteger('HEX2').buffer, new Uint8Array([0x01,0x23]).buffer));
assert(bufferIsEqual(list.hexadecimalInteger('HEX3').buffer, new Uint8Array([0x0]).buffer));
});

it('returns infinity on large number conversions', () => {
const list = new AttrList('VAL=12345678901234567890,HEX=0x0123456789abcdef0123456789abcdef');
assert.strictEqual(list.decimalInteger('VAL'), Infinity);
assert.strictEqual(list.hexadecimalIntegerAsNumber('HEX'), Infinity);
});
});