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.

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