forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fragment-loader.js
77 lines (66 loc) · 2.35 KB
/
fragment-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Fragment Loader
*/
import Event from '../events';
import EventHandler from '../event-handler';
import {ErrorTypes, ErrorDetails} from '../errors';
import {logger} from '../utils/logger';
class FragmentLoader extends EventHandler {
constructor(hls) {
super(hls, Event.FRAG_LOADING);
this.loaders = {};
}
destroy() {
for (let loaderName in this.loaders) {
let loader = this.loaders[loaderName];
if (loader) {
loader.destroy();
}
}
this.loaders = {};
EventHandler.prototype.destroy.call(this);
}
onFragLoading(data) {
let frag = data.frag,
type = frag.type,
loader = this.loaders[type],
config = this.hls.config;
frag.loaded = 0;
if (loader) {
logger.warn(`abort previous fragment loader for type:${type}`);
loader.abort();
}
loader = this.loaders[type] = frag.loader = typeof(config.fLoader) !== 'undefined' ? new config.fLoader(config) : new config.loader(config);
loader.load(frag.url, { frag : frag }, 'arraybuffer', this.loadsuccess.bind(this), this.loaderror.bind(this), this.loadtimeout.bind(this), config.fragLoadingTimeOut, 1, 0, this.loadprogress.bind(this), frag);
}
loadsuccess(event, stats, context) {
let payload = event.currentTarget.response, frag = context.frag;
stats.length = payload.byteLength;
// detach fragment loader on load success
frag.loader = undefined;
this.loaders[frag.type] = undefined;
this.hls.trigger(Event.FRAG_LOADED, {payload: payload, frag: frag, stats: stats});
}
loaderror(event, context) {
let loader = context.loader;
if (loader) {
loader.abort();
}
this.loaders[context.type] = undefined;
this.hls.trigger(Event.ERROR, {type: ErrorTypes.NETWORK_ERROR, details: ErrorDetails.FRAG_LOAD_ERROR, fatal: false, frag: context.frag, response: event});
}
loadtimeout(event, stats, context) {
let loader = context.loader;
if (loader) {
loader.abort();
}
this.loaders[context.type] = undefined;
this.hls.trigger(Event.ERROR, {type: ErrorTypes.NETWORK_ERROR, details: ErrorDetails.FRAG_LOAD_TIMEOUT, fatal: false, frag: context.frag});
}
loadprogress(event, stats, context) {
let frag = context.frag;
frag.loaded = stats.loaded;
this.hls.trigger(Event.FRAG_LOAD_PROGRESS, {frag: frag, stats: stats});
}
}
export default FragmentLoader;