VirtualBox

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

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

RDP/client: fix OSE

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