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

80 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-08-21 07:37:10 -04:00
import {AbstractDeviceList, DeviceListEvents, IDevice, PermissionState} from "tc-shared/audio/recorder";
import {Registry} from "tc-shared/events";
import * as loader from "tc-loader";
import * as native from "tc-native/connection";
2020-08-21 07:37:10 -04:00
import {audio} from "tc-native/connection";
2021-01-08 16:04:27 -05:00
import {LogCategory, logTrace} from "tc-shared/log";
2020-08-21 07:37:10 -04:00
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) {
2020-08-21 07:37:10 -04:00
return this.cachedDevices;
}
2020-08-21 07:37:10 -04:00
2021-01-08 16:04:27 -05:00
const nativeDeviceList = audio.available_devices();
logTrace(LogCategory.AUDIO, tr("Native device list: %o"), nativeDeviceList);
this.cachedDevices = nativeDeviceList
2020-08-21 07:37:10 -04:00
.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();
await new Promise(resolve => {
native.audio.initialize(() => {
inputDeviceList.getDevices();
resolve();
});
});
2020-08-21 07:37:10 -04:00
},
priority: 80,
name: "initialize input devices"
});