1 |
/* |
/* -*- c-basic-offset: 8 -*- |
2 |
rdesktop: A Remote Desktop Protocol client. |
rdesktop: A Remote Desktop Protocol client. |
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> |
28 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
29 |
#include <sys/audio.h> |
#include <sys/audioio.h> |
30 |
|
|
31 |
|
#if (defined(sun) && (defined(__svr4__) || defined(__SVR4))) |
32 |
#include <stropts.h> |
#include <stropts.h> |
33 |
|
#endif |
34 |
|
|
35 |
#define MAX_QUEUE 10 |
#define DEFAULTDEVICE "/dev/audio" |
36 |
|
|
37 |
int g_dsp_fd; |
static BOOL g_reopened; |
38 |
BOOL g_dsp_busy; |
static short g_samplewidth; |
39 |
static BOOL swapaudio; |
static char *dsp_dev; |
|
static short samplewidth; |
|
|
|
|
|
static struct audio_packet { |
|
|
struct stream s; |
|
|
uint16 tick; |
|
|
uint8 index; |
|
|
} packet_queue[MAX_QUEUE]; |
|
|
static unsigned int queue_hi, queue_lo; |
|
40 |
|
|
41 |
BOOL |
BOOL |
42 |
wave_out_open(void) |
sun_open(void) |
43 |
{ |
{ |
44 |
char *dsp_dev = "/dev/audio"; |
if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1) |
|
|
|
|
if ((g_dsp_fd = open(dsp_dev, O_WRONLY|O_NONBLOCK)) == -1) |
|
45 |
{ |
{ |
46 |
perror(dsp_dev); |
perror(dsp_dev); |
47 |
return False; |
return False; |
48 |
} |
} |
49 |
|
|
|
|
|
50 |
/* Non-blocking so that user interface is responsive */ |
/* Non-blocking so that user interface is responsive */ |
51 |
fcntl(g_dsp_fd, F_SETFL, fcntl(g_dsp_fd, F_GETFL)|O_NONBLOCK); |
fcntl(g_dsp_fd, F_SETFL, fcntl(g_dsp_fd, F_GETFL) | O_NONBLOCK); |
52 |
queue_lo = queue_hi = 0; |
|
53 |
|
g_reopened = True; |
54 |
|
|
55 |
return True; |
return True; |
56 |
} |
} |
57 |
|
|
58 |
void |
void |
59 |
wave_out_close(void) |
sun_close(void) |
60 |
{ |
{ |
61 |
ioctl(g_dsp_fd,I_FLUSH,FLUSHW); |
/* Ack all remaining packets */ |
62 |
|
while (!rdpsnd_queue_empty()) |
63 |
|
{ |
64 |
|
rdpsnd_send_completion(rdpsnd_queue_current_packet()->tick, |
65 |
|
rdpsnd_queue_current_packet()->index); |
66 |
|
rdpsnd_queue_next(); |
67 |
|
} |
68 |
|
|
69 |
|
#if defined I_FLUSH && defined FLUSHW |
70 |
|
/* Flush the audiobuffer */ |
71 |
|
ioctl(g_dsp_fd, I_FLUSH, FLUSHW); |
72 |
|
#endif |
73 |
|
#if defined AUDIO_FLUSH |
74 |
|
ioctl(g_dsp_fd, AUDIO_FLUSH, NULL); |
75 |
|
#endif |
76 |
close(g_dsp_fd); |
close(g_dsp_fd); |
77 |
} |
} |
78 |
|
|
79 |
BOOL |
BOOL |
80 |
wave_out_format_supported(WAVEFORMATEX *pwfx) |
sun_format_supported(WAVEFORMATEX * pwfx) |
81 |
{ |
{ |
82 |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
83 |
return False; |
return False; |
90 |
} |
} |
91 |
|
|
92 |
BOOL |
BOOL |
93 |
wave_out_set_format(WAVEFORMATEX *pwfx) |
sun_set_format(WAVEFORMATEX * pwfx) |
94 |
{ |
{ |
95 |
audio_info_t info; |
audio_info_t info; |
|
int test = 1; |
|
96 |
|
|
97 |
ioctl(g_dsp_fd, AUDIO_DRAIN, 0); |
ioctl(g_dsp_fd, AUDIO_DRAIN, 0); |
|
swapaudio = False; |
|
98 |
AUDIO_INITINFO(&info); |
AUDIO_INITINFO(&info); |
99 |
|
|
100 |
|
|
101 |
if (pwfx->wBitsPerSample == 8) |
if (pwfx->wBitsPerSample == 8) |
102 |
{ |
{ |
103 |
info.play.encoding = AUDIO_ENCODING_LINEAR8; |
info.play.encoding = AUDIO_ENCODING_LINEAR8; |
|
samplewidth=1; |
|
104 |
} |
} |
105 |
else if (pwfx->wBitsPerSample == 16) |
else if (pwfx->wBitsPerSample == 16) |
106 |
{ |
{ |
107 |
info.play.encoding = AUDIO_ENCODING_LINEAR; |
info.play.encoding = AUDIO_ENCODING_LINEAR; |
|
swapaudio = !(*(uint8 *) (&test)); |
|
|
samplewidth=2; |
|
108 |
} |
} |
109 |
|
|
110 |
if (pwfx->nChannels == 1 ) |
g_samplewidth = pwfx->wBitsPerSample / 8; |
111 |
{ |
|
112 |
info.play.channels = AUDIO_CHANNELS_MONO; |
if (pwfx->nChannels == 1) |
113 |
|
{ |
114 |
|
info.play.channels = 1; |
115 |
} |
} |
116 |
else if (pwfx->nChannels == 2 ) |
else if (pwfx->nChannels == 2) |
117 |
{ |
{ |
118 |
info.play.channels = AUDIO_CHANNELS_STEREO; |
info.play.channels = 2; |
119 |
samplewidth*=2; |
g_samplewidth *= 2; |
120 |
} |
} |
121 |
|
|
122 |
info.play.sample_rate = pwfx->nSamplesPerSec; |
info.play.sample_rate = pwfx->nSamplesPerSec; |
124 |
info.play.samples = 0; |
info.play.samples = 0; |
125 |
info.play.eof = 0; |
info.play.eof = 0; |
126 |
info.play.error = 0; |
info.play.error = 0; |
127 |
|
g_reopened = True; |
128 |
|
|
129 |
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
130 |
{ |
{ |
137 |
} |
} |
138 |
|
|
139 |
void |
void |
140 |
wave_out_write(STREAM s, uint16 tick, uint8 index) |
sun_volume(uint16 left, uint16 right) |
141 |
{ |
{ |
142 |
struct audio_packet *packet = &packet_queue[queue_hi]; |
audio_info_t info; |
143 |
unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE; |
uint balance; |
144 |
|
uint volume; |
145 |
|
|
146 |
|
AUDIO_INITINFO(&info); |
147 |
|
|
148 |
if (next_hi == queue_lo) |
volume = (left > right) ? left : right; |
149 |
|
|
150 |
|
if (volume / AUDIO_MID_BALANCE != 0) |
151 |
{ |
{ |
152 |
error("No space to queue audio packet\n"); |
balance = |
153 |
return; |
AUDIO_MID_BALANCE - (left / (volume / AUDIO_MID_BALANCE)) + |
154 |
|
(right / (volume / AUDIO_MID_BALANCE)); |
155 |
|
} |
156 |
|
else |
157 |
|
{ |
158 |
|
balance = AUDIO_MID_BALANCE; |
159 |
} |
} |
|
|
|
|
queue_hi = next_hi; |
|
|
|
|
|
packet->s = *s; |
|
|
packet->tick = tick; |
|
|
packet->index = index; |
|
160 |
|
|
161 |
/* we steal the data buffer from s, give it a new one */ |
info.play.gain = volume / (65536 / AUDIO_MAX_GAIN); |
162 |
s->data = malloc(s->size); |
info.play.balance = balance; |
163 |
|
|
164 |
if (!g_dsp_busy) |
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
165 |
wave_out_play(); |
{ |
166 |
|
perror("AUDIO_SETINFO"); |
167 |
|
return; |
168 |
|
} |
169 |
} |
} |
170 |
|
|
171 |
void |
void |
172 |
wave_out_play(void) |
sun_play(void) |
173 |
{ |
{ |
174 |
struct audio_packet *packet; |
struct audio_packet *packet; |
175 |
audio_info_t info; |
audio_info_t info; |
176 |
ssize_t len; |
ssize_t len; |
177 |
unsigned int i; |
unsigned int i; |
|
uint8 swap; |
|
178 |
STREAM out; |
STREAM out; |
|
static BOOL swapped = False; |
|
179 |
static BOOL sentcompletion = True; |
static BOOL sentcompletion = True; |
180 |
static int samplecnt; |
static uint32 samplecnt = 0; |
181 |
static int numsamples; |
static uint32 numsamples; |
182 |
|
|
183 |
while (1) |
while (1) |
184 |
{ |
{ |
185 |
if (queue_lo == queue_hi) |
if (g_reopened) |
186 |
|
{ |
187 |
|
/* Device was just (re)openend */ |
188 |
|
samplecnt = 0; |
189 |
|
sentcompletion = True; |
190 |
|
g_reopened = False; |
191 |
|
} |
192 |
|
|
193 |
|
if (rdpsnd_queue_empty()) |
194 |
{ |
{ |
195 |
g_dsp_busy = 0; |
g_dsp_busy = 0; |
196 |
return; |
return; |
197 |
} |
} |
198 |
|
|
199 |
packet = &packet_queue[queue_lo]; |
packet = rdpsnd_queue_current_packet(); |
200 |
out = &packet->s; |
out = &packet->s; |
201 |
|
|
202 |
if ( swapaudio && ! swapped ) |
if (sentcompletion) |
203 |
{ |
{ |
|
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; |
|
|
} |
|
|
} |
|
|
|
|
|
if ( sentcompletion ) |
|
|
{ |
|
|
if (ioctl(g_dsp_fd, AUDIO_GETINFO, &info) == -1) |
|
|
{ |
|
|
perror("AUDIO_GETINFO"); |
|
|
return; |
|
|
} |
|
|
samplecnt=info.play.samples; |
|
204 |
sentcompletion = False; |
sentcompletion = False; |
205 |
numsamples = (out->end-out->p)/samplewidth; |
numsamples = (out->end - out->p) / g_samplewidth; |
206 |
} |
} |
207 |
|
|
208 |
len=0; |
len = 0; |
209 |
|
|
210 |
if ( out->end - out->p != 0 ) |
if (out->end != out->p) |
211 |
{ |
{ |
212 |
len = write(g_dsp_fd, out->p, out->end - out->p); |
len = write(g_dsp_fd, out->p, out->end - out->p); |
213 |
if (len == -1) |
if (len == -1) |
227 |
perror("AUDIO_GETINFO"); |
perror("AUDIO_GETINFO"); |
228 |
return; |
return; |
229 |
} |
} |
230 |
if ( info.play.samples >= samplecnt+(numsamples)*0.9 ) |
|
231 |
|
/* Ack the packet, if we have played at least 70% */ |
232 |
|
if (info.play.samples >= samplecnt + ((numsamples * 7) / 10)) |
233 |
{ |
{ |
234 |
rdpsnd_send_completion(packet->tick, packet->index); |
samplecnt += numsamples; |
235 |
free(out->data); |
/* We need to add 50 to tell windows that time has passed while |
236 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
* playing this packet */ |
237 |
swapped = False; |
rdpsnd_queue_next(50); |
238 |
sentcompletion = True; |
sentcompletion = True; |
239 |
} |
} |
240 |
else |
else |
245 |
} |
} |
246 |
} |
} |
247 |
} |
} |
248 |
|
|
249 |
|
struct audio_driver * |
250 |
|
sun_register(char *options) |
251 |
|
{ |
252 |
|
static struct audio_driver sun_driver; |
253 |
|
|
254 |
|
sun_driver.wave_out_open = sun_open; |
255 |
|
sun_driver.wave_out_close = sun_close; |
256 |
|
sun_driver.wave_out_format_supported = sun_format_supported; |
257 |
|
sun_driver.wave_out_set_format = sun_set_format; |
258 |
|
sun_driver.wave_out_volume = sun_volume; |
259 |
|
sun_driver.wave_out_play = sun_play; |
260 |
|
sun_driver.name = xstrdup("sun"); |
261 |
|
sun_driver.description = |
262 |
|
xstrdup("SUN/BSD output driver, default device: " DEFAULTDEVICE " or $AUDIODEV"); |
263 |
|
sun_driver.need_byteswap_on_be = 1; |
264 |
|
sun_driver.need_resampling = 0; |
265 |
|
sun_driver.next = NULL; |
266 |
|
|
267 |
|
if (options) |
268 |
|
{ |
269 |
|
dsp_dev = xstrdup(options); |
270 |
|
} |
271 |
|
else |
272 |
|
{ |
273 |
|
dsp_dev = getenv("AUDIODEV"); |
274 |
|
|
275 |
|
if (dsp_dev == NULL) |
276 |
|
{ |
277 |
|
dsp_dev = xstrdup(DEFAULTDEVICE); |
278 |
|
} |
279 |
|
} |
280 |
|
|
281 |
|
return &sun_driver; |
282 |
|
} |