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 <guojunbo@ict.ac.cn> 2003
|
---|
6 | Copyright (C) Michael Gernoth <mike@zerfleddert.de> 2005-2008
|
---|
7 | Copyright (C) 2013 Henrik Andersson
|
---|
8 |
|
---|
9 | This program is free software: you can redistribute it and/or modify
|
---|
10 | it under the terms of the GNU General Public License as published by
|
---|
11 | the Free Software Foundation, either version 3 of the License, or
|
---|
12 | (at your option) any later version.
|
---|
13 |
|
---|
14 | This program is distributed in the hope that it will be useful,
|
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | GNU General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU General Public License
|
---|
20 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
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 RD_BOOL reopened;
|
---|
37 | static char *libao_device = NULL;
|
---|
38 |
|
---|
39 | void libao_play(void);
|
---|
40 |
|
---|
41 | void
|
---|
42 | libao_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
|
---|
43 | {
|
---|
44 | /* We need to be called rather often... */
|
---|
45 | if (o_device != NULL && !rdpsnd_queue_empty())
|
---|
46 | FD_SET(0, wfds);
|
---|
47 | }
|
---|
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 | RD_BOOL
|
---|
60 | libao_open(void)
|
---|
61 | {
|
---|
62 | ao_sample_format format;
|
---|
63 |
|
---|
64 | ao_initialize();
|
---|
65 |
|
---|
66 | if (libao_device)
|
---|
67 | {
|
---|
68 | default_driver = ao_driver_id(libao_device);
|
---|
69 | }
|
---|
70 | else
|
---|
71 | {
|
---|
72 | default_driver = ao_default_driver_id();
|
---|
73 | }
|
---|
74 |
|
---|
75 | memset(&format, 0, sizeof(format));
|
---|
76 | format.bits = 16;
|
---|
77 | format.channels = 2;
|
---|
78 | format.rate = 44100;
|
---|
79 | format.byte_format = AO_FMT_NATIVE;
|
---|
80 |
|
---|
81 | o_device = ao_open_live(default_driver, &format, NULL);
|
---|
82 | if (o_device == NULL)
|
---|
83 | {
|
---|
84 | return False;
|
---|
85 | }
|
---|
86 |
|
---|
87 | reopened = True;
|
---|
88 |
|
---|
89 | return True;
|
---|
90 | }
|
---|
91 |
|
---|
92 | void
|
---|
93 | libao_close(void)
|
---|
94 | {
|
---|
95 | /* Ack all remaining packets */
|
---|
96 | while (!rdpsnd_queue_empty())
|
---|
97 | {
|
---|
98 | rdpsnd_queue_next(0);
|
---|
99 | }
|
---|
100 |
|
---|
101 | if (o_device != NULL)
|
---|
102 | ao_close(o_device);
|
---|
103 |
|
---|
104 | o_device = NULL;
|
---|
105 |
|
---|
106 | ao_shutdown();
|
---|
107 | }
|
---|
108 |
|
---|
109 | RD_BOOL
|
---|
110 | libao_set_format(RD_WAVEFORMATEX * pwfx)
|
---|
111 | {
|
---|
112 | ao_sample_format format;
|
---|
113 |
|
---|
114 | memset(&format, 0, sizeof(format));
|
---|
115 | format.bits = pwfx->wBitsPerSample;
|
---|
116 | format.channels = pwfx->nChannels;
|
---|
117 | format.rate = 44100;
|
---|
118 | format.byte_format = AO_FMT_NATIVE;
|
---|
119 |
|
---|
120 | if (o_device != NULL)
|
---|
121 | ao_close(o_device);
|
---|
122 |
|
---|
123 | o_device = ao_open_live(default_driver, &format, NULL);
|
---|
124 | if (o_device == NULL)
|
---|
125 | {
|
---|
126 | return False;
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (rdpsnd_dsp_resample_set(44100, pwfx->wBitsPerSample, pwfx->nChannels) == False)
|
---|
130 | {
|
---|
131 | return False;
|
---|
132 | }
|
---|
133 |
|
---|
134 | reopened = True;
|
---|
135 |
|
---|
136 | return True;
|
---|
137 | }
|
---|
138 |
|
---|
139 | void
|
---|
140 | libao_play(void)
|
---|
141 | {
|
---|
142 | struct audio_packet *packet;
|
---|
143 | STREAM out;
|
---|
144 | int len;
|
---|
145 | static long prev_s, prev_us;
|
---|
146 | unsigned int duration;
|
---|
147 | struct timeval tv;
|
---|
148 | int next_tick;
|
---|
149 |
|
---|
150 | if (reopened)
|
---|
151 | {
|
---|
152 | reopened = False;
|
---|
153 | gettimeofday(&tv, NULL);
|
---|
154 | prev_s = tv.tv_sec;
|
---|
155 | prev_us = tv.tv_usec;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /* We shouldn't be called if the queue is empty, but still */
|
---|
159 | if (rdpsnd_queue_empty())
|
---|
160 | return;
|
---|
161 |
|
---|
162 | packet = rdpsnd_queue_current_packet();
|
---|
163 | out = &packet->s;
|
---|
164 |
|
---|
165 | next_tick = rdpsnd_queue_next_tick();
|
---|
166 |
|
---|
167 | len = (WAVEOUTLEN > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTLEN;
|
---|
168 | ao_play(o_device, (char *) out->p, len);
|
---|
169 | out->p += len;
|
---|
170 |
|
---|
171 | gettimeofday(&tv, NULL);
|
---|
172 |
|
---|
173 | duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000;
|
---|
174 |
|
---|
175 | if (packet->tick > next_tick)
|
---|
176 | next_tick += 65536;
|
---|
177 |
|
---|
178 | if ((out->p == out->end) || duration > next_tick - packet->tick + 500)
|
---|
179 | {
|
---|
180 | unsigned int delay_us;
|
---|
181 |
|
---|
182 | prev_s = tv.tv_sec;
|
---|
183 | prev_us = tv.tv_usec;
|
---|
184 |
|
---|
185 | if (abs((next_tick - packet->tick) - duration) > 20)
|
---|
186 | {
|
---|
187 | DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick));
|
---|
188 | DEBUG(("last: %d, is: %d, should: %d\n", packet->tick,
|
---|
189 | (packet->tick + duration) % 65536, next_tick % 65536));
|
---|
190 | }
|
---|
191 |
|
---|
192 | delay_us = ((out->size / 4) * (1000000 / 44100));
|
---|
193 |
|
---|
194 | rdpsnd_queue_next(delay_us);
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | struct audio_driver *
|
---|
199 | libao_register(char *options)
|
---|
200 | {
|
---|
201 | static struct audio_driver libao_driver;
|
---|
202 |
|
---|
203 | memset(&libao_driver, 0, sizeof(libao_driver));
|
---|
204 |
|
---|
205 | libao_driver.name = "libao";
|
---|
206 | libao_driver.description = "libao output driver, default device: system dependent";
|
---|
207 |
|
---|
208 | libao_driver.add_fds = libao_add_fds;
|
---|
209 | libao_driver.check_fds = libao_check_fds;
|
---|
210 |
|
---|
211 | libao_driver.wave_out_open = libao_open;
|
---|
212 | libao_driver.wave_out_close = libao_close;
|
---|
213 | libao_driver.wave_out_format_supported = rdpsnd_dsp_resample_supported;
|
---|
214 | libao_driver.wave_out_set_format = libao_set_format;
|
---|
215 | libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set;
|
---|
216 |
|
---|
217 | libao_driver.need_byteswap_on_be = 1;
|
---|
218 | libao_driver.need_resampling = 1;
|
---|
219 |
|
---|
220 | if (options)
|
---|
221 | {
|
---|
222 | libao_device = xstrdup(options);
|
---|
223 | }
|
---|
224 |
|
---|
225 | return &libao_driver;
|
---|
226 | }
|
---|