UNPKG

trtc-electron-sdk

Version:

trtc electron sdk

184 lines (183 loc) 6.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isSupportWebGL = exports.transferBGRA2RGBA = exports.generateUniqueId = exports.allocBuffer = exports.calcVideoStyle = exports.calcCanvasStyle = exports.calcScaleRate = exports.isNeedKeepWidthAndHeight = exports.config = exports.TRTCConfig = void 0; /** * 配置对象, 可用来打开 debug 模式 * * @example * import TRTCCloud from trtc-electron-sdk'; * const rtcCloud = TRTCCloud.getTRTCShareInstance(); * rtcCloud.getConfigObject().setDebugMode(true); */ class TRTCConfig { constructor() { this.debugMode = false; } setDebugMode(enable) { this.debugMode = enable; } getDebugMode() { return this.debugMode; } } exports.TRTCConfig = TRTCConfig; exports.config = new TRTCConfig(); function isNeedKeepWidthAndHeight(rotation, isNeedRotate) { // 需保持宽高不做变化 return rotation === 0 || rotation === 180 || !isNeedRotate; } exports.isNeedKeepWidthAndHeight = isNeedKeepWidthAndHeight; function calcScaleRate(vertical = false, contentMode = 0, contentWidth, contentHeight, containerWidth, containerHeight) { const containerRatio = containerWidth / containerHeight; const contentRatio = contentWidth / contentHeight; if (isNaN(containerRatio) || isNaN(contentRatio)) { return 1; } if (!contentMode) { if (vertical) { return containerRatio > contentRatio ? containerHeight / contentHeight : containerWidth / contentWidth; } else { return containerRatio < contentRatio ? containerHeight / contentHeight : containerWidth / contentWidth; } } else { if (vertical) { return containerRatio < contentRatio ? containerHeight / contentHeight : containerWidth / contentWidth; } else { return containerRatio > contentRatio ? containerHeight / contentHeight : containerWidth / contentWidth; } } } exports.calcScaleRate = calcScaleRate; function calcCanvasStyle(options = { contentWidth: 0, contentHeight: 0, rotation: 0, isNeedMirror: false, contentMode: 0, containerWidth: 0, containerHeight: 0, isNeedRotate: true }) { if ([0, 90, 180, 270].indexOf(options.rotation) === -1) { throw new Error('Invalid value for rotation. Only support 0, 90, 180, 270'); } const style = { width: 0, height: 0, transform: '', zoom: 1 }; const isKeepWidthHeight = isNeedKeepWidthAndHeight(options.rotation, options.isNeedRotate); if (isKeepWidthHeight) { style.width = options.contentWidth; style.height = options.contentHeight; } else { style.height = options.contentWidth; style.width = options.contentHeight; } const scale = calcScaleRate(!isKeepWidthHeight, options.contentMode, style.width, style.height, options.containerWidth, options.containerHeight); style.zoom = scale; if (options.isNeedMirror) { style.transform = `rotateY(180deg)`; } return style; } exports.calcCanvasStyle = calcCanvasStyle; function calcVideoStyle(options = { contentWidth: 0, contentHeight: 0, rotation: 0, isNeedMirror: false, contentMode: 0, containerWidth: 0, containerHeight: 0, isNeedRotate: true }) { if ([0, 90, 180, 270].indexOf(options.rotation) === -1) { throw new Error('Invalid value for rotation. Only support 0, 90, 180, 270'); } let width = 0; let height = 0; const isKeepWidthHeight = isNeedKeepWidthAndHeight(options.rotation, options.isNeedRotate); if (isKeepWidthHeight) { width = options.contentWidth; height = options.contentHeight; } else { height = options.contentWidth; width = options.contentHeight; } const scale = calcScaleRate(!isKeepWidthHeight, options.contentMode, width, height, options.containerWidth, options.containerHeight); const style = { width: `${width * scale}px`, height: `${height * scale}px`, }; if (options.isNeedMirror) { style.transform = 'rotateY(180deg)'; } return style; } exports.calcVideoStyle = calcVideoStyle; function allocBuffer(length) { if (length >= 1) { return Buffer.alloc(length); } return null; } exports.allocBuffer = allocBuffer; function generateUniqueId() { return Date.now() * 1000 + Math.floor(Math.random() * 1000); } exports.generateUniqueId = generateUniqueId; function transferBGRA2RGBA(data, width, height) { const bgraData = new Uint8ClampedArray((data.buffer || data).slice(0)); const rgbaData = new Uint8ClampedArray((data.buffer || data).slice(0)); // 将 BGRA8888 转成 RGBA8888 for (let i = 0; i < width * height; i++) { rgbaData[4 * i] = bgraData[4 * i + 2]; rgbaData[4 * i + 1] = bgraData[4 * i + 1]; rgbaData[4 * i + 2] = bgraData[4 * i]; rgbaData[4 * i + 3] = bgraData[4 * i + 3]; } return rgbaData; } exports.transferBGRA2RGBA = transferBGRA2RGBA; /** * 判断是否支持webGL * @returns {boolean} * @private */ function isSupportWebGL() { const canvas = document.createElement('canvas'); let gl; canvas.width = 1; canvas.height = 1; const options = { alpha: false, depth: false, stencil: false, antialias: false, preferLowPowerToHighPerformance: true }; try { gl = canvas.getContext('webgl', options) || canvas.getContext('experimental-webgl', options); } catch (e) { return false; } if (gl) { return true; } else { return false; } } exports.isSupportWebGL = isSupportWebGL;