cleanup: Silence compilation warnings on MinGW-w64
[mplayer.git] / libao2 / ao_jack.c
blobc4bade08ecd52fbe1ddf7082be7686cb62968dde
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"
32 #include "audio_out.h"
33 #include "audio_out_internal.h"
34 #include "libaf/af_format.h"
35 #include "osdep/timer.h"
36 #include "subopt-helper.h"
38 #include "libavutil/fifo.h"
40 #include <jack/jack.h>
42 static const ao_info_t info =
44 "JACK audio output",
45 "jack",
46 "Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>",
47 "based on ao_sdl.c"
50 LIBAO_EXTERN(jack)
52 //! maximum number of channels supported, avoids lots of mallocs
53 #define MAX_CHANS 8
54 static jack_port_t *ports[MAX_CHANS];
55 static int num_ports; ///< Number of used ports == number of channels
56 static jack_client_t *client;
57 static float jack_latency;
58 static int estimate;
59 static volatile int paused = 0; ///< set if paused
60 static volatile int underrun = 0; ///< signals if an underrun occured
62 static volatile float callback_interval = 0;
63 static volatile float callback_time = 0;
65 //! size of one chunk, if this is too small MPlayer will start to "stutter"
66 //! after a short time of playback
67 #define CHUNK_SIZE (16 * 1024)
68 //! number of "virtual" chunks the buffer consists of
69 #define NUM_CHUNKS 8
70 #define BUFFSIZE (NUM_CHUNKS * CHUNK_SIZE)
72 //! buffer for audio data
73 static AVFifoBuffer *buffer;
75 /**
76 * \brief insert len bytes into buffer
77 * \param data data to insert
78 * \param len length of data
79 * \return number of bytes inserted into buffer
81 * If there is not enough room, the buffer is filled up
83 static int write_buffer(unsigned char* data, int len) {
84 int free = av_fifo_space(buffer);
85 if (len > free) len = free;
86 return av_fifo_generic_write(buffer, data, len, NULL);
89 static void silence(float **bufs, int cnt, int num_bufs);
91 struct deinterleave {
92 float **bufs;
93 int num_bufs;
94 int cur_buf;
95 int pos;
98 static void deinterleave(void *info, void *src, int len) {
99 struct deinterleave *di = info;
100 float *s = src;
101 int i;
102 len /= sizeof(float);
103 for (i = 0; i < len; i++) {
104 di->bufs[di->cur_buf++][di->pos] = s[i];
105 if (di->cur_buf >= di->num_bufs) {
106 di->cur_buf = 0;
107 di->pos++;
113 * \brief read data from buffer and splitting it into channels
114 * \param bufs num_bufs float buffers, each will contain the data of one channel
115 * \param cnt number of samples to read per channel
116 * \param num_bufs number of channels to split the data into
117 * \return number of samples read per channel, equals cnt unless there was too
118 * little data in the buffer
120 * Assumes the data in the buffer is of type float, the number of bytes
121 * read is res * num_bufs * sizeof(float), where res is the return value.
122 * If there is not enough data in the buffer remaining parts will be filled
123 * with silence.
125 static int read_buffer(float **bufs, int cnt, int num_bufs) {
126 struct deinterleave di = {bufs, num_bufs, 0, 0};
127 int buffered = av_fifo_size(buffer);
128 if (cnt * sizeof(float) * num_bufs > buffered) {
129 silence(bufs, cnt, num_bufs);
130 cnt = buffered / sizeof(float) / num_bufs;
132 av_fifo_generic_read(buffer, &di, cnt * num_bufs * sizeof(float), deinterleave);
133 return cnt;
136 // end ring buffer stuff
138 static int control(int cmd, void *arg) {
139 return CONTROL_UNKNOWN;
143 * \brief fill the buffers with silence
144 * \param bufs num_bufs float buffers, each will contain the data of one channel
145 * \param cnt number of samples in each buffer
146 * \param num_bufs number of buffers
148 static void silence(float **bufs, int cnt, int num_bufs) {
149 int i;
150 for (i = 0; i < num_bufs; i++)
151 memset(bufs[i], 0, cnt * sizeof(float));
155 * \brief JACK Callback function
156 * \param nframes number of frames to fill into buffers
157 * \param arg unused
158 * \return currently always 0
160 * Write silence into buffers if paused or an underrun occured
162 static int outputaudio(jack_nframes_t nframes, void *arg) {
163 float *bufs[MAX_CHANS];
164 int i;
165 for (i = 0; i < num_ports; i++)
166 bufs[i] = jack_port_get_buffer(ports[i], nframes);
167 if (paused || underrun)
168 silence(bufs, nframes, num_ports);
169 else
170 if (read_buffer(bufs, nframes, num_ports) < nframes)
171 underrun = 1;
172 if (estimate) {
173 float now = (float)GetTimer() / 1000000.0;
174 float diff = callback_time + callback_interval - now;
175 if ((diff > -0.002) && (diff < 0.002))
176 callback_time += callback_interval;
177 else
178 callback_time = now;
179 callback_interval = (float)nframes / (float)ao_data.samplerate;
181 return 0;
185 * \brief print suboption usage help
187 static void print_help (void)
189 mp_msg (MSGT_AO, MSGL_FATAL,
190 "\n-ao jack commandline help:\n"
191 "Example: mplayer -ao jack:port=myout\n"
192 " connects MPlayer to the jack ports named myout\n"
193 "\nOptions:\n"
194 " port=<port name>\n"
195 " Connects to the given ports instead of the default physical ones\n"
196 " name=<client name>\n"
197 " Client name to pass to JACK\n"
198 " estimate\n"
199 " Estimates the amount of data in buffers (experimental)\n"
200 " autostart\n"
201 " Automatically start JACK server if necessary\n"
205 static int init(int rate, int channels, int format, int flags) {
206 const char **matching_ports = NULL;
207 char *port_name = NULL;
208 char *client_name = NULL;
209 int autostart = 0;
210 const opt_t subopts[] = {
211 {"port", OPT_ARG_MSTRZ, &port_name, NULL},
212 {"name", OPT_ARG_MSTRZ, &client_name, NULL},
213 {"estimate", OPT_ARG_BOOL, &estimate, NULL},
214 {"autostart", OPT_ARG_BOOL, &autostart, NULL},
215 {NULL}
217 jack_options_t open_options = JackUseExactName;
218 int port_flags = JackPortIsInput;
219 int i;
220 estimate = 1;
221 if (subopt_parse(ao_subdevice, subopts) != 0) {
222 print_help();
223 return 0;
225 if (channels > MAX_CHANS) {
226 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] Invalid number of channels: %i\n", channels);
227 goto err_out;
229 if (!client_name) {
230 client_name = malloc(40);
231 sprintf(client_name, "MPlayer [%d]", getpid());
233 if (!autostart)
234 open_options |= JackNoStartServer;
235 client = jack_client_open(client_name, open_options, NULL);
236 if (!client) {
237 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] cannot open server\n");
238 goto err_out;
240 buffer = av_fifo_alloc(BUFFSIZE);
241 jack_set_process_callback(client, outputaudio, 0);
243 // list matching ports
244 if (!port_name)
245 port_flags |= JackPortIsPhysical;
246 matching_ports = jack_get_ports(client, port_name, NULL, port_flags);
247 if (!matching_ports || !matching_ports[0]) {
248 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] no physical ports available\n");
249 goto err_out;
251 i = 1;
252 while (matching_ports[i]) i++;
253 if (channels > i) channels = i;
254 num_ports = channels;
256 // create out output ports
257 for (i = 0; i < num_ports; i++) {
258 char pname[30];
259 snprintf(pname, 30, "out_%d", i);
260 ports[i] = jack_port_register(client, pname, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
261 if (!ports[i]) {
262 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] not enough ports available\n");
263 goto err_out;
266 if (jack_activate(client)) {
267 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] activate failed\n");
268 goto err_out;
270 for (i = 0; i < num_ports; i++) {
271 if (jack_connect(client, jack_port_name(ports[i]), matching_ports[i])) {
272 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] connecting failed\n");
273 goto err_out;
276 rate = jack_get_sample_rate(client);
277 jack_latency = (float)(jack_port_get_total_latency(client, ports[0]) +
278 jack_get_buffer_size(client)) / (float)rate;
279 callback_interval = 0;
281 ao_data.channels = channels;
282 ao_data.samplerate = rate;
283 ao_data.format = AF_FORMAT_FLOAT_NE;
284 ao_data.bps = channels * rate * sizeof(float);
285 ao_data.buffersize = CHUNK_SIZE * NUM_CHUNKS;
286 ao_data.outburst = CHUNK_SIZE;
287 free(matching_ports);
288 free(port_name);
289 free(client_name);
290 return 1;
292 err_out:
293 free(matching_ports);
294 free(port_name);
295 free(client_name);
296 if (client)
297 jack_client_close(client);
298 av_fifo_free(buffer);
299 buffer = NULL;
300 return 0;
303 // close audio device
304 static void uninit(int immed) {
305 if (!immed)
306 usec_sleep(get_delay() * 1000 * 1000);
307 // HACK, make sure jack doesn't loop-output dirty buffers
308 reset();
309 usec_sleep(100 * 1000);
310 jack_client_close(client);
311 av_fifo_free(buffer);
312 buffer = NULL;
316 * \brief stop playing and empty buffers (for seeking/pause)
318 static void reset(void) {
319 paused = 1;
320 av_fifo_reset(buffer);
321 paused = 0;
325 * \brief stop playing, keep buffers (for pause)
327 static void audio_pause(void) {
328 paused = 1;
332 * \brief resume playing, after audio_pause()
334 static void audio_resume(void) {
335 paused = 0;
338 static int get_space(void) {
339 return av_fifo_space(buffer);
343 * \brief write data into buffer and reset underrun flag
345 static int play(void *data, int len, int flags) {
346 if (!(flags & AOPLAY_FINAL_CHUNK))
347 len -= len % ao_data.outburst;
348 underrun = 0;
349 return write_buffer(data, len);
352 static float get_delay(void) {
353 int buffered = av_fifo_size(buffer); // could be less
354 float in_jack = jack_latency;
355 if (estimate && callback_interval > 0) {
356 float elapsed = (float)GetTimer() / 1000000.0 - callback_time;
357 in_jack += callback_interval - elapsed;
358 if (in_jack < 0) in_jack = 0;
360 return (float)buffered / (float)ao_data.bps + in_jack;