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
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
=
46 "Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>",
52 //! maximum number of channels supported, avoids lots of mallocs
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
;
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
70 #define BUFFSIZE (NUM_CHUNKS * CHUNK_SIZE)
72 //! buffer for audio data
73 static AVFifoBuffer
*buffer
;
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
);
98 static void deinterleave(void *info
, void *src
, int len
) {
99 struct deinterleave
*di
= info
;
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
) {
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
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
);
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
) {
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
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
];
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
);
170 if (read_buffer(bufs
, nframes
, num_ports
) < nframes
)
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
;
179 callback_interval
= (float)nframes
/ (float)ao_data
.samplerate
;
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"
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"
199 " Estimates the amount of data in buffers (experimental)\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
;
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
},
217 jack_options_t open_options
= JackUseExactName
;
218 int port_flags
= JackPortIsInput
;
221 if (subopt_parse(ao_subdevice
, subopts
) != 0) {
225 if (channels
> MAX_CHANS
) {
226 mp_msg(MSGT_AO
, MSGL_FATAL
, "[JACK] Invalid number of channels: %i\n", channels
);
230 client_name
= malloc(40);
231 sprintf(client_name
, "MPlayer [%d]", getpid());
234 open_options
|= JackNoStartServer
;
235 client
= jack_client_open(client_name
, open_options
, NULL
);
237 mp_msg(MSGT_AO
, MSGL_FATAL
, "[JACK] cannot open server\n");
240 buffer
= av_fifo_alloc(BUFFSIZE
);
241 jack_set_process_callback(client
, outputaudio
, 0);
243 // list matching ports
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");
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
++) {
259 snprintf(pname
, 30, "out_%d", i
);
260 ports
[i
] = jack_port_register(client
, pname
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
262 mp_msg(MSGT_AO
, MSGL_FATAL
, "[JACK] not enough ports available\n");
266 if (jack_activate(client
)) {
267 mp_msg(MSGT_AO
, MSGL_FATAL
, "[JACK] activate failed\n");
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");
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
);
293 free(matching_ports
);
297 jack_client_close(client
);
298 av_fifo_free(buffer
);
303 // close audio device
304 static void uninit(int immed
) {
306 usec_sleep(get_delay() * 1000 * 1000);
307 // HACK, make sure jack doesn't loop-output dirty buffers
309 usec_sleep(100 * 1000);
310 jack_client_close(client
);
311 av_fifo_free(buffer
);
316 * \brief stop playing and empty buffers (for seeking/pause)
318 static void reset(void) {
320 av_fifo_reset(buffer
);
325 * \brief stop playing, keep buffers (for pause)
327 static void audio_pause(void) {
332 * \brief resume playing, after audio_pause()
334 static void audio_resume(void) {
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
;
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
;