Skip to content

Commit

Permalink
Merge pull request bytedance#727 from woopen/master
Browse files Browse the repository at this point in the history
fix: hls url parsing bytedance#723
  • Loading branch information
leonardoFu authored Apr 22, 2022
2 parents ea397a4 + e228551 commit 4a7fb5f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 32 deletions.
42 changes: 17 additions & 25 deletions packages/xgplayer-demux/src/hls/demuxer/m3u8parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class M3U8Parser {
let ref = refs.shift()
if (!ref.match('#EXTM3U')) {
throw new Error(`Invalid m3u8 file: not "#EXTM3U"`);
return null;
}
ref = refs.shift();
let nextDiscontinue = false;
Expand Down Expand Up @@ -63,39 +62,21 @@ class M3U8Parser {
ret.frags = []
}

let freg = {
const frag = {
start: ret.duration,
duration: parseFloat(refm[2]) * 1000
}

ret.duration += freg.duration;
ret.duration += frag.duration;
let nextline = refs.shift();
if (nextline.match(/#(.*):(.*)/)) {
nextline = refs.shift();
}
if (nextline.length > 0 && nextline.charAt(0) === '/' && baseurl.match(/.*\/\/.*\.\w+/g)) {
baseurl = baseurl.match(/.*\/\/.*\.\w+/g)[0];
}
if (nextline.match(/.*:\/\/.*/)) {
freg.url = nextline;
} else {
freg.url = baseurl + nextline;
}
freg.discontinue = discontinue;
ret.frags.push(freg);
}

static parseURL (url) {
let baseurl = '';
let urls = url.match(/(.*\/).*\.m3u8/);
if (urls && urls.length > 0) {
for (let i = 0; i < urls.length; i++) {
if (urls[i].match(/.*\/$/g) && urls[i].length > baseurl.length) {
baseurl = urls[i];
}
}
}
return baseurl;
frag.url = getAbsoluteUrl(nextline, baseurl)

frag.discontinue = discontinue;
ret.frags.push(frag);
}

static parseDecrypt(refm, ret) {
Expand Down Expand Up @@ -124,4 +105,15 @@ class M3U8Parser {
}
}

const REGEXP_ABSOLUTE_URL = /^(?:[a-zA-Z0-9+\-.]+:)?\/\//
const REGEXP_URL_PAIR = /^((?:[a-zA-Z0-9+\-.]+:)?\/\/[^/?#]*)?([^?#]*\/)?/

function getAbsoluteUrl (url, parentUrl) {
if (!parentUrl || !url || REGEXP_ABSOLUTE_URL.test(url)) return url
const pairs = REGEXP_URL_PAIR.exec(parentUrl)
if (!pairs) return url
if (url[0] === '/') return pairs[1] + url
return pairs[1] + pairs[2] + url
}

export default M3U8Parser;
4 changes: 1 addition & 3 deletions packages/xgplayer-hls-live/src/hls-live.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class HlsLiveController {
constructor (configs) {
this.configs = Object.assign({}, configs);
this.url = '';
this.baseurl = '';
this.sequence = 0;
this._playlist = null;
this.retrytimes = this.configs.retrytimes || 3;
Expand Down Expand Up @@ -119,7 +118,7 @@ class HlsLiveController {
let mdata;
try {
this.m3u8Text = buffer.shift();
mdata = M3U8Parser.parse(this.m3u8Text, this.baseurl);
mdata = M3U8Parser.parse(this.m3u8Text, this.url);
} catch (error) {
this._onError('M3U8_PARSER_ERROR', 'M3U8_PARSER', error, false);
}
Expand Down Expand Up @@ -245,7 +244,6 @@ class HlsLiveController {
}

load (url) {
this.baseurl = M3U8Parser.parseURL(url);
this.url = url;
this._preload();
}
Expand Down
5 changes: 1 addition & 4 deletions packages/xgplayer-hls-vod/src/hls-vod.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class HlsVodController {
constructor (configs) {
this.configs = Object.assign({}, configs);
this.url = '';
this.baseurl = '';
this.sequence = 0;
this._playlist = null;
this.retrytimes = this.configs.retrytimes || 3;
Expand Down Expand Up @@ -163,7 +162,7 @@ class HlsVodController {
_onLoaderCompete (buffer) {
if (buffer.TAG === 'M3U8_BUFFER') {
this.m3u8Text = buffer.shift()
let mdata = M3U8Parser.parse(this.m3u8Text, this.baseurl);
let mdata = M3U8Parser.parse(this.m3u8Text, this.url);
try {
this._playlist.pushM3U8(mdata);
} catch (error) {
Expand Down Expand Up @@ -267,7 +266,6 @@ class HlsVodController {
}

load (url) {
this.baseurl = M3U8Parser.parseURL(url);
this.url = url;
this.emitTo('M3U8_LOADER', LOADER_EVENTS.LADER_START, url)
}
Expand Down Expand Up @@ -337,7 +335,6 @@ class HlsVodController {
destory () {
this.configs = {};
this.url = '';
this.baseurl = '';
this.sequence = 0;
this._playlist = null;
this.retrytimes = 3;
Expand Down

0 comments on commit 4a7fb5f

Please sign in to comment.