File size: 3,293 Bytes
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#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 "../Shared/SharedAudioBuffer.h"
#include "../Shared/IPCProtocol.h"
#include "../Shared/Constants.h"
#include "HelperPluginHost.h"
#include "HelperIPC.h"

// PluginBridgeHelper — standalone process that:
// 1. Connects to parent via Unix socket
// 2. Opens shared memory for audio
// 3. Loads third-party plugins
// 4. Runs processBlock when signaled
// 5. Shows plugin GUI in its own window
// 6. If it crashes, only this process dies — Ableton survives

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
    {
        // Parse args: --pid <parentPid>
        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));

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

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

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

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

        // Give plugin host access to shared audio + semaphores
        pluginHost->setSharedAudio(&sharedAudio, &inputSem, &outputSem);

        // Start audio processing thread
        pluginHost->startProcessing();

        // Notify parent we're ready
        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)