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
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
=
47 "Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>",
53 //! maximum number of channels supported, avoids lots of mallocs
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
;
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
71 #define BUFFSIZE (NUM_CHUNKS * CHUNK_SIZE)
73 //! buffer for audio data
74 static AVFifoBuffer
*buffer
;
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
);
99 static void deinterleave(void *info
, void *src
, int len
) {
100 struct deinterleave
*di
= info
;
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
) {
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
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
);
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
) {
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
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
];
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
);
171 if (read_buffer(bufs
, nframes
, num_ports
) < nframes
)
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
;
180 callback_interval
= (float)nframes
/ (float)ao_data
.samplerate
;
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"
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"
200 " Estimates the amount of data in buffers (experimental)\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
;
211 const 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
},
218 jack_options_t open_options
= JackUseExactName
;
219 int port_flags
= JackPortIsInput
;
222 if (subopt_parse(ao_subdevice
, subopts
) != 0) {
226 if (channels
> MAX_CHANS
) {
227 mp_msg(MSGT_AO
, MSGL_FATAL
, "[JACK] Invalid number of channels: %i\n", channels
);
231 client_name
= malloc(40);
232 sprintf(client_name
, "MPlayer [%d]", getpid());
235 open_options
|= JackNoStartServer
;
236 client
= jack_client_open(client_name
, open_options
, NULL
);
238 mp_msg(MSGT_AO
, MSGL_FATAL
, "[JACK] cannot open server\n");
241 buffer
= av_fifo_alloc(BUFFSIZE
);
242 jack_set_process_callback(client
, outputaudio
, 0);
244 // list matching ports
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");
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
++) {
260 snprintf(pname
, 30, "out_%d", i
);
261 ports
[i
] = jack_port_register(client
, pname
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
263 mp_msg(MSGT_AO
, MSGL_FATAL
, "[JACK] not enough ports available\n");
267 if (jack_activate(client
)) {
268 mp_msg(MSGT_AO
, MSGL_FATAL
, "[JACK] activate failed\n");
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");
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
);
294 free(matching_ports
);
298 jack_client_close(client
);
299 av_fifo_free(buffer
);
304 // close audio device
305 static void uninit(int immed
) {
307 usec_sleep(get_delay() * 1000 * 1000);
308 // HACK, make sure jack doesn't loop-output dirty buffers
310 usec_sleep(100 * 1000);
311 jack_client_close(client
);
312 av_fifo_free(buffer
);
317 * \brief stop playing and empty buffers (for seeking/pause)
319 static void reset(void) {
321 av_fifo_reset(buffer
);
326 * \brief stop playing, keep buffers (for pause)
328 static void audio_pause(void) {
333 * \brief resume playing, after audio_pause()
335 static void audio_resume(void) {
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
;
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
;