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.

2035 lines
48KB

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