1 | import videojs from 'video.js';
|
2 | import window from 'global/window';
|
3 | import { Parser as M3u8Parser } from 'm3u8-parser';
|
4 | import { resolveUrl } from './resolve-url';
|
5 | import { getLastParts, isAudioOnly } from './playlist.js';
|
6 |
|
7 | const { log } = videojs;
|
8 |
|
9 | export const createPlaylistID = (index, uri) => {
|
10 | return `${index}-${uri}`;
|
11 | };
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export const parseManifest = ({
|
32 | onwarn,
|
33 | oninfo,
|
34 | manifestString,
|
35 | customTagParsers = [],
|
36 | customTagMappers = [],
|
37 | experimentalLLHLS
|
38 | }) => {
|
39 | const parser = new M3u8Parser();
|
40 |
|
41 | if (onwarn) {
|
42 | parser.on('warn', onwarn);
|
43 | }
|
44 | if (oninfo) {
|
45 | parser.on('info', oninfo);
|
46 | }
|
47 |
|
48 | customTagParsers.forEach(customParser => parser.addParser(customParser));
|
49 | customTagMappers.forEach(mapper => parser.addTagMapper(mapper));
|
50 |
|
51 | parser.push(manifestString);
|
52 | parser.end();
|
53 |
|
54 | const manifest = parser.manifest;
|
55 |
|
56 |
|
57 |
|
58 | if (!experimentalLLHLS) {
|
59 | [
|
60 | 'preloadSegment',
|
61 | 'skip',
|
62 | 'serverControl',
|
63 | 'renditionReports',
|
64 | 'partInf',
|
65 | 'partTargetDuration'
|
66 | ].forEach(function(k) {
|
67 | if (manifest.hasOwnProperty(k)) {
|
68 | delete manifest[k];
|
69 | }
|
70 | });
|
71 |
|
72 | if (manifest.segments) {
|
73 | manifest.segments.forEach(function(segment) {
|
74 | ['parts', 'preloadHints'].forEach(function(k) {
|
75 | if (segment.hasOwnProperty(k)) {
|
76 | delete segment[k];
|
77 | }
|
78 | });
|
79 | });
|
80 | }
|
81 | }
|
82 | if (!manifest.targetDuration) {
|
83 | let targetDuration = 10;
|
84 |
|
85 | if (manifest.segments && manifest.segments.length) {
|
86 | targetDuration = manifest
|
87 | .segments.reduce((acc, s) => Math.max(acc, s.duration), 0);
|
88 | }
|
89 |
|
90 | if (onwarn) {
|
91 | onwarn(`manifest has no targetDuration defaulting to ${targetDuration}`);
|
92 | }
|
93 | manifest.targetDuration = targetDuration;
|
94 | }
|
95 |
|
96 | const parts = getLastParts(manifest);
|
97 |
|
98 | if (parts.length && !manifest.partTargetDuration) {
|
99 | const partTargetDuration = parts.reduce((acc, p) => Math.max(acc, p.duration), 0);
|
100 |
|
101 | if (onwarn) {
|
102 | onwarn(`manifest has no partTargetDuration defaulting to ${partTargetDuration}`);
|
103 | log.error('LL-HLS manifest has parts but lacks required #EXT-X-PART-INF:PART-TARGET value. See https://datatracker.ietf.org/doc/html/draft-pantos-hls-rfc8216bis-09#section-4.4.3.7. Playback is not guaranteed.');
|
104 | }
|
105 | manifest.partTargetDuration = partTargetDuration;
|
106 | }
|
107 |
|
108 | return manifest;
|
109 | };
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | export const forEachMediaGroup = (master, callback) => {
|
121 | if (!master.mediaGroups) {
|
122 | return;
|
123 | }
|
124 | ['AUDIO', 'SUBTITLES'].forEach((mediaType) => {
|
125 | if (!master.mediaGroups[mediaType]) {
|
126 | return;
|
127 | }
|
128 | for (const groupKey in master.mediaGroups[mediaType]) {
|
129 | for (const labelKey in master.mediaGroups[mediaType][groupKey]) {
|
130 | const mediaProperties = master.mediaGroups[mediaType][groupKey][labelKey];
|
131 |
|
132 | callback(mediaProperties, mediaType, groupKey, labelKey);
|
133 | }
|
134 | }
|
135 | });
|
136 | };
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | export const setupMediaPlaylist = ({ playlist, uri, id }) => {
|
153 | playlist.id = id;
|
154 | playlist.playlistErrors_ = 0;
|
155 |
|
156 | if (uri) {
|
157 |
|
158 |
|
159 |
|
160 | playlist.uri = uri;
|
161 | }
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | playlist.attributes = playlist.attributes || {};
|
171 | };
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | export const setupMediaPlaylists = (master) => {
|
182 | let i = master.playlists.length;
|
183 |
|
184 | while (i--) {
|
185 | const playlist = master.playlists[i];
|
186 |
|
187 | setupMediaPlaylist({
|
188 | playlist,
|
189 | id: createPlaylistID(i, playlist.uri)
|
190 | });
|
191 | playlist.resolvedUri = resolveUrl(master.uri, playlist.uri);
|
192 | master.playlists[playlist.id] = playlist;
|
193 |
|
194 | master.playlists[playlist.uri] = playlist;
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | if (!playlist.attributes.BANDWIDTH) {
|
201 | log.warn('Invalid playlist STREAM-INF detected. Missing BANDWIDTH attribute.');
|
202 | }
|
203 | }
|
204 | };
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | export const resolveMediaGroupUris = (master) => {
|
213 | forEachMediaGroup(master, (properties) => {
|
214 | if (properties.uri) {
|
215 | properties.resolvedUri = resolveUrl(master.uri, properties.uri);
|
216 | }
|
217 | });
|
218 | };
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | export const masterForMedia = (media, uri) => {
|
232 | const id = createPlaylistID(0, uri);
|
233 | const master = {
|
234 | mediaGroups: {
|
235 | 'AUDIO': {},
|
236 | 'VIDEO': {},
|
237 | 'CLOSED-CAPTIONS': {},
|
238 | 'SUBTITLES': {}
|
239 | },
|
240 | uri: window.location.href,
|
241 | resolvedUri: window.location.href,
|
242 | playlists: [{
|
243 | uri,
|
244 | id,
|
245 | resolvedUri: uri,
|
246 |
|
247 |
|
248 | attributes: {}
|
249 | }]
|
250 | };
|
251 |
|
252 |
|
253 | master.playlists[id] = master.playlists[0];
|
254 |
|
255 | master.playlists[uri] = master.playlists[0];
|
256 |
|
257 | return master;
|
258 | };
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | export const addPropertiesToMaster = (master, uri) => {
|
270 | master.uri = uri;
|
271 |
|
272 | for (let i = 0; i < master.playlists.length; i++) {
|
273 | if (!master.playlists[i].uri) {
|
274 |
|
275 |
|
276 |
|
277 | const phonyUri = `placeholder-uri-${i}`;
|
278 |
|
279 | master.playlists[i].uri = phonyUri;
|
280 | }
|
281 | }
|
282 | const audioOnlyMaster = isAudioOnly(master);
|
283 |
|
284 | forEachMediaGroup(master, (properties, mediaType, groupKey, labelKey) => {
|
285 | const groupId = `placeholder-uri-${mediaType}-${groupKey}-${labelKey}`;
|
286 |
|
287 |
|
288 | if (!properties.playlists || !properties.playlists.length) {
|
289 |
|
290 |
|
291 |
|
292 | if (audioOnlyMaster && mediaType === 'AUDIO' && !properties.uri) {
|
293 | for (let i = 0; i < master.playlists.length; i++) {
|
294 | const p = master.playlists[i];
|
295 |
|
296 | if (p.attributes && p.attributes.AUDIO && p.attributes.AUDIO === groupKey) {
|
297 | return;
|
298 | }
|
299 | }
|
300 | }
|
301 |
|
302 | properties.playlists = [Object.assign({}, properties)];
|
303 | }
|
304 |
|
305 | properties.playlists.forEach(function(p, i) {
|
306 | const id = createPlaylistID(i, groupId);
|
307 |
|
308 | if (p.uri) {
|
309 | p.resolvedUri = p.resolvedUri || resolveUrl(master.uri, p.uri);
|
310 | } else {
|
311 |
|
312 |
|
313 |
|
314 |
|
315 | p.uri = i === 0 ? groupId : id;
|
316 |
|
317 |
|
318 |
|
319 | p.resolvedUri = p.uri;
|
320 | }
|
321 |
|
322 | p.id = p.id || id;
|
323 |
|
324 |
|
325 |
|
326 | p.attributes = p.attributes || {};
|
327 |
|
328 |
|
329 | master.playlists[p.id] = p;
|
330 | master.playlists[p.uri] = p;
|
331 | });
|
332 |
|
333 | });
|
334 |
|
335 | setupMediaPlaylists(master);
|
336 | resolveMediaGroupUris(master);
|
337 | };
|