1 |
astrand |
963 |
/* -*- c-basic-offset: 8 -*- |
2 |
stargo |
833 |
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
Sound Channel Process Functions - libao-driver |
4 |
|
|
Copyright (C) Matthew Chapman 2003 |
5 |
|
|
Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003 |
6 |
stargo |
1254 |
Copyright (C) Michael Gernoth mike@zerfleddert.de 2005-2006 |
7 |
stargo |
833 |
|
8 |
|
|
This program is free software; you can redistribute it and/or modify |
9 |
|
|
it under the terms of the GNU General Public License as published by |
10 |
|
|
the Free Software Foundation; either version 2 of the License, or |
11 |
|
|
(at your option) any later version. |
12 |
|
|
|
13 |
|
|
This program is distributed in the hope that it will be useful, |
14 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
|
|
GNU General Public License for more details. |
17 |
|
|
|
18 |
|
|
You should have received a copy of the GNU General Public License |
19 |
|
|
along with this program; if not, write to the Free Software |
20 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 |
|
|
*/ |
22 |
|
|
|
23 |
|
|
#include "rdesktop.h" |
24 |
stargo |
1254 |
#include "rdpsnd.h" |
25 |
stargo |
1258 |
#include "rdpsnd_dsp.h" |
26 |
stargo |
833 |
#include <unistd.h> |
27 |
|
|
#include <fcntl.h> |
28 |
|
|
#include <errno.h> |
29 |
|
|
#include <ao/ao.h> |
30 |
stargo |
891 |
#include <sys/time.h> |
31 |
stargo |
833 |
|
32 |
stargo |
1045 |
#define WAVEOUTBUF 16 |
33 |
stargo |
833 |
|
34 |
stargo |
1245 |
static ao_device *o_device = NULL; |
35 |
|
|
static int default_driver; |
36 |
|
|
static int samplerate; |
37 |
|
|
static int audiochannels; |
38 |
|
|
static BOOL reopened; |
39 |
|
|
static short samplewidth; |
40 |
stargo |
1255 |
static char *libao_device = NULL; |
41 |
stargo |
833 |
|
42 |
|
|
BOOL |
43 |
stargo |
1255 |
libao_open(void) |
44 |
stargo |
833 |
{ |
45 |
|
|
ao_sample_format format; |
46 |
stargo |
1255 |
static int warned = 0; |
47 |
stargo |
833 |
|
48 |
stargo |
1255 |
if (!warned && libao_device) |
49 |
|
|
{ |
50 |
|
|
warning("device-options not supported for libao-driver\n"); |
51 |
|
|
warned = 1; |
52 |
|
|
} |
53 |
|
|
|
54 |
stargo |
833 |
ao_initialize(); |
55 |
|
|
default_driver = ao_default_driver_id(); |
56 |
|
|
|
57 |
|
|
format.bits = 16; |
58 |
|
|
format.channels = 2; |
59 |
stargo |
1245 |
audiochannels = 2; |
60 |
stargo |
833 |
format.rate = 44100; |
61 |
stargo |
1245 |
samplerate = 44100; |
62 |
stargo |
833 |
format.byte_format = AO_FMT_LITTLE; |
63 |
|
|
|
64 |
|
|
o_device = ao_open_live(default_driver, &format, NULL); |
65 |
|
|
if (o_device == NULL) |
66 |
|
|
{ |
67 |
|
|
return False; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
g_dsp_fd = 0; |
71 |
stargo |
1254 |
rdpsnd_queue_init(); |
72 |
stargo |
833 |
|
73 |
stargo |
1245 |
reopened = True; |
74 |
stargo |
891 |
|
75 |
stargo |
833 |
return True; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
void |
79 |
stargo |
1255 |
libao_close(void) |
80 |
stargo |
833 |
{ |
81 |
|
|
/* Ack all remaining packets */ |
82 |
stargo |
1254 |
while (!rdpsnd_queue_empty()) |
83 |
stargo |
833 |
{ |
84 |
stargo |
1254 |
rdpsnd_send_completion(rdpsnd_queue_current_packet()->tick, |
85 |
|
|
rdpsnd_queue_current_packet()->index); |
86 |
|
|
rdpsnd_queue_next(); |
87 |
stargo |
833 |
} |
88 |
|
|
|
89 |
|
|
if (o_device != NULL) |
90 |
|
|
ao_close(o_device); |
91 |
stargo |
838 |
|
92 |
stargo |
833 |
ao_shutdown(); |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
BOOL |
96 |
stargo |
1255 |
libao_format_supported(WAVEFORMATEX * pwfx) |
97 |
stargo |
833 |
{ |
98 |
|
|
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
99 |
|
|
return False; |
100 |
|
|
if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2)) |
101 |
|
|
return False; |
102 |
|
|
if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16)) |
103 |
|
|
return False; |
104 |
|
|
/* The only common denominator between libao output drivers is a sample-rate of |
105 |
stargo |
835 |
44100, we need to upsample 22050 to it */ |
106 |
|
|
if ((pwfx->nSamplesPerSec != 44100) && (pwfx->nSamplesPerSec != 22050)) |
107 |
stargo |
833 |
return False; |
108 |
|
|
|
109 |
|
|
return True; |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
BOOL |
113 |
stargo |
1255 |
libao_set_format(WAVEFORMATEX * pwfx) |
114 |
stargo |
833 |
{ |
115 |
|
|
ao_sample_format format; |
116 |
|
|
|
117 |
|
|
format.bits = pwfx->wBitsPerSample; |
118 |
|
|
format.channels = pwfx->nChannels; |
119 |
stargo |
1245 |
audiochannels = pwfx->nChannels; |
120 |
stargo |
833 |
format.rate = 44100; |
121 |
stargo |
1245 |
samplerate = pwfx->nSamplesPerSec; |
122 |
stargo |
833 |
format.byte_format = AO_FMT_LITTLE; |
123 |
|
|
|
124 |
stargo |
1245 |
samplewidth = pwfx->wBitsPerSample / 8; |
125 |
stargo |
833 |
|
126 |
stargo |
837 |
if (o_device != NULL) |
127 |
stargo |
833 |
ao_close(o_device); |
128 |
|
|
|
129 |
|
|
o_device = ao_open_live(default_driver, &format, NULL); |
130 |
|
|
if (o_device == NULL) |
131 |
|
|
{ |
132 |
|
|
return False; |
133 |
|
|
} |
134 |
|
|
|
135 |
stargo |
1245 |
reopened = True; |
136 |
stargo |
833 |
|
137 |
|
|
return True; |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
void |
141 |
stargo |
1255 |
libao_play(void) |
142 |
stargo |
833 |
{ |
143 |
|
|
struct audio_packet *packet; |
144 |
|
|
STREAM out; |
145 |
stargo |
1043 |
char outbuf[WAVEOUTBUF]; |
146 |
stargo |
837 |
int offset, len, i; |
147 |
stargo |
891 |
static long prev_s, prev_us; |
148 |
|
|
unsigned int duration; |
149 |
|
|
struct timeval tv; |
150 |
|
|
int next_tick; |
151 |
stargo |
833 |
|
152 |
stargo |
1245 |
if (reopened) |
153 |
stargo |
891 |
{ |
154 |
stargo |
1245 |
reopened = False; |
155 |
stargo |
891 |
gettimeofday(&tv, NULL); |
156 |
|
|
prev_s = tv.tv_sec; |
157 |
|
|
prev_us = tv.tv_usec; |
158 |
|
|
} |
159 |
|
|
|
160 |
stargo |
1254 |
if (rdpsnd_queue_empty()) |
161 |
stargo |
833 |
{ |
162 |
stargo |
835 |
g_dsp_busy = 0; |
163 |
|
|
return; |
164 |
|
|
} |
165 |
stargo |
833 |
|
166 |
stargo |
1254 |
packet = rdpsnd_queue_current_packet(); |
167 |
stargo |
835 |
out = &packet->s; |
168 |
stargo |
833 |
|
169 |
stargo |
1254 |
next_tick = rdpsnd_queue_next_tick(); |
170 |
stargo |
891 |
|
171 |
stargo |
835 |
len = 0; |
172 |
stargo |
833 |
|
173 |
stargo |
1245 |
if (samplerate == 22050) |
174 |
stargo |
835 |
{ |
175 |
|
|
/* Resample to 44100 */ |
176 |
stargo |
1245 |
for (i = 0; (i < ((WAVEOUTBUF / 4) * (3 - samplewidth))) && (out->p < out->end); |
177 |
stargo |
837 |
i++) |
178 |
stargo |
835 |
{ |
179 |
stargo |
841 |
/* On a stereo-channel we must make sure that left and right |
180 |
|
|
does not get mixed up, so we need to expand the sample- |
181 |
|
|
data with channels in mind: 1234 -> 12123434 |
182 |
|
|
If we have a mono-channel, we can expand the data by simply |
183 |
|
|
doubling the sample-data: 1234 -> 11223344 */ |
184 |
stargo |
1245 |
if (audiochannels == 2) |
185 |
|
|
offset = ((i * 2) - (i & 1)) * samplewidth; |
186 |
stargo |
840 |
else |
187 |
stargo |
1245 |
offset = (i * 2) * samplewidth; |
188 |
stargo |
840 |
|
189 |
stargo |
1245 |
memcpy(&outbuf[offset], out->p, samplewidth); |
190 |
|
|
memcpy(&outbuf[audiochannels * samplewidth + offset], out->p, samplewidth); |
191 |
stargo |
833 |
|
192 |
stargo |
1245 |
out->p += samplewidth; |
193 |
|
|
len += 2 * samplewidth; |
194 |
stargo |
833 |
} |
195 |
|
|
} |
196 |
stargo |
835 |
else |
197 |
|
|
{ |
198 |
stargo |
836 |
len = (WAVEOUTBUF > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTBUF; |
199 |
stargo |
837 |
memcpy(outbuf, out->p, len); |
200 |
stargo |
835 |
out->p += len; |
201 |
|
|
} |
202 |
|
|
|
203 |
stargo |
1043 |
ao_play(o_device, outbuf, len); |
204 |
stargo |
835 |
|
205 |
stargo |
891 |
gettimeofday(&tv, NULL); |
206 |
|
|
|
207 |
|
|
duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000; |
208 |
|
|
|
209 |
|
|
if (packet->tick > next_tick) |
210 |
|
|
next_tick += 65536; |
211 |
|
|
|
212 |
|
|
if ((out->p == out->end) || duration > next_tick - packet->tick + 500) |
213 |
stargo |
835 |
{ |
214 |
stargo |
891 |
prev_s = tv.tv_sec; |
215 |
|
|
prev_us = tv.tv_usec; |
216 |
|
|
|
217 |
|
|
if (abs((next_tick - packet->tick) - duration) > 20) |
218 |
|
|
{ |
219 |
|
|
DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick)); |
220 |
|
|
DEBUG(("last: %d, is: %d, should: %d\n", packet->tick, |
221 |
|
|
(packet->tick + duration) % 65536, next_tick % 65536)); |
222 |
|
|
} |
223 |
|
|
|
224 |
stargo |
1254 |
rdpsnd_send_completion(((packet->tick + duration) % 65536), packet->index); |
225 |
|
|
rdpsnd_queue_next(); |
226 |
stargo |
835 |
} |
227 |
|
|
|
228 |
|
|
g_dsp_busy = 1; |
229 |
|
|
return; |
230 |
stargo |
833 |
} |
231 |
stargo |
1255 |
|
232 |
|
|
struct audio_driver * |
233 |
|
|
libao_register(char *options) |
234 |
|
|
{ |
235 |
stargo |
1256 |
static struct audio_driver libao_driver; |
236 |
|
|
|
237 |
|
|
libao_driver.wave_out_write = rdpsnd_queue_write; |
238 |
|
|
libao_driver.wave_out_open = libao_open; |
239 |
|
|
libao_driver.wave_out_close = libao_close; |
240 |
|
|
libao_driver.wave_out_format_supported = libao_format_supported; |
241 |
|
|
libao_driver.wave_out_set_format = libao_set_format; |
242 |
stargo |
1258 |
libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set; |
243 |
stargo |
1256 |
libao_driver.wave_out_play = libao_play; |
244 |
|
|
libao_driver.name = xstrdup("libao"); |
245 |
|
|
libao_driver.description = xstrdup("libao output driver"); |
246 |
stargo |
1260 |
libao_driver.need_byteswap_on_be = 0; |
247 |
stargo |
1256 |
libao_driver.next = NULL; |
248 |
|
|
|
249 |
stargo |
1255 |
if (options) |
250 |
|
|
{ |
251 |
|
|
libao_device = xstrdup(options); |
252 |
|
|
} |
253 |
|
|
|
254 |
|
|
return &libao_driver; |
255 |
|
|
} |