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.

2025 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. DEBUG ("client polling on event_fd and graph_wait_fd...");
  414. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  415. if (errno == EINTR) {
  416. printf ("poll interrupted\n");
  417. continue;
  418. }
  419. jack_error ("poll failed in client (%s)", strerror (errno));
  420. status = -1;
  421. break;
  422. }
  423. if (client->pollfd[0].revents & ~POLLIN) {
  424. jack_error ("engine has shut down socket; thread exiting");
  425. if (client->on_shutdown) {
  426. client->on_shutdown (client->on_shutdown_arg);
  427. }
  428. pthread_exit (0);
  429. }
  430. if (client->pollfd[0].revents & POLLIN) {
  431. DEBUG ("client receives an event, now reading on event fd");
  432. /* server has sent us an event. process the event and reply */
  433. if (read (client->event_fd, &event, sizeof (event)) != sizeof (event)) {
  434. jack_error ("cannot read server event (%s)", strerror (errno));
  435. err++;
  436. break;
  437. }
  438. status = 0;
  439. switch (event.type) {
  440. case PortRegistered:
  441. if (control->port_register) {
  442. control->port_register (event.x.port_id, TRUE, control->port_register_arg);
  443. }
  444. break;
  445. case PortUnregistered:
  446. if (control->port_register) {
  447. control->port_register (event.x.port_id, FALSE, control->port_register_arg);
  448. }
  449. break;
  450. case GraphReordered:
  451. status = jack_handle_reorder (client, &event);
  452. break;
  453. case PortConnected:
  454. case PortDisconnected:
  455. status = jack_client_handle_port_connection (client, &event);
  456. break;
  457. case BufferSizeChange:
  458. jack_client_invalidate_port_buffers (client);
  459. if (control->bufsize) {
  460. status = control->bufsize (control->nframes, control->bufsize_arg);
  461. }
  462. break;
  463. case SampleRateChange:
  464. if (control->srate) {
  465. status = control->srate (control->nframes, control->srate_arg);
  466. }
  467. break;
  468. case XRun:
  469. if (control->xrun) {
  470. status = control->xrun (control->xrun_arg);
  471. }
  472. break;
  473. case NewPortBufferSegment:
  474. break;
  475. }
  476. DEBUG ("client has dealt with the event, writing response on event fd");
  477. if (write (client->event_fd, &status, sizeof (status)) != sizeof (status)) {
  478. jack_error ("cannot send event response to engine (%s)", strerror (errno));
  479. err++;
  480. break;
  481. }
  482. }
  483. if (client->pollfd[1].revents & POLLIN) {
  484. DEBUG ("client told to process() (wakeup on graph_wait_fd==%d)", client->pollfd[1].fd);
  485. control->signalled_at = get_cycles();
  486. control->state = Running;
  487. if (control->process) {
  488. if (control->process (control->nframes, control->process_arg) == 0) {
  489. control->state = Finished;
  490. }
  491. } else {
  492. control->state = Finished;
  493. }
  494. control->finished_at = get_cycles();
  495. /* pass the execution token along */
  496. DEBUG ("client finished processing, writing on graph_next_fd==%d", client->graph_next_fd);
  497. if (write (client->graph_next_fd, &c, sizeof (c)) != sizeof (c)) {
  498. jack_error ("cannot continue execution of the processing graph (%s)", strerror(errno));
  499. err++;
  500. break;
  501. }
  502. DEBUG ("client reading on graph_wait_fd==%d", client->graph_wait_fd);
  503. if ((read (client->graph_wait_fd, &c, sizeof (c)) != sizeof (c))) {
  504. DEBUG ("WARNING: READ FAILED!");
  505. /*
  506. jack_error ("cannot complete execution of the processing graph (%s)", strerror(errno));
  507. err++;
  508. break;
  509. */
  510. }
  511. }
  512. }
  513. return (void *) err;
  514. }
  515. static int
  516. jack_start_thread (jack_client_t *client)
  517. {
  518. pthread_attr_t *attributes = 0;
  519. #ifdef USE_CAPABILITIES
  520. int policy = SCHED_OTHER;
  521. struct sched_param client_param, temp_param;
  522. #endif
  523. if (client->engine->real_time) {
  524. /* Get the client thread to run as an RT-FIFO
  525. scheduled thread of appropriate priority.
  526. */
  527. struct sched_param rt_param;
  528. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  529. pthread_attr_init (attributes);
  530. if (pthread_attr_setschedpolicy (attributes, SCHED_FIFO)) {
  531. jack_error ("cannot set FIFO scheduling class for RT thread");
  532. return -1;
  533. }
  534. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  535. jack_error ("Cannot set scheduling scope for RT thread");
  536. return -1;
  537. }
  538. memset (&rt_param, 0, sizeof (rt_param));
  539. rt_param.sched_priority = client->engine->client_priority;
  540. if (pthread_attr_setschedparam (attributes, &rt_param)) {
  541. jack_error ("Cannot set scheduling priority for RT thread (%s)", strerror (errno));
  542. return -1;
  543. }
  544. if (mlockall (MCL_CURRENT|MCL_FUTURE)) {
  545. jack_error ("cannot lock down all memory (%s)", strerror (errno));
  546. return -1;
  547. }
  548. }
  549. if (pthread_create (&client->thread, attributes, jack_client_thread, client)) {
  550. #ifdef USE_CAPABILITIES
  551. if (client->engine->real_time && client->engine->has_capabilities) {
  552. /* we are probably dealing with a broken glibc so try
  553. to work around the bug, see below for more details
  554. */
  555. goto capabilities_workaround;
  556. }
  557. #endif
  558. return -1;
  559. }
  560. return 0;
  561. #ifdef USE_CAPABILITIES
  562. /* we get here only with engine running realtime and capabilities */
  563. capabilities_workaround:
  564. /* the version of glibc I've played with has a bug that makes
  565. that code fail when running under a non-root user but with the
  566. proper realtime capabilities (in short, pthread_attr_setschedpolicy
  567. does not check for capabilities, only for the uid being
  568. zero). Newer versions apparently have this fixed. This
  569. workaround temporarily switches the client thread to the
  570. proper scheduler and priority, then starts the realtime
  571. thread so that it can inherit them and finally switches the
  572. client thread back to what it was before. Sigh. For ardour
  573. I have to check again and switch the thread explicitly to
  574. realtime, don't know why or how to debug - nando
  575. */
  576. /* get current scheduler and parameters of the client process */
  577. if ((policy = sched_getscheduler (0)) < 0) {
  578. jack_error ("Cannot get current client scheduler: %s", strerror(errno));
  579. return -1;
  580. }
  581. memset (&client_param, 0, sizeof (client_param));
  582. if (sched_getparam (0, &client_param)) {
  583. jack_error ("Cannot get current client scheduler parameters: %s", strerror(errno));
  584. return -1;
  585. }
  586. /* temporarily change the client process to SCHED_FIFO so that
  587. the realtime thread can inherit the scheduler and priority
  588. */
  589. memset (&temp_param, 0, sizeof (temp_param));
  590. temp_param.sched_priority = client->engine->client_priority;
  591. if (sched_setscheduler(0, SCHED_FIFO, &temp_param)) {
  592. jack_error ("Cannot temporarily set client to RT scheduler: %s", strerror(errno));
  593. return -1;
  594. }
  595. /* prepare the attributes for the realtime thread */
  596. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  597. pthread_attr_init (attributes);
  598. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  599. sched_setscheduler (0, policy, &client_param);
  600. jack_error ("Cannot set scheduling scope for RT thread");
  601. return -1;
  602. }
  603. if (pthread_attr_setinheritsched (attributes, PTHREAD_INHERIT_SCHED)) {
  604. sched_setscheduler (0, policy, &client_param);
  605. jack_error ("Cannot set scheduler inherit policy for RT thread");
  606. return -1;
  607. }
  608. /* create the RT thread */
  609. if (pthread_create (&client->thread, attributes, jack_client_thread, client)) {
  610. sched_setscheduler (0, policy, &client_param);
  611. return -1;
  612. }
  613. /* return the client process to the scheduler it was in before */
  614. if (sched_setscheduler (0, policy, &client_param)) {
  615. jack_error ("Cannot reset original client scheduler: %s", strerror(errno));
  616. return -1;
  617. }
  618. /* check again... inheritance of policy and priority works in jack_simple_client
  619. but not in ardour! So I check again and force the policy if it is not set
  620. correctly. This does not really really work either, the manager thread
  621. of the linuxthreads implementation is left running with SCHED_OTHER,
  622. that is presumably very bad.
  623. */
  624. memset (&client_param, 0, sizeof (client_param));
  625. if (pthread_getschedparam(client->thread, &policy, &client_param) == 0) {
  626. if (policy != SCHED_FIFO) {
  627. /* jack_error ("RT thread did not go SCHED_FIFO, trying again"); */
  628. memset (&client_param, 0, sizeof (client_param));
  629. client_param.sched_priority = client->engine->client_priority;
  630. if (pthread_setschedparam (client->thread, SCHED_FIFO, &client_param)) {
  631. jack_error ("Cannot set (again) FIFO scheduling class for RT thread\n");
  632. return -1;
  633. }
  634. }
  635. }
  636. return 0;
  637. #endif
  638. }
  639. int
  640. jack_activate (jack_client_t *client)
  641. {
  642. jack_request_t req;
  643. /* we need to scribble on our stack to ensure that its memory pages are
  644. * actually mapped (more important for mlockall(2) usage in
  645. * jack_start_thread()) */
  646. #define BIG_ENOUGH_STACK 1048576
  647. char buf[BIG_ENOUGH_STACK];
  648. int i;
  649. for (i = 0; i < BIG_ENOUGH_STACK; i++) {
  650. buf[i] = (char) (i & 0xff);
  651. }
  652. #undef BIG_ENOUGH_STACK
  653. #ifdef USE_CAPABILITIES
  654. if (client->engine->has_capabilities != 0 &&
  655. client->control->pid != 0 && client->engine->real_time != 0) {
  656. /* we need to ask the engine for realtime capabilities
  657. before trying to start the realtime thread
  658. */
  659. req.type = SetClientCapabilities;
  660. req.x.client_id = client->control->id;
  661. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  662. jack_error ("cannot send set client capabilities request to server");
  663. return -1;
  664. }
  665. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  666. jack_error ("cannot read set client capabilities result from server (%s)", strerror (errno));
  667. return -1;
  668. }
  669. if (req.status) {
  670. /* what to do? engine is running realtime, it is using capabilities and has
  671. them (otherwise we would not get an error return) but for some reason it
  672. could not give the client the required capabilities, so for now downgrade
  673. the client so that it still runs, albeit non-realtime - nando
  674. */
  675. jack_error ("could not receive realtime capabilities, client will run non-realtime");
  676. /* XXX wrong, this is a property of the engine
  677. client->engine->real_time = 0;
  678. */
  679. }
  680. }
  681. #endif
  682. if (client->control->type == ClientOutOfProcess && client->first_active) {
  683. pthread_mutex_init (&client_lock, NULL);
  684. pthread_cond_init (&client_ready, NULL);
  685. pthread_mutex_lock (&client_lock);
  686. if (jack_start_thread (client)) {
  687. pthread_mutex_unlock (&client_lock);
  688. return -1;
  689. }
  690. pthread_cond_wait (&client_ready, &client_lock);
  691. pthread_mutex_unlock (&client_lock);
  692. if (!client->thread_ok) {
  693. jack_error ("could not start client thread");
  694. return -1;
  695. }
  696. client->first_active = FALSE;
  697. }
  698. req.type = ActivateClient;
  699. req.x.client_id = client->control->id;
  700. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  701. jack_error ("cannot send activate client request to server");
  702. return -1;
  703. }
  704. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  705. jack_error ("cannot read activate client result from server (%s)", strerror (errno));
  706. return -1;
  707. }
  708. return req.status;
  709. }
  710. int
  711. jack_deactivate (jack_client_t *client)
  712. {
  713. jack_request_t req;
  714. req.type = DeactivateClient;
  715. req.x.client_id = client->control->id;
  716. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  717. jack_error ("cannot send deactivate client request to server");
  718. return -1;
  719. }
  720. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  721. jack_error ("cannot read deactivate client result from server (%s)", strerror (errno));
  722. return -1;
  723. }
  724. return req.status;
  725. }
  726. int
  727. jack_client_close (jack_client_t *client)
  728. {
  729. GSList *node;
  730. void *status;
  731. /* stop the thread that communicates with the jack server */
  732. pthread_cancel (client->thread);
  733. pthread_join (client->thread, &status);
  734. shmdt ((char *) client->control);
  735. shmdt (client->engine);
  736. for (node = client->port_segments; node; node = g_slist_next (node)) {
  737. shmdt (((jack_port_segment_info_t *) node->data)->address);
  738. free (node->data);
  739. }
  740. g_slist_free (client->port_segments);
  741. for (node = client->ports; node; node = g_slist_next (node)) {
  742. free (node->data);
  743. }
  744. g_slist_free (client->ports);
  745. if (client->graph_wait_fd) {
  746. close (client->graph_wait_fd);
  747. }
  748. if (client->graph_next_fd) {
  749. close (client->graph_next_fd);
  750. }
  751. close (client->event_fd);
  752. close (client->request_fd);
  753. free (client->pollfd);
  754. free (client);
  755. return 0;
  756. }
  757. int
  758. jack_load_client (const char *client_name, const char *path_to_so)
  759. {
  760. int fd;
  761. jack_client_connect_request_t req;
  762. jack_client_connect_result_t res;
  763. if ((fd = server_connect (0)) < 0) {
  764. jack_error ("cannot connect to jack server");
  765. return 0;
  766. }
  767. req.type = ClientDynamic;
  768. strncpy (req.name, client_name, sizeof (req.name) - 1);
  769. req.name[sizeof(req.name)-1] = '\0';
  770. strncpy (req.object_path, path_to_so, sizeof (req.name) - 1);
  771. req.object_path[sizeof(req.object_path)-1] = '\0';
  772. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  773. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  774. close (fd);
  775. return 0;
  776. }
  777. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  778. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  779. close (fd);
  780. return 0;
  781. }
  782. close (fd);
  783. return res.status;
  784. }
  785. jack_client_t *
  786. jack_driver_become_client (const char *client_name)
  787. {
  788. int fd;
  789. jack_client_connect_request_t req;
  790. jack_client_connect_result_t res;
  791. jack_client_t *client = 0;
  792. int port_segment_shm_id;
  793. jack_port_segment_info_t *si;
  794. void *addr;
  795. if ((fd = server_connect (0)) < 0) {
  796. jack_error ("cannot connect to jack server");
  797. return 0;
  798. }
  799. req.type = ClientDriver;
  800. strncpy (req.name, client_name, sizeof (req.name) - 1);
  801. req.name[sizeof(req.name)-1] = '\0';
  802. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  803. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  804. close (fd);
  805. return 0;
  806. }
  807. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  808. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  809. close (fd);
  810. return 0;
  811. }
  812. if (res.status) {
  813. return 0;
  814. }
  815. client = jack_client_alloc ();
  816. client->request_fd = fd;
  817. client->control = res.client_control;
  818. client->engine = res.engine_control;
  819. /* Lookup, attach and register the port/buffer segments in use
  820. right now.
  821. */
  822. if ((port_segment_shm_id = shmget (res.port_segment_key, 0, 0)) < 0) {
  823. jack_error ("cannot determine shared memory segment for port segment key 0x%x (%s)", res.port_segment_key, strerror (errno));
  824. return NULL;
  825. }
  826. if ((addr = shmat (port_segment_shm_id, 0, 0)) == (void *) -1) {
  827. jack_error ("cannot attached port segment shared memory (%s)", strerror (errno));
  828. return NULL;
  829. }
  830. si = (jack_port_segment_info_t *) malloc (sizeof (jack_port_segment_info_t));
  831. si->shm_key = res.port_segment_key;
  832. si->address = addr;
  833. /* the first chunk of the first port segment is always set by the engine
  834. to be a conveniently-sized, zero-filled lump of memory.
  835. */
  836. if (client->port_segments == NULL) {
  837. jack_zero_filled_buffer = si->address;
  838. }
  839. client->port_segments = g_slist_prepend (client->port_segments, si);
  840. /* allow the engine to act on the client's behalf
  841. when dealing with in-process clients.
  842. */
  843. client->control->private_internal_client = client;
  844. return client;
  845. }
  846. unsigned long jack_get_buffer_size (jack_client_t *client)
  847. {
  848. return client->engine->buffer_size;
  849. }
  850. unsigned long jack_get_sample_rate (jack_client_t *client)
  851. {
  852. return client->engine->time.frame_rate;
  853. }
  854. static jack_port_t *
  855. jack_port_new (jack_client_t *client, jack_port_id_t port_id, jack_control_t *control)
  856. {
  857. jack_port_t *port;
  858. jack_port_shared_t *shared;
  859. jack_port_segment_info_t *si;
  860. GSList *node;
  861. shared = &control->ports[port_id];
  862. port = (jack_port_t *) malloc (sizeof (jack_port_t));
  863. port->client_segment_base = 0;
  864. port->shared = shared;
  865. pthread_mutex_init (&port->connection_lock, NULL);
  866. port->connections = 0;
  867. port->tied = NULL;
  868. si = NULL;
  869. for (node = client->port_segments; node; node = g_slist_next (node)) {
  870. si = (jack_port_segment_info_t *) node->data;
  871. if (si->shm_key == port->shared->shm_key) {
  872. break;
  873. }
  874. }
  875. if (si == NULL) {
  876. jack_error ("cannot find port segment to match newly registered port\n");
  877. return NULL;
  878. }
  879. port->client_segment_base = si->address;
  880. return port;
  881. }
  882. jack_port_t *
  883. jack_port_register (jack_client_t *client,
  884. const char *port_name,
  885. const char *port_type,
  886. unsigned long flags,
  887. unsigned long buffer_size)
  888. {
  889. jack_request_t req;
  890. jack_port_t *port = 0;
  891. jack_port_type_info_t *type_info;
  892. int n;
  893. req.type = RegisterPort;
  894. strcpy ((char *) req.x.port_info.name, (const char *) client->control->name);
  895. strcat ((char *) req.x.port_info.name, ":");
  896. strcat ((char *) req.x.port_info.name, port_name);
  897. strncpy (req.x.port_info.type, port_type, sizeof (req.x.port_info.type) - 1);
  898. req.x.port_info.flags = flags;
  899. req.x.port_info.buffer_size = buffer_size;
  900. req.x.port_info.client_id = client->control->id;
  901. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  902. jack_error ("cannot send port registration request to server");
  903. return 0;
  904. }
  905. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  906. jack_error ("cannot read port registration result from server");
  907. return 0;
  908. }
  909. if (req.status != 0) {
  910. return NULL;
  911. }
  912. port = jack_port_new (client, req.x.port_info.port_id, client->engine);
  913. type_info = NULL;
  914. for (n = 0; builtin_port_types[n].type_name[0]; n++) {
  915. if (strcmp (req.x.port_info.type, builtin_port_types[n].type_name) == 0) {
  916. type_info = &builtin_port_types[n];
  917. break;
  918. }
  919. }
  920. if (type_info == NULL) {
  921. /* not a builtin type, so allocate a new type_info structure,
  922. and fill it appropriately.
  923. */
  924. type_info = (jack_port_type_info_t *) malloc (sizeof (jack_port_type_info_t));
  925. snprintf ((char *) type_info->type_name, sizeof (type_info->type_name), req.x.port_info.type);
  926. type_info->mixdown = NULL; /* we have no idea how to mix this */
  927. type_info->buffer_scale_factor = -1; /* use specified port buffer size */
  928. }
  929. memcpy (&port->shared->type_info, type_info, sizeof (jack_port_type_info_t));
  930. client->ports = g_slist_prepend (client->ports, port);
  931. return port;
  932. }
  933. int
  934. jack_port_unregister (jack_client_t *client, jack_port_t *port)
  935. {
  936. jack_request_t req;
  937. req.type = UnRegisterPort;
  938. req.x.port_info.port_id = port->shared->id;
  939. req.x.port_info.client_id = client->control->id;
  940. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  941. jack_error ("cannot send port registration request to server");
  942. return -1;
  943. }
  944. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  945. jack_error ("cannot read port registration result from server");
  946. return -1;
  947. }
  948. return req.status;
  949. }
  950. int
  951. jack_connect (jack_client_t *client, const char *source_port, const char *destination_port)
  952. {
  953. jack_request_t req;
  954. req.type = ConnectPorts;
  955. strncpy (req.x.connect.source_port, source_port, sizeof (req.x.connect.source_port) - 1);
  956. req.x.connect.source_port[sizeof(req.x.connect.source_port) - 1] = '\0';
  957. strncpy (req.x.connect.destination_port, destination_port, sizeof (req.x.connect.destination_port) - 1);
  958. req.x.connect.destination_port[sizeof(req.x.connect.destination_port) - 1] = '\0';
  959. DEBUG ("writing to request_fd");
  960. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  961. jack_error ("cannot send port connection request to server");
  962. return -1;
  963. }
  964. DEBUG ("reading from request_fd");
  965. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  966. jack_error ("cannot read port connection result from server");
  967. return -1;
  968. }
  969. DEBUG ("connected: %d", req.status);
  970. return req.status;
  971. }
  972. int
  973. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  974. {
  975. jack_request_t req;
  976. pthread_mutex_lock (&port->connection_lock);
  977. if (port->connections == NULL) {
  978. pthread_mutex_unlock (&port->connection_lock);
  979. return 0;
  980. }
  981. pthread_mutex_unlock (&port->connection_lock);
  982. req.type = DisconnectPort;
  983. req.x.port_info.port_id = port->shared->id;
  984. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  985. jack_error ("cannot send port disconnect request to server");
  986. return -1;
  987. }
  988. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  989. jack_error ("cannot read port disconnect result from server");
  990. return -1;
  991. }
  992. return req.status;
  993. }
  994. int
  995. jack_disconnect (jack_client_t *client, const char *source_port, const char *destination_port)
  996. {
  997. jack_request_t req;
  998. req.type = DisconnectPorts;
  999. strncpy (req.x.connect.source_port, source_port, sizeof (req.x.connect.source_port) - 1);
  1000. req.x.connect.source_port[sizeof(req.x.connect.source_port) - 1] = '\0';
  1001. strncpy (req.x.connect.destination_port, destination_port, sizeof (req.x.connect.destination_port) - 1);
  1002. req.x.connect.destination_port[sizeof(req.x.connect.destination_port) - 1] = '\0';
  1003. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1004. jack_error ("cannot send port connection request to server");
  1005. return -1;
  1006. }
  1007. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1008. jack_error ("cannot read port connection result from server");
  1009. return -1;
  1010. }
  1011. return req.status;
  1012. }
  1013. int
  1014. jack_engine_takeover_timebase (jack_client_t *client)
  1015. {
  1016. jack_request_t req;
  1017. req.type = SetTimeBaseClient;
  1018. req.x.client_id = client->control->id;
  1019. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1020. jack_error ("cannot send set time base request to server");
  1021. return -1;
  1022. }
  1023. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1024. jack_error ("cannot read set time base result from server");
  1025. return -1;
  1026. }
  1027. return req.status;
  1028. }
  1029. void
  1030. jack_set_error_function (void (*func) (const char *, ...))
  1031. {
  1032. jack_error = func;
  1033. }
  1034. jack_nframes_t
  1035. jack_port_get_latency (jack_port_t *port)
  1036. {
  1037. return port->shared->latency;
  1038. }
  1039. void
  1040. jack_port_set_latency (jack_port_t *port, jack_nframes_t nframes)
  1041. {
  1042. port->shared->latency = nframes;
  1043. }
  1044. void *
  1045. jack_port_get_buffer (jack_port_t *port, jack_nframes_t nframes)
  1046. {
  1047. GSList *node, *next;
  1048. /* Output port. The buffer was assigned by the engine
  1049. when the port was registered.
  1050. */
  1051. if (port->shared->flags & JackPortIsOutput) {
  1052. if (port->tied) {
  1053. return jack_port_get_buffer (port->tied, nframes);
  1054. }
  1055. return jack_port_buffer (port);
  1056. }
  1057. /* Input port.
  1058. */
  1059. /* since this can only be called from the process() callback,
  1060. and since no connections can be made/broken during this
  1061. phase (enforced by the jack server), there is no need
  1062. to take the connection lock here
  1063. */
  1064. if ((node = port->connections) == NULL) {
  1065. /* no connections; return a zero-filled buffer */
  1066. return jack_zero_filled_buffer;
  1067. }
  1068. if ((next = g_slist_next (node)) == NULL) {
  1069. /* one connection: use zero-copy mode - just pass
  1070. the buffer of the connected (output) port.
  1071. */
  1072. return jack_port_buffer (((jack_port_t *) node->data));
  1073. }
  1074. /* multiple connections. use a local buffer and mixdown
  1075. the incoming data to that buffer. we have already
  1076. established the existence of a mixdown function
  1077. during the connection process.
  1078. no port can have an offset of 0 - that offset refers
  1079. to the zero-filled area at the start of a shared port
  1080. segment area. so, use the offset to store the location
  1081. of a locally allocated buffer, and reset the client_segment_base
  1082. so that the jack_port_buffer() computation works correctly.
  1083. */
  1084. if (port->shared->offset == 0) {
  1085. port->shared->offset = (size_t) jack_pool_alloc (port->shared->type_info.buffer_scale_factor *
  1086. sizeof (jack_default_audio_sample_t) * nframes);
  1087. port->client_segment_base = 0;
  1088. }
  1089. port->shared->type_info.mixdown (port, nframes);
  1090. return (jack_default_audio_sample_t *) port->shared->offset;
  1091. }
  1092. int
  1093. jack_port_tie (jack_port_t *src, jack_port_t *dst)
  1094. {
  1095. if (dst->shared->client_id != src->shared->client_id) {
  1096. jack_error ("cannot tie ports not owned by the same client");
  1097. return -1;
  1098. }
  1099. if (dst->shared->flags & JackPortIsOutput) {
  1100. jack_error ("cannot tie an input port");
  1101. return -1;
  1102. }
  1103. dst->tied = src;
  1104. return 0;
  1105. }
  1106. int
  1107. jack_port_untie (jack_port_t *port)
  1108. {
  1109. if (port->tied == NULL) {
  1110. jack_error ("port \"%s\" is not tied", port->shared->name);
  1111. return -1;
  1112. }
  1113. port->tied = NULL;
  1114. return 0;
  1115. }
  1116. int
  1117. jack_set_graph_order_callback (jack_client_t *client, JackGraphOrderCallback callback, void *arg)
  1118. {
  1119. if (client->control->active) {
  1120. g_warning ("You cannot set callbacks on an active client.");
  1121. return -1;
  1122. }
  1123. client->control->graph_order = callback;
  1124. client->control->graph_order_arg = arg;
  1125. return 0;
  1126. }
  1127. int
  1128. jack_set_process_callback (jack_client_t *client, JackProcessCallback callback, void *arg)
  1129. {
  1130. if (client->control->active) {
  1131. g_warning ("You cannot set callbacks on an active client.");
  1132. return -1;
  1133. }
  1134. client->control->process_arg = arg;
  1135. client->control->process = callback;
  1136. return 0;
  1137. }
  1138. int
  1139. jack_set_buffer_size_callback (jack_client_t *client, JackBufferSizeCallback callback, void *arg)
  1140. {
  1141. if (client->control->active) {
  1142. g_warning ("You cannot set callbacks on an active client.");
  1143. return -1;
  1144. }
  1145. client->control->bufsize_arg = arg;
  1146. client->control->bufsize = callback;
  1147. /* Now invoke it */
  1148. callback (client->engine->buffer_size, arg);
  1149. return 0;
  1150. }
  1151. int
  1152. jack_set_sample_rate_callback (jack_client_t *client, JackSampleRateCallback callback, void *arg)
  1153. {
  1154. if (client->control->active) {
  1155. g_warning ("You cannot set callbacks on an active client.");
  1156. return -1;
  1157. }
  1158. client->control->srate_arg = arg;
  1159. client->control->srate = callback;
  1160. /* Now invoke it */
  1161. callback (client->engine->time.frame_rate, arg);
  1162. return 0;
  1163. }
  1164. int
  1165. jack_set_port_registration_callback(jack_client_t *client, JackPortRegistrationCallback callback, void *arg)
  1166. {
  1167. if (client->control->active) {
  1168. g_warning ("You cannot set callbacks on an active client.");
  1169. return -1;
  1170. }
  1171. client->control->port_register_arg = arg;
  1172. client->control->port_register = callback;
  1173. return 0;
  1174. }
  1175. int
  1176. jack_get_process_start_fd (jack_client_t *client)
  1177. {
  1178. /* once this has been called, the client thread
  1179. does not sleep on the graph wait fd.
  1180. */
  1181. client->pollmax = 1;
  1182. return client->graph_wait_fd;
  1183. }
  1184. int
  1185. jack_get_process_done_fd (jack_client_t *client)
  1186. {
  1187. return client->graph_next_fd;
  1188. }
  1189. int
  1190. jack_port_request_monitor_by_name (jack_client_t *client, const char *port_name, int onoff)
  1191. {
  1192. jack_port_t *port;
  1193. unsigned long i, limit;
  1194. jack_port_shared_t *ports;
  1195. limit = client->engine->port_max;
  1196. ports = &client->engine->ports[0];
  1197. for (i = 0; i < limit; i++) {
  1198. if (ports[i].in_use && strcmp (ports[i].name, port_name) == 0) {
  1199. port = jack_port_new (client, ports[i].id, client->engine);
  1200. return jack_port_request_monitor (port, onoff);
  1201. free (port);
  1202. return 0;
  1203. }
  1204. }
  1205. return -1;
  1206. }
  1207. int
  1208. jack_port_request_monitor (jack_port_t *port, int onoff)
  1209. {
  1210. if (onoff) {
  1211. port->shared->monitor_requests++;
  1212. } else if (port->shared->monitor_requests) {
  1213. port->shared->monitor_requests--;
  1214. }
  1215. if ((port->shared->flags & JackPortIsOutput) == 0) {
  1216. GSList *node;
  1217. /* this port is for input, so recurse over each of the
  1218. connected ports.
  1219. */
  1220. pthread_mutex_lock (&port->connection_lock);
  1221. for (node = port->connections; node; node = g_slist_next (node)) {
  1222. /* drop the lock because if there is a feedback loop,
  1223. we will deadlock. XXX much worse things will
  1224. happen if there is a feedback loop !!!
  1225. */
  1226. pthread_mutex_unlock (&port->connection_lock);
  1227. jack_port_request_monitor ((jack_port_t *) node->data, onoff);
  1228. pthread_mutex_lock (&port->connection_lock);
  1229. }
  1230. pthread_mutex_unlock (&port->connection_lock);
  1231. }
  1232. return 0;
  1233. }
  1234. int
  1235. jack_ensure_port_monitor_input (jack_port_t *port, int yn)
  1236. {
  1237. if (yn) {
  1238. if (port->shared->monitor_requests == 0) {
  1239. port->shared->monitor_requests++;
  1240. }
  1241. } else {
  1242. if (port->shared->monitor_requests == 1) {
  1243. port->shared->monitor_requests--;
  1244. }
  1245. }
  1246. return 0;
  1247. }
  1248. int
  1249. jack_port_monitoring_input (jack_port_t *port)
  1250. {
  1251. return port->shared->monitor_requests > 0;
  1252. }
  1253. const char *
  1254. jack_port_name (const jack_port_t *port)
  1255. {
  1256. return port->shared->name;
  1257. }
  1258. const char *
  1259. jack_port_short_name (const jack_port_t *port)
  1260. {
  1261. /* we know there is always a colon, because we put
  1262. it there ...
  1263. */
  1264. return strchr (port->shared->name, ':') + 1;
  1265. }
  1266. int
  1267. jack_port_is_mine (const jack_client_t *client, const jack_port_t *port)
  1268. {
  1269. return port->shared->client_id == client->control->id;
  1270. }
  1271. int
  1272. jack_port_flags (const jack_port_t *port)
  1273. {
  1274. return port->shared->flags;
  1275. }
  1276. const char *
  1277. jack_port_type (const jack_port_t *port)
  1278. {
  1279. return port->shared->type_info.type_name;
  1280. }
  1281. int
  1282. jack_port_set_name (jack_port_t *port, const char *new_name)
  1283. {
  1284. char *colon;
  1285. int len;
  1286. colon = strchr (port->shared->name, ':');
  1287. len = sizeof (port->shared->name) - ((int) (colon - port->shared->name)) - 2;
  1288. snprintf (colon+1, len, "%s", new_name);
  1289. return 0;
  1290. }
  1291. void
  1292. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1293. {
  1294. client->on_shutdown = function;
  1295. client->on_shutdown_arg = arg;
  1296. }
  1297. const char **
  1298. jack_get_ports (jack_client_t *client,
  1299. const char *port_name_pattern,
  1300. const char *type_name_pattern,
  1301. unsigned long flags)
  1302. {
  1303. jack_control_t *engine;
  1304. const char **matching_ports;
  1305. unsigned long match_cnt;
  1306. jack_port_shared_t *psp;
  1307. unsigned long i;
  1308. regex_t port_regex;
  1309. regex_t type_regex;
  1310. int matching;
  1311. engine = client->engine;
  1312. if (port_name_pattern && port_name_pattern[0]) {
  1313. regcomp (&port_regex, port_name_pattern, REG_EXTENDED|REG_NOSUB);
  1314. }
  1315. if (type_name_pattern && type_name_pattern[0]) {
  1316. regcomp (&type_regex, type_name_pattern, REG_EXTENDED|REG_NOSUB);
  1317. }
  1318. psp = engine->ports;
  1319. match_cnt = 0;
  1320. matching_ports = (const char **) malloc (sizeof (char *) * engine->port_max);
  1321. for (i = 0; i < engine->port_max; i++) {
  1322. matching = 1;
  1323. if (!psp[i].in_use) {
  1324. continue;
  1325. }
  1326. if (flags) {
  1327. if ((psp[i].flags & flags) != flags) {
  1328. matching = 0;
  1329. }
  1330. }
  1331. if (matching && port_name_pattern && port_name_pattern[0]) {
  1332. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1333. matching = 0;
  1334. }
  1335. }
  1336. if (matching && type_name_pattern && type_name_pattern[0]) {
  1337. if (regexec (&type_regex, psp[i].type_info.type_name, 0, NULL, 0)) {
  1338. matching = 0;
  1339. }
  1340. }
  1341. if (matching) {
  1342. matching_ports[match_cnt++] = psp[i].name;
  1343. }
  1344. }
  1345. matching_ports[match_cnt] = 0;
  1346. if (match_cnt == 0) {
  1347. free (matching_ports);
  1348. matching_ports = 0;
  1349. }
  1350. return matching_ports;
  1351. }
  1352. static inline void
  1353. jack_read_frame_time (const jack_client_t *client, jack_frame_timer_t *copy)
  1354. {
  1355. int tries = 0;
  1356. do {
  1357. /* throttle the busy wait if we don't get
  1358. the answer very quickly.
  1359. */
  1360. if (tries > 10) {
  1361. usleep (20);
  1362. tries = 0;
  1363. }
  1364. *copy = client->engine->frame_timer;
  1365. tries++;
  1366. } while (copy->guard1 != copy->guard2);
  1367. }
  1368. jack_nframes_t
  1369. jack_frames_since_cycle_start (const jack_client_t *client)
  1370. {
  1371. float usecs;
  1372. usecs = (float) (get_cycles() - client->engine->time.cycles) / client->cpu_mhz;
  1373. return (jack_nframes_t) floor ((((float) client->engine->time.frame_rate) / 1000000.0f) * usecs);
  1374. }
  1375. jack_nframes_t
  1376. jack_frame_time (const jack_client_t *client)
  1377. {
  1378. jack_frame_timer_t current;
  1379. float usecs;
  1380. jack_nframes_t elapsed;
  1381. jack_read_frame_time (client, &current);
  1382. usecs = (float) (get_cycles() - current.stamp) / client->cpu_mhz;
  1383. elapsed = (jack_nframes_t) floor ((((float) client->engine->time.frame_rate) / 1000000.0f) * usecs);
  1384. return current.frames + elapsed;
  1385. }
  1386. int
  1387. jack_port_lock (jack_client_t *client, jack_port_t *port)
  1388. {
  1389. if (port) {
  1390. port->shared->locked = 1;
  1391. return 0;
  1392. }
  1393. return -1;
  1394. }
  1395. int
  1396. jack_port_unlock (jack_client_t *client, jack_port_t *port)
  1397. {
  1398. if (port) {
  1399. port->shared->locked = 0;
  1400. return 0;
  1401. }
  1402. return -1;
  1403. }
  1404. static void
  1405. jack_audio_port_mixdown (jack_port_t *port, jack_nframes_t nframes)
  1406. {
  1407. GSList *node;
  1408. jack_port_t *input;
  1409. jack_nframes_t n;
  1410. jack_default_audio_sample_t *buffer;
  1411. jack_default_audio_sample_t *dst, *src;
  1412. /* by the time we've called this, we've already established
  1413. the existence of more than 1 connection to this input port.
  1414. */
  1415. /* no need to take connection lock, since this is called
  1416. from the process() callback, and the jack server
  1417. ensures that no changes to connections happen
  1418. during this time.
  1419. */
  1420. node = port->connections;
  1421. input = (jack_port_t *) node->data;
  1422. buffer = jack_port_buffer (port);
  1423. memcpy (buffer, jack_port_buffer (input), sizeof (jack_default_audio_sample_t) * nframes);
  1424. for (node = g_slist_next (node); node; node = g_slist_next (node)) {
  1425. input = (jack_port_t *) node->data;
  1426. n = nframes;
  1427. dst = buffer;
  1428. src = jack_port_buffer (input);
  1429. while (n--) {
  1430. *dst++ += *src++;
  1431. }
  1432. }
  1433. }
  1434. const char **
  1435. jack_port_get_connections (const jack_port_t *port)
  1436. {
  1437. const char **ret;
  1438. GSList *node;
  1439. unsigned int n;
  1440. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  1441. if (port->connections == NULL) {
  1442. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1443. return NULL;
  1444. }
  1445. ret = (const char **) malloc (sizeof (char *) * (g_slist_length (port->connections) + 1));
  1446. for (n = 0, node = port->connections; node; node = g_slist_next (node), n++) {
  1447. ret[n] = ((jack_port_t *) node->data)->shared->name;
  1448. }
  1449. ret[n] = NULL;
  1450. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1451. return ret;
  1452. }
  1453. int
  1454. jack_port_connected (const jack_port_t *port)
  1455. {
  1456. return port->connections != NULL;
  1457. }
  1458. int
  1459. jack_port_connected_to (const jack_port_t *port, const char *portname)
  1460. {
  1461. GSList *node;
  1462. int ret = FALSE;
  1463. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  1464. for (node = port->connections; node; node = g_slist_next (node)) {
  1465. jack_port_t *other_port = (jack_port_t *) node->data;
  1466. if (strcmp (other_port->shared->name, portname) == 0) {
  1467. ret = TRUE;
  1468. break;
  1469. }
  1470. }
  1471. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1472. return ret;
  1473. }
  1474. int
  1475. jack_port_connected_to_port (const jack_port_t *port, const jack_port_t *other_port)
  1476. {
  1477. GSList *node;
  1478. int ret = FALSE;
  1479. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  1480. for (node = port->connections; node; node = g_slist_next (node)) {
  1481. jack_port_t *this_port = (jack_port_t *) node->data;
  1482. if (other_port->shared == this_port->shared) {
  1483. ret = TRUE;
  1484. break;
  1485. }
  1486. }
  1487. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1488. return ret;
  1489. }
  1490. /* TRANSPORT CONTROL */
  1491. int
  1492. jack_get_transport_info (jack_client_t *client,
  1493. jack_transport_info_t *info)
  1494. {
  1495. jack_time_info_t *time_info = &client->engine->time;
  1496. if (info->valid & JackTransportState) {
  1497. info->state = time_info->transport_state;
  1498. }
  1499. if (info->valid & JackTransportPosition) {
  1500. info->position = time_info->frame;
  1501. }
  1502. if (info->valid & JackTransportLoop) {
  1503. info->loop_start = time_info->loop_start;
  1504. info->loop_end = time_info->loop_end;
  1505. }
  1506. return 0;
  1507. }
  1508. int
  1509. jack_set_transport_info (jack_client_t *client,
  1510. jack_transport_info_t *info)
  1511. {
  1512. jack_time_info_t *time_info = &client->engine->time;
  1513. if (info->valid & JackTransportState) {
  1514. time_info->transport_state = info->state;
  1515. }
  1516. if (info->valid & JackTransportPosition) {
  1517. time_info->frame = info->position;
  1518. }
  1519. if (info->valid & JackTransportLoop) {
  1520. time_info->loop_start = info->loop_start;
  1521. time_info->loop_end = info->loop_end;
  1522. }
  1523. return 0;
  1524. }
  1525. jack_nframes_t
  1526. jack_port_get_total_latency (jack_client_t *client, jack_port_t *port)
  1527. {
  1528. return port->shared->total_latency;
  1529. }
  1530. int
  1531. jack_get_mhz (void)
  1532. {
  1533. FILE *f = fopen("/proc/cpuinfo", "r");
  1534. if (f == 0)
  1535. {
  1536. perror("can't open /proc/cpuinfo\n");
  1537. exit(1);
  1538. }
  1539. for ( ; ; )
  1540. {
  1541. int mhz;
  1542. int ret;
  1543. char buf[1000];
  1544. if (fgets(buf, sizeof(buf), f) == NULL)
  1545. {
  1546. fprintf(stderr, "cannot locate cpu MHz in /proc/cpuinfo\n");
  1547. exit(1);
  1548. }
  1549. #ifdef __powerpc__
  1550. ret = sscanf(buf, "clock\t: %dMHz", &mhz);
  1551. #else
  1552. ret = sscanf(buf, "cpu MHz : %d", &mhz);
  1553. #endif /* __powerpc__ */
  1554. if (ret == 1)
  1555. {
  1556. fclose(f);
  1557. return mhz;
  1558. }
  1559. }
  1560. }
  1561. float
  1562. jack_cpu_load (jack_client_t *client)
  1563. {
  1564. return client->engine->cpu_load;
  1565. }