--- sourceforge.net/trunk/rdesktop/rdpsnd.c 2006/02/08 12:10:41 1044 +++ sourceforge.net/trunk/rdesktop/rdpsnd.c 2006/09/17 19:08:14 1264 @@ -20,6 +20,8 @@ */ #include "rdesktop.h" +#include "rdpsnd.h" +#include "rdpsnd_dsp.h" #define RDPSND_CLOSE 1 #define RDPSND_WRITE 2 @@ -30,13 +32,23 @@ #define RDPSND_NEGOTIATE 7 #define MAX_FORMATS 10 +#define MAX_QUEUE 10 + +BOOL g_dsp_busy = False; +int g_dsp_fd; static VCHANNEL *rdpsnd_channel; +static struct audio_driver *drivers = NULL; +struct audio_driver *current_driver = NULL; static BOOL device_open; static WAVEFORMATEX formats[MAX_FORMATS]; static unsigned int format_count; static unsigned int current_format; +unsigned int queue_hi, queue_lo; +struct audio_packet packet_queue[MAX_QUEUE]; + +void (*wave_out_play) (void); static STREAM rdpsnd_init_packet(uint16 type, uint16 size) @@ -66,7 +78,7 @@ STREAM s; s = rdpsnd_init_packet(RDPSND_COMPLETION, 4); - out_uint16_le(s, tick + 50); + out_uint16_le(s, tick); out_uint8(s, packet_index); out_uint8(s, 0); s_mark_end(s); @@ -87,9 +99,9 @@ in_uint16_le(in, in_format_count); in_uint8s(in, 4); /* pad, status, pad */ - if (wave_out_open()) + if (current_driver->wave_out_open()) { - wave_out_close(); + current_driver->wave_out_close(); device_available = True; } @@ -120,7 +132,7 @@ in_uint8a(in, format->cb, readcnt); in_uint8s(in, discardcnt); - if (device_available && wave_out_format_supported(format)) + if (device_available && current_driver->wave_out_format_supported(format)) { format_count++; if (format_count == MAX_FORMATS) @@ -198,15 +210,15 @@ if (!device_open || (format != current_format)) { - if (!device_open && !wave_out_open()) + if (!device_open && !current_driver->wave_out_open()) { rdpsnd_send_completion(tick, packet_index); return; } - if (!wave_out_set_format(&formats[format])) + if (!current_driver->wave_out_set_format(&formats[format])) { rdpsnd_send_completion(tick, packet_index); - wave_out_close(); + current_driver->wave_out_close(); device_open = False; return; } @@ -214,7 +226,10 @@ current_format = format; } - wave_out_write(s, tick, packet_index); + current_driver-> + wave_out_write(rdpsnd_dsp_process + (s, current_driver, &formats[current_format]), tick, + packet_index); awaiting_data_packet = False; return; } @@ -232,7 +247,7 @@ awaiting_data_packet = True; break; case RDPSND_CLOSE: - wave_out_close(); + current_driver->wave_out_close(); device_open = False; break; case RDPSND_NEGOTIATE: @@ -245,7 +260,8 @@ in_uint32(s, volume); if (device_open) { - wave_out_volume((volume & 0xffff), (volume & 0xffff0000) >> 16); + current_driver->wave_out_volume((volume & 0xffff), + (volume & 0xffff0000) >> 16); } break; default: @@ -260,5 +276,168 @@ rdpsnd_channel = channel_register("rdpsnd", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP, rdpsnd_process); + return (rdpsnd_channel != NULL); } + +BOOL +rdpsnd_auto_open(void) +{ + current_driver = drivers; + while (current_driver != NULL) + { + DEBUG(("trying %s...\n", current_driver->name)); + if (current_driver->wave_out_open()) + { + DEBUG(("selected %s\n", current_driver->name)); + return True; + } + g_dsp_fd = 0; + current_driver = current_driver->next; + } + + warning("no working audio-driver found\n"); + + return False; +} + +void +rdpsnd_register_drivers(char *options) +{ + struct audio_driver **reg; + + /* The order of registrations define the probe-order + when opening the device for the first time */ + reg = &drivers; +#if defined(RDPSND_ALSA) + *reg = alsa_register(options); + reg = &((*reg)->next); +#endif +#if defined(RDPSND_SUN) + *reg = sun_register(options); + reg = &((*reg)->next); +#endif +#if defined(RDPSND_OSS) + *reg = oss_register(options); + reg = &((*reg)->next); +#endif +#if defined(RDPSND_SGI) + *reg = sgi_register(options); + reg = &((*reg)->next); +#endif +#if defined(RDPSND_LIBAO) + *reg = libao_register(options); + reg = &((*reg)->next); +#endif +} + +BOOL +rdpsnd_select_driver(char *driver, char *options) +{ + static struct audio_driver auto_driver; + struct audio_driver *pos; + + drivers = NULL; + rdpsnd_register_drivers(options); + + if (!driver) + { + auto_driver.wave_out_open = &rdpsnd_auto_open; + current_driver = &auto_driver; + return True; + } + + pos = drivers; + while (pos != NULL) + { + if (!strcmp(pos->name, driver)) + { + DEBUG(("selected %s\n", pos->name)); + current_driver = pos; + return True; + } + pos = pos->next; + } + return False; +} + +void +rdpsnd_show_help(void) +{ + struct audio_driver *pos; + + rdpsnd_register_drivers(NULL); + + pos = drivers; + while (pos != NULL) + { + fprintf(stderr, " %s:\t%s\n", pos->name, pos->description); + pos = pos->next; + } +} + +inline void +rdpsnd_play(void) +{ + current_driver->wave_out_play(); +} + +void +rdpsnd_queue_write(STREAM s, uint16 tick, uint8 index) +{ + struct audio_packet *packet = &packet_queue[queue_hi]; + unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE; + + if (next_hi == queue_lo) + { + error("No space to queue audio packet\n"); + return; + } + + queue_hi = next_hi; + + packet->s = *s; + packet->tick = tick; + packet->index = index; + + if (!g_dsp_busy) + current_driver->wave_out_play(); +} + +inline struct audio_packet * +rdpsnd_queue_current_packet(void) +{ + return &packet_queue[queue_lo]; +} + +inline BOOL +rdpsnd_queue_empty(void) +{ + return (queue_lo == queue_hi); +} + +inline void +rdpsnd_queue_init(void) +{ + queue_lo = queue_hi = 0; +} + +inline void +rdpsnd_queue_next(void) +{ + xfree(packet_queue[queue_lo].s.data); + queue_lo = (queue_lo + 1) % MAX_QUEUE; +} + +inline int +rdpsnd_queue_next_tick(void) +{ + if (((queue_lo + 1) % MAX_QUEUE) != queue_hi) + { + return packet_queue[(queue_lo + 1) % MAX_QUEUE].tick; + } + else + { + return (packet_queue[queue_lo].tick + 65535) % 65536; + } +}