1 |
/* |
2 |
rdesktop: A Remote Desktop Protocol client. |
3 |
Sound Channel Process Functions - libao-driver |
4 |
Copyright (C) Matthew Chapman 2003 |
5 |
Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003 |
6 |
Copyright (C) Michael Gernoth mike@zerfleddert.de 2005 |
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 <ao/ao.h> |
28 |
|
29 |
#define MAX_QUEUE 10 |
30 |
#define WAVEOUTBUF 32 |
31 |
|
32 |
int g_dsp_fd; |
33 |
ao_device *o_device = NULL; |
34 |
int default_driver; |
35 |
int g_samplerate; |
36 |
BOOL g_dsp_busy = False; |
37 |
static short g_samplewidth; |
38 |
|
39 |
static struct audio_packet |
40 |
{ |
41 |
struct stream s; |
42 |
uint16 tick; |
43 |
uint8 index; |
44 |
} packet_queue[MAX_QUEUE]; |
45 |
static unsigned int queue_hi, queue_lo; |
46 |
|
47 |
BOOL |
48 |
wave_out_open(void) |
49 |
{ |
50 |
ao_sample_format format; |
51 |
|
52 |
ao_initialize(); |
53 |
default_driver = ao_default_driver_id(); |
54 |
|
55 |
format.bits = 16; |
56 |
format.channels = 2; |
57 |
format.rate = 44100; |
58 |
g_samplerate = 44100; |
59 |
format.byte_format = AO_FMT_LITTLE; |
60 |
|
61 |
o_device = ao_open_live(default_driver, &format, NULL); |
62 |
if (o_device == NULL) |
63 |
{ |
64 |
return False; |
65 |
} |
66 |
|
67 |
g_dsp_fd = 0; |
68 |
queue_lo = queue_hi = 0; |
69 |
|
70 |
return True; |
71 |
} |
72 |
|
73 |
void |
74 |
wave_out_close(void) |
75 |
{ |
76 |
/* Ack all remaining packets */ |
77 |
while (queue_lo != queue_hi) |
78 |
{ |
79 |
rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index); |
80 |
free(packet_queue[queue_lo].s.data); |
81 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
82 |
} |
83 |
|
84 |
if (o_device != NULL) |
85 |
ao_close(o_device); |
86 |
|
87 |
ao_shutdown(); |
88 |
} |
89 |
|
90 |
BOOL |
91 |
wave_out_format_supported(WAVEFORMATEX * pwfx) |
92 |
{ |
93 |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
94 |
return False; |
95 |
if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2)) |
96 |
return False; |
97 |
if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16)) |
98 |
return False; |
99 |
/* The only common denominator between libao output drivers is a sample-rate of |
100 |
44100, we need to upsample 22050 to it */ |
101 |
if ((pwfx->nSamplesPerSec != 44100) && (pwfx->nSamplesPerSec != 22050)) |
102 |
return False; |
103 |
|
104 |
return True; |
105 |
} |
106 |
|
107 |
BOOL |
108 |
wave_out_set_format(WAVEFORMATEX * pwfx) |
109 |
{ |
110 |
ao_sample_format format; |
111 |
|
112 |
format.bits = pwfx->wBitsPerSample; |
113 |
format.channels = pwfx->nChannels; |
114 |
format.rate = 44100; |
115 |
g_samplerate = pwfx->nSamplesPerSec; |
116 |
format.byte_format = AO_FMT_LITTLE; |
117 |
|
118 |
g_samplewidth = pwfx->wBitsPerSample / 8; |
119 |
|
120 |
if (o_device != NULL) |
121 |
ao_close(o_device); |
122 |
|
123 |
o_device = ao_open_live(default_driver, &format, NULL); |
124 |
if (o_device == NULL) |
125 |
{ |
126 |
return False; |
127 |
} |
128 |
|
129 |
|
130 |
return True; |
131 |
} |
132 |
|
133 |
void |
134 |
wave_out_volume(uint16 left, uint16 right) |
135 |
{ |
136 |
warning("volume changes not supported with libao-output\n"); |
137 |
} |
138 |
|
139 |
void |
140 |
wave_out_write(STREAM s, uint16 tick, uint8 index) |
141 |
{ |
142 |
struct audio_packet *packet = &packet_queue[queue_hi]; |
143 |
unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE; |
144 |
|
145 |
if (next_hi == queue_lo) |
146 |
{ |
147 |
error("No space to queue audio packet\n"); |
148 |
return; |
149 |
} |
150 |
|
151 |
queue_hi = next_hi; |
152 |
|
153 |
packet->s = *s; |
154 |
packet->tick = tick; |
155 |
packet->index = index; |
156 |
packet->s.p += 4; |
157 |
|
158 |
/* we steal the data buffer from s, give it a new one */ |
159 |
s->data = malloc(s->size); |
160 |
|
161 |
if (!g_dsp_busy) |
162 |
wave_out_play(); |
163 |
} |
164 |
|
165 |
void |
166 |
wave_out_play(void) |
167 |
{ |
168 |
struct audio_packet *packet; |
169 |
STREAM out; |
170 |
unsigned char outbuf[WAVEOUTBUF]; |
171 |
int offset, len, i; |
172 |
|
173 |
if (queue_lo == queue_hi) |
174 |
{ |
175 |
g_dsp_busy = 0; |
176 |
return; |
177 |
} |
178 |
|
179 |
packet = &packet_queue[queue_lo]; |
180 |
out = &packet->s; |
181 |
|
182 |
len = 0; |
183 |
|
184 |
if (g_samplerate == 22050) |
185 |
{ |
186 |
/* Resample to 44100 */ |
187 |
for (i = 0; (i < ((WAVEOUTBUF / 8) * (3 - g_samplewidth))) && (out->p < out->end); |
188 |
i++) |
189 |
{ |
190 |
offset = i * 4 * g_samplewidth; |
191 |
memcpy(&outbuf[0 * g_samplewidth + offset], out->p, g_samplewidth); |
192 |
memcpy(&outbuf[2 * g_samplewidth + offset], out->p, g_samplewidth); |
193 |
out->p += 2; |
194 |
|
195 |
memcpy(&outbuf[1 * g_samplewidth + offset], out->p, g_samplewidth); |
196 |
memcpy(&outbuf[3 * g_samplewidth + offset], out->p, g_samplewidth); |
197 |
out->p += 2; |
198 |
len += 4 * g_samplewidth; |
199 |
} |
200 |
} |
201 |
else |
202 |
{ |
203 |
len = (WAVEOUTBUF > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTBUF; |
204 |
memcpy(outbuf, out->p, len); |
205 |
out->p += len; |
206 |
} |
207 |
|
208 |
ao_play(o_device, outbuf, len); |
209 |
|
210 |
if (out->p == out->end) |
211 |
{ |
212 |
rdpsnd_send_completion(packet->tick, packet->index); |
213 |
free(out->data); |
214 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
215 |
} |
216 |
|
217 |
g_dsp_busy = 1; |
218 |
return; |
219 |
} |