add fetchStream

This commit is contained in:
Harvey Zack committed 2018-12-19 15:16:53 +08:00
1 parent 7264d0b3a3
commit 6cc89edcd9
10 files changed
+209 -54

No files matched your search

@@ -0,0 +1,77 @@
import fetchStream from './fetchStream';
import readFile from './readFile';
export default class FlvParse {
constructor(flv) {
this.flv = flv;
this.uint8 = [];
this.index = 0;
this.header = {};
this.tags = [];
flv.on('flvFetchStart', () => {
console.log('flvFetchStart');
});
flv.on('flvFetchInfo', info => {
console.log('flvFetchInfo', info);
});
flv.on('flvFetchCancel', () => {
console.log('flvFetchCancel');
});
flv.on('flvFetchError', error => {
console.log('flvFetchError', error);
});
flv.on('flvFetching', value => {
console.log(value);
});
flv.on('flvFetchEnd', value => {
console.log('flvFetchEnd', value);
if (value) {
this.uint8 = value;
}
});
const { url } = flv.options;
if (typeof url === 'string') {
fetchStream(flv, url);
} else {
readFile(flv, url);
}
}
parse() {
this.header.signature = this.read(3);
this.header.version = this.read(1);
this.header.flags = this.read(1);
this.header.headersize = this.read(4);
this.read(4);
while (this.index < this.uint8.length) {
const tag = {};
tag.tagType = this.read(1);
tag.dataSize = this.read(3);
tag.Timestamp = this.read(4);
tag.StreamID = this.read(3);
tag.body = this.read(FlvParse.getBodySum(tag.dataSize));
this.tags.push(tag);
this.read(4);
}
}
read(length) {
const tempUint8 = [];
for (let i = 0; i < length; i += 1) {
tempUint8.push(this.uint8[this.index]);
this.index += 1;
}
return tempUint8;
}
static getBodySum(arr) {
return arr[0] * 256 ** 2 + arr[1] * 256 + arr[2];
}
}