[go: up one dir, main page]

Menu

[r664]: / tags / 0.3 / rdpsnd_oss.c  Maximize  Restore  History

Download this file

312 lines (259 with data), 6.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
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/time.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#define MAX_QUEUE 10
int g_dsp_fd;
RDCBOOL g_dsp_busy = False;
static int g_snd_rate;
static short g_samplewidth;
static RDCBOOL g_driver_broken = False;
static struct audio_packet
{
struct stream s;
uint16 tick;
uint8 index;
} packet_queue[MAX_QUEUE];
static unsigned int queue_hi, queue_lo;
RDCBOOL
wave_out_open(void)
{
char *dsp_dev = getenv("AUDIODEV");
if (dsp_dev == NULL)
{
dsp_dev = strdup("/dev/dsp");
}
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);
return True;
}
void
wave_out_close(void)
{
close(g_dsp_fd);
}
RDCBOOL
wave_out_format_supported(WAVEFORMATEX * pwfx)
{
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;
}
RDCBOOL
wave_out_set_format(WAVEFORMATEX * pwfx)
{
int stereo, format, fragments;
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;
g_samplewidth = pwfx->wBitsPerSample / 8;
if (ioctl(g_dsp_fd, SNDCTL_DSP_SETFMT, &format) == -1)
{
perror("SNDCTL_DSP_SETFMT");
close(g_dsp_fd);
return False;
}
if (pwfx->nChannels == 2)
{
stereo = 1;
g_samplewidth *= 2;
}
else
{
stereo = 0;
}
if (ioctl(g_dsp_fd, SNDCTL_DSP_STEREO, &stereo) == -1)
{
perror("SNDCTL_DSP_CHANNELS");
close(g_dsp_fd);
return False;
}
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);
return False;
}
/* try to get 7 fragments of 2^12 bytes size */
fragments = (7 << 16) + 12;
ioctl(g_dsp_fd, SNDCTL_DSP_SETFRAGMENT, &fragments);
if (!g_driver_broken)
{
audio_buf_info info;
memset(&info, 0, sizeof(info));
if (ioctl(g_dsp_fd, SNDCTL_DSP_GETOSPACE, &info) == -1)
{
perror("SNDCTL_DSP_GETOSPACE");
close(g_dsp_fd);
return False;
}
if (info.fragments == 0 || info.fragstotal == 0 || info.fragsize == 0)
{
fprintf(stderr,
"Broken OSS-driver detected: fragments: %d, fragstotal: %d, fragsize: %d\n",
info.fragments, info.fragstotal, info.fragsize);
g_driver_broken = True;
}
}
return True;
}
void
wave_out_volume(uint16 left, uint16 right)
{
static RDCBOOL 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);
}
if (ioctl(g_dsp_fd, MIXER_WRITE(SOUND_MIXER_PCM), &volume) == -1)
{
perror("MIXER_WRITE(SOUND_MIXER_PCM)");
use_dev_mixer = True;
return;
}
}
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)
{
error("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 = (uint8 *) malloc(s->size);
if (!g_dsp_busy)
wave_out_play();
}
void
wave_out_play(void)
{
struct audio_packet *packet;
ssize_t len;
STREAM out;
static long startedat_us;
static long startedat_s;
static RDCBOOL started = False;
struct timeval tv;
audio_buf_info info;
while (1)
{
if (queue_lo == queue_hi)
{
g_dsp_busy = 0;
return;
}
packet = &packet_queue[queue_lo];
out = &packet->s;
if (!started)
{
gettimeofday(&tv, NULL);
startedat_us = tv.tv_usec;
startedat_s = tv.tv_sec;
started = True;
}
len = out->end - out->p;
if (!g_driver_broken)
{
memset(&info, 0, sizeof(info));
if (ioctl(g_dsp_fd, SNDCTL_DSP_GETOSPACE, &info) == -1)
{
perror("SNDCTL_DSP_GETOSPACE");
return;
}
if (info.fragments == 0)
{
g_dsp_busy = 1;
return;
}
if (info.fragments * info.fragsize < len
&& info.fragments * info.fragsize > 0)
{
len = info.fragments * info.fragsize;
}
}
len = write(g_dsp_fd, out->p, len);
if (len == -1)
{
if (errno != EWOULDBLOCK)
perror("write audio");
g_dsp_busy = 1;
return;
}
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);
if (elapsed >= (duration * 85) / 100)
{
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;
}
}
}
}