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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 492 - (hide annotations)
Tue Oct 14 07:46:49 2003 UTC (20 years, 7 months ago) by stargo
File MIME type: text/plain
File size: 5977 byte(s)
Fix format-detection by Robert Sanders

1 matthewc 474 /*
2     rdesktop: A Remote Desktop Protocol client.
3     Sound Channel Process Functions
4     Copyright (C) Matthew Chapman 2003
5     Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003
6    
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11    
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16    
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20     */
21    
22 matthewc 432 #include "rdesktop.h"
23    
24 matthewc 474 #define RDPSND_CLOSE 1
25     #define RDPSND_WRITE 2
26     #define RDPSND_SET_VOLUME 3
27     #define RDPSND_UNKNOWN4 4
28     #define RDPSND_COMPLETION 5
29     #define RDPSND_UNKNOWN6 6
30     #define RDPSND_NEGOTIATE 7
31    
32     #define MAX_FORMATS 10
33    
34 matthewc 432 static VCHANNEL *rdpsnd_channel;
35    
36 matthewc 474 static BOOL device_open;
37     static WAVEFORMATEX formats[MAX_FORMATS];
38     static unsigned int format_count;
39     static unsigned int current_format;
40    
41     STREAM
42     rdpsnd_init_packet(uint16 type, uint16 size)
43     {
44     STREAM s;
45    
46     s = channel_init(rdpsnd_channel, size+4);
47     out_uint16_le(s, type);
48     out_uint16_le(s, size);
49     return s;
50     }
51    
52     void
53     rdpsnd_send(STREAM s)
54     {
55     #ifdef RDPSND_DEBUG
56     printf("RDPSND send:\n");
57     hexdump(s->channel_hdr+8, s->end-s->channel_hdr-8);
58     #endif
59    
60     channel_send(s, rdpsnd_channel);
61     }
62    
63     void rdpsnd_send_completion(uint16 tick, uint8 packet_index)
64     {
65     STREAM s;
66    
67     s = rdpsnd_init_packet(RDPSND_COMPLETION, 4);
68     out_uint16_le(s, tick+50);
69     out_uint8(s, packet_index);
70     out_uint8(s, 0);
71     s_mark_end(s);
72     rdpsnd_send(s);
73     }
74    
75     void
76     rdpsnd_process_negotiate(STREAM in)
77     {
78     unsigned int in_format_count, i;
79     WAVEFORMATEX *format;
80     STREAM out;
81 stargo 491 BOOL device_available = False;
82 matthewc 474
83     in_uint8s(in, 14); /* flags, volume, pitch, UDP port */
84     in_uint16_le(in, in_format_count);
85     in_uint8s(in, 4); /* pad, status, pad */
86    
87 stargo 491 if (wave_out_open())
88     {
89     wave_out_close();
90     device_available = True;
91     }
92    
93 matthewc 474 format_count = 0;
94     if (s_check_rem(in, 18*in_format_count))
95     {
96     for (i = 0; i < in_format_count; i++)
97     {
98     format = &formats[format_count];
99     in_uint16_le(in, format->wFormatTag);
100     in_uint16_le(in, format->nChannels);
101     in_uint32_le(in, format->nSamplesPerSec);
102     in_uint32_le(in, format->nAvgBytesPerSec);
103     in_uint16_le(in, format->nBlockAlign);
104     in_uint16_le(in, format->wBitsPerSample);
105     in_uint16_le(in, format->cbSize);
106    
107 stargo 492 /* read in the buffer of unknown use */
108     int readcnt = format->cbSize;
109     int discardcnt = 0;
110     if (format->cbSize > MAX_CBSIZE)
111     {
112     fprintf(stderr, "cbSize too large for buffer: %d\n", format->cbSize);
113     readcnt = MAX_CBSIZE;
114     discardcnt = format->cbSize - MAX_CBSIZE;
115     }
116     in_uint8a(in, format->cb, readcnt);
117     in_uint8s(in, discardcnt);
118    
119 stargo 491 if (device_available && wave_out_format_supported(format))
120 matthewc 474 {
121     format_count++;
122     if (format_count == MAX_FORMATS)
123     break;
124     }
125     }
126     }
127    
128     out = rdpsnd_init_packet(RDPSND_NEGOTIATE | 0x200, 20 + 18*format_count);
129     out_uint32_le(out, 3); /* flags */
130     out_uint32(out, 0xffffffff); /* volume */
131     out_uint32(out, 0); /* pitch */
132     out_uint16(out, 0); /* UDP port */
133    
134     out_uint16_le(out, format_count);
135     out_uint8(out, 0x95); /* pad? */
136     out_uint16_le(out, 2); /* status */
137     out_uint8(out, 0x77); /* pad? */
138    
139     for (i = 0; i < format_count; i++)
140     {
141     format = &formats[i];
142     out_uint16_le(out, format->wFormatTag);
143     out_uint16_le(out, format->nChannels);
144     out_uint32_le(out, format->nSamplesPerSec);
145     out_uint32_le(out, format->nAvgBytesPerSec);
146     out_uint16_le(out, format->nBlockAlign);
147     out_uint16_le(out, format->wBitsPerSample);
148     out_uint16(out, 0); /* cbSize */
149     }
150    
151     s_mark_end(out);
152     rdpsnd_send(out);
153     }
154    
155     void
156     rdpsnd_process_unknown6(STREAM in)
157     {
158     uint16 unknown1, unknown2;
159     STREAM out;
160    
161     /* in_uint8s(in, 4); unknown */
162     in_uint16_le(in, unknown1);
163     in_uint16_le(in, unknown2);
164    
165     out = rdpsnd_init_packet(RDPSND_UNKNOWN6 | 0x2300, 4);
166     out_uint16_le(out, unknown1);
167     out_uint16_le(out, unknown2);
168     s_mark_end(out);
169     rdpsnd_send(out);
170     }
171    
172     void
173 matthewc 432 rdpsnd_process(STREAM s)
174     {
175 matthewc 474 uint8 type;
176     uint16 datalen;
177 stargo 491 uint32 volume;
178 matthewc 474 static uint16 tick, format;
179     static uint8 packet_index;
180     static BOOL awaiting_data_packet;
181    
182     #ifdef RDPSND_DEBUG
183     printf("RDPSND recv:\n");
184     hexdump(s->p, s->end-s->p);
185     #endif
186    
187     if (awaiting_data_packet)
188     {
189     if (format >= MAX_FORMATS)
190     {
191     error("RDPSND: Invalid format index\n");
192     return;
193     }
194    
195     if (!device_open || (format != current_format))
196     {
197     if (!device_open && !wave_out_open())
198     {
199     rdpsnd_send_completion(tick, packet_index);
200     return;
201     }
202     if (!wave_out_set_format(&formats[format]))
203     {
204     rdpsnd_send_completion(tick, packet_index);
205     wave_out_close();
206     device_open = False;
207     return;
208     }
209     device_open = True;
210     current_format = format;
211     }
212    
213     wave_out_write(s, tick, packet_index);
214     awaiting_data_packet = False;
215     return;
216     }
217    
218     in_uint8(s, type);
219     in_uint8s(s, 1); /* unknown? */
220     in_uint16_le(s, datalen);
221    
222     switch (type)
223     {
224     case RDPSND_WRITE:
225     in_uint16_le(s, tick);
226     in_uint16_le(s, format);
227     in_uint8(s, packet_index);
228     awaiting_data_packet = True;
229     break;
230     case RDPSND_CLOSE:
231     wave_out_close();
232     device_open = False;
233     break;
234     case RDPSND_NEGOTIATE:
235     rdpsnd_process_negotiate(s);
236     break;
237     case RDPSND_UNKNOWN6:
238     rdpsnd_process_unknown6(s);
239     break;
240     case RDPSND_SET_VOLUME:
241 stargo 491 in_uint32(s, volume);
242     if ( device_open )
243     {
244     wave_out_volume((volume & 0xffff), (volume & 0xffff0000) >> 16);
245     }
246 matthewc 474 break;
247     default:
248     unimpl("RDPSND packet type %d\n", type);
249     break;
250     }
251 matthewc 432 }
252    
253     BOOL
254     rdpsnd_init(void)
255     {
256 astrand 435 rdpsnd_channel =
257     channel_register("rdpsnd", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP,
258     rdpsnd_process);
259 matthewc 432 return (rdpsnd_channel != NULL);
260     }

  ViewVC Help
Powered by ViewVC 1.1.26