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

assertion ma_device_get_state(pDevice) == ma_device_state_stopped failed when ma_device_start in callback ma_IMMNotificationClient_OnDefaultDeviceChanged #777

Open
h20282 opened this issue Nov 20, 2023 · 1 comment
Labels

Comments

@h20282
Copy link

h20282 commented Nov 20, 2023

When I run the following code and switch the audio output device

#include <thread>

#define MA_DEBUG_OUTPUT
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"

#include "fmt/core.h"

using namespace std::chrono_literals;
int main() {
    ma_device device;

    ma_device_config config;
    config = ma_device_config_init(ma_device_type_playback);
    config.playback.format = ma_format_s16;
    config.playback.channels = 1;
    config.sampleRate = 48000;
    config.dataCallback = [](ma_device *device, void *output, const void *input,
                             ma_uint32 frame_count) {};
    config.notificationCallback =
            [](const ma_device_notification *pNotification) {
                fmt::print("{}:{}\n", pNotification->pDevice->playback.name,
                           pNotification->type);
            };
    ma_device_init(nullptr, &config, &device);

    while (1) {
        ma_device_start(&device);
        std::this_thread::sleep_for(10ms);
        ma_device_stop(&device);
        std::this_thread::sleep_for(10ms);
    }
}

image

I get this:

Assertion failed: ma_device_get_state(pDevice) == ma_device_state_stopped, file C:\Users\...\include\miniaudio.h, line 42315

the state now is ma_device_state_started
image

debug infos:

DEBUG: Loading library: user32.dll
DEBUG: Loading symbol: GetForegroundWindow
DEBUG: Loading symbol: GetDesktopWindow
DEBUG: Loading library: advapi32.dll
DEBUG: Loading symbol: RegOpenKeyExA
DEBUG: Loading symbol: RegCloseKey
DEBUG: Loading symbol: RegQueryValueExA
DEBUG: Loading library: ole32.dll
DEBUG: Loading symbol: CoInitialize
DEBUG: Loading symbol: CoInitializeEx
DEBUG: Loading symbol: CoUninitialize
DEBUG: Loading symbol: CoCreateInstance
DEBUG: Loading symbol: CoTaskMemFree
DEBUG: Loading symbol: PropVariantClear
DEBUG: Loading symbol: StringFromGUID2
DEBUG: Attempting to initialize WASAPI backend...
DEBUG: Loading library: kernel32.dll
DEBUG: Loading symbol: VerifyVersionInfoW
DEBUG: Loading symbol: VerSetConditionMask
DEBUG: Loading library: avrt.dll
DEBUG: Loading symbol: AvSetMmThreadCharacteristicsA
DEBUG: Loading symbol: AvRevertMmThreadCharacteristics
DEBUG: System Architecture:
DEBUG:   Endian: LE
DEBUG:   SSE2:   YES
DEBUG:   AVX2:   YES
DEBUG:   NEON:   NO
DEBUG: [WASAPI] Trying IAudioClient3_InitializeSharedAudioStream(actualPeriodInFrames=480)
DEBUG:     defaultPeriodInFrames=480
DEBUG:     fundamentalPeriodInFrames=480
DEBUG:     minPeriodInFrames=480
DEBUG:     maxPeriodInFrames=480
DEBUG: [WASAPI] Using IAudioClient3
DEBUG:     periodSizeInFramesOut=480
INFO: [WASAPI]
INFO:   2490W1G5 (HD Audio Driver for Display Audio) (Playback)
INFO:     Format:      16-bit Signed Integer -> 32-bit IEEE Floating Point
INFO:     Channels:    1 -> 2
INFO:     Sample Rate: 48000 -> 48000
INFO:     Buffer Size: 480*3 (1440)
INFO:     Conversion:
INFO:       Pre Format Conversion:  YES
INFO:       Post Format Conversion: NO
INFO:       Channel Routing:        YES
INFO:       Resampling:             NO
INFO:       Passthrough:            NO
INFO:       Channel Map In:         {CHANNEL_MONO}
INFO:       Channel Map Out:        {CHANNEL_FRONT_LEFT CHANNEL_FRONT_RIGHT}
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
2490W1G5 (HD Audio Driver for Display Audio):0
2490W1G5 (HD Audio Driver for Display Audio):1
DEBUG: === CHANGING DEVICE ===
DEBUG: [WASAPI] Trying IAudioClient3_InitializeSharedAudioStream(actualPeriodInFrames=480)
DEBUG:     defaultPeriodInFrames=480
DEBUG:     fundamentalPeriodInFrames=480
DEBUG:     minPeriodInFrames=480
DEBUG:     maxPeriodInFrames=480
DEBUG: [WASAPI] Using IAudioClient3
DEBUG:     periodSizeInFramesOut=480
扬声器 (Realtek(R) Audio):2
DEBUG: === DEVICE CHANGED ===
DEBUG: [WASAPI] Stream rerouting abandoned because the device is in the process of starting.
扬声器 (Realtek(R) Audio):0
扬声器 (Realtek(R) Audio):1
扬声器 (Realtek(R) Audio):0
扬声器 (Realtek(R) Audio):1
扬声器 (Realtek(R) Audio):0
扬声器 (Realtek(R) Audio):1
扬声器 (Realtek(R) Audio):0
扬声器 (Realtek(R) Audio):1
扬声器 (Realtek(R) Audio):0
扬声器 (Realtek(R) Audio):1
扬声器 (Realtek(R) Audio):0
扬声器 (Realtek(R) Audio):1
扬声器 (Realtek(R) Audio):0
扬声器 (Realtek(R) Audio):1
扬声器 (Realtek(R) Audio):0
扬声器 (Realtek(R) Audio):1
扬声器 (Realtek(R) Audio):0
扬声器 (Realtek(R) Audio):1
DEBUG: === CHANGING DEVICE ===
DEBUG: [WASAPI] Trying IAudioClient3_InitializeSharedAudioStream(actualPeriodInFrames=480)
DEBUG:     defaultPeriodInFrames=480
DEBUG:     fundamentalPeriodInFrames=480
DEBUG:     minPeriodInFrames=480
DEBUG:     maxPeriodInFrames=480
DEBUG: [WASAPI] Using IAudioClient3
DEBUG:     periodSizeInFramesOut=480
2490W1G5 (HD Audio Driver for Display Audio):2
DEBUG: === DEVICE CHANGED ===
2490W1G5 (HD Audio Driver for Display Audio):0
Assertion failed: ma_device_get_state(pDevice) == ma_device_state_stopped, file C:\Users\20536\.conan\data\miniaudio\0.11.21\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\include\miniaudio.h, line 42315

parallel-stacks:
image

@mackron
Copy link
Owner

mackron commented Nov 22, 2023

Thanks for the report. This looks like a threading synchronization error where one thread is in the process of rerouting and the other is in ma_device_start(). Will need to have a proper look, but I think the WASAPI backend needs a rewrite so that all operations are run on a worker thread. I think WASAPI is not only thread unsafe, but downright thread hostile.

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

No branches or pull requests

2 participants