-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
203 lines (179 loc) · 5.08 KB
/
index.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
export interface IQualityLevel {
bitrate: number;
width: number;
height: number;
fps: number;
audioCodec: string;
videoCodec: string;
}
export interface IHLSPlaylist extends IQualityLevel {
url: string;
}
const HLS_ATTRIBUTE_REGEXP = new RegExp(
'(?:,?)([A-Z0-9-]+?)[=](".*?"|[^",]+)(?=,|s*$)',
"g"
);
const guessCodec = (
codecs: string[]
): { audio?: string; video: string } | undefined => {
if (codecs.length === 1) {
return {
audio: undefined,
video: codecs[0],
};
}
if (codecs.length >= 2) {
// Guess based on some of the most common codecs. There is no
// way to be 100% certain which codec belongs to which track
// in the HLS standard.
const firstIsProbablyVideo = codecs[0].includes("avc");
const secondIsProbablyVideo = codecs[1].includes("avc");
const firstIsProbablyAudio = codecs[0].includes("mp4a");
const secondIsProbablyAudio = codecs[1].includes("mp4a");
if (firstIsProbablyVideo) {
return {
video: codecs[0],
audio: codecs[1],
};
}
if (secondIsProbablyVideo) {
return {
video: codecs[1],
audio: codecs[0],
};
}
if (firstIsProbablyAudio) {
return {
video: codecs[1],
audio: codecs[0],
};
}
if (secondIsProbablyAudio) {
return {
video: codecs[0],
audio: codecs[1],
};
}
}
};
const BITRATE_POLL_INTERVAL = 5 * 1000;
export class SafariBitrateMonitor {
private bitratePollInterval: number = BITRATE_POLL_INTERVAL;
private videoElement: HTMLVideoElement;
private handler: (qualityLevel: IQualityLevel) => void;
private currentBitrate: number;
private playlists: IHLSPlaylist[] = [];
private bitrateInterval: number;
constructor({
videoElement,
hlsManifestUrl,
handler,
bitratePollInterval,
}: {
videoElement: HTMLVideoElement;
hlsManifestUrl: string;
handler: (qualityLevel: IQualityLevel) => void;
bitratePollInterval?: number;
}) {
if (!videoElement || !hlsManifestUrl) {
console.error(
"[SafariBitrateMonitor] Missing video element or manifest url"
);
return;
}
this.videoElement = videoElement;
this.handler = handler;
if (bitratePollInterval) {
this.bitratePollInterval = bitratePollInterval;
}
this.init(hlsManifestUrl);
}
private async init(hlsManifestUrl: string): Promise<void> {
this.playlists = await this.getPlaylists(hlsManifestUrl);
if (this.playlists.length) {
this.startBitratePoll();
}
}
private async getPlaylists(src: string): Promise<IHLSPlaylist[]> {
const response = await fetch(src);
if (!response.ok) {
return [];
}
const manifestString = await response.text();
const manifestLines = manifestString.split("\n");
const playlists: IHLSPlaylist[] = [];
manifestLines.forEach((line, index) => {
const playlist: IHLSPlaylist = {
url: null,
width: 0,
height: 0,
bitrate: 0,
fps: 0,
audioCodec: "",
videoCodec: "",
};
if (line.includes("#EXT-X-STREAM-INF")) {
let valid = false;
playlist.url = manifestLines[index + 1];
let group: string[];
while ((group = HLS_ATTRIBUTE_REGEXP.exec(line)) !== null) {
const [, attribute, value] = group;
switch (attribute) {
case "BANDWIDTH":
valid = true;
playlist.bitrate = Number(value);
break;
case "RESOLUTION": {
const [width, height] = value.split("x");
playlist.width = Number(width);
playlist.height = Number(height);
break;
}
case "CODECS": {
const codecs = value.replace(/"/g, "").split(",");
const avCodecs = guessCodec(codecs);
playlist.videoCodec = avCodecs.video;
playlist.audioCodec = avCodecs.audio;
break;
}
case "FRAME-RATE":
if (value) {
playlist.fps = Number(value);
}
break;
default:
break;
}
}
if (valid) {
playlists.push(playlist);
}
}
});
return playlists;
}
private startBitratePoll() {
this.bitrateInterval = window.setInterval(() => {
const width = this.videoElement.videoWidth;
const height = this.videoElement.videoHeight;
const playlist = this.playlists.find(
(playlist) => playlist.width === width && playlist.height === height
);
if (playlist && playlist.bitrate !== this.currentBitrate) {
this.currentBitrate = playlist.bitrate;
this.handler({
bitrate: playlist.bitrate,
width: playlist.width,
height: playlist.height,
videoCodec: playlist.videoCodec,
audioCodec: playlist.audioCodec,
fps: playlist.fps,
});
}
}, this.bitratePollInterval);
}
public destroy() {
clearInterval(this.bitrateInterval);
this.playlists = [];
}
}