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 "rdpsnd_dsp.h" |
26 |
#include <unistd.h> |
#include <unistd.h> |
27 |
#include <fcntl.h> |
#include <fcntl.h> |
28 |
#include <errno.h> |
#include <errno.h> |
29 |
#include <ao/ao.h> |
#include <ao/ao.h> |
30 |
#include <sys/time.h> |
#include <sys/time.h> |
31 |
|
|
32 |
#define MAX_QUEUE 10 |
#define WAVEOUTBUF 16 |
|
#define WAVEOUTBUF 64 |
|
33 |
|
|
34 |
int g_dsp_fd; |
static ao_device *o_device = NULL; |
35 |
ao_device *o_device = NULL; |
static int default_driver; |
36 |
int default_driver; |
static BOOL reopened; |
37 |
int g_samplerate; |
static char *libao_device = NULL; |
|
int g_channels; |
|
|
BOOL g_dsp_busy = False; |
|
|
static BOOL g_reopened; |
|
|
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; |
|
38 |
|
|
39 |
BOOL |
BOOL |
40 |
wave_out_open(void) |
libao_open(void) |
41 |
{ |
{ |
42 |
ao_sample_format format; |
ao_sample_format format; |
43 |
|
|
44 |
ao_initialize(); |
ao_initialize(); |
45 |
default_driver = ao_default_driver_id(); |
|
46 |
|
if (libao_device) |
47 |
|
{ |
48 |
|
default_driver = ao_driver_id(libao_device); |
49 |
|
} |
50 |
|
else |
51 |
|
{ |
52 |
|
default_driver = ao_default_driver_id(); |
53 |
|
} |
54 |
|
|
55 |
format.bits = 16; |
format.bits = 16; |
56 |
format.channels = 2; |
format.channels = 2; |
|
g_channels = 2; |
|
57 |
format.rate = 44100; |
format.rate = 44100; |
|
g_samplerate = 44100; |
|
58 |
format.byte_format = AO_FMT_LITTLE; |
format.byte_format = AO_FMT_LITTLE; |
59 |
|
|
60 |
o_device = ao_open_live(default_driver, &format, NULL); |
o_device = ao_open_live(default_driver, &format, NULL); |
64 |
} |
} |
65 |
|
|
66 |
g_dsp_fd = 0; |
g_dsp_fd = 0; |
67 |
queue_lo = queue_hi = 0; |
rdpsnd_queue_init(); |
68 |
|
|
69 |
g_reopened = True; |
reopened = True; |
70 |
|
|
71 |
return True; |
return True; |
72 |
} |
} |
73 |
|
|
74 |
void |
void |
75 |
wave_out_close(void) |
libao_close(void) |
76 |
{ |
{ |
77 |
/* Ack all remaining packets */ |
/* Ack all remaining packets */ |
78 |
while (queue_lo != queue_hi) |
while (!rdpsnd_queue_empty()) |
79 |
{ |
{ |
80 |
rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index); |
rdpsnd_send_completion(rdpsnd_queue_current_packet()->tick, |
81 |
free(packet_queue[queue_lo].s.data); |
rdpsnd_queue_current_packet()->index); |
82 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
rdpsnd_queue_next(); |
83 |
} |
} |
84 |
|
|
85 |
if (o_device != NULL) |
if (o_device != NULL) |
89 |
} |
} |
90 |
|
|
91 |
BOOL |
BOOL |
92 |
wave_out_format_supported(WAVEFORMATEX * pwfx) |
libao_set_format(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; |
|
|
/* The only common denominator between libao output drivers is a sample-rate of |
|
|
44100, we need to upsample 22050 to it */ |
|
|
if ((pwfx->nSamplesPerSec != 44100) && (pwfx->nSamplesPerSec != 22050)) |
|
|
return False; |
|
|
|
|
|
return True; |
|
|
} |
|
|
|
|
|
BOOL |
|
|
wave_out_set_format(WAVEFORMATEX * pwfx) |
|
93 |
{ |
{ |
94 |
ao_sample_format format; |
ao_sample_format format; |
95 |
|
|
96 |
format.bits = pwfx->wBitsPerSample; |
format.bits = pwfx->wBitsPerSample; |
97 |
format.channels = pwfx->nChannels; |
format.channels = pwfx->nChannels; |
|
g_channels = pwfx->nChannels; |
|
98 |
format.rate = 44100; |
format.rate = 44100; |
|
g_samplerate = pwfx->nSamplesPerSec; |
|
99 |
format.byte_format = AO_FMT_LITTLE; |
format.byte_format = AO_FMT_LITTLE; |
100 |
|
|
|
g_samplewidth = pwfx->wBitsPerSample / 8; |
|
|
|
|
101 |
if (o_device != NULL) |
if (o_device != NULL) |
102 |
ao_close(o_device); |
ao_close(o_device); |
103 |
|
|
107 |
return False; |
return False; |
108 |
} |
} |
109 |
|
|
110 |
g_reopened = True; |
if (rdpsnd_dsp_resample_set(44100, pwfx->wBitsPerSample, pwfx->nChannels) == False) |
|
|
|
|
return True; |
|
|
} |
|
|
|
|
|
void |
|
|
wave_out_volume(uint16 left, uint16 right) |
|
|
{ |
|
|
warning("volume changes not supported with libao-output\n"); |
|
|
} |
|
|
|
|
|
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) |
|
111 |
{ |
{ |
112 |
error("No space to queue audio packet\n"); |
return False; |
|
return; |
|
113 |
} |
} |
114 |
|
|
115 |
queue_hi = next_hi; |
reopened = True; |
|
|
|
|
packet->s = *s; |
|
|
packet->tick = tick; |
|
|
packet->index = index; |
|
|
packet->s.p += 4; |
|
116 |
|
|
117 |
/* we steal the data buffer from s, give it a new one */ |
return True; |
|
s->data = malloc(s->size); |
|
|
|
|
|
if (!g_dsp_busy) |
|
|
wave_out_play(); |
|
118 |
} |
} |
119 |
|
|
120 |
void |
void |
121 |
wave_out_play(void) |
libao_play(void) |
122 |
{ |
{ |
123 |
struct audio_packet *packet; |
struct audio_packet *packet; |
124 |
STREAM out; |
STREAM out; |
125 |
char outbuf[WAVEOUTBUF]; |
int len; |
|
int offset, len, i; |
|
126 |
static long prev_s, prev_us; |
static long prev_s, prev_us; |
127 |
unsigned int duration; |
unsigned int duration; |
128 |
struct timeval tv; |
struct timeval tv; |
129 |
int next_tick; |
int next_tick; |
130 |
|
|
131 |
if (g_reopened) |
if (reopened) |
132 |
{ |
{ |
133 |
g_reopened = False; |
reopened = False; |
134 |
gettimeofday(&tv, NULL); |
gettimeofday(&tv, NULL); |
135 |
prev_s = tv.tv_sec; |
prev_s = tv.tv_sec; |
136 |
prev_us = tv.tv_usec; |
prev_us = tv.tv_usec; |
137 |
} |
} |
138 |
|
|
139 |
if (queue_lo == queue_hi) |
if (rdpsnd_queue_empty()) |
140 |
{ |
{ |
141 |
g_dsp_busy = 0; |
g_dsp_busy = 0; |
142 |
return; |
return; |
143 |
} |
} |
144 |
|
|
145 |
packet = &packet_queue[queue_lo]; |
packet = rdpsnd_queue_current_packet(); |
146 |
out = &packet->s; |
out = &packet->s; |
147 |
|
|
148 |
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; |
|
|
} |
|
|
|
|
|
len = 0; |
|
|
|
|
|
if (g_samplerate == 22050) |
|
|
{ |
|
|
/* Resample to 44100 */ |
|
|
for (i = 0; (i < ((WAVEOUTBUF / 4) * (3 - g_samplewidth))) && (out->p < out->end); |
|
|
i++) |
|
|
{ |
|
|
/* On a stereo-channel we must make sure that left and right |
|
|
does not get mixed up, so we need to expand the sample- |
|
|
data with channels in mind: 1234 -> 12123434 |
|
|
If we have a mono-channel, we can expand the data by simply |
|
|
doubling the sample-data: 1234 -> 11223344 */ |
|
|
if (g_channels == 2) |
|
|
offset = ((i * 2) - (i & 1)) * g_samplewidth; |
|
|
else |
|
|
offset = (i * 2) * g_samplewidth; |
|
149 |
|
|
150 |
memcpy(&outbuf[offset], out->p, g_samplewidth); |
len = (WAVEOUTBUF > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTBUF; |
151 |
memcpy(&outbuf[g_channels * g_samplewidth + offset], out->p, g_samplewidth); |
ao_play(o_device, (char *) out->p, len); |
152 |
|
out->p += len; |
|
out->p += g_samplewidth; |
|
|
len += 2 * g_samplewidth; |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
len = (WAVEOUTBUF > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTBUF; |
|
|
memcpy(outbuf, out->p, len); |
|
|
out->p += len; |
|
|
} |
|
|
|
|
|
ao_play(o_device, outbuf, len); |
|
153 |
|
|
154 |
gettimeofday(&tv, NULL); |
gettimeofday(&tv, NULL); |
155 |
|
|
170 |
(packet->tick + duration) % 65536, next_tick % 65536)); |
(packet->tick + duration) % 65536, next_tick % 65536)); |
171 |
} |
} |
172 |
|
|
173 |
/* Until all drivers are using the windows sound-ticks, we need to |
rdpsnd_send_completion(((packet->tick + duration) % 65536), packet->index); |
174 |
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; |
|
175 |
} |
} |
176 |
|
|
177 |
g_dsp_busy = 1; |
g_dsp_busy = 1; |
178 |
return; |
return; |
179 |
} |
} |
180 |
|
|
181 |
|
struct audio_driver * |
182 |
|
libao_register(char *options) |
183 |
|
{ |
184 |
|
static struct audio_driver libao_driver; |
185 |
|
struct ao_info *libao_info; |
186 |
|
static char description[101]; |
187 |
|
|
188 |
|
libao_driver.wave_out_write = rdpsnd_queue_write; |
189 |
|
libao_driver.wave_out_open = libao_open; |
190 |
|
libao_driver.wave_out_close = libao_close; |
191 |
|
libao_driver.wave_out_format_supported = rdpsnd_dsp_resample_supported; |
192 |
|
libao_driver.wave_out_set_format = libao_set_format; |
193 |
|
libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set; |
194 |
|
libao_driver.wave_out_play = libao_play; |
195 |
|
libao_driver.name = xstrdup("libao"); |
196 |
|
libao_driver.description = description; |
197 |
|
libao_driver.need_byteswap_on_be = 0; |
198 |
|
libao_driver.next = NULL; |
199 |
|
|
200 |
|
ao_initialize(); |
201 |
|
|
202 |
|
libao_info = ao_driver_info(ao_default_driver_id()); |
203 |
|
|
204 |
|
if (libao_info) |
205 |
|
{ |
206 |
|
snprintf(description, 100, "libao output driver, default device: %s", |
207 |
|
libao_info->short_name); |
208 |
|
} |
209 |
|
else |
210 |
|
{ |
211 |
|
snprintf(description, 100, "libao output driver, default device: none"); |
212 |
|
} |
213 |
|
|
214 |
|
ao_shutdown(); |
215 |
|
|
216 |
|
if (options) |
217 |
|
{ |
218 |
|
libao_device = xstrdup(options); |
219 |
|
} |
220 |
|
|
221 |
|
return &libao_driver; |
222 |
|
} |