jack1 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.

1979 lines
46KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 Paul Davis
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. $Id$
  16. */
  17. #include <config.h>
  18. #include <pthread.h>
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <stdarg.h>
  22. #include <stdio.h>
  23. #ifdef HAVE_STDINT_H
  24. #include <stdint.h>
  25. #endif
  26. #include <regex.h>
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include <sys/un.h>
  31. #ifdef USE_MLOCK
  32. #include <sys/mman.h>
  33. #endif /* USE_MLOCK */
  34. #include <jack/internal.h>
  35. #include <jack/jack.h>
  36. #include <jack/engine.h>
  37. #include <jack/pool.h>
  38. #include <jack/jslist.h>
  39. #include <jack/version.h>
  40. #include <jack/shm.h>
  41. #include <sysdeps/time.h>
  42. JACK_TIME_GLOBAL_DECL; /* One instance per process. */
  43. #include "local.h"
  44. #include <sysdeps/poll.h>
  45. #include <sysdeps/ipc.h>
  46. #ifdef JACK_USE_MACH_THREADS
  47. #include <sysdeps/pThreadUtilities.h>
  48. #endif
  49. #ifdef WITH_TIMESTAMPS
  50. #include <jack/timestamps.h>
  51. #endif /* WITH_TIMESTAMPS */
  52. char *jack_server_dir = DEFAULT_TMP_DIR;
  53. void
  54. jack_set_server_dir (const char *path)
  55. {
  56. jack_error ("jack_set_server_dir() is deprecated.\n"
  57. "Please contact the program's author");
  58. jack_server_dir = strdup (path);
  59. }
  60. static pthread_mutex_t client_lock;
  61. static pthread_cond_t client_ready;
  62. void *jack_zero_filled_buffer = NULL;
  63. #define event_fd pollfd[0].fd
  64. #define graph_wait_fd pollfd[1].fd
  65. typedef struct {
  66. int status;
  67. struct _jack_client *client;
  68. const char *client_name;
  69. } client_info;
  70. void
  71. jack_error (const char *fmt, ...)
  72. {
  73. va_list ap;
  74. char buffer[300];
  75. va_start (ap, fmt);
  76. vsnprintf (buffer, sizeof(buffer), fmt, ap);
  77. jack_error_callback (buffer);
  78. va_end (ap);
  79. }
  80. void
  81. default_jack_error_callback (const char *desc)
  82. {
  83. fprintf(stderr, "%s\n", desc);
  84. }
  85. void
  86. silent_jack_error_callback (const char *desc)
  87. {
  88. }
  89. void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  90. static int
  91. oop_client_deliver_request (void *ptr, jack_request_t *req)
  92. {
  93. int wok, rok;
  94. jack_client_t *client = (jack_client_t*) ptr;
  95. wok = (write (client->request_fd, req, sizeof (*req))
  96. == sizeof (*req));
  97. rok = (read (client->request_fd, req, sizeof (*req))
  98. == sizeof (*req));
  99. if (wok && rok) /* everything OK? */
  100. return req->status;
  101. req->status = -1; /* request failed */
  102. /* check for server shutdown */
  103. if (client->engine->engine_ok == 0)
  104. return req->status;
  105. /* otherwise report errors */
  106. if (!wok)
  107. jack_error ("cannot send request type %d to server",
  108. req->type);
  109. if (!rok)
  110. jack_error ("cannot read result for request type %d from"
  111. " server (%s)", req->type, strerror (errno));
  112. return req->status;
  113. }
  114. int
  115. jack_client_deliver_request (const jack_client_t *client, jack_request_t *req)
  116. {
  117. /* indirect through the function pointer that was set
  118. either by jack_client_new() (external) or handle_new_client()
  119. in the server.
  120. */
  121. return client->control->deliver_request (client->control->deliver_arg, req);
  122. }
  123. jack_client_t *
  124. jack_client_alloc ()
  125. {
  126. jack_client_t *client;
  127. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  128. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2);
  129. client->pollmax = 2;
  130. client->request_fd = -1;
  131. client->event_fd = -1;
  132. client->graph_wait_fd = -1;
  133. client->graph_next_fd = -1;
  134. client->ports = NULL;
  135. client->engine = NULL;
  136. client->control = NULL;
  137. client->thread_ok = FALSE;
  138. #if JACK_USE_MACH_THREADS
  139. client->rt_thread_ok = FALSE;
  140. #endif
  141. client->first_active = TRUE;
  142. client->on_shutdown = NULL;
  143. client->n_port_types = 0;
  144. client->port_segment = NULL;
  145. return client;
  146. }
  147. /*
  148. * Build the jack_client_t structure for an internal client.
  149. */
  150. jack_client_t *
  151. jack_client_alloc_internal (jack_client_control_t *cc, jack_engine_t* engine)
  152. {
  153. jack_client_t* client;
  154. client = jack_client_alloc ();
  155. client->control = cc;
  156. client->engine = engine->control;
  157. client->n_port_types = client->engine->n_port_types;
  158. client->port_segment = &engine->port_segment[0];
  159. return client;
  160. }
  161. static void
  162. jack_client_free (jack_client_t *client)
  163. {
  164. if (client->pollfd) {
  165. free (client->pollfd);
  166. }
  167. free (client);
  168. }
  169. void
  170. jack_client_invalidate_port_buffers (jack_client_t *client)
  171. {
  172. JSList *node;
  173. jack_port_t *port;
  174. /* This releases all local memory owned by input ports
  175. and sets the buffer pointer to NULL. This will cause
  176. jack_port_get_buffer() to reallocate space for the
  177. buffer on the next call (if there is one).
  178. */
  179. for (node = client->ports; node; node = jack_slist_next (node)) {
  180. port = (jack_port_t *) node->data;
  181. if (port->shared->flags & JackPortIsInput) {
  182. if (port->mix_buffer) {
  183. jack_pool_release (port->mix_buffer);
  184. port->mix_buffer = NULL;
  185. }
  186. }
  187. }
  188. }
  189. int
  190. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  191. {
  192. jack_port_t *control_port;
  193. jack_port_t *other;
  194. JSList *node;
  195. switch (event->type) {
  196. case PortConnected:
  197. other = jack_port_new (client, event->y.other_id, client->engine);
  198. control_port = jack_port_by_id (client, event->x.self_id);
  199. pthread_mutex_lock (&control_port->connection_lock);
  200. control_port->connections = jack_slist_prepend (control_port->connections, (void*)other);
  201. pthread_mutex_unlock (&control_port->connection_lock);
  202. break;
  203. case PortDisconnected:
  204. control_port = jack_port_by_id (client, event->x.self_id);
  205. pthread_mutex_lock (&control_port->connection_lock);
  206. for (node = control_port->connections; node; node = jack_slist_next (node)) {
  207. other = (jack_port_t *) node->data;
  208. if (other->shared->id == event->y.other_id) {
  209. control_port->connections = jack_slist_remove_link (control_port->connections, node);
  210. jack_slist_free_1 (node);
  211. free (other);
  212. break;
  213. }
  214. }
  215. pthread_mutex_unlock (&control_port->connection_lock);
  216. break;
  217. default:
  218. /* impossible */
  219. break;
  220. }
  221. return 0;
  222. }
  223. static int
  224. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  225. {
  226. char path[PATH_MAX+1];
  227. if (client->graph_wait_fd >= 0) {
  228. DEBUG ("closing graph_wait_fd==%d", client->graph_wait_fd);
  229. close (client->graph_wait_fd);
  230. client->graph_wait_fd = -1;
  231. }
  232. if (client->graph_next_fd >= 0) {
  233. DEBUG ("closing graph_next_fd==%d", client->graph_next_fd);
  234. close (client->graph_next_fd);
  235. client->graph_next_fd = -1;
  236. }
  237. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n);
  238. if ((client->graph_wait_fd = open (path, O_RDONLY|O_NONBLOCK)) < 0) {
  239. jack_error ("cannot open specified fifo [%s] for reading (%s)", path, strerror (errno));
  240. return -1;
  241. }
  242. DEBUG ("opened new graph_wait_fd %d (%s)", client->graph_wait_fd, path);
  243. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n+1);
  244. if ((client->graph_next_fd = open (path, O_WRONLY|O_NONBLOCK)) < 0) {
  245. jack_error ("cannot open specified fifo [%s] for writing (%s)", path, strerror (errno));
  246. return -1;
  247. }
  248. DEBUG ("opened new graph_next_fd %d (%s)", client->graph_next_fd, path);
  249. /* If the client registered its own callback for graph order events,
  250. execute it now.
  251. */
  252. if (client->control->graph_order) {
  253. client->control->graph_order (client->control->graph_order_arg);
  254. }
  255. return 0;
  256. }
  257. static int
  258. server_connect (int which)
  259. {
  260. int fd;
  261. struct sockaddr_un addr;
  262. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  263. jack_error ("cannot create client socket (%s)",
  264. strerror (errno));
  265. return -1;
  266. }
  267. addr.sun_family = AF_UNIX;
  268. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d",
  269. jack_server_dir, which);
  270. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  271. close (fd);
  272. return -1;
  273. }
  274. return fd;
  275. }
  276. static int
  277. server_event_connect (jack_client_t *client)
  278. {
  279. int fd;
  280. struct sockaddr_un addr;
  281. jack_client_connect_ack_request_t req;
  282. jack_client_connect_ack_result_t res;
  283. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  284. jack_error ("cannot create client event socket (%s)",
  285. strerror (errno));
  286. return -1;
  287. }
  288. addr.sun_family = AF_UNIX;
  289. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0",
  290. jack_server_dir);
  291. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  292. jack_error ("cannot connect to jack server for events",
  293. strerror (errno));
  294. close (fd);
  295. return -1;
  296. }
  297. req.client_id = client->control->id;
  298. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  299. jack_error ("cannot write event connect request to server (%s)",
  300. strerror (errno));
  301. close (fd);
  302. return -1;
  303. }
  304. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  305. jack_error ("cannot read event connect result from server (%s)",
  306. strerror (errno));
  307. close (fd);
  308. return -1;
  309. }
  310. if (res.status != 0) {
  311. jack_error ("cannot connect to server for event stream (%s)",
  312. strerror (errno));
  313. close (fd);
  314. return -1;
  315. }
  316. return fd;
  317. }
  318. /* Exec the JACK server in this process. Does not return. */
  319. static void
  320. _start_server (void)
  321. {
  322. FILE* fp = 0;
  323. char filename[255];
  324. char arguments[255];
  325. char buffer[255];
  326. char* command = 0;
  327. size_t pos = 0;
  328. size_t result = 0;
  329. char** argv = 0;
  330. int i = 0;
  331. int good = 0;
  332. int ret;
  333. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  334. fp = fopen(filename, "r");
  335. if (!fp) {
  336. fp = fopen("/etc/jackd.conf", "r");
  337. }
  338. if (fp) {
  339. arguments[0] = '\0';
  340. ret = fscanf(fp, "%s", buffer);
  341. while(ret != 0 && ret != EOF) {
  342. strcat(arguments, buffer);
  343. strcat(arguments, " ");
  344. ret = fscanf(fp, "%s", buffer);
  345. }
  346. if (strlen(arguments) > 0) {
  347. good = 1;
  348. }
  349. }
  350. if (!good) {
  351. #if defined(USE_CAPABILITIES)
  352. command = JACK_LOCATION "/jackstart";
  353. strncpy(arguments, JACK_LOCATION "/jackstart -T -R -d"
  354. JACK_DEFAULT_DRIVER " -p 512", 255);
  355. #else /* !USE_CAPABILITIES */
  356. command = JACK_LOCATION "/jackd";
  357. strncpy(arguments, JACK_LOCATION "/jackd -T -d"
  358. JACK_DEFAULT_DRIVER, 255);
  359. #endif /* USE_CAPABILITIES */
  360. } else {
  361. result = strcspn(arguments, " ");
  362. command = (char*)malloc(result+1);
  363. strncpy(command, arguments, result);
  364. command[result] = '\0';
  365. }
  366. argv = (char**)malloc(255);
  367. while(1) {
  368. result = strcspn(arguments+pos, " ");
  369. if (result == 0) {
  370. break;
  371. }
  372. argv[i] = (char*)malloc(result+1);
  373. strncpy(argv[i], arguments+pos, result);
  374. argv[i][result] = '\0';
  375. pos += result+1;
  376. ++i;
  377. }
  378. argv[i] = 0;
  379. execv (command, argv);
  380. /* If execv() succeeds, it does not return. There's no point
  381. * in calling jack_error() here in the child process. */
  382. perror ("exec of JACK server failed");
  383. }
  384. int
  385. start_server (void)
  386. {
  387. /* Only fork() a server when $JACK_START_SERVER is defined and
  388. * $JACK_NO_START_SERVER is not. */
  389. if (getenv("JACK_START_SERVER") == NULL ||
  390. getenv("JACK_NO_START_SERVER") != NULL) {
  391. return 1;
  392. }
  393. /* The double fork() forces the server to become a child of
  394. * init, which will always clean up zombie process state on
  395. * termination. This even works in strange corner cases where
  396. * the server terminates but this client does not.
  397. *
  398. * Since fork() is usually implemented using copy-on-write
  399. * virtual memory tricks, the overhead of the second fork() is
  400. * probably relatively small.
  401. */
  402. switch (fork()) {
  403. case 0: /* child process */
  404. switch (fork()) {
  405. case 0: /* grandchild process */
  406. _start_server();
  407. _exit (99); /* exec failed */
  408. case -1:
  409. _exit (98);
  410. default:
  411. _exit (0);
  412. }
  413. case -1: /* fork() error */
  414. return 1; /* failed to start server */
  415. }
  416. /* only the original parent process goes here */
  417. return 0; /* (probably) successful */
  418. }
  419. static int
  420. jack_request_client (ClientType type, const char* client_name,
  421. const char* so_name, const char* so_data,
  422. jack_client_connect_result_t *res, int *req_fd)
  423. {
  424. jack_client_connect_request_t req;
  425. *req_fd = -1;
  426. memset (&req, 0, sizeof (req));
  427. if (strlen (client_name) >= sizeof (req.name)) {
  428. jack_error ("\"%s\" is too long to be used as a JACK client"
  429. " name.\n"
  430. "Please use %lu characters or less.",
  431. client_name, sizeof (req.name));
  432. return -1;
  433. }
  434. if (strlen (so_name) > sizeof (req.object_path) - 1) {
  435. jack_error ("\"%s\" is too long to be used as a JACK shared"
  436. " object name.\n"
  437. "Please use %lu characters or less.",
  438. so_name, sizeof (req.object_path) - 1);
  439. return -1;
  440. }
  441. if (strlen (so_data) > sizeof (req.object_data) - 1) {
  442. jack_error ("\"%s\" is too long to be used as a JACK shared"
  443. " object data string.\n"
  444. "Please use %lu characters or less.",
  445. so_data, sizeof (req.object_data) - 1);
  446. return -1;
  447. }
  448. if ((*req_fd = server_connect (0)) < 0) {
  449. int trys;
  450. if (start_server()) {
  451. goto fail;
  452. }
  453. trys = 5;
  454. do {
  455. sleep(1);
  456. if (--trys < 0) {
  457. goto fail;
  458. }
  459. } while ((*req_fd = server_connect (0)) < 0);
  460. }
  461. req.load = TRUE;
  462. req.type = type;
  463. snprintf (req.name, sizeof (req.name), "%s", client_name);
  464. snprintf (req.object_path, sizeof (req.object_path), "%s", so_name);
  465. snprintf (req.object_data, sizeof (req.object_data), "%s", so_data);
  466. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  467. jack_error ("cannot send request to jack server (%s)",
  468. strerror (errno));
  469. goto fail;
  470. }
  471. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  472. if (errno == 0) {
  473. /* server shut the socket */
  474. jack_error ("could not attach as client "
  475. "(duplicate client name?)");
  476. goto fail;
  477. }
  478. jack_error ("cannot read response from jack server (%s)",
  479. strerror (errno));
  480. goto fail;
  481. }
  482. if (res->status) {
  483. jack_error ("could not attach as client "
  484. "(duplicate client name?)");
  485. goto fail;
  486. }
  487. if (res->protocol_v != jack_protocol_version){
  488. jack_error ("application linked against incompatible libjack"
  489. " version.");
  490. goto fail;
  491. }
  492. switch (type) {
  493. case ClientDriver:
  494. case ClientInternal:
  495. close (*req_fd);
  496. *req_fd = -1;
  497. break;
  498. default:
  499. break;
  500. }
  501. return 0;
  502. fail:
  503. if (*req_fd >= 0) {
  504. close (*req_fd);
  505. *req_fd = -1;
  506. }
  507. return -1;
  508. }
  509. int
  510. jack_attach_port_segment (jack_client_t *client, jack_port_type_id_t ptid)
  511. {
  512. /* Lookup, attach and register the port/buffer segments in use
  513. * right now.
  514. */
  515. if (client->control->type != ClientExternal) {
  516. jack_error("Only external clients need attach port segments");
  517. abort();
  518. }
  519. /* make sure we have space to store the port
  520. segment information.
  521. */
  522. if (ptid >= client->n_port_types) {
  523. client->port_segment = (jack_shm_info_t*)
  524. realloc (client->port_segment,
  525. sizeof (jack_shm_info_t) * (ptid+1));
  526. memset (&client->port_segment[client->n_port_types],
  527. 0,
  528. sizeof (jack_shm_info_t) *
  529. (ptid - client->n_port_types));
  530. client->n_port_types = ptid + 1;
  531. } else {
  532. /* release any previous segment */
  533. #if JACK_USE_MACH_THREADS
  534. /* Stephane Letz : letz@grame.fr
  535. Need a fix : this crash on MacOSX : temporary removed
  536. jack_release_shm (&client->port_segment[ptid]);
  537. */
  538. #else
  539. jack_release_shm (&client->port_segment[ptid]);
  540. #endif
  541. }
  542. /* get the index into the shm registry */
  543. client->port_segment[ptid].index =
  544. client->engine->port_types[ptid].shm_registry_index;
  545. /* attach the relevant segment */
  546. if (jack_attach_shm (&client->port_segment[ptid])) {
  547. jack_error ("cannot attach port segment shared memory"
  548. " (%s)", strerror (errno));
  549. return -1;
  550. }
  551. /* The first chunk of the audio port segment will be set by
  552. * the engine to be a zero-filled buffer. This hasn't been
  553. * done yet, but it will happen before the process cycle
  554. * (re)starts.
  555. */
  556. if (ptid == JACK_AUDIO_PORT_TYPE) {
  557. jack_zero_filled_buffer =
  558. jack_shm_addr (&client->port_segment[ptid]);
  559. }
  560. return 0;
  561. }
  562. jack_client_t *
  563. jack_client_new (const char *client_name)
  564. {
  565. int req_fd = -1;
  566. int ev_fd = -1;
  567. jack_client_connect_result_t res;
  568. jack_client_t *client;
  569. jack_port_type_id_t ptid;
  570. /* external clients need this initialized; internal clients
  571. will use the setup in the server's address space.
  572. */
  573. jack_init_time ();
  574. if (jack_initialize_shm ()) {
  575. jack_error ("Unable to initialize shared memory.");
  576. return NULL;
  577. }
  578. if (jack_request_client (ClientExternal, client_name, "", "",
  579. &res, &req_fd)) {
  580. return NULL;
  581. }
  582. client = jack_client_alloc ();
  583. strcpy (client->fifo_prefix, res.fifo_prefix);
  584. client->request_fd = req_fd;
  585. client->pollfd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  586. client->pollfd[1].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  587. /* attach the engine control/info block */
  588. client->engine_shm = res.engine_shm;
  589. if (jack_attach_shm (&client->engine_shm)) {
  590. jack_error ("cannot attached engine control shared memory"
  591. " segment");
  592. goto fail;
  593. }
  594. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  595. /* now attach the client control block */
  596. client->control_shm = res.client_shm;
  597. if (jack_attach_shm (&client->control_shm)) {
  598. jack_error ("cannot attached client control shared memory"
  599. " segment");
  600. goto fail;
  601. }
  602. client->control = (jack_client_control_t *)
  603. jack_shm_addr (&client->control_shm);
  604. /* nobody else needs to access this shared memory any more, so
  605. destroy it. because we have our own attachment to it, it won't
  606. vanish till we exit (and release it).
  607. */
  608. jack_destroy_shm (&client->control_shm);
  609. client->n_port_types = client->engine->n_port_types;
  610. client->port_segment = (jack_shm_info_t *)
  611. malloc (sizeof (jack_shm_info_t) * client->n_port_types);
  612. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  613. client->port_segment[ptid].index =
  614. client->engine->port_types[ptid].shm_registry_index;
  615. jack_attach_port_segment (client, ptid);
  616. }
  617. /* set up the client so that it does the right thing for an
  618. * external client
  619. */
  620. client->control->deliver_request = oop_client_deliver_request;
  621. client->control->deliver_arg = client;
  622. if ((ev_fd = server_event_connect (client)) < 0) {
  623. goto fail;
  624. }
  625. client->event_fd = ev_fd;
  626. #ifdef JACK_USE_MACH_THREADS
  627. /* specific resources for server/client real-time thread
  628. * communication */
  629. client->clienttask = mach_task_self();
  630. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  631. jack_error ("Can't find bootstrap port");
  632. goto fail;
  633. }
  634. if (allocate_mach_clientport(client, res.portnum) < 0) {
  635. jack_error("Can't allocate mach port");
  636. goto fail;
  637. };
  638. #endif /* JACK_USE_MACH_THREADS */
  639. return client;
  640. fail:
  641. if (client->engine) {
  642. jack_release_shm (&client->engine_shm);
  643. client->engine = 0;
  644. }
  645. if (client->control) {
  646. jack_release_shm (&client->control_shm);
  647. client->control = 0;
  648. }
  649. if (req_fd >= 0) {
  650. close (req_fd);
  651. }
  652. if (ev_fd >= 0) {
  653. close (ev_fd);
  654. }
  655. return 0;
  656. }
  657. int
  658. jack_internal_client_new (const char *client_name, const char *so_name, const char *so_data)
  659. {
  660. jack_client_connect_result_t res;
  661. int req_fd;
  662. return jack_request_client (ClientInternal, client_name, so_name, so_data, &res, &req_fd);
  663. }
  664. void
  665. jack_internal_client_close (const char *client_name)
  666. {
  667. jack_client_connect_request_t req;
  668. int fd;
  669. req.load = FALSE;
  670. snprintf (req.name, sizeof (req.name), "%s", client_name);
  671. if ((fd = server_connect (0)) < 0) {
  672. return;
  673. }
  674. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  675. jack_error ("cannot deliver ClientUnload request to JACK server.");
  676. }
  677. /* no response to this request */
  678. close (fd);
  679. return;
  680. }
  681. #if JACK_USE_MACH_THREADS
  682. int
  683. jack_drop_real_time_scheduling (pthread_t thread)
  684. {
  685. setThreadToPriority(thread, 31, false, 10000000);
  686. return 0;
  687. }
  688. int
  689. jack_acquire_real_time_scheduling (pthread_t thread, int priority) //priority is unused
  690. {
  691. setThreadToPriority(thread, 96, true, 10000000);
  692. return 0;
  693. }
  694. #else
  695. int
  696. jack_drop_real_time_scheduling (pthread_t thread)
  697. {
  698. struct sched_param rtparam;
  699. int x;
  700. memset (&rtparam, 0, sizeof (rtparam));
  701. rtparam.sched_priority = 0;
  702. if ((x = pthread_setschedparam (thread, SCHED_OTHER, &rtparam)) != 0) {
  703. jack_error ("cannot switch to normal scheduling priority(%s)\n", strerror (errno));
  704. return -1;
  705. }
  706. return 0;
  707. }
  708. int
  709. jack_acquire_real_time_scheduling (pthread_t thread, int priority)
  710. {
  711. struct sched_param rtparam;
  712. int x;
  713. memset (&rtparam, 0, sizeof (rtparam));
  714. rtparam.sched_priority = priority;
  715. if ((x = pthread_setschedparam (thread, SCHED_FIFO, &rtparam)) != 0) {
  716. jack_error ("cannot use real-time scheduling (FIFO/%d) "
  717. "(%d: %s)", rtparam.sched_priority, x,
  718. strerror (x));
  719. return -1;
  720. }
  721. return 0;
  722. }
  723. #endif
  724. int
  725. jack_set_freewheel (jack_client_t* client, int onoff)
  726. {
  727. jack_request_t request;
  728. request.type = onoff ? FreeWheel : StopFreeWheel;
  729. return jack_client_deliver_request (client, &request);
  730. }
  731. void
  732. jack_start_freewheel (jack_client_t* client)
  733. {
  734. jack_client_control_t *control = client->control;
  735. if (client->engine->real_time) {
  736. #if JACK_USE_MACH_THREADS
  737. jack_drop_real_time_scheduling (client->process_thread);
  738. #else
  739. jack_drop_real_time_scheduling (client->thread);
  740. #endif
  741. }
  742. if (control->freewheel_cb) {
  743. control->freewheel_cb (1, control->freewheel_arg);
  744. }
  745. }
  746. void
  747. jack_stop_freewheel (jack_client_t* client)
  748. {
  749. jack_client_control_t *control = client->control;
  750. if (control->freewheel_cb) {
  751. control->freewheel_cb (0, control->freewheel_arg);
  752. }
  753. if (client->engine->real_time) {
  754. #if JACK_USE_MACH_THREADS
  755. jack_acquire_real_time_scheduling (client->process_thread,
  756. client->engine->client_priority);
  757. #else
  758. jack_acquire_real_time_scheduling (client->thread,
  759. client->engine->client_priority);
  760. #endif
  761. }
  762. }
  763. static void *
  764. jack_client_thread (void *arg)
  765. {
  766. jack_client_t *client = (jack_client_t *) arg;
  767. jack_client_control_t *control = client->control;
  768. jack_event_t event;
  769. char status = 0;
  770. char c;
  771. int err = 0;
  772. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  773. pthread_mutex_lock (&client_lock);
  774. client->thread_ok = TRUE;
  775. client->thread_id = pthread_self();
  776. pthread_cond_signal (&client_ready);
  777. pthread_mutex_unlock (&client_lock);
  778. client->control->pid = getpid();
  779. client->control->pgrp = getpgrp();
  780. DEBUG ("client thread is now running");
  781. if (client->control->thread_init) {
  782. DEBUG ("calling client thread init callback");
  783. client->control->thread_init (client->control->thread_init_arg);
  784. }
  785. while (err == 0) {
  786. if (client->engine->engine_ok == 0) {
  787. if (client->on_shutdown)
  788. client->on_shutdown (client->on_shutdown_arg);
  789. else
  790. jack_error ("engine unexpectedly shutdown; "
  791. "thread exiting\n");
  792. pthread_exit (0);
  793. }
  794. DEBUG ("client polling on event_fd and graph_wait_fd...");
  795. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  796. if (errno == EINTR) {
  797. continue;
  798. }
  799. jack_error ("poll failed in client (%s)",
  800. strerror (errno));
  801. status = -1;
  802. break;
  803. }
  804. /* get an accurate timestamp on waking from poll for a
  805. * process() cycle.
  806. */
  807. if (client->pollfd[1].revents & POLLIN) {
  808. control->awake_at = jack_get_microseconds();
  809. }
  810. DEBUG ("pfd[0].revents = 0x%x pfd[1].revents = 0x%x",
  811. client->pollfd[0].revents,
  812. client->pollfd[1].revents);
  813. pthread_testcancel();
  814. if ((client->pollfd[0].revents & ~POLLIN) ||
  815. client->control->dead) {
  816. goto zombie;
  817. }
  818. if (client->pollfd[0].revents & POLLIN) {
  819. DEBUG ("client receives an event, "
  820. "now reading on event fd");
  821. /* server has sent us an event. process the
  822. * event and reply */
  823. if (read (client->event_fd, &event, sizeof (event))
  824. != sizeof (event)) {
  825. jack_error ("cannot read server event (%s)",
  826. strerror (errno));
  827. err++;
  828. break;
  829. }
  830. status = 0;
  831. switch (event.type) {
  832. case PortRegistered:
  833. if (control->port_register) {
  834. control->port_register
  835. (event.x.port_id, TRUE,
  836. control->port_register_arg);
  837. }
  838. break;
  839. case PortUnregistered:
  840. if (control->port_register) {
  841. control->port_register
  842. (event.x.port_id, FALSE,
  843. control->port_register_arg);
  844. }
  845. break;
  846. case GraphReordered:
  847. status = jack_handle_reorder (client, &event);
  848. break;
  849. case PortConnected:
  850. case PortDisconnected:
  851. status = jack_client_handle_port_connection
  852. (client, &event);
  853. break;
  854. case BufferSizeChange:
  855. jack_client_invalidate_port_buffers (client);
  856. if (control->bufsize) {
  857. status = control->bufsize
  858. (control->nframes,
  859. control->bufsize_arg);
  860. }
  861. break;
  862. case SampleRateChange:
  863. if (control->srate) {
  864. status = control->srate
  865. (control->nframes,
  866. control->srate_arg);
  867. }
  868. break;
  869. case XRun:
  870. if (control->xrun) {
  871. status = control->xrun
  872. (control->xrun_arg);
  873. }
  874. break;
  875. case AttachPortSegment:
  876. jack_attach_port_segment (client, event.y.ptid);
  877. break;
  878. case StartFreewheel:
  879. jack_start_freewheel (client);
  880. break;
  881. case StopFreewheel:
  882. jack_stop_freewheel (client);
  883. break;
  884. }
  885. DEBUG ("client has dealt with the event, writing "
  886. "response on event fd");
  887. if (write (client->event_fd, &status, sizeof (status))
  888. != sizeof (status)) {
  889. jack_error ("cannot send event response to "
  890. "engine (%s)", strerror (errno));
  891. err++;
  892. break;
  893. }
  894. }
  895. if (client->pollfd[1].revents & ~POLLIN) {
  896. goto zombie;
  897. }
  898. if (client->pollfd[1].revents & POLLIN) {
  899. #ifdef WITH_TIMESTAMPS
  900. jack_reset_timestamps ();
  901. #endif
  902. DEBUG ("client %d signalled at %" PRIu64
  903. ", awake for process at %" PRIu64
  904. " (delay = %" PRIu64
  905. " usecs) (wakeup on graph_wait_fd==%d)",
  906. getpid(),
  907. control->signalled_at,
  908. control->awake_at,
  909. control->awake_at - control->signalled_at,
  910. client->pollfd[1].fd);
  911. control->state = Running;
  912. if (control->sync_cb)
  913. jack_call_sync_client (client);
  914. if (control->process) {
  915. if (control->process (control->nframes,
  916. control->process_arg)
  917. == 0) {
  918. control->state = Finished;
  919. }
  920. } else {
  921. control->state = Finished;
  922. }
  923. if (control->timebase_cb)
  924. jack_call_timebase_master (client);
  925. control->finished_at = jack_get_microseconds();
  926. #ifdef WITH_TIMESTAMPS
  927. jack_timestamp ("finished");
  928. #endif
  929. /* pass the execution token along */
  930. DEBUG ("client finished processing at %" PRIu64
  931. " (elapsed = %" PRIu64
  932. " usecs), writing on graph_next_fd==%d",
  933. control->finished_at,
  934. control->finished_at - control->awake_at,
  935. client->graph_next_fd);
  936. if (write (client->graph_next_fd, &c, sizeof (c))
  937. != sizeof (c)) {
  938. jack_error ("cannot continue execution of the "
  939. "processing graph (%s)",
  940. strerror(errno));
  941. err++;
  942. break;
  943. }
  944. DEBUG ("client sent message to next stage by %" PRIu64
  945. ", client reading on graph_wait_fd==%d",
  946. jack_get_microseconds(), client->graph_wait_fd);
  947. #ifdef WITH_TIMESTAMPS
  948. jack_timestamp ("read pending byte from wait");
  949. #endif
  950. DEBUG("reading cleanup byte from pipe\n");
  951. if ((read (client->graph_wait_fd, &c, sizeof (c))
  952. != sizeof (c))) {
  953. DEBUG ("WARNING: READ FAILED!");
  954. #if 0
  955. jack_error ("cannot complete execution of the "
  956. "processing graph (%s)",
  957. strerror(errno));
  958. err++;
  959. break;
  960. #endif
  961. }
  962. /* check if we were killed during the process
  963. * cycle (or whatever) */
  964. if (client->control->dead) {
  965. goto zombie;
  966. }
  967. DEBUG("process cycle fully complete\n");
  968. #ifdef WITH_TIMESTAMPS
  969. jack_timestamp ("read done");
  970. jack_dump_timestamps (stdout);
  971. #endif
  972. }
  973. }
  974. return (void *) ((intptr_t)err);
  975. zombie:
  976. if (client->on_shutdown) {
  977. jack_error ("zombified - calling shutdown handler");
  978. client->on_shutdown (client->on_shutdown_arg);
  979. } else {
  980. jack_error ("zombified - exiting from JACK");
  981. jack_client_close (client);
  982. /* Need a fix : possibly make client crash if
  983. * zombified without shutdown handler
  984. */
  985. }
  986. pthread_exit (0);
  987. /*NOTREACHED*/
  988. return 0;
  989. }
  990. #ifdef JACK_USE_MACH_THREADS
  991. /* real-time thread : separated from the normal client thread, it will
  992. * communicate with the server using fast mach RPC mechanism */
  993. static void *
  994. jack_client_process_thread (void *arg)
  995. {
  996. jack_client_t *client = (jack_client_t *) arg;
  997. jack_client_control_t *control = client->control;
  998. int err = 0;
  999. client->control->pid = getpid();
  1000. DEBUG ("client process thread is now running");
  1001. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  1002. client->rt_thread_ok = TRUE;
  1003. while (err == 0) {
  1004. if (jack_client_suspend(client) < 0) {
  1005. jack_error ("jack_client_process_thread : resume error");
  1006. goto zombie;
  1007. }
  1008. control->awake_at = jack_get_microseconds();
  1009. DEBUG ("client resumed");
  1010. control->state = Running;
  1011. if (control->sync_cb)
  1012. jack_call_sync_client (client);
  1013. if (control->process) {
  1014. if (control->process (control->nframes,
  1015. control->process_arg) == 0) {
  1016. control->state = Finished;
  1017. }
  1018. } else {
  1019. control->state = Finished;
  1020. }
  1021. if (control->timebase_cb)
  1022. jack_call_timebase_master (client);
  1023. control->finished_at = jack_get_microseconds();
  1024. #ifdef WITH_TIMESTAMPS
  1025. jack_timestamp ("finished");
  1026. #endif
  1027. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  1028. control->finished_at, ((float)(control->finished_at - control->awake_at)));
  1029. /* check if we were killed during the process cycle (or whatever) */
  1030. if (client->control->dead) {
  1031. jack_error ("jack_client_process_thread : client->control->dead");
  1032. goto zombie;
  1033. }
  1034. DEBUG("process cycle fully complete\n");
  1035. }
  1036. return (void *) ((intptr_t)err);
  1037. zombie:
  1038. jack_error ("jack_client_process_thread : zombified");
  1039. client->rt_thread_ok = FALSE;
  1040. if (client->on_shutdown) {
  1041. jack_error ("zombified - calling shutdown handler");
  1042. client->on_shutdown (client->on_shutdown_arg);
  1043. } else {
  1044. jack_error ("zombified - exiting from JACK");
  1045. jack_client_close (client); /* Need a fix : possibly make client crash if zombified without shutdown handler */
  1046. }
  1047. pthread_exit (0);
  1048. /*NOTREACHED*/
  1049. return 0;
  1050. }
  1051. #endif /* JACK_USE_MACH_THREADS */
  1052. static int
  1053. jack_start_thread (jack_client_t *client)
  1054. {
  1055. #ifndef JACK_USE_MACH_THREADS
  1056. int policy = SCHED_OTHER;
  1057. struct sched_param client_param, temp_param;
  1058. #endif
  1059. pthread_attr_t *attributes = 0;
  1060. if (client->engine->real_time) {
  1061. /* Get the client thread to run as an RT-FIFO
  1062. scheduled thread of appropriate priority.
  1063. */
  1064. struct sched_param rt_param;
  1065. attributes = (pthread_attr_t *)
  1066. malloc (sizeof (pthread_attr_t));
  1067. pthread_attr_init (attributes);
  1068. if (pthread_attr_setschedpolicy (attributes, SCHED_FIFO)) {
  1069. jack_error ("cannot set FIFO scheduling class for RT "
  1070. "thread");
  1071. return -1;
  1072. }
  1073. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  1074. jack_error ("Cannot set scheduling scope for RT "
  1075. "thread");
  1076. return -1;
  1077. }
  1078. memset (&rt_param, 0, sizeof (rt_param));
  1079. rt_param.sched_priority = client->engine->client_priority;
  1080. if (pthread_attr_setschedparam (attributes, &rt_param)) {
  1081. jack_error ("Cannot set scheduling priority for RT "
  1082. "thread (%s)", strerror (errno));
  1083. return -1;
  1084. }
  1085. #ifdef USE_MLOCK
  1086. if (client->engine->do_mlock
  1087. && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1088. jack_error ("cannot lock down memory for RT thread (%s)",
  1089. strerror (errno));
  1090. #ifdef ENSURE_MLOCK
  1091. return -1;
  1092. #endif /* ENSURE_MLOCK */
  1093. }
  1094. #endif /* USE_MLOCK */
  1095. }
  1096. #ifdef JACK_USE_MACH_THREADS
  1097. if (pthread_create (&client->thread, attributes,
  1098. jack_client_thread, client)) {
  1099. return -1;
  1100. }
  1101. /* a special real-time thread to call the "process"
  1102. * callback. It will communicate with the server using fast
  1103. * mach RPC mechanism */
  1104. if (pthread_create (&client->process_thread, attributes,
  1105. jack_client_process_thread, client)) {
  1106. jack_error("pthread_create failed for process_thread \n");
  1107. return -1;
  1108. }
  1109. if (client->engine->real_time){
  1110. /* time constraint thread */
  1111. setThreadToPriority(client->process_thread, 96, TRUE, 10000000);
  1112. }else{
  1113. /* fixed priority thread */
  1114. setThreadToPriority(client->process_thread, 63, TRUE, 10000000);
  1115. }
  1116. #else /* !JACK_USE_MACH_THREADS */
  1117. if (pthread_create (&client->thread, attributes,
  1118. jack_client_thread, client) == 0) {
  1119. return 0;
  1120. }
  1121. if (!client->engine->real_time) {
  1122. return -1;
  1123. }
  1124. /* the version of glibc I've played with has a bug that makes
  1125. that code fail when running under a non-root user but with the
  1126. proper realtime capabilities (in short, pthread_attr_setschedpolicy
  1127. does not check for capabilities, only for the uid being
  1128. zero). Newer versions apparently have this fixed. This
  1129. workaround temporarily switches the client thread to the
  1130. proper scheduler and priority, then starts the realtime
  1131. thread so that it can inherit them and finally switches the
  1132. client thread back to what it was before. Sigh. For ardour
  1133. I have to check again and switch the thread explicitly to
  1134. realtime, don't know why or how to debug - nando
  1135. */
  1136. /* get current scheduler and parameters of the client process */
  1137. if ((policy = sched_getscheduler (0)) < 0) {
  1138. jack_error ("Cannot get current client scheduler: %s",
  1139. strerror(errno));
  1140. return -1;
  1141. }
  1142. memset (&client_param, 0, sizeof (client_param));
  1143. if (sched_getparam (0, &client_param)) {
  1144. jack_error ("Cannot get current client scheduler "
  1145. "parameters: %s", strerror(errno));
  1146. return -1;
  1147. }
  1148. /* temporarily change the client process to SCHED_FIFO so that
  1149. the realtime thread can inherit the scheduler and priority
  1150. */
  1151. memset (&temp_param, 0, sizeof (temp_param));
  1152. temp_param.sched_priority = client->engine->client_priority;
  1153. if (sched_setscheduler(0, SCHED_FIFO, &temp_param)) {
  1154. jack_error ("Cannot temporarily set client to RT scheduler:"
  1155. " %s", strerror(errno));
  1156. return -1;
  1157. }
  1158. /* prepare the attributes for the realtime thread */
  1159. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  1160. pthread_attr_init (attributes);
  1161. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  1162. sched_setscheduler (0, policy, &client_param);
  1163. jack_error ("Cannot set scheduling scope for RT thread");
  1164. return -1;
  1165. }
  1166. if (pthread_attr_setinheritsched (attributes, PTHREAD_INHERIT_SCHED)) {
  1167. sched_setscheduler (0, policy, &client_param);
  1168. jack_error ("Cannot set scheduler inherit policy for RT "
  1169. "thread");
  1170. return -1;
  1171. }
  1172. /* create the RT thread */
  1173. if (pthread_create (&client->thread, attributes,
  1174. jack_client_thread, client)) {
  1175. sched_setscheduler (0, policy, &client_param);
  1176. return -1;
  1177. }
  1178. /* return the client process to the scheduler it was in before */
  1179. if (sched_setscheduler (0, policy, &client_param)) {
  1180. jack_error ("Cannot reset original client scheduler: %s",
  1181. strerror(errno));
  1182. return -1;
  1183. }
  1184. /* check again... inheritance of policy and priority works in
  1185. jack_simple_client but not in ardour! So I check again and
  1186. force the policy if it is not set correctly. This does not
  1187. really really work either, the manager thread of the
  1188. linuxthreads implementation is left running with
  1189. SCHED_OTHER, that is presumably very bad.
  1190. */
  1191. memset (&client_param, 0, sizeof (client_param));
  1192. if (pthread_getschedparam(client->thread, &policy,
  1193. &client_param) == 0) {
  1194. if (policy != SCHED_FIFO) {
  1195. memset (&client_param, 0, sizeof (client_param));
  1196. client_param.sched_priority =
  1197. client->engine->client_priority;
  1198. if (pthread_setschedparam (client->thread, SCHED_FIFO,
  1199. &client_param)) {
  1200. jack_error ("Cannot set (again) FIFO scheduling"
  1201. " class for RT thread\n");
  1202. return -1;
  1203. }
  1204. }
  1205. }
  1206. #endif /* JACK_USE_MACH_THREADS */
  1207. return 0;
  1208. }
  1209. int
  1210. jack_activate (jack_client_t *client)
  1211. {
  1212. jack_request_t req;
  1213. /* we need to scribble on our stack to ensure that its memory
  1214. * pages are actually mapped (more important for mlockall(2)
  1215. * usage in jack_start_thread())
  1216. */
  1217. char buf[JACK_THREAD_STACK_TOUCH];
  1218. int i;
  1219. for (i = 0; i < JACK_THREAD_STACK_TOUCH; i++) {
  1220. buf[i] = (char) (i & 0xff);
  1221. }
  1222. if (client->control->type == ClientInternal ||
  1223. client->control->type == ClientDriver) {
  1224. goto startit;
  1225. }
  1226. /* get the pid of the client process to pass it to engine */
  1227. client->control->pid = getpid ();
  1228. #ifdef USE_CAPABILITIES
  1229. if (client->engine->has_capabilities != 0 &&
  1230. client->control->pid != 0 && client->engine->real_time != 0) {
  1231. /* we need to ask the engine for realtime capabilities
  1232. before trying to start the realtime thread
  1233. */
  1234. req.type = SetClientCapabilities;
  1235. req.x.client_id = client->control->id;
  1236. jack_client_deliver_request (client, &req);
  1237. if (req.status) {
  1238. /* what to do? engine is running realtime, it
  1239. is using capabilities and has them
  1240. (otherwise we would not get an error
  1241. return) but for some reason it could not
  1242. give the client the required capabilities,
  1243. so for now downgrade the client so that it
  1244. still runs, albeit non-realtime - nando
  1245. */
  1246. jack_error ("could not receive realtime capabilities, "
  1247. "client will run non-realtime");
  1248. /* XXX wrong, this is a property of the engine
  1249. client->engine->real_time = 0;
  1250. */
  1251. }
  1252. }
  1253. #endif /* USE_CAPABILITIES */
  1254. if (client->first_active) {
  1255. pthread_mutex_init (&client_lock, NULL);
  1256. pthread_cond_init (&client_ready, NULL);
  1257. pthread_mutex_lock (&client_lock);
  1258. if (jack_start_thread (client)) {
  1259. pthread_mutex_unlock (&client_lock);
  1260. return -1;
  1261. }
  1262. pthread_cond_wait (&client_ready, &client_lock);
  1263. pthread_mutex_unlock (&client_lock);
  1264. if (!client->thread_ok) {
  1265. jack_error ("could not start client thread");
  1266. return -1;
  1267. }
  1268. client->first_active = FALSE;
  1269. }
  1270. startit:
  1271. req.type = ActivateClient;
  1272. req.x.client_id = client->control->id;
  1273. return jack_client_deliver_request (client, &req);
  1274. }
  1275. int
  1276. jack_deactivate (jack_client_t *client)
  1277. {
  1278. jack_request_t req;
  1279. req.type = DeactivateClient;
  1280. req.x.client_id = client->control->id;
  1281. return jack_client_deliver_request (client, &req);
  1282. }
  1283. int
  1284. jack_client_close (jack_client_t *client)
  1285. {
  1286. JSList *node;
  1287. void *status;
  1288. if (client->control->active) {
  1289. jack_deactivate (client);
  1290. }
  1291. if (client->control->type == ClientExternal) {
  1292. #if JACK_USE_MACH_THREADS
  1293. if (client->rt_thread_ok) {
  1294. // MacOSX pthread_cancel not implemented in
  1295. // Darwin 5.5, 6.4
  1296. mach_port_t machThread =
  1297. pthread_mach_thread_np (client->process_thread);
  1298. thread_terminate (machThread);
  1299. }
  1300. #endif
  1301. /* stop the thread that communicates with the jack
  1302. * server, only if it was actually running
  1303. */
  1304. if (client->thread_ok){
  1305. pthread_cancel (client->thread);
  1306. pthread_join (client->thread, &status);
  1307. }
  1308. if (client->control) {
  1309. jack_release_shm (&client->control_shm);
  1310. client->control = NULL;
  1311. }
  1312. if (client->engine) {
  1313. jack_release_shm (&client->engine_shm);
  1314. client->engine = NULL;
  1315. }
  1316. if (client->port_segment) {
  1317. jack_port_type_id_t ptid;
  1318. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1319. jack_release_shm (&client->port_segment[ptid]);
  1320. }
  1321. free (client->port_segment);
  1322. client->port_segment = NULL;
  1323. }
  1324. if (client->graph_wait_fd) {
  1325. close (client->graph_wait_fd);
  1326. }
  1327. if (client->graph_next_fd) {
  1328. close (client->graph_next_fd);
  1329. }
  1330. close (client->event_fd);
  1331. close (client->request_fd);
  1332. }
  1333. for (node = client->ports; node; node = jack_slist_next (node)) {
  1334. free (node->data);
  1335. }
  1336. jack_slist_free (client->ports);
  1337. jack_client_free (client);
  1338. return 0;
  1339. }
  1340. int
  1341. jack_is_realtime (jack_client_t *client)
  1342. {
  1343. return client->engine->real_time;
  1344. }
  1345. jack_nframes_t
  1346. jack_get_buffer_size (jack_client_t *client)
  1347. {
  1348. return client->engine->buffer_size;
  1349. }
  1350. int
  1351. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1352. {
  1353. #ifdef DO_BUFFER_RESIZE
  1354. jack_request_t req;
  1355. req.type = SetBufferSize;
  1356. req.x.nframes = nframes;
  1357. return jack_client_deliver_request (client, &req);
  1358. #else
  1359. return ENOSYS;
  1360. #endif /* DO_BUFFER_RESIZE */
  1361. }
  1362. int
  1363. jack_connect (jack_client_t *client, const char *source_port,
  1364. const char *destination_port)
  1365. {
  1366. jack_request_t req;
  1367. req.type = ConnectPorts;
  1368. snprintf (req.x.connect.source_port,
  1369. sizeof (req.x.connect.source_port), "%s", source_port);
  1370. snprintf (req.x.connect.destination_port,
  1371. sizeof (req.x.connect.destination_port),
  1372. "%s", destination_port);
  1373. return jack_client_deliver_request (client, &req);
  1374. }
  1375. int
  1376. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1377. {
  1378. jack_request_t req;
  1379. pthread_mutex_lock (&port->connection_lock);
  1380. if (port->connections == NULL) {
  1381. pthread_mutex_unlock (&port->connection_lock);
  1382. return 0;
  1383. }
  1384. pthread_mutex_unlock (&port->connection_lock);
  1385. req.type = DisconnectPort;
  1386. req.x.port_info.port_id = port->shared->id;
  1387. return jack_client_deliver_request (client, &req);
  1388. }
  1389. int
  1390. jack_disconnect (jack_client_t *client, const char *source_port,
  1391. const char *destination_port)
  1392. {
  1393. jack_request_t req;
  1394. req.type = DisconnectPorts;
  1395. snprintf (req.x.connect.source_port,
  1396. sizeof (req.x.connect.source_port), "%s", source_port);
  1397. snprintf (req.x.connect.destination_port,
  1398. sizeof (req.x.connect.destination_port),
  1399. "%s", destination_port);
  1400. return jack_client_deliver_request (client, &req);
  1401. }
  1402. void
  1403. jack_set_error_function (void (*func) (const char *))
  1404. {
  1405. jack_error_callback = func;
  1406. }
  1407. int
  1408. jack_set_graph_order_callback (jack_client_t *client,
  1409. JackGraphOrderCallback callback, void *arg)
  1410. {
  1411. if (client->control->active) {
  1412. jack_error ("You cannot set callbacks on an active client.");
  1413. return -1;
  1414. }
  1415. client->control->graph_order = callback;
  1416. client->control->graph_order_arg = arg;
  1417. return 0;
  1418. }
  1419. int jack_set_xrun_callback (jack_client_t *client,
  1420. JackXRunCallback callback, void *arg)
  1421. {
  1422. if (client->control->active) {
  1423. jack_error ("You cannot set callbacks on an active client.");
  1424. return -1;
  1425. }
  1426. client->control->xrun = callback;
  1427. client->control->xrun_arg = arg;
  1428. return 0;
  1429. }
  1430. int
  1431. jack_set_process_callback (jack_client_t *client,
  1432. JackProcessCallback callback, void *arg)
  1433. {
  1434. if (client->control->active) {
  1435. jack_error ("You cannot set callbacks on an active client.");
  1436. return -1;
  1437. }
  1438. client->control->process_arg = arg;
  1439. client->control->process = callback;
  1440. return 0;
  1441. }
  1442. int
  1443. jack_set_thread_init_callback (jack_client_t *client,
  1444. JackThreadInitCallback callback, void *arg)
  1445. {
  1446. if (client->control->active) {
  1447. jack_error ("You cannot set callbacks on an active client.");
  1448. return -1;
  1449. }
  1450. client->control->thread_init_arg = arg;
  1451. client->control->thread_init = callback;
  1452. return 0;
  1453. }
  1454. int
  1455. jack_set_freewheel_callback (jack_client_t *client,
  1456. JackFreewheelCallback callback, void *arg)
  1457. {
  1458. if (client->control->active) {
  1459. jack_error ("You cannot set callbacks on an active client.");
  1460. return -1;
  1461. }
  1462. client->control->freewheel_arg = arg;
  1463. client->control->freewheel_cb = callback;
  1464. return 0;
  1465. }
  1466. int
  1467. jack_set_buffer_size_callback (jack_client_t *client,
  1468. JackBufferSizeCallback callback, void *arg)
  1469. {
  1470. client->control->bufsize_arg = arg;
  1471. client->control->bufsize = callback;
  1472. return 0;
  1473. }
  1474. int
  1475. jack_set_port_registration_callback(jack_client_t *client,
  1476. JackPortRegistrationCallback callback,
  1477. void *arg)
  1478. {
  1479. if (client->control->active) {
  1480. jack_error ("You cannot set callbacks on an active client.");
  1481. return -1;
  1482. }
  1483. client->control->port_register_arg = arg;
  1484. client->control->port_register = callback;
  1485. return 0;
  1486. }
  1487. int
  1488. jack_get_process_start_fd (jack_client_t *client)
  1489. {
  1490. /* once this has been called, the client thread
  1491. does not sleep on the graph wait fd.
  1492. */
  1493. client->pollmax = 1;
  1494. return client->graph_wait_fd;
  1495. }
  1496. int
  1497. jack_get_process_done_fd (jack_client_t *client)
  1498. {
  1499. return client->graph_next_fd;
  1500. }
  1501. void
  1502. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1503. {
  1504. client->on_shutdown = function;
  1505. client->on_shutdown_arg = arg;
  1506. }
  1507. const char **
  1508. jack_get_ports (jack_client_t *client,
  1509. const char *port_name_pattern,
  1510. const char *type_name_pattern,
  1511. unsigned long flags)
  1512. {
  1513. jack_control_t *engine;
  1514. const char **matching_ports;
  1515. unsigned long match_cnt;
  1516. jack_port_shared_t *psp;
  1517. unsigned long i;
  1518. regex_t port_regex;
  1519. regex_t type_regex;
  1520. int matching;
  1521. engine = client->engine;
  1522. if (port_name_pattern && port_name_pattern[0]) {
  1523. regcomp (&port_regex, port_name_pattern,
  1524. REG_EXTENDED|REG_NOSUB);
  1525. }
  1526. if (type_name_pattern && type_name_pattern[0]) {
  1527. regcomp (&type_regex, type_name_pattern,
  1528. REG_EXTENDED|REG_NOSUB);
  1529. }
  1530. psp = engine->ports;
  1531. match_cnt = 0;
  1532. matching_ports = (const char **)
  1533. malloc (sizeof (char *) * engine->port_max);
  1534. for (i = 0; i < engine->port_max; i++) {
  1535. matching = 1;
  1536. if (!psp[i].in_use) {
  1537. continue;
  1538. }
  1539. if (flags) {
  1540. if ((psp[i].flags & flags) != flags) {
  1541. matching = 0;
  1542. }
  1543. }
  1544. if (matching && port_name_pattern && port_name_pattern[0]) {
  1545. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1546. matching = 0;
  1547. }
  1548. }
  1549. if (matching && type_name_pattern && type_name_pattern[0]) {
  1550. jack_port_type_id_t ptid = psp[i].ptype_id;
  1551. if (regexec (&type_regex,
  1552. engine->port_types[ptid].type_name,
  1553. 0, NULL, 0)) {
  1554. matching = 0;
  1555. }
  1556. }
  1557. if (matching) {
  1558. matching_ports[match_cnt++] = psp[i].name;
  1559. }
  1560. }
  1561. matching_ports[match_cnt] = 0;
  1562. if (match_cnt == 0) {
  1563. free (matching_ports);
  1564. matching_ports = 0;
  1565. }
  1566. return matching_ports;
  1567. }
  1568. float
  1569. jack_cpu_load (jack_client_t *client)
  1570. {
  1571. return client->engine->cpu_load;
  1572. }
  1573. pthread_t
  1574. jack_client_thread_id (jack_client_t *client)
  1575. {
  1576. return client->thread_id;
  1577. }
  1578. int
  1579. jack_client_name_size(void)
  1580. {
  1581. return JACK_CLIENT_NAME_SIZE;
  1582. }
  1583. int
  1584. jack_port_name_size(void)
  1585. {
  1586. return JACK_PORT_NAME_SIZE;
  1587. }
  1588. int
  1589. jack_port_type_size(void)
  1590. {
  1591. return JACK_PORT_TYPE_SIZE;
  1592. }