@bsky.app/hls 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +93 -2
- package/dist/abr.d.ts +25 -0
- package/dist/abr.d.ts.map +1 -0
- package/dist/abr.js +67 -0
- package/dist/abr.js.map +1 -0
- package/dist/bandwidth.d.ts +8 -0
- package/dist/bandwidth.d.ts.map +1 -0
- package/dist/bandwidth.js +48 -0
- package/dist/bandwidth.js.map +1 -0
- package/dist/errors.d.ts +30 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +38 -0
- package/dist/errors.js.map +1 -0
- package/dist/fmp4.d.ts +3 -0
- package/dist/fmp4.d.ts.map +1 -0
- package/dist/fmp4.js +224 -0
- package/dist/fmp4.js.map +1 -0
- package/dist/gaps.d.ts +16 -0
- package/dist/gaps.d.ts.map +1 -0
- package/dist/gaps.js +40 -0
- package/dist/gaps.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/loader.d.ts +26 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +94 -0
- package/dist/loader.js.map +1 -0
- package/dist/mse.d.ts +18 -0
- package/dist/mse.d.ts.map +1 -0
- package/dist/mse.js +123 -0
- package/dist/mse.js.map +1 -0
- package/dist/player.d.ts +3 -0
- package/dist/player.d.ts.map +1 -0
- package/dist/player.js +715 -0
- package/dist/player.js.map +1 -0
- package/dist/playlist.d.ts +7 -0
- package/dist/playlist.d.ts.map +1 -0
- package/dist/playlist.js +151 -0
- package/dist/playlist.js.map +1 -0
- package/dist/scheduler.d.ts +24 -0
- package/dist/scheduler.d.ts.map +1 -0
- package/dist/scheduler.js +79 -0
- package/dist/scheduler.js.map +1 -0
- package/dist/subtitles.d.ts +5 -0
- package/dist/subtitles.d.ts.map +1 -0
- package/dist/subtitles.js +29 -0
- package/dist/subtitles.js.map +1 -0
- package/dist/ts/aac.d.ts +18 -0
- package/dist/ts/aac.d.ts.map +1 -0
- package/dist/ts/aac.js +55 -0
- package/dist/ts/aac.js.map +1 -0
- package/dist/ts/demux.d.ts +19 -0
- package/dist/ts/demux.d.ts.map +1 -0
- package/dist/ts/demux.js +91 -0
- package/dist/ts/demux.js.map +1 -0
- package/dist/ts/h264.d.ts +25 -0
- package/dist/ts/h264.d.ts.map +1 -0
- package/dist/ts/h264.js +171 -0
- package/dist/ts/h264.js.map +1 -0
- package/dist/ts/index.d.ts +3 -0
- package/dist/ts/index.d.ts.map +1 -0
- package/dist/ts/index.js +117 -0
- package/dist/ts/index.js.map +1 -0
- package/dist/ts/mp4.d.ts +32 -0
- package/dist/ts/mp4.d.ts.map +1 -0
- package/dist/ts/mp4.js +76 -0
- package/dist/ts/mp4.js.map +1 -0
- package/dist/types.d.ts +153 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +23 -0
- package/dist/types.js.map +1 -0
- package/package.json +45 -4
- package/src/abr.ts +114 -0
- package/src/bandwidth.ts +57 -0
- package/src/errors.ts +73 -0
- package/src/fmp4.ts +228 -0
- package/src/gaps.ts +50 -0
- package/src/index.ts +10 -0
- package/src/loader.ts +111 -0
- package/src/mse.ts +141 -0
- package/src/player.ts +834 -0
- package/src/playlist.ts +157 -0
- package/src/scheduler.ts +114 -0
- package/src/subtitles.ts +39 -0
- package/src/ts/aac.ts +63 -0
- package/src/ts/demux.ts +93 -0
- package/src/ts/h264.ts +172 -0
- package/src/ts/index.ts +131 -0
- package/src/ts/mp4.ts +242 -0
- package/src/types.ts +216 -0
package/src/ts/index.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MPEG-TS (H.264 + ADTS AAC) to fragmented MP4, for lumi's HLS output.
|
|
3
|
+
* Loaded with import() so it lands in its own chunk: only import types from
|
|
4
|
+
* outside this directory.
|
|
5
|
+
*/
|
|
6
|
+
import type {CreateSegmentProcessor, SegmentProcessor} from '../types.js'
|
|
7
|
+
import {parseAdts} from './aac.js'
|
|
8
|
+
import {demux, type Pids} from './demux.js'
|
|
9
|
+
import {parseAvc, parseSps} from './h264.js'
|
|
10
|
+
import {fragment, initSegment, u32, type Traf} from './mp4.js'
|
|
11
|
+
|
|
12
|
+
const hex = (n: number | undefined) => (n ?? 0).toString(16).padStart(2, '0')
|
|
13
|
+
|
|
14
|
+
export const createTsProcessor: CreateSegmentProcessor = () => {
|
|
15
|
+
let pids: Pids = {}
|
|
16
|
+
let avc: {sps: Uint8Array; pps: Uint8Array} | undefined
|
|
17
|
+
let nextAudio: number | undefined
|
|
18
|
+
let seq = 0
|
|
19
|
+
let init: Uint8Array = new Uint8Array()
|
|
20
|
+
let initKey = ''
|
|
21
|
+
|
|
22
|
+
const processor: SegmentProcessor = {
|
|
23
|
+
reset() {
|
|
24
|
+
pids = {}
|
|
25
|
+
avc = undefined
|
|
26
|
+
nextAudio = undefined
|
|
27
|
+
},
|
|
28
|
+
process({data, discontinuous}) {
|
|
29
|
+
if (discontinuous) processor.reset()
|
|
30
|
+
const es = demux(data, pids)
|
|
31
|
+
const video = parseAvc(es.video)
|
|
32
|
+
if (video.sps && video.pps) avc = {sps: video.sps, pps: video.pps}
|
|
33
|
+
if (video.samples.length && !avc)
|
|
34
|
+
throw new Error('H.264: no SPS/PPS before the first frame')
|
|
35
|
+
const samples = avc ? video.samples : []
|
|
36
|
+
const {frames, config: audio} = parseAdts(es.audio)
|
|
37
|
+
const hasVideo = samples.length > 0
|
|
38
|
+
const hasAudio = !!audio
|
|
39
|
+
if (!hasVideo && !hasAudio)
|
|
40
|
+
throw new Error('MPEG-TS: segment has no H.264 or AAC frames')
|
|
41
|
+
|
|
42
|
+
const trafs: Traf[] = []
|
|
43
|
+
const codecs: string[] = []
|
|
44
|
+
let startTime = Infinity
|
|
45
|
+
let endTime = -Infinity
|
|
46
|
+
const keyframes: number[] = []
|
|
47
|
+
|
|
48
|
+
if (hasVideo && avc) {
|
|
49
|
+
const {sps} = avc
|
|
50
|
+
codecs.push(`avc1.${hex(sps[1])}${hex(sps[2])}${hex(sps[3])}`)
|
|
51
|
+
let size = 0
|
|
52
|
+
const rows = samples.map((s, i) => {
|
|
53
|
+
const next = samples[i + 1]
|
|
54
|
+
const prev = samples[i - 1]
|
|
55
|
+
const duration = next
|
|
56
|
+
? next.dts - s.dts
|
|
57
|
+
: prev
|
|
58
|
+
? s.dts - prev.dts
|
|
59
|
+
: 3000
|
|
60
|
+
const bytes = s.nals.reduce((sum, nal) => sum + 4 + nal.length, 0)
|
|
61
|
+
size += bytes
|
|
62
|
+
startTime = Math.min(startTime, s.pts)
|
|
63
|
+
endTime = Math.max(endTime, s.pts + duration)
|
|
64
|
+
if (s.key) keyframes.push(s.pts / 90000)
|
|
65
|
+
// sample_depends_on and sample_is_non_sync_sample
|
|
66
|
+
return [duration, bytes, s.key ? 0x2000000 : 0x1010000, s.pts - s.dts]
|
|
67
|
+
})
|
|
68
|
+
trafs.push({
|
|
69
|
+
id: 1,
|
|
70
|
+
base: samples[0]!.dts,
|
|
71
|
+
vf: 0x1000f01,
|
|
72
|
+
rows,
|
|
73
|
+
data: samples.flatMap(s =>
|
|
74
|
+
s.nals.flatMap(nal => [u32(nal.length), nal]),
|
|
75
|
+
),
|
|
76
|
+
size,
|
|
77
|
+
})
|
|
78
|
+
startTime /= 90000
|
|
79
|
+
endTime /= 90000
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (audio) {
|
|
83
|
+
const {rate} = audio
|
|
84
|
+
let base = Math.round((frames[0]!.pts * rate) / 90000)
|
|
85
|
+
// Absorb 90 kHz rounding so continuous segments stay sample-exact
|
|
86
|
+
if (nextAudio !== undefined && Math.abs(base - nextAudio) < 512)
|
|
87
|
+
base = nextAudio
|
|
88
|
+
nextAudio = base + frames.length * 1024
|
|
89
|
+
codecs.push(audio.codec)
|
|
90
|
+
trafs.push({
|
|
91
|
+
id: 2,
|
|
92
|
+
base,
|
|
93
|
+
vf: 0x301,
|
|
94
|
+
rows: frames.map(f => [1024, f.data.length]),
|
|
95
|
+
data: frames.map(f => f.data),
|
|
96
|
+
size: frames.reduce((sum, f) => sum + f.data.length, 0),
|
|
97
|
+
})
|
|
98
|
+
if (!hasVideo) {
|
|
99
|
+
startTime = base / rate
|
|
100
|
+
endTime = nextAudio / rate
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const newInit = initSegment(
|
|
105
|
+
hasVideo && avc ? {...avc, ...parseSps(avc.sps)} : undefined,
|
|
106
|
+
audio,
|
|
107
|
+
)
|
|
108
|
+
// FNV-1a over the init bytes, which depend only on the track config
|
|
109
|
+
let hash = 0x811c9dc5
|
|
110
|
+
for (const x of newInit) hash = Math.imul(hash ^ x, 16777619)
|
|
111
|
+
const key = `${codecs}/${(hash >>> 0).toString(36)}`
|
|
112
|
+
if (key !== initKey) {
|
|
113
|
+
init = newInit
|
|
114
|
+
initKey = key
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
init,
|
|
119
|
+
initKey,
|
|
120
|
+
data: fragment(++seq, trafs),
|
|
121
|
+
mimeType: `${hasVideo ? 'video' : 'audio'}/mp4; codecs="${codecs}"`,
|
|
122
|
+
hasVideo,
|
|
123
|
+
hasAudio,
|
|
124
|
+
startTime,
|
|
125
|
+
endTime,
|
|
126
|
+
keyframes: keyframes.sort((x, y) => x - y),
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
}
|
|
130
|
+
return processor
|
|
131
|
+
}
|
package/src/ts/mp4.ts
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Just enough ISO BMFF to describe one H.264 track (id 1) and one AAC track
|
|
3
|
+
* (id 2) for MSE: an init segment, and moof+mdat fragments.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export type Part = Uint8Array | number[]
|
|
7
|
+
|
|
8
|
+
export type VideoConfig = {
|
|
9
|
+
sps: Uint8Array
|
|
10
|
+
pps: Uint8Array
|
|
11
|
+
width: number
|
|
12
|
+
height: number
|
|
13
|
+
sarW: number
|
|
14
|
+
sarH: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type AudioConfig = {
|
|
18
|
+
rate: number
|
|
19
|
+
channels: number
|
|
20
|
+
/** AudioSpecificConfig. */
|
|
21
|
+
asc: number[]
|
|
22
|
+
codec: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type Traf = {
|
|
26
|
+
id: number
|
|
27
|
+
/** baseMediaDecodeTime, in the track's timescale. */
|
|
28
|
+
base: number
|
|
29
|
+
/** trun version and flags. */
|
|
30
|
+
vf: number
|
|
31
|
+
/** Per-sample trun fields, each written as 32 bits. */
|
|
32
|
+
rows: number[][]
|
|
33
|
+
data: Part[]
|
|
34
|
+
size: number
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function concat(parts: Part[]): Uint8Array {
|
|
38
|
+
let len = 0
|
|
39
|
+
for (const p of parts) len += p.length
|
|
40
|
+
const out = new Uint8Array(len)
|
|
41
|
+
let o = 0
|
|
42
|
+
for (const p of parts) {
|
|
43
|
+
out.set(p, o)
|
|
44
|
+
o += p.length
|
|
45
|
+
}
|
|
46
|
+
return out
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const u32 = (n: number) => [
|
|
50
|
+
n >>> 24,
|
|
51
|
+
(n >> 16) & 255,
|
|
52
|
+
(n >> 8) & 255,
|
|
53
|
+
n & 255,
|
|
54
|
+
]
|
|
55
|
+
const u16 = (n: number) => [(n >> 8) & 255, n & 255]
|
|
56
|
+
const zeros = (n: number) => new Uint8Array(n)
|
|
57
|
+
const ascii = (s: string) => [...s].map(c => c.charCodeAt(0))
|
|
58
|
+
|
|
59
|
+
function box(type: string, ...parts: Part[]): Uint8Array {
|
|
60
|
+
const out = concat([[0, 0, 0, 0], ascii(type), ...parts])
|
|
61
|
+
out.set(u32(out.length))
|
|
62
|
+
return out
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const full = (type: string, vf: number, ...parts: Part[]) =>
|
|
66
|
+
box(type, u32(vf), ...parts)
|
|
67
|
+
|
|
68
|
+
const MATRIX = [1, 0, 0, 0, 1, 0, 0, 0, 16384].flatMap(n => u32(n * 65536))
|
|
69
|
+
|
|
70
|
+
function trak(
|
|
71
|
+
id: number,
|
|
72
|
+
timescale: number,
|
|
73
|
+
width: number,
|
|
74
|
+
height: number,
|
|
75
|
+
entry: Uint8Array,
|
|
76
|
+
) {
|
|
77
|
+
const video = id === 1
|
|
78
|
+
return box(
|
|
79
|
+
'trak',
|
|
80
|
+
// flags: enabled, in movie
|
|
81
|
+
full(
|
|
82
|
+
'tkhd',
|
|
83
|
+
3,
|
|
84
|
+
zeros(8),
|
|
85
|
+
u32(id),
|
|
86
|
+
zeros(20),
|
|
87
|
+
u16(video ? 0 : 0x100),
|
|
88
|
+
[0, 0],
|
|
89
|
+
MATRIX,
|
|
90
|
+
u32(width * 65536),
|
|
91
|
+
u32(height * 65536),
|
|
92
|
+
),
|
|
93
|
+
box(
|
|
94
|
+
'mdia',
|
|
95
|
+
full('mdhd', 0, zeros(8), u32(timescale), u32(0), u16(0x55c4), [0, 0]),
|
|
96
|
+
full('hdlr', 0, zeros(4), ascii(video ? 'vide' : 'soun'), zeros(13)),
|
|
97
|
+
box(
|
|
98
|
+
'minf',
|
|
99
|
+
video ? full('vmhd', 1, zeros(8)) : full('smhd', 0, zeros(4)),
|
|
100
|
+
box('dinf', full('dref', 0, u32(1), full('url ', 1))),
|
|
101
|
+
box(
|
|
102
|
+
'stbl',
|
|
103
|
+
full('stsd', 0, u32(1), entry),
|
|
104
|
+
full('stts', 0, u32(0)),
|
|
105
|
+
full('stsc', 0, u32(0)),
|
|
106
|
+
full('stsz', 0, u32(0), u32(0)),
|
|
107
|
+
full('stco', 0, u32(0)),
|
|
108
|
+
),
|
|
109
|
+
),
|
|
110
|
+
),
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function initSegment(v?: VideoConfig, a?: AudioConfig): Uint8Array {
|
|
115
|
+
const traks: Uint8Array[] = []
|
|
116
|
+
const trex: Uint8Array[] = []
|
|
117
|
+
if (v) {
|
|
118
|
+
const {sps, pps, width, height, sarW, sarH} = v
|
|
119
|
+
traks.push(
|
|
120
|
+
trak(
|
|
121
|
+
1,
|
|
122
|
+
90000,
|
|
123
|
+
Math.round((width * sarW) / sarH),
|
|
124
|
+
height,
|
|
125
|
+
box(
|
|
126
|
+
'avc1',
|
|
127
|
+
zeros(6),
|
|
128
|
+
u16(1),
|
|
129
|
+
zeros(16),
|
|
130
|
+
u16(width),
|
|
131
|
+
u16(height),
|
|
132
|
+
u32(0x480000),
|
|
133
|
+
u32(0x480000),
|
|
134
|
+
zeros(4),
|
|
135
|
+
u16(1),
|
|
136
|
+
zeros(32),
|
|
137
|
+
u16(0x18),
|
|
138
|
+
u16(0xffff),
|
|
139
|
+
// 4-byte NALU lengths, one SPS, one PPS
|
|
140
|
+
box(
|
|
141
|
+
'avcC',
|
|
142
|
+
[1, sps[1]!, sps[2]!, sps[3]!, 0xff, 0xe1],
|
|
143
|
+
u16(sps.length),
|
|
144
|
+
sps,
|
|
145
|
+
[1],
|
|
146
|
+
u16(pps.length),
|
|
147
|
+
pps,
|
|
148
|
+
),
|
|
149
|
+
sarW === sarH ? [] : box('pasp', u32(sarW), u32(sarH)),
|
|
150
|
+
),
|
|
151
|
+
),
|
|
152
|
+
)
|
|
153
|
+
}
|
|
154
|
+
if (a) {
|
|
155
|
+
traks.push(
|
|
156
|
+
trak(
|
|
157
|
+
2,
|
|
158
|
+
a.rate,
|
|
159
|
+
0,
|
|
160
|
+
0,
|
|
161
|
+
box(
|
|
162
|
+
'mp4a',
|
|
163
|
+
zeros(6),
|
|
164
|
+
u16(1),
|
|
165
|
+
zeros(8),
|
|
166
|
+
u16(a.channels),
|
|
167
|
+
u16(16),
|
|
168
|
+
zeros(4),
|
|
169
|
+
u16(a.rate),
|
|
170
|
+
[0, 0],
|
|
171
|
+
full(
|
|
172
|
+
'esds',
|
|
173
|
+
0,
|
|
174
|
+
// ES_Descriptor, DecoderConfigDescriptor (AAC, audio stream)
|
|
175
|
+
[3, 25, 0, 0, 0, 4, 17, 0x40, 0x15],
|
|
176
|
+
zeros(11),
|
|
177
|
+
[5, 2, ...a.asc, 6, 1, 2],
|
|
178
|
+
),
|
|
179
|
+
),
|
|
180
|
+
),
|
|
181
|
+
)
|
|
182
|
+
}
|
|
183
|
+
for (let id = 1; id < 3; id++) {
|
|
184
|
+
if (id === 1 ? v : a) trex.push(full('trex', 0, u32(id), u32(1), zeros(12)))
|
|
185
|
+
}
|
|
186
|
+
return concat([
|
|
187
|
+
box('ftyp', ascii('isom'), u32(1), ascii('isomavc1')),
|
|
188
|
+
box(
|
|
189
|
+
'moov',
|
|
190
|
+
full(
|
|
191
|
+
'mvhd',
|
|
192
|
+
0,
|
|
193
|
+
zeros(8),
|
|
194
|
+
u32(1000),
|
|
195
|
+
u32(0),
|
|
196
|
+
u32(0x10000),
|
|
197
|
+
u16(0x100),
|
|
198
|
+
zeros(10),
|
|
199
|
+
MATRIX,
|
|
200
|
+
zeros(24),
|
|
201
|
+
u32(3),
|
|
202
|
+
),
|
|
203
|
+
...traks,
|
|
204
|
+
box('mvex', ...trex),
|
|
205
|
+
),
|
|
206
|
+
])
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function fragment(seq: number, trafs: Traf[]): Uint8Array {
|
|
210
|
+
const moof = (offset: number) =>
|
|
211
|
+
box(
|
|
212
|
+
'moof',
|
|
213
|
+
full('mfhd', 0, u32(seq)),
|
|
214
|
+
...trafs.map(t => {
|
|
215
|
+
const b = box(
|
|
216
|
+
'traf',
|
|
217
|
+
// default-base-is-moof
|
|
218
|
+
full('tfhd', 0x20000, u32(t.id)),
|
|
219
|
+
full(
|
|
220
|
+
'tfdt',
|
|
221
|
+
1 << 24,
|
|
222
|
+
u32(Math.floor(t.base / 2 ** 32)),
|
|
223
|
+
u32(t.base % 2 ** 32),
|
|
224
|
+
),
|
|
225
|
+
full(
|
|
226
|
+
'trun',
|
|
227
|
+
t.vf,
|
|
228
|
+
u32(t.rows.length),
|
|
229
|
+
u32(offset),
|
|
230
|
+
t.rows.flatMap(r => r.flatMap(u32)),
|
|
231
|
+
),
|
|
232
|
+
)
|
|
233
|
+
offset += t.size
|
|
234
|
+
return b
|
|
235
|
+
}),
|
|
236
|
+
)
|
|
237
|
+
const header = moof(0)
|
|
238
|
+
return concat([
|
|
239
|
+
moof(header.length + 8),
|
|
240
|
+
box('mdat', ...trafs.flatMap(t => t.data)),
|
|
241
|
+
])
|
|
242
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Shared contracts between the player core and the segment processors.
|
|
3
|
+
*
|
|
4
|
+
* Module layout (src/):
|
|
5
|
+
* index.ts public API: createHlsPlayer, canPlayHls, errors, bandwidth
|
|
6
|
+
* player.ts orchestration: owns the <video>, MediaSource and scheduler
|
|
7
|
+
* playlist.ts multivariant + media playlist parsing (VOD only)
|
|
8
|
+
* loader.ts fetch with timeouts, retries and network timing
|
|
9
|
+
* bandwidth.ts module-level EWMA bandwidth estimate shared by all players
|
|
10
|
+
* abr.ts level selection
|
|
11
|
+
* scheduler.ts per-segment buffered-level map, loop-aware look-ahead
|
|
12
|
+
* mse.ts MediaSource / ManagedMediaSource + SourceBuffer queue
|
|
13
|
+
* gaps.ts stall detection and gap skipping
|
|
14
|
+
* subtitles.ts <track> elements for WebVTT renditions
|
|
15
|
+
* errors.ts HlsError and subclasses
|
|
16
|
+
* fmp4.ts SegmentProcessor for fMP4 segments (passthrough), static
|
|
17
|
+
* ts/index.ts SegmentProcessor for MPEG-TS segments, loaded with import()
|
|
18
|
+
*
|
|
19
|
+
* Only `ts/` may be large. It must not import anything from the core except
|
|
20
|
+
* types, so the bundler can put it in its own chunk.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export type Level = {
|
|
24
|
+
index: number
|
|
25
|
+
/** Absolute URL of the media playlist, query string preserved. */
|
|
26
|
+
uri: string
|
|
27
|
+
/** Peak bits per second. */
|
|
28
|
+
bandwidth: number
|
|
29
|
+
averageBandwidth?: number
|
|
30
|
+
width?: number
|
|
31
|
+
height?: number
|
|
32
|
+
/** Raw CODECS attribute, e.g. `avc1.4d401f,mp4a.40.2`. */
|
|
33
|
+
codecs?: string
|
|
34
|
+
frameRate?: number
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type SubtitleRendition = {
|
|
38
|
+
name: string
|
|
39
|
+
language?: string
|
|
40
|
+
/** Absolute URL of the subtitle media playlist. */
|
|
41
|
+
uri: string
|
|
42
|
+
isDefault: boolean
|
|
43
|
+
forced: boolean
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type MultivariantPlaylist = {
|
|
47
|
+
levels: Level[]
|
|
48
|
+
subtitles: SubtitleRendition[]
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type MediaSegment = {
|
|
52
|
+
index: number
|
|
53
|
+
/** Absolute URL, query string preserved (lumi requires its `dur` param). */
|
|
54
|
+
uri: string
|
|
55
|
+
/** Seconds from the start of the playlist, the sum of previous EXTINFs. */
|
|
56
|
+
start: number
|
|
57
|
+
duration: number
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type SegmentFormat = 'ts' | 'fmp4'
|
|
61
|
+
|
|
62
|
+
export type MediaPlaylist = {
|
|
63
|
+
segments: MediaSegment[]
|
|
64
|
+
/** Sum of all EXTINF durations. */
|
|
65
|
+
duration: number
|
|
66
|
+
targetDuration: number
|
|
67
|
+
format: SegmentFormat
|
|
68
|
+
/** Absolute URL of the EXT-X-MAP init segment (fMP4 only). */
|
|
69
|
+
initUri?: string
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type SegmentInput = {
|
|
73
|
+
data: Uint8Array
|
|
74
|
+
level: number
|
|
75
|
+
segmentIndex: number
|
|
76
|
+
/** fMP4 only: the bytes of the level's EXT-X-MAP init segment. */
|
|
77
|
+
init?: Uint8Array
|
|
78
|
+
/** The level's CODECS attribute, if the playlist has one. */
|
|
79
|
+
codecs?: string
|
|
80
|
+
/*
|
|
81
|
+
* True unless this segment directly follows the previous one passed to
|
|
82
|
+
* process(), at the same level. Processors must not carry state (partial
|
|
83
|
+
* PES packets, audio continuity) across a discontinuous input.
|
|
84
|
+
*/
|
|
85
|
+
discontinuous: boolean
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export type ProcessedSegment = {
|
|
89
|
+
/*
|
|
90
|
+
* The init segment that `data` needs, returned for every segment. The
|
|
91
|
+
* player appends it when `initKey` differs from the last init it appended.
|
|
92
|
+
*/
|
|
93
|
+
init: Uint8Array
|
|
94
|
+
/** Stable for a given track configuration (codecs, dimensions, sample rate). */
|
|
95
|
+
initKey: string
|
|
96
|
+
/** One or more moof+mdat pairs; audio and video share one SourceBuffer. */
|
|
97
|
+
data: Uint8Array
|
|
98
|
+
/** e.g. `video/mp4; codecs="avc1.4d401f,mp4a.40.2"` */
|
|
99
|
+
mimeType: string
|
|
100
|
+
hasVideo: boolean
|
|
101
|
+
hasAudio: boolean
|
|
102
|
+
/*
|
|
103
|
+
* Seconds on the media's own timeline, before the player applies any
|
|
104
|
+
* timestampOffset: raw PTS / 90000 for TS, decode time / timescale plus
|
|
105
|
+
* composition offset for fMP4. `startTime` is the first video frame's
|
|
106
|
+
* presentation time when there is video, else the first audio frame's.
|
|
107
|
+
*/
|
|
108
|
+
startTime: number
|
|
109
|
+
endTime: number
|
|
110
|
+
/** Presentation times (same timeline) of each keyframe, ascending. */
|
|
111
|
+
keyframes: number[]
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export interface SegmentProcessor {
|
|
115
|
+
/** Throws an Error with a descriptive message on malformed input. */
|
|
116
|
+
process(input: SegmentInput): ProcessedSegment
|
|
117
|
+
reset(): void
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export type CreateSegmentProcessor = () => SegmentProcessor
|
|
121
|
+
|
|
122
|
+
export type PreloadMode = 'none' | 'metadata' | 'auto'
|
|
123
|
+
|
|
124
|
+
export type HlsPlayerOptions = {
|
|
125
|
+
/** Multivariant playlist URL. */
|
|
126
|
+
src: string
|
|
127
|
+
loop?: boolean
|
|
128
|
+
/** Seconds; where playback starts. */
|
|
129
|
+
startTime?: number
|
|
130
|
+
/*
|
|
131
|
+
* `none` fetches nothing, `metadata` fetches the playlists only, `auto`
|
|
132
|
+
* also buffers segments. Defaults to `auto`.
|
|
133
|
+
*/
|
|
134
|
+
preload?: PreloadMode
|
|
135
|
+
/** Seconds of media to keep buffered ahead of the playhead. Defaults to 10. */
|
|
136
|
+
bufferAhead?: number
|
|
137
|
+
/** Show the default subtitle rendition. Defaults to false. */
|
|
138
|
+
subtitles?: boolean
|
|
139
|
+
/** Injected for tests. */
|
|
140
|
+
fetch?: typeof fetch
|
|
141
|
+
/** Injected for tests; defaults to `() => import('./ts/index.js')`. */
|
|
142
|
+
loadTsProcessor?: () => Promise<CreateSegmentProcessor>
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export type SegmentLoadedInfo = {
|
|
146
|
+
level: number
|
|
147
|
+
index: number
|
|
148
|
+
bytes: number
|
|
149
|
+
/** Milliseconds from request start to last byte. */
|
|
150
|
+
loadMs: number
|
|
151
|
+
/** Milliseconds spent in SegmentProcessor.process. */
|
|
152
|
+
processMs: number
|
|
153
|
+
/** True when this replaced a lower-level copy already in the buffer. */
|
|
154
|
+
upgrade: boolean
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export type HlsPlayerEvents = {
|
|
158
|
+
/** Fatal: the player has stopped. */
|
|
159
|
+
error: (error: import('./errors.js').HlsError) => void
|
|
160
|
+
/** Non-fatal: something failed and is being retried. */
|
|
161
|
+
warning: (error: import('./errors.js').HlsError) => void
|
|
162
|
+
/** True while waiting on the network or the TS processor chunk before the first frame. */
|
|
163
|
+
loading: (loading: boolean) => void
|
|
164
|
+
levels: (levels: Level[]) => void
|
|
165
|
+
/*
|
|
166
|
+
* Whether the video has an audio track, for showing volume controls. Emitted
|
|
167
|
+
* from the playlist's CODECS before any segment loads, and again if the
|
|
168
|
+
* first processed segment disagrees (or when CODECS is missing).
|
|
169
|
+
*/
|
|
170
|
+
audio: (hasAudio: boolean) => void
|
|
171
|
+
subtitles: (renditions: SubtitleRendition[]) => void
|
|
172
|
+
/** The level of the segment under the playhead changed. */
|
|
173
|
+
levelchange: (level: number) => void
|
|
174
|
+
segment: (info: SegmentLoadedInfo) => void
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export type HlsPlayerStats = {
|
|
178
|
+
levels: Level[]
|
|
179
|
+
format: SegmentFormat | undefined
|
|
180
|
+
/** undefined until the playlist or first segment says. */
|
|
181
|
+
hasAudio: boolean | undefined
|
|
182
|
+
/** Level of the segment under the playhead, or -1. */
|
|
183
|
+
currentLevel: number
|
|
184
|
+
/** Level ABR is currently loading. */
|
|
185
|
+
targetLevel: number
|
|
186
|
+
/** Bits per second, shared across players. */
|
|
187
|
+
bandwidthEstimate: number | undefined
|
|
188
|
+
/** Seconds subtracted from media timestamps so playback starts at 0. */
|
|
189
|
+
timelineOffset: number | undefined
|
|
190
|
+
segments: Array<{
|
|
191
|
+
index: number
|
|
192
|
+
start: number
|
|
193
|
+
duration: number
|
|
194
|
+
/** Level this segment is buffered at, or null. */
|
|
195
|
+
bufferedLevel: number | null
|
|
196
|
+
}>
|
|
197
|
+
buffered: Array<[number, number]>
|
|
198
|
+
bytesLoaded: number
|
|
199
|
+
/** Whether the TS processor chunk has been loaded. */
|
|
200
|
+
tsProcessorLoaded: boolean
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export interface HlsPlayer {
|
|
204
|
+
readonly media: HTMLVideoElement
|
|
205
|
+
setPreload(mode: PreloadMode): void
|
|
206
|
+
setBufferAhead(seconds: number): void
|
|
207
|
+
setLoop(loop: boolean): void
|
|
208
|
+
setSubtitles(enabled: boolean): void
|
|
209
|
+
on<E extends keyof HlsPlayerEvents>(
|
|
210
|
+
event: E,
|
|
211
|
+
listener: HlsPlayerEvents[E],
|
|
212
|
+
): () => void
|
|
213
|
+
getStats(): HlsPlayerStats
|
|
214
|
+
/** Stops all loading, detaches from the video element and frees resources. */
|
|
215
|
+
destroy(): void
|
|
216
|
+
}
|