Mercurial > mplayer.hg
annotate libao2/ao_jack.c @ 36299:f5e428d7991b
Add a XSync() call.
This will avoid some unpleasant visual effects
because of the transparent background to come.
author | ib |
---|---|
date | Fri, 02 Aug 2013 22:42:05 +0000 |
parents | 53cb51bbc317 |
children |
rev | line source |
---|---|
27509
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
1 /* |
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
2 * JACK audio output driver for MPlayer |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
3 * |
23734 | 4 * Copyleft 2001 by Felix Bünemann (atmosfear@users.sf.net) |
5 * and Reimar Döffinger (Reimar.Doeffinger@stud.uni-karlsruhe.de) | |
27509
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
6 * |
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
7 * This file is part of MPlayer. |
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
8 * |
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
9 * MPlayer is free software; you can redistribute it and/or modify |
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
10 * it under the terms of the GNU General Public License as published by |
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
11 * the Free Software Foundation; either version 2 of the License, or |
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
12 * (at your option) any later version. |
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
13 * |
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
14 * MPlayer is distributed in the hope that it will be useful, |
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
17 * GNU General Public License for more details. |
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
18 * |
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
19 * You should have received a copy of the GNU General Public License along |
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
20 * along with MPlayer; if not, write to the Free Software |
d97a607821f1
Replace casual GPL notices by proper license headers.
diego
parents:
25650
diff
changeset
|
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
22 */ |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
23 |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
24 #include <stdio.h> |
15605 | 25 #include <stdlib.h> |
26 #include <string.h> | |
15641
1bb5111490cd
Create a unique client name so that multiple instances work.
reimar
parents:
15630
diff
changeset
|
27 #include <unistd.h> |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
28 |
14479 | 29 #include "config.h" |
15605 | 30 #include "mp_msg.h" |
31 #include "help_mp.h" | |
32 | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
33 #include "audio_out.h" |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
34 #include "audio_out_internal.h" |
14245 | 35 #include "libaf/af_format.h" |
15605 | 36 #include "osdep/timer.h" |
37 #include "subopt-helper.h" | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
38 |
28882
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
39 #include "libavutil/fifo.h" |
15605 | 40 |
41 #include <jack/jack.h> | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
42 |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29126
diff
changeset
|
43 static const ao_info_t info = |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
44 { |
15605 | 45 "JACK audio output", |
46 "jack", | |
23734 | 47 "Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>", |
15605 | 48 "based on ao_sdl.c" |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
49 }; |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
50 |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
51 LIBAO_EXTERN(jack) |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
52 |
15605 | 53 //! maximum number of channels supported, avoids lots of mallocs |
31292
92f7d389db9f
ao_jack: increase maximum allowed channels from 6 to 8.
tack
parents:
30633
diff
changeset
|
54 #define MAX_CHANS 8 |
15605 | 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; | |
15630 | 58 static float jack_latency; |
16104
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
59 static int estimate; |
15605 | 60 static volatile int paused = 0; ///< set if paused |
61 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
|
62 |
16104
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
63 static volatile float callback_interval = 0; |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
64 static volatile float callback_time = 0; |
15605 | 65 |
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 | |
70 #define NUM_CHUNKS 8 | |
28882
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
71 #define BUFFSIZE (NUM_CHUNKS * CHUNK_SIZE) |
15605 | 72 |
73 //! buffer for audio data | |
28882
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
74 static AVFifoBuffer *buffer; |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
75 |
15605 | 76 /** |
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 | |
81 * | |
82 * If there is not enough room, the buffer is filled up | |
83 */ | |
84 static int write_buffer(unsigned char* data, int len) { | |
29126 | 85 int free = av_fifo_space(buffer); |
15605 | 86 if (len > free) len = free; |
28882
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
87 return av_fifo_generic_write(buffer, data, len, NULL); |
15605 | 88 } |
13012
2d188ebe0f3b
Update ao_jack for new bio2jack API, improve check in configure.
diego
parents:
12702
diff
changeset
|
89 |
25648
2438052a176e
Another small simplification. Slightly worse performance in the case
reimar
parents:
25647
diff
changeset
|
90 static void silence(float **bufs, int cnt, int num_bufs); |
2438052a176e
Another small simplification. Slightly worse performance in the case
reimar
parents:
25647
diff
changeset
|
91 |
28882
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
92 struct deinterleave { |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
93 float **bufs; |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
94 int num_bufs; |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
95 int cur_buf; |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
96 int pos; |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
97 }; |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
98 |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
99 static void deinterleave(void *info, void *src, int len) { |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
100 struct deinterleave *di = info; |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
101 float *s = src; |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
102 int i; |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
103 len /= sizeof(float); |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
104 for (i = 0; i < len; i++) { |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
105 di->bufs[di->cur_buf++][di->pos] = s[i]; |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
106 if (di->cur_buf >= di->num_bufs) { |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
107 di->cur_buf = 0; |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
108 di->pos++; |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
109 } |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
110 } |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
111 } |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
112 |
15605 | 113 /** |
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 | |
120 * | |
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. | |
18808
cb83184bdc70
respect AOPLAY_FINAL_CHUNK and do not discard samples read from buffer
reimar
parents:
18807
diff
changeset
|
123 * If there is not enough data in the buffer remaining parts will be filled |
cb83184bdc70
respect AOPLAY_FINAL_CHUNK and do not discard samples read from buffer
reimar
parents:
18807
diff
changeset
|
124 * with silence. |
15605 | 125 */ |
126 static int read_buffer(float **bufs, int cnt, int num_bufs) { | |
28882
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
127 struct deinterleave di = {bufs, num_bufs, 0, 0}; |
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
128 int buffered = av_fifo_size(buffer); |
25647 | 129 if (cnt * sizeof(float) * num_bufs > buffered) { |
25648
2438052a176e
Another small simplification. Slightly worse performance in the case
reimar
parents:
25647
diff
changeset
|
130 silence(bufs, cnt, num_bufs); |
15605 | 131 cnt = buffered / sizeof(float) / num_bufs; |
25647 | 132 } |
28882
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
133 av_fifo_generic_read(buffer, &di, cnt * num_bufs * sizeof(float), deinterleave); |
15605 | 134 return cnt; |
135 } | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
136 |
15605 | 137 // end ring buffer stuff |
138 | |
139 static int control(int cmd, void *arg) { | |
140 return CONTROL_UNKNOWN; | |
141 } | |
142 | |
143 /** | |
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 | |
148 */ | |
149 static void silence(float **bufs, int cnt, int num_bufs) { | |
25646 | 150 int i; |
151 for (i = 0; i < num_bufs; i++) | |
152 memset(bufs[i], 0, cnt * sizeof(float)); | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
153 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
154 |
15605 | 155 /** |
156 * \brief JACK Callback function | |
157 * \param nframes number of frames to fill into buffers | |
158 * \param arg unused | |
159 * \return currently always 0 | |
160 * | |
161 * Write silence into buffers if paused or an underrun occured | |
162 */ | |
163 static int outputaudio(jack_nframes_t nframes, void *arg) { | |
164 float *bufs[MAX_CHANS]; | |
165 int i; | |
166 for (i = 0; i < num_ports; i++) | |
167 bufs[i] = jack_port_get_buffer(ports[i], nframes); | |
18808
cb83184bdc70
respect AOPLAY_FINAL_CHUNK and do not discard samples read from buffer
reimar
parents:
18807
diff
changeset
|
168 if (paused || underrun) |
cb83184bdc70
respect AOPLAY_FINAL_CHUNK and do not discard samples read from buffer
reimar
parents:
18807
diff
changeset
|
169 silence(bufs, nframes, num_ports); |
cb83184bdc70
respect AOPLAY_FINAL_CHUNK and do not discard samples read from buffer
reimar
parents:
18807
diff
changeset
|
170 else |
15605 | 171 if (read_buffer(bufs, nframes, num_ports) < nframes) |
172 underrun = 1; | |
16104
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
173 if (estimate) { |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
174 float now = (float)GetTimer() / 1000000.0; |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
175 float diff = callback_time + callback_interval - now; |
16140 | 176 if ((diff > -0.002) && (diff < 0.002)) |
16104
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
177 callback_time += callback_interval; |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
178 else |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
179 callback_time = now; |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
180 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
|
181 } |
15605 | 182 return 0; |
183 } | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
184 |
15605 | 185 /** |
186 * \brief print suboption usage help | |
187 */ | |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
18885
diff
changeset
|
188 static void print_help (void) |
15605 | 189 { |
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" | |
194 "\nOptions:\n" | |
36201 | 195 " connect\n" |
196 " Automatically connect to output ports\n" | |
15605 | 197 " port=<port name>\n" |
16104
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
198 " Connects to the given ports instead of the default physical ones\n" |
16253 | 199 " name=<client name>\n" |
200 " Client name to pass to JACK\n" | |
16104
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
201 " estimate\n" |
28206
678a664a79ea
Replace deprecated jack_client_new with jack_client_open.
reimar
parents:
27509
diff
changeset
|
202 " Estimates the amount of data in buffers (experimental)\n" |
678a664a79ea
Replace deprecated jack_client_new with jack_client_open.
reimar
parents:
27509
diff
changeset
|
203 " autostart\n" |
678a664a79ea
Replace deprecated jack_client_new with jack_client_open.
reimar
parents:
27509
diff
changeset
|
204 " Automatically start JACK server if necessary\n" |
678a664a79ea
Replace deprecated jack_client_new with jack_client_open.
reimar
parents:
27509
diff
changeset
|
205 ); |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
206 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
207 |
15605 | 208 static int init(int rate, int channels, int format, int flags) { |
209 const char **matching_ports = NULL; | |
210 char *port_name = NULL; | |
16253 | 211 char *client_name = NULL; |
28206
678a664a79ea
Replace deprecated jack_client_new with jack_client_open.
reimar
parents:
27509
diff
changeset
|
212 int autostart = 0; |
36201 | 213 int connect = 1; |
29586
2eff450157cd
The suboption parser now takes a const options list, so mark them all const.
reimar
parents:
29263
diff
changeset
|
214 const opt_t subopts[] = { |
15605 | 215 {"port", OPT_ARG_MSTRZ, &port_name, NULL}, |
16253 | 216 {"name", OPT_ARG_MSTRZ, &client_name, NULL}, |
16104
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
217 {"estimate", OPT_ARG_BOOL, &estimate, NULL}, |
28206
678a664a79ea
Replace deprecated jack_client_new with jack_client_open.
reimar
parents:
27509
diff
changeset
|
218 {"autostart", OPT_ARG_BOOL, &autostart, NULL}, |
36201 | 219 {"connect", OPT_ARG_BOOL, &connect, NULL}, |
15605 | 220 {NULL} |
221 }; | |
28206
678a664a79ea
Replace deprecated jack_client_new with jack_client_open.
reimar
parents:
27509
diff
changeset
|
222 jack_options_t open_options = JackUseExactName; |
15605 | 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 } | |
16253 | 234 if (!client_name) { |
18885 | 235 client_name = malloc(40); |
25645 | 236 sprintf(client_name, "MPlayer [%d]", getpid()); |
16253 | 237 } |
28206
678a664a79ea
Replace deprecated jack_client_new with jack_client_open.
reimar
parents:
27509
diff
changeset
|
238 if (!autostart) |
678a664a79ea
Replace deprecated jack_client_new with jack_client_open.
reimar
parents:
27509
diff
changeset
|
239 open_options |= JackNoStartServer; |
678a664a79ea
Replace deprecated jack_client_new with jack_client_open.
reimar
parents:
27509
diff
changeset
|
240 client = jack_client_open(client_name, open_options, NULL); |
15605 | 241 if (!client) { |
242 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] cannot open server\n"); | |
243 goto err_out; | |
244 } | |
28882
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
245 buffer = av_fifo_alloc(BUFFSIZE); |
15605 | 246 jack_set_process_callback(client, outputaudio, 0); |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
247 |
36201 | 248 // list matching ports if connections should be made |
249 if (connect) { | |
36202 | 250 if (!port_name) |
251 port_flags |= JackPortIsPhysical; | |
252 matching_ports = jack_get_ports(client, port_name, NULL, port_flags); | |
253 i = 0; | |
254 while (matching_ports && matching_ports[i]) i++; | |
255 if (!i) { | |
256 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] no physical ports available\n"); | |
257 goto err_out; | |
258 } | |
259 if (channels > i) channels = i; | |
36201 | 260 } |
15605 | 261 num_ports = channels; |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
262 |
36195 | 263 // create out_* output ports |
15605 | 264 for (i = 0; i < num_ports; i++) { |
265 char pname[30]; | |
266 snprintf(pname, 30, "out_%d", i); | |
267 ports[i] = jack_port_register(client, pname, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0); | |
268 if (!ports[i]) { | |
269 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] not enough ports available\n"); | |
270 goto err_out; | |
271 } | |
272 } | |
273 if (jack_activate(client)) { | |
274 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] activate failed\n"); | |
275 goto err_out; | |
276 } | |
36139 | 277 for (i = 0; i < num_ports && matching_ports && matching_ports[i]; i++) { |
15605 | 278 if (jack_connect(client, jack_port_name(ports[i]), matching_ports[i])) { |
279 mp_msg(MSGT_AO, MSGL_FATAL, "[JACK] connecting failed\n"); | |
280 goto err_out; | |
281 } | |
282 } | |
15630 | 283 rate = jack_get_sample_rate(client); |
284 jack_latency = (float)(jack_port_get_total_latency(client, ports[0]) + | |
285 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
|
286 callback_interval = 0; |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
287 |
15605 | 288 ao_data.channels = channels; |
15630 | 289 ao_data.samplerate = rate; |
15605 | 290 ao_data.format = AF_FORMAT_FLOAT_NE; |
291 ao_data.bps = channels * rate * sizeof(float); | |
15630 | 292 ao_data.buffersize = CHUNK_SIZE * NUM_CHUNKS; |
15605 | 293 ao_data.outburst = CHUNK_SIZE; |
294 free(matching_ports); | |
295 free(port_name); | |
16253 | 296 free(client_name); |
15605 | 297 return 1; |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
298 |
15605 | 299 err_out: |
300 free(matching_ports); | |
301 free(port_name); | |
16253 | 302 free(client_name); |
15605 | 303 if (client) |
304 jack_client_close(client); | |
28882
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
305 av_fifo_free(buffer); |
15605 | 306 buffer = NULL; |
307 return 0; | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
308 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
309 |
15605 | 310 // close audio device |
311 static void uninit(int immed) { | |
312 if (!immed) | |
313 usec_sleep(get_delay() * 1000 * 1000); | |
314 // HACK, make sure jack doesn't loop-output dirty buffers | |
15868 | 315 reset(); |
15605 | 316 usec_sleep(100 * 1000); |
317 jack_client_close(client); | |
28882
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
318 av_fifo_free(buffer); |
15605 | 319 buffer = NULL; |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
320 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
321 |
15605 | 322 /** |
323 * \brief stop playing and empty buffers (for seeking/pause) | |
324 */ | |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
18885
diff
changeset
|
325 static void reset(void) { |
15605 | 326 paused = 1; |
28882
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
327 av_fifo_reset(buffer); |
15605 | 328 paused = 0; |
329 } | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
330 |
15605 | 331 /** |
332 * \brief stop playing, keep buffers (for pause) | |
333 */ | |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
18885
diff
changeset
|
334 static void audio_pause(void) { |
15605 | 335 paused = 1; |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
336 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
337 |
15605 | 338 /** |
339 * \brief resume playing, after audio_pause() | |
340 */ | |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
18885
diff
changeset
|
341 static void audio_resume(void) { |
15605 | 342 paused = 0; |
343 } | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
344 |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
18885
diff
changeset
|
345 static int get_space(void) { |
29126 | 346 return av_fifo_space(buffer); |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
347 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
348 |
15605 | 349 /** |
350 * \brief write data into buffer and reset underrun flag | |
351 */ | |
352 static int play(void *data, int len, int flags) { | |
18808
cb83184bdc70
respect AOPLAY_FINAL_CHUNK and do not discard samples read from buffer
reimar
parents:
18807
diff
changeset
|
353 if (!(flags & AOPLAY_FINAL_CHUNK)) |
25645 | 354 len -= len % ao_data.outburst; |
15605 | 355 underrun = 0; |
356 return write_buffer(data, len); | |
12662
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
357 } |
05d46af5e2bf
JACK audio support through bio2jack by Kamil Strzelecki <esack@o2.pl>
alex
parents:
diff
changeset
|
358 |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
18885
diff
changeset
|
359 static float get_delay(void) { |
28882
15f93fd5cd48
Reuse libavutil fifo code instead of reimplementing it over and over.
reimar
parents:
28823
diff
changeset
|
360 int buffered = av_fifo_size(buffer); // could be less |
15630 | 361 float in_jack = jack_latency; |
16104
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
362 if (estimate && callback_interval > 0) { |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
363 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
|
364 in_jack += callback_interval - elapsed; |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
365 if (in_jack < 0) in_jack = 0; |
fc2459a18bf3
improved audio delay estimation, supposed to help make the video smoother
reimar
parents:
15868
diff
changeset
|
366 } |
15605 | 367 return (float)buffered / (float)ao_data.bps + in_jack; |
368 } |