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.

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