@@ -280,40 +280,79 @@ export function registerIpcEvents() {
280280
281281 // Handler for warm-up audio devices request from renderer
282282 ipcMain . on ( IPC_EVENTS . WARMUP_AUDIO_DEVICES , async ( ) => {
283- // Check if warm-up has already been executed
284- if ( hasRunAudioWarmup ) {
285- Log . info ( '[WARMUP] Audio warm-up already executed, skipping...' )
286- return
287- }
283+ try {
284+ Log . info ( '[WARMUP] Warm-up request received (hasRunAudioWarmup:' , hasRunAudioWarmup , ')' )
285+
286+ // Check if warm-up has already been executed
287+ if ( hasRunAudioWarmup ) {
288+ Log . info ( '[WARMUP] Audio warm-up already executed, skipping...' )
289+ return
290+ }
288291
289- // Mark as executed
290- hasRunAudioWarmup = true
292+ // Mark as executed
293+ hasRunAudioWarmup = true
291294
292- try {
293295 Log . info ( '[WARMUP] Starting silent echo test to warm up audio devices...' )
294296
297+ if ( ! PhoneIslandController . instance ) {
298+ Log . error ( '[WARMUP] PhoneIslandController.instance not found, aborting warm-up' )
299+ return
300+ }
301+
302+ const window = PhoneIslandController . instance . window . getWindow ( )
303+ if ( ! window ) {
304+ Log . error ( '[WARMUP] Window not found, aborting warm-up' )
305+ return
306+ }
307+
308+ Log . info ( '[WARMUP] Window state BEFORE hide:' , {
309+ exists : ! ! window ,
310+ isVisible : window . isVisible ( ) ,
311+ isMuted : window . webContents ?. audioMuted
312+ } )
313+
295314 // Hide the PhoneIsland window to prevent it from showing during warm-up
296315 PhoneIslandController . instance . forceHide ( )
297316
298317 // Mute the PhoneIsland window audio
299318 PhoneIslandController . instance . muteAudio ( )
300319
301- // Wait a bit to ensure mute and hide are applied
302- await new Promise ( resolve => setTimeout ( resolve , 550 ) )
320+ // Wait and verify that hide and mute are actually applied
321+ // Use polling to ensure they're effective even on slow/loaded systems
322+ let attempts = 0
323+ const maxAttempts = 20 // Max 2 seconds (20 * 100ms)
324+
325+ while ( attempts < maxAttempts ) {
326+ await new Promise ( resolve => setTimeout ( resolve , 100 ) )
327+
328+ const isHidden = ! window . isVisible ( )
329+ const isMuted = window . webContents ?. audioMuted
330+
331+ if ( isHidden && isMuted ) {
332+ Log . info ( `[WARMUP] Window hidden and audio muted verified after ${ attempts * 100 } ms` )
333+ break
334+ }
335+
336+ if ( attempts === maxAttempts - 1 ) {
337+ Log . warning ( `[WARMUP] Could not verify hide/mute after ${ maxAttempts * 100 } ms (hidden: ${ isHidden } , muted: ${ isMuted } )` )
338+ }
339+
340+ attempts ++
341+ }
303342
304343 // Start echo test call to *43
305344 Log . info ( '[WARMUP] Starting call to *43' )
306345 PhoneIslandController . instance . call ( '*43' )
307346
308- // Keep the call active for 5 seconds to warm up devices
347+ // Keep the call active for 1. 5 seconds to warm up devices
309348 await new Promise ( resolve => setTimeout ( resolve , 1500 ) )
310349
311350 // End the call
312351 Log . info ( '[WARMUP] Ending echo test call' )
313352 PhoneIslandController . instance . window . emit ( IPC_EVENTS . END_CALL )
314353
315354 // Wait a bit before unmuting and showing
316- await new Promise ( resolve => setTimeout ( resolve , 550 ) )
355+ await new Promise ( resolve => setTimeout ( resolve , 500 ) )
317356
318357 // Unmute the PhoneIsland window audio
319358 PhoneIslandController . instance . unmuteAudio ( )
@@ -325,8 +364,12 @@ export function registerIpcEvents() {
325364 } catch ( error ) {
326365 Log . error ( '[WARMUP] Error during audio warm-up:' , error )
327366 // Make sure to unmute and show even if there's an error
328- PhoneIslandController . instance . unmuteAudio ( )
329- PhoneIslandController . instance . forceShow ( )
367+ try {
368+ PhoneIslandController . instance ?. unmuteAudio ( )
369+ PhoneIslandController . instance ?. forceShow ( )
370+ } catch ( cleanupError ) {
371+ Log . error ( '[WARMUP] Error during cleanup:' , cleanupError )
372+ }
330373 }
331374 } )
332375
0 commit comments