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.

2349 lines
53KB

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