2 * ao_jack.c - libao2 JACK Audio Output Driver for MPlayer
4 * This driver is under the same license as MPlayer.
5 * (http://www.mplayerhq.hu)
7 * Copyleft 2001 by Felix Bünemann (atmosfear@users.sf.net)
8 * and Reimar Döffinger (Reimar.Doeffinger@stud.uni-karlsruhe.de)
20 #include "audio_out.h"
21 #include "audio_out_internal.h"
22 #include "libaf/af_format.h"
23 #include "osdep/timer.h"
24 #include "subopt-helper.h"
26 #include "libvo/fastmemcpy.h"
28 #include <jack/jack.h>
30 static ao_info_t info
=
34 "Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>",
40 //! maximum number of channels supported, avoids lots of mallocs
42 static jack_port_t
*ports
[MAX_CHANS
];
43 static int num_ports
; ///< Number of used ports == number of channels
44 static jack_client_t
*client
;
45 static float jack_latency
;
47 static volatile int paused
= 0; ///< set if paused
48 static volatile int underrun
= 0; ///< signals if an underrun occured
50 static volatile float callback_interval
= 0;
51 static volatile float callback_time
= 0;
53 //! size of one chunk, if this is too small MPlayer will start to "stutter"
54 //! after a short time of playback
55 #define CHUNK_SIZE (16 * 1024)
56 //! number of "virtual" chunks the buffer consists of
58 // This type of ring buffer may never fill up completely, at least
59 // one byte must always be unused.
60 // For performance reasons (alignment etc.) one whole chunk always stays
61 // empty, not only one byte.
62 #define BUFFSIZE ((NUM_CHUNKS + 1) * CHUNK_SIZE)
64 //! buffer for audio data
65 static unsigned char *buffer
= NULL
;
67 //! buffer read position, may only be modified by playback thread or while it is stopped
68 static volatile int read_pos
;
69 //! buffer write position, may only be modified by MPlayer's thread
70 static volatile int write_pos
;
73 * \brief get the number of free bytes in the buffer
74 * \return number of free bytes in buffer
76 * may only be called by MPlayer's thread
77 * return value may change between immediately following two calls,
78 * and the real number of free bytes might be larger!
80 static int buf_free(void) {
81 int free
= read_pos
- write_pos
- CHUNK_SIZE
;
82 if (free
< 0) free
+= BUFFSIZE
;
87 * \brief get amount of data available in the buffer
88 * \return number of bytes available in buffer
90 * may only be called by the playback thread
91 * return value may change between immediately following two calls,
92 * and the real number of buffered bytes might be larger!
94 static int buf_used(void) {
95 int used
= write_pos
- read_pos
;
96 if (used
< 0) used
+= BUFFSIZE
;
101 * \brief insert len bytes into buffer
102 * \param data data to insert
103 * \param len length of data
104 * \return number of bytes inserted into buffer
106 * If there is not enough room, the buffer is filled up
108 static int write_buffer(unsigned char* data
, int len
) {
109 int first_len
= BUFFSIZE
- write_pos
;
110 int free
= buf_free();
111 if (len
> free
) len
= free
;
112 if (first_len
> len
) first_len
= len
;
113 // till end of buffer
114 fast_memcpy (&buffer
[write_pos
], data
, first_len
);
115 if (len
> first_len
) { // we have to wrap around
116 // remaining part from beginning of buffer
117 fast_memcpy (buffer
, &data
[first_len
], len
- first_len
);
119 write_pos
= (write_pos
+ len
) % BUFFSIZE
;
123 static void silence(float **bufs
, int cnt
, int num_bufs
);
126 * \brief read data from buffer and splitting it into channels
127 * \param bufs num_bufs float buffers, each will contain the data of one channel
128 * \param cnt number of samples to read per channel
129 * \param num_bufs number of channels to split the data into
130 * \return number of samples read per channel, equals cnt unless there was too
131 * little data in the buffer
133 * Assumes the data in the buffer is of type float, the number of bytes
134 * read is res * num_bufs * sizeof(float), where res is the return value.
135 * If there is not enough data in the buffer remaining parts will be filled
138 static int read_buffer(float **bufs
, int cnt
, int num_bufs
) {
139 int buffered
= buf_used();
141 if (cnt
* sizeof(float) * num_bufs
> buffered
) {
142 silence(bufs
, cnt
, num_bufs
);
143 cnt
= buffered
/ sizeof(float) / num_bufs
;
145 for (i
= 0; i
< cnt
; i
++) {
146 for (j
= 0; j
< num_bufs
; j
++) {
147 bufs
[j
][i
] = *(float *)&buffer
[read_pos
];
148 read_pos
= (read_pos
+ sizeof(float)) % BUFFSIZE
;
154 // end ring buffer stuff
156 static int control(int cmd
, void *arg
) {
157 return CONTROL_UNKNOWN
;
161 * \brief fill the buffers with silence
162 * \param bufs num_bufs float buffers, each will contain the data of one channel
163 * \param cnt number of samples in each buffer
164 * \param num_bufs number of buffers
166 static void silence(float **bufs
, int cnt
, int num_bufs
) {
168 for (i
= 0; i
< num_bufs
; i
++)
169 memset(bufs
[i
], 0, cnt
* sizeof(float));
173 * \brief JACK Callback function
174 * \param nframes number of frames to fill into buffers
176 * \return currently always 0
178 * Write silence into buffers if paused or an underrun occured
180 static int outputaudio(jack_nframes_t nframes
, void *arg
) {
181 float *bufs
[MAX_CHANS
];
183 for (i
= 0; i
< num_ports
; i
++)
184 bufs
[i
] = jack_port_get_buffer(ports
[i
], nframes
);
185 if (paused
|| underrun
)
186 silence(bufs
, nframes
, num_ports
);
188 if (read_buffer(bufs
, nframes
, num_ports
) < nframes
)
191 float now
= (float)GetTimer() / 1000000.0;
192 float diff
= callback_time
+ callback_interval
- now
;
193 if ((diff
> -0.002) && (diff
< 0.002))
194 callback_time
+= callback_interval
;
197 callback_interval
= (float)nframes
/ (float)ao_data
.samplerate
;
203 * \brief print suboption usage help
205 static void print_help (void)
207 mp_msg (MSGT_AO
, MSGL_FATAL
,
208 "\n-ao jack commandline help:\n"
209 "Example: mplayer -ao jack:port=myout\n"
210 " connects MPlayer to the jack ports named myout\n"
212 " port=<port name>\n"
213 " Connects to the given ports instead of the default physical ones\n"
214 " name=<client name>\n"
215 " Client name to pass to JACK\n"
217 " Estimates the amount of data in buffers (experimental)\n");
220 static int init(int rate
, int channels
, int format
, int flags
) {
221 const char **matching_ports
= NULL
;
222 char *port_name
= NULL
;
223 char *client_name
= NULL
;
225 {"port", OPT_ARG_MSTRZ
, &port_name
, NULL
},
226 {"name", OPT_ARG_MSTRZ
, &client_name
, NULL
},
227 {"estimate", OPT_ARG_BOOL
, &estimate
, NULL
},
230 int port_flags
= JackPortIsInput
;
233 if (subopt_parse(ao_subdevice
, subopts
) != 0) {
237 if (channels
> MAX_CHANS
) {
238 mp_msg(MSGT_AO
, MSGL_FATAL
, "[JACK] Invalid number of channels: %i\n", channels
);
242 client_name
= malloc(40);
243 sprintf(client_name
, "MPlayer [%d]", getpid());
245 client
= jack_client_new(client_name
);
247 mp_msg(MSGT_AO
, MSGL_FATAL
, "[JACK] cannot open server\n");
251 jack_set_process_callback(client
, outputaudio
, 0);
253 // list matching ports
255 port_flags
|= JackPortIsPhysical
;
256 matching_ports
= jack_get_ports(client
, port_name
, NULL
, port_flags
);
257 if (!matching_ports
|| !matching_ports
[0]) {
258 mp_msg(MSGT_AO
, MSGL_FATAL
, "[JACK] no physical ports available\n");
262 while (matching_ports
[i
]) i
++;
263 if (channels
> i
) channels
= i
;
264 num_ports
= channels
;
266 // create out output ports
267 for (i
= 0; i
< num_ports
; i
++) {
269 snprintf(pname
, 30, "out_%d", i
);
270 ports
[i
] = jack_port_register(client
, pname
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
272 mp_msg(MSGT_AO
, MSGL_FATAL
, "[JACK] not enough ports available\n");
276 if (jack_activate(client
)) {
277 mp_msg(MSGT_AO
, MSGL_FATAL
, "[JACK] activate failed\n");
280 for (i
= 0; i
< num_ports
; i
++) {
281 if (jack_connect(client
, jack_port_name(ports
[i
]), matching_ports
[i
])) {
282 mp_msg(MSGT_AO
, MSGL_FATAL
, "[JACK] connecting failed\n");
286 rate
= jack_get_sample_rate(client
);
287 jack_latency
= (float)(jack_port_get_total_latency(client
, ports
[0]) +
288 jack_get_buffer_size(client
)) / (float)rate
;
289 callback_interval
= 0;
290 buffer
= malloc(BUFFSIZE
);
292 ao_data
.channels
= channels
;
293 ao_data
.samplerate
= rate
;
294 ao_data
.format
= AF_FORMAT_FLOAT_NE
;
295 ao_data
.bps
= channels
* rate
* sizeof(float);
296 ao_data
.buffersize
= CHUNK_SIZE
* NUM_CHUNKS
;
297 ao_data
.outburst
= CHUNK_SIZE
;
298 free(matching_ports
);
304 free(matching_ports
);
308 jack_client_close(client
);
314 // close audio device
315 static void uninit(int immed
) {
317 usec_sleep(get_delay() * 1000 * 1000);
318 // HACK, make sure jack doesn't loop-output dirty buffers
320 usec_sleep(100 * 1000);
321 jack_client_close(client
);
327 * \brief stop playing and empty buffers (for seeking/pause)
329 static void reset(void) {
337 * \brief stop playing, keep buffers (for pause)
339 static void audio_pause(void) {
344 * \brief resume playing, after audio_pause()
346 static void audio_resume(void) {
350 static int get_space(void) {
355 * \brief write data into buffer and reset underrun flag
357 static int play(void *data
, int len
, int flags
) {
358 if (!(flags
& AOPLAY_FINAL_CHUNK
))
359 len
-= len
% ao_data
.outburst
;
361 return write_buffer(data
, len
);
364 static float get_delay(void) {
365 int buffered
= BUFFSIZE
- CHUNK_SIZE
- buf_free(); // could be less
366 float in_jack
= jack_latency
;
367 if (estimate
&& callback_interval
> 0) {
368 float elapsed
= (float)GetTimer() / 1000000.0 - callback_time
;
369 in_jack
+= callback_interval
- elapsed
;
370 if (in_jack
< 0) in_jack
= 0;
372 return (float)buffered
/ (float)ao_data
.bps
+ in_jack
;