forked from video-dev/hls.js
/
loader.ts
181 lines (162 loc) · 4.95 KB
/
loader.ts
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import type { Fragment } from '../loader/fragment';
import type { Part } from '../loader/fragment';
import type { LevelDetails } from '../loader/level-details';
import type { HlsUrlParameters } from './level';
export interface LoaderContext {
// target URL
url: string;
// loader response type (arraybuffer or default response type for playlist)
responseType: string;
// start byte range offset
rangeStart?: number;
// end byte range offset
rangeEnd?: number;
// true if onProgress should report partial chunk of loaded content
progressData?: boolean;
}
export interface FragmentLoaderContext extends LoaderContext {
frag: Fragment;
part: Part | null;
}
export interface LoaderConfiguration {
// Max number of load retries
maxRetry: number;
// Timeout after which `onTimeOut` callback will be triggered
// (if loading is still not finished after that delay)
timeout: number;
// Delay between an I/O error and following connection retry (ms).
// This to avoid spamming the server
retryDelay: number;
// max connection retry delay (ms)
maxRetryDelay: number;
// When streaming progressively, this is the minimum chunk size required to emit a PROGRESS event
highWaterMark: number;
}
export interface LoaderResponse {
url: string;
data: string | ArrayBuffer;
}
export interface LoaderStats {
aborted: boolean;
loaded: number;
retry: number;
total: number;
chunkCount: number;
bwEstimate: number;
loading: HlsProgressivePerformanceTiming;
parsing: HlsPerformanceTiming;
buffering: HlsProgressivePerformanceTiming;
}
export interface HlsPerformanceTiming {
start: number;
end: number;
}
export interface HlsChunkPerformanceTiming extends HlsPerformanceTiming {
executeStart: number;
executeEnd: number;
}
export interface HlsProgressivePerformanceTiming extends HlsPerformanceTiming {
first: number;
}
export type LoaderOnSuccess<T extends LoaderContext> = (
response: LoaderResponse,
stats: LoaderStats,
context: T,
networkDetails: any
) => void;
export type LoaderOnProgress<T extends LoaderContext> = (
stats: LoaderStats,
context: T,
data: string | ArrayBuffer,
networkDetails: any
) => void;
export type LoaderOnError<T extends LoaderContext> = (
error: {
// error status code
code: number;
// error description
text: string;
},
context: T,
networkDetails: any
) => void;
export type LoaderOnTimeout<T extends LoaderContext> = (
stats: LoaderStats,
context: T,
networkDetails: any
) => void;
export type LoaderOnAbort<T extends LoaderContext> = (
stats: LoaderStats,
context: T,
networkDetails: any
) => void;
export interface LoaderCallbacks<T extends LoaderContext> {
onSuccess: LoaderOnSuccess<T>;
onError: LoaderOnError<T>;
onTimeout: LoaderOnTimeout<T>;
onAbort?: LoaderOnAbort<T>;
onProgress?: LoaderOnProgress<T>;
}
export interface Loader<T extends LoaderContext> {
destroy(): void;
abort(): void;
load(
context: LoaderContext,
config: LoaderConfiguration,
callbacks: LoaderCallbacks<T>
): void;
/**
* `getCacheAge()` is called by hls.js to get the duration that a given object
* has been sitting in a cache proxy when playing live. If implemented,
* this should return a value in seconds.
*
* For HTTP based loaders, this should return the contents of the "age" header.
*
* @returns time object being lodaded
*/
getCacheAge?: () => number;
/**
* `getResponseHeader()` is called by hls.js to get the duration that a given
* object has been sitting in a proxy cache when playing live.
*
* For HTTP based loaders, this should just return the "age" header. For
* non-HTTP based loaders, if the retrieved object has been cached, this
* should return the time the object has been in a cache, as a string, possibly
* with a decimal point. Otherwise implementers should return "0" or null.
*
* Note that HLS.js will never call this for a header other than "age".
*
* @deprecated - Implement `getCacheAge()` instead.
*/
getResponseHeader?: (name: 'age') => string | null;
context: T;
loader: any;
stats: LoaderStats;
}
export enum PlaylistContextType {
MANIFEST = 'manifest',
LEVEL = 'level',
AUDIO_TRACK = 'audioTrack',
SUBTITLE_TRACK = 'subtitleTrack',
}
export enum PlaylistLevelType {
MAIN = 'main',
AUDIO = 'audio',
SUBTITLE = 'subtitle',
}
export interface PlaylistLoaderContext extends LoaderContext {
loader?: Loader<PlaylistLoaderContext>;
type: PlaylistContextType;
// the level index to load
level: number | null;
// level or track id from LevelLoadingData / TrackLoadingData
id: number | null;
// track group id
groupId: string | null;
// defines if the loader is handling a sidx request for the playlist
isSidxRequest?: boolean;
// internal representation of a parsed m3u8 level playlist
levelDetails?: LevelDetails;
// Blocking playlist request delivery directives (or null id none were added to playlist url
deliveryDirectives: HlsUrlParameters | null;
}