19 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
*/ |
*/ |
21 |
|
|
22 |
|
#include <assert.h> |
23 |
|
|
24 |
#include "rdesktop.h" |
#include "rdesktop.h" |
25 |
#include "rdpsnd.h" |
#include "rdpsnd.h" |
26 |
#include "rdpsnd_dsp.h" |
#include "rdpsnd_dsp.h" |
30 |
#define RDPSND_SET_VOLUME 3 |
#define RDPSND_SET_VOLUME 3 |
31 |
#define RDPSND_UNKNOWN4 4 |
#define RDPSND_UNKNOWN4 4 |
32 |
#define RDPSND_COMPLETION 5 |
#define RDPSND_COMPLETION 5 |
33 |
#define RDPSND_SERVERTICK 6 |
#define RDPSND_PING 6 |
34 |
#define RDPSND_NEGOTIATE 7 |
#define RDPSND_NEGOTIATE 7 |
35 |
|
|
36 |
#define MAX_FORMATS 10 |
#define MAX_FORMATS 10 |
47 |
static WAVEFORMATEX formats[MAX_FORMATS]; |
static WAVEFORMATEX formats[MAX_FORMATS]; |
48 |
static unsigned int format_count; |
static unsigned int format_count; |
49 |
static unsigned int current_format; |
static unsigned int current_format; |
50 |
unsigned int queue_hi, queue_lo; |
unsigned int queue_hi, queue_lo, queue_pending; |
51 |
struct audio_packet packet_queue[MAX_QUEUE]; |
struct audio_packet packet_queue[MAX_QUEUE]; |
52 |
|
|
53 |
void (*wave_out_play) (void); |
void (*wave_out_play) (void); |
54 |
|
|
55 |
|
static void rdpsnd_queue_write(STREAM s, uint16 tick, uint8 index); |
56 |
|
static void rdpsnd_queue_init(void); |
57 |
|
static void rdpsnd_queue_complete_pending(void); |
58 |
|
static long rdpsnd_queue_next_completion(void); |
59 |
|
|
60 |
static STREAM |
static STREAM |
61 |
rdpsnd_init_packet(uint16 type, uint16 size) |
rdpsnd_init_packet(uint16 type, uint16 size) |
62 |
{ |
{ |
79 |
channel_send(s, rdpsnd_channel); |
channel_send(s, rdpsnd_channel); |
80 |
} |
} |
81 |
|
|
82 |
void |
static void |
83 |
rdpsnd_send_completion(uint16 tick, uint8 packet_index) |
rdpsnd_send_completion(uint16 tick, uint8 packet_index) |
84 |
{ |
{ |
85 |
STREAM s; |
STREAM s; |
95 |
static void |
static void |
96 |
rdpsnd_process_negotiate(STREAM in) |
rdpsnd_process_negotiate(STREAM in) |
97 |
{ |
{ |
98 |
unsigned int in_format_count, i; |
uint16 in_format_count, i; |
99 |
|
uint8 pad; |
100 |
|
uint16 version; |
101 |
WAVEFORMATEX *format; |
WAVEFORMATEX *format; |
102 |
STREAM out; |
STREAM out; |
103 |
BOOL device_available = False; |
BOOL device_available = False; |
104 |
int readcnt; |
int readcnt; |
105 |
int discardcnt; |
int discardcnt; |
106 |
|
|
107 |
in_uint8s(in, 14); /* flags, volume, pitch, UDP port */ |
in_uint8s(in, 14); /* initial bytes not valid from server */ |
108 |
in_uint16_le(in, in_format_count); |
in_uint16_le(in, in_format_count); |
109 |
in_uint8s(in, 4); /* pad, status, pad */ |
in_uint8(in, pad); |
110 |
|
in_uint16_le(in, version); |
111 |
|
in_uint8s(in, 1); /* padding */ |
112 |
|
|
113 |
if (current_driver->wave_out_open()) |
if (current_driver->wave_out_open()) |
114 |
{ |
{ |
159 |
out_uint16(out, 0); /* UDP port */ |
out_uint16(out, 0); /* UDP port */ |
160 |
|
|
161 |
out_uint16_le(out, format_count); |
out_uint16_le(out, format_count); |
162 |
out_uint8(out, 0x95); /* pad? */ |
out_uint8(out, 0); /* padding */ |
163 |
out_uint16_le(out, 2); /* status */ |
out_uint16_le(out, 2); /* version */ |
164 |
out_uint8(out, 0x77); /* pad? */ |
out_uint8(out, 0); /* padding */ |
165 |
|
|
166 |
for (i = 0; i < format_count; i++) |
for (i = 0; i < format_count; i++) |
167 |
{ |
{ |
180 |
} |
} |
181 |
|
|
182 |
static void |
static void |
183 |
rdpsnd_process_servertick(STREAM in) |
rdpsnd_process_ping(STREAM in) |
184 |
{ |
{ |
185 |
uint16 tick1, tick2; |
uint16 tick; |
186 |
STREAM out; |
STREAM out; |
187 |
|
|
188 |
/* in_uint8s(in, 4); unknown */ |
in_uint16_le(in, tick); |
189 |
in_uint16_le(in, tick1); |
|
190 |
in_uint16_le(in, tick2); |
out = rdpsnd_init_packet(RDPSND_PING | 0x2300, 4); |
191 |
|
out_uint16_le(out, tick); |
192 |
out = rdpsnd_init_packet(RDPSND_SERVERTICK | 0x2300, 4); |
out_uint16_le(out, 0); |
|
out_uint16_le(out, tick1); |
|
|
out_uint16_le(out, tick2); |
|
193 |
s_mark_end(out); |
s_mark_end(out); |
194 |
rdpsnd_send(out); |
rdpsnd_send(out); |
195 |
} |
} |
239 |
/* Insert the 4 missing bytes retrieved from last RDPSND_WRITE */ |
/* Insert the 4 missing bytes retrieved from last RDPSND_WRITE */ |
240 |
memcpy(s->data, missing_bytes, 4); |
memcpy(s->data, missing_bytes, 4); |
241 |
|
|
242 |
current_driver-> |
rdpsnd_queue_write(rdpsnd_dsp_process |
243 |
wave_out_write(rdpsnd_dsp_process |
(s, current_driver, &formats[current_format]), tick, |
244 |
(s, current_driver, &formats[current_format]), tick, |
packet_index); |
|
packet_index); |
|
245 |
awaiting_data_packet = False; |
awaiting_data_packet = False; |
246 |
return; |
return; |
247 |
} |
} |
267 |
case RDPSND_NEGOTIATE: |
case RDPSND_NEGOTIATE: |
268 |
rdpsnd_process_negotiate(s); |
rdpsnd_process_negotiate(s); |
269 |
break; |
break; |
270 |
case RDPSND_SERVERTICK: |
case RDPSND_PING: |
271 |
rdpsnd_process_servertick(s); |
rdpsnd_process_ping(s); |
272 |
break; |
break; |
273 |
case RDPSND_SET_VOLUME: |
case RDPSND_SET_VOLUME: |
274 |
in_uint32(s, volume); |
in_uint32(s, volume); |
324 |
reg = &drivers; |
reg = &drivers; |
325 |
#if defined(RDPSND_ALSA) |
#if defined(RDPSND_ALSA) |
326 |
*reg = alsa_register(options); |
*reg = alsa_register(options); |
327 |
|
assert(*reg); |
328 |
reg = &((*reg)->next); |
reg = &((*reg)->next); |
329 |
#endif |
#endif |
330 |
#if defined(RDPSND_SUN) |
#if defined(RDPSND_SUN) |
331 |
*reg = sun_register(options); |
*reg = sun_register(options); |
332 |
|
assert(*reg); |
333 |
reg = &((*reg)->next); |
reg = &((*reg)->next); |
334 |
#endif |
#endif |
335 |
#if defined(RDPSND_OSS) |
#if defined(RDPSND_OSS) |
336 |
*reg = oss_register(options); |
*reg = oss_register(options); |
337 |
|
assert(*reg); |
338 |
reg = &((*reg)->next); |
reg = &((*reg)->next); |
339 |
#endif |
#endif |
340 |
#if defined(RDPSND_SGI) |
#if defined(RDPSND_SGI) |
341 |
*reg = sgi_register(options); |
*reg = sgi_register(options); |
342 |
|
assert(*reg); |
343 |
reg = &((*reg)->next); |
reg = &((*reg)->next); |
344 |
#endif |
#endif |
345 |
#if defined(RDPSND_LIBAO) |
#if defined(RDPSND_LIBAO) |
346 |
*reg = libao_register(options); |
*reg = libao_register(options); |
347 |
|
assert(*reg); |
348 |
reg = &((*reg)->next); |
reg = &((*reg)->next); |
349 |
#endif |
#endif |
350 |
|
*reg = NULL; |
351 |
} |
} |
352 |
|
|
353 |
BOOL |
BOOL |
369 |
return False; |
return False; |
370 |
} |
} |
371 |
|
|
372 |
|
rdpsnd_queue_init(); |
373 |
|
|
374 |
if (optarg != NULL && strlen(optarg) > 0) |
if (optarg != NULL && strlen(optarg) > 0) |
375 |
{ |
{ |
376 |
driver = options = optarg; |
driver = options = optarg; |
432 |
current_driver->wave_out_play(); |
current_driver->wave_out_play(); |
433 |
} |
} |
434 |
|
|
435 |
inline void |
void |
436 |
|
rdpsnd_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv) |
437 |
|
{ |
438 |
|
long next_pending; |
439 |
|
|
440 |
|
if (g_dsp_busy) |
441 |
|
{ |
442 |
|
FD_SET(g_dsp_fd, wfds); |
443 |
|
*n = (g_dsp_fd > *n) ? g_dsp_fd : *n; |
444 |
|
} |
445 |
|
|
446 |
|
next_pending = rdpsnd_queue_next_completion(); |
447 |
|
if (next_pending >= 0) |
448 |
|
{ |
449 |
|
long cur_timeout; |
450 |
|
|
451 |
|
cur_timeout = tv->tv_sec * 1000000 + tv->tv_usec; |
452 |
|
if (cur_timeout > next_pending) |
453 |
|
{ |
454 |
|
tv->tv_sec = next_pending / 1000000; |
455 |
|
tv->tv_usec = next_pending % 1000000; |
456 |
|
} |
457 |
|
} |
458 |
|
} |
459 |
|
|
460 |
|
void |
461 |
|
rdpsnd_check_fds(fd_set * rfds, fd_set * wfds) |
462 |
|
{ |
463 |
|
rdpsnd_queue_complete_pending(); |
464 |
|
|
465 |
|
if (g_dsp_busy && FD_ISSET(g_dsp_fd, wfds)) |
466 |
|
rdpsnd_play(); |
467 |
|
} |
468 |
|
|
469 |
|
static void |
470 |
rdpsnd_queue_write(STREAM s, uint16 tick, uint8 index) |
rdpsnd_queue_write(STREAM s, uint16 tick, uint8 index) |
471 |
{ |
{ |
472 |
struct audio_packet *packet = &packet_queue[queue_hi]; |
struct audio_packet *packet = &packet_queue[queue_hi]; |
473 |
unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE; |
unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE; |
474 |
|
|
475 |
if (next_hi == queue_lo) |
if (next_hi == queue_pending) |
476 |
{ |
{ |
477 |
error("No space to queue audio packet\n"); |
error("No space to queue audio packet\n"); |
478 |
return; |
return; |
484 |
packet->tick = tick; |
packet->tick = tick; |
485 |
packet->index = index; |
packet->index = index; |
486 |
|
|
487 |
|
gettimeofday(&packet->arrive_tv, NULL); |
488 |
|
|
489 |
if (!g_dsp_busy) |
if (!g_dsp_busy) |
490 |
current_driver->wave_out_play(); |
current_driver->wave_out_play(); |
491 |
} |
} |
492 |
|
|
493 |
inline struct audio_packet * |
struct audio_packet * |
494 |
rdpsnd_queue_current_packet(void) |
rdpsnd_queue_current_packet(void) |
495 |
{ |
{ |
496 |
return &packet_queue[queue_lo]; |
return &packet_queue[queue_lo]; |
497 |
} |
} |
498 |
|
|
499 |
inline BOOL |
BOOL |
500 |
rdpsnd_queue_empty(void) |
rdpsnd_queue_empty(void) |
501 |
{ |
{ |
502 |
return (queue_lo == queue_hi); |
return (queue_lo == queue_hi); |
503 |
} |
} |
504 |
|
|
505 |
inline void |
static void |
506 |
rdpsnd_queue_init(void) |
rdpsnd_queue_init(void) |
507 |
{ |
{ |
508 |
queue_lo = queue_hi = 0; |
queue_pending = queue_lo = queue_hi = 0; |
509 |
} |
} |
510 |
|
|
511 |
inline void |
void |
512 |
rdpsnd_queue_next(void) |
rdpsnd_queue_next(unsigned long completed_in_us) |
513 |
{ |
{ |
514 |
xfree(packet_queue[queue_lo].s.data); |
struct audio_packet *packet; |
515 |
|
|
516 |
|
assert(!rdpsnd_queue_empty()); |
517 |
|
|
518 |
|
packet = &packet_queue[queue_lo]; |
519 |
|
|
520 |
|
gettimeofday(&packet->completion_tv, NULL); |
521 |
|
|
522 |
|
packet->completion_tv.tv_usec += completed_in_us; |
523 |
|
packet->completion_tv.tv_sec += packet->completion_tv.tv_usec / 1000000; |
524 |
|
packet->completion_tv.tv_usec %= 1000000; |
525 |
|
|
526 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
527 |
|
|
528 |
|
rdpsnd_queue_complete_pending(); |
529 |
} |
} |
530 |
|
|
531 |
inline int |
int |
532 |
rdpsnd_queue_next_tick(void) |
rdpsnd_queue_next_tick(void) |
533 |
{ |
{ |
534 |
if (((queue_lo + 1) % MAX_QUEUE) != queue_hi) |
if (((queue_lo + 1) % MAX_QUEUE) != queue_hi) |
540 |
return (packet_queue[queue_lo].tick + 65535) % 65536; |
return (packet_queue[queue_lo].tick + 65535) % 65536; |
541 |
} |
} |
542 |
} |
} |
543 |
|
|
544 |
|
static void |
545 |
|
rdpsnd_queue_complete_pending(void) |
546 |
|
{ |
547 |
|
struct timeval now; |
548 |
|
long elapsed; |
549 |
|
struct audio_packet *packet; |
550 |
|
|
551 |
|
gettimeofday(&now, NULL); |
552 |
|
|
553 |
|
while (queue_pending != queue_lo) |
554 |
|
{ |
555 |
|
packet = &packet_queue[queue_pending]; |
556 |
|
|
557 |
|
if (now.tv_sec < packet->completion_tv.tv_sec) |
558 |
|
break; |
559 |
|
|
560 |
|
if ((now.tv_sec == packet->completion_tv.tv_sec) && |
561 |
|
(now.tv_usec < packet->completion_tv.tv_usec)) |
562 |
|
break; |
563 |
|
|
564 |
|
elapsed = (packet->completion_tv.tv_sec - packet->arrive_tv.tv_sec) * 1000000 + |
565 |
|
(packet->completion_tv.tv_usec - packet->arrive_tv.tv_usec); |
566 |
|
elapsed /= 1000; |
567 |
|
|
568 |
|
xfree(packet->s.data); |
569 |
|
rdpsnd_send_completion((packet->tick + elapsed) % 65536, packet->index); |
570 |
|
queue_pending = (queue_pending + 1) % MAX_QUEUE; |
571 |
|
} |
572 |
|
} |
573 |
|
|
574 |
|
static long |
575 |
|
rdpsnd_queue_next_completion(void) |
576 |
|
{ |
577 |
|
struct audio_packet *packet; |
578 |
|
long remaining; |
579 |
|
struct timeval now; |
580 |
|
|
581 |
|
if (queue_pending == queue_lo) |
582 |
|
return -1; |
583 |
|
|
584 |
|
gettimeofday(&now, NULL); |
585 |
|
|
586 |
|
packet = &packet_queue[queue_pending]; |
587 |
|
|
588 |
|
remaining = (packet->completion_tv.tv_sec - now.tv_sec) * 1000000 + |
589 |
|
(packet->completion_tv.tv_usec - now.tv_usec); |
590 |
|
|
591 |
|
if (remaining < 0) |
592 |
|
return 0; |
593 |
|
|
594 |
|
return remaining; |
595 |
|
} |