--- sourceforge.net/trunk/rdesktop/rdpsnd_oss.c 2003/10/13 16:09:45 491 +++ sourceforge.net/trunk/rdesktop/rdpsnd_oss.c 2004/08/13 09:43:31 751 @@ -23,15 +23,19 @@ #include #include #include +#include #include #include #define MAX_QUEUE 10 int g_dsp_fd; -BOOL g_dsp_busy; +BOOL g_dsp_busy = False; +static int g_snd_rate; +static short g_samplewidth; -static struct audio_packet { +static struct audio_packet +{ struct stream s; uint16 tick; uint8 index; @@ -41,16 +45,21 @@ BOOL wave_out_open(void) { - char *dsp_dev = "/dev/dsp"; + char *dsp_dev = getenv("AUDIODEV"); + + if (dsp_dev == NULL) + { + dsp_dev = "/dev/dsp"; + } - if ((g_dsp_fd = open(dsp_dev, O_WRONLY|O_NONBLOCK)) == -1) + if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1) { perror(dsp_dev); return False; } /* Non-blocking so that user interface is responsive */ - fcntl(g_dsp_fd, F_SETFL, fcntl(g_dsp_fd, F_GETFL)|O_NONBLOCK); + fcntl(g_dsp_fd, F_SETFL, fcntl(g_dsp_fd, F_GETFL) | O_NONBLOCK); return True; } @@ -61,7 +70,7 @@ } BOOL -wave_out_format_supported(WAVEFORMATEX *pwfx) +wave_out_format_supported(WAVEFORMATEX * pwfx) { if (pwfx->wFormatTag != WAVE_FORMAT_PCM) return False; @@ -74,9 +83,9 @@ } BOOL -wave_out_set_format(WAVEFORMATEX *pwfx) +wave_out_set_format(WAVEFORMATEX * pwfx) { - int speed, channels, format; + int channels, format; ioctl(g_dsp_fd, SNDCTL_DSP_RESET, NULL); ioctl(g_dsp_fd, SNDCTL_DSP_SYNC, NULL); @@ -86,6 +95,8 @@ else if (pwfx->wBitsPerSample == 16) format = AFMT_S16_LE; + g_samplewidth = pwfx->wBitsPerSample / 8; + if (ioctl(g_dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1) { perror("SNDCTL_DSP_SETFMT"); @@ -101,8 +112,13 @@ return False; } - speed = pwfx->nSamplesPerSec; - if (ioctl(g_dsp_fd, SNDCTL_DSP_SPEED, &speed) == -1) + if (channels == 2) + { + g_samplewidth *= 2; + } + + g_snd_rate = pwfx->nSamplesPerSec; + if (ioctl(g_dsp_fd, SNDCTL_DSP_SPEED, &g_snd_rate) == -1) { perror("SNDCTL_DSP_SPEED"); close(g_dsp_fd); @@ -115,13 +131,34 @@ void wave_out_volume(uint16 left, uint16 right) { + static BOOL use_dev_mixer = False; uint32 volume; + int fd_mix = -1; + + volume = left / (65536 / 100); + volume |= right / (65536 / 100) << 8; + + if (use_dev_mixer) + { + if ((fd_mix = open("/dev/mixer", O_RDWR | O_NONBLOCK)) == -1) + { + perror("open /dev/mixer"); + return; + } + + if (ioctl(fd_mix, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1) + { + perror("MIXER_WRITE(SOUND_MIXER_PCM)"); + return; + } + + close(fd_mix); + } - volume = left/(65536/100); - volume |= right/(65536/100) << 8; if (ioctl(g_dsp_fd, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1) { perror("MIXER_WRITE(SOUND_MIXER_PCM)"); + use_dev_mixer = True; return; } } @@ -158,6 +195,10 @@ struct audio_packet *packet; ssize_t len; STREAM out; + static long startedat_us; + static long startedat_s; + static BOOL started = False; + struct timeval tv; while (1) { @@ -170,7 +211,15 @@ packet = &packet_queue[queue_lo]; out = &packet->s; - len = write(g_dsp_fd, out->p, out->end-out->p); + if (!started) + { + gettimeofday(&tv, NULL); + startedat_us = tv.tv_usec; + startedat_s = tv.tv_sec; + started = True; + } + + len = write(g_dsp_fd, out->p, out->end - out->p); if (len == -1) { if (errno != EWOULDBLOCK) @@ -182,10 +231,25 @@ out->p += len; if (out->p == out->end) { - rdpsnd_send_completion(packet->tick, packet->index); - free(out->data); - queue_lo = (queue_lo + 1) % MAX_QUEUE; + 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); + + if (elapsed >= (duration * 7) / 10) + { + rdpsnd_send_completion(packet->tick, packet->index); + free(out->data); + queue_lo = (queue_lo + 1) % MAX_QUEUE; + started = False; + } + else + { + g_dsp_busy = 1; + return; + } } } - }