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.

2050 lines
80KB

  1. /*
  2. Copyright (C) 2005 Samuel TRACOL for GRAME
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. /** @file jack_test.c
  16. *
  17. * @brief This client test the jack API.
  18. *
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <errno.h>
  23. #ifndef WIN32
  24. #include <unistd.h>
  25. #endif
  26. #include <string.h>
  27. #include <getopt.h>
  28. #include <math.h>
  29. #include <assert.h>
  30. #include <stdarg.h>
  31. #include <jack/jack.h>
  32. #include <jack/intclient.h>
  33. #include <jack/transport.h>
  34. #if defined(WIN32) && !defined(M_PI)
  35. #define M_PI 3.151592653
  36. #endif
  37. #ifdef WIN32
  38. #define jack_sleep(val) Sleep((val))
  39. #else
  40. #define jack_sleep(val) usleep((val) * 1000)
  41. #endif
  42. typedef struct
  43. {
  44. jack_nframes_t ft; // running counter frame time
  45. jack_nframes_t fcs; // from sycle start...
  46. jack_nframes_t lft; // last frame time...
  47. }
  48. FrameTimeCollector;
  49. FILE *file;
  50. FrameTimeCollector* framecollect;
  51. FrameTimeCollector perpetualcollect;
  52. FrameTimeCollector lastperpetualcollect;
  53. int frames_collected = 0;
  54. // ports
  55. jack_port_t *output_port1;
  56. jack_port_t *output_port1b;
  57. jack_port_t *input_port2;
  58. jack_port_t *output_port2;
  59. jack_port_t *input_port1;
  60. // clients
  61. jack_client_t *client1;
  62. jack_client_t *client2;
  63. const char *client_name1;
  64. const char *client_name2;
  65. unsigned long sr; // sample rate
  66. // for time -t option
  67. int time_to_run = 0;
  68. int time_before_exit = 1;
  69. // standard error count
  70. int t_error = 0;
  71. int reorder = 0; // graph reorder callback
  72. int RT = 0; // is real time or not...
  73. int FW = 0; // freewheel mode
  74. int init_clbk = 0; // init callback
  75. int port_rename_clbk = 0; // portrename callback
  76. int i, j, k = 0;
  77. int port_callback_reg = 0;
  78. jack_nframes_t cur_buffer_size, old_buffer_size, cur_pos;
  79. int activated = 0;
  80. int count1, count2 = 0; // for freewheel
  81. int xrun = 0;
  82. int have_xrun = 0; // msg to tell the process1 function to write a special thing in the frametime file.
  83. int process1_activated = -1; // to control processing...
  84. int process2_activated = -1; // to control processing...
  85. unsigned long int index1 = 0;
  86. unsigned long int index2 = 0;
  87. jack_default_audio_sample_t *signal1; // signal source d'emission
  88. jack_default_audio_sample_t *signal2; // tableau de reception
  89. jack_transport_state_t ts;
  90. jack_position_t pos;
  91. jack_position_t request_pos;
  92. int silent_error = 0; // jack silent mode
  93. int verbose_mode = 0;
  94. int transport_mode = 1;
  95. jack_nframes_t input_ext_latency = 0; // test latency for PHY devices
  96. jack_nframes_t output_ext_latency = 0; // test latency for PHY devices
  97. int sync_called = 0;
  98. int starting_state = 1;
  99. int linecount = 0; // line counter for log file of sampleframe counter --> for graph function.
  100. int linebuf = 0; // reminders for graph analysis
  101. int linetransport = 0;
  102. int linefw = 0;
  103. int lineports = 0;
  104. int linecl2 = 0;
  105. int client_register = 0;
  106. /**
  107. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  108. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  109. Callbacks & basics functions
  110. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  111. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  112. */
  113. void usage()
  114. {
  115. fprintf (stderr, "\n\n"
  116. "usage: jack_test \n"
  117. " [ --time OR -t time_to_run (in seconds) ]\n"
  118. " [ --quiet OR -q (quiet mode : without jack server errors) ]\n"
  119. " [ --verbose OR -v (verbose mode : no details on tests done. Only main results & errors) ]\n"
  120. " [ --transport OR -k (Do not test transport functions.) ]\n"
  121. " --realtime OR -R (jack is in rt mode)\n\n\n"
  122. );
  123. exit(1);
  124. }
  125. void Log(const char *fmt, ...)
  126. {
  127. if (verbose_mode) {
  128. va_list ap;
  129. va_start(ap, fmt);
  130. vfprintf(stderr, fmt, ap);
  131. va_end(ap);
  132. }
  133. }
  134. void Collect(FrameTimeCollector* TheFrame)
  135. {
  136. TheFrame->lft = jack_last_frame_time(client1);
  137. TheFrame->ft = jack_frame_time(client1);
  138. TheFrame->fcs = jack_frames_since_cycle_start(client1);
  139. }
  140. void Jack_Thread_Init_Callback(void *arg)
  141. {
  142. Log("Init callback has been successfully called. (msg from callback)\n");
  143. init_clbk = 1;
  144. }
  145. void Jack_Freewheel_Callback(int starting, void *arg)
  146. {
  147. Log("Freewheel callback has been successfully called with value %i. (msg from callback)\n", starting);
  148. FW = starting;
  149. }
  150. void Jack_Client_Registration_Callback(const char* name, int val, void *arg)
  151. {
  152. Log("Client registration callback name = %s has been successfully called with value %i. (msg from callback)\n", name, val);
  153. if (val)
  154. client_register++;
  155. else
  156. client_register--;
  157. }
  158. int Jack_Port_Rename_Callback(jack_port_id_t port, const char* old_name, const char* new_name, void *arg)
  159. {
  160. Log("Rename callback has been successfully called with old_name '%s' and new_name '%s'. (msg from callback)\n", old_name, new_name);
  161. port_rename_clbk = 1;
  162. return 0;
  163. }
  164. int Jack_Update_Buffer_Size(jack_nframes_t nframes, void *arg)
  165. {
  166. cur_buffer_size = jack_get_buffer_size(client1);
  167. Log("Buffer size = %d (msg from callback)\n", cur_buffer_size);
  168. return 0;
  169. }
  170. int Jack_XRun_Callback(void *arg)
  171. {
  172. xrun++;
  173. have_xrun = 1;
  174. Log("Xrun has been detected ! (msg from callback)\n");
  175. return 0;
  176. }
  177. int Jack_Graph_Order_Callback(void *arg)
  178. {
  179. reorder++;
  180. return 0;
  181. }
  182. int Jack_Sample_Rate_Callback(jack_nframes_t nframes, void *arg)
  183. {
  184. Log("Sample rate : %i.\n", nframes);
  185. return 0;
  186. }
  187. void Jack_Error_Callback(const char *msg)
  188. {
  189. if (silent_error == 0) {
  190. fprintf(stderr, "error : %s (msg from callback)\n", msg);
  191. }
  192. }
  193. void jack_shutdown(void *arg)
  194. {
  195. printf("Jack_test has been kicked out by jackd !\n");
  196. exit(1);
  197. }
  198. void jack_info_shutdown(jack_status_t code, const char* reason, void *arg)
  199. {
  200. printf("JACK server failure : %s\n", reason);
  201. exit(1);
  202. }
  203. void Jack_Port_Register(jack_port_id_t port, int mode, void *arg)
  204. {
  205. port_callback_reg++;
  206. }
  207. void Jack_Port_Connect(jack_port_id_t a, jack_port_id_t b, int connect, void* arg)
  208. {
  209. Log("PortConnect src = %ld dst = %ld onoff = %ld (msg from callback)\n", a, b, connect);
  210. }
  211. int Jack_Sync_Callback(jack_transport_state_t state, jack_position_t *pos, void *arg)
  212. {
  213. int res = 0;
  214. switch (state) {
  215. case JackTransportStarting:
  216. sync_called++;
  217. if (starting_state == 0) {
  218. Log("sync callback : Releasing status : now ready...\n");
  219. res = 1;
  220. } else {
  221. if (sync_called == 1) {
  222. Log("sync callback : Holding status...\n");
  223. }
  224. res = 0;
  225. }
  226. break;
  227. case JackTransportStopped:
  228. Log("sync callback : JackTransportStopped...\n");
  229. res = 0;
  230. break;
  231. default:
  232. res = 0;
  233. break;
  234. }
  235. return res;
  236. }
  237. /**
  238. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  239. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  240. processing functions
  241. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  242. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  243. * Proccess1 is for client1
  244. * 4 modes, activated with process1_activated
  245. *
  246. * -1 : idle mode
  247. * 0 : write zeros to output 1
  248. * 1 : write continuously signal1 (sinusoidal test signal) to output1
  249. * 3 : mode for summation test. While record (done by process2) is not running, write signal1 to both out1 & out1b.
  250. * when record begin (index2 > 0), write signal1 in phase opposition to out1 & out2
  251. * 5 : Frames Time checking mode : write the array containing the three values of frame_time, frames cycles start and
  252. * last frame time during 150 cycles.
  253. */
  254. int process1(jack_nframes_t nframes, void *arg)
  255. {
  256. if (FW == 0) {
  257. Collect(&perpetualcollect);
  258. if (have_xrun) {
  259. fprintf(file, "%i %i\n", (perpetualcollect.ft - lastperpetualcollect.ft), (2*cur_buffer_size));
  260. have_xrun = 0;
  261. } else {
  262. fprintf(file, "%i 0\n", (perpetualcollect.ft - lastperpetualcollect.ft));
  263. }
  264. linecount++;
  265. lastperpetualcollect.ft = perpetualcollect.ft;
  266. }
  267. jack_default_audio_sample_t *out1;
  268. jack_default_audio_sample_t *out1b;
  269. activated++; // counter of callback activation
  270. if (process1_activated == 1) {
  271. out1 = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port1, nframes);
  272. for (jack_nframes_t p = 0; p < nframes; p++) {
  273. out1[p] = signal1[index1];
  274. index1++;
  275. if (index1 == 48000)
  276. index1 = 0;
  277. }
  278. }
  279. if (process1_activated == 3) {
  280. out1 = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port1, nframes);
  281. out1b = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port1b, nframes);
  282. for (jack_nframes_t p = 0; p < nframes; p++) {
  283. out1[p] = signal1[index1];
  284. if (index2 != 0) {
  285. out1b[p] = ( -1 * signal1[index1]);
  286. } else {
  287. out1b[p] = signal1[index1];
  288. }
  289. index1++;
  290. if (index1 == 48000)
  291. index1 = 0;
  292. }
  293. }
  294. if (process1_activated == 0) {
  295. out1 = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port1, nframes);
  296. memset (out1, 0, sizeof (jack_default_audio_sample_t) * nframes); //�crit des z�ros en sortie...
  297. }
  298. if (process1_activated == 5) {
  299. Collect(&framecollect[frames_collected]);
  300. frames_collected++;
  301. if (frames_collected > 798) {
  302. process1_activated = -1;
  303. }
  304. }
  305. return 0;
  306. }
  307. /**
  308. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  309. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  310. * Proccess2 is for client2
  311. * 3 modes, activated with process1_activated
  312. *
  313. * -1 : idle mode
  314. * 0 : idle mode
  315. * 1 : record in2 into signal2.(for first transmit test)
  316. * 2 : record in2 into signal2 while send signal1 in out2. used dor Tie data test.
  317. * 3 : record in2 into sigal2 for summation data test.
  318. * In records modes, at the end of the record (signal2 is full), it stop the test, setting both activation states to -1.
  319. */
  320. int process2(jack_nframes_t nframes, void *arg)
  321. {
  322. jack_default_audio_sample_t *out2;
  323. jack_default_audio_sample_t *in2;
  324. if (process2_activated == 1) { // Reception du process1 pour comparer les donnees
  325. in2 = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port2, nframes);
  326. for (unsigned int p = 0; p < nframes; p++) {
  327. signal2[index2] = in2[p];
  328. if (index2 == 95999) {
  329. process2_activated = 0;
  330. process1_activated = 0;
  331. //index2 = 0;
  332. } else {
  333. index2++;
  334. }
  335. }
  336. }
  337. if (process2_activated == 2) { // envoie de signal1 pour test tie mode et le r�cup�re direct + latence de la boucle jack...
  338. out2 = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port2, nframes);
  339. in2 = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port2, nframes);
  340. for (unsigned int p = 0; p < nframes; p++) {
  341. out2[p] = signal1[index1];
  342. index1++;
  343. if (index1 == 48000)
  344. index1 = 0;
  345. signal2[index2] = in2[p];
  346. if (index2 == 95999) {
  347. process2_activated = -1;
  348. //index2 = 0;
  349. } else {
  350. index2++;
  351. }
  352. }
  353. }
  354. if (process2_activated == 3) { // envoie de -signal1 pour sommation en oppo de phase par jack
  355. in2 = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port2, nframes);
  356. for (unsigned int p = 0; p < nframes;p++) {
  357. signal2[index2] = in2[p];
  358. if (index2 == 95999) {
  359. process2_activated = 0;
  360. process1_activated = 0;
  361. //index2 = 0;
  362. } else {
  363. index2++;
  364. }
  365. }
  366. }
  367. return 0;
  368. }
  369. // Alternate thread model
  370. static int _process (jack_nframes_t nframes)
  371. {
  372. jack_default_audio_sample_t *in, *out;
  373. in = (jack_default_audio_sample_t *)jack_port_get_buffer (input_port1, nframes);
  374. out = (jack_default_audio_sample_t *)jack_port_get_buffer (output_port1, nframes);
  375. memcpy (out, in,
  376. sizeof (jack_default_audio_sample_t) * nframes);
  377. return 0;
  378. }
  379. static void* jack_thread(void *arg)
  380. {
  381. jack_client_t* client = (jack_client_t*) arg;
  382. jack_nframes_t last_thread_time = jack_frame_time(client);
  383. while (1) {
  384. jack_nframes_t frames = jack_cycle_wait (client);
  385. jack_nframes_t current_thread_time = jack_frame_time(client);
  386. jack_nframes_t delta_time = current_thread_time - last_thread_time;
  387. Log("jack_thread : delta_time = %ld\n", delta_time);
  388. int status = _process(frames);
  389. last_thread_time = current_thread_time;
  390. jack_cycle_signal (client, status);
  391. }
  392. return 0;
  393. }
  394. // To test callback exiting
  395. int process3(jack_nframes_t nframes, void *arg)
  396. {
  397. static int process3_call = 0;
  398. if (process3_call++ > 10) {
  399. Log("process3 callback : exiting...\n");
  400. return -1;
  401. } else {
  402. Log("calling process3 callback : process3_call = %ld\n", process3_call);
  403. return 0;
  404. }
  405. }
  406. int process4(jack_nframes_t nframes, void *arg)
  407. {
  408. jack_client_t* client = (jack_client_t*) arg;
  409. static jack_nframes_t last_time = jack_frame_time(client);
  410. static jack_nframes_t tolerance = (jack_nframes_t)(cur_buffer_size * 0.1f);
  411. jack_nframes_t cur_time = jack_frame_time(client);
  412. jack_nframes_t delta_time = cur_time - last_time;
  413. Log("calling process4 callback : jack_frame_time = %ld delta_time = %ld\n", cur_time, delta_time);
  414. if (delta_time > 0 && (jack_nframes_t)abs(delta_time - cur_buffer_size) > tolerance) {
  415. printf("!!! ERROR !!! jack_frame_time seems to return incorrect values cur_buffer_size = %d, delta_time = %d tolerance %d\n", cur_buffer_size, delta_time, tolerance);
  416. }
  417. last_time = cur_time;
  418. return 0;
  419. }
  420. static void display_transport_state()
  421. {
  422. jack_transport_state_t ts;
  423. jack_position_t pos;
  424. ts = jack_transport_query(client2, &pos);
  425. switch (ts) {
  426. case JackTransportStopped:
  427. Log("Transport is stopped...\n");
  428. break;
  429. case JackTransportRolling:
  430. Log("Transport is rolling...\n");
  431. break;
  432. case JackTransportLooping:
  433. Log("Transport is looping...\n");
  434. break;
  435. case JackTransportStarting:
  436. Log("Transport is starting...\n");
  437. break;
  438. case JackTransportNetStarting:
  439. Log("Transport is starting with network sync...\n");
  440. break;
  441. }
  442. }
  443. /**
  444. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  445. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  446. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  447. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  448. MAIN FUNCTION
  449. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  450. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  451. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  452. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  453. */
  454. int main (int argc, char *argv[])
  455. {
  456. const char **inports; // array of PHY input/output
  457. const char **outports; // array of PHY input/outputs
  458. const char *server_name = NULL;
  459. const char **connexions1;
  460. const char **connexions2;
  461. jack_status_t status;
  462. char portname[128] = "port";
  463. char filename[128] = "framefile.ext";
  464. const char *nullportname = "";
  465. int option_index;
  466. int opt;
  467. int a = 0; // working number for in/out port (PHY)...
  468. int test_link = 0; // for testing the "overconnect" function
  469. int flag; // flag for ports...
  470. int is_mine = 0; // to test jack_port_is_mine function...
  471. const char *options = "kRnqvt:";
  472. float ratio; // for speed calculation in freewheel mode
  473. jack_options_t jack_options = JackNullOption;
  474. struct option long_options[] = {
  475. {"realtime", 0, 0, 'R'},
  476. {"non-realtime", 0, 0, 'n'},
  477. {"time", 0, 0, 't'},
  478. {"quiet", 0, 0, 'q'},
  479. {"verbose", 0, 0, 'v'},
  480. {"transport", 0, 0, 'k'},
  481. {0, 0, 0, 0}
  482. };
  483. client_name1 = "jack_test";
  484. time_to_run = 1;
  485. //verbose_mode = 1;
  486. //RT = 1;
  487. while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
  488. switch (opt) {
  489. case 'k':
  490. transport_mode = 0;
  491. break;
  492. case 'q':
  493. silent_error = 1;
  494. break;
  495. case 'v':
  496. verbose_mode = 1;
  497. printf("Verbose mode is activated...\n");
  498. break;
  499. case 't':
  500. time_to_run = atoi(optarg);
  501. break;
  502. case 'R':
  503. RT = 1;
  504. break;
  505. default:
  506. fprintf (stderr, "unknown option %c\n", opt);
  507. usage ();
  508. }
  509. }
  510. if (RT) {
  511. printf("Jack server is said being in realtime mode...\n");
  512. } else {
  513. printf("Jack server is said being in non-realtime mode...\n");
  514. }
  515. /**
  516. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  517. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  518. init signal data for test
  519. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  520. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  521. */
  522. framecollect = (FrameTimeCollector *) malloc(800 * sizeof(FrameTimeCollector));
  523. signal1 = (jack_default_audio_sample_t *) malloc(48000 * sizeof(jack_default_audio_sample_t));
  524. signal2 = (jack_default_audio_sample_t *) malloc(96000 * sizeof(jack_default_audio_sample_t));
  525. signal1[0] = 0;
  526. int p;
  527. for (p = 1; p < 48000;p++) {
  528. signal1[p] = (float)(sin((p * 2 * M_PI * 1000 ) / 48000));
  529. }
  530. for (p = 0; p < 95999;p++) {
  531. signal2[p] = 0.0 ;
  532. }
  533. index1 = 0;
  534. index2 = 0;
  535. /**
  536. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  537. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  538. begin test
  539. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  540. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  541. */
  542. printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
  543. printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
  544. printf("*-*-*-*-*-*-*-*-*-*-*-*-*-* Start jack server stress test *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
  545. printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
  546. printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
  547. /**
  548. * Register a client...
  549. *
  550. */
  551. Log("Register a client using jack_client_open()...\n");
  552. client1 = jack_client_open(client_name1, jack_options, &status, server_name);
  553. if (client1 == NULL) {
  554. fprintf (stderr, "jack_client_open() failed, "
  555. "status = 0x%2.0x\n", status);
  556. if (status & JackServerFailed) {
  557. fprintf(stderr, "Unable to connect to JACK server\n");
  558. }
  559. exit (1);
  560. }
  561. if (status & JackServerStarted) {
  562. fprintf(stderr, "JACK server started\n");
  563. }
  564. /**
  565. * Internal client tests...
  566. *
  567. */
  568. jack_intclient_t intclient;
  569. Log("trying to load the \"inprocess\" server internal client \n");
  570. intclient = jack_internal_client_load (client1, "inprocess",
  571. (jack_options_t)(JackLoadName|JackLoadInit),
  572. &status, "inprocess", "");
  573. if (intclient == 0 || status & JackFailure) {
  574. printf("!!! ERROR !!! cannot load internal client \"inprocess\" intclient 0x%llX status 0x%2.0x !\n", (unsigned long long)intclient, status);
  575. } else {
  576. Log("\"inprocess\" server internal client loaded\n");
  577. char* internal_name = jack_get_internal_client_name(client1, intclient);
  578. if (strcmp(internal_name, "inprocess") == 0) {
  579. Log("jack_get_internal_client_name returns %s\n", internal_name);
  580. } else {
  581. printf("!!! ERROR !!! jack_get_internal_client_name returns incorrect name %s\n", internal_name);
  582. }
  583. jack_intclient_t intclient1 = jack_internal_client_handle(client1, "inprocess", &status);
  584. if (intclient1 == intclient) {
  585. Log("jack_internal_client_handle returns correct handle\n");
  586. } else {
  587. printf("!!! ERROR !!! jack_internal_client_handle returns incorrect handle 0x%llX\n", (unsigned long long)intclient1);
  588. }
  589. // Unload internal client
  590. status = jack_internal_client_unload (client1, intclient);
  591. if (status == 0) {
  592. Log("jack_internal_client_unload done first time returns correct value\n");
  593. } else {
  594. printf("!!! ERROR !!! jack_internal_client_unload returns incorrect value 0x%2.0x\n", status);
  595. }
  596. // Unload internal client second time
  597. status = jack_internal_client_unload (client1, intclient);
  598. if (status & JackFailure && status & JackNoSuchClient) {
  599. Log("jack_internal_client_unload done second time returns correct value\n");
  600. } else {
  601. printf("!!! ERROR !!! jack_internal_client_unload returns incorrect value 0x%2.0x\n", status);
  602. }
  603. }
  604. /**
  605. * try to register another one with the same name...
  606. *
  607. */
  608. Log("trying to register a new jackd client with name %s using jack_client_new()...\n", client_name1);
  609. client2 = jack_client_new(client_name1);
  610. if (client2 == NULL) {
  611. Log ("valid : a second client with the same name cannot be registered\n");
  612. } else {
  613. printf("!!! ERROR !!! Jackd server has accepted multiples client with the same name !\n");
  614. jack_client_close(client2);
  615. }
  616. /**
  617. * try to register another one with the same name using jack_client_open ==> since JackUseExactName is not used, an new client should be opened...
  618. *
  619. */
  620. Log("trying to register a new jackd client with name %s using jack_client_open()...\n", client_name1);
  621. client2 = jack_client_open(client_name1, jack_options, &status, server_name);
  622. if (client2 != NULL) {
  623. Log ("valid : a second client with the same name can be registered (client automatic renaming)\n");
  624. jack_client_close(client2);
  625. } else {
  626. printf("!!! ERROR !!! Jackd server automatic renaming feature does not work!\n");
  627. }
  628. /**
  629. * testing client name...
  630. * Verify that the name sended at registration and the one returned by jack server is the same...
  631. *
  632. */
  633. Log("Testing name...");
  634. client_name2 = jack_get_client_name(client1);
  635. if (strcmp(client_name1, client_name2) == 0)
  636. Log(" ok\n");
  637. else
  638. printf("\n!!! ERROR !!! name returned different from the one given : %s\n", client_name2);
  639. /**
  640. * Test RT mode...
  641. * verify if the real time mode returned by jack match the optional argument defined when launching jack_test*/
  642. if (jack_is_realtime(client1) == RT)
  643. Log("Jackd is in realtime mode (RT = %i).\n", RT);
  644. else
  645. printf("!!! ERROR !!! Jackd is in a non-expected realtime mode (RT = %i).\n", RT);
  646. /**
  647. * Register all callbacks...
  648. *
  649. */
  650. if (jack_set_thread_init_callback(client1, Jack_Thread_Init_Callback, 0) != 0)
  651. printf("!!! ERROR !!! while calling jack_set_thread_init_callback()...\n");
  652. if (jack_set_freewheel_callback(client1, Jack_Freewheel_Callback, 0) != 0 )
  653. printf("\n!!! ERROR !!! while calling jack_set_freewheel_callback()...\n");
  654. if (jack_set_process_callback(client1, process1, 0) != 0) {
  655. printf("Error when calling jack_set_process_callback() !\n");
  656. }
  657. jack_on_shutdown(client1, jack_shutdown, 0);
  658. if (jack_on_info_shutdown)
  659. jack_on_info_shutdown(client1, jack_info_shutdown, 0);
  660. if (jack_set_buffer_size_callback(client1, Jack_Update_Buffer_Size, 0) != 0) {
  661. printf("Error when calling buffer_size_callback !\n");
  662. }
  663. if (jack_set_graph_order_callback(client1, Jack_Graph_Order_Callback, 0) != 0) {
  664. printf("Error when calling Jack_Graph_Order_Callback() !\n");
  665. }
  666. if (jack_set_port_rename_callback(client1, Jack_Port_Rename_Callback, 0) != 0 )
  667. printf("\n!!! ERROR !!! while calling jack_set_rename_callback()...\n");
  668. if (jack_set_xrun_callback(client1, Jack_XRun_Callback, 0 ) != 0) {
  669. printf("Error when calling jack_set_xrun_callback() !\n");
  670. }
  671. if (jack_set_sample_rate_callback(client1, Jack_Sample_Rate_Callback, 0 ) != 0) {
  672. printf("Error when calling Jack_Sample_Rate_Callback() !\n");
  673. }
  674. if (jack_set_port_registration_callback(client1, Jack_Port_Register, 0) != 0) {
  675. printf("Error when calling jack_set_port_registration_callback() !\n");
  676. }
  677. if (jack_set_port_connect_callback(client1, Jack_Port_Connect, 0) != 0) {
  678. printf("Error when calling jack_set_port_connect_callback() !\n");
  679. }
  680. if (jack_set_client_registration_callback(client1, Jack_Client_Registration_Callback, 0) != 0) {
  681. printf("Error when calling jack_set_client_registration_callback() !\n");
  682. }
  683. jack_set_error_function(Jack_Error_Callback);
  684. /**
  685. * Create file for clock "frame time" analysis
  686. *
  687. */
  688. cur_buffer_size = jack_get_buffer_size(client1);
  689. sprintf (filename, "framefile-%i.dat", cur_buffer_size);
  690. file = fopen(filename, "w");
  691. if (file == NULL) {
  692. fprintf(stderr, "Erreur dans l'ouverture du fichier log framefile.dat");
  693. exit(-1);
  694. }
  695. /**
  696. * Try to register a client with a NULL name/zero length name...
  697. *
  698. */
  699. output_port1 = jack_port_register(client1, nullportname,
  700. JACK_DEFAULT_AUDIO_TYPE,
  701. JackPortIsOutput, 0);
  702. if (output_port1 == NULL) {
  703. Log("Can't register a port with a NULL portname... ok.\n");
  704. } else {
  705. printf("!!! ERROR !!! Can register a port with a NULL portname !\n");
  706. jack_port_unregister(client1, output_port1);
  707. }
  708. /**
  709. * Register 1 port in order to stress other functions.
  710. *
  711. */
  712. output_port1 = jack_port_register(client1, portname,
  713. JACK_DEFAULT_AUDIO_TYPE,
  714. JackPortIsOutput, 0);
  715. if (output_port1 == NULL) {
  716. printf("!!! ERROR !!! Can't register any port for the client !\n");
  717. exit(1);
  718. }
  719. /**
  720. * Test port type of the just registered port.
  721. *
  722. */
  723. if (strcmp(jack_port_type(output_port1), JACK_DEFAULT_AUDIO_TYPE) != 0) {
  724. printf("!!! ERROR !!! jack_port_type returns an incorrect value!\n");
  725. } else {
  726. Log("Checking jack_port_type()... ok.\n");
  727. }
  728. /**
  729. * Try to register another port with the same name...
  730. *
  731. */
  732. output_port2 = jack_port_register(client1, portname,
  733. JACK_DEFAULT_AUDIO_TYPE,
  734. JackPortIsOutput, 0);
  735. if (output_port2 == NULL) {
  736. Log("Can't register two ports with the same name... ok\n");
  737. } else {
  738. if (strcmp (jack_port_name(output_port1), jack_port_name(output_port2)) == 0) {
  739. printf("!!! ERROR !!! Can register two ports with the same name ! (%px : %s & %px : %s).\n", output_port1, jack_port_name(output_port1), output_port2, jack_port_name(output_port2));
  740. jack_port_unregister(client1, output_port2);
  741. } else {
  742. Log("Can't register two ports with the same name... ok (auto-rename %s into %s).\n", jack_port_name(output_port1), jack_port_name(output_port2));
  743. jack_port_unregister(client1, output_port2);
  744. }
  745. }
  746. /**
  747. * Verify that both port_name and port_short_name return correct results...
  748. *
  749. */
  750. sprintf (portname, "%s:%s", jack_get_client_name(client1), jack_port_short_name(output_port1));
  751. if (strcmp(jack_port_name(output_port1), portname) != 0) {
  752. printf("!!! ERROR !!! functions jack_port_name and/or jack_short_port_name seems to be invalid !\n");
  753. printf("client_name = %s\n short_port_name = %s\n port_name = %s\n", jack_get_client_name(client1), jack_port_short_name(output_port1), jack_port_name(output_port1));
  754. }
  755. /**
  756. * Verify the function port_set_name
  757. *
  758. */
  759. if (jack_port_set_name (output_port1, "renamed-port#") == 0 ) {
  760. if (strcmp(jack_port_name(output_port1), "renamed-port#") == 0) {
  761. printf("!!! ERROR !!! functions jack_port_set_name seems to be invalid !\n");
  762. printf("jack_port_name return '%s' whereas 'renamed-port#' was expected...\n", jack_port_name(output_port1));
  763. } else {
  764. Log("Checking jack_port_set_name()... ok\n");
  765. jack_port_set_name (output_port1, "port");
  766. }
  767. } else {
  768. printf("error : port_set_name function can't be tested...\n");
  769. }
  770. port_callback_reg = 0; // number of port registration received by the callback
  771. /**
  772. * Activate the client
  773. *
  774. */
  775. if (jack_activate(client1) < 0) {
  776. printf ("Fatal error : cannot activate client1\n");
  777. exit(1);
  778. }
  779. /**
  780. * Test if portrename callback have been called.
  781. *
  782. */
  783. jack_port_set_name (output_port1, "renamed-port#");
  784. jack_sleep(1 * 1000);
  785. if (port_rename_clbk == 0)
  786. printf("!!! ERROR !!! Jack_Port_Rename_Callback was not called !!.\n");
  787. /**
  788. * Test if portregistration callback have been called.
  789. *
  790. */
  791. jack_sleep(1 * 1000);
  792. if (1 == port_callback_reg) {
  793. Log("%i ports have been successfully created, and %i callback reg ports have been received... ok\n", 1, port_callback_reg);
  794. } else {
  795. printf("!!! ERROR !!! %i ports have been created, and %i callback reg ports have been received !\n", 1, port_callback_reg);
  796. }
  797. /**
  798. * Test if init callback initThread have been called.
  799. *
  800. */
  801. if (init_clbk == 0)
  802. printf("!!! ERROR !!! Jack_Thread_Init_Callback was not called !!.\n");
  803. jack_sleep(10 * 1000); // test see the clock in the graph at the begining...
  804. /**
  805. * Stress Freewheel mode...
  806. * Try to enter freewheel mode. Check the realtime mode de-activation.
  807. * Check that the number of call of the process callback is greater than in non-freewheel mode.
  808. * Give an approximated speed ratio (number of process call) between the two modes.
  809. * Then return in normal mode.
  810. */
  811. t_error = 0;
  812. activated = 0;
  813. jack_sleep(1 * 1000);
  814. count1 = activated;
  815. Log("Testing activation freewheel mode...\n");
  816. linefw = linecount; // count for better graph reading with gnuplot
  817. jack_set_freewheel(client1, 1);
  818. activated = 0;
  819. jack_sleep(1 * 1000);
  820. count2 = activated;
  821. if (jack_is_realtime(client1) == 0) {
  822. t_error = 0;
  823. } else {
  824. printf("\n!!! ERROR !!! RT mode is always activated while freewheel mode is applied !\n");
  825. t_error = 1;
  826. }
  827. if (activated == 0)
  828. printf("!!! ERROR !!! Freewheel mode doesn't activate audio callback !!\n");
  829. jack_set_freewheel(client1, 0);
  830. jack_sleep(7 * 1000);
  831. if (jack_is_realtime(client1) == 1) {}
  832. else {
  833. printf("\n!!! ERROR !!! freewheel mode fail to reactivate RT mode when exiting !\n");
  834. t_error = 1;
  835. }
  836. if (t_error == 0) {
  837. Log("Freewheel mode appears to work well...\n");
  838. }
  839. if (count1 == 0) {
  840. Log("Audio Callback in 'standard' (non-freewheel) mode seems not to be called...\n");
  841. Log("Ratio speed would be unavailable...\n");
  842. } else {
  843. ratio = (float) ((count2 - count1) / count1);
  844. Log("Approximative speed ratio of freewheel mode = %f : 1.00\n", ratio);
  845. }
  846. /**
  847. * Stress buffer function...
  848. * get current buffer size.
  849. * Try to apply a new buffer size value ( 2 x the precedent buffer size value)
  850. * Then return in previous buffer size mode.
  851. *
  852. */
  853. float factor = 0.5f;
  854. old_buffer_size = jack_get_buffer_size(client1);
  855. Log("Testing BufferSize change & Callback...\n--> Current buffer size : %d.\n", old_buffer_size);
  856. linebuf = linecount;
  857. if (jack_set_buffer_size(client1, (jack_nframes_t)(old_buffer_size * factor)) < 0) {
  858. printf("!!! ERROR !!! jack_set_buffer_size fails !\n");
  859. }
  860. jack_sleep(1 * 1000);
  861. cur_buffer_size = jack_get_buffer_size(client1);
  862. if (abs((old_buffer_size * factor) - cur_buffer_size) > 5) { // Tolerance needed for dummy driver...
  863. printf("!!! ERROR !!! Buffer size has not been changed !\n");
  864. printf("!!! Maybe jack was compiled without the '--enable-resize' flag...\n");
  865. } else {
  866. Log("jack_set_buffer_size() command successfully applied...\n");
  867. }
  868. jack_sleep(3 * 1000);
  869. jack_set_buffer_size(client1, old_buffer_size);
  870. cur_buffer_size = jack_get_buffer_size(client1);
  871. /**
  872. * Test the last regestered port to see if port_is_mine function the right value.
  873. * A second test will be performed later.
  874. * The result will be printed at the end.
  875. *
  876. */
  877. if (jack_port_is_mine(client1, output_port1)) {
  878. is_mine = 1;
  879. } else {
  880. is_mine = 0;
  881. }
  882. /**
  883. * Check that the ID returned by the port_by_name is right.
  884. * (it seems there is a problem here in some jack versions).
  885. *
  886. */
  887. if (output_port1 != jack_port_by_name(client1, jack_port_name(output_port1))) {
  888. printf("!!! ERROR !!! function jack_port_by_name() return bad value !\n");
  889. printf("!!! jack_port_by_name(jack_port_name(_ID_) ) != _ID_returned_at_port_registering ! (%px != %px)\n", jack_port_by_name(client1, jack_port_name(output_port1)), output_port1);
  890. } else {
  891. Log("Checking jack_port_by_name() return value... ok\n");
  892. }
  893. if (NULL != jack_port_by_name(client1, jack_port_short_name(output_port1))) {
  894. printf("!!! ERROR !!! function jack_port_by_name() return a value (%px) while name is incomplete !\n", jack_port_by_name(client1, jack_port_short_name(output_port1)));
  895. } else {
  896. Log("Checking jack_port_by_name() with bad argument... ok (returned id 0)\n");
  897. }
  898. /**
  899. * remove the output port previously created
  900. * no more ports should subsist here for our client.
  901. *
  902. */
  903. if (jack_port_unregister(client1, output_port1) != 0) {
  904. printf("!!! ERROR !!! while unregistering port %s.\n", jack_port_name(output_port1));
  905. }
  906. /**
  907. * list all in ports
  908. *
  909. */
  910. inports = jack_get_ports(client1, NULL, NULL, 0);
  911. /**
  912. * Test the first PHY (physical) connection to see if it's "mine".
  913. * and report the result in the test that began before.
  914. * The result is printed later.
  915. *
  916. */
  917. if (jack_port_is_mine(client1, jack_port_by_name(client1, inports[0]))) {
  918. is_mine = 0;
  919. }
  920. /**
  921. * List all devices' flags and print them...
  922. *
  923. */
  924. Log("\nTry functions jack_get_ports, jack_port_flag & jack_port_by_name to list PHY devices...\n");
  925. Log("-----------------------------------------------------------\n");
  926. Log("---------------------------DEVICES-------------------------\n");
  927. Log("-----------------------------------------------------------\n");
  928. a = 0;
  929. while (inports[a] != NULL) {
  930. flag = jack_port_flags(jack_port_by_name(client1, inports[a]) );
  931. Log(" * %s (id : %i)\n", inports[a], jack_port_by_name(client1, inports[a]));
  932. Log(" (");
  933. if (flag & JackPortIsInput)
  934. Log("JackPortIsInput ");
  935. if (flag & JackPortIsOutput)
  936. Log("JackPortIsOutput ");
  937. if (flag & JackPortIsPhysical)
  938. Log("JackPortIsPhysical ");
  939. if (flag & JackPortCanMonitor)
  940. Log("JackPortCanMonitor ");
  941. if (flag & JackPortIsTerminal)
  942. Log("JackPortIsTerminal ");
  943. Log(")\n\n");
  944. a++;
  945. }
  946. Log("-----------------------------------------------------------\n\n");
  947. /**
  948. * list all PHY in/out ports...
  949. * This list will be used later many times.
  950. *
  951. */
  952. outports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsOutput);
  953. inports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
  954. if (outports == NULL) {
  955. printf("!!! WARNING !!! no physical capture ports founded !\n");
  956. }
  957. if (inports == NULL) {
  958. printf("!!! WARNING !!! no physical output ports founded !\n");
  959. }
  960. /**
  961. * Brute test : try to create as many ports as possible.
  962. * It stops when jack returns an error.
  963. * Then try to connect each port to physical entry...
  964. * Check also that graph reorder callback is called.
  965. *
  966. */
  967. Log("Registering as many ports as possible and connect them to physical entries...\n");
  968. lineports = linecount;
  969. t_error = 0;
  970. i = 0; // number of couple 'input-ouput'
  971. j = 0; // number of ports created
  972. port_callback_reg = 0; // number of port registration received by the callback
  973. reorder = 0; // number of graph reorder callback activation
  974. test_link = 0 ; // Test the "overconnect" function only one time
  975. while (t_error == 0) {
  976. sprintf (portname, "input_%d", i);
  977. input_port1 = jack_port_register(client1, portname,
  978. JACK_DEFAULT_AUDIO_TYPE,
  979. JackPortIsInput, 0);
  980. j++;
  981. if (input_port1 == NULL) {
  982. j--;
  983. t_error = 1;
  984. } else {
  985. // Connect created input to PHY output
  986. a = 0;
  987. while (outports[a] != NULL) {
  988. if (jack_connect(client1, outports[a], jack_port_name(input_port1))) {
  989. printf ("error : cannot connect input PHY port to client port %s\n", jack_port_name(input_port1));
  990. } else {
  991. // printf ("input PHY port %s connected to client port %s\n", outports[a], jack_port_name(input_port1));
  992. }
  993. a++;
  994. }
  995. // Try one time to "overconnect" 2 ports (the latest created)...
  996. if (test_link == 0) {
  997. if (jack_connect(client1, outports[a - 1], jack_port_name(input_port1)) == EEXIST) {
  998. // cannot over-connect input PHY port to client port. ok.
  999. test_link = 1;
  1000. }
  1001. }
  1002. }
  1003. sprintf(portname, "output_%d", i);
  1004. output_port1 = jack_port_register(client1, portname,
  1005. JACK_DEFAULT_AUDIO_TYPE,
  1006. JackPortIsOutput, 0);
  1007. j++;
  1008. if (output_port1 == NULL) {
  1009. t_error = 1;
  1010. j--;
  1011. } else {
  1012. // Connect created input to PHY output
  1013. a = 0;
  1014. while (inports[a] != NULL) {
  1015. if (jack_connect(client1, jack_port_name(output_port1), inports[a])) {
  1016. printf ("error : cannot connect input PHY port %s to client port %s\n", inports[a], jack_port_name(output_port1));
  1017. } else {
  1018. // output PHY port %s connected to client port. ok.
  1019. }
  1020. a++;
  1021. }
  1022. // Try one time to "overconnect" 2 ports (the latest created)...
  1023. if (test_link == 0) {
  1024. if (jack_connect(client1, jack_port_name(output_port1), inports[a - 1]) == EEXIST) {
  1025. // cannot over-connect output PHY port to client port. ok.
  1026. test_link = 1;
  1027. }
  1028. }
  1029. }
  1030. i++;
  1031. }
  1032. jack_sleep(1 * 1000); // To hope all port registration and reorder callback have been received...
  1033. // Check port registration callback
  1034. if (j == port_callback_reg) {
  1035. Log("%i ports have been successfully created, and %i callback reg ports have been received... ok\n", j, port_callback_reg);
  1036. } else {
  1037. printf("!!! ERROR !!! %i ports have been created, and %i callback reg ports have been received !\n", j, port_callback_reg);
  1038. }
  1039. if (reorder == (2 * j)) {
  1040. Log("%i graph reorder callback have been received... ok\n", reorder);
  1041. } else {
  1042. printf("!!! ERROR !!! %i graph reorder callback have been received (maybe non-valid value)...\n", reorder);
  1043. }
  1044. /**
  1045. * print basic test connection functions result ...
  1046. * over-connected means here that we try to connect 2 ports that are already connected.
  1047. *
  1048. */
  1049. if (test_link) {
  1050. Log("Jack links can't be 'over-connected'... ok\n");
  1051. } else {
  1052. printf("!!! ERROR !!! Jack links can be 'over-connected'...\n");
  1053. }
  1054. /**
  1055. * Print the result of the two jack_is_mine test.
  1056. *
  1057. */
  1058. if (is_mine == 1) {
  1059. Log("Checking jack_port_is_mine()... ok\n");
  1060. } else {
  1061. printf("!!! ERROR !!! jack_port_is_mine() function seems to send non-valid datas !\n");
  1062. }
  1063. /**
  1064. * Free the array of the physical input and ouput ports.
  1065. * (as mentionned in the doc of jack_get_ports)
  1066. *
  1067. */
  1068. jack_free(inports);
  1069. jack_free(outports);
  1070. /**
  1071. * Try to "reactivate" the client whereas it's already activated...
  1072. *
  1073. */
  1074. if (jack_activate(client1) < 0) {
  1075. printf("!!! ERROR !!! Cannot activate client1 a second time...\n");
  1076. exit(1);
  1077. } else {
  1078. Log("jackd server accept client.jack_activate() re-activation (while client was already activated).\n");
  1079. }
  1080. /**
  1081. * Deregister all ports previously created.
  1082. *
  1083. */
  1084. port_callback_reg = 0; // to check registration callback
  1085. Log("Deregistering all ports of the client...\n");
  1086. inports = jack_get_ports(client1, NULL, NULL, 0);
  1087. a = 0;
  1088. while (inports[a] != NULL) {
  1089. flag = jack_port_flags(jack_port_by_name(client1, inports[a]));
  1090. input_port1 = jack_port_by_name(client1, inports[a]);
  1091. if (jack_port_is_mine(client1, input_port1)) {
  1092. if (jack_port_unregister(client1, input_port1) != 0) {
  1093. printf("!!! ERROR !!! while unregistering port %s.\n", jack_port_name(output_port1));
  1094. }
  1095. }
  1096. a++;
  1097. }
  1098. // Check port registration callback again
  1099. if (j == port_callback_reg) {
  1100. Log("%i ports have been successfully created, and %i callback reg ports have been received... ok\n", j, port_callback_reg);
  1101. } else {
  1102. printf("!!! ERROR !!! %i ports have been created, and %i callback reg ports have been received !\n", j, port_callback_reg);
  1103. }
  1104. jack_free(inports); // free array of ports (as mentionned in the doc of jack_get_ports)
  1105. /**
  1106. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1107. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1108. Open a new client (second one) to test some other things...
  1109. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1110. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1111. */
  1112. Log("\n\n----------------------------------------------------------------------\n");
  1113. Log("Starting second new client 'jack_test_#2'...\n");
  1114. /* open a client connection to the JACK server */
  1115. client_name2 = "jack_test_#2";
  1116. linecl2 = linecount; // reminders for graph analysis
  1117. client2 = jack_client_new(client_name2);
  1118. if (client2 == NULL) {
  1119. fprintf(stderr, "jack_client_new() failed for %s.\n"
  1120. "status = 0x%2.0x\n", client_name2, status);
  1121. if (status & JackServerFailed) {
  1122. fprintf(stderr, "Unable to connect client2 to JACK server\n");
  1123. }
  1124. exit(1);
  1125. }
  1126. // Check client registration callback
  1127. jack_sleep(1000);
  1128. if (client_register == 0)
  1129. printf("!!! ERROR !!! Client registration callback not called!\n");
  1130. /**
  1131. * Register callback for this client.
  1132. * Callbacks are the same as the first client for most of them, excepted for process audio callback.
  1133. *
  1134. */
  1135. jack_set_port_registration_callback(client2, Jack_Port_Register, 0);
  1136. jack_set_process_callback(client2, process2, 0);
  1137. jack_on_shutdown(client2, jack_shutdown, 0);
  1138. /**
  1139. * Register one input and one output for each client.
  1140. *
  1141. */
  1142. Log("registering 1 input/output ports for each client...\n");
  1143. output_port1 = jack_port_register(client1, "out1",
  1144. JACK_DEFAULT_AUDIO_TYPE,
  1145. JackPortIsOutput, 0);
  1146. output_port2 = jack_port_register(client2, "out2",
  1147. JACK_DEFAULT_AUDIO_TYPE,
  1148. JackPortIsOutput, 0);
  1149. input_port1 = jack_port_register(client1, "in1",
  1150. JACK_DEFAULT_AUDIO_TYPE,
  1151. JackPortIsInput, 0);
  1152. input_port2 = jack_port_register(client2, "in2",
  1153. JACK_DEFAULT_AUDIO_TYPE,
  1154. JackPortIsInput, 0);
  1155. if ((output_port1 == NULL) || (output_port2 == NULL) || (input_port1 == NULL) || (input_port2 == NULL)) {
  1156. printf("!!! ERROR !!! Unable to register ports...\n");
  1157. }
  1158. /**
  1159. * Set each process mode to idle and activate client2
  1160. *
  1161. */
  1162. process2_activated = -1;
  1163. process1_activated = -1;
  1164. if (jack_activate(client2) < 0) {
  1165. printf ("Fatal error : cannot activate client2\n");
  1166. exit (1);
  1167. }
  1168. /**
  1169. * Connect the two clients and check that all connections are well-done.
  1170. *
  1171. */
  1172. Log("Testing connections functions between clients...\n");
  1173. if (jack_connect(client1, jack_port_name(output_port1), jack_port_name(input_port2)) != 0) {
  1174. printf("!!! ERROR !!! while client1 intenting to connect ports...\n");
  1175. }
  1176. if (jack_connect(client2, jack_port_name(output_port2), jack_port_name(input_port1)) != 0) {
  1177. printf("!!! ERROR !!! while client2 intenting to connect ports...\n");
  1178. }
  1179. if (jack_connect(client1, jack_port_name(output_port1), jack_port_name(input_port1)) != 0) {
  1180. printf("!!! ERROR !!! while client1 intenting to connect ports...\n");
  1181. }
  1182. /**
  1183. * Test the port_connected function...
  1184. *
  1185. */
  1186. if ((jack_port_connected(output_port1) == jack_port_connected(input_port1)) &&
  1187. (jack_port_connected(output_port2) == jack_port_connected(input_port2)) &&
  1188. (jack_port_connected(output_port2) == 1) &&
  1189. (jack_port_connected(output_port1) == 2)
  1190. ) {
  1191. Log("Checking jack_port_connected()... ok.\n");
  1192. } else {
  1193. printf("!!! ERROR !!! function jack_port_connected() return a bad value !\n");
  1194. printf("jack_port_connected(output_port1) %d\n", jack_port_connected(output_port1));
  1195. printf("jack_port_connected(output_port2) %d\n", jack_port_connected(output_port2));
  1196. printf("jack_port_connected(input_port1) %d\n", jack_port_connected(input_port1));
  1197. printf("jack_port_connected(input_port2) %d\n", jack_port_connected(input_port2));
  1198. }
  1199. /**
  1200. * Test a new time the port_by_name function...(now we are in multi-client mode)
  1201. *
  1202. */
  1203. Log("Testing again jack_port_by_name...\n");
  1204. if (output_port1 != jack_port_by_name(client1, jack_port_name(output_port1))) {
  1205. printf("!!! ERROR !!! function jack_port_by_name() return bad value in a multi-client application!\n");
  1206. printf("!!! jack_port_by_name(jack_port_name(_ID_) ) != _ID_ in multiclient application.\n");
  1207. } else {
  1208. Log("Checking jack_port_by_name() function with a multi-client application... ok\n");
  1209. }
  1210. /**
  1211. * Test the port_connected_to function...
  1212. *
  1213. */
  1214. if ((jack_port_connected_to (output_port1, jack_port_name(input_port2))) &&
  1215. (!(jack_port_connected_to (output_port2, jack_port_name(input_port2))))) {
  1216. Log("checking jack_port_connected_to()... ok\n");
  1217. } else {
  1218. printf("!!! ERROR !!! jack_port_connected_to() return bad value !\n");
  1219. }
  1220. /**
  1221. * Test the port_get_connections & port_get_all_connections functions...
  1222. *
  1223. */
  1224. Log("Testing jack_port_get_connections and jack_port_get_all_connections...\n");
  1225. a = 0;
  1226. t_error = 0;
  1227. connexions1 = jack_port_get_connections (output_port1);
  1228. connexions2 = jack_port_get_all_connections(client1, output_port1);
  1229. if ((connexions1 == NULL) || (connexions2 == NULL)) {
  1230. printf("!!! ERROR !!! port_get_connexions or port_get_all_connexions return a NULL pointer !\n");
  1231. } else {
  1232. while ((connexions1[a] != NULL) && (connexions2[a] != NULL) && (t_error == 0)) {
  1233. t_error = strcmp(connexions1[a], connexions2[a]);
  1234. a++;
  1235. }
  1236. if (t_error == 0) {
  1237. Log("Checking jack_port_get_connections Vs jack_port_get_all_connections... ok\n");
  1238. } else {
  1239. printf("!!! ERROR !!! while checking jack_port_get_connections Vs jack_port_get_all_connections...\n");
  1240. }
  1241. }
  1242. a = 0;
  1243. t_error = 0;
  1244. inports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
  1245. connexions1 = NULL;
  1246. assert(inports != NULL);
  1247. if (inports[0] != NULL) {
  1248. connexions1 = jack_port_get_connections (jack_port_by_name(client1, inports[0]));
  1249. connexions2 = jack_port_get_all_connections(client1, jack_port_by_name(client1, inports[0]));
  1250. }
  1251. jack_free (inports);
  1252. if (connexions1 == NULL) {
  1253. Log("checking jack_port_get_connections() for external client... ok\n");
  1254. } else {
  1255. while ((connexions1[a] != NULL) && (connexions2[a] != NULL) && (t_error == 0)) {
  1256. t_error = strcmp(connexions1[a], connexions2[a]);
  1257. a++;
  1258. }
  1259. }
  1260. if (t_error == 0) {
  1261. Log("Checking jack_port_get_connections() Vs jack_port_get_all_connections() on PHY port... ok\n");
  1262. } else {
  1263. printf("!!! ERROR !!! while checking jack_port_get_connections() Vs jack_port_get_all_connections() on PHY port...\n");
  1264. }
  1265. if (jack_disconnect(client1, jack_port_name(output_port1), jack_port_name(input_port1)) != 0) {
  1266. printf("!!! ERROR !!! while client1 intenting to disconnect ports...\n");
  1267. }
  1268. if (jack_disconnect(client1, jack_port_name(output_port2), jack_port_name(input_port1)) != 0) {
  1269. printf("!!! ERROR !!! while client1 intenting to disconnect ports...\n");
  1270. }
  1271. // No links should subsist now...
  1272. /**
  1273. * Checking data connexion
  1274. * establishing a link between client1.out1 --> client2.in2
  1275. * Send the signal1 test on out1. Record the result into signal2. (see process functions).
  1276. ---------------------------------------------------------------------------*/
  1277. Log("Testing connections datas between clients...\n");
  1278. jack_connect(client2, jack_port_name(output_port1), jack_port_name(input_port2) );
  1279. process2_activated = -1;
  1280. process1_activated = -1;
  1281. Log("process 2 : idle mode...\n");
  1282. Log("Sending datas...");
  1283. index1 = 0;
  1284. index2 = 0;
  1285. process1_activated = 1; // We start emitting first.
  1286. process2_activated = 1; // So record begin at least when we just begin to emitt the signal, else at next call of process with
  1287. // nframe = jack buffersize shifting.
  1288. while (process2_activated == 1) {
  1289. jack_sleep(1 * 1000);
  1290. Log(".");
  1291. }
  1292. index2 = 0;
  1293. Log("\nAnalysing datas...\n"); // search the first occurence of the first element of the reference signal in the recorded signal
  1294. while (signal2[index2] != signal1[1] ) {
  1295. index2++;
  1296. if (index2 == 95999) {
  1297. printf("!!! ERROR !!! Data not found in first connexion data check!\n");
  1298. break;
  1299. }
  1300. }
  1301. index1 = index2;
  1302. Log("Data founded at offset %i.\n", index2);
  1303. // And now we founded were the recorded data are, we can see if the two signals matches...
  1304. while ( (signal2[index2] == signal1[index2 - index1 + 1]) || (index2 == 95999) || ((index2 - index1 + 1) == 47999) ) {
  1305. index2++;
  1306. }
  1307. Log("Checking difference between datas... %i have the same value...\n", index2 - index1);
  1308. if ((index2 - index1) == 48000) {
  1309. Log("Data received are valid...\n");
  1310. } else {
  1311. printf("!!! ERROR !!! data transmission seems not to be valid in first connexion data check!\n");
  1312. }
  1313. if (jack_disconnect(client1, jack_port_name(output_port1), jack_port_name(input_port2) ) != 0)
  1314. // no more connection between ports exist now...
  1315. {
  1316. printf("Error while establishing new connexion (disconnect).\n");
  1317. }
  1318. /**
  1319. * Test TIE MODE
  1320. * (This mode seems to be problematic in standard jack version 0.100. It seems that nobody
  1321. * is used to apply this mode because the tie mode doesn't work at all. A patch seems difficult to produce
  1322. * in this version of jack. Tie mode work well in MP version.)
  1323. * Test some basic thinks (tie with 2 differents client, tie non-owned ports...)
  1324. * Tie client1.in1 and client1.out1 ports, and make some data test to check the validity of the tie.
  1325. *
  1326. */
  1327. Log("Testing tie mode...\n");
  1328. if (jack_port_tie(input_port1, output_port2) != 0) {
  1329. Log("not possible to tie two ports from two differents clients... ok\n");
  1330. } else {
  1331. printf("!!! ERROR !!! port_tie has allowed a connexion between two differents clients !\n");
  1332. jack_port_untie(output_port2);
  1333. }
  1334. Log("Testing connections datas in tie mode...\n");
  1335. int g;
  1336. for (g = 0; g < 96000; g++)
  1337. signal2[g] = 0.0;
  1338. // Create a loop (emit test) client2.out2----client.in1--tie--client1.out1-----client2.in1 (receive test)
  1339. if (jack_port_tie(input_port1, output_port1) != 0) {
  1340. printf("Unable to tie... fatal error : data test will not be performed on tie mode !!\n");
  1341. } else { // begin of tie
  1342. if (jack_connect(client1, jack_port_name(output_port1), jack_port_name(input_port2)) != 0) {
  1343. printf("!!! ERROR !!! while client1 intenting to connect ports...\n");
  1344. }
  1345. if (jack_connect(client1, jack_port_name(output_port2), jack_port_name(input_port1)) != 0) {
  1346. printf("!!! ERROR !!! while client1 intenting to connect ports...\n");
  1347. }
  1348. process1_activated = -1;
  1349. process2_activated = -1;
  1350. // We can manualy check here that the tie is effective.
  1351. // ie : playing a wav with a client, connecting ports manualy with qjackctl, and listen...
  1352. // printf("manual test\n");
  1353. // jack_sleep(50);
  1354. // printf("end of manual test\n");
  1355. index1 = 0;
  1356. index2 = 0;
  1357. process1_activated = -1;
  1358. process2_activated = 2;
  1359. Log("Sending datas...");
  1360. while (process2_activated == 2) {
  1361. jack_sleep(1 * 1000);
  1362. Log(".");
  1363. }
  1364. process1_activated = -1;
  1365. process2_activated = -1;
  1366. index2 = 0;
  1367. Log("\nAnalysing datas...\n");
  1368. // We must find at least 2 identical values to ensure we are at the right place in the siusoidal array...
  1369. while (!((signal2[index2] == signal1[1]) && (signal2[index2 + 1] == signal1[2]))) {
  1370. index2++;
  1371. if (index2 == 95999) {
  1372. printf("!!! ERROR !!! Data not found in connexion check of tie mode!\n");
  1373. break;
  1374. }
  1375. }
  1376. index1 = index2;
  1377. Log("Tie mode : Data founded at offset %i.\n", index2);
  1378. while (signal2[index2] == signal1[index2 - index1 + 1]) {
  1379. index2++;
  1380. if ((index2 == 95999) || ((index2 - index1 + 1) == 47999)) {
  1381. break;
  1382. }
  1383. }
  1384. Log("Checking difference between datas... %i have the same value...\n", index2 - index1);
  1385. if ((index2 - index1) > 47995) {
  1386. Log("Data received in tie mode are valid...\n");
  1387. } else {
  1388. // in tie mode, the buffers adress should be the same for the two tied ports.
  1389. printf("!!! ERROR !!! data transmission seems not to be valid !\n");
  1390. printf("Links topology : (emitt) client2.out2 ----> client1.in1--(tie)--client1.out1----->client2.in2 (recive)\n");
  1391. printf(" port_name : Port_adress \n");
  1392. printf(" output_port1 : %px\n", jack_port_get_buffer(output_port1, cur_buffer_size));
  1393. printf(" input_port2 : %px\n", jack_port_get_buffer(input_port2, cur_buffer_size));
  1394. printf(" output_port2 : %px\n", jack_port_get_buffer(output_port2, cur_buffer_size));
  1395. printf(" input_port1 : %px\n", jack_port_get_buffer(input_port1, cur_buffer_size));
  1396. }
  1397. jack_port_untie(output_port1);
  1398. jack_port_disconnect(client1, output_port2);
  1399. jack_port_disconnect(client1, output_port1);
  1400. } //end of tie
  1401. /**
  1402. * Testing SUMMATION CAPABILITIES OF JACK CONNECTIONS
  1403. *
  1404. * In a short test, we just check a simple summation in jack.
  1405. * A first client(client1) send two signal in phase opposition
  1406. * A second client(client2) record the summation at one of his port
  1407. * So, the result must be zero...
  1408. * See process1 for details about steps of this test
  1409. *
  1410. */
  1411. // fprintf(file, "Sum test\n");
  1412. Log("Checking summation capabilities of patching...\n");
  1413. output_port1b = jack_port_register(client1, "out1b",
  1414. JACK_DEFAULT_AUDIO_TYPE,
  1415. JackPortIsOutput, 0);
  1416. jack_connect(client2, jack_port_name(output_port1), jack_port_name(input_port2));
  1417. jack_connect(client2, jack_port_name(output_port1b), jack_port_name(input_port2));
  1418. process1_activated = 3;
  1419. process2_activated = -1;
  1420. for (g = 0; g < 96000; g++)
  1421. signal2[g] = 0.0;
  1422. index1 = 0;
  1423. index2 = 0;
  1424. Log("Sending datas...");
  1425. process2_activated = 3;
  1426. while (process2_activated == 3) {
  1427. jack_sleep(1 * 1000);
  1428. Log(".");
  1429. }
  1430. process1_activated = -1;
  1431. process2_activated = -1;
  1432. index2 = 0;
  1433. Log("\nAnalysing datas...\n"); // same idea as above, with first data check...
  1434. while (!((signal2[index2] == 0.0 ) && (signal2[(index2 + 1)] == 0.0 ))) {
  1435. index2++;
  1436. if (index2 == 95999) {
  1437. printf("!!! ERROR !!! Data not found in summation check!\n");
  1438. break;
  1439. }
  1440. }
  1441. index1 = index2;
  1442. Log("Data founded at offset %i.\n", index2);
  1443. while ( signal2[index2] == 0.0 ) {
  1444. index2++;
  1445. if ((index2 > 95998) || ((index2 - index1 + 1) > 47998)) {
  1446. break;
  1447. }
  1448. }
  1449. Log("Checking difference between datas...\n");
  1450. if ((index2 - index1) > 47996) {
  1451. Log("Data mixed received are valid...\nSummation is well done.\n");
  1452. } else {
  1453. printf("!!! ERROR !!! data transmission / summation seems not to be valid !\n");
  1454. }
  1455. jack_port_disconnect(client1, output_port1);
  1456. jack_port_disconnect(client1, output_port1b);
  1457. jack_port_unregister(client1, output_port1b);
  1458. if (jack_port_name(output_port1b) != NULL ) {
  1459. printf("!!! WARNING !!! port_name return something while the port have been unregistered !\n");
  1460. printf("!!! Name of unregistered port : %s !\n", jack_port_name(output_port1b));
  1461. } else {
  1462. Log("Checking jack_port_name() with a non valid port... ok\n");
  1463. }
  1464. if (jack_port_set_name(output_port1b, "new_name") == 0 ) {
  1465. printf("!!! WARNING !!! An unregistered port can be renamed successfully !\n");
  1466. } else {
  1467. Log("Checking renaming of an unregistered port... ok\n");
  1468. }
  1469. inports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
  1470. if (jack_port_set_name(jack_port_by_name(client1, inports[0]), "new_name") == 0 ) {
  1471. printf("!!! WARNING !!! A PHYSICAL port can be renamed successfully !\n");
  1472. } else {
  1473. Log("Checking renaming of an unregistered port... ok\n");
  1474. }
  1475. jack_free (inports);
  1476. /**
  1477. * Checking latency issues
  1478. * here are simple latency check
  1479. * We simply check that the value returned by jack seems ok
  1480. * Latency compensation is a difficult point.
  1481. * Actually, jack is not able to see "thru" client to build a full latency chain.
  1482. * Ardour use theses informations to do internally his compensations.
  1483. *
  1484. * 3 test are done : one with no connections between client, one with a serial connection, and one with parallel connection
  1485. */
  1486. Log("Checking about latency functions...\n");
  1487. t_error = 0;
  1488. jack_recompute_total_latencies(client1);
  1489. Log("jack_recompute_total_latencies...\n");
  1490. if ((jack_port_get_latency (output_port1) != 0) ||
  1491. (jack_port_get_total_latency(client1, output_port1) != 0) ) {
  1492. t_error = 1;
  1493. printf("!!! ERROR !!! default latency of a non-PHY device is not set to zero !\n");
  1494. }
  1495. inports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
  1496. outports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsOutput);
  1497. if (inports[0] != NULL) {
  1498. output_ext_latency = jack_port_get_latency (jack_port_by_name(client1, inports[0])); // from client to out driver (which has "inputs" ports..)
  1499. input_ext_latency = jack_port_get_latency (jack_port_by_name(client1, outports[0])); // from in driver (which has "output" ports..) to client
  1500. if (output_ext_latency != jack_port_get_total_latency(client1, jack_port_by_name(client1, inports[0]))) {
  1501. t_error = 1;
  1502. printf("!!! ERROR !!! get_latency & get_all_latency for a PHY device (unconnected) didn't return the same value !\n");
  1503. }
  1504. Log("Checking a serial model with 2 clients...\n");
  1505. jack_connect(client1, jack_port_name(output_port1), jack_port_name(input_port2));
  1506. jack_connect(client1, outports[0], jack_port_name(input_port1));
  1507. jack_connect(client2, jack_port_name(output_port2), inports[0]);
  1508. jack_port_set_latency(output_port2, 256);
  1509. jack_recompute_total_latencies(client1);
  1510. if ((jack_port_get_latency (output_port1) != 0) ||
  1511. (jack_port_get_total_latency(client1, output_port1) != 0) ||
  1512. (jack_port_get_latency (jack_port_by_name(client1, inports[0])) != (output_ext_latency)) ||
  1513. (jack_port_get_total_latency(client1, jack_port_by_name(client1, inports[0])) != (output_ext_latency + 256)) ||
  1514. (jack_port_get_total_latency(client1, output_port2) != (output_ext_latency + 256)) ||
  1515. (jack_port_get_total_latency(client1, input_port2) != 0) ||
  1516. (jack_port_get_total_latency(client1, input_port1) != input_ext_latency) ||
  1517. (jack_port_get_latency (jack_port_by_name(client1, outports[0])) != input_ext_latency) ||
  1518. (jack_port_get_total_latency(client1, jack_port_by_name(client1, outports[0])) != input_ext_latency)
  1519. ) {
  1520. printf("!!! WARNING !!! get_latency functions may have a problem : bad value returned !\n");
  1521. printf("!!! get_latency(output_port1) : %i (must be 0)\n", jack_port_get_latency(output_port1));
  1522. printf("!!! get_total_latency(output_port1) : %i (must be 0)\n", jack_port_get_total_latency(client1, output_port1));
  1523. printf("!!! get_latency(PHY[0]) : %i (must be external latency : %i)\n", jack_port_get_latency(jack_port_by_name(client1, inports[0])), output_ext_latency);
  1524. printf("!!! get_total_latency(PHY[0]) : %i (must be %i)\n", jack_port_get_total_latency(client1, jack_port_by_name(client1, inports[0])) , (output_ext_latency + 256));
  1525. printf("!!! get_total_latency(output_port2) : %i (must be %i)\n", jack_port_get_total_latency(client1, output_port2), (output_ext_latency + 256));
  1526. printf("!!! get_total_latency(input_port2) : %i (must be 0)\n", jack_port_get_total_latency(client1, input_port2));
  1527. printf("!!! get_total_latency(input_port1) : %i (must be %i)\n", jack_port_get_total_latency(client1, input_port1), input_ext_latency);
  1528. printf("!!! get_latency(PHY[0]) : %i (must be %i)\n", jack_port_get_latency(jack_port_by_name(client1, outports[0])), input_ext_latency);
  1529. printf("!!! get_total_latency(PHY[0]) : %i (must be %i)\n", jack_port_get_total_latency(client1, jack_port_by_name(client1, outports[0])), input_ext_latency);
  1530. } else {
  1531. Log("get_latency & get_total_latency seems quite ok...\n");
  1532. }
  1533. jack_port_disconnect(client1, output_port1);
  1534. jack_port_disconnect(client1, output_port2);
  1535. jack_port_disconnect(client1, input_port1);
  1536. jack_port_disconnect(client1, input_port2);
  1537. Log("Checking a parallel model with 2 clients...\n");
  1538. jack_connect(client2, outports[0], jack_port_name(input_port1));
  1539. jack_connect(client2, outports[0], jack_port_name(input_port2));
  1540. jack_connect(client2, jack_port_name(output_port1), inports[0]);
  1541. jack_connect(client2, jack_port_name(output_port2), inports[0]);
  1542. jack_port_set_latency(output_port1, 256);
  1543. jack_port_set_latency(output_port2, 512);
  1544. jack_recompute_total_latencies(client1);
  1545. if ((jack_port_get_latency(output_port1) != 256 ) ||
  1546. (jack_port_get_total_latency(client1, output_port1) != (256 + output_ext_latency)) ||
  1547. (jack_port_get_latency(output_port2) != 512) ||
  1548. (jack_port_get_total_latency(client1, output_port2) != (512 + output_ext_latency)) ||
  1549. (jack_port_get_latency(jack_port_by_name(client1, inports[0])) != output_ext_latency) ||
  1550. (jack_port_get_total_latency(client1, jack_port_by_name(client1, inports[0])) != (512 + output_ext_latency))
  1551. ) {
  1552. printf("!!! WARNING !!! get_latency functions may have a problem : bad value returned !\n");
  1553. printf("!!! get_latency(output_port1) : %i (must be 256)\n", jack_port_get_latency(output_port1));
  1554. printf("!!! get_total_latency(output_port1) : %i (must be 256 + output_ext_latency)\n", jack_port_get_total_latency(client1, output_port1));
  1555. printf("!!! get_latency(output_port2) : %i (must 512)\n", jack_port_get_latency(output_port2));
  1556. printf("!!! get_total_latency(output_port2) : %i (must 512 + output_ext_latency)\n", jack_port_get_total_latency(client1, output_port2));
  1557. printf("!!! get_latency(inports[0])) : %i (must output_ext_latency)\n", jack_port_get_latency(jack_port_by_name(client1, inports[0])));
  1558. printf("!!! get_total_latency(inports[0]) : %i (must 512 + output_ext_latency)\n", jack_port_get_total_latency(client1, jack_port_by_name(client1, inports[0])));
  1559. } else {
  1560. Log("get_latency & get_total_latency seems quite ok...\n");
  1561. }
  1562. } else {
  1563. printf("No physical port founded : not able to test latency functions...");
  1564. }
  1565. jack_port_disconnect(client1, input_port1);
  1566. jack_port_disconnect(client1, input_port2);
  1567. jack_port_disconnect(client1, output_port1);
  1568. jack_port_disconnect(client1, output_port2);
  1569. jack_sleep(1000);
  1570. jack_free(inports);
  1571. jack_free(outports);
  1572. /**
  1573. * Checking transport API.
  1574. * Simple transport test.
  1575. * Check a transport start with a "slow" client, simulating a delay around 1 sec before becoming ready.
  1576. *
  1577. */
  1578. Log("-----------------------------------------------------------\n");
  1579. Log("---------------------------TRANSPORT-----------------------\n");
  1580. Log("-----------------------------------------------------------\n");
  1581. lineports = linecount;
  1582. if (transport_mode) {
  1583. int wait_count;
  1584. ts = jack_transport_query(client1, &pos);
  1585. if (ts == JackTransportStopped) {
  1586. Log("Transport is stopped...\n");
  1587. } else {
  1588. jack_transport_stop(client1);
  1589. Log("Transport state : %i\n", ts);
  1590. }
  1591. if (jack_set_sync_callback(client2, Jack_Sync_Callback, 0) != 0)
  1592. printf("error while calling set_sync_callback...\n");
  1593. Log("starting transport...\n");
  1594. starting_state = 1; // Simulate starting state
  1595. jack_transport_start(client1);
  1596. // Wait until sync callback is called
  1597. while (!(sync_called)) {
  1598. jack_sleep(1 * 1000);
  1599. }
  1600. // Wait untill rolling : simulate sync time out
  1601. Log("Simulate a slow-sync client exceeding the time-out\n");
  1602. wait_count = 0;
  1603. do {
  1604. jack_sleep(100); // Wait 100 ms each cycle
  1605. wait_count++;
  1606. if (wait_count == 100) {
  1607. Log("!!! ERROR !!! max time-out exceedeed : sync time-out does not work correctly\n");
  1608. break;
  1609. }
  1610. ts = jack_transport_query(client2, &pos);
  1611. Log("Waiting....pos = %ld\n", pos.frame);
  1612. display_transport_state();
  1613. } while (ts != JackTransportRolling);
  1614. Log("Sync callback have been called %i times.\n", sync_called);
  1615. jack_transport_stop(client1);
  1616. // Wait until stopped
  1617. ts = jack_transport_query(client2, &pos);
  1618. while (ts != JackTransportStopped) {
  1619. jack_sleep(1 * 1000);
  1620. ts = jack_transport_query(client2, &pos);
  1621. }
  1622. // Simulate starting a slow-sync client that rolls after 0.5 sec
  1623. Log("Simulate a slow-sync client that needs 0.5 sec to start\n");
  1624. sync_called = 0;
  1625. wait_count = 0;
  1626. starting_state = 1; // Simulate starting state
  1627. Log("Starting transport...\n");
  1628. jack_transport_start(client1);
  1629. display_transport_state();
  1630. Log("Waiting 0.5 sec...\n");
  1631. jack_sleep(500);
  1632. starting_state = 0; // Simulate end of starting state after 0.5 sec
  1633. // Wait untill rolling
  1634. ts = jack_transport_query(client2, &pos);
  1635. while (ts != JackTransportRolling) {
  1636. jack_sleep(100); // Wait 100 ms each cycle
  1637. wait_count++;
  1638. if (wait_count == 10) {
  1639. Log("!!! ERROR !!! starting a slow-sync client does not work correctly\n");
  1640. break;
  1641. }
  1642. ts = jack_transport_query(client2, &pos);
  1643. }
  1644. if (sync_called == 0)
  1645. Log("!!! ERROR !!! starting a slow-sync client does not work correctly\n");
  1646. Log("Sync callback have been called %i times.\n", sync_called);
  1647. display_transport_state();
  1648. // Test jack_transport_locate while rolling
  1649. Log("Test jack_transport_locate while rolling\n");
  1650. ts = jack_transport_query(client2, &pos);
  1651. Log("Transport current frame = %ld\n", pos.frame);
  1652. jack_nframes_t cur_frame = pos.frame;
  1653. wait_count = 0;
  1654. do {
  1655. display_transport_state();
  1656. jack_sleep(10); // 10 ms
  1657. // locate at first...
  1658. wait_count++;
  1659. if (wait_count == 1) {
  1660. Log("Do jack_transport_locate\n");
  1661. jack_transport_locate(client1, cur_frame / 2);
  1662. } else if (wait_count == 100) {
  1663. break;
  1664. }
  1665. ts = jack_transport_query(client2, &pos);
  1666. Log("Locating.... frame = %ld\n", pos.frame);
  1667. } while (pos.frame > cur_frame);
  1668. ts = jack_transport_query(client2, &pos);
  1669. Log("Transport current frame = %ld\n", pos.frame);
  1670. if (wait_count == 100) {
  1671. printf("!!! ERROR !!! jack_transport_locate does not work correctly\n");
  1672. }
  1673. // Test jack_transport_reposition while rolling
  1674. Log("Test jack_transport_reposition while rolling\n");
  1675. ts = jack_transport_query(client2, &pos);
  1676. Log("Transport current frame = %ld\n", pos.frame);
  1677. cur_frame = pos.frame;
  1678. wait_count = 0;
  1679. do {
  1680. display_transport_state();
  1681. jack_sleep(10); // 10 ms
  1682. // locate at first...
  1683. wait_count++;
  1684. if (wait_count == 1) {
  1685. Log("Do jack_transport_reposition\n");
  1686. request_pos.frame = cur_frame / 2;
  1687. jack_transport_reposition(client1, &request_pos);
  1688. } else if (wait_count == 100) {
  1689. break;
  1690. }
  1691. ts = jack_transport_query(client2, &pos);
  1692. Log("Locating.... frame = %ld\n", pos.frame);
  1693. } while (pos.frame > cur_frame);
  1694. ts = jack_transport_query(client2, &pos);
  1695. Log("Transport current frame = %ld\n", pos.frame);
  1696. if (wait_count == 100) {
  1697. printf("!!! ERROR !!! jack_transport_reposition does not work correctly\n");
  1698. }
  1699. // Test jack_transport_reposition while stopped
  1700. jack_transport_stop(client1);
  1701. ts = jack_transport_query(client2, &pos);
  1702. Log("Transport current frame = %ld\n", pos.frame);
  1703. Log("Test jack_transport_reposition while stopped\n");
  1704. wait_count = 0;
  1705. request_pos.frame = 10000;
  1706. jack_transport_reposition(client1, &request_pos);
  1707. do {
  1708. display_transport_state();
  1709. jack_sleep(100); // 100 ms
  1710. if (wait_count++ == 10)
  1711. break;
  1712. ts = jack_transport_query(client2, &pos);
  1713. Log("Locating.... frame = %ld\n", pos.frame);
  1714. } while (pos.frame != 10000);
  1715. ts = jack_transport_query(client2, &pos);
  1716. Log("Transport current frame = %ld\n", pos.frame);
  1717. if (pos.frame != 10000) {
  1718. printf("!!! ERROR !!! jack_transport_reposition does not work correctly\n");
  1719. }
  1720. jack_transport_stop(client1);
  1721. /* Tell the JACK server that we are ready to roll. Our
  1722. * process() callback will start running now. */
  1723. } else {
  1724. printf("Transport check is disabled...\n");
  1725. }
  1726. time_before_exit = time_to_run;
  1727. while (time_before_exit != 0) {
  1728. jack_sleep (1 * 1000);
  1729. time_before_exit--;
  1730. }
  1731. if (jack_deactivate(client2) != 0) {
  1732. printf("!!! ERROR !!! jack_deactivate does not return 0 for client2 !\n");
  1733. }
  1734. if (jack_deactivate(client1) != 0) {
  1735. printf("!!! ERROR !!! jack_deactivate does not return 0 for client1 !\n");
  1736. }
  1737. /**
  1738. * Checking jack_frame_time.
  1739. */
  1740. Log("Testing jack_frame_time...\n");
  1741. jack_set_process_callback(client1, process4, client1);
  1742. jack_activate(client1);
  1743. jack_sleep(2 * 1000);
  1744. /**
  1745. * Checking alternate thread model
  1746. */
  1747. Log("Testing alternate thread model...\n");
  1748. jack_deactivate(client1);
  1749. jack_set_process_callback(client1, NULL, NULL); // remove callback
  1750. jack_set_process_thread(client1, jack_thread, client1);
  1751. jack_activate(client1);
  1752. jack_sleep(2 * 1000);
  1753. /**
  1754. * Checking callback exiting : when the return code is != 0, the client is desactivated.
  1755. */
  1756. Log("Testing callback exiting...\n");
  1757. jack_deactivate(client1);
  1758. jack_set_process_thread(client1, NULL, NULL); // remove thread callback
  1759. jack_set_process_callback(client1, process3, 0);
  1760. jack_activate(client1);
  1761. jack_sleep(3 * 1000);
  1762. /**
  1763. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1764. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1765. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1766. Closing program
  1767. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1768. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1769. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1770. */
  1771. if (jack_deactivate(client2) != 0) {
  1772. printf("!!! ERROR !!! jack_deactivate does not return 0 for client2 !\n");
  1773. }
  1774. if (jack_deactivate(client1) != 0) {
  1775. printf("!!! ERROR !!! jack_deactivate does not return 0 for client1 !\n");
  1776. }
  1777. if (jack_client_close(client2) != 0) {
  1778. printf("!!! ERROR !!! jack_client_close does not return 0 for client2 !\n");
  1779. }
  1780. if (jack_client_close(client1) != 0) {
  1781. printf("!!! ERROR !!! jack_client_close does not return 0 for client1 !\n");
  1782. }
  1783. if (xrun == 0) {
  1784. Log("No Xrun have been detected during this test... cool !\n");
  1785. } else {
  1786. printf("%i Xrun have been detected during this session (seen callback messages to see where are the problems).\n", xrun);
  1787. }
  1788. free(framecollect);
  1789. free(signal1);
  1790. free(signal2);
  1791. Log("Exiting jack_test...\n");
  1792. fclose(file);
  1793. printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
  1794. sprintf (filename, "framegraph-%i.gnu", cur_buffer_size);
  1795. file = fopen(filename, "w");
  1796. if (file == NULL) {
  1797. fprintf(stderr, "Erreur dans l'ouverture du fichier");
  1798. exit( -1);
  1799. }
  1800. fprintf(file, "reset\n");
  1801. fprintf(file, "set terminal png transparent nocrop enhanced\n");
  1802. fprintf(file, "set output 'framegraph-%i-1.png'\n", cur_buffer_size);
  1803. fprintf(file, "set title \"Frame time evolution during jack_test run\"\n");
  1804. fprintf(file, "set yrange [ %i.00000 : %i.0000 ] noreverse nowriteback\n", cur_buffer_size - (cur_buffer_size / 8), cur_buffer_size + (cur_buffer_size / 8));
  1805. fprintf(file, "set xrange [ 0.00000 : %i.0000 ] noreverse nowriteback\n" , linecount - 1);
  1806. fprintf(file, "set ylabel \"Frametime evolution (d(ft)/dt)\"\n");
  1807. fprintf(file, "set xlabel \"FrameTime\"\n");
  1808. fprintf(file, "set label \"| buf.siz:%i | fr.wl:%i | rg.ports:%i | 2nd.client:%i | trsprt:%i |\" at graph 0.01, 0.04\n", linebuf, linefw, lineports, linecl2, linetransport);
  1809. fprintf(file, "plot 'framefile-%i.dat' using 2 with impulses title \"Xruns\",'framefile-%i.dat' using 1 with line title \"Sampletime variation at %i\"\n", cur_buffer_size, cur_buffer_size, cur_buffer_size);
  1810. fprintf(file, "set output 'framegraph-%i-2.png'\n", cur_buffer_size);
  1811. fprintf(file, "set title \"Frame time evolution during jack_test run\"\n");
  1812. fprintf(file, "set yrange [ %i.00000 : %i.0000 ] noreverse nowriteback\n", (int) (cur_buffer_size / 2), (int) (2*cur_buffer_size + (cur_buffer_size / 8)));
  1813. fprintf(file, "set xrange [ 0.00000 : %i.0000 ] noreverse nowriteback\n" , linecount - 1);
  1814. fprintf(file, "set ylabel \"Frametime evolution (d(ft)/dt)\"\n");
  1815. fprintf(file, "set xlabel \"FrameTime\"\n");
  1816. fprintf(file, "set label \"| buf.siz:%i | fr.wl:%i | rg.ports:%i | 2nd.client:%i | trsprt:%i |\" at graph 0.01, 0.04\n", linebuf, linefw, lineports, linecl2, linetransport);
  1817. fprintf(file, "plot 'framefile-%i.dat' using 2 with impulses title \"Xruns\",'framefile-%i.dat' using 1 with line title \"Sampletime variation at %i\"\n", cur_buffer_size, cur_buffer_size, cur_buffer_size);
  1818. fclose(file);
  1819. return 0;
  1820. }