Remove compiler warning
[jack2.git] / tests / jack_cpu.c
blob3a3c66a990040682eb4e0275bbd0b033eec995d3
1 /*
2 Copyright (C) 2005 Samuel TRACOL
3 Copyright (C) 2006 Grame
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 /** @file jack_cpu.c
23 * @brief This client test the capacity for jackd to kick out a to heavy cpu client.
27 #include <stdio.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <getopt.h>
34 #include "jack.h"
36 jack_port_t *input_port;
37 jack_port_t *output_port;
38 jack_client_t *client;
39 unsigned long sr;
40 int idle_time = 0;
41 int percent_cpu = 0;
42 int time_to_run = 0;
43 int time_before_run = 0;
44 int time_before_exit = 1;
45 jack_nframes_t cur_buffer_size;
46 jack_nframes_t start_frame;
47 jack_nframes_t cur_frame;
49 /* a simple state machine for this client */
50 volatile enum {
51 Init,
52 Run,
53 Exit
54 } client_state = Init;
56 void usage()
58 fprintf (stderr, "\n"
59 "usage: jack_cpu \n"
60 " [ --name OR -n client_name ]\n"
61 " [ --time OR -t time_to_run (in seconds) ]\n"
62 " [ --delay OR -d delay_before_cpuload__is_applied (in seconds) ]\n"
63 " --cpu OR -c percent_cpu_load (1-99)\n"
67 JackBufferSizeCallback update_buffer_size()
69 cur_buffer_size = jack_get_buffer_size(client);
70 printf("Buffer size = %d \n", cur_buffer_size);
71 idle_time = (int) (cur_buffer_size * percent_cpu / 100);
72 printf("CPU load applies as %d sample delay.\n",idle_time);
73 return 0;
76 /**
77 * The process callback for this JACK application is called in a
78 * special realtime thread once for each audio cycle.
80 * This client follows a simple rule: when the JACK transport is
81 * running, copy the input port to the output. When it stops, exit.
84 int process(jack_nframes_t nframes, void *arg)
86 jack_default_audio_sample_t *in, *out;
87 start_frame = jack_frame_time(client);
89 in = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port, nframes);
90 out = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port, nframes);
91 memset(out, 0, sizeof (jack_default_audio_sample_t) * nframes);
93 while ((client_state == Run) && (jack_frame_time(client) < (start_frame + idle_time))) {}
94 return 0;
97 /**
98 * JACK calls this shutdown_callback if the server ever shuts down or
99 * decides to disconnect the client.
101 void jack_shutdown(void *arg)
103 printf("Jack_cpu has been kicked out by jackd !");
104 exit (1);
107 int main(int argc, char *argv[])
109 const char **ports;
110 const char *client_name;
111 int got_time = 0;
113 int option_index;
114 int opt;
115 const char *options = "t:t:d:c:";
116 struct option long_options[] =
118 {"time", 1, 0, 't'},
119 {"name", 1, 0, 'n'},
120 {"delay", 1, 0, 'd'},
121 {"cpu", 1, 0, 'c'},
122 {0, 0, 0, 0}
125 client_name = "jack-cpu";
126 while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
127 switch (opt) {
128 case 'n':
129 client_name = optarg;
130 break;
131 case 't':
132 time_to_run = atoi(optarg);
133 break;
134 case 'd':
135 time_before_run = atoi(optarg);
136 break;
137 case 'c':
138 percent_cpu = atoi(optarg);
139 got_time = 1;
140 break;
141 default:
142 fprintf(stderr, "unknown option %c\n", opt);
143 usage();
147 if (!got_time) {
148 fprintf(stderr, "CPU load not specified ! See usage as following :\n");
149 usage();
150 return -1;
153 if (time_to_run != 0)
154 printf("Running jack-cpu for %d seconds...\n", time_to_run);
156 /* open a client connection to the JACK server */
158 client = jack_client_new(client_name);
160 if (client == NULL) {
161 fprintf(stderr, "jack_client_open() failed : is jack server running ?\n");
162 exit(1);
165 /* tell the JACK server to call `process()' whenever
166 there is work to be done.
169 jack_set_process_callback(client, process, 0);
171 /* tell the JACK server to call `jack_shutdown()' if
172 it ever shuts down, either entirely, or if it
173 just decides to stop calling us.
176 jack_on_shutdown(client, jack_shutdown, 0);
178 /* display the current sample rate.
181 printf("engine sample rate: %d Hz\n", jack_get_sample_rate(client));
182 printf("computing time in samples : %d \n", idle_time);
184 /* create two ports */
186 input_port = jack_port_register (client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
187 output_port = jack_port_register (client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
189 printf("registering ports...\n");
190 if ((input_port == NULL) || (output_port == NULL)) {
191 fprintf(stderr, "no more JACK ports available\n");
192 exit(1);
195 if (jack_set_buffer_size_callback(client, update_buffer_size(), 0) != 0) {
196 printf("Error when calling buffer_size_callback !");
197 return -1;
200 if (time_before_run == 0)
201 client_state = Run;
202 /* Tell the JACK server that we are ready to roll. Our
203 * process() callback will start running now. */
205 printf("Activating as jackd client...\n");
206 if (jack_activate(client)) {
207 fprintf(stderr, "cannot activate client");
208 exit(1);
211 /* Connect the ports. You can't do this before the client is
212 * activated, because we can't make connections to clients
213 * that aren't running. Note the confusing (but necessary)
214 * orientation of the driver backend ports: playback ports are
215 * "input" to the backend, and capture ports are "output" from
216 * it.
219 ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
220 if (ports == NULL) {
221 fprintf(stderr, "no physical capture ports\n");
222 exit (1);
225 if (jack_connect(client, ports[0], jack_port_name(input_port))) {
226 fprintf (stderr, "cannot connect input ports\n");
228 free(ports);
230 ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput);
231 if (ports == NULL) {
232 fprintf(stderr, "no physical playback ports\n");
233 exit(1);
236 if (jack_connect(client, jack_port_name (output_port), ports[0])) {
237 fprintf(stderr, "cannot connect output ports\n");
239 free(ports);
241 if (time_before_run == 0) {
242 client_state = Run;
243 printf("Activating cpu load...\n");
246 if (time_to_run !=0)
247 time_before_exit = time_to_run + time_before_run;
249 while (client_state != Exit) {
250 if ((time_before_run > 0) && (client_state == Init))
251 time_before_run--;
252 sleep(1);
253 if ((time_before_run < 1) && (client_state != Run)) {
254 client_state = Run;
255 printf("Activating cpu load...\n");
257 if (time_to_run != 0)
258 time_before_exit--;
259 if (time_before_exit < 1)
260 client_state = Exit;
262 jack_client_close(client);
263 printf("Exiting after a %d seconds run.\n", time_to_run);
264 exit(0);