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.

266 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.h"
  28. jack_port_t *input_port;
  29. jack_port_t *output_port;
  30. jack_client_t *client;
  31. unsigned long sr;
  32. int 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. jack_nframes_t start_frame;
  39. jack_nframes_t cur_frame;
  40. /* a simple state machine for this client */
  41. volatile enum {
  42. Init,
  43. Run,
  44. Exit
  45. } client_state = Init;
  46. void usage()
  47. {
  48. fprintf (stderr, "\n"
  49. "usage: jack_cpu \n"
  50. " [ --name OR -n client_name ]\n"
  51. " [ --time OR -t time_to_run (in seconds) ]\n"
  52. " [ --delay OR -d delay_before_cpuload__is_applied (in seconds) ]\n"
  53. " --cpu OR -c percent_cpu_load (1-99)\n"
  54. );
  55. }
  56. JackBufferSizeCallback update_buffer_size()
  57. {
  58. cur_buffer_size = jack_get_buffer_size(client);
  59. printf("Buffer size = %d \n", cur_buffer_size);
  60. idle_time = (int) (cur_buffer_size * percent_cpu / 100);
  61. printf("CPU load applies as %d sample delay.\n",idle_time);
  62. return 0;
  63. }
  64. /**
  65. * The process callback for this JACK application is called in a
  66. * special realtime thread once for each audio cycle.
  67. *
  68. * This client follows a simple rule: when the JACK transport is
  69. * running, copy the input port to the output. When it stops, exit.
  70. */
  71. int process(jack_nframes_t nframes, void *arg)
  72. {
  73. jack_default_audio_sample_t *in, *out;
  74. start_frame = jack_frame_time(client);
  75. in = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port, nframes);
  76. out = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port, nframes);
  77. memset(out, 0, sizeof (jack_default_audio_sample_t) * nframes);
  78. while ((client_state == Run) && (jack_frame_time(client) < (start_frame + idle_time))) {}
  79. return 0;
  80. }
  81. /**
  82. * JACK calls this shutdown_callback if the server ever shuts down or
  83. * decides to disconnect the client.
  84. */
  85. void jack_shutdown(void *arg)
  86. {
  87. printf("Jack_cpu has been kicked out by jackd !");
  88. exit (1);
  89. }
  90. int main(int argc, char *argv[])
  91. {
  92. const char **ports;
  93. const char *client_name;
  94. int got_time = 0;
  95. int option_index;
  96. int opt;
  97. const char *options = "t:t:d:c:";
  98. struct option long_options[] =
  99. {
  100. {"time", 1, 0, 't'},
  101. {"name", 1, 0, 'n'},
  102. {"delay", 1, 0, 'd'},
  103. {"cpu", 1, 0, 'c'},
  104. {0, 0, 0, 0}
  105. };
  106. client_name = "jack-cpu";
  107. while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
  108. switch (opt) {
  109. case 'n':
  110. client_name = optarg;
  111. break;
  112. case 't':
  113. time_to_run = atoi(optarg);
  114. break;
  115. case 'd':
  116. time_before_run = atoi(optarg);
  117. break;
  118. case 'c':
  119. percent_cpu = atoi(optarg);
  120. got_time = 1;
  121. break;
  122. default:
  123. fprintf(stderr, "unknown option %c\n", opt);
  124. usage();
  125. }
  126. }
  127. if (!got_time) {
  128. fprintf(stderr, "CPU load not specified ! See usage as following :\n");
  129. usage();
  130. return -1;
  131. }
  132. if (time_to_run != 0)
  133. printf("Running jack-cpu for %d seconds...\n", time_to_run);
  134. /* open a client connection to the JACK server */
  135. client = jack_client_new(client_name);
  136. if (client == NULL) {
  137. fprintf(stderr, "jack_client_open() failed : is jack server running ?\n");
  138. exit(1);
  139. }
  140. /* tell the JACK server to call `process()' whenever
  141. there is work to be done.
  142. */
  143. jack_set_process_callback(client, process, 0);
  144. /* tell the JACK server to call `jack_shutdown()' if
  145. it ever shuts down, either entirely, or if it
  146. just decides to stop calling us.
  147. */
  148. jack_on_shutdown(client, jack_shutdown, 0);
  149. /* display the current sample rate.
  150. */
  151. printf("engine sample rate: %d Hz\n", jack_get_sample_rate(client));
  152. printf("computing time in samples : %d \n", idle_time);
  153. /* create two ports */
  154. input_port = jack_port_register (client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  155. output_port = jack_port_register (client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  156. printf("registering ports...\n");
  157. if ((input_port == NULL) || (output_port == NULL)) {
  158. fprintf(stderr, "no more JACK ports available\n");
  159. exit(1);
  160. }
  161. if (jack_set_buffer_size_callback(client, update_buffer_size(), 0) != 0) {
  162. printf("Error when calling buffer_size_callback !");
  163. return -1;
  164. }
  165. if (time_before_run == 0)
  166. client_state = Run;
  167. /* Tell the JACK server that we are ready to roll. Our
  168. * process() callback will start running now. */
  169. printf("Activating as jackd client...\n");
  170. if (jack_activate(client)) {
  171. fprintf(stderr, "cannot activate client");
  172. exit(1);
  173. }
  174. /* Connect the ports. You can't do this before the client is
  175. * activated, because we can't make connections to clients
  176. * that aren't running. Note the confusing (but necessary)
  177. * orientation of the driver backend ports: playback ports are
  178. * "input" to the backend, and capture ports are "output" from
  179. * it.
  180. */
  181. ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
  182. if (ports == NULL) {
  183. fprintf(stderr, "no physical capture ports\n");
  184. exit (1);
  185. }
  186. if (jack_connect(client, ports[0], jack_port_name(input_port))) {
  187. fprintf (stderr, "cannot connect input ports\n");
  188. }
  189. free(ports);
  190. ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput);
  191. if (ports == NULL) {
  192. fprintf(stderr, "no physical playback ports\n");
  193. exit(1);
  194. }
  195. if (jack_connect(client, jack_port_name (output_port), ports[0])) {
  196. fprintf(stderr, "cannot connect output ports\n");
  197. }
  198. free(ports);
  199. if (time_before_run == 0) {
  200. client_state = Run;
  201. printf("Activating cpu load...\n");
  202. }
  203. if (time_to_run !=0)
  204. time_before_exit = time_to_run + time_before_run;
  205. while (client_state != Exit) {
  206. if ((time_before_run > 0) && (client_state == Init))
  207. time_before_run--;
  208. sleep(1);
  209. if ((time_before_run < 1) && (client_state != Run)) {
  210. client_state = Run;
  211. printf("Activating cpu load...\n");
  212. }
  213. if (time_to_run != 0)
  214. time_before_exit--;
  215. if (time_before_exit < 1)
  216. client_state = Exit;
  217. }
  218. jack_client_close(client);
  219. printf("Exiting after a %d seconds run.\n", time_to_run);
  220. exit(0);
  221. }