fix(audio): prevent empty session replay

This commit is contained in:
chenjintao
2026-09-24 18:00:48 +08:00
parent 83ef72839e
commit 6fad31c37a
2 changed files with 33 additions and 18 deletions
@@ -420,17 +420,21 @@ void vad_audio_process(void) {
#endif #endif
first_sound_detected = true; first_sound_detected = true;
{ {
size_t first = (vad_preroll_next + VAD_PREROLL_FRAMES - vad_preroll_count) % VAD_PREROLL_FRAMES; size_t first = (vad_preroll_next + VAD_PREROLL_FRAMES - vad_preroll_count) % VAD_PREROLL_FRAMES;
size_t n; size_t n;
for (n = 0; n < vad_preroll_count; n++) { int preroll_queued = 0;
size_t frame_index = (first + n) % VAD_PREROLL_FRAMES; for (n = 0; n < vad_preroll_count; n++) {
if (hal_os_rb_write(ring_sender, vad_preroll[frame_index], VAD_FRAME_BYTES, 25) > 0) { size_t frame_index = (first + n) % VAD_PREROLL_FRAMES;
ql_rtos_semaphore_release(sender_sem); if (hal_os_rb_write(ring_sender, vad_preroll[frame_index], VAD_FRAME_BYTES, 25) > 0) {
} preroll_queued = 1;
} }
vad_preroll_count = 0; }
} if (preroll_queued) {
ql_rtos_semaphore_release(sender_sem);
}
vad_preroll_count = 0;
}
set_state(STATE_RECORDING_ACTIVE); set_state(STATE_RECORDING_ACTIVE);
printf("First speech confirmed by FVAD (continuous %d frames)", SPEECH_DETECT_THRESHOLD); printf("First speech confirmed by FVAD (continuous %d frames)", SPEECH_DETECT_THRESHOLD);
} }
@@ -1365,9 +1365,17 @@ sender_loop:
continue; continue;
} }
if(!app_get_data_call_net_status()) continue; if(!app_get_data_call_net_status()) continue;
if(shine_init==0){ /* One recording can release the semaphore many times while the sender
* drains the ring buffer. A stale token must never create an empty
* session after the previous encoder has already been closed. */
left = rb_get_space_used(ring_sender);
if(shine_init == 0 && left <= 0){
continue;
}
if(shine_init==0){
shine_set_config_mpeg_defaults(&(sh_config.mpeg)); //--init sh_config struct shine_set_config_mpeg_defaults(&(sh_config.mpeg)); //--init sh_config struct
sh_config.wave.channels=PCM_MONO; //PCM_STEREO;//PCM_MONO; sh_config.wave.channels=PCM_MONO; //PCM_STEREO;//PCM_MONO;
sh_config.wave.samplerate=16000;//valid samplerates: 44100,48000,32000,22050,24000,16000,11025,12000,8000 sh_config.wave.samplerate=16000;//valid samplerates: 44100,48000,32000,22050,24000,16000,11025,12000,8000
@@ -1417,10 +1425,13 @@ sender_loop:
sh_shine = NULL; sh_shine = NULL;
} }
shine_init = 0; shine_init = 0;
audio_packet_type = 0; audio_packet_type = 0;
} /* Discard semaphore tokens already represented by the PCM that this
* session drained. Recording has stopped, so no new frames race here. */
while(ql_rtos_semaphore_wait(sender_sem, 0) == 0) {}
}
} }
} }