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.

2368 lines
53KB

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