23 |
#include <unistd.h> |
#include <unistd.h> |
24 |
#include <fcntl.h> |
#include <fcntl.h> |
25 |
#include <errno.h> |
#include <errno.h> |
26 |
|
#include <sys/time.h> |
27 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
28 |
#include <sys/soundcard.h> |
#include <sys/soundcard.h> |
29 |
|
|
30 |
#define MAX_QUEUE 10 |
#define MAX_QUEUE 10 |
31 |
|
|
32 |
int g_dsp_fd; |
int g_dsp_fd; |
33 |
BOOL g_dsp_busy; |
BOOL g_dsp_busy = False; |
34 |
|
static int g_snd_rate; |
35 |
|
static short g_samplewidth; |
36 |
|
|
37 |
static struct audio_packet |
static struct audio_packet |
38 |
{ |
{ |
45 |
BOOL |
BOOL |
46 |
wave_out_open(void) |
wave_out_open(void) |
47 |
{ |
{ |
48 |
char *dsp_dev = "/dev/dsp"; |
char *dsp_dev = getenv("AUDIODEV"); |
49 |
|
|
50 |
|
if (dsp_dev == NULL) |
51 |
|
{ |
52 |
|
dsp_dev = "/dev/dsp"; |
53 |
|
} |
54 |
|
|
55 |
if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1) |
if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1) |
56 |
{ |
{ |
85 |
BOOL |
BOOL |
86 |
wave_out_set_format(WAVEFORMATEX * pwfx) |
wave_out_set_format(WAVEFORMATEX * pwfx) |
87 |
{ |
{ |
88 |
int speed, channels, format; |
int stereo, format; |
89 |
|
|
90 |
ioctl(g_dsp_fd, SNDCTL_DSP_RESET, NULL); |
ioctl(g_dsp_fd, SNDCTL_DSP_RESET, NULL); |
91 |
ioctl(g_dsp_fd, SNDCTL_DSP_SYNC, NULL); |
ioctl(g_dsp_fd, SNDCTL_DSP_SYNC, NULL); |
95 |
else if (pwfx->wBitsPerSample == 16) |
else if (pwfx->wBitsPerSample == 16) |
96 |
format = AFMT_S16_LE; |
format = AFMT_S16_LE; |
97 |
|
|
98 |
|
g_samplewidth = pwfx->wBitsPerSample / 8; |
99 |
|
|
100 |
if (ioctl(g_dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1) |
if (ioctl(g_dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1) |
101 |
{ |
{ |
102 |
perror("SNDCTL_DSP_SETFMT"); |
perror("SNDCTL_DSP_SETFMT"); |
104 |
return False; |
return False; |
105 |
} |
} |
106 |
|
|
107 |
channels = pwfx->nChannels; |
if (pwfx->nChannels == 2) |
108 |
if (ioctl(g_dsp_fd, SNDCTL_DSP_CHANNELS, &channels) == -1) |
{ |
109 |
|
stereo = 1; |
110 |
|
g_samplewidth *= 2; |
111 |
|
} |
112 |
|
else |
113 |
|
{ |
114 |
|
stereo = 0; |
115 |
|
} |
116 |
|
|
117 |
|
if (ioctl(g_dsp_fd, SNDCTL_DSP_STEREO, &stereo) == -1) |
118 |
{ |
{ |
119 |
perror("SNDCTL_DSP_CHANNELS"); |
perror("SNDCTL_DSP_CHANNELS"); |
120 |
close(g_dsp_fd); |
close(g_dsp_fd); |
121 |
return False; |
return False; |
122 |
} |
} |
123 |
|
|
124 |
speed = pwfx->nSamplesPerSec; |
g_snd_rate = pwfx->nSamplesPerSec; |
125 |
if (ioctl(g_dsp_fd, SNDCTL_DSP_SPEED, &speed) == -1) |
if (ioctl(g_dsp_fd, SNDCTL_DSP_SPEED, &g_snd_rate) == -1) |
126 |
{ |
{ |
127 |
perror("SNDCTL_DSP_SPEED"); |
perror("SNDCTL_DSP_SPEED"); |
128 |
close(g_dsp_fd); |
close(g_dsp_fd); |
135 |
void |
void |
136 |
wave_out_volume(uint16 left, uint16 right) |
wave_out_volume(uint16 left, uint16 right) |
137 |
{ |
{ |
138 |
|
static BOOL use_dev_mixer = False; |
139 |
uint32 volume; |
uint32 volume; |
140 |
|
int fd_mix = -1; |
141 |
|
|
142 |
volume = left / (65536 / 100); |
volume = left / (65536 / 100); |
143 |
volume |= right / (65536 / 100) << 8; |
volume |= right / (65536 / 100) << 8; |
144 |
|
|
145 |
|
if (use_dev_mixer) |
146 |
|
{ |
147 |
|
if ((fd_mix = open("/dev/mixer", O_RDWR | O_NONBLOCK)) == -1) |
148 |
|
{ |
149 |
|
perror("open /dev/mixer"); |
150 |
|
return; |
151 |
|
} |
152 |
|
|
153 |
|
if (ioctl(fd_mix, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1) |
154 |
|
{ |
155 |
|
perror("MIXER_WRITE(SOUND_MIXER_PCM)"); |
156 |
|
return; |
157 |
|
} |
158 |
|
|
159 |
|
close(fd_mix); |
160 |
|
} |
161 |
|
|
162 |
if (ioctl(g_dsp_fd, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1) |
if (ioctl(g_dsp_fd, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1) |
163 |
{ |
{ |
164 |
perror("MIXER_WRITE(SOUND_MIXER_PCM)"); |
perror("MIXER_WRITE(SOUND_MIXER_PCM)"); |
165 |
|
use_dev_mixer = True; |
166 |
return; |
return; |
167 |
} |
} |
168 |
} |
} |
199 |
struct audio_packet *packet; |
struct audio_packet *packet; |
200 |
ssize_t len; |
ssize_t len; |
201 |
STREAM out; |
STREAM out; |
202 |
|
static long startedat_us; |
203 |
|
static long startedat_s; |
204 |
|
static BOOL started = False; |
205 |
|
struct timeval tv; |
206 |
|
|
207 |
while (1) |
while (1) |
208 |
{ |
{ |
215 |
packet = &packet_queue[queue_lo]; |
packet = &packet_queue[queue_lo]; |
216 |
out = &packet->s; |
out = &packet->s; |
217 |
|
|
218 |
|
if (!started) |
219 |
|
{ |
220 |
|
gettimeofday(&tv, NULL); |
221 |
|
startedat_us = tv.tv_usec; |
222 |
|
startedat_s = tv.tv_sec; |
223 |
|
started = True; |
224 |
|
} |
225 |
|
|
226 |
len = write(g_dsp_fd, out->p, out->end - out->p); |
len = write(g_dsp_fd, out->p, out->end - out->p); |
227 |
if (len == -1) |
if (len == -1) |
228 |
{ |
{ |
235 |
out->p += len; |
out->p += len; |
236 |
if (out->p == out->end) |
if (out->p == out->end) |
237 |
{ |
{ |
238 |
rdpsnd_send_completion(packet->tick, packet->index); |
long long duration; |
239 |
free(out->data); |
long elapsed; |
240 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
|
241 |
|
gettimeofday(&tv, NULL); |
242 |
|
duration = (out->size * (1000000 / (g_samplewidth * g_snd_rate))); |
243 |
|
elapsed = (tv.tv_sec - startedat_s) * 1000000 + (tv.tv_usec - startedat_us); |
244 |
|
|
245 |
|
if (elapsed >= (duration * 85) / 100) |
246 |
|
{ |
247 |
|
rdpsnd_send_completion(packet->tick, packet->index); |
248 |
|
free(out->data); |
249 |
|
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
250 |
|
started = False; |
251 |
|
} |
252 |
|
else |
253 |
|
{ |
254 |
|
g_dsp_busy = 1; |
255 |
|
return; |
256 |
|
} |
257 |
} |
} |
258 |
} |
} |
|
|
|
259 |
} |
} |