Mercurial > mplayer.hg
annotate libao2/ao_jack.c @ 16105:a274b6bd2e23
ao_jack (no)estimate and vo_gl rectangle default value documented
author | reimar |
---|---|
date | Tue, 26 Jul 2005 10:55:42 +0000 |
parents | fc2459a18bf3 |
children | 0671f92d2cb4 |
rev | line source |
---|---|
15605 | 1 /* |
2 * ao_jack.c - libao2 JACK Audio Output Driver for MPlayer | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
3 * |
15605 | 4 * This driver is under the same license as MPlayer. |
5 * (http://www.mplayerhq.hu) | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
6 * |
15605 | 7 * Copyleft 2001 by Felix Bünemann (atmosfear@users.sf.net) |
8 * and Reimar Döffinger (Reimar.Doeffinger@stud.uni-karlsruhe.de) | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
9 */ |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
10 |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
11 #include <stdio.h> |
15605 | 12 #include <stdlib.h> |
13 #include <string.h> | |
15641
1bb5111490cd
Create a unique client name so that multiple instances work.
reimar
parents:
15630
diff
changeset
|
14 #include <unistd.h> |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
15 |
14479 | 16 #include "config.h" |
15605 | 17 #include "mp_msg.h" |
18 #include "help_mp.h" | |
19 | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
20 #include "audio_out.h" |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
21 #include "audio_out_internal.h" |
14245 | 22 #include "libaf/af_format.h" |
15605 | 23 #include "osdep/timer.h" |
24 #include "subopt-helper.h" | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
25 |
15605 | 26 #include "libvo/fastmemcpy.h" |
27 | |
28 #include <jack/jack.h> | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
29 |
15605 | 30 static ao_info_t info = |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
31 { |
15605 | 32 "JACK audio output", |
33 "jack", | |
34 "Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>", | |
35 "based on ao_sdl.c" | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
36 }; |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
37 |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
38 LIBAO_EXTERN(jack) |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
39 |
15605 | 40 //! maximum number of channels supported, avoids lots of mallocs |
41 #define MAX_CHANS 6 | |
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; | |
15630 | 45 static float jack_latency; |
16104
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
46 static int estimate; |
15605 | 47 static volatile int paused = 0; ///< set if paused |
48 static volatile int underrun = 0; ///< signals if an underrun occured | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
49 |
16104
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
50 static volatile float callback_interval = 0; |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
51 static volatile float callback_time = 0; |
15605 | 52 |
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 | |
57 #define NUM_CHUNKS 8 | |
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) | |
63 | |
64 //! buffer for audio data | |
65 static unsigned char *buffer = NULL; | |
66 | |
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; | |
71 | |
72 /** | |
73 * \brief get the number of free bytes in the buffer | |
74 * \return number of free bytes in buffer | |
75 * | |
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! | |
79 */ | |
80 static int buf_free() { | |
81 int free = read_pos - write_pos - CHUNK_SIZE; | |
82 if (free < 0) free += BUFFSIZE; | |
83 return free; | |
84 } | |
85 | |
86 /** | |
87 * \brief get amount of data available in the buffer | |
88 * \return number of bytes available in buffer | |
89 * | |
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! | |
93 */ | |
94 static int buf_used() { | |
95 int used = write_pos - read_pos; | |
96 if (used < 0) used += BUFFSIZE; | |
97 return used; | |
98 } | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
99 |
15605 | 100 /** |
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 | |
105 * | |
106 * If there is not enough room, the buffer is filled up | |
107 */ | |
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 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 memcpy (buffer, &data[first_len], len - first_len); | |
118 } | |
119 write_pos = (write_pos + len) % BUFFSIZE; | |
120 return len; | |
121 } | |
13012
2d188ebe0f3b
Update ao_jack for new bio2jack API, improve check in configure.
diego
parents:
12702
diff
changeset
|
122 |
15605 | 123 /** |
124 * \brief read data from buffer and splitting it into channels | |
125 * \param bufs num_bufs float buffers, each will contain the data of one channel | |
126 * \param cnt number of samples to read per channel | |
127 * \param num_bufs number of channels to split the data into | |
128 * \return number of samples read per channel, equals cnt unless there was too | |
129 * little data in the buffer | |
130 * | |
131 * Assumes the data in the buffer is of type float, the number of bytes | |
132 * read is res * num_bufs * sizeof(float), where res is the return value. | |
133 */ | |
134 static int read_buffer(float **bufs, int cnt, int num_bufs) { | |
135 int first_len = BUFFSIZE - read_pos; | |
136 int buffered = buf_used(); | |
137 int i, j; | |
138 if (cnt * sizeof(float) * num_bufs > buffered) | |
139 cnt = buffered / sizeof(float) / num_bufs; | |
140 for (i = 0; i < cnt; i++) { | |
141 for (j = 0; j < num_bufs; j++) { | |
142 bufs[j][i] = *((float *)(&buffer[read_pos])); | |
143 read_pos = (read_pos + sizeof(float)) % BUFFSIZE; | |
144 } | |
145 } | |
146 return cnt; | |
147 } | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
148 |
15605 | 149 // end ring buffer stuff |
150 | |
151 static int control(int cmd, void *arg) { | |
152 return CONTROL_UNKNOWN; | |
153 } | |
154 | |
155 /** | |
156 * \brief fill the buffers with silence | |
157 * \param bufs num_bufs float buffers, each will contain the data of one channel | |
158 * \param cnt number of samples in each buffer | |
159 * \param num_bufs number of buffers | |
160 */ | |
161 static void silence(float **bufs, int cnt, int num_bufs) { | |
162 int i, j; | |
163 for (i = 0; i < cnt; i++) | |
164 for (j = 0; j < num_bufs; j++) | |
165 bufs[j][i] = 0; | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
166 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
167 |
15605 | 168 /** |
169 * \brief JACK Callback function | |
170 * \param nframes number of frames to fill into buffers | |
171 * \param arg unused | |
172 * \return currently always 0 | |
173 * | |
174 * Write silence into buffers if paused or an underrun occured | |
175 */ | |
176 static int outputaudio(jack_nframes_t nframes, void *arg) { | |
177 float *bufs[MAX_CHANS]; | |
178 int i; | |
179 for (i = 0; i < num_ports; i++) | |
180 bufs[i] = jack_port_get_buffer(ports[i], nframes); | |
181 if (!paused && !underrun) | |
182 if (read_buffer(bufs, nframes, num_ports) < nframes) | |
183 underrun = 1; | |
184 if (paused || underrun) | |
185 silence(bufs, nframes, num_ports); | |
16104
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
186 if (estimate) { |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
187 float now = (float)GetTimer() / 1000000.0; |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
188 float diff = callback_time + callback_interval - now; |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
189 if (diff < 0.002) |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
190 callback_time += callback_interval; |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
191 else |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
192 callback_time = now; |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
193 callback_interval = (float)nframes / (float)ao_data.samplerate; |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
194 } |
15605 | 195 return 0; |
196 } | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
197 |
15605 | 198 /** |
199 * \brief print suboption usage help | |
200 */ | |
201 static void print_help () | |
202 { | |
203 mp_msg (MSGT_AO, MSGL_FATAL, | |
204 "\n-ao jack commandline help:\n" | |
205 "Example: mplayer -ao jack:port=myout\n" | |
206 " connects MPlayer to the jack ports named myout\n" | |
207 "\nOptions:\n" | |
208 " port=<port name>\n" | |
16104
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
209 " Connects to the given ports instead of the default physical ones\n" |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
210 " estimate\n" |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
211 " Estimates the amount of data in buffers (experimental)\n"); |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
212 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
213 |
15605 | 214 static int init(int rate, int channels, int format, int flags) { |
215 const char **matching_ports = NULL; | |
216 char *port_name = NULL; | |
15641
1bb5111490cd
Create a unique client name so that multiple instances work.
reimar
parents:
15630
diff
changeset
|
217 char client_name[40]; |
15605 | 218 opt_t subopts[] = { |
219 {"port", OPT_ARG_MSTRZ, &port_name, NULL}, | |
16104
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
220 {"estimate", OPT_ARG_BOOL, &estimate, NULL}, |
15605 | 221 {NULL} |
222 }; | |
223 int port_flags = JackPortIsInput; | |
224 int i; | |
16104
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
225 estimate = 1; |
15605 | 226 if (subopt_parse(ao_subdevice, subopts) != 0) { |
227 print_help(); | |
228 return 0; | |
229 } | |
230 if (channels > MAX_CHANS) { | |
231 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] Invalid number of channels: %i\n", channels); | |
232 goto err_out; | |
233 } | |
15641
1bb5111490cd
Create a unique client name so that multiple instances work.
reimar
parents:
15630
diff
changeset
|
234 sprintf(client_name, "MPlayer [%d]", getpid()); |
1bb5111490cd
Create a unique client name so that multiple instances work.
reimar
parents:
15630
diff
changeset
|
235 client = jack_client_new(client_name); |
15605 | 236 if (!client) { |
237 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] cannot open server\n"); | |
238 goto err_out; | |
239 } | |
15868 | 240 reset(); |
15605 | 241 jack_set_process_callback(client, outputaudio, 0); |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
242 |
15605 | 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 for (num_ports = 0; matching_ports && matching_ports[num_ports]; num_ports++) ; | |
248 if (!num_ports) { | |
249 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] no physical ports available\n"); | |
250 goto err_out; | |
251 } | |
252 if (channels > num_ports) channels = num_ports; | |
253 num_ports = channels; | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
254 |
15605 | 255 // create out output ports |
256 for (i = 0; i < num_ports; i++) { | |
257 char pname[30]; | |
258 snprintf(pname, 30, "out_%d", i); | |
259 ports[i] = jack_port_register(client, pname, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0); | |
260 if (!ports[i]) { | |
261 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] not enough ports available\n"); | |
262 goto err_out; | |
263 } | |
264 } | |
265 if (jack_activate(client)) { | |
266 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] activate failed\n"); | |
267 goto err_out; | |
268 } | |
269 for (i = 0; i < num_ports; i++) { | |
270 if (jack_connect(client, jack_port_name(ports[i]), matching_ports[i])) { | |
271 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] connecting failed\n"); | |
272 goto err_out; | |
273 } | |
274 } | |
15630 | 275 rate = jack_get_sample_rate(client); |
276 jack_latency = (float)(jack_port_get_total_latency(client, ports[0]) + | |
277 jack_get_buffer_size(client)) / (float)rate; | |
16104
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
278 callback_interval = 0; |
15605 | 279 buffer = (unsigned char *) malloc(BUFFSIZE); |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
280 |
15605 | 281 ao_data.channels = channels; |
15630 | 282 ao_data.samplerate = rate; |
15605 | 283 ao_data.format = AF_FORMAT_FLOAT_NE; |
284 ao_data.bps = channels * rate * sizeof(float); | |
15630 | 285 ao_data.buffersize = CHUNK_SIZE * NUM_CHUNKS; |
15605 | 286 ao_data.outburst = CHUNK_SIZE; |
287 free(matching_ports); | |
288 free(port_name); | |
289 return 1; | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
290 |
15605 | 291 err_out: |
292 free(matching_ports); | |
293 free(port_name); | |
294 if (client) | |
295 jack_client_close(client); | |
296 free(buffer); | |
297 buffer = NULL; | |
298 return 0; | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
299 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
300 |
15605 | 301 // close audio device |
302 static void uninit(int immed) { | |
303 if (!immed) | |
304 usec_sleep(get_delay() * 1000 * 1000); | |
305 // HACK, make sure jack doesn't loop-output dirty buffers | |
15868 | 306 reset(); |
15605 | 307 usec_sleep(100 * 1000); |
308 jack_client_close(client); | |
309 free(buffer); | |
310 buffer = NULL; | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
311 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
312 |
15605 | 313 /** |
314 * \brief stop playing and empty buffers (for seeking/pause) | |
315 */ | |
316 static void reset() { | |
317 paused = 1; | |
318 read_pos = 0; | |
319 write_pos = 0; | |
320 paused = 0; | |
321 } | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
322 |
15605 | 323 /** |
324 * \brief stop playing, keep buffers (for pause) | |
325 */ | |
326 static void audio_pause() { | |
327 paused = 1; | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
328 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
329 |
15605 | 330 /** |
331 * \brief resume playing, after audio_pause() | |
332 */ | |
333 static void audio_resume() { | |
334 paused = 0; | |
335 } | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
336 |
15605 | 337 static int get_space() { |
338 return buf_free(); | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
339 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
340 |
15605 | 341 /** |
342 * \brief write data into buffer and reset underrun flag | |
343 */ | |
344 static int play(void *data, int len, int flags) { | |
345 len -= len % ao_data.outburst; | |
346 underrun = 0; | |
347 return write_buffer(data, len); | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
348 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
349 |
15605 | 350 static float get_delay() { |
351 int buffered = BUFFSIZE - CHUNK_SIZE - buf_free(); // could be less | |
15630 | 352 float in_jack = jack_latency; |
16104
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
353 if (estimate && callback_interval > 0) { |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
354 float elapsed = (float)GetTimer() / 1000000.0 - callback_time; |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
355 in_jack += callback_interval - elapsed; |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
356 if (in_jack < 0) in_jack = 0; |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
357 } |
15605 | 358 return (float)buffered / (float)ao_data.bps + in_jack; |
359 } | |
360 |