VirtualBox

source: vbox/trunk/src/VBox/RDP/client/rdpsnd_alsa.c@ 33656

Last change on this file since 33656 was 33656, checked in by vboxsync, 14 years ago

*: rebrand Sun (L)GPL disclaimers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.5 KB
Line 
1/* -*- c-basic-offset: 8 -*-
2 rdesktop: A Remote Desktop Protocol client.
3 Sound Channel Process Functions - alsa-driver
4 Copyright (C) Matthew Chapman 2003-2007
5 Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003
6 Copyright (C) Michael Gernoth mike@zerfleddert.de 2006-2007
7 Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24/*
25 * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
26 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
27 * the General Public License version 2 (GPLv2) at this time for any software where
28 * a choice of GPL license versions is made available with the language indicating
29 * that GPLv2 or any later version may be used, or where a choice of which version
30 * of the GPL is applied is otherwise unspecified.
31 */
32
33#include "rdesktop.h"
34#include "rdpsnd.h"
35#include "rdpsnd_dsp.h"
36#include <unistd.h>
37#include <fcntl.h>
38#include <errno.h>
39#include <alsa/asoundlib.h>
40#include <sys/time.h>
41
42#define DEFAULTDEVICE "default"
43#define MAX_FRAMES 32
44
45static struct pollfd pfds_out[32];
46static int num_fds_out;
47
48static struct pollfd pfds_in[32];
49static int num_fds_in;
50
51static snd_pcm_t *out_handle = NULL;
52static snd_pcm_t *in_handle = NULL;
53
54static RD_BOOL reopened;
55
56static short samplewidth_out;
57static int audiochannels_out;
58static unsigned int rate_out;
59
60static short samplewidth_in;
61static int audiochannels_in;
62static unsigned int rate_in;
63
64static char *pcm_name;
65
66void alsa_play(void);
67void alsa_record(void);
68
69void
70alsa_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
71{
72 int err;
73 struct pollfd *f;
74
75 if (out_handle && !rdpsnd_queue_empty())
76 {
77 num_fds_out = snd_pcm_poll_descriptors_count(out_handle);
78
79 if (num_fds_out > sizeof(pfds_out) / sizeof(*pfds_out))
80 return;
81
82 err = snd_pcm_poll_descriptors(out_handle, pfds_out, num_fds_out);
83 if (err < 0)
84 return;
85
86 for (f = pfds_out; f < &pfds_out[num_fds_out]; f++)
87 {
88 if (f->events & POLLIN)
89 FD_SET(f->fd, rfds);
90 if (f->events & POLLOUT)
91 FD_SET(f->fd, wfds);
92 if (f->fd > *n && (f->events & (POLLIN | POLLOUT)))
93 *n = f->fd;
94 }
95 }
96
97 if (in_handle)
98 {
99 num_fds_in = snd_pcm_poll_descriptors_count(in_handle);
100
101 if (num_fds_in > sizeof(pfds_in) / sizeof(*pfds_in))
102 return;
103
104 err = snd_pcm_poll_descriptors(in_handle, pfds_in, num_fds_in);
105 if (err < 0)
106 return;
107
108 for (f = pfds_in; f < &pfds_in[num_fds_in]; f++)
109 {
110 if (f->events & POLLIN)
111 FD_SET(f->fd, rfds);
112 if (f->events & POLLOUT)
113 FD_SET(f->fd, wfds);
114 if (f->fd > *n && (f->events & (POLLIN | POLLOUT)))
115 *n = f->fd;
116 }
117 }
118}
119
120void
121alsa_check_fds(fd_set * rfds, fd_set * wfds)
122{
123 struct pollfd *f;
124 int err;
125 unsigned short revents;
126
127 if (out_handle && !rdpsnd_queue_empty())
128 {
129 for (f = pfds_out; f < &pfds_out[num_fds_out]; f++)
130 {
131 f->revents = 0;
132 if (f->fd != -1)
133 {
134 /* Fixme: This doesn't properly deal with things like POLLHUP */
135 if (FD_ISSET(f->fd, rfds))
136 f->revents |= POLLIN;
137 if (FD_ISSET(f->fd, wfds))
138 f->revents |= POLLOUT;
139 }
140 }
141
142 err = snd_pcm_poll_descriptors_revents(out_handle, pfds_out, num_fds_out, &revents);
143 if (err < 0)
144 return;
145
146 if (revents & POLLOUT)
147 alsa_play();
148 }
149
150
151 if (in_handle)
152 {
153 for (f = pfds_in; f < &pfds_in[num_fds_in]; f++)
154 {
155 f->revents = 0;
156 if (f->fd != -1)
157 {
158 /* Fixme: This doesn't properly deal with things like POLLHUP */
159 if (FD_ISSET(f->fd, rfds))
160 f->revents |= POLLIN;
161 if (FD_ISSET(f->fd, wfds))
162 f->revents |= POLLOUT;
163 }
164 }
165
166 err = snd_pcm_poll_descriptors_revents(in_handle, pfds_in, num_fds_in, &revents);
167 if (err < 0)
168 return;
169
170 if (revents & POLLIN)
171 alsa_record();
172 }
173}
174
175static RD_BOOL
176alsa_set_format(snd_pcm_t * pcm, RD_WAVEFORMATEX * pwfx)
177{
178 snd_pcm_hw_params_t *hwparams = NULL;
179 int err;
180 unsigned int buffertime;
181 short samplewidth;
182 int audiochannels;
183 unsigned int rate;
184
185 samplewidth = pwfx->wBitsPerSample / 8;
186
187 if ((err = snd_pcm_hw_params_malloc(&hwparams)) < 0)
188 {
189 error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err));
190 return False;
191 }
192
193 if ((err = snd_pcm_hw_params_any(pcm, hwparams)) < 0)
194 {
195 error("snd_pcm_hw_params_any: %s\n", snd_strerror(err));
196 return False;
197 }
198
199 if ((err = snd_pcm_hw_params_set_access(pcm, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
200 {
201 error("snd_pcm_hw_params_set_access: %s\n", snd_strerror(err));
202 return False;
203 }
204
205 if (pwfx->wBitsPerSample == 16)
206 {
207 if ((err = snd_pcm_hw_params_set_format(pcm, hwparams, SND_PCM_FORMAT_S16_LE)) < 0)
208 {
209 error("snd_pcm_hw_params_set_format: %s\n", snd_strerror(err));
210 return False;
211 }
212 }
213 else
214 {
215 if ((err = snd_pcm_hw_params_set_format(pcm, hwparams, SND_PCM_FORMAT_S8)) < 0)
216 {
217 error("snd_pcm_hw_params_set_format: %s\n", snd_strerror(err));
218 return False;
219 }
220 }
221
222#if 0
223 if ((err = snd_pcm_hw_params_set_rate_resample(pcm, hwparams, 1)) < 0)
224 {
225 error("snd_pcm_hw_params_set_rate_resample: %s\n", snd_strerror(err));
226 return False;
227 }
228#endif
229
230 rate = pwfx->nSamplesPerSec;
231 if ((err = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, 0)) < 0)
232 {
233 error("snd_pcm_hw_params_set_rate_near: %s\n", snd_strerror(err));
234 return False;
235 }
236
237 audiochannels = pwfx->nChannels;
238 if ((err = snd_pcm_hw_params_set_channels(pcm, hwparams, pwfx->nChannels)) < 0)
239 {
240 error("snd_pcm_hw_params_set_channels: %s\n", snd_strerror(err));
241 return False;
242 }
243
244
245 buffertime = 500000; /* microseconds */
246 if ((err = snd_pcm_hw_params_set_buffer_time_near(pcm, hwparams, &buffertime, 0)) < 0)
247 {
248 error("snd_pcm_hw_params_set_buffer_time_near: %s\n", snd_strerror(err));
249 return False;
250 }
251
252 if ((err = snd_pcm_hw_params(pcm, hwparams)) < 0)
253 {
254 error("snd_pcm_hw_params: %s\n", snd_strerror(err));
255 return False;
256 }
257
258 snd_pcm_hw_params_free(hwparams);
259
260 if ((err = snd_pcm_prepare(pcm)) < 0)
261 {
262 error("snd_pcm_prepare: %s\n", snd_strerror(err));
263 return False;
264 }
265
266 reopened = True;
267
268 return True;
269}
270
271RD_BOOL
272alsa_open_out(void)
273{
274 int err;
275
276 if ((err = snd_pcm_open(&out_handle, pcm_name, SND_PCM_STREAM_PLAYBACK, 0)) < 0)
277 {
278 error("snd_pcm_open: %s\n", snd_strerror(err));
279 return False;
280 }
281
282 reopened = True;
283
284 return True;
285}
286
287void
288alsa_close_out(void)
289{
290 /* Ack all remaining packets */
291 while (!rdpsnd_queue_empty())
292 rdpsnd_queue_next(0);
293
294 if (out_handle)
295 {
296 snd_pcm_close(out_handle);
297 out_handle = NULL;
298 }
299}
300
301RD_BOOL
302alsa_format_supported(RD_WAVEFORMATEX * pwfx)
303{
304#if 0
305 int err;
306 snd_pcm_hw_params_t *hwparams = NULL;
307
308 if ((err = snd_pcm_hw_params_malloc(&hwparams)) < 0)
309 {
310 error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err));
311 return False;
312 }
313
314 if ((err = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0)
315 {
316 error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err));
317 return False;
318 }
319 snd_pcm_hw_params_free(hwparams);
320#endif
321
322 if (pwfx->wFormatTag != WAVE_FORMAT_PCM)
323 return False;
324 if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2))
325 return False;
326 if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16))
327 return False;
328 if ((pwfx->nSamplesPerSec != 44100) && (pwfx->nSamplesPerSec != 22050))
329 return False;
330
331 return True;
332}
333
334RD_BOOL
335alsa_set_format_out(RD_WAVEFORMATEX * pwfx)
336{
337 if (!alsa_set_format(out_handle, pwfx))
338 return False;
339
340 samplewidth_out = pwfx->wBitsPerSample / 8;
341 audiochannels_out = pwfx->nChannels;
342 rate_out = pwfx->nSamplesPerSec;
343
344 return True;
345}
346
347void
348alsa_play(void)
349{
350 struct audio_packet *packet;
351 STREAM out;
352 int len;
353 static long prev_s, prev_us;
354 unsigned int duration;
355 struct timeval tv;
356 int next_tick;
357
358 if (reopened)
359 {
360 reopened = False;
361 gettimeofday(&tv, NULL);
362 prev_s = tv.tv_sec;
363 prev_us = tv.tv_usec;
364 }
365
366 /* We shouldn't be called if the queue is empty, but still */
367 if (rdpsnd_queue_empty())
368 return;
369
370 packet = rdpsnd_queue_current_packet();
371 out = &packet->s;
372
373 next_tick = rdpsnd_queue_next_tick();
374
375 len = (out->end - out->p) / (samplewidth_out * audiochannels_out);
376 if ((len = snd_pcm_writei(out_handle, out->p, ((MAX_FRAMES < len) ? MAX_FRAMES : len))) < 0)
377 {
378 printf("Fooo!\n");
379 snd_pcm_prepare(out_handle);
380 len = 0;
381 }
382 out->p += (len * samplewidth_out * audiochannels_out);
383
384 gettimeofday(&tv, NULL);
385
386 duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000;
387
388 if (packet->tick > next_tick)
389 next_tick += 65536;
390
391 if ((out->p == out->end) || duration > next_tick - packet->tick + 500)
392 {
393 snd_pcm_sframes_t delay_frames;
394 unsigned long delay_us;
395
396 prev_s = tv.tv_sec;
397 prev_us = tv.tv_usec;
398
399 if (abs((next_tick - packet->tick) - duration) > 20)
400 {
401 DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick));
402 DEBUG(("last: %d, is: %d, should: %d\n", packet->tick,
403 (packet->tick + duration) % 65536, next_tick % 65536));
404 }
405
406 if (snd_pcm_delay(out_handle, &delay_frames) < 0)
407 delay_frames = out->size / (samplewidth_out * audiochannels_out);
408 if (delay_frames < 0)
409 delay_frames = 0;
410
411 delay_us = delay_frames * (1000000 / rate_out);
412
413 rdpsnd_queue_next(delay_us);
414 }
415}
416
417RD_BOOL
418alsa_open_in(void)
419{
420 int err;
421
422 if ((err =
423 snd_pcm_open(&in_handle, pcm_name, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0)
424 {
425 error("snd_pcm_open: %s\n", snd_strerror(err));
426 return False;
427 }
428
429 return True;
430}
431
432void
433alsa_close_in(void)
434{
435 if (in_handle)
436 {
437 snd_pcm_close(in_handle);
438 in_handle = NULL;
439 }
440}
441
442RD_BOOL
443alsa_set_format_in(RD_WAVEFORMATEX * pwfx)
444{
445 int err;
446
447 if (!alsa_set_format(in_handle, pwfx))
448 return False;
449
450 if ((err = snd_pcm_start(in_handle)) < 0)
451 {
452 error("snd_pcm_start: %s\n", snd_strerror(err));
453 return False;
454 }
455
456 samplewidth_in = pwfx->wBitsPerSample / 8;
457 audiochannels_in = pwfx->nChannels;
458 rate_in = pwfx->nSamplesPerSec;
459
460 return True;
461}
462
463void
464alsa_record(void)
465{
466 int len;
467 char buffer[32768];
468
469 len = snd_pcm_readi(in_handle, buffer,
470 sizeof(buffer) / (samplewidth_in * audiochannels_in));
471 if (len < 0)
472 {
473 snd_pcm_prepare(in_handle);
474 len = 0;
475 }
476
477 rdpsnd_record(buffer, len * samplewidth_in * audiochannels_in);
478}
479
480struct audio_driver *
481alsa_register(char *options)
482{
483 static struct audio_driver alsa_driver;
484
485 memset(&alsa_driver, 0, sizeof(alsa_driver));
486
487 alsa_driver.name = "alsa";
488 alsa_driver.description = "ALSA output driver, default device: " DEFAULTDEVICE;
489
490 alsa_driver.add_fds = alsa_add_fds;
491 alsa_driver.check_fds = alsa_check_fds;
492
493 alsa_driver.wave_out_open = alsa_open_out;
494 alsa_driver.wave_out_close = alsa_close_out;
495 alsa_driver.wave_out_format_supported = alsa_format_supported;
496 alsa_driver.wave_out_set_format = alsa_set_format_out;
497 alsa_driver.wave_out_volume = rdpsnd_dsp_softvol_set;
498
499 alsa_driver.wave_in_open = alsa_open_in;
500 alsa_driver.wave_in_close = alsa_close_in;
501 alsa_driver.wave_in_format_supported = alsa_format_supported;
502 alsa_driver.wave_in_set_format = alsa_set_format_in;
503 alsa_driver.wave_in_volume = NULL; /* FIXME */
504
505 alsa_driver.need_byteswap_on_be = 0;
506 alsa_driver.need_resampling = 0;
507
508 if (options)
509 {
510 pcm_name = xstrdup(options);
511 }
512 else
513 {
514 pcm_name = DEFAULTDEVICE;
515 }
516
517 return &alsa_driver;
518}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette