Skip to content

Commit

Permalink
Host side of AUv3 IPC forwards incoming transactions to dedicated dis…
Browse files Browse the repository at this point in the history
…patch queues

because AUMessageChannel does not allow to call back from the receiving thread...
  • Loading branch information
sgretscher committed Jan 22, 2024
1 parent 09ae0f3 commit 02c6a65
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions IPC/ARAIPCAudioUnit_v3.mm
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ void _sendMessage (MessageID messageID, MessageEncoder * encoder) override
: AudioUnitMessageChannel { this },
_audioUnitChannel { audioUnitChannel }
{
// \todo there's also QOS_CLASS_USER_INTERACTIVE which seems more appropriate but is undocumented...
if (_instanceCount == 0)
_readAudioQueue = dispatch_queue_create ("ARA read audio samples", dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, -1));
++_instanceCount;

#if !__has_feature(objc_arc)
[_audioUnitChannel retain];
#endif
Expand All @@ -180,6 +185,27 @@ void _sendMessage (MessageID messageID, MessageEncoder * encoder) override
~ProxyPlugInMessageChannel () override
{
_audioUnitChannel.callHostBlock = nil;

--_instanceCount;
if (_instanceCount == 0)
dispatch_release (_readAudioQueue);
}

DispatchTarget getDispatchTargetForIncomingTransaction (MessageID messageID) override
{
// AUMessageChannel cannot be called back from the same thread it receives the message,
// so we dispatch to the main queue for playback requests and to a dedicated read samples queue for audio requests
// \todo maybe we should make this configurable, so hosts can set these queues if they already have appropriate ones?
if (messageID == ARA_IPC_HOST_METHOD_ID (ARAAudioAccessControllerInterface, readAudioSamples).getMessageID ())
{
return _readAudioQueue;
}
else
{
ARA_INTERNAL_ASSERT ((ARA_IPC_HOST_METHOD_ID (ARAPlaybackControllerInterface, requestStartPlayback).getMessageID () <= messageID) &&
(messageID <= ARA_IPC_HOST_METHOD_ID (ARAPlaybackControllerInterface, requestEnableCycle).getMessageID ()));
return dispatch_get_main_queue ();
}
}

protected:
Expand All @@ -191,8 +217,13 @@ void _sendMessage (MessageID messageID, MessageEncoder * encoder) override

private:
NSObject<AUMessageChannel> * __strong _Nonnull _audioUnitChannel;
static dispatch_queue_t _readAudioQueue;
static int _instanceCount;
};

dispatch_queue_t ProxyPlugInMessageChannel::_readAudioQueue { nullptr };
int ProxyPlugInMessageChannel::_instanceCount { 0 };


// host side: proxy plug-in message channel further specialization for plug-in extension messages
class ProxyPlugInExtensionMessageChannel : public ProxyPlugInMessageChannel
Expand Down

0 comments on commit 02c6a65

Please sign in to comment.