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.

2472 lines
56KB

  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. 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. jack_varargs_t va; /* variable arguments */
  771. int req_fd = -1;
  772. int ev_fd = -1;
  773. jack_client_connect_result_t res;
  774. jack_client_t *client;
  775. jack_port_type_id_t ptid;
  776. jack_status_t my_status;
  777. if (status == NULL) /* no status from caller? */
  778. status = &my_status; /* use local status word */
  779. *status = 0;
  780. /* validate parameters */
  781. if ((options & ~JackOpenOptions)) {
  782. *status |= (JackFailure|JackInvalidOption);
  783. return NULL;
  784. }
  785. /* parse variable arguments */
  786. if (ap)
  787. jack_varargs_parse(options, ap, &va);
  788. else
  789. jack_varargs_init(&va);
  790. /* External clients need to know where the tmpdir used for
  791. communication with the server lives
  792. */
  793. if (jack_get_tmpdir ()) {
  794. *status |= JackFailure;
  795. return NULL;
  796. }
  797. /* External clients need this initialized. It is already set
  798. * up in the server's address space for internal clients.
  799. */
  800. jack_init_time ();
  801. if (jack_request_client (ClientExternal, client_name, options, status,
  802. &va, &res, &req_fd)) {
  803. return NULL;
  804. }
  805. /* Allocate the jack_client_t structure in local memory.
  806. * Shared memory is not accessible yet. */
  807. client = jack_client_alloc ();
  808. strcpy (client->name, res.name);
  809. strcpy (client->fifo_prefix, res.fifo_prefix);
  810. client->request_fd = req_fd;
  811. client->pollfd[EVENT_POLL_INDEX].events =
  812. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  813. #ifndef JACK_USE_MACH_THREADS
  814. client->pollfd[WAIT_POLL_INDEX].events =
  815. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  816. #endif
  817. /* Don't access shared memory until server connected. */
  818. if (jack_initialize_shm (va.server_name)) {
  819. jack_error ("Unable to initialize shared memory.");
  820. *status |= (JackFailure|JackShmFailure);
  821. goto fail;
  822. }
  823. /* attach the engine control/info block */
  824. client->engine_shm = res.engine_shm;
  825. if (jack_attach_shm (&client->engine_shm)) {
  826. jack_error ("cannot attached engine control shared memory"
  827. " segment");
  828. goto fail;
  829. }
  830. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  831. /* initialize clock source as early as possible */
  832. jack_set_clock_source (client->engine->clock_source);
  833. /* now attach the client control block */
  834. client->control_shm = res.client_shm;
  835. if (jack_attach_shm (&client->control_shm)) {
  836. jack_error ("cannot attached client control shared memory"
  837. " segment");
  838. goto fail;
  839. }
  840. client->control = (jack_client_control_t *)
  841. jack_shm_addr (&client->control_shm);
  842. /* Nobody else needs to access this shared memory any more, so
  843. * destroy it. Because we have it attached, it won't vanish
  844. * till we exit (and release it).
  845. */
  846. jack_destroy_shm (&client->control_shm);
  847. client->n_port_types = client->engine->n_port_types;
  848. client->port_segment = (jack_shm_info_t *)
  849. malloc (sizeof (jack_shm_info_t) * client->n_port_types);
  850. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  851. client->port_segment[ptid].index =
  852. client->engine->port_types[ptid].shm_registry_index;
  853. client->port_segment[ptid].attached_at = MAP_FAILED;
  854. jack_attach_port_segment (client, ptid);
  855. }
  856. /* set up the client so that it does the right thing for an
  857. * external client
  858. */
  859. client->control->deliver_request = oop_client_deliver_request;
  860. client->control->deliver_arg = client;
  861. if ((ev_fd = server_event_connect (client, va.server_name)) < 0) {
  862. goto fail;
  863. }
  864. client->event_fd = ev_fd;
  865. #ifdef JACK_USE_MACH_THREADS
  866. /* specific resources for server/client real-time thread
  867. * communication */
  868. client->clienttask = mach_task_self();
  869. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  870. jack_error ("Can't find bootstrap port");
  871. goto fail;
  872. }
  873. if (allocate_mach_clientport(client, res.portnum) < 0) {
  874. jack_error("Can't allocate mach port");
  875. goto fail;
  876. };
  877. #endif /* JACK_USE_MACH_THREADS */
  878. return client;
  879. fail:
  880. if (client->engine) {
  881. jack_release_shm (&client->engine_shm);
  882. client->engine = 0;
  883. }
  884. if (client->control) {
  885. jack_release_shm (&client->control_shm);
  886. client->control = 0;
  887. }
  888. if (req_fd >= 0) {
  889. close (req_fd);
  890. }
  891. if (ev_fd >= 0) {
  892. close (ev_fd);
  893. }
  894. free (client);
  895. return NULL;
  896. }
  897. jack_client_t* jack_client_open(const char* ext_client_name, jack_options_t options, jack_status_t* status, ...)
  898. {
  899. va_list ap;
  900. va_start(ap, status);
  901. jack_client_t* res = jack_client_open_aux(ext_client_name, options, status, ap);
  902. va_end(ap);
  903. return res;
  904. }
  905. jack_client_t *
  906. jack_client_new (const char *client_name)
  907. {
  908. jack_options_t options = JackUseExactName;
  909. if (getenv("JACK_START_SERVER") == NULL)
  910. options |= JackNoStartServer;
  911. return jack_client_open_aux (client_name, options, NULL, NULL);
  912. }
  913. char *
  914. jack_get_client_name (jack_client_t *client)
  915. {
  916. return client->name;
  917. }
  918. int
  919. jack_internal_client_new (const char *client_name,
  920. const char *so_name, const char *so_data)
  921. {
  922. jack_client_connect_result_t res;
  923. int req_fd;
  924. jack_varargs_t va;
  925. jack_status_t status;
  926. jack_options_t options = JackUseExactName;
  927. if (getenv("JACK_START_SERVER") == NULL)
  928. options |= JackNoStartServer;
  929. jack_varargs_init (&va);
  930. va.load_name = (char *) so_name;
  931. va.load_init = (char *) so_data;
  932. return jack_request_client (ClientInternal, client_name,
  933. options, &status, &va, &res, &req_fd);
  934. }
  935. char *
  936. jack_default_server_name (void)
  937. {
  938. char *server_name;
  939. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  940. server_name = "default";
  941. return server_name;
  942. }
  943. /* returns the name of the per-user subdirectory of jack_tmpdir */
  944. char *
  945. jack_user_dir (void)
  946. {
  947. static char user_dir[PATH_MAX+1] = "";
  948. /* format the path name on the first call */
  949. if (user_dir[0] == '\0') {
  950. if (getenv ("JACK_PROMISCUOUS_SERVER")) {
  951. snprintf (user_dir, sizeof (user_dir), "%s/jack",
  952. jack_tmpdir);
  953. } else {
  954. snprintf (user_dir, sizeof (user_dir), "%s/jack-%d",
  955. jack_tmpdir, getuid ());
  956. }
  957. }
  958. return user_dir;
  959. }
  960. /* returns the name of the per-server subdirectory of jack_user_dir() */
  961. char *
  962. jack_server_dir (const char *server_name, char *server_dir)
  963. {
  964. /* format the path name into the suppled server_dir char array,
  965. * assuming that server_dir is at least as large as PATH_MAX+1 */
  966. snprintf (server_dir, PATH_MAX+1, "%s/%s",
  967. jack_user_dir (), server_name);
  968. return server_dir;
  969. }
  970. void
  971. jack_internal_client_close (const char *client_name)
  972. {
  973. jack_client_connect_request_t req;
  974. int fd;
  975. char *server_name = jack_default_server_name ();
  976. req.load = FALSE;
  977. snprintf (req.name, sizeof (req.name), "%s", client_name);
  978. if ((fd = server_connect (server_name)) < 0) {
  979. return;
  980. }
  981. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  982. jack_error ("cannot deliver ClientUnload request to JACK "
  983. "server.");
  984. }
  985. /* no response to this request */
  986. close (fd);
  987. return;
  988. }
  989. int
  990. jack_recompute_total_latencies (jack_client_t* client)
  991. {
  992. jack_request_t request;
  993. request.type = RecomputeTotalLatencies;
  994. return jack_client_deliver_request (client, &request);
  995. }
  996. int
  997. jack_recompute_total_latency (jack_client_t* client, jack_port_t* port)
  998. {
  999. jack_request_t request;
  1000. request.type = RecomputeTotalLatency;
  1001. request.x.port_info.port_id = port->shared->id;
  1002. return jack_client_deliver_request (client, &request);
  1003. }
  1004. int
  1005. jack_set_freewheel (jack_client_t* client, int onoff)
  1006. {
  1007. jack_request_t request;
  1008. request.type = onoff ? FreeWheel : StopFreeWheel;
  1009. return jack_client_deliver_request (client, &request);
  1010. }
  1011. void
  1012. jack_start_freewheel (jack_client_t* client)
  1013. {
  1014. jack_client_control_t *control = client->control;
  1015. if (client->engine->real_time) {
  1016. #if JACK_USE_MACH_THREADS
  1017. jack_drop_real_time_scheduling (client->process_thread);
  1018. #else
  1019. jack_drop_real_time_scheduling (client->thread);
  1020. #endif
  1021. }
  1022. if (control->freewheel_cb) {
  1023. control->freewheel_cb (1, control->freewheel_arg);
  1024. }
  1025. }
  1026. void
  1027. jack_stop_freewheel (jack_client_t* client)
  1028. {
  1029. jack_client_control_t *control = client->control;
  1030. if (client->engine->real_time) {
  1031. #if JACK_USE_MACH_THREADS
  1032. jack_acquire_real_time_scheduling (client->process_thread,
  1033. client->engine->client_priority);
  1034. #else
  1035. jack_acquire_real_time_scheduling (client->thread,
  1036. client->engine->client_priority);
  1037. #endif
  1038. }
  1039. if (control->freewheel_cb) {
  1040. control->freewheel_cb (0, control->freewheel_arg);
  1041. }
  1042. }
  1043. static void
  1044. jack_client_thread_suicide (jack_client_t* client)
  1045. {
  1046. if (client->on_shutdown) {
  1047. jack_error ("zombified - calling shutdown handler");
  1048. client->on_shutdown (client->on_shutdown_arg);
  1049. } else {
  1050. jack_error ("jack_client_thread zombified - exiting from JACK");
  1051. jack_client_close_aux (client);
  1052. /* Need a fix : possibly make client crash if
  1053. * zombified without shutdown handler
  1054. */
  1055. }
  1056. pthread_exit (0);
  1057. /*NOTREACHED*/
  1058. }
  1059. static int
  1060. jack_client_process_events (jack_client_t* client)
  1061. {
  1062. jack_event_t event;
  1063. char status = 0;
  1064. jack_client_control_t *control = client->control;
  1065. JSList *node;
  1066. jack_port_t* port;
  1067. if (client->pollfd[EVENT_POLL_INDEX].revents & POLLIN) {
  1068. DEBUG ("client receives an event, "
  1069. "now reading on event fd");
  1070. /* server has sent us an event. process the
  1071. * event and reply */
  1072. if (read (client->event_fd, &event, sizeof (event))
  1073. != sizeof (event)) {
  1074. jack_error ("cannot read server event (%s)",
  1075. strerror (errno));
  1076. return -1;
  1077. }
  1078. status = 0;
  1079. switch (event.type) {
  1080. case PortRegistered:
  1081. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  1082. port = node->data;
  1083. if (port->shared->id == event.x.port_id) { // Found port, update port type
  1084. port->type_info = &client->engine->port_types[port->shared->ptype_id];
  1085. }
  1086. }
  1087. if (control->port_register) {
  1088. control->port_register
  1089. (event.x.port_id, TRUE,
  1090. control->port_register_arg);
  1091. }
  1092. break;
  1093. case PortUnregistered:
  1094. if (control->port_register) {
  1095. control->port_register
  1096. (event.x.port_id, FALSE,
  1097. control->port_register_arg);
  1098. }
  1099. break;
  1100. case ClientRegistered:
  1101. if (control->client_register) {
  1102. control->client_register
  1103. (event.x.name, TRUE,
  1104. control->client_register_arg);
  1105. }
  1106. break;
  1107. case ClientUnregistered:
  1108. if (control->client_register) {
  1109. control->client_register
  1110. (event.x.name, FALSE,
  1111. control->client_register_arg);
  1112. }
  1113. break;
  1114. case GraphReordered:
  1115. status = jack_handle_reorder (client, &event);
  1116. break;
  1117. case PortConnected:
  1118. case PortDisconnected:
  1119. status = jack_client_handle_port_connection
  1120. (client, &event);
  1121. break;
  1122. case BufferSizeChange:
  1123. jack_client_invalidate_port_buffers (client);
  1124. if (control->bufsize) {
  1125. status = control->bufsize
  1126. (control->nframes,
  1127. control->bufsize_arg);
  1128. }
  1129. break;
  1130. case SampleRateChange:
  1131. if (control->srate) {
  1132. status = control->srate
  1133. (control->nframes,
  1134. control->srate_arg);
  1135. }
  1136. break;
  1137. case XRun:
  1138. if (control->xrun) {
  1139. status = control->xrun
  1140. (control->xrun_arg);
  1141. }
  1142. break;
  1143. case AttachPortSegment:
  1144. jack_attach_port_segment (client, event.y.ptid);
  1145. break;
  1146. case StartFreewheel:
  1147. jack_start_freewheel (client);
  1148. break;
  1149. case StopFreewheel:
  1150. jack_stop_freewheel (client);
  1151. break;
  1152. }
  1153. DEBUG ("client has dealt with the event, writing "
  1154. "response on event fd");
  1155. if (write (client->event_fd, &status, sizeof (status))
  1156. != sizeof (status)) {
  1157. jack_error ("cannot send event response to "
  1158. "engine (%s)", strerror (errno));
  1159. return -1;
  1160. }
  1161. }
  1162. return 0;
  1163. }
  1164. static int
  1165. jack_client_core_wait (jack_client_t* client)
  1166. {
  1167. jack_client_control_t *control = client->control;
  1168. DEBUG ("client polling on %s", client->pollmax == 2 ?
  1169. "event_fd and graph_wait_fd..." :
  1170. "event_fd only");
  1171. while (1) {
  1172. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  1173. if (errno == EINTR) {
  1174. continue;
  1175. }
  1176. jack_error ("poll failed in client (%s)",
  1177. strerror (errno));
  1178. return -1;
  1179. }
  1180. pthread_testcancel();
  1181. #ifndef JACK_USE_MACH_THREADS
  1182. /* get an accurate timestamp on waking from poll for a
  1183. * process() cycle.
  1184. */
  1185. if (client->graph_wait_fd >= 0
  1186. && client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  1187. control->awake_at = jack_get_microseconds();
  1188. }
  1189. DEBUG ("pfd[EVENT].revents = 0x%x pfd[WAIT].revents = 0x%x",
  1190. client->pollfd[EVENT_POLL_INDEX].revents,
  1191. client->pollfd[WAIT_POLL_INDEX].revents);
  1192. if (client->graph_wait_fd >= 0 &&
  1193. (client->pollfd[WAIT_POLL_INDEX].revents & ~POLLIN)) {
  1194. /* our upstream "wait" connection
  1195. closed, which either means that
  1196. an intermediate client exited, or
  1197. jackd exited, or jackd zombified
  1198. us.
  1199. we can discover the zombification
  1200. via client->control->dead, but
  1201. the other two possibilities are
  1202. impossible to identify just from
  1203. this situation. so we have to
  1204. check what we are connected to,
  1205. and act accordingly.
  1206. */
  1207. if (client->upstream_is_jackd) {
  1208. DEBUG ("WE DIE\n");
  1209. return 0;
  1210. } else {
  1211. DEBUG ("WE PUNT\n");
  1212. /* don't poll on the wait fd
  1213. * again until we get a
  1214. * GraphReordered event.
  1215. */
  1216. client->graph_wait_fd = -1;
  1217. client->pollmax = 1;
  1218. }
  1219. }
  1220. #endif
  1221. if (jack_client_process_events (client)) {
  1222. DEBUG ("event processing failed\n");
  1223. return 0;
  1224. }
  1225. if (client->graph_wait_fd >= 0 &&
  1226. (client->pollfd[WAIT_POLL_INDEX].revents & POLLIN)) {
  1227. DEBUG ("time to run process()\n");
  1228. break;
  1229. }
  1230. }
  1231. if (client->control->dead || client->pollfd[EVENT_POLL_INDEX].revents & ~POLLIN) {
  1232. DEBUG ("client appears dead or event pollfd has error status\n");
  1233. return -1;
  1234. }
  1235. return 0;
  1236. }
  1237. static int
  1238. jack_wake_next_client (jack_client_t* client)
  1239. {
  1240. char c = 0;
  1241. if (write (client->graph_next_fd, &c, sizeof (c))
  1242. != sizeof (c)) {
  1243. DEBUG("cannot write byte to fd %d", client->graph_next_fd);
  1244. jack_error ("cannot continue execution of the "
  1245. "processing graph (%s)",
  1246. strerror(errno));
  1247. return -1;
  1248. }
  1249. DEBUG ("client sent message to next stage by %" PRIu64
  1250. ", client reading on graph_wait_fd==%d",
  1251. jack_get_microseconds(), client->graph_wait_fd);
  1252. DEBUG("reading cleanup byte from pipe %d\n", client->graph_wait_fd);
  1253. if ((read (client->graph_wait_fd, &c, sizeof (c))
  1254. != sizeof (c))) {
  1255. jack_error ("cannot complete execution of the "
  1256. "processing graph (%s)",
  1257. strerror(errno));
  1258. return -1;
  1259. }
  1260. return 0;
  1261. }
  1262. static jack_nframes_t
  1263. jack_thread_first_wait (jack_client_t* client)
  1264. {
  1265. if (jack_client_core_wait (client)) {
  1266. return 0;
  1267. }
  1268. return client->control->nframes;
  1269. }
  1270. jack_nframes_t
  1271. jack_thread_wait (jack_client_t* client, int status)
  1272. {
  1273. client->control->last_status = status;
  1274. /* SECTION ONE: HOUSEKEEPING/CLEANUP FROM LAST DATA PROCESSING */
  1275. /* housekeeping/cleanup after data processing */
  1276. if (status == 0 && client->control->timebase_cb) {
  1277. jack_call_timebase_master (client);
  1278. }
  1279. /* end preemption checking */
  1280. CHECK_PREEMPTION (client->engine, FALSE);
  1281. client->control->finished_at = jack_get_microseconds();
  1282. /* wake the next client in the chain (could be the server),
  1283. and check if we were killed during the process
  1284. cycle.
  1285. */
  1286. if (jack_wake_next_client (client)) {
  1287. DEBUG("client cannot wake next, or is dead\n");
  1288. return 0;
  1289. }
  1290. if (status || client->control->dead || !client->engine->engine_ok) {
  1291. return 0;
  1292. }
  1293. /* SECTION TWO: WAIT FOR NEXT DATA PROCESSING TIME */
  1294. if (jack_client_core_wait (client)) {
  1295. return 0;
  1296. }
  1297. /* SECTION THREE: START NEXT DATA PROCESSING TIME */
  1298. /* Time to do data processing */
  1299. client->control->state = Running;
  1300. /* begin preemption checking */
  1301. CHECK_PREEMPTION (client->engine, TRUE);
  1302. if (client->control->sync_cb)
  1303. jack_call_sync_client (client);
  1304. return client->control->nframes;
  1305. }
  1306. jack_nframes_t jack_cycle_wait (jack_client_t* client)
  1307. {
  1308. /* SECTION TWO: WAIT FOR NEXT DATA PROCESSING TIME */
  1309. if (jack_client_core_wait (client)) {
  1310. return 0;
  1311. }
  1312. /* SECTION THREE: START NEXT DATA PROCESSING TIME */
  1313. /* Time to do data processing */
  1314. client->control->state = Running;
  1315. /* begin preemption checking */
  1316. CHECK_PREEMPTION (client->engine, TRUE);
  1317. if (client->control->sync_cb)
  1318. jack_call_sync_client (client);
  1319. return client->control->nframes;
  1320. }
  1321. void jack_cycle_signal(jack_client_t* client, int status)
  1322. {
  1323. client->control->last_status = status;
  1324. /* SECTION ONE: HOUSEKEEPING/CLEANUP FROM LAST DATA PROCESSING */
  1325. /* housekeeping/cleanup after data processing */
  1326. if (status == 0 && client->control->timebase_cb) {
  1327. jack_call_timebase_master (client);
  1328. }
  1329. /* end preemption checking */
  1330. CHECK_PREEMPTION (client->engine, FALSE);
  1331. client->control->finished_at = jack_get_microseconds();
  1332. /* wake the next client in the chain (could be the server),
  1333. and check if we were killed during the process
  1334. cycle.
  1335. */
  1336. if (jack_wake_next_client (client)) {
  1337. DEBUG("client cannot wake next, or is dead\n");
  1338. jack_client_thread_suicide (client);
  1339. /*NOTREACHED*/
  1340. }
  1341. if (status || client->control->dead || !client->engine->engine_ok) {
  1342. jack_client_thread_suicide (client);
  1343. /*NOTREACHED*/
  1344. }
  1345. }
  1346. static void
  1347. jack_client_thread_aux (void *arg)
  1348. {
  1349. jack_client_t *client = (jack_client_t *) arg;
  1350. jack_client_control_t *control = client->control;
  1351. pthread_mutex_lock (&client_lock);
  1352. client->thread_ok = TRUE;
  1353. client->thread_id = pthread_self();
  1354. pthread_cond_signal (&client_ready);
  1355. pthread_mutex_unlock (&client_lock);
  1356. control->pid = getpid();
  1357. control->pgrp = getpgrp();
  1358. DEBUG ("client thread is now running");
  1359. if (control->thread_init) {
  1360. DEBUG ("calling client thread init callback");
  1361. control->thread_init (control->thread_init_arg);
  1362. }
  1363. /* wait for first wakeup from server */
  1364. if (jack_thread_first_wait (client) == control->nframes) {
  1365. /* now run till we're done */
  1366. if (control->process) {
  1367. /* run process callback, then wait... ad-infinitum */
  1368. while (jack_thread_wait (client,
  1369. control->process (control->nframes,
  1370. control->process_arg)) ==
  1371. control->nframes)
  1372. ;
  1373. } else {
  1374. /* no process handling but still need to process events */
  1375. while (jack_thread_wait (client, 0) == control->nframes)
  1376. ;
  1377. }
  1378. }
  1379. jack_client_thread_suicide (client);
  1380. }
  1381. static void *
  1382. jack_client_thread (void *arg)
  1383. {
  1384. jack_client_t *client = (jack_client_t *) arg;
  1385. jack_client_control_t *control = client->control;
  1386. if (client->control->thread_cb) {
  1387. pthread_mutex_lock (&client_lock);
  1388. client->thread_ok = TRUE;
  1389. client->thread_id = pthread_self();
  1390. pthread_cond_signal (&client_ready);
  1391. pthread_mutex_unlock (&client_lock);
  1392. control->pid = getpid();
  1393. control->pgrp = getpgrp();
  1394. client->control->thread_cb(client->control->thread_cb_arg);
  1395. jack_client_thread_suicide(client);
  1396. } else {
  1397. jack_client_thread_aux(arg);
  1398. }
  1399. /*NOTREACHED*/
  1400. return (void *) 0;
  1401. }
  1402. #ifdef JACK_USE_MACH_THREADS
  1403. /* real-time thread : separated from the normal client thread, it will
  1404. * communicate with the server using fast mach RPC mechanism */
  1405. static void *
  1406. jack_client_process_thread (void *arg)
  1407. {
  1408. jack_client_t *client = (jack_client_t *) arg;
  1409. jack_client_control_t *control = client->control;
  1410. int err = 0;
  1411. if (client->control->thread_init) {
  1412. /* this means that the init callback will be called twice -taybin*/
  1413. DEBUG ("calling client thread init callback");
  1414. client->control->thread_init (client->control->thread_init_arg);
  1415. }
  1416. client->control->pid = getpid();
  1417. DEBUG ("client process thread is now running");
  1418. client->rt_thread_ok = TRUE;
  1419. while (err == 0) {
  1420. if (jack_client_suspend(client) < 0) {
  1421. jack_error ("jack_client_process_thread :resume error");
  1422. goto zombie;
  1423. }
  1424. control->awake_at = jack_get_microseconds();
  1425. DEBUG ("client resumed");
  1426. control->state = Running;
  1427. if (control->sync_cb)
  1428. jack_call_sync_client (client);
  1429. if (control->process) {
  1430. if (control->process (control->nframes,
  1431. control->process_arg) == 0) {
  1432. control->state = Finished;
  1433. }
  1434. } else {
  1435. control->state = Finished;
  1436. }
  1437. if (control->timebase_cb)
  1438. jack_call_timebase_master (client);
  1439. control->finished_at = jack_get_microseconds();
  1440. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  1441. control->finished_at,
  1442. ((float)(control->finished_at - control->awake_at)));
  1443. /* check if we were killed during the process cycle
  1444. * (or whatever) */
  1445. if (client->control->dead) {
  1446. jack_error ("jack_client_process_thread: "
  1447. "client->control->dead");
  1448. goto zombie;
  1449. }
  1450. DEBUG("process cycle fully complete\n");
  1451. }
  1452. return (void *) ((intptr_t)err);
  1453. zombie:
  1454. jack_error ("jack_client_process_thread : zombified");
  1455. client->rt_thread_ok = FALSE;
  1456. if (client->on_shutdown) {
  1457. jack_error ("zombified - calling shutdown handler");
  1458. client->on_shutdown (client->on_shutdown_arg);
  1459. } else {
  1460. jack_error ("jack_client_process_thread zombified - exiting from JACK");
  1461. /* Need a fix : possibly make client crash if
  1462. * zombified without shutdown handler */
  1463. jack_client_close_aux (client);
  1464. }
  1465. pthread_exit (0);
  1466. /*NOTREACHED*/
  1467. return 0;
  1468. }
  1469. #endif /* JACK_USE_MACH_THREADS */
  1470. static int
  1471. jack_start_thread (jack_client_t *client)
  1472. {
  1473. if (client->engine->real_time) {
  1474. #ifdef USE_MLOCK
  1475. if (client->engine->do_mlock
  1476. && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1477. jack_error ("cannot lock down memory for RT thread "
  1478. "(%s)", strerror (errno));
  1479. #ifdef ENSURE_MLOCK
  1480. return -1;
  1481. #endif /* ENSURE_MLOCK */
  1482. }
  1483. if (client->engine->do_munlock) {
  1484. cleanup_mlock ();
  1485. }
  1486. #endif /* USE_MLOCK */
  1487. }
  1488. #ifdef JACK_USE_MACH_THREADS
  1489. /* Stephane Letz : letz@grame.fr
  1490. On MacOSX, the normal thread does not need to be real-time.
  1491. */
  1492. if (jack_client_create_thread (client,
  1493. &client->thread,
  1494. client->engine->client_priority,
  1495. FALSE,
  1496. jack_client_thread, client)) {
  1497. return -1;
  1498. }
  1499. #else
  1500. if (jack_client_create_thread (client,
  1501. &client->thread,
  1502. client->engine->client_priority,
  1503. client->engine->real_time,
  1504. jack_client_thread, client)) {
  1505. return -1;
  1506. }
  1507. #endif
  1508. #ifdef JACK_USE_MACH_THREADS
  1509. /* a secondary thread that runs the process callback and uses
  1510. ultra-fast Mach primitives for inter-thread signalling.
  1511. XXX in a properly structured JACK, there would be no
  1512. need for this, because we would have client wake up
  1513. methods that encapsulated the underlying mechanism
  1514. used.
  1515. */
  1516. if (jack_client_create_thread(client,
  1517. &client->process_thread,
  1518. client->engine->client_priority,
  1519. client->engine->real_time,
  1520. jack_client_process_thread, client)) {
  1521. return -1;
  1522. }
  1523. #endif /* JACK_USE_MACH_THREADS */
  1524. return 0;
  1525. }
  1526. int
  1527. jack_activate (jack_client_t *client)
  1528. {
  1529. jack_request_t req;
  1530. /* we need to scribble on our stack to ensure that its memory
  1531. * pages are actually mapped (more important for mlockall(2)
  1532. * usage in jack_start_thread())
  1533. */
  1534. char buf[JACK_THREAD_STACK_TOUCH];
  1535. int i;
  1536. for (i = 0; i < JACK_THREAD_STACK_TOUCH; i++) {
  1537. buf[i] = (char) (i & 0xff);
  1538. }
  1539. if (client->control->type == ClientInternal ||
  1540. client->control->type == ClientDriver) {
  1541. goto startit;
  1542. }
  1543. /* get the pid of the client process to pass it to engine */
  1544. client->control->pid = getpid ();
  1545. #ifdef USE_CAPABILITIES
  1546. if (client->engine->has_capabilities != 0 &&
  1547. client->control->pid != 0 && client->engine->real_time != 0) {
  1548. /* we need to ask the engine for realtime capabilities
  1549. before trying to start the realtime thread
  1550. */
  1551. req.type = SetClientCapabilities;
  1552. req.x.client_id = client->control->id;
  1553. req.x.cap_pid = client->control->pid;
  1554. jack_client_deliver_request (client, &req);
  1555. if (req.status) {
  1556. /* what to do? engine is running realtime, it
  1557. is using capabilities and has them
  1558. (otherwise we would not get an error
  1559. return) but for some reason it could not
  1560. give the client the required capabilities.
  1561. For now, leave the client so that it
  1562. still runs, albeit non-realtime.
  1563. */
  1564. jack_error ("could not receive realtime capabilities, "
  1565. "client will run non-realtime");
  1566. }
  1567. }
  1568. #endif /* USE_CAPABILITIES */
  1569. if (client->first_active) {
  1570. pthread_mutex_init (&client_lock, NULL);
  1571. pthread_cond_init (&client_ready, NULL);
  1572. pthread_mutex_lock (&client_lock);
  1573. if (jack_start_thread (client)) {
  1574. pthread_mutex_unlock (&client_lock);
  1575. return -1;
  1576. }
  1577. pthread_cond_wait (&client_ready, &client_lock);
  1578. pthread_mutex_unlock (&client_lock);
  1579. if (!client->thread_ok) {
  1580. jack_error ("could not start client thread");
  1581. return -1;
  1582. }
  1583. client->first_active = FALSE;
  1584. }
  1585. startit:
  1586. req.type = ActivateClient;
  1587. req.x.client_id = client->control->id;
  1588. return jack_client_deliver_request (client, &req);
  1589. }
  1590. static int
  1591. jack_deactivate_aux (jack_client_t *client)
  1592. {
  1593. jack_request_t req;
  1594. int rc = ESRCH; /* already shut down */
  1595. if (client && client->control) { /* not shut down? */
  1596. rc = 0;
  1597. if (client->control->active) { /* still active? */
  1598. req.type = DeactivateClient;
  1599. req.x.client_id = client->control->id;
  1600. rc = jack_client_deliver_request (client, &req);
  1601. }
  1602. }
  1603. return rc;
  1604. }
  1605. int
  1606. jack_deactivate (jack_client_t *client)
  1607. {
  1608. return jack_deactivate_aux(client);
  1609. }
  1610. static int
  1611. jack_client_close_aux (jack_client_t *client)
  1612. {
  1613. JSList *node;
  1614. void *status;
  1615. int rc;
  1616. rc = jack_deactivate_aux (client);
  1617. if (rc == ESRCH) { /* already shut down? */
  1618. return rc;
  1619. }
  1620. if (client->control->type == ClientExternal) {
  1621. #if JACK_USE_MACH_THREADS
  1622. if (client->rt_thread_ok) {
  1623. // MacOSX pthread_cancel not implemented in
  1624. // Darwin 5.5, 6.4
  1625. mach_port_t machThread =
  1626. pthread_mach_thread_np (client->process_thread);
  1627. thread_terminate (machThread);
  1628. }
  1629. #endif
  1630. /* stop the thread that communicates with the jack
  1631. * server, only if it was actually running
  1632. */
  1633. if (client->thread_ok){
  1634. pthread_cancel (client->thread);
  1635. pthread_join (client->thread, &status);
  1636. }
  1637. if (client->control) {
  1638. jack_release_shm (&client->control_shm);
  1639. client->control = NULL;
  1640. }
  1641. if (client->engine) {
  1642. jack_release_shm (&client->engine_shm);
  1643. client->engine = NULL;
  1644. }
  1645. if (client->port_segment) {
  1646. jack_port_type_id_t ptid;
  1647. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1648. jack_release_shm (&client->port_segment[ptid]);
  1649. }
  1650. free (client->port_segment);
  1651. client->port_segment = NULL;
  1652. }
  1653. #ifndef JACK_USE_MACH_THREADS
  1654. if (client->graph_wait_fd) {
  1655. close (client->graph_wait_fd);
  1656. }
  1657. if (client->graph_next_fd) {
  1658. close (client->graph_next_fd);
  1659. }
  1660. #endif
  1661. close (client->event_fd);
  1662. if (shutdown (client->request_fd, SHUT_RDWR)) {
  1663. jack_error ("could not shutdown client request socket");
  1664. }
  1665. close (client->request_fd);
  1666. }
  1667. for (node = client->ports; node; node = jack_slist_next (node)) {
  1668. free (node->data);
  1669. }
  1670. jack_slist_free (client->ports);
  1671. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  1672. free (node->data);
  1673. }
  1674. jack_slist_free (client->ports_ext);
  1675. jack_client_free (client);
  1676. return rc;
  1677. }
  1678. int
  1679. jack_client_close (jack_client_t *client)
  1680. {
  1681. return jack_client_close_aux(client);
  1682. }
  1683. int
  1684. jack_is_realtime (jack_client_t *client)
  1685. {
  1686. return client->engine->real_time;
  1687. }
  1688. jack_nframes_t
  1689. jack_get_buffer_size (jack_client_t *client)
  1690. {
  1691. return client->engine->buffer_size;
  1692. }
  1693. int
  1694. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1695. {
  1696. #ifdef DO_BUFFER_RESIZE
  1697. jack_request_t req;
  1698. req.type = SetBufferSize;
  1699. req.x.nframes = nframes;
  1700. return jack_client_deliver_request (client, &req);
  1701. #else
  1702. return ENOSYS;
  1703. #endif /* DO_BUFFER_RESIZE */
  1704. }
  1705. int
  1706. jack_connect (jack_client_t *client, const char *source_port,
  1707. const char *destination_port)
  1708. {
  1709. jack_request_t req;
  1710. req.type = ConnectPorts;
  1711. snprintf (req.x.connect.source_port,
  1712. sizeof (req.x.connect.source_port), "%s", source_port);
  1713. snprintf (req.x.connect.destination_port,
  1714. sizeof (req.x.connect.destination_port),
  1715. "%s", destination_port);
  1716. return jack_client_deliver_request (client, &req);
  1717. }
  1718. int
  1719. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1720. {
  1721. jack_request_t req;
  1722. pthread_mutex_lock (&port->connection_lock);
  1723. if (port->connections == NULL) {
  1724. pthread_mutex_unlock (&port->connection_lock);
  1725. return 0;
  1726. }
  1727. pthread_mutex_unlock (&port->connection_lock);
  1728. req.type = DisconnectPort;
  1729. req.x.port_info.port_id = port->shared->id;
  1730. return jack_client_deliver_request (client, &req);
  1731. }
  1732. int
  1733. jack_disconnect (jack_client_t *client, const char *source_port,
  1734. const char *destination_port)
  1735. {
  1736. jack_request_t req;
  1737. req.type = DisconnectPorts;
  1738. snprintf (req.x.connect.source_port,
  1739. sizeof (req.x.connect.source_port), "%s", source_port);
  1740. snprintf (req.x.connect.destination_port,
  1741. sizeof (req.x.connect.destination_port),
  1742. "%s", destination_port);
  1743. return jack_client_deliver_request (client, &req);
  1744. }
  1745. void
  1746. jack_set_error_function (void (*func) (const char *))
  1747. {
  1748. jack_error_callback = func;
  1749. }
  1750. void
  1751. jack_set_info_function (void (*func) (const char *))
  1752. {
  1753. jack_info_callback = func;
  1754. }
  1755. int
  1756. jack_set_graph_order_callback (jack_client_t *client,
  1757. JackGraphOrderCallback callback, void *arg)
  1758. {
  1759. if (client->control->active) {
  1760. jack_error ("You cannot set callbacks on an active client.");
  1761. return -1;
  1762. }
  1763. client->control->graph_order = callback;
  1764. client->control->graph_order_arg = arg;
  1765. return 0;
  1766. }
  1767. int jack_set_xrun_callback (jack_client_t *client,
  1768. JackXRunCallback callback, void *arg)
  1769. {
  1770. if (client->control->active) {
  1771. jack_error ("You cannot set callbacks on an active client.");
  1772. return -1;
  1773. }
  1774. client->control->xrun = callback;
  1775. client->control->xrun_arg = arg;
  1776. return 0;
  1777. }
  1778. int
  1779. jack_set_process_callback (jack_client_t *client,
  1780. JackProcessCallback callback, void *arg)
  1781. {
  1782. if (client->control->active) {
  1783. jack_error ("You cannot set callbacks on an active client.");
  1784. return -1;
  1785. }
  1786. if (client->control->thread_cb) {
  1787. jack_error ("A thread callback has already been setup, both models cannot be used at the same time!");
  1788. return -1;
  1789. }
  1790. client->control->process_arg = arg;
  1791. client->control->process = callback;
  1792. return 0;
  1793. }
  1794. int
  1795. jack_set_thread_init_callback (jack_client_t *client,
  1796. JackThreadInitCallback callback, void *arg)
  1797. {
  1798. if (client->control->active) {
  1799. jack_error ("You cannot set callbacks on an active client.");
  1800. return -1;
  1801. }
  1802. client->control->thread_init_arg = arg;
  1803. client->control->thread_init = callback;
  1804. return 0;
  1805. }
  1806. int
  1807. jack_set_freewheel_callback (jack_client_t *client,
  1808. JackFreewheelCallback callback, void *arg)
  1809. {
  1810. if (client->control->active) {
  1811. jack_error ("You cannot set callbacks on an active client.");
  1812. return -1;
  1813. }
  1814. client->control->freewheel_arg = arg;
  1815. client->control->freewheel_cb = callback;
  1816. return 0;
  1817. }
  1818. int
  1819. jack_set_buffer_size_callback (jack_client_t *client,
  1820. JackBufferSizeCallback callback, void *arg)
  1821. {
  1822. client->control->bufsize_arg = arg;
  1823. client->control->bufsize = callback;
  1824. return 0;
  1825. }
  1826. int
  1827. jack_set_port_registration_callback(jack_client_t *client,
  1828. JackPortRegistrationCallback callback,
  1829. void *arg)
  1830. {
  1831. if (client->control->active) {
  1832. jack_error ("You cannot set callbacks on an active client.");
  1833. return -1;
  1834. }
  1835. client->control->port_register_arg = arg;
  1836. client->control->port_register = callback;
  1837. return 0;
  1838. }
  1839. int
  1840. jack_set_port_connect_callback(jack_client_t *client,
  1841. JackPortConnectCallback callback,
  1842. void *arg)
  1843. {
  1844. if (client->control->active) {
  1845. jack_error ("You cannot set callbacks on an active client.");
  1846. return -1;
  1847. }
  1848. client->control->port_connect_arg = arg;
  1849. client->control->port_connect = callback;
  1850. return 0;
  1851. }
  1852. int
  1853. jack_set_client_registration_callback(jack_client_t *client,
  1854. JackClientRegistrationCallback callback,
  1855. void *arg)
  1856. {
  1857. if (client->control->active) {
  1858. jack_error ("You cannot set callbacks on an active client.");
  1859. return -1;
  1860. }
  1861. client->control->client_register_arg = arg;
  1862. client->control->client_register = callback;
  1863. return 0;
  1864. }
  1865. int
  1866. jack_set_process_thread(jack_client_t* client, JackThreadCallback callback, void *arg)
  1867. {
  1868. if (client->control->active) {
  1869. jack_error ("You cannot set callbacks on an active client.");
  1870. return -1;
  1871. }
  1872. if (client->control->process) {
  1873. jack_error ("A process callback has already been setup, both models cannot be used at the same time!");
  1874. return -1;
  1875. }
  1876. client->control->thread_cb_arg = arg;
  1877. client->control->thread_cb = callback;
  1878. return 0;
  1879. }
  1880. int
  1881. jack_get_process_done_fd (jack_client_t *client)
  1882. {
  1883. return client->graph_next_fd;
  1884. }
  1885. void
  1886. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1887. {
  1888. client->on_shutdown = function;
  1889. client->on_shutdown_arg = arg;
  1890. }
  1891. const char **
  1892. jack_get_ports (jack_client_t *client,
  1893. const char *port_name_pattern,
  1894. const char *type_name_pattern,
  1895. unsigned long flags)
  1896. {
  1897. jack_control_t *engine;
  1898. const char **matching_ports;
  1899. unsigned long match_cnt;
  1900. jack_port_shared_t *psp;
  1901. unsigned long i;
  1902. regex_t port_regex;
  1903. regex_t type_regex;
  1904. int matching;
  1905. engine = client->engine;
  1906. if (port_name_pattern && port_name_pattern[0]) {
  1907. regcomp (&port_regex, port_name_pattern,
  1908. REG_EXTENDED|REG_NOSUB);
  1909. }
  1910. if (type_name_pattern && type_name_pattern[0]) {
  1911. regcomp (&type_regex, type_name_pattern,
  1912. REG_EXTENDED|REG_NOSUB);
  1913. }
  1914. psp = engine->ports;
  1915. match_cnt = 0;
  1916. matching_ports = (const char **)
  1917. malloc (sizeof (char *) * engine->port_max);
  1918. for (i = 0; i < engine->port_max; i++) {
  1919. matching = 1;
  1920. if (!psp[i].in_use) {
  1921. continue;
  1922. }
  1923. if (flags) {
  1924. if ((psp[i].flags & flags) != flags) {
  1925. matching = 0;
  1926. }
  1927. }
  1928. if (matching && port_name_pattern && port_name_pattern[0]) {
  1929. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1930. matching = 0;
  1931. }
  1932. }
  1933. if (matching && type_name_pattern && type_name_pattern[0]) {
  1934. jack_port_type_id_t ptid = psp[i].ptype_id;
  1935. if (regexec (&type_regex,
  1936. engine->port_types[ptid].type_name,
  1937. 0, NULL, 0)) {
  1938. matching = 0;
  1939. }
  1940. }
  1941. if (matching) {
  1942. matching_ports[match_cnt++] = psp[i].name;
  1943. }
  1944. }
  1945. if (port_name_pattern && port_name_pattern[0]) {
  1946. regfree (&port_regex);
  1947. }
  1948. if (type_name_pattern && type_name_pattern[0]) {
  1949. regfree (&type_regex);
  1950. }
  1951. matching_ports[match_cnt] = 0;
  1952. if (match_cnt == 0) {
  1953. free (matching_ports);
  1954. matching_ports = 0;
  1955. }
  1956. return matching_ports;
  1957. }
  1958. float
  1959. jack_cpu_load (jack_client_t *client)
  1960. {
  1961. return client->engine->cpu_load;
  1962. }
  1963. float
  1964. jack_get_xrun_delayed_usecs (jack_client_t *client)
  1965. {
  1966. return client->engine->xrun_delayed_usecs;
  1967. }
  1968. float
  1969. jack_get_max_delayed_usecs (jack_client_t *client)
  1970. {
  1971. return client->engine->max_delayed_usecs;
  1972. }
  1973. void
  1974. jack_reset_max_delayed_usecs (jack_client_t *client)
  1975. {
  1976. client->engine->max_delayed_usecs = 0.0f;
  1977. }
  1978. pthread_t
  1979. jack_client_thread_id (jack_client_t *client)
  1980. {
  1981. return client->thread_id;
  1982. }
  1983. int
  1984. jack_client_name_size(void)
  1985. {
  1986. return JACK_CLIENT_NAME_SIZE;
  1987. }
  1988. int
  1989. jack_port_name_size(void)
  1990. {
  1991. return JACK_PORT_NAME_SIZE;
  1992. }
  1993. int
  1994. jack_port_type_size(void)
  1995. {
  1996. return JACK_PORT_TYPE_SIZE;
  1997. }