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.

2034 lines
48KB

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