1 |
/* |
/* -*- c-basic-offset: 8 -*- |
2 |
rdesktop: A Remote Desktop Protocol client. |
rdesktop: A Remote Desktop Protocol client. |
3 |
Sound Channel Process Functions - SGI/IRIX |
Sound Channel Process Functions - SGI/IRIX |
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) Jeremy Meng voidfoo@cwazy.co.uk 2004 |
Copyright (C) Jeremy Meng void.foo@gmail.com 2004, 2005 |
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 |
24 |
#include <errno.h> |
#include <errno.h> |
25 |
#include <dmedia/audio.h> |
#include <dmedia/audio.h> |
26 |
|
|
27 |
#define IRIX_DEBUG 1 |
/* #define IRIX_DEBUG 1 */ |
28 |
|
|
29 |
#define IRIX_MAX_VOL 65535 |
#define IRIX_MAX_VOL 65535 |
30 |
|
|
|
#define MAX_QUEUE 10 |
|
|
|
|
|
int g_dsp_fd; |
|
31 |
ALconfig audioconfig; |
ALconfig audioconfig; |
32 |
ALport output_port; |
ALport output_port; |
33 |
|
|
|
BOOL g_dsp_busy = False; |
|
|
static BOOL g_swapaudio; |
|
34 |
static int g_snd_rate; |
static int g_snd_rate; |
|
static BOOL g_swapaudio; |
|
|
static short g_samplewidth; |
|
35 |
static int width = AL_SAMPLE_16; |
static int width = AL_SAMPLE_16; |
36 |
|
static char *sgi_output_device = NULL; |
37 |
|
|
38 |
double min_volume, max_volume, volume_range; |
double min_volume, max_volume, volume_range; |
39 |
int resource, maxFillable; |
int resource, maxFillable; |
40 |
int combinedFrameSize; |
int combinedFrameSize; |
41 |
|
|
|
static struct audio_packet |
|
|
{ |
|
|
struct stream s; |
|
|
uint16 tick; |
|
|
uint8 index; |
|
|
} packet_queue[MAX_QUEUE]; |
|
|
static unsigned int queue_hi, queue_lo; |
|
|
|
|
42 |
BOOL |
BOOL |
43 |
wave_out_open(void) |
sgi_open(void) |
44 |
{ |
{ |
45 |
ALparamInfo pinfo; |
ALparamInfo pinfo; |
46 |
|
static int warned = 0; |
47 |
|
|
48 |
#if (defined(IRIX_DEBUG)) |
#if (defined(IRIX_DEBUG)) |
49 |
fprintf(stderr, "wave_out_open: begin\n"); |
fprintf(stderr, "sgi_open: begin\n"); |
50 |
#endif |
#endif |
51 |
|
|
52 |
|
if (!warned && sgi_output_device) |
53 |
|
{ |
54 |
|
warning("device-options not supported for libao-driver\n"); |
55 |
|
warned = 1; |
56 |
|
} |
57 |
|
|
58 |
if (alGetParamInfo(AL_DEFAULT_OUTPUT, AL_GAIN, &pinfo) < 0) |
if (alGetParamInfo(AL_DEFAULT_OUTPUT, AL_GAIN, &pinfo) < 0) |
59 |
{ |
{ |
60 |
fprintf(stderr, "wave_out_open: alGetParamInfo failed: %s\n", |
fprintf(stderr, "sgi_open: alGetParamInfo failed: %s\n", |
61 |
alGetErrorString(oserror())); |
alGetErrorString(oserror())); |
62 |
} |
} |
63 |
min_volume = alFixedToDouble(pinfo.min.ll); |
min_volume = alFixedToDouble(pinfo.min.ll); |
64 |
max_volume = alFixedToDouble(pinfo.max.ll); |
max_volume = alFixedToDouble(pinfo.max.ll); |
65 |
volume_range = (max_volume - min_volume); |
volume_range = (max_volume - min_volume); |
66 |
#if (defined(IRIX_DEBUG)) |
#if (defined(IRIX_DEBUG)) |
67 |
fprintf(stderr, "wave_out_open: minvol = %lf, maxvol= %lf, range = %lf.\n", |
fprintf(stderr, "sgi_open: minvol = %lf, maxvol= %lf, range = %lf.\n", |
68 |
min_volume, max_volume, volume_range); |
min_volume, max_volume, volume_range); |
69 |
#endif |
#endif |
70 |
|
|
71 |
queue_lo = queue_hi = 0; |
rdpsnd_queue_init(); |
72 |
|
|
73 |
audioconfig = alNewConfig(); |
audioconfig = alNewConfig(); |
74 |
if (audioconfig < 0) |
if (audioconfig == (ALconfig) 0) |
75 |
{ |
{ |
76 |
fprintf(stderr, "wave_out_open: alNewConfig failed: %s\n", |
fprintf(stderr, "sgi_open: alNewConfig failed: %s\n", alGetErrorString(oserror())); |
|
alGetErrorString(oserror())); |
|
77 |
return False; |
return False; |
78 |
} |
} |
79 |
|
|
80 |
output_port = alOpenPort("rdpsnd", "w", 0); |
output_port = alOpenPort("rdpsnd", "w", 0); |
81 |
if (output_port == (ALport) 0) |
if (output_port == (ALport) 0) |
82 |
{ |
{ |
83 |
fprintf(stderr, "wave_out_open: alOpenPort failed: %s\n", |
fprintf(stderr, "sgi_open: alOpenPort failed: %s\n", alGetErrorString(oserror())); |
|
alGetErrorString(oserror())); |
|
84 |
return False; |
return False; |
85 |
} |
} |
86 |
|
|
87 |
#if (defined(IRIX_DEBUG)) |
#if (defined(IRIX_DEBUG)) |
88 |
fprintf(stderr, "wave_out_open: returning\n"); |
fprintf(stderr, "sgi_open: returning\n"); |
89 |
#endif |
#endif |
90 |
return True; |
return True; |
91 |
} |
} |
92 |
|
|
93 |
void |
void |
94 |
wave_out_close(void) |
sgi_close(void) |
95 |
{ |
{ |
96 |
/* Ack all remaining packets */ |
/* Ack all remaining packets */ |
97 |
#if (defined(IRIX_DEBUG)) |
#if (defined(IRIX_DEBUG)) |
98 |
fprintf(stderr, "wave_out_close: begin\n"); |
fprintf(stderr, "sgi_close: begin\n"); |
99 |
#endif |
#endif |
100 |
|
|
101 |
while (queue_lo != queue_hi) |
while (!rdpsnd_queue_empty()) |
102 |
{ |
{ |
103 |
rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index); |
/* We need to add 50 to tell windows that time has passed while |
104 |
free(packet_queue[queue_lo].s.data); |
* playing this packet */ |
105 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
rdpsnd_send_completion(rdpsnd_queue_current_packet()->tick + 50, |
106 |
|
rdpsnd_queue_current_packet()->index); |
107 |
|
rdpsnd_queue_next(); |
108 |
} |
} |
109 |
alDiscardFrames(output_port, 0); |
alDiscardFrames(output_port, 0); |
110 |
|
|
111 |
alClosePort(output_port); |
alClosePort(output_port); |
112 |
alFreeConfig(audioconfig); |
alFreeConfig(audioconfig); |
113 |
#if (defined(IRIX_DEBUG)) |
#if (defined(IRIX_DEBUG)) |
114 |
fprintf(stderr, "wave_out_close: returning\n"); |
fprintf(stderr, "sgi_close: returning\n"); |
115 |
#endif |
#endif |
116 |
} |
} |
117 |
|
|
118 |
BOOL |
BOOL |
119 |
wave_out_format_supported(WAVEFORMATEX * pwfx) |
sgi_format_supported(WAVEFORMATEX * pwfx) |
120 |
{ |
{ |
121 |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
122 |
return False; |
return False; |
129 |
} |
} |
130 |
|
|
131 |
BOOL |
BOOL |
132 |
wave_out_set_format(WAVEFORMATEX * pwfx) |
sgi_set_format(WAVEFORMATEX * pwfx) |
133 |
{ |
{ |
134 |
int channels; |
int channels; |
135 |
int frameSize, channelCount; |
int frameSize, channelCount; |
136 |
ALpv params; |
ALpv params; |
137 |
|
|
138 |
#if (defined(IRIX_DEBUG)) |
#if (defined(IRIX_DEBUG)) |
139 |
fprintf(stderr, "wave_out_set_format: init...\n"); |
fprintf(stderr, "sgi_set_format: init...\n"); |
140 |
#endif |
#endif |
|
/* limited support to configure an opened audio port in IRIX */ |
|
|
/* have to reopen the audio port, using same config */ |
|
|
alClosePort(output_port); |
|
|
|
|
|
g_swapaudio = False; |
|
141 |
|
|
142 |
if (pwfx->wBitsPerSample == 8) |
if (pwfx->wBitsPerSample == 8) |
143 |
width = AL_SAMPLE_8; |
width = AL_SAMPLE_8; |
144 |
else if (pwfx->wBitsPerSample == 16) |
else if (pwfx->wBitsPerSample == 16) |
|
{ |
|
145 |
width = AL_SAMPLE_16; |
width = AL_SAMPLE_16; |
|
/* Do we need to swap the 16bit values? (Are we BigEndian) */ |
|
|
#if (defined(IRIX_DEBUG)) |
|
|
g_swapaudio = 1; |
|
|
#else |
|
|
g_swapaudio = 0; |
|
|
#endif |
|
|
} |
|
146 |
|
|
147 |
g_samplewidth = pwfx->wBitsPerSample / 8; |
/* Limited support to configure an opened audio port in IRIX. The |
148 |
|
number of channels is a static setting and can not be changed after |
149 |
|
a port is opened. So if the number of channels remains the same, we |
150 |
|
can configure other settings; otherwise we have to reopen the audio |
151 |
|
port, using same config. */ |
152 |
|
|
153 |
channels = pwfx->nChannels; |
channels = pwfx->nChannels; |
154 |
g_snd_rate = pwfx->nSamplesPerSec; |
g_snd_rate = pwfx->nSamplesPerSec; |
155 |
|
|
156 |
alSetSampFmt(audioconfig, AL_SAMPFMT_TWOSCOMP); |
alSetSampFmt(audioconfig, AL_SAMPFMT_TWOSCOMP); |
157 |
alSetWidth(audioconfig, width); |
alSetWidth(audioconfig, width); |
158 |
alSetChannels(audioconfig, channels); |
if (channels != alGetChannels(audioconfig)) |
159 |
|
{ |
160 |
|
alClosePort(output_port); |
161 |
|
alSetChannels(audioconfig, channels); |
162 |
|
output_port = alOpenPort("rdpsnd", "w", audioconfig); |
163 |
|
|
164 |
output_port = alOpenPort("rdpsnd", "w", audioconfig); |
if (output_port == (ALport) 0) |
165 |
|
{ |
166 |
|
fprintf(stderr, "sgi_set_format: alOpenPort failed: %s\n", |
167 |
|
alGetErrorString(oserror())); |
168 |
|
return False; |
169 |
|
} |
170 |
|
|
|
if (output_port == (ALport) 0) |
|
|
{ |
|
|
fprintf(stderr, "wave_out_set_format: alOpenPort failed: %s\n", |
|
|
alGetErrorString(oserror())); |
|
|
return False; |
|
171 |
} |
} |
172 |
|
|
173 |
resource = alGetResource(output_port); |
resource = alGetResource(output_port); |
177 |
|
|
178 |
if (frameSize == 0 || channelCount == 0) |
if (frameSize == 0 || channelCount == 0) |
179 |
{ |
{ |
180 |
fprintf(stderr, "wave_out_set_format: bad frameSize or channelCount\n"); |
fprintf(stderr, "sgi_set_format: bad frameSize or channelCount\n"); |
181 |
return False; |
return False; |
182 |
} |
} |
183 |
combinedFrameSize = frameSize * channelCount; |
combinedFrameSize = frameSize * channelCount; |
198 |
} |
} |
199 |
|
|
200 |
#if (defined(IRIX_DEBUG)) |
#if (defined(IRIX_DEBUG)) |
201 |
fprintf(stderr, "wave_out_set_format: returning...\n"); |
fprintf(stderr, "sgi_set_format: returning...\n"); |
202 |
#endif |
#endif |
203 |
return True; |
return True; |
204 |
} |
} |
205 |
|
|
206 |
void |
void |
207 |
wave_out_volume(uint16 left, uint16 right) |
sgi_volume(uint16 left, uint16 right) |
208 |
{ |
{ |
209 |
double gainleft, gainright; |
double gainleft, gainright; |
210 |
ALpv pv[1]; |
ALpv pv[1]; |
211 |
ALfixed gain[8]; |
ALfixed gain[8]; |
212 |
|
|
213 |
#if (defined(IRIX_DEBUG)) |
#if (defined(IRIX_DEBUG)) |
214 |
fprintf(stderr, "wave_out_volume: begin\n"); |
fprintf(stderr, "sgi_volume: begin\n"); |
215 |
fprintf(stderr, "left='%d', right='%d'\n", left, right); |
fprintf(stderr, "left='%d', right='%d'\n", left, right); |
216 |
#endif |
#endif |
217 |
|
|
226 |
pv[0].sizeIn = 8; |
pv[0].sizeIn = 8; |
227 |
if (alSetParams(AL_DEFAULT_OUTPUT, pv, 1) < 0) |
if (alSetParams(AL_DEFAULT_OUTPUT, pv, 1) < 0) |
228 |
{ |
{ |
229 |
fprintf(stderr, "wave_out_volume: alSetParams failed: %s\n", |
fprintf(stderr, "sgi_volume: alSetParams failed: %s\n", |
230 |
alGetErrorString(oserror())); |
alGetErrorString(oserror())); |
231 |
return; |
return; |
232 |
} |
} |
233 |
|
|
234 |
#if (defined(IRIX_DEBUG)) |
#if (defined(IRIX_DEBUG)) |
235 |
fprintf(stderr, "wave_out_volume: returning\n"); |
fprintf(stderr, "sgi_volume: returning\n"); |
236 |
#endif |
#endif |
237 |
} |
} |
238 |
|
|
239 |
void |
void |
240 |
wave_out_write(STREAM s, uint16 tick, uint8 index) |
sgi_play(void) |
|
{ |
|
|
struct audio_packet *packet = &packet_queue[queue_hi]; |
|
|
unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE; |
|
|
|
|
|
if (next_hi == queue_lo) |
|
|
{ |
|
|
fprintf(stderr, "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 |
|
|
wave_out_play(void) |
|
241 |
{ |
{ |
242 |
struct audio_packet *packet; |
struct audio_packet *packet; |
243 |
ssize_t len; |
ssize_t len; |
244 |
unsigned int i; |
unsigned int i; |
|
uint8 swap; |
|
245 |
STREAM out; |
STREAM out; |
|
static long startedat_us; |
|
|
static long startedat_s; |
|
|
static BOOL started = False; |
|
|
static BOOL swapped = False; |
|
|
struct timeval tv; |
|
246 |
int gf; |
int gf; |
|
static long long temp; |
|
247 |
|
|
248 |
while (1) |
while (1) |
249 |
{ |
{ |
250 |
if (queue_lo == queue_hi) |
if (rdpsnd_queue_empty()) |
251 |
{ |
{ |
252 |
g_dsp_busy = False; |
g_dsp_busy = False; |
253 |
return; |
return; |
254 |
} |
} |
255 |
|
|
256 |
packet = &packet_queue[queue_lo]; |
packet = rdpsnd_queue_current_packet(); |
257 |
out = &packet->s; |
out = &packet->s; |
258 |
|
|
|
/* Swap the current packet, but only once */ |
|
|
if (g_swapaudio && !swapped) |
|
|
{ |
|
|
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 (!started) |
|
|
{ |
|
|
gettimeofday(&tv, NULL); |
|
|
startedat_us = tv.tv_usec; |
|
|
startedat_s = tv.tv_sec; |
|
|
started = True; |
|
|
} |
|
|
|
|
259 |
len = out->end - out->p; |
len = out->end - out->p; |
|
gf = alGetFillable(output_port); |
|
|
if (len > gf) |
|
|
{ |
|
|
/* len = gf * combinedFrameSize; */ |
|
|
#if (defined(IRIX_DEBUG)) |
|
|
/* fprintf(stderr,"Fillable...\n"); */ |
|
|
#endif |
|
|
} |
|
260 |
|
|
261 |
alWriteFrames(output_port, out->p, len / combinedFrameSize); |
alWriteFrames(output_port, out->p, len / combinedFrameSize); |
262 |
|
|
263 |
out->p += len; |
out->p += len; |
264 |
if (out->p == out->end) |
if (out->p == out->end) |
265 |
{ |
{ |
266 |
long long duration; |
gf = alGetFilled(output_port); |
267 |
long elapsed; |
if (gf < (4 * maxFillable / 10)) |
|
|
|
|
gettimeofday(&tv, NULL); |
|
|
duration = (out->size * (1000000 / (g_samplewidth * g_snd_rate))); |
|
|
elapsed = (tv.tv_sec - startedat_s) * 1000000 + (tv.tv_usec - startedat_us); |
|
|
/* 7/10 is not good for IRIX audio port, 4x/100 is suitable */ |
|
|
if (elapsed >= (duration * 485) / 1000) |
|
268 |
{ |
{ |
269 |
rdpsnd_send_completion(packet->tick, packet->index); |
rdpsnd_send_completion(packet->tick, packet->index); |
270 |
free(out->data); |
rdpsnd_queue_next(); |
|
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
|
|
started = False; |
|
|
swapped = False; |
|
271 |
} |
} |
272 |
else |
else |
273 |
{ |
{ |
274 |
#if (defined(IRIX_DEBUG)) |
#if (defined(IRIX_DEBUG)) |
275 |
/* fprintf(stderr,"Busy playing...\n"); */ |
/* fprintf(stderr,"Busy playing...\n"); */ |
276 |
#endif |
#endif |
277 |
g_dsp_busy = True; |
g_dsp_busy = True; |
278 |
|
usleep(10); |
279 |
return; |
return; |
280 |
} |
} |
281 |
} |
} |
282 |
} |
} |
283 |
} |
} |
284 |
|
|
285 |
|
struct audio_driver * |
286 |
|
sgi_register(char *options) |
287 |
|
{ |
288 |
|
static struct audio_driver sgi_driver; |
289 |
|
|
290 |
|
sgi_driver.wave_out_write = rdpsnd_queue_write; |
291 |
|
sgi_driver.wave_out_open = sgi_open; |
292 |
|
sgi_driver.wave_out_close = sgi_close; |
293 |
|
sgi_driver.wave_out_format_supported = sgi_format_supported; |
294 |
|
sgi_driver.wave_out_set_format = sgi_set_format; |
295 |
|
sgi_driver.wave_out_volume = sgi_volume; |
296 |
|
sgi_driver.wave_out_play = sgi_play; |
297 |
|
sgi_driver.name = xstrdup("sgi"); |
298 |
|
sgi_driver.description = xstrdup("SGI output driver"); |
299 |
|
sgi_driver.need_byteswap_on_be = 1; |
300 |
|
sgi_driver.next = NULL; |
301 |
|
|
302 |
|
if (options) |
303 |
|
{ |
304 |
|
sgi_output_device = xstrdup(options); |
305 |
|
} |
306 |
|
return &sgi_driver; |
307 |
|
} |