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.

260 lines
6.8KB

  1. /*
  2. Copyright (C) 2005 Samuel TRACOL
  3. Copyright (C) 2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. /** @file jack_cpu.c
  17. *
  18. * @brief This client test the capacity for jackd to kick out a to heavy cpu client.
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <unistd.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <getopt.h>
  27. #include <jack/jack.h>
  28. jack_port_t *input_port;
  29. jack_port_t *output_port;
  30. jack_client_t *client;
  31. unsigned long sr;
  32. jack_nframes_t idle_time = 0;
  33. int percent_cpu = 0;
  34. int time_to_run = 0;
  35. int time_before_run = 0;
  36. int time_before_exit = 1;
  37. jack_nframes_t cur_buffer_size;
  38. /* a simple state machine for this client */
  39. volatile enum {
  40. Init,
  41. Run,
  42. Exit
  43. } client_state = Init;
  44. void usage()
  45. {
  46. fprintf (stderr, "\n"
  47. "usage: jack_cpu \n"
  48. " [ --name OR -n client_name ]\n"
  49. " [ --time OR -t time_to_run (in seconds) ]\n"
  50. " [ --delay OR -d delay_before_cpuload__is_applied (in seconds) ]\n"
  51. " --cpu OR -c percent_cpu_load (1-99)\n"
  52. );
  53. }
  54. int update_buffer_size(jack_nframes_t nframes, void *arg)
  55. {
  56. cur_buffer_size = nframes;
  57. printf("Buffer size = %d \n", cur_buffer_size);
  58. idle_time = (jack_nframes_t) (cur_buffer_size * percent_cpu / 100);
  59. printf("CPU load applies as %d sample delay.\n", idle_time);
  60. return 0;
  61. }
  62. /**
  63. * The process callback for this JACK application is called in a
  64. * special realtime thread once for each audio cycle.
  65. *
  66. */
  67. int process(jack_nframes_t nframes, void *arg)
  68. {
  69. jack_default_audio_sample_t *out;
  70. jack_nframes_t start_frame = jack_frame_time(client);
  71. out = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port, nframes);
  72. memset(out, 0, sizeof (jack_default_audio_sample_t) * nframes);
  73. while ((client_state == Run) && (jack_frame_time(client) < (start_frame + idle_time))) {}
  74. return 0;
  75. }
  76. /**
  77. * JACK calls this shutdown_callback if the server ever shuts down or
  78. * decides to disconnect the client.
  79. */
  80. void jack_shutdown(void *arg)
  81. {
  82. fprintf(stderr, "JACK shut down, exiting ...\n");
  83. exit (1);
  84. }
  85. int main(int argc, char *argv[])
  86. {
  87. const char **ports;
  88. const char *client_name;
  89. int got_time = 0;
  90. jack_status_t status;
  91. int option_index;
  92. int opt;
  93. const char *options = "t:t:d:c:";
  94. struct option long_options[] =
  95. {
  96. {"time", 1, 0, 't'},
  97. {"name", 1, 0, 'n'},
  98. {"delay", 1, 0, 'd'},
  99. {"cpu", 1, 0, 'c'},
  100. {0, 0, 0, 0}
  101. };
  102. client_name = "jack-cpu";
  103. while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
  104. switch (opt) {
  105. case 'n':
  106. client_name = optarg;
  107. break;
  108. case 't':
  109. time_to_run = atoi(optarg);
  110. break;
  111. case 'd':
  112. time_before_run = atoi(optarg);
  113. break;
  114. case 'c':
  115. percent_cpu = atoi(optarg);
  116. got_time = 1;
  117. break;
  118. default:
  119. fprintf(stderr, "unknown option %c\n", opt);
  120. usage();
  121. }
  122. }
  123. if (!got_time) {
  124. fprintf(stderr, "CPU load not specified ! See usage as following :\n");
  125. usage();
  126. return -1;
  127. }
  128. if (time_to_run != 0)
  129. printf("Running jack-cpu for %d seconds...\n", time_to_run);
  130. /* open a client connection to the JACK server */
  131. client = jack_client_open (client_name, JackNoStartServer, &status);
  132. if (client == NULL) {
  133. fprintf(stderr, "jack_client_open() failed : is jack server running ?\n");
  134. exit(1);
  135. }
  136. cur_buffer_size = jack_get_buffer_size(client);
  137. printf("engine buffer size = %d \n", cur_buffer_size);
  138. printf("engine sample rate: %d Hz\n", jack_get_sample_rate(client));
  139. idle_time = (jack_nframes_t) (cur_buffer_size * percent_cpu / 100);
  140. printf("CPU load applies as %d sample delay.\n", idle_time);
  141. /* tell the JACK server to call `process()' whenever
  142. there is work to be done.
  143. */
  144. jack_set_process_callback(client, process, 0);
  145. /* tell the JACK server to call `jack_shutdown()' if
  146. it ever shuts down, either entirely, or if it
  147. just decides to stop calling us.
  148. */
  149. jack_on_shutdown(client, jack_shutdown, 0);
  150. /* create two ports */
  151. input_port = jack_port_register (client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  152. output_port = jack_port_register (client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  153. printf("registering ports...\n");
  154. if ((input_port == NULL) || (output_port == NULL)) {
  155. fprintf(stderr, "no more JACK ports available\n");
  156. exit(1);
  157. }
  158. if (jack_set_buffer_size_callback(client, update_buffer_size, 0) != 0) {
  159. printf("Error when calling buffer_size_callback !");
  160. return -1;
  161. }
  162. /* Tell the JACK server that we are ready to roll. Our
  163. * process() callback will start running now. */
  164. printf("Activating as jackd client...\n");
  165. if (jack_activate(client)) {
  166. fprintf(stderr, "cannot activate client");
  167. exit(1);
  168. }
  169. /* Connect the ports. You can't do this before the client is
  170. * activated, because we can't make connections to clients
  171. * that aren't running. Note the confusing (but necessary)
  172. * orientation of the driver backend ports: playback ports are
  173. * "input" to the backend, and capture ports are "output" from
  174. * it.
  175. */
  176. ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
  177. if (ports == NULL) {
  178. fprintf(stderr, "no physical capture ports\n");
  179. exit (1);
  180. }
  181. if (jack_connect(client, ports[0], jack_port_name(input_port))) {
  182. fprintf (stderr, "cannot connect input ports\n");
  183. }
  184. jack_free(ports);
  185. ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput);
  186. if (ports == NULL) {
  187. fprintf(stderr, "no physical playback ports\n");
  188. exit(1);
  189. }
  190. if (jack_connect(client, jack_port_name (output_port), ports[0])) {
  191. fprintf(stderr, "cannot connect output ports\n");
  192. }
  193. jack_free(ports);
  194. if (time_before_run == 0) {
  195. client_state = Run;
  196. printf("Activating cpu load...\n");
  197. }
  198. if (time_to_run !=0)
  199. time_before_exit = time_to_run + time_before_run;
  200. while (client_state != Exit) {
  201. if ((time_before_run > 0) && (client_state == Init))
  202. time_before_run--;
  203. sleep(1);
  204. if ((time_before_run < 1) && (client_state != Run)) {
  205. client_state = Run;
  206. printf("Activating cpu load...\n");
  207. }
  208. if (time_to_run != 0)
  209. time_before_exit--;
  210. if (time_before_exit < 1)
  211. client_state = Exit;
  212. }
  213. jack_client_close(client);
  214. printf("Exiting after a %d seconds run.\n", time_to_run);
  215. exit(0);
  216. }