1 |
/* |
/* -*- c-basic-offset: 8 -*- |
2 |
rdesktop: A Remote Desktop Protocol client. |
rdesktop: A Remote Desktop Protocol client. |
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> |
31 |
|
|
32 |
#define MAX_QUEUE 10 |
#define WAVEOUTLEN 16 |
|
#define WAVEOUTBUF 32 |
|
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; |
|
BOOL g_dsp_busy = False; |
|
|
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; |
57 |
format.rate = 44100; |
format.rate = 44100; |
58 |
g_samplerate = 44100; |
format.byte_format = AO_FMT_NATIVE; |
59 |
format.byte_format = AO_FMT_LITTLE; |
|
60 |
|
|
61 |
o_device = ao_open_live(default_driver, &format, NULL); |
o_device = ao_open_live(default_driver, &format, NULL); |
62 |
if (o_device == NULL) |
if (o_device == NULL) |
65 |
} |
} |
66 |
|
|
67 |
g_dsp_fd = 0; |
g_dsp_fd = 0; |
68 |
queue_lo = queue_hi = 0; |
|
69 |
|
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_queue_next(0); |
|
free(packet_queue[queue_lo].s.data); |
|
|
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
|
81 |
} |
} |
82 |
|
|
83 |
if (o_device != NULL) |
if (o_device != NULL) |
84 |
ao_close(o_device); |
ao_close(o_device); |
|
ao_shutdown(); |
|
|
} |
|
|
|
|
|
BOOL |
|
|
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; |
|
|
/* 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; |
|
85 |
|
|
86 |
return True; |
ao_shutdown(); |
87 |
} |
} |
88 |
|
|
89 |
BOOL |
BOOL |
90 |
wave_out_set_format(WAVEFORMATEX * pwfx) |
libao_set_format(WAVEFORMATEX * pwfx) |
91 |
{ |
{ |
92 |
ao_sample_format format; |
ao_sample_format format; |
93 |
|
|
94 |
format.bits = pwfx->wBitsPerSample; |
format.bits = pwfx->wBitsPerSample; |
95 |
format.channels = pwfx->nChannels; |
format.channels = pwfx->nChannels; |
96 |
format.rate = 44100; |
format.rate = 44100; |
97 |
g_samplerate = pwfx->nSamplesPerSec; |
format.byte_format = AO_FMT_NATIVE; |
|
format.byte_format = AO_FMT_LITTLE; |
|
98 |
|
|
99 |
g_samplewidth = pwfx->wBitsPerSample / 8; |
if (o_device != NULL) |
|
|
|
|
if(o_device != NULL) |
|
100 |
ao_close(o_device); |
ao_close(o_device); |
101 |
|
|
102 |
o_device = ao_open_live(default_driver, &format, NULL); |
o_device = ao_open_live(default_driver, &format, NULL); |
105 |
return False; |
return False; |
106 |
} |
} |
107 |
|
|
108 |
|
if (rdpsnd_dsp_resample_set(44100, pwfx->wBitsPerSample, pwfx->nChannels) == False) |
|
return True; |
|
|
} |
|
|
|
|
|
void |
|
|
wave_out_volume(uint16 left, uint16 right) |
|
|
{ |
|
|
} |
|
|
|
|
|
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) |
|
109 |
{ |
{ |
110 |
error("No space to queue audio packet\n"); |
return False; |
|
return; |
|
111 |
} |
} |
112 |
|
|
113 |
queue_hi = next_hi; |
reopened = True; |
|
|
|
|
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); |
|
114 |
|
|
115 |
if (!g_dsp_busy) |
return True; |
|
wave_out_play(); |
|
116 |
} |
} |
117 |
|
|
118 |
void |
void |
119 |
wave_out_play(void) |
libao_play(void) |
120 |
{ |
{ |
121 |
struct audio_packet *packet; |
struct audio_packet *packet; |
122 |
STREAM out; |
STREAM out; |
123 |
unsigned char expanded[WAVEOUTBUF]; |
int len; |
124 |
int offset,len,i; |
static long prev_s, prev_us; |
125 |
|
unsigned int duration; |
126 |
|
struct timeval tv; |
127 |
|
int next_tick; |
128 |
|
|
129 |
|
if (reopened) |
130 |
|
{ |
131 |
|
reopened = False; |
132 |
|
gettimeofday(&tv, NULL); |
133 |
|
prev_s = tv.tv_sec; |
134 |
|
prev_us = tv.tv_usec; |
135 |
|
} |
136 |
|
|
137 |
if (queue_lo == queue_hi) |
if (rdpsnd_queue_empty()) |
138 |
{ |
{ |
139 |
g_dsp_busy = 0; |
g_dsp_busy = 0; |
140 |
return; |
return; |
141 |
} |
} |
142 |
|
|
143 |
packet = &packet_queue[queue_lo]; |
packet = rdpsnd_queue_current_packet(); |
144 |
out = &packet->s; |
out = &packet->s; |
145 |
|
|
146 |
len = 0; |
next_tick = rdpsnd_queue_next_tick(); |
147 |
|
|
148 |
|
len = (WAVEOUTLEN > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTLEN; |
149 |
|
ao_play(o_device, (char *) out->p, len); |
150 |
|
out->p += len; |
151 |
|
|
152 |
|
gettimeofday(&tv, NULL); |
153 |
|
|
154 |
|
duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000; |
155 |
|
|
156 |
if (g_samplerate == 22050 ) |
if (packet->tick > next_tick) |
157 |
|
next_tick += 65536; |
158 |
|
|
159 |
|
if ((out->p == out->end) || duration > next_tick - packet->tick + 500) |
160 |
{ |
{ |
161 |
/* Resample to 44100 */ |
prev_s = tv.tv_sec; |
162 |
for(i=0; (i<((WAVEOUTBUF/8)*(3-g_samplewidth))) && (out->p < out->end); i++) |
prev_us = tv.tv_usec; |
163 |
|
|
164 |
|
if (abs((next_tick - packet->tick) - duration) > 20) |
165 |
{ |
{ |
166 |
offset=i*4*g_samplewidth; |
DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick)); |
167 |
memcpy(&expanded[0*g_samplewidth+offset],out->p,g_samplewidth); |
DEBUG(("last: %d, is: %d, should: %d\n", packet->tick, |
168 |
memcpy(&expanded[2*g_samplewidth+offset],out->p,g_samplewidth); |
(packet->tick + duration) % 65536, next_tick % 65536)); |
|
out->p += 2; |
|
|
|
|
|
memcpy(&expanded[1*g_samplewidth+offset],out->p,g_samplewidth); |
|
|
memcpy(&expanded[3*g_samplewidth+offset],out->p,g_samplewidth); |
|
|
out->p += 2; |
|
|
len += 4*g_samplewidth; |
|
169 |
} |
} |
170 |
|
|
171 |
|
rdpsnd_queue_next(duration); |
172 |
} |
} |
|
else |
|
|
{ |
|
|
len = (WAVEOUTBUF > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTBUF; |
|
|
memcpy(expanded,out->p,len); |
|
|
out->p += len; |
|
|
} |
|
173 |
|
|
174 |
ao_play(o_device, expanded, len); |
g_dsp_busy = 1; |
175 |
|
return; |
176 |
|
} |
177 |
|
|
178 |
|
struct audio_driver * |
179 |
|
libao_register(char *options) |
180 |
|
{ |
181 |
|
static struct audio_driver libao_driver; |
182 |
|
|
183 |
if (out->p == out->end) |
libao_driver.wave_out_open = libao_open; |
184 |
|
libao_driver.wave_out_close = libao_close; |
185 |
|
libao_driver.wave_out_format_supported = rdpsnd_dsp_resample_supported; |
186 |
|
libao_driver.wave_out_set_format = libao_set_format; |
187 |
|
libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set; |
188 |
|
libao_driver.wave_out_play = libao_play; |
189 |
|
libao_driver.name = xstrdup("libao"); |
190 |
|
libao_driver.description = xstrdup("libao output driver, default device: system dependent"); |
191 |
|
libao_driver.need_byteswap_on_be = 1; |
192 |
|
libao_driver.need_resampling = 1; |
193 |
|
libao_driver.next = NULL; |
194 |
|
|
195 |
|
if (options) |
196 |
{ |
{ |
197 |
rdpsnd_send_completion(packet->tick, packet->index); |
libao_device = xstrdup(options); |
|
free(out->data); |
|
|
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
|
198 |
} |
} |
199 |
|
|
200 |
g_dsp_busy = 1; |
return &libao_driver; |
|
return; |
|
201 |
} |
} |