Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
5.8KB

  1. /*
  2. * Carla Plugin Thread
  3. * Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  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.
  9. *
  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.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginThread.hpp"
  18. #include "CarlaPlugin.hpp"
  19. #include "CarlaEngine.hpp"
  20. #ifdef USE_JUCE
  21. using juce::ChildProcess;
  22. using juce::String;
  23. using juce::StringArray;
  24. #endif
  25. CARLA_BACKEND_START_NAMESPACE
  26. const char* PluginThreadMode2str(const CarlaPluginThread::Mode mode)
  27. {
  28. switch (mode)
  29. {
  30. case CarlaPluginThread::PLUGIN_THREAD_NULL:
  31. return "PLUGIN_THREAD_NULL";
  32. case CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI:
  33. return "PLUGIN_THREAD_DSSI_GUI";
  34. case CarlaPluginThread::PLUGIN_THREAD_LV2_GUI:
  35. return "PLUGIN_THREAD_LV2_GUI";
  36. case CarlaPluginThread::PLUGIN_THREAD_VST_GUI:
  37. return "PLUGIN_THREAD_VST_GUI";
  38. case CarlaPluginThread::PLUGIN_THREAD_BRIDGE:
  39. return "PLUGIN_THREAD_BRIDGE";
  40. }
  41. carla_stderr("CarlaPluginThread::PluginThreadMode2str(%i) - invalid mode", mode);
  42. return nullptr;
  43. }
  44. CarlaPluginThread::CarlaPluginThread(CarlaBackend::CarlaEngine* const engine, CarlaBackend::CarlaPlugin* const plugin, const Mode mode)
  45. : CarlaThread("CarlaPluginThread"),
  46. fEngine(engine),
  47. fPlugin(plugin),
  48. fMode(mode)
  49. {
  50. carla_debug("CarlaPluginThread::CarlaPluginThread(plugin:\"%s\", engine:\"%s\", %s)", plugin->getName(), engine->getName(), PluginThreadMode2str(mode));
  51. //setPriority(5);
  52. }
  53. void CarlaPluginThread::setMode(const CarlaPluginThread::Mode mode)
  54. {
  55. CARLA_ASSERT(! isRunning());
  56. carla_debug("CarlaPluginThread::setMode(%s)", PluginThreadMode2str(mode));
  57. fMode = mode;
  58. }
  59. void CarlaPluginThread::setOscData(const char* const binary, const char* const label, const char* const extra1, const char* const extra2)
  60. {
  61. CARLA_ASSERT(! isRunning());
  62. carla_debug("CarlaPluginThread::setOscData(\"%s\", \"%s\", \"%s\", \"%s\")", binary, label, extra1, extra2);
  63. fBinary = binary;
  64. fLabel = label;
  65. fExtra1 = extra1;
  66. fExtra2 = extra2;
  67. }
  68. void CarlaPluginThread::run()
  69. {
  70. carla_debug("CarlaPluginThread::run()");
  71. #ifdef USE_JUCE
  72. ChildProcess process;
  73. StringArray arguments;
  74. arguments.add((const char*)fBinary);
  75. String name(fPlugin->getName());
  76. if (name.isEmpty())
  77. name = "(none)";
  78. switch (fMode)
  79. {
  80. case PLUGIN_THREAD_NULL:
  81. break;
  82. case PLUGIN_THREAD_DSSI_GUI:
  83. /* osc-url */ arguments.add(String(fEngine->getOscServerPathUDP()) + "/" + String(fPlugin->getId()));
  84. /* filename */ arguments.add(fPlugin->getFilename());
  85. /* label */ arguments.add((const char*)fLabel);
  86. /* ui-title */ arguments.add(name + " (GUI)");
  87. break;
  88. case PLUGIN_THREAD_LV2_GUI:
  89. /* osc-url */ arguments.add(String(fEngine->getOscServerPathTCP()) + "/" + String(fPlugin->getId()));
  90. /* URI */ arguments.add((const char*)fLabel);
  91. /* ui-URI */ arguments.add((const char*)fExtra1);
  92. /* ui-title */ arguments.add(name + " (GUI)");
  93. break;
  94. case PLUGIN_THREAD_VST_GUI:
  95. /* osc-url */ arguments.add(String(fEngine->getOscServerPathTCP()) + "/" + String(fPlugin->getId()));
  96. /* filename */ arguments.add(fPlugin->getFilename());
  97. /* ui-title */ arguments.add(name + " (GUI)");
  98. break;
  99. case PLUGIN_THREAD_BRIDGE:
  100. /* osc-url */ arguments.add(String(fEngine->getOscServerPathTCP()) + "/" + String(fPlugin->getId()));
  101. /* stype */ arguments.add((const char*)fExtra1);
  102. /* filename */ arguments.add(fPlugin->getFilename());
  103. /* name */ arguments.add(name);
  104. /* label */ arguments.add((const char*)fLabel);
  105. /* SHM ids */ arguments.add((const char*)fExtra2);
  106. break;
  107. }
  108. if (! process.start(arguments))
  109. return;
  110. switch (fMode)
  111. {
  112. case PLUGIN_THREAD_NULL:
  113. break;
  114. case PLUGIN_THREAD_DSSI_GUI:
  115. case PLUGIN_THREAD_LV2_GUI:
  116. case PLUGIN_THREAD_VST_GUI:
  117. if (fPlugin->waitForOscGuiShow())
  118. {
  119. while (process.isRunning() && ! threadShouldExit())
  120. sleep(1000);
  121. // we only get here is UI was closed or thread asked to exit
  122. if (threadShouldExit())
  123. {
  124. if (process.isRunning())
  125. process.kill();
  126. }
  127. else
  128. {
  129. fEngine->callback(CarlaBackend::CALLBACK_SHOW_GUI, fPlugin->getId(), 0, 0, 0.0f, nullptr);
  130. }
  131. }
  132. else
  133. {
  134. if (process.isRunning() && ! process.waitForProcessToFinish(500))
  135. {
  136. process.kill();
  137. fEngine->callback(CarlaBackend::CALLBACK_SHOW_GUI, fPlugin->getId(), -1, 0, 0.0f, nullptr);
  138. carla_stderr("CarlaPluginThread::run() - GUI crashed while opening");
  139. }
  140. else
  141. {
  142. fEngine->callback(CarlaBackend::CALLBACK_SHOW_GUI, fPlugin->getId(), 0, 0, 0.0f, nullptr);
  143. carla_stderr("CarlaPluginThread::run() - GUI timeout");
  144. }
  145. }
  146. break;
  147. case PLUGIN_THREAD_BRIDGE:
  148. while (process.isRunning() && ! threadShouldExit())
  149. sleep(1000);
  150. if (threadShouldExit())
  151. {
  152. if (process.isRunning())
  153. process.kill();
  154. }
  155. break;
  156. }
  157. #endif
  158. }
  159. CARLA_BACKEND_END_NAMESPACE