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