1 | /* -*- c-basic-offset: 8 -*-
|
---|
2 | rdesktop: A Remote Desktop Protocol client.
|
---|
3 | Sound Channel Process Functions - libao-driver
|
---|
4 | Copyright (C) Matthew Chapman <matthewc.unsw.edu.au> 2003-2008
|
---|
5 | Copyright (C) GuoJunBo <[email protected]> 2003
|
---|
6 | Copyright (C) Michael Gernoth <[email protected]> 2005-2008
|
---|
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 3 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, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*
|
---|
23 | * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
24 | * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
|
---|
25 | * the General Public License version 2 (GPLv2) at this time for any software where
|
---|
26 | * a choice of GPL license versions is made available with the language indicating
|
---|
27 | * that GPLv2 or any later version may be used, or where a choice of which version
|
---|
28 | * of the GPL is applied is otherwise unspecified.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #include "rdesktop.h"
|
---|
32 | #include "rdpsnd.h"
|
---|
33 | #include "rdpsnd_dsp.h"
|
---|
34 | #include <unistd.h>
|
---|
35 | #include <fcntl.h>
|
---|
36 | #include <errno.h>
|
---|
37 | #include <ao/ao.h>
|
---|
38 | #include <sys/time.h>
|
---|
39 |
|
---|
40 | #define WAVEOUTLEN 16
|
---|
41 |
|
---|
42 | static ao_device *o_device = NULL;
|
---|
43 | static int default_driver;
|
---|
44 | static RD_BOOL reopened;
|
---|
45 | static char *libao_device = NULL;
|
---|
46 |
|
---|
47 | void libao_play(void);
|
---|
48 |
|
---|
49 | void
|
---|
50 | libao_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
|
---|
51 | {
|
---|
52 | /* We need to be called rather often... */
|
---|
53 | if (o_device != NULL && !rdpsnd_queue_empty())
|
---|
54 | FD_SET(0, wfds);
|
---|
55 | }
|
---|
56 |
|
---|
57 | void
|
---|
58 | libao_check_fds(fd_set * rfds, fd_set * wfds)
|
---|
59 | {
|
---|
60 | if (o_device == NULL)
|
---|
61 | return;
|
---|
62 |
|
---|
63 | if (!rdpsnd_queue_empty())
|
---|
64 | libao_play();
|
---|
65 | }
|
---|
66 |
|
---|
67 | RD_BOOL
|
---|
68 | libao_open(void)
|
---|
69 | {
|
---|
70 | ao_sample_format format;
|
---|
71 |
|
---|
72 | ao_initialize();
|
---|
73 |
|
---|
74 | if (libao_device)
|
---|
75 | {
|
---|
76 | default_driver = ao_driver_id(libao_device);
|
---|
77 | }
|
---|
78 | else
|
---|
79 | {
|
---|
80 | default_driver = ao_default_driver_id();
|
---|
81 | }
|
---|
82 |
|
---|
83 | format.bits = 16;
|
---|
84 | format.channels = 2;
|
---|
85 | format.rate = 44100;
|
---|
86 | format.byte_format = AO_FMT_NATIVE;
|
---|
87 |
|
---|
88 |
|
---|
89 | o_device = ao_open_live(default_driver, &format, NULL);
|
---|
90 | if (o_device == NULL)
|
---|
91 | {
|
---|
92 | return False;
|
---|
93 | }
|
---|
94 |
|
---|
95 | reopened = True;
|
---|
96 |
|
---|
97 | return True;
|
---|
98 | }
|
---|
99 |
|
---|
100 | void
|
---|
101 | libao_close(void)
|
---|
102 | {
|
---|
103 | /* Ack all remaining packets */
|
---|
104 | while (!rdpsnd_queue_empty())
|
---|
105 | {
|
---|
106 | rdpsnd_queue_next(0);
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (o_device != NULL)
|
---|
110 | ao_close(o_device);
|
---|
111 |
|
---|
112 | o_device = NULL;
|
---|
113 |
|
---|
114 | ao_shutdown();
|
---|
115 | }
|
---|
116 |
|
---|
117 | RD_BOOL
|
---|
118 | libao_set_format(RD_WAVEFORMATEX * pwfx)
|
---|
119 | {
|
---|
120 | ao_sample_format format;
|
---|
121 |
|
---|
122 | format.bits = pwfx->wBitsPerSample;
|
---|
123 | format.channels = pwfx->nChannels;
|
---|
124 | format.rate = 44100;
|
---|
125 | format.byte_format = AO_FMT_NATIVE;
|
---|
126 |
|
---|
127 | if (o_device != NULL)
|
---|
128 | ao_close(o_device);
|
---|
129 |
|
---|
130 | o_device = ao_open_live(default_driver, &format, NULL);
|
---|
131 | if (o_device == NULL)
|
---|
132 | {
|
---|
133 | return False;
|
---|
134 | }
|
---|
135 |
|
---|
136 | if (rdpsnd_dsp_resample_set(44100, pwfx->wBitsPerSample, pwfx->nChannels) == False)
|
---|
137 | {
|
---|
138 | return False;
|
---|
139 | }
|
---|
140 |
|
---|
141 | reopened = True;
|
---|
142 |
|
---|
143 | return True;
|
---|
144 | }
|
---|
145 |
|
---|
146 | void
|
---|
147 | libao_play(void)
|
---|
148 | {
|
---|
149 | struct audio_packet *packet;
|
---|
150 | STREAM out;
|
---|
151 | int len;
|
---|
152 | static long prev_s, prev_us;
|
---|
153 | unsigned int duration;
|
---|
154 | struct timeval tv;
|
---|
155 | int next_tick;
|
---|
156 |
|
---|
157 | if (reopened)
|
---|
158 | {
|
---|
159 | reopened = False;
|
---|
160 | gettimeofday(&tv, NULL);
|
---|
161 | prev_s = tv.tv_sec;
|
---|
162 | prev_us = tv.tv_usec;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /* We shouldn't be called if the queue is empty, but still */
|
---|
166 | if (rdpsnd_queue_empty())
|
---|
167 | return;
|
---|
168 |
|
---|
169 | packet = rdpsnd_queue_current_packet();
|
---|
170 | out = &packet->s;
|
---|
171 |
|
---|
172 | next_tick = rdpsnd_queue_next_tick();
|
---|
173 |
|
---|
174 | len = (WAVEOUTLEN > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTLEN;
|
---|
175 | ao_play(o_device, (char *) out->p, len);
|
---|
176 | out->p += len;
|
---|
177 |
|
---|
178 | gettimeofday(&tv, NULL);
|
---|
179 |
|
---|
180 | duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000;
|
---|
181 |
|
---|
182 | if (packet->tick > next_tick)
|
---|
183 | next_tick += 65536;
|
---|
184 |
|
---|
185 | if ((out->p == out->end) || duration > next_tick - packet->tick + 500)
|
---|
186 | {
|
---|
187 | unsigned int delay_us;
|
---|
188 |
|
---|
189 | prev_s = tv.tv_sec;
|
---|
190 | prev_us = tv.tv_usec;
|
---|
191 |
|
---|
192 | if (abs((next_tick - packet->tick) - duration) > 20)
|
---|
193 | {
|
---|
194 | DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick));
|
---|
195 | DEBUG(("last: %d, is: %d, should: %d\n", packet->tick,
|
---|
196 | (packet->tick + duration) % 65536, next_tick % 65536));
|
---|
197 | }
|
---|
198 |
|
---|
199 | delay_us = ((out->size / 4) * (1000000 / 44100));
|
---|
200 |
|
---|
201 | rdpsnd_queue_next(delay_us);
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | struct audio_driver *
|
---|
206 | libao_register(char *options)
|
---|
207 | {
|
---|
208 | static struct audio_driver libao_driver;
|
---|
209 |
|
---|
210 | memset(&libao_driver, 0, sizeof(libao_driver));
|
---|
211 |
|
---|
212 | libao_driver.name = "libao";
|
---|
213 | libao_driver.description = "libao output driver, default device: system dependent";
|
---|
214 |
|
---|
215 | libao_driver.add_fds = libao_add_fds;
|
---|
216 | libao_driver.check_fds = libao_check_fds;
|
---|
217 |
|
---|
218 | libao_driver.wave_out_open = libao_open;
|
---|
219 | libao_driver.wave_out_close = libao_close;
|
---|
220 | libao_driver.wave_out_format_supported = rdpsnd_dsp_resample_supported;
|
---|
221 | libao_driver.wave_out_set_format = libao_set_format;
|
---|
222 | libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set;
|
---|
223 |
|
---|
224 | libao_driver.need_byteswap_on_be = 1;
|
---|
225 | libao_driver.need_resampling = 1;
|
---|
226 |
|
---|
227 | if (options)
|
---|
228 | {
|
---|
229 | libao_device = xstrdup(options);
|
---|
230 | }
|
---|
231 |
|
---|
232 | return &libao_driver;
|
---|
233 | }
|
---|