2003-10-01 14:35:17 +02:00
|
|
|
/*
|
|
|
|
rdesktop: A Remote Desktop Protocol client.
|
|
|
|
Sound Channel Process Functions - Open Sound System
|
|
|
|
Copyright (C) Matthew Chapman 2003
|
|
|
|
Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003
|
|
|
|
|
|
|
|
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
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "rdesktop.h"
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/soundcard.h>
|
|
|
|
|
|
|
|
#define MAX_QUEUE 10
|
|
|
|
|
|
|
|
int g_dsp_fd;
|
2003-10-19 13:59:41 +02:00
|
|
|
BOOL g_dsp_busy = False;
|
2003-10-01 14:35:17 +02:00
|
|
|
|
2003-10-15 16:01:32 +02:00
|
|
|
static struct audio_packet
|
|
|
|
{
|
2003-10-01 14:35:17 +02:00
|
|
|
struct stream s;
|
|
|
|
uint16 tick;
|
|
|
|
uint8 index;
|
|
|
|
} packet_queue[MAX_QUEUE];
|
|
|
|
static unsigned int queue_hi, queue_lo;
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
wave_out_open(void)
|
|
|
|
{
|
|
|
|
char *dsp_dev = "/dev/dsp";
|
|
|
|
|
2003-10-15 16:01:32 +02:00
|
|
|
if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1)
|
2003-10-01 14:35:17 +02:00
|
|
|
{
|
|
|
|
perror(dsp_dev);
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Non-blocking so that user interface is responsive */
|
2003-10-15 16:01:32 +02:00
|
|
|
fcntl(g_dsp_fd, F_SETFL, fcntl(g_dsp_fd, F_GETFL) | O_NONBLOCK);
|
2003-10-01 14:35:17 +02:00
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wave_out_close(void)
|
|
|
|
{
|
|
|
|
close(g_dsp_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
2003-10-15 16:01:32 +02:00
|
|
|
wave_out_format_supported(WAVEFORMATEX * pwfx)
|
2003-10-01 14:35:17 +02:00
|
|
|
{
|
|
|
|
if (pwfx->wFormatTag != WAVE_FORMAT_PCM)
|
|
|
|
return False;
|
|
|
|
if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2))
|
|
|
|
return False;
|
|
|
|
if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16))
|
|
|
|
return False;
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
2003-10-15 16:01:32 +02:00
|
|
|
wave_out_set_format(WAVEFORMATEX * pwfx)
|
2003-10-01 14:35:17 +02:00
|
|
|
{
|
|
|
|
int speed, channels, format;
|
|
|
|
|
|
|
|
ioctl(g_dsp_fd, SNDCTL_DSP_RESET, NULL);
|
|
|
|
ioctl(g_dsp_fd, SNDCTL_DSP_SYNC, NULL);
|
|
|
|
|
|
|
|
if (pwfx->wBitsPerSample == 8)
|
|
|
|
format = AFMT_U8;
|
|
|
|
else if (pwfx->wBitsPerSample == 16)
|
|
|
|
format = AFMT_S16_LE;
|
|
|
|
|
|
|
|
if (ioctl(g_dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1)
|
|
|
|
{
|
|
|
|
perror("SNDCTL_DSP_SETFMT");
|
|
|
|
close(g_dsp_fd);
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
channels = pwfx->nChannels;
|
|
|
|
if (ioctl(g_dsp_fd, SNDCTL_DSP_CHANNELS, &channels) == -1)
|
|
|
|
{
|
|
|
|
perror("SNDCTL_DSP_CHANNELS");
|
|
|
|
close(g_dsp_fd);
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
speed = pwfx->nSamplesPerSec;
|
|
|
|
if (ioctl(g_dsp_fd, SNDCTL_DSP_SPEED, &speed) == -1)
|
|
|
|
{
|
|
|
|
perror("SNDCTL_DSP_SPEED");
|
|
|
|
close(g_dsp_fd);
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
2003-10-13 18:09:45 +02:00
|
|
|
void
|
|
|
|
wave_out_volume(uint16 left, uint16 right)
|
|
|
|
{
|
2003-10-22 14:16:03 +02:00
|
|
|
static BOOL use_dev_mixer = False;
|
2003-10-13 18:09:45 +02:00
|
|
|
uint32 volume;
|
2003-10-22 14:16:03 +02:00
|
|
|
int fd_mix = -1;
|
2003-10-13 18:09:45 +02:00
|
|
|
|
2003-10-15 16:01:32 +02:00
|
|
|
volume = left / (65536 / 100);
|
|
|
|
volume |= right / (65536 / 100) << 8;
|
2003-10-22 14:16:03 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2003-10-13 18:09:45 +02:00
|
|
|
if (ioctl(g_dsp_fd, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1)
|
|
|
|
{
|
|
|
|
perror("MIXER_WRITE(SOUND_MIXER_PCM)");
|
2003-10-22 14:16:03 +02:00
|
|
|
use_dev_mixer = True;
|
2003-10-13 18:09:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-01 14:35:17 +02:00
|
|
|
void
|
|
|
|
wave_out_write(STREAM s, uint16 tick, uint8 index)
|
|
|
|
{
|
2003-10-04 02:03:24 +02:00
|
|
|
struct audio_packet *packet = &packet_queue[queue_hi];
|
|
|
|
unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE;
|
2003-10-01 14:35:17 +02:00
|
|
|
|
|
|
|
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;
|
2003-10-13 18:09:45 +02:00
|
|
|
packet->s.p += 4;
|
2003-10-01 14:35:17 +02:00
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
struct audio_packet *packet;
|
|
|
|
ssize_t len;
|
|
|
|
STREAM out;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (queue_lo == queue_hi)
|
|
|
|
{
|
|
|
|
g_dsp_busy = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
packet = &packet_queue[queue_lo];
|
|
|
|
out = &packet->s;
|
|
|
|
|
2003-10-15 16:01:32 +02:00
|
|
|
len = write(g_dsp_fd, out->p, out->end - out->p);
|
2003-10-01 14:35:17 +02:00
|
|
|
if (len == -1)
|
|
|
|
{
|
|
|
|
if (errno != EWOULDBLOCK)
|
|
|
|
perror("write audio");
|
|
|
|
g_dsp_busy = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|