Updating audio_jack to new interface for creating a client. Fixes depricated warning.
[debian/gnuradio] / gr-audio-jack / src / audio_jack_sink.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005,2006,2010 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <audio_jack_sink.h>
28 #include <gr_io_signature.h>
29 #include <gr_prefs.h>
30 #include <stdio.h>
31 #include <iostream>
32 #include <stdexcept>
33 #include <gri_jack.h>
34
35 #ifndef NO_PTHREAD
36 #include <pthread.h>
37 #endif
38
39 typedef jack_default_audio_sample_t sample_t;
40
41
42 // Number of jack buffers in the ringbuffer
43 // TODO: make it to match at least the quantity of items passed by work()
44 static const unsigned int N_BUFFERS = 16;
45
46 static std::string 
47 default_device_name ()
48 {
49   return gr_prefs::singleton()->get_string("audio_jack", "default_output_device", "gr_sink");
50 }
51
52 int
53 jack_sink_process (jack_nframes_t nframes, void *arg)
54 {
55   audio_jack_sink *self = (audio_jack_sink *)arg;
56   unsigned int read_size = nframes*sizeof(sample_t);
57
58   if (jack_ringbuffer_read_space (self->d_ringbuffer) < read_size) {
59     self->d_nunderuns++;
60     // FIXME: move this fputs out, we shouldn't use blocking calls in process()
61     fputs ("jU", stderr);
62     return 0;
63   }
64
65   char *buffer = (char *) jack_port_get_buffer (self->d_jack_output_port, nframes);
66
67   jack_ringbuffer_read (self->d_ringbuffer, buffer, read_size);
68
69 #ifndef NO_PTHREAD
70   // Tell the sink thread there is room in the ringbuffer.
71   // If it is already running, the lock will not be available.
72   // We can't wait here in the process() thread, but we don't
73   // need to signal in that case, because the sink thread will 
74   // check for room availability.
75
76   if (pthread_mutex_trylock (&self->d_jack_process_lock) == 0) {
77     pthread_cond_signal (&self->d_ringbuffer_ready);
78     pthread_mutex_unlock (&self->d_jack_process_lock);
79   }
80 #endif
81
82   return 0;
83 }
84
85 // ----------------------------------------------------------------
86
87 audio_jack_sink_sptr
88 audio_jack_make_sink(int sampling_rate, const std::string dev, bool ok_to_block)
89 {
90   return gnuradio::get_initial_sptr(new audio_jack_sink (sampling_rate, dev,
91                                                     ok_to_block));
92 }
93
94 audio_jack_sink::audio_jack_sink (int sampling_rate,
95                                   const std::string device_name,
96                                   bool ok_to_block)
97   : gr_sync_block ("audio_jack_sink",
98                    gr_make_io_signature (0, 0, 0),
99                    gr_make_io_signature (0, 0, 0)),
100     d_sampling_rate (sampling_rate),
101     d_device_name (device_name.empty() ? default_device_name() : device_name),
102     d_ok_to_block (ok_to_block),
103     d_jack_client (0),
104     d_ringbuffer (0),
105     d_nunderuns (0)
106 {
107 #ifndef NO_PTHREAD
108   pthread_cond_init(&d_ringbuffer_ready, NULL);;
109   pthread_mutex_init(&d_jack_process_lock, NULL);
110 #endif
111   
112   // try to become a client of the JACK server
113   jack_options_t options = JackNullOption;
114   jack_status_t status;
115   const char *server_name = NULL;
116   if ((d_jack_client = jack_client_open (d_device_name.c_str (),
117                                          options, &status,
118                                          server_name)) == NULL) {
119     fprintf (stderr, "audio_jack_sink[%s]: jack server not running?\n",
120              d_device_name.c_str());
121     throw std::runtime_error ("audio_jack_sink");
122   }
123
124   // tell the JACK server to call `jack_sink_process()' whenever
125   // there is work to be done.
126   jack_set_process_callback (d_jack_client, &jack_sink_process, (void*)this);
127
128   // tell the JACK server to call `jack_shutdown()' if
129   // it ever shuts down, either entirely, or if it
130   // just decides to stop calling us.
131
132   //jack_on_shutdown (d_jack_client, &jack_shutdown, (void*)this);
133  
134   d_jack_output_port = 
135     jack_port_register (d_jack_client, "out", 
136                         JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
137
138
139   d_jack_buffer_size = jack_get_buffer_size (d_jack_client);
140
141   set_output_multiple (d_jack_buffer_size);
142
143   d_ringbuffer =
144     jack_ringbuffer_create (N_BUFFERS*d_jack_buffer_size*sizeof(sample_t));
145   if (d_ringbuffer == NULL)
146     bail ("jack_ringbuffer_create failed", 0);
147
148   assert(sizeof(float)==sizeof(sample_t));
149   set_input_signature (gr_make_io_signature (1, 1, sizeof (sample_t)));
150
151
152   jack_nframes_t sample_rate = jack_get_sample_rate (d_jack_client);
153
154   if ((jack_nframes_t)sampling_rate != sample_rate){
155     fprintf (stderr, "audio_jack_sink[%s]: unable to support sampling rate %d\n",
156              d_device_name.c_str (), sampling_rate);
157     fprintf (stderr, "  card requested %d instead.\n", sample_rate);
158   }
159 }
160
161
162 bool
163 audio_jack_sink::check_topology (int ninputs, int noutputs)
164 {
165   if (ninputs != 1)
166     return false;
167
168   // tell the JACK server that we are ready to roll 
169   if (jack_activate (d_jack_client))
170     throw std::runtime_error ("audio_jack_sink");
171
172   return true;
173 }
174
175 audio_jack_sink::~audio_jack_sink ()
176 {
177   jack_client_close (d_jack_client);
178   jack_ringbuffer_free (d_ringbuffer);
179 }
180
181 int
182 audio_jack_sink::work (int noutput_items,
183                        gr_vector_const_void_star &input_items,
184                        gr_vector_void_star &output_items)
185 {
186   // write_size and work_size are in bytes
187   int work_size = noutput_items*sizeof(sample_t);
188   unsigned int write_size;
189
190   while (work_size > 0) {
191     unsigned int write_space;   // bytes
192
193 #ifdef NO_PTHREAD
194     while ((write_space=jack_ringbuffer_write_space (d_ringbuffer)) < 
195            d_jack_buffer_size*sizeof(sample_t)) {
196       usleep(1000000*((d_jack_buffer_size-write_space/sizeof(sample_t))/d_sampling_rate));
197     }
198 #else
199     // JACK actually requires POSIX
200
201     pthread_mutex_lock (&d_jack_process_lock);
202     while ((write_space=jack_ringbuffer_write_space (d_ringbuffer)) < 
203            d_jack_buffer_size*sizeof(sample_t)) {
204
205       // wait until jack_sink_process() signals more room
206       pthread_cond_wait (&d_ringbuffer_ready, &d_jack_process_lock);
207     }
208     pthread_mutex_unlock (&d_jack_process_lock);
209 #endif
210
211     write_space -= write_space%(d_jack_buffer_size*sizeof(sample_t));
212     write_size = std::min(write_space, (unsigned int)work_size);
213
214     if (jack_ringbuffer_write (d_ringbuffer, (char *) input_items[0],
215                                write_size) < write_size) {
216       bail ("jack_ringbuffer_write failed", 0);
217     }
218     work_size -= write_size;
219   }
220
221   return noutput_items;
222 }
223
224 void
225 audio_jack_sink::output_error_msg (const char *msg, int err)
226 {
227   fprintf (stderr, "audio_jack_sink[%s]: %s: %d\n",
228            d_device_name.c_str (), msg,  err);
229 }
230
231 void
232 audio_jack_sink::bail (const char *msg, int err) throw (std::runtime_error)
233 {
234   output_error_msg (msg, err);
235   throw std::runtime_error ("audio_jack_sink");
236 }