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 |
stargo |
1254 |
Copyright (C) Michael Gernoth mike@zerfleddert.de 2003-2006 |
7 |
matthewc |
476 |
|
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 |
matthewc |
476 |
#include <unistd.h> |
26 |
|
|
#include <fcntl.h> |
27 |
|
|
#include <errno.h> |
28 |
|
|
#include <sys/ioctl.h> |
29 |
matthewc |
477 |
#include <sys/audioio.h> |
30 |
matthewc |
476 |
|
31 |
stargo |
560 |
#if (defined(sun) && (defined(__svr4__) || defined(__SVR4))) |
32 |
|
|
#include <stropts.h> |
33 |
|
|
#endif |
34 |
|
|
|
35 |
stargo |
1254 |
#define DEFAULTDEVICE "/dev/audio" |
36 |
matthewc |
476 |
|
37 |
ossman_ |
1346 |
static int dsp_fd = -1; |
38 |
|
|
static BOOL dsp_busy; |
39 |
|
|
|
40 |
stargo |
510 |
static BOOL g_reopened; |
41 |
|
|
static short g_samplewidth; |
42 |
stargo |
1255 |
static char *dsp_dev; |
43 |
matthewc |
476 |
|
44 |
stargo |
1347 |
void sun_play(void); |
45 |
ossman_ |
1346 |
|
46 |
|
|
void |
47 |
|
|
sun_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv) |
48 |
|
|
{ |
49 |
|
|
if (dsp_fd == -1) |
50 |
|
|
return; |
51 |
|
|
|
52 |
|
|
if (rdpsnd_queue_empty()) |
53 |
|
|
return; |
54 |
|
|
|
55 |
|
|
FD_SET(dsp_fd, wfds); |
56 |
|
|
if (dsp_fd > *n) |
57 |
|
|
*n = dsp_fd; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
void |
61 |
|
|
sun_check_fds(fd_set * rfds, fd_set * wfds) |
62 |
|
|
{ |
63 |
|
|
if (FD_ISSET(dsp_fd, wfds)) |
64 |
|
|
sun_play(); |
65 |
|
|
} |
66 |
|
|
|
67 |
matthewc |
476 |
BOOL |
68 |
stargo |
1255 |
sun_open(void) |
69 |
matthewc |
476 |
{ |
70 |
ossman_ |
1346 |
if ((dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1) |
71 |
matthewc |
476 |
{ |
72 |
|
|
perror(dsp_dev); |
73 |
|
|
return False; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
/* Non-blocking so that user interface is responsive */ |
77 |
ossman_ |
1346 |
fcntl(dsp_fd, F_SETFL, fcntl(dsp_fd, F_GETFL) | O_NONBLOCK); |
78 |
astrand |
499 |
|
79 |
stargo |
510 |
g_reopened = True; |
80 |
astrand |
499 |
|
81 |
matthewc |
476 |
return True; |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
void |
85 |
stargo |
1255 |
sun_close(void) |
86 |
matthewc |
476 |
{ |
87 |
matthewc |
477 |
/* Ack all remaining packets */ |
88 |
stargo |
1254 |
while (!rdpsnd_queue_empty()) |
89 |
ossman_ |
1304 |
rdpsnd_queue_next(0); |
90 |
matthewc |
477 |
|
91 |
stargo |
538 |
#if defined I_FLUSH && defined FLUSHW |
92 |
matthewc |
477 |
/* Flush the audiobuffer */ |
93 |
ossman_ |
1346 |
ioctl(dsp_fd, I_FLUSH, FLUSHW); |
94 |
stargo |
538 |
#endif |
95 |
stargo |
561 |
#if defined AUDIO_FLUSH |
96 |
ossman_ |
1346 |
ioctl(dsp_fd, AUDIO_FLUSH, NULL); |
97 |
stargo |
561 |
#endif |
98 |
ossman_ |
1346 |
close(dsp_fd); |
99 |
|
|
dsp_fd = -1; |
100 |
matthewc |
476 |
} |
101 |
|
|
|
102 |
|
|
BOOL |
103 |
jsorg71 |
1364 |
sun_format_supported(RD_WAVEFORMATEX * pwfx) |
104 |
matthewc |
476 |
{ |
105 |
|
|
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
106 |
|
|
return False; |
107 |
|
|
if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2)) |
108 |
|
|
return False; |
109 |
|
|
if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16)) |
110 |
|
|
return False; |
111 |
|
|
|
112 |
|
|
return True; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
BOOL |
116 |
jsorg71 |
1364 |
sun_set_format(RD_WAVEFORMATEX * pwfx) |
117 |
matthewc |
476 |
{ |
118 |
|
|
audio_info_t info; |
119 |
|
|
|
120 |
ossman_ |
1346 |
ioctl(dsp_fd, AUDIO_DRAIN, 0); |
121 |
matthewc |
476 |
AUDIO_INITINFO(&info); |
122 |
|
|
|
123 |
|
|
|
124 |
|
|
if (pwfx->wBitsPerSample == 8) |
125 |
|
|
{ |
126 |
|
|
info.play.encoding = AUDIO_ENCODING_LINEAR8; |
127 |
|
|
} |
128 |
|
|
else if (pwfx->wBitsPerSample == 16) |
129 |
|
|
{ |
130 |
|
|
info.play.encoding = AUDIO_ENCODING_LINEAR; |
131 |
|
|
} |
132 |
|
|
|
133 |
stargo |
510 |
g_samplewidth = pwfx->wBitsPerSample / 8; |
134 |
stargo |
491 |
|
135 |
astrand |
499 |
if (pwfx->nChannels == 1) |
136 |
|
|
{ |
137 |
matthewc |
514 |
info.play.channels = 1; |
138 |
matthewc |
476 |
} |
139 |
astrand |
499 |
else if (pwfx->nChannels == 2) |
140 |
matthewc |
476 |
{ |
141 |
matthewc |
514 |
info.play.channels = 2; |
142 |
stargo |
510 |
g_samplewidth *= 2; |
143 |
matthewc |
476 |
} |
144 |
|
|
|
145 |
|
|
info.play.sample_rate = pwfx->nSamplesPerSec; |
146 |
|
|
info.play.precision = pwfx->wBitsPerSample; |
147 |
|
|
info.play.samples = 0; |
148 |
|
|
info.play.eof = 0; |
149 |
|
|
info.play.error = 0; |
150 |
stargo |
510 |
g_reopened = True; |
151 |
matthewc |
476 |
|
152 |
ossman_ |
1346 |
if (ioctl(dsp_fd, AUDIO_SETINFO, &info) == -1) |
153 |
matthewc |
476 |
{ |
154 |
|
|
perror("AUDIO_SETINFO"); |
155 |
ossman_ |
1346 |
sun_close(); |
156 |
matthewc |
476 |
return False; |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
return True; |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
void |
163 |
stargo |
1255 |
sun_volume(uint16 left, uint16 right) |
164 |
stargo |
491 |
{ |
165 |
|
|
audio_info_t info; |
166 |
|
|
uint balance; |
167 |
|
|
uint volume; |
168 |
|
|
|
169 |
stargo |
772 |
AUDIO_INITINFO(&info); |
170 |
stargo |
491 |
|
171 |
|
|
volume = (left > right) ? left : right; |
172 |
|
|
|
173 |
astrand |
499 |
if (volume / AUDIO_MID_BALANCE != 0) |
174 |
stargo |
491 |
{ |
175 |
astrand |
499 |
balance = |
176 |
|
|
AUDIO_MID_BALANCE - (left / (volume / AUDIO_MID_BALANCE)) + |
177 |
|
|
(right / (volume / AUDIO_MID_BALANCE)); |
178 |
stargo |
491 |
} |
179 |
|
|
else |
180 |
|
|
{ |
181 |
|
|
balance = AUDIO_MID_BALANCE; |
182 |
|
|
} |
183 |
|
|
|
184 |
astrand |
499 |
info.play.gain = volume / (65536 / AUDIO_MAX_GAIN); |
185 |
stargo |
491 |
info.play.balance = balance; |
186 |
|
|
|
187 |
ossman_ |
1346 |
if (ioctl(dsp_fd, AUDIO_SETINFO, &info) == -1) |
188 |
stargo |
491 |
{ |
189 |
|
|
perror("AUDIO_SETINFO"); |
190 |
|
|
return; |
191 |
|
|
} |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
void |
195 |
stargo |
1255 |
sun_play(void) |
196 |
matthewc |
476 |
{ |
197 |
|
|
struct audio_packet *packet; |
198 |
|
|
audio_info_t info; |
199 |
|
|
ssize_t len; |
200 |
|
|
unsigned int i; |
201 |
|
|
STREAM out; |
202 |
|
|
static BOOL sentcompletion = True; |
203 |
matthewc |
477 |
static uint32 samplecnt = 0; |
204 |
|
|
static uint32 numsamples; |
205 |
matthewc |
476 |
|
206 |
|
|
while (1) |
207 |
|
|
{ |
208 |
stargo |
510 |
if (g_reopened) |
209 |
matthewc |
477 |
{ |
210 |
|
|
/* Device was just (re)openend */ |
211 |
|
|
samplecnt = 0; |
212 |
|
|
sentcompletion = True; |
213 |
stargo |
510 |
g_reopened = False; |
214 |
matthewc |
477 |
} |
215 |
|
|
|
216 |
stargo |
1254 |
if (rdpsnd_queue_empty()) |
217 |
matthewc |
476 |
return; |
218 |
|
|
|
219 |
stargo |
1254 |
packet = rdpsnd_queue_current_packet(); |
220 |
matthewc |
476 |
out = &packet->s; |
221 |
|
|
|
222 |
astrand |
499 |
if (sentcompletion) |
223 |
matthewc |
476 |
{ |
224 |
|
|
sentcompletion = False; |
225 |
stargo |
510 |
numsamples = (out->end - out->p) / g_samplewidth; |
226 |
matthewc |
476 |
} |
227 |
|
|
|
228 |
astrand |
499 |
len = 0; |
229 |
matthewc |
476 |
|
230 |
astrand |
499 |
if (out->end != out->p) |
231 |
matthewc |
476 |
{ |
232 |
ossman_ |
1346 |
len = write(dsp_fd, out->p, out->end - out->p); |
233 |
matthewc |
476 |
if (len == -1) |
234 |
|
|
{ |
235 |
|
|
if (errno != EWOULDBLOCK) |
236 |
|
|
perror("write audio"); |
237 |
|
|
return; |
238 |
|
|
} |
239 |
|
|
} |
240 |
|
|
|
241 |
|
|
out->p += len; |
242 |
|
|
if (out->p == out->end) |
243 |
|
|
{ |
244 |
ossman_ |
1346 |
if (ioctl(dsp_fd, AUDIO_GETINFO, &info) == -1) |
245 |
matthewc |
476 |
{ |
246 |
|
|
perror("AUDIO_GETINFO"); |
247 |
|
|
return; |
248 |
|
|
} |
249 |
matthewc |
477 |
|
250 |
|
|
/* Ack the packet, if we have played at least 70% */ |
251 |
astrand |
499 |
if (info.play.samples >= samplecnt + ((numsamples * 7) / 10)) |
252 |
matthewc |
476 |
{ |
253 |
matthewc |
477 |
samplecnt += numsamples; |
254 |
stargo |
1254 |
/* We need to add 50 to tell windows that time has passed while |
255 |
|
|
* playing this packet */ |
256 |
ossman_ |
1302 |
rdpsnd_queue_next(50); |
257 |
matthewc |
476 |
sentcompletion = True; |
258 |
|
|
} |
259 |
|
|
else |
260 |
|
|
{ |
261 |
|
|
return; |
262 |
|
|
} |
263 |
|
|
} |
264 |
|
|
} |
265 |
|
|
} |
266 |
stargo |
1255 |
|
267 |
stargo |
1351 |
struct audio_driver * |
268 |
|
|
sun_register(char *options) |
269 |
|
|
{ |
270 |
|
|
static struct audio_driver sun_driver; |
271 |
ossman_ |
1345 |
|
272 |
ossman_ |
1356 |
memset(&sun_driver, 0, sizeof(sun_driver)); |
273 |
|
|
|
274 |
|
|
sun_driver.name = "sun"; |
275 |
stargo |
1351 |
sun_driver.description = |
276 |
ossman_ |
1356 |
"SUN/BSD output driver, default device: " DEFAULTDEVICE " or $AUDIODEV"; |
277 |
ossman_ |
1346 |
|
278 |
stargo |
1351 |
sun_driver.add_fds = sun_add_fds; |
279 |
|
|
sun_driver.check_fds = sun_check_fds; |
280 |
ossman_ |
1345 |
|
281 |
stargo |
1351 |
sun_driver.wave_out_open = sun_open; |
282 |
|
|
sun_driver.wave_out_close = sun_close; |
283 |
|
|
sun_driver.wave_out_format_supported = sun_format_supported; |
284 |
|
|
sun_driver.wave_out_set_format = sun_set_format; |
285 |
|
|
sun_driver.wave_out_volume = sun_volume; |
286 |
ossman_ |
1345 |
|
287 |
stargo |
1351 |
sun_driver.need_byteswap_on_be = 1; |
288 |
|
|
sun_driver.need_resampling = 0; |
289 |
|
|
|
290 |
stargo |
1255 |
if (options) |
291 |
|
|
{ |
292 |
|
|
dsp_dev = xstrdup(options); |
293 |
|
|
} |
294 |
|
|
else |
295 |
|
|
{ |
296 |
|
|
dsp_dev = getenv("AUDIODEV"); |
297 |
|
|
|
298 |
|
|
if (dsp_dev == NULL) |
299 |
|
|
{ |
300 |
ossman_ |
1356 |
dsp_dev = DEFAULTDEVICE; |
301 |
stargo |
1255 |
} |
302 |
|
|
} |
303 |
|
|
|
304 |
|
|
return &sun_driver; |
305 |
|
|
} |