VirtualBox

source: vbox/trunk/src/VBox/RDP/client-1.8.3/rdpsnd_sun.c@ 70372

最後變更 在這個檔案從70372是 55123,由 vboxsync 提交於 10 年 前

rdesktop 1.8.3 modified for VBox

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 10.2 KB
 
1/* -*- c-basic-offset: 8 -*-
2 rdesktop: A Remote Desktop Protocol client.
3 Sound Channel Process Functions - Sun
4 Copyright (C) Matthew Chapman <matthewc.unsw.edu.au> 2003-2008
5 Copyright (C) GuoJunBo <[email protected]> 2003
6 Copyright (C) Michael Gernoth <[email protected]> 2003-2008
7 Copyright 2007-2008 Pierre Ossman <[email protected]> for Cendio AB
8 Copyright 2008-2011 Peter Astrand <[email protected]> for Cendio AB
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
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 <unistd.h>
36#include <fcntl.h>
37#include <errno.h>
38#include <sys/ioctl.h>
39#include <sys/audioio.h>
40#include <string.h>
41
42#if (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
43#include <stropts.h>
44#endif
45
46#define DEFAULTDEVICE "/dev/audio"
47#define MAX_LEN 512
48
49static int dsp_fd = -1;
50static int dsp_mode;
51static int dsp_refs;
52
53static RD_BOOL dsp_configured;
54static RD_BOOL dsp_broken;
55static RD_BOOL broken_2_channel_record = False;
56
57static RD_BOOL dsp_out;
58static RD_BOOL dsp_in;
59
60static int stereo;
61static int format;
62static uint32 snd_rate;
63static short samplewidth;
64static char *dsp_dev;
65
66static uint_t written_samples;
67
68void sun_play(void);
69void sun_record(void);
70
71static int
72sun_pause(void)
73{
74 audio_info_t info;
75
76 AUDIO_INITINFO(&info);
77
78 info.record.pause = 1;
79
80 if (ioctl(dsp_fd, AUDIO_SETINFO, &info) == -1)
81 return -1;
82
83#if defined I_FLUSH && defined FLUSHR
84 if (ioctl(dsp_fd, I_FLUSH, FLUSHR) == -1)
85 return -1;
86#endif
87
88 return 0;
89}
90
91static int
92sun_resume(void)
93{
94 audio_info_t info;
95
96 AUDIO_INITINFO(&info);
97
98 info.record.pause = 0;
99
100 if (ioctl(dsp_fd, AUDIO_SETINFO, &info) == -1)
101 return -1;
102
103 return 0;
104}
105
106void
107sun_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
108{
109 if (dsp_fd == -1)
110 return;
111
112 if (dsp_out && !rdpsnd_queue_empty())
113 FD_SET(dsp_fd, wfds);
114 if (dsp_in)
115 FD_SET(dsp_fd, rfds);
116 if (dsp_fd > *n)
117 *n = dsp_fd;
118}
119
120void
121sun_check_fds(fd_set * rfds, fd_set * wfds)
122{
123 if (FD_ISSET(dsp_fd, wfds))
124 sun_play();
125 if (FD_ISSET(dsp_fd, rfds))
126 sun_record();
127}
128
129RD_BOOL
130sun_open(int mode)
131{
132 audio_info_t info;
133
134 if (dsp_fd != -1)
135 {
136 dsp_refs++;
137
138 if (dsp_mode == O_RDWR)
139 return True;
140
141 if (dsp_mode == mode)
142 return True;
143
144 dsp_refs--;
145 return False;
146 }
147
148 dsp_configured = False;
149 dsp_broken = False;
150
151 written_samples = 0;
152
153 dsp_mode = O_RDWR;
154 dsp_fd = open(dsp_dev, O_RDWR | O_NONBLOCK);
155 if (dsp_fd != -1)
156 {
157 AUDIO_INITINFO(&info);
158
159 if ((ioctl(dsp_fd, AUDIO_GETINFO, &info) == -1)
160 || !(info.hw_features & AUDIO_HWFEATURE_DUPLEX))
161 {
162 close(dsp_fd);
163 dsp_fd = -1;
164 }
165 }
166
167 if (dsp_fd == -1)
168 {
169 dsp_mode = mode;
170
171 dsp_fd = open(dsp_dev, dsp_mode | O_NONBLOCK);
172 if (dsp_fd == -1)
173 {
174 perror(dsp_dev);
175 return False;
176 }
177 }
178
179 /*
180 * Pause recording until we actually start using it.
181 */
182 if (dsp_mode != O_WRONLY)
183 {
184 if (sun_pause() == -1)
185 {
186 close(dsp_fd);
187 dsp_fd = -1;
188 return False;
189 }
190 }
191
192 dsp_refs++;
193
194 return True;
195}
196
197void
198sun_close(void)
199{
200 dsp_refs--;
201
202 if (dsp_refs != 0)
203 return;
204
205 close(dsp_fd);
206 dsp_fd = -1;
207}
208
209RD_BOOL
210sun_open_out(void)
211{
212 if (!sun_open(O_WRONLY))
213 return False;
214
215 dsp_out = True;
216
217 return True;
218}
219
220void
221sun_close_out(void)
222{
223#if defined I_FLUSH && defined FLUSHW
224 /* Flush the audiobuffer */
225 ioctl(dsp_fd, I_FLUSH, FLUSHW);
226#endif
227#if defined AUDIO_FLUSH
228 ioctl(dsp_fd, AUDIO_FLUSH, NULL);
229#endif
230
231 sun_close();
232
233 /* Ack all remaining packets */
234 while (!rdpsnd_queue_empty())
235 rdpsnd_queue_next(0);
236
237 dsp_out = False;
238}
239
240RD_BOOL
241sun_open_in(void)
242{
243#if ! (defined I_FLUSH && defined FLUSHR)
244 /*
245 * It is not possible to reliably use the recording without
246 * flush operations.
247 */
248 return False;
249#endif
250
251 if (!sun_open(O_RDONLY))
252 return False;
253
254 /* 2 channel recording is known to be broken on Solaris x86
255 Sun Ray systems */
256#ifdef L_ENDIAN
257 if (strstr(dsp_dev, "/utaudio/"))
258 broken_2_channel_record = True;
259#endif
260
261 /*
262 * Unpause the stream now that we have someone using it.
263 */
264 if (sun_resume() == -1)
265 {
266 sun_close();
267 return False;
268 }
269
270 dsp_in = True;
271
272 return True;
273}
274
275void
276sun_close_in(void)
277{
278 /*
279 * Repause the stream when the user goes away.
280 */
281 sun_pause();
282
283 sun_close();
284
285 dsp_in = False;
286}
287
288RD_BOOL
289sun_format_supported(RD_WAVEFORMATEX * pwfx)
290{
291 if (pwfx->wFormatTag != WAVE_FORMAT_PCM)
292 return False;
293 if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2))
294 return False;
295 if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16))
296 return False;
297
298 return True;
299}
300
301RD_BOOL
302sun_set_format(RD_WAVEFORMATEX * pwfx)
303{
304 audio_info_t info;
305
306 ioctl(dsp_fd, AUDIO_DRAIN, 0);
307 AUDIO_INITINFO(&info);
308
309 if (dsp_configured)
310 {
311 if ((pwfx->wBitsPerSample == 8) && (format != AUDIO_ENCODING_LINEAR8))
312 return False;
313 if ((pwfx->wBitsPerSample == 16) && (format != AUDIO_ENCODING_LINEAR))
314 return False;
315
316 if ((pwfx->nChannels == 2) != ! !stereo)
317 return False;
318
319 if (pwfx->nSamplesPerSec != snd_rate)
320 return False;
321
322 return True;
323 }
324
325 sun_pause();
326
327 if (pwfx->wBitsPerSample == 8)
328 format = AUDIO_ENCODING_LINEAR8;
329 else if (pwfx->wBitsPerSample == 16)
330 format = AUDIO_ENCODING_LINEAR;
331
332 samplewidth = pwfx->wBitsPerSample / 8;
333
334 info.play.channels = pwfx->nChannels;
335 info.record.channels = info.play.channels;
336
337 if (pwfx->nChannels == 1)
338 {
339 stereo = 0;
340 }
341 else if (pwfx->nChannels == 2)
342 {
343 stereo = 1;
344 samplewidth *= 2;
345
346 if (broken_2_channel_record)
347 {
348 info.record.channels = 1;
349 }
350 }
351
352 snd_rate = pwfx->nSamplesPerSec;
353
354 info.play.sample_rate = pwfx->nSamplesPerSec;
355 info.play.precision = pwfx->wBitsPerSample;
356 info.play.encoding = format;
357 info.play.samples = 0;
358 info.play.eof = 0;
359 info.play.error = 0;
360
361 info.record.sample_rate = info.play.sample_rate;
362 info.record.precision = info.play.precision;
363 info.record.encoding = info.play.encoding;
364 info.record.samples = 0;
365 info.record.error = 0;
366
367 if (ioctl(dsp_fd, AUDIO_SETINFO, &info) == -1)
368 {
369 perror("AUDIO_SETINFO");
370 sun_close();
371 return False;
372 }
373
374 dsp_configured = True;
375
376 if (dsp_in)
377 sun_resume();
378
379 return True;
380}
381
382void
383sun_volume(uint16 left, uint16 right)
384{
385 audio_info_t info;
386 uint balance;
387 uint volume;
388
389 AUDIO_INITINFO(&info);
390
391 volume = (left > right) ? left : right;
392
393 if (volume / AUDIO_MID_BALANCE != 0)
394 {
395 balance =
396 AUDIO_MID_BALANCE - (left / (volume / AUDIO_MID_BALANCE)) +
397 (right / (volume / AUDIO_MID_BALANCE));
398 }
399 else
400 {
401 balance = AUDIO_MID_BALANCE;
402 }
403
404 info.play.gain = volume / (65536 / AUDIO_MAX_GAIN);
405 info.play.balance = balance;
406
407 if (ioctl(dsp_fd, AUDIO_SETINFO, &info) == -1)
408 {
409 perror("AUDIO_SETINFO");
410 return;
411 }
412}
413
414void
415sun_play(void)
416{
417 struct audio_packet *packet;
418 ssize_t len;
419 STREAM out;
420
421 /* We shouldn't be called if the queue is empty, but still */
422 if (rdpsnd_queue_empty())
423 return;
424
425 packet = rdpsnd_queue_current_packet();
426 out = &packet->s;
427
428 len = out->end - out->p;
429
430 len = write(dsp_fd, out->p, (len > MAX_LEN) ? MAX_LEN : len);
431 if (len == -1)
432 {
433 if (errno != EWOULDBLOCK)
434 {
435 if (!dsp_broken)
436 perror("RDPSND: write()");
437 dsp_broken = True;
438 rdpsnd_queue_next(0);
439 }
440 return;
441 }
442
443 written_samples += len / (samplewidth * (stereo ? 2 : 1));
444
445 dsp_broken = False;
446
447 out->p += len;
448
449 if (out->p == out->end)
450 {
451 audio_info_t info;
452 uint_t delay_samples;
453 unsigned long delay_us;
454
455 if (ioctl(dsp_fd, AUDIO_GETINFO, &info) != -1)
456 delay_samples = written_samples - info.play.samples;
457 else
458 delay_samples = out->size / (samplewidth * (stereo ? 2 : 1));
459
460 delay_us = delay_samples * (1000000 / snd_rate);
461 rdpsnd_queue_next(delay_us);
462 }
463}
464
465void
466sun_record(void)
467{
468 char buffer[32768];
469 int len;
470
471 len = read(dsp_fd, buffer, sizeof(buffer) / 2);
472 if (len == -1)
473 {
474 if (errno != EWOULDBLOCK)
475 perror("read audio");
476 return;
477 }
478
479 if (broken_2_channel_record)
480 {
481 unsigned int i;
482 int rec_samplewidth = samplewidth / 2;
483 /* Loop over each byte read backwards and put in place */
484 i = len - 1;
485 do
486 {
487 int samples_before = i / rec_samplewidth * 2;
488 int sample_byte = i % rec_samplewidth;
489 int ch1_offset = samples_before * rec_samplewidth + sample_byte;
490 // Channel 1
491 buffer[ch1_offset] = buffer[i];
492 // Channel 2
493 buffer[ch1_offset + rec_samplewidth] = buffer[i];
494
495 i--;
496 }
497 while (i);
498 len *= 2;
499 }
500
501 rdpsnd_record(buffer, len);
502}
503
504struct audio_driver *
505sun_register(char *options)
506{
507 static struct audio_driver sun_driver;
508
509 memset(&sun_driver, 0, sizeof(sun_driver));
510
511 sun_driver.name = "sun";
512 sun_driver.description =
513 "SUN/BSD output driver, default device: " DEFAULTDEVICE " or $AUDIODEV";
514
515 sun_driver.add_fds = sun_add_fds;
516 sun_driver.check_fds = sun_check_fds;
517
518 sun_driver.wave_out_open = sun_open_out;
519 sun_driver.wave_out_close = sun_close_out;
520 sun_driver.wave_out_format_supported = sun_format_supported;
521 sun_driver.wave_out_set_format = sun_set_format;
522 sun_driver.wave_out_volume = sun_volume;
523
524 sun_driver.wave_in_open = sun_open_in;
525 sun_driver.wave_in_close = sun_close_in;
526 sun_driver.wave_in_format_supported = sun_format_supported;
527 sun_driver.wave_in_set_format = sun_set_format;
528 sun_driver.wave_in_volume = NULL; /* FIXME */
529
530 sun_driver.need_byteswap_on_be = 1;
531 sun_driver.need_resampling = 0;
532
533 if (options)
534 {
535 dsp_dev = xstrdup(options);
536 }
537 else
538 {
539 dsp_dev = getenv("AUDIODEV");
540
541 if (dsp_dev == NULL)
542 {
543 dsp_dev = DEFAULTDEVICE;
544 }
545 }
546
547 return &sun_driver;
548}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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