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.

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