Correct JackPortAudioDriver.
[jack2.git] / example-clients / capture_client.c
blob559dc07dc89154d1692c425be8dd97be1fdf8f6b
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2003 Jack O'Quin
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.
19 * 2002/08/23 - modify for libsndfile 1.0.0 <andy@alsaplayer.org>
20 * 2003/05/26 - use ringbuffers - joq
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <sndfile.h>
29 #include <pthread.h>
30 #include <signal.h>
31 #include <getopt.h>
32 #include <jack/jack.h>
33 #include <jack/ringbuffer.h>
35 typedef struct _thread_info {
36 pthread_t thread_id;
37 SNDFILE *sf;
38 jack_nframes_t duration;
39 jack_nframes_t rb_size;
40 jack_client_t *client;
41 unsigned int channels;
42 int bitdepth;
43 char *path;
44 volatile int can_capture;
45 volatile int can_process;
46 volatile int status;
47 } jack_thread_info_t;
49 /* JACK data */
50 unsigned int nports;
51 jack_port_t **ports;
52 jack_default_audio_sample_t **in;
53 jack_nframes_t nframes;
54 const size_t sample_size = sizeof(jack_default_audio_sample_t);
56 /* Synchronization between process thread and disk thread. */
57 #define DEFAULT_RB_SIZE 16384 /* ringbuffer size in frames */
58 jack_ringbuffer_t *rb;
59 pthread_mutex_t disk_thread_lock = PTHREAD_MUTEX_INITIALIZER;
60 pthread_cond_t data_ready = PTHREAD_COND_INITIALIZER;
61 long overruns = 0;
62 jack_client_t *client;
64 static void signal_handler(int sig)
66 jack_client_close(client);
67 fprintf(stderr, "signal received, exiting ...\n");
68 exit(0);
71 static void *
72 disk_thread (void *arg)
74 jack_thread_info_t *info = (jack_thread_info_t *) arg;
75 static jack_nframes_t total_captured = 0;
76 jack_nframes_t samples_per_frame = info->channels;
77 size_t bytes_per_frame = samples_per_frame * sample_size;
78 void *framebuf = malloc (bytes_per_frame);
80 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
81 pthread_mutex_lock (&disk_thread_lock);
83 info->status = 0;
85 while (1) {
87 /* Write the data one frame at a time. This is
88 * inefficient, but makes things simpler. */
89 while (info->can_capture &&
90 (jack_ringbuffer_read_space (rb) >= bytes_per_frame)) {
92 jack_ringbuffer_read (rb, framebuf, bytes_per_frame);
94 if (sf_writef_float (info->sf, framebuf, 1) != 1) {
95 char errstr[256];
96 sf_error_str (0, errstr, sizeof (errstr) - 1);
97 fprintf (stderr,
98 "cannot write sndfile (%s)\n",
99 errstr);
100 info->status = EIO; /* write failed */
101 goto done;
104 if (++total_captured >= info->duration) {
105 printf ("disk thread finished\n");
106 goto done;
110 /* wait until process() signals more data */
111 pthread_cond_wait (&data_ready, &disk_thread_lock);
114 done:
115 pthread_mutex_unlock (&disk_thread_lock);
116 free (framebuf);
117 return 0;
120 static int
121 process (jack_nframes_t nframes, void *arg)
123 int chn;
124 size_t i;
125 jack_thread_info_t *info = (jack_thread_info_t *) arg;
127 /* Do nothing until we're ready to begin. */
128 if ((!info->can_process) || (!info->can_capture))
129 return 0;
131 for (chn = 0; chn < nports; chn++)
132 in[chn] = jack_port_get_buffer (ports[chn], nframes);
134 /* Sndfile requires interleaved data. It is simpler here to
135 * just queue interleaved samples to a single ringbuffer. */
136 for (i = 0; i < nframes; i++) {
137 for (chn = 0; chn < nports; chn++) {
138 if (jack_ringbuffer_write (rb, (void *) (in[chn]+i),
139 sample_size)
140 < sample_size)
141 overruns++;
145 /* Tell the disk thread there is work to do. If it is already
146 * running, the lock will not be available. We can't wait
147 * here in the process() thread, but we don't need to signal
148 * in that case, because the disk thread will read all the
149 * data queued before waiting again. */
150 if (pthread_mutex_trylock (&disk_thread_lock) == 0) {
151 pthread_cond_signal (&data_ready);
152 pthread_mutex_unlock (&disk_thread_lock);
155 return 0;
158 static void
159 jack_shutdown (void *arg)
161 fprintf(stderr, "JACK shut down, exiting ...\n");
162 exit(1);
165 static void
166 setup_disk_thread (jack_thread_info_t *info)
168 SF_INFO sf_info;
169 int short_mask;
171 sf_info.samplerate = jack_get_sample_rate (info->client);
172 sf_info.channels = info->channels;
174 switch (info->bitdepth) {
175 case 8: short_mask = SF_FORMAT_PCM_U8;
176 break;
177 case 16: short_mask = SF_FORMAT_PCM_16;
178 break;
179 case 24: short_mask = SF_FORMAT_PCM_24;
180 break;
181 case 32: short_mask = SF_FORMAT_PCM_32;
182 break;
183 default: short_mask = SF_FORMAT_PCM_16;
184 break;
186 sf_info.format = SF_FORMAT_WAV|short_mask;
188 if ((info->sf = sf_open (info->path, SFM_WRITE, &sf_info)) == NULL) {
189 char errstr[256];
190 sf_error_str (0, errstr, sizeof (errstr) - 1);
191 fprintf (stderr, "cannot open sndfile \"%s\" for output (%s)\n", info->path, errstr);
192 jack_client_close (info->client);
193 exit (1);
196 info->duration *= sf_info.samplerate;
197 info->can_capture = 0;
199 pthread_create (&info->thread_id, NULL, disk_thread, info);
202 static void
203 run_disk_thread (jack_thread_info_t *info)
205 info->can_capture = 1;
206 pthread_join (info->thread_id, NULL);
207 sf_close (info->sf);
208 if (overruns > 0) {
209 fprintf (stderr,
210 "jackrec failed with %ld overruns.\n", overruns);
211 fprintf (stderr, " try a bigger buffer than -B %"
212 PRIu32 ".\n", info->rb_size);
213 info->status = EPIPE;
217 static void
218 setup_ports (int sources, char *source_names[], jack_thread_info_t *info)
220 unsigned int i;
221 size_t in_size;
223 /* Allocate data structures that depend on the number of ports. */
224 nports = sources;
225 ports = (jack_port_t **) malloc (sizeof (jack_port_t *) * nports);
226 in_size = nports * sizeof (jack_default_audio_sample_t *);
227 in = (jack_default_audio_sample_t **) malloc (in_size);
228 rb = jack_ringbuffer_create (nports * sample_size * info->rb_size);
230 /* When JACK is running realtime, jack_activate() will have
231 * called mlockall() to lock our pages into memory. But, we
232 * still need to touch any newly allocated pages before
233 * process() starts using them. Otherwise, a page fault could
234 * create a delay that would force JACK to shut us down. */
235 memset(in, 0, in_size);
236 memset(rb->buf, 0, rb->size);
238 for (i = 0; i < nports; i++) {
239 char name[64];
241 sprintf (name, "input%d", i+1);
243 if ((ports[i] = jack_port_register (info->client, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0)) == 0) {
244 fprintf (stderr, "cannot register input port \"%s\"!\n", name);
245 jack_client_close (info->client);
246 exit (1);
250 for (i = 0; i < nports; i++) {
251 if (jack_connect (info->client, source_names[i], jack_port_name (ports[i]))) {
252 fprintf (stderr, "cannot connect input port %s to %s\n", jack_port_name (ports[i]), source_names[i]);
253 jack_client_close (info->client);
254 exit (1);
258 info->can_process = 1; /* process() can start, now */
262 main (int argc, char *argv[])
264 jack_thread_info_t thread_info;
265 int c;
266 int longopt_index = 0;
267 extern int optind, opterr;
268 int show_usage = 0;
269 char *optstring = "d:f:b:B:h";
270 struct option long_options[] = {
271 { "help", 0, 0, 'h' },
272 { "duration", 1, 0, 'd' },
273 { "file", 1, 0, 'f' },
274 { "bitdepth", 1, 0, 'b' },
275 { "bufsize", 1, 0, 'B' },
276 { 0, 0, 0, 0 }
279 memset (&thread_info, 0, sizeof (thread_info));
280 thread_info.rb_size = DEFAULT_RB_SIZE;
281 opterr = 0;
283 while ((c = getopt_long (argc, argv, optstring, long_options, &longopt_index)) != -1) {
284 switch (c) {
285 case 1:
286 /* getopt signals end of '-' options */
287 break;
289 case 'h':
290 show_usage++;
291 break;
292 case 'd':
293 thread_info.duration = atoi (optarg);
294 break;
295 case 'f':
296 thread_info.path = optarg;
297 break;
298 case 'b':
299 thread_info.bitdepth = atoi (optarg);
300 break;
301 case 'B':
302 thread_info.rb_size = atoi (optarg);
303 break;
304 default:
305 fprintf (stderr, "error\n");
306 show_usage++;
307 break;
311 if (show_usage || thread_info.path == NULL || optind == argc) {
312 fprintf (stderr, "usage: jackrec -f filename [ -d second ] [ -b bitdepth ] [ -B bufsize ] port1 [ port2 ... ]\n");
313 exit (1);
316 if ((client = jack_client_open ("jackrec", JackNullOption, NULL)) == 0) {
317 fprintf (stderr, "JACK server not running?\n");
318 exit (1);
321 thread_info.client = client;
322 thread_info.channels = argc - optind;
323 thread_info.can_process = 0;
325 setup_disk_thread (&thread_info);
327 jack_set_process_callback (client, process, &thread_info);
328 jack_on_shutdown (client, jack_shutdown, &thread_info);
330 if (jack_activate (client)) {
331 fprintf (stderr, "cannot activate client");
334 setup_ports (argc - optind, &argv[optind], &thread_info);
336 /* install a signal handler to properly quits jack client */
337 signal(SIGQUIT, signal_handler);
338 signal(SIGTERM, signal_handler);
339 signal(SIGHUP, signal_handler);
340 signal(SIGINT, signal_handler);
342 run_disk_thread (&thread_info);
344 jack_client_close (client);
346 jack_ringbuffer_free (rb);
348 exit (0);