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.

1986 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. /* insert -T into arguments */
  369. if (i == 1) {
  370. argv[i] = (char*)malloc(3);
  371. strncpy(argv[i], "-T", 2);
  372. argv[i][2] = '\0';
  373. ++i;
  374. }
  375. result = strcspn(arguments+pos, " ");
  376. if (result == 0) {
  377. break;
  378. }
  379. argv[i] = (char*)malloc(result+1);
  380. strncpy(argv[i], arguments+pos, result);
  381. argv[i][result] = '\0';
  382. pos += result+1;
  383. ++i;
  384. }
  385. argv[i] = 0;
  386. execv (command, argv);
  387. /* If execv() succeeds, it does not return. There's no point
  388. * in calling jack_error() here in the child process. */
  389. perror ("exec of JACK server failed");
  390. }
  391. int
  392. start_server (void)
  393. {
  394. /* Only fork() a server when $JACK_START_SERVER is defined and
  395. * $JACK_NO_START_SERVER is not. */
  396. if (getenv("JACK_START_SERVER") == NULL ||
  397. getenv("JACK_NO_START_SERVER") != NULL) {
  398. return 1;
  399. }
  400. /* The double fork() forces the server to become a child of
  401. * init, which will always clean up zombie process state on
  402. * termination. This even works in strange corner cases where
  403. * the server terminates but this client does not.
  404. *
  405. * Since fork() is usually implemented using copy-on-write
  406. * virtual memory tricks, the overhead of the second fork() is
  407. * probably relatively small.
  408. */
  409. switch (fork()) {
  410. case 0: /* child process */
  411. switch (fork()) {
  412. case 0: /* grandchild process */
  413. _start_server();
  414. _exit (99); /* exec failed */
  415. case -1:
  416. _exit (98);
  417. default:
  418. _exit (0);
  419. }
  420. case -1: /* fork() error */
  421. return 1; /* failed to start server */
  422. }
  423. /* only the original parent process goes here */
  424. return 0; /* (probably) successful */
  425. }
  426. static int
  427. jack_request_client (ClientType type, const char* client_name,
  428. const char* so_name, const char* so_data,
  429. jack_client_connect_result_t *res, int *req_fd)
  430. {
  431. jack_client_connect_request_t req;
  432. *req_fd = -1;
  433. memset (&req, 0, sizeof (req));
  434. if (strlen (client_name) >= sizeof (req.name)) {
  435. jack_error ("\"%s\" is too long to be used as a JACK client"
  436. " name.\n"
  437. "Please use %lu characters or less.",
  438. client_name, sizeof (req.name));
  439. return -1;
  440. }
  441. if (strlen (so_name) > sizeof (req.object_path) - 1) {
  442. jack_error ("\"%s\" is too long to be used as a JACK shared"
  443. " object name.\n"
  444. "Please use %lu characters or less.",
  445. so_name, sizeof (req.object_path) - 1);
  446. return -1;
  447. }
  448. if (strlen (so_data) > sizeof (req.object_data) - 1) {
  449. jack_error ("\"%s\" is too long to be used as a JACK shared"
  450. " object data string.\n"
  451. "Please use %lu characters or less.",
  452. so_data, sizeof (req.object_data) - 1);
  453. return -1;
  454. }
  455. if ((*req_fd = server_connect (0)) < 0) {
  456. int trys;
  457. if (start_server()) {
  458. goto fail;
  459. }
  460. trys = 5;
  461. do {
  462. sleep(1);
  463. if (--trys < 0) {
  464. goto fail;
  465. }
  466. } while ((*req_fd = server_connect (0)) < 0);
  467. }
  468. req.load = TRUE;
  469. req.type = type;
  470. snprintf (req.name, sizeof (req.name), "%s", client_name);
  471. snprintf (req.object_path, sizeof (req.object_path), "%s", so_name);
  472. snprintf (req.object_data, sizeof (req.object_data), "%s", so_data);
  473. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  474. jack_error ("cannot send request to jack server (%s)",
  475. strerror (errno));
  476. goto fail;
  477. }
  478. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  479. if (errno == 0) {
  480. /* server shut the socket */
  481. jack_error ("could not attach as client "
  482. "(duplicate client name?)");
  483. goto fail;
  484. }
  485. jack_error ("cannot read response from jack server (%s)",
  486. strerror (errno));
  487. goto fail;
  488. }
  489. if (res->status) {
  490. jack_error ("could not attach as client "
  491. "(duplicate client name?)");
  492. goto fail;
  493. }
  494. if (res->protocol_v != jack_protocol_version){
  495. jack_error ("application linked against incompatible libjack"
  496. " version.");
  497. goto fail;
  498. }
  499. switch (type) {
  500. case ClientDriver:
  501. case ClientInternal:
  502. close (*req_fd);
  503. *req_fd = -1;
  504. break;
  505. default:
  506. break;
  507. }
  508. return 0;
  509. fail:
  510. if (*req_fd >= 0) {
  511. close (*req_fd);
  512. *req_fd = -1;
  513. }
  514. return -1;
  515. }
  516. int
  517. jack_attach_port_segment (jack_client_t *client, jack_port_type_id_t ptid)
  518. {
  519. /* Lookup, attach and register the port/buffer segments in use
  520. * right now.
  521. */
  522. if (client->control->type != ClientExternal) {
  523. jack_error("Only external clients need attach port segments");
  524. abort();
  525. }
  526. /* make sure we have space to store the port
  527. segment information.
  528. */
  529. if (ptid >= client->n_port_types) {
  530. client->port_segment = (jack_shm_info_t*)
  531. realloc (client->port_segment,
  532. sizeof (jack_shm_info_t) * (ptid+1));
  533. memset (&client->port_segment[client->n_port_types],
  534. 0,
  535. sizeof (jack_shm_info_t) *
  536. (ptid - client->n_port_types));
  537. client->n_port_types = ptid + 1;
  538. } else {
  539. /* release any previous segment */
  540. #if JACK_USE_MACH_THREADS
  541. /* Stephane Letz : letz@grame.fr
  542. Need a fix : this crash on MacOSX : temporary removed
  543. jack_release_shm (&client->port_segment[ptid]);
  544. */
  545. #else
  546. jack_release_shm (&client->port_segment[ptid]);
  547. #endif
  548. }
  549. /* get the index into the shm registry */
  550. client->port_segment[ptid].index =
  551. client->engine->port_types[ptid].shm_registry_index;
  552. /* attach the relevant segment */
  553. if (jack_attach_shm (&client->port_segment[ptid])) {
  554. jack_error ("cannot attach port segment shared memory"
  555. " (%s)", strerror (errno));
  556. return -1;
  557. }
  558. /* The first chunk of the audio port segment will be set by
  559. * the engine to be a zero-filled buffer. This hasn't been
  560. * done yet, but it will happen before the process cycle
  561. * (re)starts.
  562. */
  563. if (ptid == JACK_AUDIO_PORT_TYPE) {
  564. jack_zero_filled_buffer =
  565. jack_shm_addr (&client->port_segment[ptid]);
  566. }
  567. return 0;
  568. }
  569. jack_client_t *
  570. jack_client_new (const char *client_name)
  571. {
  572. int req_fd = -1;
  573. int ev_fd = -1;
  574. jack_client_connect_result_t res;
  575. jack_client_t *client;
  576. jack_port_type_id_t ptid;
  577. /* external clients need this initialized; internal clients
  578. will use the setup in the server's address space.
  579. */
  580. jack_init_time ();
  581. if (jack_initialize_shm ()) {
  582. jack_error ("Unable to initialize shared memory.");
  583. return NULL;
  584. }
  585. if (jack_request_client (ClientExternal, client_name, "", "",
  586. &res, &req_fd)) {
  587. return NULL;
  588. }
  589. client = jack_client_alloc ();
  590. strcpy (client->fifo_prefix, res.fifo_prefix);
  591. client->request_fd = req_fd;
  592. client->pollfd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  593. client->pollfd[1].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  594. /* attach the engine control/info block */
  595. client->engine_shm = res.engine_shm;
  596. if (jack_attach_shm (&client->engine_shm)) {
  597. jack_error ("cannot attached engine control shared memory"
  598. " segment");
  599. goto fail;
  600. }
  601. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  602. /* now attach the client control block */
  603. client->control_shm = res.client_shm;
  604. if (jack_attach_shm (&client->control_shm)) {
  605. jack_error ("cannot attached client control shared memory"
  606. " segment");
  607. goto fail;
  608. }
  609. client->control = (jack_client_control_t *)
  610. jack_shm_addr (&client->control_shm);
  611. /* nobody else needs to access this shared memory any more, so
  612. destroy it. because we have our own attachment to it, it won't
  613. vanish till we exit (and release it).
  614. */
  615. jack_destroy_shm (&client->control_shm);
  616. client->n_port_types = client->engine->n_port_types;
  617. client->port_segment = (jack_shm_info_t *)
  618. malloc (sizeof (jack_shm_info_t) * client->n_port_types);
  619. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  620. client->port_segment[ptid].index =
  621. client->engine->port_types[ptid].shm_registry_index;
  622. jack_attach_port_segment (client, ptid);
  623. }
  624. /* set up the client so that it does the right thing for an
  625. * external client
  626. */
  627. client->control->deliver_request = oop_client_deliver_request;
  628. client->control->deliver_arg = client;
  629. if ((ev_fd = server_event_connect (client)) < 0) {
  630. goto fail;
  631. }
  632. client->event_fd = ev_fd;
  633. #ifdef JACK_USE_MACH_THREADS
  634. /* specific resources for server/client real-time thread
  635. * communication */
  636. client->clienttask = mach_task_self();
  637. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  638. jack_error ("Can't find bootstrap port");
  639. goto fail;
  640. }
  641. if (allocate_mach_clientport(client, res.portnum) < 0) {
  642. jack_error("Can't allocate mach port");
  643. goto fail;
  644. };
  645. #endif /* JACK_USE_MACH_THREADS */
  646. return client;
  647. fail:
  648. if (client->engine) {
  649. jack_release_shm (&client->engine_shm);
  650. client->engine = 0;
  651. }
  652. if (client->control) {
  653. jack_release_shm (&client->control_shm);
  654. client->control = 0;
  655. }
  656. if (req_fd >= 0) {
  657. close (req_fd);
  658. }
  659. if (ev_fd >= 0) {
  660. close (ev_fd);
  661. }
  662. return 0;
  663. }
  664. int
  665. jack_internal_client_new (const char *client_name, const char *so_name, const char *so_data)
  666. {
  667. jack_client_connect_result_t res;
  668. int req_fd;
  669. return jack_request_client (ClientInternal, client_name, so_name, so_data, &res, &req_fd);
  670. }
  671. void
  672. jack_internal_client_close (const char *client_name)
  673. {
  674. jack_client_connect_request_t req;
  675. int fd;
  676. req.load = FALSE;
  677. snprintf (req.name, sizeof (req.name), "%s", client_name);
  678. if ((fd = server_connect (0)) < 0) {
  679. return;
  680. }
  681. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  682. jack_error ("cannot deliver ClientUnload request to JACK server.");
  683. }
  684. /* no response to this request */
  685. close (fd);
  686. return;
  687. }
  688. #if JACK_USE_MACH_THREADS
  689. int
  690. jack_drop_real_time_scheduling (pthread_t thread)
  691. {
  692. setThreadToPriority(thread, 31, false, 10000000);
  693. return 0;
  694. }
  695. int
  696. jack_acquire_real_time_scheduling (pthread_t thread, int priority) //priority is unused
  697. {
  698. setThreadToPriority(thread, 96, true, 10000000);
  699. return 0;
  700. }
  701. #else
  702. int
  703. jack_drop_real_time_scheduling (pthread_t thread)
  704. {
  705. struct sched_param rtparam;
  706. int x;
  707. memset (&rtparam, 0, sizeof (rtparam));
  708. rtparam.sched_priority = 0;
  709. if ((x = pthread_setschedparam (thread, SCHED_OTHER, &rtparam)) != 0) {
  710. jack_error ("cannot switch to normal scheduling priority(%s)\n", strerror (errno));
  711. return -1;
  712. }
  713. return 0;
  714. }
  715. int
  716. jack_acquire_real_time_scheduling (pthread_t thread, int priority)
  717. {
  718. struct sched_param rtparam;
  719. int x;
  720. memset (&rtparam, 0, sizeof (rtparam));
  721. rtparam.sched_priority = priority;
  722. if ((x = pthread_setschedparam (thread, SCHED_FIFO, &rtparam)) != 0) {
  723. jack_error ("cannot use real-time scheduling (FIFO/%d) "
  724. "(%d: %s)", rtparam.sched_priority, x,
  725. strerror (x));
  726. return -1;
  727. }
  728. return 0;
  729. }
  730. #endif
  731. int
  732. jack_set_freewheel (jack_client_t* client, int onoff)
  733. {
  734. jack_request_t request;
  735. request.type = onoff ? FreeWheel : StopFreeWheel;
  736. return jack_client_deliver_request (client, &request);
  737. }
  738. void
  739. jack_start_freewheel (jack_client_t* client)
  740. {
  741. jack_client_control_t *control = client->control;
  742. if (client->engine->real_time) {
  743. #if JACK_USE_MACH_THREADS
  744. jack_drop_real_time_scheduling (client->process_thread);
  745. #else
  746. jack_drop_real_time_scheduling (client->thread);
  747. #endif
  748. }
  749. if (control->freewheel_cb) {
  750. control->freewheel_cb (1, control->freewheel_arg);
  751. }
  752. }
  753. void
  754. jack_stop_freewheel (jack_client_t* client)
  755. {
  756. jack_client_control_t *control = client->control;
  757. if (control->freewheel_cb) {
  758. control->freewheel_cb (0, control->freewheel_arg);
  759. }
  760. if (client->engine->real_time) {
  761. #if JACK_USE_MACH_THREADS
  762. jack_acquire_real_time_scheduling (client->process_thread,
  763. client->engine->client_priority);
  764. #else
  765. jack_acquire_real_time_scheduling (client->thread,
  766. client->engine->client_priority);
  767. #endif
  768. }
  769. }
  770. static void *
  771. jack_client_thread (void *arg)
  772. {
  773. jack_client_t *client = (jack_client_t *) arg;
  774. jack_client_control_t *control = client->control;
  775. jack_event_t event;
  776. char status = 0;
  777. char c;
  778. int err = 0;
  779. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  780. pthread_mutex_lock (&client_lock);
  781. client->thread_ok = TRUE;
  782. client->thread_id = pthread_self();
  783. pthread_cond_signal (&client_ready);
  784. pthread_mutex_unlock (&client_lock);
  785. client->control->pid = getpid();
  786. client->control->pgrp = getpgrp();
  787. DEBUG ("client thread is now running");
  788. if (client->control->thread_init) {
  789. DEBUG ("calling client thread init callback");
  790. client->control->thread_init (client->control->thread_init_arg);
  791. }
  792. while (err == 0) {
  793. pthread_testcancel();
  794. if (client->engine->engine_ok == 0) {
  795. if (client->on_shutdown)
  796. client->on_shutdown (client->on_shutdown_arg);
  797. else
  798. jack_error ("engine unexpectedly shutdown; "
  799. "thread exiting\n");
  800. pthread_exit (0);
  801. }
  802. DEBUG ("client polling on event_fd and graph_wait_fd...");
  803. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  804. if (errno == EINTR) {
  805. continue;
  806. }
  807. jack_error ("poll failed in client (%s)",
  808. strerror (errno));
  809. status = -1;
  810. break;
  811. }
  812. /* get an accurate timestamp on waking from poll for a
  813. * process() cycle.
  814. */
  815. if (client->pollfd[1].revents & POLLIN) {
  816. control->awake_at = jack_get_microseconds();
  817. }
  818. DEBUG ("pfd[0].revents = 0x%x pfd[1].revents = 0x%x",
  819. client->pollfd[0].revents,
  820. client->pollfd[1].revents);
  821. if ((client->pollfd[0].revents & ~POLLIN) ||
  822. client->control->dead) {
  823. goto zombie;
  824. }
  825. if (client->pollfd[0].revents & POLLIN) {
  826. DEBUG ("client receives an event, "
  827. "now reading on event fd");
  828. /* server has sent us an event. process the
  829. * event and reply */
  830. if (read (client->event_fd, &event, sizeof (event))
  831. != sizeof (event)) {
  832. jack_error ("cannot read server event (%s)",
  833. strerror (errno));
  834. err++;
  835. break;
  836. }
  837. status = 0;
  838. switch (event.type) {
  839. case PortRegistered:
  840. if (control->port_register) {
  841. control->port_register
  842. (event.x.port_id, TRUE,
  843. control->port_register_arg);
  844. }
  845. break;
  846. case PortUnregistered:
  847. if (control->port_register) {
  848. control->port_register
  849. (event.x.port_id, FALSE,
  850. control->port_register_arg);
  851. }
  852. break;
  853. case GraphReordered:
  854. status = jack_handle_reorder (client, &event);
  855. break;
  856. case PortConnected:
  857. case PortDisconnected:
  858. status = jack_client_handle_port_connection
  859. (client, &event);
  860. break;
  861. case BufferSizeChange:
  862. jack_client_invalidate_port_buffers (client);
  863. if (control->bufsize) {
  864. status = control->bufsize
  865. (control->nframes,
  866. control->bufsize_arg);
  867. }
  868. break;
  869. case SampleRateChange:
  870. if (control->srate) {
  871. status = control->srate
  872. (control->nframes,
  873. control->srate_arg);
  874. }
  875. break;
  876. case XRun:
  877. if (control->xrun) {
  878. status = control->xrun
  879. (control->xrun_arg);
  880. }
  881. break;
  882. case AttachPortSegment:
  883. jack_attach_port_segment (client, event.y.ptid);
  884. break;
  885. case StartFreewheel:
  886. jack_start_freewheel (client);
  887. break;
  888. case StopFreewheel:
  889. jack_stop_freewheel (client);
  890. break;
  891. }
  892. DEBUG ("client has dealt with the event, writing "
  893. "response on event fd");
  894. if (write (client->event_fd, &status, sizeof (status))
  895. != sizeof (status)) {
  896. jack_error ("cannot send event response to "
  897. "engine (%s)", strerror (errno));
  898. err++;
  899. break;
  900. }
  901. }
  902. if (client->pollfd[1].revents & ~POLLIN) {
  903. goto zombie;
  904. }
  905. if (client->pollfd[1].revents & POLLIN) {
  906. #ifdef WITH_TIMESTAMPS
  907. jack_reset_timestamps ();
  908. #endif
  909. DEBUG ("client %d signalled at %" PRIu64
  910. ", awake for process at %" PRIu64
  911. " (delay = %" PRIu64
  912. " usecs) (wakeup on graph_wait_fd==%d)",
  913. getpid(),
  914. control->signalled_at,
  915. control->awake_at,
  916. control->awake_at - control->signalled_at,
  917. client->pollfd[1].fd);
  918. control->state = Running;
  919. if (control->sync_cb)
  920. jack_call_sync_client (client);
  921. if (control->process) {
  922. if (control->process (control->nframes,
  923. control->process_arg)
  924. == 0) {
  925. control->state = Finished;
  926. }
  927. } else {
  928. control->state = Finished;
  929. }
  930. if (control->timebase_cb)
  931. jack_call_timebase_master (client);
  932. control->finished_at = jack_get_microseconds();
  933. #ifdef WITH_TIMESTAMPS
  934. jack_timestamp ("finished");
  935. #endif
  936. /* pass the execution token along */
  937. DEBUG ("client finished processing at %" PRIu64
  938. " (elapsed = %" PRIu64
  939. " usecs), writing on graph_next_fd==%d",
  940. control->finished_at,
  941. control->finished_at - control->awake_at,
  942. client->graph_next_fd);
  943. if (write (client->graph_next_fd, &c, sizeof (c))
  944. != sizeof (c)) {
  945. jack_error ("cannot continue execution of the "
  946. "processing graph (%s)",
  947. strerror(errno));
  948. err++;
  949. break;
  950. }
  951. DEBUG ("client sent message to next stage by %" PRIu64
  952. ", client reading on graph_wait_fd==%d",
  953. jack_get_microseconds(), client->graph_wait_fd);
  954. #ifdef WITH_TIMESTAMPS
  955. jack_timestamp ("read pending byte from wait");
  956. #endif
  957. DEBUG("reading cleanup byte from pipe\n");
  958. if ((read (client->graph_wait_fd, &c, sizeof (c))
  959. != sizeof (c))) {
  960. DEBUG ("WARNING: READ FAILED!");
  961. #if 0
  962. jack_error ("cannot complete execution of the "
  963. "processing graph (%s)",
  964. strerror(errno));
  965. err++;
  966. break;
  967. #endif
  968. }
  969. /* check if we were killed during the process
  970. * cycle (or whatever) */
  971. if (client->control->dead) {
  972. goto zombie;
  973. }
  974. DEBUG("process cycle fully complete\n");
  975. #ifdef WITH_TIMESTAMPS
  976. jack_timestamp ("read done");
  977. jack_dump_timestamps (stdout);
  978. #endif
  979. }
  980. }
  981. return (void *) ((intptr_t)err);
  982. zombie:
  983. if (client->on_shutdown) {
  984. jack_error ("zombified - calling shutdown handler");
  985. client->on_shutdown (client->on_shutdown_arg);
  986. } else {
  987. jack_error ("zombified - exiting from JACK");
  988. jack_client_close (client);
  989. /* Need a fix : possibly make client crash if
  990. * zombified without shutdown handler
  991. */
  992. }
  993. pthread_exit (0);
  994. /*NOTREACHED*/
  995. return 0;
  996. }
  997. #ifdef JACK_USE_MACH_THREADS
  998. /* real-time thread : separated from the normal client thread, it will
  999. * communicate with the server using fast mach RPC mechanism */
  1000. static void *
  1001. jack_client_process_thread (void *arg)
  1002. {
  1003. jack_client_t *client = (jack_client_t *) arg;
  1004. jack_client_control_t *control = client->control;
  1005. int err = 0;
  1006. client->control->pid = getpid();
  1007. DEBUG ("client process thread is now running");
  1008. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  1009. client->rt_thread_ok = TRUE;
  1010. while (err == 0) {
  1011. if (jack_client_suspend(client) < 0) {
  1012. jack_error ("jack_client_process_thread : resume error");
  1013. goto zombie;
  1014. }
  1015. control->awake_at = jack_get_microseconds();
  1016. DEBUG ("client resumed");
  1017. control->state = Running;
  1018. if (control->sync_cb)
  1019. jack_call_sync_client (client);
  1020. if (control->process) {
  1021. if (control->process (control->nframes,
  1022. control->process_arg) == 0) {
  1023. control->state = Finished;
  1024. }
  1025. } else {
  1026. control->state = Finished;
  1027. }
  1028. if (control->timebase_cb)
  1029. jack_call_timebase_master (client);
  1030. control->finished_at = jack_get_microseconds();
  1031. #ifdef WITH_TIMESTAMPS
  1032. jack_timestamp ("finished");
  1033. #endif
  1034. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  1035. control->finished_at, ((float)(control->finished_at - control->awake_at)));
  1036. /* check if we were killed during the process cycle (or whatever) */
  1037. if (client->control->dead) {
  1038. jack_error ("jack_client_process_thread : client->control->dead");
  1039. goto zombie;
  1040. }
  1041. DEBUG("process cycle fully complete\n");
  1042. }
  1043. return (void *) ((intptr_t)err);
  1044. zombie:
  1045. jack_error ("jack_client_process_thread : zombified");
  1046. client->rt_thread_ok = FALSE;
  1047. if (client->on_shutdown) {
  1048. jack_error ("zombified - calling shutdown handler");
  1049. client->on_shutdown (client->on_shutdown_arg);
  1050. } else {
  1051. jack_error ("zombified - exiting from JACK");
  1052. jack_client_close (client); /* Need a fix : possibly make client crash if zombified without shutdown handler */
  1053. }
  1054. pthread_exit (0);
  1055. /*NOTREACHED*/
  1056. return 0;
  1057. }
  1058. #endif /* JACK_USE_MACH_THREADS */
  1059. static int
  1060. jack_start_thread (jack_client_t *client)
  1061. {
  1062. #ifndef JACK_USE_MACH_THREADS
  1063. int policy = SCHED_OTHER;
  1064. struct sched_param client_param, temp_param;
  1065. #endif
  1066. pthread_attr_t *attributes = 0;
  1067. if (client->engine->real_time) {
  1068. /* Get the client thread to run as an RT-FIFO
  1069. scheduled thread of appropriate priority.
  1070. */
  1071. struct sched_param rt_param;
  1072. attributes = (pthread_attr_t *)
  1073. malloc (sizeof (pthread_attr_t));
  1074. pthread_attr_init (attributes);
  1075. if (pthread_attr_setschedpolicy (attributes, SCHED_FIFO)) {
  1076. jack_error ("cannot set FIFO scheduling class for RT "
  1077. "thread");
  1078. return -1;
  1079. }
  1080. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  1081. jack_error ("Cannot set scheduling scope for RT "
  1082. "thread");
  1083. return -1;
  1084. }
  1085. memset (&rt_param, 0, sizeof (rt_param));
  1086. rt_param.sched_priority = client->engine->client_priority;
  1087. if (pthread_attr_setschedparam (attributes, &rt_param)) {
  1088. jack_error ("Cannot set scheduling priority for RT "
  1089. "thread (%s)", strerror (errno));
  1090. return -1;
  1091. }
  1092. #ifdef USE_MLOCK
  1093. if (client->engine->do_mlock
  1094. && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1095. jack_error ("cannot lock down memory for RT thread (%s)",
  1096. strerror (errno));
  1097. #ifdef ENSURE_MLOCK
  1098. return -1;
  1099. #endif /* ENSURE_MLOCK */
  1100. }
  1101. #endif /* USE_MLOCK */
  1102. }
  1103. #ifdef JACK_USE_MACH_THREADS
  1104. if (pthread_create (&client->thread, attributes,
  1105. jack_client_thread, client)) {
  1106. return -1;
  1107. }
  1108. /* a special real-time thread to call the "process"
  1109. * callback. It will communicate with the server using fast
  1110. * mach RPC mechanism */
  1111. if (pthread_create (&client->process_thread, attributes,
  1112. jack_client_process_thread, client)) {
  1113. jack_error("pthread_create failed for process_thread \n");
  1114. return -1;
  1115. }
  1116. if (client->engine->real_time){
  1117. /* time constraint thread */
  1118. setThreadToPriority(client->process_thread, 96, TRUE, 10000000);
  1119. }else{
  1120. /* fixed priority thread */
  1121. setThreadToPriority(client->process_thread, 63, TRUE, 10000000);
  1122. }
  1123. #else /* !JACK_USE_MACH_THREADS */
  1124. if (pthread_create (&client->thread, attributes,
  1125. jack_client_thread, client) == 0) {
  1126. return 0;
  1127. }
  1128. if (!client->engine->real_time) {
  1129. return -1;
  1130. }
  1131. /* the version of glibc I've played with has a bug that makes
  1132. that code fail when running under a non-root user but with the
  1133. proper realtime capabilities (in short, pthread_attr_setschedpolicy
  1134. does not check for capabilities, only for the uid being
  1135. zero). Newer versions apparently have this fixed. This
  1136. workaround temporarily switches the client thread to the
  1137. proper scheduler and priority, then starts the realtime
  1138. thread so that it can inherit them and finally switches the
  1139. client thread back to what it was before. Sigh. For ardour
  1140. I have to check again and switch the thread explicitly to
  1141. realtime, don't know why or how to debug - nando
  1142. */
  1143. /* get current scheduler and parameters of the client process */
  1144. if ((policy = sched_getscheduler (0)) < 0) {
  1145. jack_error ("Cannot get current client scheduler: %s",
  1146. strerror(errno));
  1147. return -1;
  1148. }
  1149. memset (&client_param, 0, sizeof (client_param));
  1150. if (sched_getparam (0, &client_param)) {
  1151. jack_error ("Cannot get current client scheduler "
  1152. "parameters: %s", strerror(errno));
  1153. return -1;
  1154. }
  1155. /* temporarily change the client process to SCHED_FIFO so that
  1156. the realtime thread can inherit the scheduler and priority
  1157. */
  1158. memset (&temp_param, 0, sizeof (temp_param));
  1159. temp_param.sched_priority = client->engine->client_priority;
  1160. if (sched_setscheduler(0, SCHED_FIFO, &temp_param)) {
  1161. jack_error ("Cannot temporarily set client to RT scheduler:"
  1162. " %s", strerror(errno));
  1163. return -1;
  1164. }
  1165. /* prepare the attributes for the realtime thread */
  1166. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  1167. pthread_attr_init (attributes);
  1168. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  1169. sched_setscheduler (0, policy, &client_param);
  1170. jack_error ("Cannot set scheduling scope for RT thread");
  1171. return -1;
  1172. }
  1173. if (pthread_attr_setinheritsched (attributes, PTHREAD_INHERIT_SCHED)) {
  1174. sched_setscheduler (0, policy, &client_param);
  1175. jack_error ("Cannot set scheduler inherit policy for RT "
  1176. "thread");
  1177. return -1;
  1178. }
  1179. /* create the RT thread */
  1180. if (pthread_create (&client->thread, attributes,
  1181. jack_client_thread, client)) {
  1182. sched_setscheduler (0, policy, &client_param);
  1183. return -1;
  1184. }
  1185. /* return the client process to the scheduler it was in before */
  1186. if (sched_setscheduler (0, policy, &client_param)) {
  1187. jack_error ("Cannot reset original client scheduler: %s",
  1188. strerror(errno));
  1189. return -1;
  1190. }
  1191. /* check again... inheritance of policy and priority works in
  1192. jack_simple_client but not in ardour! So I check again and
  1193. force the policy if it is not set correctly. This does not
  1194. really really work either, the manager thread of the
  1195. linuxthreads implementation is left running with
  1196. SCHED_OTHER, that is presumably very bad.
  1197. */
  1198. memset (&client_param, 0, sizeof (client_param));
  1199. if (pthread_getschedparam(client->thread, &policy,
  1200. &client_param) == 0) {
  1201. if (policy != SCHED_FIFO) {
  1202. memset (&client_param, 0, sizeof (client_param));
  1203. client_param.sched_priority =
  1204. client->engine->client_priority;
  1205. if (pthread_setschedparam (client->thread, SCHED_FIFO,
  1206. &client_param)) {
  1207. jack_error ("Cannot set (again) FIFO scheduling"
  1208. " class for RT thread\n");
  1209. return -1;
  1210. }
  1211. }
  1212. }
  1213. #endif /* JACK_USE_MACH_THREADS */
  1214. return 0;
  1215. }
  1216. int
  1217. jack_activate (jack_client_t *client)
  1218. {
  1219. jack_request_t req;
  1220. /* we need to scribble on our stack to ensure that its memory
  1221. * pages are actually mapped (more important for mlockall(2)
  1222. * usage in jack_start_thread())
  1223. */
  1224. char buf[JACK_THREAD_STACK_TOUCH];
  1225. int i;
  1226. for (i = 0; i < JACK_THREAD_STACK_TOUCH; i++) {
  1227. buf[i] = (char) (i & 0xff);
  1228. }
  1229. if (client->control->type == ClientInternal ||
  1230. client->control->type == ClientDriver) {
  1231. goto startit;
  1232. }
  1233. /* get the pid of the client process to pass it to engine */
  1234. client->control->pid = getpid ();
  1235. #ifdef USE_CAPABILITIES
  1236. if (client->engine->has_capabilities != 0 &&
  1237. client->control->pid != 0 && client->engine->real_time != 0) {
  1238. /* we need to ask the engine for realtime capabilities
  1239. before trying to start the realtime thread
  1240. */
  1241. req.type = SetClientCapabilities;
  1242. req.x.client_id = client->control->id;
  1243. jack_client_deliver_request (client, &req);
  1244. if (req.status) {
  1245. /* what to do? engine is running realtime, it
  1246. is using capabilities and has them
  1247. (otherwise we would not get an error
  1248. return) but for some reason it could not
  1249. give the client the required capabilities,
  1250. so for now downgrade the client so that it
  1251. still runs, albeit non-realtime - nando
  1252. */
  1253. jack_error ("could not receive realtime capabilities, "
  1254. "client will run non-realtime");
  1255. /* XXX wrong, this is a property of the engine
  1256. client->engine->real_time = 0;
  1257. */
  1258. }
  1259. }
  1260. #endif /* USE_CAPABILITIES */
  1261. if (client->first_active) {
  1262. pthread_mutex_init (&client_lock, NULL);
  1263. pthread_cond_init (&client_ready, NULL);
  1264. pthread_mutex_lock (&client_lock);
  1265. if (jack_start_thread (client)) {
  1266. pthread_mutex_unlock (&client_lock);
  1267. return -1;
  1268. }
  1269. pthread_cond_wait (&client_ready, &client_lock);
  1270. pthread_mutex_unlock (&client_lock);
  1271. if (!client->thread_ok) {
  1272. jack_error ("could not start client thread");
  1273. return -1;
  1274. }
  1275. client->first_active = FALSE;
  1276. }
  1277. startit:
  1278. req.type = ActivateClient;
  1279. req.x.client_id = client->control->id;
  1280. return jack_client_deliver_request (client, &req);
  1281. }
  1282. int
  1283. jack_deactivate (jack_client_t *client)
  1284. {
  1285. jack_request_t req;
  1286. req.type = DeactivateClient;
  1287. req.x.client_id = client->control->id;
  1288. return jack_client_deliver_request (client, &req);
  1289. }
  1290. int
  1291. jack_client_close (jack_client_t *client)
  1292. {
  1293. JSList *node;
  1294. void *status;
  1295. if (client->control->active) {
  1296. jack_deactivate (client);
  1297. }
  1298. if (client->control->type == ClientExternal) {
  1299. #if JACK_USE_MACH_THREADS
  1300. if (client->rt_thread_ok) {
  1301. // MacOSX pthread_cancel not implemented in
  1302. // Darwin 5.5, 6.4
  1303. mach_port_t machThread =
  1304. pthread_mach_thread_np (client->process_thread);
  1305. thread_terminate (machThread);
  1306. }
  1307. #endif
  1308. /* stop the thread that communicates with the jack
  1309. * server, only if it was actually running
  1310. */
  1311. if (client->thread_ok){
  1312. pthread_cancel (client->thread);
  1313. pthread_join (client->thread, &status);
  1314. }
  1315. if (client->control) {
  1316. jack_release_shm (&client->control_shm);
  1317. client->control = NULL;
  1318. }
  1319. if (client->engine) {
  1320. jack_release_shm (&client->engine_shm);
  1321. client->engine = NULL;
  1322. }
  1323. if (client->port_segment) {
  1324. jack_port_type_id_t ptid;
  1325. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1326. jack_release_shm (&client->port_segment[ptid]);
  1327. }
  1328. free (client->port_segment);
  1329. client->port_segment = NULL;
  1330. }
  1331. if (client->graph_wait_fd) {
  1332. close (client->graph_wait_fd);
  1333. }
  1334. if (client->graph_next_fd) {
  1335. close (client->graph_next_fd);
  1336. }
  1337. close (client->event_fd);
  1338. close (client->request_fd);
  1339. }
  1340. for (node = client->ports; node; node = jack_slist_next (node)) {
  1341. free (node->data);
  1342. }
  1343. jack_slist_free (client->ports);
  1344. jack_client_free (client);
  1345. return 0;
  1346. }
  1347. int
  1348. jack_is_realtime (jack_client_t *client)
  1349. {
  1350. return client->engine->real_time;
  1351. }
  1352. jack_nframes_t
  1353. jack_get_buffer_size (jack_client_t *client)
  1354. {
  1355. return client->engine->buffer_size;
  1356. }
  1357. int
  1358. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1359. {
  1360. #ifdef DO_BUFFER_RESIZE
  1361. jack_request_t req;
  1362. req.type = SetBufferSize;
  1363. req.x.nframes = nframes;
  1364. return jack_client_deliver_request (client, &req);
  1365. #else
  1366. return ENOSYS;
  1367. #endif /* DO_BUFFER_RESIZE */
  1368. }
  1369. int
  1370. jack_connect (jack_client_t *client, const char *source_port,
  1371. const char *destination_port)
  1372. {
  1373. jack_request_t req;
  1374. req.type = ConnectPorts;
  1375. snprintf (req.x.connect.source_port,
  1376. sizeof (req.x.connect.source_port), "%s", source_port);
  1377. snprintf (req.x.connect.destination_port,
  1378. sizeof (req.x.connect.destination_port),
  1379. "%s", destination_port);
  1380. return jack_client_deliver_request (client, &req);
  1381. }
  1382. int
  1383. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1384. {
  1385. jack_request_t req;
  1386. pthread_mutex_lock (&port->connection_lock);
  1387. if (port->connections == NULL) {
  1388. pthread_mutex_unlock (&port->connection_lock);
  1389. return 0;
  1390. }
  1391. pthread_mutex_unlock (&port->connection_lock);
  1392. req.type = DisconnectPort;
  1393. req.x.port_info.port_id = port->shared->id;
  1394. return jack_client_deliver_request (client, &req);
  1395. }
  1396. int
  1397. jack_disconnect (jack_client_t *client, const char *source_port,
  1398. const char *destination_port)
  1399. {
  1400. jack_request_t req;
  1401. req.type = DisconnectPorts;
  1402. snprintf (req.x.connect.source_port,
  1403. sizeof (req.x.connect.source_port), "%s", source_port);
  1404. snprintf (req.x.connect.destination_port,
  1405. sizeof (req.x.connect.destination_port),
  1406. "%s", destination_port);
  1407. return jack_client_deliver_request (client, &req);
  1408. }
  1409. void
  1410. jack_set_error_function (void (*func) (const char *))
  1411. {
  1412. jack_error_callback = func;
  1413. }
  1414. int
  1415. jack_set_graph_order_callback (jack_client_t *client,
  1416. JackGraphOrderCallback callback, void *arg)
  1417. {
  1418. if (client->control->active) {
  1419. jack_error ("You cannot set callbacks on an active client.");
  1420. return -1;
  1421. }
  1422. client->control->graph_order = callback;
  1423. client->control->graph_order_arg = arg;
  1424. return 0;
  1425. }
  1426. int jack_set_xrun_callback (jack_client_t *client,
  1427. JackXRunCallback callback, void *arg)
  1428. {
  1429. if (client->control->active) {
  1430. jack_error ("You cannot set callbacks on an active client.");
  1431. return -1;
  1432. }
  1433. client->control->xrun = callback;
  1434. client->control->xrun_arg = arg;
  1435. return 0;
  1436. }
  1437. int
  1438. jack_set_process_callback (jack_client_t *client,
  1439. JackProcessCallback callback, void *arg)
  1440. {
  1441. if (client->control->active) {
  1442. jack_error ("You cannot set callbacks on an active client.");
  1443. return -1;
  1444. }
  1445. client->control->process_arg = arg;
  1446. client->control->process = callback;
  1447. return 0;
  1448. }
  1449. int
  1450. jack_set_thread_init_callback (jack_client_t *client,
  1451. JackThreadInitCallback callback, void *arg)
  1452. {
  1453. if (client->control->active) {
  1454. jack_error ("You cannot set callbacks on an active client.");
  1455. return -1;
  1456. }
  1457. client->control->thread_init_arg = arg;
  1458. client->control->thread_init = callback;
  1459. return 0;
  1460. }
  1461. int
  1462. jack_set_freewheel_callback (jack_client_t *client,
  1463. JackFreewheelCallback callback, void *arg)
  1464. {
  1465. if (client->control->active) {
  1466. jack_error ("You cannot set callbacks on an active client.");
  1467. return -1;
  1468. }
  1469. client->control->freewheel_arg = arg;
  1470. client->control->freewheel_cb = callback;
  1471. return 0;
  1472. }
  1473. int
  1474. jack_set_buffer_size_callback (jack_client_t *client,
  1475. JackBufferSizeCallback callback, void *arg)
  1476. {
  1477. client->control->bufsize_arg = arg;
  1478. client->control->bufsize = callback;
  1479. return 0;
  1480. }
  1481. int
  1482. jack_set_port_registration_callback(jack_client_t *client,
  1483. JackPortRegistrationCallback callback,
  1484. void *arg)
  1485. {
  1486. if (client->control->active) {
  1487. jack_error ("You cannot set callbacks on an active client.");
  1488. return -1;
  1489. }
  1490. client->control->port_register_arg = arg;
  1491. client->control->port_register = callback;
  1492. return 0;
  1493. }
  1494. int
  1495. jack_get_process_start_fd (jack_client_t *client)
  1496. {
  1497. /* once this has been called, the client thread
  1498. does not sleep on the graph wait fd.
  1499. */
  1500. client->pollmax = 1;
  1501. return client->graph_wait_fd;
  1502. }
  1503. int
  1504. jack_get_process_done_fd (jack_client_t *client)
  1505. {
  1506. return client->graph_next_fd;
  1507. }
  1508. void
  1509. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1510. {
  1511. client->on_shutdown = function;
  1512. client->on_shutdown_arg = arg;
  1513. }
  1514. const char **
  1515. jack_get_ports (jack_client_t *client,
  1516. const char *port_name_pattern,
  1517. const char *type_name_pattern,
  1518. unsigned long flags)
  1519. {
  1520. jack_control_t *engine;
  1521. const char **matching_ports;
  1522. unsigned long match_cnt;
  1523. jack_port_shared_t *psp;
  1524. unsigned long i;
  1525. regex_t port_regex;
  1526. regex_t type_regex;
  1527. int matching;
  1528. engine = client->engine;
  1529. if (port_name_pattern && port_name_pattern[0]) {
  1530. regcomp (&port_regex, port_name_pattern,
  1531. REG_EXTENDED|REG_NOSUB);
  1532. }
  1533. if (type_name_pattern && type_name_pattern[0]) {
  1534. regcomp (&type_regex, type_name_pattern,
  1535. REG_EXTENDED|REG_NOSUB);
  1536. }
  1537. psp = engine->ports;
  1538. match_cnt = 0;
  1539. matching_ports = (const char **)
  1540. malloc (sizeof (char *) * engine->port_max);
  1541. for (i = 0; i < engine->port_max; i++) {
  1542. matching = 1;
  1543. if (!psp[i].in_use) {
  1544. continue;
  1545. }
  1546. if (flags) {
  1547. if ((psp[i].flags & flags) != flags) {
  1548. matching = 0;
  1549. }
  1550. }
  1551. if (matching && port_name_pattern && port_name_pattern[0]) {
  1552. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1553. matching = 0;
  1554. }
  1555. }
  1556. if (matching && type_name_pattern && type_name_pattern[0]) {
  1557. jack_port_type_id_t ptid = psp[i].ptype_id;
  1558. if (regexec (&type_regex,
  1559. engine->port_types[ptid].type_name,
  1560. 0, NULL, 0)) {
  1561. matching = 0;
  1562. }
  1563. }
  1564. if (matching) {
  1565. matching_ports[match_cnt++] = psp[i].name;
  1566. }
  1567. }
  1568. matching_ports[match_cnt] = 0;
  1569. if (match_cnt == 0) {
  1570. free (matching_ports);
  1571. matching_ports = 0;
  1572. }
  1573. return matching_ports;
  1574. }
  1575. float
  1576. jack_cpu_load (jack_client_t *client)
  1577. {
  1578. return client->engine->cpu_load;
  1579. }
  1580. pthread_t
  1581. jack_client_thread_id (jack_client_t *client)
  1582. {
  1583. return client->thread_id;
  1584. }
  1585. int
  1586. jack_client_name_size(void)
  1587. {
  1588. return JACK_CLIENT_NAME_SIZE;
  1589. }
  1590. int
  1591. jack_port_name_size(void)
  1592. {
  1593. return JACK_PORT_NAME_SIZE;
  1594. }
  1595. int
  1596. jack_port_type_size(void)
  1597. {
  1598. return JACK_PORT_TYPE_SIZE;
  1599. }