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