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.

2279 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. #ifdef WITH_TIMESTAMPS
  54. #include <jack/timestamps.h>
  55. #endif /* WITH_TIMESTAMPS */
  56. static pthread_mutex_t client_lock;
  57. static pthread_cond_t client_ready;
  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 USE_DYNSIMD
  71. #ifdef ARCH_X86
  72. static int
  73. have_3dnow ()
  74. {
  75. unsigned int res = 0;
  76. #ifdef __x86_64__
  77. asm volatile ("pushq %%rbx\n\t" : : : "memory");
  78. #else
  79. asm volatile ("pushl %%ebx\n\t" : : : "memory");
  80. #endif
  81. asm volatile (
  82. "movl $0x80000000, %%eax\n\t" \
  83. "cpuid\n\t" \
  84. "cmpl $0x80000001, %%eax\n\t" \
  85. "jl tdnow_prexit\n\t" \
  86. \
  87. "movl $0x80000001, %%eax\n\t" \
  88. "cpuid\n\t" \
  89. \
  90. "xorl %%eax, %%eax\n\t" \
  91. \
  92. "movl $1, %%ecx\n\t" \
  93. "shll $31, %%ecx\n\t" \
  94. "testl %%ecx, %%edx\n\t" \
  95. "jz tdnow_testexit\n\t" \
  96. "movl $1, %%eax\n\t" \
  97. \
  98. "movl $1, %%ecx\n\t" \
  99. "shll $30, %%ecx\n\t" \
  100. "testl %%ecx, %%edx\n\t" \
  101. "jz tdnow_testexit\n\t" \
  102. "movl $2, %%eax\n\t" \
  103. "jmp tdnow_testexit\n\t" \
  104. \
  105. "tdnow_prexit:\n\t" \
  106. "xorl %%eax, %%eax\n\t" \
  107. "tdnow_testexit:\n\t"
  108. : "=a" (res)
  109. :
  110. : "ecx", "edx", "memory");
  111. #ifdef __x86_64__
  112. asm volatile ("popq %%rbx\n\t" : : : "memory");
  113. #else
  114. asm volatile ("popl %%ebx\n\t" : : : "memory");
  115. #endif
  116. return res;
  117. }
  118. static int
  119. have_sse ()
  120. {
  121. unsigned int res = 0;
  122. #ifdef __x86_64__
  123. asm volatile ("pushq %%rbx\n\t" : : : "memory");
  124. #else
  125. asm volatile ("pushl %%ebx\n\t" : : : "memory");
  126. #endif
  127. asm volatile (
  128. "movl $1, %%eax\n\t" \
  129. "cpuid\n\t" \
  130. \
  131. "xorl %%eax, %%eax\n\t" \
  132. \
  133. "movl $1, %%ebx\n\t" \
  134. "shll $25, %%ebx\n\t" \
  135. "testl %%ebx, %%edx\n\t" \
  136. "jz sse_testexit\n\t" \
  137. "movl $1, %%eax\n\t" \
  138. \
  139. "movl $1, %%ebx\n\t" \
  140. "shll $26, %%ebx\n\t" \
  141. "testl %%ebx, %%edx\n\t" \
  142. "jz sse_testexit\n\t" \
  143. "movl $2, %%eax\n\t" \
  144. \
  145. "movl $1, %%ebx\n\t" \
  146. "testl %%ebx, %%ecx\n\t" \
  147. "jz sse_testexit\n\t" \
  148. "movl $3, %%eax\n\t" \
  149. \
  150. "sse_testexit:\n\t"
  151. : "=a" (res)
  152. :
  153. : "ecx", "edx", "memory");
  154. #ifdef __x86_64__
  155. asm volatile ("popq %%rbx\n\t" : : : "memory");
  156. #else
  157. asm volatile ("popl %%ebx\n\t" : : : "memory");
  158. #endif
  159. return res;
  160. }
  161. static void
  162. init_cpu ()
  163. {
  164. cpu_type = ((have_3dnow() << 8) | have_sse());
  165. if (ARCH_X86_HAVE_3DNOW(cpu_type))
  166. fprintf(stderr, "Enhanced3DNow! detected\n");
  167. if (ARCH_X86_HAVE_SSE2(cpu_type))
  168. fprintf(stderr, "SSE2 detected\n");
  169. if ((!ARCH_X86_HAVE_3DNOW(cpu_type)) && (!ARCH_X86_HAVE_SSE2(cpu_type)))
  170. fprintf(stderr,
  171. "No supported SIMD instruction sets detected\n");
  172. jack_port_set_funcs();
  173. }
  174. #else /* ARCH_X86 */
  175. static void
  176. init_cpu ()
  177. {
  178. jack_port_set_funcs();
  179. }
  180. #endif /* ARCH_X86 */
  181. #endif /* USE_DYNSIMD */
  182. char *jack_tmpdir = DEFAULT_TMP_DIR;
  183. static int
  184. jack_get_tmpdir ()
  185. {
  186. FILE* in;
  187. size_t len;
  188. char buf[PATH_MAX+2]; /* allow tmpdir to live anywhere, plus newline, plus null */
  189. if ((in = popen ("jackd -l", "r")) == NULL) {
  190. return -1;
  191. }
  192. if (fgets (buf, sizeof (buf), in) == NULL) {
  193. fclose (in);
  194. return -1;
  195. }
  196. len = strlen (buf);
  197. if (buf[len-1] != '\n') {
  198. /* didn't get a whole line */
  199. fclose (in);
  200. return -1;
  201. }
  202. jack_tmpdir = (char *) malloc (len);
  203. memcpy (jack_tmpdir, buf, len-1);
  204. jack_tmpdir[len-1] = '\0';
  205. fprintf (stderr, "JACK tmpdir identified as [%s]\n", jack_tmpdir);
  206. fclose (in);
  207. return 0;
  208. }
  209. void
  210. jack_error (const char *fmt, ...)
  211. {
  212. va_list ap;
  213. char buffer[300];
  214. va_start (ap, fmt);
  215. vsnprintf (buffer, sizeof(buffer), fmt, ap);
  216. jack_error_callback (buffer);
  217. va_end (ap);
  218. }
  219. void
  220. default_jack_error_callback (const char *desc)
  221. {
  222. fprintf(stderr, "%s\n", desc);
  223. }
  224. void
  225. silent_jack_error_callback (const char *desc)
  226. {
  227. }
  228. void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  229. static int
  230. oop_client_deliver_request (void *ptr, jack_request_t *req)
  231. {
  232. int wok, rok;
  233. jack_client_t *client = (jack_client_t*) ptr;
  234. wok = (write (client->request_fd, req, sizeof (*req))
  235. == sizeof (*req));
  236. rok = (read (client->request_fd, req, sizeof (*req))
  237. == sizeof (*req));
  238. if (wok && rok) { /* everything OK? */
  239. return req->status;
  240. }
  241. req->status = -1; /* request failed */
  242. /* check for server shutdown */
  243. if (client->engine->engine_ok == 0)
  244. return req->status;
  245. /* otherwise report errors */
  246. if (!wok)
  247. jack_error ("cannot send request type %d to server",
  248. req->type);
  249. if (!rok)
  250. jack_error ("cannot read result for request type %d from"
  251. " server (%s)", req->type, strerror (errno));
  252. return req->status;
  253. }
  254. int
  255. jack_client_deliver_request (const jack_client_t *client, jack_request_t *req)
  256. {
  257. /* indirect through the function pointer that was set either
  258. * by jack_client_open() or by jack_new_client_request() in
  259. * the server.
  260. */
  261. return client->control->deliver_request (client->control->deliver_arg,
  262. req);
  263. }
  264. #if JACK_USE_MACH_THREADS
  265. jack_client_t *
  266. jack_client_alloc ()
  267. {
  268. jack_client_t *client;
  269. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  270. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 1);
  271. client->pollmax = 1;
  272. client->request_fd = -1;
  273. client->event_fd = -1;
  274. client->upstream_is_jackd = 0;
  275. client->graph_wait_fd = -1;
  276. client->graph_next_fd = -1;
  277. client->ports = NULL;
  278. client->ports_ext = NULL;
  279. client->engine = NULL;
  280. client->control = NULL;
  281. client->thread_ok = FALSE;
  282. client->rt_thread_ok = FALSE;
  283. client->first_active = TRUE;
  284. client->on_shutdown = NULL;
  285. client->n_port_types = 0;
  286. client->port_segment = NULL;
  287. return client;
  288. }
  289. #else
  290. jack_client_t *
  291. jack_client_alloc ()
  292. {
  293. jack_client_t *client;
  294. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  295. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2);
  296. client->pollmax = 2;
  297. client->request_fd = -1;
  298. client->event_fd = -1;
  299. client->upstream_is_jackd = 0;
  300. client->graph_wait_fd = -1;
  301. client->graph_next_fd = -1;
  302. client->ports = NULL;
  303. client->ports_ext = NULL;
  304. client->engine = NULL;
  305. client->control = NULL;
  306. client->thread_ok = FALSE;
  307. client->first_active = TRUE;
  308. client->on_shutdown = NULL;
  309. client->n_port_types = 0;
  310. client->port_segment = NULL;
  311. #ifdef USE_DYNSIMD
  312. init_cpu();
  313. #endif /* USE_DYNSIMD */
  314. return client;
  315. }
  316. #endif
  317. /*
  318. * Build the jack_client_t structure for an internal client.
  319. */
  320. jack_client_t *
  321. jack_client_alloc_internal (jack_client_control_t *cc, jack_engine_t* engine)
  322. {
  323. jack_client_t* client;
  324. client = jack_client_alloc ();
  325. client->control = cc;
  326. client->engine = engine->control;
  327. client->n_port_types = client->engine->n_port_types;
  328. client->port_segment = &engine->port_segment[0];
  329. return client;
  330. }
  331. static void
  332. jack_client_free (jack_client_t *client)
  333. {
  334. if (client->pollfd) {
  335. free (client->pollfd);
  336. }
  337. free (client);
  338. }
  339. void
  340. jack_client_invalidate_port_buffers (jack_client_t *client)
  341. {
  342. JSList *node;
  343. jack_port_t *port;
  344. /* This releases all local memory owned by input ports
  345. and sets the buffer pointer to NULL. This will cause
  346. jack_port_get_buffer() to reallocate space for the
  347. buffer on the next call (if there is one).
  348. */
  349. for (node = client->ports; node; node = jack_slist_next (node)) {
  350. port = (jack_port_t *) node->data;
  351. if (port->shared->flags & JackPortIsInput) {
  352. if (port->mix_buffer) {
  353. jack_pool_release (port->mix_buffer);
  354. port->mix_buffer = NULL;
  355. }
  356. }
  357. }
  358. }
  359. int
  360. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  361. {
  362. jack_port_t *control_port;
  363. jack_port_t *other;
  364. JSList *node;
  365. int need_free = FALSE;
  366. switch (event->type) {
  367. case PortConnected:
  368. other = jack_port_new (client, event->y.other_id,
  369. client->engine);
  370. /* jack_port_by_id_int() always returns an internal
  371. * port that does not need to be deallocated */
  372. control_port = jack_port_by_id_int (client, event->x.self_id,
  373. &need_free);
  374. pthread_mutex_lock (&control_port->connection_lock);
  375. control_port->connections =
  376. jack_slist_prepend (control_port->connections,
  377. (void *) other);
  378. pthread_mutex_unlock (&control_port->connection_lock);
  379. break;
  380. case PortDisconnected:
  381. /* jack_port_by_id_int() always returns an internal
  382. * port that does not need to be deallocated */
  383. control_port = jack_port_by_id_int (client, event->x.self_id,
  384. &need_free);
  385. pthread_mutex_lock (&control_port->connection_lock);
  386. for (node = control_port->connections; node;
  387. node = jack_slist_next (node)) {
  388. other = (jack_port_t *) node->data;
  389. if (other->shared->id == event->y.other_id) {
  390. control_port->connections =
  391. jack_slist_remove_link (
  392. control_port->connections,
  393. node);
  394. jack_slist_free_1 (node);
  395. free (other);
  396. break;
  397. }
  398. }
  399. pthread_mutex_unlock (&control_port->connection_lock);
  400. break;
  401. default:
  402. /* impossible */
  403. break;
  404. }
  405. return 0;
  406. }
  407. #if JACK_USE_MACH_THREADS
  408. static int
  409. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  410. {
  411. client->pollmax = 1;
  412. /* If the client registered its own callback for graph order events,
  413. execute it now.
  414. */
  415. if (client->control->graph_order) {
  416. client->control->graph_order (client->control->graph_order_arg);
  417. }
  418. return 0;
  419. }
  420. #else
  421. static int
  422. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  423. {
  424. char path[PATH_MAX+1];
  425. if (client->graph_wait_fd >= 0) {
  426. DEBUG ("closing graph_wait_fd==%d", client->graph_wait_fd);
  427. close (client->graph_wait_fd);
  428. client->graph_wait_fd = -1;
  429. }
  430. if (client->graph_next_fd >= 0) {
  431. DEBUG ("closing graph_next_fd==%d", client->graph_next_fd);
  432. close (client->graph_next_fd);
  433. client->graph_next_fd = -1;
  434. }
  435. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n);
  436. if ((client->graph_wait_fd = open (path, O_RDONLY|O_NONBLOCK)) < 0) {
  437. jack_error ("cannot open specified fifo [%s] for reading (%s)",
  438. path, strerror (errno));
  439. return -1;
  440. }
  441. DEBUG ("opened new graph_wait_fd %d (%s)", client->graph_wait_fd, path);
  442. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n+1);
  443. if ((client->graph_next_fd = open (path, O_WRONLY|O_NONBLOCK)) < 0) {
  444. jack_error ("cannot open specified fifo [%s] for writing (%s)",
  445. path, strerror (errno));
  446. return -1;
  447. }
  448. client->upstream_is_jackd = event->y.n;
  449. client->pollmax = 2;
  450. DEBUG ("opened new graph_next_fd %d (%s) (upstream is jackd? %d)",
  451. client->graph_next_fd, path,
  452. client->upstream_is_jackd);
  453. /* If the client registered its own callback for graph order events,
  454. execute it now.
  455. */
  456. if (client->control->graph_order) {
  457. client->control->graph_order (client->control->graph_order_arg);
  458. }
  459. return 0;
  460. }
  461. #endif
  462. static int
  463. server_connect (const char *server_name)
  464. {
  465. int fd;
  466. struct sockaddr_un addr;
  467. int which = 0;
  468. char server_dir[PATH_MAX+1] = "";
  469. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  470. jack_error ("cannot create client socket (%s)",
  471. strerror (errno));
  472. return -1;
  473. }
  474. //JOQ: temporary debug message
  475. //fprintf (stderr, "DEBUG: connecting to `%s' server\n", server_name);
  476. addr.sun_family = AF_UNIX;
  477. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d",
  478. jack_server_dir (server_name, server_dir) , which);
  479. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  480. close (fd);
  481. return -1;
  482. }
  483. return fd;
  484. }
  485. static int
  486. server_event_connect (jack_client_t *client, const char *server_name)
  487. {
  488. int fd;
  489. struct sockaddr_un addr;
  490. jack_client_connect_ack_request_t req;
  491. jack_client_connect_ack_result_t res;
  492. char server_dir[PATH_MAX+1] = "";
  493. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  494. jack_error ("cannot create client event socket (%s)",
  495. strerror (errno));
  496. return -1;
  497. }
  498. addr.sun_family = AF_UNIX;
  499. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0",
  500. jack_server_dir (server_name,server_dir));
  501. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  502. jack_error ("cannot connect to jack server for events",
  503. strerror (errno));
  504. close (fd);
  505. return -1;
  506. }
  507. req.client_id = client->control->id;
  508. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  509. jack_error ("cannot write event connect request to server (%s)",
  510. strerror (errno));
  511. close (fd);
  512. return -1;
  513. }
  514. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  515. jack_error ("cannot read event connect result from server (%s)",
  516. strerror (errno));
  517. close (fd);
  518. return -1;
  519. }
  520. if (res.status != 0) {
  521. jack_error ("cannot connect to server for event stream (%s)",
  522. strerror (errno));
  523. close (fd);
  524. return -1;
  525. }
  526. return fd;
  527. }
  528. /* Exec the JACK server in this process. Does not return. */
  529. static void
  530. _start_server (const char *server_name)
  531. {
  532. FILE* fp = 0;
  533. char filename[255];
  534. char arguments[255];
  535. char buffer[255];
  536. char* command = 0;
  537. size_t pos = 0;
  538. size_t result = 0;
  539. char** argv = 0;
  540. int i = 0;
  541. int good = 0;
  542. int ret;
  543. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  544. fp = fopen(filename, "r");
  545. if (!fp) {
  546. fp = fopen("/etc/jackdrc", "r");
  547. }
  548. /* if still not found, check old config name for backwards compatability */
  549. if (!fp) {
  550. fp = fopen("/etc/jackd.conf", "r");
  551. }
  552. if (fp) {
  553. arguments[0] = '\0';
  554. ret = fscanf(fp, "%s", buffer);
  555. while(ret != 0 && ret != EOF) {
  556. strcat(arguments, buffer);
  557. strcat(arguments, " ");
  558. ret = fscanf(fp, "%s", buffer);
  559. }
  560. if (strlen(arguments) > 0) {
  561. good = 1;
  562. }
  563. }
  564. if (!good) {
  565. #if defined(USE_CAPABILITIES)
  566. command = JACK_LOCATION "/jackstart";
  567. strncpy(arguments, JACK_LOCATION "/jackstart -T -R -d "
  568. JACK_DEFAULT_DRIVER " -p 512", 255);
  569. #else /* !USE_CAPABILITIES */
  570. command = JACK_LOCATION "/jackd";
  571. strncpy(arguments, JACK_LOCATION "/jackd -T -d "
  572. JACK_DEFAULT_DRIVER, 255);
  573. #endif /* USE_CAPABILITIES */
  574. } else {
  575. result = strcspn(arguments, " ");
  576. command = (char *) malloc(result+1);
  577. strncpy(command, arguments, result);
  578. command[result] = '\0';
  579. }
  580. argv = (char **) malloc (255);
  581. while(1) {
  582. /* insert -T and -nserver_name in front of arguments */
  583. if (i == 1) {
  584. argv[i] = (char *) malloc(strlen ("-T") + 1);
  585. strcpy (argv[i++], "-T");
  586. if (server_name) {
  587. size_t optlen = strlen ("-n");
  588. char *buf =
  589. malloc (optlen
  590. + strlen (server_name) + 1);
  591. strcpy (buf, "-n");
  592. strcpy (buf+optlen, server_name);
  593. argv[i++] = buf;
  594. }
  595. }
  596. result = strcspn(arguments+pos, " ");
  597. if (result == 0) {
  598. break;
  599. }
  600. argv[i] = (char*)malloc(result+1);
  601. strncpy(argv[i], arguments+pos, result);
  602. argv[i][result] = '\0';
  603. pos += result+1;
  604. ++i;
  605. }
  606. argv[i] = 0;
  607. execv (command, argv);
  608. /* If execv() succeeds, it does not return. There's no point
  609. * in calling jack_error() here in the child process. */
  610. fprintf (stderr, "exec of JACK server (command = \"%s\") failed: %s\n", command, strerror (errno));
  611. }
  612. int
  613. start_server (const char *server_name, jack_options_t options)
  614. {
  615. if ((options & JackNoStartServer)
  616. || getenv("JACK_NO_START_SERVER")) {
  617. return 1;
  618. }
  619. /* The double fork() forces the server to become a child of
  620. * init, which will always clean up zombie process state on
  621. * termination. This even works in cases where the server
  622. * terminates but this client does not.
  623. *
  624. * Since fork() is usually implemented using copy-on-write
  625. * virtual memory tricks, the overhead of the second fork() is
  626. * probably relatively small.
  627. */
  628. switch (fork()) {
  629. case 0: /* child process */
  630. switch (fork()) {
  631. case 0: /* grandchild process */
  632. _start_server(server_name);
  633. _exit (99); /* exec failed */
  634. case -1:
  635. _exit (98);
  636. default:
  637. _exit (0);
  638. }
  639. case -1: /* fork() error */
  640. return 1; /* failed to start server */
  641. }
  642. /* only the original parent process goes here */
  643. return 0; /* (probably) successful */
  644. }
  645. static int
  646. jack_request_client (ClientType type,
  647. const char* client_name, jack_options_t options,
  648. jack_status_t *status, jack_varargs_t *va,
  649. jack_client_connect_result_t *res, int *req_fd)
  650. {
  651. jack_client_connect_request_t req;
  652. *req_fd = -1;
  653. memset (&req, 0, sizeof (req));
  654. req.options = options;
  655. if (strlen (client_name) >= sizeof (req.name)) {
  656. jack_error ("\"%s\" is too long to be used as a JACK client"
  657. " name.\n"
  658. "Please use %lu characters or less.",
  659. client_name, sizeof (req.name));
  660. return -1;
  661. }
  662. if (va->load_name
  663. && (strlen (va->load_name) > sizeof (req.object_path) - 1)) {
  664. jack_error ("\"%s\" is too long to be used as a JACK shared"
  665. " object name.\n"
  666. "Please use %lu characters or less.",
  667. va->load_name, sizeof (req.object_path) - 1);
  668. return -1;
  669. }
  670. if (va->load_init
  671. && (strlen (va->load_init) > sizeof (req.object_data) - 1)) {
  672. jack_error ("\"%s\" is too long to be used as a JACK shared"
  673. " object data string.\n"
  674. "Please use %lu characters or less.",
  675. va->load_init, sizeof (req.object_data) - 1);
  676. return -1;
  677. }
  678. if ((*req_fd = server_connect (va->server_name)) < 0) {
  679. int trys;
  680. if (start_server(va->server_name, options)) {
  681. *status |= (JackFailure|JackServerFailed);
  682. goto fail;
  683. }
  684. trys = 5;
  685. do {
  686. sleep(1);
  687. if (--trys < 0) {
  688. *status |= (JackFailure|JackServerFailed);
  689. goto fail;
  690. }
  691. } while ((*req_fd = server_connect (va->server_name)) < 0);
  692. *status |= JackServerStarted;
  693. }
  694. /* format connection request */
  695. req.protocol_v = jack_protocol_version;
  696. req.load = TRUE;
  697. req.type = type;
  698. snprintf (req.name, sizeof (req.name),
  699. "%s", client_name);
  700. snprintf (req.object_path, sizeof (req.object_path),
  701. "%s", va->load_name);
  702. snprintf (req.object_data, sizeof (req.object_data),
  703. "%s", va->load_init);
  704. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  705. jack_error ("cannot send request to jack server (%s)",
  706. strerror (errno));
  707. *status |= (JackFailure|JackServerError);
  708. goto fail;
  709. }
  710. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  711. if (errno == 0) {
  712. /* server shut the socket */
  713. jack_error ("could not attach as client");
  714. *status |= (JackFailure|JackServerError);
  715. goto fail;
  716. }
  717. if (errno == ECONNRESET) {
  718. jack_error ("could not attach as JACK client "
  719. "(server has exited)");
  720. *status |= (JackFailure|JackServerError);
  721. goto fail;
  722. }
  723. jack_error ("cannot read response from jack server (%s)",
  724. strerror (errno));
  725. *status |= (JackFailure|JackServerError);
  726. goto fail;
  727. }
  728. *status |= res->status; /* return server status bits */
  729. if (*status & JackFailure) {
  730. if (*status & JackVersionError) {
  731. jack_error ("client linked with incompatible libjack"
  732. " version.");
  733. }
  734. jack_error ("could not attach to JACK server");
  735. *status |= JackServerError;
  736. goto fail;
  737. }
  738. switch (type) {
  739. case ClientDriver:
  740. case ClientInternal:
  741. close (*req_fd);
  742. *req_fd = -1;
  743. break;
  744. default:
  745. break;
  746. }
  747. return 0;
  748. fail:
  749. if (*req_fd >= 0) {
  750. close (*req_fd);
  751. *req_fd = -1;
  752. }
  753. return -1;
  754. }
  755. int
  756. jack_attach_port_segment (jack_client_t *client, jack_port_type_id_t ptid)
  757. {
  758. /* Lookup, attach and register the port/buffer segments in use
  759. * right now.
  760. */
  761. if (client->control->type != ClientExternal) {
  762. jack_error("Only external clients need attach port segments");
  763. abort();
  764. }
  765. /* make sure we have space to store the port
  766. segment information.
  767. */
  768. if (ptid >= client->n_port_types) {
  769. client->port_segment = (jack_shm_info_t*)
  770. realloc (client->port_segment,
  771. sizeof (jack_shm_info_t) * (ptid+1));
  772. memset (&client->port_segment[client->n_port_types],
  773. 0,
  774. sizeof (jack_shm_info_t) *
  775. (ptid - client->n_port_types));
  776. client->n_port_types = ptid + 1;
  777. } else {
  778. /* release any previous segment */
  779. jack_release_shm (&client->port_segment[ptid]);
  780. }
  781. /* get the index into the shm registry */
  782. client->port_segment[ptid].index =
  783. client->engine->port_types[ptid].shm_registry_index;
  784. /* attach the relevant segment */
  785. if (jack_attach_shm (&client->port_segment[ptid])) {
  786. jack_error ("cannot attach port segment shared memory"
  787. " (%s)", strerror (errno));
  788. return -1;
  789. }
  790. return 0;
  791. }
  792. jack_client_t *
  793. jack_client_open (const char *client_name,
  794. jack_options_t options,
  795. jack_status_t *status, ...)
  796. {
  797. /* optional arguments: */
  798. va_list ap; /* variable argument pointer */
  799. jack_varargs_t va; /* variable arguments */
  800. int req_fd = -1;
  801. int ev_fd = -1;
  802. jack_client_connect_result_t res;
  803. jack_client_t *client;
  804. jack_port_type_id_t ptid;
  805. jack_status_t my_status;
  806. if (status == NULL) /* no status from caller? */
  807. status = &my_status; /* use local status word */
  808. *status = 0;
  809. /* validate parameters */
  810. if ((options & ~JackOpenOptions)) {
  811. *status |= (JackFailure|JackInvalidOption);
  812. return NULL;
  813. }
  814. /* parse variable arguments */
  815. va_start (ap, status);
  816. jack_varargs_parse (options, ap, &va);
  817. va_end (ap);
  818. /* External clients need to know where the tmpdir used for
  819. communication with the server lives
  820. */
  821. if (jack_get_tmpdir ()) {
  822. *status |= JackFailure;
  823. return NULL;
  824. }
  825. /* External clients need this initialized. It is already set
  826. * up in the server's address space for internal clients.
  827. */
  828. jack_init_time ();
  829. if (jack_request_client (ClientExternal, client_name, options, status,
  830. &va, &res, &req_fd)) {
  831. return NULL;
  832. }
  833. /* Allocate the jack_client_t structure in local memory.
  834. * Shared memory is not accessible yet. */
  835. client = jack_client_alloc ();
  836. strcpy (client->name, res.name);
  837. strcpy (client->fifo_prefix, res.fifo_prefix);
  838. client->request_fd = req_fd;
  839. client->pollfd[EVENT_POLL_INDEX].events =
  840. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  841. #ifndef JACK_USE_MACH_THREADS
  842. client->pollfd[WAIT_POLL_INDEX].events =
  843. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  844. #endif
  845. /* Don't access shared memory until server connected. */
  846. if (jack_initialize_shm (va.server_name)) {
  847. jack_error ("Unable to initialize shared memory.");
  848. *status |= (JackFailure|JackShmFailure);
  849. goto fail;
  850. }
  851. /* attach the engine control/info block */
  852. client->engine_shm = res.engine_shm;
  853. if (jack_attach_shm (&client->engine_shm)) {
  854. jack_error ("cannot attached engine control shared memory"
  855. " segment");
  856. goto fail;
  857. }
  858. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  859. /* initialize clock source as early as possible */
  860. jack_set_clock_source (client->engine->clock_source);
  861. /* now attach the client control block */
  862. client->control_shm = res.client_shm;
  863. if (jack_attach_shm (&client->control_shm)) {
  864. jack_error ("cannot attached client control shared memory"
  865. " segment");
  866. goto fail;
  867. }
  868. client->control = (jack_client_control_t *)
  869. jack_shm_addr (&client->control_shm);
  870. /* Nobody else needs to access this shared memory any more, so
  871. * destroy it. Because we have it attached, it won't vanish
  872. * till we exit (and release it).
  873. */
  874. jack_destroy_shm (&client->control_shm);
  875. client->n_port_types = client->engine->n_port_types;
  876. client->port_segment = (jack_shm_info_t *)
  877. malloc (sizeof (jack_shm_info_t) * client->n_port_types);
  878. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  879. client->port_segment[ptid].index =
  880. client->engine->port_types[ptid].shm_registry_index;
  881. client->port_segment[ptid].attached_at = MAP_FAILED;
  882. jack_attach_port_segment (client, ptid);
  883. }
  884. /* set up the client so that it does the right thing for an
  885. * external client
  886. */
  887. client->control->deliver_request = oop_client_deliver_request;
  888. client->control->deliver_arg = client;
  889. if ((ev_fd = server_event_connect (client, va.server_name)) < 0) {
  890. goto fail;
  891. }
  892. client->event_fd = ev_fd;
  893. #ifdef JACK_USE_MACH_THREADS
  894. /* specific resources for server/client real-time thread
  895. * communication */
  896. client->clienttask = mach_task_self();
  897. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  898. jack_error ("Can't find bootstrap port");
  899. goto fail;
  900. }
  901. if (allocate_mach_clientport(client, res.portnum) < 0) {
  902. jack_error("Can't allocate mach port");
  903. goto fail;
  904. };
  905. #endif /* JACK_USE_MACH_THREADS */
  906. return client;
  907. fail:
  908. if (client->engine) {
  909. jack_release_shm (&client->engine_shm);
  910. client->engine = 0;
  911. }
  912. if (client->control) {
  913. jack_release_shm (&client->control_shm);
  914. client->control = 0;
  915. }
  916. if (req_fd >= 0) {
  917. close (req_fd);
  918. }
  919. if (ev_fd >= 0) {
  920. close (ev_fd);
  921. }
  922. free (client);
  923. return NULL;
  924. }
  925. jack_client_t *
  926. jack_client_new (const char *client_name)
  927. {
  928. jack_options_t options = JackUseExactName;
  929. if (getenv("JACK_START_SERVER") == NULL)
  930. options |= JackNoStartServer;
  931. return jack_client_open (client_name, options, NULL);
  932. }
  933. char *
  934. jack_get_client_name (jack_client_t *client)
  935. {
  936. return client->name;
  937. }
  938. int
  939. jack_internal_client_new (const char *client_name,
  940. const char *so_name, const char *so_data)
  941. {
  942. jack_client_connect_result_t res;
  943. int req_fd;
  944. jack_varargs_t va;
  945. jack_status_t status;
  946. jack_options_t options = JackUseExactName;
  947. if (getenv("JACK_START_SERVER") == NULL)
  948. options |= JackNoStartServer;
  949. jack_varargs_init (&va);
  950. va.load_name = (char *) so_name;
  951. va.load_init = (char *) so_data;
  952. return jack_request_client (ClientInternal, client_name,
  953. options, &status, &va, &res, &req_fd);
  954. }
  955. char *
  956. jack_default_server_name (void)
  957. {
  958. char *server_name;
  959. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  960. server_name = "default";
  961. return server_name;
  962. }
  963. /* returns the name of the per-user subdirectory of jack_tmpdir */
  964. char *
  965. jack_user_dir (void)
  966. {
  967. static char user_dir[PATH_MAX+1] = "";
  968. /* format the path name on the first call */
  969. if (user_dir[0] == '\0') {
  970. snprintf (user_dir, sizeof (user_dir), "%s/jack-%d",
  971. jack_tmpdir, getuid ());
  972. }
  973. return user_dir;
  974. }
  975. /* returns the name of the per-server subdirectory of jack_user_dir() */
  976. char *
  977. jack_server_dir (const char *server_name, char *server_dir)
  978. {
  979. /* format the path name into the suppled server_dir char array,
  980. * assuming that server_dir is at least as large as PATH_MAX+1 */
  981. snprintf (server_dir, PATH_MAX+1, "%s/%s",
  982. jack_user_dir (), server_name);
  983. return server_dir;
  984. }
  985. void
  986. jack_internal_client_close (const char *client_name)
  987. {
  988. jack_client_connect_request_t req;
  989. int fd;
  990. char *server_name = jack_default_server_name ();
  991. req.load = FALSE;
  992. snprintf (req.name, sizeof (req.name), "%s", client_name);
  993. if ((fd = server_connect (server_name)) < 0) {
  994. return;
  995. }
  996. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  997. jack_error ("cannot deliver ClientUnload request to JACK "
  998. "server.");
  999. }
  1000. /* no response to this request */
  1001. close (fd);
  1002. return;
  1003. }
  1004. int
  1005. jack_recompute_total_latencies (jack_client_t* client)
  1006. {
  1007. jack_request_t request;
  1008. request.type = RecomputeTotalLatencies;
  1009. return jack_client_deliver_request (client, &request);
  1010. }
  1011. int
  1012. jack_set_freewheel (jack_client_t* client, int onoff)
  1013. {
  1014. jack_request_t request;
  1015. request.type = onoff ? FreeWheel : StopFreeWheel;
  1016. return jack_client_deliver_request (client, &request);
  1017. }
  1018. void
  1019. jack_start_freewheel (jack_client_t* client)
  1020. {
  1021. jack_client_control_t *control = client->control;
  1022. if (client->engine->real_time) {
  1023. #if JACK_USE_MACH_THREADS
  1024. jack_drop_real_time_scheduling (client->process_thread);
  1025. #else
  1026. jack_drop_real_time_scheduling (client->thread);
  1027. #endif
  1028. }
  1029. if (control->freewheel_cb) {
  1030. control->freewheel_cb (1, control->freewheel_arg);
  1031. }
  1032. }
  1033. void
  1034. jack_stop_freewheel (jack_client_t* client)
  1035. {
  1036. jack_client_control_t *control = client->control;
  1037. if (client->engine->real_time) {
  1038. #if JACK_USE_MACH_THREADS
  1039. jack_acquire_real_time_scheduling (client->process_thread,
  1040. client->engine->client_priority);
  1041. #else
  1042. jack_acquire_real_time_scheduling (client->thread,
  1043. client->engine->client_priority);
  1044. #endif
  1045. }
  1046. if (control->freewheel_cb) {
  1047. control->freewheel_cb (0, control->freewheel_arg);
  1048. }
  1049. }
  1050. static void *
  1051. jack_client_thread (void *arg)
  1052. {
  1053. jack_client_t *client = (jack_client_t *) arg;
  1054. jack_client_control_t *control = client->control;
  1055. jack_event_t event;
  1056. char status = 0;
  1057. int err = 0;
  1058. #ifndef JACK_USE_MACH_THREADS
  1059. char c = 0;
  1060. #endif
  1061. pthread_mutex_lock (&client_lock);
  1062. client->thread_ok = TRUE;
  1063. client->thread_id = pthread_self();
  1064. pthread_cond_signal (&client_ready);
  1065. pthread_mutex_unlock (&client_lock);
  1066. client->control->pid = getpid();
  1067. client->control->pgrp = getpgrp();
  1068. DEBUG ("client thread is now running");
  1069. if (client->control->thread_init) {
  1070. DEBUG ("calling client thread init callback");
  1071. client->control->thread_init (client->control->thread_init_arg);
  1072. }
  1073. while (err == 0) {
  1074. if (client->engine->engine_ok == 0) {
  1075. if (client->on_shutdown)
  1076. client->on_shutdown (client->on_shutdown_arg);
  1077. else
  1078. jack_error ("engine unexpectedly shutdown; "
  1079. "thread exiting\n");
  1080. pthread_exit (0);
  1081. }
  1082. DEBUG ("client polling on %s", client->pollmax == 2 ?
  1083. "event_fd and graph_wait_fd..." :
  1084. "event_fd only");
  1085. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  1086. if (errno == EINTR) {
  1087. continue;
  1088. }
  1089. jack_error ("poll failed in client (%s)",
  1090. strerror (errno));
  1091. status = -1;
  1092. break;
  1093. }
  1094. /* get an accurate timestamp on waking from poll for a
  1095. * process() cycle.
  1096. */
  1097. #ifndef JACK_USE_MACH_THREADS
  1098. if (client->graph_wait_fd >= 0
  1099. && client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  1100. control->awake_at = jack_get_microseconds();
  1101. }
  1102. DEBUG ("pfd[EVENT].revents = 0x%x pfd[WAIT].revents = 0x%x",
  1103. client->pollfd[EVENT_POLL_INDEX].revents,
  1104. client->pollfd[WAIT_POLL_INDEX].revents);
  1105. #endif
  1106. pthread_testcancel();
  1107. #ifndef JACK_USE_MACH_THREADS
  1108. if (client->graph_wait_fd >= 0 &&
  1109. (client->pollfd[WAIT_POLL_INDEX].revents & ~POLLIN)) {
  1110. DEBUG ("\n\n\n\n\n\n\n\nWAITFD ERROR,"
  1111. " ZOMBIE\n\n\n\n\n");
  1112. /* our upstream "wait" connection
  1113. closed, which either means that
  1114. an intermediate client exited, or
  1115. jackd exited, or jackd zombified
  1116. us.
  1117. we can discover the zombification
  1118. via client->control->dead, but
  1119. the other two possibilities are
  1120. impossible to identify just from
  1121. this situation. so we have to
  1122. check what we are connected to,
  1123. and act accordingly.
  1124. */
  1125. if (client->upstream_is_jackd) {
  1126. DEBUG ("WE DIE\n");
  1127. goto zombie;
  1128. } else {
  1129. DEBUG ("WE PUNT\n");
  1130. /* don't poll on the wait fd
  1131. * again until we get a
  1132. * GraphReordered event.
  1133. */
  1134. client->graph_wait_fd = -1;
  1135. client->pollmax = 1;
  1136. }
  1137. }
  1138. #endif
  1139. if (client->control->dead) {
  1140. goto zombie;
  1141. }
  1142. if (client->pollfd[EVENT_POLL_INDEX].revents & ~POLLIN) {
  1143. /* jackd shutdown */
  1144. goto zombie;
  1145. }
  1146. if (client->pollfd[EVENT_POLL_INDEX].revents & POLLIN) {
  1147. DEBUG ("client receives an event, "
  1148. "now reading on event fd");
  1149. /* server has sent us an event. process the
  1150. * event and reply */
  1151. if (read (client->event_fd, &event, sizeof (event))
  1152. != sizeof (event)) {
  1153. jack_error ("cannot read server event (%s)",
  1154. strerror (errno));
  1155. err++;
  1156. break;
  1157. }
  1158. status = 0;
  1159. switch (event.type) {
  1160. case PortRegistered:
  1161. if (control->port_register) {
  1162. control->port_register
  1163. (event.x.port_id, TRUE,
  1164. control->port_register_arg);
  1165. }
  1166. break;
  1167. case PortUnregistered:
  1168. if (control->port_register) {
  1169. control->port_register
  1170. (event.x.port_id, FALSE,
  1171. control->port_register_arg);
  1172. }
  1173. break;
  1174. case GraphReordered:
  1175. status = jack_handle_reorder (client, &event);
  1176. break;
  1177. case PortConnected:
  1178. case PortDisconnected:
  1179. status = jack_client_handle_port_connection
  1180. (client, &event);
  1181. break;
  1182. case BufferSizeChange:
  1183. jack_client_invalidate_port_buffers (client);
  1184. if (control->bufsize) {
  1185. status = control->bufsize
  1186. (control->nframes,
  1187. control->bufsize_arg);
  1188. }
  1189. break;
  1190. case SampleRateChange:
  1191. if (control->srate) {
  1192. status = control->srate
  1193. (control->nframes,
  1194. control->srate_arg);
  1195. }
  1196. break;
  1197. case XRun:
  1198. if (control->xrun) {
  1199. status = control->xrun
  1200. (control->xrun_arg);
  1201. }
  1202. break;
  1203. case AttachPortSegment:
  1204. jack_attach_port_segment (client, event.y.ptid);
  1205. break;
  1206. case StartFreewheel:
  1207. jack_start_freewheel (client);
  1208. break;
  1209. case StopFreewheel:
  1210. jack_stop_freewheel (client);
  1211. break;
  1212. }
  1213. DEBUG ("client has dealt with the event, writing "
  1214. "response on event fd");
  1215. if (write (client->event_fd, &status, sizeof (status))
  1216. != sizeof (status)) {
  1217. jack_error ("cannot send event response to "
  1218. "engine (%s)", strerror (errno));
  1219. err++;
  1220. break;
  1221. }
  1222. }
  1223. #ifndef JACK_USE_MACH_THREADS
  1224. if (client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  1225. #ifdef WITH_TIMESTAMPS
  1226. jack_reset_timestamps ();
  1227. #endif
  1228. DEBUG ("client %d signalled at %" PRIu64
  1229. ", awake for process at %" PRIu64
  1230. " (delay = %" PRIu64
  1231. " usecs) (wakeup on graph_wait_fd==%d)",
  1232. getpid(),
  1233. control->signalled_at,
  1234. control->awake_at,
  1235. control->awake_at - control->signalled_at,
  1236. client->pollfd[WAIT_POLL_INDEX].fd);
  1237. control->state = Running;
  1238. /* begin preemption checking */
  1239. CHECK_PREEMPTION (client->engine, TRUE);
  1240. if (control->sync_cb)
  1241. jack_call_sync_client (client);
  1242. if (control->process) {
  1243. if (control->process (control->nframes,
  1244. control->process_arg)
  1245. == 0) {
  1246. control->state = Finished;
  1247. }
  1248. } else {
  1249. control->state = Finished;
  1250. }
  1251. if (control->timebase_cb)
  1252. jack_call_timebase_master (client);
  1253. /* end preemption checking */
  1254. CHECK_PREEMPTION (client->engine, FALSE);
  1255. control->finished_at = jack_get_microseconds();
  1256. #ifdef WITH_TIMESTAMPS
  1257. jack_timestamp ("finished");
  1258. #endif
  1259. #ifndef JACK_USE_MACH_THREADS
  1260. /* pass the execution token along */
  1261. DEBUG ("client finished processing at %" PRIu64
  1262. " (elapsed = %" PRIu64
  1263. " usecs), writing on graph_next_fd==%d",
  1264. control->finished_at,
  1265. control->finished_at - control->awake_at,
  1266. client->graph_next_fd);
  1267. if (write (client->graph_next_fd, &c, sizeof (c))
  1268. != sizeof (c)) {
  1269. jack_error ("cannot continue execution of the "
  1270. "processing graph (%s)",
  1271. strerror(errno));
  1272. err++;
  1273. break;
  1274. }
  1275. DEBUG ("client sent message to next stage by %" PRIu64
  1276. ", client reading on graph_wait_fd==%d",
  1277. jack_get_microseconds(), client->graph_wait_fd);
  1278. #endif
  1279. #ifdef WITH_TIMESTAMPS
  1280. jack_timestamp ("read pending byte from wait");
  1281. #endif
  1282. DEBUG("reading cleanup byte from pipe\n");
  1283. #ifndef JACK_USE_MACH_THREADS
  1284. if ((read (client->graph_wait_fd, &c, sizeof (c))
  1285. != sizeof (c))) {
  1286. DEBUG ("WARNING: READ FAILED!");
  1287. #if 0
  1288. jack_error ("cannot complete execution of the "
  1289. "processing graph (%s)",
  1290. strerror(errno));
  1291. err++;
  1292. break;
  1293. #endif
  1294. }
  1295. #endif
  1296. /* check if we were killed during the process
  1297. * cycle (or whatever) */
  1298. if (client->control->dead) {
  1299. goto zombie;
  1300. }
  1301. DEBUG("process cycle fully complete\n");
  1302. #ifdef WITH_TIMESTAMPS
  1303. jack_timestamp ("read done");
  1304. jack_dump_timestamps (stdout);
  1305. #endif
  1306. }
  1307. #endif
  1308. }
  1309. return (void *) ((intptr_t)err);
  1310. zombie:
  1311. if (client->on_shutdown) {
  1312. jack_error ("zombified - calling shutdown handler");
  1313. client->on_shutdown (client->on_shutdown_arg);
  1314. } else {
  1315. jack_error ("jack_client_thread zombified - exiting from JACK");
  1316. jack_client_close (client);
  1317. /* Need a fix : possibly make client crash if
  1318. * zombified without shutdown handler
  1319. */
  1320. }
  1321. pthread_exit (0);
  1322. /*NOTREACHED*/
  1323. return 0;
  1324. }
  1325. #ifdef JACK_USE_MACH_THREADS
  1326. /* real-time thread : separated from the normal client thread, it will
  1327. * communicate with the server using fast mach RPC mechanism */
  1328. static void *
  1329. jack_client_process_thread (void *arg)
  1330. {
  1331. jack_client_t *client = (jack_client_t *) arg;
  1332. jack_client_control_t *control = client->control;
  1333. int err = 0;
  1334. if (client->control->thread_init) {
  1335. /* this means that the init callback will be called twice -taybin*/
  1336. DEBUG ("calling client thread init callback");
  1337. client->control->thread_init (client->control->thread_init_arg);
  1338. }
  1339. client->control->pid = getpid();
  1340. DEBUG ("client process thread is now running");
  1341. client->rt_thread_ok = TRUE;
  1342. while (err == 0) {
  1343. if (jack_client_suspend(client) < 0) {
  1344. jack_error ("jack_client_process_thread :resume error");
  1345. goto zombie;
  1346. }
  1347. control->awake_at = jack_get_microseconds();
  1348. DEBUG ("client resumed");
  1349. control->state = Running;
  1350. if (control->sync_cb)
  1351. jack_call_sync_client (client);
  1352. if (control->process) {
  1353. if (control->process (control->nframes,
  1354. control->process_arg) == 0) {
  1355. control->state = Finished;
  1356. }
  1357. } else {
  1358. control->state = Finished;
  1359. }
  1360. if (control->timebase_cb)
  1361. jack_call_timebase_master (client);
  1362. control->finished_at = jack_get_microseconds();
  1363. #ifdef WITH_TIMESTAMPS
  1364. jack_timestamp ("finished");
  1365. #endif
  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. }