/[rdesktop]/sourceforge.net/trunk/rdesktop/rdpsnd_sun.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_sun.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1254 - (hide annotations)
Sun Sep 17 10:32:18 2006 UTC (17 years, 9 months ago) by stargo
File MIME type: text/plain
File size: 5710 byte(s)
unify queue-handling in rdpsnd.c (remove private copies from all
drivers)

1 astrand 963 /* -*- c-basic-offset: 8 -*-
2 matthewc 476 rdesktop: A Remote Desktop Protocol client.
3     Sound Channel Process Functions - Sun
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 2003-2006
7 matthewc 476
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 matthewc 476 #include <unistd.h>
26     #include <fcntl.h>
27     #include <errno.h>
28     #include <sys/ioctl.h>
29 matthewc 477 #include <sys/audioio.h>
30 matthewc 476
31 stargo 560 #if (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
32     #include <stropts.h>
33     #endif
34    
35 stargo 1254 #define DEFAULTDEVICE "/dev/audio"
36 matthewc 476
37 stargo 510 static BOOL g_reopened;
38     static BOOL g_swapaudio;
39     static short g_samplewidth;
40 matthewc 476
41     BOOL
42     wave_out_open(void)
43     {
44 matthewc 477 char *dsp_dev = getenv("AUDIODEV");
45 matthewc 476
46 astrand 499 if (dsp_dev == NULL)
47 matthewc 477 {
48 stargo 1254 dsp_dev = xstrdup(DEFAULTDEVICE);
49 matthewc 477 }
50    
51 astrand 499 if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1)
52 matthewc 476 {
53     perror(dsp_dev);
54     return False;
55     }
56    
57     /* Non-blocking so that user interface is responsive */
58 astrand 499 fcntl(g_dsp_fd, F_SETFL, fcntl(g_dsp_fd, F_GETFL) | O_NONBLOCK);
59    
60 stargo 1254 rdpsnd_queue_init();
61 stargo 510 g_reopened = True;
62 astrand 499
63 matthewc 476 return True;
64     }
65    
66     void
67     wave_out_close(void)
68     {
69 matthewc 477 /* Ack all remaining packets */
70 stargo 1254 while (!rdpsnd_queue_empty())
71 matthewc 477 {
72 stargo 1254 rdpsnd_send_completion(rdpsnd_queue_current_packet()->tick,
73     rdpsnd_queue_current_packet()->index);
74     rdpsnd_queue_next();
75 matthewc 477 }
76    
77 stargo 538 #if defined I_FLUSH && defined FLUSHW
78 matthewc 477 /* Flush the audiobuffer */
79 astrand 499 ioctl(g_dsp_fd, I_FLUSH, FLUSHW);
80 stargo 538 #endif
81 stargo 561 #if defined AUDIO_FLUSH
82     ioctl(g_dsp_fd, AUDIO_FLUSH, NULL);
83     #endif
84 matthewc 476 close(g_dsp_fd);
85     }
86    
87     BOOL
88 astrand 499 wave_out_format_supported(WAVEFORMATEX * pwfx)
89 matthewc 476 {
90     if (pwfx->wFormatTag != WAVE_FORMAT_PCM)
91     return False;
92     if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2))
93     return False;
94     if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16))
95     return False;
96    
97     return True;
98     }
99    
100     BOOL
101 astrand 499 wave_out_set_format(WAVEFORMATEX * pwfx)
102 matthewc 476 {
103     audio_info_t info;
104    
105     ioctl(g_dsp_fd, AUDIO_DRAIN, 0);
106 stargo 510 g_swapaudio = False;
107 matthewc 476 AUDIO_INITINFO(&info);
108    
109    
110     if (pwfx->wBitsPerSample == 8)
111     {
112     info.play.encoding = AUDIO_ENCODING_LINEAR8;
113     }
114     else if (pwfx->wBitsPerSample == 16)
115     {
116     info.play.encoding = AUDIO_ENCODING_LINEAR;
117 matthewc 477 /* Do we need to swap the 16bit values? (Are we BigEndian) */
118 stargo 694 #ifdef B_ENDIAN
119     g_swapaudio = 1;
120     #else
121     g_swapaudio = 0;
122     #endif
123 matthewc 476 }
124    
125 stargo 510 g_samplewidth = pwfx->wBitsPerSample / 8;
126 stargo 491
127 astrand 499 if (pwfx->nChannels == 1)
128     {
129 matthewc 514 info.play.channels = 1;
130 matthewc 476 }
131 astrand 499 else if (pwfx->nChannels == 2)
132 matthewc 476 {
133 matthewc 514 info.play.channels = 2;
134 stargo 510 g_samplewidth *= 2;
135 matthewc 476 }
136    
137     info.play.sample_rate = pwfx->nSamplesPerSec;
138     info.play.precision = pwfx->wBitsPerSample;
139     info.play.samples = 0;
140     info.play.eof = 0;
141     info.play.error = 0;
142 stargo 510 g_reopened = True;
143 matthewc 476
144     if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1)
145     {
146     perror("AUDIO_SETINFO");
147     close(g_dsp_fd);
148     return False;
149     }
150    
151     return True;
152     }
153    
154     void
155 stargo 491 wave_out_volume(uint16 left, uint16 right)
156     {
157     audio_info_t info;
158     uint balance;
159     uint volume;
160    
161 stargo 772 AUDIO_INITINFO(&info);
162 stargo 491
163     volume = (left > right) ? left : right;
164    
165 astrand 499 if (volume / AUDIO_MID_BALANCE != 0)
166 stargo 491 {
167 astrand 499 balance =
168     AUDIO_MID_BALANCE - (left / (volume / AUDIO_MID_BALANCE)) +
169     (right / (volume / AUDIO_MID_BALANCE));
170 stargo 491 }
171     else
172     {
173     balance = AUDIO_MID_BALANCE;
174     }
175    
176 astrand 499 info.play.gain = volume / (65536 / AUDIO_MAX_GAIN);
177 stargo 491 info.play.balance = balance;
178    
179     if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1)
180     {
181     perror("AUDIO_SETINFO");
182     return;
183     }
184     }
185    
186     void
187 matthewc 476 wave_out_play(void)
188     {
189     struct audio_packet *packet;
190     audio_info_t info;
191     ssize_t len;
192     unsigned int i;
193     uint8 swap;
194     STREAM out;
195     static BOOL swapped = False;
196     static BOOL sentcompletion = True;
197 matthewc 477 static uint32 samplecnt = 0;
198     static uint32 numsamples;
199 matthewc 476
200     while (1)
201     {
202 stargo 510 if (g_reopened)
203 matthewc 477 {
204     /* Device was just (re)openend */
205     samplecnt = 0;
206     swapped = False;
207     sentcompletion = True;
208 stargo 510 g_reopened = False;
209 matthewc 477 }
210    
211 stargo 1254 if (rdpsnd_queue_empty())
212 matthewc 476 {
213     g_dsp_busy = 0;
214     return;
215     }
216    
217 stargo 1254 packet = rdpsnd_queue_current_packet();
218 matthewc 476 out = &packet->s;
219    
220 matthewc 477 /* Swap the current packet, but only once */
221 stargo 510 if (g_swapaudio && !swapped)
222 matthewc 476 {
223 astrand 499 for (i = 0; i < out->end - out->p; i += 2)
224 matthewc 476 {
225     swap = *(out->p + i);
226 stargo 491 *(out->p + i) = *(out->p + i + 1);
227 matthewc 476 *(out->p + i + 1) = swap;
228     }
229 stargo 491 swapped = True;
230 matthewc 476 }
231    
232 astrand 499 if (sentcompletion)
233 matthewc 476 {
234     sentcompletion = False;
235 stargo 510 numsamples = (out->end - out->p) / g_samplewidth;
236 matthewc 476 }
237    
238 astrand 499 len = 0;
239 matthewc 476
240 astrand 499 if (out->end != out->p)
241 matthewc 476 {
242     len = write(g_dsp_fd, out->p, out->end - out->p);
243     if (len == -1)
244     {
245     if (errno != EWOULDBLOCK)
246     perror("write audio");
247     g_dsp_busy = 1;
248     return;
249     }
250     }
251    
252     out->p += len;
253     if (out->p == out->end)
254     {
255     if (ioctl(g_dsp_fd, AUDIO_GETINFO, &info) == -1)
256     {
257     perror("AUDIO_GETINFO");
258     return;
259     }
260 matthewc 477
261     /* Ack the packet, if we have played at least 70% */
262 astrand 499 if (info.play.samples >= samplecnt + ((numsamples * 7) / 10))
263 matthewc 476 {
264 matthewc 477 samplecnt += numsamples;
265 stargo 1254 /* We need to add 50 to tell windows that time has passed while
266     * playing this packet */
267     rdpsnd_send_completion(packet->tick + 50, packet->index);
268     rdpsnd_queue_next();
269 matthewc 476 swapped = False;
270     sentcompletion = True;
271     }
272     else
273     {
274     g_dsp_busy = 1;
275     return;
276     }
277     }
278     }
279     }

  ViewVC Help
Powered by ViewVC 1.1.26