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 can't initialize audio client: CoInitialize has not been called. #7478

Closed
rom1v opened this issue Mar 16, 2023 · 17 comments
Closed

WASAPI can't initialize audio client: CoInitialize has not been called. #7478

rom1v opened this issue Mar 16, 2023 · 17 comments
Assignees
Milestone

Comments

@rom1v
Copy link

rom1v commented Mar 16, 2023

The call to SDL_OpenAudioDevice(NULL, 0, &desired, &obtained, 0) fails with the following error:

WASAPI can't initialize audio client: CoInitialize has not been called.

Maybe related: #5552 (but apparently it still fails in 2.26.3)

(source: Genymobile/scrcpy#3825)

@slouken slouken added this to the 2.28.0 milestone Mar 16, 2023
@icculus
Copy link
Collaborator

icculus commented Mar 16, 2023

Oh weird, let me see if I can reproduce it here.

Is this opening the audio device without initializing the video subsystem? I'm wondering if we unintentionally relied on that to initialize COM.

@rom1v
Copy link
Author

rom1v commented Mar 16, 2023

Is this opening the audio device without initializing the video subsystem?

No, the video subsystem is necessarily initialized. The audio subsystem is initialized unless audio is explicitly disabled (in other words, it is initialized in this case).

@icculus
Copy link
Collaborator

icculus commented Mar 16, 2023

...are you opening an audio device from a background thread? It certainly seems like we are not calling WIN_CoInitialize() during device open, so that would likely trigger this issue.

@rom1v
Copy link
Author

rom1v commented Mar 16, 2023

...are you opening an audio device from a background thread?

Yes.

Concretely, I receive an audio stream from a socket from a demuxer thread. Once the stream starts, it opens its sinks (a decoder), which itself opens its sinks (an audio player). This is from there that I call SDL_OpenAudioDevice(NULL, 0, &desired, &obtained, 0);.

It is called from there for several reasons:

  • only from here we know the input format (sample rate, number of channels…) to initialize the SDL_AudioSpec
  • this is the same thread which will receive the audio packets
  • if the audio stream is disabled (because the device could not capture for example), there is no need to open the SDL device.

@slouken
Copy link
Collaborator

slouken commented Mar 16, 2023

Yes, we should call WIN_CoInitialize() during device opening and cleanup during device closing. It's safe to call this on the same thread, so it should be fine.

@icculus
Copy link
Collaborator

icculus commented Mar 16, 2023

Okay, just confirming. I'll add in a CoInitialize call, which should fix this.

icculus added a commit to icculus/SDL that referenced this issue Mar 17, 2023
Otherwise this won't work if you open a device on a background thread.

Fixes libsdl-org#7478.
@icculus
Copy link
Collaborator

icculus commented Mar 17, 2023

#7489 is untested but should fix this for SDL2, but please close the audio device from the same thread you opened it, or it will cause a leak.

For SDL3, I'm reworking how audio devices get opened, so hopefully this will be a non-issue there in any case.

@rom1v
Copy link
Author

rom1v commented Mar 17, 2023

#7489 is untested but should fix this for SDL2

Thank you very much 👍

Will the CI generate a ready-to-use SDL2.dll so that I can submit it to users that have reported this problem?

but please close the audio device from the same thread you opened it, or it will cause a leak.

Yes, opening and closing is from the same thread. 👌

slouken pushed a commit that referenced this issue Mar 17, 2023
Otherwise this won't work if you open a device on a background thread.

Fixes #7478.
@icculus
Copy link
Collaborator

icculus commented Mar 19, 2023

Will the CI generate a ready-to-use SDL2.dll so that I can mail submit it to users that have reported this problem?

I don't believe it does at the moment. :(

@icculus
Copy link
Collaborator

icculus commented Apr 12, 2023

2.26.5 should have this fix, if you want to grab those binaries.

@rom1v
Copy link
Author

rom1v commented Apr 12, 2023

2.26.5 should have this fix

No, I think it will only be in 2.28.

@icculus
Copy link
Collaborator

icculus commented Apr 12, 2023

Oh, you're right, sorry about that!

@icculus
Copy link
Collaborator

icculus commented May 29, 2023

I'm closing this under the assumption it's fixed in 2.28.0, which is coming shortly. Let's reopen this if there's still an issue, though.

@rom1v
Copy link
Author

rom1v commented Jun 14, 2023

@icculus I confirm that SDL 2.28RC1 seems to fix the probem: Genymobile/scrcpy#3825 (comment) 🎉

@icculus
Copy link
Collaborator

icculus commented Jun 15, 2023

Thank you for patiently waiting for binaries for so long, and then jumping right in to test them!

rom1v added a commit to Genymobile/scrcpy that referenced this issue Jun 20, 2023
Include the latest version of SDL in Windows releases.

Fixes #3825 <#3825>
Refs libsdl/#7478 <libsdl-org/SDL#7478>
@rom1v
Copy link
Author

rom1v commented Jun 25, 2023

I don't know if this is related, but sometimes, there is an error:

WASAPI can't find requested audio endpoint: Element not found.

While with directsound it seems to work. See Genymobile/scrcpy#4111 (just for your information).

@slouken
Copy link
Collaborator

slouken commented Jun 26, 2023

I don't know if this is related, but sometimes, there is an error:

WASAPI can't find requested audio endpoint: Element not found.

While with directsound it seems to work. See Genymobile/scrcpy#4111 (just for your information).

Please open a separate issue if you'd like this tracked, we usually don't look at closed bug reports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants