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