1 |
/* |
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 |
#include "rdesktop.h" |
23 |
|
24 |
#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 |
static VCHANNEL *rdpsnd_channel; |
35 |
|
36 |
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 |
|
82 |
in_uint8s(in, 14); /* flags, volume, pitch, UDP port */ |
83 |
in_uint16_le(in, in_format_count); |
84 |
in_uint8s(in, 4); /* pad, status, pad */ |
85 |
|
86 |
format_count = 0; |
87 |
if (s_check_rem(in, 18*in_format_count)) |
88 |
{ |
89 |
for (i = 0; i < in_format_count; i++) |
90 |
{ |
91 |
format = &formats[format_count]; |
92 |
in_uint16_le(in, format->wFormatTag); |
93 |
in_uint16_le(in, format->nChannels); |
94 |
in_uint32_le(in, format->nSamplesPerSec); |
95 |
in_uint32_le(in, format->nAvgBytesPerSec); |
96 |
in_uint16_le(in, format->nBlockAlign); |
97 |
in_uint16_le(in, format->wBitsPerSample); |
98 |
in_uint16_le(in, format->cbSize); |
99 |
|
100 |
if (wave_out_format_supported(format)) |
101 |
{ |
102 |
format_count++; |
103 |
if (format_count == MAX_FORMATS) |
104 |
break; |
105 |
} |
106 |
} |
107 |
} |
108 |
|
109 |
out = rdpsnd_init_packet(RDPSND_NEGOTIATE | 0x200, 20 + 18*format_count); |
110 |
out_uint32_le(out, 3); /* flags */ |
111 |
out_uint32(out, 0xffffffff); /* volume */ |
112 |
out_uint32(out, 0); /* pitch */ |
113 |
out_uint16(out, 0); /* UDP port */ |
114 |
|
115 |
out_uint16_le(out, format_count); |
116 |
out_uint8(out, 0x95); /* pad? */ |
117 |
out_uint16_le(out, 2); /* status */ |
118 |
out_uint8(out, 0x77); /* pad? */ |
119 |
|
120 |
for (i = 0; i < format_count; i++) |
121 |
{ |
122 |
format = &formats[i]; |
123 |
out_uint16_le(out, format->wFormatTag); |
124 |
out_uint16_le(out, format->nChannels); |
125 |
out_uint32_le(out, format->nSamplesPerSec); |
126 |
out_uint32_le(out, format->nAvgBytesPerSec); |
127 |
out_uint16_le(out, format->nBlockAlign); |
128 |
out_uint16_le(out, format->wBitsPerSample); |
129 |
out_uint16(out, 0); /* cbSize */ |
130 |
} |
131 |
|
132 |
s_mark_end(out); |
133 |
rdpsnd_send(out); |
134 |
} |
135 |
|
136 |
void |
137 |
rdpsnd_process_unknown6(STREAM in) |
138 |
{ |
139 |
uint16 unknown1, unknown2; |
140 |
STREAM out; |
141 |
|
142 |
/* in_uint8s(in, 4); unknown */ |
143 |
in_uint16_le(in, unknown1); |
144 |
in_uint16_le(in, unknown2); |
145 |
|
146 |
out = rdpsnd_init_packet(RDPSND_UNKNOWN6 | 0x2300, 4); |
147 |
out_uint16_le(out, unknown1); |
148 |
out_uint16_le(out, unknown2); |
149 |
s_mark_end(out); |
150 |
rdpsnd_send(out); |
151 |
} |
152 |
|
153 |
void |
154 |
rdpsnd_process(STREAM s) |
155 |
{ |
156 |
uint8 type; |
157 |
uint16 datalen; |
158 |
static uint16 tick, format; |
159 |
static uint8 packet_index; |
160 |
static BOOL awaiting_data_packet; |
161 |
|
162 |
#ifdef RDPSND_DEBUG |
163 |
printf("RDPSND recv:\n"); |
164 |
hexdump(s->p, s->end-s->p); |
165 |
#endif |
166 |
|
167 |
if (awaiting_data_packet) |
168 |
{ |
169 |
if (format >= MAX_FORMATS) |
170 |
{ |
171 |
error("RDPSND: Invalid format index\n"); |
172 |
return; |
173 |
} |
174 |
|
175 |
if (!device_open || (format != current_format)) |
176 |
{ |
177 |
if (!device_open && !wave_out_open()) |
178 |
{ |
179 |
rdpsnd_send_completion(tick, packet_index); |
180 |
return; |
181 |
} |
182 |
if (!wave_out_set_format(&formats[format])) |
183 |
{ |
184 |
rdpsnd_send_completion(tick, packet_index); |
185 |
wave_out_close(); |
186 |
device_open = False; |
187 |
return; |
188 |
} |
189 |
device_open = True; |
190 |
current_format = format; |
191 |
} |
192 |
|
193 |
wave_out_write(s, tick, packet_index); |
194 |
awaiting_data_packet = False; |
195 |
return; |
196 |
} |
197 |
|
198 |
in_uint8(s, type); |
199 |
in_uint8s(s, 1); /* unknown? */ |
200 |
in_uint16_le(s, datalen); |
201 |
|
202 |
switch (type) |
203 |
{ |
204 |
case RDPSND_WRITE: |
205 |
in_uint16_le(s, tick); |
206 |
in_uint16_le(s, format); |
207 |
in_uint8(s, packet_index); |
208 |
awaiting_data_packet = True; |
209 |
break; |
210 |
case RDPSND_CLOSE: |
211 |
wave_out_close(); |
212 |
device_open = False; |
213 |
break; |
214 |
case RDPSND_NEGOTIATE: |
215 |
rdpsnd_process_negotiate(s); |
216 |
break; |
217 |
case RDPSND_UNKNOWN6: |
218 |
rdpsnd_process_unknown6(s); |
219 |
break; |
220 |
case RDPSND_SET_VOLUME: |
221 |
/* uint32 volume */ |
222 |
break; |
223 |
default: |
224 |
unimpl("RDPSND packet type %d\n", type); |
225 |
break; |
226 |
} |
227 |
} |
228 |
|
229 |
BOOL |
230 |
rdpsnd_init(void) |
231 |
{ |
232 |
rdpsnd_channel = |
233 |
channel_register("rdpsnd", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP, |
234 |
rdpsnd_process); |
235 |
return (rdpsnd_channel != NULL); |
236 |
} |