jack2 codebase
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.

1447 lines
41KB

  1. // u/* -*- Mode: C++ ; c-basic-offset: 4 -*- */
  2. /*
  3. JACK control API implementation
  4. Copyright (C) 2008 Nedko Arnaudov
  5. Copyright (C) 2008 Grame
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; version 2 of the License.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #ifndef WIN32
  18. #include <stdint.h>
  19. #include <dirent.h>
  20. #include <pthread.h>
  21. #endif
  22. #include "types.h"
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <stdio.h>
  26. #include <assert.h>
  27. #include <signal.h>
  28. #include "jslist.h"
  29. #include "driver_interface.h"
  30. #include "JackError.h"
  31. #include "JackServer.h"
  32. #include "shm.h"
  33. #include "JackTools.h"
  34. #include "JackControlAPI.h"
  35. #include "JackLockedEngine.h"
  36. #include "JackConstants.h"
  37. #include "JackDriverLoader.h"
  38. #include "JackServerGlobals.h"
  39. using namespace Jack;
  40. /* JackEngine::CheckPortsConnect() has some assumptions about char values */
  41. static struct jack_constraint_enum_char_descriptor self_connect_mode_constraint_descr_array[] =
  42. {
  43. { ' ', "Don't restrict self connect requests" },
  44. { 'E', "Fail self connect requests to external ports only" },
  45. { 'e', "Ignore self connect requests to external ports only" },
  46. { 'A', "Fail all self connect requests" },
  47. { 'a', "Ignore all self connect requests" },
  48. { 0 }
  49. };
  50. struct jackctl_server
  51. {
  52. JSList * drivers;
  53. JSList * internals;
  54. JSList * parameters;
  55. class JackServer * engine;
  56. /* string, server name */
  57. union jackctl_parameter_value name;
  58. union jackctl_parameter_value default_name;
  59. /* bool, whether to be "realtime" */
  60. union jackctl_parameter_value realtime;
  61. union jackctl_parameter_value default_realtime;
  62. /* int32_t */
  63. union jackctl_parameter_value realtime_priority;
  64. union jackctl_parameter_value default_realtime_priority;
  65. /* bool, whether to exit once all clients have closed their connections */
  66. union jackctl_parameter_value temporary;
  67. union jackctl_parameter_value default_temporary;
  68. /* bool, whether to be verbose */
  69. union jackctl_parameter_value verbose;
  70. union jackctl_parameter_value default_verbose;
  71. /* int32_t, msecs; if zero, use period size. */
  72. union jackctl_parameter_value client_timeout;
  73. union jackctl_parameter_value default_client_timeout;
  74. /* uint32_t, clock source type */
  75. union jackctl_parameter_value clock_source;
  76. union jackctl_parameter_value default_clock_source;
  77. /* uint32_t, max port number */
  78. union jackctl_parameter_value port_max;
  79. union jackctl_parameter_value default_port_max;
  80. /* bool */
  81. union jackctl_parameter_value replace_registry;
  82. union jackctl_parameter_value default_replace_registry;
  83. /* bool, synchronous or asynchronous engine mode */
  84. union jackctl_parameter_value sync;
  85. union jackctl_parameter_value default_sync;
  86. /* char enum, self connect mode mode */
  87. union jackctl_parameter_value self_connect_mode;
  88. union jackctl_parameter_value default_self_connect_mode;
  89. };
  90. struct jackctl_driver
  91. {
  92. jack_driver_desc_t * desc_ptr;
  93. JSList * parameters;
  94. JSList * infos;
  95. };
  96. struct jackctl_internal
  97. {
  98. jack_driver_desc_t * desc_ptr;
  99. JSList * parameters;
  100. int refnum;
  101. };
  102. struct jackctl_parameter
  103. {
  104. const char * name;
  105. const char * short_description;
  106. const char * long_description;
  107. jackctl_param_type_t type;
  108. bool is_set;
  109. union jackctl_parameter_value * value_ptr;
  110. union jackctl_parameter_value * default_value_ptr;
  111. union jackctl_parameter_value value;
  112. union jackctl_parameter_value default_value;
  113. struct jackctl_driver * driver_ptr;
  114. char id;
  115. jack_driver_param_constraint_desc_t * constraint_ptr;
  116. };
  117. const char * jack_get_self_connect_mode_description(char mode)
  118. {
  119. struct jack_constraint_enum_char_descriptor * descr_ptr;
  120. for (descr_ptr = self_connect_mode_constraint_descr_array;
  121. descr_ptr->value;
  122. descr_ptr++)
  123. if (descr_ptr->value == mode) return descr_ptr->short_desc;
  124. return NULL;
  125. }
  126. static
  127. struct jackctl_parameter *
  128. jackctl_add_parameter(
  129. JSList ** parameters_list_ptr_ptr,
  130. const char * name,
  131. const char * short_description,
  132. const char * long_description,
  133. jackctl_param_type_t type,
  134. union jackctl_parameter_value * value_ptr,
  135. union jackctl_parameter_value * default_value_ptr,
  136. union jackctl_parameter_value value,
  137. jack_driver_param_constraint_desc_t * constraint_ptr = NULL)
  138. {
  139. struct jackctl_parameter * parameter_ptr;
  140. parameter_ptr = (struct jackctl_parameter *)malloc(sizeof(struct jackctl_parameter));
  141. if (parameter_ptr == NULL)
  142. {
  143. jack_error("Cannot allocate memory for jackctl_parameter structure.");
  144. goto fail;
  145. }
  146. parameter_ptr->name = name;
  147. parameter_ptr->short_description = short_description;
  148. parameter_ptr->long_description = long_description;
  149. parameter_ptr->type = type;
  150. parameter_ptr->is_set = false;
  151. if (value_ptr == NULL)
  152. {
  153. value_ptr = &parameter_ptr->value;
  154. }
  155. if (default_value_ptr == NULL)
  156. {
  157. default_value_ptr = &parameter_ptr->default_value;
  158. }
  159. parameter_ptr->value_ptr = value_ptr;
  160. parameter_ptr->default_value_ptr = default_value_ptr;
  161. *value_ptr = *default_value_ptr = value;
  162. parameter_ptr->driver_ptr = NULL;
  163. parameter_ptr->id = 0;
  164. parameter_ptr->constraint_ptr = constraint_ptr;
  165. *parameters_list_ptr_ptr = jack_slist_append(*parameters_list_ptr_ptr, parameter_ptr);
  166. return parameter_ptr;
  167. fail:
  168. return NULL;
  169. }
  170. static
  171. void
  172. jackctl_free_driver_parameters(
  173. struct jackctl_driver * driver_ptr)
  174. {
  175. JSList * next_node_ptr;
  176. while (driver_ptr->parameters)
  177. {
  178. next_node_ptr = driver_ptr->parameters->next;
  179. jack_constraint_free(((jackctl_parameter *)driver_ptr->parameters->data)->constraint_ptr);
  180. free(driver_ptr->parameters->data);
  181. free(driver_ptr->parameters);
  182. driver_ptr->parameters = next_node_ptr;
  183. }
  184. }
  185. static
  186. bool
  187. jackctl_add_driver_parameters(
  188. struct jackctl_driver * driver_ptr)
  189. {
  190. unsigned int i;
  191. union jackctl_parameter_value jackctl_value;
  192. jackctl_param_type_t jackctl_type;
  193. struct jackctl_parameter * parameter_ptr;
  194. jack_driver_param_desc_t * descriptor_ptr;
  195. for (i = 0 ; i < driver_ptr->desc_ptr->nparams ; i++)
  196. {
  197. descriptor_ptr = driver_ptr->desc_ptr->params + i;
  198. switch (descriptor_ptr->type)
  199. {
  200. case JackDriverParamInt:
  201. jackctl_type = JackParamInt;
  202. jackctl_value.i = descriptor_ptr->value.i;
  203. break;
  204. case JackDriverParamUInt:
  205. jackctl_type = JackParamUInt;
  206. jackctl_value.ui = descriptor_ptr->value.ui;
  207. break;
  208. case JackDriverParamChar:
  209. jackctl_type = JackParamChar;
  210. jackctl_value.c = descriptor_ptr->value.c;
  211. break;
  212. case JackDriverParamString:
  213. jackctl_type = JackParamString;
  214. strcpy(jackctl_value.str, descriptor_ptr->value.str);
  215. break;
  216. case JackDriverParamBool:
  217. jackctl_type = JackParamBool;
  218. jackctl_value.b = descriptor_ptr->value.i;
  219. break;
  220. default:
  221. jack_error("Unknown driver parameter type %i", (int)descriptor_ptr->type);
  222. assert(0);
  223. goto fail;
  224. }
  225. parameter_ptr = jackctl_add_parameter(
  226. &driver_ptr->parameters,
  227. descriptor_ptr->name,
  228. descriptor_ptr->short_desc,
  229. descriptor_ptr->long_desc,
  230. jackctl_type,
  231. NULL,
  232. NULL,
  233. jackctl_value,
  234. descriptor_ptr->constraint);
  235. if (parameter_ptr == NULL)
  236. {
  237. goto fail;
  238. }
  239. parameter_ptr->driver_ptr = driver_ptr;
  240. parameter_ptr->id = descriptor_ptr->character;
  241. }
  242. return true;
  243. fail:
  244. jackctl_free_driver_parameters(driver_ptr);
  245. return false;
  246. }
  247. /* destroy jack_driver_param_desc_t list created by jackctl_create_param_list() */
  248. static void
  249. jackctl_destroy_param_list(
  250. JSList * params)
  251. {
  252. JSList * next;
  253. while (params)
  254. {
  255. next = params->next;
  256. free(params->data);
  257. free(params);
  258. params = next;
  259. }
  260. }
  261. /* for drivers and internals are configured through jack_driver_param_t JSList */
  262. /* this function creates such list from a jackctl_parameter list */
  263. static
  264. bool
  265. jackctl_create_param_list(
  266. const JSList * paramlist,
  267. JSList ** retparamlist)
  268. {
  269. jackctl_parameter * param_ptr;
  270. jack_driver_param_t * retparam_ptr;
  271. *retparamlist = NULL;
  272. while (paramlist != NULL)
  273. {
  274. param_ptr = (jackctl_parameter *)paramlist->data;
  275. if (param_ptr->is_set)
  276. {
  277. /* jack_info("setting driver parameter %p ...", parameter_ptr); */
  278. retparam_ptr = (jack_driver_param_t *)malloc(sizeof(jack_driver_param_t));
  279. if (retparam_ptr == NULL)
  280. {
  281. jack_error ("Allocation of jack_driver_param_t structure failed");
  282. goto destroy;
  283. }
  284. retparam_ptr->character = param_ptr->id;
  285. switch (param_ptr->type)
  286. {
  287. case JackParamInt:
  288. retparam_ptr->value.i = param_ptr->value_ptr->i;
  289. break;
  290. case JackParamUInt:
  291. retparam_ptr->value.ui = param_ptr->value_ptr->ui;
  292. break;
  293. case JackParamChar:
  294. retparam_ptr->value.c = param_ptr->value_ptr->c;
  295. break;
  296. case JackParamString:
  297. strcpy(retparam_ptr->value.str, param_ptr->value_ptr->str);
  298. break;
  299. case JackParamBool:
  300. retparam_ptr->value.i = param_ptr->value_ptr->b;
  301. break;
  302. default:
  303. jack_error("Unknown parameter type %i", (int)param_ptr->type);
  304. assert(0);
  305. goto free;
  306. }
  307. *retparamlist = jack_slist_append(*retparamlist, retparam_ptr);
  308. }
  309. paramlist = paramlist->next;
  310. }
  311. return true;
  312. free:
  313. free(retparam_ptr);
  314. destroy:
  315. jackctl_destroy_param_list(*retparamlist);
  316. return false;
  317. }
  318. static int
  319. jackctl_drivers_load(
  320. struct jackctl_server * server_ptr)
  321. {
  322. struct jackctl_driver * driver_ptr;
  323. JSList *node_ptr;
  324. JSList *descriptor_node_ptr;
  325. descriptor_node_ptr = jack_drivers_load(NULL);
  326. if (descriptor_node_ptr == NULL)
  327. {
  328. jack_error("Could not find any drivers in driver directory!");
  329. return false;
  330. }
  331. while (descriptor_node_ptr != NULL)
  332. {
  333. driver_ptr = (struct jackctl_driver *)malloc(sizeof(struct jackctl_driver));
  334. if (driver_ptr == NULL)
  335. {
  336. jack_error("Memory allocation of jackctl_driver structure failed.");
  337. goto next;
  338. }
  339. driver_ptr->desc_ptr = (jack_driver_desc_t *)descriptor_node_ptr->data;
  340. driver_ptr->parameters = NULL;
  341. driver_ptr->infos = NULL;
  342. if (!jackctl_add_driver_parameters(driver_ptr))
  343. {
  344. assert(driver_ptr->parameters == NULL);
  345. free(driver_ptr);
  346. goto next;
  347. }
  348. server_ptr->drivers = jack_slist_append(server_ptr->drivers, driver_ptr);
  349. next:
  350. node_ptr = descriptor_node_ptr;
  351. descriptor_node_ptr = descriptor_node_ptr->next;
  352. free(node_ptr);
  353. }
  354. return true;
  355. }
  356. static
  357. void
  358. jackctl_server_free_drivers(
  359. struct jackctl_server * server_ptr)
  360. {
  361. JSList * next_node_ptr;
  362. struct jackctl_driver * driver_ptr;
  363. while (server_ptr->drivers)
  364. {
  365. next_node_ptr = server_ptr->drivers->next;
  366. driver_ptr = (struct jackctl_driver *)server_ptr->drivers->data;
  367. jackctl_free_driver_parameters(driver_ptr);
  368. free(driver_ptr->desc_ptr->params);
  369. free(driver_ptr->desc_ptr);
  370. free(driver_ptr);
  371. free(server_ptr->drivers);
  372. server_ptr->drivers = next_node_ptr;
  373. }
  374. }
  375. static int
  376. jackctl_internals_load(
  377. struct jackctl_server * server_ptr)
  378. {
  379. struct jackctl_internal * internal_ptr;
  380. JSList *node_ptr;
  381. JSList *descriptor_node_ptr;
  382. descriptor_node_ptr = jack_internals_load(NULL);
  383. if (descriptor_node_ptr == NULL)
  384. {
  385. jack_error("Could not find any internals in driver directory!");
  386. return false;
  387. }
  388. while (descriptor_node_ptr != NULL)
  389. {
  390. internal_ptr = (struct jackctl_internal *)malloc(sizeof(struct jackctl_internal));
  391. if (internal_ptr == NULL)
  392. {
  393. jack_error("Memory allocation of jackctl_driver structure failed.");
  394. goto next;
  395. }
  396. internal_ptr->desc_ptr = (jack_driver_desc_t *)descriptor_node_ptr->data;
  397. internal_ptr->parameters = NULL;
  398. internal_ptr->refnum = -1;
  399. if (!jackctl_add_driver_parameters((struct jackctl_driver *)internal_ptr))
  400. {
  401. assert(internal_ptr->parameters == NULL);
  402. free(internal_ptr);
  403. goto next;
  404. }
  405. server_ptr->internals = jack_slist_append(server_ptr->internals, internal_ptr);
  406. next:
  407. node_ptr = descriptor_node_ptr;
  408. descriptor_node_ptr = descriptor_node_ptr->next;
  409. free(node_ptr);
  410. }
  411. return true;
  412. }
  413. static
  414. void
  415. jackctl_server_free_internals(
  416. struct jackctl_server * server_ptr)
  417. {
  418. JSList * next_node_ptr;
  419. struct jackctl_internal * internal_ptr;
  420. while (server_ptr->internals)
  421. {
  422. next_node_ptr = server_ptr->internals->next;
  423. internal_ptr = (struct jackctl_internal *)server_ptr->internals->data;
  424. jackctl_free_driver_parameters((struct jackctl_driver *)internal_ptr);
  425. free(internal_ptr->desc_ptr->params);
  426. free(internal_ptr->desc_ptr);
  427. free(internal_ptr);
  428. free(server_ptr->internals);
  429. server_ptr->internals = next_node_ptr;
  430. }
  431. }
  432. static
  433. void
  434. jackctl_server_free_parameters(
  435. struct jackctl_server * server_ptr)
  436. {
  437. JSList * next_node_ptr;
  438. while (server_ptr->parameters)
  439. {
  440. next_node_ptr = server_ptr->parameters->next;
  441. jack_constraint_free(((jackctl_parameter *)server_ptr->parameters->data)->constraint_ptr);
  442. free(server_ptr->parameters->data);
  443. free(server_ptr->parameters);
  444. server_ptr->parameters = next_node_ptr;
  445. }
  446. }
  447. #ifdef WIN32
  448. struct jackctl_sigmask
  449. {
  450. HANDLE wait_event;
  451. };
  452. static jackctl_sigmask sigmask;
  453. static void signal_handler(int signum)
  454. {
  455. printf("Jack main caught signal %d\n", signum);
  456. (void) signal(SIGINT, SIG_DFL);
  457. SetEvent(sigmask.wait_event);
  458. }
  459. jackctl_sigmask_t *
  460. jackctl_setup_signals(
  461. unsigned int flags)
  462. {
  463. if ((sigmask.wait_event = CreateEvent(NULL, FALSE, FALSE, NULL)) == NULL) {
  464. jack_error("CreateEvent fails err = %ld", GetLastError());
  465. return 0;
  466. }
  467. (void) signal(SIGINT, signal_handler);
  468. (void) signal(SIGABRT, signal_handler);
  469. (void) signal(SIGTERM, signal_handler);
  470. return &sigmask;
  471. }
  472. void jackctl_wait_signals(jackctl_sigmask_t * signals)
  473. {
  474. if (WaitForSingleObject(signals->wait_event, INFINITE) != WAIT_OBJECT_0) {
  475. jack_error("WaitForSingleObject fails err = %ld", GetLastError());
  476. }
  477. }
  478. #else
  479. struct jackctl_sigmask
  480. {
  481. sigset_t signals;
  482. };
  483. static jackctl_sigmask sigmask;
  484. static
  485. void
  486. signal_handler(int sig)
  487. {
  488. /* this is used by the child (active) process, but it never
  489. gets called unless we are already shutting down after
  490. another signal.
  491. */
  492. char buf[64];
  493. snprintf(buf, sizeof(buf), "Received signal %d during shutdown (ignored)\n", sig);
  494. }
  495. SERVER_EXPORT jackctl_sigmask_t *
  496. jackctl_setup_signals(
  497. unsigned int flags)
  498. {
  499. sigset_t allsignals;
  500. struct sigaction action;
  501. int i;
  502. /* ensure that we are in our own process group so that
  503. kill (SIG, -pgrp) does the right thing.
  504. */
  505. setsid();
  506. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  507. /* what's this for?
  508. POSIX says that signals are delivered like this:
  509. * if a thread has blocked that signal, it is not
  510. a candidate to receive the signal.
  511. * of all threads not blocking the signal, pick
  512. one at random, and deliver the signal.
  513. this means that a simple-minded multi-threaded program can
  514. expect to get POSIX signals delivered randomly to any one
  515. of its threads,
  516. here, we block all signals that we think we might receive
  517. and want to catch. all "child" threads will inherit this
  518. setting. if we create a thread that calls sigwait() on the
  519. same set of signals, implicitly unblocking all those
  520. signals. any of those signals that are delivered to the
  521. process will be delivered to that thread, and that thread
  522. alone. this makes cleanup for a signal-driven exit much
  523. easier, since we know which thread is doing it and more
  524. importantly, we are free to call async-unsafe functions,
  525. because the code is executing in normal thread context
  526. after a return from sigwait().
  527. */
  528. sigemptyset(&sigmask.signals);
  529. sigaddset(&sigmask.signals, SIGHUP);
  530. sigaddset(&sigmask.signals, SIGINT);
  531. sigaddset(&sigmask.signals, SIGQUIT);
  532. sigaddset(&sigmask.signals, SIGPIPE);
  533. sigaddset(&sigmask.signals, SIGTERM);
  534. #ifndef __ANDROID__
  535. /* android's bionic c doesn't provide pthread_cancel() and related functions.
  536. * to solve this issue, use pthread_kill() & SIGUSR1 instead.
  537. */
  538. sigaddset(&sigmask.signals, SIGUSR1);
  539. #endif
  540. sigaddset(&sigmask.signals, SIGUSR2);
  541. /* all child threads will inherit this mask unless they
  542. * explicitly reset it
  543. */
  544. pthread_sigmask(SIG_BLOCK, &sigmask.signals, 0);
  545. /* install a do-nothing handler because otherwise pthreads
  546. behaviour is undefined when we enter sigwait.
  547. */
  548. sigfillset(&allsignals);
  549. action.sa_handler = signal_handler;
  550. action.sa_mask = allsignals;
  551. action.sa_flags = SA_RESTART|SA_RESETHAND;
  552. for (i = 1; i < NSIG; i++)
  553. {
  554. if (sigismember (&sigmask.signals, i))
  555. {
  556. sigaction(i, &action, 0);
  557. }
  558. }
  559. return &sigmask;
  560. }
  561. SERVER_EXPORT void
  562. jackctl_wait_signals(jackctl_sigmask_t * sigmask)
  563. {
  564. int sig;
  565. bool waiting = true;
  566. while (waiting) {
  567. #if defined(sun) && !defined(__sun__) // SUN compiler only, to check
  568. sigwait(&sigmask->signals);
  569. #else
  570. sigwait(&sigmask->signals, &sig);
  571. #endif
  572. fprintf(stderr, "Jack main caught signal %d\n", sig);
  573. switch (sig) {
  574. case SIGUSR1:
  575. //jack_dump_configuration(engine, 1);
  576. break;
  577. case SIGUSR2:
  578. // driver exit
  579. waiting = false;
  580. break;
  581. case SIGTTOU:
  582. break;
  583. default:
  584. waiting = false;
  585. break;
  586. }
  587. }
  588. if (sig != SIGSEGV) {
  589. // unblock signals so we can see them during shutdown.
  590. // this will help prod developers not to lose sight of
  591. // bugs that cause segfaults etc. during shutdown.
  592. sigprocmask(SIG_UNBLOCK, &sigmask->signals, 0);
  593. }
  594. }
  595. #endif
  596. static
  597. jack_driver_param_constraint_desc_t *
  598. get_realtime_priority_constraint()
  599. {
  600. jack_driver_param_constraint_desc_t * constraint_ptr;
  601. int min, max;
  602. if (!jack_get_thread_realtime_priority_range(&min, &max))
  603. {
  604. return NULL;
  605. }
  606. //jack_info("realtime priority range is (%d,%d)", min, max);
  607. constraint_ptr = (jack_driver_param_constraint_desc_t *)calloc(1, sizeof(jack_driver_param_constraint_desc_t));
  608. if (constraint_ptr == NULL)
  609. {
  610. jack_error("Cannot allocate memory for jack_driver_param_constraint_desc_t structure.");
  611. return NULL;
  612. }
  613. constraint_ptr->flags = JACK_CONSTRAINT_FLAG_RANGE;
  614. constraint_ptr->constraint.range.min.i = min;
  615. constraint_ptr->constraint.range.max.i = max;
  616. return constraint_ptr;
  617. }
  618. SERVER_EXPORT jackctl_server_t * jackctl_server_create(
  619. bool (* on_device_acquire)(const char * device_name),
  620. void (* on_device_release)(const char * device_name))
  621. {
  622. return jackctl_server_create2(on_device_acquire, on_device_release, NULL);
  623. }
  624. SERVER_EXPORT jackctl_server_t * jackctl_server_create2(
  625. bool (* on_device_acquire)(const char * device_name),
  626. void (* on_device_release)(const char * device_name),
  627. void (* on_device_reservation_loop)(void))
  628. {
  629. struct jackctl_server * server_ptr;
  630. union jackctl_parameter_value value;
  631. server_ptr = (struct jackctl_server *)malloc(sizeof(struct jackctl_server));
  632. if (server_ptr == NULL)
  633. {
  634. jack_error("Cannot allocate memory for jackctl_server structure.");
  635. goto fail;
  636. }
  637. server_ptr->drivers = NULL;
  638. server_ptr->internals = NULL;
  639. server_ptr->parameters = NULL;
  640. server_ptr->engine = NULL;
  641. strcpy(value.str, JackTools::DefaultServerName());
  642. if (jackctl_add_parameter(
  643. &server_ptr->parameters,
  644. "name",
  645. "Server name to use.",
  646. "",
  647. JackParamString,
  648. &server_ptr->name,
  649. &server_ptr->default_name,
  650. value) == NULL)
  651. {
  652. goto fail_free_parameters;
  653. }
  654. value.b = true;
  655. if (jackctl_add_parameter(
  656. &server_ptr->parameters,
  657. "realtime",
  658. "Whether to use realtime mode.",
  659. "Use realtime scheduling. This is needed for reliable low-latency performance. On most systems, it requires JACK to run with special scheduler and memory allocation privileges, which may be obtained in several ways. On Linux you should use PAM.",
  660. JackParamBool,
  661. &server_ptr->realtime,
  662. &server_ptr->default_realtime,
  663. value) == NULL)
  664. {
  665. goto fail_free_parameters;
  666. }
  667. value.i = 10;
  668. if (jackctl_add_parameter(
  669. &server_ptr->parameters,
  670. "realtime-priority",
  671. "Scheduler priority when running in realtime mode.",
  672. "",
  673. JackParamInt,
  674. &server_ptr->realtime_priority,
  675. &server_ptr->default_realtime_priority,
  676. value,
  677. get_realtime_priority_constraint()) == NULL)
  678. {
  679. goto fail_free_parameters;
  680. }
  681. value.b = false;
  682. if (jackctl_add_parameter(
  683. &server_ptr->parameters,
  684. "temporary",
  685. "Exit once all clients have closed their connections.",
  686. "",
  687. JackParamBool,
  688. &server_ptr->temporary,
  689. &server_ptr->default_temporary,
  690. value) == NULL)
  691. {
  692. goto fail_free_parameters;
  693. }
  694. value.b = false;
  695. if (jackctl_add_parameter(
  696. &server_ptr->parameters,
  697. "verbose",
  698. "Verbose mode.",
  699. "",
  700. JackParamBool,
  701. &server_ptr->verbose,
  702. &server_ptr->default_verbose,
  703. value) == NULL)
  704. {
  705. goto fail_free_parameters;
  706. }
  707. value.i = 0;
  708. if (jackctl_add_parameter(
  709. &server_ptr->parameters,
  710. "client-timeout",
  711. "Client timeout limit in milliseconds.",
  712. "",
  713. JackParamInt,
  714. &server_ptr->client_timeout,
  715. &server_ptr->default_client_timeout,
  716. value) == NULL)
  717. {
  718. goto fail_free_parameters;
  719. }
  720. value.ui = 0;
  721. if (jackctl_add_parameter(
  722. &server_ptr->parameters,
  723. "clock-source",
  724. "Clocksource type : c(ycle) | h(pet) | s(ystem).",
  725. "",
  726. JackParamUInt,
  727. &server_ptr->clock_source,
  728. &server_ptr->default_clock_source,
  729. value) == NULL)
  730. {
  731. goto fail_free_parameters;
  732. }
  733. value.ui = PORT_NUM;
  734. if (jackctl_add_parameter(
  735. &server_ptr->parameters,
  736. "port-max",
  737. "Maximum number of ports.",
  738. "",
  739. JackParamUInt,
  740. &server_ptr->port_max,
  741. &server_ptr->default_port_max,
  742. value) == NULL)
  743. {
  744. goto fail_free_parameters;
  745. }
  746. value.b = false;
  747. if (jackctl_add_parameter(
  748. &server_ptr->parameters,
  749. "replace-registry",
  750. "Replace shared memory registry.",
  751. "",
  752. JackParamBool,
  753. &server_ptr->replace_registry,
  754. &server_ptr->default_replace_registry,
  755. value) == NULL)
  756. {
  757. goto fail_free_parameters;
  758. }
  759. value.b = false;
  760. if (jackctl_add_parameter(
  761. &server_ptr->parameters,
  762. "sync",
  763. "Use server synchronous mode.",
  764. "",
  765. JackParamBool,
  766. &server_ptr->sync,
  767. &server_ptr->default_sync,
  768. value) == NULL)
  769. {
  770. goto fail_free_parameters;
  771. }
  772. value.c = JACK_DEFAULT_SELF_CONNECT_MODE;
  773. if (jackctl_add_parameter(
  774. &server_ptr->parameters,
  775. "self-connect-mode",
  776. "Self connect mode.",
  777. "Whether JACK clients are allowed to connect their own ports",
  778. JackParamChar,
  779. &server_ptr->self_connect_mode,
  780. &server_ptr->default_self_connect_mode,
  781. value,
  782. jack_constraint_compose_enum_char(
  783. JACK_CONSTRAINT_FLAG_STRICT | JACK_CONSTRAINT_FLAG_FAKE_VALUE,
  784. self_connect_mode_constraint_descr_array)) == NULL)
  785. {
  786. goto fail_free_parameters;
  787. }
  788. JackServerGlobals::on_device_acquire = on_device_acquire;
  789. JackServerGlobals::on_device_release = on_device_release;
  790. JackServerGlobals::on_device_reservation_loop = on_device_reservation_loop;
  791. if (!jackctl_drivers_load(server_ptr))
  792. {
  793. goto fail_free_parameters;
  794. }
  795. /* Allowed to fail */
  796. jackctl_internals_load(server_ptr);
  797. return server_ptr;
  798. fail_free_parameters:
  799. jackctl_server_free_parameters(server_ptr);
  800. free(server_ptr);
  801. fail:
  802. return NULL;
  803. }
  804. SERVER_EXPORT void jackctl_server_destroy(jackctl_server *server_ptr)
  805. {
  806. if (server_ptr) {
  807. jackctl_server_free_drivers(server_ptr);
  808. jackctl_server_free_internals(server_ptr);
  809. jackctl_server_free_parameters(server_ptr);
  810. free(server_ptr);
  811. }
  812. }
  813. SERVER_EXPORT const JSList * jackctl_server_get_drivers_list(jackctl_server *server_ptr)
  814. {
  815. return (server_ptr) ? server_ptr->drivers : NULL;
  816. }
  817. SERVER_EXPORT bool jackctl_server_stop(jackctl_server *server_ptr)
  818. {
  819. if (server_ptr) {
  820. server_ptr->engine->Stop();
  821. return true;
  822. } else {
  823. return false;
  824. }
  825. }
  826. SERVER_EXPORT bool jackctl_server_close(jackctl_server *server_ptr)
  827. {
  828. if (server_ptr) {
  829. server_ptr->engine->Close();
  830. delete server_ptr->engine;
  831. /* clean up shared memory and files from this server instance */
  832. jack_log("Cleaning up shared memory");
  833. jack_cleanup_shm();
  834. jack_log("Cleaning up files");
  835. JackTools::CleanupFiles(server_ptr->name.str);
  836. jack_log("Unregistering server `%s'", server_ptr->name.str);
  837. jack_unregister_server(server_ptr->name.str);
  838. server_ptr->engine = NULL;
  839. return true;
  840. } else {
  841. return false;
  842. }
  843. }
  844. SERVER_EXPORT const JSList * jackctl_server_get_parameters(jackctl_server *server_ptr)
  845. {
  846. return (server_ptr) ? server_ptr->parameters : NULL;
  847. }
  848. SERVER_EXPORT bool
  849. jackctl_server_open(
  850. jackctl_server *server_ptr,
  851. jackctl_driver *driver_ptr)
  852. {
  853. JSList * paramlist = NULL;
  854. try {
  855. if (!server_ptr || !driver_ptr) {
  856. return false;
  857. }
  858. int rc = jack_register_server(server_ptr->name.str, server_ptr->replace_registry.b);
  859. switch (rc)
  860. {
  861. case EEXIST:
  862. jack_error("`%s' server already active", server_ptr->name.str);
  863. goto fail;
  864. case ENOSPC:
  865. jack_error("Too many servers already active");
  866. goto fail;
  867. case ENOMEM:
  868. jack_error("No access to shm registry");
  869. goto fail;
  870. }
  871. jack_log("Server `%s' registered", server_ptr->name.str);
  872. /* clean up shared memory and files from any previous
  873. * instance of this server name */
  874. jack_cleanup_shm();
  875. JackTools::CleanupFiles(server_ptr->name.str);
  876. if (!server_ptr->realtime.b && server_ptr->client_timeout.i == 0) {
  877. server_ptr->client_timeout.i = 500; /* 0.5 sec; usable when non realtime. */
  878. }
  879. /* check port max value before allocating server */
  880. if (server_ptr->port_max.ui > PORT_NUM_MAX) {
  881. jack_error("Jack server started with too much ports %d (when port max can be %d)", server_ptr->port_max.ui, PORT_NUM_MAX);
  882. goto fail;
  883. }
  884. /* get the engine/driver started */
  885. server_ptr->engine = new JackServer(
  886. server_ptr->sync.b,
  887. server_ptr->temporary.b,
  888. server_ptr->client_timeout.i,
  889. server_ptr->realtime.b,
  890. server_ptr->realtime_priority.i,
  891. server_ptr->port_max.ui,
  892. server_ptr->verbose.b,
  893. (jack_timer_type_t)server_ptr->clock_source.ui,
  894. server_ptr->self_connect_mode.c,
  895. server_ptr->name.str);
  896. if (server_ptr->engine == NULL)
  897. {
  898. jack_error("Failed to create new JackServer object");
  899. goto fail_unregister;
  900. }
  901. if (!jackctl_create_param_list(driver_ptr->parameters, &paramlist)) goto fail_delete;
  902. rc = server_ptr->engine->Open(driver_ptr->desc_ptr, paramlist);
  903. jackctl_destroy_param_list(paramlist);
  904. if (rc < 0)
  905. {
  906. jack_error("JackServer::Open failed with %d", rc);
  907. goto fail_delete;
  908. }
  909. return true;
  910. } catch (std::exception&) {
  911. jack_error("jackctl_server_open error...");
  912. jackctl_destroy_param_list(paramlist);
  913. }
  914. fail_delete:
  915. delete server_ptr->engine;
  916. server_ptr->engine = NULL;
  917. fail_unregister:
  918. jack_log("Cleaning up shared memory");
  919. jack_cleanup_shm();
  920. jack_log("Cleaning up files");
  921. JackTools::CleanupFiles(server_ptr->name.str);
  922. jack_log("Unregistering server `%s'", server_ptr->name.str);
  923. jack_unregister_server(server_ptr->name.str);
  924. fail:
  925. return false;
  926. }
  927. SERVER_EXPORT bool
  928. jackctl_server_start(
  929. jackctl_server *server_ptr)
  930. {
  931. if (!server_ptr) {
  932. return false;
  933. } else {
  934. int rc = server_ptr->engine->Start();
  935. bool result = rc >= 0;
  936. if (! result)
  937. {
  938. jack_error("JackServer::Start() failed with %d", rc);
  939. }
  940. return result;
  941. }
  942. }
  943. SERVER_EXPORT const char * jackctl_driver_get_name(jackctl_driver *driver_ptr)
  944. {
  945. return (driver_ptr) ? driver_ptr->desc_ptr->name : NULL;
  946. }
  947. SERVER_EXPORT jackctl_driver_type_t jackctl_driver_get_type(jackctl_driver *driver_ptr)
  948. {
  949. return (driver_ptr) ? (jackctl_driver_type_t)driver_ptr->desc_ptr->type : (jackctl_driver_type_t)0;
  950. }
  951. SERVER_EXPORT const JSList * jackctl_driver_get_parameters(jackctl_driver *driver_ptr)
  952. {
  953. return (driver_ptr) ? driver_ptr->parameters : NULL;
  954. }
  955. SERVER_EXPORT jack_driver_desc_t * jackctl_driver_get_desc(jackctl_driver *driver_ptr)
  956. {
  957. return (driver_ptr) ? driver_ptr->desc_ptr : NULL;
  958. }
  959. SERVER_EXPORT const char * jackctl_parameter_get_name(jackctl_parameter *parameter_ptr)
  960. {
  961. return (parameter_ptr) ? parameter_ptr->name : NULL;
  962. }
  963. SERVER_EXPORT const char * jackctl_parameter_get_short_description(jackctl_parameter *parameter_ptr)
  964. {
  965. return (parameter_ptr) ? parameter_ptr->short_description : NULL;
  966. }
  967. SERVER_EXPORT const char * jackctl_parameter_get_long_description(jackctl_parameter *parameter_ptr)
  968. {
  969. return (parameter_ptr) ? parameter_ptr->long_description : NULL;
  970. }
  971. SERVER_EXPORT bool jackctl_parameter_has_range_constraint(jackctl_parameter *parameter_ptr)
  972. {
  973. return (parameter_ptr) ? (parameter_ptr->constraint_ptr != NULL && (parameter_ptr->constraint_ptr->flags & JACK_CONSTRAINT_FLAG_RANGE) != 0) : false;
  974. }
  975. SERVER_EXPORT bool jackctl_parameter_has_enum_constraint(jackctl_parameter *parameter_ptr)
  976. {
  977. return (parameter_ptr) ? (parameter_ptr->constraint_ptr != NULL && (parameter_ptr->constraint_ptr->flags & JACK_CONSTRAINT_FLAG_RANGE) == 0): false;
  978. }
  979. SERVER_EXPORT uint32_t jackctl_parameter_get_enum_constraints_count(jackctl_parameter *parameter_ptr)
  980. {
  981. if (!parameter_ptr) {
  982. return 0;
  983. }
  984. if (!jackctl_parameter_has_enum_constraint(parameter_ptr))
  985. {
  986. return 0;
  987. }
  988. return parameter_ptr->constraint_ptr->constraint.enumeration.count;
  989. }
  990. SERVER_EXPORT union jackctl_parameter_value jackctl_parameter_get_enum_constraint_value(jackctl_parameter *parameter_ptr, uint32_t index)
  991. {
  992. jack_driver_param_value_t * value_ptr;
  993. union jackctl_parameter_value jackctl_value;
  994. if (!parameter_ptr) {
  995. memset(&jackctl_value, 0, sizeof(jackctl_value));
  996. return jackctl_value;
  997. }
  998. value_ptr = &parameter_ptr->constraint_ptr->constraint.enumeration.possible_values_array[index].value;
  999. switch (parameter_ptr->type)
  1000. {
  1001. case JackParamInt:
  1002. jackctl_value.i = value_ptr->i;
  1003. break;
  1004. case JackParamUInt:
  1005. jackctl_value.ui = value_ptr->ui;
  1006. break;
  1007. case JackParamChar:
  1008. jackctl_value.c = value_ptr->c;
  1009. break;
  1010. case JackParamString:
  1011. strcpy(jackctl_value.str, value_ptr->str);
  1012. break;
  1013. default:
  1014. jack_error("Bad driver parameter type %i (enum constraint)", (int)parameter_ptr->type);
  1015. assert(0);
  1016. }
  1017. return jackctl_value;
  1018. }
  1019. SERVER_EXPORT const char * jackctl_parameter_get_enum_constraint_description(jackctl_parameter *parameter_ptr, uint32_t index)
  1020. {
  1021. return (parameter_ptr) ? parameter_ptr->constraint_ptr->constraint.enumeration.possible_values_array[index].short_desc : NULL;
  1022. }
  1023. SERVER_EXPORT void jackctl_parameter_get_range_constraint(jackctl_parameter *parameter_ptr, union jackctl_parameter_value * min_ptr, union jackctl_parameter_value * max_ptr)
  1024. {
  1025. if (!parameter_ptr || !min_ptr || !max_ptr) {
  1026. return;
  1027. }
  1028. switch (parameter_ptr->type)
  1029. {
  1030. case JackParamInt:
  1031. min_ptr->i = parameter_ptr->constraint_ptr->constraint.range.min.i;
  1032. max_ptr->i = parameter_ptr->constraint_ptr->constraint.range.max.i;
  1033. return;
  1034. case JackParamUInt:
  1035. min_ptr->ui = parameter_ptr->constraint_ptr->constraint.range.min.ui;
  1036. max_ptr->ui = parameter_ptr->constraint_ptr->constraint.range.max.ui;
  1037. return;
  1038. default:
  1039. jack_error("Bad driver parameter type %i (range constraint)", (int)parameter_ptr->type);
  1040. assert(0);
  1041. }
  1042. }
  1043. SERVER_EXPORT bool jackctl_parameter_constraint_is_strict(jackctl_parameter_t * parameter_ptr)
  1044. {
  1045. return (parameter_ptr) ? (parameter_ptr->constraint_ptr != NULL && (parameter_ptr->constraint_ptr->flags & JACK_CONSTRAINT_FLAG_STRICT) != 0) : false;
  1046. }
  1047. SERVER_EXPORT bool jackctl_parameter_constraint_is_fake_value(jackctl_parameter_t * parameter_ptr)
  1048. {
  1049. return (parameter_ptr) ? (parameter_ptr->constraint_ptr != NULL && (parameter_ptr->constraint_ptr->flags & JACK_CONSTRAINT_FLAG_FAKE_VALUE) != 0) : false;
  1050. }
  1051. SERVER_EXPORT jackctl_param_type_t jackctl_parameter_get_type(jackctl_parameter *parameter_ptr)
  1052. {
  1053. return (parameter_ptr) ? parameter_ptr->type : (jackctl_param_type_t)0;
  1054. }
  1055. SERVER_EXPORT char jackctl_parameter_get_id(jackctl_parameter_t * parameter_ptr)
  1056. {
  1057. return (parameter_ptr) ? parameter_ptr->id : 0;
  1058. }
  1059. SERVER_EXPORT bool jackctl_parameter_is_set(jackctl_parameter *parameter_ptr)
  1060. {
  1061. return (parameter_ptr) ? parameter_ptr->is_set : false;
  1062. }
  1063. SERVER_EXPORT union jackctl_parameter_value jackctl_parameter_get_value(jackctl_parameter *parameter_ptr)
  1064. {
  1065. if (parameter_ptr) {
  1066. return *parameter_ptr->value_ptr;
  1067. } else {
  1068. union jackctl_parameter_value jackctl_value;
  1069. memset(&jackctl_value, 0, sizeof(jackctl_value));
  1070. return jackctl_value;
  1071. }
  1072. }
  1073. SERVER_EXPORT bool jackctl_parameter_reset(jackctl_parameter *parameter_ptr)
  1074. {
  1075. if (!parameter_ptr) {
  1076. return false;
  1077. }
  1078. if (!parameter_ptr->is_set)
  1079. {
  1080. return true;
  1081. }
  1082. parameter_ptr->is_set = false;
  1083. *parameter_ptr->value_ptr = *parameter_ptr->default_value_ptr;
  1084. return true;
  1085. }
  1086. SERVER_EXPORT bool jackctl_parameter_set_value(jackctl_parameter *parameter_ptr, const union jackctl_parameter_value * value_ptr)
  1087. {
  1088. if (!parameter_ptr || !value_ptr) {
  1089. return false;
  1090. }
  1091. parameter_ptr->is_set = true;
  1092. *parameter_ptr->value_ptr = *value_ptr;
  1093. return true;
  1094. }
  1095. SERVER_EXPORT union jackctl_parameter_value jackctl_parameter_get_default_value(jackctl_parameter *parameter_ptr)
  1096. {
  1097. if (parameter_ptr) {
  1098. return *parameter_ptr->default_value_ptr;
  1099. } else {
  1100. union jackctl_parameter_value jackctl_value;
  1101. memset(&jackctl_value, 0, sizeof(jackctl_value));
  1102. return jackctl_value;
  1103. }
  1104. }
  1105. // Internals clients
  1106. SERVER_EXPORT const JSList * jackctl_server_get_internals_list(jackctl_server *server_ptr)
  1107. {
  1108. return (server_ptr) ? server_ptr->internals : NULL;
  1109. }
  1110. SERVER_EXPORT const char * jackctl_internal_get_name(jackctl_internal *internal_ptr)
  1111. {
  1112. return (internal_ptr) ? internal_ptr->desc_ptr->name : NULL;
  1113. }
  1114. SERVER_EXPORT const JSList * jackctl_internal_get_parameters(jackctl_internal *internal_ptr)
  1115. {
  1116. return (internal_ptr) ? internal_ptr->parameters : NULL;
  1117. }
  1118. SERVER_EXPORT bool jackctl_server_load_internal(
  1119. jackctl_server * server_ptr,
  1120. jackctl_internal * internal)
  1121. {
  1122. if (!server_ptr || !internal) {
  1123. return false;
  1124. }
  1125. int status;
  1126. if (server_ptr->engine != NULL) {
  1127. JSList * paramlist;
  1128. if (!jackctl_create_param_list(internal->parameters, &paramlist)) return false;
  1129. server_ptr->engine->InternalClientLoad2(internal->desc_ptr->name, internal->desc_ptr->name, paramlist, JackNullOption, &internal->refnum, -1, &status);
  1130. jackctl_destroy_param_list(paramlist);
  1131. return (internal->refnum > 0);
  1132. } else {
  1133. return false;
  1134. }
  1135. }
  1136. SERVER_EXPORT bool jackctl_server_unload_internal(
  1137. jackctl_server * server_ptr,
  1138. jackctl_internal * internal)
  1139. {
  1140. if (!server_ptr || !internal) {
  1141. return false;
  1142. }
  1143. int status;
  1144. if (server_ptr->engine != NULL && internal->refnum > 0) {
  1145. // Client object is internally kept in JackEngine, and will be deallocated in InternalClientUnload
  1146. return ((server_ptr->engine->GetEngine()->InternalClientUnload(internal->refnum, &status)) == 0);
  1147. } else {
  1148. return false;
  1149. }
  1150. }
  1151. SERVER_EXPORT bool jackctl_server_load_session_file(
  1152. jackctl_server * server_ptr,
  1153. const char * file)
  1154. {
  1155. if (!server_ptr || !file || !server_ptr->engine) {
  1156. return false;
  1157. }
  1158. return (server_ptr->engine->LoadInternalSessionFile(file) >= 0);
  1159. }
  1160. SERVER_EXPORT bool jackctl_server_add_slave(jackctl_server * server_ptr, jackctl_driver * driver_ptr)
  1161. {
  1162. if (server_ptr && server_ptr->engine) {
  1163. if (server_ptr->engine->IsRunning()) {
  1164. jack_error("Cannot add a slave in a running server");
  1165. return false;
  1166. } else {
  1167. JSList * paramlist;
  1168. if (!jackctl_create_param_list(driver_ptr->parameters, &paramlist)) return false;
  1169. JackDriverInfo* info = server_ptr->engine->AddSlave(driver_ptr->desc_ptr, paramlist);
  1170. jackctl_destroy_param_list(paramlist);
  1171. if (info) {
  1172. driver_ptr->infos = jack_slist_append(driver_ptr->infos, info);
  1173. return true;
  1174. } else {
  1175. return false;
  1176. }
  1177. }
  1178. } else {
  1179. return false;
  1180. }
  1181. }
  1182. SERVER_EXPORT bool jackctl_server_remove_slave(jackctl_server * server_ptr, jackctl_driver * driver_ptr)
  1183. {
  1184. if (server_ptr && server_ptr->engine) {
  1185. if (server_ptr->engine->IsRunning()) {
  1186. jack_error("Cannot remove a slave from a running server");
  1187. return false;
  1188. } else {
  1189. if (driver_ptr->infos) {
  1190. JackDriverInfo* info = (JackDriverInfo*)driver_ptr->infos->data;
  1191. assert(info);
  1192. driver_ptr->infos = jack_slist_remove(driver_ptr->infos, info);
  1193. server_ptr->engine->RemoveSlave(info);
  1194. delete info;
  1195. return true;
  1196. } else {
  1197. return false;
  1198. }
  1199. }
  1200. } else {
  1201. return false;
  1202. }
  1203. }
  1204. SERVER_EXPORT bool jackctl_server_switch_master(jackctl_server * server_ptr, jackctl_driver * driver_ptr)
  1205. {
  1206. if (server_ptr && server_ptr->engine) {
  1207. JSList * paramlist;
  1208. if (!jackctl_create_param_list(driver_ptr->parameters, &paramlist)) return false;
  1209. bool ret = (server_ptr->engine->SwitchMaster(driver_ptr->desc_ptr, paramlist) == 0);
  1210. jackctl_destroy_param_list(paramlist);
  1211. return ret;
  1212. } else {
  1213. return false;
  1214. }
  1215. }