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.

2284 lines
51KB

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