Get rid of completely pointless vt_doit variable
[mplayer/glamo.git] / libao2 / ao_jack.c
blobe92786c28daf9a3942d8ef6f9d53b757468ff474
1 /*
2 * JACK audio output driver for MPlayer
4 * Copyleft 2001 by Felix Bünemann (atmosfear@users.sf.net)
5 * and Reimar Döffinger (Reimar.Doeffinger@stud.uni-karlsruhe.de)
7 * This file is part of MPlayer.
9 * MPlayer is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * MPlayer is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * along with MPlayer; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "config.h"
30 #include "mp_msg.h"
31 #include "help_mp.h"
33 #include "audio_out.h"
34 #include "audio_out_internal.h"
35 #include "libaf/af_format.h"
36 #include "osdep/timer.h"
37 #include "subopt-helper.h"
39 #include "libavutil/fifo.h"
41 #include <jack/jack.h>
43 static const ao_info_t info =
45 "JACK audio output",
46 "jack",
47 "Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>",
48 "based on ao_sdl.c"
51 LIBAO_EXTERN(jack)
53 //! maximum number of channels supported, avoids lots of mallocs
54 #define MAX_CHANS 6
55 static jack_port_t *ports[MAX_CHANS];
56 static int num_ports; ///< Number of used ports == number of channels
57 static jack_client_t *client;
58 static float jack_latency;
59 static int estimate;
60 static volatile int paused = 0; ///< set if paused
61 static volatile int underrun = 0; ///< signals if an underrun occured
63 static volatile float callback_interval = 0;
64 static volatile float callback_time = 0;
66 //! size of one chunk, if this is too small MPlayer will start to "stutter"
67 //! after a short time of playback
68 #define CHUNK_SIZE (16 * 1024)
69 //! number of "virtual" chunks the buffer consists of
70 #define NUM_CHUNKS 8
71 #define BUFFSIZE (NUM_CHUNKS * CHUNK_SIZE)
73 //! buffer for audio data
74 static AVFifoBuffer *buffer;
76 /**
77 * \brief insert len bytes into buffer
78 * \param data data to insert
79 * \param len length of data
80 * \return number of bytes inserted into buffer
82 * If there is not enough room, the buffer is filled up
84 static int write_buffer(unsigned char* data, int len) {
85 int free = av_fifo_space(buffer);
86 if (len > free) len = free;
87 return av_fifo_generic_write(buffer, data, len, NULL);
90 static void silence(float **bufs, int cnt, int num_bufs);
92 struct deinterleave {
93 float **bufs;
94 int num_bufs;
95 int cur_buf;
96 int pos;
99 static void deinterleave(void *info, void *src, int len) {
100 struct deinterleave *di = info;
101 float *s = src;
102 int i;
103 len /= sizeof(float);
104 for (i = 0; i < len; i++) {
105 di->bufs[di->cur_buf++][di->pos] = s[i];
106 if (di->cur_buf >= di->num_bufs) {
107 di->cur_buf = 0;
108 di->pos++;
114 * \brief read data from buffer and splitting it into channels
115 * \param bufs num_bufs float buffers, each will contain the data of one channel
116 * \param cnt number of samples to read per channel
117 * \param num_bufs number of channels to split the data into
118 * \return number of samples read per channel, equals cnt unless there was too
119 * little data in the buffer
121 * Assumes the data in the buffer is of type float, the number of bytes
122 * read is res * num_bufs * sizeof(float), where res is the return value.
123 * If there is not enough data in the buffer remaining parts will be filled
124 * with silence.
126 static int read_buffer(float **bufs, int cnt, int num_bufs) {
127 struct deinterleave di = {bufs, num_bufs, 0, 0};
128 int buffered = av_fifo_size(buffer);
129 if (cnt * sizeof(float) * num_bufs > buffered) {
130 silence(bufs, cnt, num_bufs);
131 cnt = buffered / sizeof(float) / num_bufs;
133 av_fifo_generic_read(buffer, &di, cnt * num_bufs * sizeof(float), deinterleave);
134 return cnt;
137 // end ring buffer stuff
139 static int control(int cmd, void *arg) {
140 return CONTROL_UNKNOWN;
144 * \brief fill the buffers with silence
145 * \param bufs num_bufs float buffers, each will contain the data of one channel
146 * \param cnt number of samples in each buffer
147 * \param num_bufs number of buffers
149 static void silence(float **bufs, int cnt, int num_bufs) {
150 int i;
151 for (i = 0; i < num_bufs; i++)
152 memset(bufs[i], 0, cnt * sizeof(float));
156 * \brief JACK Callback function
157 * \param nframes number of frames to fill into buffers
158 * \param arg unused
159 * \return currently always 0
161 * Write silence into buffers if paused or an underrun occured
163 static int outputaudio(jack_nframes_t nframes, void *arg) {
164 float *bufs[MAX_CHANS];
165 int i;
166 for (i = 0; i < num_ports; i++)
167 bufs[i] = jack_port_get_buffer(ports[i], nframes);
168 if (paused || underrun)
169 silence(bufs, nframes, num_ports);
170 else
171 if (read_buffer(bufs, nframes, num_ports) < nframes)
172 underrun = 1;
173 if (estimate) {
174 float now = (float)GetTimer() / 1000000.0;
175 float diff = callback_time + callback_interval - now;
176 if ((diff > -0.002) && (diff < 0.002))
177 callback_time += callback_interval;
178 else
179 callback_time = now;
180 callback_interval = (float)nframes / (float)ao_data.samplerate;
182 return 0;
186 * \brief print suboption usage help
188 static void print_help (void)
190 mp_msg (MSGT_AO, MSGL_FATAL,
191 "\n-ao jack commandline help:\n"
192 "Example: mplayer -ao jack:port=myout\n"
193 " connects MPlayer to the jack ports named myout\n"
194 "\nOptions:\n"
195 " port=<port name>\n"
196 " Connects to the given ports instead of the default physical ones\n"
197 " name=<client name>\n"
198 " Client name to pass to JACK\n"
199 " estimate\n"
200 " Estimates the amount of data in buffers (experimental)\n"
201 " autostart\n"
202 " Automatically start JACK server if necessary\n"
206 static int init(int rate, int channels, int format, int flags) {
207 const char **matching_ports = NULL;
208 char *port_name = NULL;
209 char *client_name = NULL;
210 int autostart = 0;
211 opt_t subopts[] = {
212 {"port", OPT_ARG_MSTRZ, &port_name, NULL},
213 {"name", OPT_ARG_MSTRZ, &client_name, NULL},
214 {"estimate", OPT_ARG_BOOL, &estimate, NULL},
215 {"autostart", OPT_ARG_BOOL, &autostart, NULL},
216 {NULL}
218 jack_options_t open_options = JackUseExactName;
219 int port_flags = JackPortIsInput;
220 int i;
221 estimate = 1;
222 if (subopt_parse(ao_subdevice, subopts) != 0) {
223 print_help();
224 return 0;
226 if (channels > MAX_CHANS) {
227 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] Invalid number of channels: %i\n", channels);
228 goto err_out;
230 if (!client_name) {
231 client_name = malloc(40);
232 sprintf(client_name, "MPlayer [%d]", getpid());
234 if (!autostart)
235 open_options |= JackNoStartServer;
236 client = jack_client_open(client_name, open_options, NULL);
237 if (!client) {
238 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] cannot open server\n");
239 goto err_out;
241 buffer = av_fifo_alloc(BUFFSIZE);
242 jack_set_process_callback(client, outputaudio, 0);
244 // list matching ports
245 if (!port_name)
246 port_flags |= JackPortIsPhysical;
247 matching_ports = jack_get_ports(client, port_name, NULL, port_flags);
248 if (!matching_ports || !matching_ports[0]) {
249 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] no physical ports available\n");
250 goto err_out;
252 i = 1;
253 while (matching_ports[i]) i++;
254 if (channels > i) channels = i;
255 num_ports = channels;
257 // create out output ports
258 for (i = 0; i < num_ports; i++) {
259 char pname[30];
260 snprintf(pname, 30, "out_%d", i);
261 ports[i] = jack_port_register(client, pname, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
262 if (!ports[i]) {
263 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] not enough ports available\n");
264 goto err_out;
267 if (jack_activate(client)) {
268 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] activate failed\n");
269 goto err_out;
271 for (i = 0; i < num_ports; i++) {
272 if (jack_connect(client, jack_port_name(ports[i]), matching_ports[i])) {
273 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] connecting failed\n");
274 goto err_out;
277 rate = jack_get_sample_rate(client);
278 jack_latency = (float)(jack_port_get_total_latency(client, ports[0]) +
279 jack_get_buffer_size(client)) / (float)rate;
280 callback_interval = 0;
282 ao_data.channels = channels;
283 ao_data.samplerate = rate;
284 ao_data.format = AF_FORMAT_FLOAT_NE;
285 ao_data.bps = channels * rate * sizeof(float);
286 ao_data.buffersize = CHUNK_SIZE * NUM_CHUNKS;
287 ao_data.outburst = CHUNK_SIZE;
288 free(matching_ports);
289 free(port_name);
290 free(client_name);
291 return 1;
293 err_out:
294 free(matching_ports);
295 free(port_name);
296 free(client_name);
297 if (client)
298 jack_client_close(client);
299 av_fifo_free(buffer);
300 buffer = NULL;
301 return 0;
304 // close audio device
305 static void uninit(int immed) {
306 if (!immed)
307 usec_sleep(get_delay() * 1000 * 1000);
308 // HACK, make sure jack doesn't loop-output dirty buffers
309 reset();
310 usec_sleep(100 * 1000);
311 jack_client_close(client);
312 av_fifo_free(buffer);
313 buffer = NULL;
317 * \brief stop playing and empty buffers (for seeking/pause)
319 static void reset(void) {
320 paused = 1;
321 av_fifo_reset(buffer);
322 paused = 0;
326 * \brief stop playing, keep buffers (for pause)
328 static void audio_pause(void) {
329 paused = 1;
333 * \brief resume playing, after audio_pause()
335 static void audio_resume(void) {
336 paused = 0;
339 static int get_space(void) {
340 return av_fifo_space(buffer);
344 * \brief write data into buffer and reset underrun flag
346 static int play(void *data, int len, int flags) {
347 if (!(flags & AOPLAY_FINAL_CHUNK))
348 len -= len % ao_data.outburst;
349 underrun = 0;
350 return write_buffer(data, len);
353 static float get_delay(void) {
354 int buffered = av_fifo_size(buffer); // could be less
355 float in_jack = jack_latency;
356 if (estimate && callback_interval > 0) {
357 float elapsed = (float)GetTimer() / 1000000.0 - callback_time;
358 in_jack += callback_interval - elapsed;
359 if (in_jack < 0) in_jack = 0;
361 return (float)buffered / (float)ao_data.bps + in_jack;