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.

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