add config dir

This commit is contained in:
Harvey Zack committed 2018-10-16 09:57:20 +08:00
1 parent 682dda0ec8
commit 32478ecb14
16 files changed
+212 -135

No files matched your search

+30 -12
View File
@@ -1,6 +1,5 @@
import { errorHandle, request } from './utils';
import mimeCodeces from './utils/mimeCodeces';
import mseConfig from './utils/mseConfig';
import config from './config';
export default class Mse {
constructor(art) {
@@ -9,21 +8,27 @@ export default class Mse {
this.init();
this.eventBind();
this.eventStart();
this.repeat = 0;
this.maxRepeat = 5;
}
setMimeCodec() {
const { option } = this.art;
if (!option.type) {
const type = option.url.trim().toLowerCase().split('.').pop();
const type = option.url
.trim()
.toLowerCase()
.split('.')
.pop();
errorHandle(
Object.keys(mimeCodeces).includes(type),
Object.keys(config.mimeCodec).includes(type),
`Can't find video's type '${type}' from '${option.url}'`
);
option.type = type;
}
if (!option.mimeCodec) {
const mimeCodec = mimeCodeces[option.type];
const mimeCodec = config.mimeCodec[option.type];
errorHandle(
mimeCodec,
`Can't find video's mimeCodec from ${option.type}`
@@ -43,16 +48,16 @@ export default class Mse {
);
this.mediaSource = new MediaSource();
$video.src = URL.createObjectURL(this.mediaSource);
this.art.events.destroys.push(() => {
this.art.events.destroyEvents.push(() => {
URL.revokeObjectURL($video.src);
});
}
eventBind() {
const { proxy } = this.art.events;
const { instance, sourceBufferList } = mseConfig;
const { mediaSource, sourceBufferList } = config.mse;
instance.events.forEach(eventName => {
mediaSource.events.forEach(eventName => {
proxy(this.mediaSource, eventName, event => {
this.art.emit(`mediaSource:${event.type}`, event);
});
@@ -71,7 +76,7 @@ export default class Mse {
eventStart() {
const { option } = this.art;
const { proxy } = this.art.events;
const { sourceBuffer } = mseConfig;
const { sourceBuffer } = config.mse;
this.art.on('mediaSource:sourceopen', () => {
this.sourceBuffer = this.mediaSource.addSourceBuffer(option.mimeCodec);
@@ -86,8 +91,21 @@ export default class Mse {
this.mediaSource.endOfStream();
});
request(option.url).then(response => {
this.sourceBuffer.appendBuffer(response);
});
this.fetchUrl();
}
fetchUrl() {
const { option, notice } = this.art;
request(option.url)
.then(response => {
this.sourceBuffer.appendBuffer(response);
})
.catch(err => {
if (this.repeat++ < this.maxRepeat) {
this.fetchUrl();
} else {
notice.show(err);
}
});
}
}