Skip to content

Commit

Permalink
Introduce proper "Ref" wrapper types for C++ IPC data types used in t…
Browse files Browse the repository at this point in the history
…he C interface
  • Loading branch information
sgretscher committed Feb 11, 2024
1 parent 0b57e78 commit 2b9d128
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 43 deletions.
4 changes: 2 additions & 2 deletions ExamplesCommon/PlugInHosting/AudioUnitLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ AudioUnitComponent AudioUnitPrepareComponentWithIDs(OSType type, OSType subtype,
bool AudioUnitIsV2(AudioUnitComponent audioUnitComponent);
AudioUnitInstance AudioUnitOpenInstance(AudioUnitComponent audioUnitComponent, bool useIPC);
// On return, *messageChannel will be NULL if Audio Unit does not use IPC, otherwise it will point to
// a valid ARAIPCMessageChannel for all factory-related calls until AudioUnitCleanupComponent() is called.
const ARA_NAMESPACE ARAFactory * AudioUnitGetARAFactory(AudioUnitInstance audioUnit, ARA_IPC_NAMESPACE ARAIPCMessageChannel ** messageChannel);
// a valid message channel for all factory-related calls until AudioUnitCleanupComponent() is called.
const ARA_NAMESPACE ARAFactory * AudioUnitGetARAFactory(AudioUnitInstance audioUnit, ARA_IPC_NAMESPACE ARAIPCMessageChannelRef * messageChannelRef);
const ARA_NAMESPACE ARAPlugInExtensionInstance * AudioUnitBindToARADocumentController(AudioUnitInstance audioUnit, ARA_NAMESPACE ARADocumentControllerRef controllerRef, ARA_NAMESPACE ARAPlugInInstanceRoleFlags assignedRoles);
void AudioUnitStartRendering(AudioUnitInstance audioUnit, UInt32 maxBlockSize, double sampleRate);
void AudioUnitRenderBuffer(AudioUnitInstance audioUnit, UInt32 blockSize, SInt64 samplePosition, float * buffer);
Expand Down
28 changes: 14 additions & 14 deletions ExamplesCommon/PlugInHosting/AudioUnitLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
{
AudioComponent component;
#if ARA_AUDIOUNITV3_IPC_IS_AVAILABLE
const ARAFactory * ipcFactory; // cache - NULL until read via IPC
ARAIPCMessageChannel * factoryIPCChannel; // invalid until ipcFactory has been read via IPC
const ARAFactory * ipcFactory; // cache - NULL until read via IPC
ARAIPCMessageChannelRef factoryIPCChannelRef; // invalid until ipcFactory has been read via IPC
#endif
};

Expand All @@ -52,11 +52,11 @@
AUAudioUnit * __strong v3AudioUnit;
};
SInt64 samplePosition;
AURenderBlock v3RenderBlock; // only for AUv3: cache of render block outside ObjC runtime
AURenderBlock v3RenderBlock; // only for AUv3: cache of render block outside ObjC runtime
#if ARA_AUDIOUNITV3_IPC_IS_AVAILABLE
BOOL isOutOfProcess; // loaded in- or out-of-process
BOOL isBoundViaIPC; // YES if extensionIPCChannel has been initialized via ARA binding
ARAIPCMessageChannel * extensionIPCChannel; // only valid when isBoundViaIPC is YES
BOOL isOutOfProcess; // loaded in- or out-of-process
BOOL isBoundViaIPC; // YES if extensionIPCChannelRef has been initialized via ARA binding
ARAIPCMessageChannelRef extensionIPCChannelRef; // only valid when isBoundViaIPC is YES
#endif
};

Expand Down Expand Up @@ -185,10 +185,10 @@ AudioUnitInstance AudioUnitOpenInstance(AudioUnitComponent audioUnitComponent, b
return result;
}

