3 |
Sound Channel Process Functions - libao-driver |
Sound Channel Process Functions - libao-driver |
4 |
Copyright (C) Matthew Chapman 2003 |
Copyright (C) Matthew Chapman 2003 |
5 |
Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003 |
Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003 |
6 |
Copyright (C) Michael Gernoth mike@zerfleddert.de 2005 |
Copyright (C) Michael Gernoth mike@zerfleddert.de 2005-2006 |
7 |
|
|
8 |
This program is free software; you can redistribute it and/or modify |
This program is free software; you can redistribute it and/or modify |
9 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
21 |
*/ |
*/ |
22 |
|
|
23 |
#include "rdesktop.h" |
#include "rdesktop.h" |
24 |
|
#include "rdpsnd.h" |
25 |
#include <unistd.h> |
#include <unistd.h> |
26 |
#include <fcntl.h> |
#include <fcntl.h> |
27 |
#include <errno.h> |
#include <errno.h> |
28 |
#include <ao/ao.h> |
#include <ao/ao.h> |
29 |
#include <sys/time.h> |
#include <sys/time.h> |
30 |
|
|
31 |
#define MAX_QUEUE 10 |
#define WAVEOUTBUF 16 |
|
#define WAVEOUTBUF 64 |
|
32 |
|
|
33 |
int g_dsp_fd; |
static ao_device *o_device = NULL; |
34 |
ao_device *o_device = NULL; |
static int default_driver; |
35 |
int default_driver; |
static int samplerate; |
36 |
int g_samplerate; |
static int audiochannels; |
37 |
int g_channels; |
static BOOL reopened; |
38 |
BOOL g_dsp_busy = False; |
static short samplewidth; |
39 |
static BOOL g_reopened; |
static char *libao_device = NULL; |
|
static short g_samplewidth; |
|
|
|
|
|
static struct audio_packet |
|
|
{ |
|
|
struct stream s; |
|
|
uint16 tick; |
|
|
uint8 index; |
|
|
} packet_queue[MAX_QUEUE]; |
|
|
static unsigned int queue_hi, queue_lo; |
|
40 |
|
|
41 |
BOOL |
BOOL |
42 |
wave_out_open(void) |
libao_open(void) |
43 |
{ |
{ |
44 |
ao_sample_format format; |
ao_sample_format format; |
45 |
|
static int warned = 0; |
46 |
|
|
47 |
|
if (!warned && libao_device) |
48 |
|
{ |
49 |
|
warning("device-options not supported for libao-driver\n"); |
50 |
|
warned = 1; |
51 |
|
} |
52 |
|
|
53 |
ao_initialize(); |
ao_initialize(); |
54 |
default_driver = ao_default_driver_id(); |
default_driver = ao_default_driver_id(); |
55 |
|
|
56 |
format.bits = 16; |
format.bits = 16; |
57 |
format.channels = 2; |
format.channels = 2; |
58 |
g_channels = 2; |
audiochannels = 2; |
59 |
format.rate = 44100; |
format.rate = 44100; |
60 |
g_samplerate = 44100; |
samplerate = 44100; |
61 |
format.byte_format = AO_FMT_LITTLE; |
format.byte_format = AO_FMT_LITTLE; |
62 |
|
|
63 |
o_device = ao_open_live(default_driver, &format, NULL); |
o_device = ao_open_live(default_driver, &format, NULL); |
67 |
} |
} |
68 |
|
|
69 |
g_dsp_fd = 0; |
g_dsp_fd = 0; |
70 |
queue_lo = queue_hi = 0; |
rdpsnd_queue_init(); |
71 |
|
|
72 |
g_reopened = True; |
reopened = True; |
73 |
|
|
74 |
return True; |
return True; |
75 |
} |
} |
76 |
|
|
77 |
void |
void |
78 |
wave_out_close(void) |
libao_close(void) |
79 |
{ |
{ |
80 |
/* Ack all remaining packets */ |
/* Ack all remaining packets */ |
81 |
while (queue_lo != queue_hi) |
while (!rdpsnd_queue_empty()) |
82 |
{ |
{ |
83 |
rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index); |
rdpsnd_send_completion(rdpsnd_queue_current_packet()->tick, |
84 |
free(packet_queue[queue_lo].s.data); |
rdpsnd_queue_current_packet()->index); |
85 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
rdpsnd_queue_next(); |
86 |
} |
} |
87 |
|
|
88 |
if (o_device != NULL) |
if (o_device != NULL) |
92 |
} |
} |
93 |
|
|
94 |
BOOL |
BOOL |
95 |
wave_out_format_supported(WAVEFORMATEX * pwfx) |
libao_format_supported(WAVEFORMATEX * pwfx) |
96 |
{ |
{ |
97 |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
98 |
return False; |
return False; |
109 |
} |
} |
110 |
|
|
111 |
BOOL |
BOOL |
112 |
wave_out_set_format(WAVEFORMATEX * pwfx) |
libao_set_format(WAVEFORMATEX * pwfx) |
113 |
{ |
{ |
114 |
ao_sample_format format; |
ao_sample_format format; |
115 |
|
|
116 |
format.bits = pwfx->wBitsPerSample; |
format.bits = pwfx->wBitsPerSample; |
117 |
format.channels = pwfx->nChannels; |
format.channels = pwfx->nChannels; |
118 |
g_channels = pwfx->nChannels; |
audiochannels = pwfx->nChannels; |
119 |
format.rate = 44100; |
format.rate = 44100; |
120 |
g_samplerate = pwfx->nSamplesPerSec; |
samplerate = pwfx->nSamplesPerSec; |
121 |
format.byte_format = AO_FMT_LITTLE; |
format.byte_format = AO_FMT_LITTLE; |
122 |
|
|
123 |
g_samplewidth = pwfx->wBitsPerSample / 8; |
samplewidth = pwfx->wBitsPerSample / 8; |
124 |
|
|
125 |
if (o_device != NULL) |
if (o_device != NULL) |
126 |
ao_close(o_device); |
ao_close(o_device); |
131 |
return False; |
return False; |
132 |
} |
} |
133 |
|
|
134 |
g_reopened = True; |
reopened = True; |
135 |
|
|
136 |
return True; |
return True; |
137 |
} |
} |
138 |
|
|
139 |
void |
void |
140 |
wave_out_volume(uint16 left, uint16 right) |
libao_volume(uint16 left, uint16 right) |
141 |
{ |
{ |
142 |
warning("volume changes not supported with libao-output\n"); |
warning("volume changes not supported with libao-output\n"); |
143 |
} |
} |
144 |
|
|
145 |
void |
void |
146 |
wave_out_write(STREAM s, uint16 tick, uint8 index) |
libao_play(void) |
|
{ |
|
|
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 = malloc(s->size); |
|
|
|
|
|
if (!g_dsp_busy) |
|
|
wave_out_play(); |
|
|
} |
|
|
|
|
|
void |
|
|
wave_out_play(void) |
|
147 |
{ |
{ |
148 |
struct audio_packet *packet; |
struct audio_packet *packet; |
149 |
STREAM out; |
STREAM out; |
150 |
unsigned char outbuf[WAVEOUTBUF]; |
char outbuf[WAVEOUTBUF]; |
151 |
int offset, len, i; |
int offset, len, i; |
152 |
static long prev_s, prev_us; |
static long prev_s, prev_us; |
153 |
unsigned int duration; |
unsigned int duration; |
154 |
struct timeval tv; |
struct timeval tv; |
155 |
int next_tick; |
int next_tick; |
156 |
|
|
157 |
if (g_reopened) |
if (reopened) |
158 |
{ |
{ |
159 |
g_reopened = False; |
reopened = False; |
160 |
gettimeofday(&tv, NULL); |
gettimeofday(&tv, NULL); |
161 |
prev_s = tv.tv_sec; |
prev_s = tv.tv_sec; |
162 |
prev_us = tv.tv_usec; |
prev_us = tv.tv_usec; |
163 |
} |
} |
164 |
|
|
165 |
if (queue_lo == queue_hi) |
if (rdpsnd_queue_empty()) |
166 |
{ |
{ |
167 |
g_dsp_busy = 0; |
g_dsp_busy = 0; |
168 |
return; |
return; |
169 |
} |
} |
170 |
|
|
171 |
packet = &packet_queue[queue_lo]; |
packet = rdpsnd_queue_current_packet(); |
172 |
out = &packet->s; |
out = &packet->s; |
173 |
|
|
174 |
if (((queue_lo + 1) % MAX_QUEUE) != queue_hi) |
next_tick = rdpsnd_queue_next_tick(); |
|
{ |
|
|
next_tick = packet_queue[(queue_lo + 1) % MAX_QUEUE].tick; |
|
|
} |
|
|
else |
|
|
{ |
|
|
next_tick = (packet->tick + 65535) % 65536; |
|
|
} |
|
175 |
|
|
176 |
len = 0; |
len = 0; |
177 |
|
|
178 |
if (g_samplerate == 22050) |
if (samplerate == 22050) |
179 |
{ |
{ |
180 |
/* Resample to 44100 */ |
/* Resample to 44100 */ |
181 |
for (i = 0; (i < ((WAVEOUTBUF / 4) * (3 - g_samplewidth))) && (out->p < out->end); |
for (i = 0; (i < ((WAVEOUTBUF / 4) * (3 - samplewidth))) && (out->p < out->end); |
182 |
i++) |
i++) |
183 |
{ |
{ |
184 |
/* On a stereo-channel we must make sure that left and right |
/* On a stereo-channel we must make sure that left and right |
186 |
data with channels in mind: 1234 -> 12123434 |
data with channels in mind: 1234 -> 12123434 |
187 |
If we have a mono-channel, we can expand the data by simply |
If we have a mono-channel, we can expand the data by simply |
188 |
doubling the sample-data: 1234 -> 11223344 */ |
doubling the sample-data: 1234 -> 11223344 */ |
189 |
if (g_channels == 2) |
if (audiochannels == 2) |
190 |
offset = ((i * 2) - (i & 1)) * g_samplewidth; |
offset = ((i * 2) - (i & 1)) * samplewidth; |
191 |
else |
else |
192 |
offset = (i * 2) * g_samplewidth; |
offset = (i * 2) * samplewidth; |
193 |
|
|
194 |
memcpy(&outbuf[offset], out->p, g_samplewidth); |
memcpy(&outbuf[offset], out->p, samplewidth); |
195 |
memcpy(&outbuf[g_channels * g_samplewidth + offset], out->p, g_samplewidth); |
memcpy(&outbuf[audiochannels * samplewidth + offset], out->p, samplewidth); |
196 |
|
|
197 |
out->p += g_samplewidth; |
out->p += samplewidth; |
198 |
len += 2 * g_samplewidth; |
len += 2 * samplewidth; |
199 |
} |
} |
200 |
} |
} |
201 |
else |
else |
226 |
(packet->tick + duration) % 65536, next_tick % 65536)); |
(packet->tick + duration) % 65536, next_tick % 65536)); |
227 |
} |
} |
228 |
|
|
229 |
/* Until all drivers are using the windows sound-ticks, we need to |
rdpsnd_send_completion(((packet->tick + duration) % 65536), packet->index); |
230 |
substract the 50 ticks added later by rdpsnd.c */ |
rdpsnd_queue_next(); |
|
rdpsnd_send_completion(((packet->tick + duration) % 65536) - 50, packet->index); |
|
|
free(out->data); |
|
|
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
|
231 |
} |
} |
232 |
|
|
233 |
g_dsp_busy = 1; |
g_dsp_busy = 1; |
234 |
return; |
return; |
235 |
} |
} |
236 |
|
|
237 |
|
static struct audio_driver libao_driver = { |
238 |
|
wave_out_write:rdpsnd_queue_write, |
239 |
|
wave_out_open:libao_open, |
240 |
|
wave_out_close:libao_close, |
241 |
|
wave_out_format_supported:libao_format_supported, |
242 |
|
wave_out_set_format:libao_set_format, |
243 |
|
wave_out_volume:libao_volume, |
244 |
|
wave_out_play:libao_play, |
245 |
|
name:"libao", |
246 |
|
description:"libao output driver", |
247 |
|
next:NULL, |
248 |
|
}; |
249 |
|
|
250 |
|
struct audio_driver * |
251 |
|
libao_register(char *options) |
252 |
|
{ |
253 |
|
if (options) |
254 |
|
{ |
255 |
|
libao_device = xstrdup(options); |
256 |
|
} |
257 |
|
|
258 |
|
return &libao_driver; |
259 |
|
} |