1 |
/* |
/* -*- c-basic-offset: 8 -*- |
2 |
rdesktop: A Remote Desktop Protocol client. |
rdesktop: A Remote Desktop Protocol client. |
3 |
Sound Channel Process Functions - libao-driver |
Sound Channel Process Functions - libao-driver |
4 |
Copyright (C) Matthew Chapman 2003 |
Copyright (C) Matthew Chapman 2003 |
25 |
#include <fcntl.h> |
#include <fcntl.h> |
26 |
#include <errno.h> |
#include <errno.h> |
27 |
#include <ao/ao.h> |
#include <ao/ao.h> |
28 |
|
#include <sys/time.h> |
29 |
|
|
30 |
#define MAX_QUEUE 10 |
#define MAX_QUEUE 10 |
31 |
#define WAVEOUTBUF 32 |
#define WAVEOUTBUF 64 |
32 |
|
|
33 |
int g_dsp_fd; |
int g_dsp_fd; |
34 |
ao_device *o_device = NULL; |
ao_device *o_device = NULL; |
35 |
int default_driver; |
int default_driver; |
36 |
int g_samplerate; |
int g_samplerate; |
37 |
|
int g_channels; |
38 |
BOOL g_dsp_busy = False; |
BOOL g_dsp_busy = False; |
39 |
|
static BOOL g_reopened; |
40 |
static short g_samplewidth; |
static short g_samplewidth; |
41 |
|
|
42 |
static struct audio_packet |
static struct audio_packet |
57 |
|
|
58 |
format.bits = 16; |
format.bits = 16; |
59 |
format.channels = 2; |
format.channels = 2; |
60 |
|
g_channels = 2; |
61 |
format.rate = 44100; |
format.rate = 44100; |
62 |
g_samplerate = 44100; |
g_samplerate = 44100; |
63 |
format.byte_format = AO_FMT_LITTLE; |
format.byte_format = AO_FMT_LITTLE; |
71 |
g_dsp_fd = 0; |
g_dsp_fd = 0; |
72 |
queue_lo = queue_hi = 0; |
queue_lo = queue_hi = 0; |
73 |
|
|
74 |
|
g_reopened = True; |
75 |
|
|
76 |
return True; |
return True; |
77 |
} |
} |
78 |
|
|
117 |
|
|
118 |
format.bits = pwfx->wBitsPerSample; |
format.bits = pwfx->wBitsPerSample; |
119 |
format.channels = pwfx->nChannels; |
format.channels = pwfx->nChannels; |
120 |
|
g_channels = pwfx->nChannels; |
121 |
format.rate = 44100; |
format.rate = 44100; |
122 |
g_samplerate = pwfx->nSamplesPerSec; |
g_samplerate = pwfx->nSamplesPerSec; |
123 |
format.byte_format = AO_FMT_LITTLE; |
format.byte_format = AO_FMT_LITTLE; |
133 |
return False; |
return False; |
134 |
} |
} |
135 |
|
|
136 |
|
g_reopened = True; |
137 |
|
|
138 |
return True; |
return True; |
139 |
} |
} |
177 |
STREAM out; |
STREAM out; |
178 |
unsigned char outbuf[WAVEOUTBUF]; |
unsigned char outbuf[WAVEOUTBUF]; |
179 |
int offset, len, i; |
int offset, len, i; |
180 |
|
static long prev_s, prev_us; |
181 |
|
unsigned int duration; |
182 |
|
struct timeval tv; |
183 |
|
int next_tick; |
184 |
|
|
185 |
|
if (g_reopened) |
186 |
|
{ |
187 |
|
g_reopened = False; |
188 |
|
gettimeofday(&tv, NULL); |
189 |
|
prev_s = tv.tv_sec; |
190 |
|
prev_us = tv.tv_usec; |
191 |
|
} |
192 |
|
|
193 |
if (queue_lo == queue_hi) |
if (queue_lo == queue_hi) |
194 |
{ |
{ |
199 |
packet = &packet_queue[queue_lo]; |
packet = &packet_queue[queue_lo]; |
200 |
out = &packet->s; |
out = &packet->s; |
201 |
|
|
202 |
|
if (((queue_lo + 1) % MAX_QUEUE) != queue_hi) |
203 |
|
{ |
204 |
|
next_tick = packet_queue[(queue_lo + 1) % MAX_QUEUE].tick; |
205 |
|
} |
206 |
|
else |
207 |
|
{ |
208 |
|
next_tick = (packet->tick + 65535) % 65536; |
209 |
|
} |
210 |
|
|
211 |
len = 0; |
len = 0; |
212 |
|
|
213 |
if (g_samplerate == 22050) |
if (g_samplerate == 22050) |
214 |
{ |
{ |
215 |
/* Resample to 44100 */ |
/* Resample to 44100 */ |
216 |
for (i = 0; (i < ((WAVEOUTBUF / 8) * (3 - g_samplewidth))) && (out->p < out->end); |
for (i = 0; (i < ((WAVEOUTBUF / 4) * (3 - g_samplewidth))) && (out->p < out->end); |
217 |
i++) |
i++) |
218 |
{ |
{ |
219 |
offset = i * 4 * g_samplewidth; |
/* On a stereo-channel we must make sure that left and right |
220 |
memcpy(&outbuf[0 * g_samplewidth + offset], out->p, g_samplewidth); |
does not get mixed up, so we need to expand the sample- |
221 |
memcpy(&outbuf[2 * g_samplewidth + offset], out->p, g_samplewidth); |
data with channels in mind: 1234 -> 12123434 |
222 |
out->p += 2; |
If we have a mono-channel, we can expand the data by simply |
223 |
|
doubling the sample-data: 1234 -> 11223344 */ |
224 |
memcpy(&outbuf[1 * g_samplewidth + offset], out->p, g_samplewidth); |
if (g_channels == 2) |
225 |
memcpy(&outbuf[3 * g_samplewidth + offset], out->p, g_samplewidth); |
offset = ((i * 2) - (i & 1)) * g_samplewidth; |
226 |
out->p += 2; |
else |
227 |
len += 4 * g_samplewidth; |
offset = (i * 2) * g_samplewidth; |
228 |
|
|
229 |
|
memcpy(&outbuf[offset], out->p, g_samplewidth); |
230 |
|
memcpy(&outbuf[g_channels * g_samplewidth + offset], out->p, g_samplewidth); |
231 |
|
|
232 |
|
out->p += g_samplewidth; |
233 |
|
len += 2 * g_samplewidth; |
234 |
} |
} |
235 |
} |
} |
236 |
else |
else |
242 |
|
|
243 |
ao_play(o_device, outbuf, len); |
ao_play(o_device, outbuf, len); |
244 |
|
|
245 |
if (out->p == out->end) |
gettimeofday(&tv, NULL); |
246 |
|
|
247 |
|
duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000; |
248 |
|
|
249 |
|
if (packet->tick > next_tick) |
250 |
|
next_tick += 65536; |
251 |
|
|
252 |
|
if ((out->p == out->end) || duration > next_tick - packet->tick + 500) |
253 |
{ |
{ |
254 |
rdpsnd_send_completion(packet->tick, packet->index); |
prev_s = tv.tv_sec; |
255 |
|
prev_us = tv.tv_usec; |
256 |
|
|
257 |
|
if (abs((next_tick - packet->tick) - duration) > 20) |
258 |
|
{ |
259 |
|
DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick)); |
260 |
|
DEBUG(("last: %d, is: %d, should: %d\n", packet->tick, |
261 |
|
(packet->tick + duration) % 65536, next_tick % 65536)); |
262 |
|
} |
263 |
|
|
264 |
|
/* Until all drivers are using the windows sound-ticks, we need to |
265 |
|
substract the 50 ticks added later by rdpsnd.c */ |
266 |
|
rdpsnd_send_completion(((packet->tick + duration) % 65536) - 50, packet->index); |
267 |
free(out->data); |
free(out->data); |
268 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
269 |
} |
} |