cleanup...
[jack.git] / example-clients / transport_client.c
blobd5a7147d5beb44daf2813726301f6d5bb2f5525d
1 /** @file transport_client.c
3 * @brief This client demonstrates very simple use of the JACK
4 * transport API. Compare it with the simple_client example,
5 * which is even simpler. It also demonstrates taking a client
6 * name and optionally server name from the command line, rather
7 * than hard-coding either of these names.
8 */
10 #include <stdio.h>
11 #include <errno.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <string.h>
16 #include <jack/jack.h>
18 jack_port_t *input_port;
19 jack_port_t *output_port;
20 jack_client_t *client;
22 /* a simple state machine for this client */
23 volatile enum {
24 Init,
25 Run,
26 Exit
27 } client_state = Init;
29 /**
30 * The process callback for this JACK application is called in a
31 * special realtime thread once for each audio cycle.
33 * This client follows a simple rule: when the JACK transport is
34 * running, copy the input port to the output. When it stops, exit.
36 int
37 process (jack_nframes_t nframes, void *arg)
39 jack_default_audio_sample_t *in, *out;
40 jack_transport_state_t ts = jack_transport_query(client, NULL);
42 if (ts == JackTransportRolling) {
44 if (client_state == Init)
45 client_state = Run;
47 in = jack_port_get_buffer (input_port, nframes);
48 out = jack_port_get_buffer (output_port, nframes);
49 memcpy (out, in,
50 sizeof (jack_default_audio_sample_t) * nframes);
52 } else if (ts == JackTransportStopped) {
54 if (client_state == Run)
55 client_state = Exit;
58 return 0;
61 /**
62 * JACK calls this shutdown_callback if the server ever shuts down or
63 * decides to disconnect the client.
65 void
66 jack_shutdown (void *arg)
68 exit (1);
71 int
72 main (int argc, char *argv[])
74 const char **ports;
75 const char *client_name;
76 const char *server_name = NULL;
77 jack_options_t options = JackNullOption;
78 jack_status_t status;
80 if (argc >= 2) { /* client name specified? */
81 client_name = argv[1];
82 if (argc >= 3) { /* server name specified? */
83 server_name = argv[2];
84 options |= JackServerName;
86 } else { /* use basename of argv[0] */
87 client_name = strrchr(argv[0], '/');
88 if (client_name == 0) {
89 client_name = argv[0];
90 } else {
91 client_name++;
95 /* open a client connection to the JACK server */
97 client = jack_client_open (client_name, options, &status, server_name);
98 if (client == NULL) {
99 fprintf (stderr, "jack_client_open() failed, "
100 "status = 0x%2.0x\n", status);
101 if (status & JackServerFailed) {
102 fprintf (stderr, "Unable to connect to JACK server\n");
104 exit (1);
106 if (status & JackServerStarted) {
107 fprintf (stderr, "JACK server started\n");
109 if (status & JackNameNotUnique) {
110 client_name = jack_get_client_name(client);
111 fprintf (stderr, "unique name `%s' assigned\n", client_name);
114 /* tell the JACK server to call `process()' whenever
115 there is work to be done.
118 jack_set_process_callback (client, process, 0);
120 /* tell the JACK server to call `jack_shutdown()' if
121 it ever shuts down, either entirely, or if it
122 just decides to stop calling us.
125 jack_on_shutdown (client, jack_shutdown, 0);
127 /* display the current sample rate.
130 printf ("engine sample rate: %" PRIu32 "\n",
131 jack_get_sample_rate (client));
133 /* create two ports */
135 input_port = jack_port_register (client, "input",
136 JACK_DEFAULT_AUDIO_TYPE,
137 JackPortIsInput, 0);
138 output_port = jack_port_register (client, "output",
139 JACK_DEFAULT_AUDIO_TYPE,
140 JackPortIsOutput, 0);
142 if ((input_port == NULL) || (output_port == NULL)) {
143 fprintf(stderr, "no more JACK ports available\n");
144 exit (1);
147 /* Tell the JACK server that we are ready to roll. Our
148 * process() callback will start running now. */
150 if (jack_activate (client)) {
151 fprintf (stderr, "cannot activate client");
152 exit (1);
155 /* Connect the ports. You can't do this before the client is
156 * activated, because we can't make connections to clients
157 * that aren't running. Note the confusing (but necessary)
158 * orientation of the driver backend ports: playback ports are
159 * "input" to the backend, and capture ports are "output" from
160 * it.
163 ports = jack_get_ports (client, NULL, NULL,
164 JackPortIsPhysical|JackPortIsOutput);
165 if (ports == NULL) {
166 fprintf(stderr, "no physical capture ports\n");
167 exit (1);
170 if (jack_connect (client, ports[0], jack_port_name (input_port))) {
171 fprintf (stderr, "cannot connect input ports\n");
174 free (ports);
176 ports = jack_get_ports (client, NULL, NULL,
177 JackPortIsPhysical|JackPortIsInput);
178 if (ports == NULL) {
179 fprintf(stderr, "no physical playback ports\n");
180 exit (1);
183 if (jack_connect (client, jack_port_name (output_port), ports[0])) {
184 fprintf (stderr, "cannot connect output ports\n");
187 free (ports);
189 /* keep running until the transport stops */
191 while (client_state != Exit) {
192 sleep (1);
195 jack_client_close (client);
196 exit (0);