Correct JackPortAudioDriver.
[jack2.git] / example-clients / thru_client.c
blobd47fbe1532a26daff4138435a96a1a4fd0eefb3e
1 /** @file thru_client.c
3 * @brief This simple through client demonstrates the basic features of JACK
4 * as they would be used by many applications.
5 */
7 #include <stdio.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <math.h>
12 #include <signal.h>
13 #ifndef WIN32
14 #include <unistd.h>
15 #endif
16 #include <jack/jack.h>
18 jack_port_t **input_ports;
19 jack_port_t **output_ports;
20 jack_client_t *client;
22 static void signal_handler ( int sig )
24 jack_client_close ( client );
25 fprintf ( stderr, "signal received, exiting ...\n" );
26 exit ( 0 );
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.
37 int
38 process ( jack_nframes_t nframes, void *arg )
40 int i;
41 jack_default_audio_sample_t *in, *out;
42 for ( i = 0; i < 2; i++ )
44 in = jack_port_get_buffer ( input_ports[i], nframes );
45 out = jack_port_get_buffer ( output_ports[i], nframes );
46 memcpy ( out, in, nframes * sizeof ( jack_default_audio_sample_t ) );
48 return 0;
51 /**
52 * JACK calls this shutdown_callback if the server ever shuts down or
53 * decides to disconnect the client.
55 void
56 jack_shutdown ( void *arg )
58 free ( input_ports );
59 free ( output_ports );
60 exit ( 1 );
63 int
64 main ( int argc, char *argv[] )
66 int i;
67 const char **ports;
68 const char *client_name;
69 const char *server_name = NULL;
70 jack_options_t options = JackNullOption;
71 jack_status_t status;
73 if ( argc >= 2 ) /* client name specified? */
75 client_name = argv[1];
76 if ( argc >= 3 ) /* server name specified? */
78 server_name = argv[2];
79 options |= JackServerName;
82 else /* use basename of argv[0] */
84 client_name = strrchr ( argv[0], '/' );
85 if ( client_name == 0 )
87 client_name = argv[0];
89 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 )
100 fprintf ( stderr, "jack_client_open() failed, "
101 "status = 0x%2.0x\n", status );
102 if ( status & JackServerFailed )
104 fprintf ( stderr, "Unable to connect to JACK server\n" );
106 exit ( 1 );
108 if ( status & JackServerStarted )
110 fprintf ( stderr, "JACK server started\n" );
112 if ( status & JackNameNotUnique )
114 client_name = jack_get_client_name ( client );
115 fprintf ( stderr, "unique name `%s' assigned\n", client_name );
118 /* tell the JACK server to call `process()' whenever
119 there is work to be done.
122 jack_set_process_callback ( client, process, 0 );
124 /* tell the JACK server to call `jack_shutdown()' if
125 it ever shuts down, either entirely, or if it
126 just decides to stop calling us.
129 jack_on_shutdown ( client, jack_shutdown, 0 );
131 /* create two ports pairs*/
132 input_ports = ( jack_port_t** ) calloc ( 2, sizeof ( jack_port_t* ) );
133 output_ports = ( jack_port_t** ) calloc ( 2, sizeof ( jack_port_t* ) );
135 char port_name[16];
136 for ( i = 0; i < 2; i++ )
138 sprintf ( port_name, "input_%d", i + 1 );
139 input_ports[i] = jack_port_register ( client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0 );
140 sprintf ( port_name, "output_%d", i + 1 );
141 output_ports[i] = jack_port_register ( client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
142 if ( ( input_ports[i] == NULL ) || ( output_ports[i] == NULL ) )
144 fprintf ( stderr, "no more JACK ports available\n" );
145 exit ( 1 );
149 /* Tell the JACK server that we are ready to roll. Our
150 * process() callback will start running now. */
152 if ( jack_activate ( client ) )
154 fprintf ( stderr, "cannot activate client" );
155 exit ( 1 );
158 /* Connect the ports. You can't do this before the client is
159 * activated, because we can't make connections to clients
160 * that aren't running. Note the confusing (but necessary)
161 * orientation of the driver backend ports: playback ports are
162 * "input" to the backend, and capture ports are "output" from
163 * it.
166 ports = jack_get_ports ( client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput );
167 if ( ports == NULL )
169 fprintf ( stderr, "no physical capture ports\n" );
170 exit ( 1 );
173 for ( i = 0; i < 2; i++ )
174 if ( jack_connect ( client, ports[i], jack_port_name ( input_ports[i] ) ) )
175 fprintf ( stderr, "cannot connect input ports\n" );
177 free ( ports );
179 ports = jack_get_ports ( client, NULL, NULL, JackPortIsPhysical|JackPortIsInput );
180 if ( ports == NULL )
182 fprintf ( stderr, "no physical playback ports\n" );
183 exit ( 1 );
186 for ( i = 0; i < 2; i++ )
187 if ( jack_connect ( client, jack_port_name ( output_ports[i] ), ports[i] ) )
188 fprintf ( stderr, "cannot connect input ports\n" );
190 free ( ports );
192 /* install a signal handler to properly quits jack client */
193 #ifdef WIN32
194 signal ( SIGINT, signal_handler );
195 signal ( SIGABRT, signal_handler );
196 signal ( SIGTERM, signal_handler );
197 #else
198 signal ( SIGQUIT, signal_handler );
199 signal ( SIGTERM, signal_handler );
200 signal ( SIGHUP, signal_handler );
201 signal ( SIGINT, signal_handler );
202 #endif
204 /* keep running until the transport stops */
206 while (1)
208 #ifdef WIN32
209 Sleep ( 1000 );
210 #else
211 sleep ( 1 );
212 #endif
215 jack_client_close ( client );
216 exit ( 0 );