1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/dashboard-ui/bower_components/hls.js/src/demux/demuxer.js

113 lines
3.7 KiB
JavaScript
Raw Normal View History

2015-12-16 00:30:14 -05:00
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;
2016-02-24 22:15:07 -05:00
var typeSupported = {
mp4 : MediaSource.isTypeSupported('video/mp4'),
mp2t : hls.config.enableMP2TPassThrough && MediaSource.isTypeSupported('video/mp2t')
};
2015-12-16 00:30:14 -05:00
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);
2016-02-24 22:15:07 -05:00
this.w.postMessage({cmd: 'init', typeSupported : typeSupported});
2015-12-16 00:30:14 -05:00
} catch(err) {
logger.error('error while initializing DemuxerWorker, fallback on DemuxerInline');
2016-02-24 22:15:07 -05:00
this.demuxer = new DemuxerInline(hls,typeSupported);
2015-12-16 00:30:14 -05:00
}
} else {
2016-02-24 22:15:07 -05:00
this.demuxer = new DemuxerInline(hls,typeSupported);
2015-12-16 00:30:14 -05:00
}
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);
}
2016-02-24 22:15:07 -05:00
2015-12-16 00:30:14 -05:00
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) {
2016-02-24 22:15:07 -05:00
var data = ev.data;
//console.log('onWorkerMessage:' + data.event);
switch(data.event) {
2015-12-16 00:30:14 -05:00
case Event.FRAG_PARSING_INIT_SEGMENT:
var obj = {};
2016-02-24 22:15:07 -05:00
obj.tracks = data.tracks;
obj.unique = data.unique;
2015-12-16 00:30:14 -05:00
this.hls.trigger(Event.FRAG_PARSING_INIT_SEGMENT, obj);
break;
case Event.FRAG_PARSING_DATA:
this.hls.trigger(Event.FRAG_PARSING_DATA,{
2016-02-24 22:15:07 -05:00
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
2015-12-16 00:30:14 -05:00
});
break;
case Event.FRAG_PARSING_METADATA:
this.hls.trigger(Event.FRAG_PARSING_METADATA, {
2016-02-24 22:15:07 -05:00
samples: data.samples
2015-12-16 00:30:14 -05:00
});
break;
2016-02-01 12:02:17 -05:00
case Event.FRAG_PARSING_USERDATA:
this.hls.trigger(Event.FRAG_PARSING_USERDATA, {
2016-02-24 22:15:07 -05:00
samples: data.samples
2016-02-01 12:02:17 -05:00
});
break;
2015-12-16 00:30:14 -05:00
default:
2016-02-24 22:15:07 -05:00
this.hls.trigger(data.event, data.data);
2015-12-16 00:30:14 -05:00
break;
}
}
}
export default Demuxer;