1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
/**
* xrdp: A Remote Desktop Protocol server.
*
* Copyright (C) Jay Sorg 2009-2012
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "sound.h"
#include "thread_calls.h"
extern int g_rdpsnd_chan_id; /* in chansrv.c */
extern int g_display_num; /* in chansrv.c */
static struct trans *g_audio_l_trans = 0; // listener
static struct trans *g_audio_c_trans = 0; // connection
static int g_training_sent_time = 0;
static int g_cBlockNo = 0;
#if defined(XRDP_SIMPLESOUND)
static void* DEFAULT_CC
read_raw_audio_data(void* arg);
#endif
/*****************************************************************************/
static int APP_CC
sound_send_server_formats(void)
{
struct stream* s;
int bytes;
char* size_ptr;
print_got_here();
make_stream(s);
init_stream(s, 8182);
out_uint16_le(s, SNDC_FORMATS);
size_ptr = s->p;
out_uint16_le(s, 0); /* size, set later */
out_uint32_le(s, 0); /* dwFlags */
out_uint32_le(s, 0); /* dwVolume */
out_uint32_le(s, 0); /* dwPitch */
out_uint16_le(s, 0); /* wDGramPort */
out_uint16_le(s, 1); /* wNumberOfFormats */
out_uint8(s, g_cBlockNo); /* cLastBlockConfirmed */
out_uint16_le(s, 2); /* wVersion */
out_uint8(s, 0); /* bPad */
/* sndFormats */
/*
wFormatTag 2 byte offset 0
nChannels 2 byte offset 2
nSamplesPerSec 4 byte offset 4
nAvgBytesPerSec 4 byte offset 8
nBlockAlign 2 byte offset 12
wBitsPerSample 2 byte offset 14
cbSize 2 byte offset 16
data variable offset 18
*/
/* examples
01 00 02 00 44 ac 00 00 10 b1 02 00 04 00 10 00 ....D...........
00 00
01 00 02 00 22 56 00 00 88 58 01 00 04 00 10 00 ...."V...X......
00 00
*/
out_uint16_le(s, 1); // wFormatTag - WAVE_FORMAT_PCM
out_uint16_le(s, 2); // num of channels
out_uint32_le(s, 44100); // samples per sec
out_uint32_le(s, 176400); // avg bytes per sec
out_uint16_le(s, 4); // block align
out_uint16_le(s, 16); // bits per sample
out_uint16_le(s, 0); // size
s_mark_end(s);
bytes = (int)((s->end - s->data) - 4);
size_ptr[0] = bytes;
size_ptr[1] = bytes >> 8;
bytes = (int)(s->end - s->data);
send_channel_data(g_rdpsnd_chan_id, s->data, bytes);
free_stream(s);
return 0;
}
/*****************************************************************************/
static int
sound_send_training(void)
{
struct stream* s;
int bytes;
int time;
char* size_ptr;
print_got_here();
make_stream(s);
init_stream(s, 8182);
out_uint16_le(s, SNDC_TRAINING);
size_ptr = s->p;
out_uint16_le(s, 0); /* size, set later */
time = g_time2();
g_training_sent_time = time;
out_uint16_le(s, time);
out_uint16_le(s, 1024);
out_uint8s(s, (1024 - 4));
s_mark_end(s);
bytes = (int)((s->end - s->data) - 4);
size_ptr[0] = bytes;
size_ptr[1] = bytes >> 8;
bytes = (int)(s->end - s->data);
send_channel_data(g_rdpsnd_chan_id, s->data, bytes);
free_stream(s);
return 0;
}
/*****************************************************************************/
/*
0000 07 02 26 00 03 00 80 00 ff ff ff ff 00 00 00 00 ..&.............
0010 00 00 01 00 00 02 00 00 01 00 02 00 44 ac 00 00 ............D...
0020 10 b1 02 00 04 00 10 00 00 00
*/
static int APP_CC
sound_process_formats(struct stream* s, int size)
{
int num_formats;
print_got_here();
LOG(0, ("sound_process_formats:"));
if (size < 16)
{
return 1;
}
in_uint8s(s, 14);
in_uint16_le(s, num_formats);
if (num_formats > 0)
{
sound_send_training();
}
return 0;
}
/*****************************************************************************/
static int
sound_send_wave_data(char* data, int data_bytes)
{
struct stream* s;
int bytes;
int time;
char* size_ptr;
print_got_here();
if ((data_bytes < 4) || (data_bytes > 32 * 1024))
{
LOG(0, ("sound_send_wave_data: bad data_bytes %d", data_bytes));
}
/* part one of 2 PDU wave info */
LOG(10, ("sound_send_wave_data: sending %d bytes", data_bytes));
make_stream(s);
init_stream(s, data_bytes);
out_uint16_le(s, SNDC_WAVE);
size_ptr = s->p;
out_uint16_le(s, 0); /* size, set later */
time = g_time2();
out_uint16_le(s, time);
out_uint16_le(s, 0); /* wFormatNo */
g_cBlockNo++;
out_uint8(s, g_cBlockNo);
LOG(10, ("sound_send_wave_data: sending time %d, g_cBlockNo %d",
time & 0xffff, g_cBlockNo & 0xff));
out_uint8s(s, 3);
out_uint8a(s, data, 4);
s_mark_end(s);
bytes = (int)((s->end - s->data) - 4);
bytes += data_bytes;
bytes -= 4;
size_ptr[0] = bytes;
size_ptr[1] = bytes >> 8;
bytes = (int)(s->end - s->data);
send_channel_data(g_rdpsnd_chan_id, s->data, bytes);
/* part two of 2 PDU wave info */
init_stream(s, data_bytes);
out_uint32_le(s, 0);
out_uint8a(s, data + 4, data_bytes - 4);
s_mark_end(s);
bytes = (int)(s->end - s->data);
send_channel_data(g_rdpsnd_chan_id, s->data, bytes);
free_stream(s);
return 0;
}
/*****************************************************************************/
static int APP_CC
sound_process_training(struct stream* s, int size)
{
int time_diff;
print_got_here();
time_diff = g_time2() - g_training_sent_time;
LOG(0, ("sound_process_training: round trip time %u", time_diff));
return 0;
}
/*****************************************************************************/
static int APP_CC
sound_process_wave_confirm(struct stream* s, int size)
{
int wTimeStamp;
int cConfirmedBlockNo;
print_got_here();
in_uint16_le(s, wTimeStamp);
in_uint8(s, cConfirmedBlockNo);
LOG(10, ("sound_process_wave_confirm: wTimeStamp %d, cConfirmedBlockNo %d",
wTimeStamp, cConfirmedBlockNo));
return 0;
}
/*****************************************************************************/
static int APP_CC
process_pcm_message(int id, int size, struct stream* s)
{
print_got_here();
sound_send_wave_data(s->p, size);
return 0;
}
/*****************************************************************************/
/* data coming in from audio source, eg pulse, alsa */
static int DEFAULT_CC
sound_trans_audio_data_in(struct trans* trans)
{
struct stream *s;
int id;
int size;
int error;
if (trans == 0)
{
return 0;
}
if (trans != g_audio_c_trans)
{
return 1;
}
s = trans_get_in_s(trans);
in_uint32_le(s, id);
in_uint32_le(s, size);
if ((id != 0) || (size > 32 * 1024 + 8) || (size < 1))
{
LOG(0, ("sound_trans_audio_data_in: bad message id %d size %d", id, size));
return 1;
}
error = trans_force_read(trans, size - 8);
if (error == 0)
{
/* here, the entire message block is read in, process it */
error = process_pcm_message(id, size - 8, s);
}
return error;
}
/*****************************************************************************/
static int DEFAULT_CC
sound_trans_audio_conn_in(struct trans *trans, struct trans *new_trans)
{
print_got_here();
if (trans == 0)
{
return 1;
}
if (trans != g_audio_l_trans)
{
return 1;
}
if (g_audio_c_trans != 0) /* if already set, error */
{
return 1;
}
if (new_trans == 0)
{
return 1;
}
g_audio_c_trans = new_trans;
g_audio_c_trans->trans_data_in = sound_trans_audio_data_in;
g_audio_c_trans->header_size = 8;
trans_delete(g_audio_l_trans);
g_audio_l_trans = 0;
return 0;
}
/*****************************************************************************/
int APP_CC
sound_init(void)
{
char port[256];
int error;
print_got_here();
LOG(0, ("sound_init:"));
sound_send_server_formats();
g_audio_l_trans = trans_create(2, 33 * 1024, 8192);
g_snprintf(port, 255, "/tmp/xrdp_chansrv_audio_socket_%d", g_display_num);
g_audio_l_trans->trans_conn_in = sound_trans_audio_conn_in;
error = trans_listen(g_audio_l_trans, port);
if (error != 0)
{
LOG(0, ("sound_init: trans_listen failed"));
}
#if defined(XRDP_SIMPLESOUND)
/* start thread to read raw audio data from pulseaudio device */
tc_thread_create(read_raw_audio_data, 0);
#endif
return 0;
}
/*****************************************************************************/
int APP_CC
sound_deinit(void)
{
print_got_here();
if (g_audio_l_trans != 0)
{
trans_delete(g_audio_l_trans);
g_audio_l_trans = 0;
}
if (g_audio_c_trans != 0)
{
trans_delete(g_audio_c_trans);
g_audio_l_trans = 0;
}
return 0;
}
/*****************************************************************************/
/* data in from client ( clinet -> xrdp -> chansrv ) */
int APP_CC
sound_data_in(struct stream* s, int chan_id, int chan_flags, int length,
int total_length)
{
int code;
int size;
print_got_here();
in_uint8(s, code);
in_uint8s(s, 1);
in_uint16_le(s, size);
switch (code)
{
case SNDC_WAVECONFIRM:
sound_process_wave_confirm(s, size);
break;
case SNDC_TRAINING:
sound_process_training(s, size);
break;
case SNDC_FORMATS:
sound_process_formats(s, size);
break;
default:
LOG(0, ("sound_data_in: unknown code %d size %d", code, size));
break;
}
return 0;
}
/*****************************************************************************/
int APP_CC
sound_get_wait_objs(tbus* objs, int* count, int* timeout)
{
int lcount;
lcount = *count;
if (g_audio_l_trans != 0)
{
objs[lcount] = g_audio_l_trans->sck;
lcount++;
}
if (g_audio_c_trans != 0)
{
objs[lcount] = g_audio_c_trans->sck;
lcount++;
}
*count = lcount;
return 0;
}
/*****************************************************************************/
int APP_CC
sound_check_wait_objs(void)
{
if (g_audio_l_trans != 0)
{
trans_check_wait_objs(g_audio_l_trans);
}
if (g_audio_c_trans != 0)
{
trans_check_wait_objs(g_audio_c_trans);
}
return 0;
}
#if defined(XRDP_SIMPLESOUND)
static int DEFAULT_CC
sttrans_data_in(struct trans* self)
{
LOG(0, ("sttrans_data_in:\n"));
return 0;
}
/**
* read raw audio data from pulseaudio device and write it
* to a unix domain socket on which trans server is listening
*/
static void* DEFAULT_CC
read_raw_audio_data(void* arg)
{
pa_sample_spec samp_spec;
pa_simple* simple = NULL;
uint32_t bytes_read;
char* cptr;
int i;
int error;
struct trans* strans;
char path[256];
struct stream* outs;
strans = trans_create(TRANS_MODE_UNIX, 8192, 8192);
if (strans == 0)
{
LOG(0, ("read_raw_audio_data: trans_create failed\n"));
return 0;
}
strans->trans_data_in = sttrans_data_in;
g_snprintf(path, 255, "/tmp/xrdp_chansrv_audio_socket_%d", g_display_num);
if (trans_connect(strans, "", path, 100) != 0)
{
LOG(0, ("read_raw_audio_data: trans_connect failed\n"));
trans_delete(strans);
return 0;
}
/* setup audio format */
samp_spec.format = PA_SAMPLE_S16LE;
samp_spec.rate = 44100;
samp_spec.channels = 2;
/* if we are root, then for first 8 seconds connection to pulseaudo server
fails; if we are non-root, then connection succeeds on first attempt;
for now we have changed code to be non-root, but this may change in the
future - so pretend we are root and try connecting to pulseaudio server
for upto one minute */
for (i = 0; i < 60; i++)
{
simple = pa_simple_new(NULL, "xrdp", PA_STREAM_RECORD, NULL,
"record", &samp_spec, NULL, NULL, &error);
if (simple)
{
/* connected to pulseaudio server */
LOG(0, ("read_raw_audio_data: connected to pulseaudio server\n"));
break;
}
LOG(0, ("read_raw_audio_data: ERROR creating PulseAudio async interface\n"));
LOG(0, ("read_raw_audio_data: %s\n", pa_strerror(error)));
g_sleep(1000);
}
if (i == 60)
{
/* failed to connect to audio server */
trans_delete(strans);
return NULL;
}
/* insert header just once */
outs = trans_get_out_s(strans, 8192);
out_uint32_le(outs, 0);
out_uint32_le(outs, AUDIO_BUF_SIZE + 8);
cptr = outs->p;
out_uint8s(outs, AUDIO_BUF_SIZE);
s_mark_end(outs);
while (1)
{
/* read a block of raw audio data... */
g_memset(cptr, 0, 4);
bytes_read = pa_simple_read(simple, cptr, AUDIO_BUF_SIZE, &error);
if (bytes_read < 0)
{
LOG(0, ("read_raw_audio_data: ERROR reading from pulseaudio stream\n"));
LOG(0, ("read_raw_audio_data: %s\n", pa_strerror(error)));
break;
}
/* bug workaround:
even when there is no audio data, pulseaudio is returning without
errors but the data itself is zero; we use this zero data to
determine that there is no audio data present */
if (*cptr == 0 && *(cptr + 1) == 0 && *(cptr + 2) == 0 && *(cptr + 3) == 0)
{
g_sleep(10);
continue;
}
if (trans_force_write_s(strans, outs) != 0)
{
LOG(0, ("read_raw_audio_data: ERROR writing audio data to server\n"));
break;
}
}
pa_simple_free(simple);
trans_delete(strans);
return NULL;
}
#endif
|