Mercurial > mplayer.hg
annotate libao2/ao_jack.c @ 15874:483e955893b8
update libdvdread to v0.9.4
author | aurel |
---|---|
date | Thu, 30 Jun 2005 22:48:26 +0000 |
parents | 8bb0701496ec |
children | fc2459a18bf3 |
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; |
15605 | 46 static volatile int paused = 0; ///< set if paused |
47 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
|
48 |
15605 | 49 //! If this is defined try to make a more precise delay estimation. Will be slower. |
50 #undef JACK_ESTIMATE_DELAY | |
51 #ifdef JACK_ESTIMATE_DELAY | |
52 static volatile int callback_samples = 0; | |
53 static volatile unsigned int callback_time = 0; | |
54 #endif | |
55 | |
56 //! size of one chunk, if this is too small MPlayer will start to "stutter" | |
57 //! after a short time of playback | |
58 #define CHUNK_SIZE (16 * 1024) | |
59 //! number of "virtual" chunks the buffer consists of | |
60 #define NUM_CHUNKS 8 | |
61 // This type of ring buffer may never fill up completely, at least | |
62 // one byte must always be unused. | |
63 // For performance reasons (alignment etc.) one whole chunk always stays | |
64 // empty, not only one byte. | |
65 #define BUFFSIZE ((NUM_CHUNKS + 1) * CHUNK_SIZE) | |
66 | |
67 //! buffer for audio data | |
68 static unsigned char *buffer = NULL; | |
69 | |
70 //! buffer read position, may only be modified by playback thread or while it is stopped | |
71 static volatile int read_pos; | |
72 //! buffer write position, may only be modified by MPlayer's thread | |
73 static volatile int write_pos; | |
74 | |
75 /** | |
76 * \brief get the number of free bytes in the buffer | |
77 * \return number of free bytes in buffer | |
78 * | |
79 * may only be called by MPlayer's thread | |
80 * return value may change between immediately following two calls, | |
81 * and the real number of free bytes might be larger! | |
82 */ | |
83 static int buf_free() { | |
84 int free = read_pos - write_pos - CHUNK_SIZE; | |
85 if (free < 0) free += BUFFSIZE; | |
86 return free; | |
87 } | |
88 | |
89 /** | |
90 * \brief get amount of data available in the buffer | |
91 * \return number of bytes available in buffer | |
92 * | |
93 * may only be called by the playback thread | |
94 * return value may change between immediately following two calls, | |
95 * and the real number of buffered bytes might be larger! | |
96 */ | |
97 static int buf_used() { | |
98 int used = write_pos - read_pos; | |
99 if (used < 0) used += BUFFSIZE; | |
100 return used; | |
101 } | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
102 |
15605 | 103 /** |
104 * \brief insert len bytes into buffer | |
105 * \param data data to insert | |
106 * \param len length of data | |
107 * \return number of bytes inserted into buffer | |
108 * | |
109 * If there is not enough room, the buffer is filled up | |
110 */ | |
111 static int write_buffer(unsigned char* data, int len) { | |
112 int first_len = BUFFSIZE - write_pos; | |
113 int free = buf_free(); | |
114 if (len > free) len = free; | |
115 if (first_len > len) first_len = len; | |
116 // till end of buffer | |
117 memcpy (&buffer[write_pos], data, first_len); | |
118 if (len > first_len) { // we have to wrap around | |
119 // remaining part from beginning of buffer | |
120 memcpy (buffer, &data[first_len], len - first_len); | |
121 } | |
122 write_pos = (write_pos + len) % BUFFSIZE; | |
123 return len; | |
124 } | |
13012
2d188ebe0f3b
Update ao_jack for new bio2jack API, improve check in configure.
diego
parents:
12702
diff
changeset
|
125 |
15605 | 126 /** |
127 * \brief read data from buffer and splitting it into channels | |
128 * \param bufs num_bufs float buffers, each will contain the data of one channel | |
129 * \param cnt number of samples to read per channel | |
130 * \param num_bufs number of channels to split the data into | |
131 * \return number of samples read per channel, equals cnt unless there was too | |
132 * little data in the buffer | |
133 * | |
134 * Assumes the data in the buffer is of type float, the number of bytes | |
135 * read is res * num_bufs * sizeof(float), where res is the return value. | |
136 */ | |
137 static int read_buffer(float **bufs, int cnt, int num_bufs) { | |
138 int first_len = BUFFSIZE - read_pos; | |
139 int buffered = buf_used(); | |
140 int i, j; | |
141 if (cnt * sizeof(float) * num_bufs > buffered) | |
142 cnt = buffered / sizeof(float) / num_bufs; | |
143 for (i = 0; i < cnt; i++) { | |
144 for (j = 0; j < num_bufs; j++) { | |
145 bufs[j][i] = *((float *)(&buffer[read_pos])); | |
146 read_pos = (read_pos + sizeof(float)) % BUFFSIZE; | |
147 } | |
148 } | |
149 return cnt; | |
150 } | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
151 |
15605 | 152 // end ring buffer stuff |
153 | |
154 static int control(int cmd, void *arg) { | |
155 return CONTROL_UNKNOWN; | |
156 } | |
157 | |
158 /** | |
159 * \brief fill the buffers with silence | |
160 * \param bufs num_bufs float buffers, each will contain the data of one channel | |
161 * \param cnt number of samples in each buffer | |
162 * \param num_bufs number of buffers | |
163 */ | |
164 static void silence(float **bufs, int cnt, int num_bufs) { | |
165 int i, j; | |
166 for (i = 0; i < cnt; i++) | |
167 for (j = 0; j < num_bufs; j++) | |
168 bufs[j][i] = 0; | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
169 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
170 |
15605 | 171 /** |
172 * \brief JACK Callback function | |
173 * \param nframes number of frames to fill into buffers | |
174 * \param arg unused | |
175 * \return currently always 0 | |
176 * | |
177 * Write silence into buffers if paused or an underrun occured | |
178 */ | |
179 static int outputaudio(jack_nframes_t nframes, void *arg) { | |
180 float *bufs[MAX_CHANS]; | |
181 int i; | |
182 for (i = 0; i < num_ports; i++) | |
183 bufs[i] = jack_port_get_buffer(ports[i], nframes); | |
184 if (!paused && !underrun) | |
185 if (read_buffer(bufs, nframes, num_ports) < nframes) | |
186 underrun = 1; | |
187 if (paused || underrun) | |
188 silence(bufs, nframes, num_ports); | |
189 #ifdef JACK_ESTIMATE_DELAY | |
190 callback_samples = nframes; | |
191 callback_time = GetTimer(); | |
192 #endif | |
193 return 0; | |
194 } | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
195 |
15605 | 196 /** |
197 * \brief print suboption usage help | |
198 */ | |
199 static void print_help () | |
200 { | |
201 mp_msg (MSGT_AO, MSGL_FATAL, | |
202 "\n-ao jack commandline help:\n" | |
203 "Example: mplayer -ao jack:port=myout\n" | |
204 " connects MPlayer to the jack ports named myout\n" | |
205 "\nOptions:\n" | |
206 " port=<port name>\n" | |
207 " Connects to the given ports instead of the default physical ones\n"); | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
208 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
209 |
15605 | 210 static int init(int rate, int channels, int format, int flags) { |
211 const char **matching_ports = NULL; | |
212 char *port_name = NULL; | |
15641
1bb5111490cd
Create a unique client name so that multiple instances work.
reimar
parents:
15630
diff
changeset
|
213 char client_name[40]; |
15605 | 214 opt_t subopts[] = { |
215 {"port", OPT_ARG_MSTRZ, &port_name, NULL}, | |
216 {NULL} | |
217 }; | |
218 int port_flags = JackPortIsInput; | |
219 int i; | |
220 if (subopt_parse(ao_subdevice, subopts) != 0) { | |
221 print_help(); | |
222 return 0; | |
223 } | |
224 if (channels > MAX_CHANS) { | |
225 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] Invalid number of channels: %i\n", channels); | |
226 goto err_out; | |
227 } | |
15641
1bb5111490cd
Create a unique client name so that multiple instances work.
reimar
parents:
15630
diff
changeset
|
228 sprintf(client_name, "MPlayer [%d]", getpid()); |
1bb5111490cd
Create a unique client name so that multiple instances work.
reimar
parents:
15630
diff
changeset
|
229 client = jack_client_new(client_name); |
15605 | 230 if (!client) { |
231 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] cannot open server\n"); | |
232 goto err_out; | |
233 } | |
15868 | 234 reset(); |
15605 | 235 jack_set_process_callback(client, outputaudio, 0); |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
236 |
15605 | 237 // list matching ports |
238 if (!port_name) | |
239 port_flags |= JackPortIsPhysical; | |
240 matching_ports = jack_get_ports(client, port_name, NULL, port_flags); | |
241 for (num_ports = 0; matching_ports && matching_ports[num_ports]; num_ports++) ; | |
242 if (!num_ports) { | |
243 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] no physical ports available\n"); | |
244 goto err_out; | |
245 } | |
246 if (channels > num_ports) channels = num_ports; | |
247 num_ports = channels; | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
248 |
15605 | 249 // create out output ports |
250 for (i = 0; i < num_ports; i++) { | |
251 char pname[30]; | |
252 snprintf(pname, 30, "out_%d", i); | |
253 ports[i] = jack_port_register(client, pname, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0); | |
254 if (!ports[i]) { | |
255 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] not enough ports available\n"); | |
256 goto err_out; | |
257 } | |
258 } | |
259 if (jack_activate(client)) { | |
260 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] activate failed\n"); | |
261 goto err_out; | |
262 } | |
263 for (i = 0; i < num_ports; i++) { | |
264 if (jack_connect(client, jack_port_name(ports[i]), matching_ports[i])) { | |
265 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] connecting failed\n"); | |
266 goto err_out; | |
267 } | |
268 } | |
15630 | 269 rate = jack_get_sample_rate(client); |
270 jack_latency = (float)(jack_port_get_total_latency(client, ports[0]) + | |
271 jack_get_buffer_size(client)) / (float)rate; | |
15605 | 272 buffer = (unsigned char *) malloc(BUFFSIZE); |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
273 |
15605 | 274 ao_data.channels = channels; |
15630 | 275 ao_data.samplerate = rate; |
15605 | 276 ao_data.format = AF_FORMAT_FLOAT_NE; |
277 ao_data.bps = channels * rate * sizeof(float); | |
15630 | 278 ao_data.buffersize = CHUNK_SIZE * NUM_CHUNKS; |
15605 | 279 ao_data.outburst = CHUNK_SIZE; |
280 free(matching_ports); | |
281 free(port_name); | |
282 return 1; | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
283 |
15605 | 284 err_out: |
285 free(matching_ports); | |
286 free(port_name); | |
287 if (client) | |
288 jack_client_close(client); | |
289 free(buffer); | |
290 buffer = NULL; | |
291 return 0; | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
292 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
293 |
15605 | 294 // close audio device |
295 static void uninit(int immed) { | |
296 if (!immed) | |
297 usec_sleep(get_delay() * 1000 * 1000); | |
298 // HACK, make sure jack doesn't loop-output dirty buffers | |
15868 | 299 reset(); |
15605 | 300 usec_sleep(100 * 1000); |
301 jack_client_close(client); | |
302 free(buffer); | |
303 buffer = NULL; | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
304 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
305 |
15605 | 306 /** |
307 * \brief stop playing and empty buffers (for seeking/pause) | |
308 */ | |
309 static void reset() { | |
310 paused = 1; | |
311 read_pos = 0; | |
312 write_pos = 0; | |
313 paused = 0; | |
314 } | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
315 |
15605 | 316 /** |
317 * \brief stop playing, keep buffers (for pause) | |
318 */ | |
319 static void audio_pause() { | |
320 paused = 1; | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
321 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
322 |
15605 | 323 /** |
324 * \brief resume playing, after audio_pause() | |
325 */ | |
326 static void audio_resume() { | |
327 paused = 0; | |
328 } | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
329 |
15605 | 330 static int get_space() { |
331 return buf_free(); | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
332 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
333 |
15605 | 334 /** |
335 * \brief write data into buffer and reset underrun flag | |
336 */ | |
337 static int play(void *data, int len, int flags) { | |
338 len -= len % ao_data.outburst; | |
339 underrun = 0; | |
340 return write_buffer(data, len); | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
341 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
342 |
15605 | 343 static float get_delay() { |
344 int buffered = BUFFSIZE - CHUNK_SIZE - buf_free(); // could be less | |
15630 | 345 float in_jack = jack_latency; |
15605 | 346 #ifdef JACK_ESTIMATE_DELAY |
347 unsigned int elapsed = GetTimer() - callback_time; | |
348 in_jack += (float)callback_samples / (float)ao_data.samplerate - (float)elapsed / 1000.0 / 1000.0; | |
349 if (in_jack < 0) in_jack = 0; | |
350 #endif | |
351 return (float)buffered / (float)ao_data.bps + in_jack; | |
352 } | |
353 |