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.

1993 lines
78KB

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