diff --git a/engine/client/cl_main.c b/engine/client/cl_main.c index fb4eada4..e21b52d7 100644 --- a/engine/client/cl_main.c +++ b/engine/client/cl_main.c @@ -3139,7 +3139,7 @@ void CL_Init( void ) VID_Init(); // init video S_Init(); // init sound - Voice_Init( VOICE_DEFAULT_CODEC, 3 ); // init voice + Voice_Init( VOICE_DEFAULT_CODEC, 3, true ); // init voice (do not open the device) // unreliable buffer. unsed for unreliable commands and voice stream MSG_Init( &cls.datagram, "cls.datagram", cls.datagram_buf, sizeof( cls.datagram_buf )); diff --git a/engine/client/cl_parse.c b/engine/client/cl_parse.c index b23a7bf3..d61020e1 100644 --- a/engine/client/cl_parse.c +++ b/engine/client/cl_parse.c @@ -1778,7 +1778,7 @@ void CL_ParseVoiceInit( sizebuf_t *msg ) char *pszCodec = MSG_ReadString( msg ); int quality = MSG_ReadByte( msg ); - Voice_Init( pszCodec, quality ); + Voice_Init( pszCodec, quality, false ); // init requested codec and the device } /* diff --git a/engine/client/voice.c b/engine/client/voice.c index bfeaca0b..2221297b 100644 --- a/engine/client/voice.c +++ b/engine/client/voice.c @@ -428,6 +428,9 @@ void Voice_Disconnect( void ) voice.players_status[i].talking_ack = false; } } + + VoiceCapture_Shutdown(); + voice.device_opened = false; } /* @@ -588,48 +591,53 @@ Voice_Init Initialize the voice subsystem ========================= */ -qboolean Voice_Init( const char *pszCodecName, int quality ) +qboolean Voice_Init( const char *pszCodecName, int quality, qboolean preinit ) { if( !voice_enable.value ) return false; - if( Q_strcmp( pszCodecName, VOICE_OPUS_CUSTOM_CODEC )) - { - Con_Printf( S_ERROR "Server requested unsupported codec: %s\n", pszCodecName ); - return false; - } - // reinitialize only if codec parameters are different - if( !Q_strcmp( voice.codec, pszCodecName ) && voice.quality == quality ) - return true; - - Voice_Shutdown(); - - voice.autogain.block_size = 128; - - if( !Voice_InitOpusDecoder( )) + if( Q_strcmp( pszCodecName, voice.codec ) || voice.quality != quality ) { - // no reason to init encoder and open audio device - // if we can't hear other players - Con_Printf( S_ERROR "Voice chat disabled.\n" ); Voice_Shutdown(); - return false; + + 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_InitOpusDecoder( )) + { + // no reason to init encoder and open audio device + // if we can't hear other players + Con_Printf( S_ERROR "Voice chat disabled.\n" ); + Voice_Shutdown(); + return false; + } + + // we can hear others players, so it's fine to fail now + voice.initialized = true; + Q_strncpy( voice.codec, pszCodecName, sizeof( voice.codec )); + + if( !Voice_InitOpusEncoder( quality )) + { + Con_Printf( S_WARN "Other players will not be able to hear you.\n" ); + return false; + } + + voice.quality = quality; } - // we can hear others players, so it's fine to fail now - voice.initialized = true; - Q_strncpy( voice.codec, pszCodecName, sizeof( voice.codec )); - - if( !Voice_InitOpusEncoder( quality )) + if( !preinit ) { - Con_Printf( S_WARN "Other players will not be able to hear you.\n" ); - return false; + voice.device_opened = VoiceCapture_Init(); + + if( !voice.device_opened ) + Con_Printf( S_WARN "No microphone is available.\n" ); } - voice.quality = quality; - - if( !VoiceCapture_Init( )) - Con_Printf( S_WARN "No microphone is available.\n" ); - return true; } diff --git a/engine/client/voice.h b/engine/client/voice.h index f1d878cb..28746705 100644 --- a/engine/client/voice.h +++ b/engine/client/voice.h @@ -53,6 +53,7 @@ typedef struct voice_state_s qboolean initialized; qboolean is_recording; + qboolean device_opened; double start_time; voice_status_t local; @@ -92,7 +93,7 @@ extern voice_state_t voice; void CL_AddVoiceToDatagram( void ); void Voice_RegisterCvars( void ); -qboolean Voice_Init( const char *pszCodecName, int quality ); +qboolean Voice_Init( const char *pszCodecName, int quality, qboolean preinit ); void Voice_Idle( double frametime ); qboolean Voice_IsRecording( void ); void Voice_RecordStop( void );