1 |
astrand |
963 |
/* -*- c-basic-offset: 8 -*- |
2 |
matthewc |
476 |
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 |
974 |
dsp_dev = xstrdup("/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 |
stargo |
561 |
#if defined AUDIO_FLUSH |
91 |
|
|
ioctl(g_dsp_fd, AUDIO_FLUSH, NULL); |
92 |
|
|
#endif |
93 |
matthewc |
476 |
close(g_dsp_fd); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
BOOL |
97 |
astrand |
499 |
wave_out_format_supported(WAVEFORMATEX * pwfx) |
98 |
matthewc |
476 |
{ |
99 |
|
|
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
100 |
|
|
return False; |
101 |
|
|
if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2)) |
102 |
|
|
return False; |
103 |
|
|
if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16)) |
104 |
|
|
return False; |
105 |
|
|
|
106 |
|
|
return True; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
BOOL |
110 |
astrand |
499 |
wave_out_set_format(WAVEFORMATEX * pwfx) |
111 |
matthewc |
476 |
{ |
112 |
|
|
audio_info_t info; |
113 |
|
|
|
114 |
|
|
ioctl(g_dsp_fd, AUDIO_DRAIN, 0); |
115 |
stargo |
510 |
g_swapaudio = False; |
116 |
matthewc |
476 |
AUDIO_INITINFO(&info); |
117 |
|
|
|
118 |
|
|
|
119 |
|
|
if (pwfx->wBitsPerSample == 8) |
120 |
|
|
{ |
121 |
|
|
info.play.encoding = AUDIO_ENCODING_LINEAR8; |
122 |
|
|
} |
123 |
|
|
else if (pwfx->wBitsPerSample == 16) |
124 |
|
|
{ |
125 |
|
|
info.play.encoding = AUDIO_ENCODING_LINEAR; |
126 |
matthewc |
477 |
/* Do we need to swap the 16bit values? (Are we BigEndian) */ |
127 |
stargo |
694 |
#ifdef B_ENDIAN |
128 |
|
|
g_swapaudio = 1; |
129 |
|
|
#else |
130 |
|
|
g_swapaudio = 0; |
131 |
|
|
#endif |
132 |
matthewc |
476 |
} |
133 |
|
|
|
134 |
stargo |
510 |
g_samplewidth = pwfx->wBitsPerSample / 8; |
135 |
stargo |
491 |
|
136 |
astrand |
499 |
if (pwfx->nChannels == 1) |
137 |
|
|
{ |
138 |
matthewc |
514 |
info.play.channels = 1; |
139 |
matthewc |
476 |
} |
140 |
astrand |
499 |
else if (pwfx->nChannels == 2) |
141 |
matthewc |
476 |
{ |
142 |
matthewc |
514 |
info.play.channels = 2; |
143 |
stargo |
510 |
g_samplewidth *= 2; |
144 |
matthewc |
476 |
} |
145 |
|
|
|
146 |
|
|
info.play.sample_rate = pwfx->nSamplesPerSec; |
147 |
|
|
info.play.precision = pwfx->wBitsPerSample; |
148 |
|
|
info.play.samples = 0; |
149 |
|
|
info.play.eof = 0; |
150 |
|
|
info.play.error = 0; |
151 |
stargo |
510 |
g_reopened = True; |
152 |
matthewc |
476 |
|
153 |
|
|
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
154 |
|
|
{ |
155 |
|
|
perror("AUDIO_SETINFO"); |
156 |
|
|
close(g_dsp_fd); |
157 |
|
|
return False; |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
return True; |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
void |
164 |
stargo |
491 |
wave_out_volume(uint16 left, uint16 right) |
165 |
|
|
{ |
166 |
|
|
audio_info_t info; |
167 |
|
|
uint balance; |
168 |
|
|
uint volume; |
169 |
|
|
|
170 |
stargo |
772 |
AUDIO_INITINFO(&info); |
171 |
stargo |
491 |
|
172 |
|
|
volume = (left > right) ? left : right; |
173 |
|
|
|
174 |
astrand |
499 |
if (volume / AUDIO_MID_BALANCE != 0) |
175 |
stargo |
491 |
{ |
176 |
astrand |
499 |
balance = |
177 |
|
|
AUDIO_MID_BALANCE - (left / (volume / AUDIO_MID_BALANCE)) + |
178 |
|
|
(right / (volume / AUDIO_MID_BALANCE)); |
179 |
stargo |
491 |
} |
180 |
|
|
else |
181 |
|
|
{ |
182 |
|
|
balance = AUDIO_MID_BALANCE; |
183 |
|
|
} |
184 |
|
|
|
185 |
astrand |
499 |
info.play.gain = volume / (65536 / AUDIO_MAX_GAIN); |
186 |
stargo |
491 |
info.play.balance = balance; |
187 |
|
|
|
188 |
|
|
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
189 |
|
|
{ |
190 |
|
|
perror("AUDIO_SETINFO"); |
191 |
|
|
return; |
192 |
|
|
} |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
void |
196 |
matthewc |
476 |
wave_out_write(STREAM s, uint16 tick, uint8 index) |
197 |
|
|
{ |
198 |
|
|
struct audio_packet *packet = &packet_queue[queue_hi]; |
199 |
|
|
unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE; |
200 |
|
|
|
201 |
|
|
if (next_hi == queue_lo) |
202 |
|
|
{ |
203 |
|
|
error("No space to queue audio packet\n"); |
204 |
|
|
return; |
205 |
|
|
} |
206 |
astrand |
499 |
|
207 |
matthewc |
476 |
queue_hi = next_hi; |
208 |
|
|
|
209 |
|
|
packet->s = *s; |
210 |
|
|
packet->tick = tick; |
211 |
|
|
packet->index = index; |
212 |
stargo |
491 |
packet->s.p += 4; |
213 |
matthewc |
476 |
|
214 |
|
|
/* we steal the data buffer from s, give it a new one */ |
215 |
|
|
s->data = malloc(s->size); |
216 |
|
|
|
217 |
|
|
if (!g_dsp_busy) |
218 |
|
|
wave_out_play(); |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
void |
222 |
|
|
wave_out_play(void) |
223 |
|
|
{ |
224 |
|
|
struct audio_packet *packet; |
225 |
|
|
audio_info_t info; |
226 |
|
|
ssize_t len; |
227 |
|
|
unsigned int i; |
228 |
|
|
uint8 swap; |
229 |
|
|
STREAM out; |
230 |
|
|
static BOOL swapped = False; |
231 |
|
|
static BOOL sentcompletion = True; |
232 |
matthewc |
477 |
static uint32 samplecnt = 0; |
233 |
|
|
static uint32 numsamples; |
234 |
matthewc |
476 |
|
235 |
|
|
while (1) |
236 |
|
|
{ |
237 |
stargo |
510 |
if (g_reopened) |
238 |
matthewc |
477 |
{ |
239 |
|
|
/* Device was just (re)openend */ |
240 |
|
|
samplecnt = 0; |
241 |
|
|
swapped = False; |
242 |
|
|
sentcompletion = True; |
243 |
stargo |
510 |
g_reopened = False; |
244 |
matthewc |
477 |
} |
245 |
|
|
|
246 |
matthewc |
476 |
if (queue_lo == queue_hi) |
247 |
|
|
{ |
248 |
|
|
g_dsp_busy = 0; |
249 |
|
|
return; |
250 |
|
|
} |
251 |
|
|
|
252 |
|
|
packet = &packet_queue[queue_lo]; |
253 |
|
|
out = &packet->s; |
254 |
|
|
|
255 |
matthewc |
477 |
/* Swap the current packet, but only once */ |
256 |
stargo |
510 |
if (g_swapaudio && !swapped) |
257 |
matthewc |
476 |
{ |
258 |
astrand |
499 |
for (i = 0; i < out->end - out->p; i += 2) |
259 |
matthewc |
476 |
{ |
260 |
|
|
swap = *(out->p + i); |
261 |
stargo |
491 |
*(out->p + i) = *(out->p + i + 1); |
262 |
matthewc |
476 |
*(out->p + i + 1) = swap; |
263 |
|
|
} |
264 |
stargo |
491 |
swapped = True; |
265 |
matthewc |
476 |
} |
266 |
|
|
|
267 |
astrand |
499 |
if (sentcompletion) |
268 |
matthewc |
476 |
{ |
269 |
|
|
sentcompletion = False; |
270 |
stargo |
510 |
numsamples = (out->end - out->p) / g_samplewidth; |
271 |
matthewc |
476 |
} |
272 |
|
|
|
273 |
astrand |
499 |
len = 0; |
274 |
matthewc |
476 |
|
275 |
astrand |
499 |
if (out->end != out->p) |
276 |
matthewc |
476 |
{ |
277 |
|
|
len = write(g_dsp_fd, out->p, out->end - out->p); |
278 |
|
|
if (len == -1) |
279 |
|
|
{ |
280 |
|
|
if (errno != EWOULDBLOCK) |
281 |
|
|
perror("write audio"); |
282 |
|
|
g_dsp_busy = 1; |
283 |
|
|
return; |
284 |
|
|
} |
285 |
|
|
} |
286 |
|
|
|
287 |
|
|
out->p += len; |
288 |
|
|
if (out->p == out->end) |
289 |
|
|
{ |
290 |
|
|
if (ioctl(g_dsp_fd, AUDIO_GETINFO, &info) == -1) |
291 |
|
|
{ |
292 |
|
|
perror("AUDIO_GETINFO"); |
293 |
|
|
return; |
294 |
|
|
} |
295 |
matthewc |
477 |
|
296 |
|
|
/* Ack the packet, if we have played at least 70% */ |
297 |
astrand |
499 |
if (info.play.samples >= samplecnt + ((numsamples * 7) / 10)) |
298 |
matthewc |
476 |
{ |
299 |
matthewc |
477 |
samplecnt += numsamples; |
300 |
matthewc |
476 |
rdpsnd_send_completion(packet->tick, packet->index); |
301 |
|
|
free(out->data); |
302 |
|
|
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
303 |
|
|
swapped = False; |
304 |
|
|
sentcompletion = True; |
305 |
|
|
} |
306 |
|
|
else |
307 |
|
|
{ |
308 |
|
|
g_dsp_busy = 1; |
309 |
|
|
return; |
310 |
|
|
} |
311 |
|
|
} |
312 |
|
|
} |
313 |
|
|
} |