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.

617 lines
21KB

  1. /*
  2. * Carla Standalone
  3. * Copyright (C) 2011-2017 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 "CarlaDefines.h"
  18. #ifdef HAVE_LIBLO
  19. #define NSM_API_VERSION_MAJOR 1
  20. #define NSM_API_VERSION_MINOR 2
  21. #define NSM_CLIENT_FEATURES ":switch:"
  22. //#define NSM_CLIENT_FEATURES ":switch:optional-gui:"
  23. #include "CarlaHost.h"
  24. #include "CarlaOscUtils.hpp"
  25. #include "CarlaString.hpp"
  26. #include "water/files/File.h"
  27. namespace CB = CarlaBackend;
  28. // -------------------------------------------------------------------------------------------------------------------
  29. struct CarlaBackendStandalone {
  30. CarlaEngine* engine;
  31. EngineCallbackFunc engineCallback;
  32. void* engineCallbackPtr;
  33. // ...
  34. };
  35. extern CarlaBackendStandalone gStandalone;
  36. // -------------------------------------------------------------------------------------------------------------------
  37. class CarlaNSM
  38. {
  39. public:
  40. CarlaNSM() noexcept
  41. : fReplyAddress(nullptr),
  42. fServer(nullptr),
  43. fServerThread(nullptr),
  44. fServerURL(nullptr),
  45. fClientNameId(),
  46. fProjectPath(),
  47. fHasBroadcast(false),
  48. fHasOptionalGui(false),
  49. fHasServerControl(false),
  50. fStarted(),
  51. fReadyActionOpen(true),
  52. fReadyActionSave(true) {}
  53. ~CarlaNSM()
  54. {
  55. CARLA_SAFE_ASSERT(fReadyActionOpen);
  56. CARLA_SAFE_ASSERT(fReadyActionSave);
  57. if (fServerThread != nullptr)
  58. {
  59. lo_server_thread_stop(fServerThread);
  60. lo_server_thread_free(fServerThread);
  61. fServerThread = nullptr;
  62. fServer = nullptr;
  63. }
  64. if (fServerURL != nullptr)
  65. {
  66. std::free(fServerURL);
  67. fServerURL = nullptr;
  68. }
  69. if (fReplyAddress != nullptr)
  70. {
  71. lo_address_free(fReplyAddress);
  72. fReplyAddress = nullptr;
  73. }
  74. }
  75. bool announce(const int pid, const char* const executableName)
  76. {
  77. CARLA_SAFE_ASSERT_RETURN(pid != 0, false);
  78. CARLA_SAFE_ASSERT_RETURN(executableName != nullptr && executableName[0] != '\0', false);
  79. const char* const NSM_URL(std::getenv("NSM_URL"));
  80. if (NSM_URL == nullptr)
  81. return false;
  82. const lo_address nsmAddress(lo_address_new_from_url(NSM_URL));
  83. CARLA_SAFE_ASSERT_RETURN(nsmAddress != nullptr, false);
  84. const int proto = lo_address_get_protocol(nsmAddress);
  85. if (fServerThread == nullptr)
  86. {
  87. // create new OSC server
  88. fServerThread = lo_server_thread_new_with_proto(nullptr, proto, _osc_error_handler);
  89. CARLA_SAFE_ASSERT_RETURN(fServerThread != nullptr, false);
  90. // register message handlers
  91. lo_server_thread_add_method(fServerThread, "/error", "sis", _error_handler, this);
  92. lo_server_thread_add_method(fServerThread, "/reply", "ssss", _reply_handler, this);
  93. lo_server_thread_add_method(fServerThread, "/nsm/client/open", "sss", _open_handler, this);
  94. lo_server_thread_add_method(fServerThread, "/nsm/client/save", "", _save_handler, this);
  95. lo_server_thread_add_method(fServerThread, "/nsm/client/session_is_loaded", "", _loaded_handler, this);
  96. lo_server_thread_add_method(fServerThread, "/nsm/client/show_optional_gui", "", _show_gui_handler, this);
  97. lo_server_thread_add_method(fServerThread, "/nsm/client/hide_optional_gui", "", _hide_gui_handler, this);
  98. lo_server_thread_add_method(fServerThread, nullptr, nullptr, _broadcast_handler, this);
  99. fServer = lo_server_thread_get_server(fServerThread);
  100. fServerURL = lo_server_thread_get_url(fServerThread);
  101. }
  102. const char* appName = std::getenv("CARLA_NSM_NAME");
  103. if (appName == nullptr)
  104. appName = "Carla";
  105. lo_send_from(nsmAddress, fServer, LO_TT_IMMEDIATE, "/nsm/server/announce", "sssiii",
  106. appName, NSM_CLIENT_FEATURES, executableName, NSM_API_VERSION_MAJOR, NSM_API_VERSION_MINOR, pid);
  107. lo_address_free(nsmAddress);
  108. return true;
  109. }
  110. void ready(const int action)
  111. {
  112. CARLA_SAFE_ASSERT_RETURN(fServerThread != nullptr,);
  113. switch (action)
  114. {
  115. case -1: // init
  116. CARLA_SAFE_ASSERT_BREAK(! fStarted);
  117. fStarted = true;
  118. lo_server_thread_start(fServerThread);
  119. break;
  120. case 0: // error
  121. break;
  122. case 1: // reply
  123. break;
  124. case 2: // open
  125. fReadyActionOpen = true;
  126. break;
  127. case 3: // save
  128. fReadyActionSave = true;
  129. break;
  130. case 4: // session loaded
  131. break;
  132. case 5: // show gui
  133. CARLA_SAFE_ASSERT_BREAK(fReplyAddress != nullptr);
  134. CARLA_SAFE_ASSERT_BREAK(fServer != nullptr);
  135. lo_send_from(fReplyAddress, fServer, LO_TT_IMMEDIATE, "/nsm/client/gui_is_shown", "");
  136. break;
  137. case 6: // hide gui
  138. CARLA_SAFE_ASSERT_BREAK(fReplyAddress != nullptr);
  139. CARLA_SAFE_ASSERT_BREAK(fServer != nullptr);
  140. lo_send_from(fReplyAddress, fServer, LO_TT_IMMEDIATE, "/nsm/client/gui_is_hidden", "");
  141. break;
  142. }
  143. }
  144. static CarlaNSM& getInstance()
  145. {
  146. static CarlaNSM sInstance;
  147. return sInstance;
  148. }
  149. protected:
  150. int handleError(const char* const method, const int code, const char* const message)
  151. {
  152. carla_stdout("CarlaNSM::handleError(\"%s\", %i, \"%s\")", method, code, message);
  153. if (gStandalone.engineCallback != nullptr)
  154. gStandalone.engineCallback(gStandalone.engineCallbackPtr, CB::ENGINE_CALLBACK_NSM, 0, 0, code, 0.0f, message);
  155. return 1;
  156. // may be unused
  157. (void)method;
  158. }
  159. int handleReply(const char* const method, const char* const message, const char* const smName, const char* const features,
  160. const lo_message msg)
  161. {
  162. CARLA_SAFE_ASSERT_RETURN(fServerThread != nullptr, 1);
  163. carla_stdout("CarlaNSM::handleReply(\"%s\", \"%s\", \"%s\", \"%s\")", method, message, smName, features);
  164. if (std::strcmp(method, "/nsm/server/announce") == 0)
  165. {
  166. const lo_address msgAddress(lo_message_get_source(msg));
  167. CARLA_SAFE_ASSERT_RETURN(msgAddress != nullptr, 0);
  168. char* const msgURL(lo_address_get_url(msgAddress));
  169. CARLA_SAFE_ASSERT_RETURN(msgURL != nullptr, 0);
  170. if (fReplyAddress != nullptr)
  171. lo_address_free(fReplyAddress);
  172. fReplyAddress = lo_address_new_from_url(msgURL);
  173. CARLA_SAFE_ASSERT_RETURN(fReplyAddress != nullptr, 0);
  174. fHasBroadcast = std::strstr(features, ":broadcast:") != nullptr;
  175. fHasOptionalGui = std::strstr(features, ":optional-gui:") != nullptr;
  176. fHasServerControl = std::strstr(features, ":server_control:") != nullptr;
  177. #if 0
  178. // UI starts visible
  179. if (fHasOptionalGui)
  180. lo_send_from(fReplyAddress, fServer, LO_TT_IMMEDIATE, "/nsm/client/gui_is_shown", "");
  181. #endif
  182. carla_stdout("Carla started via '%s', message: %s", smName, message);
  183. if (gStandalone.engineCallback != nullptr)
  184. {
  185. int flags = 0;
  186. if (fHasBroadcast)
  187. flags |= 1 << 0;
  188. if (fHasOptionalGui)
  189. flags |= 1 << 1;
  190. if (fHasServerControl)
  191. flags |= 1 << 2;
  192. gStandalone.engineCallback(gStandalone.engineCallbackPtr, CB::ENGINE_CALLBACK_NSM, 0, 1, flags, 0.0f, smName);
  193. }
  194. std::free(msgURL);
  195. }
  196. else
  197. {
  198. carla_stdout("Got unknown NSM reply method '%s'", method);
  199. }
  200. return 0;
  201. }
  202. int handleOpen(const char* const projectPath, const char* const displayName, const char* const clientNameId)
  203. {
  204. CARLA_SAFE_ASSERT_RETURN(fReplyAddress != nullptr, 1);
  205. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr, 1);
  206. carla_stdout("CarlaNSM::handleOpen(\"%s\", \"%s\", \"%s\")", projectPath, displayName, clientNameId);
  207. if (gStandalone.engineCallback != nullptr)
  208. {
  209. fReadyActionOpen = false;
  210. gStandalone.engineCallback(gStandalone.engineCallbackPtr, CB::ENGINE_CALLBACK_NSM, 0, 2, 0, 0.0f, projectPath);
  211. for (; ! fReadyActionOpen;)
  212. carla_msleep(10);
  213. }
  214. else
  215. {
  216. using namespace water;
  217. if (carla_is_engine_running())
  218. carla_engine_close();
  219. carla_engine_init("JACK", clientNameId);
  220. fProjectPath = projectPath;
  221. fProjectPath += ".carxp";
  222. const String jfilename = String(CharPointer_UTF8(fProjectPath));
  223. if (File(jfilename).existsAsFile())
  224. carla_load_project(fProjectPath);
  225. }
  226. fClientNameId = clientNameId;
  227. lo_send_from(fReplyAddress, fServer, LO_TT_IMMEDIATE, "/reply", "ss", "/nsm/client/open", "OK");
  228. // Broadcast ourselves
  229. if (fHasBroadcast)
  230. {
  231. const char* appName = std::getenv("CARLA_NSM_NAME");
  232. if (appName == nullptr)
  233. appName = "Carla";
  234. lo_send_from(fReplyAddress, fServer, LO_TT_IMMEDIATE, "/nsm/server/broadcast", "sssss",
  235. "/non/hello", fServerURL, appName, CARLA_VERSION_STRING, fClientNameId.buffer());
  236. }
  237. return 0;
  238. }
  239. int handleSave()
  240. {
  241. CARLA_SAFE_ASSERT_RETURN(fReplyAddress != nullptr, 1);
  242. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr, 1);
  243. carla_stdout("CarlaNSM::handleSave()");
  244. if (gStandalone.engineCallback != nullptr)
  245. {
  246. fReadyActionSave = false;
  247. gStandalone.engineCallback(gStandalone.engineCallbackPtr, CB::ENGINE_CALLBACK_NSM, 0, 3, 0, 0.0f, nullptr);
  248. for (; ! fReadyActionSave;)
  249. carla_msleep(10);
  250. }
  251. else
  252. {
  253. CARLA_SAFE_ASSERT_RETURN(fProjectPath.isNotEmpty(), 0);
  254. carla_save_project(fProjectPath);
  255. }
  256. lo_send_from(fReplyAddress, fServer, LO_TT_IMMEDIATE, "/reply", "ss", "/nsm/client/save", "OK");
  257. return 0;
  258. }
  259. int handleSessionIsLoaded()
  260. {
  261. CARLA_SAFE_ASSERT_RETURN(fReplyAddress != nullptr, 1);
  262. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr, 1);
  263. carla_stdout("CarlaNSM::handleSessionIsLoaded()");
  264. if (gStandalone.engineCallback != nullptr)
  265. gStandalone.engineCallback(gStandalone.engineCallbackPtr, CB::ENGINE_CALLBACK_NSM, 0, 4, 0, 0.0f, nullptr);
  266. return 0;
  267. }
  268. int handleShowOptionalGui()
  269. {
  270. CARLA_SAFE_ASSERT_RETURN(fReplyAddress != nullptr, 1);
  271. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr, 1);
  272. carla_stdout("CarlaNSM::handleShowOptionalGui()");
  273. if (gStandalone.engineCallback != nullptr)
  274. gStandalone.engineCallback(gStandalone.engineCallbackPtr, CB::ENGINE_CALLBACK_NSM, 0, 5, 0, 0.0f, nullptr);
  275. return 0;
  276. }
  277. int handleHideOptionalGui()
  278. {
  279. CARLA_SAFE_ASSERT_RETURN(fReplyAddress != nullptr, 1);
  280. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr, 1);
  281. carla_stdout("CarlaNSM::handleHideOptionalGui()");
  282. if (gStandalone.engineCallback != nullptr)
  283. gStandalone.engineCallback(gStandalone.engineCallbackPtr, CB::ENGINE_CALLBACK_NSM, 0, 6, 0, 0.0f, nullptr);
  284. return 0;
  285. }
  286. int handleBroadcast(const char* const path, const char* const types, lo_arg** const argv, const int argc,
  287. const lo_message msg)
  288. {
  289. CARLA_SAFE_ASSERT_RETURN(fReplyAddress != nullptr, 1);
  290. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr, 1);
  291. CARLA_SAFE_ASSERT_RETURN(argc >= 0, 0);
  292. carla_stdout("CarlaNSM::handleBroadcast(%s, %s, %p, %i)", path, types, argv, argc);
  293. #if 0
  294. if (std::strcmp(path, "/non/hello") == 0)
  295. {
  296. CARLA_SAFE_ASSERT_RETURN(argc == 4, 0);
  297. CARLA_SAFE_ASSERT_RETURN(std::strcmp(types, "ssss") == 0, 0);
  298. const char* const url = &argv[0]->s;
  299. //const char* const name = &argv[1]->s;
  300. //const char* const version = &argv[2]->s;
  301. //const char* const clientId = &argv[3]->s;
  302. const lo_address targetAddress(lo_address_new_from_url(url));
  303. CARLA_SAFE_ASSERT_RETURN(targetAddress != nullptr, 0);
  304. lo_send_from(targetAddress, fServer, LO_TT_IMMEDIATE, "/signal/hello", "ss",
  305. fClientNameId.buffer(), fServerURL);
  306. lo_address_free(targetAddress);
  307. return 0;
  308. }
  309. if (std::strcmp(path, "/signal/hello") == 0)
  310. {
  311. //const char* const name = &argv[0]->s;
  312. const char* const url = &argv[1]->s;
  313. const lo_address targetAddress(lo_address_new_from_url(url));
  314. CARLA_SAFE_ASSERT_RETURN(targetAddress != nullptr, 0);
  315. lo_send_from(targetAddress, fServer, LO_TT_IMMEDIATE, "/signal/hello", "ss",
  316. fClientNameId.buffer(), fServerURL);
  317. lo_address_free(targetAddress);
  318. return 0;
  319. }
  320. if (std::strcmp(path, "/signal/list") == 0)
  321. {
  322. carla_stdout("CarlaNSM::handleBroadcast - got list");
  323. CARLA_SAFE_ASSERT_RETURN(carla_is_engine_running(), 0);
  324. const char* prefix = nullptr;
  325. if (argc > 0)
  326. prefix = &argv[0]->s;
  327. const lo_address msgAddress(lo_message_get_source(msg));
  328. CARLA_SAFE_ASSERT_RETURN(msgAddress != nullptr, 0);
  329. for (uint32_t i = 0, pluginCount = carla_get_current_plugin_count(); i < pluginCount; ++i)
  330. {
  331. const CarlaPluginInfo* const pluginInfo(carla_get_plugin_info(i));
  332. CARLA_SAFE_ASSERT_CONTINUE(pluginInfo != nullptr);
  333. /*const*/ CarlaString pluginNameId(fClientNameId + "/" + CarlaString(pluginInfo->name).replace('/','_') + "/");
  334. for (uint32_t j=0, paramCount = carla_get_parameter_count(i); j < paramCount; ++j)
  335. {
  336. const CarlaParameterInfo* const paramInfo(carla_get_parameter_info(i, j));
  337. CARLA_SAFE_ASSERT_CONTINUE(paramInfo != nullptr);
  338. const ParameterData* const paramData(carla_get_parameter_data(i, j));
  339. CARLA_SAFE_ASSERT_CONTINUE(paramData != nullptr);
  340. const ParameterRanges* const paramRanges(carla_get_parameter_ranges(i, j));
  341. CARLA_SAFE_ASSERT_CONTINUE(paramRanges != nullptr);
  342. if (paramData->type != CB::PARAMETER_INPUT /*&& paramData->type != CB::PARAMETER_OUTPUT*/)
  343. continue;
  344. if ((paramData->hints & CB::PARAMETER_IS_ENABLED) == 0)
  345. continue;
  346. if ((paramData->hints & CB::PARAMETER_IS_AUTOMABLE) == 0)
  347. continue;
  348. if (paramData->hints & CB::PARAMETER_IS_READ_ONLY)
  349. continue;
  350. const char* const dir = paramData->type == CB::PARAMETER_INPUT ? "in" : "out";
  351. const CarlaString paramNameId = pluginNameId + CarlaString(paramInfo->name).replace('/','_');
  352. const float defNorm = paramRanges->getNormalizedValue(paramRanges->def);
  353. if (prefix == nullptr || std::strncmp(paramNameId, prefix, std::strlen(prefix)) == 0)
  354. {
  355. lo_send_from(msgAddress, fServer, LO_TT_IMMEDIATE, "/reply", "sssfff",
  356. path, paramNameId.buffer(), dir, 0.0f, 1.0f, defNorm);
  357. }
  358. }
  359. }
  360. lo_send_from(msgAddress, fServer, LO_TT_IMMEDIATE, "/reply", "s", path);
  361. //return 0;
  362. }
  363. for (int i=0; i<argc; ++i)
  364. if (types[i] == 's')
  365. carla_stdout("%i: %s", i+1, &argv[i]->s);
  366. #endif
  367. return 0;
  368. // unused
  369. (void)msg;
  370. }
  371. private:
  372. lo_address fReplyAddress;
  373. lo_server fServer;
  374. lo_server_thread fServerThread;
  375. char* fServerURL;
  376. CarlaString fClientNameId;
  377. CarlaString fProjectPath;
  378. bool fHasBroadcast;
  379. bool fHasOptionalGui;
  380. bool fHasServerControl;
  381. bool fStarted;
  382. volatile bool fReadyActionOpen;
  383. volatile bool fReadyActionSave;
  384. #define handlePtr ((CarlaNSM*)data)
  385. static void _osc_error_handler(int num, const char* msg, const char* path)
  386. {
  387. carla_stderr2("CarlaNSM::_osc_error_handler(%i, \"%s\", \"%s\")", num, msg, path);
  388. }
  389. static int _error_handler(const char*, const char* types, lo_arg** argv, int argc, lo_message, void* data)
  390. {
  391. CARLA_SAFE_ASSERT_RETURN(argc == 3, 1);
  392. CARLA_SAFE_ASSERT_RETURN(std::strcmp(types, "sis") == 0, 1);
  393. const char* const method = &argv[0]->s;
  394. const int code = argv[1]->i;
  395. const char* const message = &argv[2]->s;
  396. return handlePtr->handleError(method, code, message);
  397. }
  398. static int _reply_handler(const char*, const char* types, lo_arg** argv, int argc, lo_message msg, void* data)
  399. {
  400. CARLA_SAFE_ASSERT_RETURN(argc == 4, 1);
  401. CARLA_SAFE_ASSERT_RETURN(std::strcmp(types, "ssss") == 0, 1);
  402. const char* const method = &argv[0]->s;
  403. const char* const message = &argv[1]->s;
  404. const char* const smName = &argv[2]->s;
  405. const char* const features = &argv[3]->s;
  406. return handlePtr->handleReply(method, message, smName, features, msg);
  407. }
  408. static int _open_handler(const char*, const char* types, lo_arg** argv, int argc, lo_message, void* data)
  409. {
  410. CARLA_SAFE_ASSERT_RETURN(argc == 3, 1);
  411. CARLA_SAFE_ASSERT_RETURN(std::strcmp(types, "sss") == 0, 1);
  412. const char* const projectPath = &argv[0]->s;
  413. const char* const displayName = &argv[1]->s;
  414. const char* const clientNameId = &argv[2]->s;
  415. return handlePtr->handleOpen(projectPath, displayName, clientNameId);
  416. }
  417. static int _save_handler(const char*, const char*, lo_arg**, int argc, lo_message, void* data)
  418. {
  419. CARLA_SAFE_ASSERT_RETURN(argc == 0, 1);
  420. return handlePtr->handleSave();
  421. }
  422. static int _loaded_handler(const char*, const char*, lo_arg**, int argc, lo_message, void* data)
  423. {
  424. CARLA_SAFE_ASSERT_RETURN(argc == 0, 1);
  425. return handlePtr->handleSessionIsLoaded();
  426. }
  427. static int _show_gui_handler(const char*, const char*, lo_arg**, int argc, lo_message, void* data)
  428. {
  429. CARLA_SAFE_ASSERT_RETURN(argc == 0, 1);
  430. return handlePtr->handleShowOptionalGui();
  431. }
  432. static int _hide_gui_handler(const char*, const char*, lo_arg**, int argc, lo_message, void* data)
  433. {
  434. CARLA_SAFE_ASSERT_RETURN(argc == 0, 1);
  435. return handlePtr->handleHideOptionalGui();
  436. }
  437. static int _broadcast_handler(const char* path, const char* types, lo_arg** argv, int argc, lo_message msg, void* data)
  438. {
  439. return handlePtr->handleBroadcast(path, types, argv, argc, msg);
  440. }
  441. #undef handlePtr
  442. CARLA_PREVENT_HEAP_ALLOCATION
  443. CARLA_DECLARE_NON_COPY_CLASS(CarlaNSM)
  444. };
  445. #endif // HAVE_LIBLO
  446. // -------------------------------------------------------------------------------------------------------------------
  447. CARLA_EXPORT
  448. bool carla_nsm_init(int pid, const char* executableName);
  449. bool carla_nsm_init(int pid, const char* executableName)
  450. {
  451. #ifdef HAVE_LIBLO
  452. return CarlaNSM::getInstance().announce(pid, executableName);
  453. #else
  454. return false;
  455. // unused
  456. (void)pid; (void)executableName;
  457. #endif
  458. }
  459. CARLA_EXPORT
  460. void carla_nsm_ready(int action);
  461. void carla_nsm_ready(int action)
  462. {
  463. #ifdef HAVE_LIBLO
  464. CarlaNSM::getInstance().ready(action);
  465. #endif
  466. }
  467. // -------------------------------------------------------------------------------------------------------------------