UNPKG

@tencentcloud/roomkit-web-vue3

Version:

<h1 align="center"> TUIRoomKit</h1> Conference (TUIRoomKit) is a product suitable for multi-person audio and video conversation scenarios such as business meetings, webinars, and online education. By integrating this product, you can add room management,

203 lines (202 loc) 7.17 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { TUIMediaDeviceType, TUIRoomDeviceMangerEvents } from "@tencentcloud/tuiroom-engine-js"; import { MediaDeviceState } from "../../type/MediaDeviceSetting.mjs"; import useRoomEngine from "../../../hooks/useRoomEngine.mjs"; import useDeviceManager from "../../../hooks/useDeviceManager.mjs"; import { isElectron, isWeChat } from "../../../utils/environment.mjs"; import MessageBox from "../../../components/common/base/MessageBox/index.mjs"; import { TUIToast, TOAST_TYPE } from "@tencentcloud/uikit-base-component-vue3"; import i18n from "../../../locales/index.mjs"; import { MESSAGE_DURATION, WARNING_MESSAGE } from "../../../constants/message.mjs"; import "../../../services/main.mjs"; import { roomService } from "../../../services/roomService.mjs"; import "mitt"; import "../../../services/manager/roomActionManager.mjs"; import "@tencentcloud/tui-core"; import useAudioDeviceManagerState, { testAudioVolume } from "./audioDeviceManagerState.mjs"; const { microphoneState, currentMicrophoneDevice, currentMicrophoneId, currentSpeakerDevice, currentSpeakerId, isMicrophoneTesting, isSpeakerTesting, currentAudioRoute } = useAudioDeviceManagerState(); const roomEngine = useRoomEngine(); const deviceManager = useDeviceManager(); const { t } = i18n.global; const _Microphone = class _Microphone { constructor() { if (!_Microphone.instance) { _Microphone.instance = this; } return _Microphone.instance; } onTestMicVolume(options) { testAudioVolume.value = options.volume; } async startMicDeviceTest(options) { var _a, _b; await ((_a = roomEngine.instance) == null ? void 0 : _a.startMicDeviceTest(options)); isMicrophoneTesting.value = true; (_b = deviceManager.instance) == null ? void 0 : _b.on( TUIRoomDeviceMangerEvents.onTestMicVolume, this.onTestMicVolume ); } async stopMicDeviceTest() { var _a, _b; await ((_a = roomEngine.instance) == null ? void 0 : _a.stopMicDeviceTest()); isMicrophoneTesting.value = false; (_b = deviceManager.instance) == null ? void 0 : _b.off( TUIRoomDeviceMangerEvents.onTestMicVolume, this.onTestMicVolume ); } async checkMicrophonePermission() { const audioStream = await navigator.mediaDevices.getUserMedia({ audio: { deviceId: currentMicrophoneId.value } }); audioStream.getAudioTracks()[0].stop(); } async openLocalMicrophone() { var _a; if (microphoneState.value === MediaDeviceState.NotSupportCapture && !isWeChat) { MessageBox({ title: t("Note"), message: t("The current browser does not support capturing audio"), confirmButtonText: t("Sure") }); return; } if (microphoneState.value === MediaDeviceState.NoDeviceDetected && !isWeChat) { MessageBox({ title: t("Note"), message: t("Microphone not detected on current device"), confirmButtonText: t("Sure") }); return; } try { await this.checkMicrophonePermission(); } catch (error) { MessageBox({ title: t("Note"), message: t( "The current device is not authorized, please allow access to the device permissions", { deviceType: t("microphone") } ), confirmButtonText: t("Sure") }); return; } if (microphoneState.value === MediaDeviceState.DeviceOffAndDisabled) { let warningMessage = ""; if (roomService.roomStore.isAudience) { warningMessage = WARNING_MESSAGE.UNMUTE_LOCAL_MIC_FAIL_AUDIENCE; } else if (roomService.roomStore.isMicrophoneDisableForAllUser) { warningMessage = WARNING_MESSAGE.UNMUTE_LOCAL_MIC_FAIL_MUTE_ALL; } TUIToast({ type: TOAST_TYPE.WARNING, message: t(warningMessage), duration: MESSAGE_DURATION.NORMAL }); return; } try { await ((_a = roomEngine.instance) == null ? void 0 : _a.openLocalMicrophone()); } catch (error) { if (error.code === 0 && error.message === "you have already mute the audio") { return; } throw error; } } async closeLocalMicrophone() { var _a; await ((_a = roomEngine.instance) == null ? void 0 : _a.closeLocalMicrophone()); } async muteLocalAudio() { var _a; await ((_a = roomEngine.instance) == null ? void 0 : _a.muteLocalAudio()); } async unmuteLocalAudio() { var _a; await ((_a = roomEngine.instance) == null ? void 0 : _a.unmuteLocalAudio()); } async setCurrentDevice(options) { var _a, _b; const { deviceId } = options; await ((_a = deviceManager.instance) == null ? void 0 : _a.setCurrentDevice({ type: TUIMediaDeviceType.kMediaDeviceTypeAudioInput, deviceId })); currentMicrophoneDevice.value = ((_b = deviceManager.instance) == null ? void 0 : _b.getCurrentDevice({ type: TUIMediaDeviceType.kMediaDeviceTypeAudioInput })) || null; } }; __publicField(_Microphone, "instance"); let Microphone = _Microphone; class Speaker { constructor() { __publicField(this, "speakerTestPlayer", null); } async startSpeakerDeviceTest(options) { var _a, _b; if (isElectron) { (_a = deviceManager.instance) == null ? void 0 : _a.startSpeakerDeviceTest(options); return; } this.speakerTestPlayer = document == null ? void 0 : document.createElement("audio"); if (this.speakerTestPlayer && typeof this.speakerTestPlayer.loop !== "undefined") { this.speakerTestPlayer.loop = true; } await ((_b = this.speakerTestPlayer) == null ? void 0 : _b.setSinkId(currentSpeakerId.value)); this.speakerTestPlayer.src = options.filePath; this.speakerTestPlayer.play(); isSpeakerTesting.value = true; } async stopSpeakerDeviceTest() { var _a; if (isElectron) { (_a = deviceManager.instance) == null ? void 0 : _a.stopSpeakerDeviceTest(); return; } if (this.speakerTestPlayer) { this.speakerTestPlayer.pause(); this.speakerTestPlayer.currentTime = 0; } isSpeakerTesting.value = false; } async setCurrentDevice(options) { var _a, _b; const { deviceId } = options; await ((_a = deviceManager.instance) == null ? void 0 : _a.setCurrentDevice({ type: TUIMediaDeviceType.kMediaDeviceTypeAudioOutput, deviceId })); currentSpeakerDevice.value = ((_b = deviceManager.instance) == null ? void 0 : _b.getCurrentDevice({ type: TUIMediaDeviceType.kMediaDeviceTypeAudioOutput })) || null; } async setAudioRoute(options) { var _a; await ((_a = deviceManager.instance) == null ? void 0 : _a.setAudioRoute(options)); currentAudioRoute.value = options.route; } } function useAudioDeviceManager() { return { microphone: new Microphone(), speaker: new Speaker() }; } export { useAudioDeviceManager as default };