|
1 | 1 | #import <AVFAudio/AVFAudio.h> |
2 | 2 | #import <AudioToolbox/AudioToolbox.h> |
3 | | -#include "util/assert.h" |
| 3 | +#import <dispatch/dispatch.h> |
4 | 4 |
|
5 | 5 | #include <QString> |
6 | 6 |
|
7 | 7 | #include "effects/backends/audiounit/audiounitmanager.h" |
| 8 | +#include "util/assert.h" |
8 | 9 |
|
9 | 10 | AudioUnitManager::AudioUnitManager(AVAudioUnitComponent* _Nullable component) |
10 | 11 | : m_name(component != nil ? QString::fromNSString([component name]) |
11 | 12 | : "Unknown"), |
12 | | - m_isInstantiated(false) { |
| 13 | + m_isInstantiated(false), |
| 14 | + m_instantiationGroup(dispatch_group_create()) { |
13 | 15 | } |
14 | 16 |
|
15 | 17 | AudioUnitManagerPointer AudioUnitManager::create( |
|
56 | 58 | return m_audioUnit; |
57 | 59 | } |
58 | 60 |
|
| 61 | +bool AudioUnitManager::waitForAudioUnit(int timeoutMs) const { |
| 62 | + bool success = |
| 63 | + dispatch_group_wait(m_instantiationGroup, |
| 64 | + dispatch_time(DISPATCH_TIME_NOW, timeoutMs * 1000000)) == 0; |
| 65 | + DEBUG_ASSERT(!success || m_isInstantiated.load()); |
| 66 | + return success; |
| 67 | +} |
| 68 | + |
59 | 69 | void AudioUnitManager::instantiateAudioUnitAsync( |
60 | 70 | AudioUnitManagerPointer pManager, |
61 | 71 | AVAudioUnitComponent* _Nonnull component, |
|
75 | 85 | qDebug() << "Instantiating Audio Unit" << pManager->m_name |
76 | 86 | << "asynchronously"; |
77 | 87 |
|
| 88 | + dispatch_group_enter(pManager->m_instantiationGroup); |
| 89 | + |
78 | 90 | // TODO: Fix the weird formatting of blocks |
79 | 91 | // clang-format off |
80 | 92 | AudioComponentInstantiate(component.audioComponent, options, ^(AudioUnit _Nullable audioUnit, OSStatus error) { |
81 | 93 | if (error != noErr) { |
82 | 94 | qWarning() << "Could not instantiate Audio Unit" |
83 | 95 | << pManager->m_name << ":" << error |
84 | 96 | << "(Check https://www.osstatus.com for a description)"; |
| 97 | + dispatch_group_leave(pManager->m_instantiationGroup); |
85 | 98 | return; |
86 | 99 | } |
87 | 100 |
|
88 | 101 | pManager->initializeWith(audioUnit); |
| 102 | + dispatch_group_leave(pManager->m_instantiationGroup); |
89 | 103 | }); |
90 | 104 | // clang-format on |
91 | 105 | } |
|
0 commit comments