Merge branch 'master' into develop
[jack2.git] / example-clients / capture_client.c
blobd3360b404103622e03df7d6d72d497c69fc90cf0
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 <inttypes.h>
34 #include <jack/jack.h>
35 #include <jack/ringbuffer.h>
37 typedef struct _thread_info {
38 pthread_t thread_id;
39 SNDFILE *sf;
40 jack_nframes_t duration;
41 jack_nframes_t rb_size;
42 jack_client_t *client;
43 unsigned int channels;
44 int bitdepth;
45 char *path;
46 volatile int can_capture;
47 volatile int can_process;
48 volatile int status;
49 } jack_thread_info_t;
51 /* JACK data */
52 unsigned int nports;
53 jack_port_t **ports;
54 jack_default_audio_sample_t **in;
55 jack_nframes_t nframes;
56 const size_t sample_size = sizeof(jack_default_audio_sample_t);
58 /* Synchronization between process thread and disk thread. */
59 #define DEFAULT_RB_SIZE 16384 /* ringbuffer size in frames */
60 jack_ringbuffer_t *rb;
61 pthread_mutex_t disk_thread_lock = PTHREAD_MUTEX_INITIALIZER;
62 pthread_cond_t data_ready = PTHREAD_COND_INITIALIZER;
63 long overruns = 0;
64 jack_client_t *client;
66 static void signal_handler(int sig)
68 jack_client_close(client);
69 fprintf(stderr, "signal received, exiting ...\n");
70 exit(0);
73 static void *
74 disk_thread (void *arg)
76 jack_thread_info_t *info = (jack_thread_info_t *) arg;
77 static jack_nframes_t total_captured = 0;
78 jack_nframes_t samples_per_frame = info->channels;
79 size_t bytes_per_frame = samples_per_frame * sample_size;
80 void *framebuf = malloc (bytes_per_frame);
82 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
83 pthread_mutex_lock (&disk_thread_lock);
85 info->status = 0;
87 while (1) {
89 /* Write the data one frame at a time. This is
90 * inefficient, but makes things simpler. */
91 while (info->can_capture &&
92 (jack_ringbuffer_read_space (rb) >= bytes_per_frame)) {
94 jack_ringbuffer_read (rb, framebuf, bytes_per_frame);
96 if (sf_writef_float (info->sf, framebuf, 1) != 1) {
97 char errstr[256];
98 sf_error_str (0, errstr, sizeof (errstr) - 1);
99 fprintf (stderr,
100 "cannot write sndfile (%s)\n",
101 errstr);
102 info->status = EIO; /* write failed */
103 goto done;
106 if (++total_captured >= info->duration) {
107 printf ("disk thread finished\n");
108 goto done;
112 /* wait until process() signals more data */
113 pthread_cond_wait (&data_ready, &disk_thread_lock);
116 done:
117 pthread_mutex_unlock (&disk_thread_lock);
118 free (framebuf);
119 return 0;
122 static int
123 process (jack_nframes_t nframes, void *arg)
125 int chn;
126 size_t i;
127 jack_thread_info_t *info = (jack_thread_info_t *) arg;
129 /* Do nothing until we're ready to begin. */
130 if ((!info->can_process) || (!info->can_capture))
131 return 0;
133 for (chn = 0; chn < nports; chn++)
134 in[chn] = jack_port_get_buffer (ports[chn], nframes);
136 /* Sndfile requires interleaved data. It is simpler here to
137 * just queue interleaved samples to a single ringbuffer. */
138 for (i = 0; i < nframes; i++) {
139 for (chn = 0; chn < nports; chn++) {
140 if (jack_ringbuffer_write (rb, (void *) (in[chn]+i),
141 sample_size)
142 < sample_size)
143 overruns++;
147 /* Tell the disk thread there is work to do. If it is already
148 * running, the lock will not be available. We can't wait
149 * here in the process() thread, but we don't need to signal
150 * in that case, because the disk thread will read all the
151 * data queued before waiting again. */
152 if (pthread_mutex_trylock (&disk_thread_lock) == 0) {
153 pthread_cond_signal (&data_ready);
154 pthread_mutex_unlock (&disk_thread_lock);
157 return 0;
160 static void
161 jack_shutdown (void *arg)
163 fprintf(stderr, "JACK shut down, exiting ...\n");
164 exit(1);
167 static void
168 setup_disk_thread (jack_thread_info_t *info)
170 SF_INFO sf_info;
171 int short_mask;
173 sf_info.samplerate = jack_get_sample_rate (info->client);
174 sf_info.channels = info->channels;
176 switch (info->bitdepth) {
177 case 8: short_mask = SF_FORMAT_PCM_U8;
178 break;
179 case 16: short_mask = SF_FORMAT_PCM_16;
180 break;
181 case 24: short_mask = SF_FORMAT_PCM_24;
182 break;
183 case 32: short_mask = SF_FORMAT_PCM_32;
184 break;
185 default: short_mask = SF_FORMAT_PCM_16;
186 break;
188 sf_info.format = SF_FORMAT_WAV|short_mask;
190 if ((info->sf = sf_open (info->path, SFM_WRITE, &sf_info)) == NULL) {
191 char errstr[256];
192 sf_error_str (0, errstr, sizeof (errstr) - 1);
193 fprintf (stderr, "cannot open sndfile \"%s\" for output (%s)\n", info->path, errstr);
194 jack_client_close (info->client);
195 exit (1);
198 info->duration *= sf_info.samplerate;
199 info->can_capture = 0;
201 pthread_create (&info->thread_id, NULL, disk_thread, info);
204 static void
205 run_disk_thread (jack_thread_info_t *info)
207 info->can_capture = 1;
208 pthread_join (info->thread_id, NULL);
209 sf_close (info->sf);
210 if (overruns > 0) {
211 fprintf (stderr,
212 "jackrec failed with %ld overruns.\n", overruns);
213 fprintf (stderr, " try a bigger buffer than -B %"
214 PRIu32 ".\n", info->rb_size);
215 info->status = EPIPE;
219 static void
220 setup_ports (int sources, char *source_names[], jack_thread_info_t *info)
222 unsigned int i;
223 size_t in_size;
225 /* Allocate data structures that depend on the number of ports. */
226 nports = sources;
227 ports = (jack_port_t **) malloc (sizeof (jack_port_t *) * nports);
228 in_size = nports * sizeof (jack_default_audio_sample_t *);
229 in = (jack_default_audio_sample_t **) malloc (in_size);
230 rb = jack_ringbuffer_create (nports * sample_size * info->rb_size);
232 /* When JACK is running realtime, jack_activate() will have
233 * called mlockall() to lock our pages into memory. But, we
234 * still need to touch any newly allocated pages before
235 * process() starts using them. Otherwise, a page fault could
236 * create a delay that would force JACK to shut us down. */
237 memset(in, 0, in_size);
238 memset(rb->buf, 0, rb->size);
240 for (i = 0; i < nports; i++) {
241 char name[64];
243 sprintf (name, "input%d", i+1);
245 if ((ports[i] = jack_port_register (info->client, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0)) == 0) {
246 fprintf (stderr, "cannot register input port \"%s\"!\n", name);
247 jack_client_close (info->client);
248 exit (1);
252 for (i = 0; i < nports; i++) {
253 if (jack_connect (info->client, source_names[i], jack_port_name (ports[i]))) {
254 fprintf (stderr, "cannot connect input port %s to %s\n", jack_port_name (ports[i]), source_names[i]);
255 jack_client_close (info->client);
256 exit (1);
260 info->can_process = 1; /* process() can start, now */
264 main (int argc, char *argv[])
266 jack_thread_info_t thread_info;
267 int c;
268 int longopt_index = 0;
269 extern int optind, opterr;
270 int show_usage = 0;
271 char *optstring = "d:f:b:B:h";
272 struct option long_options[] = {
273 { "help", 0, 0, 'h' },
274 { "duration", 1, 0, 'd' },
275 { "file", 1, 0, 'f' },
276 { "bitdepth", 1, 0, 'b' },
277 { "bufsize", 1, 0, 'B' },
278 { 0, 0, 0, 0 }
281 memset (&thread_info, 0, sizeof (thread_info));
282 thread_info.rb_size = DEFAULT_RB_SIZE;
283 opterr = 0;
285 while ((c = getopt_long (argc, argv, optstring, long_options, &longopt_index)) != -1) {
286 switch (c) {
287 case 1:
288 /* getopt signals end of '-' options */
289 break;
291 case 'h':
292 show_usage++;
293 break;
294 case 'd':
295 thread_info.duration = atoi (optarg);
296 break;
297 case 'f':
298 thread_info.path = optarg;
299 break;
300 case 'b':
301 thread_info.bitdepth = atoi (optarg);
302 break;
303 case 'B':
304 thread_info.rb_size = atoi (optarg);
305 break;
306 default:
307 fprintf (stderr, "error\n");
308 show_usage++;
309 break;
313 if (show_usage || thread_info.path == NULL || optind == argc) {
314 fprintf (stderr, "usage: jackrec -f filename [ -d second ] [ -b bitdepth ] [ -B bufsize ] port1 [ port2 ... ]\n");
315 exit (1);
318 if ((client = jack_client_open ("jackrec", JackNullOption, NULL)) == 0) {
319 fprintf (stderr, "JACK server not running?\n");
320 exit (1);
323 thread_info.client = client;
324 thread_info.channels = argc - optind;
325 thread_info.can_process = 0;
327 setup_disk_thread (&thread_info);
329 jack_set_process_callback (client, process, &thread_info);
330 jack_on_shutdown (client, jack_shutdown, &thread_info);
332 if (jack_activate (client)) {
333 fprintf (stderr, "cannot activate client");
336 setup_ports (argc - optind, &argv[optind], &thread_info);
338 /* install a signal handler to properly quits jack client */
339 #ifndef WIN32
340 signal(SIGQUIT, signal_handler);
341 signal(SIGHUP, signal_handler);
342 #endif
343 signal(SIGTERM, signal_handler);
344 signal(SIGINT, signal_handler);
346 run_disk_thread (&thread_info);
348 jack_client_close (client);
350 jack_ringbuffer_free (rb);
352 exit (0);