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 authored and tpoole committed Jun 12, 2024
1 parent 2516ad8 commit 4808fdc
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 @@ -1051,7 +1051,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 @@ -1159,7 +1159,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 @@ -1325,19 +1325,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 @@ -1399,7 +1403,7 @@ class CoreAudioIODevice final : public AudioIODevice,
AudioIODeviceCallback* pendingCallback = nullptr;
AsyncRestarter* restarter = nullptr;
BigInteger inputChannelsRequested, outputChannelsRequested;
CriticalSection closeLock;
CriticalSection startStopLock;

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

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

Expand Down

0 comments on commit 4808fdc

Please sign in to comment.