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.

1542 lines
48KB

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