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.

626 lines
21KB

  1. /*
  2. * Carla Standalone
  3. * Copyright (C) 2011-2018 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. {
  136. // NOTE: lo_send_from is a macro that creates local variables
  137. lo_send_from(fReplyAddress, fServer, LO_TT_IMMEDIATE, "/nsm/client/gui_is_shown", "");
  138. }
  139. break;
  140. case 6: // hide gui
  141. CARLA_SAFE_ASSERT_BREAK(fReplyAddress != nullptr);
  142. CARLA_SAFE_ASSERT_BREAK(fServer != nullptr);
  143. {
  144. // NOTE: lo_send_from is a macro that creates local variables
  145. lo_send_from(fReplyAddress, fServer, LO_TT_IMMEDIATE, "/nsm/client/gui_is_hidden", "");
  146. }
  147. break;
  148. }
  149. }
  150. static CarlaNSM& getInstance()
  151. {
  152. static CarlaNSM sInstance;
  153. return sInstance;
  154. }
  155. protected:
  156. int handleError(const char* const method, const int code, const char* const message)
  157. {
  158. carla_stdout("CarlaNSM::handleError(\"%s\", %i, \"%s\")", method, code, message);
  159. if (gStandalone.engineCallback != nullptr)
  160. gStandalone.engineCallback(gStandalone.engineCallbackPtr, CB::ENGINE_CALLBACK_NSM, 0, 0, code, 0.0f, message);
  161. return 1;
  162. // may be unused
  163. (void)method;
  164. }
  165. int handleReply(const char* const method, const char* const message, const char* const smName, const char* const features,
  166. const lo_message msg)
  167. {
  168. CARLA_SAFE_ASSERT_RETURN(fServerThread != nullptr, 1);
  169. carla_stdout("CarlaNSM::handleReply(\"%s\", \"%s\", \"%s\", \"%s\")", method, message, smName, features);
  170. if (std::strcmp(method, "/nsm/server/announce") == 0)
  171. {
  172. const lo_address msgAddress(lo_message_get_source(msg));
  173. CARLA_SAFE_ASSERT_RETURN(msgAddress != nullptr, 0);
  174. char* const msgURL(lo_address_get_url(msgAddress));
  175. CARLA_SAFE_ASSERT_RETURN(msgURL != nullptr, 0);
  176. if (fReplyAddress != nullptr)
  177. lo_address_free(fReplyAddress);
  178. fReplyAddress = lo_address_new_from_url(msgURL);
  179. CARLA_SAFE_ASSERT_RETURN(fReplyAddress != nullptr, 0);
  180. fHasBroadcast = std::strstr(features, ":broadcast:") != nullptr;
  181. fHasOptionalGui = std::strstr(features, ":optional-gui:") != nullptr;
  182. fHasServerControl = std::strstr(features, ":server_control:") != nullptr;
  183. #if 0
  184. // UI starts visible
  185. if (fHasOptionalGui)
  186. lo_send_from(fReplyAddress, fServer, LO_TT_IMMEDIATE, "/nsm/client/gui_is_shown", "");
  187. #endif
  188. carla_stdout("Carla started via '%s', message: %s", smName, message);
  189. if (gStandalone.engineCallback != nullptr)
  190. {
  191. int flags = 0;
  192. if (fHasBroadcast)
  193. flags |= 1 << 0;
  194. if (fHasOptionalGui)
  195. flags |= 1 << 1;
  196. if (fHasServerControl)
  197. flags |= 1 << 2;
  198. gStandalone.engineCallback(gStandalone.engineCallbackPtr, CB::ENGINE_CALLBACK_NSM, 0, 1, flags, 0.0f, smName);
  199. }
  200. std::free(msgURL);
  201. }
  202. else
  203. {
  204. carla_stdout("Got unknown NSM reply method '%s'", method);
  205. }
  206. return 0;
  207. }
  208. int handleOpen(const char* const projectPath, const char* const displayName, const char* const clientNameId)
  209. {
  210. CARLA_SAFE_ASSERT_RETURN(fReplyAddress != nullptr, 1);
  211. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr, 1);
  212. carla_stdout("CarlaNSM::handleOpen(\"%s\", \"%s\", \"%s\")", projectPath, displayName, clientNameId);
  213. if (gStandalone.engineCallback != nullptr)
  214. {
  215. fReadyActionOpen = false;
  216. gStandalone.engineCallback(gStandalone.engineCallbackPtr, CB::ENGINE_CALLBACK_NSM, 0, 2, 0, 0.0f, projectPath);
  217. for (; ! fReadyActionOpen;)
  218. carla_msleep(10);
  219. }
  220. else
  221. {
  222. using namespace water;
  223. if (carla_is_engine_running())
  224. carla_engine_close();
  225. carla_engine_init("JACK", clientNameId);
  226. fProjectPath = projectPath;
  227. fProjectPath += ".carxp";
  228. const String jfilename = String(CharPointer_UTF8(fProjectPath));
  229. if (File(jfilename).existsAsFile())
  230. carla_load_project(fProjectPath);
  231. }
  232. fClientNameId = clientNameId;
  233. lo_send_from(fReplyAddress, fServer, LO_TT_IMMEDIATE, "/reply", "ss", "/nsm/client/open", "OK");
  234. // Broadcast ourselves
  235. if (fHasBroadcast)
  236. {
  237. const char* appName = std::getenv("CARLA_NSM_NAME");
  238. if (appName == nullptr)
  239. appName = "Carla";
  240. lo_send_from(fReplyAddress, fServer, LO_TT_IMMEDIATE, "/nsm/server/broadcast", "sssss",
  241. "/non/hello", fServerURL, appName, CARLA_VERSION_STRING, fClientNameId.buffer());
  242. }
  243. return 0;
  244. }
  245. int handleSave()
  246. {
  247. CARLA_SAFE_ASSERT_RETURN(fReplyAddress != nullptr, 1);
  248. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr, 1);
  249. carla_stdout("CarlaNSM::handleSave()");
  250. if (gStandalone.engineCallback != nullptr)
  251. {
  252. fReadyActionSave = false;
  253. gStandalone.engineCallback(gStandalone.engineCallbackPtr, CB::ENGINE_CALLBACK_NSM, 0, 3, 0, 0.0f, nullptr);
  254. for (; ! fReadyActionSave;)
  255. carla_msleep(10);
  256. }
  257. else
  258. {
  259. CARLA_SAFE_ASSERT_RETURN(fProjectPath.isNotEmpty(), 0);
  260. carla_save_project(fProjectPath);
  261. }
  262. lo_send_from(fReplyAddress, fServer, LO_TT_IMMEDIATE, "/reply", "ss", "/nsm/client/save", "OK");
  263. return 0;
  264. }
  265. int handleSessionIsLoaded()
  266. {
  267. CARLA_SAFE_ASSERT_RETURN(fReplyAddress != nullptr, 1);
  268. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr, 1);
  269. carla_stdout("CarlaNSM::handleSessionIsLoaded()");
  270. if (gStandalone.engineCallback != nullptr)
  271. gStandalone.engineCallback(gStandalone.engineCallbackPtr, CB::ENGINE_CALLBACK_NSM, 0, 4, 0, 0.0f, nullptr);
  272. return 0;
  273. }
  274. int handleShowOptionalGui()
  275. {
  276. CARLA_SAFE_ASSERT_RETURN(fReplyAddress != nullptr, 1);
  277. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr, 1);
  278. carla_stdout("CarlaNSM::handleShowOptionalGui()");
  279. if (gStandalone.engineCallback != nullptr)
  280. gStandalone.engineCallback(gStandalone.engineCallbackPtr, CB::ENGINE_CALLBACK_NSM, 0, 5, 0, 0.0f, nullptr);
  281. return 0;
  282. }
  283. int handleHideOptionalGui()
  284. {
  285. CARLA_SAFE_ASSERT_RETURN(fReplyAddress != nullptr, 1);
  286. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr, 1);
  287. carla_stdout("CarlaNSM::handleHideOptionalGui()");
  288. if (gStandalone.engineCallback != nullptr)
  289. gStandalone.engineCallback(gStandalone.engineCallbackPtr, CB::ENGINE_CALLBACK_NSM, 0, 6, 0, 0.0f, nullptr);
  290. return 0;
  291. }
  292. int handleBroadcast(const char* const path, const char* const types, lo_arg** const argv, const int argc,
  293. const lo_message msg)
  294. {
  295. CARLA_SAFE_ASSERT_RETURN(fReplyAddress != nullptr, 1);
  296. CARLA_SAFE_ASSERT_RETURN(fServer != nullptr, 1);
  297. CARLA_SAFE_ASSERT_RETURN(argc >= 0, 0);
  298. carla_stdout("CarlaNSM::handleBroadcast(%s, %s, %p, %i)", path, types, argv, argc);
  299. #if 0
  300. if (std::strcmp(path, "/non/hello") == 0)
  301. {
  302. CARLA_SAFE_ASSERT_RETURN(argc == 4, 0);
  303. CARLA_SAFE_ASSERT_RETURN(std::strcmp(types, "ssss") == 0, 0);
  304. const char* const url = &argv[0]->s;
  305. //const char* const name = &argv[1]->s;
  306. //const char* const version = &argv[2]->s;
  307. //const char* const clientId = &argv[3]->s;
  308. const lo_address targetAddress(lo_address_new_from_url(url));
  309. CARLA_SAFE_ASSERT_RETURN(targetAddress != nullptr, 0);
  310. lo_send_from(targetAddress, fServer, LO_TT_IMMEDIATE, "/signal/hello", "ss",
  311. fClientNameId.buffer(), fServerURL);
  312. lo_address_free(targetAddress);
  313. return 0;
  314. }
  315. if (std::strcmp(path, "/signal/hello") == 0)
  316. {
  317. //const char* const name = &argv[0]->s;
  318. const char* const url = &argv[1]->s;
  319. const lo_address targetAddress(lo_address_new_from_url(url));
  320. CARLA_SAFE_ASSERT_RETURN(targetAddress != nullptr, 0);
  321. lo_send_from(targetAddress, fServer, LO_TT_IMMEDIATE, "/signal/hello", "ss",
  322. fClientNameId.buffer(), fServerURL);
  323. lo_address_free(targetAddress);
  324. return 0;
  325. }
  326. if (std::strcmp(path, "/signal/list") == 0)
  327. {
  328. carla_stdout("CarlaNSM::handleBroadcast - got list");
  329. CARLA_SAFE_ASSERT_RETURN(carla_is_engine_running(), 0);
  330. const char* prefix = nullptr;
  331. if (argc > 0)
  332. prefix = &argv[0]->s;
  333. const lo_address msgAddress(lo_message_get_source(msg));
  334. CARLA_SAFE_ASSERT_RETURN(msgAddress != nullptr, 0);
  335. for (uint32_t i = 0, pluginCount = carla_get_current_plugin_count(); i < pluginCount; ++i)
  336. {
  337. const CarlaPluginInfo* const pluginInfo(carla_get_plugin_info(i));
  338. CARLA_SAFE_ASSERT_CONTINUE(pluginInfo != nullptr);
  339. /*const*/ CarlaString pluginNameId(fClientNameId + "/" + CarlaString(pluginInfo->name).replace('/','_') + "/");
  340. for (uint32_t j=0, paramCount = carla_get_parameter_count(i); j < paramCount; ++j)
  341. {
  342. const CarlaParameterInfo* const paramInfo(carla_get_parameter_info(i, j));
  343. CARLA_SAFE_ASSERT_CONTINUE(paramInfo != nullptr);
  344. const ParameterData* const paramData(carla_get_parameter_data(i, j));
  345. CARLA_SAFE_ASSERT_CONTINUE(paramData != nullptr);
  346. const ParameterRanges* const paramRanges(carla_get_parameter_ranges(i, j));
  347. CARLA_SAFE_ASSERT_CONTINUE(paramRanges != nullptr);
  348. if (paramData->type != CB::PARAMETER_INPUT /*&& paramData->type != CB::PARAMETER_OUTPUT*/)
  349. continue;
  350. if ((paramData->hints & CB::PARAMETER_IS_ENABLED) == 0)
  351. continue;
  352. if ((paramData->hints & CB::PARAMETER_IS_AUTOMABLE) == 0)
  353. continue;
  354. if (paramData->hints & CB::PARAMETER_IS_READ_ONLY)
  355. continue;
  356. const char* const dir = paramData->type == CB::PARAMETER_INPUT ? "in" : "out";
  357. const CarlaString paramNameId = pluginNameId + CarlaString(paramInfo->name).replace('/','_');
  358. const float defNorm = paramRanges->getNormalizedValue(paramRanges->def);
  359. if (prefix == nullptr || std::strncmp(paramNameId, prefix, std::strlen(prefix)) == 0)
  360. {
  361. lo_send_from(msgAddress, fServer, LO_TT_IMMEDIATE, "/reply", "sssfff",
  362. path, paramNameId.buffer(), dir, 0.0f, 1.0f, defNorm);
  363. }
  364. }
  365. }
  366. lo_send_from(msgAddress, fServer, LO_TT_IMMEDIATE, "/reply", "s", path);
  367. //return 0;
  368. }
  369. for (int i=0; i<argc; ++i)
  370. if (types[i] == 's')
  371. carla_stdout("%i: %s", i+1, &argv[i]->s);
  372. #endif
  373. return 0;
  374. // unused
  375. (void)msg;
  376. }
  377. private:
  378. lo_address fReplyAddress;
  379. lo_server fServer;
  380. lo_server_thread fServerThread;
  381. char* fServerURL;
  382. CarlaString fClientNameId;
  383. CarlaString fProjectPath;
  384. bool fHasBroadcast;
  385. bool fHasOptionalGui;
  386. bool fHasServerControl;
  387. bool fStarted;
  388. volatile bool fReadyActionOpen;
  389. volatile bool fReadyActionSave;
  390. #define handlePtr ((CarlaNSM*)data)
  391. static void _osc_error_handler(int num, const char* msg, const char* path)
  392. {
  393. carla_stderr2("CarlaNSM::_osc_error_handler(%i, \"%s\", \"%s\")", num, msg, path);
  394. }
  395. static int _error_handler(const char*, const char* types, lo_arg** argv, int argc, lo_message, void* data)
  396. {
  397. CARLA_SAFE_ASSERT_RETURN(argc == 3, 1);
  398. CARLA_SAFE_ASSERT_RETURN(std::strcmp(types, "sis") == 0, 1);
  399. const char* const method = &argv[0]->s;
  400. const int code = argv[1]->i;
  401. const char* const message = &argv[2]->s;
  402. return handlePtr->handleError(method, code, message);
  403. }
  404. static int _reply_handler(const char*, const char* types, lo_arg** argv, int argc, lo_message msg, void* data)
  405. {
  406. CARLA_SAFE_ASSERT_RETURN(argc == 4, 1);
  407. CARLA_SAFE_ASSERT_RETURN(std::strcmp(types, "ssss") == 0, 1);
  408. const char* const method = &argv[0]->s;
  409. const char* const message = &argv[1]->s;
  410. const char* const smName = &argv[2]->s;
  411. const char* const features = &argv[3]->s;
  412. return handlePtr->handleReply(method, message, smName, features, msg);
  413. }
  414. static int _open_handler(const char*, const char* types, lo_arg** argv, int argc, lo_message, void* data)
  415. {
  416. CARLA_SAFE_ASSERT_RETURN(argc == 3, 1);
  417. CARLA_SAFE_ASSERT_RETURN(std::strcmp(types, "sss") == 0, 1);
  418. const char* const projectPath = &argv[0]->s;
  419. const char* const displayName = &argv[1]->s;
  420. const char* const clientNameId = &argv[2]->s;
  421. return handlePtr->handleOpen(projectPath, displayName, clientNameId);
  422. }
  423. static int _save_handler(const char*, const char*, lo_arg**, int argc, lo_message, void* data)
  424. {
  425. CARLA_SAFE_ASSERT_RETURN(argc == 0, 1);
  426. return handlePtr->handleSave();
  427. }
  428. static int _loaded_handler(const char*, const char*, lo_arg**, int argc, lo_message, void* data)
  429. {
  430. CARLA_SAFE_ASSERT_RETURN(argc == 0, 1);
  431. return handlePtr->handleSessionIsLoaded();
  432. }
  433. static int _show_gui_handler(const char*, const char*, lo_arg**, int argc, lo_message, void* data)
  434. {
  435. CARLA_SAFE_ASSERT_RETURN(argc == 0, 1);
  436. return handlePtr->handleShowOptionalGui();
  437. }
  438. static int _hide_gui_handler(const char*, const char*, lo_arg**, int argc, lo_message, void* data)
  439. {
  440. CARLA_SAFE_ASSERT_RETURN(argc == 0, 1);
  441. return handlePtr->handleHideOptionalGui();
  442. }
  443. static int _broadcast_handler(const char* path, const char* types, lo_arg** argv, int argc, lo_message msg, void* data)
  444. {
  445. return handlePtr->handleBroadcast(path, types, argv, argc, msg);
  446. }
  447. #undef handlePtr
  448. CARLA_PREVENT_HEAP_ALLOCATION
  449. CARLA_DECLARE_NON_COPY_CLASS(CarlaNSM)
  450. };
  451. #endif // HAVE_LIBLO
  452. // -------------------------------------------------------------------------------------------------------------------
  453. CARLA_EXPORT
  454. bool carla_nsm_init(int pid, const char* executableName);
  455. bool carla_nsm_init(int pid, const char* executableName)
  456. {
  457. #ifdef HAVE_LIBLO
  458. return CarlaNSM::getInstance().announce(pid, executableName);
  459. #else
  460. return false;
  461. // unused
  462. (void)pid; (void)executableName;
  463. #endif
  464. }
  465. CARLA_EXPORT
  466. void carla_nsm_ready(int action);
  467. void carla_nsm_ready(int action)
  468. {
  469. #ifdef HAVE_LIBLO
  470. CarlaNSM::getInstance().ready(action);
  471. #else
  472. // unused
  473. return; (void)action;
  474. #endif
  475. }
  476. // -------------------------------------------------------------------------------------------------------------------