mirror of
				https://github.com/libsdl-org/SDL.git
				synced 2025-11-04 01:34:38 +00:00 
			
		
		
		
	Fixed audio recording latency after a hitch
We want to return any data as soon as it's available, if we get a hitch and always wait, we'll never catch up.
This commit is contained in:
		@@ -465,22 +465,41 @@ static int WASAPI_WaitDevice(SDL_AudioDevice *device)
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    // WaitDevice does not hold the device lock, so check for recovery/disconnect details here.
 | 
					    // WaitDevice does not hold the device lock, so check for recovery/disconnect details here.
 | 
				
			||||||
    while (RecoverWasapiIfLost(device) && device->hidden->client && device->hidden->event) {
 | 
					    while (RecoverWasapiIfLost(device) && device->hidden->client && device->hidden->event) {
 | 
				
			||||||
        DWORD waitResult = WaitForSingleObjectEx(device->hidden->event, 200, FALSE);
 | 
					        if (device->recording) {
 | 
				
			||||||
        if (waitResult == WAIT_OBJECT_0) {
 | 
					            // Recording devices should return immediately if there is any data available
 | 
				
			||||||
            const UINT32 maxpadding = device->sample_frames;
 | 
					 | 
				
			||||||
            UINT32 padding = 0;
 | 
					            UINT32 padding = 0;
 | 
				
			||||||
            if (!WasapiFailed(device, IAudioClient_GetCurrentPadding(device->hidden->client, &padding))) {
 | 
					            if (!WasapiFailed(device, IAudioClient_GetCurrentPadding(device->hidden->client, &padding))) {
 | 
				
			||||||
                //SDL_Log("WASAPI EVENT! padding=%u maxpadding=%u", (unsigned int)padding, (unsigned int)maxpadding);*/
 | 
					                //SDL_Log("WASAPI EVENT! padding=%u maxpadding=%u", (unsigned int)padding, (unsigned int)maxpadding);
 | 
				
			||||||
                if (device->recording && (padding > 0)) {
 | 
					                if (padding > 0) {
 | 
				
			||||||
                    break;
 | 
					 | 
				
			||||||
                } else if (!device->recording && (padding <= maxpadding)) {
 | 
					 | 
				
			||||||
                    break;
 | 
					                    break;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        } else if (waitResult != WAIT_TIMEOUT) {
 | 
					
 | 
				
			||||||
            //SDL_Log("WASAPI FAILED EVENT!");*/
 | 
					            switch (WaitForSingleObjectEx(device->hidden->event, 200, FALSE)) {
 | 
				
			||||||
            IAudioClient_Stop(device->hidden->client);
 | 
					            case WAIT_OBJECT_0:
 | 
				
			||||||
            return -1;
 | 
					            case WAIT_TIMEOUT:
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            default:
 | 
				
			||||||
 | 
					                //SDL_Log("WASAPI FAILED EVENT!");
 | 
				
			||||||
 | 
					                IAudioClient_Stop(device->hidden->client);
 | 
				
			||||||
 | 
					                return -1;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            DWORD waitResult = WaitForSingleObjectEx(device->hidden->event, 200, FALSE);
 | 
				
			||||||
 | 
					            if (waitResult == WAIT_OBJECT_0) {
 | 
				
			||||||
 | 
					                UINT32 padding = 0;
 | 
				
			||||||
 | 
					                if (!WasapiFailed(device, IAudioClient_GetCurrentPadding(device->hidden->client, &padding))) {
 | 
				
			||||||
 | 
					                    //SDL_Log("WASAPI EVENT! padding=%u maxpadding=%u", (unsigned int)padding, (unsigned int)maxpadding);
 | 
				
			||||||
 | 
					                    if (padding <= (UINT32)device->sample_frames) {
 | 
				
			||||||
 | 
					                        break;
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            } else if (waitResult != WAIT_TIMEOUT) {
 | 
				
			||||||
 | 
					                //SDL_Log("WASAPI FAILED EVENT!");*/
 | 
				
			||||||
 | 
					                IAudioClient_Stop(device->hidden->client);
 | 
				
			||||||
 | 
					                return -1;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -662,7 +681,7 @@ static int mgmtthrtask_PrepDevice(void *userdata)
 | 
				
			|||||||
                    iaudioclient3_initialized = SDL_TRUE;
 | 
					                    iaudioclient3_initialized = SDL_TRUE;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            
 | 
					
 | 
				
			||||||
            IAudioClient3_Release(client3);
 | 
					            IAudioClient3_Release(client3);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user