1 |
/* -*- c-basic-offset: 8 -*- |
2 |
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 |
Copyright (C) Michael Gernoth mike@zerfleddert.de 2005-2006 |
7 |
|
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 |
#include "rdpsnd.h" |
25 |
#include "rdpsnd_dsp.h" |
26 |
#include <unistd.h> |
27 |
#include <fcntl.h> |
28 |
#include <errno.h> |
29 |
#include <ao/ao.h> |
30 |
#include <sys/time.h> |
31 |
|
32 |
#define WAVEOUTLEN 16 |
33 |
|
34 |
static ao_device *o_device = NULL; |
35 |
static int default_driver; |
36 |
static BOOL reopened; |
37 |
static char *libao_device = NULL; |
38 |
|
39 |
BOOL |
40 |
libao_open(void) |
41 |
{ |
42 |
ao_sample_format format; |
43 |
|
44 |
ao_initialize(); |
45 |
|
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; |
56 |
format.channels = 2; |
57 |
format.rate = 44100; |
58 |
format.byte_format = AO_FMT_NATIVE; |
59 |
|
60 |
|
61 |
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 |
|
69 |
reopened = True; |
70 |
|
71 |
return True; |
72 |
} |
73 |
|
74 |
void |
75 |
libao_close(void) |
76 |
{ |
77 |
/* Ack all remaining packets */ |
78 |
while (!rdpsnd_queue_empty()) |
79 |
{ |
80 |
rdpsnd_queue_next(0); |
81 |
} |
82 |
|
83 |
if (o_device != NULL) |
84 |
ao_close(o_device); |
85 |
|
86 |
ao_shutdown(); |
87 |
} |
88 |
|
89 |
BOOL |
90 |
libao_set_format(WAVEFORMATEX * pwfx) |
91 |
{ |
92 |
ao_sample_format format; |
93 |
|
94 |
format.bits = pwfx->wBitsPerSample; |
95 |
format.channels = pwfx->nChannels; |
96 |
format.rate = 44100; |
97 |
format.byte_format = AO_FMT_NATIVE; |
98 |
|
99 |
if (o_device != NULL) |
100 |
ao_close(o_device); |
101 |
|
102 |
o_device = ao_open_live(default_driver, &format, NULL); |
103 |
if (o_device == NULL) |
104 |
{ |
105 |
return False; |
106 |
} |
107 |
|
108 |
if (rdpsnd_dsp_resample_set(44100, pwfx->wBitsPerSample, pwfx->nChannels) == False) |
109 |
{ |
110 |
return False; |
111 |
} |
112 |
|
113 |
reopened = True; |
114 |
|
115 |
return True; |
116 |
} |
117 |
|
118 |
void |
119 |
libao_play(void) |
120 |
{ |
121 |
struct audio_packet *packet; |
122 |
STREAM out; |
123 |
int len; |
124 |
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 (rdpsnd_queue_empty()) |
138 |
{ |
139 |
g_dsp_busy = 0; |
140 |
return; |
141 |
} |
142 |
|
143 |
packet = rdpsnd_queue_current_packet(); |
144 |
out = &packet->s; |
145 |
|
146 |
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 (packet->tick > next_tick) |
157 |
next_tick += 65536; |
158 |
|
159 |
if ((out->p == out->end) || duration > next_tick - packet->tick + 500) |
160 |
{ |
161 |
prev_s = tv.tv_sec; |
162 |
prev_us = tv.tv_usec; |
163 |
|
164 |
if (abs((next_tick - packet->tick) - duration) > 20) |
165 |
{ |
166 |
DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick)); |
167 |
DEBUG(("last: %d, is: %d, should: %d\n", packet->tick, |
168 |
(packet->tick + duration) % 65536, next_tick % 65536)); |
169 |
} |
170 |
|
171 |
rdpsnd_queue_next(duration); |
172 |
} |
173 |
|
174 |
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 |
struct ao_info *libao_info; |
183 |
static char description[101]; |
184 |
|
185 |
libao_driver.wave_out_open = libao_open; |
186 |
libao_driver.wave_out_close = libao_close; |
187 |
libao_driver.wave_out_format_supported = rdpsnd_dsp_resample_supported; |
188 |
libao_driver.wave_out_set_format = libao_set_format; |
189 |
libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set; |
190 |
libao_driver.wave_out_play = libao_play; |
191 |
libao_driver.name = xstrdup("libao"); |
192 |
libao_driver.description = description; |
193 |
libao_driver.need_byteswap_on_be = 1; |
194 |
libao_driver.need_resampling = 1; |
195 |
libao_driver.next = NULL; |
196 |
|
197 |
ao_initialize(); |
198 |
|
199 |
libao_info = ao_driver_info(ao_default_driver_id()); |
200 |
|
201 |
if (libao_info) |
202 |
{ |
203 |
snprintf(description, 100, "libao output driver, default device: %s", |
204 |
libao_info->short_name); |
205 |
} |
206 |
else |
207 |
{ |
208 |
snprintf(description, 100, "libao output driver, default device: none"); |
209 |
} |
210 |
|
211 |
ao_shutdown(); |
212 |
|
213 |
if (options) |
214 |
{ |
215 |
libao_device = xstrdup(options); |
216 |
} |
217 |
|
218 |
return &libao_driver; |
219 |
} |