1 |
/* |
/* -*- c-basic-offset: 8 -*- |
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 |
19 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
*/ |
*/ |
21 |
|
|
22 |
|
/* |
23 |
|
This is a workaround for Esound bug 312665. |
24 |
|
FIXME: Remove this when Esound is fixed. |
25 |
|
*/ |
26 |
|
#ifdef _FILE_OFFSET_BITS |
27 |
|
#undef _FILE_OFFSET_BITS |
28 |
|
#endif |
29 |
|
|
30 |
#include "rdesktop.h" |
#include "rdesktop.h" |
31 |
|
#include "rdpsnd.h" |
32 |
#include <unistd.h> |
#include <unistd.h> |
33 |
#include <fcntl.h> |
#include <fcntl.h> |
34 |
#include <errno.h> |
#include <errno.h> |
36 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
37 |
#include <sys/soundcard.h> |
#include <sys/soundcard.h> |
38 |
|
|
39 |
#define MAX_QUEUE 10 |
#define DEFAULTDEVICE "/dev/dsp" |
40 |
|
#define MAX_LEN 512 |
41 |
|
|
42 |
int g_dsp_fd; |
static int snd_rate; |
43 |
BOOL g_dsp_busy = False; |
static short samplewidth; |
|
static int g_snd_rate; |
|
|
static short g_samplewidth; |
|
|
|
|
|
static struct audio_packet |
|
|
{ |
|
|
struct stream s; |
|
|
uint16 tick; |
|
|
uint8 index; |
|
|
} packet_queue[MAX_QUEUE]; |
|
|
static unsigned int queue_hi, queue_lo; |
|
44 |
|
|
45 |
BOOL |
BOOL |
46 |
wave_out_open(void) |
wave_out_open(void) |
47 |
{ |
{ |
48 |
char *dsp_dev = "/dev/dsp"; |
char *dsp_dev = getenv("AUDIODEV"); |
49 |
|
|
50 |
if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1) |
if (dsp_dev == NULL) |
51 |
|
{ |
52 |
|
dsp_dev = xstrdup(DEFAULTDEVICE); |
53 |
|
} |
54 |
|
|
55 |
|
if ((g_dsp_fd = open(dsp_dev, O_WRONLY)) == -1) |
56 |
{ |
{ |
57 |
perror(dsp_dev); |
perror(dsp_dev); |
58 |
return False; |
return False; |
59 |
} |
} |
60 |
|
|
|
/* Non-blocking so that user interface is responsive */ |
|
|
fcntl(g_dsp_fd, F_SETFL, fcntl(g_dsp_fd, F_GETFL) | O_NONBLOCK); |
|
61 |
return True; |
return True; |
62 |
} |
} |
63 |
|
|
83 |
BOOL |
BOOL |
84 |
wave_out_set_format(WAVEFORMATEX * pwfx) |
wave_out_set_format(WAVEFORMATEX * pwfx) |
85 |
{ |
{ |
86 |
int channels, format; |
int stereo, format, fragments; |
87 |
|
static BOOL driver_broken = False; |
88 |
|
|
89 |
ioctl(g_dsp_fd, SNDCTL_DSP_RESET, NULL); |
ioctl(g_dsp_fd, SNDCTL_DSP_RESET, NULL); |
90 |
ioctl(g_dsp_fd, SNDCTL_DSP_SYNC, NULL); |
ioctl(g_dsp_fd, SNDCTL_DSP_SYNC, NULL); |
94 |
else if (pwfx->wBitsPerSample == 16) |
else if (pwfx->wBitsPerSample == 16) |
95 |
format = AFMT_S16_LE; |
format = AFMT_S16_LE; |
96 |
|
|
97 |
g_samplewidth = pwfx->wBitsPerSample / 8; |
samplewidth = pwfx->wBitsPerSample / 8; |
98 |
|
|
99 |
if (ioctl(g_dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1) |
if (ioctl(g_dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1) |
100 |
{ |
{ |
103 |
return False; |
return False; |
104 |
} |
} |
105 |
|
|
106 |
channels = pwfx->nChannels; |
if (pwfx->nChannels == 2) |
|
if (ioctl(g_dsp_fd, SNDCTL_DSP_CHANNELS, &channels) == -1) |
|
107 |
{ |
{ |
108 |
perror("SNDCTL_DSP_CHANNELS"); |
stereo = 1; |
109 |
close(g_dsp_fd); |
samplewidth *= 2; |
110 |
return False; |
} |
111 |
|
else |
112 |
|
{ |
113 |
|
stereo = 0; |
114 |
} |
} |
115 |
|
|
116 |
if (channels == 2) |
if (ioctl(g_dsp_fd, SNDCTL_DSP_STEREO, &stereo) == -1) |
117 |
{ |
{ |
118 |
g_samplewidth *= 2; |
perror("SNDCTL_DSP_CHANNELS"); |
119 |
|
close(g_dsp_fd); |
120 |
|
return False; |
121 |
} |
} |
122 |
|
|
123 |
g_snd_rate = pwfx->nSamplesPerSec; |
snd_rate = pwfx->nSamplesPerSec; |
124 |
if (ioctl(g_dsp_fd, SNDCTL_DSP_SPEED, &g_snd_rate) == -1) |
if (ioctl(g_dsp_fd, SNDCTL_DSP_SPEED, &snd_rate) == -1) |
125 |
{ |
{ |
126 |
perror("SNDCTL_DSP_SPEED"); |
perror("SNDCTL_DSP_SPEED"); |
127 |
close(g_dsp_fd); |
close(g_dsp_fd); |
128 |
return False; |
return False; |
129 |
} |
} |
130 |
|
|
131 |
|
/* try to get 12 fragments of 2^12 bytes size */ |
132 |
|
fragments = (12 << 16) + 12; |
133 |
|
ioctl(g_dsp_fd, SNDCTL_DSP_SETFRAGMENT, &fragments); |
134 |
|
|
135 |
|
if (!driver_broken) |
136 |
|
{ |
137 |
|
audio_buf_info info; |
138 |
|
|
139 |
|
memset(&info, 0, sizeof(info)); |
140 |
|
if (ioctl(g_dsp_fd, SNDCTL_DSP_GETOSPACE, &info) == -1) |
141 |
|
{ |
142 |
|
perror("SNDCTL_DSP_GETOSPACE"); |
143 |
|
close(g_dsp_fd); |
144 |
|
return False; |
145 |
|
} |
146 |
|
|
147 |
|
if (info.fragments == 0 || info.fragstotal == 0 || info.fragsize == 0) |
148 |
|
{ |
149 |
|
fprintf(stderr, |
150 |
|
"Broken OSS-driver detected: fragments: %d, fragstotal: %d, fragsize: %d\n", |
151 |
|
info.fragments, info.fragstotal, info.fragsize); |
152 |
|
driver_broken = True; |
153 |
|
} |
154 |
|
} |
155 |
|
|
156 |
return True; |
return True; |
157 |
} |
} |
158 |
|
|
192 |
} |
} |
193 |
|
|
194 |
void |
void |
|
wave_out_write(STREAM s, uint16 tick, uint8 index) |
|
|
{ |
|
|
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 |
|
195 |
wave_out_play(void) |
wave_out_play(void) |
196 |
{ |
{ |
197 |
struct audio_packet *packet; |
struct audio_packet *packet; |
202 |
static BOOL started = False; |
static BOOL started = False; |
203 |
struct timeval tv; |
struct timeval tv; |
204 |
|
|
205 |
while (1) |
if (rdpsnd_queue_empty()) |
206 |
{ |
{ |
207 |
if (queue_lo == queue_hi) |
g_dsp_busy = 0; |
208 |
{ |
return; |
209 |
g_dsp_busy = 0; |
} |
|
return; |
|
|
} |
|
210 |
|
|
211 |
packet = &packet_queue[queue_lo]; |
packet = rdpsnd_queue_current_packet(); |
212 |
out = &packet->s; |
out = &packet->s; |
213 |
|
|
214 |
if (!started) |
if (!started) |
215 |
|
{ |
216 |
|
gettimeofday(&tv, NULL); |
217 |
|
startedat_us = tv.tv_usec; |
218 |
|
startedat_s = tv.tv_sec; |
219 |
|
started = True; |
220 |
|
} |
221 |
|
|
222 |
|
len = out->end - out->p; |
223 |
|
|
224 |
|
len = write(g_dsp_fd, out->p, (len > MAX_LEN) ? MAX_LEN : len); |
225 |
|
if (len == -1) |
226 |
|
{ |
227 |
|
if (errno != EWOULDBLOCK) |
228 |
|
perror("write audio"); |
229 |
|
g_dsp_busy = 1; |
230 |
|
return; |
231 |
|
} |
232 |
|
|
233 |
|
out->p += len; |
234 |
|
if (out->p == out->end) |
235 |
|
{ |
236 |
|
long long duration; |
237 |
|
long elapsed; |
238 |
|
|
239 |
|
gettimeofday(&tv, NULL); |
240 |
|
duration = (out->size * (1000000 / (samplewidth * snd_rate))); |
241 |
|
elapsed = (tv.tv_sec - startedat_s) * 1000000 + (tv.tv_usec - startedat_us); |
242 |
|
|
243 |
|
if (elapsed >= (duration * 85) / 100) |
244 |
{ |
{ |
245 |
gettimeofday(&tv, NULL); |
/* We need to add 50 to tell windows that time has passed while |
246 |
startedat_us = tv.tv_usec; |
* playing this packet */ |
247 |
startedat_s = tv.tv_sec; |
rdpsnd_send_completion(packet->tick + 50, packet->index); |
248 |
started = True; |
rdpsnd_queue_next(); |
249 |
|
started = False; |
250 |
} |
} |
251 |
|
else |
|
len = write(g_dsp_fd, out->p, out->end - out->p); |
|
|
if (len == -1) |
|
252 |
{ |
{ |
|
if (errno != EWOULDBLOCK) |
|
|
perror("write audio"); |
|
253 |
g_dsp_busy = 1; |
g_dsp_busy = 1; |
254 |
return; |
return; |
255 |
} |
} |
|
|
|
|
out->p += len; |
|
|
if (out->p == out->end) |
|
|
{ |
|
|
long long duration; |
|
|
long elapsed; |
|
|
|
|
|
gettimeofday(&tv, NULL); |
|
|
duration = (out->size * (1000000 / (g_samplewidth * g_snd_rate))); |
|
|
elapsed = (tv.tv_sec - startedat_s) * 1000000 + (tv.tv_usec - startedat_us); |
|
|
|
|
|
if (elapsed >= (duration * 7) / 10) |
|
|
{ |
|
|
rdpsnd_send_completion(packet->tick, packet->index); |
|
|
free(out->data); |
|
|
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
|
|
started = False; |
|
|
} |
|
|
else |
|
|
{ |
|
|
g_dsp_busy = 1; |
|
|
return; |
|
|
} |
|
|
} |
|
256 |
} |
} |
257 |
|
g_dsp_busy = 1; |
258 |
|
return; |
259 |
} |
} |