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.

633 lines
14KB

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