VirtualBox

source: vbox/trunk/src/VBox/RDP/client/rdpsnd.c@ 33656

最後變更 在這個檔案從33656是 33656,由 vboxsync 提交於 14 年 前

*: rebrand Sun (L)GPL disclaimers

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 20.8 KB
 
1/* -*- c-basic-offset: 8 -*-
2 rdesktop: A Remote Desktop Protocol client.
3 Sound Channel Process Functions
4 Copyright 2006-2007 Pierre Ossman <[email protected]> for Cendio AB
5 Copyright (C) Matthew Chapman 2003-2007
6 Copyright (C) GuoJunBo [email protected] 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/*
24 * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
25 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
26 * the General Public License version 2 (GPLv2) at this time for any software where
27 * a choice of GPL license versions is made available with the language indicating
28 * that GPLv2 or any later version may be used, or where a choice of which version
29 * of the GPL is applied is otherwise unspecified.
30 */
31
32#include <assert.h>
33
34#include "rdesktop.h"
35#include "rdpsnd.h"
36#include "rdpsnd_dsp.h"
37
38#define RDPSND_CLOSE 1
39#define RDPSND_WRITE 2
40#define RDPSND_SET_VOLUME 3
41#define RDPSND_UNKNOWN4 4
42#define RDPSND_COMPLETION 5
43#define RDPSND_PING 6
44#define RDPSND_NEGOTIATE 7
45
46#define RDPSND_REC_NEGOTIATE 39
47#define RDPSND_REC_START 40
48#define RDPSND_REC_STOP 41
49#define RDPSND_REC_DATA 42
50#define RDPSND_REC_SET_VOLUME 43
51
52#define RDPSND_FLAG_RECORD 0x00800000
53
54#define MAX_FORMATS 10
55#define MAX_QUEUE 50
56
57static VCHANNEL *rdpsnd_channel;
58static VCHANNEL *rdpsnddbg_channel;
59static struct audio_driver *drivers = NULL;
60struct audio_driver *current_driver = NULL;
61
62static RD_BOOL device_open;
63static RD_BOOL rec_device_open;
64
65static RD_WAVEFORMATEX formats[MAX_FORMATS];
66static unsigned int format_count;
67static unsigned int current_format;
68
69static RD_WAVEFORMATEX rec_formats[MAX_FORMATS];
70static unsigned int rec_format_count;
71static unsigned int rec_current_format;
72
73unsigned int queue_hi, queue_lo, queue_pending;
74struct audio_packet packet_queue[MAX_QUEUE];
75
76static char record_buffer[8192];
77static uint32 record_buffer_size;
78
79static uint8 packet_opcode;
80static struct stream packet;
81
82void (*wave_out_play) (void);
83
84static void rdpsnd_queue_write(STREAM s, uint16 tick, uint8 index);
85static void rdpsnd_queue_init(void);
86static void rdpsnd_queue_complete_pending(void);
87static long rdpsnd_queue_next_completion(void);
88
89static STREAM
90rdpsnd_init_packet(uint16 type, uint16 size)
91{
92 STREAM s;
93
94 s = channel_init(rdpsnd_channel, size + 4);
95 out_uint16_le(s, type);
96 out_uint16_le(s, size);
97 return s;
98}
99
100static void
101rdpsnd_send(STREAM s)
102{
103 channel_send(s, rdpsnd_channel);
104}
105
106static void
107rdpsnd_send_completion(uint16 tick, uint8 packet_index)
108{
109 STREAM s;
110
111 s = rdpsnd_init_packet(RDPSND_COMPLETION, 4);
112 out_uint16_le(s, tick);
113 out_uint8(s, packet_index);
114 out_uint8(s, 0);
115 s_mark_end(s);
116 rdpsnd_send(s);
117
118 DEBUG_SOUND(("RDPSND: -> RDPSND_COMPLETION(tick: %u, index: %u)\n",
119 (unsigned) tick, (unsigned) packet_index));
120}
121
122static void
123rdpsnd_flush_record(void)
124{
125 STREAM s;
126 unsigned int chunk_size;
127 char *data;
128
129 if (record_buffer_size == 0)
130 return;
131
132 assert(record_buffer_size <= sizeof(record_buffer));
133
134 data = record_buffer;
135
136 /*
137 * Microsoft's RDP server keeps dropping chunks, so we need to
138 * transmit everything inside one channel fragment or we risk
139 * making the rdpsnd server go out of sync with the byte stream.
140 */
141 while (record_buffer_size)
142 {
143 if (record_buffer_size < 1596)
144 chunk_size = record_buffer_size;
145 else
146 chunk_size = 1596;
147
148 s = rdpsnd_init_packet(RDPSND_REC_DATA, chunk_size);
149 out_uint8p(s, data, chunk_size);
150
151 s_mark_end(s);
152 rdpsnd_send(s);
153
154 data = data + chunk_size;
155 record_buffer_size -= chunk_size;
156
157 DEBUG_SOUND(("RDPSND: -> RDPSND_REC_DATA(length: %u)\n", (unsigned) chunk_size));
158 }
159
160 record_buffer_size = 0;
161}
162
163void
164rdpsnd_record(const void *data, unsigned int size)
165{
166 uint32 remain, chunk;
167
168 assert(rec_device_open);
169
170 while (size)
171 {
172 remain = sizeof(record_buffer) - record_buffer_size;
173
174 if (size >= remain)
175 chunk = remain;
176 else
177 chunk = size;
178
179 memcpy(record_buffer + record_buffer_size, data, chunk);
180
181#ifdef B_ENDIAN
182 if (current_driver->need_byteswap_on_be)
183 rdpsnd_dsp_swapbytes(record_buffer + record_buffer_size,
184 chunk, &rec_formats[rec_current_format]);
185#endif
186
187 record_buffer_size += chunk;
188
189 data = (const char *) data + chunk;
190 size -= chunk;
191
192 if (record_buffer_size == sizeof(record_buffer))
193 rdpsnd_flush_record();
194 }
195}
196
197static RD_BOOL
198rdpsnd_auto_select(void)
199{
200 static RD_BOOL failed = False;
201
202 if (!failed)
203 {
204 current_driver = drivers;
205 while (current_driver != NULL)
206 {
207 DEBUG(("trying %s...\n", current_driver->name));
208 if (current_driver->wave_out_open())
209 {
210 DEBUG(("selected %s\n", current_driver->name));
211 current_driver->wave_out_close();
212 return True;
213 }
214 current_driver = current_driver->next;
215 }
216
217 warning("no working audio-driver found\n");
218 failed = True;
219 current_driver = NULL;
220 }
221
222 return False;
223}
224
225static void
226rdpsnd_process_negotiate(STREAM in)
227{
228 uint16 in_format_count, i;
229 uint8 pad;
230 uint16 version;
231 RD_WAVEFORMATEX *format;
232 STREAM out;
233 RD_BOOL device_available = False;
234 int readcnt;
235 int discardcnt;
236
237 in_uint8s(in, 14); /* initial bytes not valid from server */
238 in_uint16_le(in, in_format_count);
239 in_uint8(in, pad);
240 in_uint16_le(in, version);
241 in_uint8s(in, 1); /* padding */
242
243 DEBUG_SOUND(("RDPSND: RDPSND_NEGOTIATE(formats: %d, pad: 0x%02x, version: %x)\n",
244 (int) in_format_count, (unsigned) pad, (unsigned) version));
245
246 if (!current_driver)
247 device_available = rdpsnd_auto_select();
248
249 if (current_driver && !device_available && current_driver->wave_out_open())
250 {
251 current_driver->wave_out_close();
252 device_available = True;
253 }
254
255 format_count = 0;
256 if (s_check_rem(in, 18 * in_format_count))
257 {
258 for (i = 0; i < in_format_count; i++)
259 {
260 format = &formats[format_count];
261 in_uint16_le(in, format->wFormatTag);
262 in_uint16_le(in, format->nChannels);
263 in_uint32_le(in, format->nSamplesPerSec);
264 in_uint32_le(in, format->nAvgBytesPerSec);
265 in_uint16_le(in, format->nBlockAlign);
266 in_uint16_le(in, format->wBitsPerSample);
267 in_uint16_le(in, format->cbSize);
268
269 /* read in the buffer of unknown use */
270 readcnt = format->cbSize;
271 discardcnt = 0;
272 if (format->cbSize > MAX_CBSIZE)
273 {
274 fprintf(stderr, "cbSize too large for buffer: %d\n",
275 format->cbSize);
276 readcnt = MAX_CBSIZE;
277 discardcnt = format->cbSize - MAX_CBSIZE;
278 }
279 in_uint8a(in, format->cb, readcnt);
280 in_uint8s(in, discardcnt);
281
282 if (current_driver && current_driver->wave_out_format_supported(format))
283 {
284 format_count++;
285 if (format_count == MAX_FORMATS)
286 break;
287 }
288 }
289 }
290
291 out = rdpsnd_init_packet(RDPSND_NEGOTIATE | 0x200, 20 + 18 * format_count);
292 out_uint32_le(out, 0x00800003); /* flags */
293 out_uint32(out, 0xffffffff); /* volume */
294 out_uint32(out, 0); /* pitch */
295 out_uint16(out, 0); /* UDP port */
296
297 out_uint16_le(out, format_count);
298 out_uint8(out, 0); /* padding */
299 out_uint16_le(out, 2); /* version */
300 out_uint8(out, 0); /* padding */
301
302 for (i = 0; i < format_count; i++)
303 {
304 format = &formats[i];
305 out_uint16_le(out, format->wFormatTag);
306 out_uint16_le(out, format->nChannels);
307 out_uint32_le(out, format->nSamplesPerSec);
308 out_uint32_le(out, format->nAvgBytesPerSec);
309 out_uint16_le(out, format->nBlockAlign);
310 out_uint16_le(out, format->wBitsPerSample);
311 out_uint16(out, 0); /* cbSize */
312 }
313
314 s_mark_end(out);
315
316 DEBUG_SOUND(("RDPSND: -> RDPSND_NEGOTIATE(formats: %d)\n", (int) format_count));
317
318 rdpsnd_send(out);
319}
320
321static void
322rdpsnd_process_ping(STREAM in)
323{
324 uint16 tick;
325 STREAM out;
326
327 in_uint16_le(in, tick);
328
329 DEBUG_SOUND(("RDPSND: RDPSND_PING(tick: 0x%04x)\n", (unsigned) tick));
330
331 out = rdpsnd_init_packet(RDPSND_PING | 0x2300, 4);
332 out_uint16_le(out, tick);
333 out_uint16_le(out, 0);
334 s_mark_end(out);
335 rdpsnd_send(out);
336
337 DEBUG_SOUND(("RDPSND: -> (tick: 0x%04x)\n", (unsigned) tick));
338}
339
340static void
341rdpsnd_process_rec_negotiate(STREAM in)
342{
343 uint16 in_format_count, i;
344 uint16 version;
345 RD_WAVEFORMATEX *format;
346 STREAM out;
347 RD_BOOL device_available = False;
348 int readcnt;
349 int discardcnt;
350
351 in_uint8s(in, 8); /* initial bytes not valid from server */
352 in_uint16_le(in, in_format_count);
353 in_uint16_le(in, version);
354
355 DEBUG_SOUND(("RDPSND: RDPSND_REC_NEGOTIATE(formats: %d, version: %x)\n",
356 (int) in_format_count, (unsigned) version));
357
358 if (!current_driver)
359 device_available = rdpsnd_auto_select();
360
361 if (current_driver && !device_available && current_driver->wave_in_open
362 && current_driver->wave_in_open())
363 {
364 current_driver->wave_in_close();
365 device_available = True;
366 }
367
368 rec_format_count = 0;
369 if (s_check_rem(in, 18 * in_format_count))
370 {
371 for (i = 0; i < in_format_count; i++)
372 {
373 format = &rec_formats[rec_format_count];
374 in_uint16_le(in, format->wFormatTag);
375 in_uint16_le(in, format->nChannels);
376 in_uint32_le(in, format->nSamplesPerSec);
377 in_uint32_le(in, format->nAvgBytesPerSec);
378 in_uint16_le(in, format->nBlockAlign);
379 in_uint16_le(in, format->wBitsPerSample);
380 in_uint16_le(in, format->cbSize);
381
382 /* read in the buffer of unknown use */
383 readcnt = format->cbSize;
384 discardcnt = 0;
385 if (format->cbSize > MAX_CBSIZE)
386 {
387 fprintf(stderr, "cbSize too large for buffer: %d\n",
388 format->cbSize);
389 readcnt = MAX_CBSIZE;
390 discardcnt = format->cbSize - MAX_CBSIZE;
391 }
392 in_uint8a(in, format->cb, readcnt);
393 in_uint8s(in, discardcnt);
394
395 if (current_driver && current_driver->wave_in_format_supported
396 && current_driver->wave_in_format_supported(format))
397 {
398 rec_format_count++;
399 if (rec_format_count == MAX_FORMATS)
400 break;
401 }
402 }
403 }
404
405 out = rdpsnd_init_packet(RDPSND_REC_NEGOTIATE, 12 + 18 * rec_format_count);
406 out_uint32_le(out, 0x00000000); /* flags */
407 out_uint32_le(out, 0xffffffff); /* volume */
408 out_uint16_le(out, rec_format_count);
409 out_uint16_le(out, 1); /* version */
410
411 for (i = 0; i < rec_format_count; i++)
412 {
413 format = &rec_formats[i];
414 out_uint16_le(out, format->wFormatTag);
415 out_uint16_le(out, format->nChannels);
416 out_uint32_le(out, format->nSamplesPerSec);
417 out_uint32_le(out, format->nAvgBytesPerSec);
418 out_uint16_le(out, format->nBlockAlign);
419 out_uint16_le(out, format->wBitsPerSample);
420 out_uint16(out, 0); /* cbSize */
421 }
422
423 s_mark_end(out);
424
425 DEBUG_SOUND(("RDPSND: -> RDPSND_REC_NEGOTIATE(formats: %d)\n", (int) rec_format_count));
426
427 rdpsnd_send(out);
428}
429
430static void
431rdpsnd_process_packet(uint8 opcode, STREAM s)
432{
433 uint16 vol_left, vol_right;
434 static uint16 tick, format;
435 static uint8 packet_index;
436
437 switch (opcode)
438 {
439 case RDPSND_WRITE:
440 in_uint16_le(s, tick);
441 in_uint16_le(s, format);
442 in_uint8(s, packet_index);
443 in_uint8s(s, 3);
444 DEBUG_SOUND(("RDPSND: RDPSND_WRITE(tick: %u, format: %u, index: %u, data: %u bytes)\n", (unsigned) tick, (unsigned) format, (unsigned) packet_index, (unsigned) s->size - 8));
445
446 if (format >= MAX_FORMATS)
447 {
448 error("RDPSND: Invalid format index\n");
449 break;
450 }
451
452 if (!device_open || (format != current_format))
453 {
454 /*
455 * If we haven't selected a device by now, then either
456 * we've failed to find a working device, or the server
457 * is sending bogus RDPSND_WRITE.
458 */
459 if (!current_driver)
460 {
461 rdpsnd_send_completion(tick, packet_index);
462 break;
463 }
464 if (!device_open && !current_driver->wave_out_open())
465 {
466 rdpsnd_send_completion(tick, packet_index);
467 break;
468 }
469 if (!current_driver->wave_out_set_format(&formats[format]))
470 {
471 rdpsnd_send_completion(tick, packet_index);
472 current_driver->wave_out_close();
473 device_open = False;
474 break;
475 }
476 device_open = True;
477 current_format = format;
478 }
479
480 rdpsnd_queue_write(rdpsnd_dsp_process
481 (s->p, s->end - s->p, current_driver,
482 &formats[current_format]), tick, packet_index);
483 return;
484 break;
485 case RDPSND_CLOSE:
486 DEBUG_SOUND(("RDPSND: RDPSND_CLOSE()\n"));
487 if (device_open)
488 current_driver->wave_out_close();
489 device_open = False;
490 break;
491 case RDPSND_NEGOTIATE:
492 rdpsnd_process_negotiate(s);
493 break;
494 case RDPSND_PING:
495 rdpsnd_process_ping(s);
496 break;
497 case RDPSND_SET_VOLUME:
498 in_uint16_le(s, vol_left);
499 in_uint16_le(s, vol_right);
500 DEBUG_SOUND(("RDPSND: RDPSND_VOLUME(left: 0x%04x (%u %%), right: 0x%04x (%u %%))\n", (unsigned) vol_left, (unsigned) vol_left / 655, (unsigned) vol_right, (unsigned) vol_right / 655));
501 if (device_open)
502 current_driver->wave_out_volume(vol_left, vol_right);
503 break;
504 case RDPSND_REC_NEGOTIATE:
505 rdpsnd_process_rec_negotiate(s);
506 break;
507 case RDPSND_REC_START:
508 in_uint16_le(s, format);
509 DEBUG_SOUND(("RDPSND: RDPSND_REC_START(format: %u)\n", (unsigned) format));
510
511 if (format >= MAX_FORMATS)
512 {
513 error("RDPSND: Invalid format index\n");
514 break;
515 }
516
517 if (rec_device_open)
518 {
519 error("RDPSND: Multiple RDPSND_REC_START\n");
520 break;
521 }
522
523 if (!current_driver->wave_in_open())
524 break;
525
526 if (!current_driver->wave_in_set_format(&rec_formats[format]))
527 {
528 error("RDPSND: Device not accepting format\n");
529 current_driver->wave_in_close();
530 break;
531 }
532 rec_current_format = format;
533 rec_device_open = True;
534 break;
535 case RDPSND_REC_STOP:
536 DEBUG_SOUND(("RDPSND: RDPSND_REC_STOP()\n"));
537 rdpsnd_flush_record();
538 if (rec_device_open)
539 current_driver->wave_in_close();
540 rec_device_open = False;
541 break;
542 case RDPSND_REC_SET_VOLUME:
543 in_uint16_le(s, vol_left);
544 in_uint16_le(s, vol_right);
545 DEBUG_SOUND(("RDPSND: RDPSND_REC_VOLUME(left: 0x%04x (%u %%), right: 0x%04x (%u %%))\n", (unsigned) vol_left, (unsigned) vol_left / 655, (unsigned) vol_right, (unsigned) vol_right / 655));
546 if (rec_device_open)
547 current_driver->wave_in_volume(vol_left, vol_right);
548 break;
549 default:
550 unimpl("RDPSND packet type %x\n", opcode);
551 break;
552 }
553}
554
555static void
556rdpsnd_process(STREAM s)
557{
558 uint16 len;
559
560 while (!s_check_end(s))
561 {
562 /* New packet */
563 if (packet.size == 0)
564 {
565 if ((s->end - s->p) < 4)
566 {
567 error("RDPSND: Split at packet header. Things will go south from here...\n");
568 return;
569 }
570 in_uint8(s, packet_opcode);
571 in_uint8s(s, 1); /* Padding */
572 in_uint16_le(s, len);
573
574 DEBUG_SOUND(("RDPSND: == Opcode %x Length: %d ==\n",
575 (int) packet_opcode, (int) len));
576
577 packet.p = packet.data;
578 packet.end = packet.data + len;
579 packet.size = len;
580 }
581 else
582 {
583 len = MIN(s->end - s->p, packet.end - packet.p);
584
585 /* Microsoft's server is so broken it's not even funny... */
586 if (packet_opcode == RDPSND_WRITE)
587 {
588 if ((packet.p - packet.data) < 12)
589 len = MIN(len, 12 - (packet.p - packet.data));
590 else if ((packet.p - packet.data) == 12)
591 {
592 DEBUG_SOUND(("RDPSND: Eating 4 bytes of %d bytes...\n",
593 len));
594 in_uint8s(s, 4);
595 len -= 4;
596 }
597 }
598
599 in_uint8a(s, packet.p, len);
600 packet.p += len;
601 }
602
603 /* Packet fully assembled */
604 if (packet.p == packet.end)
605 {
606 packet.p = packet.data;
607 rdpsnd_process_packet(packet_opcode, &packet);
608 packet.size = 0;
609 }
610 }
611}
612
613static RD_BOOL
614rdpsnddbg_line_handler(const char *line, void *data)
615{
616#ifdef WITH_DEBUG_SOUND
617 fprintf(stderr, "SNDDBG: %s\n", line);
618#endif
619 return True;
620}
621
622static void
623rdpsnddbg_process(STREAM s)
624{
625 unsigned int pkglen;
626 static char *rest = NULL;
627 char *buf;
628
629 pkglen = s->end - s->p;
630 /* str_handle_lines requires null terminated strings */
631 buf = (char *) xmalloc(pkglen + 1);
632 STRNCPY(buf, (char *) s->p, pkglen + 1);
633
634 str_handle_lines(buf, &rest, rdpsnddbg_line_handler, NULL);
635
636 xfree(buf);
637}
638
639static void
640rdpsnd_register_drivers(char *options)
641{
642 struct audio_driver **reg;
643
644 /* The order of registrations define the probe-order
645 when opening the device for the first time */
646 reg = &drivers;
647#if defined(RDPSND_ALSA)
648 *reg = alsa_register(options);
649 assert(*reg);
650 reg = &((*reg)->next);
651#endif
652#if defined(RDPSND_SUN)
653 *reg = sun_register(options);
654 assert(*reg);
655 reg = &((*reg)->next);
656#endif
657#if defined(RDPSND_OSS)
658 *reg = oss_register(options);
659 assert(*reg);
660 reg = &((*reg)->next);
661#endif
662#if defined(RDPSND_SGI)
663 *reg = sgi_register(options);
664 assert(*reg);
665 reg = &((*reg)->next);
666#endif
667#if defined(RDPSND_LIBAO)
668 *reg = libao_register(options);
669 assert(*reg);
670 reg = &((*reg)->next);
671#endif
672 *reg = NULL;
673}
674
675RD_BOOL
676rdpsnd_init(char *optarg)
677{
678 struct audio_driver *pos;
679 char *driver = NULL, *options = NULL;
680
681 drivers = NULL;
682
683 packet.data = (uint8 *) xmalloc(65536);
684 packet.p = packet.end = packet.data;
685 packet.size = 0;
686
687 rdpsnd_channel =
688 channel_register("rdpsnd", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP,
689 rdpsnd_process);
690
691 rdpsnddbg_channel =
692 channel_register("snddbg", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP,
693 rdpsnddbg_process);
694
695 if ((rdpsnd_channel == NULL) || (rdpsnddbg_channel == NULL))
696 {
697 error("channel_register\n");
698 return False;
699 }
700
701 rdpsnd_queue_init();
702
703 if (optarg != NULL && strlen(optarg) > 0)
704 {
705 driver = options = optarg;
706
707 while (*options != '\0' && *options != ':')
708 options++;
709
710 if (*options == ':')
711 {
712 *options = '\0';
713 options++;
714 }
715
716 if (*options == '\0')
717 options = NULL;
718 }
719
720 rdpsnd_register_drivers(options);
721
722 if (!driver)
723 return True;
724
725 pos = drivers;
726 while (pos != NULL)
727 {
728 if (!strcmp(pos->name, driver))
729 {
730 DEBUG(("selected %s\n", pos->name));
731 current_driver = pos;
732 return True;
733 }
734 pos = pos->next;
735 }
736 return False;
737}
738
739void
740rdpsnd_show_help(void)
741{
742 struct audio_driver *pos;
743
744 rdpsnd_register_drivers(NULL);
745
746 pos = drivers;
747 while (pos != NULL)
748 {
749 fprintf(stderr, " %s:\t%s\n", pos->name, pos->description);
750 pos = pos->next;
751 }
752}
753
754void
755rdpsnd_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
756{
757 long next_pending;
758
759 if (device_open || rec_device_open)
760 current_driver->add_fds(n, rfds, wfds, tv);
761
762 next_pending = rdpsnd_queue_next_completion();
763 if (next_pending >= 0)
764 {
765 long cur_timeout;
766
767 cur_timeout = tv->tv_sec * 1000000 + tv->tv_usec;
768 if (cur_timeout > next_pending)
769 {
770 tv->tv_sec = next_pending / 1000000;
771 tv->tv_usec = next_pending % 1000000;
772 }
773 }
774}
775
776void
777rdpsnd_check_fds(fd_set * rfds, fd_set * wfds)
778{
779 rdpsnd_queue_complete_pending();
780
781 if (device_open || rec_device_open)
782 current_driver->check_fds(rfds, wfds);
783}
784
785static void
786rdpsnd_queue_write(STREAM s, uint16 tick, uint8 index)
787{
788 struct audio_packet *packet = &packet_queue[queue_hi];
789 unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE;
790
791 if (next_hi == queue_pending)
792 {
793 error("No space to queue audio packet\n");
794 return;
795 }
796
797 queue_hi = next_hi;
798
799 packet->s = *s;
800 packet->tick = tick;
801 packet->index = index;
802
803 gettimeofday(&packet->arrive_tv, NULL);
804}
805
806struct audio_packet *
807rdpsnd_queue_current_packet(void)
808{
809 return &packet_queue[queue_lo];
810}
811
812RD_BOOL
813rdpsnd_queue_empty(void)
814{
815 return (queue_lo == queue_hi);
816}
817
818static void
819rdpsnd_queue_init(void)
820{
821 queue_pending = queue_lo = queue_hi = 0;
822}
823
824void
825rdpsnd_queue_next(unsigned long completed_in_us)
826{
827 struct audio_packet *packet;
828
829 assert(!rdpsnd_queue_empty());
830
831 packet = &packet_queue[queue_lo];
832
833 gettimeofday(&packet->completion_tv, NULL);
834
835 packet->completion_tv.tv_usec += completed_in_us;
836 packet->completion_tv.tv_sec += packet->completion_tv.tv_usec / 1000000;
837 packet->completion_tv.tv_usec %= 1000000;
838
839 queue_lo = (queue_lo + 1) % MAX_QUEUE;
840
841 rdpsnd_queue_complete_pending();
842}
843
844int
845rdpsnd_queue_next_tick(void)
846{
847 if (((queue_lo + 1) % MAX_QUEUE) != queue_hi)
848 {
849 return packet_queue[(queue_lo + 1) % MAX_QUEUE].tick;
850 }
851 else
852 {
853 return (packet_queue[queue_lo].tick + 65535) % 65536;
854 }
855}
856
857static void
858rdpsnd_queue_complete_pending(void)
859{
860 struct timeval now;
861 long elapsed;
862 struct audio_packet *packet;
863
864 gettimeofday(&now, NULL);
865
866 while (queue_pending != queue_lo)
867 {
868 packet = &packet_queue[queue_pending];
869
870 if (now.tv_sec < packet->completion_tv.tv_sec)
871 break;
872
873 if ((now.tv_sec == packet->completion_tv.tv_sec) &&
874 (now.tv_usec < packet->completion_tv.tv_usec))
875 break;
876
877 elapsed = (packet->completion_tv.tv_sec - packet->arrive_tv.tv_sec) * 1000000 +
878 (packet->completion_tv.tv_usec - packet->arrive_tv.tv_usec);
879 elapsed /= 1000;
880
881 xfree(packet->s.data);
882 rdpsnd_send_completion((packet->tick + elapsed) % 65536, packet->index);
883 queue_pending = (queue_pending + 1) % MAX_QUEUE;
884 }
885}
886
887static long
888rdpsnd_queue_next_completion(void)
889{
890 struct audio_packet *packet;
891 long remaining;
892 struct timeval now;
893
894 if (queue_pending == queue_lo)
895 return -1;
896
897 gettimeofday(&now, NULL);
898
899 packet = &packet_queue[queue_pending];
900
901 remaining = (packet->completion_tv.tv_sec - now.tv_sec) * 1000000 +
902 (packet->completion_tv.tv_usec - now.tv_usec);
903
904 if (remaining < 0)
905 return 0;
906
907 return remaining;
908}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette