1 |
astrand |
963 |
/* -*- c-basic-offset: 8 -*- |
2 |
stargo |
833 |
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
Sound Channel Process Functions - libao-driver |
4 |
jsorg71 |
1365 |
Copyright (C) Matthew Chapman 2003-2007 |
5 |
stargo |
833 |
Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003 |
6 |
jsorg71 |
1365 |
Copyright (C) Michael Gernoth mike@zerfleddert.de 2005-2007 |
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 |
stargo |
1373 |
static RD_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 |
stargo |
1422 |
if (o_device != NULL && !rdpsnd_queue_empty()) |
46 |
|
|
FD_SET(0, wfds); |
47 |
ossman_ |
1346 |
} |
48 |
|
|
|
49 |
|
|
void |
50 |
|
|
libao_check_fds(fd_set * rfds, fd_set * wfds) |
51 |
|
|
{ |
52 |
|
|
if (o_device == NULL) |
53 |
|
|
return; |
54 |
|
|
|
55 |
|
|
if (!rdpsnd_queue_empty()) |
56 |
|
|
libao_play(); |
57 |
|
|
} |
58 |
|
|
|
59 |
stargo |
1373 |
RD_BOOL |
60 |
stargo |
1255 |
libao_open(void) |
61 |
stargo |
833 |
{ |
62 |
|
|
ao_sample_format format; |
63 |
|
|
|
64 |
stargo |
1266 |
ao_initialize(); |
65 |
|
|
|
66 |
|
|
if (libao_device) |
67 |
stargo |
1255 |
{ |
68 |
stargo |
1266 |
default_driver = ao_driver_id(libao_device); |
69 |
stargo |
1255 |
} |
70 |
stargo |
1266 |
else |
71 |
|
|
{ |
72 |
|
|
default_driver = ao_default_driver_id(); |
73 |
|
|
} |
74 |
stargo |
1255 |
|
75 |
stargo |
833 |
format.bits = 16; |
76 |
|
|
format.channels = 2; |
77 |
|
|
format.rate = 44100; |
78 |
stargo |
1289 |
format.byte_format = AO_FMT_NATIVE; |
79 |
stargo |
833 |
|
80 |
stargo |
1289 |
|
81 |
stargo |
833 |
o_device = ao_open_live(default_driver, &format, NULL); |
82 |
|
|
if (o_device == NULL) |
83 |
|
|
{ |
84 |
|
|
return False; |
85 |
|
|
} |
86 |
|
|
|
87 |
stargo |
1245 |
reopened = True; |
88 |
stargo |
891 |
|
89 |
stargo |
833 |
return True; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
void |
93 |
stargo |
1255 |
libao_close(void) |
94 |
stargo |
833 |
{ |
95 |
|
|
/* Ack all remaining packets */ |
96 |
stargo |
1254 |
while (!rdpsnd_queue_empty()) |
97 |
stargo |
833 |
{ |
98 |
ossman_ |
1302 |
rdpsnd_queue_next(0); |
99 |
stargo |
833 |
} |
100 |
|
|
|
101 |
|
|
if (o_device != NULL) |
102 |
|
|
ao_close(o_device); |
103 |
stargo |
838 |
|
104 |
ossman_ |
1346 |
o_device = NULL; |
105 |
|
|
|
106 |
stargo |
833 |
ao_shutdown(); |
107 |
|
|
} |
108 |
|
|
|
109 |
stargo |
1373 |
RD_BOOL |
110 |
jsorg71 |
1364 |
libao_set_format(RD_WAVEFORMATEX * pwfx) |
111 |
stargo |
833 |
{ |
112 |
|
|
ao_sample_format format; |
113 |
|
|
|
114 |
|
|
format.bits = pwfx->wBitsPerSample; |
115 |
|
|
format.channels = pwfx->nChannels; |
116 |
|
|
format.rate = 44100; |
117 |
stargo |
1289 |
format.byte_format = AO_FMT_NATIVE; |
118 |
stargo |
833 |
|
119 |
stargo |
837 |
if (o_device != NULL) |
120 |
stargo |
833 |
ao_close(o_device); |
121 |
|
|
|
122 |
|
|
o_device = ao_open_live(default_driver, &format, NULL); |
123 |
|
|
if (o_device == NULL) |
124 |
|
|
{ |
125 |
|
|
return False; |
126 |
|
|
} |
127 |
|
|
|
128 |
stargo |
1276 |
if (rdpsnd_dsp_resample_set(44100, pwfx->wBitsPerSample, pwfx->nChannels) == False) |
129 |
|
|
{ |
130 |
|
|
return False; |
131 |
|
|
} |
132 |
|
|
|
133 |
stargo |
1245 |
reopened = True; |
134 |
stargo |
833 |
|
135 |
|
|
return True; |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
void |
139 |
stargo |
1255 |
libao_play(void) |
140 |
stargo |
833 |
{ |
141 |
|
|
struct audio_packet *packet; |
142 |
|
|
STREAM out; |
143 |
stargo |
1276 |
int len; |
144 |
stargo |
891 |
static long prev_s, prev_us; |
145 |
|
|
unsigned int duration; |
146 |
|
|
struct timeval tv; |
147 |
|
|
int next_tick; |
148 |
stargo |
833 |
|
149 |
stargo |
1245 |
if (reopened) |
150 |
stargo |
891 |
{ |
151 |
stargo |
1245 |
reopened = False; |
152 |
stargo |
891 |
gettimeofday(&tv, NULL); |
153 |
|
|
prev_s = tv.tv_sec; |
154 |
|
|
prev_us = tv.tv_usec; |
155 |
|
|
} |
156 |
|
|
|
157 |
ossman_ |
1346 |
/* We shouldn't be called if the queue is empty, but still */ |
158 |
stargo |
1254 |
if (rdpsnd_queue_empty()) |
159 |
stargo |
835 |
return; |
160 |
stargo |
833 |
|
161 |
stargo |
1254 |
packet = rdpsnd_queue_current_packet(); |
162 |
stargo |
835 |
out = &packet->s; |
163 |
stargo |
833 |
|
164 |
stargo |
1254 |
next_tick = rdpsnd_queue_next_tick(); |
165 |
stargo |
891 |
|
166 |
stargo |
1290 |
len = (WAVEOUTLEN > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTLEN; |
167 |
stargo |
1276 |
ao_play(o_device, (char *) out->p, len); |
168 |
|
|
out->p += len; |
169 |
stargo |
833 |
|
170 |
stargo |
891 |
gettimeofday(&tv, NULL); |
171 |
|
|
|
172 |
|
|
duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000; |
173 |
|
|
|
174 |
|
|
if (packet->tick > next_tick) |
175 |
|
|
next_tick += 65536; |
176 |
|
|
|
177 |
|
|
if ((out->p == out->end) || duration > next_tick - packet->tick + 500) |
178 |
stargo |
835 |
{ |
179 |
stargo |
1352 |
unsigned int delay_us; |
180 |
|
|
|
181 |
stargo |
891 |
prev_s = tv.tv_sec; |
182 |
|
|
prev_us = tv.tv_usec; |
183 |
|
|
|
184 |
|
|
if (abs((next_tick - packet->tick) - duration) > 20) |
185 |
|
|
{ |
186 |
|
|
DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick)); |
187 |
|
|
DEBUG(("last: %d, is: %d, should: %d\n", packet->tick, |
188 |
|
|
(packet->tick + duration) % 65536, next_tick % 65536)); |
189 |
|
|
} |
190 |
|
|
|
191 |
stargo |
1354 |
delay_us = ((out->size / 4) * (1000000 / 44100)); |
192 |
stargo |
1352 |
|
193 |
|
|
rdpsnd_queue_next(delay_us); |
194 |
stargo |
835 |
} |
195 |
stargo |
833 |
} |
196 |
stargo |
1255 |
|
197 |
stargo |
1351 |
struct audio_driver * |
198 |
|
|
libao_register(char *options) |
199 |
|
|
{ |
200 |
|
|
static struct audio_driver libao_driver; |
201 |
ossman_ |
1345 |
|
202 |
ossman_ |
1356 |
memset(&libao_driver, 0, sizeof(libao_driver)); |
203 |
ossman_ |
1346 |
|
204 |
ossman_ |
1356 |
libao_driver.name = "libao"; |
205 |
|
|
libao_driver.description = "libao output driver, default device: system dependent"; |
206 |
|
|
|
207 |
stargo |
1351 |
libao_driver.add_fds = libao_add_fds; |
208 |
|
|
libao_driver.check_fds = libao_check_fds; |
209 |
ossman_ |
1345 |
|
210 |
stargo |
1351 |
libao_driver.wave_out_open = libao_open; |
211 |
|
|
libao_driver.wave_out_close = libao_close; |
212 |
|
|
libao_driver.wave_out_format_supported = rdpsnd_dsp_resample_supported; |
213 |
|
|
libao_driver.wave_out_set_format = libao_set_format; |
214 |
|
|
libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set; |
215 |
ossman_ |
1345 |
|
216 |
stargo |
1351 |
libao_driver.need_byteswap_on_be = 1; |
217 |
|
|
libao_driver.need_resampling = 1; |
218 |
|
|
|
219 |
stargo |
1255 |
if (options) |
220 |
|
|
{ |
221 |
|
|
libao_device = xstrdup(options); |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
return &libao_driver; |
225 |
|
|
} |