1 |
astrand |
963 |
/* -*- c-basic-offset: 8 -*- |
2 |
stargo |
833 |
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
Sound Channel Process Functions - libao-driver |
4 |
|
|
Copyright (C) Matthew Chapman 2003 |
5 |
|
|
Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003 |
6 |
stargo |
1254 |
Copyright (C) Michael Gernoth mike@zerfleddert.de 2005-2006 |
7 |
stargo |
833 |
|
8 |
|
|
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 |
10 |
|
|
the Free Software Foundation; either version 2 of the License, or |
11 |
|
|
(at your option) any later version. |
12 |
|
|
|
13 |
|
|
This program is distributed in the hope that it will be useful, |
14 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
|
|
GNU General Public License for more details. |
17 |
|
|
|
18 |
|
|
You should have received a copy of the GNU General Public License |
19 |
|
|
along with this program; if not, write to the Free Software |
20 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 |
|
|
*/ |
22 |
|
|
|
23 |
|
|
#include "rdesktop.h" |
24 |
stargo |
1254 |
#include "rdpsnd.h" |
25 |
stargo |
1258 |
#include "rdpsnd_dsp.h" |
26 |
stargo |
833 |
#include <unistd.h> |
27 |
|
|
#include <fcntl.h> |
28 |
|
|
#include <errno.h> |
29 |
|
|
#include <ao/ao.h> |
30 |
stargo |
891 |
#include <sys/time.h> |
31 |
stargo |
833 |
|
32 |
stargo |
1295 |
#define WAVEOUTLEN 16 |
33 |
stargo |
833 |
|
34 |
stargo |
1245 |
static ao_device *o_device = NULL; |
35 |
|
|
static int default_driver; |
36 |
|
|
static BOOL reopened; |
37 |
stargo |
1255 |
static char *libao_device = NULL; |
38 |
stargo |
833 |
|
39 |
ossman_ |
1346 |
void libao_play(void); |
40 |
|
|
|
41 |
|
|
void |
42 |
|
|
libao_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv) |
43 |
|
|
{ |
44 |
stargo |
1352 |
/* We need to be called rather often... */ |
45 |
|
|
FD_SET(0, wfds); |
46 |
ossman_ |
1346 |
} |
47 |
|
|
|
48 |
|
|
void |
49 |
|
|
libao_check_fds(fd_set * rfds, fd_set * wfds) |
50 |
|
|
{ |
51 |
|
|
if (o_device == NULL) |
52 |
|
|
return; |
53 |
|
|
|
54 |
|
|
if (!rdpsnd_queue_empty()) |
55 |
|
|
libao_play(); |
56 |
|
|
} |
57 |
|
|
|
58 |
stargo |
833 |
BOOL |
59 |
stargo |
1255 |
libao_open(void) |
60 |
stargo |
833 |
{ |
61 |
|
|
ao_sample_format format; |
62 |
|
|
|
63 |
stargo |
1266 |
ao_initialize(); |
64 |
|
|
|
65 |
|
|
if (libao_device) |
66 |
stargo |
1255 |
{ |
67 |
stargo |
1266 |
default_driver = ao_driver_id(libao_device); |
68 |
stargo |
1255 |
} |
69 |
stargo |
1266 |
else |
70 |
|
|
{ |
71 |
|
|
default_driver = ao_default_driver_id(); |
72 |
|
|
} |
73 |
stargo |
1255 |
|
74 |
stargo |
833 |
format.bits = 16; |
75 |
|
|
format.channels = 2; |
76 |
|
|
format.rate = 44100; |
77 |
stargo |
1289 |
format.byte_format = AO_FMT_NATIVE; |
78 |
stargo |
833 |
|
79 |
stargo |
1289 |
|
80 |
stargo |
833 |
o_device = ao_open_live(default_driver, &format, NULL); |
81 |
|
|
if (o_device == NULL) |
82 |
|
|
{ |
83 |
|
|
return False; |
84 |
|
|
} |
85 |
|
|
|
86 |
stargo |
1245 |
reopened = True; |
87 |
stargo |
891 |
|
88 |
stargo |
833 |
return True; |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
void |
92 |
stargo |
1255 |
libao_close(void) |
93 |
stargo |
833 |
{ |
94 |
|
|
/* Ack all remaining packets */ |
95 |
stargo |
1254 |
while (!rdpsnd_queue_empty()) |
96 |
stargo |
833 |
{ |
97 |
ossman_ |
1302 |
rdpsnd_queue_next(0); |
98 |
stargo |
833 |
} |
99 |
|
|
|
100 |
|
|
if (o_device != NULL) |
101 |
|
|
ao_close(o_device); |
102 |
stargo |
838 |
|
103 |
ossman_ |
1346 |
o_device = NULL; |
104 |
|
|
|
105 |
stargo |
833 |
ao_shutdown(); |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
BOOL |
109 |
jsorg71 |
1364 |
libao_set_format(RD_WAVEFORMATEX * pwfx) |
110 |
stargo |
833 |
{ |
111 |
|
|
ao_sample_format format; |
112 |
|
|
|
113 |
|
|
format.bits = pwfx->wBitsPerSample; |
114 |
|
|
format.channels = pwfx->nChannels; |
115 |
|
|
format.rate = 44100; |
116 |
stargo |
1289 |
format.byte_format = AO_FMT_NATIVE; |
117 |
stargo |
833 |
|
118 |
stargo |
837 |
if (o_device != NULL) |
119 |
stargo |
833 |
ao_close(o_device); |
120 |
|
|
|
121 |
|
|
o_device = ao_open_live(default_driver, &format, NULL); |
122 |
|
|
if (o_device == NULL) |
123 |
|
|
{ |
124 |
|
|
return False; |
125 |
|
|
} |
126 |
|
|
|
127 |
stargo |
1276 |
if (rdpsnd_dsp_resample_set(44100, pwfx->wBitsPerSample, pwfx->nChannels) == False) |
128 |
|
|
{ |
129 |
|
|
return False; |
130 |
|
|
} |
131 |
|
|
|
132 |
stargo |
1245 |
reopened = True; |
133 |
stargo |
833 |
|
134 |
|
|
return True; |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
void |
138 |
stargo |
1255 |
libao_play(void) |
139 |
stargo |
833 |
{ |
140 |
|
|
struct audio_packet *packet; |
141 |
|
|
STREAM out; |
142 |
stargo |
1276 |
int len; |
143 |
stargo |
891 |
static long prev_s, prev_us; |
144 |
|
|
unsigned int duration; |
145 |
|
|
struct timeval tv; |
146 |
|
|
int next_tick; |
147 |
stargo |
833 |
|
148 |
stargo |
1245 |
if (reopened) |
149 |
stargo |
891 |
{ |
150 |
stargo |
1245 |
reopened = False; |
151 |
stargo |
891 |
gettimeofday(&tv, NULL); |
152 |
|
|
prev_s = tv.tv_sec; |
153 |
|
|
prev_us = tv.tv_usec; |
154 |
|
|
} |
155 |
|
|
|
156 |
ossman_ |
1346 |
/* We shouldn't be called if the queue is empty, but still */ |
157 |
stargo |
1254 |
if (rdpsnd_queue_empty()) |
158 |
stargo |
835 |
return; |
159 |
stargo |
833 |
|
160 |
stargo |
1254 |
packet = rdpsnd_queue_current_packet(); |
161 |
stargo |
835 |
out = &packet->s; |
162 |
stargo |
833 |
|
163 |
stargo |
1254 |
next_tick = rdpsnd_queue_next_tick(); |
164 |
stargo |
891 |
|
165 |
stargo |
1290 |
len = (WAVEOUTLEN > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTLEN; |
166 |
stargo |
1276 |
ao_play(o_device, (char *) out->p, len); |
167 |
|
|
out->p += len; |
168 |
stargo |
833 |
|
169 |
stargo |
891 |
gettimeofday(&tv, NULL); |
170 |
|
|
|
171 |
|
|
duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000; |
172 |
|
|
|
173 |
|
|
if (packet->tick > next_tick) |
174 |
|
|
next_tick += 65536; |
175 |
|
|
|
176 |
|
|
if ((out->p == out->end) || duration > next_tick - packet->tick + 500) |
177 |
stargo |
835 |
{ |
178 |
stargo |
1352 |
unsigned int delay_us; |
179 |
|
|
|
180 |
stargo |
891 |
prev_s = tv.tv_sec; |
181 |
|
|
prev_us = tv.tv_usec; |
182 |
|
|
|
183 |
|
|
if (abs((next_tick - packet->tick) - duration) > 20) |
184 |
|
|
{ |
185 |
|
|
DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick)); |
186 |
|
|
DEBUG(("last: %d, is: %d, should: %d\n", packet->tick, |
187 |
|
|
(packet->tick + duration) % 65536, next_tick % 65536)); |
188 |
|
|
} |
189 |
|
|
|
190 |
stargo |
1354 |
delay_us = ((out->size / 4) * (1000000 / 44100)); |
191 |
stargo |
1352 |
|
192 |
|
|
rdpsnd_queue_next(delay_us); |
193 |
stargo |
835 |
} |
194 |
stargo |
833 |
} |
195 |
stargo |
1255 |
|
196 |
stargo |
1351 |
struct audio_driver * |
197 |
|
|
libao_register(char *options) |
198 |
|
|
{ |
199 |
|
|
static struct audio_driver libao_driver; |
200 |
ossman_ |
1345 |
|
201 |
ossman_ |
1356 |
memset(&libao_driver, 0, sizeof(libao_driver)); |
202 |
ossman_ |
1346 |
|
203 |
ossman_ |
1356 |
libao_driver.name = "libao"; |
204 |
|
|
libao_driver.description = "libao output driver, default device: system dependent"; |
205 |
|
|
|
206 |
stargo |
1351 |
libao_driver.add_fds = libao_add_fds; |
207 |
|
|
libao_driver.check_fds = libao_check_fds; |
208 |
ossman_ |
1345 |
|
209 |
stargo |
1351 |
libao_driver.wave_out_open = libao_open; |
210 |
|
|
libao_driver.wave_out_close = libao_close; |
211 |
|
|
libao_driver.wave_out_format_supported = rdpsnd_dsp_resample_supported; |
212 |
|
|
libao_driver.wave_out_set_format = libao_set_format; |
213 |
|
|
libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set; |
214 |
ossman_ |
1345 |
|
215 |
stargo |
1351 |
libao_driver.need_byteswap_on_be = 1; |
216 |
|
|
libao_driver.need_resampling = 1; |
217 |
|
|
|
218 |
stargo |
1255 |
if (options) |
219 |
|
|
{ |
220 |
|
|
libao_device = xstrdup(options); |
221 |
|
|
} |
222 |
|
|
|
223 |
|
|
return &libao_driver; |
224 |
|
|
} |