1 |
/* -*- c-basic-offset: 8 -*- |
2 |
rdesktop: A Remote Desktop Protocol client. |
3 |
Sound Channel Process Functions - alsa-driver |
4 |
Copyright (C) Matthew Chapman 2003 |
5 |
Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003 |
6 |
Copyright (C) Michael Gernoth mike@zerfleddert.de 2006 |
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 "rdpsnd.h" |
25 |
#include "rdpsnd_dsp.h" |
26 |
#include <unistd.h> |
27 |
#include <fcntl.h> |
28 |
#include <errno.h> |
29 |
#include <alsa/asoundlib.h> |
30 |
#include <sys/time.h> |
31 |
|
32 |
#define DEFAULTDEVICE "default" |
33 |
#define MAX_FRAMES 32 |
34 |
|
35 |
static snd_pcm_t *pcm_handle = NULL; |
36 |
static snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK; |
37 |
static BOOL reopened; |
38 |
static short samplewidth; |
39 |
static int audiochannels; |
40 |
static unsigned int rate; |
41 |
static char *pcm_name; |
42 |
|
43 |
BOOL |
44 |
alsa_open(void) |
45 |
{ |
46 |
int err; |
47 |
|
48 |
if ((err = snd_pcm_open(&pcm_handle, pcm_name, stream, 0)) < 0) |
49 |
{ |
50 |
error("snd_pcm_open: %s\n", snd_strerror(err)); |
51 |
return False; |
52 |
} |
53 |
|
54 |
g_dsp_fd = 0; |
55 |
|
56 |
reopened = True; |
57 |
|
58 |
return True; |
59 |
} |
60 |
|
61 |
void |
62 |
alsa_close(void) |
63 |
{ |
64 |
/* Ack all remaining packets */ |
65 |
while (!rdpsnd_queue_empty()) |
66 |
rdpsnd_queue_next(0); |
67 |
|
68 |
if (pcm_handle) |
69 |
{ |
70 |
snd_pcm_drop(pcm_handle); |
71 |
snd_pcm_close(pcm_handle); |
72 |
pcm_handle = NULL; |
73 |
} |
74 |
} |
75 |
|
76 |
BOOL |
77 |
alsa_format_supported(WAVEFORMATEX * pwfx) |
78 |
{ |
79 |
#if 0 |
80 |
int err; |
81 |
snd_pcm_hw_params_t *hwparams = NULL; |
82 |
|
83 |
if ((err = snd_pcm_hw_params_malloc(&hwparams)) < 0) |
84 |
{ |
85 |
error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err)); |
86 |
return False; |
87 |
} |
88 |
|
89 |
if ((err = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0) |
90 |
{ |
91 |
error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err)); |
92 |
return False; |
93 |
} |
94 |
snd_pcm_hw_params_free(hwparams); |
95 |
#endif |
96 |
|
97 |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
98 |
return False; |
99 |
if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2)) |
100 |
return False; |
101 |
if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16)) |
102 |
return False; |
103 |
if ((pwfx->nSamplesPerSec != 44100) && (pwfx->nSamplesPerSec != 22050)) |
104 |
return False; |
105 |
|
106 |
return True; |
107 |
} |
108 |
|
109 |
BOOL |
110 |
alsa_set_format(WAVEFORMATEX * pwfx) |
111 |
{ |
112 |
snd_pcm_hw_params_t *hwparams = NULL; |
113 |
int err; |
114 |
unsigned int buffertime; |
115 |
|
116 |
samplewidth = pwfx->wBitsPerSample / 8; |
117 |
|
118 |
if ((err = snd_pcm_hw_params_malloc(&hwparams)) < 0) |
119 |
{ |
120 |
error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err)); |
121 |
return False; |
122 |
} |
123 |
|
124 |
if ((err = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0) |
125 |
{ |
126 |
error("snd_pcm_hw_params_any: %s\n", snd_strerror(err)); |
127 |
return False; |
128 |
} |
129 |
|
130 |
if ((err = |
131 |
snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) |
132 |
{ |
133 |
error("snd_pcm_hw_params_set_access: %s\n", snd_strerror(err)); |
134 |
return False; |
135 |
} |
136 |
|
137 |
if (pwfx->wBitsPerSample == 16) |
138 |
{ |
139 |
if ((err = |
140 |
snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE)) < 0) |
141 |
{ |
142 |
error("snd_pcm_hw_params_set_format: %s\n", snd_strerror(err)); |
143 |
return False; |
144 |
} |
145 |
} |
146 |
else |
147 |
{ |
148 |
if ((err = |
149 |
snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S8)) < 0) |
150 |
{ |
151 |
error("snd_pcm_hw_params_set_format: %s\n", snd_strerror(err)); |
152 |
return False; |
153 |
} |
154 |
} |
155 |
|
156 |
#if 0 |
157 |
if ((err = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 1)) < 0) |
158 |
{ |
159 |
error("snd_pcm_hw_params_set_rate_resample: %s\n", snd_strerror(err)); |
160 |
return False; |
161 |
} |
162 |
#endif |
163 |
|
164 |
rate = pwfx->nSamplesPerSec; |
165 |
if ((err = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &rate, 0)) < 0) |
166 |
{ |
167 |
error("snd_pcm_hw_params_set_rate_near: %s\n", snd_strerror(err)); |
168 |
return False; |
169 |
} |
170 |
|
171 |
audiochannels = pwfx->nChannels; |
172 |
if ((err = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, pwfx->nChannels)) < 0) |
173 |
{ |
174 |
error("snd_pcm_hw_params_set_channels: %s\n", snd_strerror(err)); |
175 |
return False; |
176 |
} |
177 |
|
178 |
|
179 |
buffertime = 500000; /* microseconds */ |
180 |
if ((err = |
181 |
snd_pcm_hw_params_set_buffer_time_near(pcm_handle, hwparams, &buffertime, 0)) < 0) |
182 |
{ |
183 |
error("snd_pcm_hw_params_set_buffer_time_near: %s\n", snd_strerror(err)); |
184 |
return False; |
185 |
} |
186 |
|
187 |
if ((err = snd_pcm_hw_params(pcm_handle, hwparams)) < 0) |
188 |
{ |
189 |
error("snd_pcm_hw_params: %s\n", snd_strerror(err)); |
190 |
return False; |
191 |
} |
192 |
|
193 |
snd_pcm_hw_params_free(hwparams); |
194 |
|
195 |
if ((err = snd_pcm_prepare(pcm_handle)) < 0) |
196 |
{ |
197 |
error("snd_pcm_prepare: %s\n", snd_strerror(err)); |
198 |
return False; |
199 |
} |
200 |
|
201 |
reopened = True; |
202 |
|
203 |
return True; |
204 |
} |
205 |
|
206 |
void |
207 |
alsa_play(void) |
208 |
{ |
209 |
struct audio_packet *packet; |
210 |
STREAM out; |
211 |
int len; |
212 |
static long prev_s, prev_us; |
213 |
unsigned int duration; |
214 |
struct timeval tv; |
215 |
int next_tick; |
216 |
|
217 |
if (reopened) |
218 |
{ |
219 |
reopened = False; |
220 |
gettimeofday(&tv, NULL); |
221 |
prev_s = tv.tv_sec; |
222 |
prev_us = tv.tv_usec; |
223 |
} |
224 |
|
225 |
if (rdpsnd_queue_empty()) |
226 |
{ |
227 |
g_dsp_busy = 0; |
228 |
return; |
229 |
} |
230 |
|
231 |
packet = rdpsnd_queue_current_packet(); |
232 |
out = &packet->s; |
233 |
|
234 |
next_tick = rdpsnd_queue_next_tick(); |
235 |
|
236 |
len = (out->end - out->p) / (samplewidth * audiochannels); |
237 |
if ((len = snd_pcm_writei(pcm_handle, out->p, ((MAX_FRAMES < len) ? MAX_FRAMES : len))) < 0) |
238 |
{ |
239 |
snd_pcm_prepare(pcm_handle); |
240 |
len = 0; |
241 |
} |
242 |
out->p += (len * samplewidth * audiochannels); |
243 |
|
244 |
gettimeofday(&tv, NULL); |
245 |
|
246 |
duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000; |
247 |
|
248 |
if (packet->tick > next_tick) |
249 |
next_tick += 65536; |
250 |
|
251 |
if ((out->p == out->end) || duration > next_tick - packet->tick + 500) |
252 |
{ |
253 |
snd_pcm_sframes_t delay_frames; |
254 |
unsigned long delay_us; |
255 |
|
256 |
prev_s = tv.tv_sec; |
257 |
prev_us = tv.tv_usec; |
258 |
|
259 |
if (abs((next_tick - packet->tick) - duration) > 20) |
260 |
{ |
261 |
DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick)); |
262 |
DEBUG(("last: %d, is: %d, should: %d\n", packet->tick, |
263 |
(packet->tick + duration) % 65536, next_tick % 65536)); |
264 |
} |
265 |
|
266 |
if (snd_pcm_delay(pcm_handle, &delay_frames) < 0) |
267 |
delay_frames = out->size / (samplewidth * audiochannels); |
268 |
if (delay_frames < 0) |
269 |
delay_frames = 0; |
270 |
|
271 |
delay_us = delay_frames * (1000000 / rate); |
272 |
|
273 |
rdpsnd_queue_next(delay_us); |
274 |
} |
275 |
|
276 |
g_dsp_busy = 1; |
277 |
return; |
278 |
} |
279 |
|
280 |
struct audio_driver * |
281 |
alsa_register(char *options) |
282 |
{ |
283 |
static struct audio_driver alsa_driver; |
284 |
|
285 |
alsa_driver.wave_out_open = alsa_open; |
286 |
alsa_driver.wave_out_close = alsa_close; |
287 |
alsa_driver.wave_out_format_supported = alsa_format_supported; |
288 |
alsa_driver.wave_out_set_format = alsa_set_format; |
289 |
alsa_driver.wave_out_volume = rdpsnd_dsp_softvol_set; |
290 |
alsa_driver.wave_out_play = alsa_play; |
291 |
alsa_driver.name = xstrdup("alsa"); |
292 |
alsa_driver.description = xstrdup("ALSA output driver, default device: " DEFAULTDEVICE); |
293 |
alsa_driver.need_byteswap_on_be = 0; |
294 |
alsa_driver.need_resampling = 0; |
295 |
alsa_driver.next = NULL; |
296 |
|
297 |
if (options) |
298 |
{ |
299 |
pcm_name = xstrdup(options); |
300 |
} |
301 |
else |
302 |
{ |
303 |
pcm_name = xstrdup(DEFAULTDEVICE); |
304 |
} |
305 |
|
306 |
return &alsa_driver; |
307 |
} |