trtc-electron-sdk
Version:
trtc electron sdk
115 lines (114 loc) • 4.53 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("../types");
const DevicePixelRatioObserver_1 = __importDefault(require("../../../base/DevicePixelRatioObserver"));
const utils_1 = require("../../../utils");
const logger_1 = __importDefault(require("../../../logger"));
class BaseStreamLayoutManager {
constructor(nativeStreamLayoutManager, context) {
this.logPrefix = "[BaseStreamLayoutManager]";
this.container = null;
this.displayArea = {
left: 0,
top: 0,
right: 0,
bottom: 0,
width: 0,
height: 0,
};
this.resizeObserver = null;
this.nativeStreamLayoutManager = nativeStreamLayoutManager;
this.layout = {
layoutMode: types_1.TRTCStreamLayoutMode.None,
};
this.context = context;
this.container = context.container;
this.onResize = (0, utils_1.debounce)(this.onResize.bind(this), 100);
if (this.container) {
this.setResizeObserver();
this.updateDisplayArea();
}
else {
logger_1.default.warn(`${this.logPrefix}constructor failed, no context.container.`);
}
this.onDevicePixelRatioChange = (0, utils_1.debounce)(this.onDevicePixelRatioChange.bind(this), 100);
DevicePixelRatioObserver_1.default.on('change', this.onDevicePixelRatioChange);
}
setLayout(layout) {
this.layout = layout;
this.refreshLayout();
}
getLayoutMode() {
return this.layout.layoutMode;
}
updateOptions(options) {
if (this.context && (this.context.mixingVideoSize.width !== options.width || this.context.mixingVideoSize.height !== options.height)) {
this.context.mixingVideoSize.width = options.width;
this.context.mixingVideoSize.height = options.height;
this.refreshLayout();
}
}
destroy() {
this.layout = {
layoutMode: types_1.TRTCStreamLayoutMode.None,
};
this.container = null;
this.context = null;
if (this.resizeObserver) {
this.resizeObserver.disconnect();
this.resizeObserver = null;
}
DevicePixelRatioObserver_1.default.off('change', this.onDevicePixelRatioChange);
}
refreshLayout() {
logger_1.default.warn(`${this.logPrefix}refreshLayout should be implemented by sub-class.`);
}
onResize(entries) {
logger_1.default.log(`${this.logPrefix}onResize entries:`, entries);
for (const entry of entries) {
if (entry.target === this.container) {
logger_1.default.debug(`${this.logPrefix}onResize:`, this.container.getBoundingClientRect());
this.updateDisplayArea();
this.refreshLayout();
break;
}
}
}
setResizeObserver() {
logger_1.default.debug(`${this.logPrefix}setResizeObserver container:`, this.container, ' resizeObserver:', this.resizeObserver);
if (this.resizeObserver) {
this.resizeObserver.disconnect();
}
if (this.container) {
this.resizeObserver = new ResizeObserver(this.onResize);
this.resizeObserver.observe(this.container);
}
else {
logger_1.default.warn(`${this.logPrefix}setResizeObserver failed, container is null.`);
}
}
updateDisplayArea() {
if (this.container) {
const containerRect = this.container.getBoundingClientRect();
this.displayArea = {
left: containerRect.left,
top: containerRect.top,
right: containerRect.right,
bottom: containerRect.bottom,
width: containerRect.width,
height: containerRect.height,
};
}
else {
logger_1.default.warn(`${this.logPrefix}updateDisplayArea failed, no container.`);
}
}
onDevicePixelRatioChange() {
logger_1.default.debug(`${this.logPrefix}onDevicePixelRatioChange:`, window.devicePixelRatio);
this.refreshLayout();
}
}
exports.default = BaseStreamLayoutManager;