1 |
/* |
2 |
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 |
Copyright (C) Michael Gernoth mike@zerfleddert.de 2003 |
7 |
|
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 |
#include <unistd.h> |
25 |
#include <fcntl.h> |
26 |
#include <errno.h> |
27 |
#include <sys/ioctl.h> |
28 |
#include <sys/audioio.h> |
29 |
#include <stropts.h> |
30 |
|
31 |
#define MAX_QUEUE 10 |
32 |
|
33 |
int g_dsp_fd; |
34 |
BOOL g_dsp_busy; |
35 |
static BOOL reopened; |
36 |
static BOOL swapaudio; |
37 |
static short samplewidth; |
38 |
|
39 |
static struct audio_packet { |
40 |
struct stream s; |
41 |
uint16 tick; |
42 |
uint8 index; |
43 |
} packet_queue[MAX_QUEUE]; |
44 |
static unsigned int queue_hi, queue_lo; |
45 |
|
46 |
BOOL |
47 |
wave_out_open(void) |
48 |
{ |
49 |
char *dsp_dev = getenv("AUDIODEV"); |
50 |
|
51 |
if ( dsp_dev == NULL ) |
52 |
{ |
53 |
dsp_dev="/dev/audio"; |
54 |
} |
55 |
|
56 |
if ((g_dsp_fd = open(dsp_dev, O_WRONLY|O_NONBLOCK)) == -1) |
57 |
{ |
58 |
perror(dsp_dev); |
59 |
return False; |
60 |
} |
61 |
|
62 |
/* Non-blocking so that user interface is responsive */ |
63 |
fcntl(g_dsp_fd, F_SETFL, fcntl(g_dsp_fd, F_GETFL)|O_NONBLOCK); |
64 |
|
65 |
queue_lo = queue_hi = 0; |
66 |
reopened = True; |
67 |
|
68 |
return True; |
69 |
} |
70 |
|
71 |
void |
72 |
wave_out_close(void) |
73 |
{ |
74 |
/* Ack all remaining packets */ |
75 |
while ( queue_lo != queue_hi ) |
76 |
{ |
77 |
rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index); |
78 |
free(packet_queue[queue_lo].s.data); |
79 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
80 |
} |
81 |
|
82 |
/* Flush the audiobuffer */ |
83 |
ioctl(g_dsp_fd,I_FLUSH,FLUSHW); |
84 |
close(g_dsp_fd); |
85 |
} |
86 |
|
87 |
BOOL |
88 |
wave_out_format_supported(WAVEFORMATEX *pwfx) |
89 |
{ |
90 |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
91 |
return False; |
92 |
if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2)) |
93 |
return False; |
94 |
if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16)) |
95 |
return False; |
96 |
|
97 |
return True; |
98 |
} |
99 |
|
100 |
BOOL |
101 |
wave_out_set_format(WAVEFORMATEX *pwfx) |
102 |
{ |
103 |
audio_info_t info; |
104 |
int test = 1; |
105 |
|
106 |
ioctl(g_dsp_fd, AUDIO_DRAIN, 0); |
107 |
swapaudio = False; |
108 |
AUDIO_INITINFO(&info); |
109 |
|
110 |
|
111 |
if (pwfx->wBitsPerSample == 8) |
112 |
{ |
113 |
info.play.encoding = AUDIO_ENCODING_LINEAR8; |
114 |
samplewidth = 1; |
115 |
} |
116 |
else if (pwfx->wBitsPerSample == 16) |
117 |
{ |
118 |
info.play.encoding = AUDIO_ENCODING_LINEAR; |
119 |
samplewidth = 2; |
120 |
/* Do we need to swap the 16bit values? (Are we BigEndian) */ |
121 |
swapaudio = !(*(uint8 *) (&test)); |
122 |
} |
123 |
|
124 |
if (pwfx->nChannels == 1 ) |
125 |
{ |
126 |
info.play.channels = AUDIO_CHANNELS_MONO; |
127 |
} |
128 |
else if (pwfx->nChannels == 2 ) |
129 |
{ |
130 |
info.play.channels = AUDIO_CHANNELS_STEREO; |
131 |
samplewidth *= 2; |
132 |
} |
133 |
|
134 |
info.play.sample_rate = pwfx->nSamplesPerSec; |
135 |
info.play.precision = pwfx->wBitsPerSample; |
136 |
info.play.samples = 0; |
137 |
info.play.eof = 0; |
138 |
info.play.error = 0; |
139 |
|
140 |
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
141 |
{ |
142 |
perror("AUDIO_SETINFO"); |
143 |
close(g_dsp_fd); |
144 |
return False; |
145 |
} |
146 |
|
147 |
return True; |
148 |
} |
149 |
|
150 |
void |
151 |
wave_out_write(STREAM s, uint16 tick, uint8 index) |
152 |
{ |
153 |
struct audio_packet *packet = &packet_queue[queue_hi]; |
154 |
unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE; |
155 |
|
156 |
if (next_hi == queue_lo) |
157 |
{ |
158 |
error("No space to queue audio packet\n"); |
159 |
return; |
160 |
} |
161 |
|
162 |
queue_hi = next_hi; |
163 |
|
164 |
packet->s = *s; |
165 |
packet->tick = tick; |
166 |
packet->index = index; |
167 |
|
168 |
/* we steal the data buffer from s, give it a new one */ |
169 |
s->data = malloc(s->size); |
170 |
|
171 |
if (!g_dsp_busy) |
172 |
wave_out_play(); |
173 |
} |
174 |
|
175 |
void |
176 |
wave_out_play(void) |
177 |
{ |
178 |
struct audio_packet *packet; |
179 |
audio_info_t info; |
180 |
ssize_t len; |
181 |
unsigned int i; |
182 |
uint8 swap; |
183 |
STREAM out; |
184 |
static BOOL swapped = False; |
185 |
static BOOL sentcompletion = True; |
186 |
static uint32 samplecnt = 0; |
187 |
static uint32 numsamples; |
188 |
|
189 |
while (1) |
190 |
{ |
191 |
if ( reopened ) |
192 |
{ |
193 |
/* Device was just (re)openend */ |
194 |
samplecnt = 0; |
195 |
swapped = False; |
196 |
sentcompletion = True; |
197 |
reopened = False; |
198 |
} |
199 |
|
200 |
if (queue_lo == queue_hi) |
201 |
{ |
202 |
g_dsp_busy = 0; |
203 |
return; |
204 |
} |
205 |
|
206 |
packet = &packet_queue[queue_lo]; |
207 |
out = &packet->s; |
208 |
|
209 |
/* Swap the current packet, but only once */ |
210 |
if ( swapaudio && ! swapped ) |
211 |
{ |
212 |
for ( i = 0; i < out->end - out->p; i+=2 ) |
213 |
{ |
214 |
swap = *(out->p + i); |
215 |
*(out->p + i ) = *(out->p + i + 1); |
216 |
*(out->p + i + 1) = swap; |
217 |
swapped = True; |
218 |
} |
219 |
} |
220 |
|
221 |
if ( sentcompletion ) |
222 |
{ |
223 |
sentcompletion = False; |
224 |
numsamples = (out->end - out->p)/samplewidth; |
225 |
} |
226 |
|
227 |
len=0; |
228 |
|
229 |
if ( out->end != out->p ) |
230 |
{ |
231 |
len = write(g_dsp_fd, out->p, out->end - out->p); |
232 |
if (len == -1) |
233 |
{ |
234 |
if (errno != EWOULDBLOCK) |
235 |
perror("write audio"); |
236 |
g_dsp_busy = 1; |
237 |
return; |
238 |
} |
239 |
} |
240 |
|
241 |
out->p += len; |
242 |
if (out->p == out->end) |
243 |
{ |
244 |
if (ioctl(g_dsp_fd, AUDIO_GETINFO, &info) == -1) |
245 |
{ |
246 |
perror("AUDIO_GETINFO"); |
247 |
return; |
248 |
} |
249 |
|
250 |
/* Ack the packet, if we have played at least 70% */ |
251 |
if ( info.play.samples >= samplecnt+((numsamples*7)/10) ) |
252 |
{ |
253 |
samplecnt += numsamples; |
254 |
rdpsnd_send_completion(packet->tick, packet->index); |
255 |
free(out->data); |
256 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
257 |
swapped = False; |
258 |
sentcompletion = True; |
259 |
} |
260 |
else |
261 |
{ |
262 |
g_dsp_busy = 1; |
263 |
return; |
264 |
} |
265 |
} |
266 |
} |
267 |
} |