Print information about detected CPU in verbose mode only.
[mplayer/glamo.git] / libao2 / ao_jack.c
blobd7f2092a43d2cfe164ad08a1fd0019fe01b6366b
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 "libvo/fastmemcpy.h"
41 #include <jack/jack.h>
43 static 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 // This type of ring buffer may never fill up completely, at least
72 // one byte must always be unused.
73 // For performance reasons (alignment etc.) one whole chunk always stays
74 // empty, not only one byte.
75 #define BUFFSIZE ((NUM_CHUNKS + 1) * CHUNK_SIZE)
77 //! buffer for audio data
78 static unsigned char *buffer = NULL;
80 //! buffer read position, may only be modified by playback thread or while it is stopped
81 static volatile int read_pos;
82 //! buffer write position, may only be modified by MPlayer's thread
83 static volatile int write_pos;
85 /**
86 * \brief get the number of free bytes in the buffer
87 * \return number of free bytes in buffer
89 * may only be called by MPlayer's thread
90 * return value may change between immediately following two calls,
91 * and the real number of free bytes might be larger!
93 static int buf_free(void) {
94 int free = read_pos - write_pos - CHUNK_SIZE;
95 if (free < 0) free += BUFFSIZE;
96 return free;
99 /**
100 * \brief get amount of data available in the buffer
101 * \return number of bytes available in buffer
103 * may only be called by the playback thread
104 * return value may change between immediately following two calls,
105 * and the real number of buffered bytes might be larger!
107 static int buf_used(void) {
108 int used = write_pos - read_pos;
109 if (used < 0) used += BUFFSIZE;
110 return used;
114 * \brief insert len bytes into buffer
115 * \param data data to insert
116 * \param len length of data
117 * \return number of bytes inserted into buffer
119 * If there is not enough room, the buffer is filled up
121 static int write_buffer(unsigned char* data, int len) {
122 int first_len = BUFFSIZE - write_pos;
123 int free = buf_free();
124 if (len > free) len = free;
125 if (first_len > len) first_len = len;
126 // till end of buffer
127 fast_memcpy (&buffer[write_pos], data, first_len);
128 if (len > first_len) { // we have to wrap around
129 // remaining part from beginning of buffer
130 fast_memcpy (buffer, &data[first_len], len - first_len);
132 write_pos = (write_pos + len) % BUFFSIZE;
133 return len;
136 static void silence(float **bufs, int cnt, int num_bufs);
139 * \brief read data from buffer and splitting it into channels
140 * \param bufs num_bufs float buffers, each will contain the data of one channel
141 * \param cnt number of samples to read per channel
142 * \param num_bufs number of channels to split the data into
143 * \return number of samples read per channel, equals cnt unless there was too
144 * little data in the buffer
146 * Assumes the data in the buffer is of type float, the number of bytes
147 * read is res * num_bufs * sizeof(float), where res is the return value.
148 * If there is not enough data in the buffer remaining parts will be filled
149 * with silence.
151 static int read_buffer(float **bufs, int cnt, int num_bufs) {
152 int buffered = buf_used();
153 int i, j;
154 if (cnt * sizeof(float) * num_bufs > buffered) {
155 silence(bufs, cnt, num_bufs);
156 cnt = buffered / sizeof(float) / num_bufs;
158 for (i = 0; i < cnt; i++) {
159 for (j = 0; j < num_bufs; j++) {
160 bufs[j][i] = *(float *)&buffer[read_pos];
161 read_pos = (read_pos + sizeof(float)) % BUFFSIZE;
164 return cnt;
167 // end ring buffer stuff
169 static int control(int cmd, void *arg) {
170 return CONTROL_UNKNOWN;
174 * \brief fill the buffers with silence
175 * \param bufs num_bufs float buffers, each will contain the data of one channel
176 * \param cnt number of samples in each buffer
177 * \param num_bufs number of buffers
179 static void silence(float **bufs, int cnt, int num_bufs) {
180 int i;
181 for (i = 0; i < num_bufs; i++)
182 memset(bufs[i], 0, cnt * sizeof(float));
186 * \brief JACK Callback function
187 * \param nframes number of frames to fill into buffers
188 * \param arg unused
189 * \return currently always 0
191 * Write silence into buffers if paused or an underrun occured
193 static int outputaudio(jack_nframes_t nframes, void *arg) {
194 float *bufs[MAX_CHANS];
195 int i;
196 for (i = 0; i < num_ports; i++)
197 bufs[i] = jack_port_get_buffer(ports[i], nframes);
198 if (paused || underrun)
199 silence(bufs, nframes, num_ports);
200 else
201 if (read_buffer(bufs, nframes, num_ports) < nframes)
202 underrun = 1;
203 if (estimate) {
204 float now = (float)GetTimer() / 1000000.0;
205 float diff = callback_time + callback_interval - now;
206 if ((diff > -0.002) && (diff < 0.002))
207 callback_time += callback_interval;
208 else
209 callback_time = now;
210 callback_interval = (float)nframes / (float)ao_data.samplerate;
212 return 0;
216 * \brief print suboption usage help
218 static void print_help (void)
220 mp_msg (MSGT_AO, MSGL_FATAL,
221 "\n-ao jack commandline help:\n"
222 "Example: mplayer -ao jack:port=myout\n"
223 " connects MPlayer to the jack ports named myout\n"
224 "\nOptions:\n"
225 " port=<port name>\n"
226 " Connects to the given ports instead of the default physical ones\n"
227 " name=<client name>\n"
228 " Client name to pass to JACK\n"
229 " estimate\n"
230 " Estimates the amount of data in buffers (experimental)\n"
231 " autostart\n"
232 " Automatically start JACK server if necessary\n"
236 static int init(int rate, int channels, int format, int flags) {
237 const char **matching_ports = NULL;
238 char *port_name = NULL;
239 char *client_name = NULL;
240 int autostart = 0;
241 opt_t subopts[] = {
242 {"port", OPT_ARG_MSTRZ, &port_name, NULL},
243 {"name", OPT_ARG_MSTRZ, &client_name, NULL},
244 {"estimate", OPT_ARG_BOOL, &estimate, NULL},
245 {"autostart", OPT_ARG_BOOL, &autostart, NULL},
246 {NULL}
248 jack_options_t open_options = JackUseExactName;
249 int port_flags = JackPortIsInput;
250 int i;
251 estimate = 1;
252 if (subopt_parse(ao_subdevice, subopts) != 0) {
253 print_help();
254 return 0;
256 if (channels > MAX_CHANS) {
257 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] Invalid number of channels: %i\n", channels);
258 goto err_out;
260 if (!client_name) {
261 client_name = malloc(40);
262 sprintf(client_name, "MPlayer [%d]", getpid());
264 if (!autostart)
265 open_options |= JackNoStartServer;
266 client = jack_client_open(client_name, open_options, NULL);
267 if (!client) {
268 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] cannot open server\n");
269 goto err_out;
271 reset();
272 jack_set_process_callback(client, outputaudio, 0);
274 // list matching ports
275 if (!port_name)
276 port_flags |= JackPortIsPhysical;
277 matching_ports = jack_get_ports(client, port_name, NULL, port_flags);
278 if (!matching_ports || !matching_ports[0]) {
279 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] no physical ports available\n");
280 goto err_out;
282 i = 1;
283 while (matching_ports[i]) i++;
284 if (channels > i) channels = i;
285 num_ports = channels;
287 // create out output ports
288 for (i = 0; i < num_ports; i++) {
289 char pname[30];
290 snprintf(pname, 30, "out_%d", i);
291 ports[i] = jack_port_register(client, pname, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
292 if (!ports[i]) {
293 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] not enough ports available\n");
294 goto err_out;
297 if (jack_activate(client)) {
298 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] activate failed\n");
299 goto err_out;
301 for (i = 0; i < num_ports; i++) {
302 if (jack_connect(client, jack_port_name(ports[i]), matching_ports[i])) {
303 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] connecting failed\n");
304 goto err_out;
307 rate = jack_get_sample_rate(client);
308 jack_latency = (float)(jack_port_get_total_latency(client, ports[0]) +
309 jack_get_buffer_size(client)) / (float)rate;
310 callback_interval = 0;
311 buffer = malloc(BUFFSIZE);
313 ao_data.channels = channels;
314 ao_data.samplerate = rate;
315 ao_data.format = AF_FORMAT_FLOAT_NE;
316 ao_data.bps = channels * rate * sizeof(float);
317 ao_data.buffersize = CHUNK_SIZE * NUM_CHUNKS;
318 ao_data.outburst = CHUNK_SIZE;
319 free(matching_ports);
320 free(port_name);
321 free(client_name);
322 return 1;
324 err_out:
325 free(matching_ports);
326 free(port_name);
327 free(client_name);
328 if (client)
329 jack_client_close(client);
330 free(buffer);
331 buffer = NULL;
332 return 0;
335 // close audio device
336 static void uninit(int immed) {
337 if (!immed)
338 usec_sleep(get_delay() * 1000 * 1000);
339 // HACK, make sure jack doesn't loop-output dirty buffers
340 reset();
341 usec_sleep(100 * 1000);
342 jack_client_close(client);
343 free(buffer);
344 buffer = NULL;
348 * \brief stop playing and empty buffers (for seeking/pause)
350 static void reset(void) {
351 paused = 1;
352 read_pos = 0;
353 write_pos = 0;
354 paused = 0;
358 * \brief stop playing, keep buffers (for pause)
360 static void audio_pause(void) {
361 paused = 1;
365 * \brief resume playing, after audio_pause()
367 static void audio_resume(void) {
368 paused = 0;
371 static int get_space(void) {
372 return buf_free();
376 * \brief write data into buffer and reset underrun flag
378 static int play(void *data, int len, int flags) {
379 if (!(flags & AOPLAY_FINAL_CHUNK))
380 len -= len % ao_data.outburst;
381 underrun = 0;
382 return write_buffer(data, len);
385 static float get_delay(void) {
386 int buffered = BUFFSIZE - CHUNK_SIZE - buf_free(); // could be less
387 float in_jack = jack_latency;
388 if (estimate && callback_interval > 0) {
389 float elapsed = (float)GetTimer() / 1000000.0 - callback_time;
390 in_jack += callback_interval - elapsed;
391 if (in_jack < 0) in_jack = 0;
393 return (float)buffered / (float)ao_data.bps + in_jack;