From 2e30acf6116c04aabeacb7f26796ff1088c0ee99 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sun, 28 Jul 2024 17:12:32 +0300 Subject: [PATCH] engine: client: voice: allow restoring voice after toggling voice_enable while connected to server --- engine/client/voice.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/engine/client/voice.c b/engine/client/voice.c index df12cc66..4801b1e1 100644 --- a/engine/client/voice.c +++ b/engine/client/voice.c @@ -32,6 +32,11 @@ CVAR_DEFINE_AUTO( voice_inputfromfile, "0", FCVAR_PRIVILEGED, "input voice from static void Voice_ApplyGainAdjust( int16_t *samples, int count, float scale ); +// in case user enabled voice after connection +// can't keep in `voice` struct because it gets zeroed on shutdown +static string voice_codec_init; +static int voice_quality_init; + /* =============================================================================== @@ -591,10 +596,17 @@ void Voice_Idle( double frametime ) { int i; - if( FBitSet( voice_enable.flags, FCVAR_CHANGED ) && !voice_enable.value ) + if( FBitSet( voice_enable.flags, FCVAR_CHANGED )) { - Voice_Shutdown(); - return; + ClearBits( voice_enable.flags, FCVAR_CHANGED ); + + if( voice_enable.value ) + { + if( cls.state == ca_active && COM_CheckString( voice_codec_init ) && voice_quality_init != 0 ) + Voice_Init( voice_codec_init, voice_quality_init, false ); + } + else + Voice_Shutdown(); } // update local player status first @@ -613,6 +625,19 @@ Initialize the voice subsystem */ qboolean Voice_Init( const char *pszCodecName, int quality, qboolean preinit ) { + if( Q_strcmp( pszCodecName, VOICE_OPUS_CUSTOM_CODEC )) + { + Con_Printf( S_ERROR "Server requested unsupported codec: %s\n", pszCodecName ); + + // reset saved codec name, we won't enable voice for this connection + voice_codec_init[0] = 0; + voice_quality_init = 0; + return false; + } + + Q_strncpy( voice_codec_init, pszCodecName, sizeof( voice_codec_init )); + voice_quality_init = quality; + if( !voice_enable.value ) return false; @@ -620,13 +645,6 @@ qboolean Voice_Init( const char *pszCodecName, int quality, qboolean preinit ) if( Q_strcmp( pszCodecName, voice.codec ) || voice.quality != quality ) { Voice_Shutdown(); - - if( Q_strcmp( pszCodecName, VOICE_OPUS_CUSTOM_CODEC )) - { - Con_Printf( S_ERROR "Server requested unsupported codec: %s\n", pszCodecName ); - return false; - } - voice.autogain.block_size = 128; if( !Voice_InitCustomMode( ))