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.

2317 lines
52KB

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