const ARAFactory * AudioUnitGetARAFactory(AudioUnitInstance audioUnitInstance, struct ARAIPCMessageChannel ** messageChannel)
const ARAFactory * AudioUnitGetARAFactory(AudioUnitInstance audioUnitInstance, ARAIPCMessageChannelRef * messageChannelRef)
{
const ARAFactory * result = NULL; // initially assume this plug-in doesn't support ARA
*messageChannel = NULL;
*messageChannelRef = NULL;

// check whether the AU supports ARA by trying to get the factory
if (audioUnitInstance->isAUv2)
Expand Down Expand Up @@ -221,12 +221,12 @@ AudioUnitInstance AudioUnitOpenInstance(AudioUnitComponent audioUnitComponent, b
{
if (!audioUnitInstance->audioUnitComponent->ipcFactory)
{
if ((audioUnitInstance->audioUnitComponent->factoryIPCChannel =
if ((audioUnitInstance->audioUnitComponent->factoryIPCChannelRef =
ARAIPCAUProxyPlugInInitializeFactoryMessageChannel(audioUnitInstance->v3AudioUnit)))
audioUnitInstance->audioUnitComponent->ipcFactory = ARAIPCAUProxyPlugInGetFactory(audioUnitInstance->audioUnitComponent->factoryIPCChannel);
audioUnitInstance->audioUnitComponent->ipcFactory = ARAIPCAUProxyPlugInGetFactory(audioUnitInstance->audioUnitComponent->factoryIPCChannelRef);
}
if ((result = audioUnitInstance->audioUnitComponent->ipcFactory))
*messageChannel = audioUnitInstance->audioUnitComponent->factoryIPCChannel;
*messageChannelRef = audioUnitInstance->audioUnitComponent->factoryIPCChannelRef;
}
}
else
Expand Down Expand Up @@ -298,7 +298,7 @@ AudioUnitInstance AudioUnitOpenInstance(AudioUnitComponent audioUnitComponent, b
{
if (@available(macOS 13.0, *))
{
instance = ARAIPCAUProxyPlugInBindToDocumentController (audioUnitInstance->v3AudioUnit, controllerRef, knownRoles, assignedRoles, &audioUnitInstance->extensionIPCChannel);
instance = ARAIPCAUProxyPlugInBindToDocumentController (audioUnitInstance->v3AudioUnit, controllerRef, knownRoles, assignedRoles, &audioUnitInstance->extensionIPCChannelRef);
audioUnitInstance->isBoundViaIPC = (instance != NULL);
}
}
Expand Down Expand Up @@ -532,7 +532,7 @@ void AudioUnitCloseInstance(AudioUnitInstance audioUnitInstance)
if (@available(macOS 13.0, *))
{
if (audioUnitInstance->isBoundViaIPC)
ARAIPCAUProxyPlugInCleanupBinding(audioUnitInstance->extensionIPCChannel);
ARAIPCAUProxyPlugInCleanupBinding(audioUnitInstance->extensionIPCChannelRef);
}
#endif
[audioUnitInstance->v3AudioUnit release];
Expand All @@ -546,7 +546,7 @@ void AudioUnitCleanupComponent(AudioUnitComponent audioUnitComponent)
if (@available(macOS 13.0, *))
{
if (audioUnitComponent->ipcFactory)
ARAIPCAUProxyPlugInUninitializeFactoryMessageChannel(audioUnitComponent->factoryIPCChannel);
ARAIPCAUProxyPlugInUninitializeFactoryMessageChannel(audioUnitComponent->factoryIPCChannelRef);
}
#endif

Expand Down
6 changes: 3 additions & 3 deletions MiniHost/MiniHost.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,9 @@ int main(int argc, const char * argv[])
ARA_INTERNAL_ASSERT(audioUnitComponent != NULL);
AudioUnitInstance audioUnit = AudioUnitOpenInstance(audioUnitComponent, false);

ARAIPCMessageChannel * messageChannel;
factory = AudioUnitGetARAFactory(audioUnit, &messageChannel);
if (messageChannel != NULL)
ARAIPCMessageChannelRef messageChannelRef;
factory = AudioUnitGetARAFactory(audioUnit, &messageChannelRef);
if (messageChannelRef != NULL)
{
ARA_WARN("this plug-in requires to use IPC, which is not supported in this simple example.");
return -1;
Expand Down
53 changes: 33 additions & 20 deletions TestHost/CompanionAPIs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
#include "IPC/IPCXMLEncoding.h"
#endif


std::string executablePath {};

// minimal set of commands to run a companion API plug-in through IPC
Expand All @@ -83,6 +84,18 @@ constexpr auto kIPCDestroyEffectMethodID { ARA::IPC::MethodID::createWithNonARAM
constexpr auto kIPCTerminateMethodID { ARA::IPC::MethodID::createWithNonARAMethodID<-6> () };


#if defined (__GNUC__)
_Pragma ("GCC diagnostic push")
_Pragma ("GCC diagnostic ignored \"-Wunused-template\"")
#endif

ARA_MAP_IPC_REF (ARA::IPC::ARAIPCMessageChannel, ARA::IPC::ARAIPCMessageChannelRef)

#if defined (__GNUC__)
_Pragma ("GCC diagnostic pop")
#endif


// helper function to create unique IPC message channel IDs for each run
static const std::string _createChannelID ()
{
Expand Down Expand Up @@ -311,7 +324,7 @@ class AUPlugInEntry : public PlugInEntry
_audioUnitComponent { AudioUnitPrepareComponentWithIDs (parseOSType (type), parseOSType (subType), parseOSType (manufacturer)) }
{
AudioUnitInstance audioUnitInstance = AudioUnitOpenInstance (_audioUnitComponent, useIPCIfPossible);
validateAndSetFactory (AudioUnitGetARAFactory (audioUnitInstance, &_messageChannel));
validateAndSetFactory (AudioUnitGetARAFactory (audioUnitInstance, &_messageChannelRef));
AudioUnitCloseInstance (audioUnitInstance);
}

Expand All @@ -322,13 +335,13 @@ class AUPlugInEntry : public PlugInEntry

bool usesIPC () const override
{
return _messageChannel != nullptr;
return _messageChannelRef != nullptr;
}

void initializeARA (ARA::ARAAssertFunction* assertFunctionAddress) override
{
if (usesIPC ())
ARA::IPC::ARAIPCProxyPlugInInitializeARA (_messageChannel, getARAFactory ()->factoryID, getDesiredAPIGeneration (getARAFactory ()));
ARA::IPC::ARAIPCProxyPlugInInitializeARA (_messageChannelRef, getARAFactory ()->factoryID, getDesiredAPIGeneration (getARAFactory ()));
else
PlugInEntry::initializeARA (assertFunctionAddress);
}
Expand All @@ -337,8 +350,8 @@ class AUPlugInEntry : public PlugInEntry
const ARA::ARADocumentProperties* properties) override
{
if (usesIPC ())
return ARA::IPC::ARAIPCProxyPlugInCreateDocumentControllerWithDocument (_messageChannel,
ARA::IPC::ARAIPCProxyPlugInGetFactoryAtIndex (_messageChannel, 0)->factoryID,
return ARA::IPC::ARAIPCProxyPlugInCreateDocumentControllerWithDocument (_messageChannelRef,
ARA::IPC::ARAIPCProxyPlugInGetFactoryAtIndex (_messageChannelRef, 0)->factoryID,
hostInstance, properties);
else
return PlugInEntry::createDocumentControllerWithDocument (hostInstance, properties);
Expand All @@ -347,7 +360,7 @@ class AUPlugInEntry : public PlugInEntry
void uninitializeARA () override
{
if (usesIPC ())
ARA::IPC::ARAIPCProxyPlugInUninitializeARA (_messageChannel, getARAFactory ()->factoryID);
ARA::IPC::ARAIPCProxyPlugInUninitializeARA (_messageChannelRef, getARAFactory ()->factoryID);
else
PlugInEntry::uninitializeARA ();
}
Expand All @@ -360,7 +373,7 @@ class AUPlugInEntry : public PlugInEntry

private:
AudioUnitComponent const _audioUnitComponent;
ARA::IPC::ARAIPCMessageChannel* _messageChannel {};
ARA::IPC::ARAIPCMessageChannelRef _messageChannelRef {};
};

#endif // defined (__APPLE__)
Expand Down Expand Up @@ -443,7 +456,7 @@ class CLAPPlugInEntry : public PlugInEntry
class IPCPlugInInstance : public PlugInInstance, protected ARA::IPC::RemoteCaller
{
public:
IPCPlugInInstance (size_t remoteRef, ARA::IPC::ARAIPCMessageChannel* messageChannel)
IPCPlugInInstance (ARA::IPC::ARAIPCPlugInInstanceRef remoteRef, ARA::IPC::ARAIPCMessageChannel* messageChannel)
: RemoteCaller { messageChannel },
_remoteRef { remoteRef }
{}
Expand All @@ -459,7 +472,7 @@ class IPCPlugInInstance : public PlugInInstance, protected ARA::IPC::RemoteCalle
{
// \todo these are the roles that our Companion API Loaders implicitly assume - they should be published properly
const ARA::ARAPlugInInstanceRoleFlags knownRoles { ARA::kARAPlaybackRendererRole | ARA::kARAEditorRendererRole | ARA::kARAEditorViewRole };
auto plugInExtension { ARA::IPC::ARAIPCProxyPlugInBindToDocumentController (_remoteRef, getMessageChannel (), documentControllerRef, knownRoles, assignedRoles) };
auto plugInExtension { ARA::IPC::ARAIPCProxyPlugInBindToDocumentController (_remoteRef, toIPCRef (getMessageChannel ()), documentControllerRef, knownRoles, assignedRoles) };
validateAndSetPlugInExtensionInstance (plugInExtension, assignedRoles);
}

Expand All @@ -484,7 +497,7 @@ class IPCPlugInInstance : public PlugInInstance, protected ARA::IPC::RemoteCalle
}

private:
size_t const _remoteRef;
ARA::IPC::ARAIPCPlugInInstanceRef const _remoteRef;
};


Expand All @@ -511,7 +524,7 @@ struct RemoteLauncher
class IPCPlugInEntry : public PlugInEntry, private RemoteLauncher, protected ARA::IPC::RemoteCaller, public ARA::IPC::ARAIPCProxyPlugInMessageHandler
{
private:
static const ARA::ARAFactory* defaultGetFactory (ARA::IPC::ARAIPCMessageChannel* messageChannel)
static const ARA::ARAFactory* defaultGetFactory (ARA::IPC::ARAIPCMessageChannelRef messageChannel)
{
const auto count { ARA::IPC::ARAIPCProxyPlugInGetFactoriesCount (messageChannel) };
ARA_INTERNAL_ASSERT (count > 0);
Expand All @@ -520,18 +533,18 @@ class IPCPlugInEntry : public PlugInEntry, private RemoteLauncher, protected ARA

IPCPlugInEntry (std::string&& description, const std::string& launchArgs,
const std::string channelID,
const std::function<const ARA::ARAFactory* (ARA::IPC::ARAIPCMessageChannel*)>& getFactoryFunction)
const std::function<const ARA::ARAFactory* (ARA::IPC::ARAIPCMessageChannelRef)>& getFactoryFunction)
: PlugInEntry { std::move (description) },
RemoteLauncher { launchArgs, channelID },
ARA::IPC::RemoteCaller { IPCMessageChannel::createConnectedToID (channelID, this) }
{
validateAndSetFactory (getFactoryFunction (static_cast<IPCMessageChannel*> (getMessageChannel ())));
validateAndSetFactory (getFactoryFunction (toIPCRef (getMessageChannel ())));
}

public:
// \todo the current ARA IPC implementation does not support sending ARA asserts to the host...
IPCPlugInEntry (std::string&& description, const std::string& launchArgs,
const std::function<const ARA::ARAFactory* (ARA::IPC::ARAIPCMessageChannel*)>& getFactoryFunction = defaultGetFactory)
const std::function<const ARA::ARAFactory* (ARA::IPC::ARAIPCMessageChannelRef)>& getFactoryFunction = defaultGetFactory)
: IPCPlugInEntry { std::move (description), launchArgs, _createChannelID (), getFactoryFunction }
{}

Expand All @@ -556,23 +569,23 @@ class IPCPlugInEntry : public PlugInEntry, private RemoteLauncher, protected ARA

void initializeARA (ARA::ARAAssertFunction* /*assertFunctionAddress*/) override
{
ARA::IPC::ARAIPCProxyPlugInInitializeARA (getMessageChannel (), getARAFactory ()->factoryID, getDesiredAPIGeneration (getARAFactory ()));
ARA::IPC::ARAIPCProxyPlugInInitializeARA (toIPCRef (getMessageChannel ()), getARAFactory ()->factoryID, getDesiredAPIGeneration (getARAFactory ()));
}

const ARA::ARADocumentControllerInstance* createDocumentControllerWithDocument (const ARA::ARADocumentControllerHostInstance* hostInstance,
const ARA::ARADocumentProperties* properties) override
{
return ARA::IPC::ARAIPCProxyPlugInCreateDocumentControllerWithDocument (getMessageChannel (), getARAFactory ()->factoryID, hostInstance, properties);
return ARA::IPC::ARAIPCProxyPlugInCreateDocumentControllerWithDocument (toIPCRef (getMessageChannel ()), getARAFactory ()->factoryID, hostInstance, properties);
}

void uninitializeARA () override
{
ARA::IPC::ARAIPCProxyPlugInUninitializeARA (getMessageChannel (), getARAFactory ()->factoryID);
ARA::IPC::ARAIPCProxyPlugInUninitializeARA (toIPCRef (getMessageChannel ()), getARAFactory ()->factoryID);
}

std::unique_ptr<PlugInInstance> createPlugInInstance () override
{
size_t remoteInstanceRef {};
ARA::IPC::ARAIPCPlugInInstanceRef remoteInstanceRef {};
remoteCall (remoteInstanceRef, kIPCCreateEffectMethodID);
return std::make_unique<IPCPlugInInstance> (remoteInstanceRef, getMessageChannel ());
}
Expand All @@ -586,7 +599,7 @@ class IPCGenericPlugInEntry : public IPCPlugInEntry
IPCGenericPlugInEntry (const std::string& commandLineArg, const std::string& apiName, const std::string& binaryName, const std::string& optionalPlugInName)
: IPCPlugInEntry { createEntryDescription (apiName, binaryName, optionalPlugInName),
commandLineArg + " " + binaryName + " " + optionalPlugInName,
[&optionalPlugInName] (ARA::IPC::ARAIPCMessageChannel* messageChannel) -> const ARA::ARAFactory*
[&optionalPlugInName] (ARA::IPC::ARAIPCMessageChannelRef messageChannel) -> const ARA::ARAFactory*
{
const auto count { ARA::IPC::ARAIPCProxyPlugInGetFactoriesCount (messageChannel) };
ARA_INTERNAL_ASSERT (count > 0);
Expand Down Expand Up @@ -720,7 +733,7 @@ int main (std::unique_ptr<PlugInEntry> plugInEntry, const std::string& channelID
auto plugInCallbacksChannel { IPCMessageChannel::createPublishingID (channelID, &handler) };

ARA::IPC::ARAIPCProxyHostAddFactory (_plugInEntry->getARAFactory ());
ARA::IPC::ARAIPCProxyHostSetBindingHandler ([] (ARA::IPC::ARAIPCMessageChannel* /*messageChannel*/, ARA::IPC::ARAIPCPlugInInstanceRef plugInInstanceRef,
ARA::IPC::ARAIPCProxyHostSetBindingHandler ([] (ARA::IPC::ARAIPCMessageChannelRef /*messageChannel*/, ARA::IPC::ARAIPCPlugInInstanceRef plugInInstanceRef,
ARA::ARADocumentControllerRef controllerRef,
ARA::ARAPlugInInstanceRoleFlags knownRoles, ARA::ARAPlugInInstanceRoleFlags assignedRoles)
-> const ARA::ARAPlugInExtensionInstance*
Expand Down
8 changes: 4 additions & 4 deletions TestPlugIn/AudioUnit_v3/Framework/TestAUv3AudioUnit.mm
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ @interface TestAUv3ARAIPCMessageChannel : NSObject<AUMessageChannel>


@implementation TestAUv3ARAIPCMessageChannel {
ARA::IPC::ARAIPCMessageChannel* _messageChannel;
ARA::IPC::ARAIPCMessageChannelRef _messageChannelRef;
}

@synthesize callHostBlock = _callHostBlock;
Expand All @@ -54,17 +54,17 @@ - (instancetype)initWithAudioUnit:(AUAudioUnit * _Nullable)audioUnit {
if (self == nil) { return nil; }

_callHostBlock = nil;
_messageChannel = ARA::IPC::ARAIPCAUProxyHostInitializeMessageChannel(audioUnit, self);
_messageChannelRef = ARA::IPC::ARAIPCAUProxyHostInitializeMessageChannel(audioUnit, self);

return self;
}

- (void)dealloc {
ARA::IPC::ARAIPCAUProxyHostUninitializeMessageChannel(_messageChannel);
ARA::IPC::ARAIPCAUProxyHostUninitializeMessageChannel(_messageChannelRef);
}

- (NSDictionary * _Nonnull)callAudioUnit:(NSDictionary *)message {
return ARA::IPC::ARAIPCAUProxyHostCommandHandler(_messageChannel, message);
return ARA::IPC::ARAIPCAUProxyHostCommandHandler(_messageChannelRef, message);
}

@end
Expand Down

0 comments on commit 2b9d128

Please sign in to comment.