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.

2045 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("Freewhell 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 && (unsigned int)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\n", cur_buffer_size, delta_time);
  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 %d status 0x%2.0x !\n", 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 %d\n", 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. jack_on_info_shutdown(client1, jack_info_shutdown, 0);
  657. if (jack_set_buffer_size_callback(client1, Jack_Update_Buffer_Size, 0) != 0) {
  658. printf("Error when calling buffer_size_callback !\n");
  659. }
  660. if (jack_set_graph_order_callback(client1, Jack_Graph_Order_Callback, 0) != 0) {
  661. printf("Error when calling Jack_Graph_Order_Callback() !\n");
  662. }
  663. if (jack_set_port_rename_callback(client1, Jack_Port_Rename_Callback, 0) != 0 )
  664. printf("\n!!! ERROR !!! while calling jack_set_rename_callback()...\n");
  665. if (jack_set_xrun_callback(client1, Jack_XRun_Callback, 0 ) != 0) {
  666. printf("Error when calling jack_set_xrun_callback() !\n");
  667. }
  668. if (jack_set_sample_rate_callback(client1, Jack_Sample_Rate_Callback, 0 ) != 0) {
  669. printf("Error when calling Jack_Sample_Rate_Callback() !\n");
  670. }
  671. if (jack_set_port_registration_callback(client1, Jack_Port_Register, 0) != 0) {
  672. printf("Error when calling jack_set_port_registration_callback() !\n");
  673. }
  674. if (jack_set_port_connect_callback(client1, Jack_Port_Connect, 0) != 0) {
  675. printf("Error when calling jack_set_port_connect_callback() !\n");
  676. }
  677. if (jack_set_client_registration_callback(client1, Jack_Client_Registration_Callback, 0) != 0) {
  678. printf("Error when calling jack_set_client_registration_callback() !\n");
  679. }
  680. jack_set_error_function(Jack_Error_Callback);
  681. /**
  682. * Create file for clock "frame time" analysis
  683. *
  684. */
  685. cur_buffer_size = jack_get_buffer_size(client1);
  686. sprintf (filename, "framefile-%i.dat", cur_buffer_size);
  687. file = fopen(filename, "w");
  688. if (file == NULL) {
  689. fprintf(stderr, "Erreur dans l'ouverture du fichier log framefile.dat");
  690. exit(-1);
  691. }
  692. /**
  693. * Try to register a client with a NULL name/zero length name...
  694. *
  695. */
  696. output_port1 = jack_port_register(client1, nullportname,
  697. JACK_DEFAULT_AUDIO_TYPE,
  698. JackPortIsOutput, 0);
  699. if (output_port1 == NULL) {
  700. Log("Can't register a port with a NULL portname... ok.\n");
  701. } else {
  702. printf("!!! ERROR !!! Can register a port with a NULL portname !\n");
  703. jack_port_unregister(client1, output_port1);
  704. }
  705. /**
  706. * Register 1 port in order to stress other functions.
  707. *
  708. */
  709. output_port1 = jack_port_register(client1, portname,
  710. JACK_DEFAULT_AUDIO_TYPE,
  711. JackPortIsOutput, 0);
  712. if (output_port1 == NULL) {
  713. printf("!!! ERROR !!! Can't register any port for the client !\n");
  714. exit(1);
  715. }
  716. /**
  717. * Test port type of the just registered port.
  718. *
  719. */
  720. if (strcmp(jack_port_type(output_port1), JACK_DEFAULT_AUDIO_TYPE) != 0) {
  721. printf("!!! ERROR !!! jack_port_type returns an incorrect value!\n");
  722. } else {
  723. Log("Checking jack_port_type()... ok.\n");
  724. }
  725. /**
  726. * Try to register another port with the same name...
  727. *
  728. */
  729. output_port2 = jack_port_register(client1, portname,
  730. JACK_DEFAULT_AUDIO_TYPE,
  731. JackPortIsOutput, 0);
  732. if (output_port2 == NULL) {
  733. Log("Can't register two ports with the same name... ok\n");
  734. } else {
  735. if (strcmp (jack_port_name(output_port1), jack_port_name(output_port2)) == 0) {
  736. 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));
  737. jack_port_unregister(client1, output_port2);
  738. } else {
  739. 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));
  740. jack_port_unregister(client1, output_port2);
  741. }
  742. }
  743. /**
  744. * Verify that both port_name and port_short_name return correct results...
  745. *
  746. */
  747. sprintf (portname, "%s:%s", jack_get_client_name(client1), jack_port_short_name(output_port1));
  748. if (strcmp(jack_port_name(output_port1), portname) != 0) {
  749. printf("!!! ERROR !!! functions jack_port_name and/or jack_short_port_name seems to be invalid !\n");
  750. 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));
  751. }
  752. /**
  753. * Verify the function port_set_name
  754. *
  755. */
  756. if (jack_port_set_name (output_port1, "renamed-port#") == 0 ) {
  757. if (strcmp(jack_port_name(output_port1), "renamed-port#") == 0) {
  758. printf("!!! ERROR !!! functions jack_port_set_name seems to be invalid !\n");
  759. printf("jack_port_name return '%s' whereas 'renamed-port#' was expected...\n", jack_port_name(output_port1));
  760. } else {
  761. Log("Checking jack_port_set_name()... ok\n");
  762. jack_port_set_name (output_port1, "port");
  763. }
  764. } else {
  765. printf("error : port_set_name function can't be tested...\n");
  766. }
  767. port_callback_reg = 0; // number of port registration received by the callback
  768. /**
  769. * Activate the client
  770. *
  771. */
  772. if (jack_activate(client1) < 0) {
  773. printf ("Fatal error : cannot activate client1\n");
  774. exit(1);
  775. }
  776. /**
  777. * Test if portrename callback have been called.
  778. *
  779. */
  780. jack_port_set_name (output_port1, "renamed-port#");
  781. jack_sleep(1 * 1000);
  782. if (port_rename_clbk == 0)
  783. printf("!!! ERROR !!! Jack_Port_Rename_Callback was not called !!.\n");
  784. /**
  785. * Test if portregistration callback have been called.
  786. *
  787. */
  788. jack_sleep(1 * 1000);
  789. if (1 == port_callback_reg) {
  790. Log("%i ports have been successfully created, and %i callback reg ports have been received... ok\n", 1, port_callback_reg);
  791. } else {
  792. printf("!!! ERROR !!! %i ports have been created, and %i callback reg ports have been received !\n", 1, port_callback_reg);
  793. }
  794. /**
  795. * Test if init callback initThread have been called.
  796. *
  797. */
  798. if (init_clbk == 0)
  799. printf("!!! ERROR !!! Jack_Thread_Init_Callback was not called !!.\n");
  800. jack_sleep(10 * 1000); // test see the clock in the graph at the begining...
  801. /**
  802. * Stress Freewheel mode...
  803. * Try to enter freewheel mode. Check the realtime mode de-activation.
  804. * Check that the number of call of the process callback is greater than in non-freewheel mode.
  805. * Give an approximated speed ratio (number of process call) between the two modes.
  806. * Then return in normal mode.
  807. */
  808. t_error = 0;
  809. activated = 0;
  810. jack_sleep(1 * 1000);
  811. count1 = activated;
  812. Log("Testing activation freewheel mode...\n");
  813. linefw = linecount; // count for better graph reading with gnuplot
  814. jack_set_freewheel(client1, 1);
  815. activated = 0;
  816. jack_sleep(1 * 1000);
  817. count2 = activated;
  818. if (jack_is_realtime(client1) == 0) {
  819. t_error = 0;
  820. } else {
  821. printf("\n!!! ERROR !!! RT mode is always activated while freewheel mode is applied !\n");
  822. t_error = 1;
  823. }
  824. if (activated == 0)
  825. printf("!!! ERROR !!! Freewheel mode doesn't activate audio callback !!\n");
  826. jack_set_freewheel(client1, 0);
  827. jack_sleep(7 * 1000);
  828. if (jack_is_realtime(client1) == 1) {}
  829. else {
  830. printf("\n!!! ERROR !!! freewheel mode fail to reactivate RT mode when exiting !\n");
  831. t_error = 1;
  832. }
  833. if (t_error == 0) {
  834. Log("Freewheel mode appears to work well...\n");
  835. }
  836. if (count1 == 0) {
  837. Log("Audio Callback in 'standard' (non-freewheel) mode seems not to be called...\n");
  838. Log("Ratio speed would be unavailable...\n");
  839. } else {
  840. ratio = (float) ((count2 - count1) / count1);
  841. Log("Approximative speed ratio of freewheel mode = %f : 1.00\n", ratio);
  842. }
  843. /**
  844. * Stress buffer function...
  845. * get current buffer size.
  846. * Try to apply a new buffer size value ( 2 x the precedent buffer size value)
  847. * Then return in previous buffer size mode.
  848. *
  849. */
  850. float factor = 0.5f;
  851. old_buffer_size = jack_get_buffer_size(client1);
  852. Log("Testing BufferSize change & Callback...\n--> Current buffer size : %i.\n", old_buffer_size);
  853. linebuf = linecount;
  854. if (jack_set_buffer_size(client1, (jack_nframes_t)(old_buffer_size * factor)) < 0) {
  855. printf("!!! ERROR !!! jack_set_buffer_size fails !\n");
  856. }
  857. jack_sleep(1 * 1000);
  858. cur_buffer_size = jack_get_buffer_size(client1);
  859. if ((old_buffer_size * factor) != cur_buffer_size) {
  860. printf("!!! ERROR !!! Buffer size has not been changed !\n");
  861. printf("!!! Maybe jack was compiled without the '--enable-resize' flag...\n");
  862. } else {
  863. Log("jack_set_buffer_size() command successfully applied...\n");
  864. }
  865. jack_sleep(3 * 1000);
  866. jack_set_buffer_size(client1, old_buffer_size);
  867. cur_buffer_size = jack_get_buffer_size(client1);
  868. /**
  869. * Test the last regestered port to see if port_is_mine function the right value.
  870. * A second test will be performed later.
  871. * The result will be printed at the end.
  872. *
  873. */
  874. if (jack_port_is_mine(client1, output_port1)) {
  875. is_mine = 1;
  876. } else {
  877. is_mine = 0;
  878. }
  879. /**
  880. * Check that the ID returned by the port_by_name is right.
  881. * (it seems there is a problem here in some jack versions).
  882. *
  883. */
  884. if (output_port1 != jack_port_by_name(client1, jack_port_name(output_port1))) {
  885. printf("!!! ERROR !!! function jack_port_by_name() return bad value !\n");
  886. 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);
  887. } else {
  888. Log("Checking jack_port_by_name() return value... ok\n");
  889. }
  890. if (NULL != jack_port_by_name(client1, jack_port_short_name(output_port1))) {
  891. 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)));
  892. } else {
  893. Log("Checking jack_port_by_name() with bad argument... ok (returned id 0)\n");
  894. }
  895. /**
  896. * remove the output port previously created
  897. * no more ports should subsist here for our client.
  898. *
  899. */
  900. if (jack_port_unregister(client1, output_port1) != 0) {
  901. printf("!!! ERROR !!! while unregistering port %s.\n", jack_port_name(output_port1));
  902. }
  903. /**
  904. * list all in ports
  905. *
  906. */
  907. inports = jack_get_ports(client1, NULL, NULL, 0);
  908. /**
  909. * Test the first PHY (physical) connection to see if it's "mine".
  910. * and report the result in the test that began before.
  911. * The result is printed later.
  912. *
  913. */
  914. if (jack_port_is_mine(client1, jack_port_by_name(client1, inports[0]))) {
  915. is_mine = 0;
  916. }
  917. /**
  918. * List all devices' flags and print them...
  919. *
  920. */
  921. Log("\nTry functions jack_get_ports, jack_port_flag & jack_port_by_name to list PHY devices...\n");
  922. Log("-----------------------------------------------------------\n");
  923. Log("---------------------------DEVICES-------------------------\n");
  924. Log("-----------------------------------------------------------\n");
  925. a = 0;
  926. while (inports[a] != NULL) {
  927. flag = jack_port_flags(jack_port_by_name(client1, inports[a]) );
  928. Log(" * %s (id : %i)\n", inports[a], jack_port_by_name(client1, inports[a]));
  929. Log(" (");
  930. if (flag & JackPortIsInput)
  931. Log("JackPortIsInput ");
  932. if (flag & JackPortIsOutput)
  933. Log("JackPortIsOutput ");
  934. if (flag & JackPortIsPhysical)
  935. Log("JackPortIsPhysical ");
  936. if (flag & JackPortCanMonitor)
  937. Log("JackPortCanMonitor ");
  938. if (flag & JackPortIsTerminal)
  939. Log("JackPortIsTerminal ");
  940. Log(")\n\n");
  941. a++;
  942. }
  943. Log("-----------------------------------------------------------\n\n");
  944. /**
  945. * list all PHY in/out ports...
  946. * This list will be used later many times.
  947. *
  948. */
  949. outports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsOutput);
  950. inports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
  951. if (outports == NULL) {
  952. printf("!!! WARNING !!! no physical capture ports founded !\n");
  953. }
  954. if (inports == NULL) {
  955. printf("!!! WARNING !!! no physical output ports founded !\n");
  956. }
  957. /**
  958. * Brute test : try to create as many ports as possible.
  959. * It stops when jack returns an error.
  960. * Then try to connect each port to physical entry...
  961. * Check also that graph reorder callback is called.
  962. *
  963. */
  964. Log("Registering as many ports as possible and connect them to physical entries...\n");
  965. lineports = linecount;
  966. t_error = 0;
  967. i = 0; // number of couple 'input-ouput'
  968. j = 0; // number of ports created
  969. port_callback_reg = 0; // number of port registration received by the callback
  970. reorder = 0; // number of graph reorder callback activation
  971. test_link = 0 ; // Test the "overconnect" function only one time
  972. while (t_error == 0) {
  973. sprintf (portname, "input_%d", i);
  974. input_port1 = jack_port_register(client1, portname,
  975. JACK_DEFAULT_AUDIO_TYPE,
  976. JackPortIsInput, 0);
  977. j++;
  978. if (input_port1 == NULL) {
  979. j--;
  980. t_error = 1;
  981. } else {
  982. // Connect created input to PHY output
  983. a = 0;
  984. while (outports[a] != NULL) {
  985. if (jack_connect(client1, outports[a], jack_port_name(input_port1))) {
  986. printf ("error : cannot connect input PHY port to client port %s\n", jack_port_name(input_port1));
  987. } else {
  988. // printf ("input PHY port %s connected to client port %s\n", outports[a], jack_port_name(input_port1));
  989. }
  990. a++;
  991. }
  992. // Try one time to "overconnect" 2 ports (the latest created)...
  993. if (test_link == 0) {
  994. if (jack_connect(client1, outports[a - 1], jack_port_name(input_port1)) == EEXIST) {
  995. // cannot over-connect input PHY port to client port. ok.
  996. test_link = 1;
  997. }
  998. }
  999. }
  1000. sprintf(portname, "output_%d", i);
  1001. output_port1 = jack_port_register(client1, portname,
  1002. JACK_DEFAULT_AUDIO_TYPE,
  1003. JackPortIsOutput, 0);
  1004. j++;
  1005. if (output_port1 == NULL) {
  1006. t_error = 1;
  1007. j--;
  1008. } else {
  1009. // Connect created input to PHY output
  1010. a = 0;
  1011. while (inports[a] != NULL) {
  1012. if (jack_connect(client1, jack_port_name(output_port1), inports[a])) {
  1013. printf ("error : cannot connect input PHY port %s to client port %s\n", inports[a], jack_port_name(output_port1));
  1014. } else {
  1015. // output PHY port %s connected to client port. ok.
  1016. }
  1017. a++;
  1018. }
  1019. // Try one time to "overconnect" 2 ports (the latest created)...
  1020. if (test_link == 0) {
  1021. if (jack_connect(client1, jack_port_name(output_port1), inports[a - 1]) == EEXIST) {
  1022. // cannot over-connect output PHY port to client port. ok.
  1023. test_link = 1;
  1024. }
  1025. }
  1026. }
  1027. i++;
  1028. }
  1029. jack_sleep(1 * 1000); // To hope all port registration and reorder callback have been received...
  1030. // Check port registration callback
  1031. if (j == port_callback_reg) {
  1032. Log("%i ports have been successfully created, and %i callback reg ports have been received... ok\n", j, port_callback_reg);
  1033. } else {
  1034. printf("!!! ERROR !!! %i ports have been created, and %i callback reg ports have been received !\n", j, port_callback_reg);
  1035. }
  1036. if (reorder == (2 * j)) {
  1037. Log("%i graph reorder callback have been received... ok\n", reorder);
  1038. } else {
  1039. printf("!!! ERROR !!! %i graph reorder callback have been received (maybe non-valid value)...\n", reorder);
  1040. }
  1041. /**
  1042. * print basic test connection functions result ...
  1043. * over-connected means here that we try to connect 2 ports that are already connected.
  1044. *
  1045. */
  1046. if (test_link) {
  1047. Log("Jack links can't be 'over-connected'... ok\n");
  1048. } else {
  1049. printf("!!! ERROR !!! Jack links can be 'over-connected'...\n");
  1050. }
  1051. /**
  1052. * Print the result of the two jack_is_mine test.
  1053. *
  1054. */
  1055. if (is_mine == 1) {
  1056. Log("Checking jack_port_is_mine()... ok\n");
  1057. } else {
  1058. printf("!!! ERROR !!! jack_port_is_mine() function seems to send non-valid datas !\n");
  1059. }
  1060. /**
  1061. * Free the array of the physical input and ouput ports.
  1062. * (as mentionned in the doc of jack_get_ports)
  1063. *
  1064. */
  1065. free(inports);
  1066. free(outports);
  1067. /**
  1068. * Try to "reactivate" the client whereas it's already activated...
  1069. *
  1070. */
  1071. if (jack_activate(client1) < 0) {
  1072. printf("!!! ERROR !!! Cannot activate client1 a second time...\n");
  1073. exit(1);
  1074. } else {
  1075. Log("jackd server accept client.jack_activate() re-activation (while client was already activated).\n");
  1076. }
  1077. /**
  1078. * Deregister all ports previously created.
  1079. *
  1080. */
  1081. port_callback_reg = 0; // to check registration callback
  1082. Log("Deregistering all ports of the client...\n");
  1083. inports = jack_get_ports(client1, NULL, NULL, 0);
  1084. a = 0;
  1085. while (inports[a] != NULL) {
  1086. flag = jack_port_flags(jack_port_by_name(client1, inports[a]));
  1087. input_port1 = jack_port_by_name(client1, inports[a]);
  1088. if (jack_port_is_mine(client1, input_port1)) {
  1089. if (jack_port_unregister(client1, input_port1) != 0) {
  1090. printf("!!! ERROR !!! while unregistering port %s.\n", jack_port_name(output_port1));
  1091. }
  1092. }
  1093. a++;
  1094. }
  1095. // Check port registration callback again
  1096. if (j == port_callback_reg) {
  1097. Log("%i ports have been successfully created, and %i callback reg ports have been received... ok\n", j, port_callback_reg);
  1098. } else {
  1099. printf("!!! ERROR !!! %i ports have been created, and %i callback reg ports have been received !\n", j, port_callback_reg);
  1100. }
  1101. free(inports); // free array of ports (as mentionned in the doc of jack_get_ports)
  1102. /**
  1103. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1104. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1105. Open a new client (second one) to test some other things...
  1106. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1107. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1108. */
  1109. Log("\n\n----------------------------------------------------------------------\n");
  1110. Log("Starting second new client 'jack_test_#2'...\n");
  1111. /* open a client connection to the JACK server */
  1112. client_name2 = "jack_test_#2";
  1113. linecl2 = linecount; // reminders for graph analysis
  1114. client2 = jack_client_new(client_name2);
  1115. if (client2 == NULL) {
  1116. fprintf(stderr, "jack_client_new() failed for %s.\n"
  1117. "status = 0x%2.0x\n", client_name2, status);
  1118. if (status & JackServerFailed) {
  1119. fprintf(stderr, "Unable to connect client2 to JACK server\n");
  1120. }
  1121. exit(1);
  1122. }
  1123. // Check client registration callback
  1124. jack_sleep(1000);
  1125. if (client_register == 0)
  1126. printf("!!! ERROR !!! Client registration callback not called!\n");
  1127. /**
  1128. * Register callback for this client.
  1129. * Callbacks are the same as the first client for most of them, excepted for process audio callback.
  1130. *
  1131. */
  1132. jack_set_port_registration_callback(client2, Jack_Port_Register, 0);
  1133. jack_set_process_callback(client2, process2, 0);
  1134. jack_on_shutdown(client2, jack_shutdown, 0);
  1135. /**
  1136. * Register one input and one output for each client.
  1137. *
  1138. */
  1139. Log("registering 1 input/output ports for each client...\n");
  1140. output_port1 = jack_port_register(client1, "out1",
  1141. JACK_DEFAULT_AUDIO_TYPE,
  1142. JackPortIsOutput, 0);
  1143. output_port2 = jack_port_register(client2, "out2",
  1144. JACK_DEFAULT_AUDIO_TYPE,
  1145. JackPortIsOutput, 0);
  1146. input_port1 = jack_port_register(client1, "in1",
  1147. JACK_DEFAULT_AUDIO_TYPE,
  1148. JackPortIsInput, 0);
  1149. input_port2 = jack_port_register(client2, "in2",
  1150. JACK_DEFAULT_AUDIO_TYPE,
  1151. JackPortIsInput, 0);
  1152. if ((output_port1 == NULL) || (output_port2 == NULL) || (input_port1 == NULL) || (input_port2 == NULL)) {
  1153. printf("!!! ERROR !!! Unable to register ports...\n");
  1154. }
  1155. /**
  1156. * Set each process mode to idle and activate client2
  1157. *
  1158. */
  1159. process2_activated = -1;
  1160. process1_activated = -1;
  1161. if (jack_activate(client2) < 0) {
  1162. printf ("Fatal error : cannot activate client2\n");
  1163. exit (1);
  1164. }
  1165. /**
  1166. * Connect the two clients and check that all connections are well-done.
  1167. *
  1168. */
  1169. Log("Testing connections functions between clients...\n");
  1170. if (jack_connect(client1, jack_port_name(output_port1), jack_port_name(input_port2)) != 0) {
  1171. printf("!!! ERROR !!! while client1 intenting to connect ports...\n");
  1172. }
  1173. if (jack_connect(client2, jack_port_name(output_port2), jack_port_name(input_port1)) != 0) {
  1174. printf("!!! ERROR !!! while client2 intenting to connect ports...\n");
  1175. }
  1176. if (jack_connect(client1, jack_port_name(output_port1), jack_port_name(input_port1)) != 0) {
  1177. printf("!!! ERROR !!! while client1 intenting to connect ports...\n");
  1178. }
  1179. /**
  1180. * Test the port_connected function...
  1181. *
  1182. */
  1183. if ((jack_port_connected(output_port1) == jack_port_connected(input_port1)) &&
  1184. (jack_port_connected(output_port2) == jack_port_connected(input_port2)) &&
  1185. (jack_port_connected(output_port2) == 1) &&
  1186. (jack_port_connected(output_port1) == 2)
  1187. ) {
  1188. Log("Checking jack_port_connected()... ok.\n");
  1189. } else {
  1190. printf("!!! ERROR !!! function jack_port_connected() return a bad value !\n");
  1191. printf("jack_port_connected(output_port1) %d\n", jack_port_connected(output_port1));
  1192. printf("jack_port_connected(output_port2) %d\n", jack_port_connected(output_port2));
  1193. printf("jack_port_connected(input_port1) %d\n", jack_port_connected(input_port1));
  1194. printf("jack_port_connected(input_port2) %d\n", jack_port_connected(input_port2));
  1195. }
  1196. /**
  1197. * Test a new time the port_by_name function...(now we are in multi-client mode)
  1198. *
  1199. */
  1200. Log("Testing again jack_port_by_name...\n");
  1201. if (output_port1 != jack_port_by_name(client1, jack_port_name(output_port1))) {
  1202. printf("!!! ERROR !!! function jack_port_by_name() return bad value in a multi-client application!\n");
  1203. printf("!!! jack_port_by_name(jack_port_name(_ID_) ) != _ID_ in multiclient application.\n");
  1204. } else {
  1205. Log("Checking jack_port_by_name() function with a multi-client application... ok\n");
  1206. }
  1207. /**
  1208. * Test the port_connected_to function...
  1209. *
  1210. */
  1211. if ((jack_port_connected_to (output_port1, jack_port_name(input_port2))) &&
  1212. (!(jack_port_connected_to (output_port2, jack_port_name(input_port2))))) {
  1213. Log("checking jack_port_connected_to()... ok\n");
  1214. } else {
  1215. printf("!!! ERROR !!! jack_port_connected_to() return bad value !\n");
  1216. }
  1217. /**
  1218. * Test the port_get_connections & port_get_all_connections functions...
  1219. *
  1220. */
  1221. Log("Testing jack_port_get_connections and jack_port_get_all_connections...\n");
  1222. a = 0;
  1223. t_error = 0;
  1224. connexions1 = jack_port_get_connections (output_port1);
  1225. connexions2 = jack_port_get_all_connections(client1, output_port1);
  1226. if ((connexions1 == NULL) || (connexions2 == NULL)) {
  1227. printf("!!! ERROR !!! port_get_connexions or port_get_all_connexions return a NULL pointer !\n");
  1228. } else {
  1229. while ((connexions1[a] != NULL) && (connexions2[a] != NULL) && (t_error == 0)) {
  1230. t_error = strcmp(connexions1[a], connexions2[a]);
  1231. a++;
  1232. }
  1233. if (t_error == 0) {
  1234. Log("Checking jack_port_get_connections Vs jack_port_get_all_connections... ok\n");
  1235. } else {
  1236. printf("!!! ERROR !!! while checking jack_port_get_connections Vs jack_port_get_all_connections...\n");
  1237. }
  1238. }
  1239. a = 0;
  1240. t_error = 0;
  1241. inports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
  1242. connexions1 = NULL;
  1243. assert(inports != NULL);
  1244. if (inports[0] != NULL) {
  1245. connexions1 = jack_port_get_connections (jack_port_by_name(client1, inports[0]));
  1246. connexions2 = jack_port_get_all_connections(client1, jack_port_by_name(client1, inports[0]));
  1247. }
  1248. free (inports);
  1249. if (connexions1 == NULL) {
  1250. Log("checking jack_port_get_connections() for external client... ok\n");
  1251. } else {
  1252. while ((connexions1[a] != NULL) && (connexions2[a] != NULL) && (t_error == 0)) {
  1253. t_error = strcmp(connexions1[a], connexions2[a]);
  1254. a++;
  1255. }
  1256. }
  1257. if (t_error == 0) {
  1258. Log("Checking jack_port_get_connections() Vs jack_port_get_all_connections() on PHY port... ok\n");
  1259. } else {
  1260. printf("!!! ERROR !!! while checking jack_port_get_connections() Vs jack_port_get_all_connections() on PHY port...\n");
  1261. }
  1262. if (jack_disconnect(client1, jack_port_name(output_port1), jack_port_name(input_port1)) != 0) {
  1263. printf("!!! ERROR !!! while client1 intenting to disconnect ports...\n");
  1264. }
  1265. if (jack_disconnect(client1, jack_port_name(output_port2), jack_port_name(input_port1)) != 0) {
  1266. printf("!!! ERROR !!! while client1 intenting to disconnect ports...\n");
  1267. }
  1268. // No links should subsist now...
  1269. /**
  1270. * Checking data connexion
  1271. * establishing a link between client1.out1 --> client2.in2
  1272. * Send the signal1 test on out1. Record the result into signal2. (see process functions).
  1273. ---------------------------------------------------------------------------*/
  1274. Log("Testing connections datas between clients...\n");
  1275. jack_connect(client2, jack_port_name(output_port1), jack_port_name(input_port2) );
  1276. process2_activated = -1;
  1277. process1_activated = -1;
  1278. Log("process 2 : idle mode...\n");
  1279. Log("Sending datas...");
  1280. index1 = 0;
  1281. index2 = 0;
  1282. process1_activated = 1; // We start emitting first.
  1283. process2_activated = 1; // So record begin at least when we just begin to emitt the signal, else at next call of process with
  1284. // nframe = jack buffersize shifting.
  1285. while (process2_activated == 1) {
  1286. jack_sleep(1 * 1000);
  1287. Log(".");
  1288. }
  1289. index2 = 0;
  1290. Log("\nAnalysing datas...\n"); // search the first occurence of the first element of the reference signal in the recorded signal
  1291. while (signal2[index2] != signal1[1] ) {
  1292. index2++;
  1293. if (index2 == 95999) {
  1294. printf("!!! ERROR !!! Data not found in first connexion data check!\n");
  1295. break;
  1296. }
  1297. }
  1298. index1 = index2;
  1299. Log("Data founded at offset %i.\n", index2);
  1300. // And now we founded were the recorded data are, we can see if the two signals matches...
  1301. while ( (signal2[index2] == signal1[index2 - index1 + 1]) || (index2 == 95999) || ((index2 - index1 + 1) == 47999) ) {
  1302. index2++;
  1303. }
  1304. Log("Checking difference between datas... %i have the same value...\n", index2 - index1);
  1305. if ((index2 - index1) == 48000) {
  1306. Log("Data received are valid...\n");
  1307. } else {
  1308. printf("!!! ERROR !!! data transmission seems not to be valid in first connexion data check!\n");
  1309. }
  1310. if (jack_disconnect(client1, jack_port_name(output_port1), jack_port_name(input_port2) ) != 0)
  1311. // no more connection between ports exist now...
  1312. {
  1313. printf("Error while establishing new connexion (disconnect).\n");
  1314. }
  1315. /**
  1316. * Test TIE MODE
  1317. * (This mode seems to be problematic in standard jack version 0.100. It seems that nobody
  1318. * is used to apply this mode because the tie mode doesn't work at all. A patch seems difficult to produce
  1319. * in this version of jack. Tie mode work well in MP version.)
  1320. * Test some basic thinks (tie with 2 differents client, tie non-owned ports...)
  1321. * Tie client1.in1 and client1.out1 ports, and make some data test to check the validity of the tie.
  1322. *
  1323. */
  1324. Log("Testing tie mode...\n");
  1325. if (jack_port_tie(input_port1, output_port2) != 0) {
  1326. Log("not possible to tie two ports from two differents clients... ok\n");
  1327. } else {
  1328. printf("!!! ERROR !!! port_tie has allowed a connexion between two differents clients !\n");
  1329. jack_port_untie(output_port2);
  1330. }
  1331. Log("Testing connections datas in tie mode...\n");
  1332. int g;
  1333. for (g = 0; g < 96000; g++)
  1334. signal2[g] = 0.0;
  1335. // Create a loop (emit test) client2.out2----client.in1--tie--client1.out1-----client2.in1 (receive test)
  1336. if (jack_port_tie(input_port1, output_port1) != 0) {
  1337. printf("Unable to tie... fatal error : data test will not be performed on tie mode !!\n");
  1338. } else { // begin of tie
  1339. if (jack_connect(client1, jack_port_name(output_port1), jack_port_name(input_port2)) != 0) {
  1340. printf("!!! ERROR !!! while client1 intenting to connect ports...\n");
  1341. }
  1342. if (jack_connect(client1, jack_port_name(output_port2), jack_port_name(input_port1)) != 0) {
  1343. printf("!!! ERROR !!! while client1 intenting to connect ports...\n");
  1344. }
  1345. process1_activated = -1;
  1346. process2_activated = -1;
  1347. // We can manualy check here that the tie is effective.
  1348. // ie : playing a wav with a client, connecting ports manualy with qjackctl, and listen...
  1349. // printf("manual test\n");
  1350. // jack_sleep(50);
  1351. // printf("end of manual test\n");
  1352. index1 = 0;
  1353. index2 = 0;
  1354. process1_activated = -1;
  1355. process2_activated = 2;
  1356. Log("Sending datas...");
  1357. while (process2_activated == 2) {
  1358. jack_sleep(1 * 1000);
  1359. Log(".");
  1360. }
  1361. process1_activated = -1;
  1362. process2_activated = -1;
  1363. index2 = 0;
  1364. Log("\nAnalysing datas...\n");
  1365. // We must find at least 2 identical values to ensure we are at the right place in the siusoidal array...
  1366. while (!((signal2[index2] == signal1[1]) && (signal2[index2 + 1] == signal1[2]))) {
  1367. index2++;
  1368. if (index2 == 95999) {
  1369. printf("!!! ERROR !!! Data not found in connexion check of tie mode!\n");
  1370. break;
  1371. }
  1372. }
  1373. index1 = index2;
  1374. Log("Tie mode : Data founded at offset %i.\n", index2);
  1375. while (signal2[index2] == signal1[index2 - index1 + 1]) {
  1376. index2++;
  1377. if ((index2 == 95999) || ((index2 - index1 + 1) == 47999)) {
  1378. break;
  1379. }
  1380. }
  1381. Log("Checking difference between datas... %i have the same value...\n", index2 - index1);
  1382. if ((index2 - index1) > 47995) {
  1383. Log("Data received in tie mode are valid...\n");
  1384. } else {
  1385. // in tie mode, the buffers adress should be the same for the two tied ports.
  1386. printf("!!! ERROR !!! data transmission seems not to be valid !\n");
  1387. printf("Links topology : (emitt) client2.out2 ----> client1.in1--(tie)--client1.out1----->client2.in2 (recive)\n");
  1388. printf(" port_name : Port_adress \n");
  1389. printf(" output_port1 : %px\n", jack_port_get_buffer(output_port1, cur_buffer_size));
  1390. printf(" input_port2 : %px\n", jack_port_get_buffer(input_port2, cur_buffer_size));
  1391. printf(" output_port2 : %px\n", jack_port_get_buffer(output_port2, cur_buffer_size));
  1392. printf(" input_port1 : %px\n", jack_port_get_buffer(input_port1, cur_buffer_size));
  1393. }
  1394. jack_port_untie(output_port1);
  1395. jack_port_disconnect(client1, output_port2);
  1396. jack_port_disconnect(client1, output_port1);
  1397. } //end of tie
  1398. /**
  1399. * Testing SUMMATION CAPABILITIES OF JACK CONNECTIONS
  1400. *
  1401. * In a short test, we just check a simple summation in jack.
  1402. * A first client(client1) send two signal in phase opposition
  1403. * A second client(client2) record the summation at one of his port
  1404. * So, the result must be zero...
  1405. * See process1 for details about steps of this test
  1406. *
  1407. */
  1408. // fprintf(file, "Sum test\n");
  1409. Log("Checking summation capabilities of patching...\n");
  1410. output_port1b = jack_port_register(client1, "out1b",
  1411. JACK_DEFAULT_AUDIO_TYPE,
  1412. JackPortIsOutput, 0);
  1413. jack_connect(client2, jack_port_name(output_port1), jack_port_name(input_port2));
  1414. jack_connect(client2, jack_port_name(output_port1b), jack_port_name(input_port2));
  1415. process1_activated = 3;
  1416. process2_activated = -1;
  1417. for (g = 0; g < 96000; g++)
  1418. signal2[g] = 0.0;
  1419. index1 = 0;
  1420. index2 = 0;
  1421. Log("Sending datas...");
  1422. process2_activated = 3;
  1423. while (process2_activated == 3) {
  1424. jack_sleep(1 * 1000);
  1425. Log(".");
  1426. }
  1427. process1_activated = -1;
  1428. process2_activated = -1;
  1429. index2 = 0;
  1430. Log("\nAnalysing datas...\n"); // same idea as above, with first data check...
  1431. while (!((signal2[index2] == 0.0 ) && (signal2[(index2 + 1)] == 0.0 ))) {
  1432. index2++;
  1433. if (index2 == 95999) {
  1434. printf("!!! ERROR !!! Data not found in summation check!\n");
  1435. break;
  1436. }
  1437. }
  1438. index1 = index2;
  1439. Log("Data founded at offset %i.\n", index2);
  1440. while ( signal2[index2] == 0.0 ) {
  1441. index2++;
  1442. if ((index2 > 95998) || ((index2 - index1 + 1) > 47998)) {
  1443. break;
  1444. }
  1445. }
  1446. Log("Checking difference between datas...\n");
  1447. if ((index2 - index1) > 47996) {
  1448. Log("Data mixed received are valid...\nSummation is well done.\n");
  1449. } else {
  1450. printf("!!! ERROR !!! data transmission / summation seems not to be valid !\n");
  1451. }
  1452. jack_port_disconnect(client1, output_port1);
  1453. jack_port_disconnect(client1, output_port1b);
  1454. jack_port_unregister(client1, output_port1b);
  1455. if (jack_port_name(output_port1b) != NULL ) {
  1456. printf("!!! WARNING !!! port_name return something while the port have been unregistered !\n");
  1457. printf("!!! Name of unregistered port : %s !\n", jack_port_name(output_port1b));
  1458. } else {
  1459. Log("Checking jack_port_name() with a non valid port... ok\n");
  1460. }
  1461. if (jack_port_set_name(output_port1b, "new_name") == 0 ) {
  1462. printf("!!! WARNING !!! An unregistered port can be renamed successfully !\n");
  1463. } else {
  1464. Log("Checking renaming of an unregistered port... ok\n");
  1465. }
  1466. inports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
  1467. if (jack_port_set_name(jack_port_by_name(client1, inports[0]), "new_name") == 0 ) {
  1468. printf("!!! WARNING !!! A PHYSICAL port can be renamed successfully !\n");
  1469. } else {
  1470. Log("Checking renaming of an unregistered port... ok\n");
  1471. }
  1472. free (inports);
  1473. /**
  1474. * Checking latency issues
  1475. * here are simple latency check
  1476. * We simply check that the value returned by jack seems ok
  1477. * Latency compensation is a difficult point.
  1478. * Actually, jack is not able to see "thru" client to build a full latency chain.
  1479. * Ardour use theses informations to do internally his compensations.
  1480. *
  1481. * 3 test are done : one with no connections between client, one with a serial connection, and one with parallel connection
  1482. */
  1483. Log("Checking about latency functions...\n");
  1484. t_error = 0;
  1485. jack_recompute_total_latencies(client1);
  1486. if ((jack_port_get_latency (output_port1) != 0) ||
  1487. (jack_port_get_total_latency(client1, output_port1) != 0) ) {
  1488. t_error = 1;
  1489. printf("!!! ERROR !!! default latency of a non-PHY device is not set to zero !\n");
  1490. }
  1491. inports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
  1492. outports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsOutput);
  1493. if (inports[0] != NULL) {
  1494. output_ext_latency = jack_port_get_latency (jack_port_by_name(client1, inports[0])); // from client to out driver (which has "inputs" ports..)
  1495. input_ext_latency = jack_port_get_latency (jack_port_by_name(client1, outports[0])); // from in driver (which has "output" ports..) to client
  1496. if (output_ext_latency != jack_port_get_total_latency(client1, jack_port_by_name(client1, inports[0]))) {
  1497. t_error = 1;
  1498. printf("!!! ERROR !!! get_latency & get_all_latency for a PHY device (unconnected) didn't return the same value !\n");
  1499. }
  1500. Log("Checking a serial model with 2 clients...\n");
  1501. jack_connect(client1, jack_port_name(output_port1), jack_port_name(input_port2));
  1502. jack_connect(client1, outports[0], jack_port_name(input_port1));
  1503. jack_connect(client2, jack_port_name(output_port2), inports[0]);
  1504. jack_port_set_latency(output_port2, 256);
  1505. jack_recompute_total_latencies(client1);
  1506. if ((jack_port_get_latency (output_port1) != 0) ||
  1507. (jack_port_get_total_latency(client1, output_port1) != 0) ||
  1508. (jack_port_get_latency (jack_port_by_name(client1, inports[0])) != (output_ext_latency)) ||
  1509. (jack_port_get_total_latency(client1, jack_port_by_name(client1, inports[0])) != (output_ext_latency + 256)) ||
  1510. (jack_port_get_total_latency(client1, output_port2) != (output_ext_latency + 256)) ||
  1511. (jack_port_get_total_latency(client1, input_port2) != 0) ||
  1512. (jack_port_get_total_latency(client1, input_port1) != input_ext_latency) ||
  1513. (jack_port_get_latency (jack_port_by_name(client1, outports[0])) != input_ext_latency) ||
  1514. (jack_port_get_total_latency(client1, jack_port_by_name(client1, outports[0])) != input_ext_latency)
  1515. ) {
  1516. printf("!!! WARNING !!! get_latency functions may have a problem : bad value returned !\n");
  1517. printf("!!! get_latency(output_port1) : %i (must be 0)\n", jack_port_get_latency(output_port1));
  1518. printf("!!! get_total_latency(output_port1) : %i (must be 0)\n", jack_port_get_total_latency(client1, output_port1));
  1519. 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);
  1520. 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));
  1521. printf("!!! get_total_latency(output_port2) : %i (must be %i)\n", jack_port_get_total_latency(client1, output_port2), (output_ext_latency + 256));
  1522. printf("!!! get_total_latency(input_port2) : %i (must be 0)\n", jack_port_get_total_latency(client1, input_port2));
  1523. printf("!!! get_total_latency(input_port1) : %i (must be %i)\n", jack_port_get_total_latency(client1, input_port1), input_ext_latency);
  1524. printf("!!! get_latency(PHY[0]) : %i (must be %i)\n", jack_port_get_latency(jack_port_by_name(client1, outports[0])), input_ext_latency);
  1525. 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);
  1526. } else {
  1527. Log("get_latency & get_total_latency seems quite ok...\n");
  1528. }
  1529. jack_port_disconnect(client1, output_port1);
  1530. jack_port_disconnect(client1, output_port2);
  1531. jack_port_disconnect(client1, input_port1);
  1532. jack_port_disconnect(client1, input_port2);
  1533. Log("Checking a parallel model with 2 clients...\n");
  1534. jack_connect(client2, outports[0], jack_port_name(input_port1));
  1535. jack_connect(client2, outports[0], jack_port_name(input_port2));
  1536. jack_connect(client2, jack_port_name(output_port1), inports[0]);
  1537. jack_connect(client2, jack_port_name(output_port2), inports[0]);
  1538. jack_port_set_latency(output_port1, 256);
  1539. jack_port_set_latency(output_port2, 512);
  1540. jack_recompute_total_latencies(client1);
  1541. if ((jack_port_get_latency(output_port1) != 256 ) ||
  1542. (jack_port_get_total_latency(client1, output_port1) != (256 + output_ext_latency)) ||
  1543. (jack_port_get_latency(output_port2) != 512) ||
  1544. (jack_port_get_total_latency(client1, output_port2) != (512 + output_ext_latency)) ||
  1545. (jack_port_get_latency(jack_port_by_name(client1, inports[0])) != output_ext_latency) ||
  1546. (jack_port_get_total_latency(client1, jack_port_by_name(client1, inports[0])) != (512 + output_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 256)\n", jack_port_get_latency(output_port1));
  1550. printf("!!! get_total_latency(output_port1) : %i (must be 256 + output_ext_latency)\n", jack_port_get_total_latency(client1, output_port1));
  1551. printf("!!! get_latency(output_port2) : %i (must 512)\n", jack_port_get_latency(output_port2));
  1552. printf("!!! get_total_latency(output_port2) : %i (must 512 + output_ext_latency)\n", jack_port_get_total_latency(client1, output_port2));
  1553. printf("!!! get_latency(inports[0])) : %i (must output_ext_latency)\n", jack_port_get_latency(jack_port_by_name(client1, inports[0])));
  1554. 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])));
  1555. } else {
  1556. Log("get_latency & get_total_latency seems quite ok...\n");
  1557. }
  1558. } else {
  1559. printf("No physical port founded : not able to test latency functions...");
  1560. }
  1561. jack_port_disconnect(client1, input_port1);
  1562. jack_port_disconnect(client1, input_port2);
  1563. jack_port_disconnect(client1, output_port1);
  1564. jack_port_disconnect(client1, output_port2);
  1565. jack_sleep(1000);
  1566. free(inports);
  1567. free(outports);
  1568. /**
  1569. * Checking transport API.
  1570. * Simple transport test.
  1571. * Check a transport start with a "slow" client, simulating a delay around 1 sec before becoming ready.
  1572. *
  1573. */
  1574. Log("-----------------------------------------------------------\n");
  1575. Log("---------------------------TRANSPORT-----------------------\n");
  1576. Log("-----------------------------------------------------------\n");
  1577. lineports = linecount;
  1578. if (transport_mode) {
  1579. int wait_count;
  1580. ts = jack_transport_query(client1, &pos);
  1581. if (ts == JackTransportStopped) {
  1582. Log("Transport is stopped...\n");
  1583. } else {
  1584. jack_transport_stop(client1);
  1585. Log("Transport state : %i\n", ts);
  1586. }
  1587. if (jack_set_sync_callback(client2, Jack_Sync_Callback, 0) != 0)
  1588. printf("error while calling set_sync_callback...\n");
  1589. Log("starting transport...\n");
  1590. starting_state = 1; // Simulate starting state
  1591. jack_transport_start(client1);
  1592. // Wait until sync callback is called
  1593. while (!(sync_called)) {
  1594. jack_sleep(1 * 1000);
  1595. }
  1596. // Wait untill rolling : simulate sync time out
  1597. Log("Simulate a slow-sync client exceeding the time-out\n");
  1598. wait_count = 0;
  1599. do {
  1600. jack_sleep(100); // Wait 100 ms each cycle
  1601. wait_count++;
  1602. if (wait_count == 100) {
  1603. Log("!!! ERROR !!! max time-out exceedeed : sync time-out does not work correctly\n");
  1604. break;
  1605. }
  1606. ts = jack_transport_query(client2, &pos);
  1607. Log("Waiting....pos = %ld\n", pos.frame);
  1608. display_transport_state();
  1609. } while (ts != JackTransportRolling);
  1610. Log("Sync callback have been called %i times.\n", sync_called);
  1611. jack_transport_stop(client1);
  1612. // Wait until stopped
  1613. ts = jack_transport_query(client2, &pos);
  1614. while (ts != JackTransportStopped) {
  1615. jack_sleep(1 * 1000);
  1616. ts = jack_transport_query(client2, &pos);
  1617. }
  1618. // Simulate starting a slow-sync client that rolls after 0.5 sec
  1619. Log("Simulate a slow-sync client that needs 0.5 sec to start\n");
  1620. sync_called = 0;
  1621. wait_count = 0;
  1622. starting_state = 1; // Simulate starting state
  1623. Log("Starting transport...\n");
  1624. jack_transport_start(client1);
  1625. display_transport_state();
  1626. Log("Waiting 0.5 sec...\n");
  1627. jack_sleep(500);
  1628. starting_state = 0; // Simulate end of starting state after 0.5 sec
  1629. // Wait untill rolling
  1630. ts = jack_transport_query(client2, &pos);
  1631. while (ts != JackTransportRolling) {
  1632. jack_sleep(100); // Wait 100 ms each cycle
  1633. wait_count++;
  1634. if (wait_count == 10) {
  1635. Log("!!! ERROR !!! starting a slow-sync client does not work correctly\n");
  1636. break;
  1637. }
  1638. ts = jack_transport_query(client2, &pos);
  1639. }
  1640. if (sync_called == 0)
  1641. Log("!!! ERROR !!! starting a slow-sync client does not work correctly\n");
  1642. Log("Sync callback have been called %i times.\n", sync_called);
  1643. display_transport_state();
  1644. // Test jack_transport_locate while rolling
  1645. Log("Test jack_transport_locate while rolling\n");
  1646. ts = jack_transport_query(client2, &pos);
  1647. Log("Transport current frame = %ld\n", pos.frame);
  1648. jack_nframes_t cur_frame = pos.frame;
  1649. wait_count = 0;
  1650. do {
  1651. display_transport_state();
  1652. jack_sleep(10); // 10 ms
  1653. // locate at first...
  1654. wait_count++;
  1655. if (wait_count == 1) {
  1656. Log("Do jack_transport_locate\n");
  1657. jack_transport_locate(client1, cur_frame / 2);
  1658. } else if (wait_count == 100) {
  1659. break;
  1660. }
  1661. ts = jack_transport_query(client2, &pos);
  1662. Log("Locating.... frame = %ld\n", pos.frame);
  1663. } while (pos.frame > cur_frame);
  1664. ts = jack_transport_query(client2, &pos);
  1665. Log("Transport current frame = %ld\n", pos.frame);
  1666. if (wait_count == 100) {
  1667. printf("!!! ERROR !!! jack_transport_locate does not work correctly\n");
  1668. }
  1669. // Test jack_transport_reposition while rolling
  1670. Log("Test jack_transport_reposition while rolling\n");
  1671. ts = jack_transport_query(client2, &pos);
  1672. Log("Transport current frame = %ld\n", pos.frame);
  1673. cur_frame = pos.frame;
  1674. wait_count = 0;
  1675. do {
  1676. display_transport_state();
  1677. jack_sleep(10); // 10 ms
  1678. // locate at first...
  1679. wait_count++;
  1680. if (wait_count == 1) {
  1681. Log("Do jack_transport_reposition\n");
  1682. request_pos.frame = cur_frame / 2;
  1683. jack_transport_reposition(client1, &request_pos);
  1684. } else if (wait_count == 100) {
  1685. break;
  1686. }
  1687. ts = jack_transport_query(client2, &pos);
  1688. Log("Locating.... frame = %ld\n", pos.frame);
  1689. } while (pos.frame > cur_frame);
  1690. ts = jack_transport_query(client2, &pos);
  1691. Log("Transport current frame = %ld\n", pos.frame);
  1692. if (wait_count == 100) {
  1693. printf("!!! ERROR !!! jack_transport_reposition does not work correctly\n");
  1694. }
  1695. // Test jack_transport_reposition while stopped
  1696. jack_transport_stop(client1);
  1697. ts = jack_transport_query(client2, &pos);
  1698. Log("Transport current frame = %ld\n", pos.frame);
  1699. Log("Test jack_transport_reposition while stopped\n");
  1700. wait_count = 0;
  1701. request_pos.frame = 10000;
  1702. jack_transport_reposition(client1, &request_pos);
  1703. do {
  1704. display_transport_state();
  1705. jack_sleep(100); // 100 ms
  1706. if (wait_count++ == 10)
  1707. break;
  1708. ts = jack_transport_query(client2, &pos);
  1709. Log("Locating.... frame = %ld\n", pos.frame);
  1710. } while (pos.frame != 10000);
  1711. ts = jack_transport_query(client2, &pos);
  1712. Log("Transport current frame = %ld\n", pos.frame);
  1713. if (pos.frame != 10000) {
  1714. printf("!!! ERROR !!! jack_transport_reposition does not work correctly\n");
  1715. }
  1716. jack_transport_stop(client1);
  1717. /* Tell the JACK server that we are ready to roll. Our
  1718. * process() callback will start running now. */
  1719. } else {
  1720. printf("Transport check is disabled...\n");
  1721. }
  1722. time_before_exit = time_to_run;
  1723. while (time_before_exit != 0) {
  1724. jack_sleep (1 * 1000);
  1725. time_before_exit--;
  1726. }
  1727. if (jack_deactivate(client2) != 0) {
  1728. printf("!!! ERROR !!! jack_deactivate does not return 0 for client2 !\n");
  1729. }
  1730. if (jack_deactivate(client1) != 0) {
  1731. printf("!!! ERROR !!! jack_deactivate does not return 0 for client1 !\n");
  1732. }
  1733. /**
  1734. * Checking jack_frame_time.
  1735. */
  1736. Log("Testing jack_frame_time...\n");
  1737. jack_set_process_callback(client1, process4, client1);
  1738. jack_activate(client1);
  1739. jack_sleep(2 * 1000);
  1740. /**
  1741. * Checking alternate thread model
  1742. */
  1743. Log("Testing alternate thread model...\n");
  1744. jack_deactivate(client1);
  1745. jack_set_process_callback(client1, NULL, NULL); // remove callback
  1746. jack_set_process_thread(client1, jack_thread, client1);
  1747. jack_activate(client1);
  1748. jack_sleep(2 * 1000);
  1749. /**
  1750. * Checking callback exiting : when the return code is != 0, the client is desactivated.
  1751. */
  1752. Log("Testing callback exiting...\n");
  1753. jack_deactivate(client1);
  1754. jack_set_process_thread(client1, NULL, NULL); // remove thread callback
  1755. jack_set_process_callback(client1, process3, 0);
  1756. jack_activate(client1);
  1757. jack_sleep(3 * 1000);
  1758. /**
  1759. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1760. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1761. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1762. Closing program
  1763. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1764. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1765. *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  1766. */
  1767. if (jack_deactivate(client2) != 0) {
  1768. printf("!!! ERROR !!! jack_deactivate does not return 0 for client2 !\n");
  1769. }
  1770. if (jack_deactivate(client1) != 0) {
  1771. printf("!!! ERROR !!! jack_deactivate does not return 0 for client1 !\n");
  1772. }
  1773. if (jack_client_close(client2) != 0) {
  1774. printf("!!! ERROR !!! jack_client_close does not return 0 for client2 !\n");
  1775. }
  1776. if (jack_client_close(client1) != 0) {
  1777. printf("!!! ERROR !!! jack_client_close does not return 0 for client1 !\n");
  1778. }
  1779. if (xrun == 0) {
  1780. Log("No Xrun have been detected during this test... cool !\n");
  1781. } else {
  1782. printf("%i Xrun have been detected during this session (seen callback messages to see where are the problems).\n", xrun);
  1783. }
  1784. free(framecollect);
  1785. free(signal1);
  1786. free(signal2);
  1787. Log("Exiting jack_test...\n");
  1788. fclose(file);
  1789. printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
  1790. sprintf (filename, "framegraph-%i.gnu", cur_buffer_size);
  1791. file = fopen(filename, "w");
  1792. if (file == NULL) {
  1793. fprintf(stderr, "Erreur dans l'ouverture du fichier");
  1794. exit( -1);
  1795. }
  1796. fprintf(file, "reset\n");
  1797. fprintf(file, "set terminal png transparent nocrop enhanced\n");
  1798. fprintf(file, "set output 'framegraph-%i-1.png'\n", cur_buffer_size);
  1799. fprintf(file, "set title \"Frame time evolution during jack_test run\"\n");
  1800. 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));
  1801. fprintf(file, "set xrange [ 0.00000 : %i.0000 ] noreverse nowriteback\n" , linecount - 1);
  1802. fprintf(file, "set ylabel \"Frametime evolution (d(ft)/dt)\"\n");
  1803. fprintf(file, "set xlabel \"FrameTime\"\n");
  1804. 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);
  1805. 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);
  1806. fprintf(file, "set output 'framegraph-%i-2.png'\n", cur_buffer_size);
  1807. fprintf(file, "set title \"Frame time evolution during jack_test run\"\n");
  1808. 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)));
  1809. fprintf(file, "set xrange [ 0.00000 : %i.0000 ] noreverse nowriteback\n" , linecount - 1);
  1810. fprintf(file, "set ylabel \"Frametime evolution (d(ft)/dt)\"\n");
  1811. fprintf(file, "set xlabel \"FrameTime\"\n");
  1812. 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);
  1813. 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);
  1814. fclose(file);
  1815. return 0;
  1816. }