优化标签解析

This commit is contained in:
Harvey Zack committed 2018-12-21 15:43:12 +08:00
1 parent a2125a2634
commit 348aabf44c
4 files changed
+46 -34

No files matched your search

@@ -1,6 +1,6 @@
import fetchStream from './fetchStream';
import readFile from './readFile';
import getMetaData from './getMetaData';
import parseScripTag from './parseScripTag';
import { mergeTypedArrays, getUint8Sum, bin2String } from '../utils';
export default class FlvParse {
@@ -10,7 +10,7 @@ export default class FlvParse {
this.uint8 = new Uint8Array(0);
this.index = 0;
this.header = null;
this.metadata = null;
this.scripTag = null;
this.tags = [];
this.done = false;
@@ -40,7 +40,7 @@ export default class FlvParse {
this.uint8 = uint8;
this.index = 0;
this.header = null;
this.metadata = null;
this.scripTag = null;
this.tags = [];
this.parse();
}
@@ -82,15 +82,20 @@ export default class FlvParse {
tag.body = this.read(tag.dataSize);
this.tags.push(tag);
this.read(4);
this.flv.emit('flvParseTag', tag);
}
if (this.tags.length > 1 && this.tags[0].tagType === 18 && !this.metadata) {
this.metadata = getMetaData(this.tags[0]);
this.flv.emit('parseMetadata', this.metadata);
if (debug) {
console.log('[flv-parse-metadata]', this.metadata);
switch (tag.tagType) {
case 18:
this.scripTag = parseScripTag(tag.body);
this.flv.emit('parseScripTag', this.scripTag);
if (debug) {
console.log('[flv-parse-scrip-tag]', this.scripTag);
}
break;
default:
break;
}
this.flv.emit('flvParseTag', tag);
}
}
@@ -1,7 +1,7 @@
import { getUint8Sum, readUint8, bin2String, bin2Boolean, bin2Float, errorHandle } from '../utils';
export default function getMetaData(scripTag) {
const readScripTag = readUint8(scripTag.body);
export default function parseScripTag(scripTagBody) {
const readScripTag = readUint8(scripTagBody);
const metadata = Object.create(null);
const amf1 = Object.create(null);
const amf2 = Object.create(null);
@@ -34,7 +34,7 @@ export default function getMetaData(scripTag) {
case 3: {
value = Object.create(null);
let endObject = false;
while (!endObject && readScripTag.index < scripTag.body.length) {
while (!endObject && readScripTag.index < scripTagBody.length) {
const nameLength = getUint8Sum(readScripTag(2));
const name = bin2String(readScripTag(nameLength));
const type = readScripTag(1)[0];
@@ -50,7 +50,7 @@ export default function getMetaData(scripTag) {
case 8: {
value = Object.create(null);
let endArray = false;
while (!endArray && readScripTag.index < scripTag.body.length) {
while (!endArray && readScripTag.index < scripTagBody.length) {
const nameLength = getUint8Sum(readScripTag(2));
const name = bin2String(readScripTag(nameLength));
const type = readScripTag(1)[0];
@@ -78,7 +78,7 @@ export default function getMetaData(scripTag) {
break;
}
default:
console.log(readScripTag(scripTag.body.length - readScripTag.index - 1));
console.log(readScripTag(scripTagBody.length - readScripTag.index - 1));
errorHandle(false, `AMF: Unknown metaData type: ${type}`);
break;
}
@@ -86,7 +86,7 @@ export default function getMetaData(scripTag) {
return value;
}
while (readScripTag.index < scripTag.body.length) {
while (readScripTag.index < scripTagBody.length) {
const nameLength = getUint8Sum(readScripTag(2));
const name = bin2String(readScripTag(nameLength));
const type = readScripTag(1)[0];
@@ -95,7 +95,7 @@ export default function getMetaData(scripTag) {
}
}
errorHandle(readScripTag.index === scripTag.body.length, 'AMF: Seems to be incompletely parsed');
errorHandle(readScripTag.index === scripTagBody.length, 'AMF: Seems to be incompletely parsed');
errorHandle(amf2.size === Object.keys(amf2.metaData).length, 'AMF: [amf2] length does not match');
metadata.amf1 = amf1;