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.

2210 lines
50KB

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