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.
23 * @brief This client test the capacity for jackd to kick out a to heavy cpu client.
36 jack_port_t
*input_port
;
37 jack_port_t
*output_port
;
38 jack_client_t
*client
;
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 */
54 } client_state
= Init
;
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
);
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
))) {}
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 !");
107 int main(int argc
, char *argv
[])
110 const char *client_name
;
115 const char *options
= "t:t:d:c:";
116 struct option long_options
[] =
120 {"delay", 1, 0, 'd'},
125 client_name
= "jack-cpu";
126 while ((opt
= getopt_long (argc
, argv
, options
, long_options
, &option_index
)) != EOF
) {
129 client_name
= optarg
;
132 time_to_run
= atoi(optarg
);
135 time_before_run
= atoi(optarg
);
138 percent_cpu
= atoi(optarg
);
142 fprintf(stderr
, "unknown option %c\n", opt
);
148 fprintf(stderr
, "CPU load not specified ! See usage as following :\n");
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");
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");
195 if (jack_set_buffer_size_callback(client
, update_buffer_size(), 0) != 0) {
196 printf("Error when calling buffer_size_callback !");
200 if (time_before_run
== 0)
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");
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
219 ports
= jack_get_ports (client
, NULL
, NULL
, JackPortIsPhysical
|JackPortIsOutput
);
221 fprintf(stderr
, "no physical capture ports\n");
225 if (jack_connect(client
, ports
[0], jack_port_name(input_port
))) {
226 fprintf (stderr
, "cannot connect input ports\n");
230 ports
= jack_get_ports (client
, NULL
, NULL
, JackPortIsPhysical
|JackPortIsInput
);
232 fprintf(stderr
, "no physical playback ports\n");
236 if (jack_connect(client
, jack_port_name (output_port
), ports
[0])) {
237 fprintf(stderr
, "cannot connect output ports\n");
241 if (time_before_run
== 0) {
243 printf("Activating cpu load...\n");
247 time_before_exit
= time_to_run
+ time_before_run
;
249 while (client_state
!= Exit
) {
250 if ((time_before_run
> 0) && (client_state
== Init
))
253 if ((time_before_run
< 1) && (client_state
!= Run
)) {
255 printf("Activating cpu load...\n");
257 if (time_to_run
!= 0)
259 if (time_before_exit
< 1)
262 jack_client_close(client
);
263 printf("Exiting after a %d seconds run.\n", time_to_run
);