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.

2047 lines
80KB

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