--- sourceforge.net/trunk/rdesktop/rdpsnd_sgi.c 2004/08/09 13:03:32 746 +++ sourceforge.net/trunk/rdesktop/rdpsnd_sgi.c 2006/09/17 11:04:50 1255 @@ -1,9 +1,9 @@ -/* +/* -*- c-basic-offset: 8 -*- rdesktop: A Remote Desktop Protocol client. Sound Channel Process Functions - SGI/IRIX Copyright (C) Matthew Chapman 2003 Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003 - Copyright (C) Michael Gernoth mike@zerfleddert.de 2003 + Copyright (C) Jeremy Meng void.foo@gmail.com 2004, 2005 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -24,106 +24,101 @@ #include #include -#define IRIX_DEBUG 1 +/* #define IRIX_DEBUG 1 */ #define IRIX_MAX_VOL 65535 -#define MAX_QUEUE 10 - -int g_dsp_fd; ALconfig audioconfig; ALport output_port; -BOOL g_dsp_busy = False; static BOOL g_swapaudio; static int g_snd_rate; static BOOL g_swapaudio; -static short g_samplewidth; static int width = AL_SAMPLE_16; +static char *sgi_output_device = NULL; double min_volume, max_volume, volume_range; int resource, maxFillable; int combinedFrameSize; -static struct audio_packet -{ - struct stream s; - uint16 tick; - uint8 index; -} packet_queue[MAX_QUEUE]; -static unsigned int queue_hi, queue_lo; - BOOL -wave_out_open(void) +sgi_open(void) { ALparamInfo pinfo; + static int warned = 0; #if (defined(IRIX_DEBUG)) - fprintf(stderr, "wave_out_open: begin\n"); + fprintf(stderr, "sgi_open: begin\n"); #endif + if (!warned && sgi_output_device) + { + warning("device-options not supported for libao-driver\n"); + warned = 1; + } + if (alGetParamInfo(AL_DEFAULT_OUTPUT, AL_GAIN, &pinfo) < 0) { - fprintf(stderr, "wave_out_open: alGetParamInfo failed: %s\n", + fprintf(stderr, "sgi_open: alGetParamInfo failed: %s\n", alGetErrorString(oserror())); } min_volume = alFixedToDouble(pinfo.min.ll); max_volume = alFixedToDouble(pinfo.max.ll); volume_range = (max_volume - min_volume); #if (defined(IRIX_DEBUG)) - fprintf(stderr, "wave_out_open: minvol = %lf, maxvol= %lf, range = %lf.\n", + fprintf(stderr, "sgi_open: minvol = %lf, maxvol= %lf, range = %lf.\n", min_volume, max_volume, volume_range); #endif - queue_lo = queue_hi = 0; + rdpsnd_queue_init(); audioconfig = alNewConfig(); - if (audioconfig < 0) + if (audioconfig == (ALconfig) 0) { - fprintf(stderr, "wave_out_open: alNewConfig failed: %s\n", - alGetErrorString(oserror())); + fprintf(stderr, "sgi_open: alNewConfig failed: %s\n", alGetErrorString(oserror())); return False; } output_port = alOpenPort("rdpsnd", "w", 0); if (output_port == (ALport) 0) { - fprintf(stderr, "wave_out_open: alOpenPort failed: %s\n", - alGetErrorString(oserror())); + fprintf(stderr, "sgi_open: alOpenPort failed: %s\n", alGetErrorString(oserror())); return False; } #if (defined(IRIX_DEBUG)) - fprintf(stderr, "wave_out_open: returning\n"); + fprintf(stderr, "sgi_open: returning\n"); #endif return True; } void -wave_out_close(void) +sgi_close(void) { /* Ack all remaining packets */ #if (defined(IRIX_DEBUG)) - fprintf(stderr, "wave_out_close: begin\n"); + fprintf(stderr, "sgi_close: begin\n"); #endif - while (queue_lo != queue_hi) + while (!rdpsnd_queue_empty()) { - rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index); - free(packet_queue[queue_lo].s.data); - queue_lo = (queue_lo + 1) % MAX_QUEUE; + /* We need to add 50 to tell windows that time has passed while + * playing this packet */ + rdpsnd_send_completion(rdpsnd_queue_current_packet()->tick + 50, + rdpsnd_queue_current_packet()->index); + rdpsnd_queue_next(); } alDiscardFrames(output_port, 0); alClosePort(output_port); alFreeConfig(audioconfig); #if (defined(IRIX_DEBUG)) - fprintf(stderr, "wave_out_close: returning\n"); + fprintf(stderr, "sgi_close: returning\n"); #endif } BOOL -wave_out_format_supported(WAVEFORMATEX * pwfx) +sgi_format_supported(WAVEFORMATEX * pwfx) { if (pwfx->wFormatTag != WAVE_FORMAT_PCM) return False; @@ -136,49 +131,54 @@ } BOOL -wave_out_set_format(WAVEFORMATEX * pwfx) +sgi_set_format(WAVEFORMATEX * pwfx) { int channels; int frameSize, channelCount; ALpv params; #if (defined(IRIX_DEBUG)) - fprintf(stderr, "wave_out_set_format: init...\n"); + fprintf(stderr, "sgi_set_format: init...\n"); #endif - /* limited support to configure an opened audio port in IRIX */ - /* have to reopen the audio port, using same config */ - alClosePort(output_port); g_swapaudio = False; - if (pwfx->wBitsPerSample == 8) width = AL_SAMPLE_8; else if (pwfx->wBitsPerSample == 16) { width = AL_SAMPLE_16; /* Do we need to swap the 16bit values? (Are we BigEndian) */ -#if (defined(IRIX_DEBUG)) +#if (defined(B_ENDIAN)) g_swapaudio = 1; #else g_swapaudio = 0; #endif } - g_samplewidth = pwfx->wBitsPerSample / 8; + /* Limited support to configure an opened audio port in IRIX. The + number of channels is a static setting and can not be changed after + a port is opened. So if the number of channels remains the same, we + can configure other settings; otherwise we have to reopen the audio + port, using same config. */ + channels = pwfx->nChannels; g_snd_rate = pwfx->nSamplesPerSec; alSetSampFmt(audioconfig, AL_SAMPFMT_TWOSCOMP); alSetWidth(audioconfig, width); - alSetChannels(audioconfig, channels); + if (channels != alGetChannels(audioconfig)) + { + alClosePort(output_port); + alSetChannels(audioconfig, channels); + output_port = alOpenPort("rdpsnd", "w", audioconfig); - output_port = alOpenPort("rdpsnd", "w", audioconfig); + if (output_port == (ALport) 0) + { + fprintf(stderr, "sgi_set_format: alOpenPort failed: %s\n", + alGetErrorString(oserror())); + return False; + } - if (output_port == (ALport) 0) - { - fprintf(stderr, "wave_out_set_format: alOpenPort failed: %s\n", - alGetErrorString(oserror())); - return False; } resource = alGetResource(output_port); @@ -188,7 +188,7 @@ if (frameSize == 0 || channelCount == 0) { - fprintf(stderr, "wave_out_set_format: bad frameSize or channelCount\n"); + fprintf(stderr, "sgi_set_format: bad frameSize or channelCount\n"); return False; } combinedFrameSize = frameSize * channelCount; @@ -209,20 +209,20 @@ } #if (defined(IRIX_DEBUG)) - fprintf(stderr, "wave_out_set_format: returning...\n"); + fprintf(stderr, "sgi_set_format: returning...\n"); #endif return True; } void -wave_out_volume(uint16 left, uint16 right) +sgi_volume(uint16 left, uint16 right) { double gainleft, gainright; ALpv pv[1]; ALfixed gain[8]; #if (defined(IRIX_DEBUG)) - fprintf(stderr, "wave_out_volume: begin\n"); + fprintf(stderr, "sgi_volume: begin\n"); fprintf(stderr, "left='%d', right='%d'\n", left, right); #endif @@ -237,67 +237,36 @@ pv[0].sizeIn = 8; if (alSetParams(AL_DEFAULT_OUTPUT, pv, 1) < 0) { - fprintf(stderr, "wave_out_volume: alSetParams failed: %s\n", + fprintf(stderr, "sgi_volume: alSetParams failed: %s\n", alGetErrorString(oserror())); return; } #if (defined(IRIX_DEBUG)) - fprintf(stderr, "wave_out_volume: returning\n"); + fprintf(stderr, "sgi_volume: returning\n"); #endif } void -wave_out_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) - { - fprintf(stderr, "No space to queue audio packet\n"); - return; - } - - queue_hi = next_hi; - - packet->s = *s; - packet->tick = tick; - packet->index = index; - packet->s.p += 4; - - /* we steal the data buffer from s, give it a new one */ - s->data = malloc(s->size); - - if (!g_dsp_busy) - wave_out_play(); -} - -void -wave_out_play(void) +sgi_play(void) { struct audio_packet *packet; ssize_t len; unsigned int i; uint8 swap; STREAM out; - static long startedat_us; - static long startedat_s; - static BOOL started = False; static BOOL swapped = False; - struct timeval tv; int gf; - static long long temp; while (1) { - if (queue_lo == queue_hi) + if (rdpsnd_queue_empty()) { g_dsp_busy = False; return; } - packet = &packet_queue[queue_lo]; + packet = rdpsnd_queue_current_packet(); out = &packet->s; /* Swap the current packet, but only once */ @@ -312,52 +281,52 @@ swapped = True; } - if (!started) - { - gettimeofday(&tv, NULL); - startedat_us = tv.tv_usec; - startedat_s = tv.tv_sec; - started = True; - } - len = out->end - out->p; - gf = alGetFillable(output_port); - if (len > gf) - { - //len = gf * combinedFrameSize; -#if (defined(IRIX_DEBUG)) - //fprintf(stderr,"Fillable...\n"); -#endif - } alWriteFrames(output_port, out->p, len / combinedFrameSize); out->p += len; if (out->p == out->end) { - long long duration; - long elapsed; - - gettimeofday(&tv, NULL); - duration = (out->size * (1000000 / (g_samplewidth * g_snd_rate))); - elapsed = (tv.tv_sec - startedat_s) * 1000000 + (tv.tv_usec - startedat_us); - /* 7/10 is not good for IRIX audio port, 4x/100 is suitable */ - if (elapsed >= (duration * 485) / 1000) + gf = alGetFilled(output_port); + if (gf < (4 * maxFillable / 10)) { rdpsnd_send_completion(packet->tick, packet->index); - free(out->data); - queue_lo = (queue_lo + 1) % MAX_QUEUE; - started = False; + rdpsnd_queue_next(); swapped = False; } else { #if (defined(IRIX_DEBUG)) - //fprintf(stderr,"Busy playing...\n"); +/* fprintf(stderr,"Busy playing...\n"); */ #endif g_dsp_busy = True; + usleep(10); return; } } } } + +static struct audio_driver sgi_driver = { + wave_out_write:rdpsnd_queue_write, + wave_out_open:sgi_open, + wave_out_close:sgi_close, + wave_out_format_supported:sgi_format_supported, + wave_out_set_format:sgi_set_format, + wave_out_volume:sgi_volume, + wave_out_play:sgi_play, + name:"sgi", + description:"SGI output driver", + next:NULL, +}; + +struct audio_driver * +sgi_register(char *options) +{ + if (options) + { + sgi_output_device = xstrdup(options); + } + return &sgi_driver; +}