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.

2165 lines
52KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. $Id$
  15. */
  16. #include <sys/socket.h>
  17. #include <sys/un.h>
  18. #include <pthread.h>
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <sys/types.h>
  22. #include <sys/ipc.h>
  23. #include <sys/shm.h>
  24. #include <sys/mman.h>
  25. #include <sys/poll.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <regex.h>
  29. #include <math.h>
  30. #include <config.h>
  31. #include <jack/jack.h>
  32. #include <jack/internal.h>
  33. #include <jack/engine.h>
  34. #include <jack/pool.h>
  35. #include <jack/error.h>
  36. #include <jack/cycles.h>
  37. #include <jack/jslist.h>
  38. #ifdef WITH_TIMESTAMPS
  39. #include <jack/timestamps.h>
  40. #endif /* WITH_TIMESTAMPS */
  41. char *jack_server_dir = "/tmp";
  42. void
  43. jack_set_server_dir (const char *path)
  44. {
  45. jack_server_dir = strdup (path);
  46. }
  47. static jack_port_t *jack_port_new (const jack_client_t *client, jack_port_id_t port_id, jack_control_t *control);
  48. static pthread_mutex_t client_lock;
  49. static pthread_cond_t client_ready;
  50. void *jack_zero_filled_buffer = 0;
  51. static void jack_audio_port_mixdown (jack_port_t *port, jack_nframes_t nframes);
  52. jack_port_type_info_t builtin_port_types[] = {
  53. { JACK_DEFAULT_AUDIO_TYPE, jack_audio_port_mixdown, 1 },
  54. { "", NULL }
  55. };
  56. struct _jack_client {
  57. jack_control_t *engine;
  58. jack_client_control_t *control;
  59. struct pollfd *pollfd;
  60. int pollmax;
  61. int graph_next_fd;
  62. int request_fd;
  63. JSList *port_segments;
  64. JSList *ports;
  65. pthread_t thread;
  66. char fifo_prefix[PATH_MAX+1];
  67. void (*on_shutdown)(void *arg);
  68. void *on_shutdown_arg;
  69. char thread_ok : 1;
  70. char first_active : 1;
  71. float cpu_mhz;
  72. pthread_t thread_id;
  73. };
  74. #define event_fd pollfd[0].fd
  75. #define graph_wait_fd pollfd[1].fd
  76. typedef struct {
  77. int status;
  78. struct _jack_client *client;
  79. const char *client_name;
  80. } client_info;
  81. static void
  82. default_jack_error (const char *fmt, ...)
  83. {
  84. va_list ap;
  85. va_start (ap, fmt);
  86. vfprintf (stderr, fmt, ap);
  87. va_end (ap);
  88. fputc ('\n', stderr);
  89. }
  90. void (*jack_error)(const char *fmt, ...) = &default_jack_error;
  91. jack_client_t *
  92. jack_client_alloc ()
  93. {
  94. jack_client_t *client;
  95. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  96. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2);
  97. client->pollmax = 2;
  98. client->request_fd = -1;
  99. client->event_fd = -1;
  100. client->graph_wait_fd = -1;
  101. client->graph_next_fd = -1;
  102. client->port_segments = NULL;
  103. client->ports = NULL;
  104. client->engine = NULL;
  105. client->control = 0;
  106. client->thread_ok = FALSE;
  107. client->first_active = TRUE;
  108. client->on_shutdown = NULL;
  109. client->cpu_mhz = (float) jack_get_mhz ();
  110. return client;
  111. }
  112. jack_port_t *
  113. jack_port_by_id (const jack_client_t *client, jack_port_id_t id)
  114. {
  115. JSList *node;
  116. for (node = client->ports; node; node = jack_slist_next (node)) {
  117. if (((jack_port_t *) node->data)->shared->id == id) {
  118. return (jack_port_t *) node->data;
  119. }
  120. }
  121. if (id >= client->engine->port_max)
  122. return NULL;
  123. if (client->engine->ports[id].in_use)
  124. return jack_port_new (client, id, client->engine);
  125. return NULL;
  126. }
  127. jack_port_t *
  128. jack_port_by_name (jack_client_t *client, const char *port_name)
  129. {
  130. unsigned long i, limit;
  131. jack_port_shared_t *port;
  132. limit = client->engine->port_max;
  133. port = &client->engine->ports[0];
  134. for (i = 0; i < limit; i++) {
  135. if (port[i].in_use && strcmp (port[i].name, port_name) == 0) {
  136. return jack_port_new (client, port[i].id, client->engine);
  137. }
  138. }
  139. return NULL;
  140. }
  141. static void
  142. jack_client_invalidate_port_buffers (jack_client_t *client)
  143. {
  144. JSList *node;
  145. jack_port_t *port;
  146. /* This releases all local memory owned by input ports
  147. and sets the buffer pointer to NULL. This will cause
  148. jack_port_get_buffer() to reallocate space for the
  149. buffer on the next call (if there is one).
  150. */
  151. for (node = client->ports; node; node = jack_slist_next (node)) {
  152. port = (jack_port_t *) node->data;
  153. if (port->shared->flags & JackPortIsInput) {
  154. if (port->client_segment_base == 0) {
  155. jack_pool_release ((void *) port->shared->offset);
  156. port->client_segment_base = 0;
  157. port->shared->offset = 0;
  158. }
  159. }
  160. }
  161. }
  162. int
  163. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  164. {
  165. jack_port_t *control_port;
  166. jack_port_t *other;
  167. JSList *node;
  168. switch (event->type) {
  169. case PortConnected:
  170. other = jack_port_new (client, event->y.other_id, client->engine);
  171. control_port = jack_port_by_id (client, event->x.self_id);
  172. pthread_mutex_lock (&control_port->connection_lock);
  173. control_port->connections = jack_slist_prepend (control_port->connections, (void*)other);
  174. pthread_mutex_unlock (&control_port->connection_lock);
  175. break;
  176. case PortDisconnected:
  177. control_port = jack_port_by_id (client, event->x.self_id);
  178. pthread_mutex_lock (&control_port->connection_lock);
  179. for (node = control_port->connections; node; node = jack_slist_next (node)) {
  180. other = (jack_port_t *) node->data;
  181. if (other->shared->id == event->y.other_id) {
  182. control_port->connections = jack_slist_remove_link (control_port->connections, node);
  183. jack_slist_free_1 (node);
  184. free (other);
  185. break;
  186. }
  187. }
  188. pthread_mutex_unlock (&control_port->connection_lock);
  189. break;
  190. default:
  191. /* impossible */
  192. break;
  193. }
  194. return 0;
  195. }
  196. static int
  197. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  198. {
  199. char path[PATH_MAX+1];
  200. if (client->graph_wait_fd >= 0) {
  201. DEBUG ("closing graph_wait_fd==%d", client->graph_wait_fd);
  202. close (client->graph_wait_fd);
  203. client->graph_wait_fd = -1;
  204. }
  205. if (client->graph_next_fd >= 0) {
  206. DEBUG ("closing graph_next_fd==%d", client->graph_next_fd);
  207. close (client->graph_next_fd);
  208. client->graph_next_fd = -1;
  209. }
  210. sprintf (path, "%s-%lu", client->fifo_prefix, event->x.n);
  211. if ((client->graph_wait_fd = open (path, O_RDONLY|O_NONBLOCK)) <= 0) {
  212. jack_error ("cannot open specified fifo [%s] for reading (%s)", path, strerror (errno));
  213. return -1;
  214. }
  215. DEBUG ("opened new graph_wait_fd %d (%s)", client->graph_wait_fd, path);
  216. sprintf (path, "%s-%lu", client->fifo_prefix, event->x.n+1);
  217. if ((client->graph_next_fd = open (path, O_WRONLY|O_NONBLOCK)) < 0) {
  218. jack_error ("cannot open specified fifo [%s] for writing (%s)", path, strerror (errno));
  219. return -1;
  220. }
  221. DEBUG ("opened new graph_next_fd %d (%s)", client->graph_next_fd, path);
  222. /* If the client registered its own callback for graph order events,
  223. execute it now.
  224. */
  225. if (client->control->graph_order) {
  226. client->control->graph_order (client->control->graph_order_arg);
  227. }
  228. return 0;
  229. }
  230. static int
  231. server_connect (int which)
  232. {
  233. int fd;
  234. struct sockaddr_un addr;
  235. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  236. jack_error ("cannot create client socket (%s)", strerror (errno));
  237. return -1;
  238. }
  239. addr.sun_family = AF_UNIX;
  240. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d", jack_server_dir, which);
  241. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  242. jack_error ("cannot connect to jack server", strerror (errno));
  243. close (fd);
  244. return -1;
  245. }
  246. return fd;
  247. }
  248. static int
  249. server_event_connect (jack_client_t *client)
  250. {
  251. int fd;
  252. struct sockaddr_un addr;
  253. jack_client_connect_ack_request_t req;
  254. jack_client_connect_ack_result_t res;
  255. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  256. jack_error ("cannot create client event socket (%s)", strerror (errno));
  257. return -1;
  258. }
  259. addr.sun_family = AF_UNIX;
  260. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0", jack_server_dir);
  261. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  262. jack_error ("cannot connect to jack server for events", strerror (errno));
  263. close (fd);
  264. return -1;
  265. }
  266. req.client_id = client->control->id;
  267. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  268. jack_error ("cannot write event connect request to server (%s)", strerror (errno));
  269. close (fd);
  270. return -1;
  271. }
  272. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  273. jack_error ("cannot read event connect result from server (%s)", strerror (errno));
  274. close (fd);
  275. return -1;
  276. }
  277. if (res.status != 0) {
  278. close (fd);
  279. return -1;
  280. }
  281. return fd;
  282. }
  283. jack_client_t *
  284. jack_client_new (const char *client_name)
  285. {
  286. int req_fd = -1;
  287. int ev_fd = -1;
  288. void *addr;
  289. jack_client_connect_request_t req;
  290. jack_client_connect_result_t res;
  291. jack_port_segment_info_t *si;
  292. jack_client_t *client;
  293. int client_shm_id;
  294. int control_shm_id;
  295. int port_segment_shm_id;
  296. int n;
  297. if (strlen (client_name) > sizeof (req.name) - 1) {
  298. jack_error ("\"%s\" is too long to be used as a JACK client name.\n"
  299. "Please use %lu characters or less.",
  300. sizeof (req.name) - 1);
  301. return NULL;
  302. }
  303. if ((req_fd = server_connect (0)) < 0) {
  304. jack_error ("cannot connect to default JACK server");
  305. return NULL;
  306. }
  307. req.type = ClientOutOfProcess;
  308. strncpy (req.name, client_name, sizeof (req.name) - 1);
  309. if (write (req_fd, &req, sizeof (req)) != sizeof (req)) {
  310. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  311. close (req_fd);
  312. return NULL;
  313. }
  314. if ((n = read (req_fd, &res, sizeof (res))) != sizeof (res)) {
  315. if (errno == 0) {
  316. /* server shut the socket */
  317. jack_error ("could not attach as client (duplicate client name?)");
  318. close (req_fd);
  319. return NULL;
  320. }
  321. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  322. close (req_fd);
  323. return NULL;
  324. }
  325. if (res.status) {
  326. close (req_fd);
  327. jack_error ("could not attach as client (duplicate client name?)");
  328. return NULL;
  329. }
  330. client = jack_client_alloc ();
  331. strcpy (client->fifo_prefix, res.fifo_prefix);
  332. client->request_fd = req_fd;
  333. client->pollfd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  334. client->pollfd[1].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  335. /* Lookup, attach and register the port/buffer segments in use
  336. right now.
  337. */
  338. if ((port_segment_shm_id = shmget (res.port_segment_key, 0, 0)) < 0) {
  339. jack_error ("cannot determine shared memory segment for port segment key 0x%x (%s)", res.port_segment_key, strerror (errno));
  340. goto fail;
  341. }
  342. if ((addr = shmat (port_segment_shm_id, 0, 0)) == (void *) -1) {
  343. jack_error ("cannot attached port segment shared memory (%s)", strerror (errno));
  344. goto fail;
  345. }
  346. si = (jack_port_segment_info_t *) malloc (sizeof (jack_port_segment_info_t));
  347. si->shm_key = res.port_segment_key;
  348. si->address = addr;
  349. /* the first chunk of the first port segment is always set by the engine
  350. to be a conveniently-sized, zero-filled lump of memory.
  351. */
  352. if (client->port_segments == NULL) {
  353. jack_zero_filled_buffer = si->address;
  354. }
  355. client->port_segments = jack_slist_prepend (client->port_segments, si);
  356. /* attach the engine control/info block */
  357. if ((control_shm_id = shmget (res.control_key, 0, 0)) < 0) {
  358. jack_error ("cannot determine shared memory segment for control key 0x%x", res.control_key);
  359. goto fail;
  360. }
  361. if ((addr = shmat (control_shm_id, 0, 0)) == (void *) -1) {
  362. jack_error ("cannot attached engine control shared memory segment");
  363. goto fail;
  364. }
  365. client->engine = (jack_control_t *) addr;
  366. /* now attach the client control block */
  367. if ((client_shm_id = shmget (res.client_key, 0, 0)) < 0) {
  368. jack_error ("cannot determine shared memory segment for client key 0x%x", res.client_key);
  369. goto fail;
  370. }
  371. if ((addr = shmat (client_shm_id, 0, 0)) == (void *) -1) {
  372. jack_error ("cannot attached client control shared memory segment");
  373. goto fail;
  374. }
  375. client->control = (jack_client_control_t *) addr;
  376. if ((ev_fd = server_event_connect (client)) < 0) {
  377. jack_error ("cannot connect to server for event stream (%s)", strerror (errno));
  378. goto fail;
  379. }
  380. client->event_fd = ev_fd;
  381. return client;
  382. fail:
  383. if (client->engine) {
  384. shmdt (client->engine);
  385. }
  386. if (client->control) {
  387. shmdt ((char *) client->control);
  388. }
  389. if (req_fd >= 0) {
  390. close (req_fd);
  391. }
  392. if (ev_fd >= 0) {
  393. close (ev_fd);
  394. }
  395. return 0;
  396. }
  397. static void *
  398. jack_client_thread (void *arg)
  399. {
  400. jack_client_t *client = (jack_client_t *) arg;
  401. jack_client_control_t *control = client->control;
  402. jack_event_t event;
  403. char status = 0;
  404. char c;
  405. int err = 0;
  406. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  407. pthread_mutex_lock (&client_lock);
  408. client->thread_ok = TRUE;
  409. client->thread_id = pthread_self();
  410. pthread_cond_signal (&client_ready);
  411. pthread_mutex_unlock (&client_lock);
  412. /* XXX reset the PID to be the actual client thread. Kai and Fernando know
  413. about this and it needs fixing.
  414. */
  415. client->control->pid = getpid()
  416. DEBUG ("client thread is now running");
  417. while (err == 0) {
  418. if (client->engine->engine_ok == 0) {
  419. jack_error ("engine unexpectedly shutdown; thread exiting\n");
  420. if (client->on_shutdown) {
  421. client->on_shutdown (client->on_shutdown_arg);
  422. }
  423. pthread_exit (0);
  424. }
  425. DEBUG ("client polling on event_fd and graph_wait_fd...");
  426. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  427. if (errno == EINTR) {
  428. printf ("poll interrupted\n");
  429. continue;
  430. }
  431. jack_error ("poll failed in client (%s)", strerror (errno));
  432. status = -1;
  433. break;
  434. }
  435. /* get an accurate timestamp on waking from poll for a process()
  436. cycle.
  437. */
  438. if (client->pollfd[1].revents & POLLIN) {
  439. control->awake_at = get_cycles();
  440. }
  441. if (client->pollfd[0].revents & ~POLLIN || client->control->dead) {
  442. jack_error ("engine has shut down socket; thread exiting");
  443. if (client->on_shutdown) {
  444. client->on_shutdown (client->on_shutdown_arg);
  445. }
  446. pthread_exit (0);
  447. }
  448. if (client->pollfd[0].revents & POLLIN) {
  449. DEBUG ("client receives an event, now reading on event fd");
  450. /* server has sent us an event. process the event and reply */
  451. if (read (client->event_fd, &event, sizeof (event)) != sizeof (event)) {
  452. jack_error ("cannot read server event (%s)", strerror (errno));
  453. err++;
  454. break;
  455. }
  456. status = 0;
  457. switch (event.type) {
  458. case PortRegistered:
  459. if (control->port_register) {
  460. control->port_register (event.x.port_id, TRUE, control->port_register_arg);
  461. }
  462. break;
  463. case PortUnregistered:
  464. if (control->port_register) {
  465. control->port_register (event.x.port_id, FALSE, control->port_register_arg);
  466. }
  467. break;
  468. case GraphReordered:
  469. status = jack_handle_reorder (client, &event);
  470. break;
  471. case PortConnected:
  472. case PortDisconnected:
  473. status = jack_client_handle_port_connection (client, &event);
  474. break;
  475. case BufferSizeChange:
  476. jack_client_invalidate_port_buffers (client);
  477. if (control->bufsize) {
  478. status = control->bufsize (control->nframes, control->bufsize_arg);
  479. }
  480. break;
  481. case SampleRateChange:
  482. if (control->srate) {
  483. status = control->srate (control->nframes, control->srate_arg);
  484. }
  485. break;
  486. case XRun:
  487. if (control->xrun) {
  488. status = control->xrun (control->xrun_arg);
  489. }
  490. break;
  491. case NewPortBufferSegment:
  492. break;
  493. }
  494. DEBUG ("client has dealt with the event, writing response on event fd");
  495. if (write (client->event_fd, &status, sizeof (status)) != sizeof (status)) {
  496. jack_error ("cannot send event response to engine (%s)", strerror (errno));
  497. err++;
  498. break;
  499. }
  500. }
  501. if (client->pollfd[1].revents & POLLIN) {
  502. #ifdef WITH_TIMESTAMPS
  503. jack_reset_timestamps ();
  504. #endif
  505. DEBUG ("client %d signalled at %Lu, awake for process at %Lu (delay = %f usecs) (wakeup on graph_wait_fd==%d)",
  506. getpid(),
  507. control->signalled_at,
  508. control->awake_at,
  509. ((float) (control->awake_at - control->signalled_at))/client->cpu_mhz,
  510. client->pollfd[1].fd);
  511. control->state = Running;
  512. if (control->process) {
  513. if (control->process (control->nframes, control->process_arg) == 0) {
  514. control->state = Finished;
  515. }
  516. } else {
  517. control->state = Finished;
  518. }
  519. control->finished_at = get_cycles();
  520. #ifdef WITH_TIMESTAMPS
  521. jack_timestamp ("finished");
  522. #endif
  523. /* pass the execution token along */
  524. DEBUG ("client finished processing at %Lu (elapsed = %f usecs), writing on graph_next_fd==%d",
  525. control->finished_at,
  526. ((float)(control->finished_at - control->awake_at)/client->cpu_mhz),
  527. client->graph_next_fd);
  528. if (write (client->graph_next_fd, &c, sizeof (c)) != sizeof (c)) {
  529. jack_error ("cannot continue execution of the processing graph (%s)", strerror(errno));
  530. err++;
  531. break;
  532. }
  533. DEBUG ("client sent message to next stage by %Lu, client reading on graph_wait_fd==%d", get_cycles(), client->graph_wait_fd);
  534. #ifdef WITH_TIMESTAMPS
  535. jack_timestamp ("read pending byte from wait");
  536. #endif
  537. DEBUG("reading cleanup byte from pipe\n");
  538. if ((read (client->graph_wait_fd, &c, sizeof (c)) != sizeof (c))) {
  539. DEBUG ("WARNING: READ FAILED!");
  540. /*
  541. jack_error ("cannot complete execution of the processing graph (%s)", strerror(errno));
  542. err++;
  543. break;
  544. */
  545. }
  546. DEBUG("process cycle fully complete\n");
  547. #ifdef WITH_TIMESTAMPS
  548. jack_timestamp ("read done");
  549. #endif
  550. }
  551. }
  552. return (void *) err;
  553. }
  554. static int
  555. jack_start_thread (jack_client_t *client)
  556. {
  557. pthread_attr_t *attributes = 0;
  558. #ifdef USE_CAPABILITIES
  559. int policy = SCHED_OTHER;
  560. struct sched_param client_param, temp_param;
  561. #endif
  562. if (client->engine->real_time) {
  563. /* Get the client thread to run as an RT-FIFO
  564. scheduled thread of appropriate priority.
  565. */
  566. struct sched_param rt_param;
  567. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  568. pthread_attr_init (attributes);
  569. if (pthread_attr_setschedpolicy (attributes, SCHED_FIFO)) {
  570. jack_error ("cannot set FIFO scheduling class for RT thread");
  571. return -1;
  572. }
  573. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  574. jack_error ("Cannot set scheduling scope for RT thread");
  575. return -1;
  576. }
  577. memset (&rt_param, 0, sizeof (rt_param));
  578. rt_param.sched_priority = client->engine->client_priority;
  579. if (pthread_attr_setschedparam (attributes, &rt_param)) {
  580. jack_error ("Cannot set scheduling priority for RT thread (%s)", strerror (errno));
  581. return -1;
  582. }
  583. if (mlockall (MCL_CURRENT|MCL_FUTURE)) {
  584. jack_error ("cannot lock down all memory (%s)", strerror (errno));
  585. return -1;
  586. }
  587. }
  588. if (pthread_create (&client->thread, attributes, jack_client_thread, client)) {
  589. #ifdef USE_CAPABILITIES
  590. if (client->engine->real_time && client->engine->has_capabilities) {
  591. /* we are probably dealing with a broken glibc so try
  592. to work around the bug, see below for more details
  593. */
  594. goto capabilities_workaround;
  595. }
  596. #endif
  597. return -1;
  598. }
  599. return 0;
  600. #ifdef USE_CAPABILITIES
  601. /* we get here only with engine running realtime and capabilities */
  602. capabilities_workaround:
  603. /* the version of glibc I've played with has a bug that makes
  604. that code fail when running under a non-root user but with the
  605. proper realtime capabilities (in short, pthread_attr_setschedpolicy
  606. does not check for capabilities, only for the uid being
  607. zero). Newer versions apparently have this fixed. This
  608. workaround temporarily switches the client thread to the
  609. proper scheduler and priority, then starts the realtime
  610. thread so that it can inherit them and finally switches the
  611. client thread back to what it was before. Sigh. For ardour
  612. I have to check again and switch the thread explicitly to
  613. realtime, don't know why or how to debug - nando
  614. */
  615. /* get current scheduler and parameters of the client process */
  616. if ((policy = sched_getscheduler (0)) < 0) {
  617. jack_error ("Cannot get current client scheduler: %s", strerror(errno));
  618. return -1;
  619. }
  620. memset (&client_param, 0, sizeof (client_param));
  621. if (sched_getparam (0, &client_param)) {
  622. jack_error ("Cannot get current client scheduler parameters: %s", strerror(errno));
  623. return -1;
  624. }
  625. /* temporarily change the client process to SCHED_FIFO so that
  626. the realtime thread can inherit the scheduler and priority
  627. */
  628. memset (&temp_param, 0, sizeof (temp_param));
  629. temp_param.sched_priority = client->engine->client_priority;
  630. if (sched_setscheduler(0, SCHED_FIFO, &temp_param)) {
  631. jack_error ("Cannot temporarily set client to RT scheduler: %s", strerror(errno));
  632. return -1;
  633. }
  634. /* prepare the attributes for the realtime thread */
  635. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  636. pthread_attr_init (attributes);
  637. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  638. sched_setscheduler (0, policy, &client_param);
  639. jack_error ("Cannot set scheduling scope for RT thread");
  640. return -1;
  641. }
  642. if (pthread_attr_setinheritsched (attributes, PTHREAD_INHERIT_SCHED)) {
  643. sched_setscheduler (0, policy, &client_param);
  644. jack_error ("Cannot set scheduler inherit policy for RT thread");
  645. return -1;
  646. }
  647. /* create the RT thread */
  648. if (pthread_create (&client->thread, attributes, jack_client_thread, client)) {
  649. sched_setscheduler (0, policy, &client_param);
  650. return -1;
  651. }
  652. /* return the client process to the scheduler it was in before */
  653. if (sched_setscheduler (0, policy, &client_param)) {
  654. jack_error ("Cannot reset original client scheduler: %s", strerror(errno));
  655. return -1;
  656. }
  657. /* check again... inheritance of policy and priority works in jack_simple_client
  658. but not in ardour! So I check again and force the policy if it is not set
  659. correctly. This does not really really work either, the manager thread
  660. of the linuxthreads implementation is left running with SCHED_OTHER,
  661. that is presumably very bad.
  662. */
  663. memset (&client_param, 0, sizeof (client_param));
  664. if (pthread_getschedparam(client->thread, &policy, &client_param) == 0) {
  665. if (policy != SCHED_FIFO) {
  666. /* jack_error ("RT thread did not go SCHED_FIFO, trying again"); */
  667. memset (&client_param, 0, sizeof (client_param));
  668. client_param.sched_priority = client->engine->client_priority;
  669. if (pthread_setschedparam (client->thread, SCHED_FIFO, &client_param)) {
  670. jack_error ("Cannot set (again) FIFO scheduling class for RT thread\n");
  671. return -1;
  672. }
  673. }
  674. }
  675. return 0;
  676. #endif
  677. }
  678. int
  679. jack_activate (jack_client_t *client)
  680. {
  681. jack_request_t req;
  682. /* we need to scribble on our stack to ensure that its memory pages are
  683. * actually mapped (more important for mlockall(2) usage in
  684. * jack_start_thread())
  685. */
  686. #define BIG_ENOUGH_STACK 1048576
  687. char buf[BIG_ENOUGH_STACK];
  688. int i;
  689. for (i = 0; i < BIG_ENOUGH_STACK; i++) {
  690. buf[i] = (char) (i & 0xff);
  691. }
  692. #undef BIG_ENOUGH_STACK
  693. /* get the pid of the client process to pass it to engine */
  694. client->control->pid = getpid ();
  695. #ifdef USE_CAPABILITIES
  696. if (client->engine->has_capabilities != 0 &&
  697. client->control->pid != 0 && client->engine->real_time != 0) {
  698. /* we need to ask the engine for realtime capabilities
  699. before trying to start the realtime thread
  700. */
  701. req.type = SetClientCapabilities;
  702. req.x.client_id = client->control->id;
  703. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  704. jack_error ("cannot send set client capabilities request to server");
  705. return -1;
  706. }
  707. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  708. jack_error ("cannot read set client capabilities result from server (%s)", strerror (errno));
  709. return -1;
  710. }
  711. if (req.status) {
  712. /* what to do? engine is running realtime, it is using capabilities and has
  713. them (otherwise we would not get an error return) but for some reason it
  714. could not give the client the required capabilities, so for now downgrade
  715. the client so that it still runs, albeit non-realtime - nando
  716. */
  717. jack_error ("could not receive realtime capabilities, client will run non-realtime");
  718. /* XXX wrong, this is a property of the engine
  719. client->engine->real_time = 0;
  720. */
  721. }
  722. }
  723. #endif
  724. if (client->control->type == ClientOutOfProcess && client->first_active) {
  725. pthread_mutex_init (&client_lock, NULL);
  726. pthread_cond_init (&client_ready, NULL);
  727. pthread_mutex_lock (&client_lock);
  728. if (jack_start_thread (client)) {
  729. pthread_mutex_unlock (&client_lock);
  730. return -1;
  731. }
  732. pthread_cond_wait (&client_ready, &client_lock);
  733. pthread_mutex_unlock (&client_lock);
  734. if (!client->thread_ok) {
  735. jack_error ("could not start client thread");
  736. return -1;
  737. }
  738. client->first_active = FALSE;
  739. }
  740. req.type = ActivateClient;
  741. req.x.client_id = client->control->id;
  742. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  743. jack_error ("cannot send activate client request to server");
  744. return -1;
  745. }
  746. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  747. jack_error ("cannot read activate client result from server (%s)", strerror (errno));
  748. return -1;
  749. }
  750. return req.status;
  751. }
  752. int
  753. jack_deactivate (jack_client_t *client)
  754. {
  755. jack_request_t req;
  756. req.type = DeactivateClient;
  757. req.x.client_id = client->control->id;
  758. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  759. jack_error ("cannot send deactivate client request to server");
  760. return -1;
  761. }
  762. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  763. jack_error ("cannot read deactivate client result from server (%s)", strerror (errno));
  764. return -1;
  765. }
  766. return req.status;
  767. }
  768. int
  769. jack_client_close (jack_client_t *client)
  770. {
  771. JSList *node;
  772. void *status;
  773. jack_deactivate (client);
  774. /* stop the thread that communicates with the jack server */
  775. pthread_cancel (client->thread);
  776. pthread_join (client->thread, &status);
  777. shmdt ((char *) client->control);
  778. shmdt (client->engine);
  779. for (node = client->port_segments; node; node = jack_slist_next (node)) {
  780. shmdt (((jack_port_segment_info_t *) node->data)->address);
  781. free (node->data);
  782. }
  783. jack_slist_free (client->port_segments);
  784. for (node = client->ports; node; node = jack_slist_next (node)) {
  785. free (node->data);
  786. }
  787. jack_slist_free (client->ports);
  788. if (client->graph_wait_fd) {
  789. close (client->graph_wait_fd);
  790. }
  791. if (client->graph_next_fd) {
  792. close (client->graph_next_fd);
  793. }
  794. close (client->event_fd);
  795. close (client->request_fd);
  796. free (client->pollfd);
  797. free (client);
  798. return 0;
  799. }
  800. int
  801. jack_load_client (const char *client_name, const char *path_to_so)
  802. {
  803. int fd;
  804. jack_client_connect_request_t req;
  805. jack_client_connect_result_t res;
  806. if ((fd = server_connect (0)) < 0) {
  807. jack_error ("cannot connect to jack server");
  808. return 0;
  809. }
  810. req.type = ClientDynamic;
  811. strncpy (req.name, client_name, sizeof (req.name) - 1);
  812. req.name[sizeof(req.name)-1] = '\0';
  813. strncpy (req.object_path, path_to_so, sizeof (req.name) - 1);
  814. req.object_path[sizeof(req.object_path)-1] = '\0';
  815. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  816. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  817. close (fd);
  818. return 0;
  819. }
  820. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  821. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  822. close (fd);
  823. return 0;
  824. }
  825. close (fd);
  826. return res.status;
  827. }
  828. jack_client_t *
  829. jack_driver_become_client (const char *client_name)
  830. {
  831. int fd;
  832. jack_client_connect_request_t req;
  833. jack_client_connect_result_t res;
  834. jack_client_t *client = 0;
  835. int port_segment_shm_id;
  836. jack_port_segment_info_t *si;
  837. void *addr;
  838. if ((fd = server_connect (0)) < 0) {
  839. jack_error ("cannot connect to jack server");
  840. return 0;
  841. }
  842. req.type = ClientDriver;
  843. strncpy (req.name, client_name, sizeof (req.name) - 1);
  844. req.name[sizeof(req.name)-1] = '\0';
  845. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  846. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  847. close (fd);
  848. return 0;
  849. }
  850. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  851. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  852. close (fd);
  853. return 0;
  854. }
  855. if (res.status) {
  856. return 0;
  857. }
  858. client = jack_client_alloc ();
  859. client->request_fd = fd;
  860. client->control = res.client_control;
  861. client->engine = res.engine_control;
  862. /* Lookup, attach and register the port/buffer segments in use
  863. right now.
  864. */
  865. if ((port_segment_shm_id = shmget (res.port_segment_key, 0, 0)) < 0) {
  866. jack_error ("cannot determine shared memory segment for port segment key 0x%x (%s)", res.port_segment_key, strerror (errno));
  867. return NULL;
  868. }
  869. if ((addr = shmat (port_segment_shm_id, 0, 0)) == (void *) -1) {
  870. jack_error ("cannot attached port segment shared memory (%s)", strerror (errno));
  871. return NULL;
  872. }
  873. si = (jack_port_segment_info_t *) malloc (sizeof (jack_port_segment_info_t));
  874. si->shm_key = res.port_segment_key;
  875. si->address = addr;
  876. /* the first chunk of the first port segment is always set by the engine
  877. to be a conveniently-sized, zero-filled lump of memory.
  878. */
  879. if (client->port_segments == NULL) {
  880. jack_zero_filled_buffer = si->address;
  881. }
  882. client->port_segments = jack_slist_prepend (client->port_segments, si);
  883. /* allow the engine to act on the client's behalf
  884. when dealing with in-process clients.
  885. */
  886. client->control->private_internal_client = client;
  887. return client;
  888. }
  889. unsigned long jack_get_buffer_size (jack_client_t *client)
  890. {
  891. return client->engine->buffer_size;
  892. }
  893. unsigned long jack_get_sample_rate (jack_client_t *client)
  894. {
  895. return client->engine->current_time.frame_rate;
  896. }
  897. static jack_port_t *
  898. jack_port_new (const jack_client_t *client, jack_port_id_t port_id, jack_control_t *control)
  899. {
  900. jack_port_t *port;
  901. jack_port_shared_t *shared;
  902. jack_port_segment_info_t *si;
  903. JSList *node;
  904. shared = &control->ports[port_id];
  905. port = (jack_port_t *) malloc (sizeof (jack_port_t));
  906. port->client_segment_base = 0;
  907. port->shared = shared;
  908. pthread_mutex_init (&port->connection_lock, NULL);
  909. port->connections = 0;
  910. port->shared->tied = NULL;
  911. si = NULL;
  912. for (node = client->port_segments; node; node = jack_slist_next (node)) {
  913. si = (jack_port_segment_info_t *) node->data;
  914. if (si->shm_key == port->shared->shm_key) {
  915. break;
  916. }
  917. }
  918. if (si == NULL) {
  919. jack_error ("cannot find port segment to match newly registered port\n");
  920. return NULL;
  921. }
  922. port->client_segment_base = si->address;
  923. return port;
  924. }
  925. jack_port_t *
  926. jack_port_register (jack_client_t *client,
  927. const char *port_name,
  928. const char *port_type,
  929. unsigned long flags,
  930. unsigned long buffer_size)
  931. {
  932. jack_request_t req;
  933. jack_port_t *port = 0;
  934. jack_port_type_info_t *type_info;
  935. int n;
  936. req.type = RegisterPort;
  937. strcpy ((char *) req.x.port_info.name, (const char *) client->control->name);
  938. strcat ((char *) req.x.port_info.name, ":");
  939. strcat ((char *) req.x.port_info.name, port_name);
  940. strncpy (req.x.port_info.type, port_type, sizeof (req.x.port_info.type) - 1);
  941. req.x.port_info.flags = flags;
  942. req.x.port_info.buffer_size = buffer_size;
  943. req.x.port_info.client_id = client->control->id;
  944. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  945. jack_error ("cannot send port registration request to server");
  946. return 0;
  947. }
  948. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  949. jack_error ("cannot read port registration result from server");
  950. return 0;
  951. }
  952. if (req.status != 0) {
  953. return NULL;
  954. }
  955. port = jack_port_new (client, req.x.port_info.port_id, client->engine);
  956. type_info = NULL;
  957. for (n = 0; builtin_port_types[n].type_name[0]; n++) {
  958. if (strcmp (req.x.port_info.type, builtin_port_types[n].type_name) == 0) {
  959. type_info = &builtin_port_types[n];
  960. break;
  961. }
  962. }
  963. if (type_info == NULL) {
  964. /* not a builtin type, so allocate a new type_info structure,
  965. and fill it appropriately.
  966. */
  967. type_info = (jack_port_type_info_t *) malloc (sizeof (jack_port_type_info_t));
  968. snprintf ((char *) type_info->type_name, sizeof (type_info->type_name), req.x.port_info.type);
  969. type_info->mixdown = NULL; /* we have no idea how to mix this */
  970. type_info->buffer_scale_factor = -1; /* use specified port buffer size */
  971. }
  972. memcpy (&port->shared->type_info, type_info, sizeof (jack_port_type_info_t));
  973. client->ports = jack_slist_prepend (client->ports, port);
  974. return port;
  975. }
  976. int
  977. jack_port_unregister (jack_client_t *client, jack_port_t *port)
  978. {
  979. jack_request_t req;
  980. req.type = UnRegisterPort;
  981. req.x.port_info.port_id = port->shared->id;
  982. req.x.port_info.client_id = client->control->id;
  983. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  984. jack_error ("cannot send port registration request to server");
  985. return -1;
  986. }
  987. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  988. jack_error ("cannot read port registration result from server");
  989. return -1;
  990. }
  991. return req.status;
  992. }
  993. int
  994. jack_connect (jack_client_t *client, const char *source_port, const char *destination_port)
  995. {
  996. jack_request_t req;
  997. req.type = ConnectPorts;
  998. strncpy (req.x.connect.source_port, source_port, sizeof (req.x.connect.source_port) - 1);
  999. req.x.connect.source_port[sizeof(req.x.connect.source_port) - 1] = '\0';
  1000. strncpy (req.x.connect.destination_port, destination_port, sizeof (req.x.connect.destination_port) - 1);
  1001. req.x.connect.destination_port[sizeof(req.x.connect.destination_port) - 1] = '\0';
  1002. DEBUG ("writing to request_fd");
  1003. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1004. jack_error ("cannot send port connection request to server");
  1005. return -1;
  1006. }
  1007. DEBUG ("reading from request_fd");
  1008. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1009. jack_error ("cannot read port connection result from server");
  1010. return -1;
  1011. }
  1012. DEBUG ("connected: %d", req.status);
  1013. return req.status;
  1014. }
  1015. int
  1016. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1017. {
  1018. jack_request_t req;
  1019. pthread_mutex_lock (&port->connection_lock);
  1020. if (port->connections == NULL) {
  1021. pthread_mutex_unlock (&port->connection_lock);
  1022. return 0;
  1023. }
  1024. pthread_mutex_unlock (&port->connection_lock);
  1025. req.type = DisconnectPort;
  1026. req.x.port_info.port_id = port->shared->id;
  1027. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1028. jack_error ("cannot send port disconnect request to server");
  1029. return -1;
  1030. }
  1031. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1032. jack_error ("cannot read port disconnect result from server");
  1033. return -1;
  1034. }
  1035. return req.status;
  1036. }
  1037. int
  1038. jack_disconnect (jack_client_t *client, const char *source_port, const char *destination_port)
  1039. {
  1040. jack_request_t req;
  1041. req.type = DisconnectPorts;
  1042. strncpy (req.x.connect.source_port, source_port, sizeof (req.x.connect.source_port) - 1);
  1043. req.x.connect.source_port[sizeof(req.x.connect.source_port) - 1] = '\0';
  1044. strncpy (req.x.connect.destination_port, destination_port, sizeof (req.x.connect.destination_port) - 1);
  1045. req.x.connect.destination_port[sizeof(req.x.connect.destination_port) - 1] = '\0';
  1046. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1047. jack_error ("cannot send port connection request to server");
  1048. return -1;
  1049. }
  1050. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1051. jack_error ("cannot read port connection result from server");
  1052. return -1;
  1053. }
  1054. return req.status;
  1055. }
  1056. int
  1057. jack_engine_takeover_timebase (jack_client_t *client)
  1058. {
  1059. jack_request_t req;
  1060. req.type = SetTimeBaseClient;
  1061. req.x.client_id = client->control->id;
  1062. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1063. jack_error ("cannot send set time base request to server");
  1064. return -1;
  1065. }
  1066. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1067. jack_error ("cannot read set time base result from server");
  1068. return -1;
  1069. }
  1070. return req.status;
  1071. }
  1072. void
  1073. jack_set_error_function (void (*func) (const char *, ...))
  1074. {
  1075. jack_error = func;
  1076. }
  1077. jack_nframes_t
  1078. jack_port_get_latency (jack_port_t *port)
  1079. {
  1080. return port->shared->latency;
  1081. }
  1082. void
  1083. jack_port_set_latency (jack_port_t *port, jack_nframes_t nframes)
  1084. {
  1085. port->shared->latency = nframes;
  1086. }
  1087. void *
  1088. jack_port_get_buffer (jack_port_t *port, jack_nframes_t nframes)
  1089. {
  1090. JSList *node, *next;
  1091. /* Output port. The buffer was assigned by the engine
  1092. when the port was registered.
  1093. */
  1094. if (port->shared->flags & JackPortIsOutput) {
  1095. if (port->shared->tied) {
  1096. return jack_port_get_buffer (port->shared->tied, nframes);
  1097. }
  1098. return jack_port_buffer (port);
  1099. }
  1100. /* Input port.
  1101. */
  1102. /* since this can only be called from the process() callback,
  1103. and since no connections can be made/broken during this
  1104. phase (enforced by the jack server), there is no need
  1105. to take the connection lock here
  1106. */
  1107. if ((node = port->connections) == NULL) {
  1108. /* no connections; return a zero-filled buffer */
  1109. return jack_zero_filled_buffer;
  1110. }
  1111. if ((next = jack_slist_next (node)) == NULL) {
  1112. /* one connection: use zero-copy mode - just pass
  1113. the buffer of the connected (output) port.
  1114. */
  1115. return jack_port_get_buffer (((jack_port_t *) node->data), nframes);
  1116. }
  1117. /* multiple connections. use a local buffer and mixdown
  1118. the incoming data to that buffer. we have already
  1119. established the existence of a mixdown function
  1120. during the connection process.
  1121. no port can have an offset of 0 - that offset refers
  1122. to the zero-filled area at the start of a shared port
  1123. segment area. so, use the offset to store the location
  1124. of a locally allocated buffer, and reset the client_segment_base
  1125. so that the jack_port_buffer() computation works correctly.
  1126. */
  1127. if (port->shared->offset == 0) {
  1128. port->shared->offset = (size_t) jack_pool_alloc (port->shared->type_info.buffer_scale_factor *
  1129. sizeof (jack_default_audio_sample_t) * nframes);
  1130. port->client_segment_base = 0;
  1131. }
  1132. port->shared->type_info.mixdown (port, nframes);
  1133. return (jack_default_audio_sample_t *) port->shared->offset;
  1134. }
  1135. int
  1136. jack_port_tie (jack_port_t *src, jack_port_t *dst)
  1137. {
  1138. if (dst->shared->client_id != src->shared->client_id) {
  1139. jack_error ("cannot tie ports not owned by the same client");
  1140. return -1;
  1141. }
  1142. if (dst->shared->flags & JackPortIsOutput) {
  1143. jack_error ("cannot tie an input port");
  1144. return -1;
  1145. }
  1146. dst->shared->tied = src;
  1147. return 0;
  1148. }
  1149. int
  1150. jack_port_untie (jack_port_t *port)
  1151. {
  1152. if (port->shared->tied == NULL) {
  1153. jack_error ("port \"%s\" is not tied", port->shared->name);
  1154. return -1;
  1155. }
  1156. port->shared->tied = NULL;
  1157. return 0;
  1158. }
  1159. int
  1160. jack_set_graph_order_callback (jack_client_t *client, JackGraphOrderCallback callback, void *arg)
  1161. {
  1162. if (client->control->active) {
  1163. jack_error ("You cannot set callbacks on an active client.");
  1164. return -1;
  1165. }
  1166. client->control->graph_order = callback;
  1167. client->control->graph_order_arg = arg;
  1168. return 0;
  1169. }
  1170. int
  1171. jack_set_process_callback (jack_client_t *client, JackProcessCallback callback, void *arg)
  1172. {
  1173. if (client->control->active) {
  1174. jack_error ("You cannot set callbacks on an active client.");
  1175. return -1;
  1176. }
  1177. client->control->process_arg = arg;
  1178. client->control->process = callback;
  1179. return 0;
  1180. }
  1181. int
  1182. jack_set_buffer_size_callback (jack_client_t *client, JackBufferSizeCallback callback, void *arg)
  1183. {
  1184. if (client->control->active) {
  1185. jack_error ("You cannot set callbacks on an active client.");
  1186. return -1;
  1187. }
  1188. client->control->bufsize_arg = arg;
  1189. client->control->bufsize = callback;
  1190. /* Now invoke it */
  1191. callback (client->engine->buffer_size, arg);
  1192. return 0;
  1193. }
  1194. int
  1195. jack_set_sample_rate_callback (jack_client_t *client, JackSampleRateCallback callback, void *arg)
  1196. {
  1197. if (client->control->active) {
  1198. jack_error ("You cannot set callbacks on an active client.");
  1199. return -1;
  1200. }
  1201. client->control->srate_arg = arg;
  1202. client->control->srate = callback;
  1203. /* Now invoke it */
  1204. callback (client->engine->current_time.frame_rate, arg);
  1205. return 0;
  1206. }
  1207. int
  1208. jack_set_port_registration_callback(jack_client_t *client, JackPortRegistrationCallback callback, void *arg)
  1209. {
  1210. if (client->control->active) {
  1211. jack_error ("You cannot set callbacks on an active client.");
  1212. return -1;
  1213. }
  1214. client->control->port_register_arg = arg;
  1215. client->control->port_register = callback;
  1216. return 0;
  1217. }
  1218. int
  1219. jack_get_process_start_fd (jack_client_t *client)
  1220. {
  1221. /* once this has been called, the client thread
  1222. does not sleep on the graph wait fd.
  1223. */
  1224. client->pollmax = 1;
  1225. return client->graph_wait_fd;
  1226. }
  1227. int
  1228. jack_get_process_done_fd (jack_client_t *client)
  1229. {
  1230. return client->graph_next_fd;
  1231. }
  1232. int
  1233. jack_port_request_monitor_by_name (jack_client_t *client, const char *port_name, int onoff)
  1234. {
  1235. jack_port_t *port;
  1236. unsigned long i, limit;
  1237. jack_port_shared_t *ports;
  1238. limit = client->engine->port_max;
  1239. ports = &client->engine->ports[0];
  1240. for (i = 0; i < limit; i++) {
  1241. if (ports[i].in_use && strcmp (ports[i].name, port_name) == 0) {
  1242. port = jack_port_new (client, ports[i].id, client->engine);
  1243. return jack_port_request_monitor (port, onoff);
  1244. free (port);
  1245. return 0;
  1246. }
  1247. }
  1248. return -1;
  1249. }
  1250. int
  1251. jack_port_request_monitor (jack_port_t *port, int onoff)
  1252. {
  1253. if (onoff) {
  1254. port->shared->monitor_requests++;
  1255. } else if (port->shared->monitor_requests) {
  1256. port->shared->monitor_requests--;
  1257. }
  1258. if ((port->shared->flags & JackPortIsOutput) == 0) {
  1259. JSList *node;
  1260. /* this port is for input, so recurse over each of the
  1261. connected ports.
  1262. */
  1263. pthread_mutex_lock (&port->connection_lock);
  1264. for (node = port->connections; node; node = jack_slist_next (node)) {
  1265. /* drop the lock because if there is a feedback loop,
  1266. we will deadlock. XXX much worse things will
  1267. happen if there is a feedback loop !!!
  1268. */
  1269. pthread_mutex_unlock (&port->connection_lock);
  1270. jack_port_request_monitor ((jack_port_t *) node->data, onoff);
  1271. pthread_mutex_lock (&port->connection_lock);
  1272. }
  1273. pthread_mutex_unlock (&port->connection_lock);
  1274. }
  1275. return 0;
  1276. }
  1277. int
  1278. jack_ensure_port_monitor_input (jack_port_t *port, int yn)
  1279. {
  1280. if (yn) {
  1281. if (port->shared->monitor_requests == 0) {
  1282. port->shared->monitor_requests++;
  1283. }
  1284. } else {
  1285. if (port->shared->monitor_requests == 1) {
  1286. port->shared->monitor_requests--;
  1287. }
  1288. }
  1289. return 0;
  1290. }
  1291. int
  1292. jack_port_monitoring_input (jack_port_t *port)
  1293. {
  1294. return port->shared->monitor_requests > 0;
  1295. }
  1296. const char *
  1297. jack_port_name (const jack_port_t *port)
  1298. {
  1299. return port->shared->name;
  1300. }
  1301. const char *
  1302. jack_port_short_name (const jack_port_t *port)
  1303. {
  1304. /* we know there is always a colon, because we put
  1305. it there ...
  1306. */
  1307. return strchr (port->shared->name, ':') + 1;
  1308. }
  1309. int
  1310. jack_port_is_mine (const jack_client_t *client, const jack_port_t *port)
  1311. {
  1312. return port->shared->client_id == client->control->id;
  1313. }
  1314. int
  1315. jack_port_flags (const jack_port_t *port)
  1316. {
  1317. return port->shared->flags;
  1318. }
  1319. const char *
  1320. jack_port_type (const jack_port_t *port)
  1321. {
  1322. return port->shared->type_info.type_name;
  1323. }
  1324. int
  1325. jack_port_set_name (jack_port_t *port, const char *new_name)
  1326. {
  1327. char *colon;
  1328. int len;
  1329. colon = strchr (port->shared->name, ':');
  1330. len = sizeof (port->shared->name) - ((int) (colon - port->shared->name)) - 2;
  1331. snprintf (colon+1, len, "%s", new_name);
  1332. return 0;
  1333. }
  1334. void
  1335. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1336. {
  1337. client->on_shutdown = function;
  1338. client->on_shutdown_arg = arg;
  1339. }
  1340. const char **
  1341. jack_get_ports (jack_client_t *client,
  1342. const char *port_name_pattern,
  1343. const char *type_name_pattern,
  1344. unsigned long flags)
  1345. {
  1346. jack_control_t *engine;
  1347. const char **matching_ports;
  1348. unsigned long match_cnt;
  1349. jack_port_shared_t *psp;
  1350. unsigned long i;
  1351. regex_t port_regex;
  1352. regex_t type_regex;
  1353. int matching;
  1354. engine = client->engine;
  1355. if (port_name_pattern && port_name_pattern[0]) {
  1356. regcomp (&port_regex, port_name_pattern, REG_EXTENDED|REG_NOSUB);
  1357. }
  1358. if (type_name_pattern && type_name_pattern[0]) {
  1359. regcomp (&type_regex, type_name_pattern, REG_EXTENDED|REG_NOSUB);
  1360. }
  1361. psp = engine->ports;
  1362. match_cnt = 0;
  1363. matching_ports = (const char **) malloc (sizeof (char *) * engine->port_max);
  1364. for (i = 0; i < engine->port_max; i++) {
  1365. matching = 1;
  1366. if (!psp[i].in_use) {
  1367. continue;
  1368. }
  1369. if (flags) {
  1370. if ((psp[i].flags & flags) != flags) {
  1371. matching = 0;
  1372. }
  1373. }
  1374. if (matching && port_name_pattern && port_name_pattern[0]) {
  1375. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1376. matching = 0;
  1377. }
  1378. }
  1379. if (matching && type_name_pattern && type_name_pattern[0]) {
  1380. if (regexec (&type_regex, psp[i].type_info.type_name, 0, NULL, 0)) {
  1381. matching = 0;
  1382. }
  1383. }
  1384. if (matching) {
  1385. matching_ports[match_cnt++] = psp[i].name;
  1386. }
  1387. }
  1388. matching_ports[match_cnt] = 0;
  1389. if (match_cnt == 0) {
  1390. free (matching_ports);
  1391. matching_ports = 0;
  1392. }
  1393. return matching_ports;
  1394. }
  1395. static inline void
  1396. jack_read_frame_time (const jack_client_t *client, jack_frame_timer_t *copy)
  1397. {
  1398. int tries = 0;
  1399. do {
  1400. /* throttle the busy wait if we don't get
  1401. the answer very quickly.
  1402. */
  1403. if (tries > 10) {
  1404. usleep (20);
  1405. tries = 0;
  1406. }
  1407. *copy = client->engine->frame_timer;
  1408. tries++;
  1409. } while (copy->guard1 != copy->guard2);
  1410. }
  1411. jack_nframes_t
  1412. jack_frames_since_cycle_start (const jack_client_t *client)
  1413. {
  1414. float usecs;
  1415. usecs = (float) (get_cycles() - client->engine->current_time.cycles) / client->cpu_mhz;
  1416. return (jack_nframes_t) floor ((((float) client->engine->current_time.frame_rate) / 1000000.0f) * usecs);
  1417. }
  1418. jack_nframes_t
  1419. jack_frame_time (const jack_client_t *client)
  1420. {
  1421. jack_frame_timer_t current;
  1422. float usecs;
  1423. jack_nframes_t elapsed;
  1424. jack_read_frame_time (client, &current);
  1425. usecs = (float) (get_cycles() - current.stamp) / client->cpu_mhz;
  1426. elapsed = (jack_nframes_t) floor ((((float) client->engine->current_time.frame_rate) / 1000000.0f) * usecs);
  1427. return current.frames + elapsed;
  1428. }
  1429. int
  1430. jack_port_lock (jack_client_t *client, jack_port_t *port)
  1431. {
  1432. if (port) {
  1433. port->shared->locked = 1;
  1434. return 0;
  1435. }
  1436. return -1;
  1437. }
  1438. int
  1439. jack_port_unlock (jack_client_t *client, jack_port_t *port)
  1440. {
  1441. if (port) {
  1442. port->shared->locked = 0;
  1443. return 0;
  1444. }
  1445. return -1;
  1446. }
  1447. static void
  1448. jack_audio_port_mixdown (jack_port_t *port, jack_nframes_t nframes)
  1449. {
  1450. JSList *node;
  1451. jack_port_t *input;
  1452. jack_nframes_t n;
  1453. jack_default_audio_sample_t *buffer;
  1454. jack_default_audio_sample_t *dst, *src;
  1455. /* by the time we've called this, we've already established
  1456. the existence of more than 1 connection to this input port.
  1457. */
  1458. /* no need to take connection lock, since this is called
  1459. from the process() callback, and the jack server
  1460. ensures that no changes to connections happen
  1461. during this time.
  1462. */
  1463. node = port->connections;
  1464. input = (jack_port_t *) node->data;
  1465. buffer = jack_port_buffer (port);
  1466. memcpy (buffer, jack_port_buffer (input), sizeof (jack_default_audio_sample_t) * nframes);
  1467. for (node = jack_slist_next (node); node; node = jack_slist_next (node)) {
  1468. input = (jack_port_t *) node->data;
  1469. n = nframes;
  1470. dst = buffer;
  1471. src = jack_port_buffer (input);
  1472. while (n--) {
  1473. *dst++ += *src++;
  1474. }
  1475. }
  1476. }
  1477. /* LOCAL (in-client) connection querying only */
  1478. int
  1479. jack_port_connected (const jack_port_t *port)
  1480. {
  1481. return jack_slist_length (port->connections);
  1482. }
  1483. int
  1484. jack_port_connected_to (const jack_port_t *port, const char *portname)
  1485. {
  1486. JSList *node;
  1487. int ret = FALSE;
  1488. /* XXX this really requires a cross-process lock
  1489. so that ports/connections cannot go away
  1490. while we are checking for them. that's hard,
  1491. and has a non-trivial performance impact
  1492. for jackd.
  1493. */
  1494. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  1495. for (node = port->connections; node; node = jack_slist_next (node)) {
  1496. jack_port_t *other_port = (jack_port_t *) node->data;
  1497. if (strcmp (other_port->shared->name, portname) == 0) {
  1498. ret = TRUE;
  1499. break;
  1500. }
  1501. }
  1502. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1503. return ret;
  1504. }
  1505. const char **
  1506. jack_port_get_connections (const jack_port_t *port)
  1507. {
  1508. const char **ret = NULL;
  1509. JSList *node;
  1510. unsigned int n;
  1511. /* XXX this really requires a cross-process lock
  1512. so that ports/connections cannot go away
  1513. while we are checking for them. that's hard,
  1514. and has a non-trivial performance impact
  1515. for jackd.
  1516. */
  1517. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  1518. if (port->connections != NULL) {
  1519. ret = (const char **) malloc (sizeof (char *) * (jack_slist_length (port->connections) + 1));
  1520. for (n = 0, node = port->connections; node; node = jack_slist_next (node), ++n) {
  1521. ret[n] = ((jack_port_t *) node->data)->shared->name;
  1522. }
  1523. ret[n] = NULL;
  1524. }
  1525. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1526. return ret;
  1527. }
  1528. /* SERVER-SIDE (all) connection querying */
  1529. const char **
  1530. jack_port_get_all_connections (const jack_client_t *client, const jack_port_t *port)
  1531. {
  1532. const char **ret;
  1533. jack_request_t req;
  1534. int i;
  1535. req.type = GetPortConnections;
  1536. req.x.port_info.name[0] = '\0';
  1537. req.x.port_info.type[0] = '\0';
  1538. req.x.port_info.flags = 0;
  1539. req.x.port_info.buffer_size = 0;
  1540. req.x.port_info.client_id = 0;
  1541. req.x.port_info.port_id = port->shared->id;
  1542. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1543. jack_error ("cannot send port connections request to server");
  1544. return 0;
  1545. }
  1546. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1547. jack_error ("cannot read port connections result from server");
  1548. return 0;
  1549. }
  1550. if (req.x.nports == 0) {
  1551. return NULL;
  1552. }
  1553. ret = (const char **) malloc (sizeof (char *) * (req.x.nports + 1));
  1554. for ( i=0; i<req.x.nports; i++ ) {
  1555. jack_port_id_t port_id;
  1556. if (read (client->request_fd, &port_id, sizeof (port_id)) != sizeof (port_id)) {
  1557. jack_error ("cannot read port id from server");
  1558. return 0;
  1559. }
  1560. ret[i] = jack_port_by_id (client, port_id)->shared->name;
  1561. }
  1562. ret[i] = NULL;
  1563. return ret;
  1564. }
  1565. /* TRANSPORT CONTROL */
  1566. int
  1567. jack_get_transport_info (jack_client_t *client,
  1568. jack_transport_info_t *info)
  1569. {
  1570. jack_time_info_t *time_info = &client->engine->current_time;
  1571. if (info->valid & JackTransportState) {
  1572. info->state = time_info->transport_state;
  1573. }
  1574. if (info->valid & JackTransportPosition) {
  1575. info->position = time_info->frame;
  1576. }
  1577. if (info->valid & JackTransportLoop) {
  1578. info->loop_start = time_info->loop_start;
  1579. info->loop_end = time_info->loop_end;
  1580. }
  1581. return 0;
  1582. }
  1583. int
  1584. jack_set_transport_info (jack_client_t *client,
  1585. jack_transport_info_t *info)
  1586. {
  1587. jack_time_info_t *time_info = &client->engine->pending_time;
  1588. if (info->valid & JackTransportState) {
  1589. time_info->transport_state = info->state;
  1590. }
  1591. if (info->valid & JackTransportPosition) {
  1592. time_info->frame = info->position;
  1593. }
  1594. if (info->valid & JackTransportLoop) {
  1595. time_info->loop_start = info->loop_start;
  1596. time_info->loop_end = info->loop_end;
  1597. }
  1598. return 0;
  1599. }
  1600. jack_nframes_t
  1601. jack_port_get_total_latency (jack_client_t *client, jack_port_t *port)
  1602. {
  1603. return port->shared->total_latency;
  1604. }
  1605. int
  1606. jack_get_mhz (void)
  1607. {
  1608. FILE *f = fopen("/proc/cpuinfo", "r");
  1609. if (f == 0)
  1610. {
  1611. perror("can't open /proc/cpuinfo\n");
  1612. exit(1);
  1613. }
  1614. for ( ; ; )
  1615. {
  1616. int mhz;
  1617. int ret;
  1618. char buf[1000];
  1619. if (fgets(buf, sizeof(buf), f) == NULL)
  1620. {
  1621. fprintf(stderr, "cannot locate cpu MHz in /proc/cpuinfo\n");
  1622. exit(1);
  1623. }
  1624. #ifdef __powerpc__
  1625. ret = sscanf(buf, "clock\t: %dMHz", &mhz);
  1626. #else
  1627. ret = sscanf(buf, "cpu MHz : %d", &mhz);
  1628. #endif /* __powerpc__ */
  1629. if (ret == 1)
  1630. {
  1631. fclose(f);
  1632. return mhz;
  1633. }
  1634. }
  1635. }
  1636. float
  1637. jack_cpu_load (jack_client_t *client)
  1638. {
  1639. return client->engine->cpu_load;
  1640. }
  1641. int
  1642. jack_add_alias (jack_client_t *client, const char *portname, const char *alias)
  1643. {
  1644. jack_request_t req;
  1645. req.type = AddAlias;
  1646. snprintf (req.x.alias.port, sizeof (req.x.alias.port), "%s", portname);
  1647. snprintf (req.x.alias.alias, sizeof (req.x.alias.alias), "%s", alias);
  1648. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1649. jack_error ("cannot send add alias request to server");
  1650. return -1;
  1651. }
  1652. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1653. jack_error ("cannot read add alias result from server (%s)", strerror (errno));
  1654. return -1;
  1655. }
  1656. return req.status;
  1657. }
  1658. int
  1659. jack_remove_alias (jack_client_t *client, const char *alias)
  1660. {
  1661. jack_request_t req;
  1662. req.type = RemoveAlias;
  1663. snprintf (req.x.alias.alias, sizeof (req.x.alias.alias), "%s", alias);
  1664. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1665. jack_error ("cannot send remove alias request to server");
  1666. return -1;
  1667. }
  1668. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1669. jack_error ("cannot remove alias result from server (%s)", strerror (errno));
  1670. return -1;
  1671. }
  1672. return req.status;
  1673. }
  1674. pthread_t
  1675. jack_client_thread_id (jack_client_t *client)
  1676. {
  1677. return client->thread_id;
  1678. }