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.

2239 lines
50KB

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