TeaWeb/shared/js/connection/DummyVoiceConnection.ts

179 lines
4.8 KiB
TypeScript
Raw Normal View History

import {
2020-09-07 10:42:00 +00:00
AbstractVoiceConnection,
2020-09-01 12:16:43 +00:00
VoiceConnectionStatus, WhisperSessionInitializer
} from "../connection/VoiceConnection";
2021-04-05 21:05:44 +00:00
import {RecorderProfile, RecorderProfileOwner} from "../voice/RecorderProfile";
2020-11-17 10:26:52 +00:00
import {AbstractServerConnection, ConnectionStatistics} from "../connection/ConnectionBase";
import {VoiceClient} from "../voice/VoiceClient";
import {VoicePlayerEvents, VoicePlayerLatencySettings, VoicePlayerState} from "../voice/VoicePlayer";
import {WhisperSession, WhisperTarget} from "../voice/VoiceWhisper";
import {Registry} from "../events";
import { tr } from "tc-shared/i18n/localize";
2021-04-05 21:05:44 +00:00
import {AbstractInput} from "tc-shared/voice/RecorderBase";
import {crashOnThrow} from "tc-shared/proto";
class DummyVoiceClient implements VoiceClient {
2020-09-07 17:07:14 +00:00
readonly events: Registry<VoicePlayerEvents>;
2020-09-07 10:42:00 +00:00
private readonly clientId: number;
private volume: number;
constructor(clientId: number) {
2020-09-07 17:07:14 +00:00
this.events = new Registry<VoicePlayerEvents>();
2020-09-07 10:42:00 +00:00
this.clientId = clientId;
this.volume = 1;
}
2020-09-07 10:42:00 +00:00
getClientId(): number {
return this.clientId;
}
2020-09-07 10:42:00 +00:00
getVolume(): number {
return this.volume;
}
2020-09-07 10:42:00 +00:00
setVolume(volume: number) {
this.volume = volume;
}
2020-09-07 10:42:00 +00:00
getState(): VoicePlayerState {
return VoicePlayerState.STOPPED;
}
2020-09-07 10:42:00 +00:00
getLatencySettings(): Readonly<VoicePlayerLatencySettings> {
return { maxBufferTime: 0, minBufferTime: 0 };
}
2020-09-07 10:42:00 +00:00
setLatencySettings(settings) { }
flushBuffer() { }
abortReplay() { }
2020-09-07 17:07:14 +00:00
resetLatencySettings() {
}
}
export class DummyVoiceConnection extends AbstractVoiceConnection {
private recorder: RecorderProfile;
private voiceClients: DummyVoiceClient[] = [];
2021-04-05 21:05:44 +00:00
private triggerUnmountEvent: boolean;
constructor(connection: AbstractServerConnection) {
super(connection);
2021-04-05 21:05:44 +00:00
this.triggerUnmountEvent = true;
}
2020-08-26 10:33:53 +00:00
async acquireVoiceRecorder(recorder: RecorderProfile | undefined): Promise<void> {
2021-01-08 20:41:26 +00:00
if(this.recorder === recorder) {
return;
2021-01-08 20:41:26 +00:00
}
2021-01-08 20:41:26 +00:00
const oldRecorder = this.recorder;
2021-04-05 21:05:44 +00:00
await crashOnThrow(async () => {
this.triggerUnmountEvent = false;
await this.recorder?.ownRecorder(undefined);
this.triggerUnmountEvent = true;
this.recorder = recorder;
const voiceConnection = this;
await this.recorder?.ownRecorder(new class extends RecorderProfileOwner {
protected handleRecorderInput(input: AbstractInput) { }
protected handleUnmount() {
if(!voiceConnection.triggerUnmountEvent) {
return;
}
const oldRecorder = voiceConnection.recorder;
voiceConnection.recorder = undefined;
voiceConnection.events.fire("notify_recorder_changed", { oldRecorder: oldRecorder, newRecorder: undefined })
}
});
});
2021-01-08 20:41:26 +00:00
this.events.fire("notify_recorder_changed", {
oldRecorder,
newRecorder: recorder
});
}
2020-09-07 10:42:00 +00:00
availableVoiceClients(): VoiceClient[] {
return this.voiceClients;
}
2020-08-26 10:33:53 +00:00
decodingSupported(codec: number): boolean {
return false;
}
2020-08-26 10:33:53 +00:00
encodingSupported(codec: number): boolean {
return false;
}
getConnectionState(): VoiceConnectionStatus {
return VoiceConnectionStatus.ClientUnsupported;
}
2020-08-26 10:33:53 +00:00
getEncoderCodec(): number {
return 0;
}
2020-11-17 14:40:29 +00:00
registerVoiceClient(clientId: number): VoiceClient {
const client = new DummyVoiceClient(clientId);
this.voiceClients.push(client);
return client;
}
2020-08-26 10:33:53 +00:00
setEncoderCodec(codec: number) {}
2020-09-07 10:42:00 +00:00
async unregisterVoiceClient(client: VoiceClient): Promise<void> {
this.voiceClients.remove(client as any);
}
2020-08-26 10:33:53 +00:00
voiceRecorder(): RecorderProfile {
return this.recorder;
}
2020-09-01 12:16:43 +00:00
dropWhisperSession(session: WhisperSession) { }
getWhisperSessionInitializer(): WhisperSessionInitializer | undefined {
return undefined;
}
getWhisperSessions(): WhisperSession[] {
return [];
}
setWhisperSessionInitializer(initializer: WhisperSessionInitializer | undefined) { }
2020-09-07 17:07:14 +00:00
getWhisperTarget(): WhisperTarget | undefined {
return undefined;
}
startWhisper(target: WhisperTarget): Promise<void> {
return Promise.reject(tr("not supported"));
}
stopWhisper() { }
2020-09-16 19:50:21 +00:00
getFailedMessage(): string {
return "";
}
2020-09-24 13:51:22 +00:00
isReplayingVoice(): boolean {
return false;
}
stopAllVoiceReplays() { }
2020-11-17 10:26:52 +00:00
async getConnectionStats(): Promise<ConnectionStatistics> {
return {
bytesReceived: 0,
bytesSend: 0
}
}
getRetryTimestamp(): number | 0 {
return 0;
}
}