Rename a pipe method, add docs
[carla.git] / source / backend / engine / CarlaEngineRunner.cpp
blobfef3f18501d20b6b6be272339676a19a1ff75f60
1 /*
2 * Carla Plugin Host
3 * Copyright (C) 2011-2022 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include "CarlaEngineRunner.hpp"
19 #include "CarlaEngineInternal.hpp"
20 #include "CarlaPlugin.hpp"
22 #include "water/misc/Time.h"
24 CARLA_BACKEND_START_NAMESPACE
26 // -----------------------------------------------------------------------
28 CarlaEngineRunner::CarlaEngineRunner(CarlaEngine* const engine) noexcept
29 : CarlaRunner("CarlaEngineRunner"),
30 kEngine(engine),
31 fEngineHasIdleOnMainThread(false),
32 fIsAlwaysRunning(false),
33 fIsPlugin(false)
35 CARLA_SAFE_ASSERT(engine != nullptr);
36 carla_debug("CarlaEngineRunner::CarlaEngineRunner(%p)", engine);
39 CarlaEngineRunner::~CarlaEngineRunner() noexcept
41 carla_debug("CarlaEngineRunner::~CarlaEngineRunner()");
44 void CarlaEngineRunner::start()
46 carla_debug("CarlaEngineRunner::start()");
47 if (isRunnerActive())
48 stopRunner();
50 fEngineHasIdleOnMainThread = kEngine->hasIdleOnMainThread();
51 fIsPlugin = kEngine->getType() == kEngineTypePlugin;
52 fIsAlwaysRunning = kEngine->getType() == kEngineTypeBridge || fIsPlugin;
54 startRunner(25);
57 void CarlaEngineRunner::stop()
59 carla_debug("CarlaEngineRunner::stop()");
60 stopRunner();
63 // -----------------------------------------------------------------------
65 bool CarlaEngineRunner::run() noexcept
67 CARLA_SAFE_ASSERT_RETURN(kEngine != nullptr, false);
69 float value;
71 #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
72 // int64_t lastPingTime = 0;
73 const CarlaEngineOsc& engineOsc(kEngine->pData->osc);
74 #endif
76 // runner must do something...
77 CARLA_SAFE_ASSERT_RETURN(fIsAlwaysRunning || kEngine->isRunning(), false);
79 #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
80 const bool oscRegistedForUDP = engineOsc.isControlRegisteredForUDP();
81 #else
82 const bool oscRegistedForUDP = false;
83 #endif
85 #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
86 if (fIsPlugin)
87 engineOsc.idle();
88 #endif
90 for (uint i=0, count = kEngine->getCurrentPluginCount(); i < count; ++i)
92 const CarlaPluginPtr plugin = kEngine->getPluginUnchecked(i);
94 CARLA_SAFE_ASSERT_CONTINUE(plugin.get() != nullptr && plugin->isEnabled());
95 CARLA_SAFE_ASSERT_UINT2(i == plugin->getId(), i, plugin->getId());
97 const uint hints = plugin->getHints();
98 const bool useIdle = (hints & PLUGIN_NEEDS_MAIN_THREAD_IDLE) == 0 || !fEngineHasIdleOnMainThread;
99 const bool updateUI = (hints & PLUGIN_HAS_CUSTOM_UI) != 0 && (hints & PLUGIN_NEEDS_UI_MAIN_THREAD) == 0;
101 // -----------------------------------------------------------
102 // DSP Idle
104 if (useIdle)
106 try {
107 plugin->idle();
108 } CARLA_SAFE_EXCEPTION("idle()")
111 // -----------------------------------------------------------
112 // Post-poned events
114 if (oscRegistedForUDP || updateUI)
116 // -------------------------------------------------------
117 // Update parameter outputs
119 for (uint32_t j=0, pcount=plugin->getParameterCount(); j < pcount; ++j)
121 if (! plugin->isParameterOutput(j))
122 continue;
124 value = plugin->getParameterValue(j);
126 #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
127 // Update OSC engine client
128 if (oscRegistedForUDP)
129 engineOsc.sendParameterValue(i, j, value);
130 #endif
131 // Update UI
132 if (updateUI)
133 plugin->uiParameterChange(j, value);
136 if (updateUI)
138 try {
139 plugin->uiIdle();
140 } CARLA_SAFE_EXCEPTION("uiIdle()")
144 #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
145 // -----------------------------------------------------------
146 // Update OSC control client peaks
148 if (oscRegistedForUDP)
149 engineOsc.sendPeaks(i, kEngine->getPeaks(i));
150 #endif
153 #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
154 if (oscRegistedForUDP)
155 engineOsc.sendRuntimeInfo();
158 if (engineOsc.isControlRegisteredForTCP())
160 const int64_t timeNow = water::Time::currentTimeMillis();
162 if (timeNow - lastPingTime > 1000)
164 engineOsc.sendPing();
165 lastPingTime = timeNow;
169 #endif
171 return true;
174 // -----------------------------------------------------------------------
176 CARLA_BACKEND_END_NAMESPACE