Merge branch 'develop' of github.com:jackaudio/jack2 into develop
[jack2.git] / example-clients / simple_session_client.c
blob67a7cbd2ad0e32d03dae7bec2c100a08d5b2f395
1 /** @file simple_session_client.c
3 * @brief This simple client demonstrates the most basic features of JACK
4 * as they would be used by many applications.
5 * this version also adds session manager functionality.
6 */
8 #include <stdio.h>
9 #include <errno.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <inttypes.h>
15 #include <jack/jack.h>
16 #include <jack/types.h>
17 #include <jack/session.h>
19 jack_port_t *input_port;
20 jack_port_t *output_port;
21 jack_client_t *client;
23 int simple_quit = 0;
25 /**
26 * The process callback for this JACK application is called in a
27 * special realtime thread once for each audio cycle.
29 * This client does nothing more than copy data from its input
30 * port to its output port. It will exit when stopped by
31 * the user (e.g. using Ctrl-C on a unix-ish operating system)
33 int
34 process (jack_nframes_t nframes, void *arg)
36 jack_default_audio_sample_t *in, *out;
38 in = jack_port_get_buffer (input_port, nframes);
39 out = jack_port_get_buffer (output_port, nframes);
40 memcpy (out, in,
41 sizeof (jack_default_audio_sample_t) * nframes);
43 return 0;
46 void
47 session_callback (jack_session_event_t *event, void *arg)
49 char retval[100];
50 printf ("session notification\n");
51 printf ("path %s, uuid %s, type: %s\n", event->session_dir, event->client_uuid, event->type == JackSessionSave ? "save" : "quit");
54 snprintf (retval, 100, "jack_simple_session_client %s", event->client_uuid);
55 event->command_line = strdup (retval);
57 jack_session_reply( client, event );
59 if (event->type == JackSessionSaveAndQuit) {
60 simple_quit = 1;
63 jack_session_event_free (event);
66 /**
67 * JACK calls this shutdown_callback if the server ever shuts down or
68 * decides to disconnect the client.
70 void
71 jack_shutdown (void *arg)
73 exit (1);
76 int
77 main (int argc, char *argv[])
79 const char **ports;
80 const char *client_name = "simple";
81 jack_status_t status;
83 /* open a client connection to the JACK server */
85 if( argc == 1 )
86 client = jack_client_open (client_name, JackNullOption, &status );
87 else if( argc == 2 )
88 client = jack_client_open (client_name, JackSessionID, &status, argv[1] );
90 if (client == NULL) {
91 fprintf (stderr, "jack_client_open() failed, "
92 "status = 0x%2.0x\n", status);
93 if (status & JackServerFailed) {
94 fprintf (stderr, "Unable to connect to JACK server\n");
96 exit (1);
98 if (status & JackServerStarted) {
99 fprintf (stderr, "JACK server started\n");
101 if (status & JackNameNotUnique) {
102 client_name = jack_get_client_name(client);
103 fprintf (stderr, "unique name `%s' assigned\n", client_name);
106 /* tell the JACK server to call `process()' whenever
107 there is work to be done.
110 jack_set_process_callback (client, process, 0);
112 /* tell the JACK server to call `jack_shutdown()' if
113 it ever shuts down, either entirely, or if it
114 just decides to stop calling us.
117 jack_on_shutdown (client, jack_shutdown, 0);
119 /* tell the JACK server to call `session_callback()' if
120 the session is saved.
123 jack_set_session_callback (client, session_callback, NULL);
125 /* display the current sample rate.
128 printf ("engine sample rate: %" PRIu32 "\n",
129 jack_get_sample_rate (client));
131 /* create two ports */
133 input_port = jack_port_register (client, "input",
134 JACK_DEFAULT_AUDIO_TYPE,
135 JackPortIsInput, 0);
136 output_port = jack_port_register (client, "output",
137 JACK_DEFAULT_AUDIO_TYPE,
138 JackPortIsOutput, 0);
140 if ((input_port == NULL) || (output_port == NULL)) {
141 fprintf(stderr, "no more JACK ports available\n");
142 exit (1);
145 /* Tell the JACK server that we are ready to roll. Our
146 * process() callback will start running now. */
148 if (jack_activate (client)) {
149 fprintf (stderr, "cannot activate client");
150 exit (1);
153 /* Connect the ports. You can't do this before the client is
154 * activated, because we can't make connections to clients
155 * that aren't running. Note the confusing (but necessary)
156 * orientation of the driver backend ports: playback ports are
157 * "input" to the backend, and capture ports are "output" from
158 * it.
162 /* only do the autoconnect when not reloading from a session.
163 * in case of a session reload, the SM will restore our connections
166 if (argc==1) {
168 ports = jack_get_ports (client, NULL, NULL,
169 JackPortIsPhysical|JackPortIsOutput);
170 if (ports == NULL) {
171 fprintf(stderr, "no physical capture ports\n");
172 exit (1);
175 if (jack_connect (client, ports[0], jack_port_name (input_port))) {
176 fprintf (stderr, "cannot connect input ports\n");
179 free (ports);
181 ports = jack_get_ports (client, NULL, NULL,
182 JackPortIsPhysical|JackPortIsInput);
183 if (ports == NULL) {
184 fprintf(stderr, "no physical playback ports\n");
185 exit (1);
188 if (jack_connect (client, jack_port_name (output_port), ports[0])) {
189 fprintf (stderr, "cannot connect output ports\n");
192 free (ports);
195 /* keep running until until we get a quit event */
197 while (!simple_quit)
198 #ifdef WIN32
199 Sleep(1*1000);
200 #else
201 sleep(1);
202 #endif
204 jack_client_close (client);
205 exit (0);