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.

2399 lines
58KB

  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 General Public License as published by
  5. the Free Software Foundation; either version 2 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 General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. $Id$
  15. */
  16. #include <unistd.h>
  17. #include <sys/socket.h>
  18. #include <sys/poll.h>
  19. #include <sys/un.h>
  20. #include <sys/stat.h>
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <sys/shm.h>
  24. #include <stdio.h>
  25. #include <stdarg.h>
  26. #include <dirent.h>
  27. #include <sys/ipc.h>
  28. #include <signal.h>
  29. #include <sys/types.h>
  30. #include <string.h>
  31. #include <limits.h>
  32. #include <sys/mman.h>
  33. #include <asm/msr.h>
  34. #include <jack/internal.h>
  35. #include <jack/engine.h>
  36. #include <jack/driver.h>
  37. #define MAX_SHM_ID 256 /* likely use is more like 16 */
  38. typedef struct {
  39. jack_port_internal_t *source;
  40. jack_port_internal_t *destination;
  41. } jack_connection_internal_t;
  42. typedef struct _jack_client_internal {
  43. jack_client_control_t *control;
  44. int request_fd;
  45. int event_fd;
  46. int subgraph_start_fd;
  47. int subgraph_wait_fd;
  48. GSList *ports; /* protected by engine->graph_lock */
  49. GSList *fed_by; /* protected by engine->graph_lock */
  50. int shm_id;
  51. int shm_key;
  52. unsigned long rank;
  53. struct _jack_client_internal *next_client; /* not a linked list! */
  54. dlhandle handle;
  55. } jack_client_internal_t;
  56. static int jack_port_assign_buffer (jack_engine_t *, jack_port_internal_t *);
  57. static jack_port_internal_t *jack_get_port_by_name (jack_engine_t *, const char *name);
  58. static void jack_client_delete (jack_engine_t *, jack_client_internal_t *);
  59. static void jack_remove_client (jack_engine_t *engine, jack_client_internal_t *client);
  60. static jack_client_internal_t *jack_client_internal_new (jack_engine_t *engine, int fd, jack_client_connect_request_t *);
  61. static jack_client_internal_t *jack_client_internal_by_id (jack_engine_t *engine, jack_client_id_t id);
  62. static void jack_sort_graph (jack_engine_t *engine, int take_lock);
  63. static int jack_rechain_graph (jack_engine_t *engine, int take_lock);
  64. static int jack_get_fifo_fd (jack_engine_t *engine, int which_fifo);
  65. static int jack_port_do_connect (jack_engine_t *engine, const char *source_port, const char *destination_port);
  66. static int jack_port_do_disconnect (jack_engine_t *engine, const char *source_port, const char *destination_port);
  67. static int jack_port_do_unregister (jack_engine_t *engine, jack_request_t *);
  68. static int jack_port_do_register (jack_engine_t *engine, jack_request_t *);
  69. static void jack_port_release (jack_engine_t *engine, jack_port_internal_t *);
  70. static void jack_port_clear_connections (jack_engine_t *engine, jack_port_internal_t *port);
  71. static int jack_port_disconnect_internal (jack_engine_t *engine, jack_port_internal_t *src,
  72. jack_port_internal_t *dst, int sort_graph);
  73. static void jack_port_registration_notify (jack_engine_t *, jack_port_id_t, int);
  74. static int jack_send_connection_notification (jack_engine_t *, jack_client_id_t, jack_port_id_t, jack_port_id_t, int);
  75. static int jack_deliver_event (jack_engine_t *, jack_client_internal_t *, jack_event_t *);
  76. static void jack_audio_port_mixdown (jack_port_t *port, nframes_t nframes);
  77. static int *jack_shm_registry;
  78. static int jack_shm_id_cnt;
  79. jack_port_type_info_t builtin_port_types[] = {
  80. { JACK_DEFAULT_AUDIO_TYPE, jack_audio_port_mixdown, 1 },
  81. { 0, NULL }
  82. };
  83. static inline int
  84. jack_client_is_inprocess (jack_client_internal_t *client)
  85. {
  86. return (client->control->type == ClientDynamic) || (client->control->type == ClientDriver);
  87. }
  88. static int
  89. make_sockets (int fd[2])
  90. {
  91. struct sockaddr_un addr;
  92. int i;
  93. /* First, the master server socket */
  94. if ((fd[0] = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  95. jack_error ("cannot create server socket (%s)", strerror (errno));
  96. return -1;
  97. }
  98. addr.sun_family = AF_UNIX;
  99. for (i = 0; i < 999; i++) {
  100. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d", jack_temp_dir, i);
  101. if (access (addr.sun_path, F_OK) != 0) {
  102. break;
  103. }
  104. }
  105. if (i == 999) {
  106. jack_error ("all possible server socket names in use!!!");
  107. close (fd[0]);
  108. return -1;
  109. }
  110. if (bind (fd[0], (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  111. jack_error ("cannot bind server to socket (%s)", strerror (errno));
  112. close (fd[0]);
  113. return -1;
  114. }
  115. if (listen (fd[0], 1) < 0) {
  116. jack_error ("cannot enable listen on server socket (%s)", strerror (errno));
  117. close (fd[0]);
  118. return -1;
  119. }
  120. /* Now the client/server event ack server socket */
  121. if ((fd[1] = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  122. jack_error ("cannot create event ACK socket (%s)", strerror (errno));
  123. close (fd[0]);
  124. return -1;
  125. }
  126. addr.sun_family = AF_UNIX;
  127. for (i = 0; i < 999; i++) {
  128. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_%d", jack_temp_dir, i);
  129. if (access (addr.sun_path, F_OK) != 0) {
  130. break;
  131. }
  132. }
  133. if (i == 999) {
  134. jack_error ("all possible server ACK socket names in use!!!");
  135. close (fd[0]);
  136. close (fd[1]);
  137. return -1;
  138. }
  139. if (bind (fd[1], (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  140. jack_error ("cannot bind server to socket (%s)", strerror (errno));
  141. close (fd[0]);
  142. close (fd[1]);
  143. return -1;
  144. }
  145. if (listen (fd[1], 1) < 0) {
  146. jack_error ("cannot enable listen on server socket (%s)", strerror (errno));
  147. close (fd[0]);
  148. close (fd[1]);
  149. return -1;
  150. }
  151. return 0;
  152. }
  153. static void
  154. jack_cleanup_clients (jack_engine_t *engine)
  155. {
  156. jack_client_control_t *ctl;
  157. jack_client_internal_t *client;
  158. GSList *node;
  159. GSList *remove = 0;
  160. static int x = 0;
  161. x++;
  162. pthread_mutex_lock (&engine->graph_lock);
  163. for (node = engine->clients; node; node = g_slist_next (node)) {
  164. client = (jack_client_internal_t *) node->data;
  165. ctl = client->control;
  166. printf ("client %s state = %d\n", ctl->name, ctl->state);
  167. if (ctl->state > JACK_CLIENT_STATE_NOT_TRIGGERED) {
  168. remove = g_slist_prepend (remove, node->data);
  169. printf ("%d: removing failed client %s\n", x, ctl->name);
  170. }
  171. }
  172. pthread_mutex_unlock (&engine->graph_lock);
  173. if (remove) {
  174. for (node = remove; node; node = g_slist_next (node)) {
  175. jack_remove_client (engine, (jack_client_internal_t *) node->data);
  176. }
  177. g_slist_free (remove);
  178. }
  179. }
  180. static int
  181. jack_initialize_shm ()
  182. {
  183. int shmid_id;
  184. void *addr;
  185. if (jack_shm_registry != NULL) {
  186. return 0;
  187. }
  188. /* grab a chunk of memory to store shm ids in. this is
  189. to allow our parent to clean up all such ids when
  190. if we exit. otherwise, they can get lost in crash
  191. or debugger driven exits.
  192. */
  193. if ((shmid_id = shmget (random(), sizeof(int) * MAX_SHM_ID, IPC_CREAT|0600)) < 0) {
  194. jack_error ("cannot create engine shm ID registry (%s)", strerror (errno));
  195. return -1;
  196. }
  197. if ((addr = shmat (shmid_id, 0, 0)) == (void *) -1) {
  198. jack_error ("cannot attach shm ID registry (%s)", strerror (errno));
  199. shmctl (shmid_id, IPC_RMID, 0);
  200. return -1;
  201. }
  202. if (shmctl (shmid_id, IPC_RMID, NULL)) {
  203. jack_error ("cannot mark shm ID registry as destroyed (%s)", strerror (errno));
  204. return -1;
  205. }
  206. jack_shm_registry = (int *) addr;
  207. jack_shm_id_cnt = 0;
  208. return 0;
  209. }
  210. static void
  211. jack_register_shm (int shmid)
  212. {
  213. if (jack_shm_id_cnt < MAX_SHM_ID) {
  214. jack_shm_registry[jack_shm_id_cnt++] = shmid;
  215. }
  216. }
  217. void
  218. jack_cleanup_shm ()
  219. {
  220. int i;
  221. for (i = 0; i < jack_shm_id_cnt; i++) {
  222. fprintf (stderr, "removing shm ID[%d] = %d\n", i, jack_shm_registry[i]);
  223. shmctl (jack_shm_registry[i], IPC_RMID, NULL);
  224. }
  225. }
  226. void
  227. jack_cleanup_files ()
  228. {
  229. DIR *dir;
  230. struct dirent *dirent;
  231. /* its important that we remove all files that jackd creates
  232. because otherwise subsequent attempts to start jackd will
  233. believe that an instance is already running.
  234. */
  235. if ((dir = opendir (jack_temp_dir)) == NULL) {
  236. fprintf (stderr, "jack(%d): cannot open jack FIFO directory (%s)\n", getpid(), strerror (errno));
  237. return;
  238. }
  239. while ((dirent = readdir (dir)) != NULL) {
  240. if (strncmp (dirent->d_name, "jack-", 5) == 0 || strncmp (dirent->d_name, "jack_", 5) == 0) {
  241. char fullpath[PATH_MAX+1];
  242. sprintf (fullpath, "%s/%s", jack_temp_dir, dirent->d_name);
  243. unlink (fullpath);
  244. }
  245. }
  246. closedir (dir);
  247. }
  248. static int
  249. jack_add_port_segment (jack_engine_t *engine, unsigned long nports)
  250. {
  251. jack_port_segment_info_t *si;
  252. key_t key;
  253. int id;
  254. char *addr;
  255. int offset;
  256. size_t size;
  257. size_t step;
  258. key = random();
  259. size = nports * sizeof (sample_t) * engine->control->buffer_size;
  260. if ((id = shmget (key, size, IPC_CREAT|0666)) < 0) {
  261. jack_error ("cannot create new port segment of %d bytes, key = 0x%x (%s)", size, key, strerror (errno));
  262. return -1;
  263. }
  264. jack_register_shm (id);
  265. if ((addr = shmat (id, 0, 0)) == (char *) -1) {
  266. jack_error ("cannot attach new port segment (%s)", strerror (errno));
  267. shmctl (id, IPC_RMID, 0);
  268. return -1;
  269. }
  270. si = (jack_port_segment_info_t *) malloc (sizeof (jack_port_segment_info_t));
  271. si->shm_key = key;
  272. si->address = addr;
  273. engine->port_segments = g_slist_prepend (engine->port_segments, si);
  274. engine->port_segment_key = key; /* XXX fix me */
  275. engine->port_segment_address = addr; /* XXX fix me */
  276. pthread_mutex_lock (&engine->buffer_lock);
  277. offset = 0;
  278. step = engine->control->buffer_size * sizeof (sample_t);
  279. while (offset < size) {
  280. jack_port_buffer_info_t *bi;
  281. bi = (jack_port_buffer_info_t *) malloc (sizeof (jack_port_buffer_info_t));
  282. bi->shm_key = key;
  283. bi->offset = offset;
  284. /* we append because we want the list to be in memory-address order */
  285. engine->port_buffer_freelist = g_slist_append (engine->port_buffer_freelist, bi);
  286. offset += step;
  287. }
  288. /* convert the first chunk of the segment into a zero-filled area */
  289. if (engine->silent_buffer == 0) {
  290. engine->silent_buffer = (jack_port_buffer_info_t *) engine->port_buffer_freelist->data;
  291. engine->port_buffer_freelist = g_slist_remove_link (engine->port_buffer_freelist, engine->port_buffer_freelist);
  292. memset (engine->port_segment_address + engine->silent_buffer->offset, 0,
  293. sizeof (sample_t) * engine->control->buffer_size);
  294. }
  295. pthread_mutex_unlock (&engine->buffer_lock);
  296. /* XXX notify all clients of new segment */
  297. return 0;
  298. }
  299. static int
  300. jack_set_buffer_size (jack_engine_t *engine, nframes_t nframes)
  301. {
  302. /* XXX this is not really right, since it only works for
  303. audio ports.
  304. */
  305. engine->control->buffer_size = nframes;
  306. jack_add_port_segment (engine, engine->control->port_max);
  307. return 0;
  308. }
  309. static int
  310. jack_set_sample_rate (jack_engine_t *engine, nframes_t nframes)
  311. {
  312. engine->control->sample_rate = nframes;
  313. return 0;
  314. }
  315. static int
  316. jack_process (jack_engine_t *engine, nframes_t nframes)
  317. {
  318. int err = 0;
  319. jack_client_internal_t *client;
  320. jack_client_control_t *ctl;
  321. GSList *node;
  322. struct pollfd pollfd[1];
  323. char c;
  324. unsigned long then, now;
  325. // rdtscl (then);
  326. if (pthread_mutex_trylock (&engine->graph_lock) != 0) {
  327. return 0;
  328. }
  329. for (node = engine->clients; node; node = g_slist_next (node)) {
  330. ctl = ((jack_client_internal_t *) node->data)->control;
  331. ctl->state = JACK_CLIENT_STATE_NOT_TRIGGERED;
  332. ctl->nframes = nframes;
  333. }
  334. if (engine->timebase_client) {
  335. engine->control->frame_time = engine->timebase_client->control->frame_time;
  336. }
  337. for (node = engine->clients; err == 0 && node; ) {
  338. client = (jack_client_internal_t *) node->data;
  339. if (!client->control->active) {
  340. node = g_slist_next (node);
  341. continue;
  342. }
  343. ctl = client->control;
  344. if (jack_client_is_inprocess (client)) {
  345. /* in-process client ("plugin") */
  346. if (ctl->process (nframes, ctl->process_arg) == 0) {
  347. ctl->state = JACK_CLIENT_STATE_FINISHED;
  348. } else {
  349. jack_error ("in-process client %s failed", client->control->name);
  350. ctl->state = JACK_CLIENT_STATE_TRIGGERED;
  351. err++;
  352. break;
  353. }
  354. node = g_slist_next (node);
  355. } else {
  356. /* out of process subgraph */
  357. if (write (client->subgraph_start_fd, &c, sizeof (c)) != sizeof (c)) {
  358. jack_error ("cannot initiate graph processing (%s)", strerror (errno));
  359. err++;
  360. break;
  361. }
  362. /* now wait for the result. use poll instead of read so that we
  363. can timeout effectively.
  364. */
  365. pollfd[0].fd = client->subgraph_wait_fd;
  366. pollfd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  367. rdtscl (then);
  368. if (poll (pollfd, 1, engine->driver->period_interval) < 0) {
  369. jack_error ("engine cannot poll for graph completion (%s)", strerror (errno));
  370. err++;
  371. break;
  372. }
  373. rdtscl (now);
  374. if (pollfd[0].revents == 0) {
  375. jack_error ("subgraph starting at %s timed out (state = %d) (time = %f usecs)",
  376. client->control->name, client->control->state,
  377. ((float)(now - then))/450.0f);
  378. err++;
  379. break;
  380. } else if (pollfd[0].revents & ~POLLIN) {
  381. jack_error ("error/hangup on graph wait fd");
  382. err++;
  383. break;
  384. } else {
  385. if (read (client->subgraph_wait_fd, &c, sizeof (c)) != sizeof (c)) {
  386. jack_error ("cannot clean up byte from graph wait fd (%s)", strerror (errno));
  387. err++;
  388. break;
  389. }
  390. }
  391. /* Move to next in-process client (or end of client list) */
  392. while (node) {
  393. if (jack_client_is_inprocess (((jack_client_internal_t *) node->data))) {
  394. break;
  395. }
  396. node = g_slist_next (node);
  397. }
  398. }
  399. }
  400. pthread_mutex_unlock (&engine->graph_lock);
  401. if (err) {
  402. jack_cleanup_clients (engine);
  403. }
  404. // rdtscl (now);
  405. // printf ("engine cycle time: %.6f usecs\n", ((float) (now - then)) / 450.00f);
  406. return 0;
  407. }
  408. static int
  409. jack_load_client (jack_engine_t *engine, jack_client_internal_t *client, const char *path_to_so)
  410. {
  411. const char *errstr;
  412. dlhandle handle;
  413. handle = dlopen (path_to_so, RTLD_NOW|RTLD_GLOBAL);
  414. if (handle == 0) {
  415. if ((errstr = dlerror ()) != 0) {
  416. jack_error ("can't load \"%s\": %s", path_to_so, errstr);
  417. } else {
  418. jack_error ("bizarre error loading driver shared object %s", path_to_so);
  419. }
  420. return -1;
  421. }
  422. client->handle = handle;
  423. #if 0
  424. initialize = dlsym (handle, "client_initialize");
  425. if ((errstr = dlerror ()) != 0) {
  426. jack_error ("no initialize function in shared object %s\n", path_to_so);
  427. dlclose (handle);
  428. return -1;
  429. }
  430. finish = dlsym (handle, "client_finish");
  431. if ((errstr = dlerror ()) != 0) {
  432. jack_error ("no finish function in in shared driver object %s", path_to_so);
  433. dlclose (handle);
  434. return -1;
  435. }
  436. #endif
  437. return 0;
  438. }
  439. static void
  440. jack_client_unload (jack_client_internal_t *client)
  441. {
  442. if (client->handle) {
  443. // client->finish (client);
  444. dlclose (client->handle);
  445. }
  446. }
  447. static int
  448. handle_new_client (jack_engine_t *engine, int client_fd)
  449. {
  450. jack_client_internal_t *client;
  451. jack_client_connect_request_t req;
  452. jack_client_connect_result_t res;
  453. if (read (client_fd, &req, sizeof (req)) != sizeof (req)) {
  454. jack_error ("cannot read connection request from client");
  455. return -1;
  456. }
  457. res.status = 0;
  458. if ((client = jack_client_internal_new (engine, client_fd, &req)) == 0) {
  459. jack_error ("cannot create new client object");
  460. return -1;
  461. }
  462. printf ("new client: %s, type %d @ %p\n", client->control->name, req.type, client->control);
  463. res.status = 0;
  464. res.client_key = client->shm_key;
  465. res.control_key = engine->control_key;
  466. res.port_segment_key = engine->port_segment_key;
  467. res.realtime = engine->control->real_time;
  468. res.realtime_priority = engine->rtpriority - 1;
  469. if (jack_client_is_inprocess (client)) {
  470. res.client_control = client->control;
  471. res.engine_control = engine->control;
  472. } else {
  473. strcpy (res.fifo_prefix, engine->fifo_prefix);
  474. }
  475. res.status = 0;
  476. if (write (client->request_fd, &res, sizeof (res)) != sizeof (res)) {
  477. jack_error ("cannot write connection response to client");
  478. jack_client_delete (engine, client);
  479. return -1;
  480. }
  481. if (res.status) {
  482. return res.status;
  483. }
  484. pthread_mutex_lock (&engine->graph_lock);
  485. engine->clients = g_slist_prepend (engine->clients, client);
  486. pthread_mutex_unlock (&engine->graph_lock);
  487. if (client->control->type != ClientDynamic) {
  488. if (engine->pfd_max >= engine->pfd_size) {
  489. engine->pfd = (struct pollfd *) realloc (engine->pfd, sizeof (struct pollfd) * engine->pfd_size + 16);
  490. engine->pfd_size += 16;
  491. }
  492. engine->pfd[engine->pfd_max].fd = client->request_fd;
  493. engine->pfd[engine->pfd_max].events = POLLIN|POLLPRI|POLLERR|POLLHUP|POLLNVAL;
  494. engine->pfd_max++;
  495. }
  496. return 0;
  497. }
  498. static int
  499. handle_client_ack_connection (jack_engine_t *engine, int client_fd)
  500. {
  501. jack_client_internal_t *client;
  502. jack_client_connect_ack_request_t req;
  503. jack_client_connect_ack_result_t res;
  504. if (read (client_fd, &req, sizeof (req)) != sizeof (req)) {
  505. jack_error ("cannot read ACK connection request from client");
  506. return -1;
  507. }
  508. if ((client = jack_client_internal_by_id (engine, req.client_id)) == NULL) {
  509. jack_error ("unknown client ID in ACK connection request");
  510. return -1;
  511. }
  512. fprintf (stderr, "client %s is on event fd %d\n", client->control->name, client_fd);
  513. client->event_fd = client_fd;
  514. res.status = 0;
  515. if (write (client->event_fd, &res, sizeof (res)) != sizeof (res)) {
  516. jack_error ("cannot write ACK connection response to client");
  517. return -1;
  518. }
  519. return 0;
  520. }
  521. static int
  522. jack_client_drop (jack_engine_t *engine, jack_client_id_t id)
  523. {
  524. jack_client_internal_t *client;
  525. if ((client = jack_client_internal_by_id (engine, id)) == 0) {
  526. jack_error ("unknown client ID in DropClient request");
  527. return -1;
  528. }
  529. jack_remove_client (engine, client);
  530. return 0;
  531. }
  532. #if 0
  533. static int
  534. jack_client_has_connections (jack_client_internal_t *client)
  535. {
  536. GSList *node;
  537. for (node = client->ports; node; node = g_slist_next (node)) {
  538. if (((jack_port_internal_t *) node->data)->connections) {
  539. return TRUE;
  540. }
  541. }
  542. return FALSE;
  543. }
  544. #endif
  545. static int
  546. jack_client_activate (jack_engine_t *engine, jack_client_id_t id)
  547. {
  548. jack_client_internal_t *client;
  549. GSList *node;
  550. int ret = -1;
  551. pthread_mutex_lock (&engine->graph_lock);
  552. for (node = engine->clients; node; node = g_slist_next (node)) {
  553. if (((jack_client_internal_t *) node->data)->control->id == id) {
  554. client = (jack_client_internal_t *) node->data;
  555. client->control->active = TRUE;
  556. /* we call thus to make sure the FIFO is built by the time
  557. the client needs it. we don't care about the return
  558. value at this point.
  559. */
  560. jack_get_fifo_fd (engine, ++engine->external_client_cnt);
  561. jack_rechain_graph (engine, FALSE);
  562. ret = 0;
  563. break;
  564. }
  565. }
  566. pthread_mutex_unlock (&engine->graph_lock);
  567. return ret;
  568. }
  569. static int
  570. jack_client_do_deactivate (jack_engine_t *engine, jack_client_internal_t *client)
  571. {
  572. /* called must hold engine->graph_lock and must have checked for and/or
  573. cleared all connections held by client.
  574. */
  575. client->control->active = FALSE;
  576. if (!jack_client_is_inprocess (client)) {
  577. engine->external_client_cnt--;
  578. }
  579. jack_sort_graph (engine, FALSE);
  580. return 0;
  581. }
  582. static void
  583. jack_client_disconnect (jack_engine_t *engine, jack_client_internal_t *client)
  584. {
  585. GSList *node;
  586. jack_port_internal_t *port;
  587. /* call tree **** MUST HOLD *** engine->graph_lock */
  588. for (node = client->ports; node; node = g_slist_next (node)) {
  589. port = (jack_port_internal_t *) node->data;
  590. jack_port_clear_connections (engine, port);
  591. jack_port_release (engine, port);
  592. }
  593. g_slist_free (client->ports);
  594. g_slist_free (client->fed_by);
  595. client->fed_by = 0;
  596. client->ports = 0;
  597. }
  598. static int
  599. jack_client_deactivate (jack_engine_t *engine, jack_client_id_t id, int to_wait)
  600. {
  601. GSList *node;
  602. int ret = -1;
  603. pthread_mutex_lock (&engine->graph_lock);
  604. for (node = engine->clients; node; node = g_slist_next (node)) {
  605. jack_client_internal_t *client = (jack_client_internal_t *) node->data;
  606. if (client->control->id == id) {
  607. GSList *portnode;
  608. jack_port_internal_t *port;
  609. if (client == engine->timebase_client) {
  610. engine->timebase_client = 0;
  611. engine->control->frame_time = 0;
  612. }
  613. for (portnode = client->ports; portnode; portnode = g_slist_next (portnode)) {
  614. port = (jack_port_internal_t *) portnode->data;
  615. jack_port_clear_connections (engine, port);
  616. }
  617. ret = jack_client_do_deactivate (engine, node->data);
  618. break;
  619. }
  620. }
  621. pthread_mutex_unlock (&engine->graph_lock);
  622. return ret;
  623. }
  624. static int
  625. jack_set_timebase (jack_engine_t *engine, jack_client_id_t client)
  626. {
  627. int ret = -1;
  628. pthread_mutex_lock (&engine->graph_lock);
  629. if ((engine->timebase_client = jack_client_internal_by_id (engine, client)) != 0) {
  630. engine->control->frame_time = engine->timebase_client->control->frame_time;
  631. ret = 0;
  632. }
  633. pthread_mutex_unlock (&engine->graph_lock);
  634. return ret;
  635. }
  636. static int
  637. handle_client_jack_error (jack_engine_t *engine, int fd)
  638. {
  639. jack_client_internal_t *client = 0;
  640. GSList *node;
  641. pthread_mutex_lock (&engine->graph_lock);
  642. for (node = engine->clients; node; node = g_slist_next (node)) {
  643. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  644. client = (jack_client_internal_t *) node->data;
  645. break;
  646. }
  647. }
  648. pthread_mutex_unlock (&engine->graph_lock);
  649. if (client == 0) {
  650. jack_error ("i/o error on unknown client fd %d", fd);
  651. return -1;
  652. }
  653. jack_remove_client (engine, client);
  654. return 0;
  655. }
  656. static int
  657. jack_client_port_monitor (jack_engine_t *engine, jack_port_id_t port_id, int onoff)
  658. {
  659. jack_port_shared_t *port;
  660. jack_client_internal_t *client = NULL;
  661. jack_event_t event;
  662. if (port_id < 0 || port_id >= engine->port_max) {
  663. jack_error ("illegal port ID in port monitor request");
  664. return -1;
  665. }
  666. port = &engine->control->ports[port_id];
  667. if (!(port->flags & JackPortCanMonitor)) {
  668. jack_error ("port monitor request made on a port (%s) that doesn't support monitoring",
  669. port->name);
  670. return -1;
  671. }
  672. pthread_mutex_lock (&engine->graph_lock);
  673. if ((client = jack_client_internal_by_id (engine, port->client_id)) == NULL) {
  674. jack_error ("unknown client owns port %d!!", port_id);
  675. pthread_mutex_unlock (&engine->graph_lock);
  676. return -1;
  677. }
  678. pthread_mutex_unlock (&engine->graph_lock);
  679. event.type = (onoff ? PortMonitor : PortUnMonitor);
  680. event.x.port_id = port_id;
  681. return jack_deliver_event (engine, client, &event);
  682. }
  683. static int
  684. handle_client_io (jack_engine_t *engine, int fd)
  685. {
  686. jack_request_t req;
  687. jack_client_internal_t *client = 0;
  688. int reply_fd;
  689. GSList *node;
  690. pthread_mutex_lock (&engine->graph_lock);
  691. for (node = engine->clients; node; node = g_slist_next (node)) {
  692. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  693. client = (jack_client_internal_t *) node->data;
  694. break;
  695. }
  696. }
  697. pthread_mutex_unlock (&engine->graph_lock);
  698. if (client == 0) {
  699. jack_error ("client input on unknown fd %d!", fd);
  700. return -1;
  701. }
  702. if (read (client->request_fd, &req, sizeof (req)) < sizeof (req)) {
  703. jack_error ("cannot read request from client");
  704. jack_remove_client (engine, client);
  705. return -1;
  706. }
  707. reply_fd = client->request_fd;
  708. switch (req.type) {
  709. case RegisterPort:
  710. req.status = jack_port_do_register (engine, &req);
  711. break;
  712. case UnRegisterPort:
  713. req.status = jack_port_do_unregister (engine, &req);
  714. break;
  715. case ConnectPorts:
  716. req.status = jack_port_do_connect (engine, req.x.connect.source_port, req.x.connect.destination_port);
  717. break;
  718. case DisconnectPorts:
  719. req.status = jack_port_do_disconnect (engine, req.x.connect.source_port, req.x.connect.destination_port);
  720. break;
  721. case DropClient:
  722. req.status = jack_client_drop (engine, req.x.client_id);
  723. reply_fd = -1;
  724. break;
  725. case ActivateClient:
  726. req.status = jack_client_activate (engine, req.x.client_id);
  727. break;
  728. case DeactivateClient:
  729. req.status = jack_client_deactivate (engine, req.x.client_id, TRUE);
  730. break;
  731. case SetTimeBaseClient:
  732. req.status = jack_set_timebase (engine, req.x.client_id);
  733. break;
  734. case RequestPortMonitor:
  735. req.status = jack_client_port_monitor (engine, req.x.port_info.port_id, TRUE);
  736. break;
  737. case RequestPortUnMonitor:
  738. req.status = jack_client_port_monitor (engine, req.x.port_info.port_id, FALSE);
  739. break;
  740. }
  741. if (reply_fd >= 0) {
  742. if (write (reply_fd, &req, sizeof (req)) < sizeof (req)) {
  743. jack_error ("cannot write request result to client");
  744. return -1;
  745. }
  746. }
  747. return 0;
  748. }
  749. static void *
  750. jack_server_thread (void *arg)
  751. {
  752. jack_engine_t *engine = (jack_engine_t *) arg;
  753. struct sockaddr_un client_addr;
  754. socklen_t client_addrlen;
  755. struct pollfd *pfd;
  756. int client_socket;
  757. int done = 0;
  758. int i;
  759. int max;
  760. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  761. engine->pfd[0].fd = engine->fds[0];
  762. engine->pfd[0].events = POLLIN|POLLERR;
  763. engine->pfd[1].fd = engine->fds[1];
  764. engine->pfd[1].events = POLLIN|POLLERR;
  765. engine->pfd_max = 2;
  766. while (!done) {
  767. /* XXX race here with new external clients
  768. causing engine->pfd to be reallocated.
  769. I don't know how to solve this
  770. short of copying the entire
  771. contents of the pfd struct. Ick.
  772. */
  773. max = engine->pfd_max;
  774. pfd = engine->pfd;
  775. if (poll (pfd, max, 10000) < 0) {
  776. if (errno == EINTR) {
  777. continue;
  778. }
  779. jack_error ("poll failed (%s)", strerror (errno));
  780. break;
  781. }
  782. /* check the master server socket */
  783. if (pfd[0].revents & POLLERR) {
  784. jack_error ("error on server socket");
  785. break;
  786. }
  787. if (pfd[0].revents & POLLIN) {
  788. memset (&client_addr, 0, sizeof (client_addr));
  789. client_addrlen = sizeof (client_addr);
  790. if ((client_socket = accept (engine->fds[0], (struct sockaddr *) &client_addr, &client_addrlen)) < 0) {
  791. jack_error ("cannot accept new connection (%s)", strerror (errno));
  792. } else if (handle_new_client (engine, client_socket) < 0) {
  793. jack_error ("cannot complete new client connection process");
  794. close (client_socket);
  795. }
  796. }
  797. /* check the ACK server socket */
  798. if (pfd[1].revents & POLLERR) {
  799. jack_error ("error on server ACK socket");
  800. break;
  801. }
  802. if (pfd[1].revents & POLLIN) {
  803. memset (&client_addr, 0, sizeof (client_addr));
  804. client_addrlen = sizeof (client_addr);
  805. if ((client_socket = accept (engine->fds[1], (struct sockaddr *) &client_addr, &client_addrlen)) < 0) {
  806. jack_error ("cannot accept new ACK connection (%s)", strerror (errno));
  807. } else if (handle_client_ack_connection (engine, client_socket)) {
  808. jack_error ("cannot complete client ACK connection process");
  809. close (client_socket);
  810. }
  811. }
  812. /* check each client socket */
  813. for (i = 2; i < max; i++) {
  814. if (pfd[i].fd < 0) {
  815. continue;
  816. }
  817. if (pfd[i].revents & ~POLLIN) {
  818. handle_client_jack_error (engine, pfd[i].fd);
  819. } else if (pfd[i].revents & POLLIN) {
  820. if (handle_client_io (engine, pfd[i].fd)) {
  821. jack_error ("bad hci\n");
  822. }
  823. }
  824. }
  825. }
  826. return 0;
  827. }
  828. static void
  829. jack_start_server (jack_engine_t *engine)
  830. {
  831. pthread_create (&engine->server_thread, 0, &jack_server_thread, engine);
  832. pthread_detach (engine->server_thread);
  833. }
  834. jack_engine_t *
  835. jack_engine_new (int realtime, int rtpriority)
  836. {
  837. jack_engine_t *engine;
  838. size_t control_size;
  839. void *addr;
  840. int i;
  841. engine = (jack_engine_t *) malloc (sizeof (jack_engine_t));
  842. engine->driver = 0;
  843. engine->process = jack_process;
  844. engine->set_sample_rate = jack_set_sample_rate;
  845. engine->set_buffer_size = jack_set_buffer_size;
  846. engine->next_client_id = 1;
  847. engine->timebase_client = 0;
  848. engine->port_max = 128;
  849. engine->rtpriority = rtpriority;
  850. engine->silent_buffer = 0;
  851. pthread_mutex_init (&engine->graph_lock, 0);
  852. pthread_mutex_init (&engine->buffer_lock, 0);
  853. pthread_mutex_init (&engine->port_lock, 0);
  854. engine->clients = 0;
  855. engine->port_segments = 0;
  856. engine->port_buffer_freelist = 0;
  857. engine->pfd_size = 16;
  858. engine->pfd_max = 0;
  859. engine->pfd = (struct pollfd *) malloc (sizeof (struct pollfd) * engine->pfd_size);
  860. engine->fifo_size = 16;
  861. engine->fifo = (int *) malloc (sizeof (int) * engine->fifo_size);
  862. for (i = 0; i < engine->fifo_size; i++) {
  863. engine->fifo[i] = -1;
  864. }
  865. /* Build a linked list of known port types. We use a list so that
  866. we can easily manage other data types without messing with
  867. reallocation of arrays, etc.
  868. */
  869. engine->port_types = NULL;
  870. for (i = 0; builtin_port_types[i].type_name; i++) {
  871. engine->port_types = g_slist_append (engine->port_types, &builtin_port_types[i]);
  872. }
  873. engine->external_client_cnt = 0;
  874. srandom (time ((time_t *) 0));
  875. engine->control_key = random();
  876. control_size = sizeof (jack_control_t) + (sizeof (jack_port_shared_t) * engine->port_max);
  877. if (jack_initialize_shm (engine)) {
  878. return 0;
  879. }
  880. if ((engine->control_shm_id = shmget (engine->control_key, control_size, IPC_CREAT|0644)) < 0) {
  881. jack_error ("cannot create engine control shared memory segment (%s)", strerror (errno));
  882. return 0;
  883. }
  884. jack_register_shm (engine->control_shm_id);
  885. if ((addr = shmat (engine->control_shm_id, 0, 0)) == (void *) -1) {
  886. jack_error ("cannot attach control shared memory segment (%s)", strerror (errno));
  887. shmctl (engine->control_shm_id, IPC_RMID, 0);
  888. return 0;
  889. }
  890. engine->control = (jack_control_t *) addr;
  891. /* Mark all ports as available */
  892. for (i = 0; i < engine->port_max; i++) {
  893. engine->control->ports[i].in_use = 0;
  894. engine->control->ports[i].id = i;
  895. }
  896. /* allocate internal port structures so that we can keep
  897. track of port connections.
  898. */
  899. engine->internal_ports = (jack_port_internal_t *) malloc (sizeof (jack_port_internal_t) * engine->port_max);
  900. for (i = 0; i < engine->port_max; i++) {
  901. engine->internal_ports[i].connections = 0;
  902. }
  903. if (make_sockets (engine->fds) < 0) {
  904. jack_error ("cannot create server sockets");
  905. return 0;
  906. }
  907. engine->control->port_max = engine->port_max;
  908. engine->control->real_time = realtime;
  909. engine->control->client_priority = engine->rtpriority - 1;
  910. engine->control->sample_rate = 0;
  911. engine->control->buffer_size = 0;
  912. engine->control->frame_time = 0;
  913. sprintf (engine->fifo_prefix, "%s/ack_fifo_%d", jack_temp_dir, getpid());
  914. (void) jack_get_fifo_fd (engine, 0);
  915. jack_start_server (engine);
  916. return engine;
  917. }
  918. static int
  919. jack_become_real_time (pthread_t thread, int priority)
  920. {
  921. struct sched_param rtparam;
  922. int x;
  923. memset (&rtparam, 0, sizeof (rtparam));
  924. rtparam.sched_priority = priority;
  925. if ((x = pthread_setschedparam (thread, SCHED_FIFO, &rtparam)) != 0) {
  926. jack_error ("cannot set thread to real-time priority (FIFO/%d) (%d: %s)", rtparam.sched_priority, x, strerror (errno));
  927. }
  928. if (mlockall (MCL_CURRENT | MCL_FUTURE) != 0) {
  929. jack_error ("cannot lock down memory for RT thread (%s)", strerror (errno));
  930. }
  931. return 0;
  932. }
  933. void
  934. cancel_cleanup1 (void *arg)
  935. {
  936. jack_engine_t *engine = (jack_engine_t *) arg;
  937. printf ("audio thread cancelled or finished\n");
  938. engine->driver->audio_stop (engine->driver);
  939. }
  940. void
  941. cancel_cleanup2 (int status, void *arg)
  942. {
  943. jack_engine_t *engine = (jack_engine_t *) arg;
  944. engine->driver->audio_stop (engine->driver);
  945. engine->driver->finish (engine->driver);
  946. }
  947. static void *
  948. jack_main_thread (void *arg)
  949. {
  950. jack_engine_t *engine = (jack_engine_t *) arg;
  951. jack_driver_t *driver = engine->driver;
  952. // unsigned long start, end;
  953. if (engine->control->real_time) {
  954. jack_become_real_time (pthread_self(), engine->rtpriority);
  955. }
  956. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  957. on_exit (cancel_cleanup2, engine);
  958. if (driver->audio_start (driver)) {
  959. jack_error ("cannot start driver");
  960. pthread_exit (0);
  961. }
  962. while (1) {
  963. // start = end;
  964. if (driver->wait (driver)) {
  965. break;
  966. }
  967. // rdtscl (end);
  968. // printf ("driver cycle time: %.6f usecs\n", ((float) (end - start)) / 450.00f);
  969. }
  970. pthread_exit (0);
  971. }
  972. int
  973. jack_run (jack_engine_t *engine)
  974. {
  975. if (engine->driver == 0) {
  976. jack_error ("engine driver not set; cannot start");
  977. return -1;
  978. }
  979. return pthread_create (&engine->main_thread, 0, jack_main_thread, engine);
  980. }
  981. int
  982. jack_wait (jack_engine_t *engine)
  983. {
  984. void *ret = 0;
  985. int err;
  986. if ((err = pthread_join (engine->main_thread, &ret)) != 0) {
  987. switch (err) {
  988. case EINVAL:
  989. jack_error ("cannot join with audio thread (thread detached, or another thread is waiting)");
  990. break;
  991. case ESRCH:
  992. jack_error ("cannot join with audio thread (thread no longer exists)");
  993. break;
  994. case EDEADLK:
  995. jack_error ("programming error: jack_wait() called by audio thread");
  996. break;
  997. default:
  998. jack_error ("cannot join with audio thread (%s)", strerror (errno));
  999. }
  1000. }
  1001. return (int) ret;
  1002. }
  1003. int
  1004. jack_engine_delete (jack_engine_t *engine)
  1005. {
  1006. if (engine) {
  1007. return pthread_cancel (engine->main_thread);
  1008. }
  1009. return 0;
  1010. }
  1011. static jack_client_internal_t *
  1012. jack_client_internal_new (jack_engine_t *engine, int fd, jack_client_connect_request_t *req)
  1013. {
  1014. jack_client_internal_t *client;
  1015. key_t shm_key = 0;
  1016. int shm_id = 0;
  1017. void *addr = 0;
  1018. switch (req->type) {
  1019. case ClientDynamic:
  1020. case ClientDriver:
  1021. break;
  1022. case ClientOutOfProcess:
  1023. shm_key = random();
  1024. if ((shm_id = shmget (shm_key, sizeof (jack_client_control_t), IPC_CREAT|0666)) < 0) {
  1025. jack_error ("cannot create client control block");
  1026. return 0;
  1027. }
  1028. jack_register_shm (shm_id);
  1029. if ((addr = shmat (shm_id, 0, 0)) == (void *) -1) {
  1030. jack_error ("cannot attach new client control block");
  1031. shmctl (shm_id, IPC_RMID, 0);
  1032. return 0;
  1033. }
  1034. break;
  1035. }
  1036. client = (jack_client_internal_t *) malloc (sizeof (jack_client_internal_t));
  1037. client->request_fd = fd;
  1038. client->event_fd = -1;
  1039. client->ports = 0;
  1040. client->fed_by = 0;
  1041. client->rank = UINT_MAX;
  1042. client->next_client = NULL;
  1043. client->handle = NULL;
  1044. if (req->type != ClientOutOfProcess) {
  1045. client->control = (jack_client_control_t *) malloc (sizeof (jack_client_control_t));
  1046. } else {
  1047. client->shm_id = shm_id;
  1048. client->shm_key = shm_key;
  1049. client->control = (jack_client_control_t *) addr;
  1050. }
  1051. client->control->type = req->type;
  1052. client->control->active = FALSE;
  1053. client->control->dead = FALSE;
  1054. client->control->id = engine->next_client_id++;
  1055. strcpy ((char *) client->control->name, req->name);
  1056. client->control->process = NULL;
  1057. client->control->process_arg = NULL;
  1058. client->control->bufsize = NULL;
  1059. client->control->bufsize_arg = NULL;
  1060. client->control->srate = NULL;
  1061. client->control->srate_arg = NULL;
  1062. client->control->port_register = NULL;
  1063. client->control->port_register_arg = NULL;
  1064. client->control->port_monitor = NULL;
  1065. client->control->port_monitor_arg = NULL;
  1066. if (req->type == ClientDynamic) {
  1067. if (jack_load_client (engine, client, req->object_path)) {
  1068. jack_error ("cannot dynamically load client from \"%s\"", req->object_path);
  1069. jack_client_delete (engine, client);
  1070. return 0;
  1071. }
  1072. }
  1073. return client;
  1074. }
  1075. static void
  1076. jack_port_clear_connections (jack_engine_t *engine, jack_port_internal_t *port)
  1077. {
  1078. GSList *node, *next;
  1079. for (node = port->connections; node; ) {
  1080. next = g_slist_next (node);
  1081. jack_port_disconnect_internal (engine,
  1082. ((jack_connection_internal_t *) node->data)->source,
  1083. ((jack_connection_internal_t *) node->data)->destination,
  1084. FALSE);
  1085. node = next;
  1086. }
  1087. g_slist_free (port->connections);
  1088. port->connections = 0;
  1089. }
  1090. static void
  1091. jack_remove_client (jack_engine_t *engine, jack_client_internal_t *client)
  1092. {
  1093. GSList *node;
  1094. int i;
  1095. printf ("removing client %s\n", client->control->name);
  1096. pthread_mutex_lock (&engine->graph_lock);
  1097. client->control->dead = TRUE;
  1098. if (client == engine->timebase_client) {
  1099. engine->timebase_client = 0;
  1100. engine->control->frame_time = 0;
  1101. }
  1102. jack_client_disconnect (engine, client);
  1103. for (node = engine->clients; node; node = g_slist_next (node)) {
  1104. if (((jack_client_internal_t *) node->data)->control->id == client->control->id) {
  1105. engine->clients = g_slist_remove_link (engine->clients, node);
  1106. g_slist_free_1 (node);
  1107. break;
  1108. }
  1109. }
  1110. jack_client_do_deactivate (engine, client);
  1111. /* rearrange the pollfd array so that things work right the
  1112. next time we go into poll(2).
  1113. */
  1114. for (i = 0; i < engine->pfd_max; i++) {
  1115. if (engine->pfd[i].fd == client->request_fd) {
  1116. if (i+1 < engine->pfd_max) {
  1117. memmove (&engine->pfd[i], &engine->pfd[i+1], sizeof (struct pollfd) * (engine->pfd_max - i));
  1118. }
  1119. engine->pfd_max--;
  1120. }
  1121. }
  1122. close (client->event_fd);
  1123. close (client->request_fd);
  1124. jack_client_delete (engine, client);
  1125. pthread_mutex_unlock (&engine->graph_lock);
  1126. }
  1127. static void
  1128. jack_client_delete (jack_engine_t *engine, jack_client_internal_t *client)
  1129. {
  1130. jack_client_disconnect (engine, client);
  1131. if (jack_client_is_inprocess (client)) {
  1132. jack_client_unload (client);
  1133. free ((char *) client->control);
  1134. } else {
  1135. shmdt ((void *) client->control);
  1136. }
  1137. free (client);
  1138. }
  1139. jack_client_internal_t *
  1140. jack_client_by_name (jack_engine_t *engine, const char *name)
  1141. {
  1142. jack_client_internal_t *client = NULL;
  1143. GSList *node;
  1144. pthread_mutex_lock (&engine->graph_lock);
  1145. for (node = engine->clients; node; node = g_slist_next (node)) {
  1146. if (strcmp ((const char *) ((jack_client_internal_t *) node->data)->control->name, name) == 0) {
  1147. client = (jack_client_internal_t *) node->data;
  1148. break;
  1149. }
  1150. }
  1151. pthread_mutex_unlock (&engine->graph_lock);
  1152. return client;
  1153. }
  1154. jack_client_internal_t *
  1155. jack_client_internal_by_id (jack_engine_t *engine, jack_client_id_t id)
  1156. {
  1157. jack_client_internal_t *client = NULL;
  1158. GSList *node;
  1159. /* call tree ***MUST HOLD*** engine->graph_lock */
  1160. for (node = engine->clients; node; node = g_slist_next (node)) {
  1161. if (((jack_client_internal_t *) node->data)->control->id == id) {
  1162. client = (jack_client_internal_t *) node->data;
  1163. break;
  1164. }
  1165. }
  1166. return client;
  1167. }
  1168. static int
  1169. jack_deliver_event (jack_engine_t *engine, jack_client_internal_t *client, jack_event_t *event)
  1170. {
  1171. char status;
  1172. if (client->control->dead) {
  1173. return 0;
  1174. }
  1175. if (jack_client_is_inprocess (client)) {
  1176. switch (event->type) {
  1177. case PortConnected:
  1178. case PortDisconnected:
  1179. jack_client_handle_port_connection (client->control->private_internal_client, event);
  1180. break;
  1181. case GraphReordered:
  1182. jack_error ("reorder event delivered to internal client!");
  1183. break;
  1184. case BufferSizeChange:
  1185. if (client->control->bufsize) {
  1186. client->control->bufsize (event->x.n, client->control->bufsize_arg);
  1187. }
  1188. break;
  1189. case SampleRateChange:
  1190. if (client->control->srate) {
  1191. client->control->srate (event->x.n, client->control->bufsize_arg);
  1192. }
  1193. break;
  1194. case PortMonitor:
  1195. if (client->control->port_monitor) {
  1196. client->control->port_monitor (event->x.port_id, TRUE, client->control->port_monitor_arg);
  1197. }
  1198. break;
  1199. case PortUnMonitor:
  1200. if (client->control->port_monitor) {
  1201. client->control->port_monitor (event->x.port_id, FALSE, client->control->port_monitor_arg);
  1202. }
  1203. break;
  1204. default:
  1205. /* internal clients don't need to know */
  1206. break;
  1207. }
  1208. } else {
  1209. if (write (client->event_fd, event, sizeof (*event)) != sizeof (*event)) {
  1210. jack_error ("cannot send event to client [%s] (%s)", client->control->name, strerror (errno));
  1211. return -1;
  1212. }
  1213. if (read (client->event_fd, &status, sizeof (status)) != sizeof (status)) {
  1214. jack_error ("cannot read event response from client [%s] (%s)", client->control->name, strerror (errno));
  1215. return -1;
  1216. }
  1217. }
  1218. return 0;
  1219. }
  1220. int
  1221. jack_client_set_order (jack_engine_t *engine, jack_client_internal_t *client)
  1222. {
  1223. jack_event_t event;
  1224. event.type = GraphReordered;
  1225. event.x.n = client->rank;
  1226. return jack_deliver_event (engine, client, &event);
  1227. }
  1228. int
  1229. jack_rechain_graph (jack_engine_t *engine, int take_lock)
  1230. {
  1231. GSList *node, *next;
  1232. unsigned long n;
  1233. int err = 0;
  1234. int need_to_reset_fifo;
  1235. jack_client_internal_t *client, *subgraph_client, *next_client;
  1236. if (take_lock) {
  1237. pthread_mutex_lock (&engine->graph_lock);
  1238. }
  1239. /* We're going to try to avoid reconnecting clients that
  1240. don't need to be reconnected. This is slightly tricky,
  1241. but worth it for performance reasons.
  1242. */
  1243. subgraph_client = 0;
  1244. n = 0;
  1245. for (n = 0, node = engine->clients, next = NULL; node; node = next) {
  1246. next = g_slist_next (node);
  1247. if (((jack_client_internal_t *) node->data)->control->active) {
  1248. client = (jack_client_internal_t *) node->data;
  1249. /* find the next active client. its ok for this to be NULL */
  1250. while (next) {
  1251. if (((jack_client_internal_t *) next->data)->control->active) {
  1252. break;
  1253. }
  1254. next = g_slist_next (next);
  1255. };
  1256. if (next == NULL) {
  1257. next_client = NULL;
  1258. } else {
  1259. next_client = (jack_client_internal_t *) next->data;
  1260. }
  1261. if (client->rank != n || client->next_client != next_client) {
  1262. client->rank = n;
  1263. client->next_client = next_client;
  1264. need_to_reset_fifo = TRUE;
  1265. } else {
  1266. need_to_reset_fifo = FALSE;
  1267. }
  1268. if (jack_client_is_inprocess (client)) {
  1269. /* break the chain for the current subgraph. the server
  1270. will wait for chain on the nth FIFO, and will
  1271. then execute this in-process client.
  1272. */
  1273. if (subgraph_client) {
  1274. subgraph_client->subgraph_wait_fd = jack_get_fifo_fd (engine, n);
  1275. }
  1276. subgraph_client = 0;
  1277. } else {
  1278. if (subgraph_client == 0) {
  1279. /* start a new subgraph. the engine will start the chain
  1280. by writing to the nth FIFO.
  1281. */
  1282. subgraph_client = client;
  1283. subgraph_client->subgraph_start_fd = jack_get_fifo_fd (engine, n);
  1284. }
  1285. if (need_to_reset_fifo) {
  1286. jack_client_set_order (engine, client);
  1287. }
  1288. n++;
  1289. }
  1290. }
  1291. };
  1292. if (subgraph_client) {
  1293. subgraph_client->subgraph_wait_fd = jack_get_fifo_fd (engine, n);
  1294. }
  1295. if (take_lock) {
  1296. pthread_mutex_unlock (&engine->graph_lock);
  1297. }
  1298. return err;
  1299. }
  1300. static void
  1301. jack_trace_terminal (jack_client_internal_t *c1, jack_client_internal_t *rbase)
  1302. {
  1303. jack_client_internal_t *c2;
  1304. /* make a copy of the existing list of routes that feed c1 */
  1305. GSList *existing;
  1306. GSList *node;
  1307. if (c1->fed_by == 0) {
  1308. return;
  1309. }
  1310. existing = g_slist_copy (c1->fed_by);
  1311. /* for each route that feeds c1, recurse, marking it as feeding
  1312. rbase as well.
  1313. */
  1314. for (node = existing; node; node = g_slist_next (node)) {
  1315. c2 = (jack_client_internal_t *) node->data;
  1316. /* c2 is a route that feeds c1 which somehow feeds base. mark
  1317. base as being fed by c2, but don't do it more than
  1318. once.
  1319. */
  1320. if (g_slist_find (rbase->fed_by, c2) != NULL) {
  1321. continue;
  1322. }
  1323. rbase->fed_by = g_slist_prepend (rbase->fed_by, c2);
  1324. if (c2 != rbase && c2 != c1) {
  1325. /* now recurse, so that we can mark base as being fed by
  1326. all routes that feed c2
  1327. */
  1328. jack_trace_terminal (c2, rbase);
  1329. }
  1330. }
  1331. }
  1332. static int
  1333. jack_client_sort (jack_client_internal_t *a, jack_client_internal_t *b)
  1334. {
  1335. /* the driver client always comes after everything else */
  1336. if (a->control->type == ClientDriver) {
  1337. return 1;
  1338. }
  1339. if (b->control->type == ClientDriver) {
  1340. return -1;
  1341. }
  1342. if (g_slist_find (a->fed_by, b)) {
  1343. /* a comes after b */
  1344. return 1;
  1345. } else if (g_slist_find (b->fed_by, a)) {
  1346. /* b comes after a */
  1347. return -1;
  1348. } else {
  1349. /* we don't care */
  1350. return 0;
  1351. }
  1352. }
  1353. static int
  1354. jack_client_feeds (jack_client_internal_t *might, jack_client_internal_t *target)
  1355. {
  1356. GSList *pnode, *cnode;
  1357. /* Check every port of `might' for an outbound connection to `target'
  1358. */
  1359. for (pnode = might->ports; pnode; pnode = g_slist_next (pnode)) {
  1360. jack_port_internal_t *port;
  1361. port = (jack_port_internal_t *) pnode->data;
  1362. for (cnode = port->connections; cnode; cnode = g_slist_next (cnode)) {
  1363. jack_connection_internal_t *c;
  1364. c = (jack_connection_internal_t *) cnode->data;
  1365. if (c->source->shared->client_id == might->control->id &&
  1366. c->destination->shared->client_id == target->control->id) {
  1367. return 1;
  1368. }
  1369. }
  1370. }
  1371. return 0;
  1372. }
  1373. /**
  1374. * Sorts the network of clients using the following
  1375. * algorithm:
  1376. *
  1377. * 1) figure out who is connected to whom:
  1378. *
  1379. * foreach client1
  1380. * foreach input port
  1381. * foreach client2
  1382. * foreach output port
  1383. * if client1->input port connected to client2->output port
  1384. * mark client1 fed by client 2
  1385. *
  1386. * 2) trace the connections as terminal arcs in the graph so that
  1387. * if client A feeds client B who feeds client C, mark client C
  1388. * as fed by client A as well as client B, and so forth.
  1389. *
  1390. * 3) now sort according to whether or not client1->fed_by (client2) is true.
  1391. * if the condition is true, client2 must execute before client1
  1392. */
  1393. static void
  1394. jack_sort_graph (jack_engine_t *engine, int take_lock)
  1395. {
  1396. GSList *node, *onode;
  1397. jack_client_internal_t *client;
  1398. jack_client_internal_t *oclient;
  1399. if (take_lock) {
  1400. pthread_mutex_lock (&engine->graph_lock);
  1401. }
  1402. for (node = engine->clients; node; node = g_slist_next (node)) {
  1403. client = (jack_client_internal_t *) node->data;
  1404. g_slist_free (client->fed_by);
  1405. client->fed_by = 0;
  1406. for (onode = engine->clients; onode; onode = g_slist_next (onode)) {
  1407. oclient = (jack_client_internal_t *) onode->data;
  1408. if (jack_client_feeds (oclient, client)) {
  1409. client->fed_by = g_slist_prepend (client->fed_by, oclient);
  1410. }
  1411. }
  1412. }
  1413. for (node = engine->clients; node; node = g_slist_next (node)) {
  1414. jack_trace_terminal ((jack_client_internal_t *) node->data,
  1415. (jack_client_internal_t *) node->data);
  1416. }
  1417. engine->clients = g_slist_sort (engine->clients, (GCompareFunc) jack_client_sort);
  1418. jack_rechain_graph (engine, FALSE);
  1419. if (take_lock) {
  1420. pthread_mutex_unlock (&engine->graph_lock);
  1421. }
  1422. }
  1423. static int
  1424. jack_port_do_connect (jack_engine_t *engine,
  1425. const char *source_port,
  1426. const char *destination_port)
  1427. {
  1428. jack_connection_internal_t *connection;
  1429. jack_port_internal_t *srcport, *dstport;
  1430. jack_port_id_t src_id, dst_id;
  1431. fprintf (stderr, "trying to connect %s and %s\n", source_port, destination_port);
  1432. if ((srcport = jack_get_port_by_name (engine, source_port)) == 0) {
  1433. jack_error ("unknown source port in attempted connection [%s]", source_port);
  1434. return -1;
  1435. }
  1436. if ((dstport = jack_get_port_by_name (engine, destination_port)) == 0) {
  1437. jack_error ("unknown destination port in attempted connection [%s]", destination_port);
  1438. return -1;
  1439. }
  1440. if ((dstport->shared->flags & JackPortIsInput) == 0) {
  1441. jack_error ("destination port in attempted connection is not an input port");
  1442. return -1;
  1443. }
  1444. if ((srcport->shared->flags & JackPortIsOutput) == 0) {
  1445. jack_error ("source port in attempted connection is not an output port");
  1446. return -1;
  1447. }
  1448. if (strcmp (srcport->shared->type_info.type_name,
  1449. dstport->shared->type_info.type_name) != 0) {
  1450. jack_error ("ports used in attemped connection are not of the same data type");
  1451. return -1;
  1452. }
  1453. connection = (jack_connection_internal_t *) malloc (sizeof (jack_connection_internal_t));
  1454. connection->source = srcport;
  1455. connection->destination = dstport;
  1456. src_id = srcport->shared->id;
  1457. dst_id = dstport->shared->id;
  1458. pthread_mutex_lock (&engine->graph_lock);
  1459. if (dstport->connections && dstport->shared->type_info.mixdown == NULL) {
  1460. jack_error ("cannot make multiple connections to a port of type [%s]", dstport->shared->type_info.type_name);
  1461. free (connection);
  1462. return -1;
  1463. } else {
  1464. dstport->connections = g_slist_prepend (dstport->connections, connection);
  1465. srcport->connections = g_slist_prepend (srcport->connections, connection);
  1466. jack_sort_graph (engine, FALSE);
  1467. jack_send_connection_notification (engine, srcport->shared->client_id, src_id, dst_id, TRUE);
  1468. jack_send_connection_notification (engine, dstport->shared->client_id, dst_id, src_id, TRUE);
  1469. }
  1470. pthread_mutex_unlock (&engine->graph_lock);
  1471. return 0;
  1472. }
  1473. int
  1474. jack_port_disconnect_internal (jack_engine_t *engine,
  1475. jack_port_internal_t *srcport,
  1476. jack_port_internal_t *dstport,
  1477. int sort_graph)
  1478. {
  1479. GSList *node;
  1480. jack_connection_internal_t *connect;
  1481. int ret = -1;
  1482. jack_port_id_t src_id, dst_id;
  1483. /* call tree **** MUST HOLD **** engine->graph_lock. */
  1484. printf ("disconnecting %s and %s\n", srcport->shared->name, dstport->shared->name);
  1485. for (node = srcport->connections; node; node = g_slist_next (node)) {
  1486. connect = (jack_connection_internal_t *) node->data;
  1487. if (connect->source == srcport && connect->destination == dstport) {
  1488. srcport->connections = g_slist_remove (srcport->connections, connect);
  1489. dstport->connections = g_slist_remove (dstport->connections, connect);
  1490. src_id = srcport->shared->id;
  1491. dst_id = dstport->shared->id;
  1492. jack_send_connection_notification (engine, srcport->shared->client_id, src_id, dst_id, FALSE);
  1493. jack_send_connection_notification (engine, dstport->shared->client_id, dst_id, src_id, FALSE);
  1494. free (connect);
  1495. ret = 0;
  1496. break;
  1497. }
  1498. }
  1499. if (sort_graph) {
  1500. jack_sort_graph (engine, FALSE);
  1501. }
  1502. if (ret == -1) {
  1503. printf ("disconnect failed\n");
  1504. }
  1505. return ret;
  1506. }
  1507. static int
  1508. jack_port_do_disconnect (jack_engine_t *engine,
  1509. const char *source_port,
  1510. const char *destination_port)
  1511. {
  1512. jack_port_internal_t *srcport, *dstport;
  1513. int ret = -1;
  1514. if ((srcport = jack_get_port_by_name (engine, source_port)) == 0) {
  1515. jack_error ("unknown source port in attempted connection [%s]", source_port);
  1516. return -1;
  1517. }
  1518. if ((dstport = jack_get_port_by_name (engine, destination_port)) == 0) {
  1519. jack_error ("unknown destination port in attempted connection [%s]", destination_port);
  1520. return -1;
  1521. }
  1522. pthread_mutex_lock (&engine->graph_lock);
  1523. ret = jack_port_disconnect_internal (engine, srcport, dstport, TRUE);
  1524. pthread_mutex_unlock (&engine->graph_lock);
  1525. return ret;
  1526. }
  1527. static int
  1528. jack_get_fifo_fd (jack_engine_t *engine, int which_fifo)
  1529. {
  1530. char path[FIFO_NAME_SIZE+1];
  1531. struct stat statbuf;
  1532. sprintf (path, "%s-%d", engine->fifo_prefix, which_fifo);
  1533. if (stat (path, &statbuf)) {
  1534. if (errno == ENOENT) {
  1535. if (mknod (path, 0666|S_IFIFO, 0) < 0) {
  1536. jack_error ("cannot create inter-client FIFO [%s] (%s)\n", path, strerror (errno));
  1537. return -1;
  1538. }
  1539. } else {
  1540. jack_error ("cannot check on FIFO %d\n", which_fifo);
  1541. return -1;
  1542. }
  1543. } else {
  1544. if (!S_ISFIFO(statbuf.st_mode)) {
  1545. jack_error ("FIFO %d (%s) already exists, but is not a FIFO!\n", which_fifo, path);
  1546. return -1;
  1547. }
  1548. }
  1549. if (which_fifo >= engine->fifo_size) {
  1550. int i;
  1551. engine->fifo = (int *) realloc (engine->fifo, sizeof (int) * engine->fifo_size + 16);
  1552. for (i = engine->fifo_size; i < engine->fifo_size + 16; i++) {
  1553. engine->fifo[i] = -1;
  1554. }
  1555. engine->fifo_size += 16;
  1556. }
  1557. if (engine->fifo[which_fifo] < 0) {
  1558. if ((engine->fifo[which_fifo] = open (path, O_RDWR|O_CREAT, 0666)) < 0) {
  1559. jack_error ("cannot open fifo [%s] (%s)", path, strerror (errno));
  1560. return -1;
  1561. }
  1562. }
  1563. return engine->fifo[which_fifo];
  1564. }
  1565. int
  1566. jack_use_driver (jack_engine_t *engine, jack_driver_t *driver)
  1567. {
  1568. if (engine->driver) {
  1569. engine->driver->detach (engine->driver, engine);
  1570. engine->driver = 0;
  1571. }
  1572. if (driver) {
  1573. if (driver->attach (driver, engine)) {
  1574. return -1;
  1575. }
  1576. }
  1577. engine->driver = driver;
  1578. return 0;
  1579. }
  1580. /* PORT RELATED FUNCTIONS */
  1581. jack_port_id_t
  1582. jack_get_free_port (jack_engine_t *engine)
  1583. {
  1584. jack_port_id_t i;
  1585. pthread_mutex_lock (&engine->port_lock);
  1586. for (i = 0; i < engine->port_max; i++) {
  1587. if (engine->control->ports[i].in_use == 0) {
  1588. engine->control->ports[i].in_use = 1;
  1589. break;
  1590. }
  1591. }
  1592. pthread_mutex_unlock (&engine->port_lock);
  1593. if (i == engine->port_max) {
  1594. return NoPort;
  1595. }
  1596. return i;
  1597. }
  1598. static void
  1599. jack_port_release (jack_engine_t *engine, jack_port_internal_t *port)
  1600. {
  1601. /* XXX add the buffer used by the port back the (correct) freelist */
  1602. pthread_mutex_lock (&engine->port_lock);
  1603. port->shared->in_use = 0;
  1604. pthread_mutex_unlock (&engine->port_lock);
  1605. }
  1606. jack_port_internal_t *
  1607. jack_get_port_internal_by_name (jack_engine_t *engine, const char *name)
  1608. {
  1609. jack_port_id_t id;
  1610. pthread_mutex_lock (&engine->port_lock);
  1611. for (id = 0; id < engine->port_max; id++) {
  1612. if (strcmp (engine->control->ports[id].name, name) == 0) {
  1613. break;
  1614. }
  1615. }
  1616. pthread_mutex_unlock (&engine->port_lock);
  1617. if (id != engine->port_max) {
  1618. return &engine->internal_ports[id];
  1619. } else {
  1620. return NULL;
  1621. }
  1622. }
  1623. int
  1624. jack_port_do_register (jack_engine_t *engine, jack_request_t *req)
  1625. {
  1626. GSList *node;
  1627. jack_port_id_t port_id;
  1628. jack_port_shared_t *shared;
  1629. jack_port_internal_t *port;
  1630. jack_client_internal_t *client;
  1631. jack_port_type_info_t *type_info;
  1632. pthread_mutex_lock (&engine->graph_lock);
  1633. if ((client = jack_client_internal_by_id (engine, req->x.port_info.client_id)) == 0) {
  1634. jack_error ("unknown client id in port registration request");
  1635. return -1;
  1636. }
  1637. pthread_mutex_unlock (&engine->graph_lock);
  1638. if ((port_id = jack_get_free_port (engine)) == NoPort) {
  1639. jack_error ("no ports available!");
  1640. return -1;
  1641. }
  1642. shared = &engine->control->ports[port_id];
  1643. strcpy (shared->name, req->x.port_info.name);
  1644. shared->client_id = req->x.port_info.client_id;
  1645. shared->flags = req->x.port_info.flags;
  1646. shared->locked = 0;
  1647. shared->buffer_size = req->x.port_info.buffer_size;
  1648. port = &engine->internal_ports[port_id];
  1649. port->shared = shared;
  1650. port->connections = 0;
  1651. type_info = NULL;
  1652. for (node = engine->port_types; node; node = g_slist_next (node)) {
  1653. if (strcmp (req->x.port_info.type, ((jack_port_type_info_t *) node->data)->type_name) == 0) {
  1654. type_info = (jack_port_type_info_t *) node->data;
  1655. break;
  1656. }
  1657. }
  1658. if (type_info == NULL) {
  1659. /* not a builtin type, so allocate a new type_info structure,
  1660. and fill it appropriately.
  1661. */
  1662. type_info = (jack_port_type_info_t *) malloc (sizeof (jack_port_type_info_t));
  1663. type_info->type_name = strdup (req->x.port_info.type);
  1664. type_info->mixdown = NULL; /* we have no idea how to mix this */
  1665. type_info->buffer_scale_factor = -1; /* use specified port buffer size */
  1666. engine->port_types = g_slist_prepend (engine->port_types, type_info);
  1667. }
  1668. memcpy (&port->shared->type_info, type_info, sizeof (jack_port_type_info_t));
  1669. if (jack_port_assign_buffer (engine, port)) {
  1670. jack_error ("cannot assign buffer for port");
  1671. return -1;
  1672. }
  1673. pthread_mutex_lock (&engine->graph_lock);
  1674. client->ports = g_slist_prepend (client->ports, port);
  1675. jack_port_registration_notify (engine, port_id, TRUE);
  1676. pthread_mutex_unlock (&engine->graph_lock);
  1677. req->x.port_info.port_id = port_id;
  1678. return 0;
  1679. }
  1680. int
  1681. jack_port_do_unregister (jack_engine_t *engine, jack_request_t *req)
  1682. {
  1683. jack_client_internal_t *client;
  1684. jack_port_shared_t *shared;
  1685. jack_port_internal_t *port;
  1686. if (req->x.port_info.port_id < 0 || req->x.port_info.port_id > engine->port_max) {
  1687. jack_error ("invalid port ID %d in unregister request\n", req->x.port_info.port_id);
  1688. return -1;
  1689. }
  1690. shared = &engine->control->ports[req->x.port_info.port_id];
  1691. pthread_mutex_lock (&engine->graph_lock);
  1692. if ((client = jack_client_internal_by_id (engine, shared->client_id)) == NULL) {
  1693. jack_error ("unknown client id in port registration request");
  1694. return -1;
  1695. }
  1696. pthread_mutex_unlock (&engine->graph_lock);
  1697. port = &engine->internal_ports[req->x.port_info.port_id];
  1698. jack_port_release (engine, &engine->internal_ports[req->x.port_info.port_id]);
  1699. pthread_mutex_lock (&engine->graph_lock);
  1700. client->ports = g_slist_remove (client->ports, port);
  1701. jack_port_registration_notify (engine, req->x.port_info.port_id, FALSE);
  1702. pthread_mutex_unlock (&engine->graph_lock);
  1703. return 0;
  1704. }
  1705. void
  1706. jack_port_registration_notify (jack_engine_t *engine, jack_port_id_t port_id, int yn)
  1707. {
  1708. jack_event_t event;
  1709. jack_client_internal_t *client;
  1710. GSList *node;
  1711. event.type = (yn ? PortRegistered : PortUnregistered);
  1712. event.x.port_id = port_id;
  1713. for (node = engine->clients; node; node = g_slist_next (node)) {
  1714. client = (jack_client_internal_t *) node->data;
  1715. if (!client->control->active) {
  1716. continue;
  1717. }
  1718. if (client->control->port_register) {
  1719. if (jack_deliver_event (engine, client, &event)) {
  1720. jack_error ("cannot send port registration notification to %s (%s)",
  1721. client->control->name, strerror (errno));
  1722. }
  1723. }
  1724. }
  1725. }
  1726. int
  1727. jack_port_assign_buffer (jack_engine_t *engine, jack_port_internal_t *port)
  1728. {
  1729. GSList *node;
  1730. jack_port_segment_info_t *psi;
  1731. jack_port_buffer_info_t *bi;
  1732. port->shared->shm_key = -1;
  1733. if (port->shared->flags & JackPortIsInput) {
  1734. return 0;
  1735. }
  1736. pthread_mutex_lock (&engine->buffer_lock);
  1737. if (engine->port_buffer_freelist == NULL) {
  1738. jack_error ("no more buffers available!");
  1739. goto out;
  1740. }
  1741. bi = (jack_port_buffer_info_t *) engine->port_buffer_freelist->data;
  1742. for (node = engine->port_segments; node; node = g_slist_next (node)) {
  1743. psi = (jack_port_segment_info_t *) node->data;
  1744. if (bi->shm_key == psi->shm_key) {
  1745. port->shared->shm_key = psi->shm_key;
  1746. port->shared->offset = bi->offset;
  1747. break;
  1748. }
  1749. }
  1750. if (port->shared->shm_key >= 0) {
  1751. engine->port_buffer_freelist = g_slist_remove (engine->port_buffer_freelist, bi);
  1752. } else {
  1753. jack_error ("port segment info for 0x%x:%d not found!", bi->shm_key, bi->offset);
  1754. }
  1755. out:
  1756. pthread_mutex_unlock (&engine->buffer_lock);
  1757. if (port->shared->shm_key < 0) {
  1758. return -1;
  1759. } else {
  1760. return 0;
  1761. }
  1762. }
  1763. static jack_port_internal_t *
  1764. jack_get_port_by_name (jack_engine_t *engine, const char *name)
  1765. {
  1766. jack_port_id_t id;
  1767. /* Note the potential race on "in_use". Other design
  1768. elements prevent this from being a problem.
  1769. */
  1770. for (id = 0; id < engine->port_max; id++) {
  1771. if (engine->control->ports[id].in_use && strcmp (engine->control->ports[id].name, name) == 0) {
  1772. return &engine->internal_ports[id];
  1773. }
  1774. }
  1775. return NULL;
  1776. }
  1777. static int
  1778. jack_send_connection_notification (jack_engine_t *engine, jack_client_id_t client_id,
  1779. jack_port_id_t self_id, jack_port_id_t other_id, int connected)
  1780. {
  1781. jack_client_internal_t *client;
  1782. jack_event_t event;
  1783. if ((client = jack_client_internal_by_id (engine, client_id)) == 0) {
  1784. jack_error ("no such client %d during connection notification", client_id);
  1785. return -1;
  1786. }
  1787. event.type = (connected ? PortConnected : PortDisconnected);
  1788. event.x.self_id = self_id;
  1789. event.y.other_id = other_id;
  1790. if (jack_deliver_event (engine, client, &event)) {
  1791. jack_error ("cannot send port connection notification to client %s (%s)",
  1792. client->control->name, strerror (errno));
  1793. return -1;
  1794. }
  1795. return 0;
  1796. }
  1797. static void
  1798. jack_audio_port_mixdown (jack_port_t *port, nframes_t nframes)
  1799. {
  1800. GSList *node;
  1801. jack_port_t *input;
  1802. nframes_t n;
  1803. sample_t *buffer;
  1804. sample_t *dst, *src;
  1805. /* by the time we've called this, we've already established
  1806. the existence of more than 1 connection to this input port.
  1807. */
  1808. node = port->connections;
  1809. input = (jack_port_t *) node->data;
  1810. buffer = jack_port_buffer (port);
  1811. memcpy (buffer, jack_port_buffer (input), sizeof (sample_t) * nframes);
  1812. for (node = g_slist_next (node); node; node = g_slist_next (node)) {
  1813. input = (jack_port_t *) node->data;
  1814. n = nframes;
  1815. dst = buffer;
  1816. src = jack_port_buffer (input);
  1817. while (n--) {
  1818. *dst++ += *src++;
  1819. }
  1820. }
  1821. }