forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demuxer.js
152 lines (138 loc) · 5.4 KB
/
demuxer.js
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
import EventEmitter from 'events';
import work from 'webworkify-webpack';
import Event from '../events';
import DemuxerInline from '../demux/demuxer-inline';
import { logger } from '../utils/logger';
import { ErrorTypes, ErrorDetails } from '../errors';
import { getMediaSource } from '../utils/mediasource-helper';
const MediaSource = getMediaSource();
class Demuxer {
constructor (hls, id) {
this.hls = hls;
this.id = id;
// observer setup
const observer = this.observer = new EventEmitter();
const config = hls.config;
observer.trigger = function trigger (event, ...data) {
observer.emit(event, event, ...data);
};
observer.off = function off (event, ...data) {
observer.removeListener(event, ...data);
};
let forwardMessage = function (ev, data) {
data = data || {};
data.frag = this.frag;
data.id = this.id;
hls.trigger(ev, data);
}.bind(this);
// forward events to main thread
observer.on(Event.FRAG_DECRYPTED, forwardMessage);
observer.on(Event.FRAG_PARSING_INIT_SEGMENT, forwardMessage);
observer.on(Event.FRAG_PARSING_DATA, forwardMessage);
observer.on(Event.FRAG_PARSED, forwardMessage);
observer.on(Event.ERROR, forwardMessage);
observer.on(Event.FRAG_PARSING_METADATA, forwardMessage);
observer.on(Event.FRAG_PARSING_USERDATA, forwardMessage);
observer.on(Event.INIT_PTS_FOUND, forwardMessage);
const typeSupported = {
mp4: MediaSource.isTypeSupported('video/mp4'),
mpeg: MediaSource.isTypeSupported('audio/mpeg'),
mp3: MediaSource.isTypeSupported('audio/mp4; codecs="mp3"')
};
// navigator.vendor is not always available in Web Worker
// refer to https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/navigator
const vendor = navigator.vendor;
if (config.enableWorker && (typeof (Worker) !== 'undefined')) {
logger.log('demuxing in webworker');
let w;
try {
w = this.w = work(require.resolve('../demux/demuxer-worker.js'));
this.onwmsg = this.onWorkerMessage.bind(this);
w.addEventListener('message', this.onwmsg);
w.onerror = function (event) {
hls.trigger(Event.ERROR, { type: ErrorTypes.OTHER_ERROR, details: ErrorDetails.INTERNAL_EXCEPTION, fatal: true, event: 'demuxerWorker', err: { message: event.message + ' (' + event.filename + ':' + event.lineno + ')' } });
};
w.postMessage({ cmd: 'init', typeSupported: typeSupported, vendor: vendor, id: id, config: JSON.stringify(config) });
} catch (err) {
logger.error('error while initializing DemuxerWorker, fallback on DemuxerInline');
if (w) {
// revoke the Object URL that was used to create demuxer worker, so as not to leak it
URL.revokeObjectURL(w.objectURL);
}
this.demuxer = new DemuxerInline(observer, typeSupported, config, vendor);
this.w = undefined;
}
} else {
this.demuxer = new DemuxerInline(observer, typeSupported, config, vendor);
}
}
destroy () {
let w = this.w;
if (w) {
w.removeEventListener('message', this.onwmsg);
w.terminate();
this.w = null;
} else {
let demuxer = this.demuxer;
if (demuxer) {
demuxer.destroy();
this.demuxer = null;
}
}
let observer = this.observer;
if (observer) {
observer.removeAllListeners();
this.observer = null;
}
}
push (data, initSegment, audioCodec, videoCodec, frag, duration, accurateTimeOffset, defaultInitPTS) {
const w = this.w;
const timeOffset = !isNaN(frag.startDTS) ? frag.startDTS : frag.start;
const decryptdata = frag.decryptdata;
const lastFrag = this.frag;
const discontinuity = !(lastFrag && (frag.cc === lastFrag.cc));
const trackSwitch = !(lastFrag && (frag.level === lastFrag.level));
const nextSN = lastFrag && (frag.sn === (lastFrag.sn + 1));
const contiguous = !trackSwitch && nextSN;
if (discontinuity) {
logger.log(`${this.id}:discontinuity detected`);
}
if (trackSwitch) {
logger.log(`${this.id}:switch detected`);
}
this.frag = frag;
if (w) {
// post fragment payload as transferable objects for ArrayBuffer (no copy)
w.postMessage({ cmd: 'demux', data, decryptdata, initSegment, audioCodec, videoCodec, timeOffset, discontinuity, trackSwitch, contiguous, duration, accurateTimeOffset, defaultInitPTS }, data instanceof ArrayBuffer ? [data] : []);
} else {
let demuxer = this.demuxer;
if (demuxer) {
demuxer.push(data, decryptdata, initSegment, audioCodec, videoCodec, timeOffset, discontinuity, trackSwitch, contiguous, duration, accurateTimeOffset, defaultInitPTS);
}
}
}
onWorkerMessage (ev) {
let data = ev.data,
hls = this.hls;
switch (data.event) {
case 'init':
// revoke the Object URL that was used to create demuxer worker, so as not to leak it
URL.revokeObjectURL(this.w.objectURL);
break;
// special case for FRAG_PARSING_DATA: data1 and data2 are transferable objects
case Event.FRAG_PARSING_DATA:
data.data.data1 = new Uint8Array(data.data1);
if (data.data2) {
data.data.data2 = new Uint8Array(data.data2);
}
/* falls through */
default:
data.data = data.data || {};
data.data.frag = this.frag;
data.data.id = this.id;
hls.trigger(data.event, data.data);
break;
}
}
}
export default Demuxer;