jack2 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.

1593 lines
49KB

  1. /*
  2. Copyright (C) 2001-2003 Paul Davis
  3. Copyright (C) 2004-2006 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "JackClient.h"
  17. #include "JackError.h"
  18. #include "JackGraphManager.h"
  19. #include "JackEngineControl.h"
  20. #include "JackClientControl.h"
  21. #include "JackGlobals.h"
  22. #include "JackTime.h"
  23. #include "JackExports.h"
  24. #include "JackAPI.h"
  25. #ifdef __APPLE__
  26. #include "JackMachThread.h"
  27. #elif WIN32
  28. #include "JackWinThread.h"
  29. #else
  30. #include "JackPosixThread.h"
  31. #endif
  32. #include <math.h>
  33. #ifdef __CLIENTDEBUG__
  34. #include "JackLibGlobals.h"
  35. #endif
  36. using namespace Jack;
  37. #ifdef __cplusplus
  38. extern "C"
  39. {
  40. #endif
  41. EXPORT int jack_client_name_size (void);
  42. EXPORT char* jack_get_client_name (jack_client_t *client);
  43. EXPORT int jack_internal_client_new (const char *client_name,
  44. const char *load_name,
  45. const char *load_init);
  46. EXPORT jack_client_t* my_jack_internal_client_new(const char* client_name);
  47. EXPORT void jack_internal_client_close (const char *client_name);
  48. EXPORT void my_jack_internal_client_close (jack_client_t* client);
  49. EXPORT int jack_is_realtime (jack_client_t *client);
  50. EXPORT void jack_on_shutdown (jack_client_t *client,
  51. void (*function)(void *arg), void *arg);
  52. EXPORT int jack_set_process_callback (jack_client_t *client,
  53. JackProcessCallback process_callback,
  54. void *arg);
  55. EXPORT int jack_set_thread_init_callback (jack_client_t *client,
  56. JackThreadInitCallback thread_init_callback,
  57. void *arg);
  58. EXPORT int jack_set_freewheel_callback (jack_client_t *client,
  59. JackFreewheelCallback freewheel_callback,
  60. void *arg);
  61. EXPORT int jack_set_freewheel(jack_client_t* client, int onoff);
  62. EXPORT int jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes);
  63. EXPORT int jack_set_buffer_size_callback (jack_client_t *client,
  64. JackBufferSizeCallback bufsize_callback,
  65. void *arg);
  66. EXPORT int jack_set_sample_rate_callback (jack_client_t *client,
  67. JackSampleRateCallback srate_callback,
  68. void *arg);
  69. EXPORT int jack_set_port_registration_callback (jack_client_t *,
  70. JackPortRegistrationCallback
  71. registration_callback, void *arg);
  72. EXPORT int jack_set_graph_order_callback (jack_client_t *,
  73. JackGraphOrderCallback graph_callback,
  74. void *);
  75. EXPORT int jack_set_xrun_callback (jack_client_t *,
  76. JackXRunCallback xrun_callback, void *arg);
  77. EXPORT int jack_activate (jack_client_t *client);
  78. EXPORT int jack_deactivate (jack_client_t *client);
  79. EXPORT jack_port_t * jack_port_register (jack_client_t *client,
  80. const char *port_name,
  81. const char *port_type,
  82. unsigned long flags,
  83. unsigned long buffer_size);
  84. EXPORT int jack_port_unregister (jack_client_t *, jack_port_t *);
  85. EXPORT void * jack_port_get_buffer (jack_port_t *, jack_nframes_t);
  86. EXPORT const char * jack_port_name (const jack_port_t *port);
  87. EXPORT const char * jack_port_short_name (const jack_port_t *port);
  88. EXPORT int jack_port_flags (const jack_port_t *port);
  89. EXPORT const char * jack_port_type (const jack_port_t *port);
  90. EXPORT int jack_port_is_mine (const jack_client_t *, const jack_port_t *port);
  91. EXPORT int jack_port_connected (const jack_port_t *port);
  92. EXPORT int jack_port_connected_to (const jack_port_t *port,
  93. const char *port_name);
  94. EXPORT const char ** jack_port_get_connections (const jack_port_t *port);
  95. EXPORT const char ** jack_port_get_all_connections (const jack_client_t *client,
  96. const jack_port_t *port);
  97. EXPORT int jack_port_tie (jack_port_t *src, jack_port_t *dst);
  98. EXPORT int jack_port_untie (jack_port_t *port);
  99. EXPORT int jack_port_lock (jack_client_t *, jack_port_t *);
  100. EXPORT int jack_port_unlock (jack_client_t *, jack_port_t *);
  101. EXPORT jack_nframes_t jack_port_get_latency (jack_port_t *port);
  102. EXPORT jack_nframes_t jack_port_get_total_latency (jack_client_t *,
  103. jack_port_t *port);
  104. EXPORT void jack_port_set_latency (jack_port_t *, jack_nframes_t);
  105. EXPORT int jack_recompute_total_latencies (jack_client_t*);
  106. EXPORT int jack_port_set_name (jack_port_t *port, const char *port_name);
  107. EXPORT int jack_port_set_alias (jack_port_t *port, const char *alias);
  108. EXPORT int jack_port_unset_alias (jack_port_t *port, const char *alias);
  109. EXPORT int jack_port_get_aliases (const jack_port_t *port, char* const aliases[2]);
  110. EXPORT int jack_port_request_monitor (jack_port_t *port, int onoff);
  111. EXPORT int jack_port_request_monitor_by_name (jack_client_t *client,
  112. const char *port_name, int onoff);
  113. EXPORT int jack_port_ensure_monitor (jack_port_t *port, int onoff);
  114. EXPORT int jack_port_monitoring_input (jack_port_t *port);
  115. EXPORT int jack_connect (jack_client_t *,
  116. const char *source_port,
  117. const char *destination_port);
  118. EXPORT int jack_disconnect (jack_client_t *,
  119. const char *source_port,
  120. const char *destination_port);
  121. EXPORT int jack_port_disconnect (jack_client_t *, jack_port_t *);
  122. EXPORT int jack_port_name_size(void);
  123. EXPORT int jack_port_type_size(void);
  124. EXPORT jack_nframes_t jack_get_sample_rate (jack_client_t *);
  125. EXPORT jack_nframes_t jack_get_buffer_size (jack_client_t *);
  126. EXPORT const char ** jack_get_ports (jack_client_t *,
  127. const char *port_name_pattern,
  128. const char *type_name_pattern,
  129. unsigned long flags);
  130. EXPORT jack_port_t * jack_port_by_name (jack_client_t *, const char *port_name);
  131. EXPORT jack_port_t * jack_port_by_id (jack_client_t *client,
  132. jack_port_id_t port_id);
  133. EXPORT int jack_engine_takeover_timebase (jack_client_t *);
  134. EXPORT jack_nframes_t jack_frames_since_cycle_start (const jack_client_t *);
  135. EXPORT jack_time_t jack_get_time(const jack_client_t *client);
  136. EXPORT jack_nframes_t jack_time_to_frames(const jack_client_t *client, jack_time_t time);
  137. EXPORT jack_time_t jack_frames_to_time(const jack_client_t *client, jack_nframes_t frames);
  138. EXPORT jack_nframes_t jack_frame_time (const jack_client_t *);
  139. EXPORT jack_nframes_t jack_last_frame_time (const jack_client_t *client);
  140. EXPORT float jack_cpu_load (jack_client_t *client);
  141. EXPORT pthread_t jack_client_thread_id (jack_client_t *);
  142. EXPORT void jack_set_error_function (void (*func)(const char *));
  143. EXPORT float jack_get_max_delayed_usecs (jack_client_t *client);
  144. EXPORT float jack_get_xrun_delayed_usecs (jack_client_t *client);
  145. EXPORT void jack_reset_max_delayed_usecs (jack_client_t *client);
  146. EXPORT int jack_release_timebase (jack_client_t *client);
  147. EXPORT int jack_set_sync_callback (jack_client_t *client,
  148. JackSyncCallback sync_callback,
  149. void *arg);
  150. EXPORT int jack_set_sync_timeout (jack_client_t *client,
  151. jack_time_t timeout);
  152. EXPORT int jack_set_timebase_callback (jack_client_t *client,
  153. int conditional,
  154. JackTimebaseCallback timebase_callback,
  155. void *arg);
  156. EXPORT int jack_transport_locate (jack_client_t *client,
  157. jack_nframes_t frame);
  158. EXPORT jack_transport_state_t jack_transport_query (const jack_client_t *client,
  159. jack_position_t *pos);
  160. EXPORT jack_nframes_t jack_get_current_transport_frame (const jack_client_t *client);
  161. EXPORT int jack_transport_reposition (jack_client_t *client,
  162. jack_position_t *pos);
  163. EXPORT void jack_transport_start (jack_client_t *client);
  164. EXPORT void jack_transport_stop (jack_client_t *client);
  165. EXPORT void jack_get_transport_info (jack_client_t *client,
  166. jack_transport_info_t *tinfo);
  167. EXPORT void jack_set_transport_info (jack_client_t *client,
  168. jack_transport_info_t *tinfo);
  169. EXPORT int jack_acquire_real_time_scheduling (pthread_t thread, int priority);
  170. EXPORT int jack_client_create_thread (jack_client_t* client,
  171. pthread_t *thread,
  172. int priority,
  173. int realtime, // boolean
  174. void *(*start_routine)(void*),
  175. void *arg);
  176. EXPORT int jack_drop_real_time_scheduling (pthread_t thread);
  177. EXPORT char * jack_get_internal_client_name (jack_client_t *client,
  178. jack_intclient_t intclient);
  179. EXPORT jack_intclient_t jack_internal_client_handle (jack_client_t *client,
  180. const char *client_name,
  181. jack_status_t *status);
  182. EXPORT jack_intclient_t jack_internal_client_load (jack_client_t *client,
  183. const char *client_name,
  184. jack_options_t options,
  185. jack_status_t *status, ...);
  186. EXPORT jack_status_t jack_internal_client_unload (jack_client_t *client,
  187. jack_intclient_t intclient);
  188. #ifdef __cplusplus
  189. }
  190. #endif
  191. #ifdef WIN32
  192. /* missing on Windows : see http://bugs.mysql.com/bug.php?id=15936 */
  193. inline double rint(double nr)
  194. {
  195. double f = floor(nr);
  196. double c = ceil(nr);
  197. return (((c -nr) >= (nr - f)) ? f : c);
  198. }
  199. #endif
  200. static inline bool CheckPort(jack_port_id_t port_index)
  201. {
  202. return (port_index < PORT_NUM);
  203. }
  204. static inline bool CheckBufferSize(jack_nframes_t buffer_size)
  205. {
  206. return (buffer_size <= BUFFER_SIZE_MAX);
  207. }
  208. static inline void WaitGraphChange()
  209. {
  210. if (GetGraphManager()->IsPendingChange()) {
  211. JackLog("WaitGraphChange...\n");
  212. JackSleep(GetEngineControl()->fPeriodUsecs * 2);
  213. }
  214. }
  215. static void default_jack_error_callback(const char *desc)
  216. {
  217. fprintf(stderr, "%s\n", desc);
  218. }
  219. void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  220. EXPORT void jack_set_error_function (void (*func)(const char *))
  221. {
  222. jack_error_callback = func;
  223. }
  224. EXPORT void* jack_port_get_buffer(jack_port_t* port, jack_nframes_t frames)
  225. {
  226. #ifdef __CLIENTDEBUG__
  227. JackLibGlobals::CheckContext();
  228. #endif
  229. jack_port_id_t myport = (jack_port_id_t)port;
  230. if (!CheckPort(myport)) {
  231. jack_error("jack_port_get_buffer called with an incorrect port %ld", myport);
  232. return NULL;
  233. } else {
  234. return GetGraphManager()->GetBuffer(myport, frames);
  235. }
  236. }
  237. EXPORT const char* jack_port_name(const jack_port_t* port)
  238. {
  239. #ifdef __CLIENTDEBUG__
  240. JackLibGlobals::CheckContext();
  241. #endif
  242. jack_port_id_t myport = (jack_port_id_t)port;
  243. if (!CheckPort(myport)) {
  244. jack_error("jack_port_name called with an incorrect port %ld", myport);
  245. return NULL;
  246. } else {
  247. return GetGraphManager()->GetPort(myport)->GetName();
  248. }
  249. }
  250. EXPORT const char* jack_port_short_name(const jack_port_t* port)
  251. {
  252. #ifdef __CLIENTDEBUG__
  253. JackLibGlobals::CheckContext();
  254. #endif
  255. jack_port_id_t myport = (jack_port_id_t)port;
  256. if (!CheckPort(myport)) {
  257. jack_error("jack_port_short_name called with an incorrect port %ld", myport);
  258. return NULL;
  259. } else {
  260. return GetGraphManager()->GetPort(myport)->GetShortName();
  261. }
  262. }
  263. EXPORT int jack_port_flags(const jack_port_t* port)
  264. {
  265. #ifdef __CLIENTDEBUG__
  266. JackLibGlobals::CheckContext();
  267. #endif
  268. jack_port_id_t myport = (jack_port_id_t)port;
  269. if (!CheckPort(myport)) {
  270. jack_error("jack_port_flags called with an incorrect port %ld", myport);
  271. return -1;
  272. } else {
  273. return GetGraphManager()->GetPort(myport)->Flags();
  274. }
  275. }
  276. EXPORT const char* jack_port_type(const jack_port_t* port)
  277. {
  278. #ifdef __CLIENTDEBUG__
  279. JackLibGlobals::CheckContext();
  280. #endif
  281. jack_port_id_t myport = (jack_port_id_t)port;
  282. if (!CheckPort(myport)) {
  283. jack_error("jack_port_flags called an incorrect port %ld", myport);
  284. return NULL;
  285. } else {
  286. return GetGraphManager()->GetPort(myport)->Type();
  287. }
  288. }
  289. EXPORT int jack_port_connected(const jack_port_t* port)
  290. {
  291. #ifdef __CLIENTDEBUG__
  292. JackLibGlobals::CheckContext();
  293. #endif
  294. jack_port_id_t myport = (jack_port_id_t)port;
  295. if (!CheckPort(myport)) {
  296. jack_error("jack_port_connected called with an incorrect port %ld", myport);
  297. return -1;
  298. } else {
  299. WaitGraphChange();
  300. return GetGraphManager()->GetConnectionsNum(myport);
  301. }
  302. }
  303. EXPORT int jack_port_connected_to(const jack_port_t* port, const char* portname)
  304. {
  305. #ifdef __CLIENTDEBUG__
  306. JackLibGlobals::CheckContext();
  307. #endif
  308. jack_port_id_t myport = (jack_port_id_t)port;
  309. if (!CheckPort(myport)) {
  310. jack_error("jack_port_connected_to called with an incorrect port %ld", myport);
  311. return -1;
  312. } else if (portname == NULL) {
  313. jack_error("jack_port_connected_to called with a NULL port name");
  314. return -1;
  315. } else {
  316. WaitGraphChange();
  317. return GetGraphManager()->ConnectedTo(myport, portname);
  318. }
  319. }
  320. EXPORT int jack_port_tie(jack_port_t* src, jack_port_t* dst)
  321. {
  322. #ifdef __CLIENTDEBUG__
  323. JackLibGlobals::CheckContext();
  324. #endif
  325. jack_port_id_t mysrc = (jack_port_id_t)src;
  326. if (!CheckPort(mysrc)) {
  327. jack_error("jack_port_tie called with a NULL src port");
  328. return -1;
  329. }
  330. jack_port_id_t mydst = (jack_port_id_t)dst;
  331. if (!CheckPort(mydst)) {
  332. jack_error("jack_port_tie called with a NULL dst port");
  333. return -1;
  334. }
  335. if (GetGraphManager()->GetPort(mysrc)->GetRefNum() != GetGraphManager()->GetPort(mydst)->GetRefNum()) {
  336. jack_error("jack_port_tie called with ports not belonging to the same client");
  337. return -1;
  338. }
  339. return GetGraphManager()->GetPort(mydst)->Tie(mysrc);
  340. }
  341. EXPORT int jack_port_untie(jack_port_t* port)
  342. {
  343. #ifdef __CLIENTDEBUG__
  344. JackLibGlobals::CheckContext();
  345. #endif
  346. jack_port_id_t myport = (jack_port_id_t)port;
  347. if (!CheckPort(myport)) {
  348. jack_error("jack_port_untie called with an incorrect port %ld", myport);
  349. return -1;
  350. } else {
  351. return GetGraphManager()->GetPort(myport)->UnTie();
  352. }
  353. }
  354. EXPORT jack_nframes_t jack_port_get_latency(jack_port_t* port)
  355. {
  356. #ifdef __CLIENTDEBUG__
  357. JackLibGlobals::CheckContext();
  358. #endif
  359. jack_port_id_t myport = (jack_port_id_t)port;
  360. if (!CheckPort(myport)) {
  361. jack_error("jack_port_get_latency called with an incorrect port %ld", myport);
  362. return 0;
  363. } else {
  364. WaitGraphChange();
  365. return GetGraphManager()->GetPort(myport)->GetLatency();
  366. }
  367. }
  368. EXPORT void jack_port_set_latency(jack_port_t* port, jack_nframes_t frames)
  369. {
  370. #ifdef __CLIENTDEBUG__
  371. JackLibGlobals::CheckContext();
  372. #endif
  373. jack_port_id_t myport = (jack_port_id_t)port;
  374. if (!CheckPort(myport)) {
  375. jack_error("jack_port_set_latency called with an incorrect port %ld", myport);
  376. } else {
  377. GetGraphManager()->GetPort(myport)->SetLatency(frames);
  378. }
  379. }
  380. EXPORT int jack_recompute_total_latencies(jack_client_t* ext_client)
  381. {
  382. // The latency computation is done each time jack_port_get_total_latency is called
  383. return 0;
  384. }
  385. EXPORT int jack_port_set_name(jack_port_t* port, const char* name)
  386. {
  387. #ifdef __CLIENTDEBUG__
  388. JackLibGlobals::CheckContext();
  389. #endif
  390. jack_port_id_t myport = (jack_port_id_t)port;
  391. if (!CheckPort(myport)) {
  392. jack_error("jack_port_set_name called with an incorrect port %ld", myport);
  393. return -1;
  394. } else if (name == NULL) {
  395. jack_error("jack_port_set_name called with a NULL port name");
  396. return -1;
  397. } else {
  398. return GetGraphManager()->GetPort(myport)->SetName(name);
  399. }
  400. }
  401. EXPORT int jack_port_set_alias(jack_port_t* port, const char* name)
  402. {
  403. #ifdef __CLIENTDEBUG__
  404. JackLibGlobals::CheckContext();
  405. #endif
  406. jack_port_id_t myport = (jack_port_id_t)port;
  407. if (!CheckPort(myport)) {
  408. jack_error("jack_port_set_alias called with an incorrect port %ld", myport);
  409. return -1;
  410. } else if (name == NULL) {
  411. jack_error("jack_port_set_alias called with a NULL port name");
  412. return -1;
  413. } else {
  414. return GetGraphManager()->GetPort(myport)->SetAlias(name);
  415. }
  416. }
  417. EXPORT int jack_port_unset_alias(jack_port_t* port, const char* name)
  418. {
  419. #ifdef __CLIENTDEBUG__
  420. JackLibGlobals::CheckContext();
  421. #endif
  422. jack_port_id_t myport = (jack_port_id_t)port;
  423. if (!CheckPort(myport)) {
  424. jack_error("jack_port_unset_alias called with an incorrect port %ld", myport);
  425. return -1;
  426. } else if (name == NULL) {
  427. jack_error("jack_port_unset_alias called with a NULL port name");
  428. return -1;
  429. } else {
  430. return GetGraphManager()->GetPort(myport)->UnsetAlias(name);
  431. }
  432. }
  433. EXPORT int jack_port_get_aliases(const jack_port_t* port, char* const aliases[2])
  434. {
  435. #ifdef __CLIENTDEBUG__
  436. JackLibGlobals::CheckContext();
  437. #endif
  438. jack_port_id_t myport = (jack_port_id_t)port;
  439. if (!CheckPort(myport)) {
  440. jack_error("jack_port_get_aliases called with an incorrect port %ld", myport);
  441. return -1;
  442. } else {
  443. return GetGraphManager()->GetPort(myport)->GetAliases(aliases);
  444. }
  445. }
  446. EXPORT int jack_port_request_monitor(jack_port_t* port, int onoff)
  447. {
  448. #ifdef __CLIENTDEBUG__
  449. JackLibGlobals::CheckContext();
  450. #endif
  451. jack_port_id_t myport = (jack_port_id_t)port;
  452. if (!CheckPort(myport)) {
  453. jack_error("jack_port_request_monitor called with an incorrect port %ld", myport);
  454. return -1;
  455. } else {
  456. return GetGraphManager()->RequestMonitor(myport, onoff);
  457. }
  458. }
  459. EXPORT int jack_port_request_monitor_by_name(jack_client_t* ext_client, const char* port_name, int onoff)
  460. {
  461. #ifdef __CLIENTDEBUG__
  462. JackLibGlobals::CheckContext();
  463. #endif
  464. JackClient* client = (JackClient*)ext_client;
  465. if (client == NULL) {
  466. jack_error("jack_port_request_monitor_by_name called with a NULL client");
  467. return -1;
  468. } else {
  469. jack_port_id_t myport = GetGraphManager()->GetPort(port_name);
  470. if (!CheckPort(myport)) {
  471. jack_error("jack_port_request_monitor_by_name called with an incorrect port %s", port_name);
  472. return -1;
  473. } else {
  474. return GetGraphManager()->RequestMonitor(myport, onoff);
  475. }
  476. }
  477. }
  478. EXPORT int jack_port_ensure_monitor(jack_port_t* port, int onoff)
  479. {
  480. #ifdef __CLIENTDEBUG__
  481. JackLibGlobals::CheckContext();
  482. #endif
  483. jack_port_id_t myport = (jack_port_id_t)port;
  484. if (!CheckPort(myport)) {
  485. jack_error("jack_port_ensure_monitor called with an incorrect port %ld", myport);
  486. return -1;
  487. } else {
  488. return GetGraphManager()->GetPort(myport)->EnsureMonitor(onoff);
  489. }
  490. }
  491. EXPORT int jack_port_monitoring_input(jack_port_t* port)
  492. {
  493. #ifdef __CLIENTDEBUG__
  494. JackLibGlobals::CheckContext();
  495. #endif
  496. jack_port_id_t myport = (jack_port_id_t)port;
  497. if (!CheckPort(myport)) {
  498. jack_error("jack_port_monitoring_input called with an incorrect port %ld", myport);
  499. return -1;
  500. } else {
  501. return GetGraphManager()->GetPort(myport)->MonitoringInput();
  502. }
  503. }
  504. EXPORT int jack_is_realtime(jack_client_t* ext_client)
  505. {
  506. #ifdef __CLIENTDEBUG__
  507. JackLibGlobals::CheckContext();
  508. #endif
  509. JackClient* client = (JackClient*)ext_client;
  510. if (client == NULL) {
  511. jack_error("jack_is_realtime called with a NULL client");
  512. return -1;
  513. } else {
  514. return GetEngineControl()->fRealTime;
  515. }
  516. }
  517. EXPORT void jack_on_shutdown(jack_client_t* ext_client, void (*function)(void* arg), void* arg)
  518. {
  519. #ifdef __CLIENTDEBUG__
  520. JackLibGlobals::CheckContext();
  521. #endif
  522. JackClient* client = (JackClient*)ext_client;
  523. if (client == NULL) {
  524. jack_error("jack_on_shutdown called with a NULL client");
  525. } else {
  526. client->OnShutdown(function, arg);
  527. }
  528. }
  529. EXPORT int jack_set_process_callback(jack_client_t* ext_client, JackProcessCallback callback, void* arg)
  530. {
  531. #ifdef __CLIENTDEBUG__
  532. JackLibGlobals::CheckContext();
  533. #endif
  534. JackClient* client = (JackClient*)ext_client;
  535. if (client == NULL) {
  536. jack_error("jack_set_process_callback called with a NULL client");
  537. return -1;
  538. } else {
  539. return client->SetProcessCallback(callback, arg);
  540. }
  541. }
  542. EXPORT int jack_set_freewheel_callback(jack_client_t* ext_client, JackFreewheelCallback freewheel_callback, void* arg)
  543. {
  544. #ifdef __CLIENTDEBUG__
  545. JackLibGlobals::CheckContext();
  546. #endif
  547. JackClient* client = (JackClient*)ext_client;
  548. if (client == NULL) {
  549. jack_error("jack_set_freewheel_callback called with a NULL client");
  550. return -1;
  551. } else {
  552. return client->SetFreewheelCallback(freewheel_callback, arg);
  553. }
  554. }
  555. EXPORT int jack_set_freewheel(jack_client_t* ext_client, int onoff)
  556. {
  557. #ifdef __CLIENTDEBUG__
  558. JackLibGlobals::CheckContext();
  559. #endif
  560. JackClient* client = (JackClient*)ext_client;
  561. if (client == NULL) {
  562. jack_error("jack_set_freewheel called with a NULL client");
  563. return -1;
  564. } else {
  565. return client->SetFreeWheel(onoff);
  566. }
  567. }
  568. EXPORT int jack_set_buffer_size(jack_client_t* ext_client, jack_nframes_t buffer_size)
  569. {
  570. #ifdef __CLIENTDEBUG__
  571. JackLibGlobals::CheckContext();
  572. #endif
  573. JackClient* client = (JackClient*)ext_client;
  574. if (client == NULL) {
  575. jack_error("jack_set_buffer_size called with a NULL client");
  576. return -1;
  577. } else if (!CheckBufferSize(buffer_size)) {
  578. return -1;
  579. } else {
  580. return client->SetBufferSize(buffer_size);
  581. }
  582. }
  583. EXPORT int jack_set_buffer_size_callback(jack_client_t* ext_client, JackBufferSizeCallback bufsize_callback, void* arg)
  584. {
  585. #ifdef __CLIENTDEBUG__
  586. JackLibGlobals::CheckContext();
  587. #endif
  588. JackClient* client = (JackClient*)ext_client;
  589. if (client == NULL) {
  590. jack_error("jack_set_buffer_size_callback called with a NULL client");
  591. return -1;
  592. } else {
  593. return client->SetBufferSizeCallback(bufsize_callback, arg);
  594. }
  595. }
  596. EXPORT int jack_set_sample_rate_callback(jack_client_t* ext_client, JackSampleRateCallback srate_callback, void* arg)
  597. {
  598. #ifdef __CLIENTDEBUG__
  599. JackLibGlobals::CheckContext();
  600. #endif
  601. JackClient* client = (JackClient*)ext_client;
  602. JackLog("jack_set_sample_rate_callback ext_client %x client %x \n", ext_client, client);
  603. if (client == NULL) {
  604. jack_error("jack_set_sample_rate_callback called with a NULL client");
  605. return -1;
  606. } else {
  607. JackLog("jack_set_sample_rate_callback: deprecated\n");
  608. return -1;
  609. }
  610. }
  611. EXPORT int jack_set_port_registration_callback(jack_client_t* ext_client, JackPortRegistrationCallback registration_callback, void* arg)
  612. {
  613. #ifdef __CLIENTDEBUG__
  614. JackLibGlobals::CheckContext();
  615. #endif
  616. JackClient* client = (JackClient*)ext_client;
  617. if (client == NULL) {
  618. jack_error("jack_set_port_registration_callback called with a NULL client");
  619. return -1;
  620. } else {
  621. return client->SetPortRegistrationCallback(registration_callback, arg);
  622. }
  623. }
  624. EXPORT int jack_set_graph_order_callback(jack_client_t* ext_client, JackGraphOrderCallback graph_callback, void* arg)
  625. {
  626. #ifdef __CLIENTDEBUG__
  627. JackLibGlobals::CheckContext();
  628. #endif
  629. JackClient* client = (JackClient*)ext_client;
  630. JackLog("jack_set_graph_order_callback ext_client %x client %x \n", ext_client, client);
  631. if (client == NULL) {
  632. jack_error("jack_set_graph_order_callback called with a NULL client");
  633. return -1;
  634. } else {
  635. return client->SetGraphOrderCallback(graph_callback, arg);
  636. }
  637. }
  638. EXPORT int jack_set_xrun_callback(jack_client_t* ext_client, JackXRunCallback xrun_callback, void* arg)
  639. {
  640. #ifdef __CLIENTDEBUG__
  641. JackLibGlobals::CheckContext();
  642. #endif
  643. JackClient* client = (JackClient*)ext_client;
  644. if (client == NULL) {
  645. jack_error("jack_set_xrun_callback called with a NULL client");
  646. return -1;
  647. } else {
  648. return client->SetXRunCallback(xrun_callback, arg);
  649. }
  650. }
  651. EXPORT int jack_set_thread_init_callback(jack_client_t* ext_client, JackThreadInitCallback init_callback, void *arg)
  652. {
  653. #ifdef __CLIENTDEBUG__
  654. JackLibGlobals::CheckContext();
  655. #endif
  656. JackClient* client = (JackClient*)ext_client;
  657. JackLog("jack_set_thread_init_callback ext_client %x client %x \n", ext_client, client);
  658. if (client == NULL) {
  659. jack_error("jack_set_thread_init_callback called with a NULL client");
  660. return -1;
  661. } else {
  662. return client->SetInitCallback(init_callback, arg);
  663. }
  664. }
  665. EXPORT int jack_activate(jack_client_t* ext_client)
  666. {
  667. #ifdef __CLIENTDEBUG__
  668. JackLibGlobals::CheckContext();
  669. #endif
  670. JackClient* client = (JackClient*)ext_client;
  671. if (client == NULL) {
  672. jack_error("jack_activate called with a NULL client");
  673. return -1;
  674. } else {
  675. return client->Activate();
  676. }
  677. }
  678. EXPORT int jack_deactivate(jack_client_t* ext_client)
  679. {
  680. #ifdef __CLIENTDEBUG__
  681. JackLibGlobals::CheckContext();
  682. #endif
  683. JackClient* client = (JackClient*)ext_client;
  684. if (client == NULL) {
  685. jack_error("jack_deactivate called with a NULL client");
  686. return -1;
  687. } else {
  688. return client->Deactivate();
  689. }
  690. }
  691. EXPORT jack_port_t* jack_port_register(jack_client_t* ext_client, const char* port_name, const char* port_type, unsigned long flags, unsigned long buffer_size)
  692. {
  693. #ifdef __CLIENTDEBUG__
  694. JackLibGlobals::CheckContext();
  695. #endif
  696. JackClient* client = (JackClient*)ext_client;
  697. if (client == NULL) {
  698. jack_error("jack_port_register called with a NULL client");
  699. return NULL;
  700. } else if ((port_name == NULL) || (port_type == NULL)) {
  701. jack_error("jack_port_register called with a NULL port name or a NULL port_type");
  702. return NULL;
  703. } else {
  704. return (jack_port_t *)client->PortRegister(port_name, port_type, flags, buffer_size);
  705. }
  706. }
  707. EXPORT int jack_port_unregister(jack_client_t* ext_client, jack_port_t* port)
  708. {
  709. #ifdef __CLIENTDEBUG__
  710. JackLibGlobals::CheckContext();
  711. #endif
  712. JackClient* client = (JackClient*)ext_client;
  713. if (client == NULL) {
  714. jack_error("jack_port_unregister called with a NULL client");
  715. return -1;
  716. }
  717. jack_port_id_t myport = (jack_port_id_t)port;
  718. if (!CheckPort(myport)) {
  719. jack_error("jack_port_unregister called with an incorrect port %ld", myport);
  720. return -1;
  721. }
  722. return client->PortUnRegister(myport);
  723. }
  724. EXPORT int jack_port_is_mine(const jack_client_t* ext_client, const jack_port_t* port)
  725. {
  726. #ifdef __CLIENTDEBUG__
  727. JackLibGlobals::CheckContext();
  728. #endif
  729. JackClient* client = (JackClient*)ext_client;
  730. if (client == NULL) {
  731. jack_error("jack_port_is_mine called with a NULL client");
  732. return -1;
  733. }
  734. jack_port_id_t myport = (jack_port_id_t)port;
  735. if (!CheckPort(myport)) {
  736. jack_error("jack_port_is_mine called with an incorrect port %ld", myport);
  737. return -1;
  738. }
  739. return client->PortIsMine(myport);
  740. }
  741. EXPORT const char** jack_port_get_connections(const jack_port_t* port)
  742. {
  743. #ifdef __CLIENTDEBUG__
  744. JackLibGlobals::CheckContext();
  745. #endif
  746. jack_port_id_t myport = (jack_port_id_t)port;
  747. if (!CheckPort(myport)) {
  748. jack_error("jack_port_get_connections called with an incorrect port %ld", myport);
  749. return NULL;
  750. } else {
  751. WaitGraphChange();
  752. return GetGraphManager()->GetConnections(myport);
  753. }
  754. }
  755. // Calling client does not need to "own" the port
  756. EXPORT const char** jack_port_get_all_connections(const jack_client_t* ext_client, const jack_port_t* port)
  757. {
  758. #ifdef __CLIENTDEBUG__
  759. JackLibGlobals::CheckContext();
  760. #endif
  761. JackClient* client = (JackClient*)ext_client;
  762. if (client == NULL) {
  763. jack_error("jack_port_get_all_connections called with a NULL client");
  764. return NULL;
  765. }
  766. jack_port_id_t myport = (jack_port_id_t)port;
  767. if (!CheckPort(myport)) {
  768. jack_error("jack_port_get_all_connections called with an incorrect port %ld", myport);
  769. return NULL;
  770. } else {
  771. WaitGraphChange();
  772. return GetGraphManager()->GetConnections(myport);
  773. }
  774. }
  775. // Does not use the client parameter
  776. EXPORT int jack_port_lock(jack_client_t* ext_client, jack_port_t* port)
  777. {
  778. #ifdef __CLIENTDEBUG__
  779. JackLibGlobals::CheckContext();
  780. #endif
  781. JackClient* client = (JackClient*)ext_client;
  782. if (client == NULL) {
  783. jack_error("jack_port_lock called with a NULL client");
  784. return -1;
  785. }
  786. jack_port_id_t myport = (jack_port_id_t)port;
  787. if (!CheckPort(myport)) {
  788. jack_error("jack_port_lock called with an incorrect port %ld", myport);
  789. return -1;
  790. } else {
  791. return (myport && client->PortIsMine(myport)) ? GetGraphManager()->GetPort(myport)->Lock() : -1;
  792. }
  793. }
  794. // Does not use the client parameter
  795. EXPORT int jack_port_unlock(jack_client_t* ext_client, jack_port_t* port)
  796. {
  797. #ifdef __CLIENTDEBUG__
  798. JackLibGlobals::CheckContext();
  799. #endif
  800. JackClient* client = (JackClient*)ext_client;
  801. if (client == NULL) {
  802. jack_error("jack_port_unlock called with a NULL client");
  803. return -1;
  804. }
  805. jack_port_id_t myport = (jack_port_id_t)port;
  806. if (!CheckPort(myport)) {
  807. jack_error("jack_port_unlock called with an incorrect port %ld", myport);
  808. return -1;
  809. } else {
  810. return (myport && client->PortIsMine(myport)) ? GetGraphManager()->GetPort(myport)->Unlock() : -1;
  811. }
  812. }
  813. EXPORT jack_nframes_t jack_port_get_total_latency(jack_client_t* ext_client, jack_port_t* port)
  814. {
  815. #ifdef __CLIENTDEBUG__
  816. JackLibGlobals::CheckContext();
  817. #endif
  818. JackClient* client = (JackClient*)ext_client;
  819. if (client == NULL) {
  820. jack_error("jack_port_get_total_latency called with a NULL client");
  821. return 0;
  822. }
  823. jack_port_id_t myport = (jack_port_id_t)port;
  824. if (!CheckPort(myport)) {
  825. jack_error("jack_port_get_total_latency called with an incorrect port %ld", myport);
  826. return 0;
  827. } else {
  828. // The latency computation is done each time
  829. WaitGraphChange();
  830. return GetGraphManager()->GetTotalLatency(myport);
  831. }
  832. }
  833. EXPORT int jack_connect(jack_client_t* ext_client, const char* src, const char* dst)
  834. {
  835. #ifdef __CLIENTDEBUG__
  836. JackLibGlobals::CheckContext();
  837. #endif
  838. JackClient* client = (JackClient*)ext_client;
  839. if (client == NULL) {
  840. jack_error("jack_connect called with a NULL client");
  841. return -1;
  842. } else if ((src == NULL) || (dst == NULL)) {
  843. jack_error("jack_connect called with a NULL port name");
  844. return -1;
  845. } else {
  846. return client->PortConnect(src, dst);
  847. }
  848. }
  849. EXPORT int jack_disconnect(jack_client_t* ext_client, const char* src, const char* dst)
  850. {
  851. #ifdef __CLIENTDEBUG__
  852. JackLibGlobals::CheckContext();
  853. #endif
  854. JackClient* client = (JackClient*)ext_client;
  855. if (client == NULL) {
  856. jack_error("jack_disconnect called with a NULL client");
  857. return -1;
  858. } else if ((src == NULL) || (dst == NULL)) {
  859. jack_error("jack_connect called with a NULL port name");
  860. return -1;
  861. } else {
  862. return client->PortDisconnect(src, dst);
  863. }
  864. }
  865. EXPORT int jack_port_connect(jack_client_t* ext_client, jack_port_t* src, jack_port_t* dst)
  866. {
  867. #ifdef __CLIENTDEBUG__
  868. JackLibGlobals::CheckContext();
  869. #endif
  870. JackClient* client = (JackClient*)ext_client;
  871. if (client == NULL) {
  872. jack_error("jack_port_connect called with a NULL client");
  873. return -1;
  874. }
  875. jack_port_id_t mysrc = (jack_port_id_t)src;
  876. if (!CheckPort(mysrc)) {
  877. jack_error("jack_port_connect called with a NULL src port");
  878. return -1;
  879. }
  880. jack_port_id_t mydst = (jack_port_id_t)dst;
  881. if (!CheckPort(mydst)) {
  882. jack_error("jack_port_connect called with a NULL dst port");
  883. return -1;
  884. }
  885. return client->PortConnect(mysrc, mydst);
  886. }
  887. EXPORT int jack_port_disconnect(jack_client_t* ext_client, jack_port_t* src)
  888. {
  889. #ifdef __CLIENTDEBUG__
  890. JackLibGlobals::CheckContext();
  891. #endif
  892. JackClient* client = (JackClient*)ext_client;
  893. if (client == NULL) {
  894. jack_error("jack_port_disconnect called with a NULL client");
  895. return -1;
  896. }
  897. jack_port_id_t myport = (jack_port_id_t)src;
  898. if (!CheckPort(myport)) {
  899. jack_error("jack_port_disconnect called with an incorrect port %ld", myport);
  900. return -1;
  901. }
  902. return client->PortDisconnect(myport);
  903. }
  904. EXPORT jack_nframes_t jack_get_sample_rate(jack_client_t* ext_client)
  905. {
  906. #ifdef __CLIENTDEBUG__
  907. JackLibGlobals::CheckContext();
  908. #endif
  909. JackClient* client = (JackClient*)ext_client;
  910. if (client == NULL) {
  911. jack_error("jack_get_sample_rate called with a NULL client");
  912. return 0;
  913. } else {
  914. return GetEngineControl()->fSampleRate;
  915. }
  916. }
  917. EXPORT jack_nframes_t jack_get_buffer_size(jack_client_t* ext_client)
  918. {
  919. #ifdef __CLIENTDEBUG__
  920. JackLibGlobals::CheckContext();
  921. #endif
  922. JackClient* client = (JackClient*)ext_client;
  923. if (client == NULL) {
  924. jack_error("jack_get_buffer_size called with a NULL client");
  925. return 0;
  926. } else {
  927. return GetEngineControl()->fBufferSize;
  928. }
  929. }
  930. EXPORT const char** jack_get_ports(jack_client_t* ext_client, const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  931. {
  932. #ifdef __CLIENTDEBUG__
  933. JackLibGlobals::CheckContext();
  934. #endif
  935. JackClient* client = (JackClient*)ext_client;
  936. if (client == NULL) {
  937. jack_error("jack_get_ports called with a NULL client");
  938. return NULL;
  939. }
  940. return GetGraphManager()->GetPorts(port_name_pattern, type_name_pattern, flags);
  941. }
  942. EXPORT jack_port_t* jack_port_by_name(jack_client_t* ext_client, const char* portname)
  943. {
  944. #ifdef __CLIENTDEBUG__
  945. JackLibGlobals::CheckContext();
  946. #endif
  947. JackClient* client = (JackClient*)ext_client;
  948. if (client == NULL) {
  949. jack_error("jack_get_ports called with a NULL client");
  950. return 0;
  951. }
  952. if (portname == NULL) {
  953. jack_error("jack_port_by_name called with a NULL port name");
  954. return NULL;
  955. } else {
  956. int res = GetGraphManager()->GetPort(portname); // returns a port index at least > 1
  957. return (res == NO_PORT) ? NULL : (jack_port_t*)res;
  958. }
  959. }
  960. EXPORT jack_port_t* jack_port_by_id(const jack_client_t* ext_client, jack_port_id_t id)
  961. {
  962. #ifdef __CLIENTDEBUG__
  963. JackLibGlobals::CheckContext();
  964. #endif
  965. /* jack_port_t* type is actually the port index */
  966. return (jack_port_t*)id;
  967. }
  968. EXPORT int jack_engine_takeover_timebase(jack_client_t* ext_client)
  969. {
  970. #ifdef __CLIENTDEBUG__
  971. JackLibGlobals::CheckContext();
  972. #endif
  973. JackClient* client = (JackClient*)ext_client;
  974. if (client == NULL) {
  975. jack_error("jack_engine_takeover_timebase called with a NULL client");
  976. return -1;
  977. } else {
  978. jack_error("jack_engine_takeover_timebase : not yet implemented\n");
  979. return 0;
  980. }
  981. }
  982. EXPORT jack_nframes_t jack_frames_since_cycle_start(const jack_client_t* ext_client)
  983. {
  984. #ifdef __CLIENTDEBUG__
  985. JackLibGlobals::CheckContext();
  986. #endif
  987. JackTimer timer;
  988. GetEngineControl()->ReadFrameTime(&timer);
  989. return (jack_nframes_t) floor((((float)GetEngineControl()->fSampleRate) / 1000000.0f) * (GetMicroSeconds() - timer.fCurrentCallback));
  990. }
  991. EXPORT jack_time_t jack_get_time(jack_client_t *client)
  992. {
  993. return GetMicroSeconds();
  994. }
  995. EXPORT jack_time_t jack_frames_to_time(const jack_client_t* ext_client, jack_nframes_t frames)
  996. {
  997. #ifdef __CLIENTDEBUG__
  998. JackLibGlobals::CheckContext();
  999. #endif
  1000. JackClient* client = (JackClient*)ext_client;
  1001. if (client == NULL) {
  1002. jack_error("jack_frames_to_time called with a NULL client");
  1003. return 0;
  1004. } else {
  1005. JackTimer timer;
  1006. GetEngineControl()->ReadFrameTime(&timer);
  1007. if (timer.fInitialized) {
  1008. return timer.fCurrentWakeup +
  1009. (long) rint(((double) ((frames - timer.fFrames)) *
  1010. ((jack_time_t)(timer.fNextWakeUp - timer.fCurrentWakeup))) / GetEngineControl()->fBufferSize);
  1011. } else {
  1012. return 0;
  1013. }
  1014. }
  1015. }
  1016. EXPORT jack_nframes_t jack_time_to_frames(const jack_client_t* ext_client, jack_time_t time)
  1017. {
  1018. #ifdef __CLIENTDEBUG__
  1019. JackLibGlobals::CheckContext();
  1020. #endif
  1021. JackClient* client = (JackClient*)ext_client;
  1022. if (client == NULL) {
  1023. jack_error("jack_time_to_frames called with a NULL client");
  1024. return 0;
  1025. } else {
  1026. JackTimer timer;
  1027. GetEngineControl()->ReadFrameTime(&timer);
  1028. if (timer.fInitialized) {
  1029. return timer.fFrames +
  1030. (long) rint(((double) ((time - timer.fCurrentWakeup)) /
  1031. ((jack_time_t)(timer.fNextWakeUp - timer.fCurrentWakeup))) * GetEngineControl()->fBufferSize);
  1032. } else {
  1033. return 0;
  1034. }
  1035. }
  1036. }
  1037. EXPORT jack_nframes_t jack_frame_time(const jack_client_t* ext_client)
  1038. {
  1039. return jack_time_to_frames(ext_client, GetMicroSeconds());
  1040. }
  1041. EXPORT jack_nframes_t jack_last_frame_time(const jack_client_t* ext_client)
  1042. {
  1043. #ifdef __CLIENTDEBUG__
  1044. JackLibGlobals::CheckContext();
  1045. #endif
  1046. JackTimer timer;
  1047. GetEngineControl()->ReadFrameTime(&timer);
  1048. return timer.fFrames;
  1049. }
  1050. EXPORT float jack_cpu_load(jack_client_t* ext_client)
  1051. {
  1052. #ifdef __CLIENTDEBUG__
  1053. JackLibGlobals::CheckContext();
  1054. #endif
  1055. JackClient* client = (JackClient*)ext_client;
  1056. if (client == NULL) {
  1057. jack_error("jack_cpu_load called with a NULL client");
  1058. return 0.0f;
  1059. } else {
  1060. return GetEngineControl()->fCPULoad;
  1061. }
  1062. }
  1063. EXPORT pthread_t jack_client_thread_id(jack_client_t* ext_client)
  1064. {
  1065. #ifdef __CLIENTDEBUG__
  1066. JackLibGlobals::CheckContext();
  1067. #endif
  1068. JackClient* client = (JackClient*)ext_client;
  1069. if (client == NULL) {
  1070. jack_error("jack_client_thread_id called with a NULL client");
  1071. return (pthread_t)NULL;
  1072. } else {
  1073. return client->GetThreadID();
  1074. }
  1075. }
  1076. EXPORT char* jack_get_client_name (jack_client_t* ext_client)
  1077. {
  1078. #ifdef __CLIENTDEBUG__
  1079. JackLibGlobals::CheckContext();
  1080. #endif
  1081. JackClient* client = (JackClient*)ext_client;
  1082. if (client == NULL) {
  1083. jack_error("jack_get_client_name called with a NULL client");
  1084. return NULL;
  1085. } else {
  1086. return client->GetClientControl()->fName;
  1087. }
  1088. }
  1089. EXPORT int jack_client_name_size(void)
  1090. {
  1091. return JACK_CLIENT_NAME_SIZE;
  1092. }
  1093. EXPORT int jack_port_name_size(void)
  1094. {
  1095. return JACK_PORT_NAME_SIZE;
  1096. }
  1097. // transport.h
  1098. EXPORT int jack_release_timebase(jack_client_t* ext_client)
  1099. {
  1100. #ifdef __CLIENTDEBUG__
  1101. JackLibGlobals::CheckContext();
  1102. #endif
  1103. JackClient* client = (JackClient*)ext_client;
  1104. if (client == NULL) {
  1105. jack_error("jack_release_timebase called with a NULL client");
  1106. return -1;
  1107. } else {
  1108. return client->ReleaseTimebase();
  1109. }
  1110. }
  1111. EXPORT int jack_set_sync_callback(jack_client_t* ext_client, JackSyncCallback sync_callback, void *arg)
  1112. {
  1113. #ifdef __CLIENTDEBUG__
  1114. JackLibGlobals::CheckContext();
  1115. #endif
  1116. JackClient* client = (JackClient*)ext_client;
  1117. if (client == NULL) {
  1118. jack_error("jack_set_sync_callback called with a NULL client");
  1119. return -1;
  1120. } else {
  1121. return client->SetSyncCallback(sync_callback, arg);
  1122. }
  1123. }
  1124. EXPORT int jack_set_sync_timeout(jack_client_t* ext_client, jack_time_t timeout)
  1125. {
  1126. #ifdef __CLIENTDEBUG__
  1127. JackLibGlobals::CheckContext();
  1128. #endif
  1129. JackClient* client = (JackClient*)ext_client;
  1130. if (client == NULL) {
  1131. jack_error("jack_set_sync_timeout called with a NULL client");
  1132. return -1;
  1133. } else {
  1134. return client->SetSyncTimeout(timeout);
  1135. }
  1136. }
  1137. EXPORT int jack_set_timebase_callback(jack_client_t* ext_client, int conditional, JackTimebaseCallback timebase_callback, void* arg)
  1138. {
  1139. #ifdef __CLIENTDEBUG__
  1140. JackLibGlobals::CheckContext();
  1141. #endif
  1142. JackClient* client = (JackClient*)ext_client;
  1143. if (client == NULL) {
  1144. jack_error("jack_set_timebase_callback called with a NULL client");
  1145. return -1;
  1146. } else {
  1147. return client->SetTimebaseCallback(conditional, timebase_callback, arg);
  1148. }
  1149. }
  1150. EXPORT int jack_transport_locate(jack_client_t* ext_client, jack_nframes_t frame)
  1151. {
  1152. #ifdef __CLIENTDEBUG__
  1153. JackLibGlobals::CheckContext();
  1154. #endif
  1155. JackClient* client = (JackClient*)ext_client;
  1156. if (client == NULL) {
  1157. jack_error("jack_transport_locate called with a NULL client");
  1158. return -1;
  1159. } else {
  1160. return client->TransportLocate(frame);
  1161. }
  1162. }
  1163. EXPORT jack_transport_state_t jack_transport_query(const jack_client_t* ext_client, jack_position_t* pos)
  1164. {
  1165. #ifdef __CLIENTDEBUG__
  1166. JackLibGlobals::CheckContext();
  1167. #endif
  1168. JackClient* client = (JackClient*)ext_client;
  1169. if (client == NULL) {
  1170. jack_error("jack_transport_query called with a NULL client");
  1171. return JackTransportStopped;
  1172. } else {
  1173. return client->TransportQuery(pos);
  1174. }
  1175. }
  1176. EXPORT jack_nframes_t jack_get_current_transport_frame(const jack_client_t* ext_client)
  1177. {
  1178. #ifdef __CLIENTDEBUG__
  1179. JackLibGlobals::CheckContext();
  1180. #endif
  1181. JackClient* client = (JackClient*)ext_client;
  1182. if (client == NULL) {
  1183. jack_error("jack_get_current_transport_frame called with a NULL client");
  1184. return 0;
  1185. } else {
  1186. return client->GetCurrentTransportFrame();
  1187. }
  1188. }
  1189. EXPORT int jack_transport_reposition(jack_client_t* ext_client, jack_position_t* pos)
  1190. {
  1191. #ifdef __CLIENTDEBUG__
  1192. JackLibGlobals::CheckContext();
  1193. #endif
  1194. JackClient* client = (JackClient*)ext_client;
  1195. if (client == NULL) {
  1196. jack_error("jack_transport_reposition called with a NULL client");
  1197. return -1;
  1198. } else {
  1199. return client->TransportReposition(pos);
  1200. }
  1201. }
  1202. EXPORT void jack_transport_start(jack_client_t* ext_client)
  1203. {
  1204. #ifdef __CLIENTDEBUG__
  1205. JackLibGlobals::CheckContext();
  1206. #endif
  1207. JackClient* client = (JackClient*)ext_client;
  1208. if (client == NULL) {
  1209. jack_error("jack_transport_start called with a NULL client");
  1210. } else {
  1211. client->TransportStart();
  1212. }
  1213. }
  1214. EXPORT void jack_transport_stop(jack_client_t* ext_client)
  1215. {
  1216. #ifdef __CLIENTDEBUG__
  1217. JackLibGlobals::CheckContext();
  1218. #endif
  1219. JackClient* client = (JackClient*)ext_client;
  1220. if (client == NULL) {
  1221. jack_error("jack_transport_stop called with a NULL client");
  1222. } else {
  1223. client->TransportStop();
  1224. }
  1225. }
  1226. // deprecated
  1227. EXPORT void jack_get_transport_info(jack_client_t* ext_client, jack_transport_info_t* tinfo)
  1228. {
  1229. JackLog("jack_get_transport_info : deprecated");
  1230. if (tinfo)
  1231. memset(tinfo, 0, sizeof(jack_transport_info_t));
  1232. }
  1233. EXPORT void jack_set_transport_info(jack_client_t* ext_client, jack_transport_info_t* tinfo)
  1234. {
  1235. JackLog("jack_set_transport_info : deprecated");
  1236. if (tinfo)
  1237. memset(tinfo, 0, sizeof(jack_transport_info_t));
  1238. }
  1239. // statistics.h
  1240. EXPORT float jack_get_max_delayed_usecs(jack_client_t* ext_client)
  1241. {
  1242. #ifdef __CLIENTDEBUG__
  1243. JackLibGlobals::CheckContext();
  1244. #endif
  1245. JackLog("jack_get_max_delayed_usecs: not yet implemented\n");
  1246. return 0.f;
  1247. }
  1248. EXPORT float jack_get_xrun_delayed_usecs(jack_client_t* ext_client)
  1249. {
  1250. #ifdef __CLIENTDEBUG__
  1251. JackLibGlobals::CheckContext();
  1252. #endif
  1253. JackLog("jack_get_xrun_delayed_usecs: not yet implemented\n");
  1254. return 0.f;
  1255. }
  1256. EXPORT void jack_reset_max_delayed_usecs(jack_client_t* ext_client)
  1257. {
  1258. #ifdef __CLIENTDEBUG__
  1259. JackLibGlobals::CheckContext();
  1260. #endif
  1261. JackLog("jack_reset_max_delayed_usecs: not yet implemented\n");
  1262. }
  1263. // thread.h
  1264. EXPORT int jack_acquire_real_time_scheduling(pthread_t thread, int priority)
  1265. {
  1266. #ifdef __APPLE__
  1267. return JackMachThread::AcquireRealTimeImp(thread, 0, 500 * 1000, 500 * 1000);
  1268. #elif WIN32
  1269. return JackWinThread::AcquireRealTimeImp(thread, priority);
  1270. #else
  1271. return JackPosixThread::AcquireRealTimeImp(thread, priority);
  1272. #endif
  1273. }
  1274. EXPORT int jack_client_create_thread(jack_client_t* client,
  1275. pthread_t *thread,
  1276. int priority,
  1277. int realtime, /* boolean */
  1278. void *(*start_routine)(void*),
  1279. void *arg)
  1280. {
  1281. #ifdef __APPLE__
  1282. return JackPosixThread::StartImp(thread, priority, realtime, start_routine, arg);
  1283. #elif WIN32
  1284. return JackWinThread::StartImp(thread, priority, realtime, (ThreadCallback)start_routine, arg);
  1285. #else
  1286. return JackPosixThread::StartImp(thread, priority, realtime, start_routine, arg);
  1287. #endif
  1288. }
  1289. EXPORT int jack_drop_real_time_scheduling(pthread_t thread)
  1290. {
  1291. #ifdef __APPLE__
  1292. return JackMachThread::DropRealTimeImp(thread);
  1293. #elif WIN32
  1294. return JackWinThread::DropRealTimeImp(thread);
  1295. #else
  1296. return JackPosixThread::DropRealTimeImp(thread);
  1297. #endif
  1298. }
  1299. // intclient.h
  1300. EXPORT char* jack_get_internal_client_name(jack_client_t* ext_client, jack_intclient_t intclient)
  1301. {
  1302. JackLog("jack_get_internal_client_name: not yet implemented\n");
  1303. return "";
  1304. }
  1305. EXPORT jack_intclient_t jack_internal_client_handle(jack_client_t* ext_client, const char* client_name, jack_status_t* status)
  1306. {
  1307. JackLog("jack_internal_client_handle: not yet implemented\n");
  1308. return 0;
  1309. }
  1310. EXPORT jack_intclient_t jack_internal_client_load(jack_client_t* ext_client, const char* client_name, jack_options_t options, jack_status_t* status, ...)
  1311. {
  1312. JackLog("jack_internal_client_load: not yet implemented\n");
  1313. return 0;
  1314. }
  1315. EXPORT jack_status_t jack_internal_client_unload(jack_client_t* ext_client, jack_intclient_t intclient)
  1316. {
  1317. JackLog("jack_internal_client_unload: not yet implemented\n");
  1318. return JackFailure;
  1319. }
  1320. // Automatic jack server launch
  1321. #ifndef WIN32
  1322. #define JACK_LOCATION
  1323. #define JACK_DEFAULT_DRIVER
  1324. /* Exec the JACK server in this process. Does not return. */
  1325. static void start_server_aux(const char *server_name)
  1326. {
  1327. FILE* fp = 0;
  1328. char filename[255];
  1329. char arguments[255];
  1330. char buffer[255];
  1331. char* command = 0;
  1332. size_t pos = 0;
  1333. size_t result = 0;
  1334. char** argv = 0;
  1335. int i = 0;
  1336. int good = 0;
  1337. int ret;
  1338. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  1339. fp = fopen(filename, "r");
  1340. if (!fp) {
  1341. fp = fopen("/etc/jackdrc", "r");
  1342. }
  1343. /* if still not found, check old config name for backwards compatability */
  1344. if (!fp) {
  1345. fp = fopen("/etc/jackd.conf", "r");
  1346. }
  1347. if (fp) {
  1348. arguments[0] = '\0';
  1349. ret = fscanf(fp, "%s", buffer);
  1350. while(ret != 0 && ret != EOF) {
  1351. strcat(arguments, buffer);
  1352. strcat(arguments, " ");
  1353. ret = fscanf(fp, "%s", buffer);
  1354. }
  1355. if (strlen(arguments) > 0) {
  1356. good = 1;
  1357. }
  1358. }
  1359. if (!good) {
  1360. command = JACK_LOCATION "/jackdmp";
  1361. strncpy(arguments, JACK_LOCATION "/jackdmp -T -d "JACK_DEFAULT_DRIVER, 255);
  1362. } else {
  1363. result = strcspn(arguments, " ");
  1364. command = (char *) malloc(result+1);
  1365. strncpy(command, arguments, result);
  1366. command[result] = '\0';
  1367. }
  1368. argv = (char **) malloc (255);
  1369. while (1) {
  1370. /* insert -T and -nserver_name in front of arguments */
  1371. if (i == 1) {
  1372. argv[i] = (char *) malloc(strlen ("-T") + 1);
  1373. strcpy (argv[i++], "-T");
  1374. if (server_name) {
  1375. size_t optlen = strlen ("-n");
  1376. char* buf = (char*)malloc(optlen + strlen (server_name) + 1);
  1377. strcpy (buf, "-n");
  1378. strcpy (buf+optlen, server_name);
  1379. argv[i++] = buf;
  1380. }
  1381. }
  1382. result = strcspn(arguments + pos, " ");
  1383. if (result == 0) {
  1384. break;
  1385. }
  1386. argv[i] = (char*)malloc(result + 1);
  1387. strncpy(argv[i], arguments+pos, result);
  1388. argv[i][result] = '\0';
  1389. pos += result + 1;
  1390. ++i;
  1391. }
  1392. argv[i] = 0;
  1393. execv(command, argv);
  1394. /* If execv() succeeds, it does not return. There's no point
  1395. * in calling jack_error() here in the child process. */
  1396. perror("exec of JACK server failed");
  1397. }
  1398. static int start_server(const char *server_name, jack_options_t options)
  1399. {
  1400. if ((options & JackNoStartServer) || getenv("JACK_NO_START_SERVER")) {
  1401. return 1;
  1402. }
  1403. /* The double fork() forces the server to become a child of
  1404. * init, which will always clean up zombie process state on
  1405. * termination. This even works in cases where the server
  1406. * terminates but this client does not.
  1407. *
  1408. * Since fork() is usually implemented using copy-on-write
  1409. * virtual memory tricks, the overhead of the second fork() is
  1410. * probably relatively small.
  1411. */
  1412. switch (fork()) {
  1413. case 0: /* child process */
  1414. switch (fork()) {
  1415. case 0: /* grandchild process */
  1416. start_server_aux(server_name);
  1417. _exit(99); /* exec failed */
  1418. case -1:
  1419. _exit(98);
  1420. default:
  1421. _exit(0);
  1422. }
  1423. case -1: /* fork() error */
  1424. return 1; /* failed to start server */
  1425. }
  1426. /* only the original parent process goes here */
  1427. return 0; /* (probably) successful */
  1428. }
  1429. int server_connect(char* name)
  1430. {
  1431. return -1;
  1432. }
  1433. int try_start_server(jack_varargs_t* va, jack_options_t options, jack_status_t* status)
  1434. {
  1435. if (server_connect(va->server_name) < 0) {
  1436. int trys;
  1437. if (start_server(va->server_name, options)) {
  1438. int my_status1 = *status | JackFailure | JackServerFailed;
  1439. *status = (jack_status_t)my_status1;
  1440. return -1;
  1441. }
  1442. trys = 5;
  1443. do {
  1444. sleep(1);
  1445. if (--trys < 0) {
  1446. int my_status1 = *status | JackFailure | JackServerFailed;
  1447. *status = (jack_status_t)my_status1;
  1448. return -1;
  1449. }
  1450. } while (server_connect(va->server_name) < 0);
  1451. int my_status1 = *status | JackServerStarted;
  1452. *status = (jack_status_t)my_status1;
  1453. }
  1454. return 0;
  1455. }
  1456. #endif