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

+7 -7
View File
@@ -34,13 +34,13 @@
<script src="../uncompiled/artplayer-plugin-flv.js"></script>
<script src="./js/common.js"></script>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?b6c9b8bafbf5fe7225160101dafbbc7d";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
// var _hmt = _hmt || [];
// (function() {
// var hm = document.createElement("script");
// hm.src = "https://hm.baidu.com/hm.js?b6c9b8bafbf5fe7225160101dafbbc7d";
// var s = document.getElementsByTagName("script")[0];
// s.parentNode.insertBefore(hm, s);
// })();
</script>
</body>
+5 -1
View File
@@ -31,10 +31,14 @@
flv: function(video, url) {
var flv = new Flv({
mediaElement: video,
url: url,
url: file instanceof File ? file : url,
});
flv.load();
this.on('destroy', () => {
flv.destroy();
});
},
},
});
+93 -24
View File
@@ -275,10 +275,14 @@
throw new FlvError(msg);
}
}
function getTagBodySize(dataSize) {
return dataSize[0] * Math.pow(256, 2) + dataSize[1] * 256 + dataSize[2];
}
var utils = /*#__PURE__*/Object.freeze({
FlvError: FlvError,
errorHandle: errorHandle
errorHandle: errorHandle,
getTagBodySize: getTagBodySize
});
function checkSupport(options) {
@@ -291,6 +295,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.ReadableStream === 'function', "Unsupported 'ReadableStream' method");
}
var EventProxy =
@@ -390,10 +395,70 @@
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 contentLength = response.headers.get('content-length');
var 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: function start(controller) {
var reader = response.body.getReader();
(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', value);
controller.enqueue(value);
read();
}).catch(function (error) {
flv.emit('flvFetchError', error);
});
})();
},
cancel: function cancel() {
flv.emit('flvFetchCancel');
}
}));
});
}
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) {
var buffer = e.target.result;
var uint8 = new Uint8Array(buffer);
flv.emit('flvFetchEnd', uint8);
});
reader.readAsArrayBuffer(file);
}
var FlvParse =
/*#__PURE__*/
function () {
function FlvParse(flv) {
var _this = this;
classCallCheck(this, FlvParse);
this.flv = flv;
@@ -401,35 +466,38 @@
this.index = 0;
this.header = {};
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');
});
flv.on('flvFetchError', function (error) {
console.log('flvFetchError', error);
});
flv.on('flvFetching', function (value) {
console.log(value);
});
flv.on('flvFetchEnd', function (value) {
console.log('flvFetchEnd', value);
if (typeof flv.options.url === 'string') {
this.fromNetwork(flv.url);
if (value) {
_this.uint8 = value;
}
});
var url = flv.options.url;
if (typeof url === 'string') {
fetchStream(flv, url);
} else {
this.fromLocal(flv.url);
readFile(flv, url);
}
}
createClass(FlvParse, [{
key: "fromNetwork",
value: function fromNetwork(url) {
console.log(this.flv.options.url);
}
}, {
key: "fromLocal",
value: function fromLocal(file) {
var _this = this;
var proxy = this.flv.events.proxy;
var reader = new FileReader();
proxy(reader, 'load', function (e) {
var buffer = e.target.result;
_this.uint8 = new Uint8Array(buffer);
_this.parse();
});
reader.readAsArrayBuffer(file);
}
}, {
key: "parse",
value: function parse() {
this.header.signature = this.read(3);
@@ -485,6 +553,7 @@
_this = possibleConstructorReturn(this, getPrototypeOf(Flv).call(this));
_this.options = Object.assign({}, Flv.DEFAULTS, options);
console.log(_this.options);
checkSupport(_this.options);
id += 1;
_this.id = id;
+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.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] = [])).push({\n fn: callback,\n ctx: ctx\n });\n\n return this;\n },\n\n once: function (name, callback, ctx) {\n var self = Line 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/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
@@ -0,0 +1,46 @@
import { errorHandle } from '../utils';
export default function fetchStream(flv, url) {
flv.emit('flvFetchStart');
fetch(url).then(response => {
errorHandle(
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();
(function read() {
reader
.read()
.then(({ done, value }) => {
if (done) {
flv.emit('flvFetchEnd');
controller.close();
return;
}
flv.emit('flvFetching', value);
controller.enqueue(value);
read();
})
.catch(error => {
flv.emit('flvFetchError', error);
});
})();
},
cancel() {
flv.emit('flvFetchCancel');
},
}),
);
});
}
@@ -1,3 +1,6 @@
import fetchStream from './fetchStream';
import readFile from './readFile';
export default class FlvParse {
constructor(flv) {
this.flv = flv;
@@ -5,28 +8,40 @@ export default class FlvParse {
this.index = 0;
this.header = {};
this.tags = [];
if (typeof flv.options.url === 'string') {
this.fromNetwork(flv.url);
} else {
this.fromLocal(flv.url);
}
}
fromNetwork(url) {
console.log(this.flv.options.url);
}
fromLocal(file) {
const {
events: { proxy },
} = this.flv;
const reader = new FileReader();
proxy(reader, 'load', e => {
const buffer = e.target.result;
this.uint8 = new Uint8Array(buffer);
this.parse();
flv.on('flvFetchStart', () => {
console.log('flvFetchStart');
});
reader.readAsArrayBuffer(file);
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() {
@@ -0,0 +1,15 @@
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 => {
const buffer = e.target.result;
const uint8 = new Uint8Array(buffer);
flv.emit('flvFetchEnd', uint8);
});
reader.readAsArrayBuffer(file);
}
@@ -11,6 +11,7 @@ class Flv extends Emitter {
constructor(options) {
super();
this.options = Object.assign({}, Flv.DEFAULTS, options);
console.log(this.options);
checkSupport(this.options);
id += 1;
this.id = id;
@@ -19,4 +19,5 @@ export default function checkSupport(options) {
errorHandle(typeof window.Promise === 'function', "Unsupported 'Promise' method");
errorHandle(typeof window.fetch === 'function', "Unsupported 'fetch' method");
errorHandle(typeof window.ReadableStream === 'function', "Unsupported 'ReadableStream' method");
}
@@ -12,4 +12,8 @@ export function errorHandle(condition, msg) {
if (!condition) {
throw new FlvError(msg);
}
}
}
export function getTagBodySize(dataSize) {
return dataSize[0] * 256 ** 2 + dataSize[1] * 256 + dataSize[2];
}