TeaSpeak-Client/modules/renderer/connection/VoiceConnection.ts

343 lines
11 KiB
TypeScript
Raw Normal View History

2020-08-21 07:37:10 -04:00
import {
AbstractVoiceConnection,
2020-09-24 16:06:52 -04:00
VoiceConnectionStatus,
WhisperSessionInitializer
2020-08-21 07:37:10 -04:00
} from "tc-shared/connection/VoiceConnection";
2020-09-24 16:06:52 -04:00
import {RecorderProfile} from "tc-shared/voice/RecorderProfile";
2020-12-02 15:00:51 -05:00
import {NativeServerConnection, NativeVoiceClient, NativeVoiceConnection, PlayerState} from "tc-native/connection";
2020-09-24 16:06:52 -04:00
import {ServerConnection} from "./ServerConnection";
import {VoiceClient} from "tc-shared/voice/VoiceClient";
import {WhisperSession, WhisperTarget} from "tc-shared/voice/VoiceWhisper";
import {NativeInput} from "../audio/AudioRecorder";
2020-09-24 16:06:52 -04:00
import {ConnectionState} from "tc-shared/ConnectionHandler";
import {VoicePlayerEvents, VoicePlayerLatencySettings, VoicePlayerState} from "tc-shared/voice/VoicePlayer";
import {Registry} from "tc-shared/events";
2020-10-01 04:56:22 -04:00
import {LogCategory, logDebug, logInfo, logWarn} from "tc-shared/log";
2020-09-24 16:06:52 -04:00
import {tr} from "tc-shared/i18n/localize";
2020-11-29 15:06:57 -05:00
import {ConnectionStatistics} from "tc-shared/connection/ConnectionBase";
2020-09-24 16:06:52 -04:00
export class NativeVoiceConnectionWrapper extends AbstractVoiceConnection {
private readonly serverConnectionStateChangedListener;
private readonly native: NativeVoiceConnection;
2020-09-24 16:06:52 -04:00
private localAudioStarted = false;
private connectionState: VoiceConnectionStatus;
private currentRecorder: RecorderProfile;
private registeredVoiceClients: {[key: number]: NativeVoiceClientWrapper} = {};
private currentlyReplayingAudio = false;
private readonly voiceClientStateChangedEventListener;
constructor(connection: ServerConnection, voice: NativeVoiceConnection) {
super(connection);
2020-09-24 16:06:52 -04:00
this.native = voice;
this.serverConnectionStateChangedListener = () => {
if(this.connection.getConnectionState() === ConnectionState.CONNECTED) {
this.setConnectionState(VoiceConnectionStatus.Connected);
} else {
this.setConnectionState(VoiceConnectionStatus.Disconnected);
}
}
this.connection.events.on("notify_connection_state_changed", this.serverConnectionStateChangedListener);
this.connectionState = VoiceConnectionStatus.Disconnected;
this.voiceClientStateChangedEventListener = this.handleVoiceClientStateChange.bind(this);
}
destroy() {
this.connection.events.off("notify_connection_state_changed", this.serverConnectionStateChangedListener);
}
getConnectionState(): VoiceConnectionStatus {
return this.connectionState;
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
getFailedMessage(): string {
/* the native voice connection can't fail */
return "this message should never appear";
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
private setConnectionState(state: VoiceConnectionStatus) {
if(this.connectionState === state) {
return;
2020-09-24 16:06:52 -04:00
}
const oldState = this.connectionState;
this.connectionState = state;
this.events.fire("notify_connection_status_changed", { oldStatus: oldState, newStatus: state });
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
encodingSupported(codec: number): boolean {
return this.native.encoding_supported(codec);
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
decodingSupported(codec: number): boolean {
return this.native.decoding_supported(codec);
}
async acquireVoiceRecorder(recorder: RecorderProfile | undefined): Promise<void> {
if(this.currentRecorder === recorder) {
return;
}
if(this.currentRecorder) {
2020-10-01 04:56:22 -04:00
this.currentRecorder.callback_unmount = undefined;
this.native.set_audio_source(undefined);
this.handleVoiceEndEvent();
2020-09-24 16:06:52 -04:00
await this.currentRecorder.unmount();
2020-10-01 04:56:22 -04:00
this.currentRecorder = undefined;
}
2019-10-25 19:51:40 -04:00
2020-10-01 04:56:22 -04:00
await recorder?.unmount();
2021-01-08 16:04:27 -05:00
const oldRecorder = recorder;
2020-09-24 16:06:52 -04:00
this.currentRecorder = recorder;
try {
if(recorder) {
if(!(recorder.input instanceof NativeInput)) {
this.currentRecorder = undefined;
throw "Recorder input must be an instance of NativeInput!";
}
recorder.current_handler = this.connection.client;
recorder.callback_unmount = () => {
2020-10-01 04:56:22 -04:00
logDebug(LogCategory.VOICE, tr("Lost voice recorder..."));
this.acquireVoiceRecorder(undefined);
2020-09-24 16:06:52 -04:00
};
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
recorder.callback_start = this.handleVoiceStartEvent.bind(this);
recorder.callback_stop = this.handleVoiceEndEvent.bind(this);
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
this.native.set_audio_source(recorder.input.getNativeConsumer());
}
} catch(error) {
this.currentRecorder = undefined;
throw error;
}
2021-01-08 16:04:27 -05:00
this.events.fire("notify_recorder_changed", {
oldRecorder,
newRecorder: recorder
});
2020-09-24 16:06:52 -04:00
}
voiceRecorder(): RecorderProfile {
return this.currentRecorder;
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
getEncoderCodec(): number {
return this.native.get_encoder_codec();
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
setEncoderCodec(codec: number) {
this.native.set_encoder_codec(codec);
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
isReplayingVoice(): boolean {
return this.currentlyReplayingAudio;
}
private setReplayingVoice(status: boolean) {
if(status === this.currentlyReplayingAudio) {
return;
}
this.currentlyReplayingAudio = status;
this.events.fire("notify_voice_replay_state_change", { replaying: status });
}
private handleVoiceClientStateChange() {
this.setReplayingVoice(this.availableVoiceClients().findIndex(client => client.getState() === VoicePlayerState.PLAYING || client.getState() === VoicePlayerState.BUFFERING) !== -1);
}
private handleVoiceStartEvent() {
const chandler = this.connection.client;
2020-09-24 16:06:52 -04:00
if(chandler.isMicrophoneMuted()) {
logWarn(LogCategory.VOICE, tr("Received local voice started event, even thou we're muted!"));
return;
}
this.native.enable_voice_send(true);
this.localAudioStarted = true;
logInfo(LogCategory.VOICE, tr("Local voice started"));
const ch = chandler.getClient();
if(ch) ch.speaking = true;
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
private handleVoiceEndEvent() {
this.native.enable_voice_send(false);
const chandler = this.connection.client;
2020-09-24 16:06:52 -04:00
const ch = chandler.getClient();
if(ch) ch.speaking = false;
2019-10-25 19:51:40 -04:00
if(!chandler.connected)
return false;
2019-10-25 19:51:40 -04:00
2020-06-13 09:05:49 -04:00
if(chandler.isMicrophoneMuted())
return false;
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
logInfo(LogCategory.VOICE, tr("Local voice ended"));
this.localAudioStarted = false;
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
availableVoiceClients(): NativeVoiceClientWrapper[] {
return Object.keys(this.registeredVoiceClients).map(clientId => this.registeredVoiceClients[clientId]);
}
registerVoiceClient(clientId: number) {
const client = new NativeVoiceClientWrapper(this.native.register_client(clientId));
client.events.on("notify_state_changed", this.voiceClientStateChangedEventListener);
this.registeredVoiceClients[clientId] = client;
return client;
}
unregisterVoiceClient(client: VoiceClient) {
if(!(client instanceof NativeVoiceClientWrapper))
throw "invalid client type";
delete this.registeredVoiceClients[client.getClientId()];
this.native.unregister_client(client.getClientId());
client.destroy();
}
stopAllVoiceReplays() {
this.availableVoiceClients().forEach(client => client.abortReplay());
}
/* whisper API */
getWhisperSessionInitializer(): WhisperSessionInitializer | undefined {
return undefined;
}
getWhisperSessions(): WhisperSession[] {
return [];
}
getWhisperTarget(): WhisperTarget | undefined {
return undefined;
}
setWhisperSessionInitializer(initializer: WhisperSessionInitializer | undefined) {
}
startWhisper(target: WhisperTarget): Promise<void> {
return Promise.resolve(undefined);
}
dropWhisperSession(session: WhisperSession) {
}
stopWhisper() {
}
2020-11-29 15:06:57 -05:00
getConnectionStats(): Promise<ConnectionStatistics> {
2020-12-02 15:00:51 -05:00
/* FIXME: This is iffy! */
const stats = (this.connection as any as NativeServerConnection)["nativeHandle"]?.statistics();
2020-11-29 15:06:57 -05:00
return Promise.resolve({
2020-12-02 15:00:51 -05:00
bytesSend: stats?.voice_bytes_send ? stats?.voice_bytes_send : 0,
bytesReceived: stats?.voice_bytes_received ? stats?.voice_bytes_received : 0
2020-11-29 15:06:57 -05:00
});
}
getRetryTimestamp(): number | 0 {
return Date.now();
}
2020-09-24 16:06:52 -04:00
}
class NativeVoiceClientWrapper implements VoiceClient {
private readonly native: NativeVoiceClient;
readonly events: Registry<VoicePlayerEvents>;
private playerState: VoicePlayerState;
constructor(native: NativeVoiceClient) {
this.events = new Registry<VoicePlayerEvents>();
this.native = native;
this.playerState = VoicePlayerState.STOPPED;
this.native.callback_state_changed = state => {
switch (state) {
case PlayerState.BUFFERING:
this.setState(VoicePlayerState.BUFFERING);
break;
case PlayerState.PLAYING:
this.setState(VoicePlayerState.PLAYING);
break;
case PlayerState.STOPPED:
this.setState(VoicePlayerState.STOPPED);
break;
case PlayerState.STOPPING:
this.setState(VoicePlayerState.STOPPING);
break;
2019-10-25 19:51:40 -04:00
}
}
2020-10-01 04:56:22 -04:00
this.resetLatencySettings();
2020-09-24 16:06:52 -04:00
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
destroy() {
this.events.destroy();
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
abortReplay() {
this.native.abort_replay();
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
flushBuffer() {
this.native.get_stream().flush_buffer();
}
2020-09-24 16:06:52 -04:00
getClientId(): number {
return this.native.client_id;
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
getState(): VoicePlayerState {
return this.playerState;
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
private setState(state: VoicePlayerState) {
if(this.playerState === state) {
return;
}
const oldState = this.playerState;
this.playerState = state;
this.events.fire("notify_state_changed", { oldState: oldState, newState: state });
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
setVolume(volume: number) {
this.native.set_volume(volume);
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
getVolume(): number {
return this.native.get_volume();
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
resetLatencySettings() {
const stream = this.native.get_stream();
stream.set_buffer_latency(0.080);
stream.set_buffer_max_latency(0.5);
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
setLatencySettings(settings: VoicePlayerLatencySettings) {
const stream = this.native.get_stream();
stream.set_buffer_latency(settings.minBufferTime / 1000);
stream.set_buffer_max_latency(settings.maxBufferTime / 1000);
}
2019-10-25 19:51:40 -04:00
2020-09-24 16:06:52 -04:00
getLatencySettings(): Readonly<VoicePlayerLatencySettings> {
const stream = this.native.get_stream();
return {
maxBufferTime: stream.get_buffer_max_latency() * 1000,
minBufferTime: stream.get_buffer_latency() * 1000
};
2019-10-25 19:51:40 -04:00
}
}