forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demuxer.js
112 lines (103 loc) · 3.71 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
import Event from '../events';
import DemuxerInline from '../demux/demuxer-inline';
import DemuxerWorker from '../demux/demuxer-worker';
import {logger} from '../utils/logger';
import Decrypter from '../crypt/decrypter';
class Demuxer {
constructor(hls) {
this.hls = hls;
var typeSupported = {
mp4 : MediaSource.isTypeSupported('video/mp4'),
mp2t : hls.config.enableMP2TPassThrough && MediaSource.isTypeSupported('video/mp2t')
};
if (hls.config.enableWorker && (typeof(Worker) !== 'undefined')) {
logger.log('demuxing in webworker');
try {
var work = require('webworkify');
this.w = work(DemuxerWorker);
this.onwmsg = this.onWorkerMessage.bind(this);
this.w.addEventListener('message', this.onwmsg);
this.w.postMessage({cmd: 'init', typeSupported : typeSupported});
} catch(err) {
logger.error('error while initializing DemuxerWorker, fallback on DemuxerInline');
this.demuxer = new DemuxerInline(hls,typeSupported);
}
} else {
this.demuxer = new DemuxerInline(hls,typeSupported);
}
this.demuxInitialized = true;
}
destroy() {
if (this.w) {
this.w.removeEventListener('message', this.onwmsg);
this.w.terminate();
this.w = null;
} else {
this.demuxer.destroy();
this.demuxer = null;
}
if (this.decrypter) {
this.decrypter.destroy();
this.decrypter = null;
}
}
pushDecrypted(data, audioCodec, videoCodec, timeOffset, cc, level, sn, duration) {
if (this.w) {
// post fragment payload as transferable objects (no copy)
this.w.postMessage({cmd: 'demux', data: data, audioCodec: audioCodec, videoCodec: videoCodec, timeOffset: timeOffset, cc: cc, level: level, sn : sn, duration: duration}, [data]);
} else {
this.demuxer.push(new Uint8Array(data), audioCodec, videoCodec, timeOffset, cc, level, sn, duration);
}
}
push(data, audioCodec, videoCodec, timeOffset, cc, level, sn, duration, decryptdata) {
if ((data.byteLength > 0) && (decryptdata != null) && (decryptdata.key != null) && (decryptdata.method === 'AES-128')) {
if (this.decrypter == null) {
this.decrypter = new Decrypter(this.hls);
}
var localthis = this;
this.decrypter.decrypt(data, decryptdata.key, decryptdata.iv, function(decryptedData){
localthis.pushDecrypted(decryptedData, audioCodec, videoCodec, timeOffset, cc, level, sn, duration);
});
} else {
this.pushDecrypted(data, audioCodec, videoCodec, timeOffset, cc, level, sn, duration);
}
}
onWorkerMessage(ev) {
var data = ev.data;
//console.log('onWorkerMessage:' + data.event);
switch(data.event) {
case Event.FRAG_PARSING_INIT_SEGMENT:
var obj = {};
obj.tracks = data.tracks;
obj.unique = data.unique;
this.hls.trigger(Event.FRAG_PARSING_INIT_SEGMENT, obj);
break;
case Event.FRAG_PARSING_DATA:
this.hls.trigger(Event.FRAG_PARSING_DATA,{
data1: new Uint8Array(data.data1),
data2: new Uint8Array(data.data2),
startPTS: data.startPTS,
endPTS: data.endPTS,
startDTS: data.startDTS,
endDTS: data.endDTS,
type: data.type,
nb: data.nb
});
break;
case Event.FRAG_PARSING_METADATA:
this.hls.trigger(Event.FRAG_PARSING_METADATA, {
samples: data.samples
});
break;
case Event.FRAG_PARSING_USERDATA:
this.hls.trigger(Event.FRAG_PARSING_USERDATA, {
samples: data.samples
});
break;
default:
this.hls.trigger(data.event, data.data);
break;
}
}
}
export default Demuxer;