3 |
Sound Channel Process Functions - Sun |
Sound Channel Process Functions - Sun |
4 |
Copyright (C) Matthew Chapman 2003 |
Copyright (C) Matthew Chapman 2003 |
5 |
Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003 |
Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003 |
6 |
Copyright (C) Michael Gernoth mike@zerfleddert.de 2003 |
Copyright (C) Michael Gernoth mike@zerfleddert.de 2003-2006 |
7 |
|
|
8 |
This program is free software; you can redistribute it and/or modify |
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 |
it under the terms of the GNU General Public License as published by |
21 |
*/ |
*/ |
22 |
|
|
23 |
#include "rdesktop.h" |
#include "rdesktop.h" |
24 |
|
#include "rdpsnd.h" |
25 |
#include <unistd.h> |
#include <unistd.h> |
26 |
#include <fcntl.h> |
#include <fcntl.h> |
27 |
#include <errno.h> |
#include <errno.h> |
32 |
#include <stropts.h> |
#include <stropts.h> |
33 |
#endif |
#endif |
34 |
|
|
35 |
#define MAX_QUEUE 10 |
#define DEFAULTDEVICE "/dev/audio" |
36 |
|
|
37 |
|
static int dsp_fd = -1; |
38 |
|
static BOOL dsp_busy; |
39 |
|
|
|
int g_dsp_fd; |
|
|
BOOL g_dsp_busy = False; |
|
40 |
static BOOL g_reopened; |
static BOOL g_reopened; |
|
static BOOL g_swapaudio; |
|
41 |
static short g_samplewidth; |
static short g_samplewidth; |
42 |
|
static char *dsp_dev; |
43 |
|
|
44 |
static struct audio_packet |
void oss_play(void); |
|
{ |
|
|
struct stream s; |
|
|
uint16 tick; |
|
|
uint8 index; |
|
|
} packet_queue[MAX_QUEUE]; |
|
|
static unsigned int queue_hi, queue_lo; |
|
45 |
|
|
46 |
BOOL |
void |
47 |
wave_out_open(void) |
sun_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv) |
48 |
{ |
{ |
49 |
char *dsp_dev = getenv("AUDIODEV"); |
if (dsp_fd == -1) |
50 |
|
return; |
51 |
|
|
52 |
if (dsp_dev == NULL) |
if (rdpsnd_queue_empty()) |
53 |
{ |
return; |
54 |
dsp_dev = xstrdup("/dev/audio"); |
|
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 |
if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1) |
BOOL |
68 |
|
sun_open(void) |
69 |
|
{ |
70 |
|
if ((dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1) |
71 |
{ |
{ |
72 |
perror(dsp_dev); |
perror(dsp_dev); |
73 |
return False; |
return False; |
74 |
} |
} |
75 |
|
|
76 |
/* Non-blocking so that user interface is responsive */ |
/* Non-blocking so that user interface is responsive */ |
77 |
fcntl(g_dsp_fd, F_SETFL, fcntl(g_dsp_fd, F_GETFL) | O_NONBLOCK); |
fcntl(dsp_fd, F_SETFL, fcntl(dsp_fd, F_GETFL) | O_NONBLOCK); |
78 |
|
|
|
queue_lo = queue_hi = 0; |
|
79 |
g_reopened = True; |
g_reopened = True; |
80 |
|
|
81 |
return True; |
return True; |
82 |
} |
} |
83 |
|
|
84 |
void |
void |
85 |
wave_out_close(void) |
sun_close(void) |
86 |
{ |
{ |
87 |
/* Ack all remaining packets */ |
/* Ack all remaining packets */ |
88 |
while (queue_lo != queue_hi) |
while (!rdpsnd_queue_empty()) |
89 |
{ |
rdpsnd_queue_next(0); |
|
rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index); |
|
|
free(packet_queue[queue_lo].s.data); |
|
|
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
|
|
} |
|
90 |
|
|
91 |
#if defined I_FLUSH && defined FLUSHW |
#if defined I_FLUSH && defined FLUSHW |
92 |
/* Flush the audiobuffer */ |
/* Flush the audiobuffer */ |
93 |
ioctl(g_dsp_fd, I_FLUSH, FLUSHW); |
ioctl(dsp_fd, I_FLUSH, FLUSHW); |
94 |
#endif |
#endif |
95 |
#if defined AUDIO_FLUSH |
#if defined AUDIO_FLUSH |
96 |
ioctl(g_dsp_fd, AUDIO_FLUSH, NULL); |
ioctl(dsp_fd, AUDIO_FLUSH, NULL); |
97 |
#endif |
#endif |
98 |
close(g_dsp_fd); |
close(dsp_fd); |
99 |
|
dsp_fd = -1; |
100 |
} |
} |
101 |
|
|
102 |
BOOL |
BOOL |
103 |
wave_out_format_supported(WAVEFORMATEX * pwfx) |
sun_format_supported(WAVEFORMATEX * pwfx) |
104 |
{ |
{ |
105 |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
106 |
return False; |
return False; |
113 |
} |
} |
114 |
|
|
115 |
BOOL |
BOOL |
116 |
wave_out_set_format(WAVEFORMATEX * pwfx) |
sun_set_format(WAVEFORMATEX * pwfx) |
117 |
{ |
{ |
118 |
audio_info_t info; |
audio_info_t info; |
119 |
|
|
120 |
ioctl(g_dsp_fd, AUDIO_DRAIN, 0); |
ioctl(dsp_fd, AUDIO_DRAIN, 0); |
|
g_swapaudio = False; |
|
121 |
AUDIO_INITINFO(&info); |
AUDIO_INITINFO(&info); |
122 |
|
|
123 |
|
|
128 |
else if (pwfx->wBitsPerSample == 16) |
else if (pwfx->wBitsPerSample == 16) |
129 |
{ |
{ |
130 |
info.play.encoding = AUDIO_ENCODING_LINEAR; |
info.play.encoding = AUDIO_ENCODING_LINEAR; |
|
/* Do we need to swap the 16bit values? (Are we BigEndian) */ |
|
|
#ifdef B_ENDIAN |
|
|
g_swapaudio = 1; |
|
|
#else |
|
|
g_swapaudio = 0; |
|
|
#endif |
|
131 |
} |
} |
132 |
|
|
133 |
g_samplewidth = pwfx->wBitsPerSample / 8; |
g_samplewidth = pwfx->wBitsPerSample / 8; |
149 |
info.play.error = 0; |
info.play.error = 0; |
150 |
g_reopened = True; |
g_reopened = True; |
151 |
|
|
152 |
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
if (ioctl(dsp_fd, AUDIO_SETINFO, &info) == -1) |
153 |
{ |
{ |
154 |
perror("AUDIO_SETINFO"); |
perror("AUDIO_SETINFO"); |
155 |
close(g_dsp_fd); |
sun_close(); |
156 |
return False; |
return False; |
157 |
} |
} |
158 |
|
|
160 |
} |
} |
161 |
|
|
162 |
void |
void |
163 |
wave_out_volume(uint16 left, uint16 right) |
sun_volume(uint16 left, uint16 right) |
164 |
{ |
{ |
165 |
audio_info_t info; |
audio_info_t info; |
166 |
uint balance; |
uint balance; |
184 |
info.play.gain = volume / (65536 / AUDIO_MAX_GAIN); |
info.play.gain = volume / (65536 / AUDIO_MAX_GAIN); |
185 |
info.play.balance = balance; |
info.play.balance = balance; |
186 |
|
|
187 |
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
if (ioctl(dsp_fd, AUDIO_SETINFO, &info) == -1) |
188 |
{ |
{ |
189 |
perror("AUDIO_SETINFO"); |
perror("AUDIO_SETINFO"); |
190 |
return; |
return; |
192 |
} |
} |
193 |
|
|
194 |
void |
void |
195 |
wave_out_write(STREAM s, uint16 tick, uint8 index) |
sun_play(void) |
|
{ |
|
|
struct audio_packet *packet = &packet_queue[queue_hi]; |
|
|
unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE; |
|
|
|
|
|
if (next_hi == queue_lo) |
|
|
{ |
|
|
error("No space to queue audio packet\n"); |
|
|
return; |
|
|
} |
|
|
|
|
|
queue_hi = next_hi; |
|
|
|
|
|
packet->s = *s; |
|
|
packet->tick = tick; |
|
|
packet->index = index; |
|
|
packet->s.p += 4; |
|
|
|
|
|
/* we steal the data buffer from s, give it a new one */ |
|
|
s->data = malloc(s->size); |
|
|
|
|
|
if (!g_dsp_busy) |
|
|
wave_out_play(); |
|
|
} |
|
|
|
|
|
void |
|
|
wave_out_play(void) |
|
196 |
{ |
{ |
197 |
struct audio_packet *packet; |
struct audio_packet *packet; |
198 |
audio_info_t info; |
audio_info_t info; |
199 |
ssize_t len; |
ssize_t len; |
200 |
unsigned int i; |
unsigned int i; |
|
uint8 swap; |
|
201 |
STREAM out; |
STREAM out; |
|
static BOOL swapped = False; |
|
202 |
static BOOL sentcompletion = True; |
static BOOL sentcompletion = True; |
203 |
static uint32 samplecnt = 0; |
static uint32 samplecnt = 0; |
204 |
static uint32 numsamples; |
static uint32 numsamples; |
209 |
{ |
{ |
210 |
/* Device was just (re)openend */ |
/* Device was just (re)openend */ |
211 |
samplecnt = 0; |
samplecnt = 0; |
|
swapped = False; |
|
212 |
sentcompletion = True; |
sentcompletion = True; |
213 |
g_reopened = False; |
g_reopened = False; |
214 |
} |
} |
215 |
|
|
216 |
if (queue_lo == queue_hi) |
if (rdpsnd_queue_empty()) |
|
{ |
|
|
g_dsp_busy = 0; |
|
217 |
return; |
return; |
|
} |
|
218 |
|
|
219 |
packet = &packet_queue[queue_lo]; |
packet = rdpsnd_queue_current_packet(); |
220 |
out = &packet->s; |
out = &packet->s; |
221 |
|
|
|
/* Swap the current packet, but only once */ |
|
|
if (g_swapaudio && !swapped) |
|
|
{ |
|
|
for (i = 0; i < out->end - out->p; i += 2) |
|
|
{ |
|
|
swap = *(out->p + i); |
|
|
*(out->p + i) = *(out->p + i + 1); |
|
|
*(out->p + i + 1) = swap; |
|
|
} |
|
|
swapped = True; |
|
|
} |
|
|
|
|
222 |
if (sentcompletion) |
if (sentcompletion) |
223 |
{ |
{ |
224 |
sentcompletion = False; |
sentcompletion = False; |
229 |
|
|
230 |
if (out->end != out->p) |
if (out->end != out->p) |
231 |
{ |
{ |
232 |
len = write(g_dsp_fd, out->p, out->end - out->p); |
len = write(dsp_fd, out->p, out->end - out->p); |
233 |
if (len == -1) |
if (len == -1) |
234 |
{ |
{ |
235 |
if (errno != EWOULDBLOCK) |
if (errno != EWOULDBLOCK) |
236 |
perror("write audio"); |
perror("write audio"); |
|
g_dsp_busy = 1; |
|
237 |
return; |
return; |
238 |
} |
} |
239 |
} |
} |
241 |
out->p += len; |
out->p += len; |
242 |
if (out->p == out->end) |
if (out->p == out->end) |
243 |
{ |
{ |
244 |
if (ioctl(g_dsp_fd, AUDIO_GETINFO, &info) == -1) |
if (ioctl(dsp_fd, AUDIO_GETINFO, &info) == -1) |
245 |
{ |
{ |
246 |
perror("AUDIO_GETINFO"); |
perror("AUDIO_GETINFO"); |
247 |
return; |
return; |
251 |
if (info.play.samples >= samplecnt + ((numsamples * 7) / 10)) |
if (info.play.samples >= samplecnt + ((numsamples * 7) / 10)) |
252 |
{ |
{ |
253 |
samplecnt += numsamples; |
samplecnt += numsamples; |
254 |
rdpsnd_send_completion(packet->tick, packet->index); |
/* We need to add 50 to tell windows that time has passed while |
255 |
free(out->data); |
* playing this packet */ |
256 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
rdpsnd_queue_next(50); |
|
swapped = False; |
|
257 |
sentcompletion = True; |
sentcompletion = True; |
258 |
} |
} |
259 |
else |
else |
260 |
{ |
{ |
|
g_dsp_busy = 1; |
|
261 |
return; |
return; |
262 |
} |
} |
263 |
} |
} |
264 |
} |
} |
265 |
} |
} |
266 |
|
|
267 |
|
static struct audio_driver sun_driver = { |
268 |
|
.name = "sun", |
269 |
|
.description = "SUN/BSD output driver, default device: " DEFAULTDEVICE " or $AUDIODEV", |
270 |
|
|
271 |
|
.add_fds = sun_add_fds, |
272 |
|
.check_fds = sun_check_fds, |
273 |
|
|
274 |
|
.wave_out_open = sun_open, |
275 |
|
.wave_out_close = sun_close, |
276 |
|
.wave_out_format_supported = sun_format_supported, |
277 |
|
.wave_out_set_format = sun_set_format, |
278 |
|
.wave_out_volume = sun_volume, |
279 |
|
|
280 |
|
.need_byteswap_on_be = 1, |
281 |
|
.need_resampling = 0, |
282 |
|
}; |
283 |
|
|
284 |
|
struct audio_driver * |
285 |
|
sun_register(char *options) |
286 |
|
{ |
287 |
|
if (options) |
288 |
|
{ |
289 |
|
dsp_dev = xstrdup(options); |
290 |
|
} |
291 |
|
else |
292 |
|
{ |
293 |
|
dsp_dev = getenv("AUDIODEV"); |
294 |
|
|
295 |
|
if (dsp_dev == NULL) |
296 |
|
{ |
297 |
|
dsp_dev = xstrdup(DEFAULTDEVICE); |
298 |
|
} |
299 |
|
} |
300 |
|
|
301 |
|
return &sun_driver; |
302 |
|
} |