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 <unistd.h> |
26 |
#include <fcntl.h> |
27 |
#include <errno.h> |
28 |
#include <alsa/asoundlib.h> |
29 |
#include <sys/time.h> |
30 |
|
31 |
#define DEFAULTDEVICE "default" |
32 |
#define MAX_FRAMES 32 |
33 |
|
34 |
static snd_pcm_t *pcm_handle = NULL; |
35 |
static snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK; |
36 |
static BOOL reopened; |
37 |
static short samplewidth; |
38 |
static int audiochannels; |
39 |
|
40 |
BOOL |
41 |
wave_out_open(void) |
42 |
{ |
43 |
char *pcm_name; |
44 |
int err; |
45 |
|
46 |
pcm_name = xstrdup(DEFAULTDEVICE); |
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 |
rdpsnd_queue_init(); |
56 |
|
57 |
reopened = True; |
58 |
|
59 |
return True; |
60 |
} |
61 |
|
62 |
void |
63 |
wave_out_close(void) |
64 |
{ |
65 |
/* Ack all remaining packets */ |
66 |
while (!rdpsnd_queue_empty()) |
67 |
{ |
68 |
rdpsnd_send_completion(rdpsnd_queue_current_packet()->tick, |
69 |
rdpsnd_queue_current_packet()->index); |
70 |
rdpsnd_queue_next(); |
71 |
} |
72 |
|
73 |
|
74 |
if (pcm_handle) |
75 |
{ |
76 |
snd_pcm_drop(pcm_handle); |
77 |
snd_pcm_close(pcm_handle); |
78 |
} |
79 |
} |
80 |
|
81 |
BOOL |
82 |
wave_out_format_supported(WAVEFORMATEX * pwfx) |
83 |
{ |
84 |
#if 0 |
85 |
int err; |
86 |
snd_pcm_hw_params_t *hwparams = NULL; |
87 |
|
88 |
if ((err = snd_pcm_hw_params_malloc(&hwparams)) < 0) |
89 |
{ |
90 |
error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err)); |
91 |
return False; |
92 |
} |
93 |
|
94 |
if ((err = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0) |
95 |
{ |
96 |
error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err)); |
97 |
return False; |
98 |
} |
99 |
snd_pcm_hw_params_free(hwparams); |
100 |
#endif |
101 |
|
102 |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
103 |
return False; |
104 |
if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2)) |
105 |
return False; |
106 |
if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16)) |
107 |
return False; |
108 |
if ((pwfx->nSamplesPerSec != 44100) && (pwfx->nSamplesPerSec != 22050)) |
109 |
return False; |
110 |
|
111 |
return True; |
112 |
} |
113 |
|
114 |
BOOL |
115 |
wave_out_set_format(WAVEFORMATEX * pwfx) |
116 |
{ |
117 |
snd_pcm_hw_params_t *hwparams = NULL; |
118 |
unsigned int rate, exact_rate; |
119 |
int err; |
120 |
unsigned int buffertime; |
121 |
|
122 |
samplewidth = pwfx->wBitsPerSample / 8; |
123 |
|
124 |
if ((err = snd_pcm_hw_params_malloc(&hwparams)) < 0) |
125 |
{ |
126 |
error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err)); |
127 |
return False; |
128 |
} |
129 |
|
130 |
if ((err = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0) |
131 |
{ |
132 |
error("snd_pcm_hw_params_any: %s\n", snd_strerror(err)); |
133 |
return False; |
134 |
} |
135 |
|
136 |
if ((err = |
137 |
snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) |
138 |
{ |
139 |
error("snd_pcm_hw_params_set_access: %s\n", snd_strerror(err)); |
140 |
return False; |
141 |
} |
142 |
|
143 |
if (pwfx->wBitsPerSample == 16) |
144 |
{ |
145 |
if ((err = |
146 |
snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE)) < 0) |
147 |
{ |
148 |
error("snd_pcm_hw_params_set_format: %s\n", snd_strerror(err)); |
149 |
return False; |
150 |
} |
151 |
} |
152 |
else |
153 |
{ |
154 |
if ((err = |
155 |
snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S8)) < 0) |
156 |
{ |
157 |
error("snd_pcm_hw_params_set_format: %s\n", snd_strerror(err)); |
158 |
return False; |
159 |
} |
160 |
} |
161 |
|
162 |
#if 0 |
163 |
if ((err = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 1)) < 0) |
164 |
{ |
165 |
error("snd_pcm_hw_params_set_rate_resample: %s\n", snd_strerror(err)); |
166 |
return False; |
167 |
} |
168 |
#endif |
169 |
|
170 |
exact_rate = rate = pwfx->nSamplesPerSec; |
171 |
if ((err = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &exact_rate, 0)) < 0) |
172 |
{ |
173 |
error("snd_pcm_hw_params_set_rate_near: %s\n", snd_strerror(err)); |
174 |
return False; |
175 |
} |
176 |
|
177 |
audiochannels = pwfx->nChannels; |
178 |
if ((err = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, pwfx->nChannels)) < 0) |
179 |
{ |
180 |
error("snd_pcm_hw_params_set_channels: %s\n", snd_strerror(err)); |
181 |
return False; |
182 |
} |
183 |
|
184 |
|
185 |
buffertime = 500000; /* microseconds */ |
186 |
if ((err = |
187 |
snd_pcm_hw_params_set_buffer_time_near(pcm_handle, hwparams, &buffertime, 0)) < 0) |
188 |
{ |
189 |
error("snd_pcm_hw_params_set_buffer_time_near: %s\n", snd_strerror(err)); |
190 |
return False; |
191 |
} |
192 |
|
193 |
if ((err = snd_pcm_hw_params(pcm_handle, hwparams)) < 0) |
194 |
{ |
195 |
error("snd_pcm_hw_params: %s\n", snd_strerror(err)); |
196 |
return False; |
197 |
} |
198 |
|
199 |
snd_pcm_hw_params_free(hwparams); |
200 |
|
201 |
if ((err = snd_pcm_prepare(pcm_handle)) < 0) |
202 |
{ |
203 |
error("snd_pcm_prepare: %s\n", snd_strerror(err)); |
204 |
return False; |
205 |
} |
206 |
|
207 |
reopened = True; |
208 |
|
209 |
return True; |
210 |
} |
211 |
|
212 |
void |
213 |
wave_out_volume(uint16 left, uint16 right) |
214 |
{ |
215 |
static int warned = 0; |
216 |
|
217 |
if (!warned) |
218 |
{ |
219 |
warning("volume changes currently not supported with experimental alsa-output\n"); |
220 |
warned = 1; |
221 |
} |
222 |
} |
223 |
|
224 |
void |
225 |
wave_out_play(void) |
226 |
{ |
227 |
struct audio_packet *packet; |
228 |
STREAM out; |
229 |
int len; |
230 |
static long prev_s, prev_us; |
231 |
unsigned int duration; |
232 |
struct timeval tv; |
233 |
int next_tick; |
234 |
|
235 |
if (reopened) |
236 |
{ |
237 |
reopened = False; |
238 |
gettimeofday(&tv, NULL); |
239 |
prev_s = tv.tv_sec; |
240 |
prev_us = tv.tv_usec; |
241 |
} |
242 |
|
243 |
if (rdpsnd_queue_empty()) |
244 |
{ |
245 |
g_dsp_busy = 0; |
246 |
return; |
247 |
} |
248 |
|
249 |
packet = rdpsnd_queue_current_packet(); |
250 |
out = &packet->s; |
251 |
|
252 |
next_tick = rdpsnd_queue_next_tick(); |
253 |
|
254 |
len = (out->end - out->p) / (samplewidth * audiochannels); |
255 |
if ((len = snd_pcm_writei(pcm_handle, out->p, ((MAX_FRAMES < len) ? MAX_FRAMES : len))) < 0) |
256 |
{ |
257 |
snd_pcm_prepare(pcm_handle); |
258 |
len = 0; |
259 |
} |
260 |
out->p += (len * samplewidth * audiochannels); |
261 |
|
262 |
gettimeofday(&tv, NULL); |
263 |
|
264 |
duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000; |
265 |
|
266 |
if (packet->tick > next_tick) |
267 |
next_tick += 65536; |
268 |
|
269 |
if ((out->p == out->end) || duration > next_tick - packet->tick + 500) |
270 |
{ |
271 |
prev_s = tv.tv_sec; |
272 |
prev_us = tv.tv_usec; |
273 |
|
274 |
if (abs((next_tick - packet->tick) - duration) > 20) |
275 |
{ |
276 |
DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick)); |
277 |
DEBUG(("last: %d, is: %d, should: %d\n", packet->tick, |
278 |
(packet->tick + duration) % 65536, next_tick % 65536)); |
279 |
} |
280 |
|
281 |
rdpsnd_send_completion(((packet->tick + duration) % 65536), packet->index); |
282 |
rdpsnd_queue_next(); |
283 |
} |
284 |
|
285 |
g_dsp_busy = 1; |
286 |
return; |
287 |
} |