1 |
/* |
/* |
2 |
rdesktop: A Remote Desktop Protocol client. |
rdesktop: A Remote Desktop Protocol client. |
3 |
Sound Channel Process Functions - Open Sound System |
Sound Channel Process Functions - Open Sound System |
4 |
Copyright (C) Matthew Chapman 2003 |
Copyright (C) Matthew Chapman 2003 |
23 |
#include <unistd.h> |
#include <unistd.h> |
24 |
#include <fcntl.h> |
#include <fcntl.h> |
25 |
#include <errno.h> |
#include <errno.h> |
26 |
|
#include <sys/time.h> |
27 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
28 |
#include <sys/soundcard.h> |
#include <sys/soundcard.h> |
29 |
|
|
31 |
|
|
32 |
int g_dsp_fd; |
int g_dsp_fd; |
33 |
BOOL g_dsp_busy = False; |
BOOL g_dsp_busy = False; |
34 |
|
static int g_snd_rate; |
35 |
|
static short g_samplewidth; |
36 |
|
static BOOL g_driver_broken = False; |
37 |
|
|
38 |
static struct audio_packet |
static struct audio_packet |
39 |
{ |
{ |
46 |
BOOL |
BOOL |
47 |
wave_out_open(void) |
wave_out_open(void) |
48 |
{ |
{ |
49 |
char *dsp_dev = "/dev/dsp"; |
char *dsp_dev = getenv("AUDIODEV"); |
50 |
|
|
51 |
|
if (dsp_dev == NULL) |
52 |
|
{ |
53 |
|
dsp_dev = strdup("/dev/dsp"); |
54 |
|
} |
55 |
|
|
56 |
if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1) |
if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1) |
57 |
{ |
{ |
86 |
BOOL |
BOOL |
87 |
wave_out_set_format(WAVEFORMATEX * pwfx) |
wave_out_set_format(WAVEFORMATEX * pwfx) |
88 |
{ |
{ |
89 |
int speed, channels, format; |
int stereo, format, fragments; |
90 |
|
|
91 |
ioctl(g_dsp_fd, SNDCTL_DSP_RESET, NULL); |
ioctl(g_dsp_fd, SNDCTL_DSP_RESET, NULL); |
92 |
ioctl(g_dsp_fd, SNDCTL_DSP_SYNC, NULL); |
ioctl(g_dsp_fd, SNDCTL_DSP_SYNC, NULL); |
96 |
else if (pwfx->wBitsPerSample == 16) |
else if (pwfx->wBitsPerSample == 16) |
97 |
format = AFMT_S16_LE; |
format = AFMT_S16_LE; |
98 |
|
|
99 |
|
g_samplewidth = pwfx->wBitsPerSample / 8; |
100 |
|
|
101 |
if (ioctl(g_dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1) |
if (ioctl(g_dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1) |
102 |
{ |
{ |
103 |
perror("SNDCTL_DSP_SETFMT"); |
perror("SNDCTL_DSP_SETFMT"); |
105 |
return False; |
return False; |
106 |
} |
} |
107 |
|
|
108 |
channels = pwfx->nChannels; |
if (pwfx->nChannels == 2) |
109 |
if (ioctl(g_dsp_fd, SNDCTL_DSP_CHANNELS, &channels) == -1) |
{ |
110 |
|
stereo = 1; |
111 |
|
g_samplewidth *= 2; |
112 |
|
} |
113 |
|
else |
114 |
|
{ |
115 |
|
stereo = 0; |
116 |
|
} |
117 |
|
|
118 |
|
if (ioctl(g_dsp_fd, SNDCTL_DSP_STEREO, &stereo) == -1) |
119 |
{ |
{ |
120 |
perror("SNDCTL_DSP_CHANNELS"); |
perror("SNDCTL_DSP_CHANNELS"); |
121 |
close(g_dsp_fd); |
close(g_dsp_fd); |
122 |
return False; |
return False; |
123 |
} |
} |
124 |
|
|
125 |
speed = pwfx->nSamplesPerSec; |
g_snd_rate = pwfx->nSamplesPerSec; |
126 |
if (ioctl(g_dsp_fd, SNDCTL_DSP_SPEED, &speed) == -1) |
if (ioctl(g_dsp_fd, SNDCTL_DSP_SPEED, &g_snd_rate) == -1) |
127 |
{ |
{ |
128 |
perror("SNDCTL_DSP_SPEED"); |
perror("SNDCTL_DSP_SPEED"); |
129 |
close(g_dsp_fd); |
close(g_dsp_fd); |
130 |
return False; |
return False; |
131 |
} |
} |
132 |
|
|
133 |
|
/* try to get 7 fragments of 2^12 bytes size */ |
134 |
|
fragments = (7 << 16) + 12; |
135 |
|
ioctl(g_dsp_fd, SNDCTL_DSP_SETFRAGMENT, &fragments); |
136 |
|
|
137 |
|
if (!g_driver_broken) |
138 |
|
{ |
139 |
|
audio_buf_info info; |
140 |
|
|
141 |
|
memset(&info,0,sizeof(info)); |
142 |
|
if (ioctl(g_dsp_fd, SNDCTL_DSP_GETOSPACE, &info) == -1) |
143 |
|
{ |
144 |
|
perror("SNDCTL_DSP_GETOSPACE"); |
145 |
|
close(g_dsp_fd); |
146 |
|
return False; |
147 |
|
} |
148 |
|
|
149 |
|
if (info.fragments == 0 || info.fragstotal == 0 || info.fragsize == 0) |
150 |
|
{ |
151 |
|
fprintf(stderr, |
152 |
|
"Broken OSS-driver detected: fragments: %d, fragstotal: %d, fragsize: %d\n", |
153 |
|
info.fragments, info.fragstotal, info.fragsize); |
154 |
|
g_driver_broken = True; |
155 |
|
} |
156 |
|
} |
157 |
|
|
158 |
return True; |
return True; |
159 |
} |
} |
160 |
|
|
170 |
|
|
171 |
if (use_dev_mixer) |
if (use_dev_mixer) |
172 |
{ |
{ |
173 |
if ((fd_mix = open( "/dev/mixer", O_RDWR|O_NONBLOCK )) == -1 ) |
if ((fd_mix = open("/dev/mixer", O_RDWR | O_NONBLOCK)) == -1) |
174 |
{ |
{ |
175 |
perror("open /dev/mixer"); |
perror("open /dev/mixer"); |
176 |
return; |
return; |
213 |
packet->s.p += 4; |
packet->s.p += 4; |
214 |
|
|
215 |
/* we steal the data buffer from s, give it a new one */ |
/* we steal the data buffer from s, give it a new one */ |
216 |
s->data = malloc(s->size); |
s->data = (uint8 *) malloc(s->size); |
217 |
|
|
218 |
if (!g_dsp_busy) |
if (!g_dsp_busy) |
219 |
wave_out_play(); |
wave_out_play(); |
225 |
struct audio_packet *packet; |
struct audio_packet *packet; |
226 |
ssize_t len; |
ssize_t len; |
227 |
STREAM out; |
STREAM out; |
228 |
|
static long startedat_us; |
229 |
|
static long startedat_s; |
230 |
|
static BOOL started = False; |
231 |
|
struct timeval tv; |
232 |
|
audio_buf_info info; |
233 |
|
|
234 |
while (1) |
while (1) |
235 |
{ |
{ |
242 |
packet = &packet_queue[queue_lo]; |
packet = &packet_queue[queue_lo]; |
243 |
out = &packet->s; |
out = &packet->s; |
244 |
|
|
245 |
len = write(g_dsp_fd, out->p, out->end - out->p); |
if (!started) |
246 |
|
{ |
247 |
|
gettimeofday(&tv, NULL); |
248 |
|
startedat_us = tv.tv_usec; |
249 |
|
startedat_s = tv.tv_sec; |
250 |
|
started = True; |
251 |
|
} |
252 |
|
|
253 |
|
len = out->end - out->p; |
254 |
|
|
255 |
|
if (!g_driver_broken) |
256 |
|
{ |
257 |
|
memset(&info,0,sizeof(info)); |
258 |
|
if (ioctl(g_dsp_fd, SNDCTL_DSP_GETOSPACE, &info) == -1) |
259 |
|
{ |
260 |
|
perror("SNDCTL_DSP_GETOSPACE"); |
261 |
|
return; |
262 |
|
} |
263 |
|
|
264 |
|
if (info.fragments == 0) |
265 |
|
{ |
266 |
|
g_dsp_busy = 1; |
267 |
|
return; |
268 |
|
} |
269 |
|
|
270 |
|
if (info.fragments * info.fragsize < len |
271 |
|
&& info.fragments * info.fragsize > 0) |
272 |
|
{ |
273 |
|
len = info.fragments * info.fragsize; |
274 |
|
} |
275 |
|
} |
276 |
|
|
277 |
|
|
278 |
|
len = write(g_dsp_fd, out->p, len); |
279 |
if (len == -1) |
if (len == -1) |
280 |
{ |
{ |
281 |
if (errno != EWOULDBLOCK) |
if (errno != EWOULDBLOCK) |
287 |
out->p += len; |
out->p += len; |
288 |
if (out->p == out->end) |
if (out->p == out->end) |
289 |
{ |
{ |
290 |
rdpsnd_send_completion(packet->tick, packet->index); |
long long duration; |
291 |
free(out->data); |
long elapsed; |
292 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
|
293 |
|
gettimeofday(&tv, NULL); |
294 |
|
duration = (out->size * (1000000 / (g_samplewidth * g_snd_rate))); |
295 |
|
elapsed = (tv.tv_sec - startedat_s) * 1000000 + (tv.tv_usec - startedat_us); |
296 |
|
|
297 |
|
if (elapsed >= (duration * 85) / 100) |
298 |
|
{ |
299 |
|
rdpsnd_send_completion(packet->tick, packet->index); |
300 |
|
free(out->data); |
301 |
|
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
302 |
|
started = False; |
303 |
|
} |
304 |
|
else |
305 |
|
{ |
306 |
|
g_dsp_busy = 1; |
307 |
|
return; |
308 |
|
} |
309 |
} |
} |
310 |
} |
} |
|
|
|
311 |
} |
} |