1 |
astrand |
963 |
/* -*- c-basic-offset: 8 -*- |
2 |
matthewc |
474 |
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
Sound Channel Process Functions |
4 |
|
|
Copyright (C) Matthew Chapman 2003 |
5 |
|
|
Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003 |
6 |
|
|
|
7 |
|
|
This program is free software; you can redistribute it and/or modify |
8 |
|
|
it under the terms of the GNU General Public License as published by |
9 |
|
|
the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
(at your option) any later version. |
11 |
|
|
|
12 |
|
|
This program is distributed in the hope that it will be useful, |
13 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
GNU General Public License for more details. |
16 |
|
|
|
17 |
|
|
You should have received a copy of the GNU General Public License |
18 |
|
|
along with this program; if not, write to the Free Software |
19 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
|
|
*/ |
21 |
|
|
|
22 |
matthewc |
432 |
#include "rdesktop.h" |
23 |
stargo |
1254 |
#include "rdpsnd.h" |
24 |
stargo |
1258 |
#include "rdpsnd_dsp.h" |
25 |
matthewc |
432 |
|
26 |
matthewc |
474 |
#define RDPSND_CLOSE 1 |
27 |
|
|
#define RDPSND_WRITE 2 |
28 |
|
|
#define RDPSND_SET_VOLUME 3 |
29 |
|
|
#define RDPSND_UNKNOWN4 4 |
30 |
|
|
#define RDPSND_COMPLETION 5 |
31 |
stargo |
1044 |
#define RDPSND_SERVERTICK 6 |
32 |
matthewc |
474 |
#define RDPSND_NEGOTIATE 7 |
33 |
|
|
|
34 |
|
|
#define MAX_FORMATS 10 |
35 |
stargo |
1254 |
#define MAX_QUEUE 10 |
36 |
matthewc |
474 |
|
37 |
stargo |
1254 |
BOOL g_dsp_busy = False; |
38 |
|
|
int g_dsp_fd; |
39 |
|
|
|
40 |
matthewc |
432 |
static VCHANNEL *rdpsnd_channel; |
41 |
stargo |
1255 |
static struct audio_driver *drivers = NULL; |
42 |
stargo |
1256 |
struct audio_driver *current_driver = NULL; |
43 |
matthewc |
432 |
|
44 |
matthewc |
474 |
static BOOL device_open; |
45 |
|
|
static WAVEFORMATEX formats[MAX_FORMATS]; |
46 |
|
|
static unsigned int format_count; |
47 |
|
|
static unsigned int current_format; |
48 |
stargo |
1256 |
unsigned int queue_hi, queue_lo; |
49 |
|
|
struct audio_packet packet_queue[MAX_QUEUE]; |
50 |
matthewc |
474 |
|
51 |
stargo |
1255 |
void (*wave_out_play) (void); |
52 |
|
|
|
53 |
astrand |
665 |
static STREAM |
54 |
matthewc |
474 |
rdpsnd_init_packet(uint16 type, uint16 size) |
55 |
|
|
{ |
56 |
|
|
STREAM s; |
57 |
|
|
|
58 |
astrand |
499 |
s = channel_init(rdpsnd_channel, size + 4); |
59 |
matthewc |
474 |
out_uint16_le(s, type); |
60 |
|
|
out_uint16_le(s, size); |
61 |
|
|
return s; |
62 |
|
|
} |
63 |
|
|
|
64 |
astrand |
665 |
static void |
65 |
matthewc |
474 |
rdpsnd_send(STREAM s) |
66 |
|
|
{ |
67 |
|
|
#ifdef RDPSND_DEBUG |
68 |
|
|
printf("RDPSND send:\n"); |
69 |
astrand |
499 |
hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8); |
70 |
matthewc |
474 |
#endif |
71 |
|
|
|
72 |
|
|
channel_send(s, rdpsnd_channel); |
73 |
|
|
} |
74 |
|
|
|
75 |
astrand |
499 |
void |
76 |
|
|
rdpsnd_send_completion(uint16 tick, uint8 packet_index) |
77 |
matthewc |
474 |
{ |
78 |
|
|
STREAM s; |
79 |
|
|
|
80 |
|
|
s = rdpsnd_init_packet(RDPSND_COMPLETION, 4); |
81 |
stargo |
1254 |
out_uint16_le(s, tick); |
82 |
matthewc |
474 |
out_uint8(s, packet_index); |
83 |
|
|
out_uint8(s, 0); |
84 |
|
|
s_mark_end(s); |
85 |
|
|
rdpsnd_send(s); |
86 |
|
|
} |
87 |
|
|
|
88 |
astrand |
665 |
static void |
89 |
matthewc |
474 |
rdpsnd_process_negotiate(STREAM in) |
90 |
|
|
{ |
91 |
|
|
unsigned int in_format_count, i; |
92 |
|
|
WAVEFORMATEX *format; |
93 |
|
|
STREAM out; |
94 |
stargo |
491 |
BOOL device_available = False; |
95 |
stargo |
493 |
int readcnt; |
96 |
|
|
int discardcnt; |
97 |
matthewc |
474 |
|
98 |
|
|
in_uint8s(in, 14); /* flags, volume, pitch, UDP port */ |
99 |
|
|
in_uint16_le(in, in_format_count); |
100 |
|
|
in_uint8s(in, 4); /* pad, status, pad */ |
101 |
|
|
|
102 |
stargo |
1255 |
if (current_driver->wave_out_open()) |
103 |
stargo |
491 |
{ |
104 |
stargo |
1255 |
current_driver->wave_out_close(); |
105 |
stargo |
491 |
device_available = True; |
106 |
|
|
} |
107 |
|
|
|
108 |
matthewc |
474 |
format_count = 0; |
109 |
astrand |
499 |
if (s_check_rem(in, 18 * in_format_count)) |
110 |
matthewc |
474 |
{ |
111 |
|
|
for (i = 0; i < in_format_count; i++) |
112 |
|
|
{ |
113 |
|
|
format = &formats[format_count]; |
114 |
|
|
in_uint16_le(in, format->wFormatTag); |
115 |
|
|
in_uint16_le(in, format->nChannels); |
116 |
|
|
in_uint32_le(in, format->nSamplesPerSec); |
117 |
|
|
in_uint32_le(in, format->nAvgBytesPerSec); |
118 |
|
|
in_uint16_le(in, format->nBlockAlign); |
119 |
|
|
in_uint16_le(in, format->wBitsPerSample); |
120 |
|
|
in_uint16_le(in, format->cbSize); |
121 |
|
|
|
122 |
stargo |
492 |
/* read in the buffer of unknown use */ |
123 |
stargo |
493 |
readcnt = format->cbSize; |
124 |
|
|
discardcnt = 0; |
125 |
stargo |
492 |
if (format->cbSize > MAX_CBSIZE) |
126 |
|
|
{ |
127 |
astrand |
499 |
fprintf(stderr, "cbSize too large for buffer: %d\n", |
128 |
|
|
format->cbSize); |
129 |
stargo |
492 |
readcnt = MAX_CBSIZE; |
130 |
|
|
discardcnt = format->cbSize - MAX_CBSIZE; |
131 |
|
|
} |
132 |
|
|
in_uint8a(in, format->cb, readcnt); |
133 |
|
|
in_uint8s(in, discardcnt); |
134 |
|
|
|
135 |
stargo |
1255 |
if (device_available && current_driver->wave_out_format_supported(format)) |
136 |
matthewc |
474 |
{ |
137 |
|
|
format_count++; |
138 |
|
|
if (format_count == MAX_FORMATS) |
139 |
|
|
break; |
140 |
|
|
} |
141 |
|
|
} |
142 |
|
|
} |
143 |
|
|
|
144 |
astrand |
499 |
out = rdpsnd_init_packet(RDPSND_NEGOTIATE | 0x200, 20 + 18 * format_count); |
145 |
|
|
out_uint32_le(out, 3); /* flags */ |
146 |
matthewc |
474 |
out_uint32(out, 0xffffffff); /* volume */ |
147 |
astrand |
499 |
out_uint32(out, 0); /* pitch */ |
148 |
|
|
out_uint16(out, 0); /* UDP port */ |
149 |
matthewc |
474 |
|
150 |
|
|
out_uint16_le(out, format_count); |
151 |
astrand |
499 |
out_uint8(out, 0x95); /* pad? */ |
152 |
|
|
out_uint16_le(out, 2); /* status */ |
153 |
|
|
out_uint8(out, 0x77); /* pad? */ |
154 |
matthewc |
474 |
|
155 |
|
|
for (i = 0; i < format_count; i++) |
156 |
|
|
{ |
157 |
|
|
format = &formats[i]; |
158 |
|
|
out_uint16_le(out, format->wFormatTag); |
159 |
|
|
out_uint16_le(out, format->nChannels); |
160 |
|
|
out_uint32_le(out, format->nSamplesPerSec); |
161 |
|
|
out_uint32_le(out, format->nAvgBytesPerSec); |
162 |
|
|
out_uint16_le(out, format->nBlockAlign); |
163 |
|
|
out_uint16_le(out, format->wBitsPerSample); |
164 |
astrand |
499 |
out_uint16(out, 0); /* cbSize */ |
165 |
matthewc |
474 |
} |
166 |
|
|
|
167 |
|
|
s_mark_end(out); |
168 |
|
|
rdpsnd_send(out); |
169 |
|
|
} |
170 |
|
|
|
171 |
astrand |
665 |
static void |
172 |
stargo |
1044 |
rdpsnd_process_servertick(STREAM in) |
173 |
matthewc |
474 |
{ |
174 |
stargo |
1044 |
uint16 tick1, tick2; |
175 |
matthewc |
474 |
STREAM out; |
176 |
|
|
|
177 |
|
|
/* in_uint8s(in, 4); unknown */ |
178 |
stargo |
1044 |
in_uint16_le(in, tick1); |
179 |
|
|
in_uint16_le(in, tick2); |
180 |
matthewc |
474 |
|
181 |
stargo |
1044 |
out = rdpsnd_init_packet(RDPSND_SERVERTICK | 0x2300, 4); |
182 |
|
|
out_uint16_le(out, tick1); |
183 |
|
|
out_uint16_le(out, tick2); |
184 |
matthewc |
474 |
s_mark_end(out); |
185 |
|
|
rdpsnd_send(out); |
186 |
|
|
} |
187 |
|
|
|
188 |
astrand |
665 |
static void |
189 |
matthewc |
432 |
rdpsnd_process(STREAM s) |
190 |
|
|
{ |
191 |
matthewc |
474 |
uint8 type; |
192 |
|
|
uint16 datalen; |
193 |
stargo |
491 |
uint32 volume; |
194 |
matthewc |
474 |
static uint16 tick, format; |
195 |
|
|
static uint8 packet_index; |
196 |
|
|
static BOOL awaiting_data_packet; |
197 |
|
|
|
198 |
|
|
#ifdef RDPSND_DEBUG |
199 |
|
|
printf("RDPSND recv:\n"); |
200 |
astrand |
499 |
hexdump(s->p, s->end - s->p); |
201 |
matthewc |
474 |
#endif |
202 |
|
|
|
203 |
|
|
if (awaiting_data_packet) |
204 |
|
|
{ |
205 |
|
|
if (format >= MAX_FORMATS) |
206 |
|
|
{ |
207 |
|
|
error("RDPSND: Invalid format index\n"); |
208 |
|
|
return; |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
if (!device_open || (format != current_format)) |
212 |
|
|
{ |
213 |
stargo |
1255 |
if (!device_open && !current_driver->wave_out_open()) |
214 |
matthewc |
474 |
{ |
215 |
|
|
rdpsnd_send_completion(tick, packet_index); |
216 |
|
|
return; |
217 |
|
|
} |
218 |
stargo |
1255 |
if (!current_driver->wave_out_set_format(&formats[format])) |
219 |
matthewc |
474 |
{ |
220 |
|
|
rdpsnd_send_completion(tick, packet_index); |
221 |
stargo |
1255 |
current_driver->wave_out_close(); |
222 |
matthewc |
474 |
device_open = False; |
223 |
|
|
return; |
224 |
|
|
} |
225 |
|
|
device_open = True; |
226 |
|
|
current_format = format; |
227 |
|
|
} |
228 |
|
|
|
229 |
stargo |
1255 |
current_driver->wave_out_write(s, tick, packet_index); |
230 |
matthewc |
474 |
awaiting_data_packet = False; |
231 |
|
|
return; |
232 |
|
|
} |
233 |
|
|
|
234 |
|
|
in_uint8(s, type); |
235 |
astrand |
499 |
in_uint8s(s, 1); /* unknown? */ |
236 |
matthewc |
474 |
in_uint16_le(s, datalen); |
237 |
|
|
|
238 |
|
|
switch (type) |
239 |
|
|
{ |
240 |
astrand |
499 |
case RDPSND_WRITE: |
241 |
|
|
in_uint16_le(s, tick); |
242 |
|
|
in_uint16_le(s, format); |
243 |
|
|
in_uint8(s, packet_index); |
244 |
|
|
awaiting_data_packet = True; |
245 |
|
|
break; |
246 |
|
|
case RDPSND_CLOSE: |
247 |
stargo |
1255 |
current_driver->wave_out_close(); |
248 |
astrand |
499 |
device_open = False; |
249 |
|
|
break; |
250 |
|
|
case RDPSND_NEGOTIATE: |
251 |
|
|
rdpsnd_process_negotiate(s); |
252 |
|
|
break; |
253 |
stargo |
1044 |
case RDPSND_SERVERTICK: |
254 |
|
|
rdpsnd_process_servertick(s); |
255 |
astrand |
499 |
break; |
256 |
|
|
case RDPSND_SET_VOLUME: |
257 |
|
|
in_uint32(s, volume); |
258 |
|
|
if (device_open) |
259 |
|
|
{ |
260 |
stargo |
1255 |
current_driver->wave_out_volume((volume & 0xffff), |
261 |
|
|
(volume & 0xffff0000) >> 16); |
262 |
astrand |
499 |
} |
263 |
|
|
break; |
264 |
|
|
default: |
265 |
|
|
unimpl("RDPSND packet type %d\n", type); |
266 |
|
|
break; |
267 |
matthewc |
474 |
} |
268 |
matthewc |
432 |
} |
269 |
|
|
|
270 |
|
|
BOOL |
271 |
|
|
rdpsnd_init(void) |
272 |
|
|
{ |
273 |
astrand |
435 |
rdpsnd_channel = |
274 |
|
|
channel_register("rdpsnd", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP, |
275 |
|
|
rdpsnd_process); |
276 |
stargo |
1254 |
|
277 |
matthewc |
432 |
return (rdpsnd_channel != NULL); |
278 |
|
|
} |
279 |
stargo |
1254 |
|
280 |
stargo |
1255 |
BOOL |
281 |
|
|
rdpsnd_auto_open(void) |
282 |
|
|
{ |
283 |
|
|
current_driver = drivers; |
284 |
|
|
while (current_driver != NULL) |
285 |
|
|
{ |
286 |
|
|
DEBUG(("trying %s...\n", current_driver->name)); |
287 |
|
|
if (current_driver->wave_out_open()) |
288 |
|
|
{ |
289 |
|
|
DEBUG(("selected %s\n", current_driver->name)); |
290 |
|
|
return True; |
291 |
|
|
} |
292 |
|
|
g_dsp_fd = 0; |
293 |
|
|
current_driver = current_driver->next; |
294 |
|
|
} |
295 |
|
|
|
296 |
|
|
warning("no working audio-driver found\n"); |
297 |
|
|
|
298 |
|
|
return False; |
299 |
|
|
} |
300 |
|
|
|
301 |
stargo |
1254 |
void |
302 |
stargo |
1255 |
rdpsnd_register_drivers(char *options) |
303 |
|
|
{ |
304 |
|
|
struct audio_driver **reg; |
305 |
|
|
|
306 |
|
|
/* The order of registrations define the probe-order |
307 |
|
|
when opening the device for the first time */ |
308 |
|
|
reg = &drivers; |
309 |
|
|
#if defined(RDPSND_ALSA) |
310 |
|
|
*reg = alsa_register(options); |
311 |
|
|
reg = &((*reg)->next); |
312 |
|
|
#endif |
313 |
|
|
#if defined(RDPSND_OSS) |
314 |
|
|
*reg = oss_register(options); |
315 |
|
|
reg = &((*reg)->next); |
316 |
|
|
#endif |
317 |
|
|
#if defined(RDPSND_SUN) |
318 |
|
|
*reg = sun_register(options); |
319 |
|
|
reg = &((*reg)->next); |
320 |
|
|
#endif |
321 |
|
|
#if defined(RDPSND_SGI) |
322 |
|
|
*reg = sgi_register(options); |
323 |
|
|
reg = &((*reg)->next); |
324 |
|
|
#endif |
325 |
|
|
#if defined(RDPSND_LIBAO) |
326 |
|
|
*reg = libao_register(options); |
327 |
|
|
reg = &((*reg)->next); |
328 |
|
|
#endif |
329 |
|
|
} |
330 |
|
|
|
331 |
|
|
BOOL |
332 |
|
|
rdpsnd_select_driver(char *driver, char *options) |
333 |
|
|
{ |
334 |
|
|
static struct audio_driver auto_driver; |
335 |
|
|
struct audio_driver *pos; |
336 |
|
|
|
337 |
|
|
drivers = NULL; |
338 |
|
|
rdpsnd_register_drivers(options); |
339 |
|
|
|
340 |
|
|
if (!driver) |
341 |
|
|
{ |
342 |
|
|
auto_driver.wave_out_open = &rdpsnd_auto_open; |
343 |
|
|
current_driver = &auto_driver; |
344 |
|
|
return True; |
345 |
|
|
} |
346 |
|
|
|
347 |
|
|
pos = drivers; |
348 |
|
|
while (pos != NULL) |
349 |
|
|
{ |
350 |
|
|
if (!strcmp(pos->name, driver)) |
351 |
|
|
{ |
352 |
|
|
DEBUG(("selected %s\n", pos->name)); |
353 |
|
|
current_driver = pos; |
354 |
|
|
return True; |
355 |
|
|
} |
356 |
|
|
pos = pos->next; |
357 |
|
|
} |
358 |
|
|
return False; |
359 |
|
|
} |
360 |
|
|
|
361 |
|
|
void |
362 |
|
|
rdpsnd_show_help(void) |
363 |
|
|
{ |
364 |
|
|
struct audio_driver *pos; |
365 |
|
|
|
366 |
|
|
rdpsnd_register_drivers(NULL); |
367 |
|
|
|
368 |
|
|
pos = drivers; |
369 |
|
|
while (pos != NULL) |
370 |
|
|
{ |
371 |
|
|
fprintf(stderr, " %s:\t%s\n", pos->name, pos->description); |
372 |
|
|
pos = pos->next; |
373 |
|
|
} |
374 |
|
|
} |
375 |
|
|
|
376 |
|
|
inline void |
377 |
|
|
rdpsnd_play(void) |
378 |
|
|
{ |
379 |
|
|
current_driver->wave_out_play(); |
380 |
|
|
} |
381 |
|
|
|
382 |
|
|
void |
383 |
stargo |
1254 |
rdpsnd_queue_write(STREAM s, uint16 tick, uint8 index) |
384 |
|
|
{ |
385 |
|
|
struct audio_packet *packet = &packet_queue[queue_hi]; |
386 |
|
|
unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE; |
387 |
|
|
|
388 |
|
|
if (next_hi == queue_lo) |
389 |
|
|
{ |
390 |
|
|
error("No space to queue audio packet\n"); |
391 |
|
|
return; |
392 |
|
|
} |
393 |
|
|
|
394 |
|
|
queue_hi = next_hi; |
395 |
|
|
|
396 |
|
|
packet->s = *s; |
397 |
stargo |
1258 |
packet->s.data = |
398 |
|
|
rdpsnd_dsp_process(s->data, s->size, current_driver, &formats[current_format]); |
399 |
|
|
packet->s.p = packet->s.data + 4; |
400 |
|
|
packet->s.end = packet->s.data + s->size; |
401 |
stargo |
1254 |
packet->tick = tick; |
402 |
|
|
packet->index = index; |
403 |
|
|
|
404 |
stargo |
1258 |
#if 0 /* Handled by DSP */ |
405 |
stargo |
1254 |
/* we steal the data buffer from s, give it a new one */ |
406 |
stargo |
1258 |
s->data = xmalloc(s->size); |
407 |
|
|
#endif |
408 |
stargo |
1254 |
|
409 |
|
|
if (!g_dsp_busy) |
410 |
stargo |
1255 |
current_driver->wave_out_play(); |
411 |
stargo |
1254 |
} |
412 |
|
|
|
413 |
|
|
inline struct audio_packet * |
414 |
|
|
rdpsnd_queue_current_packet(void) |
415 |
|
|
{ |
416 |
|
|
return &packet_queue[queue_lo]; |
417 |
|
|
} |
418 |
|
|
|
419 |
|
|
inline BOOL |
420 |
|
|
rdpsnd_queue_empty(void) |
421 |
|
|
{ |
422 |
|
|
return (queue_lo == queue_hi); |
423 |
|
|
} |
424 |
|
|
|
425 |
|
|
inline void |
426 |
|
|
rdpsnd_queue_init(void) |
427 |
|
|
{ |
428 |
|
|
queue_lo = queue_hi = 0; |
429 |
|
|
} |
430 |
|
|
|
431 |
|
|
inline void |
432 |
|
|
rdpsnd_queue_next(void) |
433 |
|
|
{ |
434 |
stargo |
1258 |
xfree(packet_queue[queue_lo].s.data); |
435 |
stargo |
1254 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
436 |
|
|
} |
437 |
|
|
|
438 |
|
|
inline int |
439 |
|
|
rdpsnd_queue_next_tick(void) |
440 |
|
|
{ |
441 |
|
|
if (((queue_lo + 1) % MAX_QUEUE) != queue_hi) |
442 |
|
|
{ |
443 |
|
|
return packet_queue[(queue_lo + 1) % MAX_QUEUE].tick; |
444 |
|
|
} |
445 |
|
|
else |
446 |
|
|
{ |
447 |
|
|
return (packet_queue[queue_lo].tick + 65535) % 65536; |
448 |
|
|
} |
449 |
|
|
} |