/[rdesktop]/jpeg/rdesktop/trunk/rdpsnd_alsa.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 /jpeg/rdesktop/trunk/rdpsnd_alsa.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1302 - (hide annotations)
Thu Oct 26 09:47:17 2006 UTC (17 years, 7 months ago) by ossman_
Original Path: sourceforge.net/trunk/rdesktop/rdpsnd_alsa.c
File MIME type: text/plain
File size: 7151 byte(s)
Rewrite the queue management a bit so that blocks are not completed until
they have finished playing. This also makes the queue system mandatory for
all backends.

1 stargo 1253 /* -*- c-basic-offset: 8 -*-
2     rdesktop: A Remote Desktop Protocol client.
3     Sound Channel Process Functions - alsa-driver
4     Copyright (C) Matthew Chapman 2003
5     Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003
6     Copyright (C) Michael Gernoth mike@zerfleddert.de 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 stargo 1254 #include "rdpsnd.h"
25 stargo 1261 #include "rdpsnd_dsp.h"
26 stargo 1253 #include <unistd.h>
27     #include <fcntl.h>
28     #include <errno.h>
29     #include <alsa/asoundlib.h>
30     #include <sys/time.h>
31    
32     #define DEFAULTDEVICE "default"
33     #define MAX_FRAMES 32
34    
35     static snd_pcm_t *pcm_handle = NULL;
36     static snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
37     static BOOL reopened;
38     static short samplewidth;
39     static int audiochannels;
40 ossman_ 1302 static unsigned int rate;
41 stargo 1255 static char *pcm_name;
42 stargo 1253
43     BOOL
44 stargo 1255 alsa_open(void)
45 stargo 1253 {
46     int err;
47    
48     if ((err = snd_pcm_open(&pcm_handle, pcm_name, stream, 0)) < 0)
49     {
50     error("snd_pcm_open: %s\n", snd_strerror(err));
51     return False;
52     }
53    
54     g_dsp_fd = 0;
55    
56     reopened = True;
57    
58     return True;
59     }
60    
61     void
62 stargo 1255 alsa_close(void)
63 stargo 1253 {
64     /* Ack all remaining packets */
65 stargo 1254 while (!rdpsnd_queue_empty())
66 ossman_ 1302 rdpsnd_queue_next(0);
67 stargo 1253
68     if (pcm_handle)
69     {
70     snd_pcm_drop(pcm_handle);
71     snd_pcm_close(pcm_handle);
72     }
73     }
74    
75     BOOL
76 stargo 1255 alsa_format_supported(WAVEFORMATEX * pwfx)
77 stargo 1253 {
78     #if 0
79     int err;
80     snd_pcm_hw_params_t *hwparams = NULL;
81    
82     if ((err = snd_pcm_hw_params_malloc(&hwparams)) < 0)
83     {
84     error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err));
85     return False;
86     }
87    
88     if ((err = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0)
89     {
90     error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err));
91     return False;
92     }
93     snd_pcm_hw_params_free(hwparams);
94     #endif
95    
96     if (pwfx->wFormatTag != WAVE_FORMAT_PCM)
97     return False;
98     if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2))
99     return False;
100     if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16))
101     return False;
102     if ((pwfx->nSamplesPerSec != 44100) && (pwfx->nSamplesPerSec != 22050))
103     return False;
104    
105     return True;
106     }
107    
108     BOOL
109 stargo 1255 alsa_set_format(WAVEFORMATEX * pwfx)
110 stargo 1253 {
111     snd_pcm_hw_params_t *hwparams = NULL;
112     int err;
113     unsigned int buffertime;
114    
115     samplewidth = pwfx->wBitsPerSample / 8;
116    
117     if ((err = snd_pcm_hw_params_malloc(&hwparams)) < 0)
118     {
119     error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err));
120     return False;
121     }
122    
123     if ((err = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0)
124     {
125     error("snd_pcm_hw_params_any: %s\n", snd_strerror(err));
126     return False;
127     }
128    
129     if ((err =
130     snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
131     {
132     error("snd_pcm_hw_params_set_access: %s\n", snd_strerror(err));
133     return False;
134     }
135    
136     if (pwfx->wBitsPerSample == 16)
137     {
138     if ((err =
139     snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE)) < 0)
140     {
141     error("snd_pcm_hw_params_set_format: %s\n", snd_strerror(err));
142     return False;
143     }
144     }
145     else
146     {
147     if ((err =
148     snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S8)) < 0)
149     {
150     error("snd_pcm_hw_params_set_format: %s\n", snd_strerror(err));
151     return False;
152     }
153     }
154    
155     #if 0
156     if ((err = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 1)) < 0)
157     {
158     error("snd_pcm_hw_params_set_rate_resample: %s\n", snd_strerror(err));
159     return False;
160     }
161     #endif
162    
163 ossman_ 1302 rate = pwfx->nSamplesPerSec;
164     if ((err = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &rate, 0)) < 0)
165 stargo 1253 {
166     error("snd_pcm_hw_params_set_rate_near: %s\n", snd_strerror(err));
167     return False;
168     }
169    
170     audiochannels = pwfx->nChannels;
171     if ((err = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, pwfx->nChannels)) < 0)
172     {
173     error("snd_pcm_hw_params_set_channels: %s\n", snd_strerror(err));
174     return False;
175     }
176    
177    
178     buffertime = 500000; /* microseconds */
179     if ((err =
180     snd_pcm_hw_params_set_buffer_time_near(pcm_handle, hwparams, &buffertime, 0)) < 0)
181     {
182     error("snd_pcm_hw_params_set_buffer_time_near: %s\n", snd_strerror(err));
183     return False;
184     }
185    
186     if ((err = snd_pcm_hw_params(pcm_handle, hwparams)) < 0)
187     {
188     error("snd_pcm_hw_params: %s\n", snd_strerror(err));
189     return False;
190     }
191    
192     snd_pcm_hw_params_free(hwparams);
193    
194     if ((err = snd_pcm_prepare(pcm_handle)) < 0)
195     {
196     error("snd_pcm_prepare: %s\n", snd_strerror(err));
197     return False;
198     }
199    
200     reopened = True;
201    
202     return True;
203     }
204    
205     void
206 stargo 1255 alsa_play(void)
207 stargo 1253 {
208     struct audio_packet *packet;
209     STREAM out;
210     int len;
211     static long prev_s, prev_us;
212     unsigned int duration;
213     struct timeval tv;
214     int next_tick;
215    
216     if (reopened)
217     {
218     reopened = False;
219     gettimeofday(&tv, NULL);
220     prev_s = tv.tv_sec;
221     prev_us = tv.tv_usec;
222     }
223    
224 stargo 1254 if (rdpsnd_queue_empty())
225 stargo 1253 {
226     g_dsp_busy = 0;
227     return;
228     }
229    
230 stargo 1254 packet = rdpsnd_queue_current_packet();
231 stargo 1253 out = &packet->s;
232    
233 stargo 1254 next_tick = rdpsnd_queue_next_tick();
234 stargo 1253
235     len = (out->end - out->p) / (samplewidth * audiochannels);
236     if ((len = snd_pcm_writei(pcm_handle, out->p, ((MAX_FRAMES < len) ? MAX_FRAMES : len))) < 0)
237     {
238     snd_pcm_prepare(pcm_handle);
239     len = 0;
240     }
241     out->p += (len * samplewidth * audiochannels);
242    
243     gettimeofday(&tv, NULL);
244    
245     duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000;
246    
247     if (packet->tick > next_tick)
248     next_tick += 65536;
249    
250     if ((out->p == out->end) || duration > next_tick - packet->tick + 500)
251     {
252 ossman_ 1302 snd_pcm_sframes_t delay_frames;
253     unsigned long delay_us;
254    
255 stargo 1253 prev_s = tv.tv_sec;
256     prev_us = tv.tv_usec;
257    
258     if (abs((next_tick - packet->tick) - duration) > 20)
259     {
260     DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick));
261     DEBUG(("last: %d, is: %d, should: %d\n", packet->tick,
262     (packet->tick + duration) % 65536, next_tick % 65536));
263     }
264    
265 ossman_ 1302 if (snd_pcm_delay(pcm_handle, &delay_frames) < 0)
266     delay_frames = out->size / (samplewidth * audiochannels);
267     if (delay_frames < 0)
268     delay_frames = 0;
269    
270     delay_us = delay_frames * (1000000 / rate);
271    
272     rdpsnd_queue_next(delay_us);
273 stargo 1253 }
274    
275     g_dsp_busy = 1;
276     return;
277     }
278 stargo 1255
279     struct audio_driver *
280     alsa_register(char *options)
281     {
282 stargo 1256 static struct audio_driver alsa_driver;
283    
284     alsa_driver.wave_out_open = alsa_open;
285     alsa_driver.wave_out_close = alsa_close;
286     alsa_driver.wave_out_format_supported = alsa_format_supported;
287     alsa_driver.wave_out_set_format = alsa_set_format;
288 stargo 1261 alsa_driver.wave_out_volume = rdpsnd_dsp_softvol_set;
289 stargo 1256 alsa_driver.wave_out_play = alsa_play;
290     alsa_driver.name = xstrdup("alsa");
291     alsa_driver.description = xstrdup("ALSA output driver, default device: " DEFAULTDEVICE);
292 stargo 1260 alsa_driver.need_byteswap_on_be = 0;
293 stargo 1279 alsa_driver.need_resampling = 0;
294 stargo 1256 alsa_driver.next = NULL;
295    
296 stargo 1255 if (options)
297     {
298     pcm_name = xstrdup(options);
299     }
300     else
301     {
302     pcm_name = xstrdup(DEFAULTDEVICE);
303     }
304    
305     return &alsa_driver;
306     }

  ViewVC Help
Powered by ViewVC 1.1.26