diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 39665dc9ff..5c32ba6190 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -946,14 +946,12 @@ void NativeShutdownGraphics() { #if PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(UWP) if (winCamera) { - winCamera->sendMessage({ CAPTUREDEVIDE_COMMAND::SHUTDOWN, nullptr }); - while (!winCamera->isShutDown()) {};// Wait for shutting down. + winCamera->waitShutDown(); delete winCamera; winCamera = nullptr; } if (winMic) { - winMic->sendMessage({ CAPTUREDEVIDE_COMMAND::SHUTDOWN, nullptr }); - while (!winMic->isShutDown()) {};// Wait for shutting down. + winMic->waitShutDown(); delete winMic; winMic = nullptr; } diff --git a/Windows/CaptureDevice.cpp b/Windows/CaptureDevice.cpp index 45cbc33fb2..6d511f8b90 100644 --- a/Windows/CaptureDevice.cpp +++ b/Windows/CaptureDevice.cpp @@ -854,6 +854,21 @@ CAPTUREDEVIDE_MESSAGE WindowsCaptureDevice::getMessage() { return message; } +void WindowsCaptureDevice::updateState(const CAPTUREDEVIDE_STATE &newState) { + std::unique_lock guard(stateMutex_); + state = newState; + stateCond_.notify_all(); +} + +void WindowsCaptureDevice::waitShutDown() { + sendMessage({ CAPTUREDEVIDE_COMMAND::SHUTDOWN, nullptr }); + + std::unique_lock guard(stateMutex_); + while (!isShutDown()) { + stateCond_.wait(guard); + } +} + void WindowsCaptureDevice::messageHandler() { CoInitializeEx(NULL, COINIT_MULTITHREADED); MFStartup(MF_VERSION); diff --git a/Windows/CaptureDevice.h b/Windows/CaptureDevice.h index 5c5224f9c8..64ef4926a6 100644 --- a/Windows/CaptureDevice.h +++ b/Windows/CaptureDevice.h @@ -194,11 +194,11 @@ public: void setError(const CAPTUREDEVIDE_ERROR &newError, const std::string &newErrorMessage) { error = newError; errorMessage = newErrorMessage; } void setSelction(const UINT32 &selection) { param.selection = selection; } - void updateState(const CAPTUREDEVIDE_STATE &newState) { state = newState; } HRESULT setDeviceParam(IMFMediaType *pType); bool isShutDown() const { return state == CAPTUREDEVIDE_STATE::SHUTDOWN; } bool isStarted() const { return state == CAPTUREDEVIDE_STATE::STARTED; } + void waitShutDown(); void sendMessage(CAPTUREDEVIDE_MESSAGE message); CAPTUREDEVIDE_MESSAGE getMessage(); @@ -210,7 +210,8 @@ public: friend class ReaderCallback; protected: -// Handle message here. + void updateState(const CAPTUREDEVIDE_STATE &newState); + // Handle message here. void messageHandler(); CAPTUREDEVIDE_TYPE type; @@ -239,6 +240,8 @@ protected: // Param updating synchronously. std::mutex paramMutex; + std::mutex stateMutex_; + std::condition_variable stateCond_; // Camera only unsigned char *imageRGB = nullptr;