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.

261 lines
6.9KB

  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 *in, *out;
  70. jack_nframes_t start_frame = jack_frame_time(client);
  71. in = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port, nframes);
  72. out = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port, nframes);
  73. memset(out, 0, sizeof (jack_default_audio_sample_t) * nframes);
  74. while ((client_state == Run) && (jack_frame_time(client) < (start_frame + idle_time))) {}
  75. return 0;
  76. }
  77. /**
  78. * JACK calls this shutdown_callback if the server ever shuts down or
  79. * decides to disconnect the client.
  80. */
  81. void jack_shutdown(void *arg)
  82. {
  83. fprintf(stderr, "JACK shut down, exiting ...\n");
  84. exit (1);
  85. }
  86. int main(int argc, char *argv[])
  87. {
  88. const char **ports;
  89. const char *client_name;
  90. int got_time = 0;
  91. jack_status_t status;
  92. int option_index;
  93. int opt;
  94. const char *options = "t:t:d:c:";
  95. struct option long_options[] =
  96. {
  97. {"time", 1, 0, 't'},
  98. {"name", 1, 0, 'n'},
  99. {"delay", 1, 0, 'd'},
  100. {"cpu", 1, 0, 'c'},
  101. {0, 0, 0, 0}
  102. };
  103. client_name = "jack-cpu";
  104. while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
  105. switch (opt) {
  106. case 'n':
  107. client_name = optarg;
  108. break;
  109. case 't':
  110. time_to_run = atoi(optarg);
  111. break;
  112. case 'd':
  113. time_before_run = atoi(optarg);
  114. break;
  115. case 'c':
  116. percent_cpu = atoi(optarg);
  117. got_time = 1;
  118. break;
  119. default:
  120. fprintf(stderr, "unknown option %c\n", opt);
  121. usage();
  122. }
  123. }
  124. if (!got_time) {
  125. fprintf(stderr, "CPU load not specified ! See usage as following :\n");
  126. usage();
  127. return -1;
  128. }
  129. if (time_to_run != 0)
  130. printf("Running jack-cpu for %d seconds...\n", time_to_run);
  131. /* open a client connection to the JACK server */
  132. client = jack_client_open (client_name, JackNoStartServer, &status);
  133. if (client == NULL) {
  134. fprintf(stderr, "jack_client_open() failed : is jack server running ?\n");
  135. exit(1);
  136. }
  137. cur_buffer_size = jack_get_buffer_size(client);
  138. printf("engine buffer size = %d \n", cur_buffer_size);
  139. printf("engine sample rate: %d Hz\n", jack_get_sample_rate(client));
  140. idle_time = (jack_nframes_t) (cur_buffer_size * percent_cpu / 100);
  141. printf("CPU load applies as %d sample delay.\n", idle_time);
  142. /* tell the JACK server to call `process()' whenever
  143. there is work to be done.
  144. */
  145. jack_set_process_callback(client, process, 0);
  146. /* tell the JACK server to call `jack_shutdown()' if
  147. it ever shuts down, either entirely, or if it
  148. just decides to stop calling us.
  149. */
  150. jack_on_shutdown(client, jack_shutdown, 0);
  151. /* create two ports */
  152. input_port = jack_port_register (client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  153. output_port = jack_port_register (client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  154. printf("registering ports...\n");
  155. if ((input_port == NULL) || (output_port == NULL)) {
  156. fprintf(stderr, "no more JACK ports available\n");
  157. exit(1);
  158. }
  159. if (jack_set_buffer_size_callback(client, update_buffer_size, 0) != 0) {
  160. printf("Error when calling buffer_size_callback !");
  161. return -1;
  162. }
  163. /* Tell the JACK server that we are ready to roll. Our
  164. * process() callback will start running now. */
  165. printf("Activating as jackd client...\n");
  166. if (jack_activate(client)) {
  167. fprintf(stderr, "cannot activate client");
  168. exit(1);
  169. }
  170. /* Connect the ports. You can't do this before the client is
  171. * activated, because we can't make connections to clients
  172. * that aren't running. Note the confusing (but necessary)
  173. * orientation of the driver backend ports: playback ports are
  174. * "input" to the backend, and capture ports are "output" from
  175. * it.
  176. */
  177. ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
  178. if (ports == NULL) {
  179. fprintf(stderr, "no physical capture ports\n");
  180. exit (1);
  181. }
  182. if (jack_connect(client, ports[0], jack_port_name(input_port))) {
  183. fprintf (stderr, "cannot connect input ports\n");
  184. }
  185. jack_free(ports);
  186. ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput);
  187. if (ports == NULL) {
  188. fprintf(stderr, "no physical playback ports\n");
  189. exit(1);
  190. }
  191. if (jack_connect(client, jack_port_name (output_port), ports[0])) {
  192. fprintf(stderr, "cannot connect output ports\n");
  193. }
  194. jack_free(ports);
  195. if (time_before_run == 0) {
  196. client_state = Run;
  197. printf("Activating cpu load...\n");
  198. }
  199. if (time_to_run !=0)
  200. time_before_exit = time_to_run + time_before_run;
  201. while (client_state != Exit) {
  202. if ((time_before_run > 0) && (client_state == Init))
  203. time_before_run--;
  204. sleep(1);
  205. if ((time_before_run < 1) && (client_state != Run)) {
  206. client_state = Run;
  207. printf("Activating cpu load...\n");
  208. }
  209. if (time_to_run != 0)
  210. time_before_exit--;
  211. if (time_before_exit < 1)
  212. client_state = Exit;
  213. }
  214. jack_client_close(client);
  215. printf("Exiting after a %d seconds run.\n", time_to_run);
  216. exit(0);
  217. }