1 | import videojs from 'video.js';
|
2 | import { createTransferableMessage } from './bin-utils';
|
3 | import { stringToArrayBuffer } from './util/string-to-array-buffer';
|
4 | import { transmux } from './segment-transmuxer';
|
5 | import { segmentXhrHeaders } from './xhr';
|
6 | import {workerCallback} from './util/worker-callback.js';
|
7 | import {
|
8 | detectContainerForBytes,
|
9 | isLikelyFmp4MediaSegment
|
10 | } from '@videojs/vhs-utils/es/containers';
|
11 |
|
12 | export const REQUEST_ERRORS = {
|
13 | FAILURE: 2,
|
14 | TIMEOUT: -101,
|
15 | ABORTED: -102
|
16 | };
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | const abortAll = (activeXhrs) => {
|
24 | activeXhrs.forEach((xhr) => {
|
25 | xhr.abort();
|
26 | });
|
27 | };
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | const getRequestStats = (request) => {
|
35 | return {
|
36 | bandwidth: request.bandwidth,
|
37 | bytesReceived: request.bytesReceived || 0,
|
38 | roundTripTime: request.roundTripTime || 0
|
39 | };
|
40 | };
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | const getProgressStats = (progressEvent) => {
|
49 | const request = progressEvent.target;
|
50 | const roundTripTime = Date.now() - request.requestTime;
|
51 | const stats = {
|
52 | bandwidth: Infinity,
|
53 | bytesReceived: 0,
|
54 | roundTripTime: roundTripTime || 0
|
55 | };
|
56 |
|
57 | stats.bytesReceived = progressEvent.loaded;
|
58 |
|
59 |
|
60 |
|
61 | stats.bandwidth = Math.floor((stats.bytesReceived / stats.roundTripTime) * 8 * 1000);
|
62 |
|
63 | return stats;
|
64 | };
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | const handleErrors = (error, request) => {
|
74 | if (request.timedout) {
|
75 | return {
|
76 | status: request.status,
|
77 | message: 'HLS request timed-out at URL: ' + request.uri,
|
78 | code: REQUEST_ERRORS.TIMEOUT,
|
79 | xhr: request
|
80 | };
|
81 | }
|
82 |
|
83 | if (request.aborted) {
|
84 | return {
|
85 | status: request.status,
|
86 | message: 'HLS request aborted at URL: ' + request.uri,
|
87 | code: REQUEST_ERRORS.ABORTED,
|
88 | xhr: request
|
89 | };
|
90 | }
|
91 |
|
92 | if (error) {
|
93 | return {
|
94 | status: request.status,
|
95 | message: 'HLS request errored at URL: ' + request.uri,
|
96 | code: REQUEST_ERRORS.FAILURE,
|
97 | xhr: request
|
98 | };
|
99 | }
|
100 |
|
101 | if (request.responseType === 'arraybuffer' && request.response.byteLength === 0) {
|
102 | return {
|
103 | status: request.status,
|
104 | message: 'Empty HLS response at URL: ' + request.uri,
|
105 | code: REQUEST_ERRORS.FAILURE,
|
106 | xhr: request
|
107 | };
|
108 | }
|
109 |
|
110 | return null;
|
111 | };
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | const handleKeyResponse = (segment, objects, finishProcessingFn) => (error, request) => {
|
124 | const response = request.response;
|
125 | const errorObj = handleErrors(error, request);
|
126 |
|
127 | if (errorObj) {
|
128 | return finishProcessingFn(errorObj, segment);
|
129 | }
|
130 |
|
131 | if (response.byteLength !== 16) {
|
132 | return finishProcessingFn({
|
133 | status: request.status,
|
134 | message: 'Invalid HLS key at URL: ' + request.uri,
|
135 | code: REQUEST_ERRORS.FAILURE,
|
136 | xhr: request
|
137 | }, segment);
|
138 | }
|
139 |
|
140 | const view = new DataView(response);
|
141 | const bytes = new Uint32Array([
|
142 | view.getUint32(0),
|
143 | view.getUint32(4),
|
144 | view.getUint32(8),
|
145 | view.getUint32(12)
|
146 | ]);
|
147 |
|
148 | for (let i = 0; i < objects.length; i++) {
|
149 | objects[i].bytes = bytes;
|
150 | }
|
151 |
|
152 | return finishProcessingFn(null, segment);
|
153 | };
|
154 |
|
155 | const parseInitSegment = (segment, callback) => {
|
156 | const type = detectContainerForBytes(segment.map.bytes);
|
157 |
|
158 |
|
159 |
|
160 | if (type !== 'mp4') {
|
161 | const uri = segment.map.resolvedUri || segment.map.uri;
|
162 |
|
163 | return callback({
|
164 | internal: true,
|
165 | message: `Found unsupported ${type || 'unknown'} container for initialization segment at URL: ${uri}`,
|
166 | code: REQUEST_ERRORS.FAILURE
|
167 | });
|
168 | }
|
169 |
|
170 | workerCallback({
|
171 | action: 'probeMp4Tracks',
|
172 | data: segment.map.bytes,
|
173 | transmuxer: segment.transmuxer,
|
174 | callback: ({tracks, data}) => {
|
175 |
|
176 | segment.map.bytes = data;
|
177 |
|
178 | tracks.forEach(function(track) {
|
179 | segment.map.tracks = segment.map.tracks || {};
|
180 |
|
181 |
|
182 | if (segment.map.tracks[track.type]) {
|
183 | return;
|
184 | }
|
185 |
|
186 | segment.map.tracks[track.type] = track;
|
187 |
|
188 | if (typeof track.id === 'number' && track.timescale) {
|
189 | segment.map.timescales = segment.map.timescales || {};
|
190 | segment.map.timescales[track.id] = track.timescale;
|
191 | }
|
192 |
|
193 | });
|
194 |
|
195 | return callback(null);
|
196 | }
|
197 | });
|
198 | };
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | const handleInitSegmentResponse =
|
209 | ({segment, finishProcessingFn}) => (error, request) => {
|
210 | const errorObj = handleErrors(error, request);
|
211 |
|
212 | if (errorObj) {
|
213 | return finishProcessingFn(errorObj, segment);
|
214 | }
|
215 | const bytes = new Uint8Array(request.response);
|
216 |
|
217 |
|
218 |
|
219 | if (segment.map.key) {
|
220 | segment.map.encryptedBytes = bytes;
|
221 | return finishProcessingFn(null, segment);
|
222 | }
|
223 |
|
224 | segment.map.bytes = bytes;
|
225 |
|
226 | parseInitSegment(segment, function(parseError) {
|
227 | if (parseError) {
|
228 | parseError.xhr = request;
|
229 | parseError.status = request.status;
|
230 |
|
231 | return finishProcessingFn(parseError, segment);
|
232 | }
|
233 |
|
234 | finishProcessingFn(null, segment);
|
235 | });
|
236 | };
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | const handleSegmentResponse = ({
|
249 | segment,
|
250 | finishProcessingFn,
|
251 | responseType
|
252 | }) => (error, request) => {
|
253 | const errorObj = handleErrors(error, request);
|
254 |
|
255 | if (errorObj) {
|
256 | return finishProcessingFn(errorObj, segment);
|
257 | }
|
258 |
|
259 | const newBytes =
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | (responseType === 'arraybuffer' || !request.responseText) ?
|
266 | request.response :
|
267 | stringToArrayBuffer(request.responseText.substring(segment.lastReachedChar || 0));
|
268 |
|
269 | segment.stats = getRequestStats(request);
|
270 |
|
271 | if (segment.key) {
|
272 | segment.encryptedBytes = new Uint8Array(newBytes);
|
273 | } else {
|
274 | segment.bytes = new Uint8Array(newBytes);
|
275 | }
|
276 |
|
277 | return finishProcessingFn(null, segment);
|
278 | };
|
279 |
|
280 | const transmuxAndNotify = ({
|
281 | segment,
|
282 | bytes,
|
283 | trackInfoFn,
|
284 | timingInfoFn,
|
285 | videoSegmentTimingInfoFn,
|
286 | audioSegmentTimingInfoFn,
|
287 | id3Fn,
|
288 | captionsFn,
|
289 | isEndOfTimeline,
|
290 | endedTimelineFn,
|
291 | dataFn,
|
292 | doneFn,
|
293 | onTransmuxerLog
|
294 | }) => {
|
295 | const fmp4Tracks = segment.map && segment.map.tracks || {};
|
296 | const isMuxed = Boolean(fmp4Tracks.audio && fmp4Tracks.video);
|
297 |
|
298 |
|
299 |
|
300 |
|
301 | let audioStartFn = timingInfoFn.bind(null, segment, 'audio', 'start');
|
302 | const audioEndFn = timingInfoFn.bind(null, segment, 'audio', 'end');
|
303 | let videoStartFn = timingInfoFn.bind(null, segment, 'video', 'start');
|
304 | const videoEndFn = timingInfoFn.bind(null, segment, 'video', 'end');
|
305 |
|
306 | const finish = () => transmux({
|
307 | bytes,
|
308 | transmuxer: segment.transmuxer,
|
309 | audioAppendStart: segment.audioAppendStart,
|
310 | gopsToAlignWith: segment.gopsToAlignWith,
|
311 | remux: isMuxed,
|
312 | onData: (result) => {
|
313 | result.type = result.type === 'combined' ? 'video' : result.type;
|
314 | dataFn(segment, result);
|
315 | },
|
316 | onTrackInfo: (trackInfo) => {
|
317 | if (trackInfoFn) {
|
318 | if (isMuxed) {
|
319 | trackInfo.isMuxed = true;
|
320 | }
|
321 | trackInfoFn(segment, trackInfo);
|
322 | }
|
323 | },
|
324 | onAudioTimingInfo: (audioTimingInfo) => {
|
325 |
|
326 | if (audioStartFn && typeof audioTimingInfo.start !== 'undefined') {
|
327 | audioStartFn(audioTimingInfo.start);
|
328 | audioStartFn = null;
|
329 | }
|
330 |
|
331 | if (audioEndFn && typeof audioTimingInfo.end !== 'undefined') {
|
332 | audioEndFn(audioTimingInfo.end);
|
333 | }
|
334 | },
|
335 | onVideoTimingInfo: (videoTimingInfo) => {
|
336 |
|
337 | if (videoStartFn && typeof videoTimingInfo.start !== 'undefined') {
|
338 | videoStartFn(videoTimingInfo.start);
|
339 | videoStartFn = null;
|
340 | }
|
341 |
|
342 | if (videoEndFn && typeof videoTimingInfo.end !== 'undefined') {
|
343 | videoEndFn(videoTimingInfo.end);
|
344 | }
|
345 | },
|
346 | onVideoSegmentTimingInfo: (videoSegmentTimingInfo) => {
|
347 | videoSegmentTimingInfoFn(videoSegmentTimingInfo);
|
348 | },
|
349 | onAudioSegmentTimingInfo: (audioSegmentTimingInfo) => {
|
350 | audioSegmentTimingInfoFn(audioSegmentTimingInfo);
|
351 | },
|
352 | onId3: (id3Frames, dispatchType) => {
|
353 | id3Fn(segment, id3Frames, dispatchType);
|
354 | },
|
355 | onCaptions: (captions) => {
|
356 | captionsFn(segment, [captions]);
|
357 | },
|
358 | isEndOfTimeline,
|
359 | onEndedTimeline: () => {
|
360 | endedTimelineFn();
|
361 | },
|
362 | onTransmuxerLog,
|
363 | onDone: (result) => {
|
364 | if (!doneFn) {
|
365 | return;
|
366 | }
|
367 | result.type = result.type === 'combined' ? 'video' : result.type;
|
368 | doneFn(null, segment, result);
|
369 | }
|
370 | });
|
371 |
|
372 |
|
373 |
|
374 |
|
375 | workerCallback({
|
376 | action: 'probeTs',
|
377 | transmuxer: segment.transmuxer,
|
378 | data: bytes,
|
379 | baseStartTime: segment.baseStartTime,
|
380 | callback: (data) => {
|
381 | segment.bytes = bytes = data.data;
|
382 |
|
383 | const probeResult = data.result;
|
384 |
|
385 | if (probeResult) {
|
386 | trackInfoFn(segment, {
|
387 | hasAudio: probeResult.hasAudio,
|
388 | hasVideo: probeResult.hasVideo,
|
389 | isMuxed
|
390 | });
|
391 | trackInfoFn = null;
|
392 |
|
393 | if (probeResult.hasAudio && !isMuxed) {
|
394 | audioStartFn(probeResult.audioStart);
|
395 | }
|
396 | if (probeResult.hasVideo) {
|
397 | videoStartFn(probeResult.videoStart);
|
398 | }
|
399 | audioStartFn = null;
|
400 | videoStartFn = null;
|
401 | }
|
402 |
|
403 | finish();
|
404 | }
|
405 | });
|
406 | };
|
407 |
|
408 | const handleSegmentBytes = ({
|
409 | segment,
|
410 | bytes,
|
411 | trackInfoFn,
|
412 | timingInfoFn,
|
413 | videoSegmentTimingInfoFn,
|
414 | audioSegmentTimingInfoFn,
|
415 | id3Fn,
|
416 | captionsFn,
|
417 | isEndOfTimeline,
|
418 | endedTimelineFn,
|
419 | dataFn,
|
420 | doneFn,
|
421 | onTransmuxerLog
|
422 | }) => {
|
423 | let bytesAsUint8Array = new Uint8Array(bytes);
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 | if (isLikelyFmp4MediaSegment(bytesAsUint8Array)) {
|
431 | segment.isFmp4 = true;
|
432 | const {tracks} = segment.map;
|
433 |
|
434 | const trackInfo = {
|
435 | isFmp4: true,
|
436 | hasVideo: !!tracks.video,
|
437 | hasAudio: !!tracks.audio
|
438 | };
|
439 |
|
440 |
|
441 |
|
442 | if (tracks.audio && tracks.audio.codec && tracks.audio.codec !== 'enca') {
|
443 | trackInfo.audioCodec = tracks.audio.codec;
|
444 | }
|
445 |
|
446 |
|
447 |
|
448 | if (tracks.video && tracks.video.codec && tracks.video.codec !== 'encv') {
|
449 | trackInfo.videoCodec = tracks.video.codec;
|
450 | }
|
451 |
|
452 | if (tracks.video && tracks.audio) {
|
453 | trackInfo.isMuxed = true;
|
454 | }
|
455 |
|
456 |
|
457 |
|
458 | trackInfoFn(segment, trackInfo);
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 | const finishLoading = (captions) => {
|
466 |
|
467 |
|
468 |
|
469 |
|
470 | dataFn(segment, {
|
471 | data: bytesAsUint8Array,
|
472 | type: trackInfo.hasAudio && !trackInfo.isMuxed ? 'audio' : 'video'
|
473 | });
|
474 | if (captions && captions.length) {
|
475 | captionsFn(segment, captions);
|
476 | }
|
477 | doneFn(null, segment, {});
|
478 | };
|
479 |
|
480 | workerCallback({
|
481 | action: 'probeMp4StartTime',
|
482 | timescales: segment.map.timescales,
|
483 | data: bytesAsUint8Array,
|
484 | transmuxer: segment.transmuxer,
|
485 | callback: ({data, startTime}) => {
|
486 |
|
487 | bytes = data.buffer;
|
488 | segment.bytes = bytesAsUint8Array = data;
|
489 |
|
490 | if (trackInfo.hasAudio && !trackInfo.isMuxed) {
|
491 | timingInfoFn(segment, 'audio', 'start', startTime);
|
492 | }
|
493 |
|
494 | if (trackInfo.hasVideo) {
|
495 | timingInfoFn(segment, 'video', 'start', startTime);
|
496 | }
|
497 |
|
498 |
|
499 |
|
500 | if (!tracks.video || !data.byteLength || !segment.transmuxer) {
|
501 | finishLoading();
|
502 | return;
|
503 | }
|
504 |
|
505 | workerCallback({
|
506 | action: 'pushMp4Captions',
|
507 | endAction: 'mp4Captions',
|
508 | transmuxer: segment.transmuxer,
|
509 | data: bytesAsUint8Array,
|
510 | timescales: segment.map.timescales,
|
511 | trackIds: [tracks.video.id],
|
512 | callback: (message) => {
|
513 |
|
514 | bytes = message.data.buffer;
|
515 | segment.bytes = bytesAsUint8Array = message.data;
|
516 | message.logs.forEach(function(log) {
|
517 | onTransmuxerLog(videojs.mergeOptions(log, {stream: 'mp4CaptionParser'}));
|
518 | });
|
519 | finishLoading(message.captions);
|
520 | }
|
521 | });
|
522 | }
|
523 | });
|
524 | return;
|
525 | }
|
526 |
|
527 |
|
528 | if (!segment.transmuxer) {
|
529 | doneFn(null, segment, {});
|
530 | return;
|
531 | }
|
532 |
|
533 | if (typeof segment.container === 'undefined') {
|
534 | segment.container = detectContainerForBytes(bytesAsUint8Array);
|
535 | }
|
536 |
|
537 | if (segment.container !== 'ts' && segment.container !== 'aac') {
|
538 | trackInfoFn(segment, {hasAudio: false, hasVideo: false});
|
539 | doneFn(null, segment, {});
|
540 | return;
|
541 | }
|
542 |
|
543 |
|
544 | transmuxAndNotify({
|
545 | segment,
|
546 | bytes,
|
547 | trackInfoFn,
|
548 | timingInfoFn,
|
549 | videoSegmentTimingInfoFn,
|
550 | audioSegmentTimingInfoFn,
|
551 | id3Fn,
|
552 | captionsFn,
|
553 | isEndOfTimeline,
|
554 | endedTimelineFn,
|
555 | dataFn,
|
556 | doneFn,
|
557 | onTransmuxerLog
|
558 | });
|
559 | };
|
560 |
|
561 | const decrypt = function({id, key, encryptedBytes, decryptionWorker}, callback) {
|
562 | const decryptionHandler = (event) => {
|
563 | if (event.data.source === id) {
|
564 | decryptionWorker.removeEventListener('message', decryptionHandler);
|
565 | const decrypted = event.data.decrypted;
|
566 |
|
567 | callback(new Uint8Array(
|
568 | decrypted.bytes,
|
569 | decrypted.byteOffset,
|
570 | decrypted.byteLength
|
571 | ));
|
572 | }
|
573 | };
|
574 |
|
575 | decryptionWorker.addEventListener('message', decryptionHandler);
|
576 |
|
577 | let keyBytes;
|
578 |
|
579 | if (key.bytes.slice) {
|
580 | keyBytes = key.bytes.slice();
|
581 | } else {
|
582 | keyBytes = new Uint32Array(Array.prototype.slice.call(key.bytes));
|
583 | }
|
584 |
|
585 |
|
586 | decryptionWorker.postMessage(createTransferableMessage({
|
587 | source: id,
|
588 | encrypted: encryptedBytes,
|
589 | key: keyBytes,
|
590 | iv: key.iv
|
591 | }), [
|
592 | encryptedBytes.buffer,
|
593 | keyBytes.buffer
|
594 | ]);
|
595 | };
|
596 |
|
597 |
|
598 |
|
599 |
|
600 |
|
601 |
|
602 |
|
603 |
|
604 |
|
605 |
|
606 |
|
607 |
|
608 |
|
609 |
|
610 |
|
611 |
|
612 |
|
613 |
|
614 |
|
615 |
|
616 |
|
617 |
|
618 |
|
619 |
|
620 |
|
621 | const decryptSegment = ({
|
622 | decryptionWorker,
|
623 | segment,
|
624 | trackInfoFn,
|
625 | timingInfoFn,
|
626 | videoSegmentTimingInfoFn,
|
627 | audioSegmentTimingInfoFn,
|
628 | id3Fn,
|
629 | captionsFn,
|
630 | isEndOfTimeline,
|
631 | endedTimelineFn,
|
632 | dataFn,
|
633 | doneFn,
|
634 | onTransmuxerLog
|
635 | }) => {
|
636 | decrypt({
|
637 | id: segment.requestId,
|
638 | key: segment.key,
|
639 | encryptedBytes: segment.encryptedBytes,
|
640 | decryptionWorker
|
641 | }, (decryptedBytes) => {
|
642 | segment.bytes = decryptedBytes;
|
643 |
|
644 | handleSegmentBytes({
|
645 | segment,
|
646 | bytes: segment.bytes,
|
647 | trackInfoFn,
|
648 | timingInfoFn,
|
649 | videoSegmentTimingInfoFn,
|
650 | audioSegmentTimingInfoFn,
|
651 | id3Fn,
|
652 | captionsFn,
|
653 | isEndOfTimeline,
|
654 | endedTimelineFn,
|
655 | dataFn,
|
656 | doneFn,
|
657 | onTransmuxerLog
|
658 | });
|
659 | });
|
660 | };
|
661 |
|
662 |
|
663 |
|
664 |
|
665 |
|
666 |
|
667 |
|
668 |
|
669 |
|
670 |
|
671 |
|
672 |
|
673 |
|
674 |
|
675 |
|
676 |
|
677 |
|
678 |
|
679 |
|
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 |
|
689 |
|
690 |
|
691 | const waitForCompletion = ({
|
692 | activeXhrs,
|
693 | decryptionWorker,
|
694 | trackInfoFn,
|
695 | timingInfoFn,
|
696 | videoSegmentTimingInfoFn,
|
697 | audioSegmentTimingInfoFn,
|
698 | id3Fn,
|
699 | captionsFn,
|
700 | isEndOfTimeline,
|
701 | endedTimelineFn,
|
702 | dataFn,
|
703 | doneFn,
|
704 | onTransmuxerLog
|
705 | }) => {
|
706 | let count = 0;
|
707 | let didError = false;
|
708 |
|
709 | return (error, segment) => {
|
710 | if (didError) {
|
711 | return;
|
712 | }
|
713 |
|
714 | if (error) {
|
715 | didError = true;
|
716 |
|
717 | abortAll(activeXhrs);
|
718 |
|
719 |
|
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 |
|
730 | return doneFn(error, segment);
|
731 | }
|
732 |
|
733 | count += 1;
|
734 |
|
735 | if (count === activeXhrs.length) {
|
736 | const segmentFinish = function() {
|
737 | if (segment.encryptedBytes) {
|
738 | return decryptSegment({
|
739 | decryptionWorker,
|
740 | segment,
|
741 | trackInfoFn,
|
742 | timingInfoFn,
|
743 | videoSegmentTimingInfoFn,
|
744 | audioSegmentTimingInfoFn,
|
745 | id3Fn,
|
746 | captionsFn,
|
747 | isEndOfTimeline,
|
748 | endedTimelineFn,
|
749 | dataFn,
|
750 | doneFn,
|
751 | onTransmuxerLog
|
752 | });
|
753 | }
|
754 |
|
755 | handleSegmentBytes({
|
756 | segment,
|
757 | bytes: segment.bytes,
|
758 | trackInfoFn,
|
759 | timingInfoFn,
|
760 | videoSegmentTimingInfoFn,
|
761 | audioSegmentTimingInfoFn,
|
762 | id3Fn,
|
763 | captionsFn,
|
764 | isEndOfTimeline,
|
765 | endedTimelineFn,
|
766 | dataFn,
|
767 | doneFn,
|
768 | onTransmuxerLog
|
769 | });
|
770 | };
|
771 |
|
772 |
|
773 | segment.endOfAllRequests = Date.now();
|
774 | if (segment.map && segment.map.encryptedBytes && !segment.map.bytes) {
|
775 | return decrypt({
|
776 | decryptionWorker,
|
777 |
|
778 |
|
779 |
|
780 | id: segment.requestId + '-init',
|
781 | encryptedBytes: segment.map.encryptedBytes,
|
782 | key: segment.map.key
|
783 | }, (decryptedBytes) => {
|
784 | segment.map.bytes = decryptedBytes;
|
785 |
|
786 | parseInitSegment(segment, (parseError) => {
|
787 | if (parseError) {
|
788 | abortAll(activeXhrs);
|
789 | return doneFn(parseError, segment);
|
790 | }
|
791 |
|
792 | segmentFinish();
|
793 | });
|
794 |
|
795 | });
|
796 | }
|
797 |
|
798 | segmentFinish();
|
799 | }
|
800 | };
|
801 | };
|
802 |
|
803 |
|
804 |
|
805 |
|
806 |
|
807 |
|
808 |
|
809 |
|
810 | const handleLoadEnd = ({ loadendState, abortFn }) => (event) => {
|
811 | const request = event.target;
|
812 |
|
813 | if (request.aborted && abortFn && !loadendState.calledAbortFn) {
|
814 | abortFn();
|
815 | loadendState.calledAbortFn = true;
|
816 | }
|
817 | };
|
818 |
|
819 |
|
820 |
|
821 |
|
822 |
|
823 |
|
824 |
|
825 |
|
826 |
|
827 |
|
828 |
|
829 |
|
830 |
|
831 |
|
832 |
|
833 |
|
834 |
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 |
|
841 |
|
842 |
|
843 |
|
844 | const handleProgress = ({
|
845 | segment,
|
846 | progressFn,
|
847 | trackInfoFn,
|
848 | timingInfoFn,
|
849 | videoSegmentTimingInfoFn,
|
850 | audioSegmentTimingInfoFn,
|
851 | id3Fn,
|
852 | captionsFn,
|
853 | isEndOfTimeline,
|
854 | endedTimelineFn,
|
855 | dataFn
|
856 | }) => (event) => {
|
857 | const request = event.target;
|
858 |
|
859 | if (request.aborted) {
|
860 | return;
|
861 | }
|
862 |
|
863 | segment.stats = videojs.mergeOptions(segment.stats, getProgressStats(event));
|
864 |
|
865 |
|
866 | if (!segment.stats.firstBytesReceivedAt && segment.stats.bytesReceived) {
|
867 | segment.stats.firstBytesReceivedAt = Date.now();
|
868 | }
|
869 |
|
870 | return progressFn(event, segment);
|
871 | };
|
872 |
|
873 |
|
874 |
|
875 |
|
876 |
|
877 |
|
878 |
|
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 |
|
887 |
|
888 |
|
889 |
|
890 |
|
891 |
|
892 |
|
893 |
|
894 |
|
895 |
|
896 |
|
897 |
|
898 |
|
899 |
|
900 |
|
901 |
|
902 |
|
903 |
|
904 |
|
905 |
|
906 |
|
907 |
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|
920 |
|
921 |
|
922 |
|
923 |
|
924 |
|
925 |
|
926 |
|
927 |
|
928 |
|
929 |
|
930 |
|
931 |
|
932 |
|
933 |
|
934 |
|
935 |
|
936 |
|
937 |
|
938 |
|
939 |
|
940 |
|
941 | export const mediaSegmentRequest = ({
|
942 | xhr,
|
943 | xhrOptions,
|
944 | decryptionWorker,
|
945 | segment,
|
946 | abortFn,
|
947 | progressFn,
|
948 | trackInfoFn,
|
949 | timingInfoFn,
|
950 | videoSegmentTimingInfoFn,
|
951 | audioSegmentTimingInfoFn,
|
952 | id3Fn,
|
953 | captionsFn,
|
954 | isEndOfTimeline,
|
955 | endedTimelineFn,
|
956 | dataFn,
|
957 | doneFn,
|
958 | onTransmuxerLog
|
959 | }) => {
|
960 | const activeXhrs = [];
|
961 | const finishProcessingFn = waitForCompletion({
|
962 | activeXhrs,
|
963 | decryptionWorker,
|
964 | trackInfoFn,
|
965 | timingInfoFn,
|
966 | videoSegmentTimingInfoFn,
|
967 | audioSegmentTimingInfoFn,
|
968 | id3Fn,
|
969 | captionsFn,
|
970 | isEndOfTimeline,
|
971 | endedTimelineFn,
|
972 | dataFn,
|
973 | doneFn,
|
974 | onTransmuxerLog
|
975 | });
|
976 |
|
977 |
|
978 | if (segment.key && !segment.key.bytes) {
|
979 | const objects = [segment.key];
|
980 |
|
981 | if (segment.map && !segment.map.bytes && segment.map.key && segment.map.key.resolvedUri === segment.key.resolvedUri) {
|
982 | objects.push(segment.map.key);
|
983 | }
|
984 | const keyRequestOptions = videojs.mergeOptions(xhrOptions, {
|
985 | uri: segment.key.resolvedUri,
|
986 | responseType: 'arraybuffer'
|
987 | });
|
988 | const keyRequestCallback = handleKeyResponse(segment, objects, finishProcessingFn);
|
989 | const keyXhr = xhr(keyRequestOptions, keyRequestCallback);
|
990 |
|
991 | activeXhrs.push(keyXhr);
|
992 | }
|
993 |
|
994 |
|
995 | if (segment.map && !segment.map.bytes) {
|
996 | const differentMapKey = segment.map.key && (!segment.key || segment.key.resolvedUri !== segment.map.key.resolvedUri);
|
997 |
|
998 | if (differentMapKey) {
|
999 | const mapKeyRequestOptions = videojs.mergeOptions(xhrOptions, {
|
1000 | uri: segment.map.key.resolvedUri,
|
1001 | responseType: 'arraybuffer'
|
1002 | });
|
1003 | const mapKeyRequestCallback = handleKeyResponse(segment, [segment.map.key], finishProcessingFn);
|
1004 | const mapKeyXhr = xhr(mapKeyRequestOptions, mapKeyRequestCallback);
|
1005 |
|
1006 | activeXhrs.push(mapKeyXhr);
|
1007 | }
|
1008 | const initSegmentOptions = videojs.mergeOptions(xhrOptions, {
|
1009 | uri: segment.map.resolvedUri,
|
1010 | responseType: 'arraybuffer',
|
1011 | headers: segmentXhrHeaders(segment.map)
|
1012 | });
|
1013 | const initSegmentRequestCallback = handleInitSegmentResponse({segment, finishProcessingFn});
|
1014 | const initSegmentXhr = xhr(initSegmentOptions, initSegmentRequestCallback);
|
1015 |
|
1016 | activeXhrs.push(initSegmentXhr);
|
1017 | }
|
1018 |
|
1019 | const segmentRequestOptions = videojs.mergeOptions(xhrOptions, {
|
1020 | uri: segment.part && segment.part.resolvedUri || segment.resolvedUri,
|
1021 | responseType: 'arraybuffer',
|
1022 | headers: segmentXhrHeaders(segment)
|
1023 | });
|
1024 |
|
1025 | const segmentRequestCallback = handleSegmentResponse({
|
1026 | segment,
|
1027 | finishProcessingFn,
|
1028 | responseType: segmentRequestOptions.responseType
|
1029 | });
|
1030 | const segmentXhr = xhr(segmentRequestOptions, segmentRequestCallback);
|
1031 |
|
1032 | segmentXhr.addEventListener(
|
1033 | 'progress',
|
1034 | handleProgress({
|
1035 | segment,
|
1036 | progressFn,
|
1037 | trackInfoFn,
|
1038 | timingInfoFn,
|
1039 | videoSegmentTimingInfoFn,
|
1040 | audioSegmentTimingInfoFn,
|
1041 | id3Fn,
|
1042 | captionsFn,
|
1043 | isEndOfTimeline,
|
1044 | endedTimelineFn,
|
1045 | dataFn
|
1046 | })
|
1047 | );
|
1048 | activeXhrs.push(segmentXhr);
|
1049 |
|
1050 |
|
1051 |
|
1052 | const loadendState = {};
|
1053 |
|
1054 | activeXhrs.forEach((activeXhr) => {
|
1055 | activeXhr.addEventListener(
|
1056 | 'loadend',
|
1057 | handleLoadEnd({ loadendState, abortFn })
|
1058 | );
|
1059 | });
|
1060 |
|
1061 | return () => abortAll(activeXhrs);
|
1062 | };
|