/[rdesktop]/sourceforge.net/trunk/rdesktop/rdpsnd_libao.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /sourceforge.net/trunk/rdesktop/rdpsnd_libao.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1256 - (show annotations)
Sun Sep 17 11:42:22 2006 UTC (17 years, 7 months ago) by stargo
File MIME type: text/plain
File size: 6064 byte(s)
fix SunCC errors/warnings and configure-variable-usage (LIBS/LDFLAGS)

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 <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <ao/ao.h>
29 #include <sys/time.h>
30
31 #define WAVEOUTBUF 16
32
33 static ao_device *o_device = NULL;
34 static int default_driver;
35 static int samplerate;
36 static int audiochannels;
37 static BOOL reopened;
38 static short samplewidth;
39 static char *libao_device = NULL;
40
41 BOOL
42 libao_open(void)
43 {
44 ao_sample_format format;
45 static int warned = 0;
46
47 if (!warned && libao_device)
48 {
49 warning("device-options not supported for libao-driver\n");
50 warned = 1;
51 }
52
53 ao_initialize();
54 default_driver = ao_default_driver_id();
55
56 format.bits = 16;
57 format.channels = 2;
58 audiochannels = 2;
59 format.rate = 44100;
60 samplerate = 44100;
61 format.byte_format = AO_FMT_LITTLE;
62
63 o_device = ao_open_live(default_driver, &format, NULL);
64 if (o_device == NULL)
65 {
66 return False;
67 }
68
69 g_dsp_fd = 0;
70 rdpsnd_queue_init();
71
72 reopened = True;
73
74 return True;
75 }
76
77 void
78 libao_close(void)
79 {
80 /* Ack all remaining packets */
81 while (!rdpsnd_queue_empty())
82 {
83 rdpsnd_send_completion(rdpsnd_queue_current_packet()->tick,
84 rdpsnd_queue_current_packet()->index);
85 rdpsnd_queue_next();
86 }
87
88 if (o_device != NULL)
89 ao_close(o_device);
90
91 ao_shutdown();
92 }
93
94 BOOL
95 libao_format_supported(WAVEFORMATEX * pwfx)
96 {
97 if (pwfx->wFormatTag != WAVE_FORMAT_PCM)
98 return False;
99 if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2))
100 return False;
101 if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16))
102 return False;
103 /* The only common denominator between libao output drivers is a sample-rate of
104 44100, we need to upsample 22050 to it */
105 if ((pwfx->nSamplesPerSec != 44100) && (pwfx->nSamplesPerSec != 22050))
106 return False;
107
108 return True;
109 }
110
111 BOOL
112 libao_set_format(WAVEFORMATEX * pwfx)
113 {
114 ao_sample_format format;
115
116 format.bits = pwfx->wBitsPerSample;
117 format.channels = pwfx->nChannels;
118 audiochannels = pwfx->nChannels;
119 format.rate = 44100;
120 samplerate = pwfx->nSamplesPerSec;
121 format.byte_format = AO_FMT_LITTLE;
122
123 samplewidth = pwfx->wBitsPerSample / 8;
124
125 if (o_device != NULL)
126 ao_close(o_device);
127
128 o_device = ao_open_live(default_driver, &format, NULL);
129 if (o_device == NULL)
130 {
131 return False;
132 }
133
134 reopened = True;
135
136 return True;
137 }
138
139 void
140 libao_volume(uint16 left, uint16 right)
141 {
142 warning("volume changes not supported with libao-output\n");
143 }
144
145 void
146 libao_play(void)
147 {
148 struct audio_packet *packet;
149 STREAM out;
150 char outbuf[WAVEOUTBUF];
151 int offset, len, i;
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 if (rdpsnd_queue_empty())
166 {
167 g_dsp_busy = 0;
168 return;
169 }
170
171 packet = rdpsnd_queue_current_packet();
172 out = &packet->s;
173
174 next_tick = rdpsnd_queue_next_tick();
175
176 len = 0;
177
178 if (samplerate == 22050)
179 {
180 /* Resample to 44100 */
181 for (i = 0; (i < ((WAVEOUTBUF / 4) * (3 - samplewidth))) && (out->p < out->end);
182 i++)
183 {
184 /* On a stereo-channel we must make sure that left and right
185 does not get mixed up, so we need to expand the sample-
186 data with channels in mind: 1234 -> 12123434
187 If we have a mono-channel, we can expand the data by simply
188 doubling the sample-data: 1234 -> 11223344 */
189 if (audiochannels == 2)
190 offset = ((i * 2) - (i & 1)) * samplewidth;
191 else
192 offset = (i * 2) * samplewidth;
193
194 memcpy(&outbuf[offset], out->p, samplewidth);
195 memcpy(&outbuf[audiochannels * samplewidth + offset], out->p, samplewidth);
196
197 out->p += samplewidth;
198 len += 2 * samplewidth;
199 }
200 }
201 else
202 {
203 len = (WAVEOUTBUF > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTBUF;
204 memcpy(outbuf, out->p, len);
205 out->p += len;
206 }
207
208 ao_play(o_device, outbuf, len);
209
210 gettimeofday(&tv, NULL);
211
212 duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000;
213
214 if (packet->tick > next_tick)
215 next_tick += 65536;
216
217 if ((out->p == out->end) || duration > next_tick - packet->tick + 500)
218 {
219 prev_s = tv.tv_sec;
220 prev_us = tv.tv_usec;
221
222 if (abs((next_tick - packet->tick) - duration) > 20)
223 {
224 DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick));
225 DEBUG(("last: %d, is: %d, should: %d\n", packet->tick,
226 (packet->tick + duration) % 65536, next_tick % 65536));
227 }
228
229 rdpsnd_send_completion(((packet->tick + duration) % 65536), packet->index);
230 rdpsnd_queue_next();
231 }
232
233 g_dsp_busy = 1;
234 return;
235 }
236
237 struct audio_driver *
238 libao_register(char *options)
239 {
240 static struct audio_driver libao_driver;
241
242 libao_driver.wave_out_write = rdpsnd_queue_write;
243 libao_driver.wave_out_open = libao_open;
244 libao_driver.wave_out_close = libao_close;
245 libao_driver.wave_out_format_supported = libao_format_supported;
246 libao_driver.wave_out_set_format = libao_set_format;
247 libao_driver.wave_out_volume = libao_volume;
248 libao_driver.wave_out_play = libao_play;
249 libao_driver.name = xstrdup("libao");
250 libao_driver.description = xstrdup("libao output driver");
251 libao_driver.next = NULL;
252
253 if (options)
254 {
255 libao_device = xstrdup(options);
256 }
257
258 return &libao_driver;
259 }

  ViewVC Help
Powered by ViewVC 1.1.26