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.

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