File size: 2,647 Bytes
5ac6c6e
 
 
 
 
a119e77
 
 
5ac6c6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <juce_audio_processors/juce_audio_processors.h>
#include <juce_audio_utils/juce_audio_utils.h>
#include <juce_gui_basics/juce_gui_basics.h>
#include <juce_events/juce_events.h>

#include "SharedAudioBuffer.h"
#include "IPCProtocol.h"
#include "Constants.h"
#include "HelperPluginHost.h"
#include "HelperIPC.h"

class HelperApplication : public juce::JUCEApplication
{
public:
    const juce::String getApplicationName() override { return "PluginBridgeHelper"; }
    const juce::String getApplicationVersion() override { return PluginBridgeConstants::kVersion; }

    void initialise(const juce::String& commandLine) override
    {
        auto args = juce::StringArray::fromTokens(commandLine, " ", "\"");

        int parentPid = 0;
        for (int i = 0; i < args.size() - 1; ++i)
        {
            if (args[i] == "--pid")
                parentPid = args[i + 1].getIntValue();
        }

        if (parentPid == 0)
        {
            DBG("HelperApplication: --pid argument required");
            quit();
            return;
        }

        DBG("HelperApplication: Starting for parent PID " + juce::String(parentPid));

        pluginHost = std::make_unique<HelperPluginHost>();
        ipcServer = std::make_unique<HelperIPC>(parentPid, *pluginHost);

        auto shmName = IPC::getShmName(parentPid);
        if (!sharedAudio.open(shmName))
        {
            DBG("HelperApplication: Failed to open shared memory: " + shmName);
            quit();
            return;
        }

        if (!inputSem.open(IPC::getInputSemName(parentPid)) ||
            !outputSem.open(IPC::getOutputSemName(parentPid)))
        {
            DBG("HelperApplication: Failed to open semaphores");
            quit();
            return;
        }

        if (!ipcServer->connect())
        {
            DBG("HelperApplication: Failed to connect to parent IPC socket");
            quit();
            return;
        }

        pluginHost->setSharedAudio(&sharedAudio, &inputSem, &outputSem);
        pluginHost->startProcessing();
        ipcServer->sendReady();

        DBG("HelperApplication: Ready and running");
    }

    void shutdown() override
    {
        if (pluginHost)
            pluginHost->stopProcessing();

        ipcServer.reset();
        pluginHost.reset();
        sharedAudio.destroy();
    }

    void systemRequestedQuit() override
    {
        quit();
    }

private:
    std::unique_ptr<HelperPluginHost> pluginHost;
    std::unique_ptr<HelperIPC> ipcServer;
    SharedAudioBuffer sharedAudio;
    SharedSemaphore inputSem;
    SharedSemaphore outputSem;
};

START_JUCE_APPLICATION(HelperApplication)