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.

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