1 |
matthewc |
476 |
/* |
2 |
|
|
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
Sound Channel Process Functions - Sun |
4 |
|
|
Copyright (C) Matthew Chapman 2003 |
5 |
|
|
Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003 |
6 |
|
|
Copyright (C) Michael Gernoth mike@zerfleddert.de 2003 |
7 |
|
|
|
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 |
|
|
#include <unistd.h> |
25 |
|
|
#include <fcntl.h> |
26 |
|
|
#include <errno.h> |
27 |
|
|
#include <sys/ioctl.h> |
28 |
matthewc |
477 |
#include <sys/audioio.h> |
29 |
matthewc |
476 |
|
30 |
stargo |
560 |
#if (defined(sun) && (defined(__svr4__) || defined(__SVR4))) |
31 |
|
|
#include <stropts.h> |
32 |
|
|
#endif |
33 |
|
|
|
34 |
matthewc |
476 |
#define MAX_QUEUE 10 |
35 |
|
|
|
36 |
|
|
int g_dsp_fd; |
37 |
stargo |
504 |
BOOL g_dsp_busy = False; |
38 |
stargo |
510 |
static BOOL g_reopened; |
39 |
|
|
static BOOL g_swapaudio; |
40 |
|
|
static short g_samplewidth; |
41 |
matthewc |
476 |
|
42 |
astrand |
499 |
static struct audio_packet |
43 |
|
|
{ |
44 |
matthewc |
476 |
struct stream s; |
45 |
|
|
uint16 tick; |
46 |
|
|
uint8 index; |
47 |
|
|
} packet_queue[MAX_QUEUE]; |
48 |
|
|
static unsigned int queue_hi, queue_lo; |
49 |
|
|
|
50 |
|
|
BOOL |
51 |
|
|
wave_out_open(void) |
52 |
|
|
{ |
53 |
matthewc |
477 |
char *dsp_dev = getenv("AUDIODEV"); |
54 |
matthewc |
476 |
|
55 |
astrand |
499 |
if (dsp_dev == NULL) |
56 |
matthewc |
477 |
{ |
57 |
astrand |
499 |
dsp_dev = "/dev/audio"; |
58 |
matthewc |
477 |
} |
59 |
|
|
|
60 |
astrand |
499 |
if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1) |
61 |
matthewc |
476 |
{ |
62 |
|
|
perror(dsp_dev); |
63 |
|
|
return False; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
/* Non-blocking so that user interface is responsive */ |
67 |
astrand |
499 |
fcntl(g_dsp_fd, F_SETFL, fcntl(g_dsp_fd, F_GETFL) | O_NONBLOCK); |
68 |
|
|
|
69 |
matthewc |
476 |
queue_lo = queue_hi = 0; |
70 |
stargo |
510 |
g_reopened = True; |
71 |
astrand |
499 |
|
72 |
matthewc |
476 |
return True; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
void |
76 |
|
|
wave_out_close(void) |
77 |
|
|
{ |
78 |
matthewc |
477 |
/* Ack all remaining packets */ |
79 |
astrand |
499 |
while (queue_lo != queue_hi) |
80 |
matthewc |
477 |
{ |
81 |
|
|
rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index); |
82 |
|
|
free(packet_queue[queue_lo].s.data); |
83 |
|
|
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
84 |
|
|
} |
85 |
|
|
|
86 |
stargo |
538 |
#if defined I_FLUSH && defined FLUSHW |
87 |
matthewc |
477 |
/* Flush the audiobuffer */ |
88 |
astrand |
499 |
ioctl(g_dsp_fd, I_FLUSH, FLUSHW); |
89 |
stargo |
538 |
#endif |
90 |
matthewc |
476 |
close(g_dsp_fd); |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
BOOL |
94 |
astrand |
499 |
wave_out_format_supported(WAVEFORMATEX * pwfx) |
95 |
matthewc |
476 |
{ |
96 |
|
|
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
97 |
|
|
return False; |
98 |
|
|
if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2)) |
99 |
|
|
return False; |
100 |
|
|
if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16)) |
101 |
|
|
return False; |
102 |
|
|
|
103 |
|
|
return True; |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
BOOL |
107 |
astrand |
499 |
wave_out_set_format(WAVEFORMATEX * pwfx) |
108 |
matthewc |
476 |
{ |
109 |
|
|
audio_info_t info; |
110 |
|
|
int test = 1; |
111 |
|
|
|
112 |
|
|
ioctl(g_dsp_fd, AUDIO_DRAIN, 0); |
113 |
stargo |
510 |
g_swapaudio = False; |
114 |
matthewc |
476 |
AUDIO_INITINFO(&info); |
115 |
|
|
|
116 |
|
|
|
117 |
|
|
if (pwfx->wBitsPerSample == 8) |
118 |
|
|
{ |
119 |
|
|
info.play.encoding = AUDIO_ENCODING_LINEAR8; |
120 |
|
|
} |
121 |
|
|
else if (pwfx->wBitsPerSample == 16) |
122 |
|
|
{ |
123 |
|
|
info.play.encoding = AUDIO_ENCODING_LINEAR; |
124 |
matthewc |
477 |
/* Do we need to swap the 16bit values? (Are we BigEndian) */ |
125 |
stargo |
510 |
g_swapaudio = !(*(uint8 *) (&test)); |
126 |
matthewc |
476 |
} |
127 |
|
|
|
128 |
stargo |
510 |
g_samplewidth = pwfx->wBitsPerSample / 8; |
129 |
stargo |
491 |
|
130 |
astrand |
499 |
if (pwfx->nChannels == 1) |
131 |
|
|
{ |
132 |
matthewc |
514 |
info.play.channels = 1; |
133 |
matthewc |
476 |
} |
134 |
astrand |
499 |
else if (pwfx->nChannels == 2) |
135 |
matthewc |
476 |
{ |
136 |
matthewc |
514 |
info.play.channels = 2; |
137 |
stargo |
510 |
g_samplewidth *= 2; |
138 |
matthewc |
476 |
} |
139 |
|
|
|
140 |
|
|
info.play.sample_rate = pwfx->nSamplesPerSec; |
141 |
|
|
info.play.precision = pwfx->wBitsPerSample; |
142 |
|
|
info.play.samples = 0; |
143 |
|
|
info.play.eof = 0; |
144 |
|
|
info.play.error = 0; |
145 |
stargo |
510 |
g_reopened = True; |
146 |
matthewc |
476 |
|
147 |
|
|
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
148 |
|
|
{ |
149 |
|
|
perror("AUDIO_SETINFO"); |
150 |
|
|
close(g_dsp_fd); |
151 |
|
|
return False; |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
return True; |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
void |
158 |
stargo |
491 |
wave_out_volume(uint16 left, uint16 right) |
159 |
|
|
{ |
160 |
|
|
audio_info_t info; |
161 |
|
|
uint balance; |
162 |
|
|
uint volume; |
163 |
|
|
|
164 |
|
|
if (ioctl(g_dsp_fd, AUDIO_GETINFO, &info) == -1) |
165 |
|
|
{ |
166 |
|
|
perror("AUDIO_GETINFO"); |
167 |
|
|
return; |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
volume = (left > right) ? left : right; |
171 |
|
|
|
172 |
astrand |
499 |
if (volume / AUDIO_MID_BALANCE != 0) |
173 |
stargo |
491 |
{ |
174 |
astrand |
499 |
balance = |
175 |
|
|
AUDIO_MID_BALANCE - (left / (volume / AUDIO_MID_BALANCE)) + |
176 |
|
|
(right / (volume / AUDIO_MID_BALANCE)); |
177 |
stargo |
491 |
} |
178 |
|
|
else |
179 |
|
|
{ |
180 |
|
|
balance = AUDIO_MID_BALANCE; |
181 |
|
|
} |
182 |
|
|
|
183 |
astrand |
499 |
info.play.gain = volume / (65536 / AUDIO_MAX_GAIN); |
184 |
stargo |
491 |
info.play.balance = balance; |
185 |
|
|
|
186 |
|
|
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
187 |
|
|
{ |
188 |
|
|
perror("AUDIO_SETINFO"); |
189 |
|
|
return; |
190 |
|
|
} |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
void |
194 |
matthewc |
476 |
wave_out_write(STREAM s, uint16 tick, uint8 index) |
195 |
|
|
{ |
196 |
|
|
struct audio_packet *packet = &packet_queue[queue_hi]; |
197 |
|
|
unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE; |
198 |
|
|
|
199 |
|
|
if (next_hi == queue_lo) |
200 |
|
|
{ |
201 |
|
|
error("No space to queue audio packet\n"); |
202 |
|
|
return; |
203 |
|
|
} |
204 |
astrand |
499 |
|
205 |
matthewc |
476 |
queue_hi = next_hi; |
206 |
|
|
|
207 |
|
|
packet->s = *s; |
208 |
|
|
packet->tick = tick; |
209 |
|
|
packet->index = index; |
210 |
stargo |
491 |
packet->s.p += 4; |
211 |
matthewc |
476 |
|
212 |
|
|
/* we steal the data buffer from s, give it a new one */ |
213 |
|
|
s->data = malloc(s->size); |
214 |
|
|
|
215 |
|
|
if (!g_dsp_busy) |
216 |
|
|
wave_out_play(); |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
void |
220 |
|
|
wave_out_play(void) |
221 |
|
|
{ |
222 |
|
|
struct audio_packet *packet; |
223 |
|
|
audio_info_t info; |
224 |
|
|
ssize_t len; |
225 |
|
|
unsigned int i; |
226 |
|
|
uint8 swap; |
227 |
|
|
STREAM out; |
228 |
|
|
static BOOL swapped = False; |
229 |
|
|
static BOOL sentcompletion = True; |
230 |
matthewc |
477 |
static uint32 samplecnt = 0; |
231 |
|
|
static uint32 numsamples; |
232 |
matthewc |
476 |
|
233 |
|
|
while (1) |
234 |
|
|
{ |
235 |
stargo |
510 |
if (g_reopened) |
236 |
matthewc |
477 |
{ |
237 |
|
|
/* Device was just (re)openend */ |
238 |
|
|
samplecnt = 0; |
239 |
|
|
swapped = False; |
240 |
|
|
sentcompletion = True; |
241 |
stargo |
510 |
g_reopened = False; |
242 |
matthewc |
477 |
} |
243 |
|
|
|
244 |
matthewc |
476 |
if (queue_lo == queue_hi) |
245 |
|
|
{ |
246 |
|
|
g_dsp_busy = 0; |
247 |
|
|
return; |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
packet = &packet_queue[queue_lo]; |
251 |
|
|
out = &packet->s; |
252 |
|
|
|
253 |
matthewc |
477 |
/* Swap the current packet, but only once */ |
254 |
stargo |
510 |
if (g_swapaudio && !swapped) |
255 |
matthewc |
476 |
{ |
256 |
astrand |
499 |
for (i = 0; i < out->end - out->p; i += 2) |
257 |
matthewc |
476 |
{ |
258 |
|
|
swap = *(out->p + i); |
259 |
stargo |
491 |
*(out->p + i) = *(out->p + i + 1); |
260 |
matthewc |
476 |
*(out->p + i + 1) = swap; |
261 |
|
|
} |
262 |
stargo |
491 |
swapped = True; |
263 |
matthewc |
476 |
} |
264 |
|
|
|
265 |
astrand |
499 |
if (sentcompletion) |
266 |
matthewc |
476 |
{ |
267 |
|
|
sentcompletion = False; |
268 |
stargo |
510 |
numsamples = (out->end - out->p) / g_samplewidth; |
269 |
matthewc |
476 |
} |
270 |
|
|
|
271 |
astrand |
499 |
len = 0; |
272 |
matthewc |
476 |
|
273 |
astrand |
499 |
if (out->end != out->p) |
274 |
matthewc |
476 |
{ |
275 |
|
|
len = write(g_dsp_fd, out->p, out->end - out->p); |
276 |
|
|
if (len == -1) |
277 |
|
|
{ |
278 |
|
|
if (errno != EWOULDBLOCK) |
279 |
|
|
perror("write audio"); |
280 |
|
|
g_dsp_busy = 1; |
281 |
|
|
return; |
282 |
|
|
} |
283 |
|
|
} |
284 |
|
|
|
285 |
|
|
out->p += len; |
286 |
|
|
if (out->p == out->end) |
287 |
|
|
{ |
288 |
|
|
if (ioctl(g_dsp_fd, AUDIO_GETINFO, &info) == -1) |
289 |
|
|
{ |
290 |
|
|
perror("AUDIO_GETINFO"); |
291 |
|
|
return; |
292 |
|
|
} |
293 |
matthewc |
477 |
|
294 |
|
|
/* Ack the packet, if we have played at least 70% */ |
295 |
astrand |
499 |
if (info.play.samples >= samplecnt + ((numsamples * 7) / 10)) |
296 |
matthewc |
476 |
{ |
297 |
matthewc |
477 |
samplecnt += numsamples; |
298 |
matthewc |
476 |
rdpsnd_send_completion(packet->tick, packet->index); |
299 |
|
|
free(out->data); |
300 |
|
|
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
301 |
|
|
swapped = False; |
302 |
|
|
sentcompletion = True; |
303 |
|
|
} |
304 |
|
|
else |
305 |
|
|
{ |
306 |
|
|
g_dsp_busy = 1; |
307 |
|
|
return; |
308 |
|
|
} |
309 |
|
|
} |
310 |
|
|
} |
311 |
|
|
} |