mirror of https://bitbucket.org/ausocean/av.git
Merged in m3u-minor-2 (pull request #353)
mjpeg-player: minor changes to xhr-loader and frag-loader Approved-by: Scott Barnard
This commit is contained in:
commit
defa938fd9
|
@ -1,19 +1,40 @@
|
||||||
|
/*
|
||||||
|
AUTHOR
|
||||||
|
Trek Hopton <trek@ausocean.org>
|
||||||
|
|
||||||
|
LICENSE
|
||||||
|
This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
|
||||||
|
|
||||||
|
It is free software: you can redistribute it and/or modify them
|
||||||
|
under the terms of the GNU General Public License as published by the
|
||||||
|
Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
option) any later version.
|
||||||
|
|
||||||
|
It is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License in gpl.txt.
|
||||||
|
If not, see http://www.gnu.org/licenses.
|
||||||
|
|
||||||
|
For hls.js Copyright notice and license, see LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Fragment Loader
|
* Fragment Loader
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Event from '../events';
|
import Event from '../events.js';
|
||||||
import EventHandler from '../event-handler';
|
import EventHandler from '../event-handler.js';
|
||||||
import { ErrorTypes, ErrorDetails } from '../errors';
|
|
||||||
import { logger } from '../utils/logger';
|
|
||||||
|
|
||||||
class FragmentLoader extends EventHandler {
|
class FragmentLoader extends EventHandler {
|
||||||
constructor (hls) {
|
constructor(hls) {
|
||||||
super(hls, Event.FRAG_LOADING);
|
super(hls, Event.FRAG_LOADING);
|
||||||
this.loaders = {};
|
this.loaders = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy () {
|
destroy() {
|
||||||
let loaders = this.loaders;
|
let loaders = this.loaders;
|
||||||
for (let loaderName in loaders) {
|
for (let loaderName in loaders) {
|
||||||
let loader = loaders[loaderName];
|
let loader = loaders[loaderName];
|
||||||
|
@ -26,7 +47,7 @@ class FragmentLoader extends EventHandler {
|
||||||
super.destroy();
|
super.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
onFragLoading (data) {
|
onFragLoading(data) {
|
||||||
const frag = data.frag,
|
const frag = data.frag,
|
||||||
type = frag.type,
|
type = frag.type,
|
||||||
loaders = this.loaders,
|
loaders = this.loaders,
|
||||||
|
@ -39,7 +60,7 @@ class FragmentLoader extends EventHandler {
|
||||||
|
|
||||||
let loader = loaders[type];
|
let loader = loaders[type];
|
||||||
if (loader) {
|
if (loader) {
|
||||||
logger.warn(`abort previous fragment loader for type: ${type}`);
|
console.warn(`abort previous fragment loader for type: ${type}`);
|
||||||
loader.abort();
|
loader.abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +96,7 @@ class FragmentLoader extends EventHandler {
|
||||||
loader.load(loaderContext, loaderConfig, loaderCallbacks);
|
loader.load(loaderContext, loaderConfig, loaderCallbacks);
|
||||||
}
|
}
|
||||||
|
|
||||||
loadsuccess (response, stats, context, networkDetails = null) {
|
loadsuccess(response, stats, context, networkDetails = null) {
|
||||||
let payload = response.data, frag = context.frag;
|
let payload = response.data, frag = context.frag;
|
||||||
// detach fragment loader on load success
|
// detach fragment loader on load success
|
||||||
frag.loader = undefined;
|
frag.loader = undefined;
|
||||||
|
@ -83,7 +104,7 @@ class FragmentLoader extends EventHandler {
|
||||||
this.hls.trigger(Event.FRAG_LOADED, { payload: payload, frag: frag, stats: stats, networkDetails: networkDetails });
|
this.hls.trigger(Event.FRAG_LOADED, { payload: payload, frag: frag, stats: stats, networkDetails: networkDetails });
|
||||||
}
|
}
|
||||||
|
|
||||||
loaderror (response, context, networkDetails = null) {
|
loaderror(response, context, networkDetails = null) {
|
||||||
const frag = context.frag;
|
const frag = context.frag;
|
||||||
let loader = frag.loader;
|
let loader = frag.loader;
|
||||||
if (loader) {
|
if (loader) {
|
||||||
|
@ -94,7 +115,7 @@ class FragmentLoader extends EventHandler {
|
||||||
this.hls.trigger(Event.ERROR, { type: ErrorTypes.NETWORK_ERROR, details: ErrorDetails.FRAG_LOAD_ERROR, fatal: false, frag: context.frag, response: response, networkDetails: networkDetails });
|
this.hls.trigger(Event.ERROR, { type: ErrorTypes.NETWORK_ERROR, details: ErrorDetails.FRAG_LOAD_ERROR, fatal: false, frag: context.frag, response: response, networkDetails: networkDetails });
|
||||||
}
|
}
|
||||||
|
|
||||||
loadtimeout (stats, context, networkDetails = null) {
|
loadtimeout(stats, context, networkDetails = null) {
|
||||||
const frag = context.frag;
|
const frag = context.frag;
|
||||||
let loader = frag.loader;
|
let loader = frag.loader;
|
||||||
if (loader) {
|
if (loader) {
|
||||||
|
@ -106,7 +127,7 @@ class FragmentLoader extends EventHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
// data will be used for progressive parsing
|
// data will be used for progressive parsing
|
||||||
loadprogress (stats, context, data, networkDetails = null) { // jshint ignore:line
|
loadprogress(stats, context, data, networkDetails = null) { // jshint ignore:line
|
||||||
let frag = context.frag;
|
let frag = context.frag;
|
||||||
frag.loaded = stats.loaded;
|
frag.loaded = stats.loaded;
|
||||||
this.hls.trigger(Event.FRAG_LOAD_PROGRESS, { frag: frag, stats: stats, networkDetails: networkDetails });
|
this.hls.trigger(Event.FRAG_LOAD_PROGRESS, { frag: frag, stats: stats, networkDetails: networkDetails });
|
||||||
|
|
|
@ -1,24 +1,45 @@
|
||||||
/**
|
/*
|
||||||
* XHR based logger
|
AUTHOR
|
||||||
|
Trek Hopton <trek@ausocean.org>
|
||||||
|
|
||||||
|
LICENSE
|
||||||
|
This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
|
||||||
|
|
||||||
|
It is free software: you can redistribute it and/or modify them
|
||||||
|
under the terms of the GNU General Public License as published by the
|
||||||
|
Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
option) any later version.
|
||||||
|
|
||||||
|
It is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License in gpl.txt.
|
||||||
|
If not, see http://www.gnu.org/licenses.
|
||||||
|
|
||||||
|
For hls.js Copyright notice and license, see LICENSE file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { logger } from '../utils/logger';
|
/**
|
||||||
|
* XHR based loader
|
||||||
|
*/
|
||||||
|
|
||||||
const { performance, XMLHttpRequest } = window;
|
const { performance, XMLHttpRequest } = window;
|
||||||
|
|
||||||
class XhrLoader {
|
class XhrLoader {
|
||||||
constructor (config) {
|
constructor(config) {
|
||||||
if (config && config.xhrSetup) {
|
if (config && config.xhrSetup) {
|
||||||
this.xhrSetup = config.xhrSetup;
|
this.xhrSetup = config.xhrSetup;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy () {
|
destroy() {
|
||||||
this.abort();
|
this.abort();
|
||||||
this.loader = null;
|
this.loader = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
abort () {
|
abort() {
|
||||||
let loader = this.loader;
|
let loader = this.loader;
|
||||||
if (loader && loader.readyState !== 4) {
|
if (loader && loader.readyState !== 4) {
|
||||||
this.stats.aborted = true;
|
this.stats.aborted = true;
|
||||||
|
@ -31,7 +52,7 @@ class XhrLoader {
|
||||||
this.retryTimeout = null;
|
this.retryTimeout = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
load (context, config, callbacks) {
|
load(context, config, callbacks) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.callbacks = callbacks;
|
this.callbacks = callbacks;
|
||||||
|
@ -40,9 +61,10 @@ class XhrLoader {
|
||||||
this.loadInternal();
|
this.loadInternal();
|
||||||
}
|
}
|
||||||
|
|
||||||
loadInternal () {
|
loadInternal() {
|
||||||
let xhr, context = this.context;
|
let xhr, context = this.context;
|
||||||
xhr = this.loader = new XMLHttpRequest();
|
xhr = this.loader = new XMLHttpRequest();
|
||||||
|
window.console.log("load internal xhr: " + context.url);
|
||||||
|
|
||||||
let stats = this.stats;
|
let stats = this.stats;
|
||||||
stats.tfirst = 0;
|
stats.tfirst = 0;
|
||||||
|
@ -82,7 +104,7 @@ class XhrLoader {
|
||||||
xhr.send();
|
xhr.send();
|
||||||
}
|
}
|
||||||
|
|
||||||
readystatechange (event) {
|
readystatechange(event) {
|
||||||
let xhr = event.currentTarget,
|
let xhr = event.currentTarget,
|
||||||
readyState = xhr.readyState,
|
readyState = xhr.readyState,
|
||||||
stats = this.stats,
|
stats = this.stats,
|
||||||
|
@ -121,11 +143,11 @@ class XhrLoader {
|
||||||
} else {
|
} else {
|
||||||
// if max nb of retries reached or if http status between 400 and 499 (such error cannot be recovered, retrying is useless), return error
|
// if max nb of retries reached or if http status between 400 and 499 (such error cannot be recovered, retrying is useless), return error
|
||||||
if (stats.retry >= config.maxRetry || (status >= 400 && status < 499)) {
|
if (stats.retry >= config.maxRetry || (status >= 400 && status < 499)) {
|
||||||
logger.error(`${status} while loading ${context.url}`);
|
console.error(`${status} while loading ${context.url}`);
|
||||||
this.callbacks.onError({ code: status, text: xhr.statusText }, context, xhr);
|
this.callbacks.onError({ code: status, text: xhr.statusText }, context, xhr);
|
||||||
} else {
|
} else {
|
||||||
// retry
|
// retry
|
||||||
logger.warn(`${status} while loading ${context.url}, retrying in ${this.retryDelay}...`);
|
console.warn(`${status} while loading ${context.url}, retrying in ${this.retryDelay}...`);
|
||||||
// aborts and resets internal state
|
// aborts and resets internal state
|
||||||
this.destroy();
|
this.destroy();
|
||||||
// schedule retry
|
// schedule retry
|
||||||
|
@ -142,12 +164,12 @@ class XhrLoader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
loadtimeout () {
|
loadtimeout() {
|
||||||
logger.warn(`timeout while loading ${this.context.url}`);
|
console.warn(`timeout while loading ${this.context.url}`);
|
||||||
this.callbacks.onTimeout(this.stats, this.context, null);
|
this.callbacks.onTimeout(this.stats, this.context, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
loadprogress (event) {
|
loadprogress(event) {
|
||||||
let xhr = event.currentTarget,
|
let xhr = event.currentTarget,
|
||||||
stats = this.stats;
|
stats = this.stats;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue