/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1267 - (hide annotations)
Mon Sep 18 09:28:42 2006 UTC (17 years, 8 months ago) by stargo
File MIME type: text/plain
File size: 6148 byte(s)
indent fixes

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

  ViewVC Help
Powered by ViewVC 1.1.26