/[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 538 - (hide annotations)
Fri Oct 31 09:38:25 2003 UTC (20 years, 7 months ago) by stargo
File MIME type: text/plain
File size: 6187 byte(s)
Be more portable to platforms where I_FLUSH && FLUSHW are not defined

1 matthewc 476 /*
2     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     Copyright (C) Michael Gernoth mike@zerfleddert.de 2003
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     #include <unistd.h>
25     #include <fcntl.h>
26     #include <errno.h>
27     #include <sys/ioctl.h>
28 matthewc 477 #include <sys/audioio.h>
29 matthewc 476 #include <stropts.h>
30    
31     #define MAX_QUEUE 10
32    
33     int g_dsp_fd;
34 stargo 504 BOOL g_dsp_busy = False;
35 stargo 510 static BOOL g_reopened;
36     static BOOL g_swapaudio;
37     static short g_samplewidth;
38 matthewc 476
39 astrand 499 static struct audio_packet
40     {
41 matthewc 476 struct stream s;
42     uint16 tick;
43     uint8 index;
44     } packet_queue[MAX_QUEUE];
45     static unsigned int queue_hi, queue_lo;
46    
47     BOOL
48     wave_out_open(void)
49     {
50 matthewc 477 char *dsp_dev = getenv("AUDIODEV");
51 matthewc 476
52 astrand 499 if (dsp_dev == NULL)
53 matthewc 477 {
54 astrand 499 dsp_dev = "/dev/audio";
55 matthewc 477 }
56    
57 astrand 499 if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1)
58 matthewc 476 {
59     perror(dsp_dev);
60     return False;
61     }
62    
63     /* Non-blocking so that user interface is responsive */
64 astrand 499 fcntl(g_dsp_fd, F_SETFL, fcntl(g_dsp_fd, F_GETFL) | O_NONBLOCK);
65    
66 matthewc 476 queue_lo = queue_hi = 0;
67 stargo 510 g_reopened = True;
68 astrand 499
69 matthewc 476 return True;
70     }
71    
72     void
73     wave_out_close(void)
74     {
75 matthewc 477 /* Ack all remaining packets */
76 astrand 499 while (queue_lo != queue_hi)
77 matthewc 477 {
78     rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index);
79     free(packet_queue[queue_lo].s.data);
80     queue_lo = (queue_lo + 1) % MAX_QUEUE;
81     }
82    
83 stargo 538 #if defined I_FLUSH && defined FLUSHW
84 matthewc 477 /* Flush the audiobuffer */
85 astrand 499 ioctl(g_dsp_fd, I_FLUSH, FLUSHW);
86 stargo 538 #endif
87 matthewc 476 close(g_dsp_fd);
88     }
89    
90     BOOL
91 astrand 499 wave_out_format_supported(WAVEFORMATEX * pwfx)
92 matthewc 476 {
93     if (pwfx->wFormatTag != WAVE_FORMAT_PCM)
94     return False;
95     if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2))
96     return False;
97     if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16))
98     return False;
99    
100     return True;
101     }
102    
103     BOOL
104 astrand 499 wave_out_set_format(WAVEFORMATEX * pwfx)
105 matthewc 476 {
106     audio_info_t info;
107     int test = 1;
108    
109     ioctl(g_dsp_fd, AUDIO_DRAIN, 0);
110 stargo 510 g_swapaudio = False;
111 matthewc 476 AUDIO_INITINFO(&info);
112    
113    
114     if (pwfx->wBitsPerSample == 8)
115     {
116     info.play.encoding = AUDIO_ENCODING_LINEAR8;
117     }
118     else if (pwfx->wBitsPerSample == 16)
119     {
120     info.play.encoding = AUDIO_ENCODING_LINEAR;
121 matthewc 477 /* Do we need to swap the 16bit values? (Are we BigEndian) */
122 stargo 510 g_swapaudio = !(*(uint8 *) (&test));
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     if (ioctl(g_dsp_fd, AUDIO_GETINFO, &info) == -1)
162     {
163     perror("AUDIO_GETINFO");
164     return;
165     }
166    
167     volume = (left > right) ? left : right;
168    
169 astrand 499 if (volume / AUDIO_MID_BALANCE != 0)
170 stargo 491 {
171 astrand 499 balance =
172     AUDIO_MID_BALANCE - (left / (volume / AUDIO_MID_BALANCE)) +
173     (right / (volume / AUDIO_MID_BALANCE));
174 stargo 491 }
175     else
176     {
177     balance = AUDIO_MID_BALANCE;
178     }
179    
180 astrand 499 info.play.gain = volume / (65536 / AUDIO_MAX_GAIN);
181 stargo 491 info.play.balance = balance;
182    
183     if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1)
184     {
185     perror("AUDIO_SETINFO");
186     return;
187     }
188     }
189    
190     void
191 matthewc 476 wave_out_write(STREAM s, uint16 tick, uint8 index)
192     {
193     struct audio_packet *packet = &packet_queue[queue_hi];
194     unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE;
195    
196     if (next_hi == queue_lo)
197     {
198     error("No space to queue audio packet\n");
199     return;
200     }
201 astrand 499
202 matthewc 476 queue_hi = next_hi;
203    
204     packet->s = *s;
205     packet->tick = tick;
206     packet->index = index;
207 stargo 491 packet->s.p += 4;
208 matthewc 476
209     /* we steal the data buffer from s, give it a new one */
210     s->data = malloc(s->size);
211    
212     if (!g_dsp_busy)
213     wave_out_play();
214     }
215    
216     void
217     wave_out_play(void)
218     {
219     struct audio_packet *packet;
220     audio_info_t info;
221     ssize_t len;
222     unsigned int i;
223     uint8 swap;
224     STREAM out;
225     static BOOL swapped = False;
226     static BOOL sentcompletion = True;
227 matthewc 477 static uint32 samplecnt = 0;
228     static uint32 numsamples;
229 matthewc 476
230     while (1)
231     {
232 stargo 510 if (g_reopened)
233 matthewc 477 {
234     /* Device was just (re)openend */
235     samplecnt = 0;
236     swapped = False;
237     sentcompletion = True;
238 stargo 510 g_reopened = False;
239 matthewc 477 }
240    
241 matthewc 476 if (queue_lo == queue_hi)
242     {
243     g_dsp_busy = 0;
244     return;
245     }
246    
247     packet = &packet_queue[queue_lo];
248     out = &packet->s;
249    
250 matthewc 477 /* Swap the current packet, but only once */
251 stargo 510 if (g_swapaudio && !swapped)
252 matthewc 476 {
253 astrand 499 for (i = 0; i < out->end - out->p; i += 2)
254 matthewc 476 {
255     swap = *(out->p + i);
256 stargo 491 *(out->p + i) = *(out->p + i + 1);
257 matthewc 476 *(out->p + i + 1) = swap;
258     }
259 stargo 491 swapped = True;
260 matthewc 476 }
261    
262 astrand 499 if (sentcompletion)
263 matthewc 476 {
264     sentcompletion = False;
265 stargo 510 numsamples = (out->end - out->p) / g_samplewidth;
266 matthewc 476 }
267    
268 astrand 499 len = 0;
269 matthewc 476
270 astrand 499 if (out->end != out->p)
271 matthewc 476 {
272     len = write(g_dsp_fd, out->p, out->end - out->p);
273     if (len == -1)
274     {
275     if (errno != EWOULDBLOCK)
276     perror("write audio");
277     g_dsp_busy = 1;
278     return;
279     }
280     }
281    
282     out->p += len;
283     if (out->p == out->end)
284     {
285     if (ioctl(g_dsp_fd, AUDIO_GETINFO, &info) == -1)
286     {
287     perror("AUDIO_GETINFO");
288     return;
289     }
290 matthewc 477
291     /* Ack the packet, if we have played at least 70% */
292 astrand 499 if (info.play.samples >= samplecnt + ((numsamples * 7) / 10))
293 matthewc 476 {
294 matthewc 477 samplecnt += numsamples;
295 matthewc 476 rdpsnd_send_completion(packet->tick, packet->index);
296     free(out->data);
297     queue_lo = (queue_lo + 1) % MAX_QUEUE;
298     swapped = False;
299     sentcompletion = True;
300     }
301     else
302     {
303     g_dsp_busy = 1;
304     return;
305     }
306     }
307     }
308     }

  ViewVC Help
Powered by ViewVC 1.1.26