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.

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