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.

1945 lines
76KB

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