annotate libao2/ao_jack.c @ 15605:904787c80946

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