add creatWorker

This commit is contained in:
Harvey Zack committed 2018-12-19 17:36:28 +08:00
1 parent 6cc89edcd9
commit 39f8c157ac
9 files changed
+222 -56

No files matched your search

+122 -29
View File
@@ -275,14 +275,17 @@
throw new FlvError(msg);
}
}
function getTagBodySize(dataSize) {
return dataSize[0] * Math.pow(256, 2) + dataSize[1] * 256 + dataSize[2];
function mergeTypedArrays(a, b) {
var c = new a.constructor(a.length + b.length);
c.set(a);
c.set(b, a.length);
return c;
}
var utils = /*#__PURE__*/Object.freeze({
FlvError: FlvError,
errorHandle: errorHandle,
getTagBodySize: getTagBodySize
mergeTypedArrays: mergeTypedArrays
});
function checkSupport(options) {
@@ -295,6 +298,7 @@
errorHandle(window.MediaSource && window.MediaSource.isTypeSupported(MP4H264MimeCodec) && (canPlay === 'probably' || canPlay === 'maybe'), "Unsupported MIME type or codec: ".concat(MP4H264MimeCodec));
errorHandle(typeof window.Promise === 'function', "Unsupported 'Promise' method");
errorHandle(typeof window.fetch === 'function', "Unsupported 'fetch' method");
errorHandle(typeof window.Worker === 'function', "Unsupported 'Worker' method");
errorHandle(typeof window.ReadableStream === 'function', "Unsupported 'ReadableStream' method");
}
@@ -329,6 +333,86 @@
return EventProxy;
}();
var CreatWorker =
/*#__PURE__*/
function () {
function CreatWorker() {
classCallCheck(this, CreatWorker);
this.workers = new Map();
}
createClass(CreatWorker, [{
key: "add",
value: function add(name, fn) {
if (!this.workers.has(name)) {
this.workers.set(name, CreatWorker.create(fn));
}
}
}, {
key: "run",
value: function run(name) {
var worker = this.workers.get(name);
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
return worker.post(args);
}
}, {
key: "kill",
value: function kill(name) {
var worker = this.workers.get(name);
worker.kill();
}
}, {
key: "killAll",
value: function killAll() {
this.workers.forEach(function (worker) {
worker.kill();
});
}
}], [{
key: "fnToStr",
value: function fnToStr(fn) {
return "\n self.onmessage = event => {\n return self.postMessage((".concat(fn, ").apply(null, event.data));\n }\n ");
}
}, {
key: "create",
value: function create(fn) {
var blob = new Blob([CreatWorker.fnToStr(fn)], {
type: 'application/javascript'
});
var objectURL = window.URL.createObjectURL(blob);
var worker = new Worker(objectURL);
worker.kill = function () {
URL.revokeObjectURL(objectURL);
worker.terminate();
};
worker.post = function (args) {
return new Promise(function (resolve, reject) {
worker.onmessage = function (event) {
resolve(event.data);
};
worker.onerror = function (error) {
reject(error);
};
worker.postMessage(args);
});
};
return worker;
}
}]);
return CreatWorker;
}();
var config = {
mediaSource: {
propertys: ['activeSourceBuffers', 'duration', 'readyState', 'sourceBuffers'],
@@ -399,17 +483,17 @@
flv.emit('flvFetchStart');
fetch(url).then(function (response) {
errorHandle(response.ok && response.status >= 200 && response.status <= 299, "".concat(response.status, " ").concat(response.statusText));
var contentLength = response.headers.get('content-length');
var contentType = response.headers.get('content-type'); // errorHandle(contentLength, 'Content-Length response header unavailable');
var contentType = response.headers.get('content-type');
errorHandle(contentType.includes('x-flv'), 'The resource does not seem to be a flv file');
flv.emit('flvFetchInfo', {
type: contentType,
length: contentLength
});
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) {
@@ -439,10 +523,6 @@
function readFile(flv, file) {
flv.emit('flvFetchStart');
flv.emit('flvFetchInfo', {
type: file.type,
length: file.size
});
var proxy = flv.events.proxy;
var reader = new FileReader();
proxy(reader, 'load', function (e) {
@@ -462,16 +542,13 @@
classCallCheck(this, FlvParse);
this.flv = flv;
this.uint8 = [];
this.uint8 = new Int8Array(0);
this.index = 0;
this.header = {};
this.header = null;
this.tags = [];
flv.on('flvFetchStart', function () {
console.log('flvFetchStart');
});
flv.on('flvFetchInfo', function (info) {
console.log('flvFetchInfo', info);
});
flv.on('flvFetchCancel', function () {
console.log('flvFetchCancel');
});
@@ -479,13 +556,19 @@
console.log('flvFetchError', error);
});
flv.on('flvFetching', function (value) {
console.log(value);
_this.uint8 = mergeTypedArrays(_this.uint8, value);
console.log(_this.uint8.length);
_this.parseHeader();
});
flv.on('flvFetchEnd', function (value) {
console.log('flvFetchEnd', value);
console.log('flvFetchEnd');
if (value) {
_this.uint8 = value;
_this.parseHeader(); // this.parseTags();
}
});
var url = flv.options.url;
@@ -498,14 +581,22 @@
}
createClass(FlvParse, [{
key: "parse",
value: function 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);
key: "parseHeader",
value: function parseHeader() {
if (this.uint8.length >= 13 && !this.header) {
var header = {};
header.signature = this.read(3);
header.version = this.read(1);
header.flags = this.read(1);
header.headersize = this.read(4);
this.header = header;
this.read(4);
console.log(this.header);
}
}
}, {
key: "parseTags",
value: function parseTags() {
while (this.index < this.uint8.length) {
var tag = {};
tag.tagType = this.read(1);
@@ -565,6 +656,7 @@
key: "load",
value: function load() {
this.events = new EventProxy(this);
this.workers = new CreatWorker(this);
this.mediaSource = new CreatMediaSource(this);
this.flvData = new FlvParse(this);
}
@@ -572,6 +664,7 @@
key: "destroy",
value: function destroy() {
this.events.destroy();
this.workers.killAll();
Flv.instances.splice(Flv.instances.indexOf(this), 1);
this.emit('destroy');
}
+1 -1
View File
@@ -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/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/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","../../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 expression must either be null or a function\");\n }\n\n subClass.prototype = Object.create(superClass && superClass.prototype, {\n constructor: {\n value: subClass,\n writable: true,\n configurable: true\n }\n });\n if (superClass) setPrototypeOf(subClass, superClass);\n}\n\nmodule.exports = _inherits;","function E () {\n // Keep this empty so it's easier to inherit from\n // (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3)\n}\n\nE.prototype = {\n on: function (name, callback, ctx) {\n var e = this.e || (this.e = {});\n\n (e[name] || (e[name] = [])).pushLine 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/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","../../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 expression must either be null or a function\");\n }\n\n subClass.prototype = Object.create(superClass && superClass.prototype, {\n constructor: {\n value: subClass,\n writable: true,\n configurable: true\n }\n });\n if (superClass) setPrototypeOf(subClass, superClass);\n}\n\nmodule.exports = _inherits;","function E () {\n // Keep this empty so it's easier to inherit from\n // (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3)\n}\n\nE.prototype = {\n on: function (name, callback, ctx) {\n var e = this.e Line truncated
@@ -0,0 +1,58 @@
export default class CreatWorker {
constructor() {
this.workers = new Map();
}
static fnToStr(fn) {
return `
self.onmessage = event => {
return self.postMessage((${fn}).apply(null, event.data));
}
`;
}
static create(fn) {
const blob = new Blob([CreatWorker.fnToStr(fn)], {
type: 'application/javascript',
});
const objectURL = window.URL.createObjectURL(blob);
const worker = new Worker(objectURL);
worker.kill = () => {
URL.revokeObjectURL(objectURL);
worker.terminate();
};
worker.post = args =>
new Promise((resolve, reject) => {
worker.onmessage = event => {
resolve(event.data);
};
worker.onerror = error => {
reject(error);
};
worker.postMessage(args);
});
return worker;
}
add(name, fn) {
if (!this.workers.has(name)) {
this.workers.set(name, CreatWorker.create(fn));
}
}
run(name, ...args) {
const worker = this.workers.get(name);
return worker.post(args);
}
kill(name) {
const worker = this.workers.get(name);
worker.kill();
}
killAll() {
this.workers.forEach(worker => {
worker.kill();
});
}
}
@@ -7,18 +7,21 @@ export default function fetchStream(flv, url) {
response.ok && response.status >= 200 && response.status <= 299,
`${response.status} ${response.statusText}`,
);
const contentLength = response.headers.get('content-length');
const contentType = response.headers.get('content-type');
// errorHandle(contentLength, 'Content-Length response header unavailable');
errorHandle(contentType.includes('x-flv'), 'The resource does not seem to be a flv file');
flv.emit('flvFetchInfo', {
type: contentType,
length: contentLength,
});
return new Response(
new ReadableStream({
start(controller) {
const reader = response.body.getReader();
flv.on('destroy', () => {
reader.cancel();
});
flv.on('readerCancel', () => {
reader.cancel();
});
(function read() {
reader
.read()
@@ -1,22 +1,19 @@
import fetchStream from './fetchStream';
import readFile from './readFile';
import { mergeTypedArrays } from '../utils';
export default class FlvParse {
constructor(flv) {
this.flv = flv;
this.uint8 = [];
this.uint8 = new Int8Array(0);
this.index = 0;
this.header = {};
this.header = null;
this.tags = [];
flv.on('flvFetchStart', () => {
console.log('flvFetchStart');
});
flv.on('flvFetchInfo', info => {
console.log('flvFetchInfo', info);
});
flv.on('flvFetchCancel', () => {
console.log('flvFetchCancel');
});
@@ -26,13 +23,17 @@ export default class FlvParse {
});
flv.on('flvFetching', value => {
console.log(value);
this.uint8 = mergeTypedArrays(this.uint8, value);
console.log(this.uint8.length);
this.parseHeader();
});
flv.on('flvFetchEnd', value => {
console.log('flvFetchEnd', value);
console.log('flvFetchEnd');
if (value) {
this.uint8 = value;
this.parseHeader();
// this.parseTags();
}
});
@@ -44,12 +45,20 @@ export default class FlvParse {
}
}
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);
parseHeader() {
if (this.uint8.length >= 13 && !this.header) {
const header = {};
header.signature = this.read(3);
header.version = this.read(1);
header.flags = this.read(1);
header.headersize = this.read(4);
this.header = header;
this.read(4);
console.log(this.header);
}
}
parseTags() {
while (this.index < this.uint8.length) {
const tag = {};
tag.tagType = this.read(1);
@@ -1,9 +1,5 @@
export default function readFile(flv, file) {
flv.emit('flvFetchStart');
flv.emit('flvFetchInfo', {
type: file.type,
length: file.size,
});
const { proxy } = flv.events;
const reader = new FileReader();
proxy(reader, 'load', e => {
@@ -1,6 +1,7 @@
import Emitter from 'tiny-emitter';
import checkSupport from './utils/checkSupport';
import EventProxy from './eventProxy';
import CreatWorker from './creatWorker';
import CreatMediaSource from './creatMediaSource';
import FlvParse from './flvParse';
import * as utils from './utils';
@@ -39,12 +40,14 @@ class Flv extends Emitter {
load() {
this.events = new EventProxy(this);
this.workers = new CreatWorker(this);
this.mediaSource = new CreatMediaSource(this);
this.flvData = new FlvParse(this);
}
destroy() {
this.events.destroy();
this.workers.killAll();
Flv.instances.splice(Flv.instances.indexOf(this), 1);
this.emit('destroy');
}
@@ -19,5 +19,6 @@ export default function checkSupport(options) {
errorHandle(typeof window.Promise === 'function', "Unsupported 'Promise' method");
errorHandle(typeof window.fetch === 'function', "Unsupported 'fetch' method");
errorHandle(typeof window.Worker === 'function', "Unsupported 'Worker' method");
errorHandle(typeof window.ReadableStream === 'function', "Unsupported 'ReadableStream' method");
}
@@ -14,6 +14,9 @@ export function errorHandle(condition, msg) {
}
}
export function getTagBodySize(dataSize) {
return dataSize[0] * 256 ** 2 + dataSize[1] * 256 + dataSize[2];
export function mergeTypedArrays(a, b) {
const c = new a.constructor(a.length + b.length);
c.set(a);
c.set(b, a.length);
return c;
}