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.

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