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