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