libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
10k stars 1.85k forks source link

Any open sdl audio device seem to prevent windows from sleeping #9601

Open nico-abram opened 6 months ago

nico-abram commented 6 months ago

Running powercfg /requests on windows while running the sample below prints

SYSTEM:
[DRIVER] Realtek High Definition Audio (HDAUDIO\{REDACTED})
An audio stream is currently in use.

Note: The sample must be ran in a folder containing a sample.wav file such as the ./test/sample.wav in the repo.

This prevents windows from going to sleep. I've tried setting SDL_HINT_VIDEO_ALLOW_SCREENSAVER and pausing via SDL_PauseAudioDevice

I'm not sure if this is intended behaviour or a bug.

#include "SDL.h"

int main() {
    Uint8 *sound;
    Uint32 soundlen;
    SDL_AudioSpec spec;

    SDL_Init(SDL_INIT_AUDIO | SDL_INIT_EVENTS);
    SDL_LoadWAV("sample.wav", &spec, &sound, &soundlen);

    SDL_SetHint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, "1");
    SDL_AudioDeviceID device = SDL_OpenAudioDevice(NULL, SDL_FALSE, &spec, NULL, 0);

    SDL_PauseAudioDevice(device, SDL_FALSE);
    SDL_PauseAudioDevice(device, SDL_TRUE);

    while(1) {}
}
icculus commented 6 months ago

"Pausing" the audio device just feeds silence to the hardware, so that definitely won't do it.

I wasn't aware of this behavior on Windows. I'll have to look into it. I'm not sure if it's something we can workaround, short of the app closing the device when not using it.