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.

2267 lines
51KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 Paul Davis
  4. Copyright (C) 2005 Jussi Laako
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  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. #include <limits.h>
  24. #ifdef HAVE_STDINT_H
  25. #include <stdint.h>
  26. #endif
  27. #include <regex.h>
  28. #include <string.h>
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31. #include <sys/un.h>
  32. #include <sys/mman.h>
  33. #include <jack/internal.h>
  34. #include <jack/jack.h>
  35. #include <jack/engine.h>
  36. #include <jack/pool.h>
  37. #include <jack/jslist.h>
  38. #include <jack/version.h>
  39. #include <jack/shm.h>
  40. #include <jack/unlock.h>
  41. #include <jack/thread.h>
  42. #include <jack/varargs.h>
  43. #include <jack/intsimd.h>
  44. #include <sysdeps/time.h>
  45. #include "local.h"
  46. #include <sysdeps/poll.h>
  47. #include <sysdeps/ipc.h>
  48. #include <sysdeps/cycles.h>
  49. #ifdef JACK_USE_MACH_THREADS
  50. #include <sysdeps/pThreadUtilities.h>
  51. #endif
  52. static pthread_mutex_t client_lock;
  53. static pthread_cond_t client_ready;
  54. #define EVENT_POLL_INDEX 0
  55. #define WAIT_POLL_INDEX 1
  56. #define event_fd pollfd[EVENT_POLL_INDEX].fd
  57. #define graph_wait_fd pollfd[WAIT_POLL_INDEX].fd
  58. typedef struct {
  59. int status;
  60. struct _jack_client *client;
  61. const char *client_name;
  62. } client_info;
  63. #ifdef USE_DYNSIMD
  64. #ifdef ARCH_X86
  65. int cpu_type = 0;
  66. static void
  67. init_cpu ()
  68. {
  69. cpu_type = ((have_3dnow() << 8) | have_sse());
  70. if (ARCH_X86_HAVE_3DNOW(cpu_type))
  71. fprintf(stderr, "Enhanced3DNow! detected\n");
  72. if (ARCH_X86_HAVE_SSE2(cpu_type))
  73. fprintf(stderr, "SSE2 detected\n");
  74. if ((!ARCH_X86_HAVE_3DNOW(cpu_type)) && (!ARCH_X86_HAVE_SSE2(cpu_type)))
  75. fprintf(stderr,
  76. "No supported SIMD instruction sets detected\n");
  77. jack_port_set_funcs();
  78. }
  79. #else /* ARCH_X86 */
  80. static void
  81. init_cpu ()
  82. {
  83. jack_port_set_funcs();
  84. }
  85. #endif /* ARCH_X86 */
  86. #endif /* USE_DYNSIMD */
  87. char *jack_tmpdir = DEFAULT_TMP_DIR;
  88. static int
  89. jack_get_tmpdir ()
  90. {
  91. FILE* in;
  92. size_t len;
  93. char buf[PATH_MAX+2]; /* allow tmpdir to live anywhere, plus newline, plus null */
  94. if ((in = popen ("jackd -l", "r")) == NULL) {
  95. return -1;
  96. }
  97. if (fgets (buf, sizeof (buf), in) == NULL) {
  98. fclose (in);
  99. return -1;
  100. }
  101. len = strlen (buf);
  102. if (buf[len-1] != '\n') {
  103. /* didn't get a whole line */
  104. fclose (in);
  105. return -1;
  106. }
  107. jack_tmpdir = (char *) malloc (len);
  108. memcpy (jack_tmpdir, buf, len-1);
  109. jack_tmpdir[len-1] = '\0';
  110. fclose (in);
  111. return 0;
  112. }
  113. void
  114. jack_error (const char *fmt, ...)
  115. {
  116. va_list ap;
  117. char buffer[300];
  118. va_start (ap, fmt);
  119. vsnprintf (buffer, sizeof(buffer), fmt, ap);
  120. jack_error_callback (buffer);
  121. va_end (ap);
  122. }
  123. void
  124. default_jack_error_callback (const char *desc)
  125. {
  126. fprintf(stderr, "%s\n", desc);
  127. }
  128. void
  129. silent_jack_error_callback (const char *desc)
  130. {
  131. }
  132. void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  133. static int
  134. oop_client_deliver_request (void *ptr, jack_request_t *req)
  135. {
  136. int wok, rok;
  137. jack_client_t *client = (jack_client_t*) ptr;
  138. wok = (write (client->request_fd, req, sizeof (*req))
  139. == sizeof (*req));
  140. rok = (read (client->request_fd, req, sizeof (*req))
  141. == sizeof (*req));
  142. if (wok && rok) { /* everything OK? */
  143. return req->status;
  144. }
  145. req->status = -1; /* request failed */
  146. /* check for server shutdown */
  147. if (client->engine->engine_ok == 0)
  148. return req->status;
  149. /* otherwise report errors */
  150. if (!wok)
  151. jack_error ("cannot send request type %d to server",
  152. req->type);
  153. if (!rok)
  154. jack_error ("cannot read result for request type %d from"
  155. " server (%s)", req->type, strerror (errno));
  156. return req->status;
  157. }
  158. int
  159. jack_client_deliver_request (const jack_client_t *client, jack_request_t *req)
  160. {
  161. /* indirect through the function pointer that was set either
  162. * by jack_client_open() or by jack_new_client_request() in
  163. * the server.
  164. */
  165. return client->control->deliver_request (client->control->deliver_arg,
  166. req);
  167. }
  168. #if JACK_USE_MACH_THREADS
  169. jack_client_t *
  170. jack_client_alloc ()
  171. {
  172. jack_client_t *client;
  173. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  174. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 1);
  175. client->pollmax = 1;
  176. client->request_fd = -1;
  177. client->event_fd = -1;
  178. client->upstream_is_jackd = 0;
  179. client->graph_wait_fd = -1;
  180. client->graph_next_fd = -1;
  181. client->ports = NULL;
  182. client->ports_ext = NULL;
  183. client->engine = NULL;
  184. client->control = NULL;
  185. client->thread_ok = FALSE;
  186. client->rt_thread_ok = FALSE;
  187. client->first_active = TRUE;
  188. client->on_shutdown = NULL;
  189. client->n_port_types = 0;
  190. client->port_segment = NULL;
  191. return client;
  192. }
  193. #else
  194. jack_client_t *
  195. jack_client_alloc ()
  196. {
  197. jack_client_t *client;
  198. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  199. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2);
  200. client->pollmax = 2;
  201. client->request_fd = -1;
  202. client->event_fd = -1;
  203. client->upstream_is_jackd = 0;
  204. client->graph_wait_fd = -1;
  205. client->graph_next_fd = -1;
  206. client->ports = NULL;
  207. client->ports_ext = NULL;
  208. client->engine = NULL;
  209. client->control = NULL;
  210. client->thread_ok = FALSE;
  211. client->first_active = TRUE;
  212. client->on_shutdown = NULL;
  213. client->n_port_types = 0;
  214. client->port_segment = NULL;
  215. #ifdef USE_DYNSIMD
  216. init_cpu();
  217. #endif /* USE_DYNSIMD */
  218. return client;
  219. }
  220. #endif
  221. /*
  222. * Build the jack_client_t structure for an internal client.
  223. */
  224. jack_client_t *
  225. jack_client_alloc_internal (jack_client_control_t *cc, jack_engine_t* engine)
  226. {
  227. jack_client_t* client;
  228. client = jack_client_alloc ();
  229. client->control = cc;
  230. client->engine = engine->control;
  231. client->n_port_types = client->engine->n_port_types;
  232. client->port_segment = &engine->port_segment[0];
  233. return client;
  234. }
  235. static void
  236. jack_client_free (jack_client_t *client)
  237. {
  238. if (client->pollfd) {
  239. free (client->pollfd);
  240. }
  241. free (client);
  242. }
  243. void
  244. jack_client_invalidate_port_buffers (jack_client_t *client)
  245. {
  246. JSList *node;
  247. jack_port_t *port;
  248. /* This releases all local memory owned by input ports
  249. and sets the buffer pointer to NULL. This will cause
  250. jack_port_get_buffer() to reallocate space for the
  251. buffer on the next call (if there is one).
  252. */
  253. for (node = client->ports; node; node = jack_slist_next (node)) {
  254. port = (jack_port_t *) node->data;
  255. if (port->shared->flags & JackPortIsInput) {
  256. if (port->mix_buffer) {
  257. jack_pool_release (port->mix_buffer);
  258. port->mix_buffer = NULL;
  259. }
  260. }
  261. }
  262. }
  263. int
  264. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  265. {
  266. jack_port_t *control_port;
  267. jack_port_t *other = 0;
  268. JSList *node;
  269. int need_free = FALSE;
  270. if (client->engine->ports[event->x.self_id].client_id == client->control->id ||
  271. client->engine->ports[event->y.other_id].client_id == client->control->id) {
  272. /* its one of ours */
  273. switch (event->type) {
  274. case PortConnected:
  275. other = jack_port_new (client, event->y.other_id,
  276. client->engine);
  277. /* jack_port_by_id_int() always returns an internal
  278. * port that does not need to be deallocated
  279. */
  280. control_port = jack_port_by_id_int (client, event->x.self_id,
  281. &need_free);
  282. pthread_mutex_lock (&control_port->connection_lock);
  283. control_port->connections =
  284. jack_slist_prepend (control_port->connections,
  285. (void *) other);
  286. pthread_mutex_unlock (&control_port->connection_lock);
  287. break;
  288. case PortDisconnected:
  289. /* jack_port_by_id_int() always returns an internal
  290. * port that does not need to be deallocated
  291. */
  292. control_port = jack_port_by_id_int (client, event->x.self_id,
  293. &need_free);
  294. pthread_mutex_lock (&control_port->connection_lock);
  295. for (node = control_port->connections; node;
  296. node = jack_slist_next (node)) {
  297. other = (jack_port_t *) node->data;
  298. if (other->shared->id == event->y.other_id) {
  299. control_port->connections =
  300. jack_slist_remove_link (
  301. control_port->connections,
  302. node);
  303. jack_slist_free_1 (node);
  304. free (other);
  305. break;
  306. }
  307. }
  308. pthread_mutex_unlock (&control_port->connection_lock);
  309. break;
  310. default:
  311. /* impossible */
  312. break;
  313. }
  314. }
  315. if (client->control->port_connect) {
  316. client->control->port_connect (event->x.self_id, event->y.other_id,
  317. (event->type == PortConnected ? 1 : 0),
  318. client->control->port_connect_arg);
  319. }
  320. return 0;
  321. }
  322. #if JACK_USE_MACH_THREADS
  323. static int
  324. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  325. {
  326. client->pollmax = 1;
  327. /* If the client registered its own callback for graph order events,
  328. execute it now.
  329. */
  330. if (client->control->graph_order) {
  331. client->control->graph_order (client->control->graph_order_arg);
  332. }
  333. return 0;
  334. }
  335. #else
  336. static int
  337. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  338. {
  339. char path[PATH_MAX+1];
  340. DEBUG ("graph reorder\n");
  341. if (client->graph_wait_fd >= 0) {
  342. DEBUG ("closing graph_wait_fd==%d", client->graph_wait_fd);
  343. close (client->graph_wait_fd);
  344. client->graph_wait_fd = -1;
  345. }
  346. if (client->graph_next_fd >= 0) {
  347. DEBUG ("closing graph_next_fd==%d", client->graph_next_fd);
  348. close (client->graph_next_fd);
  349. client->graph_next_fd = -1;
  350. }
  351. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n);
  352. if ((client->graph_wait_fd = open (path, O_RDONLY|O_NONBLOCK)) < 0) {
  353. jack_error ("cannot open specified fifo [%s] for reading (%s)",
  354. path, strerror (errno));
  355. return -1;
  356. }
  357. DEBUG ("opened new graph_wait_fd %d (%s)", client->graph_wait_fd, path);
  358. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n+1);
  359. if ((client->graph_next_fd = open (path, O_WRONLY|O_NONBLOCK)) < 0) {
  360. jack_error ("cannot open specified fifo [%s] for writing (%s)",
  361. path, strerror (errno));
  362. return -1;
  363. }
  364. client->upstream_is_jackd = event->y.n;
  365. client->pollmax = 2;
  366. DEBUG ("opened new graph_next_fd %d (%s) (upstream is jackd? %d)",
  367. client->graph_next_fd, path,
  368. client->upstream_is_jackd);
  369. /* If the client registered its own callback for graph order events,
  370. execute it now.
  371. */
  372. if (client->control->graph_order) {
  373. client->control->graph_order (client->control->graph_order_arg);
  374. }
  375. return 0;
  376. }
  377. #endif
  378. static int
  379. server_connect (const char *server_name)
  380. {
  381. int fd;
  382. struct sockaddr_un addr;
  383. int which = 0;
  384. char server_dir[PATH_MAX+1] = "";
  385. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  386. jack_error ("cannot create client socket (%s)",
  387. strerror (errno));
  388. return -1;
  389. }
  390. //JOQ: temporary debug message
  391. //fprintf (stderr, "DEBUG: connecting to `%s' server\n", server_name);
  392. addr.sun_family = AF_UNIX;
  393. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d",
  394. jack_server_dir (server_name, server_dir) , which);
  395. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  396. close (fd);
  397. return -1;
  398. }
  399. return fd;
  400. }
  401. static int
  402. server_event_connect (jack_client_t *client, const char *server_name)
  403. {
  404. int fd;
  405. struct sockaddr_un addr;
  406. jack_client_connect_ack_request_t req;
  407. jack_client_connect_ack_result_t res;
  408. char server_dir[PATH_MAX+1] = "";
  409. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  410. jack_error ("cannot create client event socket (%s)",
  411. strerror (errno));
  412. return -1;
  413. }
  414. addr.sun_family = AF_UNIX;
  415. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0",
  416. jack_server_dir (server_name,server_dir));
  417. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  418. jack_error ("cannot connect to jack server for events",
  419. strerror (errno));
  420. close (fd);
  421. return -1;
  422. }
  423. req.client_id = client->control->id;
  424. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  425. jack_error ("cannot write event connect request to server (%s)",
  426. strerror (errno));
  427. close (fd);
  428. return -1;
  429. }
  430. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  431. jack_error ("cannot read event connect result from server (%s)",
  432. strerror (errno));
  433. close (fd);
  434. return -1;
  435. }
  436. if (res.status != 0) {
  437. jack_error ("cannot connect to server for event stream (%s)",
  438. strerror (errno));
  439. close (fd);
  440. return -1;
  441. }
  442. return fd;
  443. }
  444. /* Exec the JACK server in this process. Does not return. */
  445. static void
  446. _start_server (const char *server_name)
  447. {
  448. FILE* fp = 0;
  449. char filename[255];
  450. char arguments[255];
  451. char buffer[255];
  452. char* command = 0;
  453. size_t pos = 0;
  454. size_t result = 0;
  455. char** argv = 0;
  456. int i = 0;
  457. int good = 0;
  458. int ret;
  459. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  460. fp = fopen(filename, "r");
  461. if (!fp) {
  462. fp = fopen("/etc/jackdrc", "r");
  463. }
  464. /* if still not found, check old config name for backwards compatability */
  465. if (!fp) {
  466. fp = fopen("/etc/jackd.conf", "r");
  467. }
  468. if (fp) {
  469. arguments[0] = '\0';
  470. ret = fscanf(fp, "%s", buffer);
  471. while(ret != 0 && ret != EOF) {
  472. strcat(arguments, buffer);
  473. strcat(arguments, " ");
  474. ret = fscanf(fp, "%s", buffer);
  475. }
  476. if (strlen(arguments) > 0) {
  477. good = 1;
  478. }
  479. }
  480. if (!good) {
  481. #if defined(USE_CAPABILITIES)
  482. command = JACK_LOCATION "/jackstart";
  483. strncpy(arguments, JACK_LOCATION "/jackstart -T -R -d "
  484. JACK_DEFAULT_DRIVER " -p 512", 255);
  485. #else /* !USE_CAPABILITIES */
  486. command = JACK_LOCATION "/jackd";
  487. strncpy(arguments, JACK_LOCATION "/jackd -T -d "
  488. JACK_DEFAULT_DRIVER, 255);
  489. #endif /* USE_CAPABILITIES */
  490. } else {
  491. result = strcspn(arguments, " ");
  492. command = (char *) malloc(result+1);
  493. strncpy(command, arguments, result);
  494. command[result] = '\0';
  495. }
  496. argv = (char **) malloc (255);
  497. while(1) {
  498. /* insert -T and -nserver_name in front of arguments */
  499. if (i == 1) {
  500. argv[i] = (char *) malloc(strlen ("-T") + 1);
  501. strcpy (argv[i++], "-T");
  502. if (server_name) {
  503. size_t optlen = strlen ("-n");
  504. char *buf =
  505. malloc (optlen
  506. + strlen (server_name) + 1);
  507. strcpy (buf, "-n");
  508. strcpy (buf+optlen, server_name);
  509. argv[i++] = buf;
  510. }
  511. }
  512. result = strcspn(arguments+pos, " ");
  513. if (result == 0) {
  514. break;
  515. }
  516. argv[i] = (char*)malloc(result+1);
  517. strncpy(argv[i], arguments+pos, result);
  518. argv[i][result] = '\0';
  519. pos += result+1;
  520. ++i;
  521. }
  522. argv[i] = 0;
  523. execv (command, argv);
  524. /* If execv() succeeds, it does not return. There's no point
  525. * in calling jack_error() here in the child process. */
  526. fprintf (stderr, "exec of JACK server (command = \"%s\") failed: %s\n", command, strerror (errno));
  527. }
  528. int
  529. start_server (const char *server_name, jack_options_t options)
  530. {
  531. if ((options & JackNoStartServer)
  532. || getenv("JACK_NO_START_SERVER")) {
  533. return 1;
  534. }
  535. /* The double fork() forces the server to become a child of
  536. * init, which will always clean up zombie process state on
  537. * termination. This even works in cases where the server
  538. * terminates but this client does not.
  539. *
  540. * Since fork() is usually implemented using copy-on-write
  541. * virtual memory tricks, the overhead of the second fork() is
  542. * probably relatively small.
  543. */
  544. switch (fork()) {
  545. case 0: /* child process */
  546. switch (fork()) {
  547. case 0: /* grandchild process */
  548. _start_server(server_name);
  549. _exit (99); /* exec failed */
  550. case -1:
  551. _exit (98);
  552. default:
  553. _exit (0);
  554. }
  555. case -1: /* fork() error */
  556. return 1; /* failed to start server */
  557. }
  558. /* only the original parent process goes here */
  559. return 0; /* (probably) successful */
  560. }
  561. static int
  562. jack_request_client (ClientType type,
  563. const char* client_name, jack_options_t options,
  564. jack_status_t *status, jack_varargs_t *va,
  565. jack_client_connect_result_t *res, int *req_fd)
  566. {
  567. jack_client_connect_request_t req;
  568. *req_fd = -1;
  569. memset (&req, 0, sizeof (req));
  570. req.options = options;
  571. if (strlen (client_name) >= sizeof (req.name)) {
  572. jack_error ("\"%s\" is too long to be used as a JACK client"
  573. " name.\n"
  574. "Please use %lu characters or less.",
  575. client_name, sizeof (req.name));
  576. return -1;
  577. }
  578. if (va->load_name
  579. && (strlen (va->load_name) > sizeof (req.object_path) - 1)) {
  580. jack_error ("\"%s\" is too long to be used as a JACK shared"
  581. " object name.\n"
  582. "Please use %lu characters or less.",
  583. va->load_name, sizeof (req.object_path) - 1);
  584. return -1;
  585. }
  586. if (va->load_init
  587. && (strlen (va->load_init) > sizeof (req.object_data) - 1)) {
  588. jack_error ("\"%s\" is too long to be used as a JACK shared"
  589. " object data string.\n"
  590. "Please use %lu characters or less.",
  591. va->load_init, sizeof (req.object_data) - 1);
  592. return -1;
  593. }
  594. if ((*req_fd = server_connect (va->server_name)) < 0) {
  595. int trys;
  596. if (start_server(va->server_name, options)) {
  597. *status |= (JackFailure|JackServerFailed);
  598. goto fail;
  599. }
  600. trys = 5;
  601. do {
  602. sleep(1);
  603. if (--trys < 0) {
  604. *status |= (JackFailure|JackServerFailed);
  605. goto fail;
  606. }
  607. } while ((*req_fd = server_connect (va->server_name)) < 0);
  608. *status |= JackServerStarted;
  609. }
  610. /* format connection request */
  611. req.protocol_v = jack_protocol_version;
  612. req.load = TRUE;
  613. req.type = type;
  614. snprintf (req.name, sizeof (req.name),
  615. "%s", client_name);
  616. snprintf (req.object_path, sizeof (req.object_path),
  617. "%s", va->load_name);
  618. snprintf (req.object_data, sizeof (req.object_data),
  619. "%s", va->load_init);
  620. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  621. jack_error ("cannot send request to jack server (%s)",
  622. strerror (errno));
  623. *status |= (JackFailure|JackServerError);
  624. goto fail;
  625. }
  626. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  627. if (errno == 0) {
  628. /* server shut the socket */
  629. jack_error ("could not attach as client");
  630. *status |= (JackFailure|JackServerError);
  631. goto fail;
  632. }
  633. if (errno == ECONNRESET) {
  634. jack_error ("could not attach as JACK client "
  635. "(server has exited)");
  636. *status |= (JackFailure|JackServerError);
  637. goto fail;
  638. }
  639. jack_error ("cannot read response from jack server (%s)",
  640. strerror (errno));
  641. *status |= (JackFailure|JackServerError);
  642. goto fail;
  643. }
  644. *status |= res->status; /* return server status bits */
  645. if (*status & JackFailure) {
  646. if (*status & JackVersionError) {
  647. jack_error ("client linked with incompatible libjack"
  648. " version.");
  649. }
  650. jack_error ("could not attach to JACK server");
  651. *status |= JackServerError;
  652. goto fail;
  653. }
  654. switch (type) {
  655. case ClientDriver:
  656. case ClientInternal:
  657. close (*req_fd);
  658. *req_fd = -1;
  659. break;
  660. default:
  661. break;
  662. }
  663. return 0;
  664. fail:
  665. if (*req_fd >= 0) {
  666. close (*req_fd);
  667. *req_fd = -1;
  668. }
  669. return -1;
  670. }
  671. int
  672. jack_attach_port_segment (jack_client_t *client, jack_port_type_id_t ptid)
  673. {
  674. /* Lookup, attach and register the port/buffer segments in use
  675. * right now.
  676. */
  677. if (client->control->type != ClientExternal) {
  678. jack_error("Only external clients need attach port segments");
  679. abort();
  680. }
  681. /* make sure we have space to store the port
  682. segment information.
  683. */
  684. if (ptid >= client->n_port_types) {
  685. client->port_segment = (jack_shm_info_t*)
  686. realloc (client->port_segment,
  687. sizeof (jack_shm_info_t) * (ptid+1));
  688. memset (&client->port_segment[client->n_port_types],
  689. 0,
  690. sizeof (jack_shm_info_t) *
  691. (ptid - client->n_port_types));
  692. client->n_port_types = ptid + 1;
  693. } else {
  694. /* release any previous segment */
  695. jack_release_shm (&client->port_segment[ptid]);
  696. }
  697. /* get the index into the shm registry */
  698. client->port_segment[ptid].index =
  699. client->engine->port_types[ptid].shm_registry_index;
  700. /* attach the relevant segment */
  701. if (jack_attach_shm (&client->port_segment[ptid])) {
  702. jack_error ("cannot attach port segment shared memory"
  703. " (%s)", strerror (errno));
  704. return -1;
  705. }
  706. return 0;
  707. }
  708. jack_client_t *
  709. jack_client_open (const char *client_name,
  710. jack_options_t options,
  711. jack_status_t *status, ...)
  712. {
  713. /* optional arguments: */
  714. va_list ap; /* variable argument pointer */
  715. jack_varargs_t va; /* variable arguments */
  716. int req_fd = -1;
  717. int ev_fd = -1;
  718. jack_client_connect_result_t res;
  719. jack_client_t *client;
  720. jack_port_type_id_t ptid;
  721. jack_status_t my_status;
  722. if (status == NULL) /* no status from caller? */
  723. status = &my_status; /* use local status word */
  724. *status = 0;
  725. /* validate parameters */
  726. if ((options & ~JackOpenOptions)) {
  727. *status |= (JackFailure|JackInvalidOption);
  728. return NULL;
  729. }
  730. /* parse variable arguments */
  731. va_start (ap, status);
  732. jack_varargs_parse (options, ap, &va);
  733. va_end (ap);
  734. /* External clients need to know where the tmpdir used for
  735. communication with the server lives
  736. */
  737. if (jack_get_tmpdir ()) {
  738. *status |= JackFailure;
  739. return NULL;
  740. }
  741. /* External clients need this initialized. It is already set
  742. * up in the server's address space for internal clients.
  743. */
  744. jack_init_time ();
  745. if (jack_request_client (ClientExternal, client_name, options, status,
  746. &va, &res, &req_fd)) {
  747. return NULL;
  748. }
  749. /* Allocate the jack_client_t structure in local memory.
  750. * Shared memory is not accessible yet. */
  751. client = jack_client_alloc ();
  752. strcpy (client->name, res.name);
  753. strcpy (client->fifo_prefix, res.fifo_prefix);
  754. client->request_fd = req_fd;
  755. client->pollfd[EVENT_POLL_INDEX].events =
  756. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  757. #ifndef JACK_USE_MACH_THREADS
  758. client->pollfd[WAIT_POLL_INDEX].events =
  759. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  760. #endif
  761. /* Don't access shared memory until server connected. */
  762. if (jack_initialize_shm (va.server_name)) {
  763. jack_error ("Unable to initialize shared memory.");
  764. *status |= (JackFailure|JackShmFailure);
  765. goto fail;
  766. }
  767. /* attach the engine control/info block */
  768. client->engine_shm = res.engine_shm;
  769. if (jack_attach_shm (&client->engine_shm)) {
  770. jack_error ("cannot attached engine control shared memory"
  771. " segment");
  772. goto fail;
  773. }
  774. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  775. /* initialize clock source as early as possible */
  776. jack_set_clock_source (client->engine->clock_source);
  777. /* now attach the client control block */
  778. client->control_shm = res.client_shm;
  779. if (jack_attach_shm (&client->control_shm)) {
  780. jack_error ("cannot attached client control shared memory"
  781. " segment");
  782. goto fail;
  783. }
  784. client->control = (jack_client_control_t *)
  785. jack_shm_addr (&client->control_shm);
  786. /* Nobody else needs to access this shared memory any more, so
  787. * destroy it. Because we have it attached, it won't vanish
  788. * till we exit (and release it).
  789. */
  790. jack_destroy_shm (&client->control_shm);
  791. client->n_port_types = client->engine->n_port_types;
  792. client->port_segment = (jack_shm_info_t *)
  793. malloc (sizeof (jack_shm_info_t) * client->n_port_types);
  794. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  795. client->port_segment[ptid].index =
  796. client->engine->port_types[ptid].shm_registry_index;
  797. client->port_segment[ptid].attached_at = MAP_FAILED;
  798. jack_attach_port_segment (client, ptid);
  799. }
  800. /* set up the client so that it does the right thing for an
  801. * external client
  802. */
  803. client->control->deliver_request = oop_client_deliver_request;
  804. client->control->deliver_arg = client;
  805. if ((ev_fd = server_event_connect (client, va.server_name)) < 0) {
  806. goto fail;
  807. }
  808. client->event_fd = ev_fd;
  809. #ifdef JACK_USE_MACH_THREADS
  810. /* specific resources for server/client real-time thread
  811. * communication */
  812. client->clienttask = mach_task_self();
  813. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  814. jack_error ("Can't find bootstrap port");
  815. goto fail;
  816. }
  817. if (allocate_mach_clientport(client, res.portnum) < 0) {
  818. jack_error("Can't allocate mach port");
  819. goto fail;
  820. };
  821. #endif /* JACK_USE_MACH_THREADS */
  822. return client;
  823. fail:
  824. if (client->engine) {
  825. jack_release_shm (&client->engine_shm);
  826. client->engine = 0;
  827. }
  828. if (client->control) {
  829. jack_release_shm (&client->control_shm);
  830. client->control = 0;
  831. }
  832. if (req_fd >= 0) {
  833. close (req_fd);
  834. }
  835. if (ev_fd >= 0) {
  836. close (ev_fd);
  837. }
  838. free (client);
  839. return NULL;
  840. }
  841. jack_client_t *
  842. jack_client_new (const char *client_name)
  843. {
  844. jack_options_t options = JackUseExactName;
  845. if (getenv("JACK_START_SERVER") == NULL)
  846. options |= JackNoStartServer;
  847. return jack_client_open (client_name, options, NULL);
  848. }
  849. char *
  850. jack_get_client_name (jack_client_t *client)
  851. {
  852. return client->name;
  853. }
  854. int
  855. jack_internal_client_new (const char *client_name,
  856. const char *so_name, const char *so_data)
  857. {
  858. jack_client_connect_result_t res;
  859. int req_fd;
  860. jack_varargs_t va;
  861. jack_status_t status;
  862. jack_options_t options = JackUseExactName;
  863. if (getenv("JACK_START_SERVER") == NULL)
  864. options |= JackNoStartServer;
  865. jack_varargs_init (&va);
  866. va.load_name = (char *) so_name;
  867. va.load_init = (char *) so_data;
  868. return jack_request_client (ClientInternal, client_name,
  869. options, &status, &va, &res, &req_fd);
  870. }
  871. char *
  872. jack_default_server_name (void)
  873. {
  874. char *server_name;
  875. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  876. server_name = "default";
  877. return server_name;
  878. }
  879. /* returns the name of the per-user subdirectory of jack_tmpdir */
  880. char *
  881. jack_user_dir (void)
  882. {
  883. static char user_dir[PATH_MAX+1] = "";
  884. /* format the path name on the first call */
  885. if (user_dir[0] == '\0') {
  886. if (getenv ("JACK_PROMISCUOUS_SERVER")) {
  887. snprintf (user_dir, sizeof (user_dir), "%s/jack",
  888. jack_tmpdir);
  889. } else {
  890. snprintf (user_dir, sizeof (user_dir), "%s/jack-%d",
  891. jack_tmpdir, getuid ());
  892. }
  893. }
  894. return user_dir;
  895. }
  896. /* returns the name of the per-server subdirectory of jack_user_dir() */
  897. char *
  898. jack_server_dir (const char *server_name, char *server_dir)
  899. {
  900. /* format the path name into the suppled server_dir char array,
  901. * assuming that server_dir is at least as large as PATH_MAX+1 */
  902. snprintf (server_dir, PATH_MAX+1, "%s/%s",
  903. jack_user_dir (), server_name);
  904. return server_dir;
  905. }
  906. void
  907. jack_internal_client_close (const char *client_name)
  908. {
  909. jack_client_connect_request_t req;
  910. int fd;
  911. char *server_name = jack_default_server_name ();
  912. req.load = FALSE;
  913. snprintf (req.name, sizeof (req.name), "%s", client_name);
  914. if ((fd = server_connect (server_name)) < 0) {
  915. return;
  916. }
  917. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  918. jack_error ("cannot deliver ClientUnload request to JACK "
  919. "server.");
  920. }
  921. /* no response to this request */
  922. close (fd);
  923. return;
  924. }
  925. int
  926. jack_recompute_total_latencies (jack_client_t* client)
  927. {
  928. jack_request_t request;
  929. request.type = RecomputeTotalLatencies;
  930. return jack_client_deliver_request (client, &request);
  931. }
  932. int
  933. jack_recompute_total_latency (jack_client_t* client, jack_port_t* port)
  934. {
  935. jack_request_t request;
  936. request.type = RecomputeTotalLatency;
  937. request.x.port_info.port_id = port->shared->id;
  938. return jack_client_deliver_request (client, &request);
  939. }
  940. int
  941. jack_set_freewheel (jack_client_t* client, int onoff)
  942. {
  943. jack_request_t request;
  944. request.type = onoff ? FreeWheel : StopFreeWheel;
  945. return jack_client_deliver_request (client, &request);
  946. }
  947. void
  948. jack_start_freewheel (jack_client_t* client)
  949. {
  950. jack_client_control_t *control = client->control;
  951. if (client->engine->real_time) {
  952. #if JACK_USE_MACH_THREADS
  953. jack_drop_real_time_scheduling (client->process_thread);
  954. #else
  955. jack_drop_real_time_scheduling (client->thread);
  956. #endif
  957. }
  958. if (control->freewheel_cb) {
  959. control->freewheel_cb (1, control->freewheel_arg);
  960. }
  961. }
  962. void
  963. jack_stop_freewheel (jack_client_t* client)
  964. {
  965. jack_client_control_t *control = client->control;
  966. if (client->engine->real_time) {
  967. #if JACK_USE_MACH_THREADS
  968. jack_acquire_real_time_scheduling (client->process_thread,
  969. client->engine->client_priority);
  970. #else
  971. jack_acquire_real_time_scheduling (client->thread,
  972. client->engine->client_priority);
  973. #endif
  974. }
  975. if (control->freewheel_cb) {
  976. control->freewheel_cb (0, control->freewheel_arg);
  977. }
  978. }
  979. static void
  980. jack_client_thread_suicide (jack_client_t* client)
  981. {
  982. if (client->on_shutdown) {
  983. jack_error ("zombified - calling shutdown handler");
  984. client->on_shutdown (client->on_shutdown_arg);
  985. } else {
  986. jack_error ("jack_client_thread zombified - exiting from JACK");
  987. jack_client_close (client);
  988. /* Need a fix : possibly make client crash if
  989. * zombified without shutdown handler
  990. */
  991. }
  992. pthread_exit (0);
  993. /*NOTREACHED*/
  994. }
  995. static int
  996. jack_client_process_events (jack_client_t* client)
  997. {
  998. jack_event_t event;
  999. char status = 0;
  1000. jack_client_control_t *control = client->control;
  1001. JSList *node;
  1002. jack_port_t* port;
  1003. if (client->pollfd[EVENT_POLL_INDEX].revents & POLLIN) {
  1004. DEBUG ("client receives an event, "
  1005. "now reading on event fd");
  1006. /* server has sent us an event. process the
  1007. * event and reply */
  1008. if (read (client->event_fd, &event, sizeof (event))
  1009. != sizeof (event)) {
  1010. jack_error ("cannot read server event (%s)",
  1011. strerror (errno));
  1012. return -1;
  1013. }
  1014. status = 0;
  1015. switch (event.type) {
  1016. case PortRegistered:
  1017. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  1018. port = node->data;
  1019. if (port->shared->id == event.x.port_id) { // Found port, update port type
  1020. port->type_info = &client->engine->port_types[port->shared->ptype_id];
  1021. }
  1022. }
  1023. if (control->port_register) {
  1024. control->port_register
  1025. (event.x.port_id, TRUE,
  1026. control->port_register_arg);
  1027. }
  1028. break;
  1029. case PortUnregistered:
  1030. if (control->port_register) {
  1031. control->port_register
  1032. (event.x.port_id, FALSE,
  1033. control->port_register_arg);
  1034. }
  1035. break;
  1036. case ClientRegistered:
  1037. if (control->client_register) {
  1038. control->client_register
  1039. (event.x.name, TRUE,
  1040. control->client_register_arg);
  1041. }
  1042. break;
  1043. case ClientUnregistered:
  1044. if (control->client_register) {
  1045. control->client_register
  1046. (event.x.name, FALSE,
  1047. control->client_register_arg);
  1048. }
  1049. break;
  1050. case GraphReordered:
  1051. status = jack_handle_reorder (client, &event);
  1052. break;
  1053. case PortConnected:
  1054. case PortDisconnected:
  1055. status = jack_client_handle_port_connection
  1056. (client, &event);
  1057. break;
  1058. case BufferSizeChange:
  1059. jack_client_invalidate_port_buffers (client);
  1060. if (control->bufsize) {
  1061. status = control->bufsize
  1062. (control->nframes,
  1063. control->bufsize_arg);
  1064. }
  1065. break;
  1066. case SampleRateChange:
  1067. if (control->srate) {
  1068. status = control->srate
  1069. (control->nframes,
  1070. control->srate_arg);
  1071. }
  1072. break;
  1073. case XRun:
  1074. if (control->xrun) {
  1075. status = control->xrun
  1076. (control->xrun_arg);
  1077. }
  1078. break;
  1079. case AttachPortSegment:
  1080. jack_attach_port_segment (client, event.y.ptid);
  1081. break;
  1082. case StartFreewheel:
  1083. jack_start_freewheel (client);
  1084. break;
  1085. case StopFreewheel:
  1086. jack_stop_freewheel (client);
  1087. break;
  1088. }
  1089. DEBUG ("client has dealt with the event, writing "
  1090. "response on event fd");
  1091. if (write (client->event_fd, &status, sizeof (status))
  1092. != sizeof (status)) {
  1093. jack_error ("cannot send event response to "
  1094. "engine (%s)", strerror (errno));
  1095. return -1;
  1096. }
  1097. }
  1098. return 0;
  1099. }
  1100. static int
  1101. jack_client_core_wait (jack_client_t* client)
  1102. {
  1103. jack_client_control_t *control = client->control;
  1104. DEBUG ("client polling on %s", client->pollmax == 2 ?
  1105. "event_fd and graph_wait_fd..." :
  1106. "event_fd only");
  1107. while (1) {
  1108. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  1109. if (errno == EINTR) {
  1110. continue;
  1111. }
  1112. jack_error ("poll failed in client (%s)",
  1113. strerror (errno));
  1114. return -1;
  1115. }
  1116. pthread_testcancel();
  1117. #ifndef JACK_USE_MACH_THREADS
  1118. /* get an accurate timestamp on waking from poll for a
  1119. * process() cycle.
  1120. */
  1121. if (client->graph_wait_fd >= 0
  1122. && client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  1123. control->awake_at = jack_get_microseconds();
  1124. }
  1125. DEBUG ("pfd[EVENT].revents = 0x%x pfd[WAIT].revents = 0x%x",
  1126. client->pollfd[EVENT_POLL_INDEX].revents,
  1127. client->pollfd[WAIT_POLL_INDEX].revents);
  1128. if (client->graph_wait_fd >= 0 &&
  1129. (client->pollfd[WAIT_POLL_INDEX].revents & ~POLLIN)) {
  1130. /* our upstream "wait" connection
  1131. closed, which either means that
  1132. an intermediate client exited, or
  1133. jackd exited, or jackd zombified
  1134. us.
  1135. we can discover the zombification
  1136. via client->control->dead, but
  1137. the other two possibilities are
  1138. impossible to identify just from
  1139. this situation. so we have to
  1140. check what we are connected to,
  1141. and act accordingly.
  1142. */
  1143. if (client->upstream_is_jackd) {
  1144. DEBUG ("WE DIE\n");
  1145. return 0;
  1146. } else {
  1147. DEBUG ("WE PUNT\n");
  1148. /* don't poll on the wait fd
  1149. * again until we get a
  1150. * GraphReordered event.
  1151. */
  1152. client->graph_wait_fd = -1;
  1153. client->pollmax = 1;
  1154. }
  1155. }
  1156. #endif
  1157. if (jack_client_process_events (client)) {
  1158. DEBUG ("event processing failed\n");
  1159. return 0;
  1160. }
  1161. if (client->graph_wait_fd >= 0 &&
  1162. (client->pollfd[WAIT_POLL_INDEX].revents & POLLIN)) {
  1163. DEBUG ("time to run process()\n");
  1164. break;
  1165. }
  1166. }
  1167. if (client->control->dead || client->pollfd[EVENT_POLL_INDEX].revents & ~POLLIN) {
  1168. DEBUG ("client appears dead or event pollfd has error status\n");
  1169. return -1;
  1170. }
  1171. return 0;
  1172. }
  1173. static int
  1174. jack_wake_next_client (jack_client_t* client)
  1175. {
  1176. char c = 0;
  1177. if (write (client->graph_next_fd, &c, sizeof (c))
  1178. != sizeof (c)) {
  1179. DEBUG("cannot write byte to fd %d", client->graph_next_fd);
  1180. jack_error ("cannot continue execution of the "
  1181. "processing graph (%s)",
  1182. strerror(errno));
  1183. return -1;
  1184. }
  1185. DEBUG ("client sent message to next stage by %" PRIu64
  1186. ", client reading on graph_wait_fd==%d",
  1187. jack_get_microseconds(), client->graph_wait_fd);
  1188. DEBUG("reading cleanup byte from pipe %d\n", client->graph_wait_fd);
  1189. if ((read (client->graph_wait_fd, &c, sizeof (c))
  1190. != sizeof (c))) {
  1191. jack_error ("cannot complete execution of the "
  1192. "processing graph (%s)",
  1193. strerror(errno));
  1194. return -1;
  1195. }
  1196. return 0;
  1197. }
  1198. static jack_nframes_t
  1199. jack_thread_first_wait (jack_client_t* client)
  1200. {
  1201. if (jack_client_core_wait (client)) {
  1202. return 0;
  1203. }
  1204. return client->control->nframes;
  1205. }
  1206. jack_nframes_t
  1207. jack_thread_wait (jack_client_t* client, int status)
  1208. {
  1209. jack_client_control_t *control = client->control;
  1210. /* SECTION ONE: HOUSEKEEPING/CLEANUP FROM LAST DATA PROCESSING */
  1211. /* housekeeping/cleanup after data processing */
  1212. if (status == 0 && control->timebase_cb) {
  1213. jack_call_timebase_master (client);
  1214. }
  1215. /* end preemption checking */
  1216. CHECK_PREEMPTION (client->engine, FALSE);
  1217. control->finished_at = jack_get_microseconds();
  1218. /* wake the next client in the chain (could be the server),
  1219. and check if we were killed during the process
  1220. cycle.
  1221. */
  1222. if (jack_wake_next_client (client)) {
  1223. DEBUG("client cannot wake next, or is dead\n");
  1224. return 0;
  1225. }
  1226. if (status || client->control->dead || !client->engine->engine_ok) {
  1227. return 0;
  1228. }
  1229. /* SECTION TWO: WAIT FOR NEXT DATA PROCESSING TIME */
  1230. if (jack_client_core_wait (client)) {
  1231. return 0;
  1232. }
  1233. /* SECTION THREE: START NEXT DATA PROCESSING TIME */
  1234. /* Time to do data processing */
  1235. control->state = Running;
  1236. /* begin preemption checking */
  1237. CHECK_PREEMPTION (client->engine, TRUE);
  1238. if (control->sync_cb)
  1239. jack_call_sync_client (client);
  1240. return control->nframes;
  1241. }
  1242. static void *
  1243. jack_client_thread (void *arg)
  1244. {
  1245. jack_client_t *client = (jack_client_t *) arg;
  1246. jack_client_control_t *control = client->control;
  1247. pthread_mutex_lock (&client_lock);
  1248. client->thread_ok = TRUE;
  1249. client->thread_id = pthread_self();
  1250. pthread_cond_signal (&client_ready);
  1251. pthread_mutex_unlock (&client_lock);
  1252. control->pid = getpid();
  1253. control->pgrp = getpgrp();
  1254. DEBUG ("client thread is now running");
  1255. if (control->thread_init) {
  1256. DEBUG ("calling client thread init callback");
  1257. control->thread_init (control->thread_init_arg);
  1258. }
  1259. /* wait for first wakeup from server */
  1260. if (jack_thread_first_wait (client) == control->nframes) {
  1261. /* now run till we're done */
  1262. if (control->process) {
  1263. /* run process callback, then wait... ad-infinitum */
  1264. while (jack_thread_wait (client,
  1265. control->process (control->nframes,
  1266. control->process_arg)) ==
  1267. control->nframes)
  1268. ;
  1269. } else {
  1270. /* no process handling but still need to process events */
  1271. while (jack_thread_wait (client, 0) == control->nframes)
  1272. ;
  1273. }
  1274. }
  1275. jack_client_thread_suicide (client);
  1276. /*NOTREACHED*/
  1277. return (void *) 0;
  1278. }
  1279. #ifdef JACK_USE_MACH_THREADS
  1280. /* real-time thread : separated from the normal client thread, it will
  1281. * communicate with the server using fast mach RPC mechanism */
  1282. static void *
  1283. jack_client_process_thread (void *arg)
  1284. {
  1285. jack_client_t *client = (jack_client_t *) arg;
  1286. jack_client_control_t *control = client->control;
  1287. int err = 0;
  1288. if (client->control->thread_init) {
  1289. /* this means that the init callback will be called twice -taybin*/
  1290. DEBUG ("calling client thread init callback");
  1291. client->control->thread_init (client->control->thread_init_arg);
  1292. }
  1293. client->control->pid = getpid();
  1294. DEBUG ("client process thread is now running");
  1295. client->rt_thread_ok = TRUE;
  1296. while (err == 0) {
  1297. if (jack_client_suspend(client) < 0) {
  1298. jack_error ("jack_client_process_thread :resume error");
  1299. goto zombie;
  1300. }
  1301. control->awake_at = jack_get_microseconds();
  1302. DEBUG ("client resumed");
  1303. control->state = Running;
  1304. if (control->sync_cb)
  1305. jack_call_sync_client (client);
  1306. if (control->process) {
  1307. if (control->process (control->nframes,
  1308. control->process_arg) == 0) {
  1309. control->state = Finished;
  1310. }
  1311. } else {
  1312. control->state = Finished;
  1313. }
  1314. if (control->timebase_cb)
  1315. jack_call_timebase_master (client);
  1316. control->finished_at = jack_get_microseconds();
  1317. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  1318. control->finished_at,
  1319. ((float)(control->finished_at - control->awake_at)));
  1320. /* check if we were killed during the process cycle
  1321. * (or whatever) */
  1322. if (client->control->dead) {
  1323. jack_error ("jack_client_process_thread: "
  1324. "client->control->dead");
  1325. goto zombie;
  1326. }
  1327. DEBUG("process cycle fully complete\n");
  1328. }
  1329. return (void *) ((intptr_t)err);
  1330. zombie:
  1331. jack_error ("jack_client_process_thread : zombified");
  1332. client->rt_thread_ok = FALSE;
  1333. if (client->on_shutdown) {
  1334. jack_error ("zombified - calling shutdown handler");
  1335. client->on_shutdown (client->on_shutdown_arg);
  1336. } else {
  1337. jack_error ("jack_client_process_thread zombified - exiting from JACK");
  1338. /* Need a fix : possibly make client crash if
  1339. * zombified without shutdown handler */
  1340. jack_client_close (client);
  1341. }
  1342. pthread_exit (0);
  1343. /*NOTREACHED*/
  1344. return 0;
  1345. }
  1346. #endif /* JACK_USE_MACH_THREADS */
  1347. static int
  1348. jack_start_thread (jack_client_t *client)
  1349. {
  1350. if (client->engine->real_time) {
  1351. #ifdef USE_MLOCK
  1352. if (client->engine->do_mlock
  1353. && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1354. jack_error ("cannot lock down memory for RT thread "
  1355. "(%s)", strerror (errno));
  1356. #ifdef ENSURE_MLOCK
  1357. return -1;
  1358. #endif /* ENSURE_MLOCK */
  1359. }
  1360. if (client->engine->do_munlock) {
  1361. cleanup_mlock ();
  1362. }
  1363. #endif /* USE_MLOCK */
  1364. }
  1365. #ifdef JACK_USE_MACH_THREADS
  1366. /* Stephane Letz : letz@grame.fr
  1367. On MacOSX, the normal thread does not need to be real-time.
  1368. */
  1369. if (jack_client_create_thread (client,
  1370. &client->thread,
  1371. client->engine->client_priority,
  1372. FALSE,
  1373. jack_client_thread, client)) {
  1374. return -1;
  1375. }
  1376. #else
  1377. if (jack_client_create_thread (client,
  1378. &client->thread,
  1379. client->engine->client_priority,
  1380. client->engine->real_time,
  1381. jack_client_thread, client)) {
  1382. return -1;
  1383. }
  1384. #endif
  1385. #ifdef JACK_USE_MACH_THREADS
  1386. /* a secondary thread that runs the process callback and uses
  1387. ultra-fast Mach primitives for inter-thread signalling.
  1388. XXX in a properly structured JACK, there would be no
  1389. need for this, because we would have client wake up
  1390. methods that encapsulated the underlying mechanism
  1391. used.
  1392. */
  1393. if (jack_client_create_thread(client,
  1394. &client->process_thread,
  1395. client->engine->client_priority,
  1396. client->engine->real_time,
  1397. jack_client_process_thread, client)) {
  1398. return -1;
  1399. }
  1400. #endif /* JACK_USE_MACH_THREADS */
  1401. return 0;
  1402. }
  1403. int
  1404. jack_activate (jack_client_t *client)
  1405. {
  1406. jack_request_t req;
  1407. /* we need to scribble on our stack to ensure that its memory
  1408. * pages are actually mapped (more important for mlockall(2)
  1409. * usage in jack_start_thread())
  1410. */
  1411. char buf[JACK_THREAD_STACK_TOUCH];
  1412. int i;
  1413. for (i = 0; i < JACK_THREAD_STACK_TOUCH; i++) {
  1414. buf[i] = (char) (i & 0xff);
  1415. }
  1416. if (client->control->type == ClientInternal ||
  1417. client->control->type == ClientDriver) {
  1418. goto startit;
  1419. }
  1420. /* get the pid of the client process to pass it to engine */
  1421. client->control->pid = getpid ();
  1422. #ifdef USE_CAPABILITIES
  1423. if (client->engine->has_capabilities != 0 &&
  1424. client->control->pid != 0 && client->engine->real_time != 0) {
  1425. /* we need to ask the engine for realtime capabilities
  1426. before trying to start the realtime thread
  1427. */
  1428. req.type = SetClientCapabilities;
  1429. req.x.client_id = client->control->id;
  1430. req.x.cap_pid = client->control->pid;
  1431. jack_client_deliver_request (client, &req);
  1432. if (req.status) {
  1433. /* what to do? engine is running realtime, it
  1434. is using capabilities and has them
  1435. (otherwise we would not get an error
  1436. return) but for some reason it could not
  1437. give the client the required capabilities.
  1438. For now, leave the client so that it
  1439. still runs, albeit non-realtime.
  1440. */
  1441. jack_error ("could not receive realtime capabilities, "
  1442. "client will run non-realtime");
  1443. }
  1444. }
  1445. #endif /* USE_CAPABILITIES */
  1446. if (client->first_active) {
  1447. pthread_mutex_init (&client_lock, NULL);
  1448. pthread_cond_init (&client_ready, NULL);
  1449. pthread_mutex_lock (&client_lock);
  1450. if (jack_start_thread (client)) {
  1451. pthread_mutex_unlock (&client_lock);
  1452. return -1;
  1453. }
  1454. pthread_cond_wait (&client_ready, &client_lock);
  1455. pthread_mutex_unlock (&client_lock);
  1456. if (!client->thread_ok) {
  1457. jack_error ("could not start client thread");
  1458. return -1;
  1459. }
  1460. client->first_active = FALSE;
  1461. }
  1462. startit:
  1463. req.type = ActivateClient;
  1464. req.x.client_id = client->control->id;
  1465. return jack_client_deliver_request (client, &req);
  1466. }
  1467. int
  1468. jack_deactivate (jack_client_t *client)
  1469. {
  1470. jack_request_t req;
  1471. int rc = ESRCH; /* already shut down */
  1472. if (client && client->control) { /* not shut down? */
  1473. rc = 0;
  1474. if (client->control->active) { /* still active? */
  1475. req.type = DeactivateClient;
  1476. req.x.client_id = client->control->id;
  1477. rc = jack_client_deliver_request (client, &req);
  1478. }
  1479. }
  1480. return rc;
  1481. }
  1482. int
  1483. jack_client_close (jack_client_t *client)
  1484. {
  1485. JSList *node;
  1486. void *status;
  1487. int rc;
  1488. rc = jack_deactivate (client);
  1489. if (rc == ESRCH) { /* already shut down? */
  1490. return rc;
  1491. }
  1492. if (client->control->type == ClientExternal) {
  1493. #if JACK_USE_MACH_THREADS
  1494. if (client->rt_thread_ok) {
  1495. // MacOSX pthread_cancel not implemented in
  1496. // Darwin 5.5, 6.4
  1497. mach_port_t machThread =
  1498. pthread_mach_thread_np (client->process_thread);
  1499. thread_terminate (machThread);
  1500. }
  1501. #endif
  1502. /* stop the thread that communicates with the jack
  1503. * server, only if it was actually running
  1504. */
  1505. if (client->thread_ok){
  1506. pthread_cancel (client->thread);
  1507. pthread_join (client->thread, &status);
  1508. }
  1509. if (client->control) {
  1510. jack_release_shm (&client->control_shm);
  1511. client->control = NULL;
  1512. }
  1513. if (client->engine) {
  1514. jack_release_shm (&client->engine_shm);
  1515. client->engine = NULL;
  1516. }
  1517. if (client->port_segment) {
  1518. jack_port_type_id_t ptid;
  1519. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1520. jack_release_shm (&client->port_segment[ptid]);
  1521. }
  1522. free (client->port_segment);
  1523. client->port_segment = NULL;
  1524. }
  1525. #ifndef JACK_USE_MACH_THREADS
  1526. if (client->graph_wait_fd) {
  1527. close (client->graph_wait_fd);
  1528. }
  1529. if (client->graph_next_fd) {
  1530. close (client->graph_next_fd);
  1531. }
  1532. #endif
  1533. close (client->event_fd);
  1534. if (shutdown (client->request_fd, SHUT_RDWR)) {
  1535. jack_error ("could not shutdown client request socket");
  1536. }
  1537. close (client->request_fd);
  1538. }
  1539. for (node = client->ports; node; node = jack_slist_next (node)) {
  1540. free (node->data);
  1541. }
  1542. jack_slist_free (client->ports);
  1543. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  1544. free (node->data);
  1545. }
  1546. jack_slist_free (client->ports_ext);
  1547. jack_client_free (client);
  1548. return rc;
  1549. }
  1550. int
  1551. jack_is_realtime (jack_client_t *client)
  1552. {
  1553. return client->engine->real_time;
  1554. }
  1555. jack_nframes_t
  1556. jack_get_buffer_size (jack_client_t *client)
  1557. {
  1558. return client->engine->buffer_size;
  1559. }
  1560. int
  1561. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1562. {
  1563. #ifdef DO_BUFFER_RESIZE
  1564. jack_request_t req;
  1565. req.type = SetBufferSize;
  1566. req.x.nframes = nframes;
  1567. return jack_client_deliver_request (client, &req);
  1568. #else
  1569. return ENOSYS;
  1570. #endif /* DO_BUFFER_RESIZE */
  1571. }
  1572. int
  1573. jack_connect (jack_client_t *client, const char *source_port,
  1574. const char *destination_port)
  1575. {
  1576. jack_request_t req;
  1577. req.type = ConnectPorts;
  1578. snprintf (req.x.connect.source_port,
  1579. sizeof (req.x.connect.source_port), "%s", source_port);
  1580. snprintf (req.x.connect.destination_port,
  1581. sizeof (req.x.connect.destination_port),
  1582. "%s", destination_port);
  1583. return jack_client_deliver_request (client, &req);
  1584. }
  1585. int
  1586. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1587. {
  1588. jack_request_t req;
  1589. pthread_mutex_lock (&port->connection_lock);
  1590. if (port->connections == NULL) {
  1591. pthread_mutex_unlock (&port->connection_lock);
  1592. return 0;
  1593. }
  1594. pthread_mutex_unlock (&port->connection_lock);
  1595. req.type = DisconnectPort;
  1596. req.x.port_info.port_id = port->shared->id;
  1597. return jack_client_deliver_request (client, &req);
  1598. }
  1599. int
  1600. jack_disconnect (jack_client_t *client, const char *source_port,
  1601. const char *destination_port)
  1602. {
  1603. jack_request_t req;
  1604. req.type = DisconnectPorts;
  1605. snprintf (req.x.connect.source_port,
  1606. sizeof (req.x.connect.source_port), "%s", source_port);
  1607. snprintf (req.x.connect.destination_port,
  1608. sizeof (req.x.connect.destination_port),
  1609. "%s", destination_port);
  1610. return jack_client_deliver_request (client, &req);
  1611. }
  1612. void
  1613. jack_set_error_function (void (*func) (const char *))
  1614. {
  1615. jack_error_callback = func;
  1616. }
  1617. int
  1618. jack_set_graph_order_callback (jack_client_t *client,
  1619. JackGraphOrderCallback callback, void *arg)
  1620. {
  1621. if (client->control->active) {
  1622. jack_error ("You cannot set callbacks on an active client.");
  1623. return -1;
  1624. }
  1625. client->control->graph_order = callback;
  1626. client->control->graph_order_arg = arg;
  1627. return 0;
  1628. }
  1629. int jack_set_xrun_callback (jack_client_t *client,
  1630. JackXRunCallback callback, void *arg)
  1631. {
  1632. if (client->control->active) {
  1633. jack_error ("You cannot set callbacks on an active client.");
  1634. return -1;
  1635. }
  1636. client->control->xrun = callback;
  1637. client->control->xrun_arg = arg;
  1638. return 0;
  1639. }
  1640. int
  1641. jack_set_process_callback (jack_client_t *client,
  1642. JackProcessCallback callback, void *arg)
  1643. {
  1644. if (client->control->active) {
  1645. jack_error ("You cannot set callbacks on an active client.");
  1646. return -1;
  1647. }
  1648. client->control->process_arg = arg;
  1649. client->control->process = callback;
  1650. return 0;
  1651. }
  1652. int
  1653. jack_set_thread_init_callback (jack_client_t *client,
  1654. JackThreadInitCallback callback, void *arg)
  1655. {
  1656. if (client->control->active) {
  1657. jack_error ("You cannot set callbacks on an active client.");
  1658. return -1;
  1659. }
  1660. client->control->thread_init_arg = arg;
  1661. client->control->thread_init = callback;
  1662. return 0;
  1663. }
  1664. int
  1665. jack_set_freewheel_callback (jack_client_t *client,
  1666. JackFreewheelCallback callback, void *arg)
  1667. {
  1668. if (client->control->active) {
  1669. jack_error ("You cannot set callbacks on an active client.");
  1670. return -1;
  1671. }
  1672. client->control->freewheel_arg = arg;
  1673. client->control->freewheel_cb = callback;
  1674. return 0;
  1675. }
  1676. int
  1677. jack_set_buffer_size_callback (jack_client_t *client,
  1678. JackBufferSizeCallback callback, void *arg)
  1679. {
  1680. client->control->bufsize_arg = arg;
  1681. client->control->bufsize = callback;
  1682. return 0;
  1683. }
  1684. int
  1685. jack_set_port_registration_callback(jack_client_t *client,
  1686. JackPortRegistrationCallback callback,
  1687. void *arg)
  1688. {
  1689. if (client->control->active) {
  1690. jack_error ("You cannot set callbacks on an active client.");
  1691. return -1;
  1692. }
  1693. client->control->port_register_arg = arg;
  1694. client->control->port_register = callback;
  1695. return 0;
  1696. }
  1697. int
  1698. jack_set_port_connect_callback(jack_client_t *client,
  1699. JackPortConnectCallback callback,
  1700. void *arg)
  1701. {
  1702. if (client->control->active) {
  1703. jack_error ("You cannot set callbacks on an active client.");
  1704. return -1;
  1705. }
  1706. client->control->port_connect_arg = arg;
  1707. client->control->port_connect = callback;
  1708. return 0;
  1709. }
  1710. int
  1711. jack_set_client_registration_callback(jack_client_t *client,
  1712. JackClientRegistrationCallback callback,
  1713. void *arg)
  1714. {
  1715. if (client->control->active) {
  1716. jack_error ("You cannot set callbacks on an active client.");
  1717. return -1;
  1718. }
  1719. client->control->client_register_arg = arg;
  1720. client->control->client_register = callback;
  1721. return 0;
  1722. }
  1723. int
  1724. jack_get_process_done_fd (jack_client_t *client)
  1725. {
  1726. return client->graph_next_fd;
  1727. }
  1728. void
  1729. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1730. {
  1731. client->on_shutdown = function;
  1732. client->on_shutdown_arg = arg;
  1733. }
  1734. const char **
  1735. jack_get_ports (jack_client_t *client,
  1736. const char *port_name_pattern,
  1737. const char *type_name_pattern,
  1738. unsigned long flags)
  1739. {
  1740. jack_control_t *engine;
  1741. const char **matching_ports;
  1742. unsigned long match_cnt;
  1743. jack_port_shared_t *psp;
  1744. unsigned long i;
  1745. regex_t port_regex;
  1746. regex_t type_regex;
  1747. int matching;
  1748. engine = client->engine;
  1749. if (port_name_pattern && port_name_pattern[0]) {
  1750. regcomp (&port_regex, port_name_pattern,
  1751. REG_EXTENDED|REG_NOSUB);
  1752. }
  1753. if (type_name_pattern && type_name_pattern[0]) {
  1754. regcomp (&type_regex, type_name_pattern,
  1755. REG_EXTENDED|REG_NOSUB);
  1756. }
  1757. psp = engine->ports;
  1758. match_cnt = 0;
  1759. matching_ports = (const char **)
  1760. malloc (sizeof (char *) * engine->port_max);
  1761. for (i = 0; i < engine->port_max; i++) {
  1762. matching = 1;
  1763. if (!psp[i].in_use) {
  1764. continue;
  1765. }
  1766. if (flags) {
  1767. if ((psp[i].flags & flags) != flags) {
  1768. matching = 0;
  1769. }
  1770. }
  1771. if (matching && port_name_pattern && port_name_pattern[0]) {
  1772. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1773. matching = 0;
  1774. }
  1775. }
  1776. if (matching && type_name_pattern && type_name_pattern[0]) {
  1777. jack_port_type_id_t ptid = psp[i].ptype_id;
  1778. if (regexec (&type_regex,
  1779. engine->port_types[ptid].type_name,
  1780. 0, NULL, 0)) {
  1781. matching = 0;
  1782. }
  1783. }
  1784. if (matching) {
  1785. matching_ports[match_cnt++] = psp[i].name;
  1786. }
  1787. }
  1788. if (port_name_pattern && port_name_pattern[0]) {
  1789. regfree (&port_regex);
  1790. }
  1791. if (type_name_pattern && type_name_pattern[0]) {
  1792. regfree (&type_regex);
  1793. }
  1794. matching_ports[match_cnt] = 0;
  1795. if (match_cnt == 0) {
  1796. free (matching_ports);
  1797. matching_ports = 0;
  1798. }
  1799. return matching_ports;
  1800. }
  1801. float
  1802. jack_cpu_load (jack_client_t *client)
  1803. {
  1804. return client->engine->cpu_load;
  1805. }
  1806. float
  1807. jack_get_xrun_delayed_usecs (jack_client_t *client)
  1808. {
  1809. return client->engine->xrun_delayed_usecs;
  1810. }
  1811. float
  1812. jack_get_max_delayed_usecs (jack_client_t *client)
  1813. {
  1814. return client->engine->max_delayed_usecs;
  1815. }
  1816. void
  1817. jack_reset_max_delayed_usecs (jack_client_t *client)
  1818. {
  1819. client->engine->max_delayed_usecs = 0.0f;
  1820. }
  1821. pthread_t
  1822. jack_client_thread_id (jack_client_t *client)
  1823. {
  1824. return client->thread_id;
  1825. }
  1826. int
  1827. jack_client_name_size(void)
  1828. {
  1829. return JACK_CLIENT_NAME_SIZE;
  1830. }
  1831. int
  1832. jack_port_name_size(void)
  1833. {
  1834. return JACK_PORT_NAME_SIZE;
  1835. }
  1836. int
  1837. jack_port_type_size(void)
  1838. {
  1839. return JACK_PORT_TYPE_SIZE;
  1840. }