TeaSpeak-Client/modules/renderer/audio/InputDeviceList.ts

70 lines
2.1 KiB
TypeScript

import {AbstractDeviceList, DeviceListEvents, IDevice, PermissionState} from "tc-shared/audio/recorder";
import {Registry} from "tc-shared/events";
import * as loader from "tc-loader";
import {audio} from "tc-native/connection";
interface NativeIDevice extends IDevice {
isDefault: boolean
}
class InputDeviceList extends AbstractDeviceList {
private cachedDevices: NativeIDevice[];
constructor() {
super();
this.setPermissionState("granted");
}
isRefreshAvailable(): boolean {
return false;
}
async refresh(): Promise<void> {
throw "not supported";
}
async requestPermissions(): Promise<PermissionState> {
return "granted";
}
getDefaultDeviceId(): string {
return this.getDevices().find(e => e.isDefault)?.deviceId || "default";
}
getDevices(): NativeIDevice[] {
if(this.cachedDevices)
return this.cachedDevices;
this.cachedDevices = audio.available_devices()
.filter(e => e.input_supported || e.input_default)
.filter(e => e.driver !== "Windows WDM-KS") /* If we're using WDM-KS and opening the microphone view, for some reason the channels get blocked an never release.... */
.map(device => {
return {
deviceId: device.device_id,
name: device.name,
driver: device.driver,
isDefault: device.input_default
}
});
this.setState("healthy");
return this.cachedDevices;
}
getEvents(): Registry<DeviceListEvents> {
return this.events;
}
}
export let inputDeviceList;
loader.register_task(loader.Stage.JAVASCRIPT_INITIALIZING, {
function: async () => {
inputDeviceList = new InputDeviceList();
inputDeviceList.getDevices();
},
priority: 80,
name: "initialize input devices"
});