mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-08 19:06:15 -08:00
优化amf解析
This commit is contained in:
1 parent
3afe2e1640
commit
32b2f4792a
9 files changed
+190
-119
No files matched your search
@@ -28,6 +28,7 @@ body,
|
||||
}
|
||||
|
||||
.part-top .artplayer {
|
||||
opacity: 0;
|
||||
width: 500px;
|
||||
height: 300px;
|
||||
background: #fff;
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
var flv = new Flv({
|
||||
mediaElement: video,
|
||||
url: file instanceof File ? file : url,
|
||||
debug: true,
|
||||
});
|
||||
|
||||
this.on('destroy', () => {
|
||||
|
||||
@@ -346,18 +346,22 @@
|
||||
function bin2Boolean(bin) {
|
||||
return bin === 1;
|
||||
}
|
||||
function log(name) {
|
||||
var msg = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
||||
console.log("[".concat(name, "] ").concat(msg));
|
||||
}
|
||||
function readUint8(uint8) {
|
||||
var index = 0;
|
||||
return function read(length) {
|
||||
var result = [];
|
||||
var tempUint8 = new Uint8Array(length);
|
||||
|
||||
for (var i = 0; i < length; i += 1) {
|
||||
result.push(uint8[index]);
|
||||
tempUint8[i] = uint8[index];
|
||||
index += 1;
|
||||
}
|
||||
|
||||
read.index = index;
|
||||
return result;
|
||||
return tempUint8;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -371,6 +375,7 @@
|
||||
bin2String: bin2String,
|
||||
bin2Float: bin2Float,
|
||||
bin2Boolean: bin2Boolean,
|
||||
log: log,
|
||||
readUint8: readUint8
|
||||
});
|
||||
|
||||
@@ -565,61 +570,6 @@
|
||||
return CreatMediaSource;
|
||||
}();
|
||||
|
||||
function fetchStream(flv, url) {
|
||||
flv.emit('flvFetchStart');
|
||||
fetch(url).then(function (response) {
|
||||
errorHandle(response.ok && response.status >= 200 && response.status <= 299, "".concat(response.status, " ").concat(response.statusText));
|
||||
var contentType = response.headers.get('content-type');
|
||||
errorHandle(contentType.includes('x-flv'), 'The resource does not seem to be a flv file');
|
||||
return new Response(new ReadableStream({
|
||||
start: function start(controller) {
|
||||
var reader = response.body.getReader();
|
||||
flv.on('destroy', function () {
|
||||
reader.cancel();
|
||||
});
|
||||
flv.on('readerCancel', function () {
|
||||
reader.cancel();
|
||||
});
|
||||
|
||||
(function read() {
|
||||
reader.read().then(function (_ref) {
|
||||
var done = _ref.done,
|
||||
value = _ref.value;
|
||||
|
||||
if (done) {
|
||||
flv.emit('flvFetchEnd');
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
|
||||
flv.emit('flvFetching', new Uint8Array(value));
|
||||
controller.enqueue(value);
|
||||
read();
|
||||
}).catch(function (error) {
|
||||
flv.emit('flvFetchError', error);
|
||||
});
|
||||
})();
|
||||
},
|
||||
cancel: function cancel() {
|
||||
flv.emit('flvFetchCancel');
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
function readFile(flv, file) {
|
||||
console.log(file);
|
||||
flv.emit('flvFetchStart');
|
||||
var proxy = flv.events.proxy;
|
||||
var reader = new FileReader();
|
||||
proxy(reader, 'load', function (e) {
|
||||
var buffer = e.target.result;
|
||||
var uint8 = new Uint8Array(buffer);
|
||||
flv.emit('flvFetchEnd', uint8);
|
||||
});
|
||||
reader.readAsArrayBuffer(file);
|
||||
}
|
||||
|
||||
function _arrayWithHoles(arr) {
|
||||
if (Array.isArray(arr)) return arr;
|
||||
}
|
||||
@@ -666,6 +616,61 @@
|
||||
|
||||
var slicedToArray = _slicedToArray;
|
||||
|
||||
function fetchStream(flv, url) {
|
||||
flv.emit('flvFetchStart');
|
||||
fetch(url).then(function (response) {
|
||||
errorHandle(response.ok && response.status >= 200 && response.status <= 299, "".concat(response.status, " ").concat(response.statusText));
|
||||
var contentType = response.headers.get('content-type');
|
||||
errorHandle(contentType.includes('x-flv'), 'The resource does not seem to be a flv file');
|
||||
return new Response(new ReadableStream({
|
||||
start: function start(controller) {
|
||||
var reader = response.body.getReader();
|
||||
flv.on('destroy', function () {
|
||||
reader.cancel();
|
||||
});
|
||||
flv.on('readerCancel', function () {
|
||||
reader.cancel();
|
||||
});
|
||||
|
||||
(function read() {
|
||||
reader.read().then(function (_ref) {
|
||||
var done = _ref.done,
|
||||
value = _ref.value;
|
||||
|
||||
if (done) {
|
||||
flv.emit('flvFetchEnd');
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
|
||||
flv.emit('flvFetching', new Uint8Array(value));
|
||||
controller.enqueue(value);
|
||||
read();
|
||||
}).catch(function (error) {
|
||||
throw error;
|
||||
});
|
||||
})();
|
||||
},
|
||||
cancel: function cancel() {
|
||||
flv.emit('flvFetchCancel');
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
function readFile(flv, file) {
|
||||
console.log(file);
|
||||
flv.emit('flvFetchStart');
|
||||
var proxy = flv.events.proxy;
|
||||
var reader = new FileReader();
|
||||
proxy(reader, 'load', function (e) {
|
||||
var buffer = e.target.result;
|
||||
var uint8 = new Uint8Array(buffer);
|
||||
flv.emit('flvFetchEnd', uint8);
|
||||
});
|
||||
reader.readAsArrayBuffer(file);
|
||||
}
|
||||
|
||||
function getMetaData(scripTag) {
|
||||
var readScripTag = readUint8(scripTag.body);
|
||||
var metadata = Object.create(null);
|
||||
@@ -677,6 +682,7 @@
|
||||
var _readScripTag2 = slicedToArray(_readScripTag, 1);
|
||||
|
||||
amf1.type = _readScripTag2[0];
|
||||
errorHandle(amf1.type === 2, "AMF: [amf1] type expect 2, but got ".concat(amf1.type));
|
||||
amf1.size = getUint8Sum(readScripTag(2));
|
||||
amf1.string = bin2String(readScripTag(amf1.size));
|
||||
|
||||
@@ -685,6 +691,7 @@
|
||||
var _readScripTag4 = slicedToArray(_readScripTag3, 1);
|
||||
|
||||
amf2.type = _readScripTag4[0];
|
||||
errorHandle(amf2.type === 8, "AMF: [amf1] type expect 8, but got ".concat(amf2.type));
|
||||
amf2.size = getUint8Sum(readScripTag(4));
|
||||
amf2.metaData = Object.create(null);
|
||||
|
||||
@@ -733,9 +740,9 @@
|
||||
case 8:
|
||||
{
|
||||
value = Object.create(null);
|
||||
var _endObject = false;
|
||||
var endArray = false;
|
||||
|
||||
while (!_endObject && readScripTag.index < scripTag.body.length) {
|
||||
while (!endArray && readScripTag.index < scripTag.body.length) {
|
||||
var _nameLength = getUint8Sum(readScripTag(2));
|
||||
|
||||
var _name = bin2String(readScripTag(_nameLength));
|
||||
@@ -747,7 +754,7 @@
|
||||
}
|
||||
|
||||
if (_type2 === 9) {
|
||||
_endObject = true;
|
||||
endArray = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -777,7 +784,7 @@
|
||||
}
|
||||
|
||||
default:
|
||||
console.warn("AMF: Unknown metaData type: ".concat(type, " in ").concat(readScripTag.index - 1));
|
||||
errorHandle(false, "AMF: Unknown metaData type: ".concat(type));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -795,6 +802,8 @@
|
||||
}
|
||||
}
|
||||
|
||||
errorHandle(readScripTag.index === scripTag.body.length, 'AMF: Seems to be incompletely parsed');
|
||||
errorHandle(amf2.size === Object.keys(amf2.metaData).length, 'AMF: [amf2] length does not match');
|
||||
metadata.amf1 = amf1;
|
||||
metadata.amf2 = amf2;
|
||||
return metadata;
|
||||
@@ -809,7 +818,9 @@
|
||||
classCallCheck(this, FlvParse);
|
||||
|
||||
this.flv = flv;
|
||||
var url = flv.options.url;
|
||||
var _flv$options = flv.options,
|
||||
url = _flv$options.url,
|
||||
debug = _flv$options.debug;
|
||||
this.uint8 = new Uint8Array(0);
|
||||
this.index = 0;
|
||||
this.header = null;
|
||||
@@ -817,13 +828,14 @@
|
||||
this.tags = [];
|
||||
this.done = false;
|
||||
flv.on('flvFetchStart', function () {
|
||||
console.log('[flv-fetch-start]');
|
||||
if (debug) {
|
||||
console.log('[flv-fetch-start]', url);
|
||||
}
|
||||
});
|
||||
flv.on('flvFetchCancel', function () {
|
||||
console.log('[flv-fetch-cancel]');
|
||||
});
|
||||
flv.on('flvFetchError', function (error) {
|
||||
console.log('[flv-fetch-error]', error);
|
||||
if (debug) {
|
||||
console.log('[flv-fetch-cancel]');
|
||||
}
|
||||
});
|
||||
flv.on('flvFetching', function (uint8) {
|
||||
_this.uint8 = mergeTypedArrays(_this.uint8, uint8);
|
||||
@@ -831,7 +843,10 @@
|
||||
_this.parse();
|
||||
});
|
||||
flv.on('flvFetchEnd', function (uint8) {
|
||||
console.log('[flv-fetch-end]');
|
||||
if (debug) {
|
||||
console.log('[flv-fetch-end]');
|
||||
}
|
||||
|
||||
_this.done = true;
|
||||
|
||||
if (uint8) {
|
||||
@@ -844,7 +859,11 @@
|
||||
_this.parse();
|
||||
}
|
||||
|
||||
flv.emit('parseDone');
|
||||
flv.emit('flvParseDone');
|
||||
|
||||
if (debug) {
|
||||
console.log('[flv-parse-done]');
|
||||
}
|
||||
});
|
||||
|
||||
if (typeof url === 'string') {
|
||||
@@ -857,43 +876,68 @@
|
||||
createClass(FlvParse, [{
|
||||
key: "parse",
|
||||
value: function parse() {
|
||||
var debug = this.flv.options.debug;
|
||||
|
||||
if (this.uint8.length >= 13 && !this.header) {
|
||||
var header = Object.create(null);
|
||||
header.signature = this.read(3);
|
||||
header.version = this.read(1);
|
||||
header.flags = this.read(1);
|
||||
header.headersize = this.read(4);
|
||||
header.signature = bin2String(this.read(3));
|
||||
|
||||
var _this$read = this.read(1);
|
||||
|
||||
var _this$read2 = slicedToArray(_this$read, 1);
|
||||
|
||||
header.version = _this$read2[0];
|
||||
|
||||
var _this$read3 = this.read(1);
|
||||
|
||||
var _this$read4 = slicedToArray(_this$read3, 1);
|
||||
|
||||
header.flags = _this$read4[0];
|
||||
header.headersize = getUint8Sum(this.read(4));
|
||||
this.header = header;
|
||||
this.read(4);
|
||||
this.flv.emit('parseHeader', this.header);
|
||||
console.log(this.header);
|
||||
this.flv.emit('flvParseHeader', this.header);
|
||||
|
||||
if (debug) {
|
||||
console.log('[flv-parse-header]', this.header);
|
||||
}
|
||||
}
|
||||
|
||||
while (this.index < this.uint8.length) {
|
||||
var tag = Object.create(null);
|
||||
tag.tagType = this.read(1);
|
||||
tag.dataSize = this.read(3);
|
||||
|
||||
var _this$read5 = this.read(1);
|
||||
|
||||
var _this$read6 = slicedToArray(_this$read5, 1);
|
||||
|
||||
tag.tagType = _this$read6[0];
|
||||
tag.dataSize = getUint8Sum(this.read(3));
|
||||
tag.timestamp = this.read(4);
|
||||
tag.streamID = this.read(3);
|
||||
tag.body = this.read(getUint8Sum(tag.dataSize));
|
||||
tag.body = this.read(tag.dataSize);
|
||||
this.tags.push(tag);
|
||||
this.read(4);
|
||||
this.flv.emit('parseTag', tag);
|
||||
this.flv.emit('flvParseTag', tag);
|
||||
}
|
||||
|
||||
if (this.tags.length > 1 && this.tags[0].tagType[0] === 18 && !this.metadata) {
|
||||
this.flv.emit('flvParseTags', this.tags);
|
||||
|
||||
if (this.tags.length > 1 && this.tags[0].tagType === 18 && !this.metadata) {
|
||||
this.metadata = getMetaData(this.tags[0]);
|
||||
this.flv.emit('parseMetadata', this.metadata);
|
||||
console.log(this.metadata);
|
||||
|
||||
if (debug) {
|
||||
console.log('[flv-parse-metadata]', this.metadata);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, {
|
||||
key: "read",
|
||||
value: function read(length) {
|
||||
var tempUint8 = [];
|
||||
var tempUint8 = new Uint8Array(length);
|
||||
|
||||
for (var i = 0; i < length; i += 1) {
|
||||
tempUint8.push(this.uint8[this.index]);
|
||||
tempUint8[i] = this.uint8[this.index];
|
||||
this.index += 1;
|
||||
}
|
||||
|
||||
@@ -942,7 +986,8 @@
|
||||
get: function get() {
|
||||
return {
|
||||
mediaElement: '',
|
||||
url: ''
|
||||
url: '',
|
||||
debug: false
|
||||
};
|
||||
}
|
||||
}, {
|
||||
|
||||
@@ -1 +1 @@
|
||||
{"version":3,"file":"artplayer-plugin-flv.js","sources":["../../node_modules/@babel/runtime/helpers/classCallCheck.js","../../node_modules/@babel/runtime/helpers/createClass.js","../../node_modules/@babel/runtime/helpers/typeof.js","../../node_modules/@babel/runtime/helpers/assertThisInitialized.js","../../node_modules/@babel/runtime/helpers/possibleConstructorReturn.js","../../node_modules/@babel/runtime/helpers/getPrototypeOf.js","../../node_modules/@babel/runtime/helpers/setPrototypeOf.js","../../node_modules/@babel/runtime/helpers/inherits.js","../../packages/artplayer-plugin-flv/node_modules/tiny-emitter/index.js","../../node_modules/@babel/runtime/helpers/arrayWithoutHoles.js","../../node_modules/@babel/runtime/helpers/iterableToArray.js","../../node_modules/@babel/runtime/helpers/nonIterableSpread.js","../../node_modules/@babel/runtime/helpers/toConsumableArray.js","../../node_modules/@babel/runtime/helpers/isNativeFunction.js","../../node_modules/@babel/runtime/helpers/construct.js","../../node_modules/@babel/runtime/helpers/wrapNativeSuper.js","../../packages/artplayer-plugin-flv/src/utils/index.js","../../packages/artplayer-plugin-flv/src/utils/checkSupport.js","../../packages/artplayer-plugin-flv/src/eventProxy.js","../../packages/artplayer-plugin-flv/src/creatWorker.js","../../packages/artplayer-plugin-flv/src/config.js","../../packages/artplayer-plugin-flv/src/creatMediaSource.js","../../packages/artplayer-plugin-flv/src/flvParse/fetchStream.js","../../packages/artplayer-plugin-flv/src/flvParse/readFile.js","../../node_modules/@babel/runtime/helpers/arrayWithHoles.js","../../node_modules/@babel/runtime/helpers/iterableToArrayLimit.js","../../node_modules/@babel/runtime/helpers/nonIterableRest.js","../../node_modules/@babel/runtime/helpers/slicedToArray.js","../../packages/artplayer-plugin-flv/src/flvParse/getMetaData.js","../../packages/artplayer-plugin-flv/src/flvParse/index.js","../../packages/artplayer-plugin-flv/src/index.js"],"sourcesContent":["function _classCallCheck(instance, Constructor) {\n if (!(instance instanceof Constructor)) {\n throw new TypeError(\"Cannot call a class as a function\");\n }\n}\n\nmodule.exports = _classCallCheck;","function _defineProperties(target, props) {\n for (var i = 0; i < props.length; i++) {\n var descriptor = props[i];\n descriptor.enumerable = descriptor.enumerable || false;\n descriptor.configurable = true;\n if (\"value\" in descriptor) descriptor.writable = true;\n Object.defineProperty(target, descriptor.key, descriptor);\n }\n}\n\nfunction _createClass(Constructor, protoProps, staticProps) {\n if (protoProps) _defineProperties(Constructor.prototype, protoProps);\n if (staticProps) _defineProperties(Constructor, staticProps);\n return Constructor;\n}\n\nmodule.exports = _createClass;","function _typeof2(obj) { if (typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\") { _typeof2 = function _typeof2(obj) { return typeof obj; }; } else { _typeof2 = function _typeof2(obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; }; } return _typeof2(obj); }\n\nfunction _typeof(obj) {\n if (typeof Symbol === \"function\" && _typeof2(Symbol.iterator) === \"symbol\") {\n module.exports = _typeof = function _typeof(obj) {\n return _typeof2(obj);\n };\n } else {\n module.exports = _typeof = function _typeof(obj) {\n return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : _typeof2(obj);\n };\n }\n\n return _typeof(obj);\n}\n\nmodule.exports = _typeof;","function _assertThisInitialized(self) {\n if (self === void 0) {\n throw new ReferenceError(\"this hasn't been initialised - super() hasn't been called\");\n }\n\n return self;\n}\n\nmodule.exports = _assertThisInitialized;","var _typeof = require(\"../helpers/typeof\");\n\nvar assertThisInitialized = require(\"./assertThisInitialized\");\n\nfunction _possibleConstructorReturn(self, call) {\n if (call && (_typeof(call) === \"object\" || typeof call === \"function\")) {\n return call;\n }\n\n return assertThisInitialized(self);\n}\n\nmodule.exports = _possibleConstructorReturn;","function _getPrototypeOf(o) {\n module.exports = _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {\n return o.__proto__ || Object.getPrototypeOf(o);\n };\n return _getPrototypeOf(o);\n}\n\nmodule.exports = _getPrototypeOf;","function _setPrototypeOf(o, p) {\n module.exports = _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {\n o.__proto__ = p;\n return o;\n };\n\n return _setPrototypeOf(o, p);\n}\n\nmodule.exports = _setPrototypeOf;","var setPrototypeOf = require(\"./setPrototypeOf\");\n\nfunction _inherits(subClass, superClass) {\n if (typeof superClass !== \"function\" && superClass !== null) {\n throw new TypeError(\"Super expLine truncated
|
||||
{"version":3,"file":"artplayer-plugin-flv.js","sources":["../../node_modules/@babel/runtime/helpers/classCallCheck.js","../../node_modules/@babel/runtime/helpers/createClass.js","../../node_modules/@babel/runtime/helpers/typeof.js","../../node_modules/@babel/runtime/helpers/assertThisInitialized.js","../../node_modules/@babel/runtime/helpers/possibleConstructorReturn.js","../../node_modules/@babel/runtime/helpers/getPrototypeOf.js","../../node_modules/@babel/runtime/helpers/setPrototypeOf.js","../../node_modules/@babel/runtime/helpers/inherits.js","../../packages/artplayer-plugin-flv/node_modules/tiny-emitter/index.js","../../node_modules/@babel/runtime/helpers/arrayWithoutHoles.js","../../node_modules/@babel/runtime/helpers/iterableToArray.js","../../node_modules/@babel/runtime/helpers/nonIterableSpread.js","../../node_modules/@babel/runtime/helpers/toConsumableArray.js","../../node_modules/@babel/runtime/helpers/isNativeFunction.js","../../node_modules/@babel/runtime/helpers/construct.js","../../node_modules/@babel/runtime/helpers/wrapNativeSuper.js","../../packages/artplayer-plugin-flv/src/utils/index.js","../../packages/artplayer-plugin-flv/src/utils/checkSupport.js","../../packages/artplayer-plugin-flv/src/eventProxy.js","../../packages/artplayer-plugin-flv/src/creatWorker.js","../../packages/artplayer-plugin-flv/src/config.js","../../packages/artplayer-plugin-flv/src/creatMediaSource.js","../../node_modules/@babel/runtime/helpers/arrayWithHoles.js","../../node_modules/@babel/runtime/helpers/iterableToArrayLimit.js","../../node_modules/@babel/runtime/helpers/nonIterableRest.js","../../node_modules/@babel/runtime/helpers/slicedToArray.js","../../packages/artplayer-plugin-flv/src/flvParse/fetchStream.js","../../packages/artplayer-plugin-flv/src/flvParse/readFile.js","../../packages/artplayer-plugin-flv/src/flvParse/getMetaData.js","../../packages/artplayer-plugin-flv/src/flvParse/index.js","../../packages/artplayer-plugin-flv/src/index.js"],"sourcesContent":["function _classCallCheck(instance, Constructor) {\n if (!(instance instanceof Constructor)) {\n throw new TypeError(\"Cannot call a class as a function\");\n }\n}\n\nmodule.exports = _classCallCheck;","function _defineProperties(target, props) {\n for (var i = 0; i < props.length; i++) {\n var descriptor = props[i];\n descriptor.enumerable = descriptor.enumerable || false;\n descriptor.configurable = true;\n if (\"value\" in descriptor) descriptor.writable = true;\n Object.defineProperty(target, descriptor.key, descriptor);\n }\n}\n\nfunction _createClass(Constructor, protoProps, staticProps) {\n if (protoProps) _defineProperties(Constructor.prototype, protoProps);\n if (staticProps) _defineProperties(Constructor, staticProps);\n return Constructor;\n}\n\nmodule.exports = _createClass;","function _typeof2(obj) { if (typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\") { _typeof2 = function _typeof2(obj) { return typeof obj; }; } else { _typeof2 = function _typeof2(obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; }; } return _typeof2(obj); }\n\nfunction _typeof(obj) {\n if (typeof Symbol === \"function\" && _typeof2(Symbol.iterator) === \"symbol\") {\n module.exports = _typeof = function _typeof(obj) {\n return _typeof2(obj);\n };\n } else {\n module.exports = _typeof = function _typeof(obj) {\n return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : _typeof2(obj);\n };\n }\n\n return _typeof(obj);\n}\n\nmodule.exports = _typeof;","function _assertThisInitialized(self) {\n if (self === void 0) {\n throw new ReferenceError(\"this hasn't been initialised - super() hasn't been called\");\n }\n\n return self;\n}\n\nmodule.exports = _assertThisInitialized;","var _typeof = require(\"../helpers/typeof\");\n\nvar assertThisInitialized = require(\"./assertThisInitialized\");\n\nfunction _possibleConstructorReturn(self, call) {\n if (call && (_typeof(call) === \"object\" || typeof call === \"function\")) {\n return call;\n }\n\n return assertThisInitialized(self);\n}\n\nmodule.exports = _possibleConstructorReturn;","function _getPrototypeOf(o) {\n module.exports = _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {\n return o.__proto__ || Object.getPrototypeOf(o);\n };\n return _getPrototypeOf(o);\n}\n\nmodule.exports = _getPrototypeOf;","function _setPrototypeOf(o, p) {\n module.exports = _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {\n o.__proto__ = p;\n return o;\n };\n\n return _setPrototypeOf(o, p);\n}\n\nmodule.exports = _setPrototypeOf;","var setPrototypeOf = require(\"./setPrototypeOf\");\n\nfunction _inherits(subClass, superClass) {\n if (typeof superClass !== \"function\" && superClass !== null) {\n throw new TypeError(\"Super expLine truncated
|
||||
@@ -36,7 +36,7 @@ export default function fetchStream(flv, url) {
|
||||
read();
|
||||
})
|
||||
.catch(error => {
|
||||
flv.emit('flvFetchError', error);
|
||||
throw error;
|
||||
});
|
||||
})();
|
||||
},
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
import { getUint8Sum, readUint8, bin2String, bin2Boolean, bin2Float } from '../utils';
|
||||
import { getUint8Sum, readUint8, bin2String, bin2Boolean, bin2Float, errorHandle } from '../utils';
|
||||
|
||||
export default function getMetaData(scripTag) {
|
||||
const readScripTag = readUint8(scripTag.body);
|
||||
const metadata = Object.create(null);
|
||||
const amf1 = Object.create(null);
|
||||
const amf2 = Object.create(null);
|
||||
|
||||
[amf1.type] = readScripTag(1);
|
||||
errorHandle(amf1.type === 2, `AMF: [amf1] type expect 2, but got ${amf1.type}`);
|
||||
amf1.size = getUint8Sum(readScripTag(2));
|
||||
amf1.string = bin2String(readScripTag(amf1.size));
|
||||
|
||||
[amf2.type] = readScripTag(1);
|
||||
errorHandle(amf2.type === 8, `AMF: [amf1] type expect 8, but got ${amf2.type}`);
|
||||
amf2.size = getUint8Sum(readScripTag(4));
|
||||
amf2.metaData = Object.create(null);
|
||||
|
||||
@@ -45,8 +49,8 @@ export default function getMetaData(scripTag) {
|
||||
}
|
||||
case 8: {
|
||||
value = Object.create(null);
|
||||
let endObject = false;
|
||||
while (!endObject && readScripTag.index < scripTag.body.length) {
|
||||
let endArray = false;
|
||||
while (!endArray && readScripTag.index < scripTag.body.length) {
|
||||
const nameLength = getUint8Sum(readScripTag(2));
|
||||
const name = bin2String(readScripTag(nameLength));
|
||||
const type = readScripTag(1)[0];
|
||||
@@ -54,7 +58,7 @@ export default function getMetaData(scripTag) {
|
||||
value[name] = getValue(type);
|
||||
}
|
||||
if (type === 9) {
|
||||
endObject = true;
|
||||
endArray = true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -74,7 +78,7 @@ export default function getMetaData(scripTag) {
|
||||
break;
|
||||
}
|
||||
default:
|
||||
console.warn(`AMF: Unknown metaData type: ${type} in ${readScripTag.index - 1}`);
|
||||
errorHandle(false, `AMF: Unknown metaData type: ${type}`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -90,6 +94,9 @@ export default function getMetaData(scripTag) {
|
||||
}
|
||||
}
|
||||
|
||||
errorHandle(readScripTag.index === scripTag.body.length, 'AMF: Seems to be incompletely parsed');
|
||||
errorHandle(amf2.size === Object.keys(amf2.metaData).length, 'AMF: [amf2] length does not match');
|
||||
|
||||
metadata.amf1 = amf1;
|
||||
metadata.amf2 = amf2;
|
||||
return metadata;
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import fetchStream from './fetchStream';
|
||||
import readFile from './readFile';
|
||||
import getMetaData from './getMetaData';
|
||||
import { mergeTypedArrays, getUint8Sum } from '../utils';
|
||||
import { mergeTypedArrays, getUint8Sum, bin2String } from '../utils';
|
||||
|
||||
export default class FlvParse {
|
||||
constructor(flv) {
|
||||
this.flv = flv;
|
||||
const { url } = flv.options;
|
||||
const { url, debug } = flv.options;
|
||||
this.uint8 = new Uint8Array(0);
|
||||
this.index = 0;
|
||||
this.header = null;
|
||||
@@ -15,15 +15,15 @@ export default class FlvParse {
|
||||
this.done = false;
|
||||
|
||||
flv.on('flvFetchStart', () => {
|
||||
console.log('[flv-fetch-start]');
|
||||
if (debug) {
|
||||
console.log('[flv-fetch-start]', url);
|
||||
}
|
||||
});
|
||||
|
||||
flv.on('flvFetchCancel', () => {
|
||||
console.log('[flv-fetch-cancel]');
|
||||
});
|
||||
|
||||
flv.on('flvFetchError', error => {
|
||||
console.log('[flv-fetch-error]', error);
|
||||
if (debug) {
|
||||
console.log('[flv-fetch-cancel]');
|
||||
}
|
||||
});
|
||||
|
||||
flv.on('flvFetching', uint8 => {
|
||||
@@ -32,7 +32,9 @@ export default class FlvParse {
|
||||
});
|
||||
|
||||
flv.on('flvFetchEnd', uint8 => {
|
||||
console.log('[flv-fetch-end]');
|
||||
if (debug) {
|
||||
console.log('[flv-fetch-end]');
|
||||
}
|
||||
this.done = true;
|
||||
if (uint8) {
|
||||
this.uint8 = uint8;
|
||||
@@ -42,7 +44,10 @@ export default class FlvParse {
|
||||
this.tags = [];
|
||||
this.parse();
|
||||
}
|
||||
flv.emit('parseDone');
|
||||
flv.emit('flvParseDone');
|
||||
if (debug) {
|
||||
console.log('[flv-parse-done]');
|
||||
}
|
||||
});
|
||||
|
||||
if (typeof url === 'string') {
|
||||
@@ -53,41 +58,48 @@ export default class FlvParse {
|
||||
}
|
||||
|
||||
parse() {
|
||||
const { debug } = this.flv.options;
|
||||
if (this.uint8.length >= 13 && !this.header) {
|
||||
const header = Object.create(null);
|
||||
header.signature = this.read(3);
|
||||
header.version = this.read(1);
|
||||
header.flags = this.read(1);
|
||||
header.headersize = this.read(4);
|
||||
header.signature = bin2String(this.read(3));
|
||||
[header.version] = this.read(1);
|
||||
[header.flags] = this.read(1);
|
||||
header.headersize = getUint8Sum(this.read(4));
|
||||
this.header = header;
|
||||
this.read(4);
|
||||
this.flv.emit('parseHeader', this.header);
|
||||
console.log(this.header);
|
||||
this.flv.emit('flvParseHeader', this.header);
|
||||
if (debug) {
|
||||
console.log('[flv-parse-header]', this.header);
|
||||
}
|
||||
}
|
||||
|
||||
while (this.index < this.uint8.length) {
|
||||
const tag = Object.create(null);
|
||||
tag.tagType = this.read(1);
|
||||
tag.dataSize = this.read(3);
|
||||
[tag.tagType] = this.read(1);
|
||||
tag.dataSize = getUint8Sum(this.read(3));
|
||||
tag.timestamp = this.read(4);
|
||||
tag.streamID = this.read(3);
|
||||
tag.body = this.read(getUint8Sum(tag.dataSize));
|
||||
tag.body = this.read(tag.dataSize);
|
||||
this.tags.push(tag);
|
||||
this.read(4);
|
||||
this.flv.emit('parseTag', tag);
|
||||
this.flv.emit('flvParseTag', tag);
|
||||
}
|
||||
|
||||
if (this.tags.length > 1 && this.tags[0].tagType[0] === 18 && !this.metadata) {
|
||||
this.flv.emit('flvParseTags', this.tags);
|
||||
|
||||
if (this.tags.length > 1 && this.tags[0].tagType === 18 && !this.metadata) {
|
||||
this.metadata = getMetaData(this.tags[0]);
|
||||
this.flv.emit('parseMetadata', this.metadata);
|
||||
console.log(this.metadata);
|
||||
if (debug) {
|
||||
console.log('[flv-parse-metadata]', this.metadata);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
read(length) {
|
||||
const tempUint8 = [];
|
||||
const tempUint8 = new Uint8Array(length);
|
||||
for (let i = 0; i < length; i += 1) {
|
||||
tempUint8.push(this.uint8[this.index]);
|
||||
tempUint8[i] = this.uint8[this.index];
|
||||
this.index += 1;
|
||||
}
|
||||
return tempUint8;
|
||||
|
||||
@@ -26,6 +26,7 @@ class Flv extends Emitter {
|
||||
return {
|
||||
mediaElement: '',
|
||||
url: '',
|
||||
debug: false,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -53,15 +53,19 @@ export function bin2Boolean(bin) {
|
||||
return bin === 1;
|
||||
}
|
||||
|
||||
export function log(name, msg = '') {
|
||||
console.log(`[${name}] ${msg}`);
|
||||
}
|
||||
|
||||
export function readUint8(uint8) {
|
||||
let index = 0;
|
||||
return function read(length) {
|
||||
const result = [];
|
||||
const tempUint8 = new Uint8Array(length);
|
||||
for (let i = 0; i < length; i += 1) {
|
||||
result.push(uint8[index]);
|
||||
tempUint8[i] = uint8[index];
|
||||
index += 1;
|
||||
}
|
||||
read.index = index;
|
||||
return result;
|
||||
return tempUint8;
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user