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.

2093 lines
82KB

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