UNPKG

trtc-electron-sdk

Version:

trtc electron sdk

505 lines (504 loc) 20.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TRTCDeviceManager = void 0; const events_1 = require("events"); const trtc_define_1 = require("../../trtc_define"); const logger_1 = __importDefault(require("../../logger")); const NodeTRTCEngine = require('../../../build/Release/trtc_electron_sdk.node'); /** * 设备管理器 */ class TRTCDeviceManager { constructor(options) { this.logPrefix = '[TRTCDeviceManager]'; this.promiseStore = new Map(); this.eventEmitter = new events_1.EventEmitter(); this.eventHandler = this.eventHandler.bind(this); this.isIPCMode = options.isIPCMode; if (this.isIPCMode) { this.nodeDeviceManager = new NodeTRTCEngine.NodeRemoteDeviceManager(); this.nodeDeviceManager.setRemoteDeviceManagerCallback(this.eventHandler); } else { this.nodeDeviceManager = options.nodeTRTCCloud; } } destroy() { var _a, _b; this.nodeDeviceManager = null; (_a = this.promiseStore) === null || _a === void 0 ? void 0 : _a.forEach((value) => { value.forEach(({ reject }) => { reject(); }); }); (_b = this.promiseStore) === null || _b === void 0 ? void 0 : _b.clear(); this.promiseStore = undefined; this.eventEmitter = undefined; } /** * 获取设备列表 * * @param type {TRTCDeviceType} - 设备类型 * @returns {Array<TRTCDeviceInfo>} */ getDevicesList(type) { logger_1.default.debug(`${this.logPrefix}getDevicesList`, type); const deviceInfos = this.nodeDeviceManager.getDevicesList(type); if (type === trtc_define_1.TRTCDeviceType.TRTCDeviceTypeCamera) { deviceInfos === null || deviceInfos === void 0 ? void 0 : deviceInfos.forEach((item) => { try { if (item.deviceProperties) { const properties = JSON.parse(item.deviceProperties); item.deviceProperties = properties; } else { // 虚拟摄像头可能没有 deviceProperties 字段 item.deviceProperties = {}; } } catch (e) { logger_1.default.warn(`${this.logPrefix}camera device properties parse error.`, JSON.stringify(item)); item.deviceProperties = {}; } }); } return deviceInfos; } setCurrentDevice(type, deviceId) { logger_1.default.debug(`${this.logPrefix}setCurrentDevice`, type, deviceId); if (this.isIPCMode) { return new Promise((resolve, reject) => { const key = `setCurrentDevice-${type}`; this.addPromise(key, resolve, reject); this.nodeDeviceManager.setCurrentDevice(type, deviceId); }); } else { this.nodeDeviceManager.setCurrentDevice(type, deviceId); } } getCurrentDevice(type) { logger_1.default.debug(`${this.logPrefix}getCurrentDevice`, type); if (this.isIPCMode) { return new Promise((resolve, reject) => { const key = `getCurrentDevice-${type}`; this.addPromise(key, resolve, reject); this.nodeDeviceManager.getCurrentDevice(type); }); } else { return this.nodeDeviceManager.getCurrentDevice(type); } } setCurrentDeviceVolume(type, volume) { logger_1.default.debug(`${this.logPrefix}setCurrentDeviceVolume`, type, volume); if (this.isIPCMode) { return new Promise((resolve, reject) => { const key = `setCurrentDeviceVolume-${type}`; this.addPromise(key, resolve, reject); this.nodeDeviceManager.setCurrentDeviceVolume(type, volume); }); } else { this.nodeDeviceManager.setCurrentDeviceVolume(type, volume); } } getCurrentDeviceVolume(type) { logger_1.default.debug(`${this.logPrefix}getCurrentDeviceVolume`, type); if (this.isIPCMode) { return new Promise((resolve, reject) => { const key = `getCurrentDeviceVolume-${type}`; this.addPromise(key, resolve, reject); this.nodeDeviceManager.getCurrentDeviceVolume(type); }); } else { return this.nodeDeviceManager.getCurrentDeviceVolume(type); } } setCurrentDeviceMute(type, mute) { logger_1.default.debug(`${this.logPrefix}setCurrentDeviceMute`, type, mute); if (this.isIPCMode) { return new Promise((resolve, reject) => { const key = `setCurrentDeviceMute-${type}`; this.addPromise(key, resolve, reject); this.nodeDeviceManager.setCurrentDeviceMute(type, mute); }); } else { this.nodeDeviceManager.setCurrentDeviceMute(type, mute); } } getCurrentDeviceMute(type) { logger_1.default.debug(`${this.logPrefix}getCurrentDeviceMute`); if (this.isIPCMode) { return new Promise((resolve, reject) => { const key = `getCurrentDeviceMute-${type}`; this.addPromise(key, resolve, reject); this.nodeDeviceManager.getCurrentDeviceMute(type); }); } else { return this.nodeDeviceManager.getCurrentDeviceMute(type); } } enableFollowingDefaultAudioDevice(type, enable) { logger_1.default.debug(`${this.logPrefix}enableFollowingDefaultAudioDevice`, type, enable); if (this.isIPCMode) { return new Promise((resolve, reject) => { const key = `enableFollowingDefaultAudioDevice-${type}`; this.addPromise(key, resolve, reject); this.nodeDeviceManager.enableFollowingDefaultAudioDevice(type, enable); }); } else { this.nodeDeviceManager.enableFollowingDefaultAudioDevice(type, enable); } } startMicDeviceTest(interval, playback = false) { logger_1.default.debug(`${this.logPrefix}startMicDeviceTest`, interval, playback); if (this.isIPCMode) { return new Promise((resolve, reject) => { const key = `startMicDeviceTest`; this.addPromise(key, resolve, reject); this.nodeDeviceManager.startMicDeviceTest(interval, playback); }); } else { this.nodeDeviceManager.startMicDeviceTest(interval, playback); } } stopMicDeviceTest() { logger_1.default.debug(`${this.logPrefix}stopMicDeviceTest`); if (this.isIPCMode) { return new Promise((resolve, reject) => { const key = `stopMicDeviceTest`; this.addPromise(key, resolve, reject); this.nodeDeviceManager.stopMicDeviceTest(); }); } else { this.nodeDeviceManager.stopMicDeviceTest(); } } startSpeakerDeviceTest(filePath) { logger_1.default.debug(`${this.logPrefix}startSpeakerDeviceTest`, filePath); if (this.isIPCMode) { return new Promise((resolve, reject) => { const key = `startSpeakerDeviceTest`; this.addPromise(key, resolve, reject); this.nodeDeviceManager.startSpeakerDeviceTest(filePath); }); } else { this.nodeDeviceManager.startSpeakerDeviceTest(filePath); } } stopSpeakerDeviceTest() { logger_1.default.debug(`${this.logPrefix}stopSpeakerDeviceTest`); if (this.isIPCMode) { return new Promise((resolve, reject) => { const key = `stopSpeakerDeviceTest`; this.addPromise(key, resolve, reject); this.nodeDeviceManager.stopSpeakerDeviceTest(); }); } else { this.nodeDeviceManager.stopSpeakerDeviceTest(); } } setApplicationPlayVolume(volume) { logger_1.default.debug(`${this.logPrefix}setApplicationPlayVolume`, volume); if (this.isIPCMode) { return new Promise((resolve, reject) => { const key = `setApplicationPlayVolume`; this.addPromise(key, resolve, reject); this.nodeDeviceManager.setApplicationPlayVolume(volume); }); } else { this.nodeDeviceManager.setApplicationPlayVolume(volume); } } getApplicationPlayVolume() { logger_1.default.debug(`${this.logPrefix}getApplicationPlayVolume`); if (this.isIPCMode) { return new Promise((resolve, reject) => { const key = `getApplicationPlayVolume`; this.addPromise(key, resolve, reject); this.nodeDeviceManager.getApplicationPlayVolume(); }); } else { return this.nodeDeviceManager.getApplicationPlayVolume(); } } setApplicationMuteState(mute) { logger_1.default.debug(`${this.logPrefix}setApplicationMuteState`, mute); if (this.isIPCMode) { return new Promise((resolve, reject) => { const key = `setApplicationMuteState`; this.addPromise(key, resolve, reject); this.nodeDeviceManager.setApplicationMuteState(mute); }); } else { this.nodeDeviceManager.setApplicationMuteState(mute); } } getApplicationMuteState() { logger_1.default.debug(`${this.logPrefix}getApplicationMuteState`); if (this.isIPCMode) { return new Promise((resolve, reject) => { const key = `setApplicationMuteState`; this.addPromise(key, resolve, reject); this.nodeDeviceManager.getApplicationMuteState(); }); } else { return this.nodeDeviceManager.getApplicationMuteState(); } } setCameraCaptureParam(params) { logger_1.default.debug(`${this.logPrefix}setCameraCaptureParam:`, params); if (this.isIPCMode) { return this.nodeDeviceManager.setCameraCapturerParam(params); } else { this.nodeDeviceManager.setCameraCaptureParam(params); } } /** * 监听事件 * * @param event {String} - 事件名称 * @param func {Function} - 事件回调函数 */ on(event, func) { var _a; (_a = this.eventEmitter) === null || _a === void 0 ? void 0 : _a.on(event, func); } /** * 取消事件监听 * * @param event {String} - 事件名 * @param func {Function} - 事件回调函数 */ off(event, func) { var _a; (_a = this.eventEmitter) === null || _a === void 0 ? void 0 : _a.off(event, func); } addPromise(key, resolve, reject) { var _a, _b, _c; if (!((_a = this.promiseStore) === null || _a === void 0 ? void 0 : _a.has(key))) { (_b = this.promiseStore) === null || _b === void 0 ? void 0 : _b.set(key, []); } const storedPromises = (_c = this.promiseStore) === null || _c === void 0 ? void 0 : _c.get(key); storedPromises === null || storedPromises === void 0 ? void 0 : storedPromises.push({ resolve, reject }); } removePromise(key, value) { var _a, _b; const storedPromises = (_a = this.promiseStore) === null || _a === void 0 ? void 0 : _a.get(key); if (storedPromises) { storedPromises.forEach(({ resolve, reject }) => { resolve(value); }); (_b = this.promiseStore) === null || _b === void 0 ? void 0 : _b.delete(key); } } eventHandler(args) { logger_1.default.debug('TRTCDeviceManager event:', args); const key = args[0]; const data = args[1]; switch (key) { case "onDeviceChanged": this.onDeviceChange(data.deviceId, data.type, data.state); break; case "onSetCurrentDeviceFinished": this.onSetCurrentDeviceFinished(data.type, data.result); break; case "onGetCurrentDeviceFinished": this.onGetCurrentDeviceFinished(data.type, { deviceId: data.deviceId, deviceName: data.deviceName, deviceProperties: {} }); break; case "onSetCurrentDeviceVolumeFinished": this.onSetCurrentDeviceVolumeFinished(data.type, data.result); break; case "onGetCurrentDeviceVolumeFinished": this.onGetCurrentDeviceVolumeFinished(data.type, data.volume); break; case "onSetCurrentDeviceMuteFinished": this.onSetCurrentDeviceMuteFinished(data.type, data.result); break; case "onGetCurrentDeviceMuteFinished": this.onGetCurrentDeviceMuteFinished(data.type, data.muted); break; case "onEnableFollowingDefaultAudioDeviceFinished": this.onEnableFollowingDefaultAudioDeviceFinished(data.type, data.result); break; case "onStartCameraDeviceTestFinished": this.onStartCameraDeviceTestFinished(data.result); break; case "onStopCameraDeviceTestFinished": this.onStopCameraDeviceTestFinished(data.result); break; case "onStartMicDeviceTestFinished": this.onStartMicDeviceTestFinished(data.result); break; case "onStopMicDeviceTestFinished": this.onStopMicDeviceTestFinished(data.result); break; case "onStartSpeakerDeviceTestFinished": this.onStartSpeakerDeviceTestFinished(data.result); break; case "onStopSpeakerDeviceTestFinished": this.onStopSpeakerDeviceTestFinished(data.result); break; case "onSetApplicationPlayVolumeFinished": this.onSetApplicationPlayVolumeFinished(data.result); break; case "onGetApplicationPlayVolumeFinished": this.onGetApplicationPlayVolumeFinished(data.volume); break; case "onSetApplicationMuteStateFinished": this.onSetApplicationMuteStateFinished(data.result); break; case "onGetApplicationMuteStateFinished": this.onGetApplicationMuteStateFinished(data.muted); break; default: logger_1.default.warn("TRTCDeviceManager unsupported event type:", key); break; } } onDeviceChange(deviceId, type, state) { var _a; logger_1.default.debug(`${this.logPrefix}onDeviceChange`, deviceId, type, state); (_a = this.eventEmitter) === null || _a === void 0 ? void 0 : _a.emit('onDeviceChange', deviceId, type, state); } onSetCurrentDeviceFinished(type, result) { const key = `setCurrentDevice-${type}`; this.removePromise(key, result); } onGetCurrentDeviceFinished(type, deviceInfo) { const key = `getCurrentDevice-${type}`; this.removePromise(key, deviceInfo); } onSetCurrentDeviceVolumeFinished(type, result) { const key = `setCurrentDeviceVolume-${type}`; this.removePromise(key, result); } onGetCurrentDeviceVolumeFinished(type, volume) { const key = `getCurrentDeviceVolume-${type}`; this.removePromise(key, volume); } onSetCurrentDeviceMuteFinished(type, result) { const key = `setCurrentDeviceMute-${type}`; this.removePromise(key, result); } onGetCurrentDeviceMuteFinished(type, muted) { const key = `getCurrentDeviceMute-${type}`; this.removePromise(key, muted); } onEnableFollowingDefaultAudioDeviceFinished(type, result) { const key = `enableFollowingDefaultAudioDevice-${type}`; this.removePromise(key, result); } onStartCameraDeviceTestFinished(result) { const key = `startCameraDeviceTest`; this.removePromise(key, result); } onStopCameraDeviceTestFinished(result) { const key = `stopCameraDeviceTest`; this.removePromise(key, result); } onStartMicDeviceTestFinished(result) { const key = `startMicDeviceTest`; this.removePromise(key, result); } onStopMicDeviceTestFinished(result) { const key = `stopMicDeviceTest`; this.removePromise(key, result); } onStartSpeakerDeviceTestFinished(result) { const key = `startSpeakerDeviceTest`; this.removePromise(key, result); } onStopSpeakerDeviceTestFinished(result) { const key = `stopSpeakerDeviceTest`; this.removePromise(key, result); } onSetApplicationPlayVolumeFinished(result) { const key = `setApplicationPlayVolume`; this.removePromise(key, result); } onGetApplicationPlayVolumeFinished(volume) { const key = `getApplicationPlayVolume`; this.removePromise(key, volume); } onSetApplicationMuteStateFinished(result) { const key = `setApplicationMuteState`; this.removePromise(key, result); } onGetApplicationMuteStateFinished(muted) { const key = `getApplicationMuteState`; this.removePromise(key, muted); } // ****** 这一部分接口暴露不合理,暂时通过 TRTCMediaMixingManager 暴露给用户 **************/ startCameraDeviceTest(windowID, rect) { logger_1.default.debug(`${this.logPrefix}startCameraDeviceTest`, windowID, rect); let newWindowID = 0; if (windowID instanceof Uint8Array) { for (let i = windowID.length - 1; i >= 0; i--) { newWindowID = (newWindowID * 256) + windowID[i]; } } else { newWindowID = windowID; } return new Promise((resolve, reject) => { const key = `startCameraDeviceTest`; this.addPromise(key, resolve, reject); this.nodeDeviceManager.startCameraDeviceTest(newWindowID, rect); }); } stopCameraDeviceTest() { logger_1.default.debug(`${this.logPrefix}stopCameraDeviceTest`); return new Promise((resolve, reject) => { const key = `stopCameraDeviceTest`; this.addPromise(key, resolve, reject); this.nodeDeviceManager.stopCameraDeviceTest(); }); } setCameraTestRenderMirror(mirror) { logger_1.default.debug(`${this.logPrefix}setCameraTestRenderMirror`, mirror); this.nodeDeviceManager.setCameraTestRenderMirror(mirror); } setCameraTestDeviceId(cameraId) { logger_1.default.debug(`${this.logPrefix}setCameraTestDeviceId`, cameraId); this.nodeDeviceManager.setCameraTestDeviceId(cameraId); } setCameraTestResolution(width, height) { logger_1.default.debug(`${this.logPrefix}setCameraTestResolution`, width, height); this.nodeDeviceManager.setCameraTestResolution(width, height); } setCameraTestVideoPluginPath(path) { logger_1.default.debug(`${this.logPrefix}setCameraTestVideoPluginPath`, path); this.nodeDeviceManager.setCameraTestVideoPluginPath(path); } setCameraTestVideoPluginParameter(params) { logger_1.default.debug(`${this.logPrefix}setCameraTestVideoPluginParameter`, params); this.nodeDeviceManager.setCameraTestVideoPluginParameter(params); } } exports.TRTCDeviceManager = TRTCDeviceManager; exports.default = TRTCDeviceManager;