From 728e2dbe82affeb7edcf01c298613dd8955a50a4 Mon Sep 17 00:00:00 2001 From: hogliux Date: Tue, 2 May 2017 15:25:28 +0100 Subject: [PATCH] Fixed a assertion/crash when a macOS CoreAudio device becomes unavailable during playback --- .../native/juce_mac_CoreAudio.cpp | 54 +++++++++---------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/modules/juce_audio_devices/native/juce_mac_CoreAudio.cpp b/modules/juce_audio_devices/native/juce_mac_CoreAudio.cpp index a08cbe7611..5f4ea10796 100644 --- a/modules/juce_audio_devices/native/juce_mac_CoreAudio.cpp +++ b/modules/juce_audio_devices/native/juce_mac_CoreAudio.cpp @@ -1366,21 +1366,7 @@ public: } } - void stop() override - { - AudioIODeviceCallback* lastCallback = nullptr; - - { - const ScopedLock sl (callbackLock); - std::swap (callback, lastCallback); - } - - for (int i = 0; i < devices.size(); ++i) - devices.getUnchecked(i)->device->stop(); - - if (lastCallback != nullptr) - lastCallback->audioDeviceStopped(); - } + void stop() override { shutdown ({}); } String getLastError() override { @@ -1456,6 +1442,27 @@ private: } } + void shutdown (const String& error) + { + AudioIODeviceCallback* lastCallback = nullptr; + + { + const ScopedLock sl (callbackLock); + std::swap (callback, lastCallback); + } + + for (int i = 0; i < devices.size(); ++i) + devices.getUnchecked(i)->device->stop(); + + if (lastCallback != nullptr) + { + if (error.isNotEmpty()) + lastCallback->audioDeviceError (error); + else + lastCallback->audioDeviceStopped(); + } + } + void reset() { for (int i = 0; i < devices.size(); ++i) @@ -1584,21 +1591,8 @@ private: callback->audioDeviceAboutToStart (device); } - void handleAudioDeviceStopped() - { - const ScopedLock sl (callbackLock); - - if (callback != nullptr) - callback->audioDeviceStopped(); - } - - void handleAudioDeviceError (const String& errorMessage) - { - const ScopedLock sl (callbackLock); - - if (callback != nullptr) - callback->audioDeviceError (errorMessage); - } + void handleAudioDeviceStopped() { shutdown ({}); } + void handleAudioDeviceError (const String& errorMessage) { shutdown (errorMessage.isNotEmpty() ? errorMessage : String ("unknown")); } //============================================================================== struct DeviceWrapper : private AudioIODeviceCallback