UNPKG

@jxstjh/jhvideo

Version:

HTML5 jhvideo base on MPEG2-TS Stream Player

209 lines 7.79 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import { EventEmitter } from 'events'; var playType = { REALPLAY: 'realplay', PLAYBACK: 'playback', TALK: 'talk' }; var StreamWebsocket = /** @class */ (function () { function StreamWebsocket(url, playType) { if (playType === void 0) { playType = 'playback'; } this._ws = null; this._headBuffer = null; this._lastCmd = null; this._receivedSteamHead = false; this._requestData = { playUrl: '', playType: 'playback', startTime: 0, endTime: 0, }; this.emitter = new EventEmitter(); this._requestData.playUrl = url; this._requestData.playType = playType; } StreamWebsocket.prototype.on = function (event, listener) { this.emitter.addListener(event, listener); }; StreamWebsocket.prototype.off = function (event, listener) { this.emitter.removeListener(event, listener); }; StreamWebsocket.prototype.sendArrayBuffer = function (arrBuf) { this._ws.send(arrBuf); }; StreamWebsocket.prototype.open = function (url) { var _this = this; return new Promise(function (resolve, reject) { _this.emitter.on("cmd.".concat(_this._requestData.playType), function (code, mediaInfo) { if (code === 0) { resolve({ code: code, mediaInfo: mediaInfo, playType: _this._requestData.playType, head: _this._headBuffer }); } else { // resolve({}) reject(code); } }); var _url = url || _this._requestData.playUrl; _this._ws = new WebSocket(_url); _this._ws.binaryType = 'arraybuffer'; _this._ws.onopen = function (val) { // console.log('onopen',val) }; _this._ws.onerror = function (err) { // console.log('onerror',err) // this._bError = true }; _this._ws.onclose = function () { // console.log('close') // if (this._receivedSteamHead) { // this.emitter.emit('stream.interrupt', 'codes.ERR_INTERRUPT') // } _this.emitter.emit("cmd.".concat(_this._requestData.playType), '0x-000000'); }; _this._ws.onmessage = function (msg) { var data = msg.data; if (data instanceof ArrayBuffer) { _this._handleStream(data); } else if (typeof data === 'string') { _this._handleInteract(data); } }; }); }; /** * 码流处理 * @param {*} bufData * @returns */ StreamWebsocket.prototype._handleStream = function (bufData) { if (!this._receivedSteamHead) { this._headBuffer = bufData; this._receivedSteamHead = true; var isHead = true; if (this.emitter.eventNames().includes("cmd.".concat(this._requestData.playType))) { this.emitter.emit("cmd.".concat(this._requestData.playType), 0); } this.emitter.emit('stream.input', bufData, isHead); } else { this.emitter.emit('stream.input', bufData); } }; /** * 交互信息处理 * @param {*} strData */ StreamWebsocket.prototype._handleInteract = function (strData) { var jsonData = JSON.parse(strData); if (!this._lastCmd) { // 开流 var errorCode = jsonData.errorCode, errorMsg = jsonData.errorMsg; if (parseInt(errorCode) === 0) { // 安全认证成功,与服务端建立连接 if (this._requestData.playType === playType.TALK) { this.emitter.emit("cmd.".concat(this._requestData.playType), errorCode, jsonData.audioInfo); } else { this.emitter.emit("cmd.".concat(this._requestData.playType), errorCode); } } else { var errorCodeHex = '0x0' + errorCode.toString(16); this.emitter.emit("cmd.".concat(this._requestData.playType), errorCodeHex, errorMsg); } return; } if (jsonData.hasOwnProperty('errorCode')) { // 交互消息 var errorCode = jsonData.errorCode, errorMsg = jsonData.errorMsg; if (parseInt(errorCode) === 0) { this.emitter.emit("cmd.".concat(this._lastCmd.cmd), parseInt(errorCode)); } else { var errorCodeHex = '0x0' + errorCode.toString(16); this.emitter.emit("cmd.".concat(this._lastCmd.cmd), errorCodeHex, errorMsg); } } else { // 服务端主动消息 if (!jsonData.hasOwnProperty('cmd')) { var extra = { url: this._requestData.playUrl }; var cmd = this._requestData.playType; if (this._requestData.playType === playType.PLAYBACK) { extra.startTime = this._requestData.startTime; extra.endTime = this._requestData.endTime; } this.sendCmd(cmd, extra).catch(function (err) { console.error(err); }); } else { var cmd = jsonData.cmd; if (cmd === 'end') { this.emitter.emit('stream.end'); this.close(); } else if (cmd === 'newStreamFlag') { } } } }; StreamWebsocket.prototype.sendCmd = function (cmd, extra) { var _this = this; if (extra === void 0) { extra = {}; } return new Promise(function (resolve, reject) { if (!_this._ws && _this._ws.readyState !== WebSocket.OPEN) { return reject('意外的状态'); } // 设置消息发送监听 _this.emitter.once("cmd.".concat(cmd), function (code) { if (code === 0) { resolve('codes.OK'); } else { reject(code); } }); // 发送消息 var cmdBody = __assign(__assign({ cmd: cmd }, extra), { sequence: !_this._lastCmd ? 0 : _this._lastCmd.sequence + 1 }); _this._ws.send(JSON.stringify(cmdBody)); _this._lastCmd = cmdBody; }); }; StreamWebsocket.prototype.destroy = function () { this.close(); if (this.emitter) { this.emitter.removeAllListeners(); this.emitter = null; } }; StreamWebsocket.prototype.close = function () { if (!this._ws) return; var ws = this._ws; ws.close(1000); ws.onmessage = null; ws.onclose = null; ws.onerror = null; ws.onopen = null; this._ws = null; this._receivedSteamHead = false; this._lastCmd = null; }; return StreamWebsocket; }()); export default StreamWebsocket; //# sourceMappingURL=ws-loader.js.map