Skip to content

Commit

Permalink
CoreAudio: Prevent racing between calls to start and stop a device
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony-Nicholls committed Jun 11, 2024
1 parent 113009c commit b7d5f64
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions modules/juce_audio_devices/native/juce_CoreAudio_mac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ class CoreAudioInternal final : private Timer,

CoreAudioIODevice& owner;
int bitDepth = 32;
int xruns = 0;
std::atomic<int> xruns = 0;
Array<double> sampleRates;
Array<int> bufferSizes;
AudioDeviceID deviceID;
Expand Down Expand Up @@ -1147,7 +1147,7 @@ class CoreAudioInternal final : private Timer,
return x.mSelector == kAudioDeviceProcessorOverload;
});

intern.xruns += xruns;
intern.xruns += (int) xruns;

const auto detailsChanged = std::any_of (pa, pa + numAddresses, [] (const AudioObjectPropertyAddress& x)
{
Expand Down Expand Up @@ -1313,19 +1313,23 @@ class CoreAudioIODevice final : public AudioIODevice,

void start (AudioIODeviceCallback* callback) override
{
const ScopedLock sl (startStopLock);

if (internal->start (callback))
pendingCallback = nullptr;
}

void stop() override
{
stopAndGetLastCallback();

const ScopedLock sl (startStopLock);
pendingCallback = nullptr;
}

void stopWithPendingCallback()
{
const ScopedLock sl (closeLock);
const ScopedLock sl (startStopLock);

if (pendingCallback == nullptr)
pendingCallback = stopAndGetLastCallback();
Expand Down Expand Up @@ -1387,7 +1391,7 @@ class CoreAudioIODevice final : public AudioIODevice,
AudioIODeviceCallback* pendingCallback = nullptr;
AsyncRestarter* restarter = nullptr;
BigInteger inputChannelsRequested, outputChannelsRequested;
CriticalSection closeLock;
CriticalSection startStopLock;

AudioIODeviceCallback* stopAndGetLastCallback() const
{
Expand All @@ -1412,6 +1416,7 @@ class CoreAudioIODevice final : public AudioIODevice,
getCurrentSampleRate(),
getCurrentBufferSizeSamples());

const ScopedLock sl { startStopLock };
start (pendingCallback);
}

Expand Down

0 comments on commit b7d5f64

Please sign in to comment.