Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasapi: Force WIN_CoInitialize duing device open. #7489

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/audio/wasapi/SDL_wasapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,11 @@ void WASAPI_UnrefDevice(_THIS)
our callback thread. We do that in WASAPI_ThreadDeinit().
(likewise for this->hidden->coinitialized). */
ReleaseWasapiDevice(this);

if (SDL_ThreadID() == this->hidden->open_threadid) {
WIN_CoUninitialize(); /* if you closed from a different thread than you opened, sorry, it's a leak. We can't help you. */
}

SDL_free(this->hidden->devid);
SDL_free(this->hidden);
}
Expand Down Expand Up @@ -539,6 +544,11 @@ static int WASAPI_OpenDevice(_THIS, const char *devname)

WASAPI_RefDevice(this); /* so CloseDevice() will unref to zero. */

if (FAILED(WIN_CoInitialize())) { /* WASAPI uses COM, we need to make sure it's initialized. You have to close the device from the same thread!! */
return SDL_SetError("WIN_CoInitialize failed during WASAPI device open");
}
this->hidden->open_threadid = SDL_ThreadID(); /* set this _after_ coinitialize so we don't uninit if device fails at the wrong moment. */

if (!devid) { /* is default device? */
this->hidden->default_device_generation = SDL_AtomicGet(this->iscapture ? &SDL_IMMDevice_DefaultCaptureGeneration : &SDL_IMMDevice_DefaultPlaybackGeneration);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/audio/wasapi/SDL_wasapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct SDL_PrivateAudioData
SDL_AudioStream *capturestream;
HANDLE event;
HANDLE task;
SDL_threadID open_threadid;
SDL_bool coinitialized;
int framesize;
int default_device_generation;
Expand Down