2020-01-21 06:02:29 +03:00
|
|
|
/*
|
|
|
|
* Level Controller
|
|
|
|
*/
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
import Event from '../events.js';
|
|
|
|
import EventHandler from '../event-handler.js';
|
|
|
|
import { addGroupId, computeReloadInterval } from './level-helper.js';
|
2020-01-21 06:02:29 +03:00
|
|
|
|
|
|
|
const { performance } = window;
|
|
|
|
let chromeOrFirefox;
|
|
|
|
|
|
|
|
export default class LevelController extends EventHandler {
|
2020-01-24 07:54:29 +03:00
|
|
|
constructor(hls) {
|
2020-01-21 06:02:29 +03:00
|
|
|
super(hls,
|
|
|
|
Event.MANIFEST_LOADED,
|
2020-01-24 07:54:29 +03:00
|
|
|
Event.LEVEL_LOADED);
|
2020-01-21 06:02:29 +03:00
|
|
|
|
|
|
|
this.canload = false;
|
2020-01-24 07:54:29 +03:00
|
|
|
this.currentLevelIndex = 0;
|
2020-01-21 06:02:29 +03:00
|
|
|
this.manualLevelIndex = -1;
|
|
|
|
this.timer = null;
|
|
|
|
|
|
|
|
chromeOrFirefox = /chrome|firefox/.test(navigator.userAgent.toLowerCase());
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
onHandlerDestroying() {
|
2020-01-21 06:02:29 +03:00
|
|
|
this.clearTimer();
|
|
|
|
this.manualLevelIndex = -1;
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
clearTimer() {
|
2020-01-21 06:02:29 +03:00
|
|
|
if (this.timer !== null) {
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
this.timer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
startLoad() {
|
2020-01-21 06:02:29 +03:00
|
|
|
let levels = this._levels;
|
|
|
|
|
|
|
|
this.canload = true;
|
|
|
|
this.levelRetryCount = 0;
|
|
|
|
|
|
|
|
// clean up live level details to force reload them, and reset load errors
|
|
|
|
if (levels) {
|
|
|
|
levels.forEach(level => {
|
|
|
|
level.loadError = 0;
|
|
|
|
const levelDetails = level.details;
|
|
|
|
if (levelDetails && levelDetails.live) {
|
|
|
|
level.details = undefined;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// speed up live playlist refresh if timer exists
|
|
|
|
if (this.timer !== null) {
|
|
|
|
this.loadLevel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
stopLoad() {
|
2020-01-21 06:02:29 +03:00
|
|
|
this.canload = false;
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
onManifestLoaded(data) {
|
2020-01-21 06:02:29 +03:00
|
|
|
let levels = [];
|
|
|
|
let audioTracks = [];
|
|
|
|
let bitrateStart;
|
|
|
|
let levelSet = {};
|
|
|
|
let levelFromSet = null;
|
|
|
|
let videoCodecFound = false;
|
|
|
|
let audioCodecFound = false;
|
|
|
|
|
|
|
|
// regroup redundant levels together
|
|
|
|
data.levels.forEach(level => {
|
|
|
|
const attributes = level.attrs;
|
|
|
|
level.loadError = 0;
|
|
|
|
level.fragmentError = false;
|
|
|
|
|
|
|
|
videoCodecFound = videoCodecFound || !!level.videoCodec;
|
|
|
|
audioCodecFound = audioCodecFound || !!level.audioCodec;
|
|
|
|
|
|
|
|
// erase audio codec info if browser does not support mp4a.40.34.
|
|
|
|
// demuxer will autodetect codec and fallback to mpeg/audio
|
|
|
|
if (chromeOrFirefox && level.audioCodec && level.audioCodec.indexOf('mp4a.40.34') !== -1) {
|
|
|
|
level.audioCodec = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
levelFromSet = levelSet[level.bitrate]; // FIXME: we would also have to match the resolution here
|
|
|
|
|
|
|
|
if (!levelFromSet) {
|
|
|
|
level.url = [level.url];
|
|
|
|
level.urlId = 0;
|
|
|
|
levelSet[level.bitrate] = level;
|
|
|
|
levels.push(level);
|
|
|
|
} else {
|
|
|
|
levelFromSet.url.push(level.url);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attributes) {
|
|
|
|
if (attributes.AUDIO) {
|
|
|
|
audioCodecFound = true;
|
|
|
|
addGroupId(levelFromSet || level, 'audio', attributes.AUDIO);
|
|
|
|
}
|
|
|
|
if (attributes.SUBTITLES) {
|
|
|
|
addGroupId(levelFromSet || level, 'text', attributes.SUBTITLES);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (levels.length > 0) {
|
|
|
|
// start bitrate is the first bitrate of the manifest
|
|
|
|
bitrateStart = levels[0].bitrate;
|
|
|
|
// sort level on bitrate
|
|
|
|
levels.sort((a, b) => a.bitrate - b.bitrate);
|
|
|
|
this._levels = levels;
|
|
|
|
// find index of first level in sorted levels
|
|
|
|
for (let i = 0; i < levels.length; i++) {
|
|
|
|
if (levels[i].bitrate === bitrateStart) {
|
|
|
|
this._firstLevel = i;
|
2020-01-24 07:54:29 +03:00
|
|
|
// console.log(`manifest loaded,${levels.length} level(s) found, first bitrate:${bitrateStart}`);
|
2020-01-21 06:02:29 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Audio is only alternate if manifest include a URI along with the audio group tag
|
|
|
|
this.hls.trigger(Event.MANIFEST_PARSED, {
|
|
|
|
levels,
|
|
|
|
audioTracks,
|
|
|
|
firstLevel: this._firstLevel,
|
|
|
|
stats: data.stats,
|
|
|
|
audio: audioCodecFound,
|
|
|
|
video: videoCodecFound,
|
|
|
|
altAudio: audioTracks.some(t => !!t.url)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.hls.trigger(Event.ERROR, {
|
|
|
|
type: ErrorTypes.MEDIA_ERROR,
|
|
|
|
details: ErrorDetails.MANIFEST_INCOMPATIBLE_CODECS_ERROR,
|
|
|
|
fatal: true,
|
|
|
|
url: this.hls.url,
|
|
|
|
reason: 'no level with compatible codecs found in manifest'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
get levels() {
|
2020-01-21 06:02:29 +03:00
|
|
|
return this._levels;
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
get level() {
|
2020-01-21 06:02:29 +03:00
|
|
|
return this.currentLevelIndex;
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
set level(newLevel) {
|
2020-01-21 06:02:29 +03:00
|
|
|
let levels = this._levels;
|
|
|
|
if (levels) {
|
|
|
|
newLevel = Math.min(newLevel, levels.length - 1);
|
|
|
|
if (this.currentLevelIndex !== newLevel || !levels[newLevel].details) {
|
|
|
|
this.setLevelInternal(newLevel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
setLevelInternal(newLevel) {
|
2020-01-21 06:02:29 +03:00
|
|
|
const levels = this._levels;
|
|
|
|
const hls = this.hls;
|
|
|
|
// check if level idx is valid
|
|
|
|
if (newLevel >= 0 && newLevel < levels.length) {
|
|
|
|
// stopping live reloading timer if any
|
|
|
|
this.clearTimer();
|
|
|
|
if (this.currentLevelIndex !== newLevel) {
|
2020-01-24 07:54:29 +03:00
|
|
|
console.log(`switching to level ${newLevel}`);
|
2020-01-21 06:02:29 +03:00
|
|
|
this.currentLevelIndex = newLevel;
|
|
|
|
const levelProperties = levels[newLevel];
|
|
|
|
levelProperties.level = newLevel;
|
|
|
|
hls.trigger(Event.LEVEL_SWITCHING, levelProperties);
|
|
|
|
}
|
|
|
|
const level = levels[newLevel];
|
|
|
|
const levelDetails = level.details;
|
|
|
|
|
|
|
|
// check if we need to load playlist for this level
|
|
|
|
if (!levelDetails || levelDetails.live) {
|
|
|
|
// level not retrieved yet, or live playlist we need to (re)load it
|
|
|
|
let urlId = level.urlId;
|
|
|
|
hls.trigger(Event.LEVEL_LOADING, { url: level.url[urlId], level: newLevel, id: urlId });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// invalid level id given, trigger error
|
|
|
|
hls.trigger(Event.ERROR, {
|
|
|
|
type: ErrorTypes.OTHER_ERROR,
|
|
|
|
details: ErrorDetails.LEVEL_SWITCH_ERROR,
|
|
|
|
level: newLevel,
|
|
|
|
fatal: false,
|
|
|
|
reason: 'invalid level idx'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
get manualLevel() {
|
2020-01-21 06:02:29 +03:00
|
|
|
return this.manualLevelIndex;
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
set manualLevel(newLevel) {
|
2020-01-21 06:02:29 +03:00
|
|
|
this.manualLevelIndex = newLevel;
|
|
|
|
if (this._startLevel === undefined) {
|
|
|
|
this._startLevel = newLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newLevel !== -1) {
|
|
|
|
this.level = newLevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
get firstLevel() {
|
2020-01-21 06:02:29 +03:00
|
|
|
return this._firstLevel;
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
set firstLevel(newLevel) {
|
2020-01-21 06:02:29 +03:00
|
|
|
this._firstLevel = newLevel;
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
get startLevel() {
|
2020-01-21 06:02:29 +03:00
|
|
|
// hls.startLevel takes precedence over config.startLevel
|
|
|
|
// if none of these values are defined, fallback on this._firstLevel (first quality level appearing in variant manifest)
|
|
|
|
if (this._startLevel === undefined) {
|
|
|
|
let configStartLevel = this.hls.config.startLevel;
|
|
|
|
if (configStartLevel !== undefined) {
|
|
|
|
return configStartLevel;
|
|
|
|
} else {
|
|
|
|
return this._firstLevel;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return this._startLevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
set startLevel(newLevel) {
|
2020-01-21 06:02:29 +03:00
|
|
|
this._startLevel = newLevel;
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
onLevelLoaded(data) {
|
2020-01-21 06:02:29 +03:00
|
|
|
const { level, details } = data;
|
|
|
|
const curLevel = this._levels[level];
|
|
|
|
// if current playlist is a live playlist, arm a timer to reload it
|
|
|
|
if (details.live) {
|
|
|
|
const reloadInterval = computeReloadInterval(curLevel.details, details, data.stats.trequest);
|
2020-01-24 07:54:29 +03:00
|
|
|
console.log(`live playlist, reload in ${Math.round(reloadInterval)} ms`);
|
2020-01-21 06:02:29 +03:00
|
|
|
this.timer = setTimeout(() => this.loadLevel(), reloadInterval);
|
|
|
|
} else {
|
|
|
|
this.clearTimer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
loadLevel() {
|
|
|
|
console.log('call to loadLevel, index: ' + this.currentLevelIndex + "canload: " + this.canload);
|
2020-01-21 06:02:29 +03:00
|
|
|
|
|
|
|
if (this.currentLevelIndex !== null && this.canload) {
|
|
|
|
const levelObject = this._levels[this.currentLevelIndex];
|
|
|
|
|
|
|
|
if (typeof levelObject === 'object' &&
|
|
|
|
levelObject.url.length > 0) {
|
|
|
|
const level = this.currentLevelIndex;
|
|
|
|
const id = levelObject.urlId;
|
|
|
|
const url = levelObject.url[id];
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
console.log(`Attempt loading level index ${level} with URL-id ${id}`);
|
2020-01-21 06:02:29 +03:00
|
|
|
|
|
|
|
this.hls.trigger(Event.LEVEL_LOADING, { url, level, id });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
get nextLoadLevel() {
|
2020-01-21 06:02:29 +03:00
|
|
|
if (this.manualLevelIndex !== -1) {
|
|
|
|
return this.manualLevelIndex;
|
|
|
|
} else {
|
|
|
|
return this.hls.nextAutoLevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 07:54:29 +03:00
|
|
|
set nextLoadLevel(nextLevel) {
|
2020-01-21 06:02:29 +03:00
|
|
|
this.level = nextLevel;
|
|
|
|
if (this.manualLevelIndex === -1) {
|
|
|
|
this.hls.nextAutoLevel = nextLevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|