devopvoid / webrtc-java

WebRTC for desktop platforms running Java
Apache License 2.0
248 stars 60 forks source link

java.lang.Error: MMF: Create device enumerator failed CoInitialize has not been called #43

Closed nanguantong closed 2 years ago

nanguantong commented 2 years ago

And then when use maven project, it's ok to run this jar, but error happened while running List<AudioDevice> captureDevices = MediaDevices.getAudioCaptureDevices();

the native code not called when WindowsAudioDeviceManager inited as following

ComInitializer::ComInitializer() : initialized(false)
    {
        HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);

        if (hr != RPC_E_CHANGED_MODE) {
            THROW_IF_FAILED(hr, "Initialize COM failed");

            initialized = true;
        }
    }

in WebRTCContext::initialize void WebRTCContext::initDeviceManagers() {

ifdef _WIN32

    audioDevManager = std::make_unique<avdev::WindowsAudioDeviceManager>();
    videoDevManager = std::make_unique<avdev::WindowsVideoDeviceManager>();

endif

  ......

}

in WindowsAudioDeviceManager

WindowsAudioDeviceManager::WindowsAudioDeviceManager() :
            deviceEnumerator()
        {
                       //////////////////////////////////////// not call ComInitializer /////////////////////////////////////////////
            HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&deviceEnumerator);
            THROW_IF_FAILED(hr, "MMF: Create device enumerator failed");

            enumerateDevices(eAll);

            deviceEnumerator->RegisterEndpointNotificationCallback(this);
        }
nanguantong commented 2 years ago

@devopvoid can you please help to fix this bug and then release the maven or gradle jar packet to public repo?

devopvoid commented 2 years ago

For gradle please see How to fetch the jni libs with gradle #23.

nanguantong commented 2 years ago

For gradle please see How to fetch the jni libs with gradle #23.

Yeah, got it.

nanguantong commented 2 years ago

@devopvoid can you please help to fix this bug and then release the maven or gradle jar packet to public repo?

But this is realy a bug to call CoCreateInstancebefore COM CoInitialize, so hope to fix that.

devopvoid commented 2 years ago

This is not a bug. I'm not sure where you found the ComInitializer.

  1. WindowsAudio/VideoDeviceManager are based on Media Foundation.
  2. WindowsAudioDeviceManager.h initializes MFInitializer.
  3. MFInitializer calls MFStartup
  4. Then WindowsAudioDeviceManager constructor is executed
  5. Where CoCreateInstance works fine
nanguantong commented 2 years ago

This is not a bug. I'm not sure where you found the ComInitializer.

  1. WindowsAudio/VideoDeviceManager are based on Media Foundation.
  2. WindowsAudioDeviceManager.h initializes MFInitializer.
  3. MFInitializer calls MFStartup
  4. Then WindowsAudioDeviceManager constructor is executed
  5. Where CoCreateInstance works fine

In webrtc-jni\src\main\cpp\src\platform\windows\ComInitializer.cpp call HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);

I just only call in my maven test project on windows as following:

  1. pom.xml:
        <dependency>
            <groupId>dev.onvoid.webrtc</groupId>
            <artifactId>webrtc-java</artifactId>
            <version>0.4.0-SNAPSHOT</version>
        </dependency> 
  2. image
devopvoid commented 2 years ago

Your error actually points to AudioProcessing, not MediaDevices.getAudioCaptureDevices().

Could you provide your test-class?

In the meantime you can try 0.5.0-SNAPSHOT.

nanguantong commented 2 years ago

image

nanguantong commented 2 years ago

Your error actually points to AudioProcessing, not MediaDevices.getAudioCaptureDevices().

Could you provide your test-class?

In the meantime you can try 0.5.0-SNAPSHOT.

0.5.0-SNAPSHOT used ok.

nanguantong commented 2 years ago

But 0.4.0 and (SNAPSHOT) still have this problem.

devopvoid commented 2 years ago

I won't fix it for 0.4.0-SNAPSHOT since 0.4.0 is released. The next snapshot version is 0.5.0-SNAPSHOT.

nanguantong commented 2 years ago

I won't fix it for 0.4.0-SNAPSHOT since 0.4.0 is released. The next snapshot version is 0.5.0-SNAPSHOT.

Yeh, no problem.