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.

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