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.

676 lines
16KB

  1. /*
  2. Copyright (C) 2001-2003 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 <string.h>
  17. #include <stdio.h>
  18. #include <math.h>
  19. #include <config.h>
  20. #include <jack/jack.h>
  21. #include <jack/types.h>
  22. #include <jack/internal.h>
  23. #include <jack/engine.h>
  24. #include <jack/pool.h>
  25. #include <jack/port.h>
  26. #include <jack/error.h>
  27. #include <jack/jslist.h>
  28. #include "local.h"
  29. static void jack_audio_port_mixdown (jack_port_t *port, jack_nframes_t nframes);
  30. jack_port_t *
  31. jack_port_new (const jack_client_t *client, jack_port_id_t port_id, jack_control_t *control)
  32. {
  33. jack_port_t *port;
  34. jack_port_shared_t *shared;
  35. jack_port_segment_info_t *si;
  36. JSList *node;
  37. shared = &control->ports[port_id];
  38. port = (jack_port_t *) malloc (sizeof (jack_port_t));
  39. port->client_segment_base = 0;
  40. port->shared = shared;
  41. pthread_mutex_init (&port->connection_lock, NULL);
  42. port->connections = 0;
  43. port->shared->tied = NULL;
  44. /* reset function pointers to be within client address space */
  45. switch (client->control->type) {
  46. case ClientExternal:
  47. if (client->control->id == port->shared->client_id) {
  48. /* its our port, and therefore we need to make sure that the function pointers
  49. in the shared memory object that refer to functions called within the client's
  50. address space point to the location of the correct functions within
  51. the client. without this, they point to those same functions in the server's
  52. address space, and that's a recipe for disaster.
  53. */
  54. port->shared->type_info.mixdown = jack_builtin_port_types[port->shared->type_info.type_id].mixdown;
  55. port->shared->type_info.peak = jack_builtin_port_types[port->shared->type_info.type_id].peak;
  56. port->shared->type_info.power = jack_builtin_port_types[port->shared->type_info.type_id].power;
  57. }
  58. break;
  59. default:
  60. break;
  61. }
  62. /* now find the shared memory segment that contains the buffer for this port */
  63. for (node = client->port_segments; node; node = jack_slist_next (node)) {
  64. si = (jack_port_segment_info_t *) node->data;
  65. if (strcmp (si->shm_name, port->shared->type_info.shm_info.shm_name) == 0) {
  66. break;
  67. }
  68. }
  69. if (node == NULL) {
  70. jack_error ("cannot find port memory segment [%s] for new port\n", port->shared->type_info.shm_info.shm_name);
  71. return NULL;
  72. }
  73. /* set up a base address so that port->offset can be used to
  74. compute the correct location. we don't store the location
  75. directly, because port->client_segment_base and/or
  76. port->offset can change if the buffer size or port counts
  77. are changed.
  78. */
  79. port->client_segment_base = si->address;
  80. return port;
  81. }
  82. jack_port_t *
  83. jack_port_register (jack_client_t *client,
  84. const char *port_name,
  85. const char *port_type,
  86. unsigned long flags,
  87. unsigned long buffer_size)
  88. {
  89. jack_request_t req;
  90. jack_port_t *port = 0;
  91. req.type = RegisterPort;
  92. strcpy ((char *) req.x.port_info.name, (const char *) client->control->name);
  93. strcat ((char *) req.x.port_info.name, ":");
  94. strcat ((char *) req.x.port_info.name, port_name);
  95. snprintf (req.x.port_info.type, sizeof (req.x.port_info.type), "%s", port_type);
  96. req.x.port_info.flags = flags;
  97. req.x.port_info.buffer_size = buffer_size;
  98. req.x.port_info.client_id = client->control->id;
  99. if (jack_client_deliver_request (client, &req)) {
  100. return NULL;
  101. }
  102. if ((port = jack_port_new (client, req.x.port_info.port_id, client->engine)) == NULL) {
  103. return NULL;
  104. }
  105. client->ports = jack_slist_prepend (client->ports, port);
  106. return port;
  107. }
  108. int
  109. jack_port_unregister (jack_client_t *client, jack_port_t *port)
  110. {
  111. jack_request_t req;
  112. req.type = UnRegisterPort;
  113. req.x.port_info.port_id = port->shared->id;
  114. req.x.port_info.client_id = client->control->id;
  115. return jack_client_deliver_request (client, &req);
  116. }
  117. int
  118. jack_port_lock (jack_client_t *client, jack_port_t *port)
  119. {
  120. if (port) {
  121. port->shared->locked = 1;
  122. return 0;
  123. }
  124. return -1;
  125. }
  126. int
  127. jack_port_unlock (jack_client_t *client, jack_port_t *port)
  128. {
  129. if (port) {
  130. port->shared->locked = 0;
  131. return 0;
  132. }
  133. return -1;
  134. }
  135. /* LOCAL (in-client) connection querying only */
  136. int
  137. jack_port_connected (const jack_port_t *port)
  138. {
  139. return jack_slist_length (port->connections);
  140. }
  141. int
  142. jack_port_connected_to (const jack_port_t *port, const char *portname)
  143. {
  144. JSList *node;
  145. int ret = FALSE;
  146. /* XXX this really requires a cross-process lock
  147. so that ports/connections cannot go away
  148. while we are checking for them. that's hard,
  149. and has a non-trivial performance impact
  150. for jackd.
  151. */
  152. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  153. for (node = port->connections; node; node = jack_slist_next (node)) {
  154. jack_port_t *other_port = (jack_port_t *) node->data;
  155. if (strcmp (other_port->shared->name, portname) == 0) {
  156. ret = TRUE;
  157. break;
  158. }
  159. }
  160. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  161. return ret;
  162. }
  163. const char **
  164. jack_port_get_connections (const jack_port_t *port)
  165. {
  166. const char **ret = NULL;
  167. JSList *node;
  168. unsigned int n;
  169. /* XXX this really requires a cross-process lock
  170. so that ports/connections cannot go away
  171. while we are checking for them. that's hard,
  172. and has a non-trivial performance impact
  173. for jackd.
  174. */
  175. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  176. if (port->connections != NULL) {
  177. ret = (const char **) malloc (sizeof (char *) * (jack_slist_length (port->connections) + 1));
  178. for (n = 0, node = port->connections; node; node = jack_slist_next (node), ++n) {
  179. ret[n] = ((jack_port_t *) node->data)->shared->name;
  180. }
  181. ret[n] = NULL;
  182. }
  183. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  184. return ret;
  185. }
  186. /* SERVER-SIDE (all) connection querying */
  187. const char **
  188. jack_port_get_all_connections (const jack_client_t *client, const jack_port_t *port)
  189. {
  190. const char **ret;
  191. jack_request_t req;
  192. unsigned int i;
  193. if (port == NULL) {
  194. return NULL;
  195. }
  196. req.type = GetPortConnections;
  197. req.x.port_info.name[0] = '\0';
  198. req.x.port_info.type[0] = '\0';
  199. req.x.port_info.flags = 0;
  200. req.x.port_info.buffer_size = 0;
  201. req.x.port_info.client_id = 0;
  202. req.x.port_info.port_id = port->shared->id;
  203. jack_client_deliver_request (client, &req);
  204. if (req.status != 0 || req.x.port_connections.nports == 0) {
  205. return NULL;
  206. }
  207. if (client->request_fd < 0) {
  208. /* internal client */
  209. return req.x.port_connections.ports;
  210. }
  211. ret = (const char **) malloc (sizeof (char *) * (req.x.port_connections.nports + 1));
  212. for (i = 0; i < req.x.port_connections.nports; ++i ) {
  213. jack_port_id_t port_id;
  214. if (read (client->request_fd, &port_id, sizeof (port_id)) != sizeof (port_id)) {
  215. jack_error ("cannot read port id from server");
  216. return 0;
  217. }
  218. ret[i] = jack_port_by_id (client, port_id)->shared->name;
  219. }
  220. ret[i] = NULL;
  221. return ret;
  222. }
  223. jack_port_t *
  224. jack_port_by_id (const jack_client_t *client, jack_port_id_t id)
  225. {
  226. JSList *node;
  227. for (node = client->ports; node; node = jack_slist_next (node)) {
  228. if (((jack_port_t *) node->data)->shared->id == id) {
  229. return (jack_port_t *) node->data;
  230. }
  231. }
  232. if (id >= client->engine->port_max)
  233. return NULL;
  234. if (client->engine->ports[id].in_use)
  235. return jack_port_new (client, id, client->engine);
  236. return NULL;
  237. }
  238. jack_port_t *
  239. jack_port_by_name (jack_client_t *client, const char *port_name)
  240. {
  241. unsigned long i, limit;
  242. jack_port_shared_t *port;
  243. limit = client->engine->port_max;
  244. port = &client->engine->ports[0];
  245. for (i = 0; i < limit; i++) {
  246. if (port[i].in_use && strcmp (port[i].name, port_name) == 0) {
  247. return jack_port_new (client, port[i].id, client->engine);
  248. }
  249. }
  250. return NULL;
  251. }
  252. jack_nframes_t
  253. jack_port_get_latency (jack_port_t *port)
  254. {
  255. return port->shared->latency;
  256. }
  257. jack_nframes_t
  258. jack_port_get_total_latency (jack_client_t *client, jack_port_t *port)
  259. {
  260. return port->shared->total_latency;
  261. }
  262. void
  263. jack_port_set_latency (jack_port_t *port, jack_nframes_t nframes)
  264. {
  265. port->shared->latency = nframes;
  266. }
  267. void *
  268. jack_port_get_buffer (jack_port_t *port, jack_nframes_t nframes)
  269. {
  270. JSList *node, *next;
  271. /* Output port. The buffer was assigned by the engine
  272. when the port was registered.
  273. */
  274. if (port->shared->flags & JackPortIsOutput) {
  275. if (port->shared->tied) {
  276. return jack_port_get_buffer (port->shared->tied, nframes);
  277. }
  278. return jack_port_buffer (port);
  279. }
  280. /* Input port.
  281. */
  282. /* since this can only be called from the process() callback,
  283. and since no connections can be made/broken during this
  284. phase (enforced by the jack server), there is no need
  285. to take the connection lock here
  286. */
  287. if ((node = port->connections) == NULL) {
  288. /* no connections; return a zero-filled buffer */
  289. return jack_zero_filled_buffer;
  290. }
  291. if ((next = jack_slist_next (node)) == NULL) {
  292. /* one connection: use zero-copy mode - just pass
  293. the buffer of the connected (output) port.
  294. */
  295. return jack_port_get_buffer (((jack_port_t *) node->data), nframes);
  296. }
  297. /* multiple connections. use a local buffer and mixdown
  298. the incoming data to that buffer. we have already
  299. established the existence of a mixdown function
  300. during the connection process.
  301. no port can have an offset of 0 - that offset refers
  302. to the zero-filled area at the start of a shared port
  303. segment area. so, use the offset to store the location
  304. of a locally allocated buffer, and reset the client_segment_base
  305. so that the jack_port_buffer() computation works correctly.
  306. */
  307. if (port->shared->offset == 0) {
  308. port->shared->offset = (size_t) jack_pool_alloc (port->shared->type_info.buffer_scale_factor *
  309. sizeof (jack_default_audio_sample_t) * nframes);
  310. port->client_segment_base = 0;
  311. }
  312. port->shared->type_info.mixdown (port, nframes);
  313. return (jack_default_audio_sample_t *) port->shared->offset;
  314. }
  315. int
  316. jack_port_tie (jack_port_t *src, jack_port_t *dst)
  317. {
  318. if (dst->shared->client_id != src->shared->client_id) {
  319. jack_error ("cannot tie ports not owned by the same client");
  320. return -1;
  321. }
  322. if (dst->shared->flags & JackPortIsOutput) {
  323. jack_error ("cannot tie an input port");
  324. return -1;
  325. }
  326. dst->shared->tied = src;
  327. return 0;
  328. }
  329. int
  330. jack_port_untie (jack_port_t *port)
  331. {
  332. if (port->shared->tied == NULL) {
  333. jack_error ("port \"%s\" is not tied", port->shared->name);
  334. return -1;
  335. }
  336. port->shared->tied = NULL;
  337. return 0;
  338. }
  339. int
  340. jack_port_request_monitor (jack_port_t *port, int onoff)
  341. {
  342. if (onoff) {
  343. port->shared->monitor_requests++;
  344. } else if (port->shared->monitor_requests) {
  345. port->shared->monitor_requests--;
  346. }
  347. if ((port->shared->flags & JackPortIsOutput) == 0) {
  348. JSList *node;
  349. /* this port is for input, so recurse over each of the
  350. connected ports.
  351. */
  352. pthread_mutex_lock (&port->connection_lock);
  353. for (node = port->connections; node; node = jack_slist_next (node)) {
  354. /* drop the lock because if there is a feedback loop,
  355. we will deadlock. XXX much worse things will
  356. happen if there is a feedback loop !!!
  357. */
  358. pthread_mutex_unlock (&port->connection_lock);
  359. jack_port_request_monitor ((jack_port_t *) node->data, onoff);
  360. pthread_mutex_lock (&port->connection_lock);
  361. }
  362. pthread_mutex_unlock (&port->connection_lock);
  363. }
  364. return 0;
  365. }
  366. int
  367. jack_port_request_monitor_by_name (jack_client_t *client, const char *port_name, int onoff)
  368. {
  369. jack_port_t *port;
  370. unsigned long i, limit;
  371. jack_port_shared_t *ports;
  372. limit = client->engine->port_max;
  373. ports = &client->engine->ports[0];
  374. for (i = 0; i < limit; i++) {
  375. if (ports[i].in_use && strcmp (ports[i].name, port_name) == 0) {
  376. port = jack_port_new (client, ports[i].id, client->engine);
  377. return jack_port_request_monitor (port, onoff);
  378. free (port);
  379. return 0;
  380. }
  381. }
  382. return -1;
  383. }
  384. int
  385. jack_ensure_port_monitor_input (jack_port_t *port, int yn)
  386. {
  387. if (yn) {
  388. if (port->shared->monitor_requests == 0) {
  389. port->shared->monitor_requests++;
  390. }
  391. } else {
  392. if (port->shared->monitor_requests == 1) {
  393. port->shared->monitor_requests--;
  394. }
  395. }
  396. return 0;
  397. }
  398. int
  399. jack_port_monitoring_input (jack_port_t *port)
  400. {
  401. return port->shared->monitor_requests > 0;
  402. }
  403. const char *
  404. jack_port_name (const jack_port_t *port)
  405. {
  406. return port->shared->name;
  407. }
  408. const char *
  409. jack_port_short_name (const jack_port_t *port)
  410. {
  411. /* we know there is always a colon, because we put
  412. it there ...
  413. */
  414. return strchr (port->shared->name, ':') + 1;
  415. }
  416. int
  417. jack_port_is_mine (const jack_client_t *client, const jack_port_t *port)
  418. {
  419. return port->shared->client_id == client->control->id;
  420. }
  421. int
  422. jack_port_flags (const jack_port_t *port)
  423. {
  424. return port->shared->flags;
  425. }
  426. const char *
  427. jack_port_type (const jack_port_t *port)
  428. {
  429. return port->shared->type_info.type_name;
  430. }
  431. int
  432. jack_port_set_name (jack_port_t *port, const char *new_name)
  433. {
  434. char *colon;
  435. int len;
  436. colon = strchr (port->shared->name, ':');
  437. len = sizeof (port->shared->name) - ((int) (colon - port->shared->name)) - 2;
  438. snprintf (colon+1, len, "%s", new_name);
  439. return 0;
  440. }
  441. void
  442. jack_port_set_peak_function (jack_port_t *port, double (*func)(jack_port_t* port, jack_nframes_t))
  443. {
  444. port->shared->type_info.peak = func;
  445. }
  446. void
  447. jack_port_set_power_function (jack_port_t *port, double (*func)(jack_port_t* port, jack_nframes_t))
  448. {
  449. port->shared->type_info.power = func;
  450. }
  451. double
  452. jack_port_get_peak (jack_port_t* port, jack_nframes_t nframes)
  453. {
  454. if (port->shared->type_info.peak (port, nframes)) {
  455. return port->shared->type_info.peak (port, nframes);
  456. } else {
  457. return 0;
  458. }
  459. }
  460. double
  461. jack_port_get_power (jack_port_t* port, jack_nframes_t nframes)
  462. {
  463. if (port->shared->type_info.power) {
  464. return port->shared->type_info.power (port, nframes);
  465. } else {
  466. return 0;
  467. }
  468. }
  469. /* AUDIO PORT SUPPORT */
  470. static inline float f_max(float x, float a)
  471. {
  472. x -= a;
  473. x += fabs (x);
  474. x *= 0.5;
  475. x += a;
  476. return (x);
  477. }
  478. static double
  479. jack_audio_port_peak (jack_port_t *port, jack_nframes_t nframes)
  480. {
  481. jack_nframes_t n;
  482. jack_default_audio_sample_t *buf = (jack_default_audio_sample_t *) jack_port_get_buffer (port, nframes);
  483. float max = 0;
  484. for (n = 0; n < nframes; ++n) {
  485. max = f_max (buf[n], max);
  486. }
  487. return max;
  488. }
  489. static void
  490. jack_audio_port_mixdown (jack_port_t *port, jack_nframes_t nframes)
  491. {
  492. JSList *node;
  493. jack_port_t *input;
  494. jack_nframes_t n;
  495. jack_default_audio_sample_t *buffer;
  496. jack_default_audio_sample_t *dst, *src;
  497. /* by the time we've called this, we've already established
  498. the existence of more than 1 connection to this input port.
  499. */
  500. /* no need to take connection lock, since this is called
  501. from the process() callback, and the jack server
  502. ensures that no changes to connections happen
  503. during this time.
  504. */
  505. node = port->connections;
  506. input = (jack_port_t *) node->data;
  507. buffer = jack_port_buffer (port);
  508. memcpy (buffer, jack_port_buffer (input), sizeof (jack_default_audio_sample_t) * nframes);
  509. for (node = jack_slist_next (node); node; node = jack_slist_next (node)) {
  510. input = (jack_port_t *) node->data;
  511. n = nframes;
  512. dst = buffer;
  513. src = jack_port_buffer (input);
  514. while (n--) {
  515. *dst++ += *src++;
  516. }
  517. }
  518. }
  519. jack_port_type_info_t jack_builtin_port_types[] = {
  520. { .type_name = JACK_DEFAULT_AUDIO_TYPE,
  521. .mixdown = jack_audio_port_mixdown,
  522. .peak = jack_audio_port_peak,
  523. .power = NULL,
  524. .buffer_scale_factor = 1,
  525. },
  526. { .type_name = "",
  527. }
  528. };